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);
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);
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
150 set_bit(TTY_IO_ERROR, &info->tty->flags);
152 if (port->type == PORT_UNKNOWN)
156 * Initialise and allocate the transmit and temporary
159 if (!info->xmit.buf) {
160 page = get_zeroed_page(GFP_KERNEL);
164 info->xmit.buf = (unsigned char *) page;
165 uart_circ_clear(&info->xmit);
168 retval = port->ops->startup(port);
172 * Initialise the hardware port settings.
174 uart_change_speed(state, NULL);
177 * Setup the RTS and DTR signals once the
178 * port is open and ready to respond.
180 if (info->tty->termios->c_cflag & CBAUD)
181 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
184 if (info->flags & UIF_CTS_FLOW) {
185 spin_lock_irq(&port->lock);
186 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
187 info->tty->hw_stopped = 1;
188 spin_unlock_irq(&port->lock);
191 info->flags |= UIF_INITIALIZED;
193 clear_bit(TTY_IO_ERROR, &info->tty->flags);
196 if (retval && capable(CAP_SYS_ADMIN))
203 * This routine will shutdown a serial port; interrupts are disabled, and
204 * DTR is dropped if the hangup on close termio flag is on. Calls to
205 * uart_shutdown are serialised by the per-port semaphore.
207 static void uart_shutdown(struct uart_state *state)
209 struct uart_info *info = state->info;
210 struct uart_port *port = state->port;
212 if (!(info->flags & UIF_INITIALIZED))
216 * Turn off DTR and RTS early.
218 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
219 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
222 * clear delta_msr_wait queue to avoid mem leaks: we may free
223 * the irq here so the queue might never be woken up. Note
224 * that we won't end up waiting on delta_msr_wait again since
225 * any outstanding file descriptors should be pointing at
226 * hung_up_tty_fops now.
228 wake_up_interruptible(&info->delta_msr_wait);
231 * Free the IRQ and disable the port.
233 port->ops->shutdown(port);
236 * Ensure that the IRQ handler isn't running on another CPU.
238 synchronize_irq(port->irq);
241 * Free the transmit buffer page.
243 if (info->xmit.buf) {
244 free_page((unsigned long)info->xmit.buf);
245 info->xmit.buf = NULL;
249 * kill off our tasklet
251 tasklet_kill(&info->tlet);
253 set_bit(TTY_IO_ERROR, &info->tty->flags);
255 info->flags &= ~UIF_INITIALIZED;
259 * uart_update_timeout - update per-port FIFO timeout.
260 * @port: uart_port structure describing the port
261 * @cflag: termios cflag value
262 * @baud: speed of the port
264 * Set the port FIFO timeout value. The @cflag value should
265 * reflect the actual hardware settings.
268 uart_update_timeout(struct uart_port *port, unsigned int cflag,
273 /* byte size and parity */
274 switch (cflag & CSIZE) {
295 * The total number of bits to be transmitted in the fifo.
297 bits = bits * port->fifosize;
300 * Figure the timeout to send the above number of bits.
301 * Add .02 seconds of slop
303 port->timeout = (HZ * bits) / baud + HZ/50;
306 EXPORT_SYMBOL(uart_update_timeout);
309 * uart_get_baud_rate - return baud rate for a particular port
310 * @port: uart_port structure describing the port in question.
311 * @termios: desired termios settings.
312 * @old: old termios (or NULL)
313 * @min: minimum acceptable baud rate
314 * @max: maximum acceptable baud rate
316 * Decode the termios structure into a numeric baud rate,
317 * taking account of the magic 38400 baud rate (with spd_*
318 * flags), and mapping the %B0 rate to 9600 baud.
320 * If the new baud rate is invalid, try the old termios setting.
321 * If it's still invalid, we try 9600 baud.
323 * Update the @termios structure to reflect the baud rate
324 * we're actually going to be using.
327 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
328 struct termios *old, unsigned int min, unsigned int max)
330 unsigned int try, baud, altbaud = 38400;
331 unsigned int flags = port->flags & UPF_SPD_MASK;
333 if (flags == UPF_SPD_HI)
335 if (flags == UPF_SPD_VHI)
337 if (flags == UPF_SPD_SHI)
339 if (flags == UPF_SPD_WARP)
342 for (try = 0; try < 2; try++) {
343 baud = tty_termios_baud_rate(termios);
346 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
353 * Special case: B0 rate.
358 if (baud >= min && baud <= max)
362 * Oops, the quotient was zero. Try again with
363 * the old baud rate if possible.
365 termios->c_cflag &= ~CBAUD;
367 termios->c_cflag |= old->c_cflag & CBAUD;
373 * As a last resort, if the quotient is zero,
374 * default to 9600 bps
376 termios->c_cflag |= B9600;
382 EXPORT_SYMBOL(uart_get_baud_rate);
385 * uart_get_divisor - return uart clock divisor
386 * @port: uart_port structure describing the port.
387 * @baud: desired baud rate
389 * Calculate the uart clock divisor for the port.
392 uart_get_divisor(struct uart_port *port, unsigned int baud)
397 * Old custom speed handling.
399 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
400 quot = port->custom_divisor;
402 quot = (port->uartclk + (8 * baud)) / (16 * baud);
407 EXPORT_SYMBOL(uart_get_divisor);
410 uart_change_speed(struct uart_state *state, struct termios *old_termios)
412 struct tty_struct *tty = state->info->tty;
413 struct uart_port *port = state->port;
414 struct termios *termios;
417 * If we have no tty, termios, or the port does not exist,
418 * then we can't set the parameters for this port.
420 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
423 termios = tty->termios;
426 * Set flags based on termios cflag
428 if (termios->c_cflag & CRTSCTS)
429 state->info->flags |= UIF_CTS_FLOW;
431 state->info->flags &= ~UIF_CTS_FLOW;
433 if (termios->c_cflag & CLOCAL)
434 state->info->flags &= ~UIF_CHECK_CD;
436 state->info->flags |= UIF_CHECK_CD;
438 port->ops->set_termios(port, termios, old_termios);
442 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
449 spin_lock_irqsave(&port->lock, flags);
450 if (uart_circ_chars_free(circ) != 0) {
451 circ->buf[circ->head] = c;
452 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
454 spin_unlock_irqrestore(&port->lock, flags);
457 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
459 struct uart_state *state = tty->driver_data;
461 __uart_put_char(state->port, &state->info->xmit, ch);
464 static void uart_flush_chars(struct tty_struct *tty)
470 uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
472 struct uart_state *state = tty->driver_data;
473 struct uart_port *port = state->port;
474 struct circ_buf *circ = &state->info->xmit;
481 spin_lock_irqsave(&port->lock, flags);
483 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
488 memcpy(circ->buf + circ->head, buf, c);
489 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
494 spin_unlock_irqrestore(&port->lock, flags);
500 static int uart_write_room(struct tty_struct *tty)
502 struct uart_state *state = tty->driver_data;
504 return uart_circ_chars_free(&state->info->xmit);
507 static int uart_chars_in_buffer(struct tty_struct *tty)
509 struct uart_state *state = tty->driver_data;
511 return uart_circ_chars_pending(&state->info->xmit);
514 static void uart_flush_buffer(struct tty_struct *tty)
516 struct uart_state *state = tty->driver_data;
517 struct uart_port *port = state->port;
520 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
522 spin_lock_irqsave(&port->lock, flags);
523 uart_circ_clear(&state->info->xmit);
524 spin_unlock_irqrestore(&port->lock, flags);
529 * This function is used to send a high-priority XON/XOFF character to
532 static void uart_send_xchar(struct tty_struct *tty, char ch)
534 struct uart_state *state = tty->driver_data;
535 struct uart_port *port = state->port;
538 if (port->ops->send_xchar)
539 port->ops->send_xchar(port, ch);
543 spin_lock_irqsave(&port->lock, flags);
544 port->ops->start_tx(port);
545 spin_unlock_irqrestore(&port->lock, flags);
550 static void uart_throttle(struct tty_struct *tty)
552 struct uart_state *state = tty->driver_data;
555 uart_send_xchar(tty, STOP_CHAR(tty));
557 if (tty->termios->c_cflag & CRTSCTS)
558 uart_clear_mctrl(state->port, TIOCM_RTS);
561 static void uart_unthrottle(struct tty_struct *tty)
563 struct uart_state *state = tty->driver_data;
564 struct uart_port *port = state->port;
570 uart_send_xchar(tty, START_CHAR(tty));
573 if (tty->termios->c_cflag & CRTSCTS)
574 uart_set_mctrl(port, TIOCM_RTS);
577 static int uart_get_info(struct uart_state *state,
578 struct serial_struct __user *retinfo)
580 struct uart_port *port = state->port;
581 struct serial_struct tmp;
583 memset(&tmp, 0, sizeof(tmp));
584 tmp.type = port->type;
585 tmp.line = port->line;
586 tmp.port = port->iobase;
587 if (HIGH_BITS_OFFSET)
588 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
590 tmp.flags = port->flags;
591 tmp.xmit_fifo_size = port->fifosize;
592 tmp.baud_base = port->uartclk / 16;
593 tmp.close_delay = state->close_delay / 10;
594 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
595 ASYNC_CLOSING_WAIT_NONE :
596 state->closing_wait / 10;
597 tmp.custom_divisor = port->custom_divisor;
598 tmp.hub6 = port->hub6;
599 tmp.io_type = port->iotype;
600 tmp.iomem_reg_shift = port->regshift;
601 tmp.iomem_base = (void *)port->mapbase;
603 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
608 static int uart_set_info(struct uart_state *state,
609 struct serial_struct __user *newinfo)
611 struct serial_struct new_serial;
612 struct uart_port *port = state->port;
613 unsigned long new_port;
614 unsigned int change_irq, change_port, old_flags, closing_wait;
615 unsigned int old_custom_divisor, close_delay;
618 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
621 new_port = new_serial.port;
622 if (HIGH_BITS_OFFSET)
623 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
625 new_serial.irq = irq_canonicalize(new_serial.irq);
626 close_delay = new_serial.close_delay * 10;
627 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
628 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
631 * This semaphore protects state->count. It is also
632 * very useful to prevent opens. Also, take the
633 * port configuration semaphore to make sure that a
634 * module insertion/removal doesn't change anything
639 change_irq = new_serial.irq != port->irq;
642 * Since changing the 'type' of the port changes its resource
643 * allocations, we should treat type changes the same as
646 change_port = new_port != port->iobase ||
647 (unsigned long)new_serial.iomem_base != port->mapbase ||
648 new_serial.hub6 != port->hub6 ||
649 new_serial.io_type != port->iotype ||
650 new_serial.iomem_reg_shift != port->regshift ||
651 new_serial.type != port->type;
653 old_flags = port->flags;
654 old_custom_divisor = port->custom_divisor;
656 if (!capable(CAP_SYS_ADMIN)) {
658 if (change_irq || change_port ||
659 (new_serial.baud_base != port->uartclk / 16) ||
660 (close_delay != state->close_delay) ||
661 (closing_wait != state->closing_wait) ||
662 (new_serial.xmit_fifo_size != port->fifosize) ||
663 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
665 port->flags = ((port->flags & ~UPF_USR_MASK) |
666 (new_serial.flags & UPF_USR_MASK));
667 port->custom_divisor = new_serial.custom_divisor;
672 * Ask the low level driver to verify the settings.
674 if (port->ops->verify_port)
675 retval = port->ops->verify_port(port, &new_serial);
677 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
678 (new_serial.baud_base < 9600))
684 if (change_port || change_irq) {
688 * Make sure that we are the sole user of this port.
690 if (uart_users(state) > 1)
694 * We need to shutdown the serial port at the old
695 * port/type/irq combination.
697 uart_shutdown(state);
701 unsigned long old_iobase, old_mapbase;
702 unsigned int old_type, old_iotype, old_hub6, old_shift;
704 old_iobase = port->iobase;
705 old_mapbase = port->mapbase;
706 old_type = port->type;
707 old_hub6 = port->hub6;
708 old_iotype = port->iotype;
709 old_shift = port->regshift;
712 * Free and release old regions
714 if (old_type != PORT_UNKNOWN)
715 port->ops->release_port(port);
717 port->iobase = new_port;
718 port->type = new_serial.type;
719 port->hub6 = new_serial.hub6;
720 port->iotype = new_serial.io_type;
721 port->regshift = new_serial.iomem_reg_shift;
722 port->mapbase = (unsigned long)new_serial.iomem_base;
725 * Claim and map the new regions
727 if (port->type != PORT_UNKNOWN) {
728 retval = port->ops->request_port(port);
730 /* Always success - Jean II */
735 * If we fail to request resources for the
736 * new port, try to restore the old settings.
738 if (retval && old_type != PORT_UNKNOWN) {
739 port->iobase = old_iobase;
740 port->type = old_type;
741 port->hub6 = old_hub6;
742 port->iotype = old_iotype;
743 port->regshift = old_shift;
744 port->mapbase = old_mapbase;
745 retval = port->ops->request_port(port);
747 * If we failed to restore the old settings,
751 port->type = PORT_UNKNOWN;
760 port->irq = new_serial.irq;
761 port->uartclk = new_serial.baud_base * 16;
762 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
763 (new_serial.flags & UPF_CHANGE_MASK);
764 port->custom_divisor = new_serial.custom_divisor;
765 state->close_delay = close_delay;
766 state->closing_wait = closing_wait;
767 port->fifosize = new_serial.xmit_fifo_size;
768 if (state->info->tty)
769 state->info->tty->low_latency =
770 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
774 if (port->type == PORT_UNKNOWN)
776 if (state->info->flags & UIF_INITIALIZED) {
777 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
778 old_custom_divisor != port->custom_divisor) {
780 * If they're setting up a custom divisor or speed,
781 * instead of clearing it, then bitch about it. No
782 * need to rate-limit; it's CAP_SYS_ADMIN only.
784 if (port->flags & UPF_SPD_MASK) {
787 "%s sets custom speed on %s. This "
788 "is deprecated.\n", current->comm,
789 tty_name(state->info->tty, buf));
791 uart_change_speed(state, NULL);
794 retval = uart_startup(state, 1);
802 * uart_get_lsr_info - get line status register info.
803 * Note: uart_ioctl protects us against hangups.
805 static int uart_get_lsr_info(struct uart_state *state,
806 unsigned int __user *value)
808 struct uart_port *port = state->port;
811 result = port->ops->tx_empty(port);
814 * If we're about to load something into the transmit
815 * register, we'll pretend the transmitter isn't empty to
816 * avoid a race condition (depending on when the transmit
817 * interrupt happens).
820 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
821 !state->info->tty->stopped && !state->info->tty->hw_stopped))
822 result &= ~TIOCSER_TEMT;
824 return put_user(result, value);
827 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
829 struct uart_state *state = tty->driver_data;
830 struct uart_port *port = state->port;
834 if ((!file || !tty_hung_up_p(file)) &&
835 !(tty->flags & (1 << TTY_IO_ERROR))) {
836 result = port->mctrl;
838 spin_lock_irq(&port->lock);
839 result |= port->ops->get_mctrl(port);
840 spin_unlock_irq(&port->lock);
848 uart_tiocmset(struct tty_struct *tty, struct file *file,
849 unsigned int set, unsigned int clear)
851 struct uart_state *state = tty->driver_data;
852 struct uart_port *port = state->port;
856 if ((!file || !tty_hung_up_p(file)) &&
857 !(tty->flags & (1 << TTY_IO_ERROR))) {
858 uart_update_mctrl(port, set, clear);
865 static void uart_break_ctl(struct tty_struct *tty, int break_state)
867 struct uart_state *state = tty->driver_data;
868 struct uart_port *port = state->port;
870 BUG_ON(!kernel_locked());
874 if (port->type != PORT_UNKNOWN)
875 port->ops->break_ctl(port, break_state);
880 static int uart_do_autoconfig(struct uart_state *state)
882 struct uart_port *port = state->port;
885 if (!capable(CAP_SYS_ADMIN))
889 * Take the per-port semaphore. This prevents count from
890 * changing, and hence any extra opens of the port while
891 * we're auto-configuring.
893 if (down_interruptible(&state->sem))
897 if (uart_users(state) == 1) {
898 uart_shutdown(state);
901 * If we already have a port type configured,
902 * we must release its resources.
904 if (port->type != PORT_UNKNOWN)
905 port->ops->release_port(port);
907 flags = UART_CONFIG_TYPE;
908 if (port->flags & UPF_AUTO_IRQ)
909 flags |= UART_CONFIG_IRQ;
912 * This will claim the ports resources if
915 port->ops->config_port(port, flags);
917 ret = uart_startup(state, 1);
924 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
925 * - mask passed in arg for lines of interest
926 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
927 * Caller should use TIOCGICOUNT to see which one it was
930 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
932 struct uart_port *port = state->port;
933 DECLARE_WAITQUEUE(wait, current);
934 struct uart_icount cprev, cnow;
938 * note the counters on entry
940 spin_lock_irq(&port->lock);
941 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
944 * Force modem status interrupts on
946 port->ops->enable_ms(port);
947 spin_unlock_irq(&port->lock);
949 add_wait_queue(&state->info->delta_msr_wait, &wait);
951 spin_lock_irq(&port->lock);
952 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
953 spin_unlock_irq(&port->lock);
955 set_current_state(TASK_INTERRUPTIBLE);
957 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
958 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
959 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
960 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
967 /* see if a signal did it */
968 if (signal_pending(current)) {
976 current->state = TASK_RUNNING;
977 remove_wait_queue(&state->info->delta_msr_wait, &wait);
983 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
984 * Return: write counters to the user passed counter struct
985 * NB: both 1->0 and 0->1 transitions are counted except for
986 * RI where only 0->1 is counted.
988 static int uart_get_count(struct uart_state *state,
989 struct serial_icounter_struct __user *icnt)
991 struct serial_icounter_struct icount;
992 struct uart_icount cnow;
993 struct uart_port *port = state->port;
995 spin_lock_irq(&port->lock);
996 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
997 spin_unlock_irq(&port->lock);
999 icount.cts = cnow.cts;
1000 icount.dsr = cnow.dsr;
1001 icount.rng = cnow.rng;
1002 icount.dcd = cnow.dcd;
1003 icount.rx = cnow.rx;
1004 icount.tx = cnow.tx;
1005 icount.frame = cnow.frame;
1006 icount.overrun = cnow.overrun;
1007 icount.parity = cnow.parity;
1008 icount.brk = cnow.brk;
1009 icount.buf_overrun = cnow.buf_overrun;
1011 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1015 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1018 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1021 struct uart_state *state = tty->driver_data;
1022 void __user *uarg = (void __user *)arg;
1023 int ret = -ENOIOCTLCMD;
1025 BUG_ON(!kernel_locked());
1028 * These ioctls don't rely on the hardware to be present.
1032 ret = uart_get_info(state, uarg);
1036 ret = uart_set_info(state, uarg);
1040 ret = uart_do_autoconfig(state);
1043 case TIOCSERGWILD: /* obsolete */
1044 case TIOCSERSWILD: /* obsolete */
1049 if (ret != -ENOIOCTLCMD)
1052 if (tty->flags & (1 << TTY_IO_ERROR)) {
1058 * The following should only be used when hardware is present.
1062 ret = uart_wait_modem_status(state, arg);
1066 ret = uart_get_count(state, uarg);
1070 if (ret != -ENOIOCTLCMD)
1075 if (tty_hung_up_p(filp)) {
1081 * All these rely on hardware being present and need to be
1082 * protected against the tty being hung up.
1085 case TIOCSERGETLSR: /* Get line status register */
1086 ret = uart_get_lsr_info(state, uarg);
1090 struct uart_port *port = state->port;
1091 if (port->ops->ioctl)
1092 ret = port->ops->ioctl(port, cmd, arg);
1102 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1104 struct uart_state *state = tty->driver_data;
1105 unsigned long flags;
1106 unsigned int cflag = tty->termios->c_cflag;
1108 BUG_ON(!kernel_locked());
1111 * These are the bits that are used to setup various
1112 * flags in the low level driver.
1114 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1116 if ((cflag ^ old_termios->c_cflag) == 0 &&
1117 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1120 uart_change_speed(state, old_termios);
1122 /* Handle transition to B0 status */
1123 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1124 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1126 /* Handle transition away from B0 status */
1127 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1128 unsigned int mask = TIOCM_DTR;
1129 if (!(cflag & CRTSCTS) ||
1130 !test_bit(TTY_THROTTLED, &tty->flags))
1132 uart_set_mctrl(state->port, mask);
1135 /* Handle turning off CRTSCTS */
1136 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1137 spin_lock_irqsave(&state->port->lock, flags);
1138 tty->hw_stopped = 0;
1140 spin_unlock_irqrestore(&state->port->lock, flags);
1143 /* Handle turning on CRTSCTS */
1144 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1145 spin_lock_irqsave(&state->port->lock, flags);
1146 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1147 tty->hw_stopped = 1;
1148 state->port->ops->stop_tx(state->port);
1150 spin_unlock_irqrestore(&state->port->lock, flags);
1155 * No need to wake up processes in open wait, since they
1156 * sample the CLOCAL flag once, and don't recheck it.
1157 * XXX It's not clear whether the current behavior is correct
1158 * or not. Hence, this may change.....
1160 if (!(old_termios->c_cflag & CLOCAL) &&
1161 (tty->termios->c_cflag & CLOCAL))
1162 wake_up_interruptible(&state->info->open_wait);
1167 * In 2.4.5, calls to this will be serialized via the BKL in
1168 * linux/drivers/char/tty_io.c:tty_release()
1169 * linux/drivers/char/tty_io.c:do_tty_handup()
1171 static void uart_close(struct tty_struct *tty, struct file *filp)
1173 struct uart_state *state = tty->driver_data;
1174 struct uart_port *port;
1176 BUG_ON(!kernel_locked());
1178 if (!state || !state->port)
1183 DPRINTK("uart_close(%d) called\n", port->line);
1187 if (tty_hung_up_p(filp))
1190 if ((tty->count == 1) && (state->count != 1)) {
1192 * Uh, oh. tty->count is 1, which means that the tty
1193 * structure will be freed. state->count should always
1194 * be one in these conditions. If it's greater than
1195 * one, we've got real problems, since it means the
1196 * serial port won't be shutdown.
1198 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1199 "state->count is %d\n", state->count);
1202 if (--state->count < 0) {
1203 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1204 tty->name, state->count);
1211 * Now we wait for the transmit buffer to clear; and we notify
1212 * the line discipline to only process XON/XOFF characters by
1213 * setting tty->closing.
1217 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1218 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1221 * At this point, we stop accepting input. To do this, we
1222 * disable the receive line status interrupts.
1224 if (state->info->flags & UIF_INITIALIZED) {
1225 unsigned long flags;
1226 spin_lock_irqsave(&port->lock, flags);
1227 port->ops->stop_rx(port);
1228 spin_unlock_irqrestore(&port->lock, flags);
1230 * Before we drop DTR, make sure the UART transmitter
1231 * has completely drained; this is especially
1232 * important if there is a transmit FIFO!
1234 uart_wait_until_sent(tty, port->timeout);
1237 uart_shutdown(state);
1238 uart_flush_buffer(tty);
1240 tty_ldisc_flush(tty);
1243 state->info->tty = NULL;
1245 if (state->info->blocked_open) {
1246 if (state->close_delay)
1247 msleep_interruptible(state->close_delay);
1248 } else if (!uart_console(port)) {
1249 uart_change_pm(state, 3);
1253 * Wake up anyone trying to open this port.
1255 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1256 wake_up_interruptible(&state->info->open_wait);
1262 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1264 struct uart_state *state = tty->driver_data;
1265 struct uart_port *port = state->port;
1266 unsigned long char_time, expire;
1268 BUG_ON(!kernel_locked());
1270 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1274 * Set the check interval to be 1/5 of the estimated time to
1275 * send a single character, and make it at least 1. The check
1276 * interval should also be less than the timeout.
1278 * Note: we have to use pretty tight timings here to satisfy
1281 char_time = (port->timeout - HZ/50) / port->fifosize;
1282 char_time = char_time / 5;
1285 if (timeout && timeout < char_time)
1286 char_time = timeout;
1289 * If the transmitter hasn't cleared in twice the approximate
1290 * amount of time to send the entire FIFO, it probably won't
1291 * ever clear. This assumes the UART isn't doing flow
1292 * control, which is currently the case. Hence, if it ever
1293 * takes longer than port->timeout, this is probably due to a
1294 * UART bug of some kind. So, we clamp the timeout parameter at
1297 if (timeout == 0 || timeout > 2 * port->timeout)
1298 timeout = 2 * port->timeout;
1300 expire = jiffies + timeout;
1302 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1303 port->line, jiffies, expire);
1306 * Check whether the transmitter is empty every 'char_time'.
1307 * 'timeout' / 'expire' give us the maximum amount of time
1310 while (!port->ops->tx_empty(port)) {
1311 msleep_interruptible(jiffies_to_msecs(char_time));
1312 if (signal_pending(current))
1314 if (time_after(jiffies, expire))
1317 set_current_state(TASK_RUNNING); /* might not be needed */
1321 * This is called with the BKL held in
1322 * linux/drivers/char/tty_io.c:do_tty_hangup()
1323 * We're called from the eventd thread, so we can sleep for
1324 * a _short_ time only.
1326 static void uart_hangup(struct tty_struct *tty)
1328 struct uart_state *state = tty->driver_data;
1330 BUG_ON(!kernel_locked());
1331 DPRINTK("uart_hangup(%d)\n", state->port->line);
1334 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1335 uart_flush_buffer(tty);
1336 uart_shutdown(state);
1338 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1339 state->info->tty = NULL;
1340 wake_up_interruptible(&state->info->open_wait);
1341 wake_up_interruptible(&state->info->delta_msr_wait);
1347 * Copy across the serial console cflag setting into the termios settings
1348 * for the initial open of the port. This allows continuity between the
1349 * kernel settings, and the settings init adopts when it opens the port
1350 * for the first time.
1352 static void uart_update_termios(struct uart_state *state)
1354 struct tty_struct *tty = state->info->tty;
1355 struct uart_port *port = state->port;
1357 if (uart_console(port) && port->cons->cflag) {
1358 tty->termios->c_cflag = port->cons->cflag;
1359 port->cons->cflag = 0;
1363 * If the device failed to grab its irq resources,
1364 * or some other error occurred, don't try to talk
1365 * to the port hardware.
1367 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1369 * Make termios settings take effect.
1371 uart_change_speed(state, NULL);
1374 * And finally enable the RTS and DTR signals.
1376 if (tty->termios->c_cflag & CBAUD)
1377 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1382 * Block the open until the port is ready. We must be called with
1383 * the per-port semaphore held.
1386 uart_block_til_ready(struct file *filp, struct uart_state *state)
1388 DECLARE_WAITQUEUE(wait, current);
1389 struct uart_info *info = state->info;
1390 struct uart_port *port = state->port;
1393 info->blocked_open++;
1396 add_wait_queue(&info->open_wait, &wait);
1398 set_current_state(TASK_INTERRUPTIBLE);
1401 * If we have been hung up, tell userspace/restart open.
1403 if (tty_hung_up_p(filp) || info->tty == NULL)
1407 * If the port has been closed, tell userspace/restart open.
1409 if (!(info->flags & UIF_INITIALIZED))
1413 * If non-blocking mode is set, or CLOCAL mode is set,
1414 * we don't want to wait for the modem status lines to
1415 * indicate that the port is ready.
1417 * Also, if the port is not enabled/configured, we want
1418 * to allow the open to succeed here. Note that we will
1419 * have set TTY_IO_ERROR for a non-existant port.
1421 if ((filp->f_flags & O_NONBLOCK) ||
1422 (info->tty->termios->c_cflag & CLOCAL) ||
1423 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1428 * Set DTR to allow modem to know we're waiting. Do
1429 * not set RTS here - we want to make sure we catch
1430 * the data from the modem.
1432 if (info->tty->termios->c_cflag & CBAUD)
1433 uart_set_mctrl(port, TIOCM_DTR);
1436 * and wait for the carrier to indicate that the
1437 * modem is ready for us.
1439 spin_lock_irq(&port->lock);
1440 mctrl = port->ops->get_mctrl(port);
1441 spin_unlock_irq(&port->lock);
1442 if (mctrl & TIOCM_CAR)
1449 if (signal_pending(current))
1452 set_current_state(TASK_RUNNING);
1453 remove_wait_queue(&info->open_wait, &wait);
1456 info->blocked_open--;
1458 if (signal_pending(current))
1459 return -ERESTARTSYS;
1461 if (!info->tty || tty_hung_up_p(filp))
1467 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1469 struct uart_state *state;
1472 state = drv->state + line;
1473 if (down_interruptible(&state->sem)) {
1474 state = ERR_PTR(-ERESTARTSYS);
1482 state = ERR_PTR(-ENXIO);
1487 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1489 memset(state->info, 0, sizeof(struct uart_info));
1490 init_waitqueue_head(&state->info->open_wait);
1491 init_waitqueue_head(&state->info->delta_msr_wait);
1494 * Link the info into the other structures.
1496 state->port->info = state->info;
1498 tasklet_init(&state->info->tlet, uart_tasklet_action,
1499 (unsigned long)state);
1503 state = ERR_PTR(-ENOMEM);
1513 * In 2.4.5, calls to uart_open are serialised by the BKL in
1514 * linux/fs/devices.c:chrdev_open()
1515 * Note that if this fails, then uart_close() _will_ be called.
1517 * In time, we want to scrap the "opening nonpresent ports"
1518 * behaviour and implement an alternative way for setserial
1519 * to set base addresses/ports/types. This will allow us to
1520 * get rid of a certain amount of extra tests.
1522 static int uart_open(struct tty_struct *tty, struct file *filp)
1524 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1525 struct uart_state *state;
1526 int retval, line = tty->index;
1528 BUG_ON(!kernel_locked());
1529 DPRINTK("uart_open(%d) called\n", line);
1532 * tty->driver->num won't change, so we won't fail here with
1533 * tty->driver_data set to something non-NULL (and therefore
1534 * we won't get caught by uart_close()).
1537 if (line >= tty->driver->num)
1541 * We take the semaphore inside uart_get to guarantee that we won't
1542 * be re-entered while allocating the info structure, or while we
1543 * request any IRQs that the driver may need. This also has the nice
1544 * side-effect that it delays the action of uart_hangup, so we can
1545 * guarantee that info->tty will always contain something reasonable.
1547 state = uart_get(drv, line);
1548 if (IS_ERR(state)) {
1549 retval = PTR_ERR(state);
1554 * Once we set tty->driver_data here, we are guaranteed that
1555 * uart_close() will decrement the driver module use count.
1556 * Any failures from here onwards should not touch the count.
1558 tty->driver_data = state;
1559 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1561 state->info->tty = tty;
1564 * If the port is in the middle of closing, bail out now.
1566 if (tty_hung_up_p(filp)) {
1574 * Make sure the device is in D0 state.
1576 if (state->count == 1)
1577 uart_change_pm(state, 0);
1580 * Start up the serial port.
1582 retval = uart_startup(state, 0);
1585 * If we succeeded, wait until the port is ready.
1588 retval = uart_block_til_ready(filp, state);
1592 * If this is the first open to succeed, adjust things to suit.
1594 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1595 state->info->flags |= UIF_NORMAL_ACTIVE;
1597 uart_update_termios(state);
1604 static const char *uart_type(struct uart_port *port)
1606 const char *str = NULL;
1608 if (port->ops->type)
1609 str = port->ops->type(port);
1617 #ifdef CONFIG_PROC_FS
1619 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1621 struct uart_state *state = drv->state + i;
1622 struct uart_port *port = state->port;
1624 unsigned int status;
1630 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1631 port->line, uart_type(port),
1632 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1633 port->iotype == UPIO_MEM ? port->mapbase :
1634 (unsigned long) port->iobase,
1637 if (port->type == PORT_UNKNOWN) {
1642 if(capable(CAP_SYS_ADMIN))
1644 spin_lock_irq(&port->lock);
1645 status = port->ops->get_mctrl(port);
1646 spin_unlock_irq(&port->lock);
1648 ret += sprintf(buf + ret, " tx:%d rx:%d",
1649 port->icount.tx, port->icount.rx);
1650 if (port->icount.frame)
1651 ret += sprintf(buf + ret, " fe:%d",
1652 port->icount.frame);
1653 if (port->icount.parity)
1654 ret += sprintf(buf + ret, " pe:%d",
1655 port->icount.parity);
1656 if (port->icount.brk)
1657 ret += sprintf(buf + ret, " brk:%d",
1659 if (port->icount.overrun)
1660 ret += sprintf(buf + ret, " oe:%d",
1661 port->icount.overrun);
1663 #define INFOBIT(bit,str) \
1664 if (port->mctrl & (bit)) \
1665 strncat(stat_buf, (str), sizeof(stat_buf) - \
1666 strlen(stat_buf) - 2)
1667 #define STATBIT(bit,str) \
1668 if (status & (bit)) \
1669 strncat(stat_buf, (str), sizeof(stat_buf) - \
1670 strlen(stat_buf) - 2)
1674 INFOBIT(TIOCM_RTS, "|RTS");
1675 STATBIT(TIOCM_CTS, "|CTS");
1676 INFOBIT(TIOCM_DTR, "|DTR");
1677 STATBIT(TIOCM_DSR, "|DSR");
1678 STATBIT(TIOCM_CAR, "|CD");
1679 STATBIT(TIOCM_RNG, "|RI");
1682 strcat(stat_buf, "\n");
1684 ret += sprintf(buf + ret, stat_buf);
1694 static int uart_read_proc(char *page, char **start, off_t off,
1695 int count, int *eof, void *data)
1697 struct tty_driver *ttydrv = data;
1698 struct uart_driver *drv = ttydrv->driver_state;
1702 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1704 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1705 l = uart_line_info(page + len, drv, i);
1707 if (len + begin > off + count)
1709 if (len + begin < off) {
1716 if (off >= len + begin)
1718 *start = page + (off - begin);
1719 return (count < begin + len - off) ? count : (begin + len - off);
1723 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1725 * Check whether an invalid uart number has been specified, and
1726 * if so, search for the first available port that does have
1729 struct uart_port * __init
1730 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1732 int idx = co->index;
1734 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1735 ports[idx].membase == NULL))
1736 for (idx = 0; idx < nr; idx++)
1737 if (ports[idx].iobase != 0 ||
1738 ports[idx].membase != NULL)
1747 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1748 * @options: pointer to option string
1749 * @baud: pointer to an 'int' variable for the baud rate.
1750 * @parity: pointer to an 'int' variable for the parity.
1751 * @bits: pointer to an 'int' variable for the number of data bits.
1752 * @flow: pointer to an 'int' variable for the flow control character.
1754 * uart_parse_options decodes a string containing the serial console
1755 * options. The format of the string is <baud><parity><bits><flow>,
1759 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1763 *baud = simple_strtoul(s, NULL, 10);
1764 while (*s >= '0' && *s <= '9')
1779 static struct baud_rates baud_rates[] = {
1780 { 921600, B921600 },
1781 { 460800, B460800 },
1782 { 230400, B230400 },
1783 { 115200, B115200 },
1795 * uart_set_options - setup the serial console parameters
1796 * @port: pointer to the serial ports uart_port structure
1797 * @co: console pointer
1799 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1800 * @bits: number of data bits
1801 * @flow: flow control character - 'r' (rts)
1804 uart_set_options(struct uart_port *port, struct console *co,
1805 int baud, int parity, int bits, int flow)
1807 struct termios termios;
1811 * Ensure that the serial console lock is initialised
1814 spin_lock_init(&port->lock);
1816 memset(&termios, 0, sizeof(struct termios));
1818 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1821 * Construct a cflag setting.
1823 for (i = 0; baud_rates[i].rate; i++)
1824 if (baud_rates[i].rate <= baud)
1827 termios.c_cflag |= baud_rates[i].cflag;
1830 termios.c_cflag |= CS7;
1832 termios.c_cflag |= CS8;
1836 termios.c_cflag |= PARODD;
1839 termios.c_cflag |= PARENB;
1844 termios.c_cflag |= CRTSCTS;
1846 port->ops->set_termios(port, &termios, NULL);
1847 co->cflag = termios.c_cflag;
1851 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1853 static void uart_change_pm(struct uart_state *state, int pm_state)
1855 struct uart_port *port = state->port;
1857 port->ops->pm(port, pm_state, state->pm_state);
1858 state->pm_state = pm_state;
1861 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1863 struct uart_state *state = drv->state + port->line;
1867 if (state->info && state->info->flags & UIF_INITIALIZED) {
1868 struct uart_ops *ops = port->ops;
1870 spin_lock_irq(&port->lock);
1872 ops->set_mctrl(port, 0);
1874 spin_unlock_irq(&port->lock);
1877 * Wait for the transmitter to empty.
1879 while (!ops->tx_empty(port)) {
1883 ops->shutdown(port);
1887 * Disable the console device before suspending.
1889 if (uart_console(port))
1890 console_stop(port->cons);
1892 uart_change_pm(state, 3);
1899 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1901 struct uart_state *state = drv->state + port->line;
1905 uart_change_pm(state, 0);
1908 * Re-enable the console device after suspending.
1910 if (uart_console(port)) {
1911 struct termios termios;
1914 * First try to use the console cflag setting.
1916 memset(&termios, 0, sizeof(struct termios));
1917 termios.c_cflag = port->cons->cflag;
1920 * If that's unset, use the tty termios setting.
1922 if (state->info && state->info->tty && termios.c_cflag == 0)
1923 termios = *state->info->tty->termios;
1925 port->ops->set_termios(port, &termios, NULL);
1926 console_start(port->cons);
1929 if (state->info && state->info->flags & UIF_INITIALIZED) {
1930 struct uart_ops *ops = port->ops;
1932 ops->set_mctrl(port, 0);
1934 uart_change_speed(state, NULL);
1935 spin_lock_irq(&port->lock);
1936 ops->set_mctrl(port, port->mctrl);
1937 ops->start_tx(port);
1938 spin_unlock_irq(&port->lock);
1947 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1951 switch (port->iotype) {
1953 snprintf(address, sizeof(address),
1954 "I/O 0x%x", port->iobase);
1957 snprintf(address, sizeof(address),
1958 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1962 snprintf(address, sizeof(address),
1963 "MMIO 0x%lx", port->mapbase);
1966 strlcpy(address, "*unknown*", sizeof(address));
1970 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
1971 port->dev ? port->dev->bus_id : "",
1972 port->dev ? ": " : "",
1973 drv->dev_name, port->line, address, port->irq, uart_type(port));
1977 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1978 struct uart_port *port)
1983 * If there isn't a port here, don't do anything further.
1985 if (!port->iobase && !port->mapbase && !port->membase)
1989 * Now do the auto configuration stuff. Note that config_port
1990 * is expected to claim the resources and map the port for us.
1992 flags = UART_CONFIG_TYPE;
1993 if (port->flags & UPF_AUTO_IRQ)
1994 flags |= UART_CONFIG_IRQ;
1995 if (port->flags & UPF_BOOT_AUTOCONF) {
1996 port->type = PORT_UNKNOWN;
1997 port->ops->config_port(port, flags);
2000 if (port->type != PORT_UNKNOWN) {
2001 unsigned long flags;
2003 uart_report_port(drv, port);
2006 * Ensure that the modem control lines are de-activated.
2007 * We probably don't need a spinlock around this, but
2009 spin_lock_irqsave(&port->lock, flags);
2010 port->ops->set_mctrl(port, 0);
2011 spin_unlock_irqrestore(&port->lock, flags);
2014 * Power down all ports by default, except the
2015 * console if we have one.
2017 if (!uart_console(port))
2018 uart_change_pm(state, 3);
2023 * This reverses the effects of uart_configure_port, hanging up the
2024 * port before removal.
2027 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2029 struct uart_port *port = state->port;
2030 struct uart_info *info = state->info;
2032 if (info && info->tty)
2033 tty_vhangup(info->tty);
2040 * Free the port IO and memory resources, if any.
2042 if (port->type != PORT_UNKNOWN)
2043 port->ops->release_port(port);
2046 * Indicate that there isn't a port here anymore.
2048 port->type = PORT_UNKNOWN;
2051 * Kill the tasklet, and free resources.
2054 tasklet_kill(&info->tlet);
2061 static struct tty_operations uart_ops = {
2063 .close = uart_close,
2064 .write = uart_write,
2065 .put_char = uart_put_char,
2066 .flush_chars = uart_flush_chars,
2067 .write_room = uart_write_room,
2068 .chars_in_buffer= uart_chars_in_buffer,
2069 .flush_buffer = uart_flush_buffer,
2070 .ioctl = uart_ioctl,
2071 .throttle = uart_throttle,
2072 .unthrottle = uart_unthrottle,
2073 .send_xchar = uart_send_xchar,
2074 .set_termios = uart_set_termios,
2076 .start = uart_start,
2077 .hangup = uart_hangup,
2078 .break_ctl = uart_break_ctl,
2079 .wait_until_sent= uart_wait_until_sent,
2080 #ifdef CONFIG_PROC_FS
2081 .read_proc = uart_read_proc,
2083 .tiocmget = uart_tiocmget,
2084 .tiocmset = uart_tiocmset,
2088 * uart_register_driver - register a driver with the uart core layer
2089 * @drv: low level driver structure
2091 * Register a uart driver with the core driver. We in turn register
2092 * with the tty layer, and initialise the core driver per-port state.
2094 * We have a proc file in /proc/tty/driver which is named after the
2097 * drv->port should be NULL, and the per-port structures should be
2098 * registered using uart_add_one_port after this call has succeeded.
2100 int uart_register_driver(struct uart_driver *drv)
2102 struct tty_driver *normal = NULL;
2108 * Maybe we should be using a slab cache for this, especially if
2109 * we have a large number of ports to handle.
2111 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2116 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2118 normal = alloc_tty_driver(drv->nr);
2122 drv->tty_driver = normal;
2124 normal->owner = drv->owner;
2125 normal->driver_name = drv->driver_name;
2126 normal->devfs_name = drv->devfs_name;
2127 normal->name = drv->dev_name;
2128 normal->major = drv->major;
2129 normal->minor_start = drv->minor;
2130 normal->type = TTY_DRIVER_TYPE_SERIAL;
2131 normal->subtype = SERIAL_TYPE_NORMAL;
2132 normal->init_termios = tty_std_termios;
2133 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2134 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2135 normal->driver_state = drv;
2136 tty_set_operations(normal, &uart_ops);
2139 * Initialise the UART state(s).
2141 for (i = 0; i < drv->nr; i++) {
2142 struct uart_state *state = drv->state + i;
2144 state->close_delay = 500; /* .5 seconds */
2145 state->closing_wait = 30000; /* 30 seconds */
2147 init_MUTEX(&state->sem);
2150 retval = tty_register_driver(normal);
2153 put_tty_driver(normal);
2160 * uart_unregister_driver - remove a driver from the uart core layer
2161 * @drv: low level driver structure
2163 * Remove all references to a driver from the core driver. The low
2164 * level driver must have removed all its ports via the
2165 * uart_remove_one_port() if it registered them with uart_add_one_port().
2166 * (ie, drv->port == NULL)
2168 void uart_unregister_driver(struct uart_driver *drv)
2170 struct tty_driver *p = drv->tty_driver;
2171 tty_unregister_driver(p);
2174 drv->tty_driver = NULL;
2177 struct tty_driver *uart_console_device(struct console *co, int *index)
2179 struct uart_driver *p = co->data;
2181 return p->tty_driver;
2185 * uart_add_one_port - attach a driver-defined port structure
2186 * @drv: pointer to the uart low level driver structure for this port
2187 * @port: uart port structure to use for this port.
2189 * This allows the driver to register its own uart_port structure
2190 * with the core driver. The main purpose is to allow the low
2191 * level uart drivers to expand uart_port, rather than having yet
2192 * more levels of structures.
2194 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2196 struct uart_state *state;
2199 BUG_ON(in_interrupt());
2201 if (port->line >= drv->nr)
2204 state = drv->state + port->line;
2214 port->cons = drv->cons;
2215 port->info = state->info;
2218 * If this port is a console, then the spinlock is already
2221 if (!uart_console(port))
2222 spin_lock_init(&port->lock);
2224 uart_configure_port(drv, state, port);
2227 * Register the port whether it's detected or not. This allows
2228 * setserial to be used to alter this ports parameters.
2230 tty_register_device(drv->tty_driver, port->line, port->dev);
2233 * If this driver supports console, and it hasn't been
2234 * successfully registered yet, try to re-register it.
2235 * It may be that the port was not available.
2237 if (port->type != PORT_UNKNOWN &&
2238 port->cons && !(port->cons->flags & CON_ENABLED))
2239 register_console(port->cons);
2248 * uart_remove_one_port - detach a driver defined port structure
2249 * @drv: pointer to the uart low level driver structure for this port
2250 * @port: uart port structure for this port
2252 * This unhooks (and hangs up) the specified port structure from the
2253 * core driver. No further calls will be made to the low-level code
2256 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2258 struct uart_state *state = drv->state + port->line;
2260 BUG_ON(in_interrupt());
2262 if (state->port != port)
2263 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2269 * Remove the devices from devfs
2271 tty_unregister_device(drv->tty_driver, port->line);
2273 uart_unconfigure_port(drv, state);
2281 * Are the two ports equivalent?
2283 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2285 if (port1->iotype != port2->iotype)
2288 switch (port1->iotype) {
2290 return (port1->iobase == port2->iobase);
2292 return (port1->iobase == port2->iobase) &&
2293 (port1->hub6 == port2->hub6);
2295 return (port1->membase == port2->membase);
2299 EXPORT_SYMBOL(uart_match_port);
2301 EXPORT_SYMBOL(uart_write_wakeup);
2302 EXPORT_SYMBOL(uart_register_driver);
2303 EXPORT_SYMBOL(uart_unregister_driver);
2304 EXPORT_SYMBOL(uart_suspend_port);
2305 EXPORT_SYMBOL(uart_resume_port);
2306 EXPORT_SYMBOL(uart_add_one_port);
2307 EXPORT_SYMBOL(uart_remove_one_port);
2309 MODULE_DESCRIPTION("Serial driver core");
2310 MODULE_LICENSE("GPL");