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/init.h>
28 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/serial_core.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
42 * This is used to lock changes in serial line configuration.
44 static DEFINE_MUTEX(port_mutex);
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
50 static struct lock_class_key port_lock_key;
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
55 struct ktermios *old_termios);
56 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
57 static void uart_change_pm(struct uart_state *state,
58 enum uart_pm_state pm_state);
60 static void uart_port_shutdown(struct tty_port *port);
62 static int uart_dcd_enabled(struct uart_port *uport)
64 return !!(uport->status & UPSTAT_DCD_ENABLE);
67 static inline struct uart_port *uart_port_ref(struct uart_state *state)
69 if (atomic_add_unless(&state->refcount, 1, 0))
70 return state->uart_port;
74 static inline void uart_port_deref(struct uart_port *uport)
76 if (uport && atomic_dec_and_test(&uport->state->refcount))
77 wake_up(&uport->state->remove_wait);
80 #define uart_port_lock(state, flags) \
82 struct uart_port *__uport = uart_port_ref(state); \
84 spin_lock_irqsave(&__uport->lock, flags); \
88 #define uart_port_unlock(uport, flags) \
90 struct uart_port *__uport = uport; \
92 spin_unlock_irqrestore(&__uport->lock, flags); \
93 uart_port_deref(__uport); \
96 static inline struct uart_port *uart_port_check(struct uart_state *state)
98 lockdep_assert_held(&state->port.mutex);
99 return state->uart_port;
103 * This routine is used by the interrupt handler to schedule processing in
104 * the software interrupt portion of the driver.
106 void uart_write_wakeup(struct uart_port *port)
108 struct uart_state *state = port->state;
110 * This means you called this function _after_ the port was
111 * closed. No cookie for you.
114 tty_port_tty_wakeup(&state->port);
117 static void uart_stop(struct tty_struct *tty)
119 struct uart_state *state = tty->driver_data;
120 struct uart_port *port;
123 port = uart_port_lock(state, flags);
125 port->ops->stop_tx(port);
126 uart_port_unlock(port, flags);
129 static void __uart_start(struct tty_struct *tty)
131 struct uart_state *state = tty->driver_data;
132 struct uart_port *port = state->uart_port;
134 if (port && !uart_tx_stopped(port))
135 port->ops->start_tx(port);
138 static void uart_start(struct tty_struct *tty)
140 struct uart_state *state = tty->driver_data;
141 struct uart_port *port;
144 port = uart_port_lock(state, flags);
146 uart_port_unlock(port, flags);
150 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
155 spin_lock_irqsave(&port->lock, flags);
157 port->mctrl = (old & ~clear) | set;
158 if (old != port->mctrl)
159 port->ops->set_mctrl(port, port->mctrl);
160 spin_unlock_irqrestore(&port->lock, flags);
163 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
164 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
167 * Startup the port. This will be called once per open. All calls
168 * will be serialised by the per-port mutex.
170 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
173 struct uart_port *uport = uart_port_check(state);
177 if (uport->type == PORT_UNKNOWN)
181 * Make sure the device is in D0 state.
183 uart_change_pm(state, UART_PM_STATE_ON);
186 * Initialise and allocate the transmit and temporary
189 if (!state->xmit.buf) {
190 /* This is protected by the per port mutex */
191 page = get_zeroed_page(GFP_KERNEL);
195 state->xmit.buf = (unsigned char *) page;
196 uart_circ_clear(&state->xmit);
199 retval = uport->ops->startup(uport);
201 if (uart_console(uport) && uport->cons->cflag) {
202 tty->termios.c_cflag = uport->cons->cflag;
203 uport->cons->cflag = 0;
206 * Initialise the hardware port settings.
208 uart_change_speed(tty, state, NULL);
211 * Setup the RTS and DTR signals once the
212 * port is open and ready to respond.
214 if (init_hw && C_BAUD(tty))
215 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
219 * This is to allow setserial on this port. People may want to set
220 * port/irq/type and then reconfigure the port properly if it failed
223 if (retval && capable(CAP_SYS_ADMIN))
229 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
232 struct tty_port *port = &state->port;
235 if (tty_port_initialized(port))
238 retval = uart_port_startup(tty, state, init_hw);
240 set_bit(TTY_IO_ERROR, &tty->flags);
246 * This routine will shutdown a serial port; interrupts are disabled, and
247 * DTR is dropped if the hangup on close termio flag is on. Calls to
248 * uart_shutdown are serialised by the per-port semaphore.
250 * uport == NULL if uart_port has already been removed
252 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
254 struct uart_port *uport = uart_port_check(state);
255 struct tty_port *port = &state->port;
258 * Set the TTY IO error marker
261 set_bit(TTY_IO_ERROR, &tty->flags);
263 if (tty_port_initialized(port)) {
264 tty_port_set_initialized(port, 0);
267 * Turn off DTR and RTS early.
269 if (uport && uart_console(uport) && tty)
270 uport->cons->cflag = tty->termios.c_cflag;
272 if (!tty || C_HUPCL(tty))
273 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
275 uart_port_shutdown(port);
279 * It's possible for shutdown to be called after suspend if we get
280 * a DCD drop (hangup) at just the right time. Clear suspended bit so
281 * we don't try to resume a port that has been shutdown.
283 tty_port_set_suspended(port, 0);
286 * Free the transmit buffer page.
288 if (state->xmit.buf) {
289 free_page((unsigned long)state->xmit.buf);
290 state->xmit.buf = NULL;
295 * uart_update_timeout - update per-port FIFO timeout.
296 * @port: uart_port structure describing the port
297 * @cflag: termios cflag value
298 * @baud: speed of the port
300 * Set the port FIFO timeout value. The @cflag value should
301 * reflect the actual hardware settings.
304 uart_update_timeout(struct uart_port *port, unsigned int cflag,
309 /* byte size and parity */
310 switch (cflag & CSIZE) {
331 * The total number of bits to be transmitted in the fifo.
333 bits = bits * port->fifosize;
336 * Figure the timeout to send the above number of bits.
337 * Add .02 seconds of slop
339 port->timeout = (HZ * bits) / baud + HZ/50;
342 EXPORT_SYMBOL(uart_update_timeout);
345 * uart_get_baud_rate - return baud rate for a particular port
346 * @port: uart_port structure describing the port in question.
347 * @termios: desired termios settings.
348 * @old: old termios (or NULL)
349 * @min: minimum acceptable baud rate
350 * @max: maximum acceptable baud rate
352 * Decode the termios structure into a numeric baud rate,
353 * taking account of the magic 38400 baud rate (with spd_*
354 * flags), and mapping the %B0 rate to 9600 baud.
356 * If the new baud rate is invalid, try the old termios setting.
357 * If it's still invalid, we try 9600 baud.
359 * Update the @termios structure to reflect the baud rate
360 * we're actually going to be using. Don't do this for the case
361 * where B0 is requested ("hang up").
364 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
365 struct ktermios *old, unsigned int min, unsigned int max)
369 unsigned int altbaud;
371 upf_t flags = port->flags & UPF_SPD_MASK;
391 for (try = 0; try < 2; try++) {
392 baud = tty_termios_baud_rate(termios);
395 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
398 if (try == 0 && baud == 38400)
402 * Special case: B0 rate.
409 if (baud >= min && baud <= max)
413 * Oops, the quotient was zero. Try again with
414 * the old baud rate if possible.
416 termios->c_cflag &= ~CBAUD;
418 baud = tty_termios_baud_rate(old);
420 tty_termios_encode_baud_rate(termios,
427 * As a last resort, if the range cannot be met then clip to
428 * the nearest chip supported rate.
432 tty_termios_encode_baud_rate(termios,
435 tty_termios_encode_baud_rate(termios,
439 /* Should never happen */
444 EXPORT_SYMBOL(uart_get_baud_rate);
447 * uart_get_divisor - return uart clock divisor
448 * @port: uart_port structure describing the port.
449 * @baud: desired baud rate
451 * Calculate the uart clock divisor for the port.
454 uart_get_divisor(struct uart_port *port, unsigned int baud)
459 * Old custom speed handling.
461 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
462 quot = port->custom_divisor;
464 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
469 EXPORT_SYMBOL(uart_get_divisor);
471 /* Caller holds port mutex */
472 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
473 struct ktermios *old_termios)
475 struct uart_port *uport = uart_port_check(state);
476 struct ktermios *termios;
480 * If we have no tty, termios, or the port does not exist,
481 * then we can't set the parameters for this port.
483 if (!tty || uport->type == PORT_UNKNOWN)
486 termios = &tty->termios;
487 uport->ops->set_termios(uport, termios, old_termios);
490 * Set modem status enables based on termios cflag
492 spin_lock_irq(&uport->lock);
493 if (termios->c_cflag & CRTSCTS)
494 uport->status |= UPSTAT_CTS_ENABLE;
496 uport->status &= ~UPSTAT_CTS_ENABLE;
498 if (termios->c_cflag & CLOCAL)
499 uport->status &= ~UPSTAT_DCD_ENABLE;
501 uport->status |= UPSTAT_DCD_ENABLE;
503 /* reset sw-assisted CTS flow control based on (possibly) new mode */
504 hw_stopped = uport->hw_stopped;
505 uport->hw_stopped = uart_softcts_mode(uport) &&
506 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
507 if (uport->hw_stopped) {
509 uport->ops->stop_tx(uport);
514 spin_unlock_irq(&uport->lock);
517 static int uart_put_char(struct tty_struct *tty, unsigned char c)
519 struct uart_state *state = tty->driver_data;
520 struct uart_port *port;
521 struct circ_buf *circ;
529 port = uart_port_lock(state, flags);
530 if (port && uart_circ_chars_free(circ) != 0) {
531 circ->buf[circ->head] = c;
532 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
535 uart_port_unlock(port, flags);
539 static void uart_flush_chars(struct tty_struct *tty)
544 static int uart_write(struct tty_struct *tty,
545 const unsigned char *buf, int count)
547 struct uart_state *state = tty->driver_data;
548 struct uart_port *port;
549 struct circ_buf *circ;
554 * This means you called this function _after_ the port was
555 * closed. No cookie for you.
566 port = uart_port_lock(state, flags);
568 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
573 memcpy(circ->buf + circ->head, buf, c);
574 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
581 uart_port_unlock(port, flags);
585 static int uart_write_room(struct tty_struct *tty)
587 struct uart_state *state = tty->driver_data;
588 struct uart_port *port;
592 port = uart_port_lock(state, flags);
593 ret = uart_circ_chars_free(&state->xmit);
594 uart_port_unlock(port, flags);
598 static int uart_chars_in_buffer(struct tty_struct *tty)
600 struct uart_state *state = tty->driver_data;
601 struct uart_port *port;
605 port = uart_port_lock(state, flags);
606 ret = uart_circ_chars_pending(&state->xmit);
607 uart_port_unlock(port, flags);
611 static void uart_flush_buffer(struct tty_struct *tty)
613 struct uart_state *state = tty->driver_data;
614 struct uart_port *port;
618 * This means you called this function _after_ the port was
619 * closed. No cookie for you.
626 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
628 port = uart_port_lock(state, flags);
631 uart_circ_clear(&state->xmit);
632 if (port->ops->flush_buffer)
633 port->ops->flush_buffer(port);
634 uart_port_unlock(port, flags);
635 tty_port_tty_wakeup(&state->port);
639 * This function is used to send a high-priority XON/XOFF character to
642 static void uart_send_xchar(struct tty_struct *tty, char ch)
644 struct uart_state *state = tty->driver_data;
645 struct uart_port *port;
648 port = uart_port_ref(state);
652 if (port->ops->send_xchar)
653 port->ops->send_xchar(port, ch);
655 spin_lock_irqsave(&port->lock, flags);
658 port->ops->start_tx(port);
659 spin_unlock_irqrestore(&port->lock, flags);
661 uart_port_deref(port);
664 static void uart_throttle(struct tty_struct *tty)
666 struct uart_state *state = tty->driver_data;
667 struct uart_port *port;
670 port = uart_port_ref(state);
675 mask |= UPSTAT_AUTOXOFF;
677 mask |= UPSTAT_AUTORTS;
679 if (port->status & mask) {
680 port->ops->throttle(port);
681 mask &= ~port->status;
684 if (mask & UPSTAT_AUTORTS)
685 uart_clear_mctrl(port, TIOCM_RTS);
687 if (mask & UPSTAT_AUTOXOFF)
688 uart_send_xchar(tty, STOP_CHAR(tty));
690 uart_port_deref(port);
693 static void uart_unthrottle(struct tty_struct *tty)
695 struct uart_state *state = tty->driver_data;
696 struct uart_port *port;
699 port = uart_port_ref(state);
704 mask |= UPSTAT_AUTOXOFF;
706 mask |= UPSTAT_AUTORTS;
708 if (port->status & mask) {
709 port->ops->unthrottle(port);
710 mask &= ~port->status;
713 if (mask & UPSTAT_AUTORTS)
714 uart_set_mctrl(port, TIOCM_RTS);
716 if (mask & UPSTAT_AUTOXOFF)
717 uart_send_xchar(tty, START_CHAR(tty));
719 uart_port_deref(port);
722 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
724 struct uart_state *state = container_of(port, struct uart_state, port);
725 struct uart_port *uport;
728 memset(retinfo, 0, sizeof(*retinfo));
731 * Ensure the state we copy is consistent and no hardware changes
734 mutex_lock(&port->mutex);
735 uport = uart_port_check(state);
739 retinfo->type = uport->type;
740 retinfo->line = uport->line;
741 retinfo->port = uport->iobase;
742 if (HIGH_BITS_OFFSET)
743 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
744 retinfo->irq = uport->irq;
745 retinfo->flags = uport->flags;
746 retinfo->xmit_fifo_size = uport->fifosize;
747 retinfo->baud_base = uport->uartclk / 16;
748 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
749 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
750 ASYNC_CLOSING_WAIT_NONE :
751 jiffies_to_msecs(port->closing_wait) / 10;
752 retinfo->custom_divisor = uport->custom_divisor;
753 retinfo->hub6 = uport->hub6;
754 retinfo->io_type = uport->iotype;
755 retinfo->iomem_reg_shift = uport->regshift;
756 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
760 mutex_unlock(&port->mutex);
764 static int uart_get_info_user(struct tty_port *port,
765 struct serial_struct __user *retinfo)
767 struct serial_struct tmp;
769 if (uart_get_info(port, &tmp) < 0)
772 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
777 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
778 struct uart_state *state,
779 struct serial_struct *new_info)
781 struct uart_port *uport = uart_port_check(state);
782 unsigned long new_port;
783 unsigned int change_irq, change_port, closing_wait;
784 unsigned int old_custom_divisor, close_delay;
785 upf_t old_flags, new_flags;
791 new_port = new_info->port;
792 if (HIGH_BITS_OFFSET)
793 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
795 new_info->irq = irq_canonicalize(new_info->irq);
796 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
797 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
798 ASYNC_CLOSING_WAIT_NONE :
799 msecs_to_jiffies(new_info->closing_wait * 10);
802 change_irq = !(uport->flags & UPF_FIXED_PORT)
803 && new_info->irq != uport->irq;
806 * Since changing the 'type' of the port changes its resource
807 * allocations, we should treat type changes the same as
810 change_port = !(uport->flags & UPF_FIXED_PORT)
811 && (new_port != uport->iobase ||
812 (unsigned long)new_info->iomem_base != uport->mapbase ||
813 new_info->hub6 != uport->hub6 ||
814 new_info->io_type != uport->iotype ||
815 new_info->iomem_reg_shift != uport->regshift ||
816 new_info->type != uport->type);
818 old_flags = uport->flags;
819 new_flags = new_info->flags;
820 old_custom_divisor = uport->custom_divisor;
822 if (!capable(CAP_SYS_ADMIN)) {
824 if (change_irq || change_port ||
825 (new_info->baud_base != uport->uartclk / 16) ||
826 (close_delay != port->close_delay) ||
827 (closing_wait != port->closing_wait) ||
828 (new_info->xmit_fifo_size &&
829 new_info->xmit_fifo_size != uport->fifosize) ||
830 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
832 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
833 (new_flags & UPF_USR_MASK));
834 uport->custom_divisor = new_info->custom_divisor;
839 * Ask the low level driver to verify the settings.
841 if (uport->ops->verify_port)
842 retval = uport->ops->verify_port(uport, new_info);
844 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
845 (new_info->baud_base < 9600))
851 if (change_port || change_irq) {
855 * Make sure that we are the sole user of this port.
857 if (tty_port_users(port) > 1)
861 * We need to shutdown the serial port at the old
862 * port/type/irq combination.
864 uart_shutdown(tty, state);
868 unsigned long old_iobase, old_mapbase;
869 unsigned int old_type, old_iotype, old_hub6, old_shift;
871 old_iobase = uport->iobase;
872 old_mapbase = uport->mapbase;
873 old_type = uport->type;
874 old_hub6 = uport->hub6;
875 old_iotype = uport->iotype;
876 old_shift = uport->regshift;
879 * Free and release old regions
881 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
882 uport->ops->release_port(uport);
884 uport->iobase = new_port;
885 uport->type = new_info->type;
886 uport->hub6 = new_info->hub6;
887 uport->iotype = new_info->io_type;
888 uport->regshift = new_info->iomem_reg_shift;
889 uport->mapbase = (unsigned long)new_info->iomem_base;
892 * Claim and map the new regions
894 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
895 retval = uport->ops->request_port(uport);
897 /* Always success - Jean II */
902 * If we fail to request resources for the
903 * new port, try to restore the old settings.
906 uport->iobase = old_iobase;
907 uport->type = old_type;
908 uport->hub6 = old_hub6;
909 uport->iotype = old_iotype;
910 uport->regshift = old_shift;
911 uport->mapbase = old_mapbase;
913 if (old_type != PORT_UNKNOWN) {
914 retval = uport->ops->request_port(uport);
916 * If we failed to restore the old settings,
920 uport->type = PORT_UNKNOWN;
928 /* Added to return the correct error -Ram Gupta */
934 uport->irq = new_info->irq;
935 if (!(uport->flags & UPF_FIXED_PORT))
936 uport->uartclk = new_info->baud_base * 16;
937 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
938 (new_flags & UPF_CHANGE_MASK);
939 uport->custom_divisor = new_info->custom_divisor;
940 port->close_delay = close_delay;
941 port->closing_wait = closing_wait;
942 if (new_info->xmit_fifo_size)
943 uport->fifosize = new_info->xmit_fifo_size;
944 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
948 if (uport->type == PORT_UNKNOWN)
950 if (tty_port_initialized(port)) {
951 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
952 old_custom_divisor != uport->custom_divisor) {
954 * If they're setting up a custom divisor or speed,
955 * instead of clearing it, then bitch about it. No
956 * need to rate-limit; it's CAP_SYS_ADMIN only.
958 if (uport->flags & UPF_SPD_MASK) {
959 dev_notice(uport->dev,
960 "%s sets custom speed on %s. This is deprecated.\n",
962 tty_name(port->tty));
964 uart_change_speed(tty, state, NULL);
967 retval = uart_startup(tty, state, 1);
975 static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
976 struct serial_struct __user *newinfo)
978 struct serial_struct new_serial;
979 struct tty_port *port = &state->port;
982 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
986 * This semaphore protects port->count. It is also
987 * very useful to prevent opens. Also, take the
988 * port configuration semaphore to make sure that a
989 * module insertion/removal doesn't change anything
992 mutex_lock(&port->mutex);
993 retval = uart_set_info(tty, port, state, &new_serial);
994 mutex_unlock(&port->mutex);
999 * uart_get_lsr_info - get line status register info
1000 * @tty: tty associated with the UART
1001 * @state: UART being queried
1002 * @value: returned modem value
1004 static int uart_get_lsr_info(struct tty_struct *tty,
1005 struct uart_state *state, unsigned int __user *value)
1007 struct uart_port *uport = uart_port_check(state);
1008 unsigned int result;
1010 result = uport->ops->tx_empty(uport);
1013 * If we're about to load something into the transmit
1014 * register, we'll pretend the transmitter isn't empty to
1015 * avoid a race condition (depending on when the transmit
1016 * interrupt happens).
1018 if (uport->x_char ||
1019 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1020 !uart_tx_stopped(uport)))
1021 result &= ~TIOCSER_TEMT;
1023 return put_user(result, value);
1026 static int uart_tiocmget(struct tty_struct *tty)
1028 struct uart_state *state = tty->driver_data;
1029 struct tty_port *port = &state->port;
1030 struct uart_port *uport;
1033 mutex_lock(&port->mutex);
1034 uport = uart_port_check(state);
1038 if (!tty_io_error(tty)) {
1039 result = uport->mctrl;
1040 spin_lock_irq(&uport->lock);
1041 result |= uport->ops->get_mctrl(uport);
1042 spin_unlock_irq(&uport->lock);
1045 mutex_unlock(&port->mutex);
1050 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1052 struct uart_state *state = tty->driver_data;
1053 struct tty_port *port = &state->port;
1054 struct uart_port *uport;
1057 mutex_lock(&port->mutex);
1058 uport = uart_port_check(state);
1062 if (!tty_io_error(tty)) {
1063 uart_update_mctrl(uport, set, clear);
1067 mutex_unlock(&port->mutex);
1071 static int uart_break_ctl(struct tty_struct *tty, int break_state)
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 (uport->type != PORT_UNKNOWN)
1084 uport->ops->break_ctl(uport, break_state);
1087 mutex_unlock(&port->mutex);
1091 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1093 struct tty_port *port = &state->port;
1094 struct uart_port *uport;
1097 if (!capable(CAP_SYS_ADMIN))
1101 * Take the per-port semaphore. This prevents count from
1102 * changing, and hence any extra opens of the port while
1103 * we're auto-configuring.
1105 if (mutex_lock_interruptible(&port->mutex))
1106 return -ERESTARTSYS;
1108 uport = uart_port_check(state);
1115 if (tty_port_users(port) == 1) {
1116 uart_shutdown(tty, state);
1119 * If we already have a port type configured,
1120 * we must release its resources.
1122 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1123 uport->ops->release_port(uport);
1125 flags = UART_CONFIG_TYPE;
1126 if (uport->flags & UPF_AUTO_IRQ)
1127 flags |= UART_CONFIG_IRQ;
1130 * This will claim the ports resources if
1133 uport->ops->config_port(uport, flags);
1135 ret = uart_startup(tty, state, 1);
1140 mutex_unlock(&port->mutex);
1144 static void uart_enable_ms(struct uart_port *uport)
1147 * Force modem status interrupts on
1149 if (uport->ops->enable_ms)
1150 uport->ops->enable_ms(uport);
1154 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1155 * - mask passed in arg for lines of interest
1156 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1157 * Caller should use TIOCGICOUNT to see which one it was
1159 * FIXME: This wants extracting into a common all driver implementation
1160 * of TIOCMWAIT using tty_port.
1162 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1164 struct uart_port *uport;
1165 struct tty_port *port = &state->port;
1166 DECLARE_WAITQUEUE(wait, current);
1167 struct uart_icount cprev, cnow;
1171 * note the counters on entry
1173 uport = uart_port_ref(state);
1176 spin_lock_irq(&uport->lock);
1177 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1178 uart_enable_ms(uport);
1179 spin_unlock_irq(&uport->lock);
1181 add_wait_queue(&port->delta_msr_wait, &wait);
1183 spin_lock_irq(&uport->lock);
1184 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1185 spin_unlock_irq(&uport->lock);
1187 set_current_state(TASK_INTERRUPTIBLE);
1189 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1190 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1191 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1192 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1199 /* see if a signal did it */
1200 if (signal_pending(current)) {
1207 __set_current_state(TASK_RUNNING);
1208 remove_wait_queue(&port->delta_msr_wait, &wait);
1209 uart_port_deref(uport);
1215 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1216 * Return: write counters to the user passed counter struct
1217 * NB: both 1->0 and 0->1 transitions are counted except for
1218 * RI where only 0->1 is counted.
1220 static int uart_get_icount(struct tty_struct *tty,
1221 struct serial_icounter_struct *icount)
1223 struct uart_state *state = tty->driver_data;
1224 struct uart_icount cnow;
1225 struct uart_port *uport;
1227 uport = uart_port_ref(state);
1230 spin_lock_irq(&uport->lock);
1231 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1232 spin_unlock_irq(&uport->lock);
1233 uart_port_deref(uport);
1235 icount->cts = cnow.cts;
1236 icount->dsr = cnow.dsr;
1237 icount->rng = cnow.rng;
1238 icount->dcd = cnow.dcd;
1239 icount->rx = cnow.rx;
1240 icount->tx = cnow.tx;
1241 icount->frame = cnow.frame;
1242 icount->overrun = cnow.overrun;
1243 icount->parity = cnow.parity;
1244 icount->brk = cnow.brk;
1245 icount->buf_overrun = cnow.buf_overrun;
1250 static int uart_get_rs485_config(struct uart_port *port,
1251 struct serial_rs485 __user *rs485)
1253 unsigned long flags;
1254 struct serial_rs485 aux;
1256 spin_lock_irqsave(&port->lock, flags);
1258 spin_unlock_irqrestore(&port->lock, flags);
1260 if (copy_to_user(rs485, &aux, sizeof(aux)))
1266 static int uart_set_rs485_config(struct uart_port *port,
1267 struct serial_rs485 __user *rs485_user)
1269 struct serial_rs485 rs485;
1271 unsigned long flags;
1273 if (!port->rs485_config)
1274 return -ENOIOCTLCMD;
1276 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1279 spin_lock_irqsave(&port->lock, flags);
1280 ret = port->rs485_config(port, &rs485);
1281 spin_unlock_irqrestore(&port->lock, flags);
1285 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1292 * Called via sys_ioctl. We can use spin_lock_irq() here.
1295 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1297 struct uart_state *state = tty->driver_data;
1298 struct tty_port *port = &state->port;
1299 struct uart_port *uport;
1300 void __user *uarg = (void __user *)arg;
1301 int ret = -ENOIOCTLCMD;
1305 * These ioctls don't rely on the hardware to be present.
1309 ret = uart_get_info_user(port, uarg);
1313 down_write(&tty->termios_rwsem);
1314 ret = uart_set_info_user(tty, state, uarg);
1315 up_write(&tty->termios_rwsem);
1319 down_write(&tty->termios_rwsem);
1320 ret = uart_do_autoconfig(tty, state);
1321 up_write(&tty->termios_rwsem);
1324 case TIOCSERGWILD: /* obsolete */
1325 case TIOCSERSWILD: /* obsolete */
1330 if (ret != -ENOIOCTLCMD)
1333 if (tty_io_error(tty)) {
1339 * The following should only be used when hardware is present.
1343 ret = uart_wait_modem_status(state, arg);
1347 if (ret != -ENOIOCTLCMD)
1350 mutex_lock(&port->mutex);
1351 uport = uart_port_check(state);
1353 if (!uport || tty_io_error(tty)) {
1359 * All these rely on hardware being present and need to be
1360 * protected against the tty being hung up.
1364 case TIOCSERGETLSR: /* Get line status register */
1365 ret = uart_get_lsr_info(tty, state, uarg);
1369 ret = uart_get_rs485_config(uport, uarg);
1373 ret = uart_set_rs485_config(uport, uarg);
1376 if (uport->ops->ioctl)
1377 ret = uport->ops->ioctl(uport, cmd, arg);
1381 mutex_unlock(&port->mutex);
1386 static void uart_set_ldisc(struct tty_struct *tty)
1388 struct uart_state *state = tty->driver_data;
1389 struct uart_port *uport;
1391 mutex_lock(&state->port.mutex);
1392 uport = uart_port_check(state);
1393 if (uport && uport->ops->set_ldisc)
1394 uport->ops->set_ldisc(uport, &tty->termios);
1395 mutex_unlock(&state->port.mutex);
1398 static void uart_set_termios(struct tty_struct *tty,
1399 struct ktermios *old_termios)
1401 struct uart_state *state = tty->driver_data;
1402 struct uart_port *uport;
1403 unsigned int cflag = tty->termios.c_cflag;
1404 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1405 bool sw_changed = false;
1407 mutex_lock(&state->port.mutex);
1408 uport = uart_port_check(state);
1413 * Drivers doing software flow control also need to know
1414 * about changes to these input settings.
1416 if (uport->flags & UPF_SOFT_FLOW) {
1417 iflag_mask |= IXANY|IXON|IXOFF;
1419 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1420 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1424 * These are the bits that are used to setup various
1425 * flags in the low level driver. We can ignore the Bfoo
1426 * bits in c_cflag; c_[io]speed will always be set
1427 * appropriately by set_termios() in tty_ioctl.c
1429 if ((cflag ^ old_termios->c_cflag) == 0 &&
1430 tty->termios.c_ospeed == old_termios->c_ospeed &&
1431 tty->termios.c_ispeed == old_termios->c_ispeed &&
1432 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1437 uart_change_speed(tty, state, old_termios);
1438 /* reload cflag from termios; port driver may have overriden flags */
1439 cflag = tty->termios.c_cflag;
1441 /* Handle transition to B0 status */
1442 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1443 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1444 /* Handle transition away from B0 status */
1445 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1446 unsigned int mask = TIOCM_DTR;
1447 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1449 uart_set_mctrl(uport, mask);
1452 mutex_unlock(&state->port.mutex);
1456 * Calls to uart_close() are serialised via the tty_lock in
1457 * drivers/tty/tty_io.c:tty_release()
1458 * drivers/tty/tty_io.c:do_tty_hangup()
1460 static void uart_close(struct tty_struct *tty, struct file *filp)
1462 struct uart_state *state = tty->driver_data;
1463 struct tty_port *port;
1466 struct uart_driver *drv = tty->driver->driver_state;
1468 state = drv->state + tty->index;
1469 port = &state->port;
1470 spin_lock_irq(&port->lock);
1472 spin_unlock_irq(&port->lock);
1476 port = &state->port;
1477 pr_debug("uart_close(%d) called\n", tty->index);
1479 tty_port_close(tty->port, tty, filp);
1482 static void uart_tty_port_shutdown(struct tty_port *port)
1484 struct uart_state *state = container_of(port, struct uart_state, port);
1485 struct uart_port *uport = uart_port_check(state);
1488 * At this point, we stop accepting input. To do this, we
1489 * disable the receive line status interrupts.
1491 if (WARN(!uport, "detached port still initialized!\n"))
1494 spin_lock_irq(&uport->lock);
1495 uport->ops->stop_rx(uport);
1496 spin_unlock_irq(&uport->lock);
1498 uart_port_shutdown(port);
1501 * It's possible for shutdown to be called after suspend if we get
1502 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1503 * we don't try to resume a port that has been shutdown.
1505 tty_port_set_suspended(port, 0);
1507 uart_change_pm(state, UART_PM_STATE_OFF);
1511 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1513 struct uart_state *state = tty->driver_data;
1514 struct uart_port *port;
1515 unsigned long char_time, expire;
1517 port = uart_port_ref(state);
1518 if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) {
1519 uart_port_deref(port);
1524 * Set the check interval to be 1/5 of the estimated time to
1525 * send a single character, and make it at least 1. The check
1526 * interval should also be less than the timeout.
1528 * Note: we have to use pretty tight timings here to satisfy
1531 char_time = (port->timeout - HZ/50) / port->fifosize;
1532 char_time = char_time / 5;
1535 if (timeout && timeout < char_time)
1536 char_time = timeout;
1539 * If the transmitter hasn't cleared in twice the approximate
1540 * amount of time to send the entire FIFO, it probably won't
1541 * ever clear. This assumes the UART isn't doing flow
1542 * control, which is currently the case. Hence, if it ever
1543 * takes longer than port->timeout, this is probably due to a
1544 * UART bug of some kind. So, we clamp the timeout parameter at
1547 if (timeout == 0 || timeout > 2 * port->timeout)
1548 timeout = 2 * port->timeout;
1550 expire = jiffies + timeout;
1552 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1553 port->line, jiffies, expire);
1556 * Check whether the transmitter is empty every 'char_time'.
1557 * 'timeout' / 'expire' give us the maximum amount of time
1560 while (!port->ops->tx_empty(port)) {
1561 msleep_interruptible(jiffies_to_msecs(char_time));
1562 if (signal_pending(current))
1564 if (time_after(jiffies, expire))
1567 uart_port_deref(port);
1571 * Calls to uart_hangup() are serialised by the tty_lock in
1572 * drivers/tty/tty_io.c:do_tty_hangup()
1573 * This runs from a workqueue and can sleep for a _short_ time only.
1575 static void uart_hangup(struct tty_struct *tty)
1577 struct uart_state *state = tty->driver_data;
1578 struct tty_port *port = &state->port;
1579 struct uart_port *uport;
1580 unsigned long flags;
1582 pr_debug("uart_hangup(%d)\n", tty->index);
1584 mutex_lock(&port->mutex);
1585 uport = uart_port_check(state);
1586 WARN(!uport, "hangup of detached port!\n");
1588 if (tty_port_active(port)) {
1589 uart_flush_buffer(tty);
1590 uart_shutdown(tty, state);
1591 spin_lock_irqsave(&port->lock, flags);
1593 spin_unlock_irqrestore(&port->lock, flags);
1594 tty_port_set_active(port, 0);
1595 tty_port_tty_set(port, NULL);
1596 if (uport && !uart_console(uport))
1597 uart_change_pm(state, UART_PM_STATE_OFF);
1598 wake_up_interruptible(&port->open_wait);
1599 wake_up_interruptible(&port->delta_msr_wait);
1601 mutex_unlock(&port->mutex);
1604 /* uport == NULL if uart_port has already been removed */
1605 static void uart_port_shutdown(struct tty_port *port)
1607 struct uart_state *state = container_of(port, struct uart_state, port);
1608 struct uart_port *uport = uart_port_check(state);
1611 * clear delta_msr_wait queue to avoid mem leaks: we may free
1612 * the irq here so the queue might never be woken up. Note
1613 * that we won't end up waiting on delta_msr_wait again since
1614 * any outstanding file descriptors should be pointing at
1615 * hung_up_tty_fops now.
1617 wake_up_interruptible(&port->delta_msr_wait);
1620 * Free the IRQ and disable the port.
1623 uport->ops->shutdown(uport);
1626 * Ensure that the IRQ handler isn't running on another CPU.
1629 synchronize_irq(uport->irq);
1632 static int uart_carrier_raised(struct tty_port *port)
1634 struct uart_state *state = container_of(port, struct uart_state, port);
1635 struct uart_port *uport;
1638 uport = uart_port_ref(state);
1640 * Should never observe uport == NULL since checks for hangup should
1641 * abort the tty_port_block_til_ready() loop before checking for carrier
1642 * raised -- but report carrier raised if it does anyway so open will
1643 * continue and not sleep
1645 if (WARN_ON(!uport))
1647 spin_lock_irq(&uport->lock);
1648 uart_enable_ms(uport);
1649 mctrl = uport->ops->get_mctrl(uport);
1650 spin_unlock_irq(&uport->lock);
1651 uart_port_deref(uport);
1652 if (mctrl & TIOCM_CAR)
1657 static void uart_dtr_rts(struct tty_port *port, int onoff)
1659 struct uart_state *state = container_of(port, struct uart_state, port);
1660 struct uart_port *uport;
1662 uport = uart_port_ref(state);
1667 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1669 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1671 uart_port_deref(uport);
1675 * Calls to uart_open are serialised by the tty_lock in
1676 * drivers/tty/tty_io.c:tty_open()
1677 * Note that if this fails, then uart_close() _will_ be called.
1679 * In time, we want to scrap the "opening nonpresent ports"
1680 * behaviour and implement an alternative way for setserial
1681 * to set base addresses/ports/types. This will allow us to
1682 * get rid of a certain amount of extra tests.
1684 static int uart_open(struct tty_struct *tty, struct file *filp)
1686 struct uart_driver *drv = tty->driver->driver_state;
1687 int retval, line = tty->index;
1688 struct uart_state *state = drv->state + line;
1690 tty->driver_data = state;
1692 retval = tty_port_open(&state->port, tty, filp);
1699 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1701 struct uart_state *state = container_of(port, struct uart_state, port);
1702 struct uart_port *uport;
1704 uport = uart_port_check(state);
1705 if (!uport || uport->flags & UPF_DEAD)
1708 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1711 * Start up the serial port.
1713 return uart_startup(tty, state, 0);
1716 static const char *uart_type(struct uart_port *port)
1718 const char *str = NULL;
1720 if (port->ops->type)
1721 str = port->ops->type(port);
1729 #ifdef CONFIG_PROC_FS
1731 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1733 struct uart_state *state = drv->state + i;
1734 struct tty_port *port = &state->port;
1735 enum uart_pm_state pm_state;
1736 struct uart_port *uport;
1738 unsigned int status;
1741 mutex_lock(&port->mutex);
1742 uport = uart_port_check(state);
1746 mmio = uport->iotype >= UPIO_MEM;
1747 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1748 uport->line, uart_type(uport),
1749 mmio ? "mmio:0x" : "port:",
1750 mmio ? (unsigned long long)uport->mapbase
1751 : (unsigned long long)uport->iobase,
1754 if (uport->type == PORT_UNKNOWN) {
1759 if (capable(CAP_SYS_ADMIN)) {
1760 pm_state = state->pm_state;
1761 if (pm_state != UART_PM_STATE_ON)
1762 uart_change_pm(state, UART_PM_STATE_ON);
1763 spin_lock_irq(&uport->lock);
1764 status = uport->ops->get_mctrl(uport);
1765 spin_unlock_irq(&uport->lock);
1766 if (pm_state != UART_PM_STATE_ON)
1767 uart_change_pm(state, pm_state);
1769 seq_printf(m, " tx:%d rx:%d",
1770 uport->icount.tx, uport->icount.rx);
1771 if (uport->icount.frame)
1772 seq_printf(m, " fe:%d", uport->icount.frame);
1773 if (uport->icount.parity)
1774 seq_printf(m, " pe:%d", uport->icount.parity);
1775 if (uport->icount.brk)
1776 seq_printf(m, " brk:%d", uport->icount.brk);
1777 if (uport->icount.overrun)
1778 seq_printf(m, " oe:%d", uport->icount.overrun);
1780 #define INFOBIT(bit, str) \
1781 if (uport->mctrl & (bit)) \
1782 strncat(stat_buf, (str), sizeof(stat_buf) - \
1783 strlen(stat_buf) - 2)
1784 #define STATBIT(bit, str) \
1785 if (status & (bit)) \
1786 strncat(stat_buf, (str), sizeof(stat_buf) - \
1787 strlen(stat_buf) - 2)
1791 INFOBIT(TIOCM_RTS, "|RTS");
1792 STATBIT(TIOCM_CTS, "|CTS");
1793 INFOBIT(TIOCM_DTR, "|DTR");
1794 STATBIT(TIOCM_DSR, "|DSR");
1795 STATBIT(TIOCM_CAR, "|CD");
1796 STATBIT(TIOCM_RNG, "|RI");
1800 seq_puts(m, stat_buf);
1806 mutex_unlock(&port->mutex);
1809 static int uart_proc_show(struct seq_file *m, void *v)
1811 struct tty_driver *ttydrv = m->private;
1812 struct uart_driver *drv = ttydrv->driver_state;
1815 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1816 for (i = 0; i < drv->nr; i++)
1817 uart_line_info(m, drv, i);
1821 static int uart_proc_open(struct inode *inode, struct file *file)
1823 return single_open(file, uart_proc_show, PDE_DATA(inode));
1826 static const struct file_operations uart_proc_fops = {
1827 .owner = THIS_MODULE,
1828 .open = uart_proc_open,
1830 .llseek = seq_lseek,
1831 .release = single_release,
1835 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1837 * uart_console_write - write a console message to a serial port
1838 * @port: the port to write the message
1839 * @s: array of characters
1840 * @count: number of characters in string to write
1841 * @putchar: function to write character to port
1843 void uart_console_write(struct uart_port *port, const char *s,
1845 void (*putchar)(struct uart_port *, int))
1849 for (i = 0; i < count; i++, s++) {
1851 putchar(port, '\r');
1855 EXPORT_SYMBOL_GPL(uart_console_write);
1858 * Check whether an invalid uart number has been specified, and
1859 * if so, search for the first available port that does have
1862 struct uart_port * __init
1863 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1865 int idx = co->index;
1867 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1868 ports[idx].membase == NULL))
1869 for (idx = 0; idx < nr; idx++)
1870 if (ports[idx].iobase != 0 ||
1871 ports[idx].membase != NULL)
1880 * uart_parse_earlycon - Parse earlycon options
1881 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1882 * @iotype: ptr for decoded iotype (out)
1883 * @addr: ptr for decoded mapbase/iobase (out)
1884 * @options: ptr for <options> field; NULL if not present (out)
1886 * Decodes earlycon kernel command line parameters of the form
1887 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1888 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1891 * earlycon=<name>,0x<addr>,<options>
1892 * console=<name>,0x<addr>,<options>
1893 * is also accepted; the returned @iotype will be UPIO_MEM.
1895 * Returns 0 on success or -EINVAL on failure
1897 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
1900 if (strncmp(p, "mmio,", 5) == 0) {
1903 } else if (strncmp(p, "mmio16,", 7) == 0) {
1904 *iotype = UPIO_MEM16;
1906 } else if (strncmp(p, "mmio32,", 7) == 0) {
1907 *iotype = UPIO_MEM32;
1909 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1910 *iotype = UPIO_MEM32BE;
1912 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1913 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1914 UPIO_MEM32BE : UPIO_MEM32;
1916 } else if (strncmp(p, "io,", 3) == 0) {
1917 *iotype = UPIO_PORT;
1919 } else if (strncmp(p, "0x", 2) == 0) {
1926 * Before you replace it with kstrtoull(), think about options separator
1927 * (',') it will not tolerate
1929 *addr = simple_strtoull(p, NULL, 0);
1937 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1940 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1941 * @options: pointer to option string
1942 * @baud: pointer to an 'int' variable for the baud rate.
1943 * @parity: pointer to an 'int' variable for the parity.
1944 * @bits: pointer to an 'int' variable for the number of data bits.
1945 * @flow: pointer to an 'int' variable for the flow control character.
1947 * uart_parse_options decodes a string containing the serial console
1948 * options. The format of the string is <baud><parity><bits><flow>,
1952 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1956 *baud = simple_strtoul(s, NULL, 10);
1957 while (*s >= '0' && *s <= '9')
1966 EXPORT_SYMBOL_GPL(uart_parse_options);
1969 * uart_set_options - setup the serial console parameters
1970 * @port: pointer to the serial ports uart_port structure
1971 * @co: console pointer
1973 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1974 * @bits: number of data bits
1975 * @flow: flow control character - 'r' (rts)
1978 uart_set_options(struct uart_port *port, struct console *co,
1979 int baud, int parity, int bits, int flow)
1981 struct ktermios termios;
1982 static struct ktermios dummy;
1985 * Ensure that the serial console lock is initialised
1987 * If this port is a console, then the spinlock is already
1990 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1991 spin_lock_init(&port->lock);
1992 lockdep_set_class(&port->lock, &port_lock_key);
1995 memset(&termios, 0, sizeof(struct ktermios));
1997 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
1998 tty_termios_encode_baud_rate(&termios, baud, baud);
2001 termios.c_cflag |= CS7;
2003 termios.c_cflag |= CS8;
2007 termios.c_cflag |= PARODD;
2010 termios.c_cflag |= PARENB;
2015 termios.c_cflag |= CRTSCTS;
2018 * some uarts on other side don't support no flow control.
2019 * So we set * DTR in host uart to make them happy
2021 port->mctrl |= TIOCM_DTR;
2023 port->ops->set_termios(port, &termios, &dummy);
2025 * Allow the setting of the UART parameters with a NULL console
2029 co->cflag = termios.c_cflag;
2033 EXPORT_SYMBOL_GPL(uart_set_options);
2034 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2037 * uart_change_pm - set power state of the port
2039 * @state: port descriptor
2040 * @pm_state: new state
2042 * Locking: port->mutex has to be held
2044 static void uart_change_pm(struct uart_state *state,
2045 enum uart_pm_state pm_state)
2047 struct uart_port *port = uart_port_check(state);
2049 if (state->pm_state != pm_state) {
2050 if (port && port->ops->pm)
2051 port->ops->pm(port, pm_state, state->pm_state);
2052 state->pm_state = pm_state;
2057 struct uart_port *port;
2058 struct uart_driver *driver;
2061 static int serial_match_port(struct device *dev, void *data)
2063 struct uart_match *match = data;
2064 struct tty_driver *tty_drv = match->driver->tty_driver;
2065 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2068 return dev->devt == devt; /* Actually, only one tty per port */
2071 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2073 struct uart_state *state = drv->state + uport->line;
2074 struct tty_port *port = &state->port;
2075 struct device *tty_dev;
2076 struct uart_match match = {uport, drv};
2078 mutex_lock(&port->mutex);
2080 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2081 if (device_may_wakeup(tty_dev)) {
2082 if (!enable_irq_wake(uport->irq))
2083 uport->irq_wake = 1;
2084 put_device(tty_dev);
2085 mutex_unlock(&port->mutex);
2088 put_device(tty_dev);
2090 /* Nothing to do if the console is not suspending */
2091 if (!console_suspend_enabled && uart_console(uport))
2094 uport->suspended = 1;
2096 if (tty_port_initialized(port)) {
2097 const struct uart_ops *ops = uport->ops;
2100 tty_port_set_suspended(port, 1);
2101 tty_port_set_initialized(port, 0);
2103 spin_lock_irq(&uport->lock);
2104 ops->stop_tx(uport);
2105 ops->set_mctrl(uport, 0);
2106 ops->stop_rx(uport);
2107 spin_unlock_irq(&uport->lock);
2110 * Wait for the transmitter to empty.
2112 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2115 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2117 drv->tty_driver->name_base + uport->line);
2119 ops->shutdown(uport);
2123 * Disable the console device before suspending.
2125 if (uart_console(uport))
2126 console_stop(uport->cons);
2128 uart_change_pm(state, UART_PM_STATE_OFF);
2130 mutex_unlock(&port->mutex);
2135 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2137 struct uart_state *state = drv->state + uport->line;
2138 struct tty_port *port = &state->port;
2139 struct device *tty_dev;
2140 struct uart_match match = {uport, drv};
2141 struct ktermios termios;
2143 mutex_lock(&port->mutex);
2145 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2146 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2147 if (uport->irq_wake) {
2148 disable_irq_wake(uport->irq);
2149 uport->irq_wake = 0;
2151 put_device(tty_dev);
2152 mutex_unlock(&port->mutex);
2155 put_device(tty_dev);
2156 uport->suspended = 0;
2159 * Re-enable the console device after suspending.
2161 if (uart_console(uport)) {
2163 * First try to use the console cflag setting.
2165 memset(&termios, 0, sizeof(struct ktermios));
2166 termios.c_cflag = uport->cons->cflag;
2169 * If that's unset, use the tty termios setting.
2171 if (port->tty && termios.c_cflag == 0)
2172 termios = port->tty->termios;
2174 if (console_suspend_enabled)
2175 uart_change_pm(state, UART_PM_STATE_ON);
2176 uport->ops->set_termios(uport, &termios, NULL);
2177 if (console_suspend_enabled)
2178 console_start(uport->cons);
2181 if (tty_port_suspended(port)) {
2182 const struct uart_ops *ops = uport->ops;
2185 uart_change_pm(state, UART_PM_STATE_ON);
2186 spin_lock_irq(&uport->lock);
2187 ops->set_mctrl(uport, 0);
2188 spin_unlock_irq(&uport->lock);
2189 if (console_suspend_enabled || !uart_console(uport)) {
2190 /* Protected by port mutex for now */
2191 struct tty_struct *tty = port->tty;
2192 ret = ops->startup(uport);
2195 uart_change_speed(tty, state, NULL);
2196 spin_lock_irq(&uport->lock);
2197 ops->set_mctrl(uport, uport->mctrl);
2198 ops->start_tx(uport);
2199 spin_unlock_irq(&uport->lock);
2200 tty_port_set_initialized(port, 1);
2203 * Failed to resume - maybe hardware went away?
2204 * Clear the "initialized" flag so we won't try
2205 * to call the low level drivers shutdown method.
2207 uart_shutdown(tty, state);
2211 tty_port_set_suspended(port, 0);
2214 mutex_unlock(&port->mutex);
2220 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2224 switch (port->iotype) {
2226 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2229 snprintf(address, sizeof(address),
2230 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2238 snprintf(address, sizeof(address),
2239 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2242 strlcpy(address, "*unknown*", sizeof(address));
2246 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2247 port->dev ? dev_name(port->dev) : "",
2248 port->dev ? ": " : "",
2250 drv->tty_driver->name_base + port->line,
2251 address, port->irq, port->uartclk / 16, uart_type(port));
2255 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2256 struct uart_port *port)
2261 * If there isn't a port here, don't do anything further.
2263 if (!port->iobase && !port->mapbase && !port->membase)
2267 * Now do the auto configuration stuff. Note that config_port
2268 * is expected to claim the resources and map the port for us.
2271 if (port->flags & UPF_AUTO_IRQ)
2272 flags |= UART_CONFIG_IRQ;
2273 if (port->flags & UPF_BOOT_AUTOCONF) {
2274 if (!(port->flags & UPF_FIXED_TYPE)) {
2275 port->type = PORT_UNKNOWN;
2276 flags |= UART_CONFIG_TYPE;
2278 port->ops->config_port(port, flags);
2281 if (port->type != PORT_UNKNOWN) {
2282 unsigned long flags;
2284 uart_report_port(drv, port);
2286 /* Power up port for set_mctrl() */
2287 uart_change_pm(state, UART_PM_STATE_ON);
2290 * Ensure that the modem control lines are de-activated.
2291 * keep the DTR setting that is set in uart_set_options()
2292 * We probably don't need a spinlock around this, but
2294 spin_lock_irqsave(&port->lock, flags);
2295 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2296 spin_unlock_irqrestore(&port->lock, flags);
2299 * If this driver supports console, and it hasn't been
2300 * successfully registered yet, try to re-register it.
2301 * It may be that the port was not available.
2303 if (port->cons && !(port->cons->flags & CON_ENABLED))
2304 register_console(port->cons);
2307 * Power down all ports by default, except the
2308 * console if we have one.
2310 if (!uart_console(port))
2311 uart_change_pm(state, UART_PM_STATE_OFF);
2315 #ifdef CONFIG_CONSOLE_POLL
2317 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2319 struct uart_driver *drv = driver->driver_state;
2320 struct uart_state *state = drv->state + line;
2321 struct tty_port *tport;
2322 struct uart_port *port;
2332 tport = &state->port;
2333 mutex_lock(&tport->mutex);
2335 port = uart_port_check(state);
2336 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2341 if (port->ops->poll_init) {
2343 * We don't set initialized as we only initialized the hw,
2344 * e.g. state->xmit is still uninitialized.
2346 if (!tty_port_initialized(tport))
2347 ret = port->ops->poll_init(port);
2350 if (!ret && options) {
2351 uart_parse_options(options, &baud, &parity, &bits, &flow);
2352 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2355 mutex_unlock(&tport->mutex);
2359 static int uart_poll_get_char(struct tty_driver *driver, int line)
2361 struct uart_driver *drv = driver->driver_state;
2362 struct uart_state *state = drv->state + line;
2363 struct uart_port *port;
2367 port = uart_port_ref(state);
2369 ret = port->ops->poll_get_char(port);
2370 uart_port_deref(port);
2375 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2377 struct uart_driver *drv = driver->driver_state;
2378 struct uart_state *state = drv->state + line;
2379 struct uart_port *port;
2384 port = uart_port_ref(state);
2389 port->ops->poll_put_char(port, '\r');
2390 port->ops->poll_put_char(port, ch);
2391 uart_port_deref(port);
2395 static const struct tty_operations uart_ops = {
2397 .close = uart_close,
2398 .write = uart_write,
2399 .put_char = uart_put_char,
2400 .flush_chars = uart_flush_chars,
2401 .write_room = uart_write_room,
2402 .chars_in_buffer= uart_chars_in_buffer,
2403 .flush_buffer = uart_flush_buffer,
2404 .ioctl = uart_ioctl,
2405 .throttle = uart_throttle,
2406 .unthrottle = uart_unthrottle,
2407 .send_xchar = uart_send_xchar,
2408 .set_termios = uart_set_termios,
2409 .set_ldisc = uart_set_ldisc,
2411 .start = uart_start,
2412 .hangup = uart_hangup,
2413 .break_ctl = uart_break_ctl,
2414 .wait_until_sent= uart_wait_until_sent,
2415 #ifdef CONFIG_PROC_FS
2416 .proc_fops = &uart_proc_fops,
2418 .tiocmget = uart_tiocmget,
2419 .tiocmset = uart_tiocmset,
2420 .get_icount = uart_get_icount,
2421 #ifdef CONFIG_CONSOLE_POLL
2422 .poll_init = uart_poll_init,
2423 .poll_get_char = uart_poll_get_char,
2424 .poll_put_char = uart_poll_put_char,
2428 static const struct tty_port_operations uart_port_ops = {
2429 .carrier_raised = uart_carrier_raised,
2430 .dtr_rts = uart_dtr_rts,
2431 .activate = uart_port_activate,
2432 .shutdown = uart_tty_port_shutdown,
2436 * uart_register_driver - register a driver with the uart core layer
2437 * @drv: low level driver structure
2439 * Register a uart driver with the core driver. We in turn register
2440 * with the tty layer, and initialise the core driver per-port state.
2442 * We have a proc file in /proc/tty/driver which is named after the
2445 * drv->port should be NULL, and the per-port structures should be
2446 * registered using uart_add_one_port after this call has succeeded.
2448 int uart_register_driver(struct uart_driver *drv)
2450 struct tty_driver *normal;
2456 * Maybe we should be using a slab cache for this, especially if
2457 * we have a large number of ports to handle.
2459 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2463 normal = alloc_tty_driver(drv->nr);
2467 drv->tty_driver = normal;
2469 normal->driver_name = drv->driver_name;
2470 normal->name = drv->dev_name;
2471 normal->major = drv->major;
2472 normal->minor_start = drv->minor;
2473 normal->type = TTY_DRIVER_TYPE_SERIAL;
2474 normal->subtype = SERIAL_TYPE_NORMAL;
2475 normal->init_termios = tty_std_termios;
2476 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2477 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2478 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2479 normal->driver_state = drv;
2480 tty_set_operations(normal, &uart_ops);
2483 * Initialise the UART state(s).
2485 for (i = 0; i < drv->nr; i++) {
2486 struct uart_state *state = drv->state + i;
2487 struct tty_port *port = &state->port;
2489 tty_port_init(port);
2490 port->ops = &uart_port_ops;
2493 retval = tty_register_driver(normal);
2497 for (i = 0; i < drv->nr; i++)
2498 tty_port_destroy(&drv->state[i].port);
2499 put_tty_driver(normal);
2507 * uart_unregister_driver - remove a driver from the uart core layer
2508 * @drv: low level driver structure
2510 * Remove all references to a driver from the core driver. The low
2511 * level driver must have removed all its ports via the
2512 * uart_remove_one_port() if it registered them with uart_add_one_port().
2513 * (ie, drv->port == NULL)
2515 void uart_unregister_driver(struct uart_driver *drv)
2517 struct tty_driver *p = drv->tty_driver;
2520 tty_unregister_driver(p);
2522 for (i = 0; i < drv->nr; i++)
2523 tty_port_destroy(&drv->state[i].port);
2526 drv->tty_driver = NULL;
2529 struct tty_driver *uart_console_device(struct console *co, int *index)
2531 struct uart_driver *p = co->data;
2533 return p->tty_driver;
2536 static ssize_t uart_get_attr_uartclk(struct device *dev,
2537 struct device_attribute *attr, char *buf)
2539 struct serial_struct tmp;
2540 struct tty_port *port = dev_get_drvdata(dev);
2542 uart_get_info(port, &tmp);
2543 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2546 static ssize_t uart_get_attr_type(struct device *dev,
2547 struct device_attribute *attr, char *buf)
2549 struct serial_struct tmp;
2550 struct tty_port *port = dev_get_drvdata(dev);
2552 uart_get_info(port, &tmp);
2553 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2555 static ssize_t uart_get_attr_line(struct device *dev,
2556 struct device_attribute *attr, char *buf)
2558 struct serial_struct tmp;
2559 struct tty_port *port = dev_get_drvdata(dev);
2561 uart_get_info(port, &tmp);
2562 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2565 static ssize_t uart_get_attr_port(struct device *dev,
2566 struct device_attribute *attr, char *buf)
2568 struct serial_struct tmp;
2569 struct tty_port *port = dev_get_drvdata(dev);
2570 unsigned long ioaddr;
2572 uart_get_info(port, &tmp);
2574 if (HIGH_BITS_OFFSET)
2575 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2576 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2579 static ssize_t uart_get_attr_irq(struct device *dev,
2580 struct device_attribute *attr, char *buf)
2582 struct serial_struct tmp;
2583 struct tty_port *port = dev_get_drvdata(dev);
2585 uart_get_info(port, &tmp);
2586 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2589 static ssize_t uart_get_attr_flags(struct device *dev,
2590 struct device_attribute *attr, char *buf)
2592 struct serial_struct tmp;
2593 struct tty_port *port = dev_get_drvdata(dev);
2595 uart_get_info(port, &tmp);
2596 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2599 static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2600 struct device_attribute *attr, char *buf)
2602 struct serial_struct tmp;
2603 struct tty_port *port = dev_get_drvdata(dev);
2605 uart_get_info(port, &tmp);
2606 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2610 static ssize_t uart_get_attr_close_delay(struct device *dev,
2611 struct device_attribute *attr, char *buf)
2613 struct serial_struct tmp;
2614 struct tty_port *port = dev_get_drvdata(dev);
2616 uart_get_info(port, &tmp);
2617 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2621 static ssize_t uart_get_attr_closing_wait(struct device *dev,
2622 struct device_attribute *attr, char *buf)
2624 struct serial_struct tmp;
2625 struct tty_port *port = dev_get_drvdata(dev);
2627 uart_get_info(port, &tmp);
2628 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2631 static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2632 struct device_attribute *attr, char *buf)
2634 struct serial_struct tmp;
2635 struct tty_port *port = dev_get_drvdata(dev);
2637 uart_get_info(port, &tmp);
2638 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2641 static ssize_t uart_get_attr_io_type(struct device *dev,
2642 struct device_attribute *attr, char *buf)
2644 struct serial_struct tmp;
2645 struct tty_port *port = dev_get_drvdata(dev);
2647 uart_get_info(port, &tmp);
2648 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2651 static ssize_t uart_get_attr_iomem_base(struct device *dev,
2652 struct device_attribute *attr, char *buf)
2654 struct serial_struct tmp;
2655 struct tty_port *port = dev_get_drvdata(dev);
2657 uart_get_info(port, &tmp);
2658 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2661 static ssize_t uart_get_attr_iomem_reg_shift(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.iomem_reg_shift);
2671 static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2672 static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2673 static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2674 static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2675 static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2676 static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2677 static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2678 static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2679 static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2680 static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2681 static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2682 static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2683 static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2685 static struct attribute *tty_dev_attrs[] = {
2686 &dev_attr_type.attr,
2687 &dev_attr_line.attr,
2688 &dev_attr_port.attr,
2690 &dev_attr_flags.attr,
2691 &dev_attr_xmit_fifo_size.attr,
2692 &dev_attr_uartclk.attr,
2693 &dev_attr_close_delay.attr,
2694 &dev_attr_closing_wait.attr,
2695 &dev_attr_custom_divisor.attr,
2696 &dev_attr_io_type.attr,
2697 &dev_attr_iomem_base.attr,
2698 &dev_attr_iomem_reg_shift.attr,
2702 static const struct attribute_group tty_dev_attr_group = {
2703 .attrs = tty_dev_attrs,
2707 * uart_add_one_port - attach a driver-defined port structure
2708 * @drv: pointer to the uart low level driver structure for this port
2709 * @uport: uart port structure to use for this port.
2711 * This allows the driver to register its own uart_port structure
2712 * with the core driver. The main purpose is to allow the low
2713 * level uart drivers to expand uart_port, rather than having yet
2714 * more levels of structures.
2716 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2718 struct uart_state *state;
2719 struct tty_port *port;
2721 struct device *tty_dev;
2724 BUG_ON(in_interrupt());
2726 if (uport->line >= drv->nr)
2729 state = drv->state + uport->line;
2730 port = &state->port;
2732 mutex_lock(&port_mutex);
2733 mutex_lock(&port->mutex);
2734 if (state->uart_port) {
2739 /* Link the port to the driver state table and vice versa */
2740 atomic_set(&state->refcount, 1);
2741 init_waitqueue_head(&state->remove_wait);
2742 state->uart_port = uport;
2743 uport->state = state;
2745 state->pm_state = UART_PM_STATE_UNDEFINED;
2746 uport->cons = drv->cons;
2747 uport->minor = drv->tty_driver->minor_start + uport->line;
2750 * If this port is a console, then the spinlock is already
2753 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2754 spin_lock_init(&uport->lock);
2755 lockdep_set_class(&uport->lock, &port_lock_key);
2757 if (uport->cons && uport->dev)
2758 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2760 uart_configure_port(drv, state, uport);
2762 port->console = uart_console(uport);
2765 if (uport->attr_group)
2768 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2770 if (!uport->tty_groups) {
2774 uport->tty_groups[0] = &tty_dev_attr_group;
2775 if (uport->attr_group)
2776 uport->tty_groups[1] = uport->attr_group;
2779 * Register the port whether it's detected or not. This allows
2780 * setserial to be used to alter this port's parameters.
2782 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2783 uport->line, uport->dev, port, uport->tty_groups);
2784 if (likely(!IS_ERR(tty_dev))) {
2785 device_set_wakeup_capable(tty_dev, 1);
2787 dev_err(uport->dev, "Cannot register tty device on line %d\n",
2792 * Ensure UPF_DEAD is not set.
2794 uport->flags &= ~UPF_DEAD;
2797 mutex_unlock(&port->mutex);
2798 mutex_unlock(&port_mutex);
2804 * uart_remove_one_port - detach a driver defined port structure
2805 * @drv: pointer to the uart low level driver structure for this port
2806 * @uport: uart port structure for this port
2808 * This unhooks (and hangs up) the specified port structure from the
2809 * core driver. No further calls will be made to the low-level code
2812 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2814 struct uart_state *state = drv->state + uport->line;
2815 struct tty_port *port = &state->port;
2816 struct uart_port *uart_port;
2817 struct tty_struct *tty;
2820 BUG_ON(in_interrupt());
2822 mutex_lock(&port_mutex);
2825 * Mark the port "dead" - this prevents any opens from
2826 * succeeding while we shut down the port.
2828 mutex_lock(&port->mutex);
2829 uart_port = uart_port_check(state);
2830 if (uart_port != uport)
2831 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2835 mutex_unlock(&port->mutex);
2839 uport->flags |= UPF_DEAD;
2840 mutex_unlock(&port->mutex);
2843 * Remove the devices from the tty layer
2845 tty_unregister_device(drv->tty_driver, uport->line);
2847 tty = tty_port_tty_get(port);
2849 tty_vhangup(port->tty);
2854 * If the port is used as a console, unregister it
2856 if (uart_console(uport))
2857 unregister_console(uport->cons);
2860 * Free the port IO and memory resources, if any.
2862 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
2863 uport->ops->release_port(uport);
2864 kfree(uport->tty_groups);
2867 * Indicate that there isn't a port here anymore.
2869 uport->type = PORT_UNKNOWN;
2871 mutex_lock(&port->mutex);
2872 WARN_ON(atomic_dec_return(&state->refcount) < 0);
2873 wait_event(state->remove_wait, !atomic_read(&state->refcount));
2874 state->uart_port = NULL;
2875 mutex_unlock(&port->mutex);
2877 mutex_unlock(&port_mutex);
2883 * Are the two ports equivalent?
2885 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2887 if (port1->iotype != port2->iotype)
2890 switch (port1->iotype) {
2892 return (port1->iobase == port2->iobase);
2894 return (port1->iobase == port2->iobase) &&
2895 (port1->hub6 == port2->hub6);
2902 return (port1->mapbase == port2->mapbase);
2906 EXPORT_SYMBOL(uart_match_port);
2909 * uart_handle_dcd_change - handle a change of carrier detect state
2910 * @uport: uart_port structure for the open port
2911 * @status: new carrier detect status, nonzero if active
2913 * Caller must hold uport->lock
2915 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2917 struct tty_port *port = &uport->state->port;
2918 struct tty_struct *tty = port->tty;
2919 struct tty_ldisc *ld;
2921 lockdep_assert_held_once(&uport->lock);
2924 ld = tty_ldisc_ref(tty);
2926 if (ld->ops->dcd_change)
2927 ld->ops->dcd_change(tty, status);
2928 tty_ldisc_deref(ld);
2932 uport->icount.dcd++;
2934 if (uart_dcd_enabled(uport)) {
2936 wake_up_interruptible(&port->open_wait);
2941 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2944 * uart_handle_cts_change - handle a change of clear-to-send state
2945 * @uport: uart_port structure for the open port
2946 * @status: new clear to send status, nonzero if active
2948 * Caller must hold uport->lock
2950 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2952 lockdep_assert_held_once(&uport->lock);
2954 uport->icount.cts++;
2956 if (uart_softcts_mode(uport)) {
2957 if (uport->hw_stopped) {
2959 uport->hw_stopped = 0;
2960 uport->ops->start_tx(uport);
2961 uart_write_wakeup(uport);
2965 uport->hw_stopped = 1;
2966 uport->ops->stop_tx(uport);
2972 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2975 * uart_insert_char - push a char to the uart layer
2977 * User is responsible to call tty_flip_buffer_push when they are done with
2980 * @port: corresponding port
2981 * @status: state of the serial port RX buffer (LSR for 8250)
2982 * @overrun: mask of overrun bits in @status
2983 * @ch: character to push
2984 * @flag: flag for the character (see TTY_NORMAL and friends)
2986 void uart_insert_char(struct uart_port *port, unsigned int status,
2987 unsigned int overrun, unsigned int ch, unsigned int flag)
2989 struct tty_port *tport = &port->state->port;
2991 if ((status & port->ignore_status_mask & ~overrun) == 0)
2992 if (tty_insert_flip_char(tport, ch, flag) == 0)
2993 ++port->icount.buf_overrun;
2996 * Overrun is special. Since it's reported immediately,
2997 * it doesn't affect the current character.
2999 if (status & ~port->ignore_status_mask & overrun)
3000 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3001 ++port->icount.buf_overrun;
3003 EXPORT_SYMBOL_GPL(uart_insert_char);
3005 EXPORT_SYMBOL(uart_write_wakeup);
3006 EXPORT_SYMBOL(uart_register_driver);
3007 EXPORT_SYMBOL(uart_unregister_driver);
3008 EXPORT_SYMBOL(uart_suspend_port);
3009 EXPORT_SYMBOL(uart_resume_port);
3010 EXPORT_SYMBOL(uart_add_one_port);
3011 EXPORT_SYMBOL(uart_remove_one_port);
3013 MODULE_DESCRIPTION("Serial driver core");
3014 MODULE_LICENSE("GPL");