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;
75 * This means you called this function _after_ the port was
76 * closed. No cookie for you.
79 tasklet_schedule(&info->tlet);
82 static void uart_stop(struct tty_struct *tty)
84 struct uart_state *state = tty->driver_data;
85 struct uart_port *port = state->port;
88 spin_lock_irqsave(&port->lock, flags);
89 port->ops->stop_tx(port);
90 spin_unlock_irqrestore(&port->lock, flags);
93 static void __uart_start(struct tty_struct *tty)
95 struct uart_state *state = tty->driver_data;
96 struct uart_port *port = state->port;
98 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
99 !tty->stopped && !tty->hw_stopped)
100 port->ops->start_tx(port);
103 static void uart_start(struct tty_struct *tty)
105 struct uart_state *state = tty->driver_data;
106 struct uart_port *port = state->port;
109 spin_lock_irqsave(&port->lock, flags);
111 spin_unlock_irqrestore(&port->lock, flags);
114 static void uart_tasklet_action(unsigned long data)
116 struct uart_state *state = (struct uart_state *)data;
117 tty_wakeup(state->info->tty);
121 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
126 spin_lock_irqsave(&port->lock, flags);
128 port->mctrl = (old & ~clear) | set;
129 if (old != port->mctrl)
130 port->ops->set_mctrl(port, port->mctrl);
131 spin_unlock_irqrestore(&port->lock, flags);
134 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
135 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
138 * Startup the port. This will be called once per open. All calls
139 * will be serialised by the per-port semaphore.
141 static int uart_startup(struct uart_state *state, int init_hw)
143 struct uart_info *info = state->info;
144 struct uart_port *port = state->port;
148 if (info->flags & UIF_INITIALIZED)
152 * Set the TTY IO error marker - we will only clear this
153 * once we have successfully opened the port. Also set
154 * up the tty->alt_speed kludge
156 set_bit(TTY_IO_ERROR, &info->tty->flags);
158 if (port->type == PORT_UNKNOWN)
162 * Initialise and allocate the transmit and temporary
165 if (!info->xmit.buf) {
166 page = get_zeroed_page(GFP_KERNEL);
170 info->xmit.buf = (unsigned char *) page;
171 uart_circ_clear(&info->xmit);
174 retval = port->ops->startup(port);
178 * Initialise the hardware port settings.
180 uart_change_speed(state, NULL);
183 * Setup the RTS and DTR signals once the
184 * port is open and ready to respond.
186 if (info->tty->termios->c_cflag & CBAUD)
187 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
190 if (info->flags & UIF_CTS_FLOW) {
191 spin_lock_irq(&port->lock);
192 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
193 info->tty->hw_stopped = 1;
194 spin_unlock_irq(&port->lock);
197 info->flags |= UIF_INITIALIZED;
199 clear_bit(TTY_IO_ERROR, &info->tty->flags);
202 if (retval && capable(CAP_SYS_ADMIN))
209 * This routine will shutdown a serial port; interrupts are disabled, and
210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore.
213 static void uart_shutdown(struct uart_state *state)
215 struct uart_info *info = state->info;
216 struct uart_port *port = state->port;
219 * Set the TTY IO error marker
222 set_bit(TTY_IO_ERROR, &info->tty->flags);
224 if (info->flags & UIF_INITIALIZED) {
225 info->flags &= ~UIF_INITIALIZED;
228 * Turn off DTR and RTS early.
230 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
231 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
234 * clear delta_msr_wait queue to avoid mem leaks: we may free
235 * the irq here so the queue might never be woken up. Note
236 * that we won't end up waiting on delta_msr_wait again since
237 * any outstanding file descriptors should be pointing at
238 * hung_up_tty_fops now.
240 wake_up_interruptible(&info->delta_msr_wait);
243 * Free the IRQ and disable the port.
245 port->ops->shutdown(port);
248 * Ensure that the IRQ handler isn't running on another CPU.
250 synchronize_irq(port->irq);
254 * kill off our tasklet
256 tasklet_kill(&info->tlet);
259 * Free the transmit buffer page.
261 if (info->xmit.buf) {
262 free_page((unsigned long)info->xmit.buf);
263 info->xmit.buf = NULL;
268 * uart_update_timeout - update per-port FIFO timeout.
269 * @port: uart_port structure describing the port
270 * @cflag: termios cflag value
271 * @baud: speed of the port
273 * Set the port FIFO timeout value. The @cflag value should
274 * reflect the actual hardware settings.
277 uart_update_timeout(struct uart_port *port, unsigned int cflag,
282 /* byte size and parity */
283 switch (cflag & CSIZE) {
304 * The total number of bits to be transmitted in the fifo.
306 bits = bits * port->fifosize;
309 * Figure the timeout to send the above number of bits.
310 * Add .02 seconds of slop
312 port->timeout = (HZ * bits) / baud + HZ/50;
315 EXPORT_SYMBOL(uart_update_timeout);
318 * uart_get_baud_rate - return baud rate for a particular port
319 * @port: uart_port structure describing the port in question.
320 * @termios: desired termios settings.
321 * @old: old termios (or NULL)
322 * @min: minimum acceptable baud rate
323 * @max: maximum acceptable baud rate
325 * Decode the termios structure into a numeric baud rate,
326 * taking account of the magic 38400 baud rate (with spd_*
327 * flags), and mapping the %B0 rate to 9600 baud.
329 * If the new baud rate is invalid, try the old termios setting.
330 * If it's still invalid, we try 9600 baud.
332 * Update the @termios structure to reflect the baud rate
333 * we're actually going to be using.
336 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
337 struct termios *old, unsigned int min, unsigned int max)
339 unsigned int try, baud, altbaud = 38400;
340 upf_t flags = port->flags & UPF_SPD_MASK;
342 if (flags == UPF_SPD_HI)
344 if (flags == UPF_SPD_VHI)
346 if (flags == UPF_SPD_SHI)
348 if (flags == UPF_SPD_WARP)
351 for (try = 0; try < 2; try++) {
352 baud = tty_termios_baud_rate(termios);
355 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
362 * Special case: B0 rate.
367 if (baud >= min && baud <= max)
371 * Oops, the quotient was zero. Try again with
372 * the old baud rate if possible.
374 termios->c_cflag &= ~CBAUD;
376 termios->c_cflag |= old->c_cflag & CBAUD;
382 * As a last resort, if the quotient is zero,
383 * default to 9600 bps
385 termios->c_cflag |= B9600;
391 EXPORT_SYMBOL(uart_get_baud_rate);
394 * uart_get_divisor - return uart clock divisor
395 * @port: uart_port structure describing the port.
396 * @baud: desired baud rate
398 * Calculate the uart clock divisor for the port.
401 uart_get_divisor(struct uart_port *port, unsigned int baud)
406 * Old custom speed handling.
408 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
409 quot = port->custom_divisor;
411 quot = (port->uartclk + (8 * baud)) / (16 * baud);
416 EXPORT_SYMBOL(uart_get_divisor);
419 uart_change_speed(struct uart_state *state, struct termios *old_termios)
421 struct tty_struct *tty = state->info->tty;
422 struct uart_port *port = state->port;
423 struct termios *termios;
426 * If we have no tty, termios, or the port does not exist,
427 * then we can't set the parameters for this port.
429 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
432 termios = tty->termios;
435 * Set flags based on termios cflag
437 if (termios->c_cflag & CRTSCTS)
438 state->info->flags |= UIF_CTS_FLOW;
440 state->info->flags &= ~UIF_CTS_FLOW;
442 if (termios->c_cflag & CLOCAL)
443 state->info->flags &= ~UIF_CHECK_CD;
445 state->info->flags |= UIF_CHECK_CD;
447 port->ops->set_termios(port, termios, old_termios);
451 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
458 spin_lock_irqsave(&port->lock, flags);
459 if (uart_circ_chars_free(circ) != 0) {
460 circ->buf[circ->head] = c;
461 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
463 spin_unlock_irqrestore(&port->lock, flags);
466 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
468 struct uart_state *state = tty->driver_data;
470 __uart_put_char(state->port, &state->info->xmit, ch);
473 static void uart_flush_chars(struct tty_struct *tty)
479 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
481 struct uart_state *state = tty->driver_data;
482 struct uart_port *port;
483 struct circ_buf *circ;
488 * This means you called this function _after_ the port was
489 * closed. No cookie for you.
491 if (!state || !state->info) {
497 circ = &state->info->xmit;
502 spin_lock_irqsave(&port->lock, flags);
504 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
509 memcpy(circ->buf + circ->head, buf, c);
510 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
515 spin_unlock_irqrestore(&port->lock, flags);
521 static int uart_write_room(struct tty_struct *tty)
523 struct uart_state *state = tty->driver_data;
525 return uart_circ_chars_free(&state->info->xmit);
528 static int uart_chars_in_buffer(struct tty_struct *tty)
530 struct uart_state *state = tty->driver_data;
532 return uart_circ_chars_pending(&state->info->xmit);
535 static void uart_flush_buffer(struct tty_struct *tty)
537 struct uart_state *state = tty->driver_data;
538 struct uart_port *port = state->port;
542 * This means you called this function _after_ the port was
543 * closed. No cookie for you.
545 if (!state || !state->info) {
550 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
552 spin_lock_irqsave(&port->lock, flags);
553 uart_circ_clear(&state->info->xmit);
554 spin_unlock_irqrestore(&port->lock, flags);
559 * This function is used to send a high-priority XON/XOFF character to
562 static void uart_send_xchar(struct tty_struct *tty, char ch)
564 struct uart_state *state = tty->driver_data;
565 struct uart_port *port = state->port;
568 if (port->ops->send_xchar)
569 port->ops->send_xchar(port, ch);
573 spin_lock_irqsave(&port->lock, flags);
574 port->ops->start_tx(port);
575 spin_unlock_irqrestore(&port->lock, flags);
580 static void uart_throttle(struct tty_struct *tty)
582 struct uart_state *state = tty->driver_data;
585 uart_send_xchar(tty, STOP_CHAR(tty));
587 if (tty->termios->c_cflag & CRTSCTS)
588 uart_clear_mctrl(state->port, TIOCM_RTS);
591 static void uart_unthrottle(struct tty_struct *tty)
593 struct uart_state *state = tty->driver_data;
594 struct uart_port *port = state->port;
600 uart_send_xchar(tty, START_CHAR(tty));
603 if (tty->termios->c_cflag & CRTSCTS)
604 uart_set_mctrl(port, TIOCM_RTS);
607 static int uart_get_info(struct uart_state *state,
608 struct serial_struct __user *retinfo)
610 struct uart_port *port = state->port;
611 struct serial_struct tmp;
613 memset(&tmp, 0, sizeof(tmp));
614 tmp.type = port->type;
615 tmp.line = port->line;
616 tmp.port = port->iobase;
617 if (HIGH_BITS_OFFSET)
618 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
620 tmp.flags = port->flags;
621 tmp.xmit_fifo_size = port->fifosize;
622 tmp.baud_base = port->uartclk / 16;
623 tmp.close_delay = state->close_delay / 10;
624 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
625 ASYNC_CLOSING_WAIT_NONE :
626 state->closing_wait / 10;
627 tmp.custom_divisor = port->custom_divisor;
628 tmp.hub6 = port->hub6;
629 tmp.io_type = port->iotype;
630 tmp.iomem_reg_shift = port->regshift;
631 tmp.iomem_base = (void *)port->mapbase;
633 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
638 static int uart_set_info(struct uart_state *state,
639 struct serial_struct __user *newinfo)
641 struct serial_struct new_serial;
642 struct uart_port *port = state->port;
643 unsigned long new_port;
644 unsigned int change_irq, change_port, closing_wait;
645 unsigned int old_custom_divisor, close_delay;
646 upf_t old_flags, new_flags;
649 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
652 new_port = new_serial.port;
653 if (HIGH_BITS_OFFSET)
654 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
656 new_serial.irq = irq_canonicalize(new_serial.irq);
657 close_delay = new_serial.close_delay * 10;
658 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
659 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
662 * This semaphore protects state->count. It is also
663 * very useful to prevent opens. Also, take the
664 * port configuration semaphore to make sure that a
665 * module insertion/removal doesn't change anything
668 mutex_lock(&state->mutex);
670 change_irq = new_serial.irq != port->irq;
673 * Since changing the 'type' of the port changes its resource
674 * allocations, we should treat type changes the same as
677 change_port = 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 != port->fifosize) ||
695 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
697 port->flags = ((port->flags & ~UPF_USR_MASK) |
698 (new_flags & UPF_USR_MASK));
699 port->custom_divisor = new_serial.custom_divisor;
704 * Ask the low level driver to verify the settings.
706 if (port->ops->verify_port)
707 retval = port->ops->verify_port(port, &new_serial);
709 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
710 (new_serial.baud_base < 9600))
716 if (change_port || change_irq) {
720 * Make sure that we are the sole user of this port.
722 if (uart_users(state) > 1)
726 * We need to shutdown the serial port at the old
727 * port/type/irq combination.
729 uart_shutdown(state);
733 unsigned long old_iobase, old_mapbase;
734 unsigned int old_type, old_iotype, old_hub6, old_shift;
736 old_iobase = port->iobase;
737 old_mapbase = port->mapbase;
738 old_type = port->type;
739 old_hub6 = port->hub6;
740 old_iotype = port->iotype;
741 old_shift = port->regshift;
744 * Free and release old regions
746 if (old_type != PORT_UNKNOWN)
747 port->ops->release_port(port);
749 port->iobase = new_port;
750 port->type = new_serial.type;
751 port->hub6 = new_serial.hub6;
752 port->iotype = new_serial.io_type;
753 port->regshift = new_serial.iomem_reg_shift;
754 port->mapbase = (unsigned long)new_serial.iomem_base;
757 * Claim and map the new regions
759 if (port->type != PORT_UNKNOWN) {
760 retval = port->ops->request_port(port);
762 /* Always success - Jean II */
767 * If we fail to request resources for the
768 * new port, try to restore the old settings.
770 if (retval && old_type != PORT_UNKNOWN) {
771 port->iobase = old_iobase;
772 port->type = old_type;
773 port->hub6 = old_hub6;
774 port->iotype = old_iotype;
775 port->regshift = old_shift;
776 port->mapbase = old_mapbase;
777 retval = port->ops->request_port(port);
779 * If we failed to restore the old settings,
783 port->type = PORT_UNKNOWN;
792 port->irq = new_serial.irq;
793 port->uartclk = new_serial.baud_base * 16;
794 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
795 (new_flags & UPF_CHANGE_MASK);
796 port->custom_divisor = new_serial.custom_divisor;
797 state->close_delay = close_delay;
798 state->closing_wait = closing_wait;
799 port->fifosize = new_serial.xmit_fifo_size;
800 if (state->info->tty)
801 state->info->tty->low_latency =
802 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
806 if (port->type == PORT_UNKNOWN)
808 if (state->info->flags & UIF_INITIALIZED) {
809 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
810 old_custom_divisor != port->custom_divisor) {
812 * If they're setting up a custom divisor or speed,
813 * instead of clearing it, then bitch about it. No
814 * need to rate-limit; it's CAP_SYS_ADMIN only.
816 if (port->flags & UPF_SPD_MASK) {
819 "%s sets custom speed on %s. This "
820 "is deprecated.\n", current->comm,
821 tty_name(state->info->tty, buf));
823 uart_change_speed(state, NULL);
826 retval = uart_startup(state, 1);
828 mutex_unlock(&state->mutex);
834 * uart_get_lsr_info - get line status register info.
835 * Note: uart_ioctl protects us against hangups.
837 static int uart_get_lsr_info(struct uart_state *state,
838 unsigned int __user *value)
840 struct uart_port *port = state->port;
843 result = port->ops->tx_empty(port);
846 * If we're about to load something into the transmit
847 * register, we'll pretend the transmitter isn't empty to
848 * avoid a race condition (depending on when the transmit
849 * interrupt happens).
852 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
853 !state->info->tty->stopped && !state->info->tty->hw_stopped))
854 result &= ~TIOCSER_TEMT;
856 return put_user(result, value);
859 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
861 struct uart_state *state = tty->driver_data;
862 struct uart_port *port = state->port;
865 mutex_lock(&state->mutex);
866 if ((!file || !tty_hung_up_p(file)) &&
867 !(tty->flags & (1 << TTY_IO_ERROR))) {
868 result = port->mctrl;
870 spin_lock_irq(&port->lock);
871 result |= port->ops->get_mctrl(port);
872 spin_unlock_irq(&port->lock);
874 mutex_unlock(&state->mutex);
880 uart_tiocmset(struct tty_struct *tty, struct file *file,
881 unsigned int set, unsigned int clear)
883 struct uart_state *state = tty->driver_data;
884 struct uart_port *port = state->port;
887 mutex_lock(&state->mutex);
888 if ((!file || !tty_hung_up_p(file)) &&
889 !(tty->flags & (1 << TTY_IO_ERROR))) {
890 uart_update_mctrl(port, set, clear);
893 mutex_unlock(&state->mutex);
897 static void uart_break_ctl(struct tty_struct *tty, int break_state)
899 struct uart_state *state = tty->driver_data;
900 struct uart_port *port = state->port;
902 BUG_ON(!kernel_locked());
904 mutex_lock(&state->mutex);
906 if (port->type != PORT_UNKNOWN)
907 port->ops->break_ctl(port, break_state);
909 mutex_unlock(&state->mutex);
912 static int uart_do_autoconfig(struct uart_state *state)
914 struct uart_port *port = state->port;
917 if (!capable(CAP_SYS_ADMIN))
921 * Take the per-port semaphore. This prevents count from
922 * changing, and hence any extra opens of the port while
923 * we're auto-configuring.
925 if (mutex_lock_interruptible(&state->mutex))
929 if (uart_users(state) == 1) {
930 uart_shutdown(state);
933 * If we already have a port type configured,
934 * we must release its resources.
936 if (port->type != PORT_UNKNOWN)
937 port->ops->release_port(port);
939 flags = UART_CONFIG_TYPE;
940 if (port->flags & UPF_AUTO_IRQ)
941 flags |= UART_CONFIG_IRQ;
944 * This will claim the ports resources if
947 port->ops->config_port(port, flags);
949 ret = uart_startup(state, 1);
951 mutex_unlock(&state->mutex);
956 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
957 * - mask passed in arg for lines of interest
958 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
959 * Caller should use TIOCGICOUNT to see which one it was
962 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
964 struct uart_port *port = state->port;
965 DECLARE_WAITQUEUE(wait, current);
966 struct uart_icount cprev, cnow;
970 * note the counters on entry
972 spin_lock_irq(&port->lock);
973 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
976 * Force modem status interrupts on
978 port->ops->enable_ms(port);
979 spin_unlock_irq(&port->lock);
981 add_wait_queue(&state->info->delta_msr_wait, &wait);
983 spin_lock_irq(&port->lock);
984 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
985 spin_unlock_irq(&port->lock);
987 set_current_state(TASK_INTERRUPTIBLE);
989 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
990 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
991 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
992 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
999 /* see if a signal did it */
1000 if (signal_pending(current)) {
1008 current->state = TASK_RUNNING;
1009 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1015 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1016 * Return: write counters to the user passed counter struct
1017 * NB: both 1->0 and 0->1 transitions are counted except for
1018 * RI where only 0->1 is counted.
1020 static int uart_get_count(struct uart_state *state,
1021 struct serial_icounter_struct __user *icnt)
1023 struct serial_icounter_struct icount;
1024 struct uart_icount cnow;
1025 struct uart_port *port = state->port;
1027 spin_lock_irq(&port->lock);
1028 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1029 spin_unlock_irq(&port->lock);
1031 icount.cts = cnow.cts;
1032 icount.dsr = cnow.dsr;
1033 icount.rng = cnow.rng;
1034 icount.dcd = cnow.dcd;
1035 icount.rx = cnow.rx;
1036 icount.tx = cnow.tx;
1037 icount.frame = cnow.frame;
1038 icount.overrun = cnow.overrun;
1039 icount.parity = cnow.parity;
1040 icount.brk = cnow.brk;
1041 icount.buf_overrun = cnow.buf_overrun;
1043 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1047 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1050 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1053 struct uart_state *state = tty->driver_data;
1054 void __user *uarg = (void __user *)arg;
1055 int ret = -ENOIOCTLCMD;
1057 BUG_ON(!kernel_locked());
1060 * These ioctls don't rely on the hardware to be present.
1064 ret = uart_get_info(state, uarg);
1068 ret = uart_set_info(state, uarg);
1072 ret = uart_do_autoconfig(state);
1075 case TIOCSERGWILD: /* obsolete */
1076 case TIOCSERSWILD: /* obsolete */
1081 if (ret != -ENOIOCTLCMD)
1084 if (tty->flags & (1 << TTY_IO_ERROR)) {
1090 * The following should only be used when hardware is present.
1094 ret = uart_wait_modem_status(state, arg);
1098 ret = uart_get_count(state, uarg);
1102 if (ret != -ENOIOCTLCMD)
1105 mutex_lock(&state->mutex);
1107 if (tty_hung_up_p(filp)) {
1113 * All these rely on hardware being present and need to be
1114 * protected against the tty being hung up.
1117 case TIOCSERGETLSR: /* Get line status register */
1118 ret = uart_get_lsr_info(state, uarg);
1122 struct uart_port *port = state->port;
1123 if (port->ops->ioctl)
1124 ret = port->ops->ioctl(port, cmd, arg);
1129 mutex_unlock(&state->mutex);
1134 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1136 struct uart_state *state = tty->driver_data;
1137 unsigned long flags;
1138 unsigned int cflag = tty->termios->c_cflag;
1140 BUG_ON(!kernel_locked());
1143 * These are the bits that are used to setup various
1144 * flags in the low level driver.
1146 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1148 if ((cflag ^ old_termios->c_cflag) == 0 &&
1149 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1152 uart_change_speed(state, old_termios);
1154 /* Handle transition to B0 status */
1155 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1156 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1158 /* Handle transition away from B0 status */
1159 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1160 unsigned int mask = TIOCM_DTR;
1161 if (!(cflag & CRTSCTS) ||
1162 !test_bit(TTY_THROTTLED, &tty->flags))
1164 uart_set_mctrl(state->port, mask);
1167 /* Handle turning off CRTSCTS */
1168 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1169 spin_lock_irqsave(&state->port->lock, flags);
1170 tty->hw_stopped = 0;
1172 spin_unlock_irqrestore(&state->port->lock, flags);
1175 /* Handle turning on CRTSCTS */
1176 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1177 spin_lock_irqsave(&state->port->lock, flags);
1178 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1179 tty->hw_stopped = 1;
1180 state->port->ops->stop_tx(state->port);
1182 spin_unlock_irqrestore(&state->port->lock, flags);
1187 * No need to wake up processes in open wait, since they
1188 * sample the CLOCAL flag once, and don't recheck it.
1189 * XXX It's not clear whether the current behavior is correct
1190 * or not. Hence, this may change.....
1192 if (!(old_termios->c_cflag & CLOCAL) &&
1193 (tty->termios->c_cflag & CLOCAL))
1194 wake_up_interruptible(&state->info->open_wait);
1199 * In 2.4.5, calls to this will be serialized via the BKL in
1200 * linux/drivers/char/tty_io.c:tty_release()
1201 * linux/drivers/char/tty_io.c:do_tty_handup()
1203 static void uart_close(struct tty_struct *tty, struct file *filp)
1205 struct uart_state *state = tty->driver_data;
1206 struct uart_port *port;
1208 BUG_ON(!kernel_locked());
1210 if (!state || !state->port)
1215 DPRINTK("uart_close(%d) called\n", port->line);
1217 mutex_lock(&state->mutex);
1219 if (tty_hung_up_p(filp))
1222 if ((tty->count == 1) && (state->count != 1)) {
1224 * Uh, oh. tty->count is 1, which means that the tty
1225 * structure will be freed. state->count should always
1226 * be one in these conditions. If it's greater than
1227 * one, we've got real problems, since it means the
1228 * serial port won't be shutdown.
1230 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1231 "state->count is %d\n", state->count);
1234 if (--state->count < 0) {
1235 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1236 tty->name, state->count);
1243 * Now we wait for the transmit buffer to clear; and we notify
1244 * the line discipline to only process XON/XOFF characters by
1245 * setting tty->closing.
1249 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1250 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1253 * At this point, we stop accepting input. To do this, we
1254 * disable the receive line status interrupts.
1256 if (state->info->flags & UIF_INITIALIZED) {
1257 unsigned long flags;
1258 spin_lock_irqsave(&port->lock, flags);
1259 port->ops->stop_rx(port);
1260 spin_unlock_irqrestore(&port->lock, flags);
1262 * Before we drop DTR, make sure the UART transmitter
1263 * has completely drained; this is especially
1264 * important if there is a transmit FIFO!
1266 uart_wait_until_sent(tty, port->timeout);
1269 uart_shutdown(state);
1270 uart_flush_buffer(tty);
1272 tty_ldisc_flush(tty);
1275 state->info->tty = NULL;
1277 if (state->info->blocked_open) {
1278 if (state->close_delay)
1279 msleep_interruptible(state->close_delay);
1280 } else if (!uart_console(port)) {
1281 uart_change_pm(state, 3);
1285 * Wake up anyone trying to open this port.
1287 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1288 wake_up_interruptible(&state->info->open_wait);
1291 mutex_unlock(&state->mutex);
1294 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1296 struct uart_state *state = tty->driver_data;
1297 struct uart_port *port = state->port;
1298 unsigned long char_time, expire;
1300 BUG_ON(!kernel_locked());
1302 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1306 * Set the check interval to be 1/5 of the estimated time to
1307 * send a single character, and make it at least 1. The check
1308 * interval should also be less than the timeout.
1310 * Note: we have to use pretty tight timings here to satisfy
1313 char_time = (port->timeout - HZ/50) / port->fifosize;
1314 char_time = char_time / 5;
1317 if (timeout && timeout < char_time)
1318 char_time = timeout;
1321 * If the transmitter hasn't cleared in twice the approximate
1322 * amount of time to send the entire FIFO, it probably won't
1323 * ever clear. This assumes the UART isn't doing flow
1324 * control, which is currently the case. Hence, if it ever
1325 * takes longer than port->timeout, this is probably due to a
1326 * UART bug of some kind. So, we clamp the timeout parameter at
1329 if (timeout == 0 || timeout > 2 * port->timeout)
1330 timeout = 2 * port->timeout;
1332 expire = jiffies + timeout;
1334 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1335 port->line, jiffies, expire);
1338 * Check whether the transmitter is empty every 'char_time'.
1339 * 'timeout' / 'expire' give us the maximum amount of time
1342 while (!port->ops->tx_empty(port)) {
1343 msleep_interruptible(jiffies_to_msecs(char_time));
1344 if (signal_pending(current))
1346 if (time_after(jiffies, expire))
1349 set_current_state(TASK_RUNNING); /* might not be needed */
1353 * This is called with the BKL held in
1354 * linux/drivers/char/tty_io.c:do_tty_hangup()
1355 * We're called from the eventd thread, so we can sleep for
1356 * a _short_ time only.
1358 static void uart_hangup(struct tty_struct *tty)
1360 struct uart_state *state = tty->driver_data;
1362 BUG_ON(!kernel_locked());
1363 DPRINTK("uart_hangup(%d)\n", state->port->line);
1365 mutex_lock(&state->mutex);
1366 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1367 uart_flush_buffer(tty);
1368 uart_shutdown(state);
1370 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1371 state->info->tty = NULL;
1372 wake_up_interruptible(&state->info->open_wait);
1373 wake_up_interruptible(&state->info->delta_msr_wait);
1375 mutex_unlock(&state->mutex);
1379 * Copy across the serial console cflag setting into the termios settings
1380 * for the initial open of the port. This allows continuity between the
1381 * kernel settings, and the settings init adopts when it opens the port
1382 * for the first time.
1384 static void uart_update_termios(struct uart_state *state)
1386 struct tty_struct *tty = state->info->tty;
1387 struct uart_port *port = state->port;
1389 if (uart_console(port) && port->cons->cflag) {
1390 tty->termios->c_cflag = port->cons->cflag;
1391 port->cons->cflag = 0;
1395 * If the device failed to grab its irq resources,
1396 * or some other error occurred, don't try to talk
1397 * to the port hardware.
1399 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1401 * Make termios settings take effect.
1403 uart_change_speed(state, NULL);
1406 * And finally enable the RTS and DTR signals.
1408 if (tty->termios->c_cflag & CBAUD)
1409 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1414 * Block the open until the port is ready. We must be called with
1415 * the per-port semaphore held.
1418 uart_block_til_ready(struct file *filp, struct uart_state *state)
1420 DECLARE_WAITQUEUE(wait, current);
1421 struct uart_info *info = state->info;
1422 struct uart_port *port = state->port;
1425 info->blocked_open++;
1428 add_wait_queue(&info->open_wait, &wait);
1430 set_current_state(TASK_INTERRUPTIBLE);
1433 * If we have been hung up, tell userspace/restart open.
1435 if (tty_hung_up_p(filp) || info->tty == NULL)
1439 * If the port has been closed, tell userspace/restart open.
1441 if (!(info->flags & UIF_INITIALIZED))
1445 * If non-blocking mode is set, or CLOCAL mode is set,
1446 * we don't want to wait for the modem status lines to
1447 * indicate that the port is ready.
1449 * Also, if the port is not enabled/configured, we want
1450 * to allow the open to succeed here. Note that we will
1451 * have set TTY_IO_ERROR for a non-existant port.
1453 if ((filp->f_flags & O_NONBLOCK) ||
1454 (info->tty->termios->c_cflag & CLOCAL) ||
1455 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1460 * Set DTR to allow modem to know we're waiting. Do
1461 * not set RTS here - we want to make sure we catch
1462 * the data from the modem.
1464 if (info->tty->termios->c_cflag & CBAUD)
1465 uart_set_mctrl(port, TIOCM_DTR);
1468 * and wait for the carrier to indicate that the
1469 * modem is ready for us.
1471 spin_lock_irq(&port->lock);
1472 port->ops->enable_ms(port);
1473 mctrl = port->ops->get_mctrl(port);
1474 spin_unlock_irq(&port->lock);
1475 if (mctrl & TIOCM_CAR)
1478 mutex_unlock(&state->mutex);
1480 mutex_lock(&state->mutex);
1482 if (signal_pending(current))
1485 set_current_state(TASK_RUNNING);
1486 remove_wait_queue(&info->open_wait, &wait);
1489 info->blocked_open--;
1491 if (signal_pending(current))
1492 return -ERESTARTSYS;
1494 if (!info->tty || tty_hung_up_p(filp))
1500 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1502 struct uart_state *state;
1505 state = drv->state + line;
1506 if (mutex_lock_interruptible(&state->mutex)) {
1512 if (!state->port || state->port->flags & UPF_DEAD) {
1518 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1520 memset(state->info, 0, sizeof(struct uart_info));
1521 init_waitqueue_head(&state->info->open_wait);
1522 init_waitqueue_head(&state->info->delta_msr_wait);
1525 * Link the info into the other structures.
1527 state->port->info = state->info;
1529 tasklet_init(&state->info->tlet, uart_tasklet_action,
1530 (unsigned long)state);
1540 mutex_unlock(&state->mutex);
1542 return ERR_PTR(ret);
1546 * In 2.4.5, calls to uart_open are serialised by the BKL in
1547 * linux/fs/devices.c:chrdev_open()
1548 * Note that if this fails, then uart_close() _will_ be called.
1550 * In time, we want to scrap the "opening nonpresent ports"
1551 * behaviour and implement an alternative way for setserial
1552 * to set base addresses/ports/types. This will allow us to
1553 * get rid of a certain amount of extra tests.
1555 static int uart_open(struct tty_struct *tty, struct file *filp)
1557 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1558 struct uart_state *state;
1559 int retval, line = tty->index;
1561 BUG_ON(!kernel_locked());
1562 DPRINTK("uart_open(%d) called\n", line);
1565 * tty->driver->num won't change, so we won't fail here with
1566 * tty->driver_data set to something non-NULL (and therefore
1567 * we won't get caught by uart_close()).
1570 if (line >= tty->driver->num)
1574 * We take the semaphore inside uart_get to guarantee that we won't
1575 * be re-entered while allocating the info structure, or while we
1576 * request any IRQs that the driver may need. This also has the nice
1577 * side-effect that it delays the action of uart_hangup, so we can
1578 * guarantee that info->tty will always contain something reasonable.
1580 state = uart_get(drv, line);
1581 if (IS_ERR(state)) {
1582 retval = PTR_ERR(state);
1587 * Once we set tty->driver_data here, we are guaranteed that
1588 * uart_close() will decrement the driver module use count.
1589 * Any failures from here onwards should not touch the count.
1591 tty->driver_data = state;
1592 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1594 state->info->tty = tty;
1597 * If the port is in the middle of closing, bail out now.
1599 if (tty_hung_up_p(filp)) {
1602 mutex_unlock(&state->mutex);
1607 * Make sure the device is in D0 state.
1609 if (state->count == 1)
1610 uart_change_pm(state, 0);
1613 * Start up the serial port.
1615 retval = uart_startup(state, 0);
1618 * If we succeeded, wait until the port is ready.
1621 retval = uart_block_til_ready(filp, state);
1622 mutex_unlock(&state->mutex);
1625 * If this is the first open to succeed, adjust things to suit.
1627 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1628 state->info->flags |= UIF_NORMAL_ACTIVE;
1630 uart_update_termios(state);
1637 static const char *uart_type(struct uart_port *port)
1639 const char *str = NULL;
1641 if (port->ops->type)
1642 str = port->ops->type(port);
1650 #ifdef CONFIG_PROC_FS
1652 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1654 struct uart_state *state = drv->state + i;
1655 struct uart_port *port = state->port;
1657 unsigned int status;
1663 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1664 port->line, uart_type(port),
1665 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1666 port->iotype == UPIO_MEM ? port->mapbase :
1667 (unsigned long) port->iobase,
1670 if (port->type == PORT_UNKNOWN) {
1675 if(capable(CAP_SYS_ADMIN))
1677 spin_lock_irq(&port->lock);
1678 status = port->ops->get_mctrl(port);
1679 spin_unlock_irq(&port->lock);
1681 ret += sprintf(buf + ret, " tx:%d rx:%d",
1682 port->icount.tx, port->icount.rx);
1683 if (port->icount.frame)
1684 ret += sprintf(buf + ret, " fe:%d",
1685 port->icount.frame);
1686 if (port->icount.parity)
1687 ret += sprintf(buf + ret, " pe:%d",
1688 port->icount.parity);
1689 if (port->icount.brk)
1690 ret += sprintf(buf + ret, " brk:%d",
1692 if (port->icount.overrun)
1693 ret += sprintf(buf + ret, " oe:%d",
1694 port->icount.overrun);
1696 #define INFOBIT(bit,str) \
1697 if (port->mctrl & (bit)) \
1698 strncat(stat_buf, (str), sizeof(stat_buf) - \
1699 strlen(stat_buf) - 2)
1700 #define STATBIT(bit,str) \
1701 if (status & (bit)) \
1702 strncat(stat_buf, (str), sizeof(stat_buf) - \
1703 strlen(stat_buf) - 2)
1707 INFOBIT(TIOCM_RTS, "|RTS");
1708 STATBIT(TIOCM_CTS, "|CTS");
1709 INFOBIT(TIOCM_DTR, "|DTR");
1710 STATBIT(TIOCM_DSR, "|DSR");
1711 STATBIT(TIOCM_CAR, "|CD");
1712 STATBIT(TIOCM_RNG, "|RI");
1715 strcat(stat_buf, "\n");
1717 ret += sprintf(buf + ret, stat_buf);
1727 static int uart_read_proc(char *page, char **start, off_t off,
1728 int count, int *eof, void *data)
1730 struct tty_driver *ttydrv = data;
1731 struct uart_driver *drv = ttydrv->driver_state;
1735 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1737 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1738 l = uart_line_info(page + len, drv, i);
1740 if (len + begin > off + count)
1742 if (len + begin < off) {
1749 if (off >= len + begin)
1751 *start = page + (off - begin);
1752 return (count < begin + len - off) ? count : (begin + len - off);
1756 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1758 * uart_console_write - write a console message to a serial port
1759 * @port: the port to write the message
1760 * @s: array of characters
1761 * @count: number of characters in string to write
1762 * @write: function to write character to port
1764 void uart_console_write(struct uart_port *port, const char *s,
1766 void (*putchar)(struct uart_port *, int))
1770 for (i = 0; i < count; i++, s++) {
1772 putchar(port, '\r');
1776 EXPORT_SYMBOL_GPL(uart_console_write);
1779 * Check whether an invalid uart number has been specified, and
1780 * if so, search for the first available port that does have
1783 struct uart_port * __init
1784 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1786 int idx = co->index;
1788 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1789 ports[idx].membase == NULL))
1790 for (idx = 0; idx < nr; idx++)
1791 if (ports[idx].iobase != 0 ||
1792 ports[idx].membase != NULL)
1801 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1802 * @options: pointer to option string
1803 * @baud: pointer to an 'int' variable for the baud rate.
1804 * @parity: pointer to an 'int' variable for the parity.
1805 * @bits: pointer to an 'int' variable for the number of data bits.
1806 * @flow: pointer to an 'int' variable for the flow control character.
1808 * uart_parse_options decodes a string containing the serial console
1809 * options. The format of the string is <baud><parity><bits><flow>,
1813 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1817 *baud = simple_strtoul(s, NULL, 10);
1818 while (*s >= '0' && *s <= '9')
1833 static const struct baud_rates baud_rates[] = {
1834 { 921600, B921600 },
1835 { 460800, B460800 },
1836 { 230400, B230400 },
1837 { 115200, B115200 },
1849 * uart_set_options - setup the serial console parameters
1850 * @port: pointer to the serial ports uart_port structure
1851 * @co: console pointer
1853 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1854 * @bits: number of data bits
1855 * @flow: flow control character - 'r' (rts)
1858 uart_set_options(struct uart_port *port, struct console *co,
1859 int baud, int parity, int bits, int flow)
1861 struct termios termios;
1865 * Ensure that the serial console lock is initialised
1868 spin_lock_init(&port->lock);
1870 memset(&termios, 0, sizeof(struct termios));
1872 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1875 * Construct a cflag setting.
1877 for (i = 0; baud_rates[i].rate; i++)
1878 if (baud_rates[i].rate <= baud)
1881 termios.c_cflag |= baud_rates[i].cflag;
1884 termios.c_cflag |= CS7;
1886 termios.c_cflag |= CS8;
1890 termios.c_cflag |= PARODD;
1893 termios.c_cflag |= PARENB;
1898 termios.c_cflag |= CRTSCTS;
1900 port->ops->set_termios(port, &termios, NULL);
1901 co->cflag = termios.c_cflag;
1905 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1907 static void uart_change_pm(struct uart_state *state, int pm_state)
1909 struct uart_port *port = state->port;
1911 port->ops->pm(port, pm_state, state->pm_state);
1912 state->pm_state = pm_state;
1915 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1917 struct uart_state *state = drv->state + port->line;
1919 mutex_lock(&state->mutex);
1921 if (state->info && state->info->flags & UIF_INITIALIZED) {
1922 const struct uart_ops *ops = port->ops;
1924 spin_lock_irq(&port->lock);
1926 ops->set_mctrl(port, 0);
1928 spin_unlock_irq(&port->lock);
1931 * Wait for the transmitter to empty.
1933 while (!ops->tx_empty(port)) {
1937 ops->shutdown(port);
1941 * Disable the console device before suspending.
1943 if (uart_console(port))
1944 console_stop(port->cons);
1946 uart_change_pm(state, 3);
1948 mutex_unlock(&state->mutex);
1953 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1955 struct uart_state *state = drv->state + port->line;
1957 mutex_lock(&state->mutex);
1959 uart_change_pm(state, 0);
1962 * Re-enable the console device after suspending.
1964 if (uart_console(port)) {
1965 struct termios termios;
1968 * First try to use the console cflag setting.
1970 memset(&termios, 0, sizeof(struct termios));
1971 termios.c_cflag = port->cons->cflag;
1974 * If that's unset, use the tty termios setting.
1976 if (state->info && state->info->tty && termios.c_cflag == 0)
1977 termios = *state->info->tty->termios;
1979 port->ops->set_termios(port, &termios, NULL);
1980 console_start(port->cons);
1983 if (state->info && state->info->flags & UIF_INITIALIZED) {
1984 const struct uart_ops *ops = port->ops;
1987 ops->set_mctrl(port, 0);
1988 ret = ops->startup(port);
1990 uart_change_speed(state, NULL);
1991 spin_lock_irq(&port->lock);
1992 ops->set_mctrl(port, port->mctrl);
1993 ops->start_tx(port);
1994 spin_unlock_irq(&port->lock);
1997 * Failed to resume - maybe hardware went away?
1998 * Clear the "initialized" flag so we won't try
1999 * to call the low level drivers shutdown method.
2001 state->info->flags &= ~UIF_INITIALIZED;
2002 uart_shutdown(state);
2006 mutex_unlock(&state->mutex);
2012 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2016 switch (port->iotype) {
2018 snprintf(address, sizeof(address),
2019 "I/O 0x%x", port->iobase);
2022 snprintf(address, sizeof(address),
2023 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
2028 snprintf(address, sizeof(address),
2029 "MMIO 0x%lx", port->mapbase);
2032 strlcpy(address, "*unknown*", sizeof(address));
2036 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2037 port->dev ? port->dev->bus_id : "",
2038 port->dev ? ": " : "",
2039 drv->dev_name, port->line, address, port->irq, uart_type(port));
2043 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2044 struct uart_port *port)
2049 * If there isn't a port here, don't do anything further.
2051 if (!port->iobase && !port->mapbase && !port->membase)
2055 * Now do the auto configuration stuff. Note that config_port
2056 * is expected to claim the resources and map the port for us.
2058 flags = UART_CONFIG_TYPE;
2059 if (port->flags & UPF_AUTO_IRQ)
2060 flags |= UART_CONFIG_IRQ;
2061 if (port->flags & UPF_BOOT_AUTOCONF) {
2062 port->type = PORT_UNKNOWN;
2063 port->ops->config_port(port, flags);
2066 if (port->type != PORT_UNKNOWN) {
2067 unsigned long flags;
2069 uart_report_port(drv, port);
2072 * Ensure that the modem control lines are de-activated.
2073 * We probably don't need a spinlock around this, but
2075 spin_lock_irqsave(&port->lock, flags);
2076 port->ops->set_mctrl(port, 0);
2077 spin_unlock_irqrestore(&port->lock, flags);
2080 * Power down all ports by default, except the
2081 * console if we have one.
2083 if (!uart_console(port))
2084 uart_change_pm(state, 3);
2088 static struct tty_operations uart_ops = {
2090 .close = uart_close,
2091 .write = uart_write,
2092 .put_char = uart_put_char,
2093 .flush_chars = uart_flush_chars,
2094 .write_room = uart_write_room,
2095 .chars_in_buffer= uart_chars_in_buffer,
2096 .flush_buffer = uart_flush_buffer,
2097 .ioctl = uart_ioctl,
2098 .throttle = uart_throttle,
2099 .unthrottle = uart_unthrottle,
2100 .send_xchar = uart_send_xchar,
2101 .set_termios = uart_set_termios,
2103 .start = uart_start,
2104 .hangup = uart_hangup,
2105 .break_ctl = uart_break_ctl,
2106 .wait_until_sent= uart_wait_until_sent,
2107 #ifdef CONFIG_PROC_FS
2108 .read_proc = uart_read_proc,
2110 .tiocmget = uart_tiocmget,
2111 .tiocmset = uart_tiocmset,
2115 * uart_register_driver - register a driver with the uart core layer
2116 * @drv: low level driver structure
2118 * Register a uart driver with the core driver. We in turn register
2119 * with the tty layer, and initialise the core driver per-port state.
2121 * We have a proc file in /proc/tty/driver which is named after the
2124 * drv->port should be NULL, and the per-port structures should be
2125 * registered using uart_add_one_port after this call has succeeded.
2127 int uart_register_driver(struct uart_driver *drv)
2129 struct tty_driver *normal = NULL;
2135 * Maybe we should be using a slab cache for this, especially if
2136 * we have a large number of ports to handle.
2138 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2143 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2145 normal = alloc_tty_driver(drv->nr);
2149 drv->tty_driver = normal;
2151 normal->owner = drv->owner;
2152 normal->driver_name = drv->driver_name;
2153 normal->devfs_name = drv->devfs_name;
2154 normal->name = drv->dev_name;
2155 normal->major = drv->major;
2156 normal->minor_start = drv->minor;
2157 normal->type = TTY_DRIVER_TYPE_SERIAL;
2158 normal->subtype = SERIAL_TYPE_NORMAL;
2159 normal->init_termios = tty_std_termios;
2160 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2161 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2162 normal->driver_state = drv;
2163 tty_set_operations(normal, &uart_ops);
2166 * Initialise the UART state(s).
2168 for (i = 0; i < drv->nr; i++) {
2169 struct uart_state *state = drv->state + i;
2171 state->close_delay = 500; /* .5 seconds */
2172 state->closing_wait = 30000; /* 30 seconds */
2174 mutex_init(&state->mutex);
2177 retval = tty_register_driver(normal);
2180 put_tty_driver(normal);
2187 * uart_unregister_driver - remove a driver from the uart core layer
2188 * @drv: low level driver structure
2190 * Remove all references to a driver from the core driver. The low
2191 * level driver must have removed all its ports via the
2192 * uart_remove_one_port() if it registered them with uart_add_one_port().
2193 * (ie, drv->port == NULL)
2195 void uart_unregister_driver(struct uart_driver *drv)
2197 struct tty_driver *p = drv->tty_driver;
2198 tty_unregister_driver(p);
2201 drv->tty_driver = NULL;
2204 struct tty_driver *uart_console_device(struct console *co, int *index)
2206 struct uart_driver *p = co->data;
2208 return p->tty_driver;
2212 * uart_add_one_port - attach a driver-defined port structure
2213 * @drv: pointer to the uart low level driver structure for this port
2214 * @port: uart port structure to use for this port.
2216 * This allows the driver to register its own uart_port structure
2217 * with the core driver. The main purpose is to allow the low
2218 * level uart drivers to expand uart_port, rather than having yet
2219 * more levels of structures.
2221 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2223 struct uart_state *state;
2226 BUG_ON(in_interrupt());
2228 if (port->line >= drv->nr)
2231 state = drv->state + port->line;
2233 mutex_lock(&port_mutex);
2234 mutex_lock(&state->mutex);
2242 port->cons = drv->cons;
2243 port->info = state->info;
2246 * If this port is a console, then the spinlock is already
2249 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED)))
2250 spin_lock_init(&port->lock);
2252 uart_configure_port(drv, state, port);
2255 * Register the port whether it's detected or not. This allows
2256 * setserial to be used to alter this ports parameters.
2258 tty_register_device(drv->tty_driver, port->line, port->dev);
2261 * If this driver supports console, and it hasn't been
2262 * successfully registered yet, try to re-register it.
2263 * It may be that the port was not available.
2265 if (port->type != PORT_UNKNOWN &&
2266 port->cons && !(port->cons->flags & CON_ENABLED))
2267 register_console(port->cons);
2270 * Ensure UPF_DEAD is not set.
2272 port->flags &= ~UPF_DEAD;
2275 mutex_unlock(&state->mutex);
2276 mutex_unlock(&port_mutex);
2282 * uart_remove_one_port - detach a driver defined port structure
2283 * @drv: pointer to the uart low level driver structure for this port
2284 * @port: uart port structure for this port
2286 * This unhooks (and hangs up) the specified port structure from the
2287 * core driver. No further calls will be made to the low-level code
2290 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2292 struct uart_state *state = drv->state + port->line;
2293 struct uart_info *info;
2295 BUG_ON(in_interrupt());
2297 if (state->port != port)
2298 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2301 mutex_lock(&port_mutex);
2304 * Mark the port "dead" - this prevents any opens from
2305 * succeeding while we shut down the port.
2307 mutex_lock(&state->mutex);
2308 port->flags |= UPF_DEAD;
2309 mutex_unlock(&state->mutex);
2312 * Remove the devices from devfs
2314 tty_unregister_device(drv->tty_driver, port->line);
2317 if (info && info->tty)
2318 tty_vhangup(info->tty);
2321 * All users of this port should now be disconnected from
2322 * this driver, and the port shut down. We should be the
2323 * only thread fiddling with this port from now on.
2328 * Free the port IO and memory resources, if any.
2330 if (port->type != PORT_UNKNOWN)
2331 port->ops->release_port(port);
2334 * Indicate that there isn't a port here anymore.
2336 port->type = PORT_UNKNOWN;
2339 * Kill the tasklet, and free resources.
2342 tasklet_kill(&info->tlet);
2347 mutex_unlock(&port_mutex);
2353 * Are the two ports equivalent?
2355 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2357 if (port1->iotype != port2->iotype)
2360 switch (port1->iotype) {
2362 return (port1->iobase == port2->iobase);
2364 return (port1->iobase == port2->iobase) &&
2365 (port1->hub6 == port2->hub6);
2367 return (port1->mapbase == port2->mapbase);
2371 EXPORT_SYMBOL(uart_match_port);
2373 EXPORT_SYMBOL(uart_write_wakeup);
2374 EXPORT_SYMBOL(uart_register_driver);
2375 EXPORT_SYMBOL(uart_unregister_driver);
2376 EXPORT_SYMBOL(uart_suspend_port);
2377 EXPORT_SYMBOL(uart_resume_port);
2378 EXPORT_SYMBOL(uart_add_one_port);
2379 EXPORT_SYMBOL(uart_remove_one_port);
2381 MODULE_DESCRIPTION("Serial driver core");
2382 MODULE_LICENSE("GPL");