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 */
37 #include <asm/uaccess.h>
41 #define DPRINTK(x...) printk(x)
43 #define DPRINTK(x...) do { } while (0)
47 * This is used to lock changes in serial line configuration.
49 static DECLARE_MUTEX(port_sem);
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 termios *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;
72 tasklet_schedule(&info->tlet);
75 static void uart_stop(struct tty_struct *tty)
77 struct uart_state *state = tty->driver_data;
78 struct uart_port *port = state->port;
81 spin_lock_irqsave(&port->lock, flags);
82 port->ops->stop_tx(port, 1);
83 spin_unlock_irqrestore(&port->lock, flags);
86 static void __uart_start(struct tty_struct *tty)
88 struct uart_state *state = tty->driver_data;
89 struct uart_port *port = state->port;
91 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
92 !tty->stopped && !tty->hw_stopped)
93 port->ops->start_tx(port, 1);
96 static void uart_start(struct tty_struct *tty)
98 struct uart_state *state = tty->driver_data;
99 struct uart_port *port = state->port;
102 spin_lock_irqsave(&port->lock, flags);
104 spin_unlock_irqrestore(&port->lock, flags);
107 static void uart_tasklet_action(unsigned long data)
109 struct uart_state *state = (struct uart_state *)data;
110 struct tty_struct *tty;
112 tty = state->info->tty;
114 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
115 tty->ldisc.write_wakeup)
116 tty->ldisc.write_wakeup(tty);
117 wake_up_interruptible(&tty->write_wait);
122 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
127 spin_lock_irqsave(&port->lock, flags);
129 port->mctrl = (old & ~clear) | set;
130 if (old != port->mctrl)
131 port->ops->set_mctrl(port, port->mctrl);
132 spin_unlock_irqrestore(&port->lock, flags);
135 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
136 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
139 * Startup the port. This will be called once per open. All calls
140 * will be serialised by the per-port semaphore.
142 static int uart_startup(struct uart_state *state, int init_hw)
144 struct uart_info *info = state->info;
145 struct uart_port *port = state->port;
149 if (info->flags & UIF_INITIALIZED)
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
158 set_bit(TTY_IO_ERROR, &info->tty->flags);
160 if (port->type == PORT_UNKNOWN)
164 * Initialise and allocate the transmit and temporary
167 if (!info->xmit.buf) {
168 page = get_zeroed_page(GFP_KERNEL);
172 info->xmit.buf = (unsigned char *) page;
173 info->tmpbuf = info->xmit.buf + UART_XMIT_SIZE;
174 init_MUTEX(&info->tmpbuf_sem);
175 uart_circ_clear(&info->xmit);
178 retval = port->ops->startup(port);
182 * Initialise the hardware port settings.
184 uart_change_speed(state, NULL);
187 * Setup the RTS and DTR signals once the
188 * port is open and ready to respond.
190 if (info->tty->termios->c_cflag & CBAUD)
191 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
194 info->flags |= UIF_INITIALIZED;
196 clear_bit(TTY_IO_ERROR, &info->tty->flags);
199 if (retval && capable(CAP_SYS_ADMIN))
206 * This routine will shutdown a serial port; interrupts are disabled, and
207 * DTR is dropped if the hangup on close termio flag is on. Calls to
208 * uart_shutdown are serialised by the per-port semaphore.
210 static void uart_shutdown(struct uart_state *state)
212 struct uart_info *info = state->info;
213 struct uart_port *port = state->port;
215 if (!(info->flags & UIF_INITIALIZED))
219 * Turn off DTR and RTS early.
221 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
222 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
225 * clear delta_msr_wait queue to avoid mem leaks: we may free
226 * the irq here so the queue might never be woken up. Note
227 * that we won't end up waiting on delta_msr_wait again since
228 * any outstanding file descriptors should be pointing at
229 * hung_up_tty_fops now.
231 wake_up_interruptible(&info->delta_msr_wait);
234 * Free the IRQ and disable the port.
236 port->ops->shutdown(port);
239 * Ensure that the IRQ handler isn't running on another CPU.
241 synchronize_irq(port->irq);
244 * Free the transmit buffer page.
246 if (info->xmit.buf) {
247 free_page((unsigned long)info->xmit.buf);
248 info->xmit.buf = NULL;
253 * kill off our tasklet
255 tasklet_kill(&info->tlet);
257 set_bit(TTY_IO_ERROR, &info->tty->flags);
259 info->flags &= ~UIF_INITIALIZED;
263 * uart_update_timeout - update per-port FIFO timeout.
264 * @port: uart_port structure describing the port
265 * @cflag: termios cflag value
266 * @baud: speed of the port
268 * Set the port FIFO timeout value. The @cflag value should
269 * reflect the actual hardware settings.
272 uart_update_timeout(struct uart_port *port, unsigned int cflag,
277 /* byte size and parity */
278 switch (cflag & CSIZE) {
299 * The total number of bits to be transmitted in the fifo.
301 bits = bits * port->fifosize;
304 * Figure the timeout to send the above number of bits.
305 * Add .02 seconds of slop
307 port->timeout = (HZ * bits) / baud + HZ/50;
310 EXPORT_SYMBOL(uart_update_timeout);
313 * uart_get_baud_rate - return baud rate for a particular port
314 * @port: uart_port structure describing the port in question.
315 * @termios: desired termios settings.
316 * @old: old termios (or NULL)
317 * @min: minimum acceptable baud rate
318 * @max: maximum acceptable baud rate
320 * Decode the termios structure into a numeric baud rate,
321 * taking account of the magic 38400 baud rate (with spd_*
322 * flags), and mapping the %B0 rate to 9600 baud.
324 * If the new baud rate is invalid, try the old termios setting.
325 * If it's still invalid, we try 9600 baud.
327 * Update the @termios structure to reflect the baud rate
328 * we're actually going to be using.
331 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
332 struct termios *old, unsigned int min, unsigned int max)
334 unsigned int try, baud, altbaud = 38400;
335 unsigned int flags = port->flags & UPF_SPD_MASK;
337 if (flags == UPF_SPD_HI)
339 if (flags == UPF_SPD_VHI)
341 if (flags == UPF_SPD_SHI)
343 if (flags == UPF_SPD_WARP)
346 for (try = 0; try < 2; try++) {
347 baud = tty_termios_baud_rate(termios);
350 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Special case: B0 rate.
362 if (baud >= min && baud <= max)
366 * Oops, the quotient was zero. Try again with
367 * the old baud rate if possible.
369 termios->c_cflag &= ~CBAUD;
371 termios->c_cflag |= old->c_cflag & CBAUD;
377 * As a last resort, if the quotient is zero,
378 * default to 9600 bps
380 termios->c_cflag |= B9600;
386 EXPORT_SYMBOL(uart_get_baud_rate);
389 * uart_get_divisor - return uart clock divisor
390 * @port: uart_port structure describing the port.
391 * @baud: desired baud rate
393 * Calculate the uart clock divisor for the port.
396 uart_get_divisor(struct uart_port *port, unsigned int baud)
401 * Old custom speed handling.
403 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
404 quot = port->custom_divisor;
406 quot = port->uartclk / (16 * baud);
411 EXPORT_SYMBOL(uart_get_divisor);
414 uart_change_speed(struct uart_state *state, struct termios *old_termios)
416 struct tty_struct *tty = state->info->tty;
417 struct uart_port *port = state->port;
418 struct termios *termios;
421 * If we have no tty, termios, or the port does not exist,
422 * then we can't set the parameters for this port.
424 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
427 termios = tty->termios;
430 * Set flags based on termios cflag
432 if (termios->c_cflag & CRTSCTS)
433 state->info->flags |= UIF_CTS_FLOW;
435 state->info->flags &= ~UIF_CTS_FLOW;
437 if (termios->c_cflag & CLOCAL)
438 state->info->flags &= ~UIF_CHECK_CD;
440 state->info->flags |= UIF_CHECK_CD;
442 port->ops->set_termios(port, termios, old_termios);
446 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
453 spin_lock_irqsave(&port->lock, flags);
454 if (uart_circ_chars_free(circ) != 0) {
455 circ->buf[circ->head] = c;
456 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
458 spin_unlock_irqrestore(&port->lock, flags);
462 __uart_user_write(struct uart_port *port, struct circ_buf *circ,
463 const unsigned char __user *buf, int count)
468 if (down_interruptible(&port->info->tmpbuf_sem))
473 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
479 c -= copy_from_user(port->info->tmpbuf, buf, c);
485 spin_lock_irqsave(&port->lock, flags);
486 c1 = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
489 memcpy(circ->buf + circ->head, port->info->tmpbuf, c);
490 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
491 spin_unlock_irqrestore(&port->lock, flags);
496 up(&port->info->tmpbuf_sem);
502 __uart_kern_write(struct uart_port *port, struct circ_buf *circ,
503 const unsigned char *buf, int count)
508 spin_lock_irqsave(&port->lock, flags);
510 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
515 memcpy(circ->buf + circ->head, buf, c);
516 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
521 spin_unlock_irqrestore(&port->lock, flags);
526 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
528 struct uart_state *state = tty->driver_data;
530 __uart_put_char(state->port, &state->info->xmit, ch);
533 static void uart_flush_chars(struct tty_struct *tty)
539 uart_write(struct tty_struct *tty, int from_user, const unsigned char * buf,
542 struct uart_state *state = tty->driver_data;
545 if (!state->info->xmit.buf)
549 ret = __uart_user_write(state->port, &state->info->xmit,
550 (const unsigned char __user *)buf, count);
552 ret = __uart_kern_write(state->port, &state->info->xmit,
559 static int uart_write_room(struct tty_struct *tty)
561 struct uart_state *state = tty->driver_data;
563 return uart_circ_chars_free(&state->info->xmit);
566 static int uart_chars_in_buffer(struct tty_struct *tty)
568 struct uart_state *state = tty->driver_data;
570 return uart_circ_chars_pending(&state->info->xmit);
573 static void uart_flush_buffer(struct tty_struct *tty)
575 struct uart_state *state = tty->driver_data;
576 struct uart_port *port = state->port;
579 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
581 spin_lock_irqsave(&port->lock, flags);
582 uart_circ_clear(&state->info->xmit);
583 spin_unlock_irqrestore(&port->lock, flags);
584 wake_up_interruptible(&tty->write_wait);
585 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
586 tty->ldisc.write_wakeup)
587 (tty->ldisc.write_wakeup)(tty);
591 * This function is used to send a high-priority XON/XOFF character to
594 static void uart_send_xchar(struct tty_struct *tty, char ch)
596 struct uart_state *state = tty->driver_data;
597 struct uart_port *port = state->port;
600 if (port->ops->send_xchar)
601 port->ops->send_xchar(port, ch);
605 spin_lock_irqsave(&port->lock, flags);
606 port->ops->start_tx(port, 0);
607 spin_unlock_irqrestore(&port->lock, flags);
612 static void uart_throttle(struct tty_struct *tty)
614 struct uart_state *state = tty->driver_data;
617 uart_send_xchar(tty, STOP_CHAR(tty));
619 if (tty->termios->c_cflag & CRTSCTS)
620 uart_clear_mctrl(state->port, TIOCM_RTS);
623 static void uart_unthrottle(struct tty_struct *tty)
625 struct uart_state *state = tty->driver_data;
626 struct uart_port *port = state->port;
632 uart_send_xchar(tty, START_CHAR(tty));
635 if (tty->termios->c_cflag & CRTSCTS)
636 uart_set_mctrl(port, TIOCM_RTS);
639 static int uart_get_info(struct uart_state *state,
640 struct serial_struct __user *retinfo)
642 struct uart_port *port = state->port;
643 struct serial_struct tmp;
645 memset(&tmp, 0, sizeof(tmp));
646 tmp.type = port->type;
647 tmp.line = port->line;
648 tmp.port = port->iobase;
649 if (HIGH_BITS_OFFSET)
650 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
652 tmp.flags = port->flags;
653 tmp.xmit_fifo_size = port->fifosize;
654 tmp.baud_base = port->uartclk / 16;
655 tmp.close_delay = state->close_delay;
656 tmp.closing_wait = state->closing_wait;
657 tmp.custom_divisor = port->custom_divisor;
658 tmp.hub6 = port->hub6;
659 tmp.io_type = port->iotype;
660 tmp.iomem_reg_shift = port->regshift;
661 tmp.iomem_base = (void *)port->mapbase;
663 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
668 static int uart_set_info(struct uart_state *state,
669 struct serial_struct __user *newinfo)
671 struct serial_struct new_serial;
672 struct uart_port *port = state->port;
673 unsigned long new_port;
674 unsigned int change_irq, change_port, old_flags;
675 unsigned int old_custom_divisor;
678 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
681 new_port = new_serial.port;
682 if (HIGH_BITS_OFFSET)
683 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
685 new_serial.irq = irq_canonicalize(new_serial.irq);
688 * This semaphore protects state->count. It is also
689 * very useful to prevent opens. Also, take the
690 * port configuration semaphore to make sure that a
691 * module insertion/removal doesn't change anything
696 change_irq = new_serial.irq != port->irq;
699 * Since changing the 'type' of the port changes its resource
700 * allocations, we should treat type changes the same as
703 change_port = new_port != port->iobase ||
704 (unsigned long)new_serial.iomem_base != port->mapbase ||
705 new_serial.hub6 != port->hub6 ||
706 new_serial.io_type != port->iotype ||
707 new_serial.iomem_reg_shift != port->regshift ||
708 new_serial.type != port->type;
710 old_flags = port->flags;
711 old_custom_divisor = port->custom_divisor;
713 if (!capable(CAP_SYS_ADMIN)) {
715 if (change_irq || change_port ||
716 (new_serial.baud_base != port->uartclk / 16) ||
717 (new_serial.close_delay != state->close_delay) ||
718 (new_serial.closing_wait != state->closing_wait) ||
719 (new_serial.xmit_fifo_size != port->fifosize) ||
720 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
722 port->flags = ((port->flags & ~UPF_USR_MASK) |
723 (new_serial.flags & UPF_USR_MASK));
724 port->custom_divisor = new_serial.custom_divisor;
729 * Ask the low level driver to verify the settings.
731 if (port->ops->verify_port)
732 retval = port->ops->verify_port(port, &new_serial);
734 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
735 (new_serial.baud_base < 9600))
741 if (change_port || change_irq) {
745 * Make sure that we are the sole user of this port.
747 if (uart_users(state) > 1)
751 * We need to shutdown the serial port at the old
752 * port/type/irq combination.
754 uart_shutdown(state);
758 unsigned long old_iobase, old_mapbase;
759 unsigned int old_type, old_iotype, old_hub6, old_shift;
761 old_iobase = port->iobase;
762 old_mapbase = port->mapbase;
763 old_type = port->type;
764 old_hub6 = port->hub6;
765 old_iotype = port->iotype;
766 old_shift = port->regshift;
769 * Free and release old regions
771 if (old_type != PORT_UNKNOWN)
772 port->ops->release_port(port);
774 port->iobase = new_port;
775 port->type = new_serial.type;
776 port->hub6 = new_serial.hub6;
777 port->iotype = new_serial.io_type;
778 port->regshift = new_serial.iomem_reg_shift;
779 port->mapbase = (unsigned long)new_serial.iomem_base;
782 * Claim and map the new regions
784 if (port->type != PORT_UNKNOWN) {
785 retval = port->ops->request_port(port);
787 /* Always success - Jean II */
792 * If we fail to request resources for the
793 * new port, try to restore the old settings.
795 if (retval && old_type != PORT_UNKNOWN) {
796 port->iobase = old_iobase;
797 port->type = old_type;
798 port->hub6 = old_hub6;
799 port->iotype = old_iotype;
800 port->regshift = old_shift;
801 port->mapbase = old_mapbase;
802 retval = port->ops->request_port(port);
804 * If we failed to restore the old settings,
808 port->type = PORT_UNKNOWN;
817 port->irq = new_serial.irq;
818 port->uartclk = new_serial.baud_base * 16;
819 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
820 (new_serial.flags & UPF_CHANGE_MASK);
821 port->custom_divisor = new_serial.custom_divisor;
822 state->close_delay = new_serial.close_delay * HZ / 100;
823 state->closing_wait = new_serial.closing_wait * HZ / 100;
824 port->fifosize = new_serial.xmit_fifo_size;
825 if (state->info->tty)
826 state->info->tty->low_latency =
827 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
831 if (port->type == PORT_UNKNOWN)
833 if (state->info->flags & UIF_INITIALIZED) {
834 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
835 old_custom_divisor != port->custom_divisor) {
837 * If they're setting up a custom divisor or speed,
838 * instead of clearing it, then bitch about it. No
839 * need to rate-limit; it's CAP_SYS_ADMIN only.
841 if (port->flags & UPF_SPD_MASK) {
844 "%s sets custom speed on %s. This "
845 "is deprecated.\n", current->comm,
846 tty_name(state->info->tty, buf));
848 uart_change_speed(state, NULL);
851 retval = uart_startup(state, 1);
859 * uart_get_lsr_info - get line status register info.
860 * Note: uart_ioctl protects us against hangups.
862 static int uart_get_lsr_info(struct uart_state *state,
863 unsigned int __user *value)
865 struct uart_port *port = state->port;
868 result = port->ops->tx_empty(port);
871 * If we're about to load something into the transmit
872 * register, we'll pretend the transmitter isn't empty to
873 * avoid a race condition (depending on when the transmit
874 * interrupt happens).
877 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
878 !state->info->tty->stopped && !state->info->tty->hw_stopped))
879 result &= ~TIOCSER_TEMT;
881 return put_user(result, value);
884 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
886 struct uart_state *state = tty->driver_data;
887 struct uart_port *port = state->port;
891 if ((!file || !tty_hung_up_p(file)) &&
892 !(tty->flags & (1 << TTY_IO_ERROR))) {
893 result = port->mctrl;
894 result |= port->ops->get_mctrl(port);
902 uart_tiocmset(struct tty_struct *tty, struct file *file,
903 unsigned int set, unsigned int clear)
905 struct uart_state *state = tty->driver_data;
906 struct uart_port *port = state->port;
910 if ((!file || !tty_hung_up_p(file)) &&
911 !(tty->flags & (1 << TTY_IO_ERROR))) {
912 uart_update_mctrl(port, set, clear);
919 static void uart_break_ctl(struct tty_struct *tty, int break_state)
921 struct uart_state *state = tty->driver_data;
922 struct uart_port *port = state->port;
924 BUG_ON(!kernel_locked());
928 if (port->type != PORT_UNKNOWN)
929 port->ops->break_ctl(port, break_state);
934 static int uart_do_autoconfig(struct uart_state *state)
936 struct uart_port *port = state->port;
939 if (!capable(CAP_SYS_ADMIN))
943 * Take the per-port semaphore. This prevents count from
944 * changing, and hence any extra opens of the port while
945 * we're auto-configuring.
947 if (down_interruptible(&state->sem))
951 if (uart_users(state) == 1) {
952 uart_shutdown(state);
955 * If we already have a port type configured,
956 * we must release its resources.
958 if (port->type != PORT_UNKNOWN)
959 port->ops->release_port(port);
961 flags = UART_CONFIG_TYPE;
962 if (port->flags & UPF_AUTO_IRQ)
963 flags |= UART_CONFIG_IRQ;
966 * This will claim the ports resources if
969 port->ops->config_port(port, flags);
971 ret = uart_startup(state, 1);
978 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
979 * - mask passed in arg for lines of interest
980 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
981 * Caller should use TIOCGICOUNT to see which one it was
984 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
986 struct uart_port *port = state->port;
987 DECLARE_WAITQUEUE(wait, current);
988 struct uart_icount cprev, cnow;
992 * note the counters on entry
994 spin_lock_irq(&port->lock);
995 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
998 * Force modem status interrupts on
1000 port->ops->enable_ms(port);
1001 spin_unlock_irq(&port->lock);
1003 add_wait_queue(&state->info->delta_msr_wait, &wait);
1005 spin_lock_irq(&port->lock);
1006 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1007 spin_unlock_irq(&port->lock);
1009 set_current_state(TASK_INTERRUPTIBLE);
1011 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1012 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1013 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1014 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1021 /* see if a signal did it */
1022 if (signal_pending(current)) {
1030 current->state = TASK_RUNNING;
1031 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1037 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1038 * Return: write counters to the user passed counter struct
1039 * NB: both 1->0 and 0->1 transitions are counted except for
1040 * RI where only 0->1 is counted.
1042 static int uart_get_count(struct uart_state *state,
1043 struct serial_icounter_struct __user *icnt)
1045 struct serial_icounter_struct icount;
1046 struct uart_icount cnow;
1047 struct uart_port *port = state->port;
1049 spin_lock_irq(&port->lock);
1050 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1051 spin_unlock_irq(&port->lock);
1053 icount.cts = cnow.cts;
1054 icount.dsr = cnow.dsr;
1055 icount.rng = cnow.rng;
1056 icount.dcd = cnow.dcd;
1057 icount.rx = cnow.rx;
1058 icount.tx = cnow.tx;
1059 icount.frame = cnow.frame;
1060 icount.overrun = cnow.overrun;
1061 icount.parity = cnow.parity;
1062 icount.brk = cnow.brk;
1063 icount.buf_overrun = cnow.buf_overrun;
1065 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1069 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1072 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1075 struct uart_state *state = tty->driver_data;
1076 void __user *uarg = (void __user *)arg;
1077 int ret = -ENOIOCTLCMD;
1079 BUG_ON(!kernel_locked());
1082 * These ioctls don't rely on the hardware to be present.
1086 ret = uart_get_info(state, uarg);
1090 ret = uart_set_info(state, uarg);
1094 ret = uart_do_autoconfig(state);
1097 case TIOCSERGWILD: /* obsolete */
1098 case TIOCSERSWILD: /* obsolete */
1103 if (ret != -ENOIOCTLCMD)
1106 if (tty->flags & (1 << TTY_IO_ERROR)) {
1112 * The following should only be used when hardware is present.
1116 ret = uart_wait_modem_status(state, arg);
1120 ret = uart_get_count(state, uarg);
1124 if (ret != -ENOIOCTLCMD)
1129 if (tty_hung_up_p(filp)) {
1135 * All these rely on hardware being present and need to be
1136 * protected against the tty being hung up.
1139 case TIOCSERGETLSR: /* Get line status register */
1140 ret = uart_get_lsr_info(state, uarg);
1144 struct uart_port *port = state->port;
1145 if (port->ops->ioctl)
1146 ret = port->ops->ioctl(port, cmd, arg);
1156 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1158 struct uart_state *state = tty->driver_data;
1159 unsigned long flags;
1160 unsigned int cflag = tty->termios->c_cflag;
1162 BUG_ON(!kernel_locked());
1165 * These are the bits that are used to setup various
1166 * flags in the low level driver.
1168 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1170 if ((cflag ^ old_termios->c_cflag) == 0 &&
1171 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1174 uart_change_speed(state, old_termios);
1176 /* Handle transition to B0 status */
1177 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1178 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1180 /* Handle transition away from B0 status */
1181 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1182 unsigned int mask = TIOCM_DTR;
1183 if (!(cflag & CRTSCTS) ||
1184 !test_bit(TTY_THROTTLED, &tty->flags))
1186 uart_set_mctrl(state->port, mask);
1189 /* Handle turning off CRTSCTS */
1190 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1191 spin_lock_irqsave(&state->port->lock, flags);
1192 tty->hw_stopped = 0;
1194 spin_unlock_irqrestore(&state->port->lock, flags);
1199 * No need to wake up processes in open wait, since they
1200 * sample the CLOCAL flag once, and don't recheck it.
1201 * XXX It's not clear whether the current behavior is correct
1202 * or not. Hence, this may change.....
1204 if (!(old_termios->c_cflag & CLOCAL) &&
1205 (tty->termios->c_cflag & CLOCAL))
1206 wake_up_interruptible(&state->info->open_wait);
1211 * In 2.4.5, calls to this will be serialized via the BKL in
1212 * linux/drivers/char/tty_io.c:tty_release()
1213 * linux/drivers/char/tty_io.c:do_tty_handup()
1215 static void uart_close(struct tty_struct *tty, struct file *filp)
1217 struct uart_state *state = tty->driver_data;
1218 struct uart_port *port;
1220 BUG_ON(!kernel_locked());
1222 if (!state || !state->port)
1227 DPRINTK("uart_close(%d) called\n", port->line);
1231 if (tty_hung_up_p(filp))
1234 if ((tty->count == 1) && (state->count != 1)) {
1236 * Uh, oh. tty->count is 1, which means that the tty
1237 * structure will be freed. state->count should always
1238 * be one in these conditions. If it's greater than
1239 * one, we've got real problems, since it means the
1240 * serial port won't be shutdown.
1242 printk("uart_close: bad serial port count; tty->count is 1, "
1243 "state->count is %d\n", state->count);
1246 if (--state->count < 0) {
1247 printk("rs_close: bad serial port count for %s: %d\n",
1248 tty->name, state->count);
1255 * Now we wait for the transmit buffer to clear; and we notify
1256 * the line discipline to only process XON/XOFF characters by
1257 * setting tty->closing.
1261 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1262 tty_wait_until_sent(tty, state->closing_wait);
1265 * At this point, we stop accepting input. To do this, we
1266 * disable the receive line status interrupts.
1268 if (state->info->flags & UIF_INITIALIZED) {
1269 unsigned long flags;
1270 spin_lock_irqsave(&port->lock, flags);
1271 port->ops->stop_rx(port);
1272 spin_unlock_irqrestore(&port->lock, flags);
1274 * Before we drop DTR, make sure the UART transmitter
1275 * has completely drained; this is especially
1276 * important if there is a transmit FIFO!
1278 uart_wait_until_sent(tty, port->timeout);
1281 uart_shutdown(state);
1282 uart_flush_buffer(tty);
1283 if (tty->ldisc.flush_buffer)
1284 tty->ldisc.flush_buffer(tty);
1286 state->info->tty = NULL;
1288 if (state->info->blocked_open) {
1289 if (state->close_delay) {
1290 set_current_state(TASK_INTERRUPTIBLE);
1291 schedule_timeout(state->close_delay);
1293 } else if (!uart_console(port)) {
1294 uart_change_pm(state, 3);
1298 * Wake up anyone trying to open this port.
1300 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1301 wake_up_interruptible(&state->info->open_wait);
1307 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1309 struct uart_state *state = tty->driver_data;
1310 struct uart_port *port = state->port;
1311 unsigned long char_time, expire;
1313 BUG_ON(!kernel_locked());
1315 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1319 * Set the check interval to be 1/5 of the estimated time to
1320 * send a single character, and make it at least 1. The check
1321 * interval should also be less than the timeout.
1323 * Note: we have to use pretty tight timings here to satisfy
1326 char_time = (port->timeout - HZ/50) / port->fifosize;
1327 char_time = char_time / 5;
1330 if (timeout && timeout < char_time)
1331 char_time = timeout;
1334 * If the transmitter hasn't cleared in twice the approximate
1335 * amount of time to send the entire FIFO, it probably won't
1336 * ever clear. This assumes the UART isn't doing flow
1337 * control, which is currently the case. Hence, if it ever
1338 * takes longer than port->timeout, this is probably due to a
1339 * UART bug of some kind. So, we clamp the timeout parameter at
1342 if (timeout == 0 || timeout > 2 * port->timeout)
1343 timeout = 2 * port->timeout;
1345 expire = jiffies + timeout;
1347 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1348 port->line, jiffies, expire);
1351 * Check whether the transmitter is empty every 'char_time'.
1352 * 'timeout' / 'expire' give us the maximum amount of time
1355 while (!port->ops->tx_empty(port)) {
1356 set_current_state(TASK_INTERRUPTIBLE);
1357 schedule_timeout(char_time);
1358 if (signal_pending(current))
1360 if (time_after(jiffies, expire))
1363 set_current_state(TASK_RUNNING); /* might not be needed */
1367 * This is called with the BKL held in
1368 * linux/drivers/char/tty_io.c:do_tty_hangup()
1369 * We're called from the eventd thread, so we can sleep for
1370 * a _short_ time only.
1372 static void uart_hangup(struct tty_struct *tty)
1374 struct uart_state *state = tty->driver_data;
1376 BUG_ON(!kernel_locked());
1377 DPRINTK("uart_hangup(%d)\n", state->port->line);
1380 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1381 uart_flush_buffer(tty);
1382 uart_shutdown(state);
1384 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1385 state->info->tty = NULL;
1386 wake_up_interruptible(&state->info->open_wait);
1387 wake_up_interruptible(&state->info->delta_msr_wait);
1393 * Copy across the serial console cflag setting into the termios settings
1394 * for the initial open of the port. This allows continuity between the
1395 * kernel settings, and the settings init adopts when it opens the port
1396 * for the first time.
1398 static void uart_update_termios(struct uart_state *state)
1400 struct tty_struct *tty = state->info->tty;
1401 struct uart_port *port = state->port;
1403 if (uart_console(port) && port->cons->cflag) {
1404 tty->termios->c_cflag = port->cons->cflag;
1405 port->cons->cflag = 0;
1409 * If the device failed to grab its irq resources,
1410 * or some other error occurred, don't try to talk
1411 * to the port hardware.
1413 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1415 * Make termios settings take effect.
1417 uart_change_speed(state, NULL);
1420 * And finally enable the RTS and DTR signals.
1422 if (tty->termios->c_cflag & CBAUD)
1423 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1428 * Block the open until the port is ready. We must be called with
1429 * the per-port semaphore held.
1432 uart_block_til_ready(struct file *filp, struct uart_state *state)
1434 DECLARE_WAITQUEUE(wait, current);
1435 struct uart_info *info = state->info;
1436 struct uart_port *port = state->port;
1438 info->blocked_open++;
1441 add_wait_queue(&info->open_wait, &wait);
1443 set_current_state(TASK_INTERRUPTIBLE);
1446 * If we have been hung up, tell userspace/restart open.
1448 if (tty_hung_up_p(filp) || info->tty == NULL)
1452 * If the port has been closed, tell userspace/restart open.
1454 if (!(info->flags & UIF_INITIALIZED))
1458 * If non-blocking mode is set, or CLOCAL mode is set,
1459 * we don't want to wait for the modem status lines to
1460 * indicate that the port is ready.
1462 * Also, if the port is not enabled/configured, we want
1463 * to allow the open to succeed here. Note that we will
1464 * have set TTY_IO_ERROR for a non-existant port.
1466 if ((filp->f_flags & O_NONBLOCK) ||
1467 (info->tty->termios->c_cflag & CLOCAL) ||
1468 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1473 * Set DTR to allow modem to know we're waiting. Do
1474 * not set RTS here - we want to make sure we catch
1475 * the data from the modem.
1477 if (info->tty->termios->c_cflag & CBAUD)
1478 uart_set_mctrl(port, TIOCM_DTR);
1481 * and wait for the carrier to indicate that the
1482 * modem is ready for us.
1484 if (port->ops->get_mctrl(port) & TIOCM_CAR)
1491 if (signal_pending(current))
1494 set_current_state(TASK_RUNNING);
1495 remove_wait_queue(&info->open_wait, &wait);
1498 info->blocked_open--;
1500 if (signal_pending(current))
1501 return -ERESTARTSYS;
1503 if (!info->tty || tty_hung_up_p(filp))
1509 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1511 struct uart_state *state;
1514 state = drv->state + line;
1515 if (down_interruptible(&state->sem)) {
1516 state = ERR_PTR(-ERESTARTSYS);
1524 state = ERR_PTR(-ENXIO);
1529 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1531 memset(state->info, 0, sizeof(struct uart_info));
1532 init_waitqueue_head(&state->info->open_wait);
1533 init_waitqueue_head(&state->info->delta_msr_wait);
1536 * Link the info into the other structures.
1538 state->port->info = state->info;
1540 tasklet_init(&state->info->tlet, uart_tasklet_action,
1541 (unsigned long)state);
1545 state = ERR_PTR(-ENOMEM);
1555 * In 2.4.5, calls to uart_open are serialised by the BKL in
1556 * linux/fs/devices.c:chrdev_open()
1557 * Note that if this fails, then uart_close() _will_ be called.
1559 * In time, we want to scrap the "opening nonpresent ports"
1560 * behaviour and implement an alternative way for setserial
1561 * to set base addresses/ports/types. This will allow us to
1562 * get rid of a certain amount of extra tests.
1564 static int uart_open(struct tty_struct *tty, struct file *filp)
1566 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1567 struct uart_state *state;
1568 int retval, line = tty->index;
1570 BUG_ON(!kernel_locked());
1571 DPRINTK("uart_open(%d) called\n", line);
1574 * tty->driver->num won't change, so we won't fail here with
1575 * tty->driver_data set to something non-NULL (and therefore
1576 * we won't get caught by uart_close()).
1579 if (line >= tty->driver->num)
1583 * We take the semaphore inside uart_get to guarantee that we won't
1584 * be re-entered while allocating the info structure, or while we
1585 * request any IRQs that the driver may need. This also has the nice
1586 * side-effect that it delays the action of uart_hangup, so we can
1587 * guarantee that info->tty will always contain something reasonable.
1589 state = uart_get(drv, line);
1590 if (IS_ERR(state)) {
1591 retval = PTR_ERR(state);
1596 * Once we set tty->driver_data here, we are guaranteed that
1597 * uart_close() will decrement the driver module use count.
1598 * Any failures from here onwards should not touch the count.
1600 tty->driver_data = state;
1601 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1603 state->info->tty = tty;
1606 * If the port is in the middle of closing, bail out now.
1608 if (tty_hung_up_p(filp)) {
1616 * Make sure the device is in D0 state.
1618 if (state->count == 1)
1619 uart_change_pm(state, 0);
1622 * Start up the serial port.
1624 retval = uart_startup(state, 0);
1627 * If we succeeded, wait until the port is ready.
1630 retval = uart_block_til_ready(filp, state);
1634 * If this is the first open to succeed, adjust things to suit.
1636 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1637 state->info->flags |= UIF_NORMAL_ACTIVE;
1639 uart_update_termios(state);
1646 static const char *uart_type(struct uart_port *port)
1648 const char *str = NULL;
1650 if (port->ops->type)
1651 str = port->ops->type(port);
1659 #ifdef CONFIG_PROC_FS
1661 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1663 struct uart_state *state = drv->state + i;
1664 struct uart_port *port = state->port;
1666 unsigned int status;
1672 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1673 port->line, uart_type(port),
1674 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1675 port->iotype == UPIO_MEM ? port->mapbase :
1676 (unsigned long) port->iobase,
1679 if (port->type == PORT_UNKNOWN) {
1684 if(capable(CAP_SYS_ADMIN))
1686 status = port->ops->get_mctrl(port);
1688 ret += sprintf(buf + ret, " tx:%d rx:%d",
1689 port->icount.tx, port->icount.rx);
1690 if (port->icount.frame)
1691 ret += sprintf(buf + ret, " fe:%d",
1692 port->icount.frame);
1693 if (port->icount.parity)
1694 ret += sprintf(buf + ret, " pe:%d",
1695 port->icount.parity);
1696 if (port->icount.brk)
1697 ret += sprintf(buf + ret, " brk:%d",
1699 if (port->icount.overrun)
1700 ret += sprintf(buf + ret, " oe:%d",
1701 port->icount.overrun);
1703 #define INFOBIT(bit,str) \
1704 if (port->mctrl & (bit)) \
1705 strncat(stat_buf, (str), sizeof(stat_buf) - \
1706 strlen(stat_buf) - 2)
1707 #define STATBIT(bit,str) \
1708 if (status & (bit)) \
1709 strncat(stat_buf, (str), sizeof(stat_buf) - \
1710 strlen(stat_buf) - 2)
1714 INFOBIT(TIOCM_RTS, "|RTS");
1715 STATBIT(TIOCM_CTS, "|CTS");
1716 INFOBIT(TIOCM_DTR, "|DTR");
1717 STATBIT(TIOCM_DSR, "|DSR");
1718 STATBIT(TIOCM_CAR, "|CD");
1719 STATBIT(TIOCM_RNG, "|RI");
1722 strcat(stat_buf, "\n");
1724 ret += sprintf(buf + ret, stat_buf);
1734 static int uart_read_proc(char *page, char **start, off_t off,
1735 int count, int *eof, void *data)
1737 struct tty_driver *ttydrv = data;
1738 struct uart_driver *drv = ttydrv->driver_state;
1742 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1744 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1745 l = uart_line_info(page + len, drv, i);
1747 if (len + begin > off + count)
1749 if (len + begin < off) {
1756 if (off >= len + begin)
1758 *start = page + (off - begin);
1759 return (count < begin + len - off) ? count : (begin + len - off);
1763 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1765 * Check whether an invalid uart number has been specified, and
1766 * if so, search for the first available port that does have
1769 struct uart_port * __init
1770 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1772 int idx = co->index;
1774 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1775 ports[idx].membase == NULL))
1776 for (idx = 0; idx < nr; idx++)
1777 if (ports[idx].iobase != 0 ||
1778 ports[idx].membase != NULL)
1787 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1788 * @options: pointer to option string
1789 * @baud: pointer to an 'int' variable for the baud rate.
1790 * @parity: pointer to an 'int' variable for the parity.
1791 * @bits: pointer to an 'int' variable for the number of data bits.
1792 * @flow: pointer to an 'int' variable for the flow control character.
1794 * uart_parse_options decodes a string containing the serial console
1795 * options. The format of the string is <baud><parity><bits><flow>,
1799 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1803 *baud = simple_strtoul(s, NULL, 10);
1804 while (*s >= '0' && *s <= '9')
1819 static struct baud_rates baud_rates[] = {
1820 { 921600, B921600 },
1821 { 460800, B460800 },
1822 { 230400, B230400 },
1823 { 115200, B115200 },
1835 * uart_set_options - setup the serial console parameters
1836 * @port: pointer to the serial ports uart_port structure
1837 * @co: console pointer
1839 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1840 * @bits: number of data bits
1841 * @flow: flow control character - 'r' (rts)
1844 uart_set_options(struct uart_port *port, struct console *co,
1845 int baud, int parity, int bits, int flow)
1847 struct termios termios;
1850 memset(&termios, 0, sizeof(struct termios));
1852 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1855 * Construct a cflag setting.
1857 for (i = 0; baud_rates[i].rate; i++)
1858 if (baud_rates[i].rate <= baud)
1861 termios.c_cflag |= baud_rates[i].cflag;
1864 termios.c_cflag |= CS7;
1866 termios.c_cflag |= CS8;
1870 termios.c_cflag |= PARODD;
1873 termios.c_cflag |= PARENB;
1878 termios.c_cflag |= CRTSCTS;
1880 port->ops->set_termios(port, &termios, NULL);
1881 co->cflag = termios.c_cflag;
1885 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1887 static void uart_change_pm(struct uart_state *state, int pm_state)
1889 struct uart_port *port = state->port;
1891 port->ops->pm(port, pm_state, state->pm_state);
1892 state->pm_state = pm_state;
1895 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1897 struct uart_state *state = drv->state + port->line;
1901 if (state->info && state->info->flags & UIF_INITIALIZED) {
1902 struct uart_ops *ops = port->ops;
1904 spin_lock_irq(&port->lock);
1905 ops->stop_tx(port, 0);
1906 ops->set_mctrl(port, 0);
1908 spin_unlock_irq(&port->lock);
1911 * Wait for the transmitter to empty.
1913 while (!ops->tx_empty(port)) {
1914 set_current_state(TASK_UNINTERRUPTIBLE);
1915 schedule_timeout(10*HZ/1000);
1917 set_current_state(TASK_RUNNING);
1919 ops->shutdown(port);
1923 * Disable the console device before suspending.
1925 if (uart_console(port))
1926 console_stop(port->cons);
1928 uart_change_pm(state, 3);
1935 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1937 struct uart_state *state = drv->state + port->line;
1941 uart_change_pm(state, 0);
1944 * Re-enable the console device after suspending.
1946 if (uart_console(port)) {
1947 uart_change_speed(state, NULL);
1948 console_start(port->cons);
1951 if (state->info && state->info->flags & UIF_INITIALIZED) {
1952 struct uart_ops *ops = port->ops;
1954 ops->set_mctrl(port, 0);
1956 uart_change_speed(state, NULL);
1957 spin_lock_irq(&port->lock);
1958 ops->set_mctrl(port, port->mctrl);
1959 ops->start_tx(port, 0);
1960 spin_unlock_irq(&port->lock);
1969 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1971 printk("%s%d", drv->dev_name, port->line);
1973 switch (port->iotype) {
1975 printk("I/O 0x%x", port->iobase);
1978 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1981 printk("MMIO 0x%lx", port->mapbase);
1984 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
1988 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1989 struct uart_port *port)
1994 * If there isn't a port here, don't do anything further.
1996 if (!port->iobase && !port->mapbase && !port->membase)
2000 * Now do the auto configuration stuff. Note that config_port
2001 * is expected to claim the resources and map the port for us.
2003 flags = UART_CONFIG_TYPE;
2004 if (port->flags & UPF_AUTO_IRQ)
2005 flags |= UART_CONFIG_IRQ;
2006 if (port->flags & UPF_BOOT_AUTOCONF) {
2007 port->type = PORT_UNKNOWN;
2008 port->ops->config_port(port, flags);
2011 if (port->type != PORT_UNKNOWN) {
2012 unsigned long flags;
2014 uart_report_port(drv, port);
2017 * Ensure that the modem control lines are de-activated.
2018 * We probably don't need a spinlock around this, but
2020 spin_lock_irqsave(&port->lock, flags);
2021 port->ops->set_mctrl(port, 0);
2022 spin_unlock_irqrestore(&port->lock, flags);
2025 * Power down all ports by default, except the
2026 * console if we have one.
2028 if (!uart_console(port))
2029 uart_change_pm(state, 3);
2034 * This reverses the effects of uart_configure_port, hanging up the
2035 * port before removal.
2038 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2040 struct uart_port *port = state->port;
2041 struct uart_info *info = state->info;
2043 if (info && info->tty)
2044 tty_vhangup(info->tty);
2051 * Free the port IO and memory resources, if any.
2053 if (port->type != PORT_UNKNOWN)
2054 port->ops->release_port(port);
2057 * Indicate that there isn't a port here anymore.
2059 port->type = PORT_UNKNOWN;
2062 * Kill the tasklet, and free resources.
2065 tasklet_kill(&info->tlet);
2072 static struct tty_operations uart_ops = {
2074 .close = uart_close,
2075 .write = uart_write,
2076 .put_char = uart_put_char,
2077 .flush_chars = uart_flush_chars,
2078 .write_room = uart_write_room,
2079 .chars_in_buffer= uart_chars_in_buffer,
2080 .flush_buffer = uart_flush_buffer,
2081 .ioctl = uart_ioctl,
2082 .throttle = uart_throttle,
2083 .unthrottle = uart_unthrottle,
2084 .send_xchar = uart_send_xchar,
2085 .set_termios = uart_set_termios,
2087 .start = uart_start,
2088 .hangup = uart_hangup,
2089 .break_ctl = uart_break_ctl,
2090 .wait_until_sent= uart_wait_until_sent,
2091 #ifdef CONFIG_PROC_FS
2092 .read_proc = uart_read_proc,
2094 .tiocmget = uart_tiocmget,
2095 .tiocmset = uart_tiocmset,
2099 * uart_register_driver - register a driver with the uart core layer
2100 * @drv: low level driver structure
2102 * Register a uart driver with the core driver. We in turn register
2103 * with the tty layer, and initialise the core driver per-port state.
2105 * We have a proc file in /proc/tty/driver which is named after the
2108 * drv->port should be NULL, and the per-port structures should be
2109 * registered using uart_add_one_port after this call has succeeded.
2111 int uart_register_driver(struct uart_driver *drv)
2113 struct tty_driver *normal = NULL;
2119 * Maybe we should be using a slab cache for this, especially if
2120 * we have a large number of ports to handle.
2122 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2127 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2129 normal = alloc_tty_driver(drv->nr);
2133 drv->tty_driver = normal;
2135 normal->owner = drv->owner;
2136 normal->driver_name = drv->driver_name;
2137 normal->devfs_name = drv->devfs_name;
2138 normal->name = drv->dev_name;
2139 normal->major = drv->major;
2140 normal->minor_start = drv->minor;
2141 normal->type = TTY_DRIVER_TYPE_SERIAL;
2142 normal->subtype = SERIAL_TYPE_NORMAL;
2143 normal->init_termios = tty_std_termios;
2144 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2145 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2146 normal->driver_state = drv;
2147 tty_set_operations(normal, &uart_ops);
2150 * Initialise the UART state(s).
2152 for (i = 0; i < drv->nr; i++) {
2153 struct uart_state *state = drv->state + i;
2155 state->close_delay = 5 * HZ / 10;
2156 state->closing_wait = 30 * HZ;
2158 init_MUTEX(&state->sem);
2161 retval = tty_register_driver(normal);
2164 put_tty_driver(normal);
2171 * uart_unregister_driver - remove a driver from the uart core layer
2172 * @drv: low level driver structure
2174 * Remove all references to a driver from the core driver. The low
2175 * level driver must have removed all its ports via the
2176 * uart_remove_one_port() if it registered them with uart_add_one_port().
2177 * (ie, drv->port == NULL)
2179 void uart_unregister_driver(struct uart_driver *drv)
2181 struct tty_driver *p = drv->tty_driver;
2182 tty_unregister_driver(p);
2185 drv->tty_driver = NULL;
2188 struct tty_driver *uart_console_device(struct console *co, int *index)
2190 struct uart_driver *p = co->data;
2192 return p->tty_driver;
2196 * uart_add_one_port - attach a driver-defined port structure
2197 * @drv: pointer to the uart low level driver structure for this port
2198 * @port: uart port structure to use for this port.
2200 * This allows the driver to register its own uart_port structure
2201 * with the core driver. The main purpose is to allow the low
2202 * level uart drivers to expand uart_port, rather than having yet
2203 * more levels of structures.
2205 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2207 struct uart_state *state;
2210 BUG_ON(in_interrupt());
2212 if (port->line >= drv->nr)
2215 state = drv->state + port->line;
2225 spin_lock_init(&port->lock);
2226 port->cons = drv->cons;
2227 port->info = state->info;
2229 uart_configure_port(drv, state, port);
2232 * Register the port whether it's detected or not. This allows
2233 * setserial to be used to alter this ports parameters.
2235 tty_register_device(drv->tty_driver, port->line, port->dev);
2244 * uart_remove_one_port - detach a driver defined port structure
2245 * @drv: pointer to the uart low level driver structure for this port
2246 * @port: uart port structure for this port
2248 * This unhooks (and hangs up) the specified port structure from the
2249 * core driver. No further calls will be made to the low-level code
2252 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2254 struct uart_state *state = drv->state + port->line;
2256 BUG_ON(in_interrupt());
2258 if (state->port != port)
2259 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2265 * Remove the devices from devfs
2267 tty_unregister_device(drv->tty_driver, port->line);
2269 uart_unconfigure_port(drv, state);
2277 * Are the two ports equivalent?
2279 static int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2281 if (port1->iotype != port2->iotype)
2284 switch (port1->iotype) {
2286 return (port1->iobase == port2->iobase);
2288 return (port1->iobase == port2->iobase) &&
2289 (port1->hub6 == port2->hub6);
2291 return (port1->membase == port2->membase);
2297 * Try to find an unused uart_state slot for a port.
2299 static struct uart_state *
2300 uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2305 * First, find a port entry which matches. Note: if we do
2306 * find a matching entry, and it has a non-zero use count,
2307 * then we can't register the port.
2309 for (i = 0; i < drv->nr; i++)
2310 if (uart_match_port(drv->state[i].port, port))
2311 return &drv->state[i];
2314 * We didn't find a matching entry, so look for the first
2315 * free entry. We look for one which hasn't been previously
2316 * used (indicated by zero iobase).
2318 for (i = 0; i < drv->nr; i++)
2319 if (drv->state[i].port->type == PORT_UNKNOWN &&
2320 drv->state[i].port->iobase == 0 &&
2321 drv->state[i].count == 0)
2322 return &drv->state[i];
2325 * That also failed. Last resort is to find any currently
2326 * entry which doesn't have a real port associated with it.
2328 for (i = 0; i < drv->nr; i++)
2329 if (drv->state[i].port->type == PORT_UNKNOWN &&
2330 drv->state[i].count == 0)
2331 return &drv->state[i];
2337 * uart_register_port: register uart settings with a port
2338 * @drv: pointer to the uart low level driver structure for this port
2339 * @port: uart port structure describing the port
2341 * Register UART settings with the specified low level driver. Detect
2342 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2343 * IRQ if UPF_AUTO_IRQ is set.
2345 * We try to pick the same port for the same IO base address, so that
2346 * when a modem is plugged in, unplugged and plugged back in, it gets
2347 * allocated the same port.
2349 * Returns negative error, or positive line number.
2351 int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2353 struct uart_state *state;
2358 state = uart_find_match_or_unused(drv, port);
2362 * Ok, we've found a line that we can use.
2364 * If we find a port that matches this one, and it appears
2365 * to be in-use (even if it doesn't have a type) we shouldn't
2366 * alter it underneath itself - the port may be open and
2367 * trying to do useful work.
2369 if (uart_users(state) != 0) {
2375 * If the port is already initialised, don't touch it.
2377 if (state->port->type == PORT_UNKNOWN) {
2378 state->port->iobase = port->iobase;
2379 state->port->membase = port->membase;
2380 state->port->irq = port->irq;
2381 state->port->uartclk = port->uartclk;
2382 state->port->fifosize = port->fifosize;
2383 state->port->regshift = port->regshift;
2384 state->port->iotype = port->iotype;
2385 state->port->flags = port->flags;
2386 state->port->line = state - drv->state;
2387 state->port->mapbase = port->mapbase;
2389 uart_configure_port(drv, state, state->port);
2392 ret = state->port->line;
2401 * uart_unregister_port - de-allocate a port
2402 * @drv: pointer to the uart low level driver structure for this port
2403 * @line: line index previously returned from uart_register_port()
2405 * Hang up the specified line associated with the low level driver,
2406 * and mark the port as unused.
2408 void uart_unregister_port(struct uart_driver *drv, int line)
2410 struct uart_state *state;
2412 if (line < 0 || line >= drv->nr) {
2413 printk(KERN_ERR "Attempt to unregister ");
2414 printk("%s%d", drv->dev_name, line);
2419 state = drv->state + line;
2422 uart_unconfigure_port(drv, state);
2426 EXPORT_SYMBOL(uart_write_wakeup);
2427 EXPORT_SYMBOL(uart_register_driver);
2428 EXPORT_SYMBOL(uart_unregister_driver);
2429 EXPORT_SYMBOL(uart_suspend_port);
2430 EXPORT_SYMBOL(uart_resume_port);
2431 EXPORT_SYMBOL(uart_register_port);
2432 EXPORT_SYMBOL(uart_unregister_port);
2433 EXPORT_SYMBOL(uart_add_one_port);
2434 EXPORT_SYMBOL(uart_remove_one_port);
2436 MODULE_DESCRIPTION("Serial driver core");
2437 MODULE_LICENSE("GPL");