Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / serial_core.c
blob36b1ae083fb7b1574d4b8951fdf1efcfec2a68df
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, 1);
84 spin_unlock_irqrestore(&port->lock, flags);
87 static void __uart_start(struct tty_struct *tty)
89 struct uart_state *state = tty->driver_data;
90 struct uart_port *port = state->port;
92 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
93 !tty->stopped && !tty->hw_stopped)
94 port->ops->start_tx(port, 1);
97 static void uart_start(struct tty_struct *tty)
99 struct uart_state *state = tty->driver_data;
100 struct uart_port *port = state->port;
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 if (info->tty)
151 set_bit(TTY_IO_ERROR, &info->tty->flags);
153 if (port->type == PORT_UNKNOWN)
154 return 0;
157 * Initialise and allocate the transmit and temporary
158 * buffer.
160 if (!info->xmit.buf) {
161 page = get_zeroed_page(GFP_KERNEL);
162 if (!page)
163 return -ENOMEM;
165 info->xmit.buf = (unsigned char *) page;
166 uart_circ_clear(&info->xmit);
169 retval = port->ops->startup(port);
170 if (retval == 0) {
171 if (init_hw) {
173 * Initialise the hardware port settings.
175 uart_change_speed(state, NULL);
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (info->tty->termios->c_cflag & CBAUD)
182 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
185 info->flags |= UIF_INITIALIZED;
187 clear_bit(TTY_IO_ERROR, &info->tty->flags);
190 if (retval && capable(CAP_SYS_ADMIN))
191 retval = 0;
193 return retval;
197 * This routine will shutdown a serial port; interrupts are disabled, and
198 * DTR is dropped if the hangup on close termio flag is on. Calls to
199 * uart_shutdown are serialised by the per-port semaphore.
201 static void uart_shutdown(struct uart_state *state)
203 struct uart_info *info = state->info;
204 struct uart_port *port = state->port;
206 if (!(info->flags & UIF_INITIALIZED))
207 return;
210 * Turn off DTR and RTS early.
212 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
213 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
216 * clear delta_msr_wait queue to avoid mem leaks: we may free
217 * the irq here so the queue might never be woken up. Note
218 * that we won't end up waiting on delta_msr_wait again since
219 * any outstanding file descriptors should be pointing at
220 * hung_up_tty_fops now.
222 wake_up_interruptible(&info->delta_msr_wait);
225 * Free the IRQ and disable the port.
227 port->ops->shutdown(port);
230 * Ensure that the IRQ handler isn't running on another CPU.
232 synchronize_irq(port->irq);
235 * Free the transmit buffer page.
237 if (info->xmit.buf) {
238 free_page((unsigned long)info->xmit.buf);
239 info->xmit.buf = NULL;
243 * kill off our tasklet
245 tasklet_kill(&info->tlet);
246 if (info->tty)
247 set_bit(TTY_IO_ERROR, &info->tty->flags);
249 info->flags &= ~UIF_INITIALIZED;
253 * uart_update_timeout - update per-port FIFO timeout.
254 * @port: uart_port structure describing the port
255 * @cflag: termios cflag value
256 * @baud: speed of the port
258 * Set the port FIFO timeout value. The @cflag value should
259 * reflect the actual hardware settings.
261 void
262 uart_update_timeout(struct uart_port *port, unsigned int cflag,
263 unsigned int baud)
265 unsigned int bits;
267 /* byte size and parity */
268 switch (cflag & CSIZE) {
269 case CS5:
270 bits = 7;
271 break;
272 case CS6:
273 bits = 8;
274 break;
275 case CS7:
276 bits = 9;
277 break;
278 default:
279 bits = 10;
280 break; // CS8
283 if (cflag & CSTOPB)
284 bits++;
285 if (cflag & PARENB)
286 bits++;
289 * The total number of bits to be transmitted in the fifo.
291 bits = bits * port->fifosize;
294 * Figure the timeout to send the above number of bits.
295 * Add .02 seconds of slop
297 port->timeout = (HZ * bits) / baud + HZ/50;
300 EXPORT_SYMBOL(uart_update_timeout);
303 * uart_get_baud_rate - return baud rate for a particular port
304 * @port: uart_port structure describing the port in question.
305 * @termios: desired termios settings.
306 * @old: old termios (or NULL)
307 * @min: minimum acceptable baud rate
308 * @max: maximum acceptable baud rate
310 * Decode the termios structure into a numeric baud rate,
311 * taking account of the magic 38400 baud rate (with spd_*
312 * flags), and mapping the %B0 rate to 9600 baud.
314 * If the new baud rate is invalid, try the old termios setting.
315 * If it's still invalid, we try 9600 baud.
317 * Update the @termios structure to reflect the baud rate
318 * we're actually going to be using.
320 unsigned int
321 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
322 struct termios *old, unsigned int min, unsigned int max)
324 unsigned int try, baud, altbaud = 38400;
325 unsigned int flags = port->flags & UPF_SPD_MASK;
327 if (flags == UPF_SPD_HI)
328 altbaud = 57600;
329 if (flags == UPF_SPD_VHI)
330 altbaud = 115200;
331 if (flags == UPF_SPD_SHI)
332 altbaud = 230400;
333 if (flags == UPF_SPD_WARP)
334 altbaud = 460800;
336 for (try = 0; try < 2; try++) {
337 baud = tty_termios_baud_rate(termios);
340 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
341 * Die! Die! Die!
343 if (baud == 38400)
344 baud = altbaud;
347 * Special case: B0 rate.
349 if (baud == 0)
350 baud = 9600;
352 if (baud >= min && baud <= max)
353 return baud;
356 * Oops, the quotient was zero. Try again with
357 * the old baud rate if possible.
359 termios->c_cflag &= ~CBAUD;
360 if (old) {
361 termios->c_cflag |= old->c_cflag & CBAUD;
362 old = NULL;
363 continue;
367 * As a last resort, if the quotient is zero,
368 * default to 9600 bps
370 termios->c_cflag |= B9600;
373 return 0;
376 EXPORT_SYMBOL(uart_get_baud_rate);
379 * uart_get_divisor - return uart clock divisor
380 * @port: uart_port structure describing the port.
381 * @baud: desired baud rate
383 * Calculate the uart clock divisor for the port.
385 unsigned int
386 uart_get_divisor(struct uart_port *port, unsigned int baud)
388 unsigned int quot;
391 * Old custom speed handling.
393 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
394 quot = port->custom_divisor;
395 else
396 quot = (port->uartclk + (8 * baud)) / (16 * baud);
398 return quot;
401 EXPORT_SYMBOL(uart_get_divisor);
403 static void
404 uart_change_speed(struct uart_state *state, struct termios *old_termios)
406 struct tty_struct *tty = state->info->tty;
407 struct uart_port *port = state->port;
408 struct termios *termios;
411 * If we have no tty, termios, or the port does not exist,
412 * then we can't set the parameters for this port.
414 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
415 return;
417 termios = tty->termios;
420 * Set flags based on termios cflag
422 if (termios->c_cflag & CRTSCTS)
423 state->info->flags |= UIF_CTS_FLOW;
424 else
425 state->info->flags &= ~UIF_CTS_FLOW;
427 if (termios->c_cflag & CLOCAL)
428 state->info->flags &= ~UIF_CHECK_CD;
429 else
430 state->info->flags |= UIF_CHECK_CD;
432 port->ops->set_termios(port, termios, old_termios);
435 static inline void
436 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
438 unsigned long flags;
440 if (!circ->buf)
441 return;
443 spin_lock_irqsave(&port->lock, flags);
444 if (uart_circ_chars_free(circ) != 0) {
445 circ->buf[circ->head] = c;
446 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
448 spin_unlock_irqrestore(&port->lock, flags);
451 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
453 struct uart_state *state = tty->driver_data;
455 __uart_put_char(state->port, &state->info->xmit, ch);
458 static void uart_flush_chars(struct tty_struct *tty)
460 uart_start(tty);
463 static int
464 uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
466 struct uart_state *state = tty->driver_data;
467 struct uart_port *port = state->port;
468 struct circ_buf *circ = &state->info->xmit;
469 unsigned long flags;
470 int c, ret = 0;
472 if (!circ->buf)
473 return 0;
475 spin_lock_irqsave(&port->lock, flags);
476 while (1) {
477 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
478 if (count < c)
479 c = count;
480 if (c <= 0)
481 break;
482 memcpy(circ->buf + circ->head, buf, c);
483 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
484 buf += c;
485 count -= c;
486 ret += c;
488 spin_unlock_irqrestore(&port->lock, flags);
490 uart_start(tty);
491 return ret;
494 static int uart_write_room(struct tty_struct *tty)
496 struct uart_state *state = tty->driver_data;
498 return uart_circ_chars_free(&state->info->xmit);
501 static int uart_chars_in_buffer(struct tty_struct *tty)
503 struct uart_state *state = tty->driver_data;
505 return uart_circ_chars_pending(&state->info->xmit);
508 static void uart_flush_buffer(struct tty_struct *tty)
510 struct uart_state *state = tty->driver_data;
511 struct uart_port *port = state->port;
512 unsigned long flags;
514 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
516 spin_lock_irqsave(&port->lock, flags);
517 uart_circ_clear(&state->info->xmit);
518 spin_unlock_irqrestore(&port->lock, flags);
519 tty_wakeup(tty);
523 * This function is used to send a high-priority XON/XOFF character to
524 * the device
526 static void uart_send_xchar(struct tty_struct *tty, char ch)
528 struct uart_state *state = tty->driver_data;
529 struct uart_port *port = state->port;
530 unsigned long flags;
532 if (port->ops->send_xchar)
533 port->ops->send_xchar(port, ch);
534 else {
535 port->x_char = ch;
536 if (ch) {
537 spin_lock_irqsave(&port->lock, flags);
538 port->ops->start_tx(port, 0);
539 spin_unlock_irqrestore(&port->lock, flags);
544 static void uart_throttle(struct tty_struct *tty)
546 struct uart_state *state = tty->driver_data;
548 if (I_IXOFF(tty))
549 uart_send_xchar(tty, STOP_CHAR(tty));
551 if (tty->termios->c_cflag & CRTSCTS)
552 uart_clear_mctrl(state->port, TIOCM_RTS);
555 static void uart_unthrottle(struct tty_struct *tty)
557 struct uart_state *state = tty->driver_data;
558 struct uart_port *port = state->port;
560 if (I_IXOFF(tty)) {
561 if (port->x_char)
562 port->x_char = 0;
563 else
564 uart_send_xchar(tty, START_CHAR(tty));
567 if (tty->termios->c_cflag & CRTSCTS)
568 uart_set_mctrl(port, TIOCM_RTS);
571 static int uart_get_info(struct uart_state *state,
572 struct serial_struct __user *retinfo)
574 struct uart_port *port = state->port;
575 struct serial_struct tmp;
577 memset(&tmp, 0, sizeof(tmp));
578 tmp.type = port->type;
579 tmp.line = port->line;
580 tmp.port = port->iobase;
581 if (HIGH_BITS_OFFSET)
582 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
583 tmp.irq = port->irq;
584 tmp.flags = port->flags;
585 tmp.xmit_fifo_size = port->fifosize;
586 tmp.baud_base = port->uartclk / 16;
587 tmp.close_delay = state->close_delay / 10;
588 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
589 ASYNC_CLOSING_WAIT_NONE :
590 state->closing_wait / 10;
591 tmp.custom_divisor = port->custom_divisor;
592 tmp.hub6 = port->hub6;
593 tmp.io_type = port->iotype;
594 tmp.iomem_reg_shift = port->regshift;
595 tmp.iomem_base = (void *)port->mapbase;
597 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
598 return -EFAULT;
599 return 0;
602 static int uart_set_info(struct uart_state *state,
603 struct serial_struct __user *newinfo)
605 struct serial_struct new_serial;
606 struct uart_port *port = state->port;
607 unsigned long new_port;
608 unsigned int change_irq, change_port, old_flags, closing_wait;
609 unsigned int old_custom_divisor, close_delay;
610 int retval = 0;
612 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
613 return -EFAULT;
615 new_port = new_serial.port;
616 if (HIGH_BITS_OFFSET)
617 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
619 new_serial.irq = irq_canonicalize(new_serial.irq);
620 close_delay = new_serial.close_delay * 10;
621 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
622 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
625 * This semaphore protects state->count. It is also
626 * very useful to prevent opens. Also, take the
627 * port configuration semaphore to make sure that a
628 * module insertion/removal doesn't change anything
629 * under us.
631 down(&state->sem);
633 change_irq = new_serial.irq != port->irq;
636 * Since changing the 'type' of the port changes its resource
637 * allocations, we should treat type changes the same as
638 * IO port changes.
640 change_port = new_port != port->iobase ||
641 (unsigned long)new_serial.iomem_base != port->mapbase ||
642 new_serial.hub6 != port->hub6 ||
643 new_serial.io_type != port->iotype ||
644 new_serial.iomem_reg_shift != port->regshift ||
645 new_serial.type != port->type;
647 old_flags = port->flags;
648 old_custom_divisor = port->custom_divisor;
650 if (!capable(CAP_SYS_ADMIN)) {
651 retval = -EPERM;
652 if (change_irq || change_port ||
653 (new_serial.baud_base != port->uartclk / 16) ||
654 (close_delay != state->close_delay) ||
655 (closing_wait != state->closing_wait) ||
656 (new_serial.xmit_fifo_size != port->fifosize) ||
657 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
658 goto exit;
659 port->flags = ((port->flags & ~UPF_USR_MASK) |
660 (new_serial.flags & UPF_USR_MASK));
661 port->custom_divisor = new_serial.custom_divisor;
662 goto check_and_exit;
666 * Ask the low level driver to verify the settings.
668 if (port->ops->verify_port)
669 retval = port->ops->verify_port(port, &new_serial);
671 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
672 (new_serial.baud_base < 9600))
673 retval = -EINVAL;
675 if (retval)
676 goto exit;
678 if (change_port || change_irq) {
679 retval = -EBUSY;
682 * Make sure that we are the sole user of this port.
684 if (uart_users(state) > 1)
685 goto exit;
688 * We need to shutdown the serial port at the old
689 * port/type/irq combination.
691 uart_shutdown(state);
694 if (change_port) {
695 unsigned long old_iobase, old_mapbase;
696 unsigned int old_type, old_iotype, old_hub6, old_shift;
698 old_iobase = port->iobase;
699 old_mapbase = port->mapbase;
700 old_type = port->type;
701 old_hub6 = port->hub6;
702 old_iotype = port->iotype;
703 old_shift = port->regshift;
706 * Free and release old regions
708 if (old_type != PORT_UNKNOWN)
709 port->ops->release_port(port);
711 port->iobase = new_port;
712 port->type = new_serial.type;
713 port->hub6 = new_serial.hub6;
714 port->iotype = new_serial.io_type;
715 port->regshift = new_serial.iomem_reg_shift;
716 port->mapbase = (unsigned long)new_serial.iomem_base;
719 * Claim and map the new regions
721 if (port->type != PORT_UNKNOWN) {
722 retval = port->ops->request_port(port);
723 } else {
724 /* Always success - Jean II */
725 retval = 0;
729 * If we fail to request resources for the
730 * new port, try to restore the old settings.
732 if (retval && old_type != PORT_UNKNOWN) {
733 port->iobase = old_iobase;
734 port->type = old_type;
735 port->hub6 = old_hub6;
736 port->iotype = old_iotype;
737 port->regshift = old_shift;
738 port->mapbase = old_mapbase;
739 retval = port->ops->request_port(port);
741 * If we failed to restore the old settings,
742 * we fail like this.
744 if (retval)
745 port->type = PORT_UNKNOWN;
748 * We failed anyway.
750 retval = -EBUSY;
754 port->irq = new_serial.irq;
755 port->uartclk = new_serial.baud_base * 16;
756 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
757 (new_serial.flags & UPF_CHANGE_MASK);
758 port->custom_divisor = new_serial.custom_divisor;
759 state->close_delay = close_delay;
760 state->closing_wait = closing_wait;
761 port->fifosize = new_serial.xmit_fifo_size;
762 if (state->info->tty)
763 state->info->tty->low_latency =
764 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
766 check_and_exit:
767 retval = 0;
768 if (port->type == PORT_UNKNOWN)
769 goto exit;
770 if (state->info->flags & UIF_INITIALIZED) {
771 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
772 old_custom_divisor != port->custom_divisor) {
774 * If they're setting up a custom divisor or speed,
775 * instead of clearing it, then bitch about it. No
776 * need to rate-limit; it's CAP_SYS_ADMIN only.
778 if (port->flags & UPF_SPD_MASK) {
779 char buf[64];
780 printk(KERN_NOTICE
781 "%s sets custom speed on %s. This "
782 "is deprecated.\n", current->comm,
783 tty_name(state->info->tty, buf));
785 uart_change_speed(state, NULL);
787 } else
788 retval = uart_startup(state, 1);
789 exit:
790 up(&state->sem);
791 return retval;
796 * uart_get_lsr_info - get line status register info.
797 * Note: uart_ioctl protects us against hangups.
799 static int uart_get_lsr_info(struct uart_state *state,
800 unsigned int __user *value)
802 struct uart_port *port = state->port;
803 unsigned int result;
805 result = port->ops->tx_empty(port);
808 * If we're about to load something into the transmit
809 * register, we'll pretend the transmitter isn't empty to
810 * avoid a race condition (depending on when the transmit
811 * interrupt happens).
813 if (port->x_char ||
814 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
815 !state->info->tty->stopped && !state->info->tty->hw_stopped))
816 result &= ~TIOCSER_TEMT;
818 return put_user(result, value);
821 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
823 struct uart_state *state = tty->driver_data;
824 struct uart_port *port = state->port;
825 int result = -EIO;
827 down(&state->sem);
828 if ((!file || !tty_hung_up_p(file)) &&
829 !(tty->flags & (1 << TTY_IO_ERROR))) {
830 result = port->mctrl;
831 result |= port->ops->get_mctrl(port);
833 up(&state->sem);
835 return result;
838 static int
839 uart_tiocmset(struct tty_struct *tty, struct file *file,
840 unsigned int set, unsigned int clear)
842 struct uart_state *state = tty->driver_data;
843 struct uart_port *port = state->port;
844 int ret = -EIO;
846 down(&state->sem);
847 if ((!file || !tty_hung_up_p(file)) &&
848 !(tty->flags & (1 << TTY_IO_ERROR))) {
849 uart_update_mctrl(port, set, clear);
850 ret = 0;
852 up(&state->sem);
853 return ret;
856 static void uart_break_ctl(struct tty_struct *tty, int break_state)
858 struct uart_state *state = tty->driver_data;
859 struct uart_port *port = state->port;
861 BUG_ON(!kernel_locked());
863 down(&state->sem);
865 if (port->type != PORT_UNKNOWN)
866 port->ops->break_ctl(port, break_state);
868 up(&state->sem);
871 static int uart_do_autoconfig(struct uart_state *state)
873 struct uart_port *port = state->port;
874 int flags, ret;
876 if (!capable(CAP_SYS_ADMIN))
877 return -EPERM;
880 * Take the per-port semaphore. This prevents count from
881 * changing, and hence any extra opens of the port while
882 * we're auto-configuring.
884 if (down_interruptible(&state->sem))
885 return -ERESTARTSYS;
887 ret = -EBUSY;
888 if (uart_users(state) == 1) {
889 uart_shutdown(state);
892 * If we already have a port type configured,
893 * we must release its resources.
895 if (port->type != PORT_UNKNOWN)
896 port->ops->release_port(port);
898 flags = UART_CONFIG_TYPE;
899 if (port->flags & UPF_AUTO_IRQ)
900 flags |= UART_CONFIG_IRQ;
903 * This will claim the ports resources if
904 * a port is found.
906 port->ops->config_port(port, flags);
908 ret = uart_startup(state, 1);
910 up(&state->sem);
911 return ret;
915 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
916 * - mask passed in arg for lines of interest
917 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
918 * Caller should use TIOCGICOUNT to see which one it was
920 static int
921 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
923 struct uart_port *port = state->port;
924 DECLARE_WAITQUEUE(wait, current);
925 struct uart_icount cprev, cnow;
926 int ret;
929 * note the counters on entry
931 spin_lock_irq(&port->lock);
932 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
935 * Force modem status interrupts on
937 port->ops->enable_ms(port);
938 spin_unlock_irq(&port->lock);
940 add_wait_queue(&state->info->delta_msr_wait, &wait);
941 for (;;) {
942 spin_lock_irq(&port->lock);
943 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
944 spin_unlock_irq(&port->lock);
946 set_current_state(TASK_INTERRUPTIBLE);
948 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
949 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
950 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
951 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
952 ret = 0;
953 break;
956 schedule();
958 /* see if a signal did it */
959 if (signal_pending(current)) {
960 ret = -ERESTARTSYS;
961 break;
964 cprev = cnow;
967 current->state = TASK_RUNNING;
968 remove_wait_queue(&state->info->delta_msr_wait, &wait);
970 return ret;
974 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
975 * Return: write counters to the user passed counter struct
976 * NB: both 1->0 and 0->1 transitions are counted except for
977 * RI where only 0->1 is counted.
979 static int uart_get_count(struct uart_state *state,
980 struct serial_icounter_struct __user *icnt)
982 struct serial_icounter_struct icount;
983 struct uart_icount cnow;
984 struct uart_port *port = state->port;
986 spin_lock_irq(&port->lock);
987 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
988 spin_unlock_irq(&port->lock);
990 icount.cts = cnow.cts;
991 icount.dsr = cnow.dsr;
992 icount.rng = cnow.rng;
993 icount.dcd = cnow.dcd;
994 icount.rx = cnow.rx;
995 icount.tx = cnow.tx;
996 icount.frame = cnow.frame;
997 icount.overrun = cnow.overrun;
998 icount.parity = cnow.parity;
999 icount.brk = cnow.brk;
1000 icount.buf_overrun = cnow.buf_overrun;
1002 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1006 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1008 static int
1009 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1010 unsigned long arg)
1012 struct uart_state *state = tty->driver_data;
1013 void __user *uarg = (void __user *)arg;
1014 int ret = -ENOIOCTLCMD;
1016 BUG_ON(!kernel_locked());
1019 * These ioctls don't rely on the hardware to be present.
1021 switch (cmd) {
1022 case TIOCGSERIAL:
1023 ret = uart_get_info(state, uarg);
1024 break;
1026 case TIOCSSERIAL:
1027 ret = uart_set_info(state, uarg);
1028 break;
1030 case TIOCSERCONFIG:
1031 ret = uart_do_autoconfig(state);
1032 break;
1034 case TIOCSERGWILD: /* obsolete */
1035 case TIOCSERSWILD: /* obsolete */
1036 ret = 0;
1037 break;
1040 if (ret != -ENOIOCTLCMD)
1041 goto out;
1043 if (tty->flags & (1 << TTY_IO_ERROR)) {
1044 ret = -EIO;
1045 goto out;
1049 * The following should only be used when hardware is present.
1051 switch (cmd) {
1052 case TIOCMIWAIT:
1053 ret = uart_wait_modem_status(state, arg);
1054 break;
1056 case TIOCGICOUNT:
1057 ret = uart_get_count(state, uarg);
1058 break;
1061 if (ret != -ENOIOCTLCMD)
1062 goto out;
1064 down(&state->sem);
1066 if (tty_hung_up_p(filp)) {
1067 ret = -EIO;
1068 goto out_up;
1072 * All these rely on hardware being present and need to be
1073 * protected against the tty being hung up.
1075 switch (cmd) {
1076 case TIOCSERGETLSR: /* Get line status register */
1077 ret = uart_get_lsr_info(state, uarg);
1078 break;
1080 default: {
1081 struct uart_port *port = state->port;
1082 if (port->ops->ioctl)
1083 ret = port->ops->ioctl(port, cmd, arg);
1084 break;
1087 out_up:
1088 up(&state->sem);
1089 out:
1090 return ret;
1093 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1095 struct uart_state *state = tty->driver_data;
1096 unsigned long flags;
1097 unsigned int cflag = tty->termios->c_cflag;
1099 BUG_ON(!kernel_locked());
1102 * These are the bits that are used to setup various
1103 * flags in the low level driver.
1105 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1107 if ((cflag ^ old_termios->c_cflag) == 0 &&
1108 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1109 return;
1111 uart_change_speed(state, old_termios);
1113 /* Handle transition to B0 status */
1114 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1115 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1117 /* Handle transition away from B0 status */
1118 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1119 unsigned int mask = TIOCM_DTR;
1120 if (!(cflag & CRTSCTS) ||
1121 !test_bit(TTY_THROTTLED, &tty->flags))
1122 mask |= TIOCM_RTS;
1123 uart_set_mctrl(state->port, mask);
1126 /* Handle turning off CRTSCTS */
1127 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1128 spin_lock_irqsave(&state->port->lock, flags);
1129 tty->hw_stopped = 0;
1130 __uart_start(tty);
1131 spin_unlock_irqrestore(&state->port->lock, flags);
1134 #if 0
1136 * No need to wake up processes in open wait, since they
1137 * sample the CLOCAL flag once, and don't recheck it.
1138 * XXX It's not clear whether the current behavior is correct
1139 * or not. Hence, this may change.....
1141 if (!(old_termios->c_cflag & CLOCAL) &&
1142 (tty->termios->c_cflag & CLOCAL))
1143 wake_up_interruptible(&state->info->open_wait);
1144 #endif
1148 * In 2.4.5, calls to this will be serialized via the BKL in
1149 * linux/drivers/char/tty_io.c:tty_release()
1150 * linux/drivers/char/tty_io.c:do_tty_handup()
1152 static void uart_close(struct tty_struct *tty, struct file *filp)
1154 struct uart_state *state = tty->driver_data;
1155 struct uart_port *port;
1157 BUG_ON(!kernel_locked());
1159 if (!state || !state->port)
1160 return;
1162 port = state->port;
1164 DPRINTK("uart_close(%d) called\n", port->line);
1166 down(&state->sem);
1168 if (tty_hung_up_p(filp))
1169 goto done;
1171 if ((tty->count == 1) && (state->count != 1)) {
1173 * Uh, oh. tty->count is 1, which means that the tty
1174 * structure will be freed. state->count should always
1175 * be one in these conditions. If it's greater than
1176 * one, we've got real problems, since it means the
1177 * serial port won't be shutdown.
1179 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1180 "state->count is %d\n", state->count);
1181 state->count = 1;
1183 if (--state->count < 0) {
1184 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1185 tty->name, state->count);
1186 state->count = 0;
1188 if (state->count)
1189 goto done;
1192 * Now we wait for the transmit buffer to clear; and we notify
1193 * the line discipline to only process XON/XOFF characters by
1194 * setting tty->closing.
1196 tty->closing = 1;
1198 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1199 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1202 * At this point, we stop accepting input. To do this, we
1203 * disable the receive line status interrupts.
1205 if (state->info->flags & UIF_INITIALIZED) {
1206 unsigned long flags;
1207 spin_lock_irqsave(&port->lock, flags);
1208 port->ops->stop_rx(port);
1209 spin_unlock_irqrestore(&port->lock, flags);
1211 * Before we drop DTR, make sure the UART transmitter
1212 * has completely drained; this is especially
1213 * important if there is a transmit FIFO!
1215 uart_wait_until_sent(tty, port->timeout);
1218 uart_shutdown(state);
1219 uart_flush_buffer(tty);
1221 tty_ldisc_flush(tty);
1223 tty->closing = 0;
1224 state->info->tty = NULL;
1226 if (state->info->blocked_open) {
1227 if (state->close_delay)
1228 msleep_interruptible(state->close_delay);
1229 } else if (!uart_console(port)) {
1230 uart_change_pm(state, 3);
1234 * Wake up anyone trying to open this port.
1236 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1237 wake_up_interruptible(&state->info->open_wait);
1239 done:
1240 up(&state->sem);
1243 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1245 struct uart_state *state = tty->driver_data;
1246 struct uart_port *port = state->port;
1247 unsigned long char_time, expire;
1249 BUG_ON(!kernel_locked());
1251 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1252 return;
1255 * Set the check interval to be 1/5 of the estimated time to
1256 * send a single character, and make it at least 1. The check
1257 * interval should also be less than the timeout.
1259 * Note: we have to use pretty tight timings here to satisfy
1260 * the NIST-PCTS.
1262 char_time = (port->timeout - HZ/50) / port->fifosize;
1263 char_time = char_time / 5;
1264 if (char_time == 0)
1265 char_time = 1;
1266 if (timeout && timeout < char_time)
1267 char_time = timeout;
1270 * If the transmitter hasn't cleared in twice the approximate
1271 * amount of time to send the entire FIFO, it probably won't
1272 * ever clear. This assumes the UART isn't doing flow
1273 * control, which is currently the case. Hence, if it ever
1274 * takes longer than port->timeout, this is probably due to a
1275 * UART bug of some kind. So, we clamp the timeout parameter at
1276 * 2*port->timeout.
1278 if (timeout == 0 || timeout > 2 * port->timeout)
1279 timeout = 2 * port->timeout;
1281 expire = jiffies + timeout;
1283 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1284 port->line, jiffies, expire);
1287 * Check whether the transmitter is empty every 'char_time'.
1288 * 'timeout' / 'expire' give us the maximum amount of time
1289 * we wait.
1291 while (!port->ops->tx_empty(port)) {
1292 msleep_interruptible(jiffies_to_msecs(char_time));
1293 if (signal_pending(current))
1294 break;
1295 if (time_after(jiffies, expire))
1296 break;
1298 set_current_state(TASK_RUNNING); /* might not be needed */
1302 * This is called with the BKL held in
1303 * linux/drivers/char/tty_io.c:do_tty_hangup()
1304 * We're called from the eventd thread, so we can sleep for
1305 * a _short_ time only.
1307 static void uart_hangup(struct tty_struct *tty)
1309 struct uart_state *state = tty->driver_data;
1311 BUG_ON(!kernel_locked());
1312 DPRINTK("uart_hangup(%d)\n", state->port->line);
1314 down(&state->sem);
1315 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1316 uart_flush_buffer(tty);
1317 uart_shutdown(state);
1318 state->count = 0;
1319 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1320 state->info->tty = NULL;
1321 wake_up_interruptible(&state->info->open_wait);
1322 wake_up_interruptible(&state->info->delta_msr_wait);
1324 up(&state->sem);
1328 * Copy across the serial console cflag setting into the termios settings
1329 * for the initial open of the port. This allows continuity between the
1330 * kernel settings, and the settings init adopts when it opens the port
1331 * for the first time.
1333 static void uart_update_termios(struct uart_state *state)
1335 struct tty_struct *tty = state->info->tty;
1336 struct uart_port *port = state->port;
1338 if (uart_console(port) && port->cons->cflag) {
1339 tty->termios->c_cflag = port->cons->cflag;
1340 port->cons->cflag = 0;
1344 * If the device failed to grab its irq resources,
1345 * or some other error occurred, don't try to talk
1346 * to the port hardware.
1348 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1350 * Make termios settings take effect.
1352 uart_change_speed(state, NULL);
1355 * And finally enable the RTS and DTR signals.
1357 if (tty->termios->c_cflag & CBAUD)
1358 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1363 * Block the open until the port is ready. We must be called with
1364 * the per-port semaphore held.
1366 static int
1367 uart_block_til_ready(struct file *filp, struct uart_state *state)
1369 DECLARE_WAITQUEUE(wait, current);
1370 struct uart_info *info = state->info;
1371 struct uart_port *port = state->port;
1373 info->blocked_open++;
1374 state->count--;
1376 add_wait_queue(&info->open_wait, &wait);
1377 while (1) {
1378 set_current_state(TASK_INTERRUPTIBLE);
1381 * If we have been hung up, tell userspace/restart open.
1383 if (tty_hung_up_p(filp) || info->tty == NULL)
1384 break;
1387 * If the port has been closed, tell userspace/restart open.
1389 if (!(info->flags & UIF_INITIALIZED))
1390 break;
1393 * If non-blocking mode is set, or CLOCAL mode is set,
1394 * we don't want to wait for the modem status lines to
1395 * indicate that the port is ready.
1397 * Also, if the port is not enabled/configured, we want
1398 * to allow the open to succeed here. Note that we will
1399 * have set TTY_IO_ERROR for a non-existant port.
1401 if ((filp->f_flags & O_NONBLOCK) ||
1402 (info->tty->termios->c_cflag & CLOCAL) ||
1403 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1404 break;
1408 * Set DTR to allow modem to know we're waiting. Do
1409 * not set RTS here - we want to make sure we catch
1410 * the data from the modem.
1412 if (info->tty->termios->c_cflag & CBAUD)
1413 uart_set_mctrl(port, TIOCM_DTR);
1416 * and wait for the carrier to indicate that the
1417 * modem is ready for us.
1419 if (port->ops->get_mctrl(port) & TIOCM_CAR)
1420 break;
1422 up(&state->sem);
1423 schedule();
1424 down(&state->sem);
1426 if (signal_pending(current))
1427 break;
1429 set_current_state(TASK_RUNNING);
1430 remove_wait_queue(&info->open_wait, &wait);
1432 state->count++;
1433 info->blocked_open--;
1435 if (signal_pending(current))
1436 return -ERESTARTSYS;
1438 if (!info->tty || tty_hung_up_p(filp))
1439 return -EAGAIN;
1441 return 0;
1444 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1446 struct uart_state *state;
1448 down(&port_sem);
1449 state = drv->state + line;
1450 if (down_interruptible(&state->sem)) {
1451 state = ERR_PTR(-ERESTARTSYS);
1452 goto out;
1455 state->count++;
1456 if (!state->port) {
1457 state->count--;
1458 up(&state->sem);
1459 state = ERR_PTR(-ENXIO);
1460 goto out;
1463 if (!state->info) {
1464 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1465 if (state->info) {
1466 memset(state->info, 0, sizeof(struct uart_info));
1467 init_waitqueue_head(&state->info->open_wait);
1468 init_waitqueue_head(&state->info->delta_msr_wait);
1471 * Link the info into the other structures.
1473 state->port->info = state->info;
1475 tasklet_init(&state->info->tlet, uart_tasklet_action,
1476 (unsigned long)state);
1477 } else {
1478 state->count--;
1479 up(&state->sem);
1480 state = ERR_PTR(-ENOMEM);
1484 out:
1485 up(&port_sem);
1486 return state;
1490 * In 2.4.5, calls to uart_open are serialised by the BKL in
1491 * linux/fs/devices.c:chrdev_open()
1492 * Note that if this fails, then uart_close() _will_ be called.
1494 * In time, we want to scrap the "opening nonpresent ports"
1495 * behaviour and implement an alternative way for setserial
1496 * to set base addresses/ports/types. This will allow us to
1497 * get rid of a certain amount of extra tests.
1499 static int uart_open(struct tty_struct *tty, struct file *filp)
1501 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1502 struct uart_state *state;
1503 int retval, line = tty->index;
1505 BUG_ON(!kernel_locked());
1506 DPRINTK("uart_open(%d) called\n", line);
1509 * tty->driver->num won't change, so we won't fail here with
1510 * tty->driver_data set to something non-NULL (and therefore
1511 * we won't get caught by uart_close()).
1513 retval = -ENODEV;
1514 if (line >= tty->driver->num)
1515 goto fail;
1518 * We take the semaphore inside uart_get to guarantee that we won't
1519 * be re-entered while allocating the info structure, or while we
1520 * request any IRQs that the driver may need. This also has the nice
1521 * side-effect that it delays the action of uart_hangup, so we can
1522 * guarantee that info->tty will always contain something reasonable.
1524 state = uart_get(drv, line);
1525 if (IS_ERR(state)) {
1526 retval = PTR_ERR(state);
1527 goto fail;
1531 * Once we set tty->driver_data here, we are guaranteed that
1532 * uart_close() will decrement the driver module use count.
1533 * Any failures from here onwards should not touch the count.
1535 tty->driver_data = state;
1536 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1537 tty->alt_speed = 0;
1538 state->info->tty = tty;
1541 * If the port is in the middle of closing, bail out now.
1543 if (tty_hung_up_p(filp)) {
1544 retval = -EAGAIN;
1545 state->count--;
1546 up(&state->sem);
1547 goto fail;
1551 * Make sure the device is in D0 state.
1553 if (state->count == 1)
1554 uart_change_pm(state, 0);
1557 * Start up the serial port.
1559 retval = uart_startup(state, 0);
1562 * If we succeeded, wait until the port is ready.
1564 if (retval == 0)
1565 retval = uart_block_til_ready(filp, state);
1566 up(&state->sem);
1569 * If this is the first open to succeed, adjust things to suit.
1571 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1572 state->info->flags |= UIF_NORMAL_ACTIVE;
1574 uart_update_termios(state);
1577 fail:
1578 return retval;
1581 static const char *uart_type(struct uart_port *port)
1583 const char *str = NULL;
1585 if (port->ops->type)
1586 str = port->ops->type(port);
1588 if (!str)
1589 str = "unknown";
1591 return str;
1594 #ifdef CONFIG_PROC_FS
1596 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1598 struct uart_state *state = drv->state + i;
1599 struct uart_port *port = state->port;
1600 char stat_buf[32];
1601 unsigned int status;
1602 int ret;
1604 if (!port)
1605 return 0;
1607 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1608 port->line, uart_type(port),
1609 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1610 port->iotype == UPIO_MEM ? port->mapbase :
1611 (unsigned long) port->iobase,
1612 port->irq);
1614 if (port->type == PORT_UNKNOWN) {
1615 strcat(buf, "\n");
1616 return ret + 1;
1619 if(capable(CAP_SYS_ADMIN))
1621 status = port->ops->get_mctrl(port);
1623 ret += sprintf(buf + ret, " tx:%d rx:%d",
1624 port->icount.tx, port->icount.rx);
1625 if (port->icount.frame)
1626 ret += sprintf(buf + ret, " fe:%d",
1627 port->icount.frame);
1628 if (port->icount.parity)
1629 ret += sprintf(buf + ret, " pe:%d",
1630 port->icount.parity);
1631 if (port->icount.brk)
1632 ret += sprintf(buf + ret, " brk:%d",
1633 port->icount.brk);
1634 if (port->icount.overrun)
1635 ret += sprintf(buf + ret, " oe:%d",
1636 port->icount.overrun);
1638 #define INFOBIT(bit,str) \
1639 if (port->mctrl & (bit)) \
1640 strncat(stat_buf, (str), sizeof(stat_buf) - \
1641 strlen(stat_buf) - 2)
1642 #define STATBIT(bit,str) \
1643 if (status & (bit)) \
1644 strncat(stat_buf, (str), sizeof(stat_buf) - \
1645 strlen(stat_buf) - 2)
1647 stat_buf[0] = '\0';
1648 stat_buf[1] = '\0';
1649 INFOBIT(TIOCM_RTS, "|RTS");
1650 STATBIT(TIOCM_CTS, "|CTS");
1651 INFOBIT(TIOCM_DTR, "|DTR");
1652 STATBIT(TIOCM_DSR, "|DSR");
1653 STATBIT(TIOCM_CAR, "|CD");
1654 STATBIT(TIOCM_RNG, "|RI");
1655 if (stat_buf[0])
1656 stat_buf[0] = ' ';
1657 strcat(stat_buf, "\n");
1659 ret += sprintf(buf + ret, stat_buf);
1660 } else {
1661 strcat(buf, "\n");
1662 ret++;
1664 #undef STATBIT
1665 #undef INFOBIT
1666 return ret;
1669 static int uart_read_proc(char *page, char **start, off_t off,
1670 int count, int *eof, void *data)
1672 struct tty_driver *ttydrv = data;
1673 struct uart_driver *drv = ttydrv->driver_state;
1674 int i, len = 0, l;
1675 off_t begin = 0;
1677 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1678 "", "", "");
1679 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1680 l = uart_line_info(page + len, drv, i);
1681 len += l;
1682 if (len + begin > off + count)
1683 goto done;
1684 if (len + begin < off) {
1685 begin += len;
1686 len = 0;
1689 *eof = 1;
1690 done:
1691 if (off >= len + begin)
1692 return 0;
1693 *start = page + (off - begin);
1694 return (count < begin + len - off) ? count : (begin + len - off);
1696 #endif
1698 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1700 * Check whether an invalid uart number has been specified, and
1701 * if so, search for the first available port that does have
1702 * console support.
1704 struct uart_port * __init
1705 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1707 int idx = co->index;
1709 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1710 ports[idx].membase == NULL))
1711 for (idx = 0; idx < nr; idx++)
1712 if (ports[idx].iobase != 0 ||
1713 ports[idx].membase != NULL)
1714 break;
1716 co->index = idx;
1718 return ports + idx;
1722 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1723 * @options: pointer to option string
1724 * @baud: pointer to an 'int' variable for the baud rate.
1725 * @parity: pointer to an 'int' variable for the parity.
1726 * @bits: pointer to an 'int' variable for the number of data bits.
1727 * @flow: pointer to an 'int' variable for the flow control character.
1729 * uart_parse_options decodes a string containing the serial console
1730 * options. The format of the string is <baud><parity><bits><flow>,
1731 * eg: 115200n8r
1733 void __init
1734 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1736 char *s = options;
1738 *baud = simple_strtoul(s, NULL, 10);
1739 while (*s >= '0' && *s <= '9')
1740 s++;
1741 if (*s)
1742 *parity = *s++;
1743 if (*s)
1744 *bits = *s++ - '0';
1745 if (*s)
1746 *flow = *s;
1749 struct baud_rates {
1750 unsigned int rate;
1751 unsigned int cflag;
1754 static struct baud_rates baud_rates[] = {
1755 { 921600, B921600 },
1756 { 460800, B460800 },
1757 { 230400, B230400 },
1758 { 115200, B115200 },
1759 { 57600, B57600 },
1760 { 38400, B38400 },
1761 { 19200, B19200 },
1762 { 9600, B9600 },
1763 { 4800, B4800 },
1764 { 2400, B2400 },
1765 { 1200, B1200 },
1766 { 0, B38400 }
1770 * uart_set_options - setup the serial console parameters
1771 * @port: pointer to the serial ports uart_port structure
1772 * @co: console pointer
1773 * @baud: baud rate
1774 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1775 * @bits: number of data bits
1776 * @flow: flow control character - 'r' (rts)
1778 int __init
1779 uart_set_options(struct uart_port *port, struct console *co,
1780 int baud, int parity, int bits, int flow)
1782 struct termios termios;
1783 int i;
1785 memset(&termios, 0, sizeof(struct termios));
1787 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1790 * Construct a cflag setting.
1792 for (i = 0; baud_rates[i].rate; i++)
1793 if (baud_rates[i].rate <= baud)
1794 break;
1796 termios.c_cflag |= baud_rates[i].cflag;
1798 if (bits == 7)
1799 termios.c_cflag |= CS7;
1800 else
1801 termios.c_cflag |= CS8;
1803 switch (parity) {
1804 case 'o': case 'O':
1805 termios.c_cflag |= PARODD;
1806 /*fall through*/
1807 case 'e': case 'E':
1808 termios.c_cflag |= PARENB;
1809 break;
1812 if (flow == 'r')
1813 termios.c_cflag |= CRTSCTS;
1815 port->ops->set_termios(port, &termios, NULL);
1816 co->cflag = termios.c_cflag;
1818 return 0;
1820 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1822 static void uart_change_pm(struct uart_state *state, int pm_state)
1824 struct uart_port *port = state->port;
1825 if (port->ops->pm)
1826 port->ops->pm(port, pm_state, state->pm_state);
1827 state->pm_state = pm_state;
1830 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1832 struct uart_state *state = drv->state + port->line;
1834 down(&state->sem);
1836 if (state->info && state->info->flags & UIF_INITIALIZED) {
1837 struct uart_ops *ops = port->ops;
1839 spin_lock_irq(&port->lock);
1840 ops->stop_tx(port, 0);
1841 ops->set_mctrl(port, 0);
1842 ops->stop_rx(port);
1843 spin_unlock_irq(&port->lock);
1846 * Wait for the transmitter to empty.
1848 while (!ops->tx_empty(port)) {
1849 msleep(10);
1852 ops->shutdown(port);
1856 * Disable the console device before suspending.
1858 if (uart_console(port))
1859 console_stop(port->cons);
1861 uart_change_pm(state, 3);
1863 up(&state->sem);
1865 return 0;
1868 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1870 struct uart_state *state = drv->state + port->line;
1872 down(&state->sem);
1874 uart_change_pm(state, 0);
1877 * Re-enable the console device after suspending.
1879 if (uart_console(port)) {
1880 struct termios termios;
1883 * First try to use the console cflag setting.
1885 memset(&termios, 0, sizeof(struct termios));
1886 termios.c_cflag = port->cons->cflag;
1889 * If that's unset, use the tty termios setting.
1891 if (state->info && state->info->tty && termios.c_cflag == 0)
1892 termios = *state->info->tty->termios;
1894 port->ops->set_termios(port, &termios, NULL);
1895 console_start(port->cons);
1898 if (state->info && state->info->flags & UIF_INITIALIZED) {
1899 struct uart_ops *ops = port->ops;
1901 ops->set_mctrl(port, 0);
1902 ops->startup(port);
1903 uart_change_speed(state, NULL);
1904 spin_lock_irq(&port->lock);
1905 ops->set_mctrl(port, port->mctrl);
1906 ops->start_tx(port, 0);
1907 spin_unlock_irq(&port->lock);
1910 up(&state->sem);
1912 return 0;
1915 static inline void
1916 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1918 printk("%s%d", drv->dev_name, port->line);
1919 printk(" at ");
1920 switch (port->iotype) {
1921 case UPIO_PORT:
1922 printk("I/O 0x%x", port->iobase);
1923 break;
1924 case UPIO_HUB6:
1925 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1926 break;
1927 case UPIO_MEM:
1928 case UPIO_MEM32:
1929 printk("MMIO 0x%lx", port->mapbase);
1930 break;
1932 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
1935 static void
1936 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1937 struct uart_port *port)
1939 unsigned int flags;
1942 * If there isn't a port here, don't do anything further.
1944 if (!port->iobase && !port->mapbase && !port->membase)
1945 return;
1948 * Now do the auto configuration stuff. Note that config_port
1949 * is expected to claim the resources and map the port for us.
1951 flags = UART_CONFIG_TYPE;
1952 if (port->flags & UPF_AUTO_IRQ)
1953 flags |= UART_CONFIG_IRQ;
1954 if (port->flags & UPF_BOOT_AUTOCONF) {
1955 port->type = PORT_UNKNOWN;
1956 port->ops->config_port(port, flags);
1959 if (port->type != PORT_UNKNOWN) {
1960 unsigned long flags;
1962 uart_report_port(drv, port);
1965 * Ensure that the modem control lines are de-activated.
1966 * We probably don't need a spinlock around this, but
1968 spin_lock_irqsave(&port->lock, flags);
1969 port->ops->set_mctrl(port, 0);
1970 spin_unlock_irqrestore(&port->lock, flags);
1973 * Power down all ports by default, except the
1974 * console if we have one.
1976 if (!uart_console(port))
1977 uart_change_pm(state, 3);
1982 * This reverses the effects of uart_configure_port, hanging up the
1983 * port before removal.
1985 static void
1986 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
1988 struct uart_port *port = state->port;
1989 struct uart_info *info = state->info;
1991 if (info && info->tty)
1992 tty_vhangup(info->tty);
1994 down(&state->sem);
1996 state->info = NULL;
1999 * Free the port IO and memory resources, if any.
2001 if (port->type != PORT_UNKNOWN)
2002 port->ops->release_port(port);
2005 * Indicate that there isn't a port here anymore.
2007 port->type = PORT_UNKNOWN;
2010 * Kill the tasklet, and free resources.
2012 if (info) {
2013 tasklet_kill(&info->tlet);
2014 kfree(info);
2017 up(&state->sem);
2020 static struct tty_operations uart_ops = {
2021 .open = uart_open,
2022 .close = uart_close,
2023 .write = uart_write,
2024 .put_char = uart_put_char,
2025 .flush_chars = uart_flush_chars,
2026 .write_room = uart_write_room,
2027 .chars_in_buffer= uart_chars_in_buffer,
2028 .flush_buffer = uart_flush_buffer,
2029 .ioctl = uart_ioctl,
2030 .throttle = uart_throttle,
2031 .unthrottle = uart_unthrottle,
2032 .send_xchar = uart_send_xchar,
2033 .set_termios = uart_set_termios,
2034 .stop = uart_stop,
2035 .start = uart_start,
2036 .hangup = uart_hangup,
2037 .break_ctl = uart_break_ctl,
2038 .wait_until_sent= uart_wait_until_sent,
2039 #ifdef CONFIG_PROC_FS
2040 .read_proc = uart_read_proc,
2041 #endif
2042 .tiocmget = uart_tiocmget,
2043 .tiocmset = uart_tiocmset,
2047 * uart_register_driver - register a driver with the uart core layer
2048 * @drv: low level driver structure
2050 * Register a uart driver with the core driver. We in turn register
2051 * with the tty layer, and initialise the core driver per-port state.
2053 * We have a proc file in /proc/tty/driver which is named after the
2054 * normal driver.
2056 * drv->port should be NULL, and the per-port structures should be
2057 * registered using uart_add_one_port after this call has succeeded.
2059 int uart_register_driver(struct uart_driver *drv)
2061 struct tty_driver *normal = NULL;
2062 int i, retval;
2064 BUG_ON(drv->state);
2067 * Maybe we should be using a slab cache for this, especially if
2068 * we have a large number of ports to handle.
2070 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2071 retval = -ENOMEM;
2072 if (!drv->state)
2073 goto out;
2075 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2077 normal = alloc_tty_driver(drv->nr);
2078 if (!normal)
2079 goto out;
2081 drv->tty_driver = normal;
2083 normal->owner = drv->owner;
2084 normal->driver_name = drv->driver_name;
2085 normal->devfs_name = drv->devfs_name;
2086 normal->name = drv->dev_name;
2087 normal->major = drv->major;
2088 normal->minor_start = drv->minor;
2089 normal->type = TTY_DRIVER_TYPE_SERIAL;
2090 normal->subtype = SERIAL_TYPE_NORMAL;
2091 normal->init_termios = tty_std_termios;
2092 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2093 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2094 normal->driver_state = drv;
2095 tty_set_operations(normal, &uart_ops);
2098 * Initialise the UART state(s).
2100 for (i = 0; i < drv->nr; i++) {
2101 struct uart_state *state = drv->state + i;
2103 state->close_delay = 500; /* .5 seconds */
2104 state->closing_wait = 30000; /* 30 seconds */
2106 init_MUTEX(&state->sem);
2109 retval = tty_register_driver(normal);
2110 out:
2111 if (retval < 0) {
2112 put_tty_driver(normal);
2113 kfree(drv->state);
2115 return retval;
2119 * uart_unregister_driver - remove a driver from the uart core layer
2120 * @drv: low level driver structure
2122 * Remove all references to a driver from the core driver. The low
2123 * level driver must have removed all its ports via the
2124 * uart_remove_one_port() if it registered them with uart_add_one_port().
2125 * (ie, drv->port == NULL)
2127 void uart_unregister_driver(struct uart_driver *drv)
2129 struct tty_driver *p = drv->tty_driver;
2130 tty_unregister_driver(p);
2131 put_tty_driver(p);
2132 kfree(drv->state);
2133 drv->tty_driver = NULL;
2136 struct tty_driver *uart_console_device(struct console *co, int *index)
2138 struct uart_driver *p = co->data;
2139 *index = co->index;
2140 return p->tty_driver;
2144 * uart_add_one_port - attach a driver-defined port structure
2145 * @drv: pointer to the uart low level driver structure for this port
2146 * @port: uart port structure to use for this port.
2148 * This allows the driver to register its own uart_port structure
2149 * with the core driver. The main purpose is to allow the low
2150 * level uart drivers to expand uart_port, rather than having yet
2151 * more levels of structures.
2153 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2155 struct uart_state *state;
2156 int ret = 0;
2158 BUG_ON(in_interrupt());
2160 if (port->line >= drv->nr)
2161 return -EINVAL;
2163 state = drv->state + port->line;
2165 down(&port_sem);
2166 if (state->port) {
2167 ret = -EINVAL;
2168 goto out;
2171 state->port = port;
2173 spin_lock_init(&port->lock);
2174 port->cons = drv->cons;
2175 port->info = state->info;
2177 uart_configure_port(drv, state, port);
2180 * Register the port whether it's detected or not. This allows
2181 * setserial to be used to alter this ports parameters.
2183 tty_register_device(drv->tty_driver, port->line, port->dev);
2186 * If this driver supports console, and it hasn't been
2187 * successfully registered yet, try to re-register it.
2188 * It may be that the port was not available.
2190 if (port->type != PORT_UNKNOWN &&
2191 port->cons && !(port->cons->flags & CON_ENABLED))
2192 register_console(port->cons);
2194 out:
2195 up(&port_sem);
2197 return ret;
2201 * uart_remove_one_port - detach a driver defined port structure
2202 * @drv: pointer to the uart low level driver structure for this port
2203 * @port: uart port structure for this port
2205 * This unhooks (and hangs up) the specified port structure from the
2206 * core driver. No further calls will be made to the low-level code
2207 * for this port.
2209 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2211 struct uart_state *state = drv->state + port->line;
2213 BUG_ON(in_interrupt());
2215 if (state->port != port)
2216 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2217 state->port, port);
2219 down(&port_sem);
2222 * Remove the devices from devfs
2224 tty_unregister_device(drv->tty_driver, port->line);
2226 uart_unconfigure_port(drv, state);
2227 state->port = NULL;
2228 up(&port_sem);
2230 return 0;
2234 * Are the two ports equivalent?
2236 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2238 if (port1->iotype != port2->iotype)
2239 return 0;
2241 switch (port1->iotype) {
2242 case UPIO_PORT:
2243 return (port1->iobase == port2->iobase);
2244 case UPIO_HUB6:
2245 return (port1->iobase == port2->iobase) &&
2246 (port1->hub6 == port2->hub6);
2247 case UPIO_MEM:
2248 return (port1->membase == port2->membase);
2250 return 0;
2252 EXPORT_SYMBOL(uart_match_port);
2255 * Try to find an unused uart_state slot for a port.
2257 static struct uart_state *
2258 uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2260 int i;
2263 * First, find a port entry which matches. Note: if we do
2264 * find a matching entry, and it has a non-zero use count,
2265 * then we can't register the port.
2267 for (i = 0; i < drv->nr; i++)
2268 if (uart_match_port(drv->state[i].port, port))
2269 return &drv->state[i];
2272 * We didn't find a matching entry, so look for the first
2273 * free entry. We look for one which hasn't been previously
2274 * used (indicated by zero iobase).
2276 for (i = 0; i < drv->nr; i++)
2277 if (drv->state[i].port->type == PORT_UNKNOWN &&
2278 drv->state[i].port->iobase == 0 &&
2279 drv->state[i].count == 0)
2280 return &drv->state[i];
2283 * That also failed. Last resort is to find any currently
2284 * entry which doesn't have a real port associated with it.
2286 for (i = 0; i < drv->nr; i++)
2287 if (drv->state[i].port->type == PORT_UNKNOWN &&
2288 drv->state[i].count == 0)
2289 return &drv->state[i];
2291 return NULL;
2295 * uart_register_port: register uart settings with a port
2296 * @drv: pointer to the uart low level driver structure for this port
2297 * @port: uart port structure describing the port
2299 * Register UART settings with the specified low level driver. Detect
2300 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2301 * IRQ if UPF_AUTO_IRQ is set.
2303 * We try to pick the same port for the same IO base address, so that
2304 * when a modem is plugged in, unplugged and plugged back in, it gets
2305 * allocated the same port.
2307 * Returns negative error, or positive line number.
2309 int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2311 struct uart_state *state;
2312 int ret;
2314 down(&port_sem);
2316 state = uart_find_match_or_unused(drv, port);
2318 if (state) {
2320 * Ok, we've found a line that we can use.
2322 * If we find a port that matches this one, and it appears
2323 * to be in-use (even if it doesn't have a type) we shouldn't
2324 * alter it underneath itself - the port may be open and
2325 * trying to do useful work.
2327 if (uart_users(state) != 0) {
2328 ret = -EBUSY;
2329 goto out;
2333 * If the port is already initialised, don't touch it.
2335 if (state->port->type == PORT_UNKNOWN) {
2336 state->port->iobase = port->iobase;
2337 state->port->membase = port->membase;
2338 state->port->irq = port->irq;
2339 state->port->uartclk = port->uartclk;
2340 state->port->fifosize = port->fifosize;
2341 state->port->regshift = port->regshift;
2342 state->port->iotype = port->iotype;
2343 state->port->flags = port->flags;
2344 state->port->line = state - drv->state;
2345 state->port->mapbase = port->mapbase;
2347 uart_configure_port(drv, state, state->port);
2350 ret = state->port->line;
2351 } else
2352 ret = -ENOSPC;
2353 out:
2354 up(&port_sem);
2355 return ret;
2359 * uart_unregister_port - de-allocate a port
2360 * @drv: pointer to the uart low level driver structure for this port
2361 * @line: line index previously returned from uart_register_port()
2363 * Hang up the specified line associated with the low level driver,
2364 * and mark the port as unused.
2366 void uart_unregister_port(struct uart_driver *drv, int line)
2368 struct uart_state *state;
2370 if (line < 0 || line >= drv->nr) {
2371 printk(KERN_ERR "Attempt to unregister ");
2372 printk("%s%d", drv->dev_name, line);
2373 printk("\n");
2374 return;
2377 state = drv->state + line;
2379 down(&port_sem);
2380 uart_unconfigure_port(drv, state);
2381 up(&port_sem);
2384 EXPORT_SYMBOL(uart_write_wakeup);
2385 EXPORT_SYMBOL(uart_register_driver);
2386 EXPORT_SYMBOL(uart_unregister_driver);
2387 EXPORT_SYMBOL(uart_suspend_port);
2388 EXPORT_SYMBOL(uart_resume_port);
2389 EXPORT_SYMBOL(uart_register_port);
2390 EXPORT_SYMBOL(uart_unregister_port);
2391 EXPORT_SYMBOL(uart_add_one_port);
2392 EXPORT_SYMBOL(uart_remove_one_port);
2394 MODULE_DESCRIPTION("Serial driver core");
2395 MODULE_LICENSE("GPL");