Linux 2.6.21-rc3
[linux/fpc-iii.git] / drivers / serial / serial_core.c
blob0422c0f1f85207715ebe221d182de4f48f85b655
1 /*
2 * linux/drivers/char/core.c
4 * Driver core for serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_core.h>
31 #include <linux/smp_lock.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/delay.h>
35 #include <linux/mutex.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
40 #undef DEBUG
41 #ifdef DEBUG
42 #define DPRINTK(x...) printk(x)
43 #else
44 #define DPRINTK(x...) do { } while (0)
45 #endif
48 * This is used to lock changes in serial line configuration.
50 static DEFINE_MUTEX(port_mutex);
53 * lockdep: port->lock is initialized in two places, but we
54 * want only one lock-class:
56 static struct lock_class_key port_lock_key;
58 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
60 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
62 #ifdef CONFIG_SERIAL_CORE_CONSOLE
63 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
64 #else
65 #define uart_console(port) (0)
66 #endif
68 static void uart_change_speed(struct uart_state *state, struct ktermios *old_termios);
69 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
70 static void uart_change_pm(struct uart_state *state, int pm_state);
73 * This routine is used by the interrupt handler to schedule processing in
74 * the software interrupt portion of the driver.
76 void uart_write_wakeup(struct uart_port *port)
78 struct uart_info *info = port->info;
80 * This means you called this function _after_ the port was
81 * closed. No cookie for you.
83 BUG_ON(!info);
84 tasklet_schedule(&info->tlet);
87 static void uart_stop(struct tty_struct *tty)
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->port;
91 unsigned long flags;
93 spin_lock_irqsave(&port->lock, flags);
94 port->ops->stop_tx(port);
95 spin_unlock_irqrestore(&port->lock, flags);
98 static void __uart_start(struct tty_struct *tty)
100 struct uart_state *state = tty->driver_data;
101 struct uart_port *port = state->port;
103 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
104 !tty->stopped && !tty->hw_stopped)
105 port->ops->start_tx(port);
108 static void uart_start(struct tty_struct *tty)
110 struct uart_state *state = tty->driver_data;
111 struct uart_port *port = state->port;
112 unsigned long flags;
114 spin_lock_irqsave(&port->lock, flags);
115 __uart_start(tty);
116 spin_unlock_irqrestore(&port->lock, flags);
119 static void uart_tasklet_action(unsigned long data)
121 struct uart_state *state = (struct uart_state *)data;
122 tty_wakeup(state->info->tty);
125 static inline void
126 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
128 unsigned long flags;
129 unsigned int old;
131 spin_lock_irqsave(&port->lock, flags);
132 old = port->mctrl;
133 port->mctrl = (old & ~clear) | set;
134 if (old != port->mctrl)
135 port->ops->set_mctrl(port, port->mctrl);
136 spin_unlock_irqrestore(&port->lock, flags);
139 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
140 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
143 * Startup the port. This will be called once per open. All calls
144 * will be serialised by the per-port semaphore.
146 static int uart_startup(struct uart_state *state, int init_hw)
148 struct uart_info *info = state->info;
149 struct uart_port *port = state->port;
150 unsigned long page;
151 int retval = 0;
153 if (info->flags & UIF_INITIALIZED)
154 return 0;
157 * Set the TTY IO error marker - we will only clear this
158 * once we have successfully opened the port. Also set
159 * up the tty->alt_speed kludge
161 set_bit(TTY_IO_ERROR, &info->tty->flags);
163 if (port->type == PORT_UNKNOWN)
164 return 0;
167 * Initialise and allocate the transmit and temporary
168 * buffer.
170 if (!info->xmit.buf) {
171 page = get_zeroed_page(GFP_KERNEL);
172 if (!page)
173 return -ENOMEM;
175 info->xmit.buf = (unsigned char *) page;
176 uart_circ_clear(&info->xmit);
179 retval = port->ops->startup(port);
180 if (retval == 0) {
181 if (init_hw) {
183 * Initialise the hardware port settings.
185 uart_change_speed(state, NULL);
188 * Setup the RTS and DTR signals once the
189 * port is open and ready to respond.
191 if (info->tty->termios->c_cflag & CBAUD)
192 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
195 if (info->flags & UIF_CTS_FLOW) {
196 spin_lock_irq(&port->lock);
197 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
198 info->tty->hw_stopped = 1;
199 spin_unlock_irq(&port->lock);
202 info->flags |= UIF_INITIALIZED;
204 clear_bit(TTY_IO_ERROR, &info->tty->flags);
207 if (retval && capable(CAP_SYS_ADMIN))
208 retval = 0;
210 return retval;
214 * This routine will shutdown a serial port; interrupts are disabled, and
215 * DTR is dropped if the hangup on close termio flag is on. Calls to
216 * uart_shutdown are serialised by the per-port semaphore.
218 static void uart_shutdown(struct uart_state *state)
220 struct uart_info *info = state->info;
221 struct uart_port *port = state->port;
224 * Set the TTY IO error marker
226 if (info->tty)
227 set_bit(TTY_IO_ERROR, &info->tty->flags);
229 if (info->flags & UIF_INITIALIZED) {
230 info->flags &= ~UIF_INITIALIZED;
233 * Turn off DTR and RTS early.
235 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
236 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
239 * clear delta_msr_wait queue to avoid mem leaks: we may free
240 * the irq here so the queue might never be woken up. Note
241 * that we won't end up waiting on delta_msr_wait again since
242 * any outstanding file descriptors should be pointing at
243 * hung_up_tty_fops now.
245 wake_up_interruptible(&info->delta_msr_wait);
248 * Free the IRQ and disable the port.
250 port->ops->shutdown(port);
253 * Ensure that the IRQ handler isn't running on another CPU.
255 synchronize_irq(port->irq);
259 * kill off our tasklet
261 tasklet_kill(&info->tlet);
264 * Free the transmit buffer page.
266 if (info->xmit.buf) {
267 free_page((unsigned long)info->xmit.buf);
268 info->xmit.buf = NULL;
273 * uart_update_timeout - update per-port FIFO timeout.
274 * @port: uart_port structure describing the port
275 * @cflag: termios cflag value
276 * @baud: speed of the port
278 * Set the port FIFO timeout value. The @cflag value should
279 * reflect the actual hardware settings.
281 void
282 uart_update_timeout(struct uart_port *port, unsigned int cflag,
283 unsigned int baud)
285 unsigned int bits;
287 /* byte size and parity */
288 switch (cflag & CSIZE) {
289 case CS5:
290 bits = 7;
291 break;
292 case CS6:
293 bits = 8;
294 break;
295 case CS7:
296 bits = 9;
297 break;
298 default:
299 bits = 10;
300 break; // CS8
303 if (cflag & CSTOPB)
304 bits++;
305 if (cflag & PARENB)
306 bits++;
309 * The total number of bits to be transmitted in the fifo.
311 bits = bits * port->fifosize;
314 * Figure the timeout to send the above number of bits.
315 * Add .02 seconds of slop
317 port->timeout = (HZ * bits) / baud + HZ/50;
320 EXPORT_SYMBOL(uart_update_timeout);
323 * uart_get_baud_rate - return baud rate for a particular port
324 * @port: uart_port structure describing the port in question.
325 * @termios: desired termios settings.
326 * @old: old termios (or NULL)
327 * @min: minimum acceptable baud rate
328 * @max: maximum acceptable baud rate
330 * Decode the termios structure into a numeric baud rate,
331 * taking account of the magic 38400 baud rate (with spd_*
332 * flags), and mapping the %B0 rate to 9600 baud.
334 * If the new baud rate is invalid, try the old termios setting.
335 * If it's still invalid, we try 9600 baud.
337 * Update the @termios structure to reflect the baud rate
338 * we're actually going to be using.
340 unsigned int
341 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
342 struct ktermios *old, unsigned int min, unsigned int max)
344 unsigned int try, baud, altbaud = 38400;
345 upf_t flags = port->flags & UPF_SPD_MASK;
347 if (flags == UPF_SPD_HI)
348 altbaud = 57600;
349 if (flags == UPF_SPD_VHI)
350 altbaud = 115200;
351 if (flags == UPF_SPD_SHI)
352 altbaud = 230400;
353 if (flags == UPF_SPD_WARP)
354 altbaud = 460800;
356 for (try = 0; try < 2; try++) {
357 baud = tty_termios_baud_rate(termios);
360 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
361 * Die! Die! Die!
363 if (baud == 38400)
364 baud = altbaud;
367 * Special case: B0 rate.
369 if (baud == 0)
370 baud = 9600;
372 if (baud >= min && baud <= max)
373 return baud;
376 * Oops, the quotient was zero. Try again with
377 * the old baud rate if possible.
379 termios->c_cflag &= ~CBAUD;
380 if (old) {
381 termios->c_cflag |= old->c_cflag & CBAUD;
382 old = NULL;
383 continue;
387 * As a last resort, if the quotient is zero,
388 * default to 9600 bps
390 termios->c_cflag |= B9600;
393 return 0;
396 EXPORT_SYMBOL(uart_get_baud_rate);
399 * uart_get_divisor - return uart clock divisor
400 * @port: uart_port structure describing the port.
401 * @baud: desired baud rate
403 * Calculate the uart clock divisor for the port.
405 unsigned int
406 uart_get_divisor(struct uart_port *port, unsigned int baud)
408 unsigned int quot;
411 * Old custom speed handling.
413 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
414 quot = port->custom_divisor;
415 else
416 quot = (port->uartclk + (8 * baud)) / (16 * baud);
418 return quot;
421 EXPORT_SYMBOL(uart_get_divisor);
423 static void
424 uart_change_speed(struct uart_state *state, struct ktermios *old_termios)
426 struct tty_struct *tty = state->info->tty;
427 struct uart_port *port = state->port;
428 struct ktermios *termios;
431 * If we have no tty, termios, or the port does not exist,
432 * then we can't set the parameters for this port.
434 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
435 return;
437 termios = tty->termios;
440 * Set flags based on termios cflag
442 if (termios->c_cflag & CRTSCTS)
443 state->info->flags |= UIF_CTS_FLOW;
444 else
445 state->info->flags &= ~UIF_CTS_FLOW;
447 if (termios->c_cflag & CLOCAL)
448 state->info->flags &= ~UIF_CHECK_CD;
449 else
450 state->info->flags |= UIF_CHECK_CD;
452 port->ops->set_termios(port, termios, old_termios);
455 static inline void
456 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
458 unsigned long flags;
460 if (!circ->buf)
461 return;
463 spin_lock_irqsave(&port->lock, flags);
464 if (uart_circ_chars_free(circ) != 0) {
465 circ->buf[circ->head] = c;
466 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
468 spin_unlock_irqrestore(&port->lock, flags);
471 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
473 struct uart_state *state = tty->driver_data;
475 __uart_put_char(state->port, &state->info->xmit, ch);
478 static void uart_flush_chars(struct tty_struct *tty)
480 uart_start(tty);
483 static int
484 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
486 struct uart_state *state = tty->driver_data;
487 struct uart_port *port;
488 struct circ_buf *circ;
489 unsigned long flags;
490 int c, ret = 0;
493 * This means you called this function _after_ the port was
494 * closed. No cookie for you.
496 if (!state || !state->info) {
497 WARN_ON(1);
498 return -EL3HLT;
501 port = state->port;
502 circ = &state->info->xmit;
504 if (!circ->buf)
505 return 0;
507 spin_lock_irqsave(&port->lock, flags);
508 while (1) {
509 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
510 if (count < c)
511 c = count;
512 if (c <= 0)
513 break;
514 memcpy(circ->buf + circ->head, buf, c);
515 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
516 buf += c;
517 count -= c;
518 ret += c;
520 spin_unlock_irqrestore(&port->lock, flags);
522 uart_start(tty);
523 return ret;
526 static int uart_write_room(struct tty_struct *tty)
528 struct uart_state *state = tty->driver_data;
530 return uart_circ_chars_free(&state->info->xmit);
533 static int uart_chars_in_buffer(struct tty_struct *tty)
535 struct uart_state *state = tty->driver_data;
537 return uart_circ_chars_pending(&state->info->xmit);
540 static void uart_flush_buffer(struct tty_struct *tty)
542 struct uart_state *state = tty->driver_data;
543 struct uart_port *port = state->port;
544 unsigned long flags;
547 * This means you called this function _after_ the port was
548 * closed. No cookie for you.
550 if (!state || !state->info) {
551 WARN_ON(1);
552 return;
555 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
557 spin_lock_irqsave(&port->lock, flags);
558 uart_circ_clear(&state->info->xmit);
559 spin_unlock_irqrestore(&port->lock, flags);
560 tty_wakeup(tty);
564 * This function is used to send a high-priority XON/XOFF character to
565 * the device
567 static void uart_send_xchar(struct tty_struct *tty, char ch)
569 struct uart_state *state = tty->driver_data;
570 struct uart_port *port = state->port;
571 unsigned long flags;
573 if (port->ops->send_xchar)
574 port->ops->send_xchar(port, ch);
575 else {
576 port->x_char = ch;
577 if (ch) {
578 spin_lock_irqsave(&port->lock, flags);
579 port->ops->start_tx(port);
580 spin_unlock_irqrestore(&port->lock, flags);
585 static void uart_throttle(struct tty_struct *tty)
587 struct uart_state *state = tty->driver_data;
589 if (I_IXOFF(tty))
590 uart_send_xchar(tty, STOP_CHAR(tty));
592 if (tty->termios->c_cflag & CRTSCTS)
593 uart_clear_mctrl(state->port, TIOCM_RTS);
596 static void uart_unthrottle(struct tty_struct *tty)
598 struct uart_state *state = tty->driver_data;
599 struct uart_port *port = state->port;
601 if (I_IXOFF(tty)) {
602 if (port->x_char)
603 port->x_char = 0;
604 else
605 uart_send_xchar(tty, START_CHAR(tty));
608 if (tty->termios->c_cflag & CRTSCTS)
609 uart_set_mctrl(port, TIOCM_RTS);
612 static int uart_get_info(struct uart_state *state,
613 struct serial_struct __user *retinfo)
615 struct uart_port *port = state->port;
616 struct serial_struct tmp;
618 memset(&tmp, 0, sizeof(tmp));
619 tmp.type = port->type;
620 tmp.line = port->line;
621 tmp.port = port->iobase;
622 if (HIGH_BITS_OFFSET)
623 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
624 tmp.irq = port->irq;
625 tmp.flags = port->flags;
626 tmp.xmit_fifo_size = port->fifosize;
627 tmp.baud_base = port->uartclk / 16;
628 tmp.close_delay = state->close_delay / 10;
629 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
630 ASYNC_CLOSING_WAIT_NONE :
631 state->closing_wait / 10;
632 tmp.custom_divisor = port->custom_divisor;
633 tmp.hub6 = port->hub6;
634 tmp.io_type = port->iotype;
635 tmp.iomem_reg_shift = port->regshift;
636 tmp.iomem_base = (void *)port->mapbase;
638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
639 return -EFAULT;
640 return 0;
643 static int uart_set_info(struct uart_state *state,
644 struct serial_struct __user *newinfo)
646 struct serial_struct new_serial;
647 struct uart_port *port = state->port;
648 unsigned long new_port;
649 unsigned int change_irq, change_port, closing_wait;
650 unsigned int old_custom_divisor, close_delay;
651 upf_t old_flags, new_flags;
652 int retval = 0;
654 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
655 return -EFAULT;
657 new_port = new_serial.port;
658 if (HIGH_BITS_OFFSET)
659 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
661 new_serial.irq = irq_canonicalize(new_serial.irq);
662 close_delay = new_serial.close_delay * 10;
663 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
664 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
667 * This semaphore protects state->count. It is also
668 * very useful to prevent opens. Also, take the
669 * port configuration semaphore to make sure that a
670 * module insertion/removal doesn't change anything
671 * under us.
673 mutex_lock(&state->mutex);
675 change_irq = new_serial.irq != port->irq;
678 * Since changing the 'type' of the port changes its resource
679 * allocations, we should treat type changes the same as
680 * IO port changes.
682 change_port = new_port != port->iobase ||
683 (unsigned long)new_serial.iomem_base != port->mapbase ||
684 new_serial.hub6 != port->hub6 ||
685 new_serial.io_type != port->iotype ||
686 new_serial.iomem_reg_shift != port->regshift ||
687 new_serial.type != port->type;
689 old_flags = port->flags;
690 new_flags = new_serial.flags;
691 old_custom_divisor = port->custom_divisor;
693 if (!capable(CAP_SYS_ADMIN)) {
694 retval = -EPERM;
695 if (change_irq || change_port ||
696 (new_serial.baud_base != port->uartclk / 16) ||
697 (close_delay != state->close_delay) ||
698 (closing_wait != state->closing_wait) ||
699 (new_serial.xmit_fifo_size &&
700 new_serial.xmit_fifo_size != port->fifosize) ||
701 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
702 goto exit;
703 port->flags = ((port->flags & ~UPF_USR_MASK) |
704 (new_flags & UPF_USR_MASK));
705 port->custom_divisor = new_serial.custom_divisor;
706 goto check_and_exit;
710 * Ask the low level driver to verify the settings.
712 if (port->ops->verify_port)
713 retval = port->ops->verify_port(port, &new_serial);
715 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
716 (new_serial.baud_base < 9600))
717 retval = -EINVAL;
719 if (retval)
720 goto exit;
722 if (change_port || change_irq) {
723 retval = -EBUSY;
726 * Make sure that we are the sole user of this port.
728 if (uart_users(state) > 1)
729 goto exit;
732 * We need to shutdown the serial port at the old
733 * port/type/irq combination.
735 uart_shutdown(state);
738 if (change_port) {
739 unsigned long old_iobase, old_mapbase;
740 unsigned int old_type, old_iotype, old_hub6, old_shift;
742 old_iobase = port->iobase;
743 old_mapbase = port->mapbase;
744 old_type = port->type;
745 old_hub6 = port->hub6;
746 old_iotype = port->iotype;
747 old_shift = port->regshift;
750 * Free and release old regions
752 if (old_type != PORT_UNKNOWN)
753 port->ops->release_port(port);
755 port->iobase = new_port;
756 port->type = new_serial.type;
757 port->hub6 = new_serial.hub6;
758 port->iotype = new_serial.io_type;
759 port->regshift = new_serial.iomem_reg_shift;
760 port->mapbase = (unsigned long)new_serial.iomem_base;
763 * Claim and map the new regions
765 if (port->type != PORT_UNKNOWN) {
766 retval = port->ops->request_port(port);
767 } else {
768 /* Always success - Jean II */
769 retval = 0;
773 * If we fail to request resources for the
774 * new port, try to restore the old settings.
776 if (retval && old_type != PORT_UNKNOWN) {
777 port->iobase = old_iobase;
778 port->type = old_type;
779 port->hub6 = old_hub6;
780 port->iotype = old_iotype;
781 port->regshift = old_shift;
782 port->mapbase = old_mapbase;
783 retval = port->ops->request_port(port);
785 * If we failed to restore the old settings,
786 * we fail like this.
788 if (retval)
789 port->type = PORT_UNKNOWN;
792 * We failed anyway.
794 retval = -EBUSY;
795 goto exit; // Added to return the correct error -Ram Gupta
799 port->irq = new_serial.irq;
800 port->uartclk = new_serial.baud_base * 16;
801 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
802 (new_flags & UPF_CHANGE_MASK);
803 port->custom_divisor = new_serial.custom_divisor;
804 state->close_delay = close_delay;
805 state->closing_wait = closing_wait;
806 if (new_serial.xmit_fifo_size)
807 port->fifosize = new_serial.xmit_fifo_size;
808 if (state->info->tty)
809 state->info->tty->low_latency =
810 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
812 check_and_exit:
813 retval = 0;
814 if (port->type == PORT_UNKNOWN)
815 goto exit;
816 if (state->info->flags & UIF_INITIALIZED) {
817 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
818 old_custom_divisor != port->custom_divisor) {
820 * If they're setting up a custom divisor or speed,
821 * instead of clearing it, then bitch about it. No
822 * need to rate-limit; it's CAP_SYS_ADMIN only.
824 if (port->flags & UPF_SPD_MASK) {
825 char buf[64];
826 printk(KERN_NOTICE
827 "%s sets custom speed on %s. This "
828 "is deprecated.\n", current->comm,
829 tty_name(state->info->tty, buf));
831 uart_change_speed(state, NULL);
833 } else
834 retval = uart_startup(state, 1);
835 exit:
836 mutex_unlock(&state->mutex);
837 return retval;
842 * uart_get_lsr_info - get line status register info.
843 * Note: uart_ioctl protects us against hangups.
845 static int uart_get_lsr_info(struct uart_state *state,
846 unsigned int __user *value)
848 struct uart_port *port = state->port;
849 unsigned int result;
851 result = port->ops->tx_empty(port);
854 * If we're about to load something into the transmit
855 * register, we'll pretend the transmitter isn't empty to
856 * avoid a race condition (depending on when the transmit
857 * interrupt happens).
859 if (port->x_char ||
860 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
861 !state->info->tty->stopped && !state->info->tty->hw_stopped))
862 result &= ~TIOCSER_TEMT;
864 return put_user(result, value);
867 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
869 struct uart_state *state = tty->driver_data;
870 struct uart_port *port = state->port;
871 int result = -EIO;
873 mutex_lock(&state->mutex);
874 if ((!file || !tty_hung_up_p(file)) &&
875 !(tty->flags & (1 << TTY_IO_ERROR))) {
876 result = port->mctrl;
878 spin_lock_irq(&port->lock);
879 result |= port->ops->get_mctrl(port);
880 spin_unlock_irq(&port->lock);
882 mutex_unlock(&state->mutex);
884 return result;
887 static int
888 uart_tiocmset(struct tty_struct *tty, struct file *file,
889 unsigned int set, unsigned int clear)
891 struct uart_state *state = tty->driver_data;
892 struct uart_port *port = state->port;
893 int ret = -EIO;
895 mutex_lock(&state->mutex);
896 if ((!file || !tty_hung_up_p(file)) &&
897 !(tty->flags & (1 << TTY_IO_ERROR))) {
898 uart_update_mctrl(port, set, clear);
899 ret = 0;
901 mutex_unlock(&state->mutex);
902 return ret;
905 static void uart_break_ctl(struct tty_struct *tty, int break_state)
907 struct uart_state *state = tty->driver_data;
908 struct uart_port *port = state->port;
910 BUG_ON(!kernel_locked());
912 mutex_lock(&state->mutex);
914 if (port->type != PORT_UNKNOWN)
915 port->ops->break_ctl(port, break_state);
917 mutex_unlock(&state->mutex);
920 static int uart_do_autoconfig(struct uart_state *state)
922 struct uart_port *port = state->port;
923 int flags, ret;
925 if (!capable(CAP_SYS_ADMIN))
926 return -EPERM;
929 * Take the per-port semaphore. This prevents count from
930 * changing, and hence any extra opens of the port while
931 * we're auto-configuring.
933 if (mutex_lock_interruptible(&state->mutex))
934 return -ERESTARTSYS;
936 ret = -EBUSY;
937 if (uart_users(state) == 1) {
938 uart_shutdown(state);
941 * If we already have a port type configured,
942 * we must release its resources.
944 if (port->type != PORT_UNKNOWN)
945 port->ops->release_port(port);
947 flags = UART_CONFIG_TYPE;
948 if (port->flags & UPF_AUTO_IRQ)
949 flags |= UART_CONFIG_IRQ;
952 * This will claim the ports resources if
953 * a port is found.
955 port->ops->config_port(port, flags);
957 ret = uart_startup(state, 1);
959 mutex_unlock(&state->mutex);
960 return ret;
964 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
965 * - mask passed in arg for lines of interest
966 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
967 * Caller should use TIOCGICOUNT to see which one it was
969 static int
970 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
972 struct uart_port *port = state->port;
973 DECLARE_WAITQUEUE(wait, current);
974 struct uart_icount cprev, cnow;
975 int ret;
978 * note the counters on entry
980 spin_lock_irq(&port->lock);
981 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
984 * Force modem status interrupts on
986 port->ops->enable_ms(port);
987 spin_unlock_irq(&port->lock);
989 add_wait_queue(&state->info->delta_msr_wait, &wait);
990 for (;;) {
991 spin_lock_irq(&port->lock);
992 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
993 spin_unlock_irq(&port->lock);
995 set_current_state(TASK_INTERRUPTIBLE);
997 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
998 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
999 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1000 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1001 ret = 0;
1002 break;
1005 schedule();
1007 /* see if a signal did it */
1008 if (signal_pending(current)) {
1009 ret = -ERESTARTSYS;
1010 break;
1013 cprev = cnow;
1016 current->state = TASK_RUNNING;
1017 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1019 return ret;
1023 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1024 * Return: write counters to the user passed counter struct
1025 * NB: both 1->0 and 0->1 transitions are counted except for
1026 * RI where only 0->1 is counted.
1028 static int uart_get_count(struct uart_state *state,
1029 struct serial_icounter_struct __user *icnt)
1031 struct serial_icounter_struct icount;
1032 struct uart_icount cnow;
1033 struct uart_port *port = state->port;
1035 spin_lock_irq(&port->lock);
1036 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1037 spin_unlock_irq(&port->lock);
1039 icount.cts = cnow.cts;
1040 icount.dsr = cnow.dsr;
1041 icount.rng = cnow.rng;
1042 icount.dcd = cnow.dcd;
1043 icount.rx = cnow.rx;
1044 icount.tx = cnow.tx;
1045 icount.frame = cnow.frame;
1046 icount.overrun = cnow.overrun;
1047 icount.parity = cnow.parity;
1048 icount.brk = cnow.brk;
1049 icount.buf_overrun = cnow.buf_overrun;
1051 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1055 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1057 static int
1058 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1059 unsigned long arg)
1061 struct uart_state *state = tty->driver_data;
1062 void __user *uarg = (void __user *)arg;
1063 int ret = -ENOIOCTLCMD;
1065 BUG_ON(!kernel_locked());
1068 * These ioctls don't rely on the hardware to be present.
1070 switch (cmd) {
1071 case TIOCGSERIAL:
1072 ret = uart_get_info(state, uarg);
1073 break;
1075 case TIOCSSERIAL:
1076 ret = uart_set_info(state, uarg);
1077 break;
1079 case TIOCSERCONFIG:
1080 ret = uart_do_autoconfig(state);
1081 break;
1083 case TIOCSERGWILD: /* obsolete */
1084 case TIOCSERSWILD: /* obsolete */
1085 ret = 0;
1086 break;
1089 if (ret != -ENOIOCTLCMD)
1090 goto out;
1092 if (tty->flags & (1 << TTY_IO_ERROR)) {
1093 ret = -EIO;
1094 goto out;
1098 * The following should only be used when hardware is present.
1100 switch (cmd) {
1101 case TIOCMIWAIT:
1102 ret = uart_wait_modem_status(state, arg);
1103 break;
1105 case TIOCGICOUNT:
1106 ret = uart_get_count(state, uarg);
1107 break;
1110 if (ret != -ENOIOCTLCMD)
1111 goto out;
1113 mutex_lock(&state->mutex);
1115 if (tty_hung_up_p(filp)) {
1116 ret = -EIO;
1117 goto out_up;
1121 * All these rely on hardware being present and need to be
1122 * protected against the tty being hung up.
1124 switch (cmd) {
1125 case TIOCSERGETLSR: /* Get line status register */
1126 ret = uart_get_lsr_info(state, uarg);
1127 break;
1129 default: {
1130 struct uart_port *port = state->port;
1131 if (port->ops->ioctl)
1132 ret = port->ops->ioctl(port, cmd, arg);
1133 break;
1136 out_up:
1137 mutex_unlock(&state->mutex);
1138 out:
1139 return ret;
1142 static void uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1144 struct uart_state *state = tty->driver_data;
1145 unsigned long flags;
1146 unsigned int cflag = tty->termios->c_cflag;
1148 BUG_ON(!kernel_locked());
1151 * These are the bits that are used to setup various
1152 * flags in the low level driver.
1154 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1156 if ((cflag ^ old_termios->c_cflag) == 0 &&
1157 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1158 return;
1160 uart_change_speed(state, old_termios);
1162 /* Handle transition to B0 status */
1163 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1164 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1166 /* Handle transition away from B0 status */
1167 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1168 unsigned int mask = TIOCM_DTR;
1169 if (!(cflag & CRTSCTS) ||
1170 !test_bit(TTY_THROTTLED, &tty->flags))
1171 mask |= TIOCM_RTS;
1172 uart_set_mctrl(state->port, mask);
1175 /* Handle turning off CRTSCTS */
1176 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1177 spin_lock_irqsave(&state->port->lock, flags);
1178 tty->hw_stopped = 0;
1179 __uart_start(tty);
1180 spin_unlock_irqrestore(&state->port->lock, flags);
1183 /* Handle turning on CRTSCTS */
1184 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1185 spin_lock_irqsave(&state->port->lock, flags);
1186 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1187 tty->hw_stopped = 1;
1188 state->port->ops->stop_tx(state->port);
1190 spin_unlock_irqrestore(&state->port->lock, flags);
1193 #if 0
1195 * No need to wake up processes in open wait, since they
1196 * sample the CLOCAL flag once, and don't recheck it.
1197 * XXX It's not clear whether the current behavior is correct
1198 * or not. Hence, this may change.....
1200 if (!(old_termios->c_cflag & CLOCAL) &&
1201 (tty->termios->c_cflag & CLOCAL))
1202 wake_up_interruptible(&state->info->open_wait);
1203 #endif
1207 * In 2.4.5, calls to this will be serialized via the BKL in
1208 * linux/drivers/char/tty_io.c:tty_release()
1209 * linux/drivers/char/tty_io.c:do_tty_handup()
1211 static void uart_close(struct tty_struct *tty, struct file *filp)
1213 struct uart_state *state = tty->driver_data;
1214 struct uart_port *port;
1216 BUG_ON(!kernel_locked());
1218 if (!state || !state->port)
1219 return;
1221 port = state->port;
1223 DPRINTK("uart_close(%d) called\n", port->line);
1225 mutex_lock(&state->mutex);
1227 if (tty_hung_up_p(filp))
1228 goto done;
1230 if ((tty->count == 1) && (state->count != 1)) {
1232 * Uh, oh. tty->count is 1, which means that the tty
1233 * structure will be freed. state->count should always
1234 * be one in these conditions. If it's greater than
1235 * one, we've got real problems, since it means the
1236 * serial port won't be shutdown.
1238 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1239 "state->count is %d\n", state->count);
1240 state->count = 1;
1242 if (--state->count < 0) {
1243 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1244 tty->name, state->count);
1245 state->count = 0;
1247 if (state->count)
1248 goto done;
1251 * Now we wait for the transmit buffer to clear; and we notify
1252 * the line discipline to only process XON/XOFF characters by
1253 * setting tty->closing.
1255 tty->closing = 1;
1257 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1258 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1261 * At this point, we stop accepting input. To do this, we
1262 * disable the receive line status interrupts.
1264 if (state->info->flags & UIF_INITIALIZED) {
1265 unsigned long flags;
1266 spin_lock_irqsave(&port->lock, flags);
1267 port->ops->stop_rx(port);
1268 spin_unlock_irqrestore(&port->lock, flags);
1270 * Before we drop DTR, make sure the UART transmitter
1271 * has completely drained; this is especially
1272 * important if there is a transmit FIFO!
1274 uart_wait_until_sent(tty, port->timeout);
1277 uart_shutdown(state);
1278 uart_flush_buffer(tty);
1280 tty_ldisc_flush(tty);
1282 tty->closing = 0;
1283 state->info->tty = NULL;
1285 if (state->info->blocked_open) {
1286 if (state->close_delay)
1287 msleep_interruptible(state->close_delay);
1288 } else if (!uart_console(port)) {
1289 uart_change_pm(state, 3);
1293 * Wake up anyone trying to open this port.
1295 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1296 wake_up_interruptible(&state->info->open_wait);
1298 done:
1299 mutex_unlock(&state->mutex);
1302 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1304 struct uart_state *state = tty->driver_data;
1305 struct uart_port *port = state->port;
1306 unsigned long char_time, expire;
1308 BUG_ON(!kernel_locked());
1310 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1311 return;
1314 * Set the check interval to be 1/5 of the estimated time to
1315 * send a single character, and make it at least 1. The check
1316 * interval should also be less than the timeout.
1318 * Note: we have to use pretty tight timings here to satisfy
1319 * the NIST-PCTS.
1321 char_time = (port->timeout - HZ/50) / port->fifosize;
1322 char_time = char_time / 5;
1323 if (char_time == 0)
1324 char_time = 1;
1325 if (timeout && timeout < char_time)
1326 char_time = timeout;
1329 * If the transmitter hasn't cleared in twice the approximate
1330 * amount of time to send the entire FIFO, it probably won't
1331 * ever clear. This assumes the UART isn't doing flow
1332 * control, which is currently the case. Hence, if it ever
1333 * takes longer than port->timeout, this is probably due to a
1334 * UART bug of some kind. So, we clamp the timeout parameter at
1335 * 2*port->timeout.
1337 if (timeout == 0 || timeout > 2 * port->timeout)
1338 timeout = 2 * port->timeout;
1340 expire = jiffies + timeout;
1342 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1343 port->line, jiffies, expire);
1346 * Check whether the transmitter is empty every 'char_time'.
1347 * 'timeout' / 'expire' give us the maximum amount of time
1348 * we wait.
1350 while (!port->ops->tx_empty(port)) {
1351 msleep_interruptible(jiffies_to_msecs(char_time));
1352 if (signal_pending(current))
1353 break;
1354 if (time_after(jiffies, expire))
1355 break;
1357 set_current_state(TASK_RUNNING); /* might not be needed */
1361 * This is called with the BKL held in
1362 * linux/drivers/char/tty_io.c:do_tty_hangup()
1363 * We're called from the eventd thread, so we can sleep for
1364 * a _short_ time only.
1366 static void uart_hangup(struct tty_struct *tty)
1368 struct uart_state *state = tty->driver_data;
1370 BUG_ON(!kernel_locked());
1371 DPRINTK("uart_hangup(%d)\n", state->port->line);
1373 mutex_lock(&state->mutex);
1374 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1375 uart_flush_buffer(tty);
1376 uart_shutdown(state);
1377 state->count = 0;
1378 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1379 state->info->tty = NULL;
1380 wake_up_interruptible(&state->info->open_wait);
1381 wake_up_interruptible(&state->info->delta_msr_wait);
1383 mutex_unlock(&state->mutex);
1387 * Copy across the serial console cflag setting into the termios settings
1388 * for the initial open of the port. This allows continuity between the
1389 * kernel settings, and the settings init adopts when it opens the port
1390 * for the first time.
1392 static void uart_update_termios(struct uart_state *state)
1394 struct tty_struct *tty = state->info->tty;
1395 struct uart_port *port = state->port;
1397 if (uart_console(port) && port->cons->cflag) {
1398 tty->termios->c_cflag = port->cons->cflag;
1399 port->cons->cflag = 0;
1403 * If the device failed to grab its irq resources,
1404 * or some other error occurred, don't try to talk
1405 * to the port hardware.
1407 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1409 * Make termios settings take effect.
1411 uart_change_speed(state, NULL);
1414 * And finally enable the RTS and DTR signals.
1416 if (tty->termios->c_cflag & CBAUD)
1417 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1422 * Block the open until the port is ready. We must be called with
1423 * the per-port semaphore held.
1425 static int
1426 uart_block_til_ready(struct file *filp, struct uart_state *state)
1428 DECLARE_WAITQUEUE(wait, current);
1429 struct uart_info *info = state->info;
1430 struct uart_port *port = state->port;
1431 unsigned int mctrl;
1433 info->blocked_open++;
1434 state->count--;
1436 add_wait_queue(&info->open_wait, &wait);
1437 while (1) {
1438 set_current_state(TASK_INTERRUPTIBLE);
1441 * If we have been hung up, tell userspace/restart open.
1443 if (tty_hung_up_p(filp) || info->tty == NULL)
1444 break;
1447 * If the port has been closed, tell userspace/restart open.
1449 if (!(info->flags & UIF_INITIALIZED))
1450 break;
1453 * If non-blocking mode is set, or CLOCAL mode is set,
1454 * we don't want to wait for the modem status lines to
1455 * indicate that the port is ready.
1457 * Also, if the port is not enabled/configured, we want
1458 * to allow the open to succeed here. Note that we will
1459 * have set TTY_IO_ERROR for a non-existant port.
1461 if ((filp->f_flags & O_NONBLOCK) ||
1462 (info->tty->termios->c_cflag & CLOCAL) ||
1463 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1464 break;
1468 * Set DTR to allow modem to know we're waiting. Do
1469 * not set RTS here - we want to make sure we catch
1470 * the data from the modem.
1472 if (info->tty->termios->c_cflag & CBAUD)
1473 uart_set_mctrl(port, TIOCM_DTR);
1476 * and wait for the carrier to indicate that the
1477 * modem is ready for us.
1479 spin_lock_irq(&port->lock);
1480 port->ops->enable_ms(port);
1481 mctrl = port->ops->get_mctrl(port);
1482 spin_unlock_irq(&port->lock);
1483 if (mctrl & TIOCM_CAR)
1484 break;
1486 mutex_unlock(&state->mutex);
1487 schedule();
1488 mutex_lock(&state->mutex);
1490 if (signal_pending(current))
1491 break;
1493 set_current_state(TASK_RUNNING);
1494 remove_wait_queue(&info->open_wait, &wait);
1496 state->count++;
1497 info->blocked_open--;
1499 if (signal_pending(current))
1500 return -ERESTARTSYS;
1502 if (!info->tty || tty_hung_up_p(filp))
1503 return -EAGAIN;
1505 return 0;
1508 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1510 struct uart_state *state;
1511 int ret = 0;
1513 state = drv->state + line;
1514 if (mutex_lock_interruptible(&state->mutex)) {
1515 ret = -ERESTARTSYS;
1516 goto err;
1519 state->count++;
1520 if (!state->port || state->port->flags & UPF_DEAD) {
1521 ret = -ENXIO;
1522 goto err_unlock;
1525 if (!state->info) {
1526 state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
1527 if (state->info) {
1528 init_waitqueue_head(&state->info->open_wait);
1529 init_waitqueue_head(&state->info->delta_msr_wait);
1532 * Link the info into the other structures.
1534 state->port->info = state->info;
1536 tasklet_init(&state->info->tlet, uart_tasklet_action,
1537 (unsigned long)state);
1538 } else {
1539 ret = -ENOMEM;
1540 goto err_unlock;
1543 return state;
1545 err_unlock:
1546 state->count--;
1547 mutex_unlock(&state->mutex);
1548 err:
1549 return ERR_PTR(ret);
1553 * In 2.4.5, calls to uart_open are serialised by the BKL in
1554 * linux/fs/devices.c:chrdev_open()
1555 * Note that if this fails, then uart_close() _will_ be called.
1557 * In time, we want to scrap the "opening nonpresent ports"
1558 * behaviour and implement an alternative way for setserial
1559 * to set base addresses/ports/types. This will allow us to
1560 * get rid of a certain amount of extra tests.
1562 static int uart_open(struct tty_struct *tty, struct file *filp)
1564 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1565 struct uart_state *state;
1566 int retval, line = tty->index;
1568 BUG_ON(!kernel_locked());
1569 DPRINTK("uart_open(%d) called\n", line);
1572 * tty->driver->num won't change, so we won't fail here with
1573 * tty->driver_data set to something non-NULL (and therefore
1574 * we won't get caught by uart_close()).
1576 retval = -ENODEV;
1577 if (line >= tty->driver->num)
1578 goto fail;
1581 * We take the semaphore inside uart_get to guarantee that we won't
1582 * be re-entered while allocating the info structure, or while we
1583 * request any IRQs that the driver may need. This also has the nice
1584 * side-effect that it delays the action of uart_hangup, so we can
1585 * guarantee that info->tty will always contain something reasonable.
1587 state = uart_get(drv, line);
1588 if (IS_ERR(state)) {
1589 retval = PTR_ERR(state);
1590 goto fail;
1594 * Once we set tty->driver_data here, we are guaranteed that
1595 * uart_close() will decrement the driver module use count.
1596 * Any failures from here onwards should not touch the count.
1598 tty->driver_data = state;
1599 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1600 tty->alt_speed = 0;
1601 state->info->tty = tty;
1604 * If the port is in the middle of closing, bail out now.
1606 if (tty_hung_up_p(filp)) {
1607 retval = -EAGAIN;
1608 state->count--;
1609 mutex_unlock(&state->mutex);
1610 goto fail;
1614 * Make sure the device is in D0 state.
1616 if (state->count == 1)
1617 uart_change_pm(state, 0);
1620 * Start up the serial port.
1622 retval = uart_startup(state, 0);
1625 * If we succeeded, wait until the port is ready.
1627 if (retval == 0)
1628 retval = uart_block_til_ready(filp, state);
1629 mutex_unlock(&state->mutex);
1632 * If this is the first open to succeed, adjust things to suit.
1634 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1635 state->info->flags |= UIF_NORMAL_ACTIVE;
1637 uart_update_termios(state);
1640 fail:
1641 return retval;
1644 static const char *uart_type(struct uart_port *port)
1646 const char *str = NULL;
1648 if (port->ops->type)
1649 str = port->ops->type(port);
1651 if (!str)
1652 str = "unknown";
1654 return str;
1657 #ifdef CONFIG_PROC_FS
1659 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1661 struct uart_state *state = drv->state + i;
1662 int pm_state;
1663 struct uart_port *port = state->port;
1664 char stat_buf[32];
1665 unsigned int status;
1666 int mmio, ret;
1668 if (!port)
1669 return 0;
1671 mmio = port->iotype >= UPIO_MEM;
1672 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1673 port->line, uart_type(port),
1674 mmio ? "mmio:0x" : "port:",
1675 mmio ? port->mapbase : (unsigned long) port->iobase,
1676 port->irq);
1678 if (port->type == PORT_UNKNOWN) {
1679 strcat(buf, "\n");
1680 return ret + 1;
1683 if(capable(CAP_SYS_ADMIN))
1685 mutex_lock(&state->mutex);
1686 pm_state = state->pm_state;
1687 if (pm_state)
1688 uart_change_pm(state, 0);
1689 spin_lock_irq(&port->lock);
1690 status = port->ops->get_mctrl(port);
1691 spin_unlock_irq(&port->lock);
1692 if (pm_state)
1693 uart_change_pm(state, pm_state);
1694 mutex_unlock(&state->mutex);
1696 ret += sprintf(buf + ret, " tx:%d rx:%d",
1697 port->icount.tx, port->icount.rx);
1698 if (port->icount.frame)
1699 ret += sprintf(buf + ret, " fe:%d",
1700 port->icount.frame);
1701 if (port->icount.parity)
1702 ret += sprintf(buf + ret, " pe:%d",
1703 port->icount.parity);
1704 if (port->icount.brk)
1705 ret += sprintf(buf + ret, " brk:%d",
1706 port->icount.brk);
1707 if (port->icount.overrun)
1708 ret += sprintf(buf + ret, " oe:%d",
1709 port->icount.overrun);
1711 #define INFOBIT(bit,str) \
1712 if (port->mctrl & (bit)) \
1713 strncat(stat_buf, (str), sizeof(stat_buf) - \
1714 strlen(stat_buf) - 2)
1715 #define STATBIT(bit,str) \
1716 if (status & (bit)) \
1717 strncat(stat_buf, (str), sizeof(stat_buf) - \
1718 strlen(stat_buf) - 2)
1720 stat_buf[0] = '\0';
1721 stat_buf[1] = '\0';
1722 INFOBIT(TIOCM_RTS, "|RTS");
1723 STATBIT(TIOCM_CTS, "|CTS");
1724 INFOBIT(TIOCM_DTR, "|DTR");
1725 STATBIT(TIOCM_DSR, "|DSR");
1726 STATBIT(TIOCM_CAR, "|CD");
1727 STATBIT(TIOCM_RNG, "|RI");
1728 if (stat_buf[0])
1729 stat_buf[0] = ' ';
1730 strcat(stat_buf, "\n");
1732 ret += sprintf(buf + ret, stat_buf);
1733 } else {
1734 strcat(buf, "\n");
1735 ret++;
1737 #undef STATBIT
1738 #undef INFOBIT
1739 return ret;
1742 static int uart_read_proc(char *page, char **start, off_t off,
1743 int count, int *eof, void *data)
1745 struct tty_driver *ttydrv = data;
1746 struct uart_driver *drv = ttydrv->driver_state;
1747 int i, len = 0, l;
1748 off_t begin = 0;
1750 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1751 "", "", "");
1752 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1753 l = uart_line_info(page + len, drv, i);
1754 len += l;
1755 if (len + begin > off + count)
1756 goto done;
1757 if (len + begin < off) {
1758 begin += len;
1759 len = 0;
1762 *eof = 1;
1763 done:
1764 if (off >= len + begin)
1765 return 0;
1766 *start = page + (off - begin);
1767 return (count < begin + len - off) ? count : (begin + len - off);
1769 #endif
1771 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1773 * uart_console_write - write a console message to a serial port
1774 * @port: the port to write the message
1775 * @s: array of characters
1776 * @count: number of characters in string to write
1777 * @write: function to write character to port
1779 void uart_console_write(struct uart_port *port, const char *s,
1780 unsigned int count,
1781 void (*putchar)(struct uart_port *, int))
1783 unsigned int i;
1785 for (i = 0; i < count; i++, s++) {
1786 if (*s == '\n')
1787 putchar(port, '\r');
1788 putchar(port, *s);
1791 EXPORT_SYMBOL_GPL(uart_console_write);
1794 * Check whether an invalid uart number has been specified, and
1795 * if so, search for the first available port that does have
1796 * console support.
1798 struct uart_port * __init
1799 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1801 int idx = co->index;
1803 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1804 ports[idx].membase == NULL))
1805 for (idx = 0; idx < nr; idx++)
1806 if (ports[idx].iobase != 0 ||
1807 ports[idx].membase != NULL)
1808 break;
1810 co->index = idx;
1812 return ports + idx;
1816 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1817 * @options: pointer to option string
1818 * @baud: pointer to an 'int' variable for the baud rate.
1819 * @parity: pointer to an 'int' variable for the parity.
1820 * @bits: pointer to an 'int' variable for the number of data bits.
1821 * @flow: pointer to an 'int' variable for the flow control character.
1823 * uart_parse_options decodes a string containing the serial console
1824 * options. The format of the string is <baud><parity><bits><flow>,
1825 * eg: 115200n8r
1827 void __init
1828 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1830 char *s = options;
1832 *baud = simple_strtoul(s, NULL, 10);
1833 while (*s >= '0' && *s <= '9')
1834 s++;
1835 if (*s)
1836 *parity = *s++;
1837 if (*s)
1838 *bits = *s++ - '0';
1839 if (*s)
1840 *flow = *s;
1843 struct baud_rates {
1844 unsigned int rate;
1845 unsigned int cflag;
1848 static const struct baud_rates baud_rates[] = {
1849 { 921600, B921600 },
1850 { 460800, B460800 },
1851 { 230400, B230400 },
1852 { 115200, B115200 },
1853 { 57600, B57600 },
1854 { 38400, B38400 },
1855 { 19200, B19200 },
1856 { 9600, B9600 },
1857 { 4800, B4800 },
1858 { 2400, B2400 },
1859 { 1200, B1200 },
1860 { 0, B38400 }
1864 * uart_set_options - setup the serial console parameters
1865 * @port: pointer to the serial ports uart_port structure
1866 * @co: console pointer
1867 * @baud: baud rate
1868 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1869 * @bits: number of data bits
1870 * @flow: flow control character - 'r' (rts)
1872 int __init
1873 uart_set_options(struct uart_port *port, struct console *co,
1874 int baud, int parity, int bits, int flow)
1876 struct ktermios termios;
1877 int i;
1880 * Ensure that the serial console lock is initialised
1881 * early.
1883 spin_lock_init(&port->lock);
1884 lockdep_set_class(&port->lock, &port_lock_key);
1886 memset(&termios, 0, sizeof(struct ktermios));
1888 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1891 * Construct a cflag setting.
1893 for (i = 0; baud_rates[i].rate; i++)
1894 if (baud_rates[i].rate <= baud)
1895 break;
1897 termios.c_cflag |= baud_rates[i].cflag;
1899 if (bits == 7)
1900 termios.c_cflag |= CS7;
1901 else
1902 termios.c_cflag |= CS8;
1904 switch (parity) {
1905 case 'o': case 'O':
1906 termios.c_cflag |= PARODD;
1907 /*fall through*/
1908 case 'e': case 'E':
1909 termios.c_cflag |= PARENB;
1910 break;
1913 if (flow == 'r')
1914 termios.c_cflag |= CRTSCTS;
1916 port->ops->set_termios(port, &termios, NULL);
1917 co->cflag = termios.c_cflag;
1919 return 0;
1921 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1923 static void uart_change_pm(struct uart_state *state, int pm_state)
1925 struct uart_port *port = state->port;
1927 if (state->pm_state != pm_state) {
1928 if (port->ops->pm)
1929 port->ops->pm(port, pm_state, state->pm_state);
1930 state->pm_state = pm_state;
1934 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1936 struct uart_state *state = drv->state + port->line;
1938 mutex_lock(&state->mutex);
1940 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1941 if (uart_console(port)) {
1942 mutex_unlock(&state->mutex);
1943 return 0;
1945 #endif
1947 if (state->info && state->info->flags & UIF_INITIALIZED) {
1948 const struct uart_ops *ops = port->ops;
1950 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
1951 | UIF_SUSPENDED;
1953 spin_lock_irq(&port->lock);
1954 ops->stop_tx(port);
1955 ops->set_mctrl(port, 0);
1956 ops->stop_rx(port);
1957 spin_unlock_irq(&port->lock);
1960 * Wait for the transmitter to empty.
1962 while (!ops->tx_empty(port)) {
1963 msleep(10);
1966 ops->shutdown(port);
1970 * Disable the console device before suspending.
1972 if (uart_console(port))
1973 console_stop(port->cons);
1975 uart_change_pm(state, 3);
1977 mutex_unlock(&state->mutex);
1979 return 0;
1982 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1984 struct uart_state *state = drv->state + port->line;
1986 mutex_lock(&state->mutex);
1988 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
1989 if (uart_console(port)) {
1990 mutex_unlock(&state->mutex);
1991 return 0;
1993 #endif
1995 uart_change_pm(state, 0);
1998 * Re-enable the console device after suspending.
2000 if (uart_console(port)) {
2001 struct ktermios termios;
2004 * First try to use the console cflag setting.
2006 memset(&termios, 0, sizeof(struct ktermios));
2007 termios.c_cflag = port->cons->cflag;
2010 * If that's unset, use the tty termios setting.
2012 if (state->info && state->info->tty && termios.c_cflag == 0)
2013 termios = *state->info->tty->termios;
2015 port->ops->set_termios(port, &termios, NULL);
2016 console_start(port->cons);
2019 if (state->info && state->info->flags & UIF_SUSPENDED) {
2020 const struct uart_ops *ops = port->ops;
2021 int ret;
2023 ops->set_mctrl(port, 0);
2024 ret = ops->startup(port);
2025 if (ret == 0) {
2026 uart_change_speed(state, NULL);
2027 spin_lock_irq(&port->lock);
2028 ops->set_mctrl(port, port->mctrl);
2029 ops->start_tx(port);
2030 spin_unlock_irq(&port->lock);
2031 state->info->flags |= UIF_INITIALIZED;
2032 } else {
2034 * Failed to resume - maybe hardware went away?
2035 * Clear the "initialized" flag so we won't try
2036 * to call the low level drivers shutdown method.
2038 uart_shutdown(state);
2041 state->info->flags &= ~UIF_SUSPENDED;
2044 mutex_unlock(&state->mutex);
2046 return 0;
2049 static inline void
2050 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2052 char address[64];
2054 switch (port->iotype) {
2055 case UPIO_PORT:
2056 snprintf(address, sizeof(address),
2057 "I/O 0x%x", port->iobase);
2058 break;
2059 case UPIO_HUB6:
2060 snprintf(address, sizeof(address),
2061 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
2062 break;
2063 case UPIO_MEM:
2064 case UPIO_MEM32:
2065 case UPIO_AU:
2066 case UPIO_TSI:
2067 snprintf(address, sizeof(address),
2068 "MMIO 0x%lx", port->mapbase);
2069 break;
2070 default:
2071 strlcpy(address, "*unknown*", sizeof(address));
2072 break;
2075 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2076 port->dev ? port->dev->bus_id : "",
2077 port->dev ? ": " : "",
2078 drv->dev_name, port->line, address, port->irq, uart_type(port));
2081 static void
2082 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2083 struct uart_port *port)
2085 unsigned int flags;
2088 * If there isn't a port here, don't do anything further.
2090 if (!port->iobase && !port->mapbase && !port->membase)
2091 return;
2094 * Now do the auto configuration stuff. Note that config_port
2095 * is expected to claim the resources and map the port for us.
2097 flags = UART_CONFIG_TYPE;
2098 if (port->flags & UPF_AUTO_IRQ)
2099 flags |= UART_CONFIG_IRQ;
2100 if (port->flags & UPF_BOOT_AUTOCONF) {
2101 port->type = PORT_UNKNOWN;
2102 port->ops->config_port(port, flags);
2105 if (port->type != PORT_UNKNOWN) {
2106 unsigned long flags;
2108 uart_report_port(drv, port);
2110 /* Power up port for set_mctrl() */
2111 uart_change_pm(state, 0);
2114 * Ensure that the modem control lines are de-activated.
2115 * We probably don't need a spinlock around this, but
2117 spin_lock_irqsave(&port->lock, flags);
2118 port->ops->set_mctrl(port, 0);
2119 spin_unlock_irqrestore(&port->lock, flags);
2122 * Power down all ports by default, except the
2123 * console if we have one.
2125 if (!uart_console(port))
2126 uart_change_pm(state, 3);
2130 static const struct tty_operations uart_ops = {
2131 .open = uart_open,
2132 .close = uart_close,
2133 .write = uart_write,
2134 .put_char = uart_put_char,
2135 .flush_chars = uart_flush_chars,
2136 .write_room = uart_write_room,
2137 .chars_in_buffer= uart_chars_in_buffer,
2138 .flush_buffer = uart_flush_buffer,
2139 .ioctl = uart_ioctl,
2140 .throttle = uart_throttle,
2141 .unthrottle = uart_unthrottle,
2142 .send_xchar = uart_send_xchar,
2143 .set_termios = uart_set_termios,
2144 .stop = uart_stop,
2145 .start = uart_start,
2146 .hangup = uart_hangup,
2147 .break_ctl = uart_break_ctl,
2148 .wait_until_sent= uart_wait_until_sent,
2149 #ifdef CONFIG_PROC_FS
2150 .read_proc = uart_read_proc,
2151 #endif
2152 .tiocmget = uart_tiocmget,
2153 .tiocmset = uart_tiocmset,
2157 * uart_register_driver - register a driver with the uart core layer
2158 * @drv: low level driver structure
2160 * Register a uart driver with the core driver. We in turn register
2161 * with the tty layer, and initialise the core driver per-port state.
2163 * We have a proc file in /proc/tty/driver which is named after the
2164 * normal driver.
2166 * drv->port should be NULL, and the per-port structures should be
2167 * registered using uart_add_one_port after this call has succeeded.
2169 int uart_register_driver(struct uart_driver *drv)
2171 struct tty_driver *normal = NULL;
2172 int i, retval;
2174 BUG_ON(drv->state);
2177 * Maybe we should be using a slab cache for this, especially if
2178 * we have a large number of ports to handle.
2180 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2181 retval = -ENOMEM;
2182 if (!drv->state)
2183 goto out;
2185 normal = alloc_tty_driver(drv->nr);
2186 if (!normal)
2187 goto out;
2189 drv->tty_driver = normal;
2191 normal->owner = drv->owner;
2192 normal->driver_name = drv->driver_name;
2193 normal->name = drv->dev_name;
2194 normal->major = drv->major;
2195 normal->minor_start = drv->minor;
2196 normal->type = TTY_DRIVER_TYPE_SERIAL;
2197 normal->subtype = SERIAL_TYPE_NORMAL;
2198 normal->init_termios = tty_std_termios;
2199 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2200 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2201 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2202 normal->driver_state = drv;
2203 tty_set_operations(normal, &uart_ops);
2206 * Initialise the UART state(s).
2208 for (i = 0; i < drv->nr; i++) {
2209 struct uart_state *state = drv->state + i;
2211 state->close_delay = 500; /* .5 seconds */
2212 state->closing_wait = 30000; /* 30 seconds */
2214 mutex_init(&state->mutex);
2217 retval = tty_register_driver(normal);
2218 out:
2219 if (retval < 0) {
2220 put_tty_driver(normal);
2221 kfree(drv->state);
2223 return retval;
2227 * uart_unregister_driver - remove a driver from the uart core layer
2228 * @drv: low level driver structure
2230 * Remove all references to a driver from the core driver. The low
2231 * level driver must have removed all its ports via the
2232 * uart_remove_one_port() if it registered them with uart_add_one_port().
2233 * (ie, drv->port == NULL)
2235 void uart_unregister_driver(struct uart_driver *drv)
2237 struct tty_driver *p = drv->tty_driver;
2238 tty_unregister_driver(p);
2239 put_tty_driver(p);
2240 kfree(drv->state);
2241 drv->tty_driver = NULL;
2244 struct tty_driver *uart_console_device(struct console *co, int *index)
2246 struct uart_driver *p = co->data;
2247 *index = co->index;
2248 return p->tty_driver;
2252 * uart_add_one_port - attach a driver-defined port structure
2253 * @drv: pointer to the uart low level driver structure for this port
2254 * @port: uart port structure to use for this port.
2256 * This allows the driver to register its own uart_port structure
2257 * with the core driver. The main purpose is to allow the low
2258 * level uart drivers to expand uart_port, rather than having yet
2259 * more levels of structures.
2261 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2263 struct uart_state *state;
2264 int ret = 0;
2266 BUG_ON(in_interrupt());
2268 if (port->line >= drv->nr)
2269 return -EINVAL;
2271 state = drv->state + port->line;
2273 mutex_lock(&port_mutex);
2274 mutex_lock(&state->mutex);
2275 if (state->port) {
2276 ret = -EINVAL;
2277 goto out;
2280 state->port = port;
2282 port->cons = drv->cons;
2283 port->info = state->info;
2286 * If this port is a console, then the spinlock is already
2287 * initialised.
2289 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2290 spin_lock_init(&port->lock);
2291 lockdep_set_class(&port->lock, &port_lock_key);
2294 uart_configure_port(drv, state, port);
2297 * Register the port whether it's detected or not. This allows
2298 * setserial to be used to alter this ports parameters.
2300 tty_register_device(drv->tty_driver, port->line, port->dev);
2303 * If this driver supports console, and it hasn't been
2304 * successfully registered yet, try to re-register it.
2305 * It may be that the port was not available.
2307 if (port->type != PORT_UNKNOWN &&
2308 port->cons && !(port->cons->flags & CON_ENABLED))
2309 register_console(port->cons);
2312 * Ensure UPF_DEAD is not set.
2314 port->flags &= ~UPF_DEAD;
2316 out:
2317 mutex_unlock(&state->mutex);
2318 mutex_unlock(&port_mutex);
2320 return ret;
2324 * uart_remove_one_port - detach a driver defined port structure
2325 * @drv: pointer to the uart low level driver structure for this port
2326 * @port: uart port structure for this port
2328 * This unhooks (and hangs up) the specified port structure from the
2329 * core driver. No further calls will be made to the low-level code
2330 * for this port.
2332 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2334 struct uart_state *state = drv->state + port->line;
2335 struct uart_info *info;
2337 BUG_ON(in_interrupt());
2339 if (state->port != port)
2340 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2341 state->port, port);
2343 mutex_lock(&port_mutex);
2346 * Mark the port "dead" - this prevents any opens from
2347 * succeeding while we shut down the port.
2349 mutex_lock(&state->mutex);
2350 port->flags |= UPF_DEAD;
2351 mutex_unlock(&state->mutex);
2354 * Remove the devices from the tty layer
2356 tty_unregister_device(drv->tty_driver, port->line);
2358 info = state->info;
2359 if (info && info->tty)
2360 tty_vhangup(info->tty);
2363 * All users of this port should now be disconnected from
2364 * this driver, and the port shut down. We should be the
2365 * only thread fiddling with this port from now on.
2367 state->info = NULL;
2370 * Free the port IO and memory resources, if any.
2372 if (port->type != PORT_UNKNOWN)
2373 port->ops->release_port(port);
2376 * Indicate that there isn't a port here anymore.
2378 port->type = PORT_UNKNOWN;
2381 * Kill the tasklet, and free resources.
2383 if (info) {
2384 tasklet_kill(&info->tlet);
2385 kfree(info);
2388 state->port = NULL;
2389 mutex_unlock(&port_mutex);
2391 return 0;
2395 * Are the two ports equivalent?
2397 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2399 if (port1->iotype != port2->iotype)
2400 return 0;
2402 switch (port1->iotype) {
2403 case UPIO_PORT:
2404 return (port1->iobase == port2->iobase);
2405 case UPIO_HUB6:
2406 return (port1->iobase == port2->iobase) &&
2407 (port1->hub6 == port2->hub6);
2408 case UPIO_MEM:
2409 case UPIO_MEM32:
2410 case UPIO_AU:
2411 case UPIO_TSI:
2412 return (port1->mapbase == port2->mapbase);
2414 return 0;
2416 EXPORT_SYMBOL(uart_match_port);
2418 EXPORT_SYMBOL(uart_write_wakeup);
2419 EXPORT_SYMBOL(uart_register_driver);
2420 EXPORT_SYMBOL(uart_unregister_driver);
2421 EXPORT_SYMBOL(uart_suspend_port);
2422 EXPORT_SYMBOL(uart_resume_port);
2423 EXPORT_SYMBOL(uart_add_one_port);
2424 EXPORT_SYMBOL(uart_remove_one_port);
2426 MODULE_DESCRIPTION("Serial driver core");
2427 MODULE_LICENSE("GPL");