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>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/device.h>
32 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
33 #include <linux/serial_core.h>
34 #include <linux/delay.h>
35 #include <linux/mutex.h>
38 #include <asm/uaccess.h>
41 * This is used to lock changes in serial line configuration.
43 static DEFINE_MUTEX(port_mutex);
46 * lockdep: port->lock is initialized in two places, but we
47 * want only one lock-class:
49 static struct lock_class_key port_lock_key;
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
54 struct ktermios *old_termios);
55 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
56 static void uart_change_pm(struct uart_state *state,
57 enum uart_pm_state pm_state);
59 static void uart_port_shutdown(struct tty_port *port);
62 * This routine is used by the interrupt handler to schedule processing in
63 * the software interrupt portion of the driver.
65 void uart_write_wakeup(struct uart_port *port)
67 struct uart_state *state = port->state;
69 * This means you called this function _after_ the port was
70 * closed. No cookie for you.
73 tty_wakeup(state->port.tty);
76 static void uart_stop(struct tty_struct *tty)
78 struct uart_state *state = tty->driver_data;
79 struct uart_port *port = state->uart_port;
82 spin_lock_irqsave(&port->lock, flags);
83 port->ops->stop_tx(port);
84 spin_unlock_irqrestore(&port->lock, flags);
87 static void __uart_start(struct tty_struct *tty)
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->uart_port;
92 if (!tty->stopped && !tty->hw_stopped)
93 port->ops->start_tx(port);
96 static void uart_start(struct tty_struct *tty)
98 struct uart_state *state = tty->driver_data;
99 struct uart_port *port = state->uart_port;
102 spin_lock_irqsave(&port->lock, flags);
104 spin_unlock_irqrestore(&port->lock, flags);
108 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
113 spin_lock_irqsave(&port->lock, flags);
115 port->mctrl = (old & ~clear) | set;
116 if (old != port->mctrl)
117 port->ops->set_mctrl(port, port->mctrl);
118 spin_unlock_irqrestore(&port->lock, flags);
121 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
122 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
125 * Startup the port. This will be called once per open. All calls
126 * will be serialised by the per-port mutex.
128 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
131 struct uart_port *uport = state->uart_port;
132 struct tty_port *port = &state->port;
136 if (uport->type == PORT_UNKNOWN)
140 * Make sure the device is in D0 state.
142 uart_change_pm(state, UART_PM_STATE_ON);
145 * Initialise and allocate the transmit and temporary
148 if (!state->xmit.buf) {
149 /* This is protected by the per port mutex */
150 page = get_zeroed_page(GFP_KERNEL);
154 state->xmit.buf = (unsigned char *) page;
155 uart_circ_clear(&state->xmit);
158 retval = uport->ops->startup(uport);
160 if (uart_console(uport) && uport->cons->cflag) {
161 tty->termios.c_cflag = uport->cons->cflag;
162 uport->cons->cflag = 0;
165 * Initialise the hardware port settings.
167 uart_change_speed(tty, state, NULL);
171 * Setup the RTS and DTR signals once the
172 * port is open and ready to respond.
174 if (tty->termios.c_cflag & CBAUD)
175 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
178 * if hw support flow control without software intervention,
179 * then skip the below check
181 if (tty_port_cts_enabled(port) &&
182 !(uport->flags & UPF_HARD_FLOW)) {
183 spin_lock_irq(&uport->lock);
184 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
186 spin_unlock_irq(&uport->lock);
191 * This is to allow setserial on this port. People may want to set
192 * port/irq/type and then reconfigure the port properly if it failed
195 if (retval && capable(CAP_SYS_ADMIN))
201 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
204 struct tty_port *port = &state->port;
207 if (port->flags & ASYNC_INITIALIZED)
211 * Set the TTY IO error marker - we will only clear this
212 * once we have successfully opened the port.
214 set_bit(TTY_IO_ERROR, &tty->flags);
216 retval = uart_port_startup(tty, state, init_hw);
218 set_bit(ASYNCB_INITIALIZED, &port->flags);
219 clear_bit(TTY_IO_ERROR, &tty->flags);
220 } else if (retval > 0)
227 * This routine will shutdown a serial port; interrupts are disabled, and
228 * DTR is dropped if the hangup on close termio flag is on. Calls to
229 * uart_shutdown are serialised by the per-port semaphore.
231 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
233 struct uart_port *uport = state->uart_port;
234 struct tty_port *port = &state->port;
237 * Set the TTY IO error marker
240 set_bit(TTY_IO_ERROR, &tty->flags);
242 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
244 * Turn off DTR and RTS early.
246 if (uart_console(uport) && tty)
247 uport->cons->cflag = tty->termios.c_cflag;
249 if (!tty || (tty->termios.c_cflag & HUPCL))
250 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
252 uart_port_shutdown(port);
256 * It's possible for shutdown to be called after suspend if we get
257 * a DCD drop (hangup) at just the right time. Clear suspended bit so
258 * we don't try to resume a port that has been shutdown.
260 clear_bit(ASYNCB_SUSPENDED, &port->flags);
263 * Free the transmit buffer page.
265 if (state->xmit.buf) {
266 free_page((unsigned long)state->xmit.buf);
267 state->xmit.buf = NULL;
272 * uart_update_timeout - update per-port FIFO timeout.
273 * @port: uart_port structure describing the port
274 * @cflag: termios cflag value
275 * @baud: speed of the port
277 * Set the port FIFO timeout value. The @cflag value should
278 * reflect the actual hardware settings.
281 uart_update_timeout(struct uart_port *port, unsigned int cflag,
286 /* byte size and parity */
287 switch (cflag & CSIZE) {
308 * The total number of bits to be transmitted in the fifo.
310 bits = bits * port->fifosize;
313 * Figure the timeout to send the above number of bits.
314 * Add .02 seconds of slop
316 port->timeout = (HZ * bits) / baud + HZ/50;
319 EXPORT_SYMBOL(uart_update_timeout);
322 * uart_get_baud_rate - return baud rate for a particular port
323 * @port: uart_port structure describing the port in question.
324 * @termios: desired termios settings.
325 * @old: old termios (or NULL)
326 * @min: minimum acceptable baud rate
327 * @max: maximum acceptable baud rate
329 * Decode the termios structure into a numeric baud rate,
330 * taking account of the magic 38400 baud rate (with spd_*
331 * flags), and mapping the %B0 rate to 9600 baud.
333 * If the new baud rate is invalid, try the old termios setting.
334 * If it's still invalid, we try 9600 baud.
336 * Update the @termios structure to reflect the baud rate
337 * we're actually going to be using. Don't do this for the case
338 * where B0 is requested ("hang up").
341 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
342 struct ktermios *old, unsigned int min, unsigned int max)
344 unsigned int try, baud, altbaud = 38400;
346 upf_t flags = port->flags & UPF_SPD_MASK;
348 if (flags == UPF_SPD_HI)
350 else if (flags == UPF_SPD_VHI)
352 else if (flags == UPF_SPD_SHI)
354 else if (flags == UPF_SPD_WARP)
357 for (try = 0; try < 2; try++) {
358 baud = tty_termios_baud_rate(termios);
361 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
368 * Special case: B0 rate.
375 if (baud >= min && baud <= max)
379 * Oops, the quotient was zero. Try again with
380 * the old baud rate if possible.
382 termios->c_cflag &= ~CBAUD;
384 baud = tty_termios_baud_rate(old);
386 tty_termios_encode_baud_rate(termios,
393 * As a last resort, if the range cannot be met then clip to
394 * the nearest chip supported rate.
398 tty_termios_encode_baud_rate(termios,
401 tty_termios_encode_baud_rate(termios,
405 /* Should never happen */
410 EXPORT_SYMBOL(uart_get_baud_rate);
413 * uart_get_divisor - return uart clock divisor
414 * @port: uart_port structure describing the port.
415 * @baud: desired baud rate
417 * Calculate the uart clock divisor for the port.
420 uart_get_divisor(struct uart_port *port, unsigned int baud)
425 * Old custom speed handling.
427 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
428 quot = port->custom_divisor;
430 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
435 EXPORT_SYMBOL(uart_get_divisor);
437 /* FIXME: Consistent locking policy */
438 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
439 struct ktermios *old_termios)
441 struct tty_port *port = &state->port;
442 struct uart_port *uport = state->uart_port;
443 struct ktermios *termios;
446 * If we have no tty, termios, or the port does not exist,
447 * then we can't set the parameters for this port.
449 if (!tty || uport->type == PORT_UNKNOWN)
452 termios = &tty->termios;
453 uport->ops->set_termios(uport, termios, old_termios);
456 * Set flags based on termios cflag
458 if (termios->c_cflag & CRTSCTS)
459 set_bit(ASYNCB_CTS_FLOW, &port->flags);
461 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
463 if (termios->c_cflag & CLOCAL)
464 clear_bit(ASYNCB_CHECK_CD, &port->flags);
466 set_bit(ASYNCB_CHECK_CD, &port->flags);
469 static inline int __uart_put_char(struct uart_port *port,
470 struct circ_buf *circ, unsigned char c)
478 spin_lock_irqsave(&port->lock, flags);
479 if (uart_circ_chars_free(circ) != 0) {
480 circ->buf[circ->head] = c;
481 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
484 spin_unlock_irqrestore(&port->lock, flags);
488 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
490 struct uart_state *state = tty->driver_data;
492 return __uart_put_char(state->uart_port, &state->xmit, ch);
495 static void uart_flush_chars(struct tty_struct *tty)
500 static int uart_write(struct tty_struct *tty,
501 const unsigned char *buf, int count)
503 struct uart_state *state = tty->driver_data;
504 struct uart_port *port;
505 struct circ_buf *circ;
510 * This means you called this function _after_ the port was
511 * closed. No cookie for you.
518 port = state->uart_port;
524 spin_lock_irqsave(&port->lock, flags);
526 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
531 memcpy(circ->buf + circ->head, buf, c);
532 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
537 spin_unlock_irqrestore(&port->lock, flags);
543 static int uart_write_room(struct tty_struct *tty)
545 struct uart_state *state = tty->driver_data;
549 spin_lock_irqsave(&state->uart_port->lock, flags);
550 ret = uart_circ_chars_free(&state->xmit);
551 spin_unlock_irqrestore(&state->uart_port->lock, flags);
555 static int uart_chars_in_buffer(struct tty_struct *tty)
557 struct uart_state *state = tty->driver_data;
561 spin_lock_irqsave(&state->uart_port->lock, flags);
562 ret = uart_circ_chars_pending(&state->xmit);
563 spin_unlock_irqrestore(&state->uart_port->lock, flags);
567 static void uart_flush_buffer(struct tty_struct *tty)
569 struct uart_state *state = tty->driver_data;
570 struct uart_port *port;
574 * This means you called this function _after_ the port was
575 * closed. No cookie for you.
582 port = state->uart_port;
583 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
585 spin_lock_irqsave(&port->lock, flags);
586 uart_circ_clear(&state->xmit);
587 if (port->ops->flush_buffer)
588 port->ops->flush_buffer(port);
589 spin_unlock_irqrestore(&port->lock, flags);
594 * This function is used to send a high-priority XON/XOFF character to
597 static void uart_send_xchar(struct tty_struct *tty, char ch)
599 struct uart_state *state = tty->driver_data;
600 struct uart_port *port = state->uart_port;
603 if (port->ops->send_xchar)
604 port->ops->send_xchar(port, ch);
608 spin_lock_irqsave(&port->lock, flags);
609 port->ops->start_tx(port);
610 spin_unlock_irqrestore(&port->lock, flags);
615 static void uart_throttle(struct tty_struct *tty)
617 struct uart_state *state = tty->driver_data;
618 struct uart_port *port = state->uart_port;
622 mask |= UPF_SOFT_FLOW;
623 if (tty->termios.c_cflag & CRTSCTS)
624 mask |= UPF_HARD_FLOW;
626 if (port->flags & mask) {
627 port->ops->throttle(port);
628 mask &= ~port->flags;
631 if (mask & UPF_SOFT_FLOW)
632 uart_send_xchar(tty, STOP_CHAR(tty));
634 if (mask & UPF_HARD_FLOW)
635 uart_clear_mctrl(port, TIOCM_RTS);
638 static void uart_unthrottle(struct tty_struct *tty)
640 struct uart_state *state = tty->driver_data;
641 struct uart_port *port = state->uart_port;
645 mask |= UPF_SOFT_FLOW;
646 if (tty->termios.c_cflag & CRTSCTS)
647 mask |= UPF_HARD_FLOW;
649 if (port->flags & mask) {
650 port->ops->unthrottle(port);
651 mask &= ~port->flags;
654 if (mask & UPF_SOFT_FLOW) {
658 uart_send_xchar(tty, START_CHAR(tty));
661 if (mask & UPF_HARD_FLOW)
662 uart_set_mctrl(port, TIOCM_RTS);
665 static void do_uart_get_info(struct tty_port *port,
666 struct serial_struct *retinfo)
668 struct uart_state *state = container_of(port, struct uart_state, port);
669 struct uart_port *uport = state->uart_port;
671 memset(retinfo, 0, sizeof(*retinfo));
673 retinfo->type = uport->type;
674 retinfo->line = uport->line;
675 retinfo->port = uport->iobase;
676 if (HIGH_BITS_OFFSET)
677 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
678 retinfo->irq = uport->irq;
679 retinfo->flags = uport->flags;
680 retinfo->xmit_fifo_size = uport->fifosize;
681 retinfo->baud_base = uport->uartclk / 16;
682 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
683 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
684 ASYNC_CLOSING_WAIT_NONE :
685 jiffies_to_msecs(port->closing_wait) / 10;
686 retinfo->custom_divisor = uport->custom_divisor;
687 retinfo->hub6 = uport->hub6;
688 retinfo->io_type = uport->iotype;
689 retinfo->iomem_reg_shift = uport->regshift;
690 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
693 static void uart_get_info(struct tty_port *port,
694 struct serial_struct *retinfo)
696 /* Ensure the state we copy is consistent and no hardware changes
698 mutex_lock(&port->mutex);
699 do_uart_get_info(port, retinfo);
700 mutex_unlock(&port->mutex);
703 static int uart_get_info_user(struct tty_port *port,
704 struct serial_struct __user *retinfo)
706 struct serial_struct tmp;
707 uart_get_info(port, &tmp);
709 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
714 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
715 struct uart_state *state,
716 struct serial_struct *new_info)
718 struct uart_port *uport = state->uart_port;
719 unsigned long new_port;
720 unsigned int change_irq, change_port, closing_wait;
721 unsigned int old_custom_divisor, close_delay;
722 upf_t old_flags, new_flags;
725 new_port = new_info->port;
726 if (HIGH_BITS_OFFSET)
727 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
729 new_info->irq = irq_canonicalize(new_info->irq);
730 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
731 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
732 ASYNC_CLOSING_WAIT_NONE :
733 msecs_to_jiffies(new_info->closing_wait * 10);
736 change_irq = !(uport->flags & UPF_FIXED_PORT)
737 && new_info->irq != uport->irq;
740 * Since changing the 'type' of the port changes its resource
741 * allocations, we should treat type changes the same as
744 change_port = !(uport->flags & UPF_FIXED_PORT)
745 && (new_port != uport->iobase ||
746 (unsigned long)new_info->iomem_base != uport->mapbase ||
747 new_info->hub6 != uport->hub6 ||
748 new_info->io_type != uport->iotype ||
749 new_info->iomem_reg_shift != uport->regshift ||
750 new_info->type != uport->type);
752 old_flags = uport->flags;
753 new_flags = new_info->flags;
754 old_custom_divisor = uport->custom_divisor;
756 if (!capable(CAP_SYS_ADMIN)) {
758 if (change_irq || change_port ||
759 (new_info->baud_base != uport->uartclk / 16) ||
760 (close_delay != port->close_delay) ||
761 (closing_wait != port->closing_wait) ||
762 (new_info->xmit_fifo_size &&
763 new_info->xmit_fifo_size != uport->fifosize) ||
764 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
766 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
767 (new_flags & UPF_USR_MASK));
768 uport->custom_divisor = new_info->custom_divisor;
773 * Ask the low level driver to verify the settings.
775 if (uport->ops->verify_port)
776 retval = uport->ops->verify_port(uport, new_info);
778 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
779 (new_info->baud_base < 9600))
785 if (change_port || change_irq) {
789 * Make sure that we are the sole user of this port.
791 if (tty_port_users(port) > 1)
795 * We need to shutdown the serial port at the old
796 * port/type/irq combination.
798 uart_shutdown(tty, state);
802 unsigned long old_iobase, old_mapbase;
803 unsigned int old_type, old_iotype, old_hub6, old_shift;
805 old_iobase = uport->iobase;
806 old_mapbase = uport->mapbase;
807 old_type = uport->type;
808 old_hub6 = uport->hub6;
809 old_iotype = uport->iotype;
810 old_shift = uport->regshift;
813 * Free and release old regions
815 if (old_type != PORT_UNKNOWN)
816 uport->ops->release_port(uport);
818 uport->iobase = new_port;
819 uport->type = new_info->type;
820 uport->hub6 = new_info->hub6;
821 uport->iotype = new_info->io_type;
822 uport->regshift = new_info->iomem_reg_shift;
823 uport->mapbase = (unsigned long)new_info->iomem_base;
826 * Claim and map the new regions
828 if (uport->type != PORT_UNKNOWN) {
829 retval = uport->ops->request_port(uport);
831 /* Always success - Jean II */
836 * If we fail to request resources for the
837 * new port, try to restore the old settings.
840 uport->iobase = old_iobase;
841 uport->type = old_type;
842 uport->hub6 = old_hub6;
843 uport->iotype = old_iotype;
844 uport->regshift = old_shift;
845 uport->mapbase = old_mapbase;
847 if (old_type != PORT_UNKNOWN) {
848 retval = uport->ops->request_port(uport);
850 * If we failed to restore the old settings,
854 uport->type = PORT_UNKNOWN;
862 /* Added to return the correct error -Ram Gupta */
868 uport->irq = new_info->irq;
869 if (!(uport->flags & UPF_FIXED_PORT))
870 uport->uartclk = new_info->baud_base * 16;
871 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
872 (new_flags & UPF_CHANGE_MASK);
873 uport->custom_divisor = new_info->custom_divisor;
874 port->close_delay = close_delay;
875 port->closing_wait = closing_wait;
876 if (new_info->xmit_fifo_size)
877 uport->fifosize = new_info->xmit_fifo_size;
878 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
882 if (uport->type == PORT_UNKNOWN)
884 if (port->flags & ASYNC_INITIALIZED) {
885 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
886 old_custom_divisor != uport->custom_divisor) {
888 * If they're setting up a custom divisor or speed,
889 * instead of clearing it, then bitch about it. No
890 * need to rate-limit; it's CAP_SYS_ADMIN only.
892 if (uport->flags & UPF_SPD_MASK) {
895 "%s sets custom speed on %s. This "
896 "is deprecated.\n", current->comm,
897 tty_name(port->tty, buf));
899 uart_change_speed(tty, state, NULL);
902 retval = uart_startup(tty, state, 1);
907 static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
908 struct serial_struct __user *newinfo)
910 struct serial_struct new_serial;
911 struct tty_port *port = &state->port;
914 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
918 * This semaphore protects port->count. It is also
919 * very useful to prevent opens. Also, take the
920 * port configuration semaphore to make sure that a
921 * module insertion/removal doesn't change anything
924 mutex_lock(&port->mutex);
925 retval = uart_set_info(tty, port, state, &new_serial);
926 mutex_unlock(&port->mutex);
931 * uart_get_lsr_info - get line status register info
932 * @tty: tty associated with the UART
933 * @state: UART being queried
934 * @value: returned modem value
936 * Note: uart_ioctl protects us against hangups.
938 static int uart_get_lsr_info(struct tty_struct *tty,
939 struct uart_state *state, unsigned int __user *value)
941 struct uart_port *uport = state->uart_port;
944 result = uport->ops->tx_empty(uport);
947 * If we're about to load something into the transmit
948 * register, we'll pretend the transmitter isn't empty to
949 * avoid a race condition (depending on when the transmit
950 * interrupt happens).
953 ((uart_circ_chars_pending(&state->xmit) > 0) &&
954 !tty->stopped && !tty->hw_stopped))
955 result &= ~TIOCSER_TEMT;
957 return put_user(result, value);
960 static int uart_tiocmget(struct tty_struct *tty)
962 struct uart_state *state = tty->driver_data;
963 struct tty_port *port = &state->port;
964 struct uart_port *uport = state->uart_port;
967 mutex_lock(&port->mutex);
968 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
969 result = uport->mctrl;
970 spin_lock_irq(&uport->lock);
971 result |= uport->ops->get_mctrl(uport);
972 spin_unlock_irq(&uport->lock);
974 mutex_unlock(&port->mutex);
980 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
982 struct uart_state *state = tty->driver_data;
983 struct uart_port *uport = state->uart_port;
984 struct tty_port *port = &state->port;
987 mutex_lock(&port->mutex);
988 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
989 uart_update_mctrl(uport, set, clear);
992 mutex_unlock(&port->mutex);
996 static int uart_break_ctl(struct tty_struct *tty, int break_state)
998 struct uart_state *state = tty->driver_data;
999 struct tty_port *port = &state->port;
1000 struct uart_port *uport = state->uart_port;
1002 mutex_lock(&port->mutex);
1004 if (uport->type != PORT_UNKNOWN)
1005 uport->ops->break_ctl(uport, break_state);
1007 mutex_unlock(&port->mutex);
1011 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1013 struct uart_port *uport = state->uart_port;
1014 struct tty_port *port = &state->port;
1017 if (!capable(CAP_SYS_ADMIN))
1021 * Take the per-port semaphore. This prevents count from
1022 * changing, and hence any extra opens of the port while
1023 * we're auto-configuring.
1025 if (mutex_lock_interruptible(&port->mutex))
1026 return -ERESTARTSYS;
1029 if (tty_port_users(port) == 1) {
1030 uart_shutdown(tty, state);
1033 * If we already have a port type configured,
1034 * we must release its resources.
1036 if (uport->type != PORT_UNKNOWN)
1037 uport->ops->release_port(uport);
1039 flags = UART_CONFIG_TYPE;
1040 if (uport->flags & UPF_AUTO_IRQ)
1041 flags |= UART_CONFIG_IRQ;
1044 * This will claim the ports resources if
1047 uport->ops->config_port(uport, flags);
1049 ret = uart_startup(tty, state, 1);
1051 mutex_unlock(&port->mutex);
1055 static void uart_enable_ms(struct uart_port *uport)
1058 * Force modem status interrupts on
1060 if (uport->ops->enable_ms)
1061 uport->ops->enable_ms(uport);
1065 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1066 * - mask passed in arg for lines of interest
1067 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1068 * Caller should use TIOCGICOUNT to see which one it was
1070 * FIXME: This wants extracting into a common all driver implementation
1071 * of TIOCMWAIT using tty_port.
1074 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1076 struct uart_port *uport = state->uart_port;
1077 struct tty_port *port = &state->port;
1078 DECLARE_WAITQUEUE(wait, current);
1079 struct uart_icount cprev, cnow;
1083 * note the counters on entry
1085 spin_lock_irq(&uport->lock);
1086 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1087 uart_enable_ms(uport);
1088 spin_unlock_irq(&uport->lock);
1090 add_wait_queue(&port->delta_msr_wait, &wait);
1092 spin_lock_irq(&uport->lock);
1093 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1094 spin_unlock_irq(&uport->lock);
1096 set_current_state(TASK_INTERRUPTIBLE);
1098 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1099 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1100 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1101 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1108 /* see if a signal did it */
1109 if (signal_pending(current)) {
1117 current->state = TASK_RUNNING;
1118 remove_wait_queue(&port->delta_msr_wait, &wait);
1124 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1125 * Return: write counters to the user passed counter struct
1126 * NB: both 1->0 and 0->1 transitions are counted except for
1127 * RI where only 0->1 is counted.
1129 static int uart_get_icount(struct tty_struct *tty,
1130 struct serial_icounter_struct *icount)
1132 struct uart_state *state = tty->driver_data;
1133 struct uart_icount cnow;
1134 struct uart_port *uport = state->uart_port;
1136 spin_lock_irq(&uport->lock);
1137 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1138 spin_unlock_irq(&uport->lock);
1140 icount->cts = cnow.cts;
1141 icount->dsr = cnow.dsr;
1142 icount->rng = cnow.rng;
1143 icount->dcd = cnow.dcd;
1144 icount->rx = cnow.rx;
1145 icount->tx = cnow.tx;
1146 icount->frame = cnow.frame;
1147 icount->overrun = cnow.overrun;
1148 icount->parity = cnow.parity;
1149 icount->brk = cnow.brk;
1150 icount->buf_overrun = cnow.buf_overrun;
1156 * Called via sys_ioctl. We can use spin_lock_irq() here.
1159 uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1162 struct uart_state *state = tty->driver_data;
1163 struct tty_port *port = &state->port;
1164 void __user *uarg = (void __user *)arg;
1165 int ret = -ENOIOCTLCMD;
1169 * These ioctls don't rely on the hardware to be present.
1173 ret = uart_get_info_user(port, uarg);
1177 ret = uart_set_info_user(tty, state, uarg);
1181 ret = uart_do_autoconfig(tty, state);
1184 case TIOCSERGWILD: /* obsolete */
1185 case TIOCSERSWILD: /* obsolete */
1190 if (ret != -ENOIOCTLCMD)
1193 if (tty->flags & (1 << TTY_IO_ERROR)) {
1199 * The following should only be used when hardware is present.
1203 ret = uart_wait_modem_status(state, arg);
1207 if (ret != -ENOIOCTLCMD)
1210 mutex_lock(&port->mutex);
1212 if (tty->flags & (1 << TTY_IO_ERROR)) {
1218 * All these rely on hardware being present and need to be
1219 * protected against the tty being hung up.
1222 case TIOCSERGETLSR: /* Get line status register */
1223 ret = uart_get_lsr_info(tty, state, uarg);
1227 struct uart_port *uport = state->uart_port;
1228 if (uport->ops->ioctl)
1229 ret = uport->ops->ioctl(uport, cmd, arg);
1234 mutex_unlock(&port->mutex);
1239 static void uart_set_ldisc(struct tty_struct *tty)
1241 struct uart_state *state = tty->driver_data;
1242 struct uart_port *uport = state->uart_port;
1244 if (uport->ops->set_ldisc)
1245 uport->ops->set_ldisc(uport, tty->termios.c_line);
1248 static void uart_set_termios(struct tty_struct *tty,
1249 struct ktermios *old_termios)
1251 struct uart_state *state = tty->driver_data;
1252 struct uart_port *uport = state->uart_port;
1253 unsigned long flags;
1254 unsigned int cflag = tty->termios.c_cflag;
1255 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1256 bool sw_changed = false;
1259 * Drivers doing software flow control also need to know
1260 * about changes to these input settings.
1262 if (uport->flags & UPF_SOFT_FLOW) {
1263 iflag_mask |= IXANY|IXON|IXOFF;
1265 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1266 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1270 * These are the bits that are used to setup various
1271 * flags in the low level driver. We can ignore the Bfoo
1272 * bits in c_cflag; c_[io]speed will always be set
1273 * appropriately by set_termios() in tty_ioctl.c
1275 if ((cflag ^ old_termios->c_cflag) == 0 &&
1276 tty->termios.c_ospeed == old_termios->c_ospeed &&
1277 tty->termios.c_ispeed == old_termios->c_ispeed &&
1278 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1283 uart_change_speed(tty, state, old_termios);
1284 /* reload cflag from termios; port driver may have overriden flags */
1285 cflag = tty->termios.c_cflag;
1287 /* Handle transition to B0 status */
1288 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1289 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1290 /* Handle transition away from B0 status */
1291 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1292 unsigned int mask = TIOCM_DTR;
1293 if (!(cflag & CRTSCTS) ||
1294 !test_bit(TTY_THROTTLED, &tty->flags))
1296 uart_set_mctrl(uport, mask);
1300 * If the port is doing h/w assisted flow control, do nothing.
1301 * We assume that tty->hw_stopped has never been set.
1303 if (uport->flags & UPF_HARD_FLOW)
1306 /* Handle turning off CRTSCTS */
1307 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1308 spin_lock_irqsave(&uport->lock, flags);
1309 tty->hw_stopped = 0;
1311 spin_unlock_irqrestore(&uport->lock, flags);
1313 /* Handle turning on CRTSCTS */
1314 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1315 spin_lock_irqsave(&uport->lock, flags);
1316 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) {
1317 tty->hw_stopped = 1;
1318 uport->ops->stop_tx(uport);
1320 spin_unlock_irqrestore(&uport->lock, flags);
1325 * Calls to uart_close() are serialised via the tty_lock in
1326 * drivers/tty/tty_io.c:tty_release()
1327 * drivers/tty/tty_io.c:do_tty_hangup()
1328 * This runs from a workqueue and can sleep for a _short_ time only.
1330 static void uart_close(struct tty_struct *tty, struct file *filp)
1332 struct uart_state *state = tty->driver_data;
1333 struct tty_port *port;
1334 struct uart_port *uport;
1335 unsigned long flags;
1340 uport = state->uart_port;
1341 port = &state->port;
1343 pr_debug("uart_close(%d) called\n", uport ? uport->line : -1);
1345 if (!port->count || tty_port_close_start(port, tty, filp) == 0)
1349 * At this point, we stop accepting input. To do this, we
1350 * disable the receive line status interrupts.
1352 if (port->flags & ASYNC_INITIALIZED) {
1353 unsigned long flags;
1354 spin_lock_irqsave(&uport->lock, flags);
1355 uport->ops->stop_rx(uport);
1356 spin_unlock_irqrestore(&uport->lock, flags);
1358 * Before we drop DTR, make sure the UART transmitter
1359 * has completely drained; this is especially
1360 * important if there is a transmit FIFO!
1362 uart_wait_until_sent(tty, uport->timeout);
1365 mutex_lock(&port->mutex);
1366 uart_shutdown(tty, state);
1367 uart_flush_buffer(tty);
1369 tty_ldisc_flush(tty);
1371 tty_port_tty_set(port, NULL);
1373 spin_lock_irqsave(&port->lock, flags);
1375 if (port->blocked_open) {
1376 spin_unlock_irqrestore(&port->lock, flags);
1377 if (port->close_delay)
1378 msleep_interruptible(
1379 jiffies_to_msecs(port->close_delay));
1380 spin_lock_irqsave(&port->lock, flags);
1381 } else if (!uart_console(uport)) {
1382 spin_unlock_irqrestore(&port->lock, flags);
1383 uart_change_pm(state, UART_PM_STATE_OFF);
1384 spin_lock_irqsave(&port->lock, flags);
1388 * Wake up anyone trying to open this port.
1390 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1391 clear_bit(ASYNCB_CLOSING, &port->flags);
1392 spin_unlock_irqrestore(&port->lock, flags);
1393 wake_up_interruptible(&port->open_wait);
1394 wake_up_interruptible(&port->close_wait);
1396 mutex_unlock(&port->mutex);
1399 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1401 struct uart_state *state = tty->driver_data;
1402 struct uart_port *port = state->uart_port;
1403 unsigned long char_time, expire;
1405 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1409 * Set the check interval to be 1/5 of the estimated time to
1410 * send a single character, and make it at least 1. The check
1411 * interval should also be less than the timeout.
1413 * Note: we have to use pretty tight timings here to satisfy
1416 char_time = (port->timeout - HZ/50) / port->fifosize;
1417 char_time = char_time / 5;
1420 if (timeout && timeout < char_time)
1421 char_time = timeout;
1424 * If the transmitter hasn't cleared in twice the approximate
1425 * amount of time to send the entire FIFO, it probably won't
1426 * ever clear. This assumes the UART isn't doing flow
1427 * control, which is currently the case. Hence, if it ever
1428 * takes longer than port->timeout, this is probably due to a
1429 * UART bug of some kind. So, we clamp the timeout parameter at
1432 if (timeout == 0 || timeout > 2 * port->timeout)
1433 timeout = 2 * port->timeout;
1435 expire = jiffies + timeout;
1437 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1438 port->line, jiffies, expire);
1441 * Check whether the transmitter is empty every 'char_time'.
1442 * 'timeout' / 'expire' give us the maximum amount of time
1445 while (!port->ops->tx_empty(port)) {
1446 msleep_interruptible(jiffies_to_msecs(char_time));
1447 if (signal_pending(current))
1449 if (time_after(jiffies, expire))
1455 * Calls to uart_hangup() are serialised by the tty_lock in
1456 * drivers/tty/tty_io.c:do_tty_hangup()
1457 * This runs from a workqueue and can sleep for a _short_ time only.
1459 static void uart_hangup(struct tty_struct *tty)
1461 struct uart_state *state = tty->driver_data;
1462 struct tty_port *port = &state->port;
1463 unsigned long flags;
1465 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1467 mutex_lock(&port->mutex);
1468 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1469 uart_flush_buffer(tty);
1470 uart_shutdown(tty, state);
1471 spin_lock_irqsave(&port->lock, flags);
1473 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1474 spin_unlock_irqrestore(&port->lock, flags);
1475 tty_port_tty_set(port, NULL);
1476 if (!uart_console(state->uart_port))
1477 uart_change_pm(state, UART_PM_STATE_OFF);
1478 wake_up_interruptible(&port->open_wait);
1479 wake_up_interruptible(&port->delta_msr_wait);
1481 mutex_unlock(&port->mutex);
1484 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1489 static void uart_port_shutdown(struct tty_port *port)
1491 struct uart_state *state = container_of(port, struct uart_state, port);
1492 struct uart_port *uport = state->uart_port;
1495 * clear delta_msr_wait queue to avoid mem leaks: we may free
1496 * the irq here so the queue might never be woken up. Note
1497 * that we won't end up waiting on delta_msr_wait again since
1498 * any outstanding file descriptors should be pointing at
1499 * hung_up_tty_fops now.
1501 wake_up_interruptible(&port->delta_msr_wait);
1504 * Free the IRQ and disable the port.
1506 uport->ops->shutdown(uport);
1509 * Ensure that the IRQ handler isn't running on another CPU.
1511 synchronize_irq(uport->irq);
1514 static int uart_carrier_raised(struct tty_port *port)
1516 struct uart_state *state = container_of(port, struct uart_state, port);
1517 struct uart_port *uport = state->uart_port;
1519 spin_lock_irq(&uport->lock);
1520 uart_enable_ms(uport);
1521 mctrl = uport->ops->get_mctrl(uport);
1522 spin_unlock_irq(&uport->lock);
1523 if (mctrl & TIOCM_CAR)
1528 static void uart_dtr_rts(struct tty_port *port, int onoff)
1530 struct uart_state *state = container_of(port, struct uart_state, port);
1531 struct uart_port *uport = state->uart_port;
1534 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1536 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1540 * Calls to uart_open are serialised by the tty_lock in
1541 * drivers/tty/tty_io.c:tty_open()
1542 * Note that if this fails, then uart_close() _will_ be called.
1544 * In time, we want to scrap the "opening nonpresent ports"
1545 * behaviour and implement an alternative way for setserial
1546 * to set base addresses/ports/types. This will allow us to
1547 * get rid of a certain amount of extra tests.
1549 static int uart_open(struct tty_struct *tty, struct file *filp)
1551 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1552 int retval, line = tty->index;
1553 struct uart_state *state = drv->state + line;
1554 struct tty_port *port = &state->port;
1556 pr_debug("uart_open(%d) called\n", line);
1559 * We take the semaphore here to guarantee that we won't be re-entered
1560 * while allocating the state structure, or while we request any IRQs
1561 * that the driver may need. This also has the nice side-effect that
1562 * it delays the action of uart_hangup, so we can guarantee that
1563 * state->port.tty will always contain something reasonable.
1565 if (mutex_lock_interruptible(&port->mutex)) {
1566 retval = -ERESTARTSYS;
1571 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1577 * Once we set tty->driver_data here, we are guaranteed that
1578 * uart_close() will decrement the driver module use count.
1579 * Any failures from here onwards should not touch the count.
1581 tty->driver_data = state;
1582 state->uart_port->state = state;
1583 state->port.low_latency =
1584 (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1585 tty_port_tty_set(port, tty);
1588 * Start up the serial port.
1590 retval = uart_startup(tty, state, 0);
1593 * If we succeeded, wait until the port is ready.
1595 mutex_unlock(&port->mutex);
1597 retval = tty_port_block_til_ready(port, tty, filp);
1603 mutex_unlock(&port->mutex);
1607 static const char *uart_type(struct uart_port *port)
1609 const char *str = NULL;
1611 if (port->ops->type)
1612 str = port->ops->type(port);
1620 #ifdef CONFIG_PROC_FS
1622 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1624 struct uart_state *state = drv->state + i;
1625 struct tty_port *port = &state->port;
1626 enum uart_pm_state pm_state;
1627 struct uart_port *uport = state->uart_port;
1629 unsigned int status;
1635 mmio = uport->iotype >= UPIO_MEM;
1636 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1637 uport->line, uart_type(uport),
1638 mmio ? "mmio:0x" : "port:",
1639 mmio ? (unsigned long long)uport->mapbase
1640 : (unsigned long long)uport->iobase,
1643 if (uport->type == PORT_UNKNOWN) {
1648 if (capable(CAP_SYS_ADMIN)) {
1649 mutex_lock(&port->mutex);
1650 pm_state = state->pm_state;
1651 if (pm_state != UART_PM_STATE_ON)
1652 uart_change_pm(state, UART_PM_STATE_ON);
1653 spin_lock_irq(&uport->lock);
1654 status = uport->ops->get_mctrl(uport);
1655 spin_unlock_irq(&uport->lock);
1656 if (pm_state != UART_PM_STATE_ON)
1657 uart_change_pm(state, pm_state);
1658 mutex_unlock(&port->mutex);
1660 seq_printf(m, " tx:%d rx:%d",
1661 uport->icount.tx, uport->icount.rx);
1662 if (uport->icount.frame)
1663 seq_printf(m, " fe:%d",
1664 uport->icount.frame);
1665 if (uport->icount.parity)
1666 seq_printf(m, " pe:%d",
1667 uport->icount.parity);
1668 if (uport->icount.brk)
1669 seq_printf(m, " brk:%d",
1671 if (uport->icount.overrun)
1672 seq_printf(m, " oe:%d",
1673 uport->icount.overrun);
1675 #define INFOBIT(bit, str) \
1676 if (uport->mctrl & (bit)) \
1677 strncat(stat_buf, (str), sizeof(stat_buf) - \
1678 strlen(stat_buf) - 2)
1679 #define STATBIT(bit, str) \
1680 if (status & (bit)) \
1681 strncat(stat_buf, (str), sizeof(stat_buf) - \
1682 strlen(stat_buf) - 2)
1686 INFOBIT(TIOCM_RTS, "|RTS");
1687 STATBIT(TIOCM_CTS, "|CTS");
1688 INFOBIT(TIOCM_DTR, "|DTR");
1689 STATBIT(TIOCM_DSR, "|DSR");
1690 STATBIT(TIOCM_CAR, "|CD");
1691 STATBIT(TIOCM_RNG, "|RI");
1695 seq_puts(m, stat_buf);
1702 static int uart_proc_show(struct seq_file *m, void *v)
1704 struct tty_driver *ttydrv = m->private;
1705 struct uart_driver *drv = ttydrv->driver_state;
1708 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1710 for (i = 0; i < drv->nr; i++)
1711 uart_line_info(m, drv, i);
1715 static int uart_proc_open(struct inode *inode, struct file *file)
1717 return single_open(file, uart_proc_show, PDE_DATA(inode));
1720 static const struct file_operations uart_proc_fops = {
1721 .owner = THIS_MODULE,
1722 .open = uart_proc_open,
1724 .llseek = seq_lseek,
1725 .release = single_release,
1729 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1731 * uart_console_write - write a console message to a serial port
1732 * @port: the port to write the message
1733 * @s: array of characters
1734 * @count: number of characters in string to write
1735 * @write: function to write character to port
1737 void uart_console_write(struct uart_port *port, const char *s,
1739 void (*putchar)(struct uart_port *, int))
1743 for (i = 0; i < count; i++, s++) {
1745 putchar(port, '\r');
1749 EXPORT_SYMBOL_GPL(uart_console_write);
1752 * Check whether an invalid uart number has been specified, and
1753 * if so, search for the first available port that does have
1756 struct uart_port * __init
1757 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1759 int idx = co->index;
1761 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1762 ports[idx].membase == NULL))
1763 for (idx = 0; idx < nr; idx++)
1764 if (ports[idx].iobase != 0 ||
1765 ports[idx].membase != NULL)
1774 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1775 * @options: pointer to option string
1776 * @baud: pointer to an 'int' variable for the baud rate.
1777 * @parity: pointer to an 'int' variable for the parity.
1778 * @bits: pointer to an 'int' variable for the number of data bits.
1779 * @flow: pointer to an 'int' variable for the flow control character.
1781 * uart_parse_options decodes a string containing the serial console
1782 * options. The format of the string is <baud><parity><bits><flow>,
1786 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1790 *baud = simple_strtoul(s, NULL, 10);
1791 while (*s >= '0' && *s <= '9')
1800 EXPORT_SYMBOL_GPL(uart_parse_options);
1807 static const struct baud_rates baud_rates[] = {
1808 { 921600, B921600 },
1809 { 460800, B460800 },
1810 { 230400, B230400 },
1811 { 115200, B115200 },
1823 * uart_set_options - setup the serial console parameters
1824 * @port: pointer to the serial ports uart_port structure
1825 * @co: console pointer
1827 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1828 * @bits: number of data bits
1829 * @flow: flow control character - 'r' (rts)
1832 uart_set_options(struct uart_port *port, struct console *co,
1833 int baud, int parity, int bits, int flow)
1835 struct ktermios termios;
1836 static struct ktermios dummy;
1840 * Ensure that the serial console lock is initialised
1842 * If this port is a console, then the spinlock is already
1845 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1846 spin_lock_init(&port->lock);
1847 lockdep_set_class(&port->lock, &port_lock_key);
1850 memset(&termios, 0, sizeof(struct ktermios));
1852 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1855 * Construct a cflag setting.
1857 for (i = 0; baud_rates[i].rate; i++)
1858 if (baud_rates[i].rate <= baud)
1861 termios.c_cflag |= baud_rates[i].cflag;
1864 termios.c_cflag |= CS7;
1866 termios.c_cflag |= CS8;
1870 termios.c_cflag |= PARODD;
1873 termios.c_cflag |= PARENB;
1878 termios.c_cflag |= CRTSCTS;
1881 * some uarts on other side don't support no flow control.
1882 * So we set * DTR in host uart to make them happy
1884 port->mctrl |= TIOCM_DTR;
1886 port->ops->set_termios(port, &termios, &dummy);
1888 * Allow the setting of the UART parameters with a NULL console
1892 co->cflag = termios.c_cflag;
1896 EXPORT_SYMBOL_GPL(uart_set_options);
1897 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1900 * uart_change_pm - set power state of the port
1902 * @state: port descriptor
1903 * @pm_state: new state
1905 * Locking: port->mutex has to be held
1907 static void uart_change_pm(struct uart_state *state,
1908 enum uart_pm_state pm_state)
1910 struct uart_port *port = state->uart_port;
1912 if (state->pm_state != pm_state) {
1914 port->ops->pm(port, pm_state, state->pm_state);
1915 state->pm_state = pm_state;
1920 struct uart_port *port;
1921 struct uart_driver *driver;
1924 static int serial_match_port(struct device *dev, void *data)
1926 struct uart_match *match = data;
1927 struct tty_driver *tty_drv = match->driver->tty_driver;
1928 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1931 return dev->devt == devt; /* Actually, only one tty per port */
1934 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1936 struct uart_state *state = drv->state + uport->line;
1937 struct tty_port *port = &state->port;
1938 struct device *tty_dev;
1939 struct uart_match match = {uport, drv};
1941 mutex_lock(&port->mutex);
1943 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1944 if (device_may_wakeup(tty_dev)) {
1945 if (!enable_irq_wake(uport->irq))
1946 uport->irq_wake = 1;
1947 put_device(tty_dev);
1948 mutex_unlock(&port->mutex);
1951 put_device(tty_dev);
1953 if (console_suspend_enabled || !uart_console(uport))
1954 uport->suspended = 1;
1956 if (port->flags & ASYNC_INITIALIZED) {
1957 const struct uart_ops *ops = uport->ops;
1960 if (console_suspend_enabled || !uart_console(uport)) {
1961 set_bit(ASYNCB_SUSPENDED, &port->flags);
1962 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1964 spin_lock_irq(&uport->lock);
1965 ops->stop_tx(uport);
1966 ops->set_mctrl(uport, 0);
1967 ops->stop_rx(uport);
1968 spin_unlock_irq(&uport->lock);
1972 * Wait for the transmitter to empty.
1974 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1977 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1979 uport->dev ? dev_name(uport->dev) : "",
1980 uport->dev ? ": " : "",
1982 drv->tty_driver->name_base + uport->line);
1984 if (console_suspend_enabled || !uart_console(uport))
1985 ops->shutdown(uport);
1989 * Disable the console device before suspending.
1991 if (console_suspend_enabled && uart_console(uport))
1992 console_stop(uport->cons);
1994 if (console_suspend_enabled || !uart_console(uport))
1995 uart_change_pm(state, UART_PM_STATE_OFF);
1997 mutex_unlock(&port->mutex);
2002 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2004 struct uart_state *state = drv->state + uport->line;
2005 struct tty_port *port = &state->port;
2006 struct device *tty_dev;
2007 struct uart_match match = {uport, drv};
2008 struct ktermios termios;
2010 mutex_lock(&port->mutex);
2012 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2013 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2014 if (uport->irq_wake) {
2015 disable_irq_wake(uport->irq);
2016 uport->irq_wake = 0;
2018 put_device(tty_dev);
2019 mutex_unlock(&port->mutex);
2022 put_device(tty_dev);
2023 uport->suspended = 0;
2026 * Re-enable the console device after suspending.
2028 if (uart_console(uport)) {
2030 * First try to use the console cflag setting.
2032 memset(&termios, 0, sizeof(struct ktermios));
2033 termios.c_cflag = uport->cons->cflag;
2036 * If that's unset, use the tty termios setting.
2038 if (port->tty && termios.c_cflag == 0)
2039 termios = port->tty->termios;
2041 if (console_suspend_enabled)
2042 uart_change_pm(state, UART_PM_STATE_ON);
2043 uport->ops->set_termios(uport, &termios, NULL);
2044 if (console_suspend_enabled)
2045 console_start(uport->cons);
2048 if (port->flags & ASYNC_SUSPENDED) {
2049 const struct uart_ops *ops = uport->ops;
2052 uart_change_pm(state, UART_PM_STATE_ON);
2053 spin_lock_irq(&uport->lock);
2054 ops->set_mctrl(uport, 0);
2055 spin_unlock_irq(&uport->lock);
2056 if (console_suspend_enabled || !uart_console(uport)) {
2057 /* Protected by port mutex for now */
2058 struct tty_struct *tty = port->tty;
2059 ret = ops->startup(uport);
2062 uart_change_speed(tty, state, NULL);
2063 spin_lock_irq(&uport->lock);
2064 ops->set_mctrl(uport, uport->mctrl);
2065 ops->start_tx(uport);
2066 spin_unlock_irq(&uport->lock);
2067 set_bit(ASYNCB_INITIALIZED, &port->flags);
2070 * Failed to resume - maybe hardware went away?
2071 * Clear the "initialized" flag so we won't try
2072 * to call the low level drivers shutdown method.
2074 uart_shutdown(tty, state);
2078 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2081 mutex_unlock(&port->mutex);
2087 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2091 switch (port->iotype) {
2093 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2096 snprintf(address, sizeof(address),
2097 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2103 snprintf(address, sizeof(address),
2104 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2107 strlcpy(address, "*unknown*", sizeof(address));
2111 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2112 port->dev ? dev_name(port->dev) : "",
2113 port->dev ? ": " : "",
2115 drv->tty_driver->name_base + port->line,
2116 address, port->irq, port->uartclk / 16, uart_type(port));
2120 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2121 struct uart_port *port)
2126 * If there isn't a port here, don't do anything further.
2128 if (!port->iobase && !port->mapbase && !port->membase)
2132 * Now do the auto configuration stuff. Note that config_port
2133 * is expected to claim the resources and map the port for us.
2136 if (port->flags & UPF_AUTO_IRQ)
2137 flags |= UART_CONFIG_IRQ;
2138 if (port->flags & UPF_BOOT_AUTOCONF) {
2139 if (!(port->flags & UPF_FIXED_TYPE)) {
2140 port->type = PORT_UNKNOWN;
2141 flags |= UART_CONFIG_TYPE;
2143 port->ops->config_port(port, flags);
2146 if (port->type != PORT_UNKNOWN) {
2147 unsigned long flags;
2149 uart_report_port(drv, port);
2151 /* Power up port for set_mctrl() */
2152 uart_change_pm(state, UART_PM_STATE_ON);
2155 * Ensure that the modem control lines are de-activated.
2156 * keep the DTR setting that is set in uart_set_options()
2157 * We probably don't need a spinlock around this, but
2159 spin_lock_irqsave(&port->lock, flags);
2160 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2161 spin_unlock_irqrestore(&port->lock, flags);
2164 * If this driver supports console, and it hasn't been
2165 * successfully registered yet, try to re-register it.
2166 * It may be that the port was not available.
2168 if (port->cons && !(port->cons->flags & CON_ENABLED))
2169 register_console(port->cons);
2172 * Power down all ports by default, except the
2173 * console if we have one.
2175 if (!uart_console(port))
2176 uart_change_pm(state, UART_PM_STATE_OFF);
2180 #ifdef CONFIG_CONSOLE_POLL
2182 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2184 struct uart_driver *drv = driver->driver_state;
2185 struct uart_state *state = drv->state + line;
2186 struct uart_port *port;
2193 if (!state || !state->uart_port)
2196 port = state->uart_port;
2197 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2200 if (port->ops->poll_init) {
2201 struct tty_port *tport = &state->port;
2204 mutex_lock(&tport->mutex);
2206 * We don't set ASYNCB_INITIALIZED as we only initialized the
2207 * hw, e.g. state->xmit is still uninitialized.
2209 if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
2210 ret = port->ops->poll_init(port);
2211 mutex_unlock(&tport->mutex);
2217 uart_parse_options(options, &baud, &parity, &bits, &flow);
2218 return uart_set_options(port, NULL, baud, parity, bits, flow);
2224 static int uart_poll_get_char(struct tty_driver *driver, int line)
2226 struct uart_driver *drv = driver->driver_state;
2227 struct uart_state *state = drv->state + line;
2228 struct uart_port *port;
2230 if (!state || !state->uart_port)
2233 port = state->uart_port;
2234 return port->ops->poll_get_char(port);
2237 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2239 struct uart_driver *drv = driver->driver_state;
2240 struct uart_state *state = drv->state + line;
2241 struct uart_port *port;
2243 if (!state || !state->uart_port)
2246 port = state->uart_port;
2249 port->ops->poll_put_char(port, '\r');
2250 port->ops->poll_put_char(port, ch);
2254 static const struct tty_operations uart_ops = {
2256 .close = uart_close,
2257 .write = uart_write,
2258 .put_char = uart_put_char,
2259 .flush_chars = uart_flush_chars,
2260 .write_room = uart_write_room,
2261 .chars_in_buffer= uart_chars_in_buffer,
2262 .flush_buffer = uart_flush_buffer,
2263 .ioctl = uart_ioctl,
2264 .throttle = uart_throttle,
2265 .unthrottle = uart_unthrottle,
2266 .send_xchar = uart_send_xchar,
2267 .set_termios = uart_set_termios,
2268 .set_ldisc = uart_set_ldisc,
2270 .start = uart_start,
2271 .hangup = uart_hangup,
2272 .break_ctl = uart_break_ctl,
2273 .wait_until_sent= uart_wait_until_sent,
2274 #ifdef CONFIG_PROC_FS
2275 .proc_fops = &uart_proc_fops,
2277 .tiocmget = uart_tiocmget,
2278 .tiocmset = uart_tiocmset,
2279 .get_icount = uart_get_icount,
2280 #ifdef CONFIG_CONSOLE_POLL
2281 .poll_init = uart_poll_init,
2282 .poll_get_char = uart_poll_get_char,
2283 .poll_put_char = uart_poll_put_char,
2287 static const struct tty_port_operations uart_port_ops = {
2288 .activate = uart_port_activate,
2289 .shutdown = uart_port_shutdown,
2290 .carrier_raised = uart_carrier_raised,
2291 .dtr_rts = uart_dtr_rts,
2295 * uart_register_driver - register a driver with the uart core layer
2296 * @drv: low level driver structure
2298 * Register a uart driver with the core driver. We in turn register
2299 * with the tty layer, and initialise the core driver per-port state.
2301 * We have a proc file in /proc/tty/driver which is named after the
2304 * drv->port should be NULL, and the per-port structures should be
2305 * registered using uart_add_one_port after this call has succeeded.
2307 int uart_register_driver(struct uart_driver *drv)
2309 struct tty_driver *normal;
2315 * Maybe we should be using a slab cache for this, especially if
2316 * we have a large number of ports to handle.
2318 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2322 normal = alloc_tty_driver(drv->nr);
2326 drv->tty_driver = normal;
2328 normal->driver_name = drv->driver_name;
2329 normal->name = drv->dev_name;
2330 normal->major = drv->major;
2331 normal->minor_start = drv->minor;
2332 normal->type = TTY_DRIVER_TYPE_SERIAL;
2333 normal->subtype = SERIAL_TYPE_NORMAL;
2334 normal->init_termios = tty_std_termios;
2335 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2336 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2337 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2338 normal->driver_state = drv;
2339 tty_set_operations(normal, &uart_ops);
2342 * Initialise the UART state(s).
2344 for (i = 0; i < drv->nr; i++) {
2345 struct uart_state *state = drv->state + i;
2346 struct tty_port *port = &state->port;
2348 tty_port_init(port);
2349 port->ops = &uart_port_ops;
2350 port->close_delay = HZ / 2; /* .5 seconds */
2351 port->closing_wait = 30 * HZ;/* 30 seconds */
2354 retval = tty_register_driver(normal);
2358 for (i = 0; i < drv->nr; i++)
2359 tty_port_destroy(&drv->state[i].port);
2360 put_tty_driver(normal);
2368 * uart_unregister_driver - remove a driver from the uart core layer
2369 * @drv: low level driver structure
2371 * Remove all references to a driver from the core driver. The low
2372 * level driver must have removed all its ports via the
2373 * uart_remove_one_port() if it registered them with uart_add_one_port().
2374 * (ie, drv->port == NULL)
2376 void uart_unregister_driver(struct uart_driver *drv)
2378 struct tty_driver *p = drv->tty_driver;
2381 tty_unregister_driver(p);
2383 for (i = 0; i < drv->nr; i++)
2384 tty_port_destroy(&drv->state[i].port);
2387 drv->tty_driver = NULL;
2390 struct tty_driver *uart_console_device(struct console *co, int *index)
2392 struct uart_driver *p = co->data;
2394 return p->tty_driver;
2397 static ssize_t uart_get_attr_uartclk(struct device *dev,
2398 struct device_attribute *attr, char *buf)
2400 struct serial_struct tmp;
2401 struct tty_port *port = dev_get_drvdata(dev);
2403 uart_get_info(port, &tmp);
2404 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2407 static ssize_t uart_get_attr_type(struct device *dev,
2408 struct device_attribute *attr, char *buf)
2410 struct serial_struct tmp;
2411 struct tty_port *port = dev_get_drvdata(dev);
2413 uart_get_info(port, &tmp);
2414 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2416 static ssize_t uart_get_attr_line(struct device *dev,
2417 struct device_attribute *attr, char *buf)
2419 struct serial_struct tmp;
2420 struct tty_port *port = dev_get_drvdata(dev);
2422 uart_get_info(port, &tmp);
2423 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2426 static ssize_t uart_get_attr_port(struct device *dev,
2427 struct device_attribute *attr, char *buf)
2429 struct serial_struct tmp;
2430 struct tty_port *port = dev_get_drvdata(dev);
2431 unsigned long ioaddr;
2433 uart_get_info(port, &tmp);
2435 if (HIGH_BITS_OFFSET)
2436 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2437 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2440 static ssize_t uart_get_attr_irq(struct device *dev,
2441 struct device_attribute *attr, char *buf)
2443 struct serial_struct tmp;
2444 struct tty_port *port = dev_get_drvdata(dev);
2446 uart_get_info(port, &tmp);
2447 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2450 static ssize_t uart_get_attr_flags(struct device *dev,
2451 struct device_attribute *attr, char *buf)
2453 struct serial_struct tmp;
2454 struct tty_port *port = dev_get_drvdata(dev);
2456 uart_get_info(port, &tmp);
2457 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2460 static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2461 struct device_attribute *attr, char *buf)
2463 struct serial_struct tmp;
2464 struct tty_port *port = dev_get_drvdata(dev);
2466 uart_get_info(port, &tmp);
2467 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2471 static ssize_t uart_get_attr_close_delay(struct device *dev,
2472 struct device_attribute *attr, char *buf)
2474 struct serial_struct tmp;
2475 struct tty_port *port = dev_get_drvdata(dev);
2477 uart_get_info(port, &tmp);
2478 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2482 static ssize_t uart_get_attr_closing_wait(struct device *dev,
2483 struct device_attribute *attr, char *buf)
2485 struct serial_struct tmp;
2486 struct tty_port *port = dev_get_drvdata(dev);
2488 uart_get_info(port, &tmp);
2489 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2492 static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2493 struct device_attribute *attr, char *buf)
2495 struct serial_struct tmp;
2496 struct tty_port *port = dev_get_drvdata(dev);
2498 uart_get_info(port, &tmp);
2499 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2502 static ssize_t uart_get_attr_io_type(struct device *dev,
2503 struct device_attribute *attr, char *buf)
2505 struct serial_struct tmp;
2506 struct tty_port *port = dev_get_drvdata(dev);
2508 uart_get_info(port, &tmp);
2509 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2512 static ssize_t uart_get_attr_iomem_base(struct device *dev,
2513 struct device_attribute *attr, char *buf)
2515 struct serial_struct tmp;
2516 struct tty_port *port = dev_get_drvdata(dev);
2518 uart_get_info(port, &tmp);
2519 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2522 static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2523 struct device_attribute *attr, char *buf)
2525 struct serial_struct tmp;
2526 struct tty_port *port = dev_get_drvdata(dev);
2528 uart_get_info(port, &tmp);
2529 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2532 static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2533 static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2534 static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2535 static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2536 static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2537 static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2538 static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2539 static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2540 static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2541 static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2542 static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2543 static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2544 static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2546 static struct attribute *tty_dev_attrs[] = {
2547 &dev_attr_type.attr,
2548 &dev_attr_line.attr,
2549 &dev_attr_port.attr,
2551 &dev_attr_flags.attr,
2552 &dev_attr_xmit_fifo_size.attr,
2553 &dev_attr_uartclk.attr,
2554 &dev_attr_close_delay.attr,
2555 &dev_attr_closing_wait.attr,
2556 &dev_attr_custom_divisor.attr,
2557 &dev_attr_io_type.attr,
2558 &dev_attr_iomem_base.attr,
2559 &dev_attr_iomem_reg_shift.attr,
2563 static const struct attribute_group tty_dev_attr_group = {
2564 .attrs = tty_dev_attrs,
2568 * uart_add_one_port - attach a driver-defined port structure
2569 * @drv: pointer to the uart low level driver structure for this port
2570 * @uport: uart port structure to use for this port.
2572 * This allows the driver to register its own uart_port structure
2573 * with the core driver. The main purpose is to allow the low
2574 * level uart drivers to expand uart_port, rather than having yet
2575 * more levels of structures.
2577 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2579 struct uart_state *state;
2580 struct tty_port *port;
2582 struct device *tty_dev;
2585 BUG_ON(in_interrupt());
2587 if (uport->line >= drv->nr)
2590 state = drv->state + uport->line;
2591 port = &state->port;
2593 mutex_lock(&port_mutex);
2594 mutex_lock(&port->mutex);
2595 if (state->uart_port) {
2600 state->uart_port = uport;
2601 state->pm_state = UART_PM_STATE_UNDEFINED;
2603 uport->cons = drv->cons;
2604 uport->state = state;
2607 * If this port is a console, then the spinlock is already
2610 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2611 spin_lock_init(&uport->lock);
2612 lockdep_set_class(&uport->lock, &port_lock_key);
2615 uart_configure_port(drv, state, uport);
2618 if (uport->attr_group)
2621 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2623 if (!uport->tty_groups) {
2627 uport->tty_groups[0] = &tty_dev_attr_group;
2628 if (uport->attr_group)
2629 uport->tty_groups[1] = uport->attr_group;
2632 * Register the port whether it's detected or not. This allows
2633 * setserial to be used to alter this port's parameters.
2635 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2636 uport->line, uport->dev, port, uport->tty_groups);
2637 if (likely(!IS_ERR(tty_dev))) {
2638 device_set_wakeup_capable(tty_dev, 1);
2640 printk(KERN_ERR "Cannot register tty device on line %d\n",
2645 * Ensure UPF_DEAD is not set.
2647 uport->flags &= ~UPF_DEAD;
2650 mutex_unlock(&port->mutex);
2651 mutex_unlock(&port_mutex);
2657 * uart_remove_one_port - detach a driver defined port structure
2658 * @drv: pointer to the uart low level driver structure for this port
2659 * @uport: uart port structure for this port
2661 * This unhooks (and hangs up) the specified port structure from the
2662 * core driver. No further calls will be made to the low-level code
2665 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2667 struct uart_state *state = drv->state + uport->line;
2668 struct tty_port *port = &state->port;
2669 struct tty_struct *tty;
2672 BUG_ON(in_interrupt());
2674 if (state->uart_port != uport)
2675 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2676 state->uart_port, uport);
2678 mutex_lock(&port_mutex);
2681 * Mark the port "dead" - this prevents any opens from
2682 * succeeding while we shut down the port.
2684 mutex_lock(&port->mutex);
2685 if (!state->uart_port) {
2686 mutex_unlock(&port->mutex);
2690 uport->flags |= UPF_DEAD;
2691 mutex_unlock(&port->mutex);
2694 * Remove the devices from the tty layer
2696 tty_unregister_device(drv->tty_driver, uport->line);
2698 tty = tty_port_tty_get(port);
2700 tty_vhangup(port->tty);
2705 * If the port is used as a console, unregister it
2707 if (uart_console(uport))
2708 unregister_console(uport->cons);
2711 * Free the port IO and memory resources, if any.
2713 if (uport->type != PORT_UNKNOWN)
2714 uport->ops->release_port(uport);
2715 kfree(uport->tty_groups);
2718 * Indicate that there isn't a port here anymore.
2720 uport->type = PORT_UNKNOWN;
2722 state->uart_port = NULL;
2724 mutex_unlock(&port_mutex);
2730 * Are the two ports equivalent?
2732 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2734 if (port1->iotype != port2->iotype)
2737 switch (port1->iotype) {
2739 return (port1->iobase == port2->iobase);
2741 return (port1->iobase == port2->iobase) &&
2742 (port1->hub6 == port2->hub6);
2747 return (port1->mapbase == port2->mapbase);
2751 EXPORT_SYMBOL(uart_match_port);
2754 * uart_handle_dcd_change - handle a change of carrier detect state
2755 * @uport: uart_port structure for the open port
2756 * @status: new carrier detect status, nonzero if active
2758 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2760 struct tty_port *port = &uport->state->port;
2761 struct tty_struct *tty = port->tty;
2762 struct tty_ldisc *ld = tty ? tty_ldisc_ref(tty) : NULL;
2765 if (ld->ops->dcd_change)
2766 ld->ops->dcd_change(tty, status);
2767 tty_ldisc_deref(ld);
2770 uport->icount.dcd++;
2772 if (port->flags & ASYNC_CHECK_CD) {
2774 wake_up_interruptible(&port->open_wait);
2779 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2782 * uart_handle_cts_change - handle a change of clear-to-send state
2783 * @uport: uart_port structure for the open port
2784 * @status: new clear to send status, nonzero if active
2786 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2788 struct tty_port *port = &uport->state->port;
2789 struct tty_struct *tty = port->tty;
2791 uport->icount.cts++;
2793 /* skip below code if the hw flow control is supported */
2794 if (tty_port_cts_enabled(port) &&
2795 !(uport->flags & UPF_HARD_FLOW)) {
2796 if (tty->hw_stopped) {
2798 tty->hw_stopped = 0;
2799 uport->ops->start_tx(uport);
2800 uart_write_wakeup(uport);
2804 tty->hw_stopped = 1;
2805 uport->ops->stop_tx(uport);
2810 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2813 * uart_insert_char - push a char to the uart layer
2815 * User is responsible to call tty_flip_buffer_push when they are done with
2818 * @port: corresponding port
2819 * @status: state of the serial port RX buffer (LSR for 8250)
2820 * @overrun: mask of overrun bits in @status
2821 * @ch: character to push
2822 * @flag: flag for the character (see TTY_NORMAL and friends)
2824 void uart_insert_char(struct uart_port *port, unsigned int status,
2825 unsigned int overrun, unsigned int ch, unsigned int flag)
2827 struct tty_port *tport = &port->state->port;
2829 if ((status & port->ignore_status_mask & ~overrun) == 0)
2830 if (tty_insert_flip_char(tport, ch, flag) == 0)
2831 ++port->icount.buf_overrun;
2834 * Overrun is special. Since it's reported immediately,
2835 * it doesn't affect the current character.
2837 if (status & ~port->ignore_status_mask & overrun)
2838 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
2839 ++port->icount.buf_overrun;
2841 EXPORT_SYMBOL_GPL(uart_insert_char);
2843 EXPORT_SYMBOL(uart_write_wakeup);
2844 EXPORT_SYMBOL(uart_register_driver);
2845 EXPORT_SYMBOL(uart_unregister_driver);
2846 EXPORT_SYMBOL(uart_suspend_port);
2847 EXPORT_SYMBOL(uart_resume_port);
2848 EXPORT_SYMBOL(uart_add_one_port);
2849 EXPORT_SYMBOL(uart_remove_one_port);
2851 MODULE_DESCRIPTION("Serial driver core");
2852 MODULE_LICENSE("GPL");