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/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/serial_core.h>
33 #include <linux/smp_lock.h>
34 #include <linux/device.h>
35 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
36 #include <linux/delay.h>
37 #include <linux/mutex.h>
40 #include <asm/uaccess.h>
43 * This is used to lock changes in serial line configuration.
45 static DEFINE_MUTEX(port_mutex);
48 * lockdep: port->lock is initialized in two places, but we
49 * want only one lock-class:
51 static struct lock_class_key port_lock_key;
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 #define uart_users(state) ((state)->count + (state)->port.blocked_open)
57 #ifdef CONFIG_SERIAL_CORE_CONSOLE
58 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
60 #define uart_console(port) (0)
63 static void uart_change_speed(struct uart_state *state,
64 struct ktermios *old_termios);
65 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
66 static void uart_change_pm(struct uart_state *state, int pm_state);
69 * This routine is used by the interrupt handler to schedule processing in
70 * the software interrupt portion of the driver.
72 void uart_write_wakeup(struct uart_port *port)
74 struct uart_state *state = port->state;
76 * This means you called this function _after_ the port was
77 * closed. No cookie for you.
80 tasklet_schedule(&state->tlet);
83 static void uart_stop(struct tty_struct *tty)
85 struct uart_state *state = tty->driver_data;
86 struct uart_port *port = state->uart_port;
89 spin_lock_irqsave(&port->lock, flags);
90 port->ops->stop_tx(port);
91 spin_unlock_irqrestore(&port->lock, flags);
94 static void __uart_start(struct tty_struct *tty)
96 struct uart_state *state = tty->driver_data;
97 struct uart_port *port = state->uart_port;
99 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
100 !tty->stopped && !tty->hw_stopped)
101 port->ops->start_tx(port);
104 static void uart_start(struct tty_struct *tty)
106 struct uart_state *state = tty->driver_data;
107 struct uart_port *port = state->uart_port;
110 spin_lock_irqsave(&port->lock, flags);
112 spin_unlock_irqrestore(&port->lock, flags);
115 static void uart_tasklet_action(unsigned long data)
117 struct uart_state *state = (struct uart_state *)data;
118 tty_wakeup(state->port.tty);
122 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
127 spin_lock_irqsave(&port->lock, flags);
129 port->mctrl = (old & ~clear) | set;
130 if (old != port->mctrl)
131 port->ops->set_mctrl(port, port->mctrl);
132 spin_unlock_irqrestore(&port->lock, flags);
135 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
136 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
139 * Startup the port. This will be called once per open. All calls
140 * will be serialised by the per-port mutex.
142 static int uart_startup(struct uart_state *state, int init_hw)
144 struct uart_port *uport = state->uart_port;
145 struct tty_port *port = &state->port;
149 if (state->flags & UIF_INITIALIZED)
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
157 set_bit(TTY_IO_ERROR, &port->tty->flags);
159 if (uport->type == PORT_UNKNOWN)
163 * Initialise and allocate the transmit and temporary
166 if (!state->xmit.buf) {
167 /* This is protected by the per port mutex */
168 page = get_zeroed_page(GFP_KERNEL);
172 state->xmit.buf = (unsigned char *) page;
173 uart_circ_clear(&state->xmit);
176 retval = uport->ops->startup(uport);
180 * Initialise the hardware port settings.
182 uart_change_speed(state, NULL);
185 * Setup the RTS and DTR signals once the
186 * port is open and ready to respond.
188 if (port->tty->termios->c_cflag & CBAUD)
189 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
192 if (state->flags & UIF_CTS_FLOW) {
193 spin_lock_irq(&uport->lock);
194 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
195 port->tty->hw_stopped = 1;
196 spin_unlock_irq(&uport->lock);
199 state->flags |= UIF_INITIALIZED;
201 clear_bit(TTY_IO_ERROR, &port->tty->flags);
204 if (retval && capable(CAP_SYS_ADMIN))
211 * This routine will shutdown a serial port; interrupts are disabled, and
212 * DTR is dropped if the hangup on close termio flag is on. Calls to
213 * uart_shutdown are serialised by the per-port semaphore.
215 static void uart_shutdown(struct uart_state *state)
217 struct uart_port *port = state->uart_port;
218 struct tty_struct *tty = state->port.tty;
221 * Set the TTY IO error marker
224 set_bit(TTY_IO_ERROR, &tty->flags);
226 if (state->flags & UIF_INITIALIZED) {
227 state->flags &= ~UIF_INITIALIZED;
230 * Turn off DTR and RTS early.
232 if (!tty || (tty->termios->c_cflag & HUPCL))
233 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
236 * clear delta_msr_wait queue to avoid mem leaks: we may free
237 * the irq here so the queue might never be woken up. Note
238 * that we won't end up waiting on delta_msr_wait again since
239 * any outstanding file descriptors should be pointing at
240 * hung_up_tty_fops now.
242 wake_up_interruptible(&state->delta_msr_wait);
245 * Free the IRQ and disable the port.
247 port->ops->shutdown(port);
250 * Ensure that the IRQ handler isn't running on another CPU.
252 synchronize_irq(port->irq);
256 * kill off our tasklet
258 tasklet_kill(&state->tlet);
261 * Free the transmit buffer page.
263 if (state->xmit.buf) {
264 free_page((unsigned long)state->xmit.buf);
265 state->xmit.buf = NULL;
270 * uart_update_timeout - update per-port FIFO timeout.
271 * @port: uart_port structure describing the port
272 * @cflag: termios cflag value
273 * @baud: speed of the port
275 * Set the port FIFO timeout value. The @cflag value should
276 * reflect the actual hardware settings.
279 uart_update_timeout(struct uart_port *port, unsigned int cflag,
284 /* byte size and parity */
285 switch (cflag & CSIZE) {
306 * The total number of bits to be transmitted in the fifo.
308 bits = bits * port->fifosize;
311 * Figure the timeout to send the above number of bits.
312 * Add .02 seconds of slop
314 port->timeout = (HZ * bits) / baud + HZ/50;
317 EXPORT_SYMBOL(uart_update_timeout);
320 * uart_get_baud_rate - return baud rate for a particular port
321 * @port: uart_port structure describing the port in question.
322 * @termios: desired termios settings.
323 * @old: old termios (or NULL)
324 * @min: minimum acceptable baud rate
325 * @max: maximum acceptable baud rate
327 * Decode the termios structure into a numeric baud rate,
328 * taking account of the magic 38400 baud rate (with spd_*
329 * flags), and mapping the %B0 rate to 9600 baud.
331 * If the new baud rate is invalid, try the old termios setting.
332 * If it's still invalid, we try 9600 baud.
334 * Update the @termios structure to reflect the baud rate
335 * we're actually going to be using. Don't do this for the case
336 * where B0 is requested ("hang up").
339 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
340 struct ktermios *old, unsigned int min, unsigned int max)
342 unsigned int try, baud, altbaud = 38400;
344 upf_t flags = port->flags & UPF_SPD_MASK;
346 if (flags == UPF_SPD_HI)
348 if (flags == UPF_SPD_VHI)
350 if (flags == UPF_SPD_SHI)
352 if (flags == UPF_SPD_WARP)
355 for (try = 0; try < 2; try++) {
356 baud = tty_termios_baud_rate(termios);
359 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
366 * Special case: B0 rate.
373 if (baud >= min && baud <= max)
377 * Oops, the quotient was zero. Try again with
378 * the old baud rate if possible.
380 termios->c_cflag &= ~CBAUD;
382 baud = tty_termios_baud_rate(old);
384 tty_termios_encode_baud_rate(termios,
391 * As a last resort, if the quotient is zero,
392 * default to 9600 bps
395 tty_termios_encode_baud_rate(termios, 9600, 9600);
401 EXPORT_SYMBOL(uart_get_baud_rate);
404 * uart_get_divisor - return uart clock divisor
405 * @port: uart_port structure describing the port.
406 * @baud: desired baud rate
408 * Calculate the uart clock divisor for the port.
411 uart_get_divisor(struct uart_port *port, unsigned int baud)
416 * Old custom speed handling.
418 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
419 quot = port->custom_divisor;
421 quot = (port->uartclk + (8 * baud)) / (16 * baud);
426 EXPORT_SYMBOL(uart_get_divisor);
428 /* FIXME: Consistent locking policy */
430 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
432 struct tty_struct *tty = state->port.tty;
433 struct uart_port *port = state->uart_port;
434 struct ktermios *termios;
437 * If we have no tty, termios, or the port does not exist,
438 * then we can't set the parameters for this port.
440 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
443 termios = tty->termios;
446 * Set flags based on termios cflag
448 if (termios->c_cflag & CRTSCTS)
449 state->flags |= UIF_CTS_FLOW;
451 state->flags &= ~UIF_CTS_FLOW;
453 if (termios->c_cflag & CLOCAL)
454 state->flags &= ~UIF_CHECK_CD;
456 state->flags |= UIF_CHECK_CD;
458 port->ops->set_termios(port, termios, old_termios);
462 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
470 spin_lock_irqsave(&port->lock, flags);
471 if (uart_circ_chars_free(circ) != 0) {
472 circ->buf[circ->head] = c;
473 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
476 spin_unlock_irqrestore(&port->lock, flags);
480 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
482 struct uart_state *state = tty->driver_data;
484 return __uart_put_char(state->uart_port, &state->xmit, ch);
487 static void uart_flush_chars(struct tty_struct *tty)
493 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
495 struct uart_state *state = tty->driver_data;
496 struct uart_port *port;
497 struct circ_buf *circ;
502 * This means you called this function _after_ the port was
503 * closed. No cookie for you.
510 port = state->uart_port;
516 spin_lock_irqsave(&port->lock, flags);
518 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
523 memcpy(circ->buf + circ->head, buf, c);
524 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
529 spin_unlock_irqrestore(&port->lock, flags);
535 static int uart_write_room(struct tty_struct *tty)
537 struct uart_state *state = tty->driver_data;
541 spin_lock_irqsave(&state->uart_port->lock, flags);
542 ret = uart_circ_chars_free(&state->xmit);
543 spin_unlock_irqrestore(&state->uart_port->lock, flags);
547 static int uart_chars_in_buffer(struct tty_struct *tty)
549 struct uart_state *state = tty->driver_data;
553 spin_lock_irqsave(&state->uart_port->lock, flags);
554 ret = uart_circ_chars_pending(&state->xmit);
555 spin_unlock_irqrestore(&state->uart_port->lock, flags);
559 static void uart_flush_buffer(struct tty_struct *tty)
561 struct uart_state *state = tty->driver_data;
562 struct uart_port *port;
566 * This means you called this function _after_ the port was
567 * closed. No cookie for you.
574 port = state->uart_port;
575 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
577 spin_lock_irqsave(&port->lock, flags);
578 uart_circ_clear(&state->xmit);
579 if (port->ops->flush_buffer)
580 port->ops->flush_buffer(port);
581 spin_unlock_irqrestore(&port->lock, flags);
586 * This function is used to send a high-priority XON/XOFF character to
589 static void uart_send_xchar(struct tty_struct *tty, char ch)
591 struct uart_state *state = tty->driver_data;
592 struct uart_port *port = state->uart_port;
595 if (port->ops->send_xchar)
596 port->ops->send_xchar(port, ch);
600 spin_lock_irqsave(&port->lock, flags);
601 port->ops->start_tx(port);
602 spin_unlock_irqrestore(&port->lock, flags);
607 static void uart_throttle(struct tty_struct *tty)
609 struct uart_state *state = tty->driver_data;
612 uart_send_xchar(tty, STOP_CHAR(tty));
614 if (tty->termios->c_cflag & CRTSCTS)
615 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
618 static void uart_unthrottle(struct tty_struct *tty)
620 struct uart_state *state = tty->driver_data;
621 struct uart_port *port = state->uart_port;
627 uart_send_xchar(tty, START_CHAR(tty));
630 if (tty->termios->c_cflag & CRTSCTS)
631 uart_set_mctrl(port, TIOCM_RTS);
634 static int uart_get_info(struct uart_state *state,
635 struct serial_struct __user *retinfo)
637 struct uart_port *port = state->uart_port;
638 struct serial_struct tmp;
640 memset(&tmp, 0, sizeof(tmp));
642 /* Ensure the state we copy is consistent and no hardware changes
644 mutex_lock(&state->mutex);
646 tmp.type = port->type;
647 tmp.line = port->line;
648 tmp.port = port->iobase;
649 if (HIGH_BITS_OFFSET)
650 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
652 tmp.flags = port->flags;
653 tmp.xmit_fifo_size = port->fifosize;
654 tmp.baud_base = port->uartclk / 16;
655 tmp.close_delay = state->port.close_delay / 10;
656 tmp.closing_wait = state->port.closing_wait == USF_CLOSING_WAIT_NONE ?
657 ASYNC_CLOSING_WAIT_NONE :
658 state->port.closing_wait / 10;
659 tmp.custom_divisor = port->custom_divisor;
660 tmp.hub6 = port->hub6;
661 tmp.io_type = port->iotype;
662 tmp.iomem_reg_shift = port->regshift;
663 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
665 mutex_unlock(&state->mutex);
667 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
672 static int uart_set_info(struct uart_state *state,
673 struct serial_struct __user *newinfo)
675 struct serial_struct new_serial;
676 struct uart_port *uport = state->uart_port;
677 struct tty_port *port = &state->port;
678 unsigned long new_port;
679 unsigned int change_irq, change_port, closing_wait;
680 unsigned int old_custom_divisor, close_delay;
681 upf_t old_flags, new_flags;
684 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
687 new_port = new_serial.port;
688 if (HIGH_BITS_OFFSET)
689 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
691 new_serial.irq = irq_canonicalize(new_serial.irq);
692 close_delay = new_serial.close_delay * 10;
693 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
694 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
697 * This semaphore protects state->count. It is also
698 * very useful to prevent opens. Also, take the
699 * port configuration semaphore to make sure that a
700 * module insertion/removal doesn't change anything
703 mutex_lock(&state->mutex);
705 change_irq = !(uport->flags & UPF_FIXED_PORT)
706 && new_serial.irq != uport->irq;
709 * Since changing the 'type' of the port changes its resource
710 * allocations, we should treat type changes the same as
713 change_port = !(uport->flags & UPF_FIXED_PORT)
714 && (new_port != uport->iobase ||
715 (unsigned long)new_serial.iomem_base != uport->mapbase ||
716 new_serial.hub6 != uport->hub6 ||
717 new_serial.io_type != uport->iotype ||
718 new_serial.iomem_reg_shift != uport->regshift ||
719 new_serial.type != uport->type);
721 old_flags = uport->flags;
722 new_flags = new_serial.flags;
723 old_custom_divisor = uport->custom_divisor;
725 if (!capable(CAP_SYS_ADMIN)) {
727 if (change_irq || change_port ||
728 (new_serial.baud_base != uport->uartclk / 16) ||
729 (close_delay != port->close_delay) ||
730 (closing_wait != port->closing_wait) ||
731 (new_serial.xmit_fifo_size &&
732 new_serial.xmit_fifo_size != uport->fifosize) ||
733 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
735 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
736 (new_flags & UPF_USR_MASK));
737 uport->custom_divisor = new_serial.custom_divisor;
742 * Ask the low level driver to verify the settings.
744 if (uport->ops->verify_port)
745 retval = uport->ops->verify_port(uport, &new_serial);
747 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
748 (new_serial.baud_base < 9600))
754 if (change_port || change_irq) {
758 * Make sure that we are the sole user of this port.
760 if (uart_users(state) > 1)
764 * We need to shutdown the serial port at the old
765 * port/type/irq combination.
767 uart_shutdown(state);
771 unsigned long old_iobase, old_mapbase;
772 unsigned int old_type, old_iotype, old_hub6, old_shift;
774 old_iobase = uport->iobase;
775 old_mapbase = uport->mapbase;
776 old_type = uport->type;
777 old_hub6 = uport->hub6;
778 old_iotype = uport->iotype;
779 old_shift = uport->regshift;
782 * Free and release old regions
784 if (old_type != PORT_UNKNOWN)
785 uport->ops->release_port(uport);
787 uport->iobase = new_port;
788 uport->type = new_serial.type;
789 uport->hub6 = new_serial.hub6;
790 uport->iotype = new_serial.io_type;
791 uport->regshift = new_serial.iomem_reg_shift;
792 uport->mapbase = (unsigned long)new_serial.iomem_base;
795 * Claim and map the new regions
797 if (uport->type != PORT_UNKNOWN) {
798 retval = uport->ops->request_port(uport);
800 /* Always success - Jean II */
805 * If we fail to request resources for the
806 * new port, try to restore the old settings.
808 if (retval && old_type != PORT_UNKNOWN) {
809 uport->iobase = old_iobase;
810 uport->type = old_type;
811 uport->hub6 = old_hub6;
812 uport->iotype = old_iotype;
813 uport->regshift = old_shift;
814 uport->mapbase = old_mapbase;
815 retval = uport->ops->request_port(uport);
817 * If we failed to restore the old settings,
821 uport->type = PORT_UNKNOWN;
827 /* Added to return the correct error -Ram Gupta */
833 uport->irq = new_serial.irq;
834 if (!(uport->flags & UPF_FIXED_PORT))
835 uport->uartclk = new_serial.baud_base * 16;
836 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
837 (new_flags & UPF_CHANGE_MASK);
838 uport->custom_divisor = new_serial.custom_divisor;
839 port->close_delay = close_delay;
840 port->closing_wait = closing_wait;
841 if (new_serial.xmit_fifo_size)
842 uport->fifosize = new_serial.xmit_fifo_size;
844 port->tty->low_latency =
845 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
849 if (uport->type == PORT_UNKNOWN)
851 if (state->flags & UIF_INITIALIZED) {
852 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
853 old_custom_divisor != uport->custom_divisor) {
855 * If they're setting up a custom divisor or speed,
856 * instead of clearing it, then bitch about it. No
857 * need to rate-limit; it's CAP_SYS_ADMIN only.
859 if (uport->flags & UPF_SPD_MASK) {
862 "%s sets custom speed on %s. This "
863 "is deprecated.\n", current->comm,
864 tty_name(port->tty, buf));
866 uart_change_speed(state, NULL);
869 retval = uart_startup(state, 1);
871 mutex_unlock(&state->mutex);
877 * uart_get_lsr_info - get line status register info.
878 * Note: uart_ioctl protects us against hangups.
880 static int uart_get_lsr_info(struct uart_state *state,
881 unsigned int __user *value)
883 struct uart_port *uport = state->uart_port;
884 struct tty_port *port = &state->port;
887 result = uport->ops->tx_empty(uport);
890 * If we're about to load something into the transmit
891 * register, we'll pretend the transmitter isn't empty to
892 * avoid a race condition (depending on when the transmit
893 * interrupt happens).
896 ((uart_circ_chars_pending(&state->xmit) > 0) &&
897 !port->tty->stopped && !port->tty->hw_stopped))
898 result &= ~TIOCSER_TEMT;
900 return put_user(result, value);
903 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
905 struct uart_state *state = tty->driver_data;
906 struct uart_port *uport = state->uart_port;
909 mutex_lock(&state->mutex);
910 if ((!file || !tty_hung_up_p(file)) &&
911 !(tty->flags & (1 << TTY_IO_ERROR))) {
912 result = uport->mctrl;
914 spin_lock_irq(&uport->lock);
915 result |= uport->ops->get_mctrl(uport);
916 spin_unlock_irq(&uport->lock);
918 mutex_unlock(&state->mutex);
924 uart_tiocmset(struct tty_struct *tty, struct file *file,
925 unsigned int set, unsigned int clear)
927 struct uart_state *state = tty->driver_data;
928 struct uart_port *uport = state->uart_port;
931 mutex_lock(&state->mutex);
932 if ((!file || !tty_hung_up_p(file)) &&
933 !(tty->flags & (1 << TTY_IO_ERROR))) {
934 uart_update_mctrl(uport, set, clear);
937 mutex_unlock(&state->mutex);
941 static int uart_break_ctl(struct tty_struct *tty, int break_state)
943 struct uart_state *state = tty->driver_data;
944 struct uart_port *uport = state->uart_port;
946 mutex_lock(&state->mutex);
948 if (uport->type != PORT_UNKNOWN)
949 uport->ops->break_ctl(uport, break_state);
951 mutex_unlock(&state->mutex);
955 static int uart_do_autoconfig(struct uart_state *state)
957 struct uart_port *uport = state->uart_port;
960 if (!capable(CAP_SYS_ADMIN))
964 * Take the per-port semaphore. This prevents count from
965 * changing, and hence any extra opens of the port while
966 * we're auto-configuring.
968 if (mutex_lock_interruptible(&state->mutex))
972 if (uart_users(state) == 1) {
973 uart_shutdown(state);
976 * If we already have a port type configured,
977 * we must release its resources.
979 if (uport->type != PORT_UNKNOWN)
980 uport->ops->release_port(uport);
982 flags = UART_CONFIG_TYPE;
983 if (uport->flags & UPF_AUTO_IRQ)
984 flags |= UART_CONFIG_IRQ;
987 * This will claim the ports resources if
990 uport->ops->config_port(uport, flags);
992 ret = uart_startup(state, 1);
994 mutex_unlock(&state->mutex);
999 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1000 * - mask passed in arg for lines of interest
1001 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1002 * Caller should use TIOCGICOUNT to see which one it was
1005 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1007 struct uart_port *uport = state->uart_port;
1008 DECLARE_WAITQUEUE(wait, current);
1009 struct uart_icount cprev, cnow;
1013 * note the counters on entry
1015 spin_lock_irq(&uport->lock);
1016 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1019 * Force modem status interrupts on
1021 uport->ops->enable_ms(uport);
1022 spin_unlock_irq(&uport->lock);
1024 add_wait_queue(&state->delta_msr_wait, &wait);
1026 spin_lock_irq(&uport->lock);
1027 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1028 spin_unlock_irq(&uport->lock);
1030 set_current_state(TASK_INTERRUPTIBLE);
1032 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1033 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1034 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1035 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1042 /* see if a signal did it */
1043 if (signal_pending(current)) {
1051 current->state = TASK_RUNNING;
1052 remove_wait_queue(&state->delta_msr_wait, &wait);
1058 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1059 * Return: write counters to the user passed counter struct
1060 * NB: both 1->0 and 0->1 transitions are counted except for
1061 * RI where only 0->1 is counted.
1063 static int uart_get_count(struct uart_state *state,
1064 struct serial_icounter_struct __user *icnt)
1066 struct serial_icounter_struct icount;
1067 struct uart_icount cnow;
1068 struct uart_port *uport = state->uart_port;
1070 spin_lock_irq(&uport->lock);
1071 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1072 spin_unlock_irq(&uport->lock);
1074 icount.cts = cnow.cts;
1075 icount.dsr = cnow.dsr;
1076 icount.rng = cnow.rng;
1077 icount.dcd = cnow.dcd;
1078 icount.rx = cnow.rx;
1079 icount.tx = cnow.tx;
1080 icount.frame = cnow.frame;
1081 icount.overrun = cnow.overrun;
1082 icount.parity = cnow.parity;
1083 icount.brk = cnow.brk;
1084 icount.buf_overrun = cnow.buf_overrun;
1086 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1090 * Called via sys_ioctl. We can use spin_lock_irq() here.
1093 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1096 struct uart_state *state = tty->driver_data;
1097 void __user *uarg = (void __user *)arg;
1098 int ret = -ENOIOCTLCMD;
1102 * These ioctls don't rely on the hardware to be present.
1106 ret = uart_get_info(state, uarg);
1110 ret = uart_set_info(state, uarg);
1114 ret = uart_do_autoconfig(state);
1117 case TIOCSERGWILD: /* obsolete */
1118 case TIOCSERSWILD: /* obsolete */
1123 if (ret != -ENOIOCTLCMD)
1126 if (tty->flags & (1 << TTY_IO_ERROR)) {
1132 * The following should only be used when hardware is present.
1136 ret = uart_wait_modem_status(state, arg);
1140 ret = uart_get_count(state, uarg);
1144 if (ret != -ENOIOCTLCMD)
1147 mutex_lock(&state->mutex);
1149 if (tty_hung_up_p(filp)) {
1155 * All these rely on hardware being present and need to be
1156 * protected against the tty being hung up.
1159 case TIOCSERGETLSR: /* Get line status register */
1160 ret = uart_get_lsr_info(state, uarg);
1164 struct uart_port *uport = state->uart_port;
1165 if (uport->ops->ioctl)
1166 ret = uport->ops->ioctl(uport, cmd, arg);
1171 mutex_unlock(&state->mutex);
1176 static void uart_set_ldisc(struct tty_struct *tty)
1178 struct uart_state *state = tty->driver_data;
1179 struct uart_port *uport = state->uart_port;
1181 if (uport->ops->set_ldisc)
1182 uport->ops->set_ldisc(uport);
1185 static void uart_set_termios(struct tty_struct *tty,
1186 struct ktermios *old_termios)
1188 struct uart_state *state = tty->driver_data;
1189 unsigned long flags;
1190 unsigned int cflag = tty->termios->c_cflag;
1194 * These are the bits that are used to setup various
1195 * flags in the low level driver. We can ignore the Bfoo
1196 * bits in c_cflag; c_[io]speed will always be set
1197 * appropriately by set_termios() in tty_ioctl.c
1199 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1200 if ((cflag ^ old_termios->c_cflag) == 0 &&
1201 tty->termios->c_ospeed == old_termios->c_ospeed &&
1202 tty->termios->c_ispeed == old_termios->c_ispeed &&
1203 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1207 uart_change_speed(state, old_termios);
1209 /* Handle transition to B0 status */
1210 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1211 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1213 /* Handle transition away from B0 status */
1214 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1215 unsigned int mask = TIOCM_DTR;
1216 if (!(cflag & CRTSCTS) ||
1217 !test_bit(TTY_THROTTLED, &tty->flags))
1219 uart_set_mctrl(state->uart_port, mask);
1222 /* Handle turning off CRTSCTS */
1223 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1224 spin_lock_irqsave(&state->uart_port->lock, flags);
1225 tty->hw_stopped = 0;
1227 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1230 /* Handle turning on CRTSCTS */
1231 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1232 spin_lock_irqsave(&state->uart_port->lock, flags);
1233 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1234 tty->hw_stopped = 1;
1235 state->uart_port->ops->stop_tx(state->uart_port);
1237 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1241 * No need to wake up processes in open wait, since they
1242 * sample the CLOCAL flag once, and don't recheck it.
1243 * XXX It's not clear whether the current behavior is correct
1244 * or not. Hence, this may change.....
1246 if (!(old_termios->c_cflag & CLOCAL) &&
1247 (tty->termios->c_cflag & CLOCAL))
1248 wake_up_interruptible(&state->uart_port.open_wait);
1253 * In 2.4.5, calls to this will be serialized via the BKL in
1254 * linux/drivers/char/tty_io.c:tty_release()
1255 * linux/drivers/char/tty_io.c:do_tty_handup()
1257 static void uart_close(struct tty_struct *tty, struct file *filp)
1259 struct uart_state *state = tty->driver_data;
1260 struct tty_port *port;
1261 struct uart_port *uport;
1263 BUG_ON(!kernel_locked());
1265 uport = state->uart_port;
1266 port = &state->port;
1268 pr_debug("uart_close(%d) called\n", uport->line);
1270 mutex_lock(&state->mutex);
1272 if (tty_hung_up_p(filp))
1275 if ((tty->count == 1) && (state->count != 1)) {
1277 * Uh, oh. tty->count is 1, which means that the tty
1278 * structure will be freed. state->count should always
1279 * be one in these conditions. If it's greater than
1280 * one, we've got real problems, since it means the
1281 * serial port won't be shutdown.
1283 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1284 "state->count is %d\n", state->count);
1287 if (--state->count < 0) {
1288 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1289 tty->name, state->count);
1296 * Now we wait for the transmit buffer to clear; and we notify
1297 * the line discipline to only process XON/XOFF characters by
1298 * setting tty->closing.
1302 if (port->closing_wait != USF_CLOSING_WAIT_NONE)
1303 tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait));
1306 * At this point, we stop accepting input. To do this, we
1307 * disable the receive line status interrupts.
1309 if (state->flags & UIF_INITIALIZED) {
1310 unsigned long flags;
1311 spin_lock_irqsave(&port->lock, flags);
1312 uport->ops->stop_rx(uport);
1313 spin_unlock_irqrestore(&port->lock, flags);
1315 * Before we drop DTR, make sure the UART transmitter
1316 * has completely drained; this is especially
1317 * important if there is a transmit FIFO!
1319 uart_wait_until_sent(tty, uport->timeout);
1322 uart_shutdown(state);
1323 uart_flush_buffer(tty);
1325 tty_ldisc_flush(tty);
1330 if (port->blocked_open) {
1331 if (port->close_delay)
1332 msleep_interruptible(port->close_delay);
1333 } else if (!uart_console(uport)) {
1334 uart_change_pm(state, 3);
1338 * Wake up anyone trying to open this port.
1340 state->flags &= ~UIF_NORMAL_ACTIVE;
1341 wake_up_interruptible(&port->open_wait);
1344 mutex_unlock(&state->mutex);
1347 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1349 struct uart_state *state = tty->driver_data;
1350 struct uart_port *port = state->uart_port;
1351 unsigned long char_time, expire;
1353 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1359 * Set the check interval to be 1/5 of the estimated time to
1360 * send a single character, and make it at least 1. The check
1361 * interval should also be less than the timeout.
1363 * Note: we have to use pretty tight timings here to satisfy
1366 char_time = (port->timeout - HZ/50) / port->fifosize;
1367 char_time = char_time / 5;
1370 if (timeout && timeout < char_time)
1371 char_time = timeout;
1374 * If the transmitter hasn't cleared in twice the approximate
1375 * amount of time to send the entire FIFO, it probably won't
1376 * ever clear. This assumes the UART isn't doing flow
1377 * control, which is currently the case. Hence, if it ever
1378 * takes longer than port->timeout, this is probably due to a
1379 * UART bug of some kind. So, we clamp the timeout parameter at
1382 if (timeout == 0 || timeout > 2 * port->timeout)
1383 timeout = 2 * port->timeout;
1385 expire = jiffies + timeout;
1387 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1388 port->line, jiffies, expire);
1391 * Check whether the transmitter is empty every 'char_time'.
1392 * 'timeout' / 'expire' give us the maximum amount of time
1395 while (!port->ops->tx_empty(port)) {
1396 msleep_interruptible(jiffies_to_msecs(char_time));
1397 if (signal_pending(current))
1399 if (time_after(jiffies, expire))
1402 set_current_state(TASK_RUNNING); /* might not be needed */
1407 * This is called with the BKL held in
1408 * linux/drivers/char/tty_io.c:do_tty_hangup()
1409 * We're called from the eventd thread, so we can sleep for
1410 * a _short_ time only.
1412 static void uart_hangup(struct tty_struct *tty)
1414 struct uart_state *state = tty->driver_data;
1415 struct tty_port *port = &state->port;
1417 BUG_ON(!kernel_locked());
1418 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1420 mutex_lock(&state->mutex);
1421 if (state->flags & UIF_NORMAL_ACTIVE) {
1422 uart_flush_buffer(tty);
1423 uart_shutdown(state);
1425 state->flags &= ~UIF_NORMAL_ACTIVE;
1427 wake_up_interruptible(&port->open_wait);
1428 wake_up_interruptible(&state->delta_msr_wait);
1430 mutex_unlock(&state->mutex);
1434 * Copy across the serial console cflag setting into the termios settings
1435 * for the initial open of the port. This allows continuity between the
1436 * kernel settings, and the settings init adopts when it opens the port
1437 * for the first time.
1439 static void uart_update_termios(struct uart_state *state)
1441 struct tty_struct *tty = state->port.tty;
1442 struct uart_port *port = state->uart_port;
1444 if (uart_console(port) && port->cons->cflag) {
1445 tty->termios->c_cflag = port->cons->cflag;
1446 port->cons->cflag = 0;
1450 * If the device failed to grab its irq resources,
1451 * or some other error occurred, don't try to talk
1452 * to the port hardware.
1454 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1456 * Make termios settings take effect.
1458 uart_change_speed(state, NULL);
1461 * And finally enable the RTS and DTR signals.
1463 if (tty->termios->c_cflag & CBAUD)
1464 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1469 * Block the open until the port is ready. We must be called with
1470 * the per-port semaphore held.
1473 uart_block_til_ready(struct file *filp, struct uart_state *state)
1475 DECLARE_WAITQUEUE(wait, current);
1476 struct uart_port *uport = state->uart_port;
1477 struct tty_port *port = &state->port;
1480 port->blocked_open++;
1483 add_wait_queue(&port->open_wait, &wait);
1485 set_current_state(TASK_INTERRUPTIBLE);
1488 * If we have been hung up, tell userspace/restart open.
1490 if (tty_hung_up_p(filp) || port->tty == NULL)
1494 * If the port has been closed, tell userspace/restart open.
1496 if (!(state->flags & UIF_INITIALIZED))
1500 * If non-blocking mode is set, or CLOCAL mode is set,
1501 * we don't want to wait for the modem status lines to
1502 * indicate that the port is ready.
1504 * Also, if the port is not enabled/configured, we want
1505 * to allow the open to succeed here. Note that we will
1506 * have set TTY_IO_ERROR for a non-existant port.
1508 if ((filp->f_flags & O_NONBLOCK) ||
1509 (port->tty->termios->c_cflag & CLOCAL) ||
1510 (port->tty->flags & (1 << TTY_IO_ERROR)))
1514 * Set DTR to allow modem to know we're waiting. Do
1515 * not set RTS here - we want to make sure we catch
1516 * the data from the modem.
1518 if (port->tty->termios->c_cflag & CBAUD)
1519 uart_set_mctrl(uport, TIOCM_DTR);
1522 * and wait for the carrier to indicate that the
1523 * modem is ready for us.
1525 spin_lock_irq(&uport->lock);
1526 uport->ops->enable_ms(uport);
1527 mctrl = uport->ops->get_mctrl(uport);
1528 spin_unlock_irq(&uport->lock);
1529 if (mctrl & TIOCM_CAR)
1532 mutex_unlock(&state->mutex);
1534 mutex_lock(&state->mutex);
1536 if (signal_pending(current))
1539 set_current_state(TASK_RUNNING);
1540 remove_wait_queue(&port->open_wait, &wait);
1543 port->blocked_open--;
1545 if (signal_pending(current))
1546 return -ERESTARTSYS;
1548 if (!port->tty || tty_hung_up_p(filp))
1554 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1556 struct uart_state *state;
1559 state = drv->state + line;
1560 if (mutex_lock_interruptible(&state->mutex)) {
1566 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1574 mutex_unlock(&state->mutex);
1576 return ERR_PTR(ret);
1580 * calls to uart_open are serialised by the BKL in
1581 * fs/char_dev.c:chrdev_open()
1582 * Note that if this fails, then uart_close() _will_ be called.
1584 * In time, we want to scrap the "opening nonpresent ports"
1585 * behaviour and implement an alternative way for setserial
1586 * to set base addresses/ports/types. This will allow us to
1587 * get rid of a certain amount of extra tests.
1589 static int uart_open(struct tty_struct *tty, struct file *filp)
1591 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1592 struct uart_state *state;
1593 int retval, line = tty->index;
1595 BUG_ON(!kernel_locked());
1596 pr_debug("uart_open(%d) called\n", line);
1599 * tty->driver->num won't change, so we won't fail here with
1600 * tty->driver_data set to something non-NULL (and therefore
1601 * we won't get caught by uart_close()).
1604 if (line >= tty->driver->num)
1608 * We take the semaphore inside uart_get to guarantee that we won't
1609 * be re-entered while allocating the state structure, or while we
1610 * request any IRQs that the driver may need. This also has the nice
1611 * side-effect that it delays the action of uart_hangup, so we can
1612 * guarantee that state->port.tty will always contain something
1615 state = uart_get(drv, line);
1616 if (IS_ERR(state)) {
1617 retval = PTR_ERR(state);
1622 * Once we set tty->driver_data here, we are guaranteed that
1623 * uart_close() will decrement the driver module use count.
1624 * Any failures from here onwards should not touch the count.
1626 tty->driver_data = state;
1627 state->uart_port->state = state;
1628 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1630 state->port.tty = tty;
1633 * If the port is in the middle of closing, bail out now.
1635 if (tty_hung_up_p(filp)) {
1638 mutex_unlock(&state->mutex);
1643 * Make sure the device is in D0 state.
1645 if (state->count == 1)
1646 uart_change_pm(state, 0);
1649 * Start up the serial port.
1651 retval = uart_startup(state, 0);
1654 * If we succeeded, wait until the port is ready.
1657 retval = uart_block_til_ready(filp, state);
1658 mutex_unlock(&state->mutex);
1661 * If this is the first open to succeed, adjust things to suit.
1663 if (retval == 0 && !(state->flags & UIF_NORMAL_ACTIVE)) {
1664 state->flags |= UIF_NORMAL_ACTIVE;
1666 uart_update_termios(state);
1673 static const char *uart_type(struct uart_port *port)
1675 const char *str = NULL;
1677 if (port->ops->type)
1678 str = port->ops->type(port);
1686 #ifdef CONFIG_PROC_FS
1688 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1690 struct uart_state *state = drv->state + i;
1692 struct uart_port *port = state->uart_port;
1694 unsigned int status;
1700 mmio = port->iotype >= UPIO_MEM;
1701 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1702 port->line, uart_type(port),
1703 mmio ? "mmio:0x" : "port:",
1704 mmio ? (unsigned long long)port->mapbase
1705 : (unsigned long long) port->iobase,
1708 if (port->type == PORT_UNKNOWN) {
1713 if (capable(CAP_SYS_ADMIN)) {
1714 mutex_lock(&state->mutex);
1715 pm_state = state->pm_state;
1717 uart_change_pm(state, 0);
1718 spin_lock_irq(&port->lock);
1719 status = port->ops->get_mctrl(port);
1720 spin_unlock_irq(&port->lock);
1722 uart_change_pm(state, pm_state);
1723 mutex_unlock(&state->mutex);
1725 seq_printf(m, " tx:%d rx:%d",
1726 port->icount.tx, port->icount.rx);
1727 if (port->icount.frame)
1728 seq_printf(m, " fe:%d",
1729 port->icount.frame);
1730 if (port->icount.parity)
1731 seq_printf(m, " pe:%d",
1732 port->icount.parity);
1733 if (port->icount.brk)
1734 seq_printf(m, " brk:%d",
1736 if (port->icount.overrun)
1737 seq_printf(m, " oe:%d",
1738 port->icount.overrun);
1740 #define INFOBIT(bit, str) \
1741 if (port->mctrl & (bit)) \
1742 strncat(stat_buf, (str), sizeof(stat_buf) - \
1743 strlen(stat_buf) - 2)
1744 #define STATBIT(bit, str) \
1745 if (status & (bit)) \
1746 strncat(stat_buf, (str), sizeof(stat_buf) - \
1747 strlen(stat_buf) - 2)
1751 INFOBIT(TIOCM_RTS, "|RTS");
1752 STATBIT(TIOCM_CTS, "|CTS");
1753 INFOBIT(TIOCM_DTR, "|DTR");
1754 STATBIT(TIOCM_DSR, "|DSR");
1755 STATBIT(TIOCM_CAR, "|CD");
1756 STATBIT(TIOCM_RNG, "|RI");
1760 seq_puts(m, stat_buf);
1767 static int uart_proc_show(struct seq_file *m, void *v)
1769 struct tty_driver *ttydrv = m->private;
1770 struct uart_driver *drv = ttydrv->driver_state;
1773 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1775 for (i = 0; i < drv->nr; i++)
1776 uart_line_info(m, drv, i);
1780 static int uart_proc_open(struct inode *inode, struct file *file)
1782 return single_open(file, uart_proc_show, PDE(inode)->data);
1785 static const struct file_operations uart_proc_fops = {
1786 .owner = THIS_MODULE,
1787 .open = uart_proc_open,
1789 .llseek = seq_lseek,
1790 .release = single_release,
1794 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1796 * uart_console_write - write a console message to a serial port
1797 * @port: the port to write the message
1798 * @s: array of characters
1799 * @count: number of characters in string to write
1800 * @write: function to write character to port
1802 void uart_console_write(struct uart_port *port, const char *s,
1804 void (*putchar)(struct uart_port *, int))
1808 for (i = 0; i < count; i++, s++) {
1810 putchar(port, '\r');
1814 EXPORT_SYMBOL_GPL(uart_console_write);
1817 * Check whether an invalid uart number has been specified, and
1818 * if so, search for the first available port that does have
1821 struct uart_port * __init
1822 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1824 int idx = co->index;
1826 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1827 ports[idx].membase == NULL))
1828 for (idx = 0; idx < nr; idx++)
1829 if (ports[idx].iobase != 0 ||
1830 ports[idx].membase != NULL)
1839 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1840 * @options: pointer to option string
1841 * @baud: pointer to an 'int' variable for the baud rate.
1842 * @parity: pointer to an 'int' variable for the parity.
1843 * @bits: pointer to an 'int' variable for the number of data bits.
1844 * @flow: pointer to an 'int' variable for the flow control character.
1846 * uart_parse_options decodes a string containing the serial console
1847 * options. The format of the string is <baud><parity><bits><flow>,
1851 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1855 *baud = simple_strtoul(s, NULL, 10);
1856 while (*s >= '0' && *s <= '9')
1865 EXPORT_SYMBOL_GPL(uart_parse_options);
1872 static const struct baud_rates baud_rates[] = {
1873 { 921600, B921600 },
1874 { 460800, B460800 },
1875 { 230400, B230400 },
1876 { 115200, B115200 },
1888 * uart_set_options - setup the serial console parameters
1889 * @port: pointer to the serial ports uart_port structure
1890 * @co: console pointer
1892 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1893 * @bits: number of data bits
1894 * @flow: flow control character - 'r' (rts)
1897 uart_set_options(struct uart_port *port, struct console *co,
1898 int baud, int parity, int bits, int flow)
1900 struct ktermios termios;
1901 static struct ktermios dummy;
1905 * Ensure that the serial console lock is initialised
1908 spin_lock_init(&port->lock);
1909 lockdep_set_class(&port->lock, &port_lock_key);
1911 memset(&termios, 0, sizeof(struct ktermios));
1913 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1916 * Construct a cflag setting.
1918 for (i = 0; baud_rates[i].rate; i++)
1919 if (baud_rates[i].rate <= baud)
1922 termios.c_cflag |= baud_rates[i].cflag;
1925 termios.c_cflag |= CS7;
1927 termios.c_cflag |= CS8;
1931 termios.c_cflag |= PARODD;
1934 termios.c_cflag |= PARENB;
1939 termios.c_cflag |= CRTSCTS;
1942 * some uarts on other side don't support no flow control.
1943 * So we set * DTR in host uart to make them happy
1945 port->mctrl |= TIOCM_DTR;
1947 port->ops->set_termios(port, &termios, &dummy);
1949 * Allow the setting of the UART parameters with a NULL console
1953 co->cflag = termios.c_cflag;
1957 EXPORT_SYMBOL_GPL(uart_set_options);
1958 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1960 static void uart_change_pm(struct uart_state *state, int pm_state)
1962 struct uart_port *port = state->uart_port;
1964 if (state->pm_state != pm_state) {
1966 port->ops->pm(port, pm_state, state->pm_state);
1967 state->pm_state = pm_state;
1972 struct uart_port *port;
1973 struct uart_driver *driver;
1976 static int serial_match_port(struct device *dev, void *data)
1978 struct uart_match *match = data;
1979 struct tty_driver *tty_drv = match->driver->tty_driver;
1980 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1983 return dev->devt == devt; /* Actually, only one tty per port */
1986 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1988 struct uart_state *state = drv->state + port->line;
1989 struct device *tty_dev;
1990 struct uart_match match = {port, drv};
1992 mutex_lock(&state->mutex);
1994 if (!console_suspend_enabled && uart_console(port)) {
1995 /* we're going to avoid suspending serial console */
1996 mutex_unlock(&state->mutex);
2000 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2001 if (device_may_wakeup(tty_dev)) {
2002 enable_irq_wake(port->irq);
2003 put_device(tty_dev);
2004 mutex_unlock(&state->mutex);
2007 port->suspended = 1;
2009 if (state->flags & UIF_INITIALIZED) {
2010 const struct uart_ops *ops = port->ops;
2013 state->flags = (state->flags & ~UIF_INITIALIZED)
2016 spin_lock_irq(&port->lock);
2018 ops->set_mctrl(port, 0);
2020 spin_unlock_irq(&port->lock);
2023 * Wait for the transmitter to empty.
2025 for (tries = 3; !ops->tx_empty(port) && tries; tries--)
2028 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2030 port->dev ? dev_name(port->dev) : "",
2031 port->dev ? ": " : "",
2033 drv->tty_driver->name_base + port->line);
2035 ops->shutdown(port);
2039 * Disable the console device before suspending.
2041 if (uart_console(port))
2042 console_stop(port->cons);
2044 uart_change_pm(state, 3);
2046 mutex_unlock(&state->mutex);
2051 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2053 struct uart_state *state = drv->state + port->line;
2054 struct device *tty_dev;
2055 struct uart_match match = {port, drv};
2057 mutex_lock(&state->mutex);
2059 if (!console_suspend_enabled && uart_console(port)) {
2060 /* no need to resume serial console, it wasn't suspended */
2061 mutex_unlock(&state->mutex);
2065 tty_dev = device_find_child(port->dev, &match, serial_match_port);
2066 if (!port->suspended && device_may_wakeup(tty_dev)) {
2067 disable_irq_wake(port->irq);
2068 mutex_unlock(&state->mutex);
2071 port->suspended = 0;
2074 * Re-enable the console device after suspending.
2076 if (uart_console(port)) {
2077 struct ktermios termios;
2080 * First try to use the console cflag setting.
2082 memset(&termios, 0, sizeof(struct ktermios));
2083 termios.c_cflag = port->cons->cflag;
2086 * If that's unset, use the tty termios setting.
2088 if (state->port.tty && termios.c_cflag == 0)
2089 termios = *state->port.tty->termios;
2091 uart_change_pm(state, 0);
2092 port->ops->set_termios(port, &termios, NULL);
2093 console_start(port->cons);
2096 if (state->flags & UIF_SUSPENDED) {
2097 const struct uart_ops *ops = port->ops;
2100 uart_change_pm(state, 0);
2101 spin_lock_irq(&port->lock);
2102 ops->set_mctrl(port, 0);
2103 spin_unlock_irq(&port->lock);
2104 ret = ops->startup(port);
2106 uart_change_speed(state, NULL);
2107 spin_lock_irq(&port->lock);
2108 ops->set_mctrl(port, port->mctrl);
2109 ops->start_tx(port);
2110 spin_unlock_irq(&port->lock);
2111 state->flags |= UIF_INITIALIZED;
2114 * Failed to resume - maybe hardware went away?
2115 * Clear the "initialized" flag so we won't try
2116 * to call the low level drivers shutdown method.
2118 uart_shutdown(state);
2121 state->flags &= ~UIF_SUSPENDED;
2124 mutex_unlock(&state->mutex);
2130 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2134 switch (port->iotype) {
2136 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2139 snprintf(address, sizeof(address),
2140 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2147 snprintf(address, sizeof(address),
2148 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2151 strlcpy(address, "*unknown*", sizeof(address));
2155 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2156 port->dev ? dev_name(port->dev) : "",
2157 port->dev ? ": " : "",
2159 drv->tty_driver->name_base + port->line,
2160 address, port->irq, uart_type(port));
2164 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2165 struct uart_port *port)
2170 * If there isn't a port here, don't do anything further.
2172 if (!port->iobase && !port->mapbase && !port->membase)
2176 * Now do the auto configuration stuff. Note that config_port
2177 * is expected to claim the resources and map the port for us.
2180 if (port->flags & UPF_AUTO_IRQ)
2181 flags |= UART_CONFIG_IRQ;
2182 if (port->flags & UPF_BOOT_AUTOCONF) {
2183 if (!(port->flags & UPF_FIXED_TYPE)) {
2184 port->type = PORT_UNKNOWN;
2185 flags |= UART_CONFIG_TYPE;
2187 port->ops->config_port(port, flags);
2190 if (port->type != PORT_UNKNOWN) {
2191 unsigned long flags;
2193 uart_report_port(drv, port);
2195 /* Power up port for set_mctrl() */
2196 uart_change_pm(state, 0);
2199 * Ensure that the modem control lines are de-activated.
2200 * keep the DTR setting that is set in uart_set_options()
2201 * We probably don't need a spinlock around this, but
2203 spin_lock_irqsave(&port->lock, flags);
2204 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2205 spin_unlock_irqrestore(&port->lock, flags);
2208 * If this driver supports console, and it hasn't been
2209 * successfully registered yet, try to re-register it.
2210 * It may be that the port was not available.
2212 if (port->cons && !(port->cons->flags & CON_ENABLED))
2213 register_console(port->cons);
2216 * Power down all ports by default, except the
2217 * console if we have one.
2219 if (!uart_console(port))
2220 uart_change_pm(state, 3);
2224 #ifdef CONFIG_CONSOLE_POLL
2226 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2228 struct uart_driver *drv = driver->driver_state;
2229 struct uart_state *state = drv->state + line;
2230 struct uart_port *port;
2236 if (!state || !state->uart_port)
2239 port = state->uart_port;
2240 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2244 uart_parse_options(options, &baud, &parity, &bits, &flow);
2245 return uart_set_options(port, NULL, baud, parity, bits, flow);
2251 static int uart_poll_get_char(struct tty_driver *driver, int line)
2253 struct uart_driver *drv = driver->driver_state;
2254 struct uart_state *state = drv->state + line;
2255 struct uart_port *port;
2257 if (!state || !state->uart_port)
2260 port = state->uart_port;
2261 return port->ops->poll_get_char(port);
2264 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2266 struct uart_driver *drv = driver->driver_state;
2267 struct uart_state *state = drv->state + line;
2268 struct uart_port *port;
2270 if (!state || !state->uart_port)
2273 port = state->uart_port;
2274 port->ops->poll_put_char(port, ch);
2278 static const struct tty_operations uart_ops = {
2280 .close = uart_close,
2281 .write = uart_write,
2282 .put_char = uart_put_char,
2283 .flush_chars = uart_flush_chars,
2284 .write_room = uart_write_room,
2285 .chars_in_buffer= uart_chars_in_buffer,
2286 .flush_buffer = uart_flush_buffer,
2287 .ioctl = uart_ioctl,
2288 .throttle = uart_throttle,
2289 .unthrottle = uart_unthrottle,
2290 .send_xchar = uart_send_xchar,
2291 .set_termios = uart_set_termios,
2292 .set_ldisc = uart_set_ldisc,
2294 .start = uart_start,
2295 .hangup = uart_hangup,
2296 .break_ctl = uart_break_ctl,
2297 .wait_until_sent= uart_wait_until_sent,
2298 #ifdef CONFIG_PROC_FS
2299 .proc_fops = &uart_proc_fops,
2301 .tiocmget = uart_tiocmget,
2302 .tiocmset = uart_tiocmset,
2303 #ifdef CONFIG_CONSOLE_POLL
2304 .poll_init = uart_poll_init,
2305 .poll_get_char = uart_poll_get_char,
2306 .poll_put_char = uart_poll_put_char,
2311 * uart_register_driver - register a driver with the uart core layer
2312 * @drv: low level driver structure
2314 * Register a uart driver with the core driver. We in turn register
2315 * with the tty layer, and initialise the core driver per-port state.
2317 * We have a proc file in /proc/tty/driver which is named after the
2320 * drv->port should be NULL, and the per-port structures should be
2321 * registered using uart_add_one_port after this call has succeeded.
2323 int uart_register_driver(struct uart_driver *drv)
2325 struct tty_driver *normal = NULL;
2331 * Maybe we should be using a slab cache for this, especially if
2332 * we have a large number of ports to handle.
2334 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2339 normal = alloc_tty_driver(drv->nr);
2343 drv->tty_driver = normal;
2345 normal->owner = drv->owner;
2346 normal->driver_name = drv->driver_name;
2347 normal->name = drv->dev_name;
2348 normal->major = drv->major;
2349 normal->minor_start = drv->minor;
2350 normal->type = TTY_DRIVER_TYPE_SERIAL;
2351 normal->subtype = SERIAL_TYPE_NORMAL;
2352 normal->init_termios = tty_std_termios;
2353 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2354 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2355 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2356 normal->driver_state = drv;
2357 tty_set_operations(normal, &uart_ops);
2360 * Initialise the UART state(s).
2362 for (i = 0; i < drv->nr; i++) {
2363 struct uart_state *state = drv->state + i;
2365 mutex_init(&state->mutex);
2367 tty_port_init(&state->port);
2368 state->port.close_delay = 500; /* .5 seconds */
2369 state->port.closing_wait = 30000; /* 30 seconds */
2370 init_waitqueue_head(&state->delta_msr_wait);
2371 tasklet_init(&state->tlet, uart_tasklet_action,
2372 (unsigned long)state);
2375 retval = tty_register_driver(normal);
2378 put_tty_driver(normal);
2385 * uart_unregister_driver - remove a driver from the uart core layer
2386 * @drv: low level driver structure
2388 * Remove all references to a driver from the core driver. The low
2389 * level driver must have removed all its ports via the
2390 * uart_remove_one_port() if it registered them with uart_add_one_port().
2391 * (ie, drv->port == NULL)
2393 void uart_unregister_driver(struct uart_driver *drv)
2395 struct tty_driver *p = drv->tty_driver;
2396 tty_unregister_driver(p);
2399 drv->tty_driver = NULL;
2402 struct tty_driver *uart_console_device(struct console *co, int *index)
2404 struct uart_driver *p = co->data;
2406 return p->tty_driver;
2410 * uart_add_one_port - attach a driver-defined port structure
2411 * @drv: pointer to the uart low level driver structure for this port
2412 * @port: uart port structure to use for this port.
2414 * This allows the driver to register its own uart_port structure
2415 * with the core driver. The main purpose is to allow the low
2416 * level uart drivers to expand uart_port, rather than having yet
2417 * more levels of structures.
2419 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2421 struct uart_state *state;
2423 struct device *tty_dev;
2425 BUG_ON(in_interrupt());
2427 if (port->line >= drv->nr)
2430 state = drv->state + port->line;
2432 mutex_lock(&port_mutex);
2433 mutex_lock(&state->mutex);
2434 if (state->uart_port) {
2439 state->uart_port = port;
2440 state->pm_state = -1;
2442 port->cons = drv->cons;
2443 port->state = state;
2446 * If this port is a console, then the spinlock is already
2449 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2450 spin_lock_init(&port->lock);
2451 lockdep_set_class(&port->lock, &port_lock_key);
2454 uart_configure_port(drv, state, port);
2457 * Register the port whether it's detected or not. This allows
2458 * setserial to be used to alter this ports parameters.
2460 tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
2461 if (likely(!IS_ERR(tty_dev))) {
2462 device_init_wakeup(tty_dev, 1);
2463 device_set_wakeup_enable(tty_dev, 0);
2465 printk(KERN_ERR "Cannot register tty device on line %d\n",
2469 * Ensure UPF_DEAD is not set.
2471 port->flags &= ~UPF_DEAD;
2474 mutex_unlock(&state->mutex);
2475 mutex_unlock(&port_mutex);
2481 * uart_remove_one_port - detach a driver defined port structure
2482 * @drv: pointer to the uart low level driver structure for this port
2483 * @port: uart port structure for this port
2485 * This unhooks (and hangs up) the specified port structure from the
2486 * core driver. No further calls will be made to the low-level code
2489 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2491 struct uart_state *state = drv->state + port->line;
2493 BUG_ON(in_interrupt());
2495 if (state->uart_port != port)
2496 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2497 state->uart_port, port);
2499 mutex_lock(&port_mutex);
2502 * Mark the port "dead" - this prevents any opens from
2503 * succeeding while we shut down the port.
2505 mutex_lock(&state->mutex);
2506 port->flags |= UPF_DEAD;
2507 mutex_unlock(&state->mutex);
2510 * Remove the devices from the tty layer
2512 tty_unregister_device(drv->tty_driver, port->line);
2514 if (state->port.tty)
2515 tty_vhangup(state->port.tty);
2518 * Free the port IO and memory resources, if any.
2520 if (port->type != PORT_UNKNOWN)
2521 port->ops->release_port(port);
2524 * Indicate that there isn't a port here anymore.
2526 port->type = PORT_UNKNOWN;
2529 * Kill the tasklet, and free resources.
2531 tasklet_kill(&state->tlet);
2533 state->uart_port = NULL;
2534 mutex_unlock(&port_mutex);
2540 * Are the two ports equivalent?
2542 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2544 if (port1->iotype != port2->iotype)
2547 switch (port1->iotype) {
2549 return (port1->iobase == port2->iobase);
2551 return (port1->iobase == port2->iobase) &&
2552 (port1->hub6 == port2->hub6);
2558 return (port1->mapbase == port2->mapbase);
2562 EXPORT_SYMBOL(uart_match_port);
2564 EXPORT_SYMBOL(uart_write_wakeup);
2565 EXPORT_SYMBOL(uart_register_driver);
2566 EXPORT_SYMBOL(uart_unregister_driver);
2567 EXPORT_SYMBOL(uart_suspend_port);
2568 EXPORT_SYMBOL(uart_resume_port);
2569 EXPORT_SYMBOL(uart_add_one_port);
2570 EXPORT_SYMBOL(uart_remove_one_port);
2572 MODULE_DESCRIPTION("Serial driver core");
2573 MODULE_LICENSE("GPL");