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>
38 #include <asm/uaccess.h>
42 #define DPRINTK(x...) printk(x)
44 #define DPRINTK(x...) do { } while (0)
48 * This is used to lock changes in serial line configuration.
50 static DECLARE_MUTEX(port_sem);
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
56 #ifdef CONFIG_SERIAL_CORE_CONSOLE
57 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
59 #define uart_console(port) (0)
62 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
63 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
64 static void uart_change_pm(struct uart_state *state, int pm_state);
67 * This routine is used by the interrupt handler to schedule processing in
68 * the software interrupt portion of the driver.
70 void uart_write_wakeup(struct uart_port *port)
72 struct uart_info *info = port->info;
73 tasklet_schedule(&info->tlet);
76 static void uart_stop(struct tty_struct *tty)
78 struct uart_state *state = tty->driver_data;
79 struct uart_port *port = state->port;
82 spin_lock_irqsave(&port->lock, flags);
83 port->ops->stop_tx(port, 1);
84 spin_unlock_irqrestore(&port->lock, flags);
87 static void __uart_start(struct tty_struct *tty)
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->port;
92 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
93 !tty->stopped && !tty->hw_stopped)
94 port->ops->start_tx(port, 1);
97 static void uart_start(struct tty_struct *tty)
99 struct uart_state *state = tty->driver_data;
100 struct uart_port *port = state->port;
103 spin_lock_irqsave(&port->lock, flags);
105 spin_unlock_irqrestore(&port->lock, flags);
108 static void uart_tasklet_action(unsigned long data)
110 struct uart_state *state = (struct uart_state *)data;
111 tty_wakeup(state->info->tty);
115 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
120 spin_lock_irqsave(&port->lock, flags);
122 port->mctrl = (old & ~clear) | set;
123 if (old != port->mctrl)
124 port->ops->set_mctrl(port, port->mctrl);
125 spin_unlock_irqrestore(&port->lock, flags);
128 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
129 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
132 * Startup the port. This will be called once per open. All calls
133 * will be serialised by the per-port semaphore.
135 static int uart_startup(struct uart_state *state, int init_hw)
137 struct uart_info *info = state->info;
138 struct uart_port *port = state->port;
142 if (info->flags & UIF_INITIALIZED)
146 * Set the TTY IO error marker - we will only clear this
147 * once we have successfully opened the port. Also set
148 * up the tty->alt_speed kludge
151 set_bit(TTY_IO_ERROR, &info->tty->flags);
153 if (port->type == PORT_UNKNOWN)
157 * Initialise and allocate the transmit and temporary
160 if (!info->xmit.buf) {
161 page = get_zeroed_page(GFP_KERNEL);
165 info->xmit.buf = (unsigned char *) page;
166 uart_circ_clear(&info->xmit);
169 retval = port->ops->startup(port);
173 * Initialise the hardware port settings.
175 uart_change_speed(state, NULL);
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (info->tty->termios->c_cflag & CBAUD)
182 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
185 info->flags |= UIF_INITIALIZED;
187 clear_bit(TTY_IO_ERROR, &info->tty->flags);
190 if (retval && capable(CAP_SYS_ADMIN))
197 * This routine will shutdown a serial port; interrupts are disabled, and
198 * DTR is dropped if the hangup on close termio flag is on. Calls to
199 * uart_shutdown are serialised by the per-port semaphore.
201 static void uart_shutdown(struct uart_state *state)
203 struct uart_info *info = state->info;
204 struct uart_port *port = state->port;
206 if (!(info->flags & UIF_INITIALIZED))
210 * Turn off DTR and RTS early.
212 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
213 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
216 * clear delta_msr_wait queue to avoid mem leaks: we may free
217 * the irq here so the queue might never be woken up. Note
218 * that we won't end up waiting on delta_msr_wait again since
219 * any outstanding file descriptors should be pointing at
220 * hung_up_tty_fops now.
222 wake_up_interruptible(&info->delta_msr_wait);
225 * Free the IRQ and disable the port.
227 port->ops->shutdown(port);
230 * Ensure that the IRQ handler isn't running on another CPU.
232 synchronize_irq(port->irq);
235 * Free the transmit buffer page.
237 if (info->xmit.buf) {
238 free_page((unsigned long)info->xmit.buf);
239 info->xmit.buf = NULL;
243 * kill off our tasklet
245 tasklet_kill(&info->tlet);
247 set_bit(TTY_IO_ERROR, &info->tty->flags);
249 info->flags &= ~UIF_INITIALIZED;
253 * uart_update_timeout - update per-port FIFO timeout.
254 * @port: uart_port structure describing the port
255 * @cflag: termios cflag value
256 * @baud: speed of the port
258 * Set the port FIFO timeout value. The @cflag value should
259 * reflect the actual hardware settings.
262 uart_update_timeout(struct uart_port *port, unsigned int cflag,
267 /* byte size and parity */
268 switch (cflag & CSIZE) {
289 * The total number of bits to be transmitted in the fifo.
291 bits = bits * port->fifosize;
294 * Figure the timeout to send the above number of bits.
295 * Add .02 seconds of slop
297 port->timeout = (HZ * bits) / baud + HZ/50;
300 EXPORT_SYMBOL(uart_update_timeout);
303 * uart_get_baud_rate - return baud rate for a particular port
304 * @port: uart_port structure describing the port in question.
305 * @termios: desired termios settings.
306 * @old: old termios (or NULL)
307 * @min: minimum acceptable baud rate
308 * @max: maximum acceptable baud rate
310 * Decode the termios structure into a numeric baud rate,
311 * taking account of the magic 38400 baud rate (with spd_*
312 * flags), and mapping the %B0 rate to 9600 baud.
314 * If the new baud rate is invalid, try the old termios setting.
315 * If it's still invalid, we try 9600 baud.
317 * Update the @termios structure to reflect the baud rate
318 * we're actually going to be using.
321 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
322 struct termios *old, unsigned int min, unsigned int max)
324 unsigned int try, baud, altbaud = 38400;
325 unsigned int flags = port->flags & UPF_SPD_MASK;
327 if (flags == UPF_SPD_HI)
329 if (flags == UPF_SPD_VHI)
331 if (flags == UPF_SPD_SHI)
333 if (flags == UPF_SPD_WARP)
336 for (try = 0; try < 2; try++) {
337 baud = tty_termios_baud_rate(termios);
340 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
347 * Special case: B0 rate.
352 if (baud >= min && baud <= max)
356 * Oops, the quotient was zero. Try again with
357 * the old baud rate if possible.
359 termios->c_cflag &= ~CBAUD;
361 termios->c_cflag |= old->c_cflag & CBAUD;
367 * As a last resort, if the quotient is zero,
368 * default to 9600 bps
370 termios->c_cflag |= B9600;
376 EXPORT_SYMBOL(uart_get_baud_rate);
379 * uart_get_divisor - return uart clock divisor
380 * @port: uart_port structure describing the port.
381 * @baud: desired baud rate
383 * Calculate the uart clock divisor for the port.
386 uart_get_divisor(struct uart_port *port, unsigned int baud)
391 * Old custom speed handling.
393 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
394 quot = port->custom_divisor;
396 quot = (port->uartclk + (8 * baud)) / (16 * baud);
401 EXPORT_SYMBOL(uart_get_divisor);
404 uart_change_speed(struct uart_state *state, struct termios *old_termios)
406 struct tty_struct *tty = state->info->tty;
407 struct uart_port *port = state->port;
408 struct termios *termios;
411 * If we have no tty, termios, or the port does not exist,
412 * then we can't set the parameters for this port.
414 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
417 termios = tty->termios;
420 * Set flags based on termios cflag
422 if (termios->c_cflag & CRTSCTS)
423 state->info->flags |= UIF_CTS_FLOW;
425 state->info->flags &= ~UIF_CTS_FLOW;
427 if (termios->c_cflag & CLOCAL)
428 state->info->flags &= ~UIF_CHECK_CD;
430 state->info->flags |= UIF_CHECK_CD;
432 port->ops->set_termios(port, termios, old_termios);
436 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
443 spin_lock_irqsave(&port->lock, flags);
444 if (uart_circ_chars_free(circ) != 0) {
445 circ->buf[circ->head] = c;
446 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
448 spin_unlock_irqrestore(&port->lock, flags);
451 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
453 struct uart_state *state = tty->driver_data;
455 __uart_put_char(state->port, &state->info->xmit, ch);
458 static void uart_flush_chars(struct tty_struct *tty)
464 uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
466 struct uart_state *state = tty->driver_data;
467 struct uart_port *port = state->port;
468 struct circ_buf *circ = &state->info->xmit;
475 spin_lock_irqsave(&port->lock, flags);
477 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
482 memcpy(circ->buf + circ->head, buf, c);
483 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
488 spin_unlock_irqrestore(&port->lock, flags);
494 static int uart_write_room(struct tty_struct *tty)
496 struct uart_state *state = tty->driver_data;
498 return uart_circ_chars_free(&state->info->xmit);
501 static int uart_chars_in_buffer(struct tty_struct *tty)
503 struct uart_state *state = tty->driver_data;
505 return uart_circ_chars_pending(&state->info->xmit);
508 static void uart_flush_buffer(struct tty_struct *tty)
510 struct uart_state *state = tty->driver_data;
511 struct uart_port *port = state->port;
514 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
516 spin_lock_irqsave(&port->lock, flags);
517 uart_circ_clear(&state->info->xmit);
518 spin_unlock_irqrestore(&port->lock, flags);
523 * This function is used to send a high-priority XON/XOFF character to
526 static void uart_send_xchar(struct tty_struct *tty, char ch)
528 struct uart_state *state = tty->driver_data;
529 struct uart_port *port = state->port;
532 if (port->ops->send_xchar)
533 port->ops->send_xchar(port, ch);
537 spin_lock_irqsave(&port->lock, flags);
538 port->ops->start_tx(port, 0);
539 spin_unlock_irqrestore(&port->lock, flags);
544 static void uart_throttle(struct tty_struct *tty)
546 struct uart_state *state = tty->driver_data;
549 uart_send_xchar(tty, STOP_CHAR(tty));
551 if (tty->termios->c_cflag & CRTSCTS)
552 uart_clear_mctrl(state->port, TIOCM_RTS);
555 static void uart_unthrottle(struct tty_struct *tty)
557 struct uart_state *state = tty->driver_data;
558 struct uart_port *port = state->port;
564 uart_send_xchar(tty, START_CHAR(tty));
567 if (tty->termios->c_cflag & CRTSCTS)
568 uart_set_mctrl(port, TIOCM_RTS);
571 static int uart_get_info(struct uart_state *state,
572 struct serial_struct __user *retinfo)
574 struct uart_port *port = state->port;
575 struct serial_struct tmp;
577 memset(&tmp, 0, sizeof(tmp));
578 tmp.type = port->type;
579 tmp.line = port->line;
580 tmp.port = port->iobase;
581 if (HIGH_BITS_OFFSET)
582 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
584 tmp.flags = port->flags;
585 tmp.xmit_fifo_size = port->fifosize;
586 tmp.baud_base = port->uartclk / 16;
587 tmp.close_delay = state->close_delay / 10;
588 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
589 ASYNC_CLOSING_WAIT_NONE :
590 state->closing_wait / 10;
591 tmp.custom_divisor = port->custom_divisor;
592 tmp.hub6 = port->hub6;
593 tmp.io_type = port->iotype;
594 tmp.iomem_reg_shift = port->regshift;
595 tmp.iomem_base = (void *)port->mapbase;
597 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
602 static int uart_set_info(struct uart_state *state,
603 struct serial_struct __user *newinfo)
605 struct serial_struct new_serial;
606 struct uart_port *port = state->port;
607 unsigned long new_port;
608 unsigned int change_irq, change_port, old_flags, closing_wait;
609 unsigned int old_custom_divisor, close_delay;
612 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
615 new_port = new_serial.port;
616 if (HIGH_BITS_OFFSET)
617 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
619 new_serial.irq = irq_canonicalize(new_serial.irq);
620 close_delay = new_serial.close_delay * 10;
621 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
622 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
625 * This semaphore protects state->count. It is also
626 * very useful to prevent opens. Also, take the
627 * port configuration semaphore to make sure that a
628 * module insertion/removal doesn't change anything
633 change_irq = new_serial.irq != port->irq;
636 * Since changing the 'type' of the port changes its resource
637 * allocations, we should treat type changes the same as
640 change_port = new_port != port->iobase ||
641 (unsigned long)new_serial.iomem_base != port->mapbase ||
642 new_serial.hub6 != port->hub6 ||
643 new_serial.io_type != port->iotype ||
644 new_serial.iomem_reg_shift != port->regshift ||
645 new_serial.type != port->type;
647 old_flags = port->flags;
648 old_custom_divisor = port->custom_divisor;
650 if (!capable(CAP_SYS_ADMIN)) {
652 if (change_irq || change_port ||
653 (new_serial.baud_base != port->uartclk / 16) ||
654 (close_delay != state->close_delay) ||
655 (closing_wait != state->closing_wait) ||
656 (new_serial.xmit_fifo_size != port->fifosize) ||
657 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
659 port->flags = ((port->flags & ~UPF_USR_MASK) |
660 (new_serial.flags & UPF_USR_MASK));
661 port->custom_divisor = new_serial.custom_divisor;
666 * Ask the low level driver to verify the settings.
668 if (port->ops->verify_port)
669 retval = port->ops->verify_port(port, &new_serial);
671 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
672 (new_serial.baud_base < 9600))
678 if (change_port || change_irq) {
682 * Make sure that we are the sole user of this port.
684 if (uart_users(state) > 1)
688 * We need to shutdown the serial port at the old
689 * port/type/irq combination.
691 uart_shutdown(state);
695 unsigned long old_iobase, old_mapbase;
696 unsigned int old_type, old_iotype, old_hub6, old_shift;
698 old_iobase = port->iobase;
699 old_mapbase = port->mapbase;
700 old_type = port->type;
701 old_hub6 = port->hub6;
702 old_iotype = port->iotype;
703 old_shift = port->regshift;
706 * Free and release old regions
708 if (old_type != PORT_UNKNOWN)
709 port->ops->release_port(port);
711 port->iobase = new_port;
712 port->type = new_serial.type;
713 port->hub6 = new_serial.hub6;
714 port->iotype = new_serial.io_type;
715 port->regshift = new_serial.iomem_reg_shift;
716 port->mapbase = (unsigned long)new_serial.iomem_base;
719 * Claim and map the new regions
721 if (port->type != PORT_UNKNOWN) {
722 retval = port->ops->request_port(port);
724 /* Always success - Jean II */
729 * If we fail to request resources for the
730 * new port, try to restore the old settings.
732 if (retval && old_type != PORT_UNKNOWN) {
733 port->iobase = old_iobase;
734 port->type = old_type;
735 port->hub6 = old_hub6;
736 port->iotype = old_iotype;
737 port->regshift = old_shift;
738 port->mapbase = old_mapbase;
739 retval = port->ops->request_port(port);
741 * If we failed to restore the old settings,
745 port->type = PORT_UNKNOWN;
754 port->irq = new_serial.irq;
755 port->uartclk = new_serial.baud_base * 16;
756 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
757 (new_serial.flags & UPF_CHANGE_MASK);
758 port->custom_divisor = new_serial.custom_divisor;
759 state->close_delay = close_delay;
760 state->closing_wait = closing_wait;
761 port->fifosize = new_serial.xmit_fifo_size;
762 if (state->info->tty)
763 state->info->tty->low_latency =
764 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
768 if (port->type == PORT_UNKNOWN)
770 if (state->info->flags & UIF_INITIALIZED) {
771 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
772 old_custom_divisor != port->custom_divisor) {
774 * If they're setting up a custom divisor or speed,
775 * instead of clearing it, then bitch about it. No
776 * need to rate-limit; it's CAP_SYS_ADMIN only.
778 if (port->flags & UPF_SPD_MASK) {
781 "%s sets custom speed on %s. This "
782 "is deprecated.\n", current->comm,
783 tty_name(state->info->tty, buf));
785 uart_change_speed(state, NULL);
788 retval = uart_startup(state, 1);
796 * uart_get_lsr_info - get line status register info.
797 * Note: uart_ioctl protects us against hangups.
799 static int uart_get_lsr_info(struct uart_state *state,
800 unsigned int __user *value)
802 struct uart_port *port = state->port;
805 result = port->ops->tx_empty(port);
808 * If we're about to load something into the transmit
809 * register, we'll pretend the transmitter isn't empty to
810 * avoid a race condition (depending on when the transmit
811 * interrupt happens).
814 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
815 !state->info->tty->stopped && !state->info->tty->hw_stopped))
816 result &= ~TIOCSER_TEMT;
818 return put_user(result, value);
821 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
823 struct uart_state *state = tty->driver_data;
824 struct uart_port *port = state->port;
828 if ((!file || !tty_hung_up_p(file)) &&
829 !(tty->flags & (1 << TTY_IO_ERROR))) {
830 result = port->mctrl;
832 spin_lock_irq(&port->lock);
833 result |= port->ops->get_mctrl(port);
834 spin_unlock_irq(&port->lock);
842 uart_tiocmset(struct tty_struct *tty, struct file *file,
843 unsigned int set, unsigned int clear)
845 struct uart_state *state = tty->driver_data;
846 struct uart_port *port = state->port;
850 if ((!file || !tty_hung_up_p(file)) &&
851 !(tty->flags & (1 << TTY_IO_ERROR))) {
852 uart_update_mctrl(port, set, clear);
859 static void uart_break_ctl(struct tty_struct *tty, int break_state)
861 struct uart_state *state = tty->driver_data;
862 struct uart_port *port = state->port;
864 BUG_ON(!kernel_locked());
868 if (port->type != PORT_UNKNOWN)
869 port->ops->break_ctl(port, break_state);
874 static int uart_do_autoconfig(struct uart_state *state)
876 struct uart_port *port = state->port;
879 if (!capable(CAP_SYS_ADMIN))
883 * Take the per-port semaphore. This prevents count from
884 * changing, and hence any extra opens of the port while
885 * we're auto-configuring.
887 if (down_interruptible(&state->sem))
891 if (uart_users(state) == 1) {
892 uart_shutdown(state);
895 * If we already have a port type configured,
896 * we must release its resources.
898 if (port->type != PORT_UNKNOWN)
899 port->ops->release_port(port);
901 flags = UART_CONFIG_TYPE;
902 if (port->flags & UPF_AUTO_IRQ)
903 flags |= UART_CONFIG_IRQ;
906 * This will claim the ports resources if
909 port->ops->config_port(port, flags);
911 ret = uart_startup(state, 1);
918 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
919 * - mask passed in arg for lines of interest
920 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
921 * Caller should use TIOCGICOUNT to see which one it was
924 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
926 struct uart_port *port = state->port;
927 DECLARE_WAITQUEUE(wait, current);
928 struct uart_icount cprev, cnow;
932 * note the counters on entry
934 spin_lock_irq(&port->lock);
935 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
938 * Force modem status interrupts on
940 port->ops->enable_ms(port);
941 spin_unlock_irq(&port->lock);
943 add_wait_queue(&state->info->delta_msr_wait, &wait);
945 spin_lock_irq(&port->lock);
946 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
947 spin_unlock_irq(&port->lock);
949 set_current_state(TASK_INTERRUPTIBLE);
951 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
952 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
953 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
954 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
961 /* see if a signal did it */
962 if (signal_pending(current)) {
970 current->state = TASK_RUNNING;
971 remove_wait_queue(&state->info->delta_msr_wait, &wait);
977 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
978 * Return: write counters to the user passed counter struct
979 * NB: both 1->0 and 0->1 transitions are counted except for
980 * RI where only 0->1 is counted.
982 static int uart_get_count(struct uart_state *state,
983 struct serial_icounter_struct __user *icnt)
985 struct serial_icounter_struct icount;
986 struct uart_icount cnow;
987 struct uart_port *port = state->port;
989 spin_lock_irq(&port->lock);
990 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
991 spin_unlock_irq(&port->lock);
993 icount.cts = cnow.cts;
994 icount.dsr = cnow.dsr;
995 icount.rng = cnow.rng;
996 icount.dcd = cnow.dcd;
999 icount.frame = cnow.frame;
1000 icount.overrun = cnow.overrun;
1001 icount.parity = cnow.parity;
1002 icount.brk = cnow.brk;
1003 icount.buf_overrun = cnow.buf_overrun;
1005 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1009 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1012 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1015 struct uart_state *state = tty->driver_data;
1016 void __user *uarg = (void __user *)arg;
1017 int ret = -ENOIOCTLCMD;
1019 BUG_ON(!kernel_locked());
1022 * These ioctls don't rely on the hardware to be present.
1026 ret = uart_get_info(state, uarg);
1030 ret = uart_set_info(state, uarg);
1034 ret = uart_do_autoconfig(state);
1037 case TIOCSERGWILD: /* obsolete */
1038 case TIOCSERSWILD: /* obsolete */
1043 if (ret != -ENOIOCTLCMD)
1046 if (tty->flags & (1 << TTY_IO_ERROR)) {
1052 * The following should only be used when hardware is present.
1056 ret = uart_wait_modem_status(state, arg);
1060 ret = uart_get_count(state, uarg);
1064 if (ret != -ENOIOCTLCMD)
1069 if (tty_hung_up_p(filp)) {
1075 * All these rely on hardware being present and need to be
1076 * protected against the tty being hung up.
1079 case TIOCSERGETLSR: /* Get line status register */
1080 ret = uart_get_lsr_info(state, uarg);
1084 struct uart_port *port = state->port;
1085 if (port->ops->ioctl)
1086 ret = port->ops->ioctl(port, cmd, arg);
1096 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1098 struct uart_state *state = tty->driver_data;
1099 unsigned long flags;
1100 unsigned int cflag = tty->termios->c_cflag;
1102 BUG_ON(!kernel_locked());
1105 * These are the bits that are used to setup various
1106 * flags in the low level driver.
1108 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1110 if ((cflag ^ old_termios->c_cflag) == 0 &&
1111 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1114 uart_change_speed(state, old_termios);
1116 /* Handle transition to B0 status */
1117 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1118 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1120 /* Handle transition away from B0 status */
1121 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1122 unsigned int mask = TIOCM_DTR;
1123 if (!(cflag & CRTSCTS) ||
1124 !test_bit(TTY_THROTTLED, &tty->flags))
1126 uart_set_mctrl(state->port, mask);
1129 /* Handle turning off CRTSCTS */
1130 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1131 spin_lock_irqsave(&state->port->lock, flags);
1132 tty->hw_stopped = 0;
1134 spin_unlock_irqrestore(&state->port->lock, flags);
1139 * No need to wake up processes in open wait, since they
1140 * sample the CLOCAL flag once, and don't recheck it.
1141 * XXX It's not clear whether the current behavior is correct
1142 * or not. Hence, this may change.....
1144 if (!(old_termios->c_cflag & CLOCAL) &&
1145 (tty->termios->c_cflag & CLOCAL))
1146 wake_up_interruptible(&state->info->open_wait);
1151 * In 2.4.5, calls to this will be serialized via the BKL in
1152 * linux/drivers/char/tty_io.c:tty_release()
1153 * linux/drivers/char/tty_io.c:do_tty_handup()
1155 static void uart_close(struct tty_struct *tty, struct file *filp)
1157 struct uart_state *state = tty->driver_data;
1158 struct uart_port *port;
1160 BUG_ON(!kernel_locked());
1162 if (!state || !state->port)
1167 DPRINTK("uart_close(%d) called\n", port->line);
1171 if (tty_hung_up_p(filp))
1174 if ((tty->count == 1) && (state->count != 1)) {
1176 * Uh, oh. tty->count is 1, which means that the tty
1177 * structure will be freed. state->count should always
1178 * be one in these conditions. If it's greater than
1179 * one, we've got real problems, since it means the
1180 * serial port won't be shutdown.
1182 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1183 "state->count is %d\n", state->count);
1186 if (--state->count < 0) {
1187 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1188 tty->name, state->count);
1195 * Now we wait for the transmit buffer to clear; and we notify
1196 * the line discipline to only process XON/XOFF characters by
1197 * setting tty->closing.
1201 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1202 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1205 * At this point, we stop accepting input. To do this, we
1206 * disable the receive line status interrupts.
1208 if (state->info->flags & UIF_INITIALIZED) {
1209 unsigned long flags;
1210 spin_lock_irqsave(&port->lock, flags);
1211 port->ops->stop_rx(port);
1212 spin_unlock_irqrestore(&port->lock, flags);
1214 * Before we drop DTR, make sure the UART transmitter
1215 * has completely drained; this is especially
1216 * important if there is a transmit FIFO!
1218 uart_wait_until_sent(tty, port->timeout);
1221 uart_shutdown(state);
1222 uart_flush_buffer(tty);
1224 tty_ldisc_flush(tty);
1227 state->info->tty = NULL;
1229 if (state->info->blocked_open) {
1230 if (state->close_delay)
1231 msleep_interruptible(state->close_delay);
1232 } else if (!uart_console(port)) {
1233 uart_change_pm(state, 3);
1237 * Wake up anyone trying to open this port.
1239 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1240 wake_up_interruptible(&state->info->open_wait);
1246 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1248 struct uart_state *state = tty->driver_data;
1249 struct uart_port *port = state->port;
1250 unsigned long char_time, expire;
1252 BUG_ON(!kernel_locked());
1254 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1258 * Set the check interval to be 1/5 of the estimated time to
1259 * send a single character, and make it at least 1. The check
1260 * interval should also be less than the timeout.
1262 * Note: we have to use pretty tight timings here to satisfy
1265 char_time = (port->timeout - HZ/50) / port->fifosize;
1266 char_time = char_time / 5;
1269 if (timeout && timeout < char_time)
1270 char_time = timeout;
1273 * If the transmitter hasn't cleared in twice the approximate
1274 * amount of time to send the entire FIFO, it probably won't
1275 * ever clear. This assumes the UART isn't doing flow
1276 * control, which is currently the case. Hence, if it ever
1277 * takes longer than port->timeout, this is probably due to a
1278 * UART bug of some kind. So, we clamp the timeout parameter at
1281 if (timeout == 0 || timeout > 2 * port->timeout)
1282 timeout = 2 * port->timeout;
1284 expire = jiffies + timeout;
1286 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1287 port->line, jiffies, expire);
1290 * Check whether the transmitter is empty every 'char_time'.
1291 * 'timeout' / 'expire' give us the maximum amount of time
1294 while (!port->ops->tx_empty(port)) {
1295 msleep_interruptible(jiffies_to_msecs(char_time));
1296 if (signal_pending(current))
1298 if (time_after(jiffies, expire))
1301 set_current_state(TASK_RUNNING); /* might not be needed */
1305 * This is called with the BKL held in
1306 * linux/drivers/char/tty_io.c:do_tty_hangup()
1307 * We're called from the eventd thread, so we can sleep for
1308 * a _short_ time only.
1310 static void uart_hangup(struct tty_struct *tty)
1312 struct uart_state *state = tty->driver_data;
1314 BUG_ON(!kernel_locked());
1315 DPRINTK("uart_hangup(%d)\n", state->port->line);
1318 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1319 uart_flush_buffer(tty);
1320 uart_shutdown(state);
1322 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1323 state->info->tty = NULL;
1324 wake_up_interruptible(&state->info->open_wait);
1325 wake_up_interruptible(&state->info->delta_msr_wait);
1331 * Copy across the serial console cflag setting into the termios settings
1332 * for the initial open of the port. This allows continuity between the
1333 * kernel settings, and the settings init adopts when it opens the port
1334 * for the first time.
1336 static void uart_update_termios(struct uart_state *state)
1338 struct tty_struct *tty = state->info->tty;
1339 struct uart_port *port = state->port;
1341 if (uart_console(port) && port->cons->cflag) {
1342 tty->termios->c_cflag = port->cons->cflag;
1343 port->cons->cflag = 0;
1347 * If the device failed to grab its irq resources,
1348 * or some other error occurred, don't try to talk
1349 * to the port hardware.
1351 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1353 * Make termios settings take effect.
1355 uart_change_speed(state, NULL);
1358 * And finally enable the RTS and DTR signals.
1360 if (tty->termios->c_cflag & CBAUD)
1361 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1366 * Block the open until the port is ready. We must be called with
1367 * the per-port semaphore held.
1370 uart_block_til_ready(struct file *filp, struct uart_state *state)
1372 DECLARE_WAITQUEUE(wait, current);
1373 struct uart_info *info = state->info;
1374 struct uart_port *port = state->port;
1377 info->blocked_open++;
1380 add_wait_queue(&info->open_wait, &wait);
1382 set_current_state(TASK_INTERRUPTIBLE);
1385 * If we have been hung up, tell userspace/restart open.
1387 if (tty_hung_up_p(filp) || info->tty == NULL)
1391 * If the port has been closed, tell userspace/restart open.
1393 if (!(info->flags & UIF_INITIALIZED))
1397 * If non-blocking mode is set, or CLOCAL mode is set,
1398 * we don't want to wait for the modem status lines to
1399 * indicate that the port is ready.
1401 * Also, if the port is not enabled/configured, we want
1402 * to allow the open to succeed here. Note that we will
1403 * have set TTY_IO_ERROR for a non-existant port.
1405 if ((filp->f_flags & O_NONBLOCK) ||
1406 (info->tty->termios->c_cflag & CLOCAL) ||
1407 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1412 * Set DTR to allow modem to know we're waiting. Do
1413 * not set RTS here - we want to make sure we catch
1414 * the data from the modem.
1416 if (info->tty->termios->c_cflag & CBAUD)
1417 uart_set_mctrl(port, TIOCM_DTR);
1420 * and wait for the carrier to indicate that the
1421 * modem is ready for us.
1423 spin_lock_irq(&port->lock);
1424 mctrl = port->ops->get_mctrl(port);
1425 spin_unlock_irq(&port->lock);
1426 if (mctrl & TIOCM_CAR)
1433 if (signal_pending(current))
1436 set_current_state(TASK_RUNNING);
1437 remove_wait_queue(&info->open_wait, &wait);
1440 info->blocked_open--;
1442 if (signal_pending(current))
1443 return -ERESTARTSYS;
1445 if (!info->tty || tty_hung_up_p(filp))
1451 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1453 struct uart_state *state;
1456 state = drv->state + line;
1457 if (down_interruptible(&state->sem)) {
1458 state = ERR_PTR(-ERESTARTSYS);
1466 state = ERR_PTR(-ENXIO);
1471 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1473 memset(state->info, 0, sizeof(struct uart_info));
1474 init_waitqueue_head(&state->info->open_wait);
1475 init_waitqueue_head(&state->info->delta_msr_wait);
1478 * Link the info into the other structures.
1480 state->port->info = state->info;
1482 tasklet_init(&state->info->tlet, uart_tasklet_action,
1483 (unsigned long)state);
1487 state = ERR_PTR(-ENOMEM);
1497 * In 2.4.5, calls to uart_open are serialised by the BKL in
1498 * linux/fs/devices.c:chrdev_open()
1499 * Note that if this fails, then uart_close() _will_ be called.
1501 * In time, we want to scrap the "opening nonpresent ports"
1502 * behaviour and implement an alternative way for setserial
1503 * to set base addresses/ports/types. This will allow us to
1504 * get rid of a certain amount of extra tests.
1506 static int uart_open(struct tty_struct *tty, struct file *filp)
1508 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1509 struct uart_state *state;
1510 int retval, line = tty->index;
1512 BUG_ON(!kernel_locked());
1513 DPRINTK("uart_open(%d) called\n", line);
1516 * tty->driver->num won't change, so we won't fail here with
1517 * tty->driver_data set to something non-NULL (and therefore
1518 * we won't get caught by uart_close()).
1521 if (line >= tty->driver->num)
1525 * We take the semaphore inside uart_get to guarantee that we won't
1526 * be re-entered while allocating the info structure, or while we
1527 * request any IRQs that the driver may need. This also has the nice
1528 * side-effect that it delays the action of uart_hangup, so we can
1529 * guarantee that info->tty will always contain something reasonable.
1531 state = uart_get(drv, line);
1532 if (IS_ERR(state)) {
1533 retval = PTR_ERR(state);
1538 * Once we set tty->driver_data here, we are guaranteed that
1539 * uart_close() will decrement the driver module use count.
1540 * Any failures from here onwards should not touch the count.
1542 tty->driver_data = state;
1543 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1545 state->info->tty = tty;
1548 * If the port is in the middle of closing, bail out now.
1550 if (tty_hung_up_p(filp)) {
1558 * Make sure the device is in D0 state.
1560 if (state->count == 1)
1561 uart_change_pm(state, 0);
1564 * Start up the serial port.
1566 retval = uart_startup(state, 0);
1569 * If we succeeded, wait until the port is ready.
1572 retval = uart_block_til_ready(filp, state);
1576 * If this is the first open to succeed, adjust things to suit.
1578 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1579 state->info->flags |= UIF_NORMAL_ACTIVE;
1581 uart_update_termios(state);
1588 static const char *uart_type(struct uart_port *port)
1590 const char *str = NULL;
1592 if (port->ops->type)
1593 str = port->ops->type(port);
1601 #ifdef CONFIG_PROC_FS
1603 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1605 struct uart_state *state = drv->state + i;
1606 struct uart_port *port = state->port;
1608 unsigned int status;
1614 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1615 port->line, uart_type(port),
1616 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1617 port->iotype == UPIO_MEM ? port->mapbase :
1618 (unsigned long) port->iobase,
1621 if (port->type == PORT_UNKNOWN) {
1626 if(capable(CAP_SYS_ADMIN))
1628 spin_lock_irq(&port->lock);
1629 status = port->ops->get_mctrl(port);
1630 spin_unlock_irq(&port->lock);
1632 ret += sprintf(buf + ret, " tx:%d rx:%d",
1633 port->icount.tx, port->icount.rx);
1634 if (port->icount.frame)
1635 ret += sprintf(buf + ret, " fe:%d",
1636 port->icount.frame);
1637 if (port->icount.parity)
1638 ret += sprintf(buf + ret, " pe:%d",
1639 port->icount.parity);
1640 if (port->icount.brk)
1641 ret += sprintf(buf + ret, " brk:%d",
1643 if (port->icount.overrun)
1644 ret += sprintf(buf + ret, " oe:%d",
1645 port->icount.overrun);
1647 #define INFOBIT(bit,str) \
1648 if (port->mctrl & (bit)) \
1649 strncat(stat_buf, (str), sizeof(stat_buf) - \
1650 strlen(stat_buf) - 2)
1651 #define STATBIT(bit,str) \
1652 if (status & (bit)) \
1653 strncat(stat_buf, (str), sizeof(stat_buf) - \
1654 strlen(stat_buf) - 2)
1658 INFOBIT(TIOCM_RTS, "|RTS");
1659 STATBIT(TIOCM_CTS, "|CTS");
1660 INFOBIT(TIOCM_DTR, "|DTR");
1661 STATBIT(TIOCM_DSR, "|DSR");
1662 STATBIT(TIOCM_CAR, "|CD");
1663 STATBIT(TIOCM_RNG, "|RI");
1666 strcat(stat_buf, "\n");
1668 ret += sprintf(buf + ret, stat_buf);
1678 static int uart_read_proc(char *page, char **start, off_t off,
1679 int count, int *eof, void *data)
1681 struct tty_driver *ttydrv = data;
1682 struct uart_driver *drv = ttydrv->driver_state;
1686 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1688 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1689 l = uart_line_info(page + len, drv, i);
1691 if (len + begin > off + count)
1693 if (len + begin < off) {
1700 if (off >= len + begin)
1702 *start = page + (off - begin);
1703 return (count < begin + len - off) ? count : (begin + len - off);
1707 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1709 * Check whether an invalid uart number has been specified, and
1710 * if so, search for the first available port that does have
1713 struct uart_port * __init
1714 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1716 int idx = co->index;
1718 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1719 ports[idx].membase == NULL))
1720 for (idx = 0; idx < nr; idx++)
1721 if (ports[idx].iobase != 0 ||
1722 ports[idx].membase != NULL)
1731 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1732 * @options: pointer to option string
1733 * @baud: pointer to an 'int' variable for the baud rate.
1734 * @parity: pointer to an 'int' variable for the parity.
1735 * @bits: pointer to an 'int' variable for the number of data bits.
1736 * @flow: pointer to an 'int' variable for the flow control character.
1738 * uart_parse_options decodes a string containing the serial console
1739 * options. The format of the string is <baud><parity><bits><flow>,
1743 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1747 *baud = simple_strtoul(s, NULL, 10);
1748 while (*s >= '0' && *s <= '9')
1763 static struct baud_rates baud_rates[] = {
1764 { 921600, B921600 },
1765 { 460800, B460800 },
1766 { 230400, B230400 },
1767 { 115200, B115200 },
1779 * uart_set_options - setup the serial console parameters
1780 * @port: pointer to the serial ports uart_port structure
1781 * @co: console pointer
1783 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1784 * @bits: number of data bits
1785 * @flow: flow control character - 'r' (rts)
1788 uart_set_options(struct uart_port *port, struct console *co,
1789 int baud, int parity, int bits, int flow)
1791 struct termios termios;
1794 memset(&termios, 0, sizeof(struct termios));
1796 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1799 * Construct a cflag setting.
1801 for (i = 0; baud_rates[i].rate; i++)
1802 if (baud_rates[i].rate <= baud)
1805 termios.c_cflag |= baud_rates[i].cflag;
1808 termios.c_cflag |= CS7;
1810 termios.c_cflag |= CS8;
1814 termios.c_cflag |= PARODD;
1817 termios.c_cflag |= PARENB;
1822 termios.c_cflag |= CRTSCTS;
1824 port->ops->set_termios(port, &termios, NULL);
1825 co->cflag = termios.c_cflag;
1829 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1831 static void uart_change_pm(struct uart_state *state, int pm_state)
1833 struct uart_port *port = state->port;
1835 port->ops->pm(port, pm_state, state->pm_state);
1836 state->pm_state = pm_state;
1839 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1841 struct uart_state *state = drv->state + port->line;
1845 if (state->info && state->info->flags & UIF_INITIALIZED) {
1846 struct uart_ops *ops = port->ops;
1848 spin_lock_irq(&port->lock);
1849 ops->stop_tx(port, 0);
1850 ops->set_mctrl(port, 0);
1852 spin_unlock_irq(&port->lock);
1855 * Wait for the transmitter to empty.
1857 while (!ops->tx_empty(port)) {
1861 ops->shutdown(port);
1865 * Disable the console device before suspending.
1867 if (uart_console(port))
1868 console_stop(port->cons);
1870 uart_change_pm(state, 3);
1877 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1879 struct uart_state *state = drv->state + port->line;
1883 uart_change_pm(state, 0);
1886 * Re-enable the console device after suspending.
1888 if (uart_console(port)) {
1889 struct termios termios;
1892 * First try to use the console cflag setting.
1894 memset(&termios, 0, sizeof(struct termios));
1895 termios.c_cflag = port->cons->cflag;
1898 * If that's unset, use the tty termios setting.
1900 if (state->info && state->info->tty && termios.c_cflag == 0)
1901 termios = *state->info->tty->termios;
1903 port->ops->set_termios(port, &termios, NULL);
1904 console_start(port->cons);
1907 if (state->info && state->info->flags & UIF_INITIALIZED) {
1908 struct uart_ops *ops = port->ops;
1910 ops->set_mctrl(port, 0);
1912 uart_change_speed(state, NULL);
1913 spin_lock_irq(&port->lock);
1914 ops->set_mctrl(port, port->mctrl);
1915 ops->start_tx(port, 0);
1916 spin_unlock_irq(&port->lock);
1925 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1927 printk("%s%d", drv->dev_name, port->line);
1929 switch (port->iotype) {
1931 printk("I/O 0x%x", port->iobase);
1934 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1938 printk("MMIO 0x%lx", port->mapbase);
1941 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
1945 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1946 struct uart_port *port)
1951 * If there isn't a port here, don't do anything further.
1953 if (!port->iobase && !port->mapbase && !port->membase)
1957 * Now do the auto configuration stuff. Note that config_port
1958 * is expected to claim the resources and map the port for us.
1960 flags = UART_CONFIG_TYPE;
1961 if (port->flags & UPF_AUTO_IRQ)
1962 flags |= UART_CONFIG_IRQ;
1963 if (port->flags & UPF_BOOT_AUTOCONF) {
1964 port->type = PORT_UNKNOWN;
1965 port->ops->config_port(port, flags);
1968 if (port->type != PORT_UNKNOWN) {
1969 unsigned long flags;
1971 uart_report_port(drv, port);
1974 * Ensure that the modem control lines are de-activated.
1975 * We probably don't need a spinlock around this, but
1977 spin_lock_irqsave(&port->lock, flags);
1978 port->ops->set_mctrl(port, 0);
1979 spin_unlock_irqrestore(&port->lock, flags);
1982 * Power down all ports by default, except the
1983 * console if we have one.
1985 if (!uart_console(port))
1986 uart_change_pm(state, 3);
1991 * This reverses the effects of uart_configure_port, hanging up the
1992 * port before removal.
1995 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
1997 struct uart_port *port = state->port;
1998 struct uart_info *info = state->info;
2000 if (info && info->tty)
2001 tty_vhangup(info->tty);
2008 * Free the port IO and memory resources, if any.
2010 if (port->type != PORT_UNKNOWN)
2011 port->ops->release_port(port);
2014 * Indicate that there isn't a port here anymore.
2016 port->type = PORT_UNKNOWN;
2019 * Kill the tasklet, and free resources.
2022 tasklet_kill(&info->tlet);
2029 static struct tty_operations uart_ops = {
2031 .close = uart_close,
2032 .write = uart_write,
2033 .put_char = uart_put_char,
2034 .flush_chars = uart_flush_chars,
2035 .write_room = uart_write_room,
2036 .chars_in_buffer= uart_chars_in_buffer,
2037 .flush_buffer = uart_flush_buffer,
2038 .ioctl = uart_ioctl,
2039 .throttle = uart_throttle,
2040 .unthrottle = uart_unthrottle,
2041 .send_xchar = uart_send_xchar,
2042 .set_termios = uart_set_termios,
2044 .start = uart_start,
2045 .hangup = uart_hangup,
2046 .break_ctl = uart_break_ctl,
2047 .wait_until_sent= uart_wait_until_sent,
2048 #ifdef CONFIG_PROC_FS
2049 .read_proc = uart_read_proc,
2051 .tiocmget = uart_tiocmget,
2052 .tiocmset = uart_tiocmset,
2056 * uart_register_driver - register a driver with the uart core layer
2057 * @drv: low level driver structure
2059 * Register a uart driver with the core driver. We in turn register
2060 * with the tty layer, and initialise the core driver per-port state.
2062 * We have a proc file in /proc/tty/driver which is named after the
2065 * drv->port should be NULL, and the per-port structures should be
2066 * registered using uart_add_one_port after this call has succeeded.
2068 int uart_register_driver(struct uart_driver *drv)
2070 struct tty_driver *normal = NULL;
2076 * Maybe we should be using a slab cache for this, especially if
2077 * we have a large number of ports to handle.
2079 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2084 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2086 normal = alloc_tty_driver(drv->nr);
2090 drv->tty_driver = normal;
2092 normal->owner = drv->owner;
2093 normal->driver_name = drv->driver_name;
2094 normal->devfs_name = drv->devfs_name;
2095 normal->name = drv->dev_name;
2096 normal->major = drv->major;
2097 normal->minor_start = drv->minor;
2098 normal->type = TTY_DRIVER_TYPE_SERIAL;
2099 normal->subtype = SERIAL_TYPE_NORMAL;
2100 normal->init_termios = tty_std_termios;
2101 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2102 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2103 normal->driver_state = drv;
2104 tty_set_operations(normal, &uart_ops);
2107 * Initialise the UART state(s).
2109 for (i = 0; i < drv->nr; i++) {
2110 struct uart_state *state = drv->state + i;
2112 state->close_delay = 500; /* .5 seconds */
2113 state->closing_wait = 30000; /* 30 seconds */
2115 init_MUTEX(&state->sem);
2118 retval = tty_register_driver(normal);
2121 put_tty_driver(normal);
2128 * uart_unregister_driver - remove a driver from the uart core layer
2129 * @drv: low level driver structure
2131 * Remove all references to a driver from the core driver. The low
2132 * level driver must have removed all its ports via the
2133 * uart_remove_one_port() if it registered them with uart_add_one_port().
2134 * (ie, drv->port == NULL)
2136 void uart_unregister_driver(struct uart_driver *drv)
2138 struct tty_driver *p = drv->tty_driver;
2139 tty_unregister_driver(p);
2142 drv->tty_driver = NULL;
2145 struct tty_driver *uart_console_device(struct console *co, int *index)
2147 struct uart_driver *p = co->data;
2149 return p->tty_driver;
2153 * uart_add_one_port - attach a driver-defined port structure
2154 * @drv: pointer to the uart low level driver structure for this port
2155 * @port: uart port structure to use for this port.
2157 * This allows the driver to register its own uart_port structure
2158 * with the core driver. The main purpose is to allow the low
2159 * level uart drivers to expand uart_port, rather than having yet
2160 * more levels of structures.
2162 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2164 struct uart_state *state;
2167 BUG_ON(in_interrupt());
2169 if (port->line >= drv->nr)
2172 state = drv->state + port->line;
2182 spin_lock_init(&port->lock);
2183 port->cons = drv->cons;
2184 port->info = state->info;
2186 uart_configure_port(drv, state, port);
2189 * Register the port whether it's detected or not. This allows
2190 * setserial to be used to alter this ports parameters.
2192 tty_register_device(drv->tty_driver, port->line, port->dev);
2195 * If this driver supports console, and it hasn't been
2196 * successfully registered yet, try to re-register it.
2197 * It may be that the port was not available.
2199 if (port->type != PORT_UNKNOWN &&
2200 port->cons && !(port->cons->flags & CON_ENABLED))
2201 register_console(port->cons);
2210 * uart_remove_one_port - detach a driver defined port structure
2211 * @drv: pointer to the uart low level driver structure for this port
2212 * @port: uart port structure for this port
2214 * This unhooks (and hangs up) the specified port structure from the
2215 * core driver. No further calls will be made to the low-level code
2218 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2220 struct uart_state *state = drv->state + port->line;
2222 BUG_ON(in_interrupt());
2224 if (state->port != port)
2225 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2231 * Remove the devices from devfs
2233 tty_unregister_device(drv->tty_driver, port->line);
2235 uart_unconfigure_port(drv, state);
2243 * Are the two ports equivalent?
2245 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2247 if (port1->iotype != port2->iotype)
2250 switch (port1->iotype) {
2252 return (port1->iobase == port2->iobase);
2254 return (port1->iobase == port2->iobase) &&
2255 (port1->hub6 == port2->hub6);
2257 return (port1->membase == port2->membase);
2261 EXPORT_SYMBOL(uart_match_port);
2264 * Try to find an unused uart_state slot for a port.
2266 static struct uart_state *
2267 uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2272 * First, find a port entry which matches. Note: if we do
2273 * find a matching entry, and it has a non-zero use count,
2274 * then we can't register the port.
2276 for (i = 0; i < drv->nr; i++)
2277 if (uart_match_port(drv->state[i].port, port))
2278 return &drv->state[i];
2281 * We didn't find a matching entry, so look for the first
2282 * free entry. We look for one which hasn't been previously
2283 * used (indicated by zero iobase).
2285 for (i = 0; i < drv->nr; i++)
2286 if (drv->state[i].port->type == PORT_UNKNOWN &&
2287 drv->state[i].port->iobase == 0 &&
2288 drv->state[i].count == 0)
2289 return &drv->state[i];
2292 * That also failed. Last resort is to find any currently
2293 * entry which doesn't have a real port associated with it.
2295 for (i = 0; i < drv->nr; i++)
2296 if (drv->state[i].port->type == PORT_UNKNOWN &&
2297 drv->state[i].count == 0)
2298 return &drv->state[i];
2304 * uart_register_port: register uart settings with a port
2305 * @drv: pointer to the uart low level driver structure for this port
2306 * @port: uart port structure describing the port
2308 * Register UART settings with the specified low level driver. Detect
2309 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2310 * IRQ if UPF_AUTO_IRQ is set.
2312 * We try to pick the same port for the same IO base address, so that
2313 * when a modem is plugged in, unplugged and plugged back in, it gets
2314 * allocated the same port.
2316 * Returns negative error, or positive line number.
2318 int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2320 struct uart_state *state;
2325 state = uart_find_match_or_unused(drv, port);
2329 * Ok, we've found a line that we can use.
2331 * If we find a port that matches this one, and it appears
2332 * to be in-use (even if it doesn't have a type) we shouldn't
2333 * alter it underneath itself - the port may be open and
2334 * trying to do useful work.
2336 if (uart_users(state) != 0) {
2342 * If the port is already initialised, don't touch it.
2344 if (state->port->type == PORT_UNKNOWN) {
2345 state->port->iobase = port->iobase;
2346 state->port->membase = port->membase;
2347 state->port->irq = port->irq;
2348 state->port->uartclk = port->uartclk;
2349 state->port->fifosize = port->fifosize;
2350 state->port->regshift = port->regshift;
2351 state->port->iotype = port->iotype;
2352 state->port->flags = port->flags;
2353 state->port->line = state - drv->state;
2354 state->port->mapbase = port->mapbase;
2356 uart_configure_port(drv, state, state->port);
2359 ret = state->port->line;
2368 * uart_unregister_port - de-allocate a port
2369 * @drv: pointer to the uart low level driver structure for this port
2370 * @line: line index previously returned from uart_register_port()
2372 * Hang up the specified line associated with the low level driver,
2373 * and mark the port as unused.
2375 void uart_unregister_port(struct uart_driver *drv, int line)
2377 struct uart_state *state;
2379 if (line < 0 || line >= drv->nr) {
2380 printk(KERN_ERR "Attempt to unregister ");
2381 printk("%s%d", drv->dev_name, line);
2386 state = drv->state + line;
2389 uart_unconfigure_port(drv, state);
2393 EXPORT_SYMBOL(uart_write_wakeup);
2394 EXPORT_SYMBOL(uart_register_driver);
2395 EXPORT_SYMBOL(uart_unregister_driver);
2396 EXPORT_SYMBOL(uart_suspend_port);
2397 EXPORT_SYMBOL(uart_resume_port);
2398 EXPORT_SYMBOL(uart_register_port);
2399 EXPORT_SYMBOL(uart_unregister_port);
2400 EXPORT_SYMBOL(uart_add_one_port);
2401 EXPORT_SYMBOL(uart_remove_one_port);
2403 MODULE_DESCRIPTION("Serial driver core");
2404 MODULE_LICENSE("GPL");