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 ? (state)->info->port.blocked_open : 0))
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;
219 * Set the TTY IO error marker
222 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
224 if (info->flags & UIF_INITIALIZED) {
225 info->flags &= ~UIF_INITIALIZED;
228 * Turn off DTR and RTS early.
230 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
231 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
234 * clear delta_msr_wait queue to avoid mem leaks: we may free
235 * the irq here so the queue might never be woken up. Note
236 * that we won't end up waiting on delta_msr_wait again since
237 * any outstanding file descriptors should be pointing at
238 * hung_up_tty_fops now.
240 wake_up_interruptible(&info->delta_msr_wait);
243 * Free the IRQ and disable the port.
245 port->ops->shutdown(port);
248 * Ensure that the IRQ handler isn't running on another CPU.
250 synchronize_irq(port->irq);
254 * kill off our tasklet
256 tasklet_kill(&info->tlet);
259 * Free the transmit buffer page.
261 if (info->xmit.buf) {
262 free_page((unsigned long)info->xmit.buf);
263 info->xmit.buf = NULL;
268 * uart_update_timeout - update per-port FIFO timeout.
269 * @port: uart_port structure describing the port
270 * @cflag: termios cflag value
271 * @baud: speed of the port
273 * Set the port FIFO timeout value. The @cflag value should
274 * reflect the actual hardware settings.
277 uart_update_timeout(struct uart_port *port, unsigned int cflag,
282 /* byte size and parity */
283 switch (cflag & CSIZE) {
304 * The total number of bits to be transmitted in the fifo.
306 bits = bits * port->fifosize;
309 * Figure the timeout to send the above number of bits.
310 * Add .02 seconds of slop
312 port->timeout = (HZ * bits) / baud + HZ/50;
315 EXPORT_SYMBOL(uart_update_timeout);
318 * uart_get_baud_rate - return baud rate for a particular port
319 * @port: uart_port structure describing the port in question.
320 * @termios: desired termios settings.
321 * @old: old termios (or NULL)
322 * @min: minimum acceptable baud rate
323 * @max: maximum acceptable baud rate
325 * Decode the termios structure into a numeric baud rate,
326 * taking account of the magic 38400 baud rate (with spd_*
327 * flags), and mapping the %B0 rate to 9600 baud.
329 * If the new baud rate is invalid, try the old termios setting.
330 * If it's still invalid, we try 9600 baud.
332 * Update the @termios structure to reflect the baud rate
333 * we're actually going to be using. Don't do this for the case
334 * where B0 is requested ("hang up").
337 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old, unsigned int min, unsigned int max)
340 unsigned int try, baud, altbaud = 38400;
342 upf_t flags = port->flags & UPF_SPD_MASK;
344 if (flags == UPF_SPD_HI)
346 if (flags == UPF_SPD_VHI)
348 if (flags == UPF_SPD_SHI)
350 if (flags == UPF_SPD_WARP)
353 for (try = 0; try < 2; try++) {
354 baud = tty_termios_baud_rate(termios);
357 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
364 * Special case: B0 rate.
371 if (baud >= min && baud <= max)
375 * Oops, the quotient was zero. Try again with
376 * the old baud rate if possible.
378 termios->c_cflag &= ~CBAUD;
380 baud = tty_termios_baud_rate(old);
382 tty_termios_encode_baud_rate(termios,
389 * As a last resort, if the quotient is zero,
390 * default to 9600 bps
393 tty_termios_encode_baud_rate(termios, 9600, 9600);
399 EXPORT_SYMBOL(uart_get_baud_rate);
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
406 * Calculate the uart clock divisor for the port.
409 uart_get_divisor(struct uart_port *port, unsigned int baud)
414 * Old custom speed handling.
416 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
417 quot = port->custom_divisor;
419 quot = (port->uartclk + (8 * baud)) / (16 * baud);
424 EXPORT_SYMBOL(uart_get_divisor);
426 /* FIXME: Consistent locking policy */
428 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
430 struct tty_struct *tty = state->info->port.tty;
431 struct uart_port *port = state->port;
432 struct ktermios *termios;
435 * If we have no tty, termios, or the port does not exist,
436 * then we can't set the parameters for this port.
438 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
441 termios = tty->termios;
444 * Set flags based on termios cflag
446 if (termios->c_cflag & CRTSCTS)
447 state->info->flags |= UIF_CTS_FLOW;
449 state->info->flags &= ~UIF_CTS_FLOW;
451 if (termios->c_cflag & CLOCAL)
452 state->info->flags &= ~UIF_CHECK_CD;
454 state->info->flags |= UIF_CHECK_CD;
456 port->ops->set_termios(port, termios, old_termios);
460 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
468 spin_lock_irqsave(&port->lock, flags);
469 if (uart_circ_chars_free(circ) != 0) {
470 circ->buf[circ->head] = c;
471 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
474 spin_unlock_irqrestore(&port->lock, flags);
478 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
480 struct uart_state *state = tty->driver_data;
482 return __uart_put_char(state->port, &state->info->xmit, ch);
485 static void uart_flush_chars(struct tty_struct *tty)
491 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
493 struct uart_state *state = tty->driver_data;
494 struct uart_port *port;
495 struct circ_buf *circ;
500 * This means you called this function _after_ the port was
501 * closed. No cookie for you.
503 if (!state || !state->info) {
509 circ = &state->info->xmit;
514 spin_lock_irqsave(&port->lock, flags);
516 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
521 memcpy(circ->buf + circ->head, buf, c);
522 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
527 spin_unlock_irqrestore(&port->lock, flags);
533 static int uart_write_room(struct tty_struct *tty)
535 struct uart_state *state = tty->driver_data;
539 spin_lock_irqsave(&state->port->lock, flags);
540 ret = uart_circ_chars_free(&state->info->xmit);
541 spin_unlock_irqrestore(&state->port->lock, flags);
545 static int uart_chars_in_buffer(struct tty_struct *tty)
547 struct uart_state *state = tty->driver_data;
551 spin_lock_irqsave(&state->port->lock, flags);
552 ret = uart_circ_chars_pending(&state->info->xmit);
553 spin_unlock_irqrestore(&state->port->lock, flags);
557 static void uart_flush_buffer(struct tty_struct *tty)
559 struct uart_state *state = tty->driver_data;
560 struct uart_port *port;
564 * This means you called this function _after_ the port was
565 * closed. No cookie for you.
567 if (!state || !state->info) {
573 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
575 spin_lock_irqsave(&port->lock, flags);
576 uart_circ_clear(&state->info->xmit);
577 if (port->ops->flush_buffer)
578 port->ops->flush_buffer(port);
579 spin_unlock_irqrestore(&port->lock, flags);
584 * This function is used to send a high-priority XON/XOFF character to
587 static void uart_send_xchar(struct tty_struct *tty, char ch)
589 struct uart_state *state = tty->driver_data;
590 struct uart_port *port = state->port;
593 if (port->ops->send_xchar)
594 port->ops->send_xchar(port, ch);
598 spin_lock_irqsave(&port->lock, flags);
599 port->ops->start_tx(port);
600 spin_unlock_irqrestore(&port->lock, flags);
605 static void uart_throttle(struct tty_struct *tty)
607 struct uart_state *state = tty->driver_data;
610 uart_send_xchar(tty, STOP_CHAR(tty));
612 if (tty->termios->c_cflag & CRTSCTS)
613 uart_clear_mctrl(state->port, TIOCM_RTS);
616 static void uart_unthrottle(struct tty_struct *tty)
618 struct uart_state *state = tty->driver_data;
619 struct uart_port *port = state->port;
625 uart_send_xchar(tty, START_CHAR(tty));
628 if (tty->termios->c_cflag & CRTSCTS)
629 uart_set_mctrl(port, TIOCM_RTS);
632 static int uart_get_info(struct uart_state *state,
633 struct serial_struct __user *retinfo)
635 struct uart_port *port = state->port;
636 struct serial_struct tmp;
638 memset(&tmp, 0, sizeof(tmp));
640 /* Ensure the state we copy is consistent and no hardware changes
642 mutex_lock(&state->mutex);
644 tmp.type = port->type;
645 tmp.line = port->line;
646 tmp.port = port->iobase;
647 if (HIGH_BITS_OFFSET)
648 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
650 tmp.flags = port->flags;
651 tmp.xmit_fifo_size = port->fifosize;
652 tmp.baud_base = port->uartclk / 16;
653 tmp.close_delay = state->close_delay / 10;
654 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
655 ASYNC_CLOSING_WAIT_NONE :
656 state->closing_wait / 10;
657 tmp.custom_divisor = port->custom_divisor;
658 tmp.hub6 = port->hub6;
659 tmp.io_type = port->iotype;
660 tmp.iomem_reg_shift = port->regshift;
661 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
663 mutex_unlock(&state->mutex);
665 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
670 static int uart_set_info(struct uart_state *state,
671 struct serial_struct __user *newinfo)
673 struct serial_struct new_serial;
674 struct uart_port *port = state->port;
675 unsigned long new_port;
676 unsigned int change_irq, change_port, closing_wait;
677 unsigned int old_custom_divisor, close_delay;
678 upf_t old_flags, new_flags;
681 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
684 new_port = new_serial.port;
685 if (HIGH_BITS_OFFSET)
686 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
688 new_serial.irq = irq_canonicalize(new_serial.irq);
689 close_delay = new_serial.close_delay * 10;
690 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
691 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
694 * This semaphore protects state->count. It is also
695 * very useful to prevent opens. Also, take the
696 * port configuration semaphore to make sure that a
697 * module insertion/removal doesn't change anything
700 mutex_lock(&state->mutex);
702 change_irq = !(port->flags & UPF_FIXED_PORT)
703 && new_serial.irq != port->irq;
706 * Since changing the 'type' of the port changes its resource
707 * allocations, we should treat type changes the same as
710 change_port = !(port->flags & UPF_FIXED_PORT)
711 && (new_port != port->iobase ||
712 (unsigned long)new_serial.iomem_base != port->mapbase ||
713 new_serial.hub6 != port->hub6 ||
714 new_serial.io_type != port->iotype ||
715 new_serial.iomem_reg_shift != port->regshift ||
716 new_serial.type != port->type);
718 old_flags = port->flags;
719 new_flags = new_serial.flags;
720 old_custom_divisor = port->custom_divisor;
722 if (!capable(CAP_SYS_ADMIN)) {
724 if (change_irq || change_port ||
725 (new_serial.baud_base != port->uartclk / 16) ||
726 (close_delay != state->close_delay) ||
727 (closing_wait != state->closing_wait) ||
728 (new_serial.xmit_fifo_size &&
729 new_serial.xmit_fifo_size != port->fifosize) ||
730 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
732 port->flags = ((port->flags & ~UPF_USR_MASK) |
733 (new_flags & UPF_USR_MASK));
734 port->custom_divisor = new_serial.custom_divisor;
739 * Ask the low level driver to verify the settings.
741 if (port->ops->verify_port)
742 retval = port->ops->verify_port(port, &new_serial);
744 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
745 (new_serial.baud_base < 9600))
751 if (change_port || change_irq) {
755 * Make sure that we are the sole user of this port.
757 if (uart_users(state) > 1)
761 * We need to shutdown the serial port at the old
762 * port/type/irq combination.
764 uart_shutdown(state);
768 unsigned long old_iobase, old_mapbase;
769 unsigned int old_type, old_iotype, old_hub6, old_shift;
771 old_iobase = port->iobase;
772 old_mapbase = port->mapbase;
773 old_type = port->type;
774 old_hub6 = port->hub6;
775 old_iotype = port->iotype;
776 old_shift = port->regshift;
779 * Free and release old regions
781 if (old_type != PORT_UNKNOWN)
782 port->ops->release_port(port);
784 port->iobase = new_port;
785 port->type = new_serial.type;
786 port->hub6 = new_serial.hub6;
787 port->iotype = new_serial.io_type;
788 port->regshift = new_serial.iomem_reg_shift;
789 port->mapbase = (unsigned long)new_serial.iomem_base;
792 * Claim and map the new regions
794 if (port->type != PORT_UNKNOWN) {
795 retval = port->ops->request_port(port);
797 /* Always success - Jean II */
802 * If we fail to request resources for the
803 * new port, try to restore the old settings.
805 if (retval && old_type != PORT_UNKNOWN) {
806 port->iobase = old_iobase;
807 port->type = old_type;
808 port->hub6 = old_hub6;
809 port->iotype = old_iotype;
810 port->regshift = old_shift;
811 port->mapbase = old_mapbase;
812 retval = port->ops->request_port(port);
814 * If we failed to restore the old settings,
818 port->type = PORT_UNKNOWN;
824 /* Added to return the correct error -Ram Gupta */
830 port->irq = new_serial.irq;
831 if (!(port->flags & UPF_FIXED_PORT))
832 port->uartclk = new_serial.baud_base * 16;
833 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
834 (new_flags & UPF_CHANGE_MASK);
835 port->custom_divisor = new_serial.custom_divisor;
836 state->close_delay = close_delay;
837 state->closing_wait = closing_wait;
838 if (new_serial.xmit_fifo_size)
839 port->fifosize = new_serial.xmit_fifo_size;
840 if (state->info->port.tty)
841 state->info->port.tty->low_latency =
842 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
846 if (port->type == PORT_UNKNOWN)
848 if (state->info->flags & UIF_INITIALIZED) {
849 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
850 old_custom_divisor != port->custom_divisor) {
852 * If they're setting up a custom divisor or speed,
853 * instead of clearing it, then bitch about it. No
854 * need to rate-limit; it's CAP_SYS_ADMIN only.
856 if (port->flags & UPF_SPD_MASK) {
859 "%s sets custom speed on %s. This "
860 "is deprecated.\n", current->comm,
861 tty_name(state->info->port.tty, buf));
863 uart_change_speed(state, NULL);
866 retval = uart_startup(state, 1);
868 mutex_unlock(&state->mutex);
874 * uart_get_lsr_info - get line status register info.
875 * Note: uart_ioctl protects us against hangups.
877 static int uart_get_lsr_info(struct uart_state *state,
878 unsigned int __user *value)
880 struct uart_port *port = state->port;
883 result = port->ops->tx_empty(port);
886 * If we're about to load something into the transmit
887 * register, we'll pretend the transmitter isn't empty to
888 * avoid a race condition (depending on when the transmit
889 * interrupt happens).
892 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
893 !state->info->port.tty->stopped && !state->info->port.tty->hw_stopped))
894 result &= ~TIOCSER_TEMT;
896 return put_user(result, value);
899 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
901 struct uart_state *state = tty->driver_data;
902 struct uart_port *port = state->port;
905 mutex_lock(&state->mutex);
906 if ((!file || !tty_hung_up_p(file)) &&
907 !(tty->flags & (1 << TTY_IO_ERROR))) {
908 result = port->mctrl;
910 spin_lock_irq(&port->lock);
911 result |= port->ops->get_mctrl(port);
912 spin_unlock_irq(&port->lock);
914 mutex_unlock(&state->mutex);
920 uart_tiocmset(struct tty_struct *tty, struct file *file,
921 unsigned int set, unsigned int clear)
923 struct uart_state *state = tty->driver_data;
924 struct uart_port *port = state->port;
927 mutex_lock(&state->mutex);
928 if ((!file || !tty_hung_up_p(file)) &&
929 !(tty->flags & (1 << TTY_IO_ERROR))) {
930 uart_update_mctrl(port, set, clear);
933 mutex_unlock(&state->mutex);
937 static int uart_break_ctl(struct tty_struct *tty, int break_state)
939 struct uart_state *state = tty->driver_data;
940 struct uart_port *port = state->port;
942 mutex_lock(&state->mutex);
944 if (port->type != PORT_UNKNOWN)
945 port->ops->break_ctl(port, break_state);
947 mutex_unlock(&state->mutex);
951 static int uart_do_autoconfig(struct uart_state *state)
953 struct uart_port *port = state->port;
956 if (!capable(CAP_SYS_ADMIN))
960 * Take the per-port semaphore. This prevents count from
961 * changing, and hence any extra opens of the port while
962 * we're auto-configuring.
964 if (mutex_lock_interruptible(&state->mutex))
968 if (uart_users(state) == 1) {
969 uart_shutdown(state);
972 * If we already have a port type configured,
973 * we must release its resources.
975 if (port->type != PORT_UNKNOWN)
976 port->ops->release_port(port);
978 flags = UART_CONFIG_TYPE;
979 if (port->flags & UPF_AUTO_IRQ)
980 flags |= UART_CONFIG_IRQ;
983 * This will claim the ports resources if
986 port->ops->config_port(port, flags);
988 ret = uart_startup(state, 1);
990 mutex_unlock(&state->mutex);
995 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
996 * - mask passed in arg for lines of interest
997 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
998 * Caller should use TIOCGICOUNT to see which one it was
1001 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1003 struct uart_port *port = state->port;
1004 DECLARE_WAITQUEUE(wait, current);
1005 struct uart_icount cprev, cnow;
1009 * note the counters on entry
1011 spin_lock_irq(&port->lock);
1012 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
1015 * Force modem status interrupts on
1017 port->ops->enable_ms(port);
1018 spin_unlock_irq(&port->lock);
1020 add_wait_queue(&state->info->delta_msr_wait, &wait);
1022 spin_lock_irq(&port->lock);
1023 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1024 spin_unlock_irq(&port->lock);
1026 set_current_state(TASK_INTERRUPTIBLE);
1028 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1029 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1030 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1031 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1038 /* see if a signal did it */
1039 if (signal_pending(current)) {
1047 current->state = TASK_RUNNING;
1048 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1054 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1055 * Return: write counters to the user passed counter struct
1056 * NB: both 1->0 and 0->1 transitions are counted except for
1057 * RI where only 0->1 is counted.
1059 static int uart_get_count(struct uart_state *state,
1060 struct serial_icounter_struct __user *icnt)
1062 struct serial_icounter_struct icount;
1063 struct uart_icount cnow;
1064 struct uart_port *port = state->port;
1066 spin_lock_irq(&port->lock);
1067 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1068 spin_unlock_irq(&port->lock);
1070 icount.cts = cnow.cts;
1071 icount.dsr = cnow.dsr;
1072 icount.rng = cnow.rng;
1073 icount.dcd = cnow.dcd;
1074 icount.rx = cnow.rx;
1075 icount.tx = cnow.tx;
1076 icount.frame = cnow.frame;
1077 icount.overrun = cnow.overrun;
1078 icount.parity = cnow.parity;
1079 icount.brk = cnow.brk;
1080 icount.buf_overrun = cnow.buf_overrun;
1082 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1086 * Called via sys_ioctl. We can use spin_lock_irq() here.
1089 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1092 struct uart_state *state = tty->driver_data;
1093 void __user *uarg = (void __user *)arg;
1094 int ret = -ENOIOCTLCMD;
1098 * These ioctls don't rely on the hardware to be present.
1102 ret = uart_get_info(state, uarg);
1106 ret = uart_set_info(state, uarg);
1110 ret = uart_do_autoconfig(state);
1113 case TIOCSERGWILD: /* obsolete */
1114 case TIOCSERSWILD: /* obsolete */
1119 if (ret != -ENOIOCTLCMD)
1122 if (tty->flags & (1 << TTY_IO_ERROR)) {
1128 * The following should only be used when hardware is present.
1132 ret = uart_wait_modem_status(state, arg);
1136 ret = uart_get_count(state, uarg);
1140 if (ret != -ENOIOCTLCMD)
1143 mutex_lock(&state->mutex);
1145 if (tty_hung_up_p(filp)) {
1151 * All these rely on hardware being present and need to be
1152 * protected against the tty being hung up.
1155 case TIOCSERGETLSR: /* Get line status register */
1156 ret = uart_get_lsr_info(state, uarg);
1160 struct uart_port *port = state->port;
1161 if (port->ops->ioctl)
1162 ret = port->ops->ioctl(port, cmd, arg);
1167 mutex_unlock(&state->mutex);
1172 static void uart_set_ldisc(struct tty_struct *tty)
1174 struct uart_state *state = tty->driver_data;
1175 struct uart_port *port = state->port;
1177 if (port->ops->set_ldisc)
1178 port->ops->set_ldisc(port);
1181 static void uart_set_termios(struct tty_struct *tty,
1182 struct ktermios *old_termios)
1184 struct uart_state *state = tty->driver_data;
1185 unsigned long flags;
1186 unsigned int cflag = tty->termios->c_cflag;
1190 * These are the bits that are used to setup various
1191 * flags in the low level driver. We can ignore the Bfoo
1192 * bits in c_cflag; c_[io]speed will always be set
1193 * appropriately by set_termios() in tty_ioctl.c
1195 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1196 if ((cflag ^ old_termios->c_cflag) == 0 &&
1197 tty->termios->c_ospeed == old_termios->c_ospeed &&
1198 tty->termios->c_ispeed == old_termios->c_ispeed &&
1199 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1203 uart_change_speed(state, old_termios);
1205 /* Handle transition to B0 status */
1206 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1207 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1209 /* Handle transition away from B0 status */
1210 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1211 unsigned int mask = TIOCM_DTR;
1212 if (!(cflag & CRTSCTS) ||
1213 !test_bit(TTY_THROTTLED, &tty->flags))
1215 uart_set_mctrl(state->port, mask);
1218 /* Handle turning off CRTSCTS */
1219 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1220 spin_lock_irqsave(&state->port->lock, flags);
1221 tty->hw_stopped = 0;
1223 spin_unlock_irqrestore(&state->port->lock, flags);
1226 /* Handle turning on CRTSCTS */
1227 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1228 spin_lock_irqsave(&state->port->lock, flags);
1229 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1230 tty->hw_stopped = 1;
1231 state->port->ops->stop_tx(state->port);
1233 spin_unlock_irqrestore(&state->port->lock, flags);
1237 * No need to wake up processes in open wait, since they
1238 * sample the CLOCAL flag once, and don't recheck it.
1239 * XXX It's not clear whether the current behavior is correct
1240 * or not. Hence, this may change.....
1242 if (!(old_termios->c_cflag & CLOCAL) &&
1243 (tty->termios->c_cflag & CLOCAL))
1244 wake_up_interruptible(&state->info->port.open_wait);
1249 * In 2.4.5, calls to this will be serialized via the BKL in
1250 * linux/drivers/char/tty_io.c:tty_release()
1251 * linux/drivers/char/tty_io.c:do_tty_handup()
1253 static void uart_close(struct tty_struct *tty, struct file *filp)
1255 struct uart_state *state = tty->driver_data;
1256 struct uart_port *port;
1258 BUG_ON(!kernel_locked());
1260 if (!state || !state->port)
1265 pr_debug("uart_close(%d) called\n", port->line);
1267 mutex_lock(&state->mutex);
1269 if (tty_hung_up_p(filp))
1272 if ((tty->count == 1) && (state->count != 1)) {
1274 * Uh, oh. tty->count is 1, which means that the tty
1275 * structure will be freed. state->count should always
1276 * be one in these conditions. If it's greater than
1277 * one, we've got real problems, since it means the
1278 * serial port won't be shutdown.
1280 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1281 "state->count is %d\n", state->count);
1284 if (--state->count < 0) {
1285 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1286 tty->name, state->count);
1293 * Now we wait for the transmit buffer to clear; and we notify
1294 * the line discipline to only process XON/XOFF characters by
1295 * setting tty->closing.
1299 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1300 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1303 * At this point, we stop accepting input. To do this, we
1304 * disable the receive line status interrupts.
1306 if (state->info->flags & UIF_INITIALIZED) {
1307 unsigned long flags;
1308 spin_lock_irqsave(&port->lock, flags);
1309 port->ops->stop_rx(port);
1310 spin_unlock_irqrestore(&port->lock, flags);
1312 * Before we drop DTR, make sure the UART transmitter
1313 * has completely drained; this is especially
1314 * important if there is a transmit FIFO!
1316 uart_wait_until_sent(tty, port->timeout);
1319 uart_shutdown(state);
1320 uart_flush_buffer(tty);
1322 tty_ldisc_flush(tty);
1325 state->info->port.tty = NULL;
1327 if (state->info->port.blocked_open) {
1328 if (state->close_delay)
1329 msleep_interruptible(state->close_delay);
1330 } else if (!uart_console(port)) {
1331 uart_change_pm(state, 3);
1335 * Wake up anyone trying to open this port.
1337 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1338 wake_up_interruptible(&state->info->port.open_wait);
1341 mutex_unlock(&state->mutex);
1344 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1346 struct uart_state *state = tty->driver_data;
1347 struct uart_port *port = state->port;
1348 unsigned long char_time, expire;
1350 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1356 * Set the check interval to be 1/5 of the estimated time to
1357 * send a single character, and make it at least 1. The check
1358 * interval should also be less than the timeout.
1360 * Note: we have to use pretty tight timings here to satisfy
1363 char_time = (port->timeout - HZ/50) / port->fifosize;
1364 char_time = char_time / 5;
1367 if (timeout && timeout < char_time)
1368 char_time = timeout;
1371 * If the transmitter hasn't cleared in twice the approximate
1372 * amount of time to send the entire FIFO, it probably won't
1373 * ever clear. This assumes the UART isn't doing flow
1374 * control, which is currently the case. Hence, if it ever
1375 * takes longer than port->timeout, this is probably due to a
1376 * UART bug of some kind. So, we clamp the timeout parameter at
1379 if (timeout == 0 || timeout > 2 * port->timeout)
1380 timeout = 2 * port->timeout;
1382 expire = jiffies + timeout;
1384 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1385 port->line, jiffies, expire);
1388 * Check whether the transmitter is empty every 'char_time'.
1389 * 'timeout' / 'expire' give us the maximum amount of time
1392 while (!port->ops->tx_empty(port)) {
1393 msleep_interruptible(jiffies_to_msecs(char_time));
1394 if (signal_pending(current))
1396 if (time_after(jiffies, expire))
1399 set_current_state(TASK_RUNNING); /* might not be needed */
1404 * This is called with the BKL held in
1405 * linux/drivers/char/tty_io.c:do_tty_hangup()
1406 * We're called from the eventd thread, so we can sleep for
1407 * a _short_ time only.
1409 static void uart_hangup(struct tty_struct *tty)
1411 struct uart_state *state = tty->driver_data;
1413 BUG_ON(!kernel_locked());
1414 pr_debug("uart_hangup(%d)\n", state->port->line);
1416 mutex_lock(&state->mutex);
1417 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1418 uart_flush_buffer(tty);
1419 uart_shutdown(state);
1421 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1422 state->info->port.tty = NULL;
1423 wake_up_interruptible(&state->info->port.open_wait);
1424 wake_up_interruptible(&state->info->delta_msr_wait);
1426 mutex_unlock(&state->mutex);
1430 * Copy across the serial console cflag setting into the termios settings
1431 * for the initial open of the port. This allows continuity between the
1432 * kernel settings, and the settings init adopts when it opens the port
1433 * for the first time.
1435 static void uart_update_termios(struct uart_state *state)
1437 struct tty_struct *tty = state->info->port.tty;
1438 struct uart_port *port = state->port;
1440 if (uart_console(port) && port->cons->cflag) {
1441 tty->termios->c_cflag = port->cons->cflag;
1442 port->cons->cflag = 0;
1446 * If the device failed to grab its irq resources,
1447 * or some other error occurred, don't try to talk
1448 * to the port hardware.
1450 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1452 * Make termios settings take effect.
1454 uart_change_speed(state, NULL);
1457 * And finally enable the RTS and DTR signals.
1459 if (tty->termios->c_cflag & CBAUD)
1460 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1465 * Block the open until the port is ready. We must be called with
1466 * the per-port semaphore held.
1469 uart_block_til_ready(struct file *filp, struct uart_state *state)
1471 DECLARE_WAITQUEUE(wait, current);
1472 struct uart_info *info = state->info;
1473 struct uart_port *port = state->port;
1476 info->port.blocked_open++;
1479 add_wait_queue(&info->port.open_wait, &wait);
1481 set_current_state(TASK_INTERRUPTIBLE);
1484 * If we have been hung up, tell userspace/restart open.
1486 if (tty_hung_up_p(filp) || info->port.tty == NULL)
1490 * If the port has been closed, tell userspace/restart open.
1492 if (!(info->flags & UIF_INITIALIZED))
1496 * If non-blocking mode is set, or CLOCAL mode is set,
1497 * we don't want to wait for the modem status lines to
1498 * indicate that the port is ready.
1500 * Also, if the port is not enabled/configured, we want
1501 * to allow the open to succeed here. Note that we will
1502 * have set TTY_IO_ERROR for a non-existant port.
1504 if ((filp->f_flags & O_NONBLOCK) ||
1505 (info->port.tty->termios->c_cflag & CLOCAL) ||
1506 (info->port.tty->flags & (1 << TTY_IO_ERROR)))
1510 * Set DTR to allow modem to know we're waiting. Do
1511 * not set RTS here - we want to make sure we catch
1512 * the data from the modem.
1514 if (info->port.tty->termios->c_cflag & CBAUD)
1515 uart_set_mctrl(port, TIOCM_DTR);
1518 * and wait for the carrier to indicate that the
1519 * modem is ready for us.
1521 spin_lock_irq(&port->lock);
1522 port->ops->enable_ms(port);
1523 mctrl = port->ops->get_mctrl(port);
1524 spin_unlock_irq(&port->lock);
1525 if (mctrl & TIOCM_CAR)
1528 mutex_unlock(&state->mutex);
1530 mutex_lock(&state->mutex);
1532 if (signal_pending(current))
1535 set_current_state(TASK_RUNNING);
1536 remove_wait_queue(&info->port.open_wait, &wait);
1539 info->port.blocked_open--;
1541 if (signal_pending(current))
1542 return -ERESTARTSYS;
1544 if (!info->port.tty || tty_hung_up_p(filp))
1550 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1552 struct uart_state *state;
1555 state = drv->state + line;
1556 if (mutex_lock_interruptible(&state->mutex)) {
1562 if (!state->port || state->port->flags & UPF_DEAD) {
1567 /* BKL: RACE HERE - LEAK */
1568 /* We should move this into the uart_state structure and kill off
1569 this whole complexity */
1571 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
1573 init_waitqueue_head(&state->info->port.open_wait);
1574 init_waitqueue_head(&state->info->delta_msr_wait);
1577 * Link the info into the other structures.
1579 state->port->info = state->info;
1581 tasklet_init(&state->info->tlet, uart_tasklet_action,
1582 (unsigned long)state);
1592 mutex_unlock(&state->mutex);
1594 return ERR_PTR(ret);
1598 * calls to uart_open are serialised by the BKL in
1599 * fs/char_dev.c:chrdev_open()
1600 * Note that if this fails, then uart_close() _will_ be called.
1602 * In time, we want to scrap the "opening nonpresent ports"
1603 * behaviour and implement an alternative way for setserial
1604 * to set base addresses/ports/types. This will allow us to
1605 * get rid of a certain amount of extra tests.
1607 static int uart_open(struct tty_struct *tty, struct file *filp)
1609 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1610 struct uart_state *state;
1611 int retval, line = tty->index;
1613 BUG_ON(!kernel_locked());
1614 pr_debug("uart_open(%d) called\n", line);
1617 * tty->driver->num won't change, so we won't fail here with
1618 * tty->driver_data set to something non-NULL (and therefore
1619 * we won't get caught by uart_close()).
1622 if (line >= tty->driver->num)
1626 * We take the semaphore inside uart_get to guarantee that we won't
1627 * be re-entered while allocating the info structure, or while we
1628 * request any IRQs that the driver may need. This also has the nice
1629 * side-effect that it delays the action of uart_hangup, so we can
1630 * guarantee that info->port.tty will always contain something reasonable.
1632 state = uart_get(drv, line);
1633 if (IS_ERR(state)) {
1634 retval = PTR_ERR(state);
1639 * Once we set tty->driver_data here, we are guaranteed that
1640 * uart_close() will decrement the driver module use count.
1641 * Any failures from here onwards should not touch the count.
1643 tty->driver_data = state;
1644 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1646 state->info->port.tty = tty;
1649 * If the port is in the middle of closing, bail out now.
1651 if (tty_hung_up_p(filp)) {
1654 mutex_unlock(&state->mutex);
1659 * Make sure the device is in D0 state.
1661 if (state->count == 1)
1662 uart_change_pm(state, 0);
1665 * Start up the serial port.
1667 retval = uart_startup(state, 0);
1670 * If we succeeded, wait until the port is ready.
1673 retval = uart_block_til_ready(filp, state);
1674 mutex_unlock(&state->mutex);
1677 * If this is the first open to succeed, adjust things to suit.
1679 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1680 state->info->flags |= UIF_NORMAL_ACTIVE;
1682 uart_update_termios(state);
1689 static const char *uart_type(struct uart_port *port)
1691 const char *str = NULL;
1693 if (port->ops->type)
1694 str = port->ops->type(port);
1702 #ifdef CONFIG_PROC_FS
1704 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1706 struct uart_state *state = drv->state + i;
1708 struct uart_port *port = state->port;
1710 unsigned int status;
1716 mmio = port->iotype >= UPIO_MEM;
1717 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
1718 port->line, uart_type(port),
1719 mmio ? "mmio:0x" : "port:",
1720 mmio ? (unsigned long long)port->mapbase
1721 : (unsigned long long) port->iobase,
1724 if (port->type == PORT_UNKNOWN) {
1729 if (capable(CAP_SYS_ADMIN)) {
1730 mutex_lock(&state->mutex);
1731 pm_state = state->pm_state;
1733 uart_change_pm(state, 0);
1734 spin_lock_irq(&port->lock);
1735 status = port->ops->get_mctrl(port);
1736 spin_unlock_irq(&port->lock);
1738 uart_change_pm(state, pm_state);
1739 mutex_unlock(&state->mutex);
1741 ret += sprintf(buf + ret, " tx:%d rx:%d",
1742 port->icount.tx, port->icount.rx);
1743 if (port->icount.frame)
1744 ret += sprintf(buf + ret, " fe:%d",
1745 port->icount.frame);
1746 if (port->icount.parity)
1747 ret += sprintf(buf + ret, " pe:%d",
1748 port->icount.parity);
1749 if (port->icount.brk)
1750 ret += sprintf(buf + ret, " brk:%d",
1752 if (port->icount.overrun)
1753 ret += sprintf(buf + ret, " oe:%d",
1754 port->icount.overrun);
1756 #define INFOBIT(bit, str) \
1757 if (port->mctrl & (bit)) \
1758 strncat(stat_buf, (str), sizeof(stat_buf) - \
1759 strlen(stat_buf) - 2)
1760 #define STATBIT(bit, str) \
1761 if (status & (bit)) \
1762 strncat(stat_buf, (str), sizeof(stat_buf) - \
1763 strlen(stat_buf) - 2)
1767 INFOBIT(TIOCM_RTS, "|RTS");
1768 STATBIT(TIOCM_CTS, "|CTS");
1769 INFOBIT(TIOCM_DTR, "|DTR");
1770 STATBIT(TIOCM_DSR, "|DSR");
1771 STATBIT(TIOCM_CAR, "|CD");
1772 STATBIT(TIOCM_RNG, "|RI");
1775 strcat(stat_buf, "\n");
1777 ret += sprintf(buf + ret, stat_buf);
1787 static int uart_read_proc(char *page, char **start, off_t off,
1788 int count, int *eof, void *data)
1790 struct tty_driver *ttydrv = data;
1791 struct uart_driver *drv = ttydrv->driver_state;
1795 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1797 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1798 l = uart_line_info(page + len, drv, i);
1800 if (len + begin > off + count)
1802 if (len + begin < off) {
1809 if (off >= len + begin)
1811 *start = page + (off - begin);
1812 return (count < begin + len - off) ? count : (begin + len - off);
1816 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1818 * uart_console_write - write a console message to a serial port
1819 * @port: the port to write the message
1820 * @s: array of characters
1821 * @count: number of characters in string to write
1822 * @write: function to write character to port
1824 void uart_console_write(struct uart_port *port, const char *s,
1826 void (*putchar)(struct uart_port *, int))
1830 for (i = 0; i < count; i++, s++) {
1832 putchar(port, '\r');
1836 EXPORT_SYMBOL_GPL(uart_console_write);
1839 * Check whether an invalid uart number has been specified, and
1840 * if so, search for the first available port that does have
1843 struct uart_port * __init
1844 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1846 int idx = co->index;
1848 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1849 ports[idx].membase == NULL))
1850 for (idx = 0; idx < nr; idx++)
1851 if (ports[idx].iobase != 0 ||
1852 ports[idx].membase != NULL)
1861 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1862 * @options: pointer to option string
1863 * @baud: pointer to an 'int' variable for the baud rate.
1864 * @parity: pointer to an 'int' variable for the parity.
1865 * @bits: pointer to an 'int' variable for the number of data bits.
1866 * @flow: pointer to an 'int' variable for the flow control character.
1868 * uart_parse_options decodes a string containing the serial console
1869 * options. The format of the string is <baud><parity><bits><flow>,
1873 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1877 *baud = simple_strtoul(s, NULL, 10);
1878 while (*s >= '0' && *s <= '9')
1887 EXPORT_SYMBOL_GPL(uart_parse_options);
1894 static const struct baud_rates baud_rates[] = {
1895 { 921600, B921600 },
1896 { 460800, B460800 },
1897 { 230400, B230400 },
1898 { 115200, B115200 },
1910 * uart_set_options - setup the serial console parameters
1911 * @port: pointer to the serial ports uart_port structure
1912 * @co: console pointer
1914 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1915 * @bits: number of data bits
1916 * @flow: flow control character - 'r' (rts)
1919 uart_set_options(struct uart_port *port, struct console *co,
1920 int baud, int parity, int bits, int flow)
1922 struct ktermios termios;
1923 static struct ktermios dummy;
1927 * Ensure that the serial console lock is initialised
1930 spin_lock_init(&port->lock);
1931 lockdep_set_class(&port->lock, &port_lock_key);
1933 memset(&termios, 0, sizeof(struct ktermios));
1935 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1938 * Construct a cflag setting.
1940 for (i = 0; baud_rates[i].rate; i++)
1941 if (baud_rates[i].rate <= baud)
1944 termios.c_cflag |= baud_rates[i].cflag;
1947 termios.c_cflag |= CS7;
1949 termios.c_cflag |= CS8;
1953 termios.c_cflag |= PARODD;
1956 termios.c_cflag |= PARENB;
1961 termios.c_cflag |= CRTSCTS;
1964 * some uarts on other side don't support no flow control.
1965 * So we set * DTR in host uart to make them happy
1967 port->mctrl |= TIOCM_DTR;
1969 port->ops->set_termios(port, &termios, &dummy);
1971 * Allow the setting of the UART parameters with a NULL console
1975 co->cflag = termios.c_cflag;
1979 EXPORT_SYMBOL_GPL(uart_set_options);
1980 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1982 static void uart_change_pm(struct uart_state *state, int pm_state)
1984 struct uart_port *port = state->port;
1986 if (state->pm_state != pm_state) {
1988 port->ops->pm(port, pm_state, state->pm_state);
1989 state->pm_state = pm_state;
1994 struct uart_port *port;
1995 struct uart_driver *driver;
1998 static int serial_match_port(struct device *dev, void *data)
2000 struct uart_match *match = data;
2001 struct tty_driver *tty_drv = match->driver->tty_driver;
2002 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2005 return dev->devt == devt; /* Actually, only one tty per port */
2008 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
2010 struct uart_state *state = drv->state + port->line;
2011 struct device *tty_dev;
2012 struct uart_match match = {port, drv};
2014 mutex_lock(&state->mutex);
2016 if (!console_suspend_enabled && uart_console(port)) {
2017 /* we're going to avoid suspending serial console */
2018 mutex_unlock(&state->mutex);
2022 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2023 if (device_may_wakeup(tty_dev)) {
2024 enable_irq_wake(port->irq);
2025 put_device(tty_dev);
2026 mutex_unlock(&state->mutex);
2029 port->suspended = 1;
2031 if (state->info && state->info->flags & UIF_INITIALIZED) {
2032 const struct uart_ops *ops = port->ops;
2035 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
2038 spin_lock_irq(&port->lock);
2040 ops->set_mctrl(port, 0);
2042 spin_unlock_irq(&port->lock);
2045 * Wait for the transmitter to empty.
2047 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
2050 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2052 port->dev ? port->dev->bus_id : "",
2053 port->dev ? ": " : "",
2055 drv->tty_driver->name_base + port->line);
2057 ops->shutdown(port);
2061 * Disable the console device before suspending.
2063 if (uart_console(port))
2064 console_stop(port->cons);
2066 uart_change_pm(state, 3);
2068 mutex_unlock(&state->mutex);
2073 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2075 struct uart_state *state = drv->state + port->line;
2076 struct device *tty_dev;
2077 struct uart_match match = {port, drv};
2079 mutex_lock(&state->mutex);
2081 if (!console_suspend_enabled && uart_console(port)) {
2082 /* no need to resume serial console, it wasn't suspended */
2083 mutex_unlock(&state->mutex);
2087 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2088 if (!port->suspended && device_may_wakeup(tty_dev)) {
2089 disable_irq_wake(port->irq);
2090 mutex_unlock(&state->mutex);
2093 port->suspended = 0;
2096 * Re-enable the console device after suspending.
2098 if (uart_console(port)) {
2099 struct ktermios termios;
2102 * First try to use the console cflag setting.
2104 memset(&termios, 0, sizeof(struct ktermios));
2105 termios.c_cflag = port->cons->cflag;
2108 * If that's unset, use the tty termios setting.
2110 if (state->info && state->info->port.tty && termios.c_cflag == 0)
2111 termios = *state->info->port.tty->termios;
2113 uart_change_pm(state, 0);
2114 port->ops->set_termios(port, &termios, NULL);
2115 console_start(port->cons);
2118 if (state->info && state->info->flags & UIF_SUSPENDED) {
2119 const struct uart_ops *ops = port->ops;
2122 uart_change_pm(state, 0);
2123 spin_lock_irq(&port->lock);
2124 ops->set_mctrl(port, 0);
2125 spin_unlock_irq(&port->lock);
2126 ret = ops->startup(port);
2128 uart_change_speed(state, NULL);
2129 spin_lock_irq(&port->lock);
2130 ops->set_mctrl(port, port->mctrl);
2131 ops->start_tx(port);
2132 spin_unlock_irq(&port->lock);
2133 state->info->flags |= UIF_INITIALIZED;
2136 * Failed to resume - maybe hardware went away?
2137 * Clear the "initialized" flag so we won't try
2138 * to call the low level drivers shutdown method.
2140 uart_shutdown(state);
2143 state->info->flags &= ~UIF_SUSPENDED;
2146 mutex_unlock(&state->mutex);
2152 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2156 switch (port->iotype) {
2158 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2161 snprintf(address, sizeof(address),
2162 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2169 snprintf(address, sizeof(address),
2170 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2173 strlcpy(address, "*unknown*", sizeof(address));
2177 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2178 port->dev ? port->dev->bus_id : "",
2179 port->dev ? ": " : "",
2181 drv->tty_driver->name_base + port->line,
2182 address, port->irq, uart_type(port));
2186 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2187 struct uart_port *port)
2192 * If there isn't a port here, don't do anything further.
2194 if (!port->iobase && !port->mapbase && !port->membase)
2198 * Now do the auto configuration stuff. Note that config_port
2199 * is expected to claim the resources and map the port for us.
2201 flags = UART_CONFIG_TYPE;
2202 if (port->flags & UPF_AUTO_IRQ)
2203 flags |= UART_CONFIG_IRQ;
2204 if (port->flags & UPF_BOOT_AUTOCONF) {
2205 port->type = PORT_UNKNOWN;
2206 port->ops->config_port(port, flags);
2209 if (port->type != PORT_UNKNOWN) {
2210 unsigned long flags;
2212 uart_report_port(drv, port);
2214 /* Power up port for set_mctrl() */
2215 uart_change_pm(state, 0);
2218 * Ensure that the modem control lines are de-activated.
2219 * keep the DTR setting that is set in uart_set_options()
2220 * We probably don't need a spinlock around this, but
2222 spin_lock_irqsave(&port->lock, flags);
2223 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2224 spin_unlock_irqrestore(&port->lock, flags);
2227 * If this driver supports console, and it hasn't been
2228 * successfully registered yet, try to re-register it.
2229 * It may be that the port was not available.
2231 if (port->cons && !(port->cons->flags & CON_ENABLED))
2232 register_console(port->cons);
2235 * Power down all ports by default, except the
2236 * console if we have one.
2238 if (!uart_console(port))
2239 uart_change_pm(state, 3);
2243 #ifdef CONFIG_CONSOLE_POLL
2245 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2247 struct uart_driver *drv = driver->driver_state;
2248 struct uart_state *state = drv->state + line;
2249 struct uart_port *port;
2255 if (!state || !state->port)
2259 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2263 uart_parse_options(options, &baud, &parity, &bits, &flow);
2264 return uart_set_options(port, NULL, baud, parity, bits, flow);
2270 static int uart_poll_get_char(struct tty_driver *driver, int line)
2272 struct uart_driver *drv = driver->driver_state;
2273 struct uart_state *state = drv->state + line;
2274 struct uart_port *port;
2276 if (!state || !state->port)
2280 return port->ops->poll_get_char(port);
2283 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2285 struct uart_driver *drv = driver->driver_state;
2286 struct uart_state *state = drv->state + line;
2287 struct uart_port *port;
2289 if (!state || !state->port)
2293 port->ops->poll_put_char(port, ch);
2297 static const struct tty_operations uart_ops = {
2299 .close = uart_close,
2300 .write = uart_write,
2301 .put_char = uart_put_char,
2302 .flush_chars = uart_flush_chars,
2303 .write_room = uart_write_room,
2304 .chars_in_buffer= uart_chars_in_buffer,
2305 .flush_buffer = uart_flush_buffer,
2306 .ioctl = uart_ioctl,
2307 .throttle = uart_throttle,
2308 .unthrottle = uart_unthrottle,
2309 .send_xchar = uart_send_xchar,
2310 .set_termios = uart_set_termios,
2311 .set_ldisc = uart_set_ldisc,
2313 .start = uart_start,
2314 .hangup = uart_hangup,
2315 .break_ctl = uart_break_ctl,
2316 .wait_until_sent= uart_wait_until_sent,
2317 #ifdef CONFIG_PROC_FS
2318 .read_proc = uart_read_proc,
2320 .tiocmget = uart_tiocmget,
2321 .tiocmset = uart_tiocmset,
2322 #ifdef CONFIG_CONSOLE_POLL
2323 .poll_init = uart_poll_init,
2324 .poll_get_char = uart_poll_get_char,
2325 .poll_put_char = uart_poll_put_char,
2330 * uart_register_driver - register a driver with the uart core layer
2331 * @drv: low level driver structure
2333 * Register a uart driver with the core driver. We in turn register
2334 * with the tty layer, and initialise the core driver per-port state.
2336 * We have a proc file in /proc/tty/driver which is named after the
2339 * drv->port should be NULL, and the per-port structures should be
2340 * registered using uart_add_one_port after this call has succeeded.
2342 int uart_register_driver(struct uart_driver *drv)
2344 struct tty_driver *normal = NULL;
2350 * Maybe we should be using a slab cache for this, especially if
2351 * we have a large number of ports to handle.
2353 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2358 normal = alloc_tty_driver(drv->nr);
2362 drv->tty_driver = normal;
2364 normal->owner = drv->owner;
2365 normal->driver_name = drv->driver_name;
2366 normal->name = drv->dev_name;
2367 normal->major = drv->major;
2368 normal->minor_start = drv->minor;
2369 normal->type = TTY_DRIVER_TYPE_SERIAL;
2370 normal->subtype = SERIAL_TYPE_NORMAL;
2371 normal->init_termios = tty_std_termios;
2372 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2373 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2374 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2375 normal->driver_state = drv;
2376 tty_set_operations(normal, &uart_ops);
2379 * Initialise the UART state(s).
2381 for (i = 0; i < drv->nr; i++) {
2382 struct uart_state *state = drv->state + i;
2384 state->close_delay = 500; /* .5 seconds */
2385 state->closing_wait = 30000; /* 30 seconds */
2387 mutex_init(&state->mutex);
2390 retval = tty_register_driver(normal);
2393 put_tty_driver(normal);
2400 * uart_unregister_driver - remove a driver from the uart core layer
2401 * @drv: low level driver structure
2403 * Remove all references to a driver from the core driver. The low
2404 * level driver must have removed all its ports via the
2405 * uart_remove_one_port() if it registered them with uart_add_one_port().
2406 * (ie, drv->port == NULL)
2408 void uart_unregister_driver(struct uart_driver *drv)
2410 struct tty_driver *p = drv->tty_driver;
2411 tty_unregister_driver(p);
2414 drv->tty_driver = NULL;
2417 struct tty_driver *uart_console_device(struct console *co, int *index)
2419 struct uart_driver *p = co->data;
2421 return p->tty_driver;
2425 * uart_add_one_port - attach a driver-defined port structure
2426 * @drv: pointer to the uart low level driver structure for this port
2427 * @port: uart port structure to use for this port.
2429 * This allows the driver to register its own uart_port structure
2430 * with the core driver. The main purpose is to allow the low
2431 * level uart drivers to expand uart_port, rather than having yet
2432 * more levels of structures.
2434 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2436 struct uart_state *state;
2438 struct device *tty_dev;
2440 BUG_ON(in_interrupt());
2442 if (port->line >= drv->nr)
2445 state = drv->state + port->line;
2447 mutex_lock(&port_mutex);
2448 mutex_lock(&state->mutex);
2455 state->pm_state = -1;
2457 port->cons = drv->cons;
2458 port->info = state->info;
2461 * If this port is a console, then the spinlock is already
2464 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2465 spin_lock_init(&port->lock);
2466 lockdep_set_class(&port->lock, &port_lock_key);
2469 uart_configure_port(drv, state, port);
2472 * Register the port whether it's detected or not. This allows
2473 * setserial to be used to alter this ports parameters.
2475 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2476 if (likely(!IS_ERR(tty_dev))) {
2477 device_init_wakeup(tty_dev, 1);
2478 device_set_wakeup_enable(tty_dev, 0);
2480 printk(KERN_ERR "Cannot register tty device on line %d\n",
2484 * Ensure UPF_DEAD is not set.
2486 port->flags &= ~UPF_DEAD;
2489 mutex_unlock(&state->mutex);
2490 mutex_unlock(&port_mutex);
2496 * uart_remove_one_port - detach a driver defined port structure
2497 * @drv: pointer to the uart low level driver structure for this port
2498 * @port: uart port structure for this port
2500 * This unhooks (and hangs up) the specified port structure from the
2501 * core driver. No further calls will be made to the low-level code
2504 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2506 struct uart_state *state = drv->state + port->line;
2507 struct uart_info *info;
2509 BUG_ON(in_interrupt());
2511 if (state->port != port)
2512 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2515 mutex_lock(&port_mutex);
2518 * Mark the port "dead" - this prevents any opens from
2519 * succeeding while we shut down the port.
2521 mutex_lock(&state->mutex);
2522 port->flags |= UPF_DEAD;
2523 mutex_unlock(&state->mutex);
2526 * Remove the devices from the tty layer
2528 tty_unregister_device(drv->tty_driver, port->line);
2531 if (info && info->port.tty)
2532 tty_vhangup(info->port.tty);
2535 * All users of this port should now be disconnected from
2536 * this driver, and the port shut down. We should be the
2537 * only thread fiddling with this port from now on.
2542 * Free the port IO and memory resources, if any.
2544 if (port->type != PORT_UNKNOWN)
2545 port->ops->release_port(port);
2548 * Indicate that there isn't a port here anymore.
2550 port->type = PORT_UNKNOWN;
2553 * Kill the tasklet, and free resources.
2556 tasklet_kill(&info->tlet);
2561 mutex_unlock(&port_mutex);
2567 * Are the two ports equivalent?
2569 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2571 if (port1->iotype != port2->iotype)
2574 switch (port1->iotype) {
2576 return (port1->iobase == port2->iobase);
2578 return (port1->iobase == port2->iobase) &&
2579 (port1->hub6 == port2->hub6);
2585 return (port1->mapbase == port2->mapbase);
2589 EXPORT_SYMBOL(uart_match_port);
2591 EXPORT_SYMBOL(uart_write_wakeup);
2592 EXPORT_SYMBOL(uart_register_driver);
2593 EXPORT_SYMBOL(uart_unregister_driver);
2594 EXPORT_SYMBOL(uart_suspend_port);
2595 EXPORT_SYMBOL(uart_resume_port);
2596 EXPORT_SYMBOL(uart_add_one_port);
2597 EXPORT_SYMBOL(uart_remove_one_port);
2599 MODULE_DESCRIPTION("Serial driver core");
2600 MODULE_LICENSE("GPL");