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/config.h>
26 #include <linux/module.h>
27 #include <linux/tty.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/console.h>
31 #include <linux/serial_core.h>
32 #include <linux/smp_lock.h>
33 #include <linux/device.h>
34 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
43 #define DPRINTK(x...) printk(x)
45 #define DPRINTK(x...) do { } while (0)
49 * This is used to lock changes in serial line configuration.
51 static DEFINE_MUTEX(port_mutex);
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
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, struct termios *old_termios);
64 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
65 static void uart_change_pm(struct uart_state *state, int pm_state);
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
71 void uart_write_wakeup(struct uart_port *port)
73 struct uart_info *info = port->info;
74 tasklet_schedule(&info->tlet);
77 static void uart_stop(struct tty_struct *tty)
79 struct uart_state *state = tty->driver_data;
80 struct uart_port *port = state->port;
83 spin_lock_irqsave(&port->lock, flags);
84 port->ops->stop_tx(port);
85 spin_unlock_irqrestore(&port->lock, flags);
88 static void __uart_start(struct tty_struct *tty)
90 struct uart_state *state = tty->driver_data;
91 struct uart_port *port = state->port;
93 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
94 !tty->stopped && !tty->hw_stopped)
95 port->ops->start_tx(port);
98 static void uart_start(struct tty_struct *tty)
100 struct uart_state *state = tty->driver_data;
101 struct uart_port *port = state->port;
104 spin_lock_irqsave(&port->lock, flags);
106 spin_unlock_irqrestore(&port->lock, flags);
109 static void uart_tasklet_action(unsigned long data)
111 struct uart_state *state = (struct uart_state *)data;
112 tty_wakeup(state->info->tty);
116 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
121 spin_lock_irqsave(&port->lock, flags);
123 port->mctrl = (old & ~clear) | set;
124 if (old != port->mctrl)
125 port->ops->set_mctrl(port, port->mctrl);
126 spin_unlock_irqrestore(&port->lock, flags);
129 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
130 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
133 * Startup the port. This will be called once per open. All calls
134 * will be serialised by the per-port semaphore.
136 static int uart_startup(struct uart_state *state, int init_hw)
138 struct uart_info *info = state->info;
139 struct uart_port *port = state->port;
143 if (info->flags & UIF_INITIALIZED)
147 * Set the TTY IO error marker - we will only clear this
148 * once we have successfully opened the port. Also set
149 * up the tty->alt_speed kludge
151 set_bit(TTY_IO_ERROR, &info->tty->flags);
153 if (port->type == PORT_UNKNOWN)
157 * Initialise and allocate the transmit and temporary
160 if (!info->xmit.buf) {
161 page = get_zeroed_page(GFP_KERNEL);
165 info->xmit.buf = (unsigned char *) page;
166 uart_circ_clear(&info->xmit);
169 retval = port->ops->startup(port);
173 * Initialise the hardware port settings.
175 uart_change_speed(state, NULL);
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (info->tty->termios->c_cflag & CBAUD)
182 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
185 if (info->flags & UIF_CTS_FLOW) {
186 spin_lock_irq(&port->lock);
187 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
188 info->tty->hw_stopped = 1;
189 spin_unlock_irq(&port->lock);
192 info->flags |= UIF_INITIALIZED;
194 clear_bit(TTY_IO_ERROR, &info->tty->flags);
197 if (retval && capable(CAP_SYS_ADMIN))
204 * This routine will shutdown a serial port; interrupts are disabled, and
205 * DTR is dropped if the hangup on close termio flag is on. Calls to
206 * uart_shutdown are serialised by the per-port semaphore.
208 static void uart_shutdown(struct uart_state *state)
210 struct uart_info *info = state->info;
211 struct uart_port *port = state->port;
214 * Set the TTY IO error marker
217 set_bit(TTY_IO_ERROR, &info->tty->flags);
219 if (info->flags & UIF_INITIALIZED) {
220 info->flags &= ~UIF_INITIALIZED;
223 * Turn off DTR and RTS early.
225 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
226 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
229 * clear delta_msr_wait queue to avoid mem leaks: we may free
230 * the irq here so the queue might never be woken up. Note
231 * that we won't end up waiting on delta_msr_wait again since
232 * any outstanding file descriptors should be pointing at
233 * hung_up_tty_fops now.
235 wake_up_interruptible(&info->delta_msr_wait);
238 * Free the IRQ and disable the port.
240 port->ops->shutdown(port);
243 * Ensure that the IRQ handler isn't running on another CPU.
245 synchronize_irq(port->irq);
249 * kill off our tasklet
251 tasklet_kill(&info->tlet);
254 * Free the transmit buffer page.
256 if (info->xmit.buf) {
257 free_page((unsigned long)info->xmit.buf);
258 info->xmit.buf = NULL;
263 * uart_update_timeout - update per-port FIFO timeout.
264 * @port: uart_port structure describing the port
265 * @cflag: termios cflag value
266 * @baud: speed of the port
268 * Set the port FIFO timeout value. The @cflag value should
269 * reflect the actual hardware settings.
272 uart_update_timeout(struct uart_port *port, unsigned int cflag,
277 /* byte size and parity */
278 switch (cflag & CSIZE) {
299 * The total number of bits to be transmitted in the fifo.
301 bits = bits * port->fifosize;
304 * Figure the timeout to send the above number of bits.
305 * Add .02 seconds of slop
307 port->timeout = (HZ * bits) / baud + HZ/50;
310 EXPORT_SYMBOL(uart_update_timeout);
313 * uart_get_baud_rate - return baud rate for a particular port
314 * @port: uart_port structure describing the port in question.
315 * @termios: desired termios settings.
316 * @old: old termios (or NULL)
317 * @min: minimum acceptable baud rate
318 * @max: maximum acceptable baud rate
320 * Decode the termios structure into a numeric baud rate,
321 * taking account of the magic 38400 baud rate (with spd_*
322 * flags), and mapping the %B0 rate to 9600 baud.
324 * If the new baud rate is invalid, try the old termios setting.
325 * If it's still invalid, we try 9600 baud.
327 * Update the @termios structure to reflect the baud rate
328 * we're actually going to be using.
331 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
332 struct termios *old, unsigned int min, unsigned int max)
334 unsigned int try, baud, altbaud = 38400;
335 unsigned int flags = port->flags & UPF_SPD_MASK;
337 if (flags == UPF_SPD_HI)
339 if (flags == UPF_SPD_VHI)
341 if (flags == UPF_SPD_SHI)
343 if (flags == UPF_SPD_WARP)
346 for (try = 0; try < 2; try++) {
347 baud = tty_termios_baud_rate(termios);
350 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Special case: B0 rate.
362 if (baud >= min && baud <= max)
366 * Oops, the quotient was zero. Try again with
367 * the old baud rate if possible.
369 termios->c_cflag &= ~CBAUD;
371 termios->c_cflag |= old->c_cflag & CBAUD;
377 * As a last resort, if the quotient is zero,
378 * default to 9600 bps
380 termios->c_cflag |= B9600;
386 EXPORT_SYMBOL(uart_get_baud_rate);
389 * uart_get_divisor - return uart clock divisor
390 * @port: uart_port structure describing the port.
391 * @baud: desired baud rate
393 * Calculate the uart clock divisor for the port.
396 uart_get_divisor(struct uart_port *port, unsigned int baud)
401 * Old custom speed handling.
403 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
404 quot = port->custom_divisor;
406 quot = (port->uartclk + (8 * baud)) / (16 * baud);
411 EXPORT_SYMBOL(uart_get_divisor);
414 uart_change_speed(struct uart_state *state, struct termios *old_termios)
416 struct tty_struct *tty = state->info->tty;
417 struct uart_port *port = state->port;
418 struct termios *termios;
421 * If we have no tty, termios, or the port does not exist,
422 * then we can't set the parameters for this port.
424 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
427 termios = tty->termios;
430 * Set flags based on termios cflag
432 if (termios->c_cflag & CRTSCTS)
433 state->info->flags |= UIF_CTS_FLOW;
435 state->info->flags &= ~UIF_CTS_FLOW;
437 if (termios->c_cflag & CLOCAL)
438 state->info->flags &= ~UIF_CHECK_CD;
440 state->info->flags |= UIF_CHECK_CD;
442 port->ops->set_termios(port, termios, old_termios);
446 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
453 spin_lock_irqsave(&port->lock, flags);
454 if (uart_circ_chars_free(circ) != 0) {
455 circ->buf[circ->head] = c;
456 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
458 spin_unlock_irqrestore(&port->lock, flags);
461 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
463 struct uart_state *state = tty->driver_data;
465 __uart_put_char(state->port, &state->info->xmit, ch);
468 static void uart_flush_chars(struct tty_struct *tty)
474 uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
476 struct uart_state *state = tty->driver_data;
477 struct uart_port *port = state->port;
478 struct circ_buf *circ = &state->info->xmit;
485 spin_lock_irqsave(&port->lock, flags);
487 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
492 memcpy(circ->buf + circ->head, buf, c);
493 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
498 spin_unlock_irqrestore(&port->lock, flags);
504 static int uart_write_room(struct tty_struct *tty)
506 struct uart_state *state = tty->driver_data;
508 return uart_circ_chars_free(&state->info->xmit);
511 static int uart_chars_in_buffer(struct tty_struct *tty)
513 struct uart_state *state = tty->driver_data;
515 return uart_circ_chars_pending(&state->info->xmit);
518 static void uart_flush_buffer(struct tty_struct *tty)
520 struct uart_state *state = tty->driver_data;
521 struct uart_port *port = state->port;
524 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
526 spin_lock_irqsave(&port->lock, flags);
527 uart_circ_clear(&state->info->xmit);
528 spin_unlock_irqrestore(&port->lock, flags);
533 * This function is used to send a high-priority XON/XOFF character to
536 static void uart_send_xchar(struct tty_struct *tty, char ch)
538 struct uart_state *state = tty->driver_data;
539 struct uart_port *port = state->port;
542 if (port->ops->send_xchar)
543 port->ops->send_xchar(port, ch);
547 spin_lock_irqsave(&port->lock, flags);
548 port->ops->start_tx(port);
549 spin_unlock_irqrestore(&port->lock, flags);
554 static void uart_throttle(struct tty_struct *tty)
556 struct uart_state *state = tty->driver_data;
559 uart_send_xchar(tty, STOP_CHAR(tty));
561 if (tty->termios->c_cflag & CRTSCTS)
562 uart_clear_mctrl(state->port, TIOCM_RTS);
565 static void uart_unthrottle(struct tty_struct *tty)
567 struct uart_state *state = tty->driver_data;
568 struct uart_port *port = state->port;
574 uart_send_xchar(tty, START_CHAR(tty));
577 if (tty->termios->c_cflag & CRTSCTS)
578 uart_set_mctrl(port, TIOCM_RTS);
581 static int uart_get_info(struct uart_state *state,
582 struct serial_struct __user *retinfo)
584 struct uart_port *port = state->port;
585 struct serial_struct tmp;
587 memset(&tmp, 0, sizeof(tmp));
588 tmp.type = port->type;
589 tmp.line = port->line;
590 tmp.port = port->iobase;
591 if (HIGH_BITS_OFFSET)
592 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
594 tmp.flags = port->flags;
595 tmp.xmit_fifo_size = port->fifosize;
596 tmp.baud_base = port->uartclk / 16;
597 tmp.close_delay = state->close_delay / 10;
598 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
599 ASYNC_CLOSING_WAIT_NONE :
600 state->closing_wait / 10;
601 tmp.custom_divisor = port->custom_divisor;
602 tmp.hub6 = port->hub6;
603 tmp.io_type = port->iotype;
604 tmp.iomem_reg_shift = port->regshift;
605 tmp.iomem_base = (void *)port->mapbase;
607 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
612 static int uart_set_info(struct uart_state *state,
613 struct serial_struct __user *newinfo)
615 struct serial_struct new_serial;
616 struct uart_port *port = state->port;
617 unsigned long new_port;
618 unsigned int change_irq, change_port, old_flags, closing_wait;
619 unsigned int old_custom_divisor, close_delay;
622 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
625 new_port = new_serial.port;
626 if (HIGH_BITS_OFFSET)
627 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
629 new_serial.irq = irq_canonicalize(new_serial.irq);
630 close_delay = new_serial.close_delay * 10;
631 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
632 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
635 * This semaphore protects state->count. It is also
636 * very useful to prevent opens. Also, take the
637 * port configuration semaphore to make sure that a
638 * module insertion/removal doesn't change anything
641 mutex_lock(&state->mutex);
643 change_irq = new_serial.irq != port->irq;
646 * Since changing the 'type' of the port changes its resource
647 * allocations, we should treat type changes the same as
650 change_port = new_port != port->iobase ||
651 (unsigned long)new_serial.iomem_base != port->mapbase ||
652 new_serial.hub6 != port->hub6 ||
653 new_serial.io_type != port->iotype ||
654 new_serial.iomem_reg_shift != port->regshift ||
655 new_serial.type != port->type;
657 old_flags = port->flags;
658 old_custom_divisor = port->custom_divisor;
660 if (!capable(CAP_SYS_ADMIN)) {
662 if (change_irq || change_port ||
663 (new_serial.baud_base != port->uartclk / 16) ||
664 (close_delay != state->close_delay) ||
665 (closing_wait != state->closing_wait) ||
666 (new_serial.xmit_fifo_size != port->fifosize) ||
667 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
669 port->flags = ((port->flags & ~UPF_USR_MASK) |
670 (new_serial.flags & UPF_USR_MASK));
671 port->custom_divisor = new_serial.custom_divisor;
676 * Ask the low level driver to verify the settings.
678 if (port->ops->verify_port)
679 retval = port->ops->verify_port(port, &new_serial);
681 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
682 (new_serial.baud_base < 9600))
688 if (change_port || change_irq) {
692 * Make sure that we are the sole user of this port.
694 if (uart_users(state) > 1)
698 * We need to shutdown the serial port at the old
699 * port/type/irq combination.
701 uart_shutdown(state);
705 unsigned long old_iobase, old_mapbase;
706 unsigned int old_type, old_iotype, old_hub6, old_shift;
708 old_iobase = port->iobase;
709 old_mapbase = port->mapbase;
710 old_type = port->type;
711 old_hub6 = port->hub6;
712 old_iotype = port->iotype;
713 old_shift = port->regshift;
716 * Free and release old regions
718 if (old_type != PORT_UNKNOWN)
719 port->ops->release_port(port);
721 port->iobase = new_port;
722 port->type = new_serial.type;
723 port->hub6 = new_serial.hub6;
724 port->iotype = new_serial.io_type;
725 port->regshift = new_serial.iomem_reg_shift;
726 port->mapbase = (unsigned long)new_serial.iomem_base;
729 * Claim and map the new regions
731 if (port->type != PORT_UNKNOWN) {
732 retval = port->ops->request_port(port);
734 /* Always success - Jean II */
739 * If we fail to request resources for the
740 * new port, try to restore the old settings.
742 if (retval && old_type != PORT_UNKNOWN) {
743 port->iobase = old_iobase;
744 port->type = old_type;
745 port->hub6 = old_hub6;
746 port->iotype = old_iotype;
747 port->regshift = old_shift;
748 port->mapbase = old_mapbase;
749 retval = port->ops->request_port(port);
751 * If we failed to restore the old settings,
755 port->type = PORT_UNKNOWN;
764 port->irq = new_serial.irq;
765 port->uartclk = new_serial.baud_base * 16;
766 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
767 (new_serial.flags & UPF_CHANGE_MASK);
768 port->custom_divisor = new_serial.custom_divisor;
769 state->close_delay = close_delay;
770 state->closing_wait = closing_wait;
771 port->fifosize = new_serial.xmit_fifo_size;
772 if (state->info->tty)
773 state->info->tty->low_latency =
774 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
778 if (port->type == PORT_UNKNOWN)
780 if (state->info->flags & UIF_INITIALIZED) {
781 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
782 old_custom_divisor != port->custom_divisor) {
784 * If they're setting up a custom divisor or speed,
785 * instead of clearing it, then bitch about it. No
786 * need to rate-limit; it's CAP_SYS_ADMIN only.
788 if (port->flags & UPF_SPD_MASK) {
791 "%s sets custom speed on %s. This "
792 "is deprecated.\n", current->comm,
793 tty_name(state->info->tty, buf));
795 uart_change_speed(state, NULL);
798 retval = uart_startup(state, 1);
800 mutex_unlock(&state->mutex);
806 * uart_get_lsr_info - get line status register info.
807 * Note: uart_ioctl protects us against hangups.
809 static int uart_get_lsr_info(struct uart_state *state,
810 unsigned int __user *value)
812 struct uart_port *port = state->port;
815 result = port->ops->tx_empty(port);
818 * If we're about to load something into the transmit
819 * register, we'll pretend the transmitter isn't empty to
820 * avoid a race condition (depending on when the transmit
821 * interrupt happens).
824 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
825 !state->info->tty->stopped && !state->info->tty->hw_stopped))
826 result &= ~TIOCSER_TEMT;
828 return put_user(result, value);
831 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
833 struct uart_state *state = tty->driver_data;
834 struct uart_port *port = state->port;
837 mutex_lock(&state->mutex);
838 if ((!file || !tty_hung_up_p(file)) &&
839 !(tty->flags & (1 << TTY_IO_ERROR))) {
840 result = port->mctrl;
842 spin_lock_irq(&port->lock);
843 result |= port->ops->get_mctrl(port);
844 spin_unlock_irq(&port->lock);
846 mutex_unlock(&state->mutex);
852 uart_tiocmset(struct tty_struct *tty, struct file *file,
853 unsigned int set, unsigned int clear)
855 struct uart_state *state = tty->driver_data;
856 struct uart_port *port = state->port;
859 mutex_lock(&state->mutex);
860 if ((!file || !tty_hung_up_p(file)) &&
861 !(tty->flags & (1 << TTY_IO_ERROR))) {
862 uart_update_mctrl(port, set, clear);
865 mutex_unlock(&state->mutex);
869 static void uart_break_ctl(struct tty_struct *tty, int break_state)
871 struct uart_state *state = tty->driver_data;
872 struct uart_port *port = state->port;
874 BUG_ON(!kernel_locked());
876 mutex_lock(&state->mutex);
878 if (port->type != PORT_UNKNOWN)
879 port->ops->break_ctl(port, break_state);
881 mutex_unlock(&state->mutex);
884 static int uart_do_autoconfig(struct uart_state *state)
886 struct uart_port *port = state->port;
889 if (!capable(CAP_SYS_ADMIN))
893 * Take the per-port semaphore. This prevents count from
894 * changing, and hence any extra opens of the port while
895 * we're auto-configuring.
897 if (mutex_lock_interruptible(&state->mutex))
901 if (uart_users(state) == 1) {
902 uart_shutdown(state);
905 * If we already have a port type configured,
906 * we must release its resources.
908 if (port->type != PORT_UNKNOWN)
909 port->ops->release_port(port);
911 flags = UART_CONFIG_TYPE;
912 if (port->flags & UPF_AUTO_IRQ)
913 flags |= UART_CONFIG_IRQ;
916 * This will claim the ports resources if
919 port->ops->config_port(port, flags);
921 ret = uart_startup(state, 1);
923 mutex_unlock(&state->mutex);
928 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
929 * - mask passed in arg for lines of interest
930 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
931 * Caller should use TIOCGICOUNT to see which one it was
934 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
936 struct uart_port *port = state->port;
937 DECLARE_WAITQUEUE(wait, current);
938 struct uart_icount cprev, cnow;
942 * note the counters on entry
944 spin_lock_irq(&port->lock);
945 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
948 * Force modem status interrupts on
950 port->ops->enable_ms(port);
951 spin_unlock_irq(&port->lock);
953 add_wait_queue(&state->info->delta_msr_wait, &wait);
955 spin_lock_irq(&port->lock);
956 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
957 spin_unlock_irq(&port->lock);
959 set_current_state(TASK_INTERRUPTIBLE);
961 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
962 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
963 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
964 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
971 /* see if a signal did it */
972 if (signal_pending(current)) {
980 current->state = TASK_RUNNING;
981 remove_wait_queue(&state->info->delta_msr_wait, &wait);
987 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
988 * Return: write counters to the user passed counter struct
989 * NB: both 1->0 and 0->1 transitions are counted except for
990 * RI where only 0->1 is counted.
992 static int uart_get_count(struct uart_state *state,
993 struct serial_icounter_struct __user *icnt)
995 struct serial_icounter_struct icount;
996 struct uart_icount cnow;
997 struct uart_port *port = state->port;
999 spin_lock_irq(&port->lock);
1000 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1001 spin_unlock_irq(&port->lock);
1003 icount.cts = cnow.cts;
1004 icount.dsr = cnow.dsr;
1005 icount.rng = cnow.rng;
1006 icount.dcd = cnow.dcd;
1007 icount.rx = cnow.rx;
1008 icount.tx = cnow.tx;
1009 icount.frame = cnow.frame;
1010 icount.overrun = cnow.overrun;
1011 icount.parity = cnow.parity;
1012 icount.brk = cnow.brk;
1013 icount.buf_overrun = cnow.buf_overrun;
1015 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1019 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1022 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1025 struct uart_state *state = tty->driver_data;
1026 void __user *uarg = (void __user *)arg;
1027 int ret = -ENOIOCTLCMD;
1029 BUG_ON(!kernel_locked());
1032 * These ioctls don't rely on the hardware to be present.
1036 ret = uart_get_info(state, uarg);
1040 ret = uart_set_info(state, uarg);
1044 ret = uart_do_autoconfig(state);
1047 case TIOCSERGWILD: /* obsolete */
1048 case TIOCSERSWILD: /* obsolete */
1053 if (ret != -ENOIOCTLCMD)
1056 if (tty->flags & (1 << TTY_IO_ERROR)) {
1062 * The following should only be used when hardware is present.
1066 ret = uart_wait_modem_status(state, arg);
1070 ret = uart_get_count(state, uarg);
1074 if (ret != -ENOIOCTLCMD)
1077 mutex_lock(&state->mutex);
1079 if (tty_hung_up_p(filp)) {
1085 * All these rely on hardware being present and need to be
1086 * protected against the tty being hung up.
1089 case TIOCSERGETLSR: /* Get line status register */
1090 ret = uart_get_lsr_info(state, uarg);
1094 struct uart_port *port = state->port;
1095 if (port->ops->ioctl)
1096 ret = port->ops->ioctl(port, cmd, arg);
1101 mutex_unlock(&state->mutex);
1106 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1108 struct uart_state *state = tty->driver_data;
1109 unsigned long flags;
1110 unsigned int cflag = tty->termios->c_cflag;
1112 BUG_ON(!kernel_locked());
1115 * These are the bits that are used to setup various
1116 * flags in the low level driver.
1118 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1120 if ((cflag ^ old_termios->c_cflag) == 0 &&
1121 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1124 uart_change_speed(state, old_termios);
1126 /* Handle transition to B0 status */
1127 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1128 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1130 /* Handle transition away from B0 status */
1131 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1132 unsigned int mask = TIOCM_DTR;
1133 if (!(cflag & CRTSCTS) ||
1134 !test_bit(TTY_THROTTLED, &tty->flags))
1136 uart_set_mctrl(state->port, mask);
1139 /* Handle turning off CRTSCTS */
1140 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1141 spin_lock_irqsave(&state->port->lock, flags);
1142 tty->hw_stopped = 0;
1144 spin_unlock_irqrestore(&state->port->lock, flags);
1147 /* Handle turning on CRTSCTS */
1148 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1149 spin_lock_irqsave(&state->port->lock, flags);
1150 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1151 tty->hw_stopped = 1;
1152 state->port->ops->stop_tx(state->port);
1154 spin_unlock_irqrestore(&state->port->lock, flags);
1159 * No need to wake up processes in open wait, since they
1160 * sample the CLOCAL flag once, and don't recheck it.
1161 * XXX It's not clear whether the current behavior is correct
1162 * or not. Hence, this may change.....
1164 if (!(old_termios->c_cflag & CLOCAL) &&
1165 (tty->termios->c_cflag & CLOCAL))
1166 wake_up_interruptible(&state->info->open_wait);
1171 * In 2.4.5, calls to this will be serialized via the BKL in
1172 * linux/drivers/char/tty_io.c:tty_release()
1173 * linux/drivers/char/tty_io.c:do_tty_handup()
1175 static void uart_close(struct tty_struct *tty, struct file *filp)
1177 struct uart_state *state = tty->driver_data;
1178 struct uart_port *port;
1180 BUG_ON(!kernel_locked());
1182 if (!state || !state->port)
1187 DPRINTK("uart_close(%d) called\n", port->line);
1189 mutex_lock(&state->mutex);
1191 if (tty_hung_up_p(filp))
1194 if ((tty->count == 1) && (state->count != 1)) {
1196 * Uh, oh. tty->count is 1, which means that the tty
1197 * structure will be freed. state->count should always
1198 * be one in these conditions. If it's greater than
1199 * one, we've got real problems, since it means the
1200 * serial port won't be shutdown.
1202 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1203 "state->count is %d\n", state->count);
1206 if (--state->count < 0) {
1207 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1208 tty->name, state->count);
1215 * Now we wait for the transmit buffer to clear; and we notify
1216 * the line discipline to only process XON/XOFF characters by
1217 * setting tty->closing.
1221 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1222 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1225 * At this point, we stop accepting input. To do this, we
1226 * disable the receive line status interrupts.
1228 if (state->info->flags & UIF_INITIALIZED) {
1229 unsigned long flags;
1230 spin_lock_irqsave(&port->lock, flags);
1231 port->ops->stop_rx(port);
1232 spin_unlock_irqrestore(&port->lock, flags);
1234 * Before we drop DTR, make sure the UART transmitter
1235 * has completely drained; this is especially
1236 * important if there is a transmit FIFO!
1238 uart_wait_until_sent(tty, port->timeout);
1241 uart_shutdown(state);
1242 uart_flush_buffer(tty);
1244 tty_ldisc_flush(tty);
1247 state->info->tty = NULL;
1249 if (state->info->blocked_open) {
1250 if (state->close_delay)
1251 msleep_interruptible(state->close_delay);
1252 } else if (!uart_console(port)) {
1253 uart_change_pm(state, 3);
1257 * Wake up anyone trying to open this port.
1259 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1260 wake_up_interruptible(&state->info->open_wait);
1263 mutex_unlock(&state->mutex);
1266 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1268 struct uart_state *state = tty->driver_data;
1269 struct uart_port *port = state->port;
1270 unsigned long char_time, expire;
1272 BUG_ON(!kernel_locked());
1274 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1278 * Set the check interval to be 1/5 of the estimated time to
1279 * send a single character, and make it at least 1. The check
1280 * interval should also be less than the timeout.
1282 * Note: we have to use pretty tight timings here to satisfy
1285 char_time = (port->timeout - HZ/50) / port->fifosize;
1286 char_time = char_time / 5;
1289 if (timeout && timeout < char_time)
1290 char_time = timeout;
1293 * If the transmitter hasn't cleared in twice the approximate
1294 * amount of time to send the entire FIFO, it probably won't
1295 * ever clear. This assumes the UART isn't doing flow
1296 * control, which is currently the case. Hence, if it ever
1297 * takes longer than port->timeout, this is probably due to a
1298 * UART bug of some kind. So, we clamp the timeout parameter at
1301 if (timeout == 0 || timeout > 2 * port->timeout)
1302 timeout = 2 * port->timeout;
1304 expire = jiffies + timeout;
1306 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1307 port->line, jiffies, expire);
1310 * Check whether the transmitter is empty every 'char_time'.
1311 * 'timeout' / 'expire' give us the maximum amount of time
1314 while (!port->ops->tx_empty(port)) {
1315 msleep_interruptible(jiffies_to_msecs(char_time));
1316 if (signal_pending(current))
1318 if (time_after(jiffies, expire))
1321 set_current_state(TASK_RUNNING); /* might not be needed */
1325 * This is called with the BKL held in
1326 * linux/drivers/char/tty_io.c:do_tty_hangup()
1327 * We're called from the eventd thread, so we can sleep for
1328 * a _short_ time only.
1330 static void uart_hangup(struct tty_struct *tty)
1332 struct uart_state *state = tty->driver_data;
1334 BUG_ON(!kernel_locked());
1335 DPRINTK("uart_hangup(%d)\n", state->port->line);
1337 mutex_lock(&state->mutex);
1338 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1339 uart_flush_buffer(tty);
1340 uart_shutdown(state);
1342 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1343 state->info->tty = NULL;
1344 wake_up_interruptible(&state->info->open_wait);
1345 wake_up_interruptible(&state->info->delta_msr_wait);
1347 mutex_unlock(&state->mutex);
1351 * Copy across the serial console cflag setting into the termios settings
1352 * for the initial open of the port. This allows continuity between the
1353 * kernel settings, and the settings init adopts when it opens the port
1354 * for the first time.
1356 static void uart_update_termios(struct uart_state *state)
1358 struct tty_struct *tty = state->info->tty;
1359 struct uart_port *port = state->port;
1361 if (uart_console(port) && port->cons->cflag) {
1362 tty->termios->c_cflag = port->cons->cflag;
1363 port->cons->cflag = 0;
1367 * If the device failed to grab its irq resources,
1368 * or some other error occurred, don't try to talk
1369 * to the port hardware.
1371 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1373 * Make termios settings take effect.
1375 uart_change_speed(state, NULL);
1378 * And finally enable the RTS and DTR signals.
1380 if (tty->termios->c_cflag & CBAUD)
1381 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1386 * Block the open until the port is ready. We must be called with
1387 * the per-port semaphore held.
1390 uart_block_til_ready(struct file *filp, struct uart_state *state)
1392 DECLARE_WAITQUEUE(wait, current);
1393 struct uart_info *info = state->info;
1394 struct uart_port *port = state->port;
1397 info->blocked_open++;
1400 add_wait_queue(&info->open_wait, &wait);
1402 set_current_state(TASK_INTERRUPTIBLE);
1405 * If we have been hung up, tell userspace/restart open.
1407 if (tty_hung_up_p(filp) || info->tty == NULL)
1411 * If the port has been closed, tell userspace/restart open.
1413 if (!(info->flags & UIF_INITIALIZED))
1417 * If non-blocking mode is set, or CLOCAL mode is set,
1418 * we don't want to wait for the modem status lines to
1419 * indicate that the port is ready.
1421 * Also, if the port is not enabled/configured, we want
1422 * to allow the open to succeed here. Note that we will
1423 * have set TTY_IO_ERROR for a non-existant port.
1425 if ((filp->f_flags & O_NONBLOCK) ||
1426 (info->tty->termios->c_cflag & CLOCAL) ||
1427 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1432 * Set DTR to allow modem to know we're waiting. Do
1433 * not set RTS here - we want to make sure we catch
1434 * the data from the modem.
1436 if (info->tty->termios->c_cflag & CBAUD)
1437 uart_set_mctrl(port, TIOCM_DTR);
1440 * and wait for the carrier to indicate that the
1441 * modem is ready for us.
1443 spin_lock_irq(&port->lock);
1444 port->ops->enable_ms(port);
1445 mctrl = port->ops->get_mctrl(port);
1446 spin_unlock_irq(&port->lock);
1447 if (mctrl & TIOCM_CAR)
1450 mutex_unlock(&state->mutex);
1452 mutex_lock(&state->mutex);
1454 if (signal_pending(current))
1457 set_current_state(TASK_RUNNING);
1458 remove_wait_queue(&info->open_wait, &wait);
1461 info->blocked_open--;
1463 if (signal_pending(current))
1464 return -ERESTARTSYS;
1466 if (!info->tty || tty_hung_up_p(filp))
1472 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1474 struct uart_state *state;
1476 mutex_lock(&port_mutex);
1477 state = drv->state + line;
1478 if (mutex_lock_interruptible(&state->mutex)) {
1479 state = ERR_PTR(-ERESTARTSYS);
1486 mutex_unlock(&state->mutex);
1487 state = ERR_PTR(-ENXIO);
1492 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1494 memset(state->info, 0, sizeof(struct uart_info));
1495 init_waitqueue_head(&state->info->open_wait);
1496 init_waitqueue_head(&state->info->delta_msr_wait);
1499 * Link the info into the other structures.
1501 state->port->info = state->info;
1503 tasklet_init(&state->info->tlet, uart_tasklet_action,
1504 (unsigned long)state);
1507 mutex_unlock(&state->mutex);
1508 state = ERR_PTR(-ENOMEM);
1513 mutex_unlock(&port_mutex);
1518 * In 2.4.5, calls to uart_open are serialised by the BKL in
1519 * linux/fs/devices.c:chrdev_open()
1520 * Note that if this fails, then uart_close() _will_ be called.
1522 * In time, we want to scrap the "opening nonpresent ports"
1523 * behaviour and implement an alternative way for setserial
1524 * to set base addresses/ports/types. This will allow us to
1525 * get rid of a certain amount of extra tests.
1527 static int uart_open(struct tty_struct *tty, struct file *filp)
1529 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1530 struct uart_state *state;
1531 int retval, line = tty->index;
1533 BUG_ON(!kernel_locked());
1534 DPRINTK("uart_open(%d) called\n", line);
1537 * tty->driver->num won't change, so we won't fail here with
1538 * tty->driver_data set to something non-NULL (and therefore
1539 * we won't get caught by uart_close()).
1542 if (line >= tty->driver->num)
1546 * We take the semaphore inside uart_get to guarantee that we won't
1547 * be re-entered while allocating the info structure, or while we
1548 * request any IRQs that the driver may need. This also has the nice
1549 * side-effect that it delays the action of uart_hangup, so we can
1550 * guarantee that info->tty will always contain something reasonable.
1552 state = uart_get(drv, line);
1553 if (IS_ERR(state)) {
1554 retval = PTR_ERR(state);
1559 * Once we set tty->driver_data here, we are guaranteed that
1560 * uart_close() will decrement the driver module use count.
1561 * Any failures from here onwards should not touch the count.
1563 tty->driver_data = state;
1564 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1566 state->info->tty = tty;
1569 * If the port is in the middle of closing, bail out now.
1571 if (tty_hung_up_p(filp)) {
1574 mutex_unlock(&state->mutex);
1579 * Make sure the device is in D0 state.
1581 if (state->count == 1)
1582 uart_change_pm(state, 0);
1585 * Start up the serial port.
1587 retval = uart_startup(state, 0);
1590 * If we succeeded, wait until the port is ready.
1593 retval = uart_block_til_ready(filp, state);
1594 mutex_unlock(&state->mutex);
1597 * If this is the first open to succeed, adjust things to suit.
1599 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1600 state->info->flags |= UIF_NORMAL_ACTIVE;
1602 uart_update_termios(state);
1609 static const char *uart_type(struct uart_port *port)
1611 const char *str = NULL;
1613 if (port->ops->type)
1614 str = port->ops->type(port);
1622 #ifdef CONFIG_PROC_FS
1624 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1626 struct uart_state *state = drv->state + i;
1627 struct uart_port *port = state->port;
1629 unsigned int status;
1635 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1636 port->line, uart_type(port),
1637 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1638 port->iotype == UPIO_MEM ? port->mapbase :
1639 (unsigned long) port->iobase,
1642 if (port->type == PORT_UNKNOWN) {
1647 if(capable(CAP_SYS_ADMIN))
1649 spin_lock_irq(&port->lock);
1650 status = port->ops->get_mctrl(port);
1651 spin_unlock_irq(&port->lock);
1653 ret += sprintf(buf + ret, " tx:%d rx:%d",
1654 port->icount.tx, port->icount.rx);
1655 if (port->icount.frame)
1656 ret += sprintf(buf + ret, " fe:%d",
1657 port->icount.frame);
1658 if (port->icount.parity)
1659 ret += sprintf(buf + ret, " pe:%d",
1660 port->icount.parity);
1661 if (port->icount.brk)
1662 ret += sprintf(buf + ret, " brk:%d",
1664 if (port->icount.overrun)
1665 ret += sprintf(buf + ret, " oe:%d",
1666 port->icount.overrun);
1668 #define INFOBIT(bit,str) \
1669 if (port->mctrl & (bit)) \
1670 strncat(stat_buf, (str), sizeof(stat_buf) - \
1671 strlen(stat_buf) - 2)
1672 #define STATBIT(bit,str) \
1673 if (status & (bit)) \
1674 strncat(stat_buf, (str), sizeof(stat_buf) - \
1675 strlen(stat_buf) - 2)
1679 INFOBIT(TIOCM_RTS, "|RTS");
1680 STATBIT(TIOCM_CTS, "|CTS");
1681 INFOBIT(TIOCM_DTR, "|DTR");
1682 STATBIT(TIOCM_DSR, "|DSR");
1683 STATBIT(TIOCM_CAR, "|CD");
1684 STATBIT(TIOCM_RNG, "|RI");
1687 strcat(stat_buf, "\n");
1689 ret += sprintf(buf + ret, stat_buf);
1699 static int uart_read_proc(char *page, char **start, off_t off,
1700 int count, int *eof, void *data)
1702 struct tty_driver *ttydrv = data;
1703 struct uart_driver *drv = ttydrv->driver_state;
1707 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1709 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1710 l = uart_line_info(page + len, drv, i);
1712 if (len + begin > off + count)
1714 if (len + begin < off) {
1721 if (off >= len + begin)
1723 *start = page + (off - begin);
1724 return (count < begin + len - off) ? count : (begin + len - off);
1728 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1730 * Check whether an invalid uart number has been specified, and
1731 * if so, search for the first available port that does have
1734 struct uart_port * __init
1735 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1737 int idx = co->index;
1739 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1740 ports[idx].membase == NULL))
1741 for (idx = 0; idx < nr; idx++)
1742 if (ports[idx].iobase != 0 ||
1743 ports[idx].membase != NULL)
1752 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1753 * @options: pointer to option string
1754 * @baud: pointer to an 'int' variable for the baud rate.
1755 * @parity: pointer to an 'int' variable for the parity.
1756 * @bits: pointer to an 'int' variable for the number of data bits.
1757 * @flow: pointer to an 'int' variable for the flow control character.
1759 * uart_parse_options decodes a string containing the serial console
1760 * options. The format of the string is <baud><parity><bits><flow>,
1764 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1768 *baud = simple_strtoul(s, NULL, 10);
1769 while (*s >= '0' && *s <= '9')
1784 static const struct baud_rates baud_rates[] = {
1785 { 921600, B921600 },
1786 { 460800, B460800 },
1787 { 230400, B230400 },
1788 { 115200, B115200 },
1800 * uart_set_options - setup the serial console parameters
1801 * @port: pointer to the serial ports uart_port structure
1802 * @co: console pointer
1804 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1805 * @bits: number of data bits
1806 * @flow: flow control character - 'r' (rts)
1809 uart_set_options(struct uart_port *port, struct console *co,
1810 int baud, int parity, int bits, int flow)
1812 struct termios termios;
1816 * Ensure that the serial console lock is initialised
1819 spin_lock_init(&port->lock);
1821 memset(&termios, 0, sizeof(struct termios));
1823 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1826 * Construct a cflag setting.
1828 for (i = 0; baud_rates[i].rate; i++)
1829 if (baud_rates[i].rate <= baud)
1832 termios.c_cflag |= baud_rates[i].cflag;
1835 termios.c_cflag |= CS7;
1837 termios.c_cflag |= CS8;
1841 termios.c_cflag |= PARODD;
1844 termios.c_cflag |= PARENB;
1849 termios.c_cflag |= CRTSCTS;
1851 port->ops->set_termios(port, &termios, NULL);
1852 co->cflag = termios.c_cflag;
1856 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1858 static void uart_change_pm(struct uart_state *state, int pm_state)
1860 struct uart_port *port = state->port;
1862 port->ops->pm(port, pm_state, state->pm_state);
1863 state->pm_state = pm_state;
1866 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1868 struct uart_state *state = drv->state + port->line;
1870 mutex_lock(&state->mutex);
1872 if (state->info && state->info->flags & UIF_INITIALIZED) {
1873 struct uart_ops *ops = port->ops;
1875 spin_lock_irq(&port->lock);
1877 ops->set_mctrl(port, 0);
1879 spin_unlock_irq(&port->lock);
1882 * Wait for the transmitter to empty.
1884 while (!ops->tx_empty(port)) {
1888 ops->shutdown(port);
1892 * Disable the console device before suspending.
1894 if (uart_console(port))
1895 console_stop(port->cons);
1897 uart_change_pm(state, 3);
1899 mutex_unlock(&state->mutex);
1904 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1906 struct uart_state *state = drv->state + port->line;
1908 mutex_lock(&state->mutex);
1910 uart_change_pm(state, 0);
1913 * Re-enable the console device after suspending.
1915 if (uart_console(port)) {
1916 struct termios termios;
1919 * First try to use the console cflag setting.
1921 memset(&termios, 0, sizeof(struct termios));
1922 termios.c_cflag = port->cons->cflag;
1925 * If that's unset, use the tty termios setting.
1927 if (state->info && state->info->tty && termios.c_cflag == 0)
1928 termios = *state->info->tty->termios;
1930 port->ops->set_termios(port, &termios, NULL);
1931 console_start(port->cons);
1934 if (state->info && state->info->flags & UIF_INITIALIZED) {
1935 struct uart_ops *ops = port->ops;
1938 ops->set_mctrl(port, 0);
1939 ret = ops->startup(port);
1941 uart_change_speed(state, NULL);
1942 spin_lock_irq(&port->lock);
1943 ops->set_mctrl(port, port->mctrl);
1944 ops->start_tx(port);
1945 spin_unlock_irq(&port->lock);
1948 * Failed to resume - maybe hardware went away?
1949 * Clear the "initialized" flag so we won't try
1950 * to call the low level drivers shutdown method.
1952 state->info->flags &= ~UIF_INITIALIZED;
1953 uart_shutdown(state);
1957 mutex_unlock(&state->mutex);
1963 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1967 switch (port->iotype) {
1969 snprintf(address, sizeof(address),
1970 "I/O 0x%x", port->iobase);
1973 snprintf(address, sizeof(address),
1974 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1979 snprintf(address, sizeof(address),
1980 "MMIO 0x%lx", port->mapbase);
1983 strlcpy(address, "*unknown*", sizeof(address));
1987 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
1988 port->dev ? port->dev->bus_id : "",
1989 port->dev ? ": " : "",
1990 drv->dev_name, port->line, address, port->irq, uart_type(port));
1994 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1995 struct uart_port *port)
2000 * If there isn't a port here, don't do anything further.
2002 if (!port->iobase && !port->mapbase && !port->membase)
2006 * Now do the auto configuration stuff. Note that config_port
2007 * is expected to claim the resources and map the port for us.
2009 flags = UART_CONFIG_TYPE;
2010 if (port->flags & UPF_AUTO_IRQ)
2011 flags |= UART_CONFIG_IRQ;
2012 if (port->flags & UPF_BOOT_AUTOCONF) {
2013 port->type = PORT_UNKNOWN;
2014 port->ops->config_port(port, flags);
2017 if (port->type != PORT_UNKNOWN) {
2018 unsigned long flags;
2020 uart_report_port(drv, port);
2023 * Ensure that the modem control lines are de-activated.
2024 * We probably don't need a spinlock around this, but
2026 spin_lock_irqsave(&port->lock, flags);
2027 port->ops->set_mctrl(port, 0);
2028 spin_unlock_irqrestore(&port->lock, flags);
2031 * Power down all ports by default, except the
2032 * console if we have one.
2034 if (!uart_console(port))
2035 uart_change_pm(state, 3);
2040 * This reverses the effects of uart_configure_port, hanging up the
2041 * port before removal.
2044 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2046 struct uart_port *port = state->port;
2047 struct uart_info *info = state->info;
2049 if (info && info->tty)
2050 tty_vhangup(info->tty);
2052 mutex_lock(&state->mutex);
2057 * Free the port IO and memory resources, if any.
2059 if (port->type != PORT_UNKNOWN)
2060 port->ops->release_port(port);
2063 * Indicate that there isn't a port here anymore.
2065 port->type = PORT_UNKNOWN;
2068 * Kill the tasklet, and free resources.
2071 tasklet_kill(&info->tlet);
2075 mutex_unlock(&state->mutex);
2078 static struct tty_operations uart_ops = {
2080 .close = uart_close,
2081 .write = uart_write,
2082 .put_char = uart_put_char,
2083 .flush_chars = uart_flush_chars,
2084 .write_room = uart_write_room,
2085 .chars_in_buffer= uart_chars_in_buffer,
2086 .flush_buffer = uart_flush_buffer,
2087 .ioctl = uart_ioctl,
2088 .throttle = uart_throttle,
2089 .unthrottle = uart_unthrottle,
2090 .send_xchar = uart_send_xchar,
2091 .set_termios = uart_set_termios,
2093 .start = uart_start,
2094 .hangup = uart_hangup,
2095 .break_ctl = uart_break_ctl,
2096 .wait_until_sent= uart_wait_until_sent,
2097 #ifdef CONFIG_PROC_FS
2098 .read_proc = uart_read_proc,
2100 .tiocmget = uart_tiocmget,
2101 .tiocmset = uart_tiocmset,
2105 * uart_register_driver - register a driver with the uart core layer
2106 * @drv: low level driver structure
2108 * Register a uart driver with the core driver. We in turn register
2109 * with the tty layer, and initialise the core driver per-port state.
2111 * We have a proc file in /proc/tty/driver which is named after the
2114 * drv->port should be NULL, and the per-port structures should be
2115 * registered using uart_add_one_port after this call has succeeded.
2117 int uart_register_driver(struct uart_driver *drv)
2119 struct tty_driver *normal = NULL;
2125 * Maybe we should be using a slab cache for this, especially if
2126 * we have a large number of ports to handle.
2128 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2133 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2135 normal = alloc_tty_driver(drv->nr);
2139 drv->tty_driver = normal;
2141 normal->owner = drv->owner;
2142 normal->driver_name = drv->driver_name;
2143 normal->devfs_name = drv->devfs_name;
2144 normal->name = drv->dev_name;
2145 normal->major = drv->major;
2146 normal->minor_start = drv->minor;
2147 normal->type = TTY_DRIVER_TYPE_SERIAL;
2148 normal->subtype = SERIAL_TYPE_NORMAL;
2149 normal->init_termios = tty_std_termios;
2150 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2151 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2152 normal->driver_state = drv;
2153 tty_set_operations(normal, &uart_ops);
2156 * Initialise the UART state(s).
2158 for (i = 0; i < drv->nr; i++) {
2159 struct uart_state *state = drv->state + i;
2161 state->close_delay = 500; /* .5 seconds */
2162 state->closing_wait = 30000; /* 30 seconds */
2164 mutex_init(&state->mutex);
2167 retval = tty_register_driver(normal);
2170 put_tty_driver(normal);
2177 * uart_unregister_driver - remove a driver from the uart core layer
2178 * @drv: low level driver structure
2180 * Remove all references to a driver from the core driver. The low
2181 * level driver must have removed all its ports via the
2182 * uart_remove_one_port() if it registered them with uart_add_one_port().
2183 * (ie, drv->port == NULL)
2185 void uart_unregister_driver(struct uart_driver *drv)
2187 struct tty_driver *p = drv->tty_driver;
2188 tty_unregister_driver(p);
2191 drv->tty_driver = NULL;
2194 struct tty_driver *uart_console_device(struct console *co, int *index)
2196 struct uart_driver *p = co->data;
2198 return p->tty_driver;
2202 * uart_add_one_port - attach a driver-defined port structure
2203 * @drv: pointer to the uart low level driver structure for this port
2204 * @port: uart port structure to use for this port.
2206 * This allows the driver to register its own uart_port structure
2207 * with the core driver. The main purpose is to allow the low
2208 * level uart drivers to expand uart_port, rather than having yet
2209 * more levels of structures.
2211 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2213 struct uart_state *state;
2216 BUG_ON(in_interrupt());
2218 if (port->line >= drv->nr)
2221 state = drv->state + port->line;
2223 mutex_lock(&port_mutex);
2231 port->cons = drv->cons;
2232 port->info = state->info;
2235 * If this port is a console, then the spinlock is already
2238 if (!uart_console(port))
2239 spin_lock_init(&port->lock);
2241 uart_configure_port(drv, state, port);
2244 * Register the port whether it's detected or not. This allows
2245 * setserial to be used to alter this ports parameters.
2247 tty_register_device(drv->tty_driver, port->line, port->dev);
2250 * If this driver supports console, and it hasn't been
2251 * successfully registered yet, try to re-register it.
2252 * It may be that the port was not available.
2254 if (port->type != PORT_UNKNOWN &&
2255 port->cons && !(port->cons->flags & CON_ENABLED))
2256 register_console(port->cons);
2259 mutex_unlock(&port_mutex);
2265 * uart_remove_one_port - detach a driver defined port structure
2266 * @drv: pointer to the uart low level driver structure for this port
2267 * @port: uart port structure for this port
2269 * This unhooks (and hangs up) the specified port structure from the
2270 * core driver. No further calls will be made to the low-level code
2273 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2275 struct uart_state *state = drv->state + port->line;
2277 BUG_ON(in_interrupt());
2279 if (state->port != port)
2280 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2283 mutex_lock(&port_mutex);
2286 * Remove the devices from devfs
2288 tty_unregister_device(drv->tty_driver, port->line);
2290 uart_unconfigure_port(drv, state);
2292 mutex_unlock(&port_mutex);
2298 * Are the two ports equivalent?
2300 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2302 if (port1->iotype != port2->iotype)
2305 switch (port1->iotype) {
2307 return (port1->iobase == port2->iobase);
2309 return (port1->iobase == port2->iobase) &&
2310 (port1->hub6 == port2->hub6);
2312 return (port1->mapbase == port2->mapbase);
2316 EXPORT_SYMBOL(uart_match_port);
2318 EXPORT_SYMBOL(uart_write_wakeup);
2319 EXPORT_SYMBOL(uart_register_driver);
2320 EXPORT_SYMBOL(uart_unregister_driver);
2321 EXPORT_SYMBOL(uart_suspend_port);
2322 EXPORT_SYMBOL(uart_resume_port);
2323 EXPORT_SYMBOL(uart_add_one_port);
2324 EXPORT_SYMBOL(uart_remove_one_port);
2326 MODULE_DESCRIPTION("Serial driver core");
2327 MODULE_LICENSE("GPL");