2 * linux/drivers/char/core.c
4 * Driver core for serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_core.h>
31 #include <linux/smp_lock.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
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 #define uart_users(state) ((state)->count + (state)->info.port.blocked_open)
55 #ifdef CONFIG_SERIAL_CORE_CONSOLE
56 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
58 #define uart_console(port) (0)
61 static void uart_change_speed(struct uart_state *state,
62 struct ktermios *old_termios);
63 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
64 static void uart_change_pm(struct uart_state *state, int pm_state);
67 * This routine is used by the interrupt handler to schedule processing in
68 * the software interrupt portion of the driver.
70 void uart_write_wakeup(struct uart_port *port)
72 struct uart_info *info = port->info;
74 * This means you called this function _after_ the port was
75 * closed. No cookie for you.
78 tasklet_schedule(&info->tlet);
81 static void uart_stop(struct tty_struct *tty)
83 struct uart_state *state = tty->driver_data;
84 struct uart_port *port = state->port;
87 spin_lock_irqsave(&port->lock, flags);
88 port->ops->stop_tx(port);
89 spin_unlock_irqrestore(&port->lock, flags);
92 static void __uart_start(struct tty_struct *tty)
94 struct uart_state *state = tty->driver_data;
95 struct uart_port *port = state->port;
97 if (!uart_circ_empty(&state->info.xmit) && state->info.xmit.buf &&
98 !tty->stopped && !tty->hw_stopped)
99 port->ops->start_tx(port);
102 static void uart_start(struct tty_struct *tty)
104 struct uart_state *state = tty->driver_data;
105 struct uart_port *port = state->port;
108 spin_lock_irqsave(&port->lock, flags);
110 spin_unlock_irqrestore(&port->lock, flags);
113 static void uart_tasklet_action(unsigned long data)
115 struct uart_state *state = (struct uart_state *)data;
116 tty_wakeup(state->info.port.tty);
120 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
125 spin_lock_irqsave(&port->lock, flags);
127 port->mctrl = (old & ~clear) | set;
128 if (old != port->mctrl)
129 port->ops->set_mctrl(port, port->mctrl);
130 spin_unlock_irqrestore(&port->lock, flags);
133 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
134 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
137 * Startup the port. This will be called once per open. All calls
138 * will be serialised by the per-port mutex.
140 static int uart_startup(struct uart_state *state, int init_hw)
142 struct uart_info *info = &state->info;
143 struct uart_port *port = state->port;
147 if (info->flags & UIF_INITIALIZED)
151 * Set the TTY IO error marker - we will only clear this
152 * once we have successfully opened the port. Also set
153 * up the tty->alt_speed kludge
155 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
157 if (port->type == PORT_UNKNOWN)
161 * Initialise and allocate the transmit and temporary
164 if (!info->xmit.buf) {
165 /* This is protected by the per port mutex */
166 page = get_zeroed_page(GFP_KERNEL);
170 info->xmit.buf = (unsigned char *) page;
171 uart_circ_clear(&info->xmit);
174 retval = port->ops->startup(port);
178 * Initialise the hardware port settings.
180 uart_change_speed(state, NULL);
183 * Setup the RTS and DTR signals once the
184 * port is open and ready to respond.
186 if (info->port.tty->termios->c_cflag & CBAUD)
187 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
190 if (info->flags & UIF_CTS_FLOW) {
191 spin_lock_irq(&port->lock);
192 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
193 info->port.tty->hw_stopped = 1;
194 spin_unlock_irq(&port->lock);
197 info->flags |= UIF_INITIALIZED;
199 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
202 if (retval && capable(CAP_SYS_ADMIN))
209 * This routine will shutdown a serial port; interrupts are disabled, and
210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore.
213 static void uart_shutdown(struct uart_state *state)
215 struct uart_info *info = &state->info;
216 struct uart_port *port = state->port;
217 struct tty_struct *tty = info->port.tty;
220 * Set the TTY IO error marker
223 set_bit(TTY_IO_ERROR, &tty->flags);
225 if (info->flags & UIF_INITIALIZED) {
226 info->flags &= ~UIF_INITIALIZED;
229 * Turn off DTR and RTS early.
231 if (!tty || (tty->termios->c_cflag & HUPCL))
232 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
235 * clear delta_msr_wait queue to avoid mem leaks: we may free
236 * the irq here so the queue might never be woken up. Note
237 * that we won't end up waiting on delta_msr_wait again since
238 * any outstanding file descriptors should be pointing at
239 * hung_up_tty_fops now.
241 wake_up_interruptible(&info->delta_msr_wait);
244 * Free the IRQ and disable the port.
246 port->ops->shutdown(port);
249 * Ensure that the IRQ handler isn't running on another CPU.
251 synchronize_irq(port->irq);
255 * kill off our tasklet
257 tasklet_kill(&info->tlet);
260 * Free the transmit buffer page.
262 if (info->xmit.buf) {
263 free_page((unsigned long)info->xmit.buf);
264 info->xmit.buf = NULL;
269 * uart_update_timeout - update per-port FIFO timeout.
270 * @port: uart_port structure describing the port
271 * @cflag: termios cflag value
272 * @baud: speed of the port
274 * Set the port FIFO timeout value. The @cflag value should
275 * reflect the actual hardware settings.
278 uart_update_timeout(struct uart_port *port, unsigned int cflag,
283 /* byte size and parity */
284 switch (cflag & CSIZE) {
305 * The total number of bits to be transmitted in the fifo.
307 bits = bits * port->fifosize;
310 * Figure the timeout to send the above number of bits.
311 * Add .02 seconds of slop
313 port->timeout = (HZ * bits) / baud + HZ/50;
316 EXPORT_SYMBOL(uart_update_timeout);
319 * uart_get_baud_rate - return baud rate for a particular port
320 * @port: uart_port structure describing the port in question.
321 * @termios: desired termios settings.
322 * @old: old termios (or NULL)
323 * @min: minimum acceptable baud rate
324 * @max: maximum acceptable baud rate
326 * Decode the termios structure into a numeric baud rate,
327 * taking account of the magic 38400 baud rate (with spd_*
328 * flags), and mapping the %B0 rate to 9600 baud.
330 * If the new baud rate is invalid, try the old termios setting.
331 * If it's still invalid, we try 9600 baud.
333 * Update the @termios structure to reflect the baud rate
334 * we're actually going to be using. Don't do this for the case
335 * where B0 is requested ("hang up").
338 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
339 struct ktermios *old, unsigned int min, unsigned int max)
341 unsigned int try, baud, altbaud = 38400;
343 upf_t flags = port->flags & UPF_SPD_MASK;
345 if (flags == UPF_SPD_HI)
347 if (flags == UPF_SPD_VHI)
349 if (flags == UPF_SPD_SHI)
351 if (flags == UPF_SPD_WARP)
354 for (try = 0; try < 2; try++) {
355 baud = tty_termios_baud_rate(termios);
358 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
365 * Special case: B0 rate.
372 if (baud >= min && baud <= max)
376 * Oops, the quotient was zero. Try again with
377 * the old baud rate if possible.
379 termios->c_cflag &= ~CBAUD;
381 baud = tty_termios_baud_rate(old);
383 tty_termios_encode_baud_rate(termios,
390 * As a last resort, if the quotient is zero,
391 * default to 9600 bps
394 tty_termios_encode_baud_rate(termios, 9600, 9600);
400 EXPORT_SYMBOL(uart_get_baud_rate);
403 * uart_get_divisor - return uart clock divisor
404 * @port: uart_port structure describing the port.
405 * @baud: desired baud rate
407 * Calculate the uart clock divisor for the port.
410 uart_get_divisor(struct uart_port *port, unsigned int baud)
415 * Old custom speed handling.
417 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
418 quot = port->custom_divisor;
420 quot = (port->uartclk + (8 * baud)) / (16 * baud);
425 EXPORT_SYMBOL(uart_get_divisor);
427 /* FIXME: Consistent locking policy */
429 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
431 struct tty_struct *tty = state->info.port.tty;
432 struct uart_port *port = state->port;
433 struct ktermios *termios;
436 * If we have no tty, termios, or the port does not exist,
437 * then we can't set the parameters for this port.
439 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
442 termios = tty->termios;
445 * Set flags based on termios cflag
447 if (termios->c_cflag & CRTSCTS)
448 state->info.flags |= UIF_CTS_FLOW;
450 state->info.flags &= ~UIF_CTS_FLOW;
452 if (termios->c_cflag & CLOCAL)
453 state->info.flags &= ~UIF_CHECK_CD;
455 state->info.flags |= UIF_CHECK_CD;
457 port->ops->set_termios(port, termios, old_termios);
461 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
469 spin_lock_irqsave(&port->lock, flags);
470 if (uart_circ_chars_free(circ) != 0) {
471 circ->buf[circ->head] = c;
472 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
475 spin_unlock_irqrestore(&port->lock, flags);
479 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
481 struct uart_state *state = tty->driver_data;
483 return __uart_put_char(state->port, &state->info.xmit, ch);
486 static void uart_flush_chars(struct tty_struct *tty)
492 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
494 struct uart_state *state = tty->driver_data;
495 struct uart_port *port;
496 struct circ_buf *circ;
501 * This means you called this function _after_ the port was
502 * closed. No cookie for you.
510 circ = &state->info.xmit;
515 spin_lock_irqsave(&port->lock, flags);
517 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
522 memcpy(circ->buf + circ->head, buf, c);
523 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
528 spin_unlock_irqrestore(&port->lock, flags);
534 static int uart_write_room(struct tty_struct *tty)
536 struct uart_state *state = tty->driver_data;
540 spin_lock_irqsave(&state->port->lock, flags);
541 ret = uart_circ_chars_free(&state->info.xmit);
542 spin_unlock_irqrestore(&state->port->lock, flags);
546 static int uart_chars_in_buffer(struct tty_struct *tty)
548 struct uart_state *state = tty->driver_data;
552 spin_lock_irqsave(&state->port->lock, flags);
553 ret = uart_circ_chars_pending(&state->info.xmit);
554 spin_unlock_irqrestore(&state->port->lock, flags);
558 static void uart_flush_buffer(struct tty_struct *tty)
560 struct uart_state *state = tty->driver_data;
561 struct uart_port *port;
565 * This means you called this function _after_ the port was
566 * closed. No cookie for you.
574 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
576 spin_lock_irqsave(&port->lock, flags);
577 uart_circ_clear(&state->info.xmit);
578 if (port->ops->flush_buffer)
579 port->ops->flush_buffer(port);
580 spin_unlock_irqrestore(&port->lock, flags);
585 * This function is used to send a high-priority XON/XOFF character to
588 static void uart_send_xchar(struct tty_struct *tty, char ch)
590 struct uart_state *state = tty->driver_data;
591 struct uart_port *port = state->port;
594 if (port->ops->send_xchar)
595 port->ops->send_xchar(port, ch);
599 spin_lock_irqsave(&port->lock, flags);
600 port->ops->start_tx(port);
601 spin_unlock_irqrestore(&port->lock, flags);
606 static void uart_throttle(struct tty_struct *tty)
608 struct uart_state *state = tty->driver_data;
611 uart_send_xchar(tty, STOP_CHAR(tty));
613 if (tty->termios->c_cflag & CRTSCTS)
614 uart_clear_mctrl(state->port, TIOCM_RTS);
617 static void uart_unthrottle(struct tty_struct *tty)
619 struct uart_state *state = tty->driver_data;
620 struct uart_port *port = state->port;
626 uart_send_xchar(tty, START_CHAR(tty));
629 if (tty->termios->c_cflag & CRTSCTS)
630 uart_set_mctrl(port, TIOCM_RTS);
633 static int uart_get_info(struct uart_state *state,
634 struct serial_struct __user *retinfo)
636 struct uart_port *port = state->port;
637 struct serial_struct tmp;
639 memset(&tmp, 0, sizeof(tmp));
641 /* Ensure the state we copy is consistent and no hardware changes
643 mutex_lock(&state->mutex);
645 tmp.type = port->type;
646 tmp.line = port->line;
647 tmp.port = port->iobase;
648 if (HIGH_BITS_OFFSET)
649 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
651 tmp.flags = port->flags;
652 tmp.xmit_fifo_size = port->fifosize;
653 tmp.baud_base = port->uartclk / 16;
654 tmp.close_delay = state->close_delay / 10;
655 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
656 ASYNC_CLOSING_WAIT_NONE :
657 state->closing_wait / 10;
658 tmp.custom_divisor = port->custom_divisor;
659 tmp.hub6 = port->hub6;
660 tmp.io_type = port->iotype;
661 tmp.iomem_reg_shift = port->regshift;
662 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
664 mutex_unlock(&state->mutex);
666 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
671 static int uart_set_info(struct uart_state *state,
672 struct serial_struct __user *newinfo)
674 struct serial_struct new_serial;
675 struct uart_port *port = state->port;
676 unsigned long new_port;
677 unsigned int change_irq, change_port, closing_wait;
678 unsigned int old_custom_divisor, close_delay;
679 upf_t old_flags, new_flags;
682 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
685 new_port = new_serial.port;
686 if (HIGH_BITS_OFFSET)
687 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
689 new_serial.irq = irq_canonicalize(new_serial.irq);
690 close_delay = new_serial.close_delay * 10;
691 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
692 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
695 * This semaphore protects state->count. It is also
696 * very useful to prevent opens. Also, take the
697 * port configuration semaphore to make sure that a
698 * module insertion/removal doesn't change anything
701 mutex_lock(&state->mutex);
703 change_irq = !(port->flags & UPF_FIXED_PORT)
704 && new_serial.irq != port->irq;
707 * Since changing the 'type' of the port changes its resource
708 * allocations, we should treat type changes the same as
711 change_port = !(port->flags & UPF_FIXED_PORT)
712 && (new_port != port->iobase ||
713 (unsigned long)new_serial.iomem_base != port->mapbase ||
714 new_serial.hub6 != port->hub6 ||
715 new_serial.io_type != port->iotype ||
716 new_serial.iomem_reg_shift != port->regshift ||
717 new_serial.type != port->type);
719 old_flags = port->flags;
720 new_flags = new_serial.flags;
721 old_custom_divisor = port->custom_divisor;
723 if (!capable(CAP_SYS_ADMIN)) {
725 if (change_irq || change_port ||
726 (new_serial.baud_base != port->uartclk / 16) ||
727 (close_delay != state->close_delay) ||
728 (closing_wait != state->closing_wait) ||
729 (new_serial.xmit_fifo_size &&
730 new_serial.xmit_fifo_size != port->fifosize) ||
731 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
733 port->flags = ((port->flags & ~UPF_USR_MASK) |
734 (new_flags & UPF_USR_MASK));
735 port->custom_divisor = new_serial.custom_divisor;
740 * Ask the low level driver to verify the settings.
742 if (port->ops->verify_port)
743 retval = port->ops->verify_port(port, &new_serial);
745 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
746 (new_serial.baud_base < 9600))
752 if (change_port || change_irq) {
756 * Make sure that we are the sole user of this port.
758 if (uart_users(state) > 1)
762 * We need to shutdown the serial port at the old
763 * port/type/irq combination.
765 uart_shutdown(state);
769 unsigned long old_iobase, old_mapbase;
770 unsigned int old_type, old_iotype, old_hub6, old_shift;
772 old_iobase = port->iobase;
773 old_mapbase = port->mapbase;
774 old_type = port->type;
775 old_hub6 = port->hub6;
776 old_iotype = port->iotype;
777 old_shift = port->regshift;
780 * Free and release old regions
782 if (old_type != PORT_UNKNOWN)
783 port->ops->release_port(port);
785 port->iobase = new_port;
786 port->type = new_serial.type;
787 port->hub6 = new_serial.hub6;
788 port->iotype = new_serial.io_type;
789 port->regshift = new_serial.iomem_reg_shift;
790 port->mapbase = (unsigned long)new_serial.iomem_base;
793 * Claim and map the new regions
795 if (port->type != PORT_UNKNOWN) {
796 retval = port->ops->request_port(port);
798 /* Always success - Jean II */
803 * If we fail to request resources for the
804 * new port, try to restore the old settings.
806 if (retval && old_type != PORT_UNKNOWN) {
807 port->iobase = old_iobase;
808 port->type = old_type;
809 port->hub6 = old_hub6;
810 port->iotype = old_iotype;
811 port->regshift = old_shift;
812 port->mapbase = old_mapbase;
813 retval = port->ops->request_port(port);
815 * If we failed to restore the old settings,
819 port->type = PORT_UNKNOWN;
825 /* Added to return the correct error -Ram Gupta */
831 port->irq = new_serial.irq;
832 if (!(port->flags & UPF_FIXED_PORT))
833 port->uartclk = new_serial.baud_base * 16;
834 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
835 (new_flags & UPF_CHANGE_MASK);
836 port->custom_divisor = new_serial.custom_divisor;
837 state->close_delay = close_delay;
838 state->closing_wait = closing_wait;
839 if (new_serial.xmit_fifo_size)
840 port->fifosize = new_serial.xmit_fifo_size;
841 if (state->info.port.tty)
842 state->info.port.tty->low_latency =
843 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
847 if (port->type == PORT_UNKNOWN)
849 if (state->info.flags & UIF_INITIALIZED) {
850 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
851 old_custom_divisor != port->custom_divisor) {
853 * If they're setting up a custom divisor or speed,
854 * instead of clearing it, then bitch about it. No
855 * need to rate-limit; it's CAP_SYS_ADMIN only.
857 if (port->flags & UPF_SPD_MASK) {
860 "%s sets custom speed on %s. This "
861 "is deprecated.\n", current->comm,
862 tty_name(state->info.port.tty, buf));
864 uart_change_speed(state, NULL);
867 retval = uart_startup(state, 1);
869 mutex_unlock(&state->mutex);
875 * uart_get_lsr_info - get line status register info.
876 * Note: uart_ioctl protects us against hangups.
878 static int uart_get_lsr_info(struct uart_state *state,
879 unsigned int __user *value)
881 struct uart_port *port = state->port;
884 result = port->ops->tx_empty(port);
887 * If we're about to load something into the transmit
888 * register, we'll pretend the transmitter isn't empty to
889 * avoid a race condition (depending on when the transmit
890 * interrupt happens).
893 ((uart_circ_chars_pending(&state->info.xmit) > 0) &&
894 !state->info.port.tty->stopped && !state->info.port.tty->hw_stopped))
895 result &= ~TIOCSER_TEMT;
897 return put_user(result, value);
900 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
902 struct uart_state *state = tty->driver_data;
903 struct uart_port *port = state->port;
906 mutex_lock(&state->mutex);
907 if ((!file || !tty_hung_up_p(file)) &&
908 !(tty->flags & (1 << TTY_IO_ERROR))) {
909 result = port->mctrl;
911 spin_lock_irq(&port->lock);
912 result |= port->ops->get_mctrl(port);
913 spin_unlock_irq(&port->lock);
915 mutex_unlock(&state->mutex);
921 uart_tiocmset(struct tty_struct *tty, struct file *file,
922 unsigned int set, unsigned int clear)
924 struct uart_state *state = tty->driver_data;
925 struct uart_port *port = state->port;
928 mutex_lock(&state->mutex);
929 if ((!file || !tty_hung_up_p(file)) &&
930 !(tty->flags & (1 << TTY_IO_ERROR))) {
931 uart_update_mctrl(port, set, clear);
934 mutex_unlock(&state->mutex);
938 static int uart_break_ctl(struct tty_struct *tty, int break_state)
940 struct uart_state *state = tty->driver_data;
941 struct uart_port *port = state->port;
943 mutex_lock(&state->mutex);
945 if (port->type != PORT_UNKNOWN)
946 port->ops->break_ctl(port, break_state);
948 mutex_unlock(&state->mutex);
952 static int uart_do_autoconfig(struct uart_state *state)
954 struct uart_port *port = state->port;
957 if (!capable(CAP_SYS_ADMIN))
961 * Take the per-port semaphore. This prevents count from
962 * changing, and hence any extra opens of the port while
963 * we're auto-configuring.
965 if (mutex_lock_interruptible(&state->mutex))
969 if (uart_users(state) == 1) {
970 uart_shutdown(state);
973 * If we already have a port type configured,
974 * we must release its resources.
976 if (port->type != PORT_UNKNOWN)
977 port->ops->release_port(port);
979 flags = UART_CONFIG_TYPE;
980 if (port->flags & UPF_AUTO_IRQ)
981 flags |= UART_CONFIG_IRQ;
984 * This will claim the ports resources if
987 port->ops->config_port(port, flags);
989 ret = uart_startup(state, 1);
991 mutex_unlock(&state->mutex);
996 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
997 * - mask passed in arg for lines of interest
998 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
999 * Caller should use TIOCGICOUNT to see which one it was
1002 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1004 struct uart_port *port = state->port;
1005 DECLARE_WAITQUEUE(wait, current);
1006 struct uart_icount cprev, cnow;
1010 * note the counters on entry
1012 spin_lock_irq(&port->lock);
1013 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1016 * Force modem status interrupts on
1018 port->ops->enable_ms(port);
1019 spin_unlock_irq(&port->lock);
1021 add_wait_queue(&state->info.delta_msr_wait, &wait);
1023 spin_lock_irq(&port->lock);
1024 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1025 spin_unlock_irq(&port->lock);
1027 set_current_state(TASK_INTERRUPTIBLE);
1029 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1030 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1031 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1032 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1039 /* see if a signal did it */
1040 if (signal_pending(current)) {
1048 current->state = TASK_RUNNING;
1049 remove_wait_queue(&state->info.delta_msr_wait, &wait);
1055 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1056 * Return: write counters to the user passed counter struct
1057 * NB: both 1->0 and 0->1 transitions are counted except for
1058 * RI where only 0->1 is counted.
1060 static int uart_get_count(struct uart_state *state,
1061 struct serial_icounter_struct __user *icnt)
1063 struct serial_icounter_struct icount;
1064 struct uart_icount cnow;
1065 struct uart_port *port = state->port;
1067 spin_lock_irq(&port->lock);
1068 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1069 spin_unlock_irq(&port->lock);
1071 icount.cts = cnow.cts;
1072 icount.dsr = cnow.dsr;
1073 icount.rng = cnow.rng;
1074 icount.dcd = cnow.dcd;
1075 icount.rx = cnow.rx;
1076 icount.tx = cnow.tx;
1077 icount.frame = cnow.frame;
1078 icount.overrun = cnow.overrun;
1079 icount.parity = cnow.parity;
1080 icount.brk = cnow.brk;
1081 icount.buf_overrun = cnow.buf_overrun;
1083 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1087 * Called via sys_ioctl. We can use spin_lock_irq() here.
1090 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1093 struct uart_state *state = tty->driver_data;
1094 void __user *uarg = (void __user *)arg;
1095 int ret = -ENOIOCTLCMD;
1099 * These ioctls don't rely on the hardware to be present.
1103 ret = uart_get_info(state, uarg);
1107 ret = uart_set_info(state, uarg);
1111 ret = uart_do_autoconfig(state);
1114 case TIOCSERGWILD: /* obsolete */
1115 case TIOCSERSWILD: /* obsolete */
1120 if (ret != -ENOIOCTLCMD)
1123 if (tty->flags & (1 << TTY_IO_ERROR)) {
1129 * The following should only be used when hardware is present.
1133 ret = uart_wait_modem_status(state, arg);
1137 ret = uart_get_count(state, uarg);
1141 if (ret != -ENOIOCTLCMD)
1144 mutex_lock(&state->mutex);
1146 if (tty_hung_up_p(filp)) {
1152 * All these rely on hardware being present and need to be
1153 * protected against the tty being hung up.
1156 case TIOCSERGETLSR: /* Get line status register */
1157 ret = uart_get_lsr_info(state, uarg);
1161 struct uart_port *port = state->port;
1162 if (port->ops->ioctl)
1163 ret = port->ops->ioctl(port, cmd, arg);
1168 mutex_unlock(&state->mutex);
1173 static void uart_set_ldisc(struct tty_struct *tty)
1175 struct uart_state *state = tty->driver_data;
1176 struct uart_port *port = state->port;
1178 if (port->ops->set_ldisc)
1179 port->ops->set_ldisc(port);
1182 static void uart_set_termios(struct tty_struct *tty,
1183 struct ktermios *old_termios)
1185 struct uart_state *state = tty->driver_data;
1186 unsigned long flags;
1187 unsigned int cflag = tty->termios->c_cflag;
1191 * These are the bits that are used to setup various
1192 * flags in the low level driver. We can ignore the Bfoo
1193 * bits in c_cflag; c_[io]speed will always be set
1194 * appropriately by set_termios() in tty_ioctl.c
1196 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1197 if ((cflag ^ old_termios->c_cflag) == 0 &&
1198 tty->termios->c_ospeed == old_termios->c_ospeed &&
1199 tty->termios->c_ispeed == old_termios->c_ispeed &&
1200 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1204 uart_change_speed(state, old_termios);
1206 /* Handle transition to B0 status */
1207 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1208 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1210 /* Handle transition away from B0 status */
1211 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1212 unsigned int mask = TIOCM_DTR;
1213 if (!(cflag & CRTSCTS) ||
1214 !test_bit(TTY_THROTTLED, &tty->flags))
1216 uart_set_mctrl(state->port, mask);
1219 /* Handle turning off CRTSCTS */
1220 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1221 spin_lock_irqsave(&state->port->lock, flags);
1222 tty->hw_stopped = 0;
1224 spin_unlock_irqrestore(&state->port->lock, flags);
1227 /* Handle turning on CRTSCTS */
1228 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1229 spin_lock_irqsave(&state->port->lock, flags);
1230 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1231 tty->hw_stopped = 1;
1232 state->port->ops->stop_tx(state->port);
1234 spin_unlock_irqrestore(&state->port->lock, flags);
1238 * No need to wake up processes in open wait, since they
1239 * sample the CLOCAL flag once, and don't recheck it.
1240 * XXX It's not clear whether the current behavior is correct
1241 * or not. Hence, this may change.....
1243 if (!(old_termios->c_cflag & CLOCAL) &&
1244 (tty->termios->c_cflag & CLOCAL))
1245 wake_up_interruptible(&info->port.open_wait);
1250 * In 2.4.5, calls to this will be serialized via the BKL in
1251 * linux/drivers/char/tty_io.c:tty_release()
1252 * linux/drivers/char/tty_io.c:do_tty_handup()
1254 static void uart_close(struct tty_struct *tty, struct file *filp)
1256 struct uart_state *state = tty->driver_data;
1257 struct uart_port *port;
1259 BUG_ON(!kernel_locked());
1261 if (!state || !state->port)
1266 pr_debug("uart_close(%d) called\n", port->line);
1268 mutex_lock(&state->mutex);
1270 if (tty_hung_up_p(filp))
1273 if ((tty->count == 1) && (state->count != 1)) {
1275 * Uh, oh. tty->count is 1, which means that the tty
1276 * structure will be freed. state->count should always
1277 * be one in these conditions. If it's greater than
1278 * one, we've got real problems, since it means the
1279 * serial port won't be shutdown.
1281 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1282 "state->count is %d\n", state->count);
1285 if (--state->count < 0) {
1286 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1287 tty->name, state->count);
1294 * Now we wait for the transmit buffer to clear; and we notify
1295 * the line discipline to only process XON/XOFF characters by
1296 * setting tty->closing.
1300 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1301 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1304 * At this point, we stop accepting input. To do this, we
1305 * disable the receive line status interrupts.
1307 if (state->info.flags & UIF_INITIALIZED) {
1308 unsigned long flags;
1309 spin_lock_irqsave(&port->lock, flags);
1310 port->ops->stop_rx(port);
1311 spin_unlock_irqrestore(&port->lock, flags);
1313 * Before we drop DTR, make sure the UART transmitter
1314 * has completely drained; this is especially
1315 * important if there is a transmit FIFO!
1317 uart_wait_until_sent(tty, port->timeout);
1320 uart_shutdown(state);
1321 uart_flush_buffer(tty);
1323 tty_ldisc_flush(tty);
1326 state->info.port.tty = NULL;
1328 if (state->info.port.blocked_open) {
1329 if (state->close_delay)
1330 msleep_interruptible(state->close_delay);
1331 } else if (!uart_console(port)) {
1332 uart_change_pm(state, 3);
1336 * Wake up anyone trying to open this port.
1338 state->info.flags &= ~UIF_NORMAL_ACTIVE;
1339 wake_up_interruptible(&state->info.port.open_wait);
1342 mutex_unlock(&state->mutex);
1345 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1347 struct uart_state *state = tty->driver_data;
1348 struct uart_port *port = state->port;
1349 unsigned long char_time, expire;
1351 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1357 * Set the check interval to be 1/5 of the estimated time to
1358 * send a single character, and make it at least 1. The check
1359 * interval should also be less than the timeout.
1361 * Note: we have to use pretty tight timings here to satisfy
1364 char_time = (port->timeout - HZ/50) / port->fifosize;
1365 char_time = char_time / 5;
1368 if (timeout && timeout < char_time)
1369 char_time = timeout;
1372 * If the transmitter hasn't cleared in twice the approximate
1373 * amount of time to send the entire FIFO, it probably won't
1374 * ever clear. This assumes the UART isn't doing flow
1375 * control, which is currently the case. Hence, if it ever
1376 * takes longer than port->timeout, this is probably due to a
1377 * UART bug of some kind. So, we clamp the timeout parameter at
1380 if (timeout == 0 || timeout > 2 * port->timeout)
1381 timeout = 2 * port->timeout;
1383 expire = jiffies + timeout;
1385 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1386 port->line, jiffies, expire);
1389 * Check whether the transmitter is empty every 'char_time'.
1390 * 'timeout' / 'expire' give us the maximum amount of time
1393 while (!port->ops->tx_empty(port)) {
1394 msleep_interruptible(jiffies_to_msecs(char_time));
1395 if (signal_pending(current))
1397 if (time_after(jiffies, expire))
1400 set_current_state(TASK_RUNNING); /* might not be needed */
1405 * This is called with the BKL held in
1406 * linux/drivers/char/tty_io.c:do_tty_hangup()
1407 * We're called from the eventd thread, so we can sleep for
1408 * a _short_ time only.
1410 static void uart_hangup(struct tty_struct *tty)
1412 struct uart_state *state = tty->driver_data;
1413 struct uart_info *info = &state->info;
1415 BUG_ON(!kernel_locked());
1416 pr_debug("uart_hangup(%d)\n", state->port->line);
1418 mutex_lock(&state->mutex);
1419 if (info->flags & UIF_NORMAL_ACTIVE) {
1420 uart_flush_buffer(tty);
1421 uart_shutdown(state);
1423 info->flags &= ~UIF_NORMAL_ACTIVE;
1424 info->port.tty = NULL;
1425 wake_up_interruptible(&info->port.open_wait);
1426 wake_up_interruptible(&info->delta_msr_wait);
1428 mutex_unlock(&state->mutex);
1432 * Copy across the serial console cflag setting into the termios settings
1433 * for the initial open of the port. This allows continuity between the
1434 * kernel settings, and the settings init adopts when it opens the port
1435 * for the first time.
1437 static void uart_update_termios(struct uart_state *state)
1439 struct tty_struct *tty = state->info.port.tty;
1440 struct uart_port *port = state->port;
1442 if (uart_console(port) && port->cons->cflag) {
1443 tty->termios->c_cflag = port->cons->cflag;
1444 port->cons->cflag = 0;
1448 * If the device failed to grab its irq resources,
1449 * or some other error occurred, don't try to talk
1450 * to the port hardware.
1452 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1454 * Make termios settings take effect.
1456 uart_change_speed(state, NULL);
1459 * And finally enable the RTS and DTR signals.
1461 if (tty->termios->c_cflag & CBAUD)
1462 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1467 * Block the open until the port is ready. We must be called with
1468 * the per-port semaphore held.
1471 uart_block_til_ready(struct file *filp, struct uart_state *state)
1473 DECLARE_WAITQUEUE(wait, current);
1474 struct uart_info *info = &state->info;
1475 struct uart_port *port = state->port;
1478 info->port.blocked_open++;
1481 add_wait_queue(&info->port.open_wait, &wait);
1483 set_current_state(TASK_INTERRUPTIBLE);
1486 * If we have been hung up, tell userspace/restart open.
1488 if (tty_hung_up_p(filp) || info->port.tty == NULL)
1492 * If the port has been closed, tell userspace/restart open.
1494 if (!(info->flags & UIF_INITIALIZED))
1498 * If non-blocking mode is set, or CLOCAL mode is set,
1499 * we don't want to wait for the modem status lines to
1500 * indicate that the port is ready.
1502 * Also, if the port is not enabled/configured, we want
1503 * to allow the open to succeed here. Note that we will
1504 * have set TTY_IO_ERROR for a non-existant port.
1506 if ((filp->f_flags & O_NONBLOCK) ||
1507 (info->port.tty->termios->c_cflag & CLOCAL) ||
1508 (info->port.tty->flags & (1 << TTY_IO_ERROR)))
1512 * Set DTR to allow modem to know we're waiting. Do
1513 * not set RTS here - we want to make sure we catch
1514 * the data from the modem.
1516 if (info->port.tty->termios->c_cflag & CBAUD)
1517 uart_set_mctrl(port, TIOCM_DTR);
1520 * and wait for the carrier to indicate that the
1521 * modem is ready for us.
1523 spin_lock_irq(&port->lock);
1524 port->ops->enable_ms(port);
1525 mctrl = port->ops->get_mctrl(port);
1526 spin_unlock_irq(&port->lock);
1527 if (mctrl & TIOCM_CAR)
1530 mutex_unlock(&state->mutex);
1532 mutex_lock(&state->mutex);
1534 if (signal_pending(current))
1537 set_current_state(TASK_RUNNING);
1538 remove_wait_queue(&info->port.open_wait, &wait);
1541 info->port.blocked_open--;
1543 if (signal_pending(current))
1544 return -ERESTARTSYS;
1546 if (!info->port.tty || tty_hung_up_p(filp))
1552 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1554 struct uart_state *state;
1557 state = drv->state + line;
1558 if (mutex_lock_interruptible(&state->mutex)) {
1564 if (!state->port || state->port->flags & UPF_DEAD) {
1572 mutex_unlock(&state->mutex);
1574 return ERR_PTR(ret);
1578 * calls to uart_open are serialised by the BKL in
1579 * fs/char_dev.c:chrdev_open()
1580 * Note that if this fails, then uart_close() _will_ be called.
1582 * In time, we want to scrap the "opening nonpresent ports"
1583 * behaviour and implement an alternative way for setserial
1584 * to set base addresses/ports/types. This will allow us to
1585 * get rid of a certain amount of extra tests.
1587 static int uart_open(struct tty_struct *tty, struct file *filp)
1589 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1590 struct uart_state *state;
1591 int retval, line = tty->index;
1593 BUG_ON(!kernel_locked());
1594 pr_debug("uart_open(%d) called\n", line);
1597 * tty->driver->num won't change, so we won't fail here with
1598 * tty->driver_data set to something non-NULL (and therefore
1599 * we won't get caught by uart_close()).
1602 if (line >= tty->driver->num)
1606 * We take the semaphore inside uart_get to guarantee that we won't
1607 * be re-entered while allocating the info structure, or while we
1608 * request any IRQs that the driver may need. This also has the nice
1609 * side-effect that it delays the action of uart_hangup, so we can
1610 * guarantee that info->port.tty will always contain something reasonable.
1612 state = uart_get(drv, line);
1613 if (IS_ERR(state)) {
1614 retval = PTR_ERR(state);
1619 * Once we set tty->driver_data here, we are guaranteed that
1620 * uart_close() will decrement the driver module use count.
1621 * Any failures from here onwards should not touch the count.
1623 tty->driver_data = state;
1624 state->port->info = &state->info;
1625 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1627 state->info.port.tty = tty;
1630 * If the port is in the middle of closing, bail out now.
1632 if (tty_hung_up_p(filp)) {
1635 mutex_unlock(&state->mutex);
1640 * Make sure the device is in D0 state.
1642 if (state->count == 1)
1643 uart_change_pm(state, 0);
1646 * Start up the serial port.
1648 retval = uart_startup(state, 0);
1651 * If we succeeded, wait until the port is ready.
1654 retval = uart_block_til_ready(filp, state);
1655 mutex_unlock(&state->mutex);
1658 * If this is the first open to succeed, adjust things to suit.
1660 if (retval == 0 && !(state->info.flags & UIF_NORMAL_ACTIVE)) {
1661 state->info.flags |= UIF_NORMAL_ACTIVE;
1663 uart_update_termios(state);
1670 static const char *uart_type(struct uart_port *port)
1672 const char *str = NULL;
1674 if (port->ops->type)
1675 str = port->ops->type(port);
1683 #ifdef CONFIG_PROC_FS
1685 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1687 struct uart_state *state = drv->state + i;
1689 struct uart_port *port = state->port;
1691 unsigned int status;
1697 mmio = port->iotype >= UPIO_MEM;
1698 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
1699 port->line, uart_type(port),
1700 mmio ? "mmio:0x" : "port:",
1701 mmio ? (unsigned long long)port->mapbase
1702 : (unsigned long long) port->iobase,
1705 if (port->type == PORT_UNKNOWN) {
1710 if (capable(CAP_SYS_ADMIN)) {
1711 mutex_lock(&state->mutex);
1712 pm_state = state->pm_state;
1714 uart_change_pm(state, 0);
1715 spin_lock_irq(&port->lock);
1716 status = port->ops->get_mctrl(port);
1717 spin_unlock_irq(&port->lock);
1719 uart_change_pm(state, pm_state);
1720 mutex_unlock(&state->mutex);
1722 ret += sprintf(buf + ret, " tx:%d rx:%d",
1723 port->icount.tx, port->icount.rx);
1724 if (port->icount.frame)
1725 ret += sprintf(buf + ret, " fe:%d",
1726 port->icount.frame);
1727 if (port->icount.parity)
1728 ret += sprintf(buf + ret, " pe:%d",
1729 port->icount.parity);
1730 if (port->icount.brk)
1731 ret += sprintf(buf + ret, " brk:%d",
1733 if (port->icount.overrun)
1734 ret += sprintf(buf + ret, " oe:%d",
1735 port->icount.overrun);
1737 #define INFOBIT(bit, str) \
1738 if (port->mctrl & (bit)) \
1739 strncat(stat_buf, (str), sizeof(stat_buf) - \
1740 strlen(stat_buf) - 2)
1741 #define STATBIT(bit, str) \
1742 if (status & (bit)) \
1743 strncat(stat_buf, (str), sizeof(stat_buf) - \
1744 strlen(stat_buf) - 2)
1748 INFOBIT(TIOCM_RTS, "|RTS");
1749 STATBIT(TIOCM_CTS, "|CTS");
1750 INFOBIT(TIOCM_DTR, "|DTR");
1751 STATBIT(TIOCM_DSR, "|DSR");
1752 STATBIT(TIOCM_CAR, "|CD");
1753 STATBIT(TIOCM_RNG, "|RI");
1756 strcat(stat_buf, "\n");
1758 ret += sprintf(buf + ret, stat_buf);
1768 static int uart_read_proc(char *page, char **start, off_t off,
1769 int count, int *eof, void *data)
1771 struct tty_driver *ttydrv = data;
1772 struct uart_driver *drv = ttydrv->driver_state;
1776 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1778 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1779 l = uart_line_info(page + len, drv, i);
1781 if (len + begin > off + count)
1783 if (len + begin < off) {
1790 if (off >= len + begin)
1792 *start = page + (off - begin);
1793 return (count < begin + len - off) ? count : (begin + len - off);
1797 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1799 * uart_console_write - write a console message to a serial port
1800 * @port: the port to write the message
1801 * @s: array of characters
1802 * @count: number of characters in string to write
1803 * @write: function to write character to port
1805 void uart_console_write(struct uart_port *port, const char *s,
1807 void (*putchar)(struct uart_port *, int))
1811 for (i = 0; i < count; i++, s++) {
1813 putchar(port, '\r');
1817 EXPORT_SYMBOL_GPL(uart_console_write);
1820 * Check whether an invalid uart number has been specified, and
1821 * if so, search for the first available port that does have
1824 struct uart_port * __init
1825 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1827 int idx = co->index;
1829 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1830 ports[idx].membase == NULL))
1831 for (idx = 0; idx < nr; idx++)
1832 if (ports[idx].iobase != 0 ||
1833 ports[idx].membase != NULL)
1842 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1843 * @options: pointer to option string
1844 * @baud: pointer to an 'int' variable for the baud rate.
1845 * @parity: pointer to an 'int' variable for the parity.
1846 * @bits: pointer to an 'int' variable for the number of data bits.
1847 * @flow: pointer to an 'int' variable for the flow control character.
1849 * uart_parse_options decodes a string containing the serial console
1850 * options. The format of the string is <baud><parity><bits><flow>,
1854 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1858 *baud = simple_strtoul(s, NULL, 10);
1859 while (*s >= '0' && *s <= '9')
1868 EXPORT_SYMBOL_GPL(uart_parse_options);
1875 static const struct baud_rates baud_rates[] = {
1876 { 921600, B921600 },
1877 { 460800, B460800 },
1878 { 230400, B230400 },
1879 { 115200, B115200 },
1891 * uart_set_options - setup the serial console parameters
1892 * @port: pointer to the serial ports uart_port structure
1893 * @co: console pointer
1895 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1896 * @bits: number of data bits
1897 * @flow: flow control character - 'r' (rts)
1900 uart_set_options(struct uart_port *port, struct console *co,
1901 int baud, int parity, int bits, int flow)
1903 struct ktermios termios;
1904 static struct ktermios dummy;
1908 * Ensure that the serial console lock is initialised
1911 spin_lock_init(&port->lock);
1912 lockdep_set_class(&port->lock, &port_lock_key);
1914 memset(&termios, 0, sizeof(struct ktermios));
1916 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1919 * Construct a cflag setting.
1921 for (i = 0; baud_rates[i].rate; i++)
1922 if (baud_rates[i].rate <= baud)
1925 termios.c_cflag |= baud_rates[i].cflag;
1928 termios.c_cflag |= CS7;
1930 termios.c_cflag |= CS8;
1934 termios.c_cflag |= PARODD;
1937 termios.c_cflag |= PARENB;
1942 termios.c_cflag |= CRTSCTS;
1945 * some uarts on other side don't support no flow control.
1946 * So we set * DTR in host uart to make them happy
1948 port->mctrl |= TIOCM_DTR;
1950 port->ops->set_termios(port, &termios, &dummy);
1952 * Allow the setting of the UART parameters with a NULL console
1956 co->cflag = termios.c_cflag;
1960 EXPORT_SYMBOL_GPL(uart_set_options);
1961 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1963 static void uart_change_pm(struct uart_state *state, int pm_state)
1965 struct uart_port *port = state->port;
1967 if (state->pm_state != pm_state) {
1969 port->ops->pm(port, pm_state, state->pm_state);
1970 state->pm_state = pm_state;
1975 struct uart_port *port;
1976 struct uart_driver *driver;
1979 static int serial_match_port(struct device *dev, void *data)
1981 struct uart_match *match = data;
1982 struct tty_driver *tty_drv = match->driver->tty_driver;
1983 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1986 return dev->devt == devt; /* Actually, only one tty per port */
1989 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1991 struct uart_state *state = drv->state + port->line;
1992 struct device *tty_dev;
1993 struct uart_match match = {port, drv};
1995 mutex_lock(&state->mutex);
1997 if (!console_suspend_enabled && uart_console(port)) {
1998 /* we're going to avoid suspending serial console */
1999 mutex_unlock(&state->mutex);
2003 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2004 if (device_may_wakeup(tty_dev)) {
2005 enable_irq_wake(port->irq);
2006 put_device(tty_dev);
2007 mutex_unlock(&state->mutex);
2010 port->suspended = 1;
2012 if (state->info.flags & UIF_INITIALIZED) {
2013 const struct uart_ops *ops = port->ops;
2016 state->info.flags = (state->info.flags & ~UIF_INITIALIZED)
2019 spin_lock_irq(&port->lock);
2021 ops->set_mctrl(port, 0);
2023 spin_unlock_irq(&port->lock);
2026 * Wait for the transmitter to empty.
2028 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
2031 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2033 port->dev ? dev_name(port->dev) : "",
2034 port->dev ? ": " : "",
2036 drv->tty_driver->name_base + port->line);
2038 ops->shutdown(port);
2042 * Disable the console device before suspending.
2044 if (uart_console(port))
2045 console_stop(port->cons);
2047 uart_change_pm(state, 3);
2049 mutex_unlock(&state->mutex);
2054 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2056 struct uart_state *state = drv->state + port->line;
2057 struct device *tty_dev;
2058 struct uart_match match = {port, drv};
2060 mutex_lock(&state->mutex);
2062 if (!console_suspend_enabled && uart_console(port)) {
2063 /* no need to resume serial console, it wasn't suspended */
2064 mutex_unlock(&state->mutex);
2068 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2069 if (!port->suspended && device_may_wakeup(tty_dev)) {
2070 disable_irq_wake(port->irq);
2071 mutex_unlock(&state->mutex);
2074 port->suspended = 0;
2077 * Re-enable the console device after suspending.
2079 if (uart_console(port)) {
2080 struct ktermios termios;
2083 * First try to use the console cflag setting.
2085 memset(&termios, 0, sizeof(struct ktermios));
2086 termios.c_cflag = port->cons->cflag;
2089 * If that's unset, use the tty termios setting.
2091 if (state->info.port.tty && termios.c_cflag == 0)
2092 termios = *state->info.port.tty->termios;
2094 uart_change_pm(state, 0);
2095 port->ops->set_termios(port, &termios, NULL);
2096 console_start(port->cons);
2099 if (state->info.flags & UIF_SUSPENDED) {
2100 const struct uart_ops *ops = port->ops;
2103 uart_change_pm(state, 0);
2104 spin_lock_irq(&port->lock);
2105 ops->set_mctrl(port, 0);
2106 spin_unlock_irq(&port->lock);
2107 ret = ops->startup(port);
2109 uart_change_speed(state, NULL);
2110 spin_lock_irq(&port->lock);
2111 ops->set_mctrl(port, port->mctrl);
2112 ops->start_tx(port);
2113 spin_unlock_irq(&port->lock);
2114 state->info.flags |= UIF_INITIALIZED;
2117 * Failed to resume - maybe hardware went away?
2118 * Clear the "initialized" flag so we won't try
2119 * to call the low level drivers shutdown method.
2121 uart_shutdown(state);
2124 state->info.flags &= ~UIF_SUSPENDED;
2127 mutex_unlock(&state->mutex);
2133 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2137 switch (port->iotype) {
2139 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2142 snprintf(address, sizeof(address),
2143 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2150 snprintf(address, sizeof(address),
2151 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2154 strlcpy(address, "*unknown*", sizeof(address));
2158 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2159 port->dev ? dev_name(port->dev) : "",
2160 port->dev ? ": " : "",
2162 drv->tty_driver->name_base + port->line,
2163 address, port->irq, uart_type(port));
2167 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2168 struct uart_port *port)
2173 * If there isn't a port here, don't do anything further.
2175 if (!port->iobase && !port->mapbase && !port->membase)
2179 * Now do the auto configuration stuff. Note that config_port
2180 * is expected to claim the resources and map the port for us.
2183 if (port->flags & UPF_AUTO_IRQ)
2184 flags |= UART_CONFIG_IRQ;
2185 if (port->flags & UPF_BOOT_AUTOCONF) {
2186 if (!(port->flags & UPF_FIXED_TYPE)) {
2187 port->type = PORT_UNKNOWN;
2188 flags |= UART_CONFIG_TYPE;
2190 port->ops->config_port(port, flags);
2193 if (port->type != PORT_UNKNOWN) {
2194 unsigned long flags;
2196 uart_report_port(drv, port);
2198 /* Power up port for set_mctrl() */
2199 uart_change_pm(state, 0);
2202 * Ensure that the modem control lines are de-activated.
2203 * keep the DTR setting that is set in uart_set_options()
2204 * We probably don't need a spinlock around this, but
2206 spin_lock_irqsave(&port->lock, flags);
2207 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2208 spin_unlock_irqrestore(&port->lock, flags);
2211 * If this driver supports console, and it hasn't been
2212 * successfully registered yet, try to re-register it.
2213 * It may be that the port was not available.
2215 if (port->cons && !(port->cons->flags & CON_ENABLED))
2216 register_console(port->cons);
2219 * Power down all ports by default, except the
2220 * console if we have one.
2222 if (!uart_console(port))
2223 uart_change_pm(state, 3);
2227 #ifdef CONFIG_CONSOLE_POLL
2229 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2231 struct uart_driver *drv = driver->driver_state;
2232 struct uart_state *state = drv->state + line;
2233 struct uart_port *port;
2239 if (!state || !state->port)
2243 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2247 uart_parse_options(options, &baud, &parity, &bits, &flow);
2248 return uart_set_options(port, NULL, baud, parity, bits, flow);
2254 static int uart_poll_get_char(struct tty_driver *driver, int line)
2256 struct uart_driver *drv = driver->driver_state;
2257 struct uart_state *state = drv->state + line;
2258 struct uart_port *port;
2260 if (!state || !state->port)
2264 return port->ops->poll_get_char(port);
2267 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2269 struct uart_driver *drv = driver->driver_state;
2270 struct uart_state *state = drv->state + line;
2271 struct uart_port *port;
2273 if (!state || !state->port)
2277 port->ops->poll_put_char(port, ch);
2281 static const struct tty_operations uart_ops = {
2283 .close = uart_close,
2284 .write = uart_write,
2285 .put_char = uart_put_char,
2286 .flush_chars = uart_flush_chars,
2287 .write_room = uart_write_room,
2288 .chars_in_buffer= uart_chars_in_buffer,
2289 .flush_buffer = uart_flush_buffer,
2290 .ioctl = uart_ioctl,
2291 .throttle = uart_throttle,
2292 .unthrottle = uart_unthrottle,
2293 .send_xchar = uart_send_xchar,
2294 .set_termios = uart_set_termios,
2295 .set_ldisc = uart_set_ldisc,
2297 .start = uart_start,
2298 .hangup = uart_hangup,
2299 .break_ctl = uart_break_ctl,
2300 .wait_until_sent= uart_wait_until_sent,
2301 #ifdef CONFIG_PROC_FS
2302 .read_proc = uart_read_proc,
2304 .tiocmget = uart_tiocmget,
2305 .tiocmset = uart_tiocmset,
2306 #ifdef CONFIG_CONSOLE_POLL
2307 .poll_init = uart_poll_init,
2308 .poll_get_char = uart_poll_get_char,
2309 .poll_put_char = uart_poll_put_char,
2314 * uart_register_driver - register a driver with the uart core layer
2315 * @drv: low level driver structure
2317 * Register a uart driver with the core driver. We in turn register
2318 * with the tty layer, and initialise the core driver per-port state.
2320 * We have a proc file in /proc/tty/driver which is named after the
2323 * drv->port should be NULL, and the per-port structures should be
2324 * registered using uart_add_one_port after this call has succeeded.
2326 int uart_register_driver(struct uart_driver *drv)
2328 struct tty_driver *normal = NULL;
2334 * Maybe we should be using a slab cache for this, especially if
2335 * we have a large number of ports to handle.
2337 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2342 normal = alloc_tty_driver(drv->nr);
2346 drv->tty_driver = normal;
2348 normal->owner = drv->owner;
2349 normal->driver_name = drv->driver_name;
2350 normal->name = drv->dev_name;
2351 normal->major = drv->major;
2352 normal->minor_start = drv->minor;
2353 normal->type = TTY_DRIVER_TYPE_SERIAL;
2354 normal->subtype = SERIAL_TYPE_NORMAL;
2355 normal->init_termios = tty_std_termios;
2356 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2357 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2358 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2359 normal->driver_state = drv;
2360 tty_set_operations(normal, &uart_ops);
2363 * Initialise the UART state(s).
2365 for (i = 0; i < drv->nr; i++) {
2366 struct uart_state *state = drv->state + i;
2368 state->close_delay = 500; /* .5 seconds */
2369 state->closing_wait = 30000; /* 30 seconds */
2370 mutex_init(&state->mutex);
2372 tty_port_init(&state->info.port);
2373 init_waitqueue_head(&state->info.delta_msr_wait);
2374 tasklet_init(&state->info.tlet, uart_tasklet_action,
2375 (unsigned long)state);
2378 retval = tty_register_driver(normal);
2381 put_tty_driver(normal);
2388 * uart_unregister_driver - remove a driver from the uart core layer
2389 * @drv: low level driver structure
2391 * Remove all references to a driver from the core driver. The low
2392 * level driver must have removed all its ports via the
2393 * uart_remove_one_port() if it registered them with uart_add_one_port().
2394 * (ie, drv->port == NULL)
2396 void uart_unregister_driver(struct uart_driver *drv)
2398 struct tty_driver *p = drv->tty_driver;
2399 tty_unregister_driver(p);
2402 drv->tty_driver = NULL;
2405 struct tty_driver *uart_console_device(struct console *co, int *index)
2407 struct uart_driver *p = co->data;
2409 return p->tty_driver;
2413 * uart_add_one_port - attach a driver-defined port structure
2414 * @drv: pointer to the uart low level driver structure for this port
2415 * @port: uart port structure to use for this port.
2417 * This allows the driver to register its own uart_port structure
2418 * with the core driver. The main purpose is to allow the low
2419 * level uart drivers to expand uart_port, rather than having yet
2420 * more levels of structures.
2422 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2424 struct uart_state *state;
2426 struct device *tty_dev;
2428 BUG_ON(in_interrupt());
2430 if (port->line >= drv->nr)
2433 state = drv->state + port->line;
2435 mutex_lock(&port_mutex);
2436 mutex_lock(&state->mutex);
2443 state->pm_state = -1;
2445 port->cons = drv->cons;
2446 port->info = &state->info;
2449 * If this port is a console, then the spinlock is already
2452 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2453 spin_lock_init(&port->lock);
2454 lockdep_set_class(&port->lock, &port_lock_key);
2457 uart_configure_port(drv, state, port);
2460 * Register the port whether it's detected or not. This allows
2461 * setserial to be used to alter this ports parameters.
2463 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2464 if (likely(!IS_ERR(tty_dev))) {
2465 device_init_wakeup(tty_dev, 1);
2466 device_set_wakeup_enable(tty_dev, 0);
2468 printk(KERN_ERR "Cannot register tty device on line %d\n",
2472 * Ensure UPF_DEAD is not set.
2474 port->flags &= ~UPF_DEAD;
2477 mutex_unlock(&state->mutex);
2478 mutex_unlock(&port_mutex);
2484 * uart_remove_one_port - detach a driver defined port structure
2485 * @drv: pointer to the uart low level driver structure for this port
2486 * @port: uart port structure for this port
2488 * This unhooks (and hangs up) the specified port structure from the
2489 * core driver. No further calls will be made to the low-level code
2492 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2494 struct uart_state *state = drv->state + port->line;
2495 struct uart_info *info;
2497 BUG_ON(in_interrupt());
2499 if (state->port != port)
2500 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2503 mutex_lock(&port_mutex);
2506 * Mark the port "dead" - this prevents any opens from
2507 * succeeding while we shut down the port.
2509 mutex_lock(&state->mutex);
2510 port->flags |= UPF_DEAD;
2511 mutex_unlock(&state->mutex);
2514 * Remove the devices from the tty layer
2516 tty_unregister_device(drv->tty_driver, port->line);
2518 info = &state->info;
2519 if (info && info->port.tty)
2520 tty_vhangup(info->port.tty);
2523 * Free the port IO and memory resources, if any.
2525 if (port->type != PORT_UNKNOWN)
2526 port->ops->release_port(port);
2529 * Indicate that there isn't a port here anymore.
2531 port->type = PORT_UNKNOWN;
2534 * Kill the tasklet, and free resources.
2537 tasklet_kill(&info->tlet);
2540 mutex_unlock(&port_mutex);
2546 * Are the two ports equivalent?
2548 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2550 if (port1->iotype != port2->iotype)
2553 switch (port1->iotype) {
2555 return (port1->iobase == port2->iobase);
2557 return (port1->iobase == port2->iobase) &&
2558 (port1->hub6 == port2->hub6);
2564 return (port1->mapbase == port2->mapbase);
2568 EXPORT_SYMBOL(uart_match_port);
2570 EXPORT_SYMBOL(uart_write_wakeup);
2571 EXPORT_SYMBOL(uart_register_driver);
2572 EXPORT_SYMBOL(uart_unregister_driver);
2573 EXPORT_SYMBOL(uart_suspend_port);
2574 EXPORT_SYMBOL(uart_resume_port);
2575 EXPORT_SYMBOL(uart_add_one_port);
2576 EXPORT_SYMBOL(uart_remove_one_port);
2578 MODULE_DESCRIPTION("Serial driver core");
2579 MODULE_LICENSE("GPL");