2 * Driver core for serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/slab.h>
27 #include <linux/sched/signal.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/device.h>
34 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
35 #include <linux/serial_core.h>
36 #include <linux/delay.h>
37 #include <linux/mutex.h>
39 #include <linux/irq.h>
40 #include <linux/uaccess.h>
43 * This is used to lock changes in serial line configuration.
45 static DEFINE_MUTEX(port_mutex);
48 * lockdep: port->lock is initialized in two places, but we
49 * want only one lock-class:
51 static struct lock_class_key port_lock_key;
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
56 struct ktermios *old_termios);
57 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
58 static void uart_change_pm(struct uart_state *state,
59 enum uart_pm_state pm_state);
61 static void uart_port_shutdown(struct tty_port *port);
63 static int uart_dcd_enabled(struct uart_port *uport)
65 return !!(uport->status & UPSTAT_DCD_ENABLE);
68 static inline struct uart_port *uart_port_ref(struct uart_state *state)
70 if (atomic_add_unless(&state->refcount, 1, 0))
71 return state->uart_port;
75 static inline void uart_port_deref(struct uart_port *uport)
77 if (atomic_dec_and_test(&uport->state->refcount))
78 wake_up(&uport->state->remove_wait);
81 #define uart_port_lock(state, flags) \
83 struct uart_port *__uport = uart_port_ref(state); \
85 spin_lock_irqsave(&__uport->lock, flags); \
89 #define uart_port_unlock(uport, flags) \
91 struct uart_port *__uport = uport; \
93 spin_unlock_irqrestore(&__uport->lock, flags); \
94 uart_port_deref(__uport); \
98 static inline struct uart_port *uart_port_check(struct uart_state *state)
100 lockdep_assert_held(&state->port.mutex);
101 return state->uart_port;
105 * This routine is used by the interrupt handler to schedule processing in
106 * the software interrupt portion of the driver.
108 void uart_write_wakeup(struct uart_port *port)
110 struct uart_state *state = port->state;
112 * This means you called this function _after_ the port was
113 * closed. No cookie for you.
116 tty_port_tty_wakeup(&state->port);
119 static void uart_stop(struct tty_struct *tty)
121 struct uart_state *state = tty->driver_data;
122 struct uart_port *port;
125 port = uart_port_lock(state, flags);
127 port->ops->stop_tx(port);
128 uart_port_unlock(port, flags);
131 static void __uart_start(struct tty_struct *tty)
133 struct uart_state *state = tty->driver_data;
134 struct uart_port *port = state->uart_port;
136 if (port && !uart_tx_stopped(port))
137 port->ops->start_tx(port);
140 static void uart_start(struct tty_struct *tty)
142 struct uart_state *state = tty->driver_data;
143 struct uart_port *port;
146 port = uart_port_lock(state, flags);
148 uart_port_unlock(port, flags);
152 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
157 spin_lock_irqsave(&port->lock, flags);
159 port->mctrl = (old & ~clear) | set;
160 if (old != port->mctrl)
161 port->ops->set_mctrl(port, port->mctrl);
162 spin_unlock_irqrestore(&port->lock, flags);
165 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
166 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
168 static void uart_port_dtr_rts(struct uart_port *uport, int raise)
170 int rs485_on = uport->rs485_config &&
171 (uport->rs485.flags & SER_RS485_ENABLED);
172 int RTS_after_send = !!(uport->rs485.flags & SER_RS485_RTS_AFTER_SEND);
175 if (rs485_on && !RTS_after_send) {
176 uart_set_mctrl(uport, TIOCM_DTR);
177 uart_clear_mctrl(uport, TIOCM_RTS);
179 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
182 unsigned int clear = TIOCM_DTR;
184 clear |= (!rs485_on || !RTS_after_send) ? TIOCM_RTS : 0;
185 uart_clear_mctrl(uport, clear);
190 * Startup the port. This will be called once per open. All calls
191 * will be serialised by the per-port mutex.
193 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
196 struct uart_port *uport = uart_port_check(state);
198 unsigned long flags = 0;
201 if (uport->type == PORT_UNKNOWN)
205 * Make sure the device is in D0 state.
207 uart_change_pm(state, UART_PM_STATE_ON);
210 * Initialise and allocate the transmit and temporary
213 page = get_zeroed_page(GFP_KERNEL);
217 uart_port_lock(state, flags);
218 if (!state->xmit.buf) {
219 state->xmit.buf = (unsigned char *) page;
220 uart_circ_clear(&state->xmit);
221 uart_port_unlock(uport, flags);
223 uart_port_unlock(uport, flags);
225 * Do not free() the page under the port lock, see
231 retval = uport->ops->startup(uport);
233 if (uart_console(uport) && uport->cons->cflag) {
234 tty->termios.c_cflag = uport->cons->cflag;
235 uport->cons->cflag = 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;
288 unsigned long flags = 0;
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;
306 if (!tty || C_HUPCL(tty))
307 uart_port_dtr_rts(uport, 0);
309 uart_port_shutdown(port);
313 * It's possible for shutdown to be called after suspend if we get
314 * a DCD drop (hangup) at just the right time. Clear suspended bit so
315 * we don't try to resume a port that has been shutdown.
317 tty_port_set_suspended(port, 0);
320 * Do not free() the transmit buffer page under the port lock since
321 * this can create various circular locking scenarios. For instance,
322 * console driver may need to allocate/free a debug object, which
323 * can endup in printk() recursion.
325 uart_port_lock(state, flags);
326 xmit_buf = state->xmit.buf;
327 state->xmit.buf = NULL;
328 uart_port_unlock(uport, flags);
331 free_page((unsigned long)xmit_buf);
335 * uart_update_timeout - update per-port FIFO timeout.
336 * @port: uart_port structure describing the port
337 * @cflag: termios cflag value
338 * @baud: speed of the port
340 * Set the port FIFO timeout value. The @cflag value should
341 * reflect the actual hardware settings.
344 uart_update_timeout(struct uart_port *port, unsigned int cflag,
349 /* byte size and parity */
350 switch (cflag & CSIZE) {
371 * The total number of bits to be transmitted in the fifo.
373 bits = bits * port->fifosize;
376 * Figure the timeout to send the above number of bits.
377 * Add .02 seconds of slop
379 port->timeout = (HZ * bits) / baud + HZ/50;
382 EXPORT_SYMBOL(uart_update_timeout);
385 * uart_get_baud_rate - return baud rate for a particular port
386 * @port: uart_port structure describing the port in question.
387 * @termios: desired termios settings.
388 * @old: old termios (or NULL)
389 * @min: minimum acceptable baud rate
390 * @max: maximum acceptable baud rate
392 * Decode the termios structure into a numeric baud rate,
393 * taking account of the magic 38400 baud rate (with spd_*
394 * flags), and mapping the %B0 rate to 9600 baud.
396 * If the new baud rate is invalid, try the old termios setting.
397 * If it's still invalid, we try 9600 baud.
399 * Update the @termios structure to reflect the baud rate
400 * we're actually going to be using. Don't do this for the case
401 * where B0 is requested ("hang up").
404 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
405 struct ktermios *old, unsigned int min, unsigned int max)
409 unsigned int altbaud;
411 upf_t flags = port->flags & UPF_SPD_MASK;
431 for (try = 0; try < 2; try++) {
432 baud = tty_termios_baud_rate(termios);
435 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
438 if (try == 0 && baud == 38400)
442 * Special case: B0 rate.
449 if (baud >= min && baud <= max)
453 * Oops, the quotient was zero. Try again with
454 * the old baud rate if possible.
456 termios->c_cflag &= ~CBAUD;
458 baud = tty_termios_baud_rate(old);
460 tty_termios_encode_baud_rate(termios,
467 * As a last resort, if the range cannot be met then clip to
468 * the nearest chip supported rate.
472 tty_termios_encode_baud_rate(termios,
475 tty_termios_encode_baud_rate(termios,
479 /* Should never happen */
484 EXPORT_SYMBOL(uart_get_baud_rate);
487 * uart_get_divisor - return uart clock divisor
488 * @port: uart_port structure describing the port.
489 * @baud: desired baud rate
491 * Calculate the uart clock divisor for the port.
494 uart_get_divisor(struct uart_port *port, unsigned int baud)
499 * Old custom speed handling.
501 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
502 quot = port->custom_divisor;
504 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
509 EXPORT_SYMBOL(uart_get_divisor);
511 /* Caller holds port mutex */
512 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
513 struct ktermios *old_termios)
515 struct uart_port *uport = uart_port_check(state);
516 struct ktermios *termios;
520 * If we have no tty, termios, or the port does not exist,
521 * then we can't set the parameters for this port.
523 if (!tty || uport->type == PORT_UNKNOWN)
526 termios = &tty->termios;
527 uport->ops->set_termios(uport, termios, old_termios);
530 * Set modem status enables based on termios cflag
532 spin_lock_irq(&uport->lock);
533 if (termios->c_cflag & CRTSCTS)
534 uport->status |= UPSTAT_CTS_ENABLE;
536 uport->status &= ~UPSTAT_CTS_ENABLE;
538 if (termios->c_cflag & CLOCAL)
539 uport->status &= ~UPSTAT_DCD_ENABLE;
541 uport->status |= UPSTAT_DCD_ENABLE;
543 /* reset sw-assisted CTS flow control based on (possibly) new mode */
544 hw_stopped = uport->hw_stopped;
545 uport->hw_stopped = uart_softcts_mode(uport) &&
546 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
547 if (uport->hw_stopped) {
549 uport->ops->stop_tx(uport);
554 spin_unlock_irq(&uport->lock);
557 static int uart_put_char(struct tty_struct *tty, unsigned char c)
559 struct uart_state *state = tty->driver_data;
560 struct uart_port *port;
561 struct circ_buf *circ;
566 port = uart_port_lock(state, flags);
568 uart_port_unlock(port, flags);
572 if (port && uart_circ_chars_free(circ) != 0) {
573 circ->buf[circ->head] = c;
574 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
577 uart_port_unlock(port, flags);
581 static void uart_flush_chars(struct tty_struct *tty)
586 static int uart_write(struct tty_struct *tty,
587 const unsigned char *buf, int count)
589 struct uart_state *state = tty->driver_data;
590 struct uart_port *port;
591 struct circ_buf *circ;
596 * This means you called this function _after_ the port was
597 * closed. No cookie for you.
604 port = uart_port_lock(state, flags);
607 uart_port_unlock(port, flags);
612 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
617 memcpy(circ->buf + circ->head, buf, c);
618 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
625 uart_port_unlock(port, flags);
629 static int uart_write_room(struct tty_struct *tty)
631 struct uart_state *state = tty->driver_data;
632 struct uart_port *port;
636 port = uart_port_lock(state, flags);
637 ret = uart_circ_chars_free(&state->xmit);
638 uart_port_unlock(port, flags);
642 static int uart_chars_in_buffer(struct tty_struct *tty)
644 struct uart_state *state = tty->driver_data;
645 struct uart_port *port;
649 port = uart_port_lock(state, flags);
650 ret = uart_circ_chars_pending(&state->xmit);
651 uart_port_unlock(port, flags);
655 static void uart_flush_buffer(struct tty_struct *tty)
657 struct uart_state *state = tty->driver_data;
658 struct uart_port *port;
662 * This means you called this function _after_ the port was
663 * closed. No cookie for you.
670 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
672 port = uart_port_lock(state, flags);
675 uart_circ_clear(&state->xmit);
676 if (port->ops->flush_buffer)
677 port->ops->flush_buffer(port);
678 uart_port_unlock(port, flags);
679 tty_port_tty_wakeup(&state->port);
683 * This function is used to send a high-priority XON/XOFF character to
686 static void uart_send_xchar(struct tty_struct *tty, char ch)
688 struct uart_state *state = tty->driver_data;
689 struct uart_port *port;
692 port = uart_port_ref(state);
696 if (port->ops->send_xchar)
697 port->ops->send_xchar(port, ch);
699 spin_lock_irqsave(&port->lock, flags);
702 port->ops->start_tx(port);
703 spin_unlock_irqrestore(&port->lock, flags);
705 uart_port_deref(port);
708 static void uart_throttle(struct tty_struct *tty)
710 struct uart_state *state = tty->driver_data;
711 struct uart_port *port;
714 port = uart_port_ref(state);
719 mask |= UPSTAT_AUTOXOFF;
721 mask |= UPSTAT_AUTORTS;
723 if (port->status & mask) {
724 port->ops->throttle(port);
725 mask &= ~port->status;
728 if (mask & UPSTAT_AUTORTS)
729 uart_clear_mctrl(port, TIOCM_RTS);
731 if (mask & UPSTAT_AUTOXOFF)
732 uart_send_xchar(tty, STOP_CHAR(tty));
734 uart_port_deref(port);
737 static void uart_unthrottle(struct tty_struct *tty)
739 struct uart_state *state = tty->driver_data;
740 struct uart_port *port;
743 port = uart_port_ref(state);
748 mask |= UPSTAT_AUTOXOFF;
750 mask |= UPSTAT_AUTORTS;
752 if (port->status & mask) {
753 port->ops->unthrottle(port);
754 mask &= ~port->status;
757 if (mask & UPSTAT_AUTORTS)
758 uart_set_mctrl(port, TIOCM_RTS);
760 if (mask & UPSTAT_AUTOXOFF)
761 uart_send_xchar(tty, START_CHAR(tty));
763 uart_port_deref(port);
766 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
768 struct uart_state *state = container_of(port, struct uart_state, port);
769 struct uart_port *uport;
772 memset(retinfo, 0, sizeof(*retinfo));
775 * Ensure the state we copy is consistent and no hardware changes
778 mutex_lock(&port->mutex);
779 uport = uart_port_check(state);
783 retinfo->type = uport->type;
784 retinfo->line = uport->line;
785 retinfo->port = uport->iobase;
786 if (HIGH_BITS_OFFSET)
787 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
788 retinfo->irq = uport->irq;
789 retinfo->flags = (__force int)uport->flags;
790 retinfo->xmit_fifo_size = uport->fifosize;
791 retinfo->baud_base = uport->uartclk / 16;
792 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
793 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
794 ASYNC_CLOSING_WAIT_NONE :
795 jiffies_to_msecs(port->closing_wait) / 10;
796 retinfo->custom_divisor = uport->custom_divisor;
797 retinfo->hub6 = uport->hub6;
798 retinfo->io_type = uport->iotype;
799 retinfo->iomem_reg_shift = uport->regshift;
800 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
804 mutex_unlock(&port->mutex);
808 static int uart_get_info_user(struct tty_port *port,
809 struct serial_struct __user *retinfo)
811 struct serial_struct tmp;
813 if (uart_get_info(port, &tmp) < 0)
816 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
821 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
822 struct uart_state *state,
823 struct serial_struct *new_info)
825 struct uart_port *uport = uart_port_check(state);
826 unsigned long new_port;
827 unsigned int change_irq, change_port, closing_wait;
828 unsigned int old_custom_divisor, close_delay;
829 upf_t old_flags, new_flags;
835 new_port = new_info->port;
836 if (HIGH_BITS_OFFSET)
837 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
839 new_info->irq = irq_canonicalize(new_info->irq);
840 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
841 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
842 ASYNC_CLOSING_WAIT_NONE :
843 msecs_to_jiffies(new_info->closing_wait * 10);
846 change_irq = !(uport->flags & UPF_FIXED_PORT)
847 && new_info->irq != uport->irq;
850 * Since changing the 'type' of the port changes its resource
851 * allocations, we should treat type changes the same as
854 change_port = !(uport->flags & UPF_FIXED_PORT)
855 && (new_port != uport->iobase ||
856 (unsigned long)new_info->iomem_base != uport->mapbase ||
857 new_info->hub6 != uport->hub6 ||
858 new_info->io_type != uport->iotype ||
859 new_info->iomem_reg_shift != uport->regshift ||
860 new_info->type != uport->type);
862 old_flags = uport->flags;
863 new_flags = (__force upf_t)new_info->flags;
864 old_custom_divisor = uport->custom_divisor;
866 if (!capable(CAP_SYS_ADMIN)) {
868 if (change_irq || change_port ||
869 (new_info->baud_base != uport->uartclk / 16) ||
870 (close_delay != port->close_delay) ||
871 (closing_wait != port->closing_wait) ||
872 (new_info->xmit_fifo_size &&
873 new_info->xmit_fifo_size != uport->fifosize) ||
874 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
876 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
877 (new_flags & UPF_USR_MASK));
878 uport->custom_divisor = new_info->custom_divisor;
883 * Ask the low level driver to verify the settings.
885 if (uport->ops->verify_port)
886 retval = uport->ops->verify_port(uport, new_info);
888 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
889 (new_info->baud_base < 9600))
895 if (change_port || change_irq) {
899 * Make sure that we are the sole user of this port.
901 if (tty_port_users(port) > 1)
905 * We need to shutdown the serial port at the old
906 * port/type/irq combination.
908 uart_shutdown(tty, state);
912 unsigned long old_iobase, old_mapbase;
913 unsigned int old_type, old_iotype, old_hub6, old_shift;
915 old_iobase = uport->iobase;
916 old_mapbase = uport->mapbase;
917 old_type = uport->type;
918 old_hub6 = uport->hub6;
919 old_iotype = uport->iotype;
920 old_shift = uport->regshift;
923 * Free and release old regions
925 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
926 uport->ops->release_port(uport);
928 uport->iobase = new_port;
929 uport->type = new_info->type;
930 uport->hub6 = new_info->hub6;
931 uport->iotype = new_info->io_type;
932 uport->regshift = new_info->iomem_reg_shift;
933 uport->mapbase = (unsigned long)new_info->iomem_base;
936 * Claim and map the new regions
938 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
939 retval = uport->ops->request_port(uport);
941 /* Always success - Jean II */
946 * If we fail to request resources for the
947 * new port, try to restore the old settings.
950 uport->iobase = old_iobase;
951 uport->type = old_type;
952 uport->hub6 = old_hub6;
953 uport->iotype = old_iotype;
954 uport->regshift = old_shift;
955 uport->mapbase = old_mapbase;
957 if (old_type != PORT_UNKNOWN) {
958 retval = uport->ops->request_port(uport);
960 * If we failed to restore the old settings,
964 uport->type = PORT_UNKNOWN;
972 /* Added to return the correct error -Ram Gupta */
978 uport->irq = new_info->irq;
979 if (!(uport->flags & UPF_FIXED_PORT))
980 uport->uartclk = new_info->baud_base * 16;
981 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
982 (new_flags & UPF_CHANGE_MASK);
983 uport->custom_divisor = new_info->custom_divisor;
984 port->close_delay = close_delay;
985 port->closing_wait = closing_wait;
986 if (new_info->xmit_fifo_size)
987 uport->fifosize = new_info->xmit_fifo_size;
988 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
992 if (uport->type == PORT_UNKNOWN)
994 if (tty_port_initialized(port)) {
995 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
996 old_custom_divisor != uport->custom_divisor) {
998 * If they're setting up a custom divisor or speed,
999 * instead of clearing it, then bitch about it.
1001 if (uport->flags & UPF_SPD_MASK) {
1002 dev_notice_ratelimited(uport->dev,
1003 "%s sets custom speed on %s. This is deprecated.\n",
1005 tty_name(port->tty));
1007 uart_change_speed(tty, state, NULL);
1010 retval = uart_startup(tty, state, 1);
1012 tty_port_set_initialized(port, true);
1020 static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
1021 struct serial_struct __user *newinfo)
1023 struct serial_struct new_serial;
1024 struct tty_port *port = &state->port;
1027 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1031 * This semaphore protects port->count. It is also
1032 * very useful to prevent opens. Also, take the
1033 * port configuration semaphore to make sure that a
1034 * module insertion/removal doesn't change anything
1037 mutex_lock(&port->mutex);
1038 retval = uart_set_info(tty, port, state, &new_serial);
1039 mutex_unlock(&port->mutex);
1044 * uart_get_lsr_info - get line status register info
1045 * @tty: tty associated with the UART
1046 * @state: UART being queried
1047 * @value: returned modem value
1049 static int uart_get_lsr_info(struct tty_struct *tty,
1050 struct uart_state *state, unsigned int __user *value)
1052 struct uart_port *uport = uart_port_check(state);
1053 unsigned int result;
1055 result = uport->ops->tx_empty(uport);
1058 * If we're about to load something into the transmit
1059 * register, we'll pretend the transmitter isn't empty to
1060 * avoid a race condition (depending on when the transmit
1061 * interrupt happens).
1063 if (uport->x_char ||
1064 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1065 !uart_tx_stopped(uport)))
1066 result &= ~TIOCSER_TEMT;
1068 return put_user(result, value);
1071 static int uart_tiocmget(struct tty_struct *tty)
1073 struct uart_state *state = tty->driver_data;
1074 struct tty_port *port = &state->port;
1075 struct uart_port *uport;
1078 mutex_lock(&port->mutex);
1079 uport = uart_port_check(state);
1083 if (!tty_io_error(tty)) {
1084 result = uport->mctrl;
1085 spin_lock_irq(&uport->lock);
1086 result |= uport->ops->get_mctrl(uport);
1087 spin_unlock_irq(&uport->lock);
1090 mutex_unlock(&port->mutex);
1095 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1097 struct uart_state *state = tty->driver_data;
1098 struct tty_port *port = &state->port;
1099 struct uart_port *uport;
1102 mutex_lock(&port->mutex);
1103 uport = uart_port_check(state);
1107 if (!tty_io_error(tty)) {
1108 uart_update_mctrl(uport, set, clear);
1112 mutex_unlock(&port->mutex);
1116 static int uart_break_ctl(struct tty_struct *tty, int break_state)
1118 struct uart_state *state = tty->driver_data;
1119 struct tty_port *port = &state->port;
1120 struct uart_port *uport;
1123 mutex_lock(&port->mutex);
1124 uport = uart_port_check(state);
1128 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1129 uport->ops->break_ctl(uport, break_state);
1132 mutex_unlock(&port->mutex);
1136 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1138 struct tty_port *port = &state->port;
1139 struct uart_port *uport;
1142 if (!capable(CAP_SYS_ADMIN))
1146 * Take the per-port semaphore. This prevents count from
1147 * changing, and hence any extra opens of the port while
1148 * we're auto-configuring.
1150 if (mutex_lock_interruptible(&port->mutex))
1151 return -ERESTARTSYS;
1153 uport = uart_port_check(state);
1160 if (tty_port_users(port) == 1) {
1161 uart_shutdown(tty, state);
1164 * If we already have a port type configured,
1165 * we must release its resources.
1167 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1168 uport->ops->release_port(uport);
1170 flags = UART_CONFIG_TYPE;
1171 if (uport->flags & UPF_AUTO_IRQ)
1172 flags |= UART_CONFIG_IRQ;
1175 * This will claim the ports resources if
1178 uport->ops->config_port(uport, flags);
1180 ret = uart_startup(tty, state, 1);
1182 tty_port_set_initialized(port, true);
1187 mutex_unlock(&port->mutex);
1191 static void uart_enable_ms(struct uart_port *uport)
1194 * Force modem status interrupts on
1196 if (uport->ops->enable_ms)
1197 uport->ops->enable_ms(uport);
1201 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1202 * - mask passed in arg for lines of interest
1203 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1204 * Caller should use TIOCGICOUNT to see which one it was
1206 * FIXME: This wants extracting into a common all driver implementation
1207 * of TIOCMWAIT using tty_port.
1209 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1211 struct uart_port *uport;
1212 struct tty_port *port = &state->port;
1213 DECLARE_WAITQUEUE(wait, current);
1214 struct uart_icount cprev, cnow;
1218 * note the counters on entry
1220 uport = uart_port_ref(state);
1223 spin_lock_irq(&uport->lock);
1224 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1225 uart_enable_ms(uport);
1226 spin_unlock_irq(&uport->lock);
1228 add_wait_queue(&port->delta_msr_wait, &wait);
1230 spin_lock_irq(&uport->lock);
1231 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1232 spin_unlock_irq(&uport->lock);
1234 set_current_state(TASK_INTERRUPTIBLE);
1236 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1237 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1238 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1239 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1246 /* see if a signal did it */
1247 if (signal_pending(current)) {
1254 __set_current_state(TASK_RUNNING);
1255 remove_wait_queue(&port->delta_msr_wait, &wait);
1256 uart_port_deref(uport);
1262 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1263 * Return: write counters to the user passed counter struct
1264 * NB: both 1->0 and 0->1 transitions are counted except for
1265 * RI where only 0->1 is counted.
1267 static int uart_get_icount(struct tty_struct *tty,
1268 struct serial_icounter_struct *icount)
1270 struct uart_state *state = tty->driver_data;
1271 struct uart_icount cnow;
1272 struct uart_port *uport;
1274 uport = uart_port_ref(state);
1277 spin_lock_irq(&uport->lock);
1278 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1279 spin_unlock_irq(&uport->lock);
1280 uart_port_deref(uport);
1282 icount->cts = cnow.cts;
1283 icount->dsr = cnow.dsr;
1284 icount->rng = cnow.rng;
1285 icount->dcd = cnow.dcd;
1286 icount->rx = cnow.rx;
1287 icount->tx = cnow.tx;
1288 icount->frame = cnow.frame;
1289 icount->overrun = cnow.overrun;
1290 icount->parity = cnow.parity;
1291 icount->brk = cnow.brk;
1292 icount->buf_overrun = cnow.buf_overrun;
1297 static int uart_get_rs485_config(struct uart_port *port,
1298 struct serial_rs485 __user *rs485)
1300 unsigned long flags;
1301 struct serial_rs485 aux;
1303 spin_lock_irqsave(&port->lock, flags);
1305 spin_unlock_irqrestore(&port->lock, flags);
1307 if (copy_to_user(rs485, &aux, sizeof(aux)))
1313 static int uart_set_rs485_config(struct uart_port *port,
1314 struct serial_rs485 __user *rs485_user)
1316 struct serial_rs485 rs485;
1318 unsigned long flags;
1320 if (!port->rs485_config)
1321 return -ENOIOCTLCMD;
1323 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1326 spin_lock_irqsave(&port->lock, flags);
1327 ret = port->rs485_config(port, &rs485);
1328 spin_unlock_irqrestore(&port->lock, flags);
1332 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1339 * Called via sys_ioctl. We can use spin_lock_irq() here.
1342 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1344 struct uart_state *state = tty->driver_data;
1345 struct tty_port *port = &state->port;
1346 struct uart_port *uport;
1347 void __user *uarg = (void __user *)arg;
1348 int ret = -ENOIOCTLCMD;
1352 * These ioctls don't rely on the hardware to be present.
1356 ret = uart_get_info_user(port, uarg);
1360 down_write(&tty->termios_rwsem);
1361 ret = uart_set_info_user(tty, state, uarg);
1362 up_write(&tty->termios_rwsem);
1366 down_write(&tty->termios_rwsem);
1367 ret = uart_do_autoconfig(tty, state);
1368 up_write(&tty->termios_rwsem);
1371 case TIOCSERGWILD: /* obsolete */
1372 case TIOCSERSWILD: /* obsolete */
1377 if (ret != -ENOIOCTLCMD)
1380 if (tty_io_error(tty)) {
1386 * The following should only be used when hardware is present.
1390 ret = uart_wait_modem_status(state, arg);
1394 if (ret != -ENOIOCTLCMD)
1397 mutex_lock(&port->mutex);
1398 uport = uart_port_check(state);
1400 if (!uport || tty_io_error(tty)) {
1406 * All these rely on hardware being present and need to be
1407 * protected against the tty being hung up.
1411 case TIOCSERGETLSR: /* Get line status register */
1412 ret = uart_get_lsr_info(tty, state, uarg);
1416 ret = uart_get_rs485_config(uport, uarg);
1420 ret = uart_set_rs485_config(uport, uarg);
1423 if (uport->ops->ioctl)
1424 ret = uport->ops->ioctl(uport, cmd, arg);
1428 mutex_unlock(&port->mutex);
1433 static void uart_set_ldisc(struct tty_struct *tty)
1435 struct uart_state *state = tty->driver_data;
1436 struct uart_port *uport;
1437 struct tty_port *port = &state->port;
1439 if (!tty_port_initialized(port))
1442 mutex_lock(&state->port.mutex);
1443 uport = uart_port_check(state);
1444 if (uport && uport->ops->set_ldisc)
1445 uport->ops->set_ldisc(uport, &tty->termios);
1446 mutex_unlock(&state->port.mutex);
1449 static void uart_set_termios(struct tty_struct *tty,
1450 struct ktermios *old_termios)
1452 struct uart_state *state = tty->driver_data;
1453 struct uart_port *uport;
1454 unsigned int cflag = tty->termios.c_cflag;
1455 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1456 bool sw_changed = false;
1458 mutex_lock(&state->port.mutex);
1459 uport = uart_port_check(state);
1464 * Drivers doing software flow control also need to know
1465 * about changes to these input settings.
1467 if (uport->flags & UPF_SOFT_FLOW) {
1468 iflag_mask |= IXANY|IXON|IXOFF;
1470 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1471 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1475 * These are the bits that are used to setup various
1476 * flags in the low level driver. We can ignore the Bfoo
1477 * bits in c_cflag; c_[io]speed will always be set
1478 * appropriately by set_termios() in tty_ioctl.c
1480 if ((cflag ^ old_termios->c_cflag) == 0 &&
1481 tty->termios.c_ospeed == old_termios->c_ospeed &&
1482 tty->termios.c_ispeed == old_termios->c_ispeed &&
1483 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1488 uart_change_speed(tty, state, old_termios);
1489 /* reload cflag from termios; port driver may have overriden flags */
1490 cflag = tty->termios.c_cflag;
1492 /* Handle transition to B0 status */
1493 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1494 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1495 /* Handle transition away from B0 status */
1496 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1497 unsigned int mask = TIOCM_DTR;
1498 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1500 uart_set_mctrl(uport, mask);
1503 mutex_unlock(&state->port.mutex);
1507 * Calls to uart_close() are serialised via the tty_lock in
1508 * drivers/tty/tty_io.c:tty_release()
1509 * drivers/tty/tty_io.c:do_tty_hangup()
1511 static void uart_close(struct tty_struct *tty, struct file *filp)
1513 struct uart_state *state = tty->driver_data;
1514 struct tty_port *port;
1517 struct uart_driver *drv = tty->driver->driver_state;
1519 state = drv->state + tty->index;
1520 port = &state->port;
1521 spin_lock_irq(&port->lock);
1523 spin_unlock_irq(&port->lock);
1527 port = &state->port;
1528 pr_debug("uart_close(%d) called\n", tty->index);
1530 tty_port_close(tty->port, tty, filp);
1533 static void uart_tty_port_shutdown(struct tty_port *port)
1535 struct uart_state *state = container_of(port, struct uart_state, port);
1536 struct uart_port *uport = uart_port_check(state);
1539 * At this point, we stop accepting input. To do this, we
1540 * disable the receive line status interrupts.
1542 if (WARN(!uport, "detached port still initialized!\n"))
1545 spin_lock_irq(&uport->lock);
1546 uport->ops->stop_rx(uport);
1547 spin_unlock_irq(&uport->lock);
1549 uart_port_shutdown(port);
1552 * It's possible for shutdown to be called after suspend if we get
1553 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1554 * we don't try to resume a port that has been shutdown.
1556 tty_port_set_suspended(port, 0);
1558 uart_change_pm(state, UART_PM_STATE_OFF);
1562 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1564 struct uart_state *state = tty->driver_data;
1565 struct uart_port *port;
1566 unsigned long char_time, expire;
1568 port = uart_port_ref(state);
1572 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
1573 uart_port_deref(port);
1578 * Set the check interval to be 1/5 of the estimated time to
1579 * send a single character, and make it at least 1. The check
1580 * interval should also be less than the timeout.
1582 * Note: we have to use pretty tight timings here to satisfy
1585 char_time = (port->timeout - HZ/50) / port->fifosize;
1586 char_time = char_time / 5;
1589 if (timeout && timeout < char_time)
1590 char_time = timeout;
1593 * If the transmitter hasn't cleared in twice the approximate
1594 * amount of time to send the entire FIFO, it probably won't
1595 * ever clear. This assumes the UART isn't doing flow
1596 * control, which is currently the case. Hence, if it ever
1597 * takes longer than port->timeout, this is probably due to a
1598 * UART bug of some kind. So, we clamp the timeout parameter at
1601 if (timeout == 0 || timeout > 2 * port->timeout)
1602 timeout = 2 * port->timeout;
1604 expire = jiffies + timeout;
1606 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1607 port->line, jiffies, expire);
1610 * Check whether the transmitter is empty every 'char_time'.
1611 * 'timeout' / 'expire' give us the maximum amount of time
1614 while (!port->ops->tx_empty(port)) {
1615 msleep_interruptible(jiffies_to_msecs(char_time));
1616 if (signal_pending(current))
1618 if (time_after(jiffies, expire))
1621 uart_port_deref(port);
1625 * Calls to uart_hangup() are serialised by the tty_lock in
1626 * drivers/tty/tty_io.c:do_tty_hangup()
1627 * This runs from a workqueue and can sleep for a _short_ time only.
1629 static void uart_hangup(struct tty_struct *tty)
1631 struct uart_state *state = tty->driver_data;
1632 struct tty_port *port = &state->port;
1633 struct uart_port *uport;
1634 unsigned long flags;
1636 pr_debug("uart_hangup(%d)\n", tty->index);
1638 mutex_lock(&port->mutex);
1639 uport = uart_port_check(state);
1640 WARN(!uport, "hangup of detached port!\n");
1642 if (tty_port_active(port)) {
1643 uart_flush_buffer(tty);
1644 uart_shutdown(tty, state);
1645 spin_lock_irqsave(&port->lock, flags);
1647 spin_unlock_irqrestore(&port->lock, flags);
1648 tty_port_set_active(port, 0);
1649 tty_port_tty_set(port, NULL);
1650 if (uport && !uart_console(uport))
1651 uart_change_pm(state, UART_PM_STATE_OFF);
1652 wake_up_interruptible(&port->open_wait);
1653 wake_up_interruptible(&port->delta_msr_wait);
1655 mutex_unlock(&port->mutex);
1658 /* uport == NULL if uart_port has already been removed */
1659 static void uart_port_shutdown(struct tty_port *port)
1661 struct uart_state *state = container_of(port, struct uart_state, port);
1662 struct uart_port *uport = uart_port_check(state);
1665 * clear delta_msr_wait queue to avoid mem leaks: we may free
1666 * the irq here so the queue might never be woken up. Note
1667 * that we won't end up waiting on delta_msr_wait again since
1668 * any outstanding file descriptors should be pointing at
1669 * hung_up_tty_fops now.
1671 wake_up_interruptible(&port->delta_msr_wait);
1674 * Free the IRQ and disable the port.
1677 uport->ops->shutdown(uport);
1680 * Ensure that the IRQ handler isn't running on another CPU.
1683 synchronize_irq(uport->irq);
1686 static int uart_carrier_raised(struct tty_port *port)
1688 struct uart_state *state = container_of(port, struct uart_state, port);
1689 struct uart_port *uport;
1692 uport = uart_port_ref(state);
1694 * Should never observe uport == NULL since checks for hangup should
1695 * abort the tty_port_block_til_ready() loop before checking for carrier
1696 * raised -- but report carrier raised if it does anyway so open will
1697 * continue and not sleep
1699 if (WARN_ON(!uport))
1701 spin_lock_irq(&uport->lock);
1702 uart_enable_ms(uport);
1703 mctrl = uport->ops->get_mctrl(uport);
1704 spin_unlock_irq(&uport->lock);
1705 uart_port_deref(uport);
1706 if (mctrl & TIOCM_CAR)
1711 static void uart_dtr_rts(struct tty_port *port, int raise)
1713 struct uart_state *state = container_of(port, struct uart_state, port);
1714 struct uart_port *uport;
1716 uport = uart_port_ref(state);
1719 uart_port_dtr_rts(uport, raise);
1720 uart_port_deref(uport);
1724 * Calls to uart_open are serialised by the tty_lock in
1725 * drivers/tty/tty_io.c:tty_open()
1726 * Note that if this fails, then uart_close() _will_ be called.
1728 * In time, we want to scrap the "opening nonpresent ports"
1729 * behaviour and implement an alternative way for setserial
1730 * to set base addresses/ports/types. This will allow us to
1731 * get rid of a certain amount of extra tests.
1733 static int uart_open(struct tty_struct *tty, struct file *filp)
1735 struct uart_state *state = tty->driver_data;
1738 retval = tty_port_open(&state->port, tty, filp);
1745 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1747 struct uart_state *state = container_of(port, struct uart_state, port);
1748 struct uart_port *uport;
1751 uport = uart_port_check(state);
1752 if (!uport || uport->flags & UPF_DEAD)
1755 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1758 * Start up the serial port.
1760 ret = uart_startup(tty, state, 0);
1762 tty_port_set_active(port, 1);
1767 static const char *uart_type(struct uart_port *port)
1769 const char *str = NULL;
1771 if (port->ops->type)
1772 str = port->ops->type(port);
1780 #ifdef CONFIG_PROC_FS
1782 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1784 struct uart_state *state = drv->state + i;
1785 struct tty_port *port = &state->port;
1786 enum uart_pm_state pm_state;
1787 struct uart_port *uport;
1789 unsigned int status;
1792 mutex_lock(&port->mutex);
1793 uport = uart_port_check(state);
1797 mmio = uport->iotype >= UPIO_MEM;
1798 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1799 uport->line, uart_type(uport),
1800 mmio ? "mmio:0x" : "port:",
1801 mmio ? (unsigned long long)uport->mapbase
1802 : (unsigned long long)uport->iobase,
1805 if (uport->type == PORT_UNKNOWN) {
1810 if (capable(CAP_SYS_ADMIN)) {
1811 pm_state = state->pm_state;
1812 if (pm_state != UART_PM_STATE_ON)
1813 uart_change_pm(state, UART_PM_STATE_ON);
1814 spin_lock_irq(&uport->lock);
1815 status = uport->ops->get_mctrl(uport);
1816 spin_unlock_irq(&uport->lock);
1817 if (pm_state != UART_PM_STATE_ON)
1818 uart_change_pm(state, pm_state);
1820 seq_printf(m, " tx:%d rx:%d",
1821 uport->icount.tx, uport->icount.rx);
1822 if (uport->icount.frame)
1823 seq_printf(m, " fe:%d", uport->icount.frame);
1824 if (uport->icount.parity)
1825 seq_printf(m, " pe:%d", uport->icount.parity);
1826 if (uport->icount.brk)
1827 seq_printf(m, " brk:%d", uport->icount.brk);
1828 if (uport->icount.overrun)
1829 seq_printf(m, " oe:%d", uport->icount.overrun);
1831 #define INFOBIT(bit, str) \
1832 if (uport->mctrl & (bit)) \
1833 strncat(stat_buf, (str), sizeof(stat_buf) - \
1834 strlen(stat_buf) - 2)
1835 #define STATBIT(bit, str) \
1836 if (status & (bit)) \
1837 strncat(stat_buf, (str), sizeof(stat_buf) - \
1838 strlen(stat_buf) - 2)
1842 INFOBIT(TIOCM_RTS, "|RTS");
1843 STATBIT(TIOCM_CTS, "|CTS");
1844 INFOBIT(TIOCM_DTR, "|DTR");
1845 STATBIT(TIOCM_DSR, "|DSR");
1846 STATBIT(TIOCM_CAR, "|CD");
1847 STATBIT(TIOCM_RNG, "|RI");
1851 seq_puts(m, stat_buf);
1857 mutex_unlock(&port->mutex);
1860 static int uart_proc_show(struct seq_file *m, void *v)
1862 struct tty_driver *ttydrv = m->private;
1863 struct uart_driver *drv = ttydrv->driver_state;
1866 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1867 for (i = 0; i < drv->nr; i++)
1868 uart_line_info(m, drv, i);
1872 static int uart_proc_open(struct inode *inode, struct file *file)
1874 return single_open(file, uart_proc_show, PDE_DATA(inode));
1877 static const struct file_operations uart_proc_fops = {
1878 .owner = THIS_MODULE,
1879 .open = uart_proc_open,
1881 .llseek = seq_lseek,
1882 .release = single_release,
1886 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1888 * uart_console_write - write a console message to a serial port
1889 * @port: the port to write the message
1890 * @s: array of characters
1891 * @count: number of characters in string to write
1892 * @putchar: function to write character to port
1894 void uart_console_write(struct uart_port *port, const char *s,
1896 void (*putchar)(struct uart_port *, int))
1900 for (i = 0; i < count; i++, s++) {
1902 putchar(port, '\r');
1906 EXPORT_SYMBOL_GPL(uart_console_write);
1909 * Check whether an invalid uart number has been specified, and
1910 * if so, search for the first available port that does have
1913 struct uart_port * __init
1914 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1916 int idx = co->index;
1918 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1919 ports[idx].membase == NULL))
1920 for (idx = 0; idx < nr; idx++)
1921 if (ports[idx].iobase != 0 ||
1922 ports[idx].membase != NULL)
1931 * uart_parse_earlycon - Parse earlycon options
1932 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1933 * @iotype: ptr for decoded iotype (out)
1934 * @addr: ptr for decoded mapbase/iobase (out)
1935 * @options: ptr for <options> field; NULL if not present (out)
1937 * Decodes earlycon kernel command line parameters of the form
1938 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1939 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1942 * earlycon=<name>,0x<addr>,<options>
1943 * console=<name>,0x<addr>,<options>
1944 * is also accepted; the returned @iotype will be UPIO_MEM.
1946 * Returns 0 on success or -EINVAL on failure
1948 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
1951 if (strncmp(p, "mmio,", 5) == 0) {
1954 } else if (strncmp(p, "mmio16,", 7) == 0) {
1955 *iotype = UPIO_MEM16;
1957 } else if (strncmp(p, "mmio32,", 7) == 0) {
1958 *iotype = UPIO_MEM32;
1960 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1961 *iotype = UPIO_MEM32BE;
1963 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1964 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1965 UPIO_MEM32BE : UPIO_MEM32;
1967 } else if (strncmp(p, "io,", 3) == 0) {
1968 *iotype = UPIO_PORT;
1970 } else if (strncmp(p, "0x", 2) == 0) {
1977 * Before you replace it with kstrtoull(), think about options separator
1978 * (',') it will not tolerate
1980 *addr = simple_strtoull(p, NULL, 0);
1988 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1991 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1992 * @options: pointer to option string
1993 * @baud: pointer to an 'int' variable for the baud rate.
1994 * @parity: pointer to an 'int' variable for the parity.
1995 * @bits: pointer to an 'int' variable for the number of data bits.
1996 * @flow: pointer to an 'int' variable for the flow control character.
1998 * uart_parse_options decodes a string containing the serial console
1999 * options. The format of the string is <baud><parity><bits><flow>,
2003 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
2007 *baud = simple_strtoul(s, NULL, 10);
2008 while (*s >= '0' && *s <= '9')
2017 EXPORT_SYMBOL_GPL(uart_parse_options);
2020 * uart_set_options - setup the serial console parameters
2021 * @port: pointer to the serial ports uart_port structure
2022 * @co: console pointer
2024 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2025 * @bits: number of data bits
2026 * @flow: flow control character - 'r' (rts)
2029 uart_set_options(struct uart_port *port, struct console *co,
2030 int baud, int parity, int bits, int flow)
2032 struct ktermios termios;
2033 static struct ktermios dummy;
2036 * Ensure that the serial console lock is initialised
2038 * If this port is a console, then the spinlock is already
2041 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2042 spin_lock_init(&port->lock);
2043 lockdep_set_class(&port->lock, &port_lock_key);
2046 memset(&termios, 0, sizeof(struct ktermios));
2048 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2049 tty_termios_encode_baud_rate(&termios, baud, baud);
2052 termios.c_cflag |= CS7;
2054 termios.c_cflag |= CS8;
2058 termios.c_cflag |= PARODD;
2061 termios.c_cflag |= PARENB;
2066 termios.c_cflag |= CRTSCTS;
2069 * some uarts on other side don't support no flow control.
2070 * So we set * DTR in host uart to make them happy
2072 port->mctrl |= TIOCM_DTR;
2074 port->ops->set_termios(port, &termios, &dummy);
2076 * Allow the setting of the UART parameters with a NULL console
2080 co->cflag = termios.c_cflag;
2084 EXPORT_SYMBOL_GPL(uart_set_options);
2085 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2088 * uart_change_pm - set power state of the port
2090 * @state: port descriptor
2091 * @pm_state: new state
2093 * Locking: port->mutex has to be held
2095 static void uart_change_pm(struct uart_state *state,
2096 enum uart_pm_state pm_state)
2098 struct uart_port *port = uart_port_check(state);
2100 if (state->pm_state != pm_state) {
2101 if (port && port->ops->pm)
2102 port->ops->pm(port, pm_state, state->pm_state);
2103 state->pm_state = pm_state;
2108 struct uart_port *port;
2109 struct uart_driver *driver;
2112 static int serial_match_port(struct device *dev, void *data)
2114 struct uart_match *match = data;
2115 struct tty_driver *tty_drv = match->driver->tty_driver;
2116 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2119 return dev->devt == devt; /* Actually, only one tty per port */
2122 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2124 struct uart_state *state = drv->state + uport->line;
2125 struct tty_port *port = &state->port;
2126 struct device *tty_dev;
2127 struct uart_match match = {uport, drv};
2129 mutex_lock(&port->mutex);
2131 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2132 if (tty_dev && device_may_wakeup(tty_dev)) {
2133 enable_irq_wake(uport->irq);
2134 put_device(tty_dev);
2135 mutex_unlock(&port->mutex);
2138 put_device(tty_dev);
2140 /* Nothing to do if the console is not suspending */
2141 if (!console_suspend_enabled && uart_console(uport))
2144 uport->suspended = 1;
2146 if (tty_port_initialized(port)) {
2147 const struct uart_ops *ops = uport->ops;
2150 tty_port_set_suspended(port, 1);
2151 tty_port_set_initialized(port, 0);
2153 spin_lock_irq(&uport->lock);
2154 ops->stop_tx(uport);
2155 ops->set_mctrl(uport, 0);
2156 ops->stop_rx(uport);
2157 spin_unlock_irq(&uport->lock);
2160 * Wait for the transmitter to empty.
2162 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2165 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2168 ops->shutdown(uport);
2172 * Disable the console device before suspending.
2174 if (uart_console(uport))
2175 console_stop(uport->cons);
2177 uart_change_pm(state, UART_PM_STATE_OFF);
2179 mutex_unlock(&port->mutex);
2184 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2186 struct uart_state *state = drv->state + uport->line;
2187 struct tty_port *port = &state->port;
2188 struct device *tty_dev;
2189 struct uart_match match = {uport, drv};
2190 struct ktermios termios;
2192 mutex_lock(&port->mutex);
2194 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2195 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2196 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2197 disable_irq_wake(uport->irq);
2198 put_device(tty_dev);
2199 mutex_unlock(&port->mutex);
2202 put_device(tty_dev);
2203 uport->suspended = 0;
2206 * Re-enable the console device after suspending.
2208 if (uart_console(uport)) {
2210 * First try to use the console cflag setting.
2212 memset(&termios, 0, sizeof(struct ktermios));
2213 termios.c_cflag = uport->cons->cflag;
2216 * If that's unset, use the tty termios setting.
2218 if (port->tty && termios.c_cflag == 0)
2219 termios = port->tty->termios;
2221 if (console_suspend_enabled)
2222 uart_change_pm(state, UART_PM_STATE_ON);
2223 uport->ops->set_termios(uport, &termios, NULL);
2224 if (console_suspend_enabled)
2225 console_start(uport->cons);
2228 if (tty_port_suspended(port)) {
2229 const struct uart_ops *ops = uport->ops;
2232 uart_change_pm(state, UART_PM_STATE_ON);
2233 spin_lock_irq(&uport->lock);
2234 ops->set_mctrl(uport, 0);
2235 spin_unlock_irq(&uport->lock);
2236 if (console_suspend_enabled || !uart_console(uport)) {
2237 /* Protected by port mutex for now */
2238 struct tty_struct *tty = port->tty;
2239 ret = ops->startup(uport);
2242 uart_change_speed(tty, state, NULL);
2243 spin_lock_irq(&uport->lock);
2244 ops->set_mctrl(uport, uport->mctrl);
2245 ops->start_tx(uport);
2246 spin_unlock_irq(&uport->lock);
2247 tty_port_set_initialized(port, 1);
2250 * Failed to resume - maybe hardware went away?
2251 * Clear the "initialized" flag so we won't try
2252 * to call the low level drivers shutdown method.
2254 uart_shutdown(tty, state);
2258 tty_port_set_suspended(port, 0);
2261 mutex_unlock(&port->mutex);
2267 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2271 switch (port->iotype) {
2273 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2276 snprintf(address, sizeof(address),
2277 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2285 snprintf(address, sizeof(address),
2286 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2289 strlcpy(address, "*unknown*", sizeof(address));
2293 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2294 port->dev ? dev_name(port->dev) : "",
2295 port->dev ? ": " : "",
2297 address, port->irq, port->uartclk / 16, uart_type(port));
2301 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2302 struct uart_port *port)
2307 * If there isn't a port here, don't do anything further.
2309 if (!port->iobase && !port->mapbase && !port->membase)
2313 * Now do the auto configuration stuff. Note that config_port
2314 * is expected to claim the resources and map the port for us.
2317 if (port->flags & UPF_AUTO_IRQ)
2318 flags |= UART_CONFIG_IRQ;
2319 if (port->flags & UPF_BOOT_AUTOCONF) {
2320 if (!(port->flags & UPF_FIXED_TYPE)) {
2321 port->type = PORT_UNKNOWN;
2322 flags |= UART_CONFIG_TYPE;
2324 port->ops->config_port(port, flags);
2327 if (port->type != PORT_UNKNOWN) {
2328 unsigned long flags;
2330 uart_report_port(drv, port);
2332 /* Power up port for set_mctrl() */
2333 uart_change_pm(state, UART_PM_STATE_ON);
2336 * Ensure that the modem control lines are de-activated.
2337 * keep the DTR setting that is set in uart_set_options()
2338 * We probably don't need a spinlock around this, but
2340 spin_lock_irqsave(&port->lock, flags);
2341 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2342 spin_unlock_irqrestore(&port->lock, flags);
2345 * If this driver supports console, and it hasn't been
2346 * successfully registered yet, try to re-register it.
2347 * It may be that the port was not available.
2349 if (port->cons && !(port->cons->flags & CON_ENABLED))
2350 register_console(port->cons);
2353 * Power down all ports by default, except the
2354 * console if we have one.
2356 if (!uart_console(port))
2357 uart_change_pm(state, UART_PM_STATE_OFF);
2361 #ifdef CONFIG_CONSOLE_POLL
2363 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2365 struct uart_driver *drv = driver->driver_state;
2366 struct uart_state *state = drv->state + line;
2367 struct tty_port *tport;
2368 struct uart_port *port;
2375 tport = &state->port;
2376 mutex_lock(&tport->mutex);
2378 port = uart_port_check(state);
2379 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2384 if (port->ops->poll_init) {
2386 * We don't set initialized as we only initialized the hw,
2387 * e.g. state->xmit is still uninitialized.
2389 if (!tty_port_initialized(tport))
2390 ret = port->ops->poll_init(port);
2393 if (!ret && options) {
2394 uart_parse_options(options, &baud, &parity, &bits, &flow);
2395 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2398 mutex_unlock(&tport->mutex);
2402 static int uart_poll_get_char(struct tty_driver *driver, int line)
2404 struct uart_driver *drv = driver->driver_state;
2405 struct uart_state *state = drv->state + line;
2406 struct uart_port *port;
2409 port = uart_port_ref(state);
2411 ret = port->ops->poll_get_char(port);
2412 uart_port_deref(port);
2418 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2420 struct uart_driver *drv = driver->driver_state;
2421 struct uart_state *state = drv->state + line;
2422 struct uart_port *port;
2424 port = uart_port_ref(state);
2429 port->ops->poll_put_char(port, '\r');
2430 port->ops->poll_put_char(port, ch);
2431 uart_port_deref(port);
2435 static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
2437 struct uart_driver *drv = driver->driver_state;
2438 struct uart_state *state = drv->state + tty->index;
2440 tty->driver_data = state;
2442 return tty_standard_install(driver, tty);
2445 static const struct tty_operations uart_ops = {
2446 .install = uart_install,
2448 .close = uart_close,
2449 .write = uart_write,
2450 .put_char = uart_put_char,
2451 .flush_chars = uart_flush_chars,
2452 .write_room = uart_write_room,
2453 .chars_in_buffer= uart_chars_in_buffer,
2454 .flush_buffer = uart_flush_buffer,
2455 .ioctl = uart_ioctl,
2456 .throttle = uart_throttle,
2457 .unthrottle = uart_unthrottle,
2458 .send_xchar = uart_send_xchar,
2459 .set_termios = uart_set_termios,
2460 .set_ldisc = uart_set_ldisc,
2462 .start = uart_start,
2463 .hangup = uart_hangup,
2464 .break_ctl = uart_break_ctl,
2465 .wait_until_sent= uart_wait_until_sent,
2466 #ifdef CONFIG_PROC_FS
2467 .proc_fops = &uart_proc_fops,
2469 .tiocmget = uart_tiocmget,
2470 .tiocmset = uart_tiocmset,
2471 .get_icount = uart_get_icount,
2472 #ifdef CONFIG_CONSOLE_POLL
2473 .poll_init = uart_poll_init,
2474 .poll_get_char = uart_poll_get_char,
2475 .poll_put_char = uart_poll_put_char,
2479 static const struct tty_port_operations uart_port_ops = {
2480 .carrier_raised = uart_carrier_raised,
2481 .dtr_rts = uart_dtr_rts,
2482 .activate = uart_port_activate,
2483 .shutdown = uart_tty_port_shutdown,
2487 * uart_register_driver - register a driver with the uart core layer
2488 * @drv: low level driver structure
2490 * Register a uart driver with the core driver. We in turn register
2491 * with the tty layer, and initialise the core driver per-port state.
2493 * We have a proc file in /proc/tty/driver which is named after the
2496 * drv->port should be NULL, and the per-port structures should be
2497 * registered using uart_add_one_port after this call has succeeded.
2499 int uart_register_driver(struct uart_driver *drv)
2501 struct tty_driver *normal;
2507 * Maybe we should be using a slab cache for this, especially if
2508 * we have a large number of ports to handle.
2510 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2514 normal = alloc_tty_driver(drv->nr);
2518 drv->tty_driver = normal;
2520 normal->driver_name = drv->driver_name;
2521 normal->name = drv->dev_name;
2522 normal->major = drv->major;
2523 normal->minor_start = drv->minor;
2524 normal->type = TTY_DRIVER_TYPE_SERIAL;
2525 normal->subtype = SERIAL_TYPE_NORMAL;
2526 normal->init_termios = tty_std_termios;
2527 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2528 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2529 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2530 normal->driver_state = drv;
2531 tty_set_operations(normal, &uart_ops);
2534 * Initialise the UART state(s).
2536 for (i = 0; i < drv->nr; i++) {
2537 struct uart_state *state = drv->state + i;
2538 struct tty_port *port = &state->port;
2540 tty_port_init(port);
2541 port->ops = &uart_port_ops;
2544 retval = tty_register_driver(normal);
2548 for (i = 0; i < drv->nr; i++)
2549 tty_port_destroy(&drv->state[i].port);
2550 put_tty_driver(normal);
2558 * uart_unregister_driver - remove a driver from the uart core layer
2559 * @drv: low level driver structure
2561 * Remove all references to a driver from the core driver. The low
2562 * level driver must have removed all its ports via the
2563 * uart_remove_one_port() if it registered them with uart_add_one_port().
2564 * (ie, drv->port == NULL)
2566 void uart_unregister_driver(struct uart_driver *drv)
2568 struct tty_driver *p = drv->tty_driver;
2571 tty_unregister_driver(p);
2573 for (i = 0; i < drv->nr; i++)
2574 tty_port_destroy(&drv->state[i].port);
2577 drv->tty_driver = NULL;
2580 struct tty_driver *uart_console_device(struct console *co, int *index)
2582 struct uart_driver *p = co->data;
2584 return p->tty_driver;
2587 static ssize_t uart_get_attr_uartclk(struct device *dev,
2588 struct device_attribute *attr, char *buf)
2590 struct serial_struct tmp;
2591 struct tty_port *port = dev_get_drvdata(dev);
2593 uart_get_info(port, &tmp);
2594 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2597 static ssize_t uart_get_attr_type(struct device *dev,
2598 struct device_attribute *attr, char *buf)
2600 struct serial_struct tmp;
2601 struct tty_port *port = dev_get_drvdata(dev);
2603 uart_get_info(port, &tmp);
2604 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2606 static ssize_t uart_get_attr_line(struct device *dev,
2607 struct device_attribute *attr, char *buf)
2609 struct serial_struct tmp;
2610 struct tty_port *port = dev_get_drvdata(dev);
2612 uart_get_info(port, &tmp);
2613 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2616 static ssize_t uart_get_attr_port(struct device *dev,
2617 struct device_attribute *attr, char *buf)
2619 struct serial_struct tmp;
2620 struct tty_port *port = dev_get_drvdata(dev);
2621 unsigned long ioaddr;
2623 uart_get_info(port, &tmp);
2625 if (HIGH_BITS_OFFSET)
2626 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2627 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2630 static ssize_t uart_get_attr_irq(struct device *dev,
2631 struct device_attribute *attr, char *buf)
2633 struct serial_struct tmp;
2634 struct tty_port *port = dev_get_drvdata(dev);
2636 uart_get_info(port, &tmp);
2637 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2640 static ssize_t uart_get_attr_flags(struct device *dev,
2641 struct device_attribute *attr, char *buf)
2643 struct serial_struct tmp;
2644 struct tty_port *port = dev_get_drvdata(dev);
2646 uart_get_info(port, &tmp);
2647 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2650 static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2651 struct device_attribute *attr, char *buf)
2653 struct serial_struct tmp;
2654 struct tty_port *port = dev_get_drvdata(dev);
2656 uart_get_info(port, &tmp);
2657 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2661 static ssize_t uart_get_attr_close_delay(struct device *dev,
2662 struct device_attribute *attr, char *buf)
2664 struct serial_struct tmp;
2665 struct tty_port *port = dev_get_drvdata(dev);
2667 uart_get_info(port, &tmp);
2668 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2672 static ssize_t uart_get_attr_closing_wait(struct device *dev,
2673 struct device_attribute *attr, char *buf)
2675 struct serial_struct tmp;
2676 struct tty_port *port = dev_get_drvdata(dev);
2678 uart_get_info(port, &tmp);
2679 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2682 static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2683 struct device_attribute *attr, char *buf)
2685 struct serial_struct tmp;
2686 struct tty_port *port = dev_get_drvdata(dev);
2688 uart_get_info(port, &tmp);
2689 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2692 static ssize_t uart_get_attr_io_type(struct device *dev,
2693 struct device_attribute *attr, char *buf)
2695 struct serial_struct tmp;
2696 struct tty_port *port = dev_get_drvdata(dev);
2698 uart_get_info(port, &tmp);
2699 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2702 static ssize_t uart_get_attr_iomem_base(struct device *dev,
2703 struct device_attribute *attr, char *buf)
2705 struct serial_struct tmp;
2706 struct tty_port *port = dev_get_drvdata(dev);
2708 uart_get_info(port, &tmp);
2709 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2712 static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2713 struct device_attribute *attr, char *buf)
2715 struct serial_struct tmp;
2716 struct tty_port *port = dev_get_drvdata(dev);
2718 uart_get_info(port, &tmp);
2719 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2722 static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2723 static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2724 static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2725 static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2726 static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2727 static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2728 static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2729 static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2730 static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2731 static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2732 static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2733 static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2734 static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2736 static struct attribute *tty_dev_attrs[] = {
2737 &dev_attr_type.attr,
2738 &dev_attr_line.attr,
2739 &dev_attr_port.attr,
2741 &dev_attr_flags.attr,
2742 &dev_attr_xmit_fifo_size.attr,
2743 &dev_attr_uartclk.attr,
2744 &dev_attr_close_delay.attr,
2745 &dev_attr_closing_wait.attr,
2746 &dev_attr_custom_divisor.attr,
2747 &dev_attr_io_type.attr,
2748 &dev_attr_iomem_base.attr,
2749 &dev_attr_iomem_reg_shift.attr,
2753 static const struct attribute_group tty_dev_attr_group = {
2754 .attrs = tty_dev_attrs,
2758 * uart_add_one_port - attach a driver-defined port structure
2759 * @drv: pointer to the uart low level driver structure for this port
2760 * @uport: uart port structure to use for this port.
2762 * This allows the driver to register its own uart_port structure
2763 * with the core driver. The main purpose is to allow the low
2764 * level uart drivers to expand uart_port, rather than having yet
2765 * more levels of structures.
2767 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2769 struct uart_state *state;
2770 struct tty_port *port;
2772 struct device *tty_dev;
2775 BUG_ON(in_interrupt());
2777 if (uport->line >= drv->nr)
2780 state = drv->state + uport->line;
2781 port = &state->port;
2783 mutex_lock(&port_mutex);
2784 mutex_lock(&port->mutex);
2785 if (state->uart_port) {
2790 /* Link the port to the driver state table and vice versa */
2791 atomic_set(&state->refcount, 1);
2792 init_waitqueue_head(&state->remove_wait);
2793 state->uart_port = uport;
2794 uport->state = state;
2796 state->pm_state = UART_PM_STATE_UNDEFINED;
2797 uport->cons = drv->cons;
2798 uport->minor = drv->tty_driver->minor_start + uport->line;
2799 uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
2800 drv->tty_driver->name_base + uport->line);
2807 * If this port is a console, then the spinlock is already
2810 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2811 spin_lock_init(&uport->lock);
2812 lockdep_set_class(&uport->lock, &port_lock_key);
2814 if (uport->cons && uport->dev)
2815 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2817 tty_port_link_device(port, drv->tty_driver, uport->line);
2818 uart_configure_port(drv, state, uport);
2820 port->console = uart_console(uport);
2823 if (uport->attr_group)
2826 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2828 if (!uport->tty_groups) {
2832 uport->tty_groups[0] = &tty_dev_attr_group;
2833 if (uport->attr_group)
2834 uport->tty_groups[1] = uport->attr_group;
2837 * Register the port whether it's detected or not. This allows
2838 * setserial to be used to alter this port's parameters.
2840 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
2841 uport->line, uport->dev, port, uport->tty_groups);
2842 if (likely(!IS_ERR(tty_dev))) {
2843 device_set_wakeup_capable(tty_dev, 1);
2845 dev_err(uport->dev, "Cannot register tty device on line %d\n",
2850 * Ensure UPF_DEAD is not set.
2852 uport->flags &= ~UPF_DEAD;
2855 mutex_unlock(&port->mutex);
2856 mutex_unlock(&port_mutex);
2862 * uart_remove_one_port - detach a driver defined port structure
2863 * @drv: pointer to the uart low level driver structure for this port
2864 * @uport: uart port structure for this port
2866 * This unhooks (and hangs up) the specified port structure from the
2867 * core driver. No further calls will be made to the low-level code
2870 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2872 struct uart_state *state = drv->state + uport->line;
2873 struct tty_port *port = &state->port;
2874 struct uart_port *uart_port;
2875 struct tty_struct *tty;
2878 BUG_ON(in_interrupt());
2880 mutex_lock(&port_mutex);
2883 * Mark the port "dead" - this prevents any opens from
2884 * succeeding while we shut down the port.
2886 mutex_lock(&port->mutex);
2887 uart_port = uart_port_check(state);
2888 if (uart_port != uport)
2889 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2893 mutex_unlock(&port->mutex);
2897 uport->flags |= UPF_DEAD;
2898 mutex_unlock(&port->mutex);
2901 * Remove the devices from the tty layer
2903 tty_port_unregister_device(port, drv->tty_driver, uport->line);
2905 tty = tty_port_tty_get(port);
2907 tty_vhangup(port->tty);
2912 * If the port is used as a console, unregister it
2914 if (uart_console(uport))
2915 unregister_console(uport->cons);
2918 * Free the port IO and memory resources, if any.
2920 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
2921 uport->ops->release_port(uport);
2922 kfree(uport->tty_groups);
2926 * Indicate that there isn't a port here anymore.
2928 uport->type = PORT_UNKNOWN;
2930 mutex_lock(&port->mutex);
2931 WARN_ON(atomic_dec_return(&state->refcount) < 0);
2932 wait_event(state->remove_wait, !atomic_read(&state->refcount));
2933 state->uart_port = NULL;
2934 mutex_unlock(&port->mutex);
2936 mutex_unlock(&port_mutex);
2942 * Are the two ports equivalent?
2944 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2946 if (port1->iotype != port2->iotype)
2949 switch (port1->iotype) {
2951 return (port1->iobase == port2->iobase);
2953 return (port1->iobase == port2->iobase) &&
2954 (port1->hub6 == port2->hub6);
2961 return (port1->mapbase == port2->mapbase);
2965 EXPORT_SYMBOL(uart_match_port);
2968 * uart_handle_dcd_change - handle a change of carrier detect state
2969 * @uport: uart_port structure for the open port
2970 * @status: new carrier detect status, nonzero if active
2972 * Caller must hold uport->lock
2974 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2976 struct tty_port *port = &uport->state->port;
2977 struct tty_struct *tty = port->tty;
2978 struct tty_ldisc *ld;
2980 lockdep_assert_held_once(&uport->lock);
2983 ld = tty_ldisc_ref(tty);
2985 if (ld->ops->dcd_change)
2986 ld->ops->dcd_change(tty, status);
2987 tty_ldisc_deref(ld);
2991 uport->icount.dcd++;
2993 if (uart_dcd_enabled(uport)) {
2995 wake_up_interruptible(&port->open_wait);
3000 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3003 * uart_handle_cts_change - handle a change of clear-to-send state
3004 * @uport: uart_port structure for the open port
3005 * @status: new clear to send status, nonzero if active
3007 * Caller must hold uport->lock
3009 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3011 lockdep_assert_held_once(&uport->lock);
3013 uport->icount.cts++;
3015 if (uart_softcts_mode(uport)) {
3016 if (uport->hw_stopped) {
3018 uport->hw_stopped = 0;
3019 uport->ops->start_tx(uport);
3020 uart_write_wakeup(uport);
3024 uport->hw_stopped = 1;
3025 uport->ops->stop_tx(uport);
3031 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3034 * uart_insert_char - push a char to the uart layer
3036 * User is responsible to call tty_flip_buffer_push when they are done with
3039 * @port: corresponding port
3040 * @status: state of the serial port RX buffer (LSR for 8250)
3041 * @overrun: mask of overrun bits in @status
3042 * @ch: character to push
3043 * @flag: flag for the character (see TTY_NORMAL and friends)
3045 void uart_insert_char(struct uart_port *port, unsigned int status,
3046 unsigned int overrun, unsigned int ch, unsigned int flag)
3048 struct tty_port *tport = &port->state->port;
3050 if ((status & port->ignore_status_mask & ~overrun) == 0)
3051 if (tty_insert_flip_char(tport, ch, flag) == 0)
3052 ++port->icount.buf_overrun;
3055 * Overrun is special. Since it's reported immediately,
3056 * it doesn't affect the current character.
3058 if (status & ~port->ignore_status_mask & overrun)
3059 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3060 ++port->icount.buf_overrun;
3062 EXPORT_SYMBOL_GPL(uart_insert_char);
3064 EXPORT_SYMBOL(uart_write_wakeup);
3065 EXPORT_SYMBOL(uart_register_driver);
3066 EXPORT_SYMBOL(uart_unregister_driver);
3067 EXPORT_SYMBOL(uart_suspend_port);
3068 EXPORT_SYMBOL(uart_resume_port);
3069 EXPORT_SYMBOL(uart_add_one_port);
3070 EXPORT_SYMBOL(uart_remove_one_port);
3072 MODULE_DESCRIPTION("Serial driver core");
3073 MODULE_LICENSE("GPL");