[PATCH] v4l: 771: the wm8775 is a wolfson microelectronics 24 bit 96khz adc with 4
[linux-2.6/suspend2-2.6.18.git] / drivers / serial / serial_core.c
blob427a23858076ffe03fcf9a166b7ca852a37533cb
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/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>
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 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)
58 #else
59 #define uart_console(port) (0)
60 #endif
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;
80 unsigned long flags;
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;
101 unsigned long flags;
103 spin_lock_irqsave(&port->lock, flags);
104 __uart_start(tty);
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);
114 static inline void
115 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
117 unsigned long flags;
118 unsigned int old;
120 spin_lock_irqsave(&port->lock, flags);
121 old = port->mctrl;
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;
139 unsigned long page;
140 int retval = 0;
142 if (info->flags & UIF_INITIALIZED)
143 return 0;
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)
153 return 0;
156 * Initialise and allocate the transmit and temporary
157 * buffer.
159 if (!info->xmit.buf) {
160 page = get_zeroed_page(GFP_KERNEL);
161 if (!page)
162 return -ENOMEM;
164 info->xmit.buf = (unsigned char *) page;
165 uart_circ_clear(&info->xmit);
168 retval = port->ops->startup(port);
169 if (retval == 0) {
170 if (init_hw) {
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))
197 retval = 0;
199 return retval;
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))
213 return;
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);
252 if (info->tty)
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.
267 void
268 uart_update_timeout(struct uart_port *port, unsigned int cflag,
269 unsigned int baud)
271 unsigned int bits;
273 /* byte size and parity */
274 switch (cflag & CSIZE) {
275 case CS5:
276 bits = 7;
277 break;
278 case CS6:
279 bits = 8;
280 break;
281 case CS7:
282 bits = 9;
283 break;
284 default:
285 bits = 10;
286 break; // CS8
289 if (cflag & CSTOPB)
290 bits++;
291 if (cflag & PARENB)
292 bits++;
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.
326 unsigned int
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)
334 altbaud = 57600;
335 if (flags == UPF_SPD_VHI)
336 altbaud = 115200;
337 if (flags == UPF_SPD_SHI)
338 altbaud = 230400;
339 if (flags == UPF_SPD_WARP)
340 altbaud = 460800;
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...
347 * Die! Die! Die!
349 if (baud == 38400)
350 baud = altbaud;
353 * Special case: B0 rate.
355 if (baud == 0)
356 baud = 9600;
358 if (baud >= min && baud <= max)
359 return baud;
362 * Oops, the quotient was zero. Try again with
363 * the old baud rate if possible.
365 termios->c_cflag &= ~CBAUD;
366 if (old) {
367 termios->c_cflag |= old->c_cflag & CBAUD;
368 old = NULL;
369 continue;
373 * As a last resort, if the quotient is zero,
374 * default to 9600 bps
376 termios->c_cflag |= B9600;
379 return 0;
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.
391 unsigned int
392 uart_get_divisor(struct uart_port *port, unsigned int baud)
394 unsigned int quot;
397 * Old custom speed handling.
399 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
400 quot = port->custom_divisor;
401 else
402 quot = (port->uartclk + (8 * baud)) / (16 * baud);
404 return quot;
407 EXPORT_SYMBOL(uart_get_divisor);
409 static void
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)
421 return;
423 termios = tty->termios;
426 * Set flags based on termios cflag
428 if (termios->c_cflag & CRTSCTS)
429 state->info->flags |= UIF_CTS_FLOW;
430 else
431 state->info->flags &= ~UIF_CTS_FLOW;
433 if (termios->c_cflag & CLOCAL)
434 state->info->flags &= ~UIF_CHECK_CD;
435 else
436 state->info->flags |= UIF_CHECK_CD;
438 port->ops->set_termios(port, termios, old_termios);
441 static inline void
442 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
444 unsigned long flags;
446 if (!circ->buf)
447 return;
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)
466 uart_start(tty);
469 static int
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;
475 unsigned long flags;
476 int c, ret = 0;
478 if (!circ->buf)
479 return 0;
481 spin_lock_irqsave(&port->lock, flags);
482 while (1) {
483 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
484 if (count < c)
485 c = count;
486 if (c <= 0)
487 break;
488 memcpy(circ->buf + circ->head, buf, c);
489 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
490 buf += c;
491 count -= c;
492 ret += c;
494 spin_unlock_irqrestore(&port->lock, flags);
496 uart_start(tty);
497 return ret;
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;
518 unsigned long flags;
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);
525 tty_wakeup(tty);
529 * This function is used to send a high-priority XON/XOFF character to
530 * the device
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;
536 unsigned long flags;
538 if (port->ops->send_xchar)
539 port->ops->send_xchar(port, ch);
540 else {
541 port->x_char = ch;
542 if (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;
554 if (I_IXOFF(tty))
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;
566 if (I_IXOFF(tty)) {
567 if (port->x_char)
568 port->x_char = 0;
569 else
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;
589 tmp.irq = port->irq;
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)))
604 return -EFAULT;
605 return 0;
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;
616 int retval = 0;
618 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
619 return -EFAULT;
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
635 * under us.
637 down(&state->sem);
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
644 * IO port changes.
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)) {
657 retval = -EPERM;
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))
664 goto exit;
665 port->flags = ((port->flags & ~UPF_USR_MASK) |
666 (new_serial.flags & UPF_USR_MASK));
667 port->custom_divisor = new_serial.custom_divisor;
668 goto check_and_exit;
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))
679 retval = -EINVAL;
681 if (retval)
682 goto exit;
684 if (change_port || change_irq) {
685 retval = -EBUSY;
688 * Make sure that we are the sole user of this port.
690 if (uart_users(state) > 1)
691 goto exit;
694 * We need to shutdown the serial port at the old
695 * port/type/irq combination.
697 uart_shutdown(state);
700 if (change_port) {
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);
729 } else {
730 /* Always success - Jean II */
731 retval = 0;
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,
748 * we fail like this.
750 if (retval)
751 port->type = PORT_UNKNOWN;
754 * We failed anyway.
756 retval = -EBUSY;
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;
772 check_and_exit:
773 retval = 0;
774 if (port->type == PORT_UNKNOWN)
775 goto exit;
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) {
785 char buf[64];
786 printk(KERN_NOTICE
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);
793 } else
794 retval = uart_startup(state, 1);
795 exit:
796 up(&state->sem);
797 return retval;
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;
809 unsigned int result;
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).
819 if (port->x_char ||
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;
831 int result = -EIO;
833 down(&state->sem);
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);
842 up(&state->sem);
844 return result;
847 static int
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;
853 int ret = -EIO;
855 down(&state->sem);
856 if ((!file || !tty_hung_up_p(file)) &&
857 !(tty->flags & (1 << TTY_IO_ERROR))) {
858 uart_update_mctrl(port, set, clear);
859 ret = 0;
861 up(&state->sem);
862 return ret;
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());
872 down(&state->sem);
874 if (port->type != PORT_UNKNOWN)
875 port->ops->break_ctl(port, break_state);
877 up(&state->sem);
880 static int uart_do_autoconfig(struct uart_state *state)
882 struct uart_port *port = state->port;
883 int flags, ret;
885 if (!capable(CAP_SYS_ADMIN))
886 return -EPERM;
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))
894 return -ERESTARTSYS;
896 ret = -EBUSY;
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
913 * a port is found.
915 port->ops->config_port(port, flags);
917 ret = uart_startup(state, 1);
919 up(&state->sem);
920 return ret;
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
929 static int
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;
935 int ret;
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);
950 for (;;) {
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))) {
961 ret = 0;
962 break;
965 schedule();
967 /* see if a signal did it */
968 if (signal_pending(current)) {
969 ret = -ERESTARTSYS;
970 break;
973 cprev = cnow;
976 current->state = TASK_RUNNING;
977 remove_wait_queue(&state->info->delta_msr_wait, &wait);
979 return ret;
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.
1017 static int
1018 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1019 unsigned long arg)
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.
1030 switch (cmd) {
1031 case TIOCGSERIAL:
1032 ret = uart_get_info(state, uarg);
1033 break;
1035 case TIOCSSERIAL:
1036 ret = uart_set_info(state, uarg);
1037 break;
1039 case TIOCSERCONFIG:
1040 ret = uart_do_autoconfig(state);
1041 break;
1043 case TIOCSERGWILD: /* obsolete */
1044 case TIOCSERSWILD: /* obsolete */
1045 ret = 0;
1046 break;
1049 if (ret != -ENOIOCTLCMD)
1050 goto out;
1052 if (tty->flags & (1 << TTY_IO_ERROR)) {
1053 ret = -EIO;
1054 goto out;
1058 * The following should only be used when hardware is present.
1060 switch (cmd) {
1061 case TIOCMIWAIT:
1062 ret = uart_wait_modem_status(state, arg);
1063 break;
1065 case TIOCGICOUNT:
1066 ret = uart_get_count(state, uarg);
1067 break;
1070 if (ret != -ENOIOCTLCMD)
1071 goto out;
1073 down(&state->sem);
1075 if (tty_hung_up_p(filp)) {
1076 ret = -EIO;
1077 goto out_up;
1081 * All these rely on hardware being present and need to be
1082 * protected against the tty being hung up.
1084 switch (cmd) {
1085 case TIOCSERGETLSR: /* Get line status register */
1086 ret = uart_get_lsr_info(state, uarg);
1087 break;
1089 default: {
1090 struct uart_port *port = state->port;
1091 if (port->ops->ioctl)
1092 ret = port->ops->ioctl(port, cmd, arg);
1093 break;
1096 out_up:
1097 up(&state->sem);
1098 out:
1099 return ret;
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)
1118 return;
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))
1131 mask |= TIOCM_RTS;
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;
1139 __uart_start(tty);
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);
1153 #if 0
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);
1163 #endif
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)
1179 return;
1181 port = state->port;
1183 DPRINTK("uart_close(%d) called\n", port->line);
1185 down(&state->sem);
1187 if (tty_hung_up_p(filp))
1188 goto done;
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);
1200 state->count = 1;
1202 if (--state->count < 0) {
1203 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1204 tty->name, state->count);
1205 state->count = 0;
1207 if (state->count)
1208 goto done;
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.
1215 tty->closing = 1;
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);
1242 tty->closing = 0;
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);
1258 done:
1259 up(&state->sem);
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)
1271 return;
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
1279 * the NIST-PCTS.
1281 char_time = (port->timeout - HZ/50) / port->fifosize;
1282 char_time = char_time / 5;
1283 if (char_time == 0)
1284 char_time = 1;
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
1295 * 2*port->timeout.
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
1308 * we wait.
1310 while (!port->ops->tx_empty(port)) {
1311 msleep_interruptible(jiffies_to_msecs(char_time));
1312 if (signal_pending(current))
1313 break;
1314 if (time_after(jiffies, expire))
1315 break;
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);
1333 down(&state->sem);
1334 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1335 uart_flush_buffer(tty);
1336 uart_shutdown(state);
1337 state->count = 0;
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);
1343 up(&state->sem);
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.
1385 static int
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;
1391 unsigned int mctrl;
1393 info->blocked_open++;
1394 state->count--;
1396 add_wait_queue(&info->open_wait, &wait);
1397 while (1) {
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)
1404 break;
1407 * If the port has been closed, tell userspace/restart open.
1409 if (!(info->flags & UIF_INITIALIZED))
1410 break;
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))) {
1424 break;
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)
1443 break;
1445 up(&state->sem);
1446 schedule();
1447 down(&state->sem);
1449 if (signal_pending(current))
1450 break;
1452 set_current_state(TASK_RUNNING);
1453 remove_wait_queue(&info->open_wait, &wait);
1455 state->count++;
1456 info->blocked_open--;
1458 if (signal_pending(current))
1459 return -ERESTARTSYS;
1461 if (!info->tty || tty_hung_up_p(filp))
1462 return -EAGAIN;
1464 return 0;
1467 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1469 struct uart_state *state;
1471 down(&port_sem);
1472 state = drv->state + line;
1473 if (down_interruptible(&state->sem)) {
1474 state = ERR_PTR(-ERESTARTSYS);
1475 goto out;
1478 state->count++;
1479 if (!state->port) {
1480 state->count--;
1481 up(&state->sem);
1482 state = ERR_PTR(-ENXIO);
1483 goto out;
1486 if (!state->info) {
1487 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1488 if (state->info) {
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);
1500 } else {
1501 state->count--;
1502 up(&state->sem);
1503 state = ERR_PTR(-ENOMEM);
1507 out:
1508 up(&port_sem);
1509 return state;
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()).
1536 retval = -ENODEV;
1537 if (line >= tty->driver->num)
1538 goto fail;
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);
1550 goto fail;
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;
1560 tty->alt_speed = 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)) {
1567 retval = -EAGAIN;
1568 state->count--;
1569 up(&state->sem);
1570 goto fail;
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.
1587 if (retval == 0)
1588 retval = uart_block_til_ready(filp, state);
1589 up(&state->sem);
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);
1600 fail:
1601 return retval;
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);
1611 if (!str)
1612 str = "unknown";
1614 return str;
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;
1623 char stat_buf[32];
1624 unsigned int status;
1625 int ret;
1627 if (!port)
1628 return 0;
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,
1635 port->irq);
1637 if (port->type == PORT_UNKNOWN) {
1638 strcat(buf, "\n");
1639 return ret + 1;
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",
1658 port->icount.brk);
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)
1672 stat_buf[0] = '\0';
1673 stat_buf[1] = '\0';
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");
1680 if (stat_buf[0])
1681 stat_buf[0] = ' ';
1682 strcat(stat_buf, "\n");
1684 ret += sprintf(buf + ret, stat_buf);
1685 } else {
1686 strcat(buf, "\n");
1687 ret++;
1689 #undef STATBIT
1690 #undef INFOBIT
1691 return ret;
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;
1699 int i, len = 0, l;
1700 off_t begin = 0;
1702 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1703 "", "", "");
1704 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1705 l = uart_line_info(page + len, drv, i);
1706 len += l;
1707 if (len + begin > off + count)
1708 goto done;
1709 if (len + begin < off) {
1710 begin += len;
1711 len = 0;
1714 *eof = 1;
1715 done:
1716 if (off >= len + begin)
1717 return 0;
1718 *start = page + (off - begin);
1719 return (count < begin + len - off) ? count : (begin + len - off);
1721 #endif
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
1727 * console support.
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)
1739 break;
1741 co->index = idx;
1743 return ports + idx;
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>,
1756 * eg: 115200n8r
1758 void __init
1759 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1761 char *s = options;
1763 *baud = simple_strtoul(s, NULL, 10);
1764 while (*s >= '0' && *s <= '9')
1765 s++;
1766 if (*s)
1767 *parity = *s++;
1768 if (*s)
1769 *bits = *s++ - '0';
1770 if (*s)
1771 *flow = *s;
1774 struct baud_rates {
1775 unsigned int rate;
1776 unsigned int cflag;
1779 static struct baud_rates baud_rates[] = {
1780 { 921600, B921600 },
1781 { 460800, B460800 },
1782 { 230400, B230400 },
1783 { 115200, B115200 },
1784 { 57600, B57600 },
1785 { 38400, B38400 },
1786 { 19200, B19200 },
1787 { 9600, B9600 },
1788 { 4800, B4800 },
1789 { 2400, B2400 },
1790 { 1200, B1200 },
1791 { 0, B38400 }
1795 * uart_set_options - setup the serial console parameters
1796 * @port: pointer to the serial ports uart_port structure
1797 * @co: console pointer
1798 * @baud: baud rate
1799 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1800 * @bits: number of data bits
1801 * @flow: flow control character - 'r' (rts)
1803 int __init
1804 uart_set_options(struct uart_port *port, struct console *co,
1805 int baud, int parity, int bits, int flow)
1807 struct termios termios;
1808 int i;
1811 * Ensure that the serial console lock is initialised
1812 * early.
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)
1825 break;
1827 termios.c_cflag |= baud_rates[i].cflag;
1829 if (bits == 7)
1830 termios.c_cflag |= CS7;
1831 else
1832 termios.c_cflag |= CS8;
1834 switch (parity) {
1835 case 'o': case 'O':
1836 termios.c_cflag |= PARODD;
1837 /*fall through*/
1838 case 'e': case 'E':
1839 termios.c_cflag |= PARENB;
1840 break;
1843 if (flow == 'r')
1844 termios.c_cflag |= CRTSCTS;
1846 port->ops->set_termios(port, &termios, NULL);
1847 co->cflag = termios.c_cflag;
1849 return 0;
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;
1856 if (port->ops->pm)
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;
1865 down(&state->sem);
1867 if (state->info && state->info->flags & UIF_INITIALIZED) {
1868 struct uart_ops *ops = port->ops;
1870 spin_lock_irq(&port->lock);
1871 ops->stop_tx(port);
1872 ops->set_mctrl(port, 0);
1873 ops->stop_rx(port);
1874 spin_unlock_irq(&port->lock);
1877 * Wait for the transmitter to empty.
1879 while (!ops->tx_empty(port)) {
1880 msleep(10);
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);
1894 up(&state->sem);
1896 return 0;
1899 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1901 struct uart_state *state = drv->state + port->line;
1903 down(&state->sem);
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);
1933 ops->startup(port);
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);
1941 up(&state->sem);
1943 return 0;
1946 static inline void
1947 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1949 char address[64];
1951 switch (port->iotype) {
1952 case UPIO_PORT:
1953 snprintf(address, sizeof(address),
1954 "I/O 0x%x", port->iobase);
1955 break;
1956 case UPIO_HUB6:
1957 snprintf(address, sizeof(address),
1958 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1959 break;
1960 case UPIO_MEM:
1961 case UPIO_MEM32:
1962 case UPIO_AU:
1963 snprintf(address, sizeof(address),
1964 "MMIO 0x%lx", port->mapbase);
1965 break;
1966 default:
1967 strlcpy(address, "*unknown*", sizeof(address));
1968 break;
1971 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
1972 port->dev ? port->dev->bus_id : "",
1973 port->dev ? ": " : "",
1974 drv->dev_name, port->line, address, port->irq, uart_type(port));
1977 static void
1978 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1979 struct uart_port *port)
1981 unsigned int flags;
1984 * If there isn't a port here, don't do anything further.
1986 if (!port->iobase && !port->mapbase && !port->membase)
1987 return;
1990 * Now do the auto configuration stuff. Note that config_port
1991 * is expected to claim the resources and map the port for us.
1993 flags = UART_CONFIG_TYPE;
1994 if (port->flags & UPF_AUTO_IRQ)
1995 flags |= UART_CONFIG_IRQ;
1996 if (port->flags & UPF_BOOT_AUTOCONF) {
1997 port->type = PORT_UNKNOWN;
1998 port->ops->config_port(port, flags);
2001 if (port->type != PORT_UNKNOWN) {
2002 unsigned long flags;
2004 uart_report_port(drv, port);
2007 * Ensure that the modem control lines are de-activated.
2008 * We probably don't need a spinlock around this, but
2010 spin_lock_irqsave(&port->lock, flags);
2011 port->ops->set_mctrl(port, 0);
2012 spin_unlock_irqrestore(&port->lock, flags);
2015 * Power down all ports by default, except the
2016 * console if we have one.
2018 if (!uart_console(port))
2019 uart_change_pm(state, 3);
2024 * This reverses the effects of uart_configure_port, hanging up the
2025 * port before removal.
2027 static void
2028 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2030 struct uart_port *port = state->port;
2031 struct uart_info *info = state->info;
2033 if (info && info->tty)
2034 tty_vhangup(info->tty);
2036 down(&state->sem);
2038 state->info = NULL;
2041 * Free the port IO and memory resources, if any.
2043 if (port->type != PORT_UNKNOWN)
2044 port->ops->release_port(port);
2047 * Indicate that there isn't a port here anymore.
2049 port->type = PORT_UNKNOWN;
2052 * Kill the tasklet, and free resources.
2054 if (info) {
2055 tasklet_kill(&info->tlet);
2056 kfree(info);
2059 up(&state->sem);
2062 static struct tty_operations uart_ops = {
2063 .open = uart_open,
2064 .close = uart_close,
2065 .write = uart_write,
2066 .put_char = uart_put_char,
2067 .flush_chars = uart_flush_chars,
2068 .write_room = uart_write_room,
2069 .chars_in_buffer= uart_chars_in_buffer,
2070 .flush_buffer = uart_flush_buffer,
2071 .ioctl = uart_ioctl,
2072 .throttle = uart_throttle,
2073 .unthrottle = uart_unthrottle,
2074 .send_xchar = uart_send_xchar,
2075 .set_termios = uart_set_termios,
2076 .stop = uart_stop,
2077 .start = uart_start,
2078 .hangup = uart_hangup,
2079 .break_ctl = uart_break_ctl,
2080 .wait_until_sent= uart_wait_until_sent,
2081 #ifdef CONFIG_PROC_FS
2082 .read_proc = uart_read_proc,
2083 #endif
2084 .tiocmget = uart_tiocmget,
2085 .tiocmset = uart_tiocmset,
2089 * uart_register_driver - register a driver with the uart core layer
2090 * @drv: low level driver structure
2092 * Register a uart driver with the core driver. We in turn register
2093 * with the tty layer, and initialise the core driver per-port state.
2095 * We have a proc file in /proc/tty/driver which is named after the
2096 * normal driver.
2098 * drv->port should be NULL, and the per-port structures should be
2099 * registered using uart_add_one_port after this call has succeeded.
2101 int uart_register_driver(struct uart_driver *drv)
2103 struct tty_driver *normal = NULL;
2104 int i, retval;
2106 BUG_ON(drv->state);
2109 * Maybe we should be using a slab cache for this, especially if
2110 * we have a large number of ports to handle.
2112 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2113 retval = -ENOMEM;
2114 if (!drv->state)
2115 goto out;
2117 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2119 normal = alloc_tty_driver(drv->nr);
2120 if (!normal)
2121 goto out;
2123 drv->tty_driver = normal;
2125 normal->owner = drv->owner;
2126 normal->driver_name = drv->driver_name;
2127 normal->devfs_name = drv->devfs_name;
2128 normal->name = drv->dev_name;
2129 normal->major = drv->major;
2130 normal->minor_start = drv->minor;
2131 normal->type = TTY_DRIVER_TYPE_SERIAL;
2132 normal->subtype = SERIAL_TYPE_NORMAL;
2133 normal->init_termios = tty_std_termios;
2134 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2135 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2136 normal->driver_state = drv;
2137 tty_set_operations(normal, &uart_ops);
2140 * Initialise the UART state(s).
2142 for (i = 0; i < drv->nr; i++) {
2143 struct uart_state *state = drv->state + i;
2145 state->close_delay = 500; /* .5 seconds */
2146 state->closing_wait = 30000; /* 30 seconds */
2148 init_MUTEX(&state->sem);
2151 retval = tty_register_driver(normal);
2152 out:
2153 if (retval < 0) {
2154 put_tty_driver(normal);
2155 kfree(drv->state);
2157 return retval;
2161 * uart_unregister_driver - remove a driver from the uart core layer
2162 * @drv: low level driver structure
2164 * Remove all references to a driver from the core driver. The low
2165 * level driver must have removed all its ports via the
2166 * uart_remove_one_port() if it registered them with uart_add_one_port().
2167 * (ie, drv->port == NULL)
2169 void uart_unregister_driver(struct uart_driver *drv)
2171 struct tty_driver *p = drv->tty_driver;
2172 tty_unregister_driver(p);
2173 put_tty_driver(p);
2174 kfree(drv->state);
2175 drv->tty_driver = NULL;
2178 struct tty_driver *uart_console_device(struct console *co, int *index)
2180 struct uart_driver *p = co->data;
2181 *index = co->index;
2182 return p->tty_driver;
2186 * uart_add_one_port - attach a driver-defined port structure
2187 * @drv: pointer to the uart low level driver structure for this port
2188 * @port: uart port structure to use for this port.
2190 * This allows the driver to register its own uart_port structure
2191 * with the core driver. The main purpose is to allow the low
2192 * level uart drivers to expand uart_port, rather than having yet
2193 * more levels of structures.
2195 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2197 struct uart_state *state;
2198 int ret = 0;
2200 BUG_ON(in_interrupt());
2202 if (port->line >= drv->nr)
2203 return -EINVAL;
2205 state = drv->state + port->line;
2207 down(&port_sem);
2208 if (state->port) {
2209 ret = -EINVAL;
2210 goto out;
2213 state->port = port;
2215 port->cons = drv->cons;
2216 port->info = state->info;
2219 * If this port is a console, then the spinlock is already
2220 * initialised.
2222 if (!uart_console(port))
2223 spin_lock_init(&port->lock);
2225 uart_configure_port(drv, state, port);
2228 * Register the port whether it's detected or not. This allows
2229 * setserial to be used to alter this ports parameters.
2231 tty_register_device(drv->tty_driver, port->line, port->dev);
2234 * If this driver supports console, and it hasn't been
2235 * successfully registered yet, try to re-register it.
2236 * It may be that the port was not available.
2238 if (port->type != PORT_UNKNOWN &&
2239 port->cons && !(port->cons->flags & CON_ENABLED))
2240 register_console(port->cons);
2242 out:
2243 up(&port_sem);
2245 return ret;
2249 * uart_remove_one_port - detach a driver defined port structure
2250 * @drv: pointer to the uart low level driver structure for this port
2251 * @port: uart port structure for this port
2253 * This unhooks (and hangs up) the specified port structure from the
2254 * core driver. No further calls will be made to the low-level code
2255 * for this port.
2257 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2259 struct uart_state *state = drv->state + port->line;
2261 BUG_ON(in_interrupt());
2263 if (state->port != port)
2264 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2265 state->port, port);
2267 down(&port_sem);
2270 * Remove the devices from devfs
2272 tty_unregister_device(drv->tty_driver, port->line);
2274 uart_unconfigure_port(drv, state);
2275 state->port = NULL;
2276 up(&port_sem);
2278 return 0;
2282 * Are the two ports equivalent?
2284 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2286 if (port1->iotype != port2->iotype)
2287 return 0;
2289 switch (port1->iotype) {
2290 case UPIO_PORT:
2291 return (port1->iobase == port2->iobase);
2292 case UPIO_HUB6:
2293 return (port1->iobase == port2->iobase) &&
2294 (port1->hub6 == port2->hub6);
2295 case UPIO_MEM:
2296 return (port1->membase == port2->membase);
2298 return 0;
2300 EXPORT_SYMBOL(uart_match_port);
2302 EXPORT_SYMBOL(uart_write_wakeup);
2303 EXPORT_SYMBOL(uart_register_driver);
2304 EXPORT_SYMBOL(uart_unregister_driver);
2305 EXPORT_SYMBOL(uart_suspend_port);
2306 EXPORT_SYMBOL(uart_resume_port);
2307 EXPORT_SYMBOL(uart_add_one_port);
2308 EXPORT_SYMBOL(uart_remove_one_port);
2310 MODULE_DESCRIPTION("Serial driver core");
2311 MODULE_LICENSE("GPL");