2 * linux/drivers/char/core.c
4 * Driver core for serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_core.h>
31 #include <linux/smp_lock.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/delay.h>
35 #include <linux/mutex.h>
38 #include <asm/uaccess.h>
41 * This is used to lock changes in serial line configuration.
43 static DEFINE_MUTEX(port_mutex);
46 * lockdep: port->lock is initialized in two places, but we
47 * want only one lock-class:
49 static struct lock_class_key port_lock_key;
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
55 #ifdef CONFIG_SERIAL_CORE_CONSOLE
56 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
58 #define uart_console(port) (0)
61 static void uart_change_speed(struct uart_state *state, struct ktermios *old_termios);
62 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
63 static void uart_change_pm(struct uart_state *state, int pm_state);
66 * This routine is used by the interrupt handler to schedule processing in
67 * the software interrupt portion of the driver.
69 void uart_write_wakeup(struct uart_port *port)
71 struct uart_info *info = port->info;
73 * This means you called this function _after_ the port was
74 * closed. No cookie for you.
77 tasklet_schedule(&info->tlet);
80 static void uart_stop(struct tty_struct *tty)
82 struct uart_state *state = tty->driver_data;
83 struct uart_port *port = state->port;
86 spin_lock_irqsave(&port->lock, flags);
87 port->ops->stop_tx(port);
88 spin_unlock_irqrestore(&port->lock, flags);
91 static void __uart_start(struct tty_struct *tty)
93 struct uart_state *state = tty->driver_data;
94 struct uart_port *port = state->port;
96 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
97 !tty->stopped && !tty->hw_stopped)
98 port->ops->start_tx(port);
101 static void uart_start(struct tty_struct *tty)
103 struct uart_state *state = tty->driver_data;
104 struct uart_port *port = state->port;
107 spin_lock_irqsave(&port->lock, flags);
109 spin_unlock_irqrestore(&port->lock, flags);
112 static void uart_tasklet_action(unsigned long data)
114 struct uart_state *state = (struct uart_state *)data;
115 tty_wakeup(state->info->tty);
119 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
124 spin_lock_irqsave(&port->lock, flags);
126 port->mctrl = (old & ~clear) | set;
127 if (old != port->mctrl)
128 port->ops->set_mctrl(port, port->mctrl);
129 spin_unlock_irqrestore(&port->lock, flags);
132 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
133 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
136 * Startup the port. This will be called once per open. All calls
137 * will be serialised by the per-port semaphore.
139 static int uart_startup(struct uart_state *state, int init_hw)
141 struct uart_info *info = state->info;
142 struct uart_port *port = state->port;
146 if (info->flags & UIF_INITIALIZED)
150 * Set the TTY IO error marker - we will only clear this
151 * once we have successfully opened the port. Also set
152 * up the tty->alt_speed kludge
154 set_bit(TTY_IO_ERROR, &info->tty->flags);
156 if (port->type == PORT_UNKNOWN)
160 * Initialise and allocate the transmit and temporary
163 if (!info->xmit.buf) {
164 page = get_zeroed_page(GFP_KERNEL);
168 info->xmit.buf = (unsigned char *) page;
169 uart_circ_clear(&info->xmit);
172 retval = port->ops->startup(port);
176 * Initialise the hardware port settings.
178 uart_change_speed(state, NULL);
181 * Setup the RTS and DTR signals once the
182 * port is open and ready to respond.
184 if (info->tty->termios->c_cflag & CBAUD)
185 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
188 if (info->flags & UIF_CTS_FLOW) {
189 spin_lock_irq(&port->lock);
190 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
191 info->tty->hw_stopped = 1;
192 spin_unlock_irq(&port->lock);
195 info->flags |= UIF_INITIALIZED;
197 clear_bit(TTY_IO_ERROR, &info->tty->flags);
200 if (retval && capable(CAP_SYS_ADMIN))
207 * This routine will shutdown a serial port; interrupts are disabled, and
208 * DTR is dropped if the hangup on close termio flag is on. Calls to
209 * uart_shutdown are serialised by the per-port semaphore.
211 static void uart_shutdown(struct uart_state *state)
213 struct uart_info *info = state->info;
214 struct uart_port *port = state->port;
217 * Set the TTY IO error marker
220 set_bit(TTY_IO_ERROR, &info->tty->flags);
222 if (info->flags & UIF_INITIALIZED) {
223 info->flags &= ~UIF_INITIALIZED;
226 * Turn off DTR and RTS early.
228 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
229 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
232 * clear delta_msr_wait queue to avoid mem leaks: we may free
233 * the irq here so the queue might never be woken up. Note
234 * that we won't end up waiting on delta_msr_wait again since
235 * any outstanding file descriptors should be pointing at
236 * hung_up_tty_fops now.
238 wake_up_interruptible(&info->delta_msr_wait);
241 * Free the IRQ and disable the port.
243 port->ops->shutdown(port);
246 * Ensure that the IRQ handler isn't running on another CPU.
248 synchronize_irq(port->irq);
252 * kill off our tasklet
254 tasklet_kill(&info->tlet);
257 * Free the transmit buffer page.
259 if (info->xmit.buf) {
260 free_page((unsigned long)info->xmit.buf);
261 info->xmit.buf = NULL;
266 * uart_update_timeout - update per-port FIFO timeout.
267 * @port: uart_port structure describing the port
268 * @cflag: termios cflag value
269 * @baud: speed of the port
271 * Set the port FIFO timeout value. The @cflag value should
272 * reflect the actual hardware settings.
275 uart_update_timeout(struct uart_port *port, unsigned int cflag,
280 /* byte size and parity */
281 switch (cflag & CSIZE) {
302 * The total number of bits to be transmitted in the fifo.
304 bits = bits * port->fifosize;
307 * Figure the timeout to send the above number of bits.
308 * Add .02 seconds of slop
310 port->timeout = (HZ * bits) / baud + HZ/50;
313 EXPORT_SYMBOL(uart_update_timeout);
316 * uart_get_baud_rate - return baud rate for a particular port
317 * @port: uart_port structure describing the port in question.
318 * @termios: desired termios settings.
319 * @old: old termios (or NULL)
320 * @min: minimum acceptable baud rate
321 * @max: maximum acceptable baud rate
323 * Decode the termios structure into a numeric baud rate,
324 * taking account of the magic 38400 baud rate (with spd_*
325 * flags), and mapping the %B0 rate to 9600 baud.
327 * If the new baud rate is invalid, try the old termios setting.
328 * If it's still invalid, we try 9600 baud.
330 * Update the @termios structure to reflect the baud rate
331 * we're actually going to be using.
334 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
335 struct ktermios *old, unsigned int min, unsigned int max)
337 unsigned int try, baud, altbaud = 38400;
338 upf_t flags = port->flags & UPF_SPD_MASK;
340 if (flags == UPF_SPD_HI)
342 if (flags == UPF_SPD_VHI)
344 if (flags == UPF_SPD_SHI)
346 if (flags == UPF_SPD_WARP)
349 for (try = 0; try < 2; try++) {
350 baud = tty_termios_baud_rate(termios);
353 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
360 * Special case: B0 rate.
365 if (baud >= min && baud <= max)
369 * Oops, the quotient was zero. Try again with
370 * the old baud rate if possible.
372 termios->c_cflag &= ~CBAUD;
374 termios->c_cflag |= old->c_cflag & CBAUD;
380 * As a last resort, if the quotient is zero,
381 * default to 9600 bps
383 termios->c_cflag |= B9600;
389 EXPORT_SYMBOL(uart_get_baud_rate);
392 * uart_get_divisor - return uart clock divisor
393 * @port: uart_port structure describing the port.
394 * @baud: desired baud rate
396 * Calculate the uart clock divisor for the port.
399 uart_get_divisor(struct uart_port *port, unsigned int baud)
404 * Old custom speed handling.
406 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
407 quot = port->custom_divisor;
409 quot = (port->uartclk + (8 * baud)) / (16 * baud);
414 EXPORT_SYMBOL(uart_get_divisor);
417 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
419 struct tty_struct *tty = state->info->tty;
420 struct uart_port *port = state->port;
421 struct ktermios *termios;
424 * If we have no tty, termios, or the port does not exist,
425 * then we can't set the parameters for this port.
427 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
430 termios = tty->termios;
433 * Set flags based on termios cflag
435 if (termios->c_cflag & CRTSCTS)
436 state->info->flags |= UIF_CTS_FLOW;
438 state->info->flags &= ~UIF_CTS_FLOW;
440 if (termios->c_cflag & CLOCAL)
441 state->info->flags &= ~UIF_CHECK_CD;
443 state->info->flags |= UIF_CHECK_CD;
445 port->ops->set_termios(port, termios, old_termios);
449 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
456 spin_lock_irqsave(&port->lock, flags);
457 if (uart_circ_chars_free(circ) != 0) {
458 circ->buf[circ->head] = c;
459 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
461 spin_unlock_irqrestore(&port->lock, flags);
464 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
466 struct uart_state *state = tty->driver_data;
468 __uart_put_char(state->port, &state->info->xmit, ch);
471 static void uart_flush_chars(struct tty_struct *tty)
477 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
479 struct uart_state *state = tty->driver_data;
480 struct uart_port *port;
481 struct circ_buf *circ;
486 * This means you called this function _after_ the port was
487 * closed. No cookie for you.
489 if (!state || !state->info) {
495 circ = &state->info->xmit;
500 spin_lock_irqsave(&port->lock, flags);
502 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
507 memcpy(circ->buf + circ->head, buf, c);
508 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
513 spin_unlock_irqrestore(&port->lock, flags);
519 static int uart_write_room(struct tty_struct *tty)
521 struct uart_state *state = tty->driver_data;
523 return uart_circ_chars_free(&state->info->xmit);
526 static int uart_chars_in_buffer(struct tty_struct *tty)
528 struct uart_state *state = tty->driver_data;
530 return uart_circ_chars_pending(&state->info->xmit);
533 static void uart_flush_buffer(struct tty_struct *tty)
535 struct uart_state *state = tty->driver_data;
536 struct uart_port *port = state->port;
540 * This means you called this function _after_ the port was
541 * closed. No cookie for you.
543 if (!state || !state->info) {
548 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
550 spin_lock_irqsave(&port->lock, flags);
551 uart_circ_clear(&state->info->xmit);
552 spin_unlock_irqrestore(&port->lock, flags);
557 * This function is used to send a high-priority XON/XOFF character to
560 static void uart_send_xchar(struct tty_struct *tty, char ch)
562 struct uart_state *state = tty->driver_data;
563 struct uart_port *port = state->port;
566 if (port->ops->send_xchar)
567 port->ops->send_xchar(port, ch);
571 spin_lock_irqsave(&port->lock, flags);
572 port->ops->start_tx(port);
573 spin_unlock_irqrestore(&port->lock, flags);
578 static void uart_throttle(struct tty_struct *tty)
580 struct uart_state *state = tty->driver_data;
583 uart_send_xchar(tty, STOP_CHAR(tty));
585 if (tty->termios->c_cflag & CRTSCTS)
586 uart_clear_mctrl(state->port, TIOCM_RTS);
589 static void uart_unthrottle(struct tty_struct *tty)
591 struct uart_state *state = tty->driver_data;
592 struct uart_port *port = state->port;
598 uart_send_xchar(tty, START_CHAR(tty));
601 if (tty->termios->c_cflag & CRTSCTS)
602 uart_set_mctrl(port, TIOCM_RTS);
605 static int uart_get_info(struct uart_state *state,
606 struct serial_struct __user *retinfo)
608 struct uart_port *port = state->port;
609 struct serial_struct tmp;
611 memset(&tmp, 0, sizeof(tmp));
612 tmp.type = port->type;
613 tmp.line = port->line;
614 tmp.port = port->iobase;
615 if (HIGH_BITS_OFFSET)
616 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
618 tmp.flags = port->flags;
619 tmp.xmit_fifo_size = port->fifosize;
620 tmp.baud_base = port->uartclk / 16;
621 tmp.close_delay = state->close_delay / 10;
622 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
623 ASYNC_CLOSING_WAIT_NONE :
624 state->closing_wait / 10;
625 tmp.custom_divisor = port->custom_divisor;
626 tmp.hub6 = port->hub6;
627 tmp.io_type = port->iotype;
628 tmp.iomem_reg_shift = port->regshift;
629 tmp.iomem_base = (void *)(unsigned long)port->mapbase;
631 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
636 static int uart_set_info(struct uart_state *state,
637 struct serial_struct __user *newinfo)
639 struct serial_struct new_serial;
640 struct uart_port *port = state->port;
641 unsigned long new_port;
642 unsigned int change_irq, change_port, closing_wait;
643 unsigned int old_custom_divisor, close_delay;
644 upf_t old_flags, new_flags;
647 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
650 new_port = new_serial.port;
651 if (HIGH_BITS_OFFSET)
652 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
654 new_serial.irq = irq_canonicalize(new_serial.irq);
655 close_delay = new_serial.close_delay * 10;
656 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
657 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
660 * This semaphore protects state->count. It is also
661 * very useful to prevent opens. Also, take the
662 * port configuration semaphore to make sure that a
663 * module insertion/removal doesn't change anything
666 mutex_lock(&state->mutex);
668 change_irq = !(port->flags & UPF_FIXED_PORT)
669 && new_serial.irq != port->irq;
672 * Since changing the 'type' of the port changes its resource
673 * allocations, we should treat type changes the same as
676 change_port = !(port->flags & UPF_FIXED_PORT)
677 && (new_port != port->iobase ||
678 (unsigned long)new_serial.iomem_base != port->mapbase ||
679 new_serial.hub6 != port->hub6 ||
680 new_serial.io_type != port->iotype ||
681 new_serial.iomem_reg_shift != port->regshift ||
682 new_serial.type != port->type);
684 old_flags = port->flags;
685 new_flags = new_serial.flags;
686 old_custom_divisor = port->custom_divisor;
688 if (!capable(CAP_SYS_ADMIN)) {
690 if (change_irq || change_port ||
691 (new_serial.baud_base != port->uartclk / 16) ||
692 (close_delay != state->close_delay) ||
693 (closing_wait != state->closing_wait) ||
694 (new_serial.xmit_fifo_size &&
695 new_serial.xmit_fifo_size != port->fifosize) ||
696 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
698 port->flags = ((port->flags & ~UPF_USR_MASK) |
699 (new_flags & UPF_USR_MASK));
700 port->custom_divisor = new_serial.custom_divisor;
705 * Ask the low level driver to verify the settings.
707 if (port->ops->verify_port)
708 retval = port->ops->verify_port(port, &new_serial);
710 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
711 (new_serial.baud_base < 9600))
717 if (change_port || change_irq) {
721 * Make sure that we are the sole user of this port.
723 if (uart_users(state) > 1)
727 * We need to shutdown the serial port at the old
728 * port/type/irq combination.
730 uart_shutdown(state);
734 unsigned long old_iobase, old_mapbase;
735 unsigned int old_type, old_iotype, old_hub6, old_shift;
737 old_iobase = port->iobase;
738 old_mapbase = port->mapbase;
739 old_type = port->type;
740 old_hub6 = port->hub6;
741 old_iotype = port->iotype;
742 old_shift = port->regshift;
745 * Free and release old regions
747 if (old_type != PORT_UNKNOWN)
748 port->ops->release_port(port);
750 port->iobase = new_port;
751 port->type = new_serial.type;
752 port->hub6 = new_serial.hub6;
753 port->iotype = new_serial.io_type;
754 port->regshift = new_serial.iomem_reg_shift;
755 port->mapbase = (unsigned long)new_serial.iomem_base;
758 * Claim and map the new regions
760 if (port->type != PORT_UNKNOWN) {
761 retval = port->ops->request_port(port);
763 /* Always success - Jean II */
768 * If we fail to request resources for the
769 * new port, try to restore the old settings.
771 if (retval && old_type != PORT_UNKNOWN) {
772 port->iobase = old_iobase;
773 port->type = old_type;
774 port->hub6 = old_hub6;
775 port->iotype = old_iotype;
776 port->regshift = old_shift;
777 port->mapbase = old_mapbase;
778 retval = port->ops->request_port(port);
780 * If we failed to restore the old settings,
784 port->type = PORT_UNKNOWN;
790 goto exit; // Added to return the correct error -Ram Gupta
795 port->irq = new_serial.irq;
796 if (!(port->flags & UPF_FIXED_PORT))
797 port->uartclk = new_serial.baud_base * 16;
798 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
799 (new_flags & UPF_CHANGE_MASK);
800 port->custom_divisor = new_serial.custom_divisor;
801 state->close_delay = close_delay;
802 state->closing_wait = closing_wait;
803 if (new_serial.xmit_fifo_size)
804 port->fifosize = new_serial.xmit_fifo_size;
805 if (state->info->tty)
806 state->info->tty->low_latency =
807 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
811 if (port->type == PORT_UNKNOWN)
813 if (state->info->flags & UIF_INITIALIZED) {
814 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
815 old_custom_divisor != port->custom_divisor) {
817 * If they're setting up a custom divisor or speed,
818 * instead of clearing it, then bitch about it. No
819 * need to rate-limit; it's CAP_SYS_ADMIN only.
821 if (port->flags & UPF_SPD_MASK) {
824 "%s sets custom speed on %s. This "
825 "is deprecated.\n", current->comm,
826 tty_name(state->info->tty, buf));
828 uart_change_speed(state, NULL);
831 retval = uart_startup(state, 1);
833 mutex_unlock(&state->mutex);
839 * uart_get_lsr_info - get line status register info.
840 * Note: uart_ioctl protects us against hangups.
842 static int uart_get_lsr_info(struct uart_state *state,
843 unsigned int __user *value)
845 struct uart_port *port = state->port;
848 result = port->ops->tx_empty(port);
851 * If we're about to load something into the transmit
852 * register, we'll pretend the transmitter isn't empty to
853 * avoid a race condition (depending on when the transmit
854 * interrupt happens).
857 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
858 !state->info->tty->stopped && !state->info->tty->hw_stopped))
859 result &= ~TIOCSER_TEMT;
861 return put_user(result, value);
864 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
866 struct uart_state *state = tty->driver_data;
867 struct uart_port *port = state->port;
870 mutex_lock(&state->mutex);
871 if ((!file || !tty_hung_up_p(file)) &&
872 !(tty->flags & (1 << TTY_IO_ERROR))) {
873 result = port->mctrl;
875 spin_lock_irq(&port->lock);
876 result |= port->ops->get_mctrl(port);
877 spin_unlock_irq(&port->lock);
879 mutex_unlock(&state->mutex);
885 uart_tiocmset(struct tty_struct *tty, struct file *file,
886 unsigned int set, unsigned int clear)
888 struct uart_state *state = tty->driver_data;
889 struct uart_port *port = state->port;
892 mutex_lock(&state->mutex);
893 if ((!file || !tty_hung_up_p(file)) &&
894 !(tty->flags & (1 << TTY_IO_ERROR))) {
895 uart_update_mctrl(port, set, clear);
898 mutex_unlock(&state->mutex);
902 static void uart_break_ctl(struct tty_struct *tty, int break_state)
904 struct uart_state *state = tty->driver_data;
905 struct uart_port *port = state->port;
907 BUG_ON(!kernel_locked());
909 mutex_lock(&state->mutex);
911 if (port->type != PORT_UNKNOWN)
912 port->ops->break_ctl(port, break_state);
914 mutex_unlock(&state->mutex);
917 static int uart_do_autoconfig(struct uart_state *state)
919 struct uart_port *port = state->port;
922 if (!capable(CAP_SYS_ADMIN))
926 * Take the per-port semaphore. This prevents count from
927 * changing, and hence any extra opens of the port while
928 * we're auto-configuring.
930 if (mutex_lock_interruptible(&state->mutex))
934 if (uart_users(state) == 1) {
935 uart_shutdown(state);
938 * If we already have a port type configured,
939 * we must release its resources.
941 if (port->type != PORT_UNKNOWN)
942 port->ops->release_port(port);
944 flags = UART_CONFIG_TYPE;
945 if (port->flags & UPF_AUTO_IRQ)
946 flags |= UART_CONFIG_IRQ;
949 * This will claim the ports resources if
952 port->ops->config_port(port, flags);
954 ret = uart_startup(state, 1);
956 mutex_unlock(&state->mutex);
961 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
962 * - mask passed in arg for lines of interest
963 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
964 * Caller should use TIOCGICOUNT to see which one it was
967 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
969 struct uart_port *port = state->port;
970 DECLARE_WAITQUEUE(wait, current);
971 struct uart_icount cprev, cnow;
975 * note the counters on entry
977 spin_lock_irq(&port->lock);
978 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
981 * Force modem status interrupts on
983 port->ops->enable_ms(port);
984 spin_unlock_irq(&port->lock);
986 add_wait_queue(&state->info->delta_msr_wait, &wait);
988 spin_lock_irq(&port->lock);
989 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
990 spin_unlock_irq(&port->lock);
992 set_current_state(TASK_INTERRUPTIBLE);
994 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
995 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
996 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
997 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1004 /* see if a signal did it */
1005 if (signal_pending(current)) {
1013 current->state = TASK_RUNNING;
1014 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1020 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1021 * Return: write counters to the user passed counter struct
1022 * NB: both 1->0 and 0->1 transitions are counted except for
1023 * RI where only 0->1 is counted.
1025 static int uart_get_count(struct uart_state *state,
1026 struct serial_icounter_struct __user *icnt)
1028 struct serial_icounter_struct icount;
1029 struct uart_icount cnow;
1030 struct uart_port *port = state->port;
1032 spin_lock_irq(&port->lock);
1033 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1034 spin_unlock_irq(&port->lock);
1036 icount.cts = cnow.cts;
1037 icount.dsr = cnow.dsr;
1038 icount.rng = cnow.rng;
1039 icount.dcd = cnow.dcd;
1040 icount.rx = cnow.rx;
1041 icount.tx = cnow.tx;
1042 icount.frame = cnow.frame;
1043 icount.overrun = cnow.overrun;
1044 icount.parity = cnow.parity;
1045 icount.brk = cnow.brk;
1046 icount.buf_overrun = cnow.buf_overrun;
1048 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1052 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1055 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1058 struct uart_state *state = tty->driver_data;
1059 void __user *uarg = (void __user *)arg;
1060 int ret = -ENOIOCTLCMD;
1062 BUG_ON(!kernel_locked());
1065 * These ioctls don't rely on the hardware to be present.
1069 ret = uart_get_info(state, uarg);
1073 ret = uart_set_info(state, uarg);
1077 ret = uart_do_autoconfig(state);
1080 case TIOCSERGWILD: /* obsolete */
1081 case TIOCSERSWILD: /* obsolete */
1086 if (ret != -ENOIOCTLCMD)
1089 if (tty->flags & (1 << TTY_IO_ERROR)) {
1095 * The following should only be used when hardware is present.
1099 ret = uart_wait_modem_status(state, arg);
1103 ret = uart_get_count(state, uarg);
1107 if (ret != -ENOIOCTLCMD)
1110 mutex_lock(&state->mutex);
1112 if (tty_hung_up_p(filp)) {
1118 * All these rely on hardware being present and need to be
1119 * protected against the tty being hung up.
1122 case TIOCSERGETLSR: /* Get line status register */
1123 ret = uart_get_lsr_info(state, uarg);
1127 struct uart_port *port = state->port;
1128 if (port->ops->ioctl)
1129 ret = port->ops->ioctl(port, cmd, arg);
1134 mutex_unlock(&state->mutex);
1139 static void uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1141 struct uart_state *state = tty->driver_data;
1142 unsigned long flags;
1143 unsigned int cflag = tty->termios->c_cflag;
1145 BUG_ON(!kernel_locked());
1148 * These are the bits that are used to setup various
1149 * flags in the low level driver. We can ignore the Bfoo
1150 * bits in c_cflag; c_[io]speed will always be set
1151 * appropriately by set_termios() in tty_ioctl.c
1153 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1154 if ((cflag ^ old_termios->c_cflag) == 0 &&
1155 tty->termios->c_ospeed == old_termios->c_ospeed &&
1156 tty->termios->c_ispeed == old_termios->c_ispeed &&
1157 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1160 uart_change_speed(state, old_termios);
1162 /* Handle transition to B0 status */
1163 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1164 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1166 /* Handle transition away from B0 status */
1167 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1168 unsigned int mask = TIOCM_DTR;
1169 if (!(cflag & CRTSCTS) ||
1170 !test_bit(TTY_THROTTLED, &tty->flags))
1172 uart_set_mctrl(state->port, mask);
1175 /* Handle turning off CRTSCTS */
1176 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1177 spin_lock_irqsave(&state->port->lock, flags);
1178 tty->hw_stopped = 0;
1180 spin_unlock_irqrestore(&state->port->lock, flags);
1183 /* Handle turning on CRTSCTS */
1184 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1185 spin_lock_irqsave(&state->port->lock, flags);
1186 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1187 tty->hw_stopped = 1;
1188 state->port->ops->stop_tx(state->port);
1190 spin_unlock_irqrestore(&state->port->lock, flags);
1195 * No need to wake up processes in open wait, since they
1196 * sample the CLOCAL flag once, and don't recheck it.
1197 * XXX It's not clear whether the current behavior is correct
1198 * or not. Hence, this may change.....
1200 if (!(old_termios->c_cflag & CLOCAL) &&
1201 (tty->termios->c_cflag & CLOCAL))
1202 wake_up_interruptible(&state->info->open_wait);
1207 * In 2.4.5, calls to this will be serialized via the BKL in
1208 * linux/drivers/char/tty_io.c:tty_release()
1209 * linux/drivers/char/tty_io.c:do_tty_handup()
1211 static void uart_close(struct tty_struct *tty, struct file *filp)
1213 struct uart_state *state = tty->driver_data;
1214 struct uart_port *port;
1216 BUG_ON(!kernel_locked());
1218 if (!state || !state->port)
1223 pr_debug("uart_close(%d) called\n", port->line);
1225 mutex_lock(&state->mutex);
1227 if (tty_hung_up_p(filp))
1230 if ((tty->count == 1) && (state->count != 1)) {
1232 * Uh, oh. tty->count is 1, which means that the tty
1233 * structure will be freed. state->count should always
1234 * be one in these conditions. If it's greater than
1235 * one, we've got real problems, since it means the
1236 * serial port won't be shutdown.
1238 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1239 "state->count is %d\n", state->count);
1242 if (--state->count < 0) {
1243 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1244 tty->name, state->count);
1251 * Now we wait for the transmit buffer to clear; and we notify
1252 * the line discipline to only process XON/XOFF characters by
1253 * setting tty->closing.
1257 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1258 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1261 * At this point, we stop accepting input. To do this, we
1262 * disable the receive line status interrupts.
1264 if (state->info->flags & UIF_INITIALIZED) {
1265 unsigned long flags;
1266 spin_lock_irqsave(&port->lock, flags);
1267 port->ops->stop_rx(port);
1268 spin_unlock_irqrestore(&port->lock, flags);
1270 * Before we drop DTR, make sure the UART transmitter
1271 * has completely drained; this is especially
1272 * important if there is a transmit FIFO!
1274 uart_wait_until_sent(tty, port->timeout);
1277 uart_shutdown(state);
1278 uart_flush_buffer(tty);
1280 tty_ldisc_flush(tty);
1283 state->info->tty = NULL;
1285 if (state->info->blocked_open) {
1286 if (state->close_delay)
1287 msleep_interruptible(state->close_delay);
1288 } else if (!uart_console(port)) {
1289 uart_change_pm(state, 3);
1293 * Wake up anyone trying to open this port.
1295 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1296 wake_up_interruptible(&state->info->open_wait);
1299 mutex_unlock(&state->mutex);
1302 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1304 struct uart_state *state = tty->driver_data;
1305 struct uart_port *port = state->port;
1306 unsigned long char_time, expire;
1308 BUG_ON(!kernel_locked());
1310 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1314 * Set the check interval to be 1/5 of the estimated time to
1315 * send a single character, and make it at least 1. The check
1316 * interval should also be less than the timeout.
1318 * Note: we have to use pretty tight timings here to satisfy
1321 char_time = (port->timeout - HZ/50) / port->fifosize;
1322 char_time = char_time / 5;
1325 if (timeout && timeout < char_time)
1326 char_time = timeout;
1329 * If the transmitter hasn't cleared in twice the approximate
1330 * amount of time to send the entire FIFO, it probably won't
1331 * ever clear. This assumes the UART isn't doing flow
1332 * control, which is currently the case. Hence, if it ever
1333 * takes longer than port->timeout, this is probably due to a
1334 * UART bug of some kind. So, we clamp the timeout parameter at
1337 if (timeout == 0 || timeout > 2 * port->timeout)
1338 timeout = 2 * port->timeout;
1340 expire = jiffies + timeout;
1342 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1343 port->line, jiffies, expire);
1346 * Check whether the transmitter is empty every 'char_time'.
1347 * 'timeout' / 'expire' give us the maximum amount of time
1350 while (!port->ops->tx_empty(port)) {
1351 msleep_interruptible(jiffies_to_msecs(char_time));
1352 if (signal_pending(current))
1354 if (time_after(jiffies, expire))
1357 set_current_state(TASK_RUNNING); /* might not be needed */
1361 * This is called with the BKL held in
1362 * linux/drivers/char/tty_io.c:do_tty_hangup()
1363 * We're called from the eventd thread, so we can sleep for
1364 * a _short_ time only.
1366 static void uart_hangup(struct tty_struct *tty)
1368 struct uart_state *state = tty->driver_data;
1370 BUG_ON(!kernel_locked());
1371 pr_debug("uart_hangup(%d)\n", state->port->line);
1373 mutex_lock(&state->mutex);
1374 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1375 uart_flush_buffer(tty);
1376 uart_shutdown(state);
1378 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1379 state->info->tty = NULL;
1380 wake_up_interruptible(&state->info->open_wait);
1381 wake_up_interruptible(&state->info->delta_msr_wait);
1383 mutex_unlock(&state->mutex);
1387 * Copy across the serial console cflag setting into the termios settings
1388 * for the initial open of the port. This allows continuity between the
1389 * kernel settings, and the settings init adopts when it opens the port
1390 * for the first time.
1392 static void uart_update_termios(struct uart_state *state)
1394 struct tty_struct *tty = state->info->tty;
1395 struct uart_port *port = state->port;
1397 if (uart_console(port) && port->cons->cflag) {
1398 tty->termios->c_cflag = port->cons->cflag;
1399 port->cons->cflag = 0;
1403 * If the device failed to grab its irq resources,
1404 * or some other error occurred, don't try to talk
1405 * to the port hardware.
1407 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1409 * Make termios settings take effect.
1411 uart_change_speed(state, NULL);
1414 * And finally enable the RTS and DTR signals.
1416 if (tty->termios->c_cflag & CBAUD)
1417 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1422 * Block the open until the port is ready. We must be called with
1423 * the per-port semaphore held.
1426 uart_block_til_ready(struct file *filp, struct uart_state *state)
1428 DECLARE_WAITQUEUE(wait, current);
1429 struct uart_info *info = state->info;
1430 struct uart_port *port = state->port;
1433 info->blocked_open++;
1436 add_wait_queue(&info->open_wait, &wait);
1438 set_current_state(TASK_INTERRUPTIBLE);
1441 * If we have been hung up, tell userspace/restart open.
1443 if (tty_hung_up_p(filp) || info->tty == NULL)
1447 * If the port has been closed, tell userspace/restart open.
1449 if (!(info->flags & UIF_INITIALIZED))
1453 * If non-blocking mode is set, or CLOCAL mode is set,
1454 * we don't want to wait for the modem status lines to
1455 * indicate that the port is ready.
1457 * Also, if the port is not enabled/configured, we want
1458 * to allow the open to succeed here. Note that we will
1459 * have set TTY_IO_ERROR for a non-existant port.
1461 if ((filp->f_flags & O_NONBLOCK) ||
1462 (info->tty->termios->c_cflag & CLOCAL) ||
1463 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1468 * Set DTR to allow modem to know we're waiting. Do
1469 * not set RTS here - we want to make sure we catch
1470 * the data from the modem.
1472 if (info->tty->termios->c_cflag & CBAUD)
1473 uart_set_mctrl(port, TIOCM_DTR);
1476 * and wait for the carrier to indicate that the
1477 * modem is ready for us.
1479 spin_lock_irq(&port->lock);
1480 port->ops->enable_ms(port);
1481 mctrl = port->ops->get_mctrl(port);
1482 spin_unlock_irq(&port->lock);
1483 if (mctrl & TIOCM_CAR)
1486 mutex_unlock(&state->mutex);
1488 mutex_lock(&state->mutex);
1490 if (signal_pending(current))
1493 set_current_state(TASK_RUNNING);
1494 remove_wait_queue(&info->open_wait, &wait);
1497 info->blocked_open--;
1499 if (signal_pending(current))
1500 return -ERESTARTSYS;
1502 if (!info->tty || tty_hung_up_p(filp))
1508 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1510 struct uart_state *state;
1513 state = drv->state + line;
1514 if (mutex_lock_interruptible(&state->mutex)) {
1520 if (!state->port || state->port->flags & UPF_DEAD) {
1526 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
1528 init_waitqueue_head(&state->info->open_wait);
1529 init_waitqueue_head(&state->info->delta_msr_wait);
1532 * Link the info into the other structures.
1534 state->port->info = state->info;
1536 tasklet_init(&state->info->tlet, uart_tasklet_action,
1537 (unsigned long)state);
1547 mutex_unlock(&state->mutex);
1549 return ERR_PTR(ret);
1553 * In 2.4.5, calls to uart_open are serialised by the BKL in
1554 * linux/fs/devices.c:chrdev_open()
1555 * Note that if this fails, then uart_close() _will_ be called.
1557 * In time, we want to scrap the "opening nonpresent ports"
1558 * behaviour and implement an alternative way for setserial
1559 * to set base addresses/ports/types. This will allow us to
1560 * get rid of a certain amount of extra tests.
1562 static int uart_open(struct tty_struct *tty, struct file *filp)
1564 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1565 struct uart_state *state;
1566 int retval, line = tty->index;
1568 BUG_ON(!kernel_locked());
1569 pr_debug("uart_open(%d) called\n", line);
1572 * tty->driver->num won't change, so we won't fail here with
1573 * tty->driver_data set to something non-NULL (and therefore
1574 * we won't get caught by uart_close()).
1577 if (line >= tty->driver->num)
1581 * We take the semaphore inside uart_get to guarantee that we won't
1582 * be re-entered while allocating the info structure, or while we
1583 * request any IRQs that the driver may need. This also has the nice
1584 * side-effect that it delays the action of uart_hangup, so we can
1585 * guarantee that info->tty will always contain something reasonable.
1587 state = uart_get(drv, line);
1588 if (IS_ERR(state)) {
1589 retval = PTR_ERR(state);
1594 * Once we set tty->driver_data here, we are guaranteed that
1595 * uart_close() will decrement the driver module use count.
1596 * Any failures from here onwards should not touch the count.
1598 tty->driver_data = state;
1599 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1601 state->info->tty = tty;
1604 * If the port is in the middle of closing, bail out now.
1606 if (tty_hung_up_p(filp)) {
1609 mutex_unlock(&state->mutex);
1614 * Make sure the device is in D0 state.
1616 if (state->count == 1)
1617 uart_change_pm(state, 0);
1620 * Start up the serial port.
1622 retval = uart_startup(state, 0);
1625 * If we succeeded, wait until the port is ready.
1628 retval = uart_block_til_ready(filp, state);
1629 mutex_unlock(&state->mutex);
1632 * If this is the first open to succeed, adjust things to suit.
1634 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1635 state->info->flags |= UIF_NORMAL_ACTIVE;
1637 uart_update_termios(state);
1644 static const char *uart_type(struct uart_port *port)
1646 const char *str = NULL;
1648 if (port->ops->type)
1649 str = port->ops->type(port);
1657 #ifdef CONFIG_PROC_FS
1659 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1661 struct uart_state *state = drv->state + i;
1663 struct uart_port *port = state->port;
1665 unsigned int status;
1671 mmio = port->iotype >= UPIO_MEM;
1672 ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
1673 port->line, uart_type(port),
1674 mmio ? "mmio:0x" : "port:",
1675 mmio ? (unsigned long long)port->mapbase
1676 : (unsigned long long) port->iobase,
1679 if (port->type == PORT_UNKNOWN) {
1684 if(capable(CAP_SYS_ADMIN))
1686 mutex_lock(&state->mutex);
1687 pm_state = state->pm_state;
1689 uart_change_pm(state, 0);
1690 spin_lock_irq(&port->lock);
1691 status = port->ops->get_mctrl(port);
1692 spin_unlock_irq(&port->lock);
1694 uart_change_pm(state, pm_state);
1695 mutex_unlock(&state->mutex);
1697 ret += sprintf(buf + ret, " tx:%d rx:%d",
1698 port->icount.tx, port->icount.rx);
1699 if (port->icount.frame)
1700 ret += sprintf(buf + ret, " fe:%d",
1701 port->icount.frame);
1702 if (port->icount.parity)
1703 ret += sprintf(buf + ret, " pe:%d",
1704 port->icount.parity);
1705 if (port->icount.brk)
1706 ret += sprintf(buf + ret, " brk:%d",
1708 if (port->icount.overrun)
1709 ret += sprintf(buf + ret, " oe:%d",
1710 port->icount.overrun);
1712 #define INFOBIT(bit,str) \
1713 if (port->mctrl & (bit)) \
1714 strncat(stat_buf, (str), sizeof(stat_buf) - \
1715 strlen(stat_buf) - 2)
1716 #define STATBIT(bit,str) \
1717 if (status & (bit)) \
1718 strncat(stat_buf, (str), sizeof(stat_buf) - \
1719 strlen(stat_buf) - 2)
1723 INFOBIT(TIOCM_RTS, "|RTS");
1724 STATBIT(TIOCM_CTS, "|CTS");
1725 INFOBIT(TIOCM_DTR, "|DTR");
1726 STATBIT(TIOCM_DSR, "|DSR");
1727 STATBIT(TIOCM_CAR, "|CD");
1728 STATBIT(TIOCM_RNG, "|RI");
1731 strcat(stat_buf, "\n");
1733 ret += sprintf(buf + ret, stat_buf);
1743 static int uart_read_proc(char *page, char **start, off_t off,
1744 int count, int *eof, void *data)
1746 struct tty_driver *ttydrv = data;
1747 struct uart_driver *drv = ttydrv->driver_state;
1751 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1753 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1754 l = uart_line_info(page + len, drv, i);
1756 if (len + begin > off + count)
1758 if (len + begin < off) {
1765 if (off >= len + begin)
1767 *start = page + (off - begin);
1768 return (count < begin + len - off) ? count : (begin + len - off);
1772 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1774 * uart_console_write - write a console message to a serial port
1775 * @port: the port to write the message
1776 * @s: array of characters
1777 * @count: number of characters in string to write
1778 * @write: function to write character to port
1780 void uart_console_write(struct uart_port *port, const char *s,
1782 void (*putchar)(struct uart_port *, int))
1786 for (i = 0; i < count; i++, s++) {
1788 putchar(port, '\r');
1792 EXPORT_SYMBOL_GPL(uart_console_write);
1795 * Check whether an invalid uart number has been specified, and
1796 * if so, search for the first available port that does have
1799 struct uart_port * __init
1800 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1802 int idx = co->index;
1804 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1805 ports[idx].membase == NULL))
1806 for (idx = 0; idx < nr; idx++)
1807 if (ports[idx].iobase != 0 ||
1808 ports[idx].membase != NULL)
1817 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1818 * @options: pointer to option string
1819 * @baud: pointer to an 'int' variable for the baud rate.
1820 * @parity: pointer to an 'int' variable for the parity.
1821 * @bits: pointer to an 'int' variable for the number of data bits.
1822 * @flow: pointer to an 'int' variable for the flow control character.
1824 * uart_parse_options decodes a string containing the serial console
1825 * options. The format of the string is <baud><parity><bits><flow>,
1829 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1833 *baud = simple_strtoul(s, NULL, 10);
1834 while (*s >= '0' && *s <= '9')
1849 static const struct baud_rates baud_rates[] = {
1850 { 921600, B921600 },
1851 { 460800, B460800 },
1852 { 230400, B230400 },
1853 { 115200, B115200 },
1865 * uart_set_options - setup the serial console parameters
1866 * @port: pointer to the serial ports uart_port structure
1867 * @co: console pointer
1869 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1870 * @bits: number of data bits
1871 * @flow: flow control character - 'r' (rts)
1874 uart_set_options(struct uart_port *port, struct console *co,
1875 int baud, int parity, int bits, int flow)
1877 struct ktermios termios;
1881 * Ensure that the serial console lock is initialised
1884 spin_lock_init(&port->lock);
1885 lockdep_set_class(&port->lock, &port_lock_key);
1887 memset(&termios, 0, sizeof(struct ktermios));
1889 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1892 * Construct a cflag setting.
1894 for (i = 0; baud_rates[i].rate; i++)
1895 if (baud_rates[i].rate <= baud)
1898 termios.c_cflag |= baud_rates[i].cflag;
1901 termios.c_cflag |= CS7;
1903 termios.c_cflag |= CS8;
1907 termios.c_cflag |= PARODD;
1910 termios.c_cflag |= PARENB;
1915 termios.c_cflag |= CRTSCTS;
1918 * some uarts on other side don't support no flow control.
1919 * So we set * DTR in host uart to make them happy
1921 port->mctrl |= TIOCM_DTR;
1923 port->ops->set_termios(port, &termios, NULL);
1924 co->cflag = termios.c_cflag;
1928 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1930 static void uart_change_pm(struct uart_state *state, int pm_state)
1932 struct uart_port *port = state->port;
1934 if (state->pm_state != pm_state) {
1936 port->ops->pm(port, pm_state, state->pm_state);
1937 state->pm_state = pm_state;
1941 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1943 struct uart_state *state = drv->state + port->line;
1945 mutex_lock(&state->mutex);
1947 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1948 if (uart_console(port)) {
1949 mutex_unlock(&state->mutex);
1954 if (state->info && state->info->flags & UIF_INITIALIZED) {
1955 const struct uart_ops *ops = port->ops;
1957 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
1960 spin_lock_irq(&port->lock);
1962 ops->set_mctrl(port, 0);
1964 spin_unlock_irq(&port->lock);
1967 * Wait for the transmitter to empty.
1969 while (!ops->tx_empty(port)) {
1973 ops->shutdown(port);
1977 * Disable the console device before suspending.
1979 if (uart_console(port))
1980 console_stop(port->cons);
1982 uart_change_pm(state, 3);
1984 mutex_unlock(&state->mutex);
1989 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1991 struct uart_state *state = drv->state + port->line;
1993 mutex_lock(&state->mutex);
1995 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1996 if (uart_console(port)) {
1997 mutex_unlock(&state->mutex);
2002 uart_change_pm(state, 0);
2005 * Re-enable the console device after suspending.
2007 if (uart_console(port)) {
2008 struct ktermios termios;
2011 * First try to use the console cflag setting.
2013 memset(&termios, 0, sizeof(struct ktermios));
2014 termios.c_cflag = port->cons->cflag;
2017 * If that's unset, use the tty termios setting.
2019 if (state->info && state->info->tty && termios.c_cflag == 0)
2020 termios = *state->info->tty->termios;
2022 port->ops->set_termios(port, &termios, NULL);
2023 console_start(port->cons);
2026 if (state->info && state->info->flags & UIF_SUSPENDED) {
2027 const struct uart_ops *ops = port->ops;
2030 ops->set_mctrl(port, 0);
2031 ret = ops->startup(port);
2033 uart_change_speed(state, NULL);
2034 spin_lock_irq(&port->lock);
2035 ops->set_mctrl(port, port->mctrl);
2036 ops->start_tx(port);
2037 spin_unlock_irq(&port->lock);
2038 state->info->flags |= UIF_INITIALIZED;
2041 * Failed to resume - maybe hardware went away?
2042 * Clear the "initialized" flag so we won't try
2043 * to call the low level drivers shutdown method.
2045 uart_shutdown(state);
2048 state->info->flags &= ~UIF_SUSPENDED;
2051 mutex_unlock(&state->mutex);
2057 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2061 switch (port->iotype) {
2063 snprintf(address, sizeof(address),
2064 "I/O 0x%x", port->iobase);
2067 snprintf(address, sizeof(address),
2068 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
2075 snprintf(address, sizeof(address),
2076 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2079 strlcpy(address, "*unknown*", sizeof(address));
2083 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2084 port->dev ? port->dev->bus_id : "",
2085 port->dev ? ": " : "",
2086 drv->dev_name, port->line, address, port->irq, uart_type(port));
2090 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2091 struct uart_port *port)
2096 * If there isn't a port here, don't do anything further.
2098 if (!port->iobase && !port->mapbase && !port->membase)
2102 * Now do the auto configuration stuff. Note that config_port
2103 * is expected to claim the resources and map the port for us.
2105 flags = UART_CONFIG_TYPE;
2106 if (port->flags & UPF_AUTO_IRQ)
2107 flags |= UART_CONFIG_IRQ;
2108 if (port->flags & UPF_BOOT_AUTOCONF) {
2109 port->type = PORT_UNKNOWN;
2110 port->ops->config_port(port, flags);
2113 if (port->type != PORT_UNKNOWN) {
2114 unsigned long flags;
2116 uart_report_port(drv, port);
2118 /* Power up port for set_mctrl() */
2119 uart_change_pm(state, 0);
2122 * Ensure that the modem control lines are de-activated.
2123 * We probably don't need a spinlock around this, but
2125 spin_lock_irqsave(&port->lock, flags);
2126 port->ops->set_mctrl(port, 0);
2127 spin_unlock_irqrestore(&port->lock, flags);
2130 * Power down all ports by default, except the
2131 * console if we have one.
2133 if (!uart_console(port))
2134 uart_change_pm(state, 3);
2138 static const struct tty_operations uart_ops = {
2140 .close = uart_close,
2141 .write = uart_write,
2142 .put_char = uart_put_char,
2143 .flush_chars = uart_flush_chars,
2144 .write_room = uart_write_room,
2145 .chars_in_buffer= uart_chars_in_buffer,
2146 .flush_buffer = uart_flush_buffer,
2147 .ioctl = uart_ioctl,
2148 .throttle = uart_throttle,
2149 .unthrottle = uart_unthrottle,
2150 .send_xchar = uart_send_xchar,
2151 .set_termios = uart_set_termios,
2153 .start = uart_start,
2154 .hangup = uart_hangup,
2155 .break_ctl = uart_break_ctl,
2156 .wait_until_sent= uart_wait_until_sent,
2157 #ifdef CONFIG_PROC_FS
2158 .read_proc = uart_read_proc,
2160 .tiocmget = uart_tiocmget,
2161 .tiocmset = uart_tiocmset,
2165 * uart_register_driver - register a driver with the uart core layer
2166 * @drv: low level driver structure
2168 * Register a uart driver with the core driver. We in turn register
2169 * with the tty layer, and initialise the core driver per-port state.
2171 * We have a proc file in /proc/tty/driver which is named after the
2174 * drv->port should be NULL, and the per-port structures should be
2175 * registered using uart_add_one_port after this call has succeeded.
2177 int uart_register_driver(struct uart_driver *drv)
2179 struct tty_driver *normal = NULL;
2185 * Maybe we should be using a slab cache for this, especially if
2186 * we have a large number of ports to handle.
2188 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2193 normal = alloc_tty_driver(drv->nr);
2197 drv->tty_driver = normal;
2199 normal->owner = drv->owner;
2200 normal->driver_name = drv->driver_name;
2201 normal->name = drv->dev_name;
2202 normal->major = drv->major;
2203 normal->minor_start = drv->minor;
2204 normal->type = TTY_DRIVER_TYPE_SERIAL;
2205 normal->subtype = SERIAL_TYPE_NORMAL;
2206 normal->init_termios = tty_std_termios;
2207 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2208 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2209 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2210 normal->driver_state = drv;
2211 tty_set_operations(normal, &uart_ops);
2214 * Initialise the UART state(s).
2216 for (i = 0; i < drv->nr; i++) {
2217 struct uart_state *state = drv->state + i;
2219 state->close_delay = 500; /* .5 seconds */
2220 state->closing_wait = 30000; /* 30 seconds */
2222 mutex_init(&state->mutex);
2225 retval = tty_register_driver(normal);
2228 put_tty_driver(normal);
2235 * uart_unregister_driver - remove a driver from the uart core layer
2236 * @drv: low level driver structure
2238 * Remove all references to a driver from the core driver. The low
2239 * level driver must have removed all its ports via the
2240 * uart_remove_one_port() if it registered them with uart_add_one_port().
2241 * (ie, drv->port == NULL)
2243 void uart_unregister_driver(struct uart_driver *drv)
2245 struct tty_driver *p = drv->tty_driver;
2246 tty_unregister_driver(p);
2249 drv->tty_driver = NULL;
2252 struct tty_driver *uart_console_device(struct console *co, int *index)
2254 struct uart_driver *p = co->data;
2256 return p->tty_driver;
2260 * uart_add_one_port - attach a driver-defined port structure
2261 * @drv: pointer to the uart low level driver structure for this port
2262 * @port: uart port structure to use for this port.
2264 * This allows the driver to register its own uart_port structure
2265 * with the core driver. The main purpose is to allow the low
2266 * level uart drivers to expand uart_port, rather than having yet
2267 * more levels of structures.
2269 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2271 struct uart_state *state;
2274 BUG_ON(in_interrupt());
2276 if (port->line >= drv->nr)
2279 state = drv->state + port->line;
2281 mutex_lock(&port_mutex);
2282 mutex_lock(&state->mutex);
2290 port->cons = drv->cons;
2291 port->info = state->info;
2294 * If this port is a console, then the spinlock is already
2297 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2298 spin_lock_init(&port->lock);
2299 lockdep_set_class(&port->lock, &port_lock_key);
2302 uart_configure_port(drv, state, port);
2305 * Register the port whether it's detected or not. This allows
2306 * setserial to be used to alter this ports parameters.
2308 tty_register_device(drv->tty_driver, port->line, port->dev);
2311 * If this driver supports console, and it hasn't been
2312 * successfully registered yet, try to re-register it.
2313 * It may be that the port was not available.
2315 if (port->type != PORT_UNKNOWN &&
2316 port->cons && !(port->cons->flags & CON_ENABLED))
2317 register_console(port->cons);
2320 * Ensure UPF_DEAD is not set.
2322 port->flags &= ~UPF_DEAD;
2325 mutex_unlock(&state->mutex);
2326 mutex_unlock(&port_mutex);
2332 * uart_remove_one_port - detach a driver defined port structure
2333 * @drv: pointer to the uart low level driver structure for this port
2334 * @port: uart port structure for this port
2336 * This unhooks (and hangs up) the specified port structure from the
2337 * core driver. No further calls will be made to the low-level code
2340 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2342 struct uart_state *state = drv->state + port->line;
2343 struct uart_info *info;
2345 BUG_ON(in_interrupt());
2347 if (state->port != port)
2348 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2351 mutex_lock(&port_mutex);
2354 * Mark the port "dead" - this prevents any opens from
2355 * succeeding while we shut down the port.
2357 mutex_lock(&state->mutex);
2358 port->flags |= UPF_DEAD;
2359 mutex_unlock(&state->mutex);
2362 * Remove the devices from the tty layer
2364 tty_unregister_device(drv->tty_driver, port->line);
2367 if (info && info->tty)
2368 tty_vhangup(info->tty);
2371 * All users of this port should now be disconnected from
2372 * this driver, and the port shut down. We should be the
2373 * only thread fiddling with this port from now on.
2378 * Free the port IO and memory resources, if any.
2380 if (port->type != PORT_UNKNOWN)
2381 port->ops->release_port(port);
2384 * Indicate that there isn't a port here anymore.
2386 port->type = PORT_UNKNOWN;
2389 * Kill the tasklet, and free resources.
2392 tasklet_kill(&info->tlet);
2397 mutex_unlock(&port_mutex);
2403 * Are the two ports equivalent?
2405 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2407 if (port1->iotype != port2->iotype)
2410 switch (port1->iotype) {
2412 return (port1->iobase == port2->iobase);
2414 return (port1->iobase == port2->iobase) &&
2415 (port1->hub6 == port2->hub6);
2421 return (port1->mapbase == port2->mapbase);
2425 EXPORT_SYMBOL(uart_match_port);
2427 EXPORT_SYMBOL(uart_write_wakeup);
2428 EXPORT_SYMBOL(uart_register_driver);
2429 EXPORT_SYMBOL(uart_unregister_driver);
2430 EXPORT_SYMBOL(uart_suspend_port);
2431 EXPORT_SYMBOL(uart_resume_port);
2432 EXPORT_SYMBOL(uart_add_one_port);
2433 EXPORT_SYMBOL(uart_remove_one_port);
2435 MODULE_DESCRIPTION("Serial driver core");
2436 MODULE_LICENSE("GPL");