[PATCH] Clarify help text of SKGE/SK98LIN/SKY2
[firewire-audio.git] / drivers / serial / serial_core.c
blob943770470b9db316af2ed25cce1fca9296df43a2
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>
36 #include <linux/mutex.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
41 #undef DEBUG
42 #ifdef DEBUG
43 #define DPRINTK(x...) printk(x)
44 #else
45 #define DPRINTK(x...) do { } while (0)
46 #endif
49 * This is used to lock changes in serial line configuration.
51 static DEFINE_MUTEX(port_mutex);
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
57 #ifdef CONFIG_SERIAL_CORE_CONSOLE
58 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
59 #else
60 #define uart_console(port) (0)
61 #endif
63 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
64 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
65 static void uart_change_pm(struct uart_state *state, int pm_state);
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
71 void uart_write_wakeup(struct uart_port *port)
73 struct uart_info *info = port->info;
74 tasklet_schedule(&info->tlet);
77 static void uart_stop(struct tty_struct *tty)
79 struct uart_state *state = tty->driver_data;
80 struct uart_port *port = state->port;
81 unsigned long flags;
83 spin_lock_irqsave(&port->lock, flags);
84 port->ops->stop_tx(port);
85 spin_unlock_irqrestore(&port->lock, flags);
88 static void __uart_start(struct tty_struct *tty)
90 struct uart_state *state = tty->driver_data;
91 struct uart_port *port = state->port;
93 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
94 !tty->stopped && !tty->hw_stopped)
95 port->ops->start_tx(port);
98 static void uart_start(struct tty_struct *tty)
100 struct uart_state *state = tty->driver_data;
101 struct uart_port *port = state->port;
102 unsigned long flags;
104 spin_lock_irqsave(&port->lock, flags);
105 __uart_start(tty);
106 spin_unlock_irqrestore(&port->lock, flags);
109 static void uart_tasklet_action(unsigned long data)
111 struct uart_state *state = (struct uart_state *)data;
112 tty_wakeup(state->info->tty);
115 static inline void
116 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
118 unsigned long flags;
119 unsigned int old;
121 spin_lock_irqsave(&port->lock, flags);
122 old = port->mctrl;
123 port->mctrl = (old & ~clear) | set;
124 if (old != port->mctrl)
125 port->ops->set_mctrl(port, port->mctrl);
126 spin_unlock_irqrestore(&port->lock, flags);
129 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
130 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
133 * Startup the port. This will be called once per open. All calls
134 * will be serialised by the per-port semaphore.
136 static int uart_startup(struct uart_state *state, int init_hw)
138 struct uart_info *info = state->info;
139 struct uart_port *port = state->port;
140 unsigned long page;
141 int retval = 0;
143 if (info->flags & UIF_INITIALIZED)
144 return 0;
147 * Set the TTY IO error marker - we will only clear this
148 * once we have successfully opened the port. Also set
149 * up the tty->alt_speed kludge
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 if (info->flags & UIF_CTS_FLOW) {
186 spin_lock_irq(&port->lock);
187 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
188 info->tty->hw_stopped = 1;
189 spin_unlock_irq(&port->lock);
192 info->flags |= UIF_INITIALIZED;
194 clear_bit(TTY_IO_ERROR, &info->tty->flags);
197 if (retval && capable(CAP_SYS_ADMIN))
198 retval = 0;
200 return retval;
204 * This routine will shutdown a serial port; interrupts are disabled, and
205 * DTR is dropped if the hangup on close termio flag is on. Calls to
206 * uart_shutdown are serialised by the per-port semaphore.
208 static void uart_shutdown(struct uart_state *state)
210 struct uart_info *info = state->info;
211 struct uart_port *port = state->port;
214 * Set the TTY IO error marker
216 if (info->tty)
217 set_bit(TTY_IO_ERROR, &info->tty->flags);
219 if (info->flags & UIF_INITIALIZED) {
220 info->flags &= ~UIF_INITIALIZED;
223 * Turn off DTR and RTS early.
225 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
226 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
229 * clear delta_msr_wait queue to avoid mem leaks: we may free
230 * the irq here so the queue might never be woken up. Note
231 * that we won't end up waiting on delta_msr_wait again since
232 * any outstanding file descriptors should be pointing at
233 * hung_up_tty_fops now.
235 wake_up_interruptible(&info->delta_msr_wait);
238 * Free the IRQ and disable the port.
240 port->ops->shutdown(port);
243 * Ensure that the IRQ handler isn't running on another CPU.
245 synchronize_irq(port->irq);
249 * kill off our tasklet
251 tasklet_kill(&info->tlet);
254 * Free the transmit buffer page.
256 if (info->xmit.buf) {
257 free_page((unsigned long)info->xmit.buf);
258 info->xmit.buf = NULL;
263 * uart_update_timeout - update per-port FIFO timeout.
264 * @port: uart_port structure describing the port
265 * @cflag: termios cflag value
266 * @baud: speed of the port
268 * Set the port FIFO timeout value. The @cflag value should
269 * reflect the actual hardware settings.
271 void
272 uart_update_timeout(struct uart_port *port, unsigned int cflag,
273 unsigned int baud)
275 unsigned int bits;
277 /* byte size and parity */
278 switch (cflag & CSIZE) {
279 case CS5:
280 bits = 7;
281 break;
282 case CS6:
283 bits = 8;
284 break;
285 case CS7:
286 bits = 9;
287 break;
288 default:
289 bits = 10;
290 break; // CS8
293 if (cflag & CSTOPB)
294 bits++;
295 if (cflag & PARENB)
296 bits++;
299 * The total number of bits to be transmitted in the fifo.
301 bits = bits * port->fifosize;
304 * Figure the timeout to send the above number of bits.
305 * Add .02 seconds of slop
307 port->timeout = (HZ * bits) / baud + HZ/50;
310 EXPORT_SYMBOL(uart_update_timeout);
313 * uart_get_baud_rate - return baud rate for a particular port
314 * @port: uart_port structure describing the port in question.
315 * @termios: desired termios settings.
316 * @old: old termios (or NULL)
317 * @min: minimum acceptable baud rate
318 * @max: maximum acceptable baud rate
320 * Decode the termios structure into a numeric baud rate,
321 * taking account of the magic 38400 baud rate (with spd_*
322 * flags), and mapping the %B0 rate to 9600 baud.
324 * If the new baud rate is invalid, try the old termios setting.
325 * If it's still invalid, we try 9600 baud.
327 * Update the @termios structure to reflect the baud rate
328 * we're actually going to be using.
330 unsigned int
331 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
332 struct termios *old, unsigned int min, unsigned int max)
334 unsigned int try, baud, altbaud = 38400;
335 unsigned int flags = port->flags & UPF_SPD_MASK;
337 if (flags == UPF_SPD_HI)
338 altbaud = 57600;
339 if (flags == UPF_SPD_VHI)
340 altbaud = 115200;
341 if (flags == UPF_SPD_SHI)
342 altbaud = 230400;
343 if (flags == UPF_SPD_WARP)
344 altbaud = 460800;
346 for (try = 0; try < 2; try++) {
347 baud = tty_termios_baud_rate(termios);
350 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
351 * Die! Die! Die!
353 if (baud == 38400)
354 baud = altbaud;
357 * Special case: B0 rate.
359 if (baud == 0)
360 baud = 9600;
362 if (baud >= min && baud <= max)
363 return baud;
366 * Oops, the quotient was zero. Try again with
367 * the old baud rate if possible.
369 termios->c_cflag &= ~CBAUD;
370 if (old) {
371 termios->c_cflag |= old->c_cflag & CBAUD;
372 old = NULL;
373 continue;
377 * As a last resort, if the quotient is zero,
378 * default to 9600 bps
380 termios->c_cflag |= B9600;
383 return 0;
386 EXPORT_SYMBOL(uart_get_baud_rate);
389 * uart_get_divisor - return uart clock divisor
390 * @port: uart_port structure describing the port.
391 * @baud: desired baud rate
393 * Calculate the uart clock divisor for the port.
395 unsigned int
396 uart_get_divisor(struct uart_port *port, unsigned int baud)
398 unsigned int quot;
401 * Old custom speed handling.
403 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
404 quot = port->custom_divisor;
405 else
406 quot = (port->uartclk + (8 * baud)) / (16 * baud);
408 return quot;
411 EXPORT_SYMBOL(uart_get_divisor);
413 static void
414 uart_change_speed(struct uart_state *state, struct termios *old_termios)
416 struct tty_struct *tty = state->info->tty;
417 struct uart_port *port = state->port;
418 struct termios *termios;
421 * If we have no tty, termios, or the port does not exist,
422 * then we can't set the parameters for this port.
424 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
425 return;
427 termios = tty->termios;
430 * Set flags based on termios cflag
432 if (termios->c_cflag & CRTSCTS)
433 state->info->flags |= UIF_CTS_FLOW;
434 else
435 state->info->flags &= ~UIF_CTS_FLOW;
437 if (termios->c_cflag & CLOCAL)
438 state->info->flags &= ~UIF_CHECK_CD;
439 else
440 state->info->flags |= UIF_CHECK_CD;
442 port->ops->set_termios(port, termios, old_termios);
445 static inline void
446 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
448 unsigned long flags;
450 if (!circ->buf)
451 return;
453 spin_lock_irqsave(&port->lock, flags);
454 if (uart_circ_chars_free(circ) != 0) {
455 circ->buf[circ->head] = c;
456 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
458 spin_unlock_irqrestore(&port->lock, flags);
461 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
463 struct uart_state *state = tty->driver_data;
465 __uart_put_char(state->port, &state->info->xmit, ch);
468 static void uart_flush_chars(struct tty_struct *tty)
470 uart_start(tty);
473 static int
474 uart_write(struct tty_struct *tty, const unsigned char * buf, int count)
476 struct uart_state *state = tty->driver_data;
477 struct uart_port *port = state->port;
478 struct circ_buf *circ = &state->info->xmit;
479 unsigned long flags;
480 int c, ret = 0;
482 if (!circ->buf)
483 return 0;
485 spin_lock_irqsave(&port->lock, flags);
486 while (1) {
487 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
488 if (count < c)
489 c = count;
490 if (c <= 0)
491 break;
492 memcpy(circ->buf + circ->head, buf, c);
493 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
494 buf += c;
495 count -= c;
496 ret += c;
498 spin_unlock_irqrestore(&port->lock, flags);
500 uart_start(tty);
501 return ret;
504 static int uart_write_room(struct tty_struct *tty)
506 struct uart_state *state = tty->driver_data;
508 return uart_circ_chars_free(&state->info->xmit);
511 static int uart_chars_in_buffer(struct tty_struct *tty)
513 struct uart_state *state = tty->driver_data;
515 return uart_circ_chars_pending(&state->info->xmit);
518 static void uart_flush_buffer(struct tty_struct *tty)
520 struct uart_state *state = tty->driver_data;
521 struct uart_port *port = state->port;
522 unsigned long flags;
524 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
526 spin_lock_irqsave(&port->lock, flags);
527 uart_circ_clear(&state->info->xmit);
528 spin_unlock_irqrestore(&port->lock, flags);
529 tty_wakeup(tty);
533 * This function is used to send a high-priority XON/XOFF character to
534 * the device
536 static void uart_send_xchar(struct tty_struct *tty, char ch)
538 struct uart_state *state = tty->driver_data;
539 struct uart_port *port = state->port;
540 unsigned long flags;
542 if (port->ops->send_xchar)
543 port->ops->send_xchar(port, ch);
544 else {
545 port->x_char = ch;
546 if (ch) {
547 spin_lock_irqsave(&port->lock, flags);
548 port->ops->start_tx(port);
549 spin_unlock_irqrestore(&port->lock, flags);
554 static void uart_throttle(struct tty_struct *tty)
556 struct uart_state *state = tty->driver_data;
558 if (I_IXOFF(tty))
559 uart_send_xchar(tty, STOP_CHAR(tty));
561 if (tty->termios->c_cflag & CRTSCTS)
562 uart_clear_mctrl(state->port, TIOCM_RTS);
565 static void uart_unthrottle(struct tty_struct *tty)
567 struct uart_state *state = tty->driver_data;
568 struct uart_port *port = state->port;
570 if (I_IXOFF(tty)) {
571 if (port->x_char)
572 port->x_char = 0;
573 else
574 uart_send_xchar(tty, START_CHAR(tty));
577 if (tty->termios->c_cflag & CRTSCTS)
578 uart_set_mctrl(port, TIOCM_RTS);
581 static int uart_get_info(struct uart_state *state,
582 struct serial_struct __user *retinfo)
584 struct uart_port *port = state->port;
585 struct serial_struct tmp;
587 memset(&tmp, 0, sizeof(tmp));
588 tmp.type = port->type;
589 tmp.line = port->line;
590 tmp.port = port->iobase;
591 if (HIGH_BITS_OFFSET)
592 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
593 tmp.irq = port->irq;
594 tmp.flags = port->flags;
595 tmp.xmit_fifo_size = port->fifosize;
596 tmp.baud_base = port->uartclk / 16;
597 tmp.close_delay = state->close_delay / 10;
598 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
599 ASYNC_CLOSING_WAIT_NONE :
600 state->closing_wait / 10;
601 tmp.custom_divisor = port->custom_divisor;
602 tmp.hub6 = port->hub6;
603 tmp.io_type = port->iotype;
604 tmp.iomem_reg_shift = port->regshift;
605 tmp.iomem_base = (void *)port->mapbase;
607 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
608 return -EFAULT;
609 return 0;
612 static int uart_set_info(struct uart_state *state,
613 struct serial_struct __user *newinfo)
615 struct serial_struct new_serial;
616 struct uart_port *port = state->port;
617 unsigned long new_port;
618 unsigned int change_irq, change_port, old_flags, closing_wait;
619 unsigned int old_custom_divisor, close_delay;
620 int retval = 0;
622 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
623 return -EFAULT;
625 new_port = new_serial.port;
626 if (HIGH_BITS_OFFSET)
627 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
629 new_serial.irq = irq_canonicalize(new_serial.irq);
630 close_delay = new_serial.close_delay * 10;
631 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
632 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
635 * This semaphore protects state->count. It is also
636 * very useful to prevent opens. Also, take the
637 * port configuration semaphore to make sure that a
638 * module insertion/removal doesn't change anything
639 * under us.
641 mutex_lock(&state->mutex);
643 change_irq = new_serial.irq != port->irq;
646 * Since changing the 'type' of the port changes its resource
647 * allocations, we should treat type changes the same as
648 * IO port changes.
650 change_port = new_port != port->iobase ||
651 (unsigned long)new_serial.iomem_base != port->mapbase ||
652 new_serial.hub6 != port->hub6 ||
653 new_serial.io_type != port->iotype ||
654 new_serial.iomem_reg_shift != port->regshift ||
655 new_serial.type != port->type;
657 old_flags = port->flags;
658 old_custom_divisor = port->custom_divisor;
660 if (!capable(CAP_SYS_ADMIN)) {
661 retval = -EPERM;
662 if (change_irq || change_port ||
663 (new_serial.baud_base != port->uartclk / 16) ||
664 (close_delay != state->close_delay) ||
665 (closing_wait != state->closing_wait) ||
666 (new_serial.xmit_fifo_size != port->fifosize) ||
667 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
668 goto exit;
669 port->flags = ((port->flags & ~UPF_USR_MASK) |
670 (new_serial.flags & UPF_USR_MASK));
671 port->custom_divisor = new_serial.custom_divisor;
672 goto check_and_exit;
676 * Ask the low level driver to verify the settings.
678 if (port->ops->verify_port)
679 retval = port->ops->verify_port(port, &new_serial);
681 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
682 (new_serial.baud_base < 9600))
683 retval = -EINVAL;
685 if (retval)
686 goto exit;
688 if (change_port || change_irq) {
689 retval = -EBUSY;
692 * Make sure that we are the sole user of this port.
694 if (uart_users(state) > 1)
695 goto exit;
698 * We need to shutdown the serial port at the old
699 * port/type/irq combination.
701 uart_shutdown(state);
704 if (change_port) {
705 unsigned long old_iobase, old_mapbase;
706 unsigned int old_type, old_iotype, old_hub6, old_shift;
708 old_iobase = port->iobase;
709 old_mapbase = port->mapbase;
710 old_type = port->type;
711 old_hub6 = port->hub6;
712 old_iotype = port->iotype;
713 old_shift = port->regshift;
716 * Free and release old regions
718 if (old_type != PORT_UNKNOWN)
719 port->ops->release_port(port);
721 port->iobase = new_port;
722 port->type = new_serial.type;
723 port->hub6 = new_serial.hub6;
724 port->iotype = new_serial.io_type;
725 port->regshift = new_serial.iomem_reg_shift;
726 port->mapbase = (unsigned long)new_serial.iomem_base;
729 * Claim and map the new regions
731 if (port->type != PORT_UNKNOWN) {
732 retval = port->ops->request_port(port);
733 } else {
734 /* Always success - Jean II */
735 retval = 0;
739 * If we fail to request resources for the
740 * new port, try to restore the old settings.
742 if (retval && old_type != PORT_UNKNOWN) {
743 port->iobase = old_iobase;
744 port->type = old_type;
745 port->hub6 = old_hub6;
746 port->iotype = old_iotype;
747 port->regshift = old_shift;
748 port->mapbase = old_mapbase;
749 retval = port->ops->request_port(port);
751 * If we failed to restore the old settings,
752 * we fail like this.
754 if (retval)
755 port->type = PORT_UNKNOWN;
758 * We failed anyway.
760 retval = -EBUSY;
764 port->irq = new_serial.irq;
765 port->uartclk = new_serial.baud_base * 16;
766 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
767 (new_serial.flags & UPF_CHANGE_MASK);
768 port->custom_divisor = new_serial.custom_divisor;
769 state->close_delay = close_delay;
770 state->closing_wait = closing_wait;
771 port->fifosize = new_serial.xmit_fifo_size;
772 if (state->info->tty)
773 state->info->tty->low_latency =
774 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
776 check_and_exit:
777 retval = 0;
778 if (port->type == PORT_UNKNOWN)
779 goto exit;
780 if (state->info->flags & UIF_INITIALIZED) {
781 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
782 old_custom_divisor != port->custom_divisor) {
784 * If they're setting up a custom divisor or speed,
785 * instead of clearing it, then bitch about it. No
786 * need to rate-limit; it's CAP_SYS_ADMIN only.
788 if (port->flags & UPF_SPD_MASK) {
789 char buf[64];
790 printk(KERN_NOTICE
791 "%s sets custom speed on %s. This "
792 "is deprecated.\n", current->comm,
793 tty_name(state->info->tty, buf));
795 uart_change_speed(state, NULL);
797 } else
798 retval = uart_startup(state, 1);
799 exit:
800 mutex_unlock(&state->mutex);
801 return retval;
806 * uart_get_lsr_info - get line status register info.
807 * Note: uart_ioctl protects us against hangups.
809 static int uart_get_lsr_info(struct uart_state *state,
810 unsigned int __user *value)
812 struct uart_port *port = state->port;
813 unsigned int result;
815 result = port->ops->tx_empty(port);
818 * If we're about to load something into the transmit
819 * register, we'll pretend the transmitter isn't empty to
820 * avoid a race condition (depending on when the transmit
821 * interrupt happens).
823 if (port->x_char ||
824 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
825 !state->info->tty->stopped && !state->info->tty->hw_stopped))
826 result &= ~TIOCSER_TEMT;
828 return put_user(result, value);
831 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
833 struct uart_state *state = tty->driver_data;
834 struct uart_port *port = state->port;
835 int result = -EIO;
837 mutex_lock(&state->mutex);
838 if ((!file || !tty_hung_up_p(file)) &&
839 !(tty->flags & (1 << TTY_IO_ERROR))) {
840 result = port->mctrl;
842 spin_lock_irq(&port->lock);
843 result |= port->ops->get_mctrl(port);
844 spin_unlock_irq(&port->lock);
846 mutex_unlock(&state->mutex);
848 return result;
851 static int
852 uart_tiocmset(struct tty_struct *tty, struct file *file,
853 unsigned int set, unsigned int clear)
855 struct uart_state *state = tty->driver_data;
856 struct uart_port *port = state->port;
857 int ret = -EIO;
859 mutex_lock(&state->mutex);
860 if ((!file || !tty_hung_up_p(file)) &&
861 !(tty->flags & (1 << TTY_IO_ERROR))) {
862 uart_update_mctrl(port, set, clear);
863 ret = 0;
865 mutex_unlock(&state->mutex);
866 return ret;
869 static void uart_break_ctl(struct tty_struct *tty, int break_state)
871 struct uart_state *state = tty->driver_data;
872 struct uart_port *port = state->port;
874 BUG_ON(!kernel_locked());
876 mutex_lock(&state->mutex);
878 if (port->type != PORT_UNKNOWN)
879 port->ops->break_ctl(port, break_state);
881 mutex_unlock(&state->mutex);
884 static int uart_do_autoconfig(struct uart_state *state)
886 struct uart_port *port = state->port;
887 int flags, ret;
889 if (!capable(CAP_SYS_ADMIN))
890 return -EPERM;
893 * Take the per-port semaphore. This prevents count from
894 * changing, and hence any extra opens of the port while
895 * we're auto-configuring.
897 if (mutex_lock_interruptible(&state->mutex))
898 return -ERESTARTSYS;
900 ret = -EBUSY;
901 if (uart_users(state) == 1) {
902 uart_shutdown(state);
905 * If we already have a port type configured,
906 * we must release its resources.
908 if (port->type != PORT_UNKNOWN)
909 port->ops->release_port(port);
911 flags = UART_CONFIG_TYPE;
912 if (port->flags & UPF_AUTO_IRQ)
913 flags |= UART_CONFIG_IRQ;
916 * This will claim the ports resources if
917 * a port is found.
919 port->ops->config_port(port, flags);
921 ret = uart_startup(state, 1);
923 mutex_unlock(&state->mutex);
924 return ret;
928 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
929 * - mask passed in arg for lines of interest
930 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
931 * Caller should use TIOCGICOUNT to see which one it was
933 static int
934 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
936 struct uart_port *port = state->port;
937 DECLARE_WAITQUEUE(wait, current);
938 struct uart_icount cprev, cnow;
939 int ret;
942 * note the counters on entry
944 spin_lock_irq(&port->lock);
945 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
948 * Force modem status interrupts on
950 port->ops->enable_ms(port);
951 spin_unlock_irq(&port->lock);
953 add_wait_queue(&state->info->delta_msr_wait, &wait);
954 for (;;) {
955 spin_lock_irq(&port->lock);
956 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
957 spin_unlock_irq(&port->lock);
959 set_current_state(TASK_INTERRUPTIBLE);
961 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
962 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
963 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
964 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
965 ret = 0;
966 break;
969 schedule();
971 /* see if a signal did it */
972 if (signal_pending(current)) {
973 ret = -ERESTARTSYS;
974 break;
977 cprev = cnow;
980 current->state = TASK_RUNNING;
981 remove_wait_queue(&state->info->delta_msr_wait, &wait);
983 return ret;
987 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
988 * Return: write counters to the user passed counter struct
989 * NB: both 1->0 and 0->1 transitions are counted except for
990 * RI where only 0->1 is counted.
992 static int uart_get_count(struct uart_state *state,
993 struct serial_icounter_struct __user *icnt)
995 struct serial_icounter_struct icount;
996 struct uart_icount cnow;
997 struct uart_port *port = state->port;
999 spin_lock_irq(&port->lock);
1000 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1001 spin_unlock_irq(&port->lock);
1003 icount.cts = cnow.cts;
1004 icount.dsr = cnow.dsr;
1005 icount.rng = cnow.rng;
1006 icount.dcd = cnow.dcd;
1007 icount.rx = cnow.rx;
1008 icount.tx = cnow.tx;
1009 icount.frame = cnow.frame;
1010 icount.overrun = cnow.overrun;
1011 icount.parity = cnow.parity;
1012 icount.brk = cnow.brk;
1013 icount.buf_overrun = cnow.buf_overrun;
1015 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1019 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1021 static int
1022 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1023 unsigned long arg)
1025 struct uart_state *state = tty->driver_data;
1026 void __user *uarg = (void __user *)arg;
1027 int ret = -ENOIOCTLCMD;
1029 BUG_ON(!kernel_locked());
1032 * These ioctls don't rely on the hardware to be present.
1034 switch (cmd) {
1035 case TIOCGSERIAL:
1036 ret = uart_get_info(state, uarg);
1037 break;
1039 case TIOCSSERIAL:
1040 ret = uart_set_info(state, uarg);
1041 break;
1043 case TIOCSERCONFIG:
1044 ret = uart_do_autoconfig(state);
1045 break;
1047 case TIOCSERGWILD: /* obsolete */
1048 case TIOCSERSWILD: /* obsolete */
1049 ret = 0;
1050 break;
1053 if (ret != -ENOIOCTLCMD)
1054 goto out;
1056 if (tty->flags & (1 << TTY_IO_ERROR)) {
1057 ret = -EIO;
1058 goto out;
1062 * The following should only be used when hardware is present.
1064 switch (cmd) {
1065 case TIOCMIWAIT:
1066 ret = uart_wait_modem_status(state, arg);
1067 break;
1069 case TIOCGICOUNT:
1070 ret = uart_get_count(state, uarg);
1071 break;
1074 if (ret != -ENOIOCTLCMD)
1075 goto out;
1077 mutex_lock(&state->mutex);
1079 if (tty_hung_up_p(filp)) {
1080 ret = -EIO;
1081 goto out_up;
1085 * All these rely on hardware being present and need to be
1086 * protected against the tty being hung up.
1088 switch (cmd) {
1089 case TIOCSERGETLSR: /* Get line status register */
1090 ret = uart_get_lsr_info(state, uarg);
1091 break;
1093 default: {
1094 struct uart_port *port = state->port;
1095 if (port->ops->ioctl)
1096 ret = port->ops->ioctl(port, cmd, arg);
1097 break;
1100 out_up:
1101 mutex_unlock(&state->mutex);
1102 out:
1103 return ret;
1106 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1108 struct uart_state *state = tty->driver_data;
1109 unsigned long flags;
1110 unsigned int cflag = tty->termios->c_cflag;
1112 BUG_ON(!kernel_locked());
1115 * These are the bits that are used to setup various
1116 * flags in the low level driver.
1118 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1120 if ((cflag ^ old_termios->c_cflag) == 0 &&
1121 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1122 return;
1124 uart_change_speed(state, old_termios);
1126 /* Handle transition to B0 status */
1127 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1128 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1130 /* Handle transition away from B0 status */
1131 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1132 unsigned int mask = TIOCM_DTR;
1133 if (!(cflag & CRTSCTS) ||
1134 !test_bit(TTY_THROTTLED, &tty->flags))
1135 mask |= TIOCM_RTS;
1136 uart_set_mctrl(state->port, mask);
1139 /* Handle turning off CRTSCTS */
1140 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1141 spin_lock_irqsave(&state->port->lock, flags);
1142 tty->hw_stopped = 0;
1143 __uart_start(tty);
1144 spin_unlock_irqrestore(&state->port->lock, flags);
1147 /* Handle turning on CRTSCTS */
1148 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1149 spin_lock_irqsave(&state->port->lock, flags);
1150 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1151 tty->hw_stopped = 1;
1152 state->port->ops->stop_tx(state->port);
1154 spin_unlock_irqrestore(&state->port->lock, flags);
1157 #if 0
1159 * No need to wake up processes in open wait, since they
1160 * sample the CLOCAL flag once, and don't recheck it.
1161 * XXX It's not clear whether the current behavior is correct
1162 * or not. Hence, this may change.....
1164 if (!(old_termios->c_cflag & CLOCAL) &&
1165 (tty->termios->c_cflag & CLOCAL))
1166 wake_up_interruptible(&state->info->open_wait);
1167 #endif
1171 * In 2.4.5, calls to this will be serialized via the BKL in
1172 * linux/drivers/char/tty_io.c:tty_release()
1173 * linux/drivers/char/tty_io.c:do_tty_handup()
1175 static void uart_close(struct tty_struct *tty, struct file *filp)
1177 struct uart_state *state = tty->driver_data;
1178 struct uart_port *port;
1180 BUG_ON(!kernel_locked());
1182 if (!state || !state->port)
1183 return;
1185 port = state->port;
1187 DPRINTK("uart_close(%d) called\n", port->line);
1189 mutex_lock(&state->mutex);
1191 if (tty_hung_up_p(filp))
1192 goto done;
1194 if ((tty->count == 1) && (state->count != 1)) {
1196 * Uh, oh. tty->count is 1, which means that the tty
1197 * structure will be freed. state->count should always
1198 * be one in these conditions. If it's greater than
1199 * one, we've got real problems, since it means the
1200 * serial port won't be shutdown.
1202 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1203 "state->count is %d\n", state->count);
1204 state->count = 1;
1206 if (--state->count < 0) {
1207 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1208 tty->name, state->count);
1209 state->count = 0;
1211 if (state->count)
1212 goto done;
1215 * Now we wait for the transmit buffer to clear; and we notify
1216 * the line discipline to only process XON/XOFF characters by
1217 * setting tty->closing.
1219 tty->closing = 1;
1221 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1222 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1225 * At this point, we stop accepting input. To do this, we
1226 * disable the receive line status interrupts.
1228 if (state->info->flags & UIF_INITIALIZED) {
1229 unsigned long flags;
1230 spin_lock_irqsave(&port->lock, flags);
1231 port->ops->stop_rx(port);
1232 spin_unlock_irqrestore(&port->lock, flags);
1234 * Before we drop DTR, make sure the UART transmitter
1235 * has completely drained; this is especially
1236 * important if there is a transmit FIFO!
1238 uart_wait_until_sent(tty, port->timeout);
1241 uart_shutdown(state);
1242 uart_flush_buffer(tty);
1244 tty_ldisc_flush(tty);
1246 tty->closing = 0;
1247 state->info->tty = NULL;
1249 if (state->info->blocked_open) {
1250 if (state->close_delay)
1251 msleep_interruptible(state->close_delay);
1252 } else if (!uart_console(port)) {
1253 uart_change_pm(state, 3);
1257 * Wake up anyone trying to open this port.
1259 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1260 wake_up_interruptible(&state->info->open_wait);
1262 done:
1263 mutex_unlock(&state->mutex);
1266 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1268 struct uart_state *state = tty->driver_data;
1269 struct uart_port *port = state->port;
1270 unsigned long char_time, expire;
1272 BUG_ON(!kernel_locked());
1274 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1275 return;
1278 * Set the check interval to be 1/5 of the estimated time to
1279 * send a single character, and make it at least 1. The check
1280 * interval should also be less than the timeout.
1282 * Note: we have to use pretty tight timings here to satisfy
1283 * the NIST-PCTS.
1285 char_time = (port->timeout - HZ/50) / port->fifosize;
1286 char_time = char_time / 5;
1287 if (char_time == 0)
1288 char_time = 1;
1289 if (timeout && timeout < char_time)
1290 char_time = timeout;
1293 * If the transmitter hasn't cleared in twice the approximate
1294 * amount of time to send the entire FIFO, it probably won't
1295 * ever clear. This assumes the UART isn't doing flow
1296 * control, which is currently the case. Hence, if it ever
1297 * takes longer than port->timeout, this is probably due to a
1298 * UART bug of some kind. So, we clamp the timeout parameter at
1299 * 2*port->timeout.
1301 if (timeout == 0 || timeout > 2 * port->timeout)
1302 timeout = 2 * port->timeout;
1304 expire = jiffies + timeout;
1306 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1307 port->line, jiffies, expire);
1310 * Check whether the transmitter is empty every 'char_time'.
1311 * 'timeout' / 'expire' give us the maximum amount of time
1312 * we wait.
1314 while (!port->ops->tx_empty(port)) {
1315 msleep_interruptible(jiffies_to_msecs(char_time));
1316 if (signal_pending(current))
1317 break;
1318 if (time_after(jiffies, expire))
1319 break;
1321 set_current_state(TASK_RUNNING); /* might not be needed */
1325 * This is called with the BKL held in
1326 * linux/drivers/char/tty_io.c:do_tty_hangup()
1327 * We're called from the eventd thread, so we can sleep for
1328 * a _short_ time only.
1330 static void uart_hangup(struct tty_struct *tty)
1332 struct uart_state *state = tty->driver_data;
1334 BUG_ON(!kernel_locked());
1335 DPRINTK("uart_hangup(%d)\n", state->port->line);
1337 mutex_lock(&state->mutex);
1338 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1339 uart_flush_buffer(tty);
1340 uart_shutdown(state);
1341 state->count = 0;
1342 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1343 state->info->tty = NULL;
1344 wake_up_interruptible(&state->info->open_wait);
1345 wake_up_interruptible(&state->info->delta_msr_wait);
1347 mutex_unlock(&state->mutex);
1351 * Copy across the serial console cflag setting into the termios settings
1352 * for the initial open of the port. This allows continuity between the
1353 * kernel settings, and the settings init adopts when it opens the port
1354 * for the first time.
1356 static void uart_update_termios(struct uart_state *state)
1358 struct tty_struct *tty = state->info->tty;
1359 struct uart_port *port = state->port;
1361 if (uart_console(port) && port->cons->cflag) {
1362 tty->termios->c_cflag = port->cons->cflag;
1363 port->cons->cflag = 0;
1367 * If the device failed to grab its irq resources,
1368 * or some other error occurred, don't try to talk
1369 * to the port hardware.
1371 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1373 * Make termios settings take effect.
1375 uart_change_speed(state, NULL);
1378 * And finally enable the RTS and DTR signals.
1380 if (tty->termios->c_cflag & CBAUD)
1381 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1386 * Block the open until the port is ready. We must be called with
1387 * the per-port semaphore held.
1389 static int
1390 uart_block_til_ready(struct file *filp, struct uart_state *state)
1392 DECLARE_WAITQUEUE(wait, current);
1393 struct uart_info *info = state->info;
1394 struct uart_port *port = state->port;
1395 unsigned int mctrl;
1397 info->blocked_open++;
1398 state->count--;
1400 add_wait_queue(&info->open_wait, &wait);
1401 while (1) {
1402 set_current_state(TASK_INTERRUPTIBLE);
1405 * If we have been hung up, tell userspace/restart open.
1407 if (tty_hung_up_p(filp) || info->tty == NULL)
1408 break;
1411 * If the port has been closed, tell userspace/restart open.
1413 if (!(info->flags & UIF_INITIALIZED))
1414 break;
1417 * If non-blocking mode is set, or CLOCAL mode is set,
1418 * we don't want to wait for the modem status lines to
1419 * indicate that the port is ready.
1421 * Also, if the port is not enabled/configured, we want
1422 * to allow the open to succeed here. Note that we will
1423 * have set TTY_IO_ERROR for a non-existant port.
1425 if ((filp->f_flags & O_NONBLOCK) ||
1426 (info->tty->termios->c_cflag & CLOCAL) ||
1427 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1428 break;
1432 * Set DTR to allow modem to know we're waiting. Do
1433 * not set RTS here - we want to make sure we catch
1434 * the data from the modem.
1436 if (info->tty->termios->c_cflag & CBAUD)
1437 uart_set_mctrl(port, TIOCM_DTR);
1440 * and wait for the carrier to indicate that the
1441 * modem is ready for us.
1443 spin_lock_irq(&port->lock);
1444 port->ops->enable_ms(port);
1445 mctrl = port->ops->get_mctrl(port);
1446 spin_unlock_irq(&port->lock);
1447 if (mctrl & TIOCM_CAR)
1448 break;
1450 mutex_unlock(&state->mutex);
1451 schedule();
1452 mutex_lock(&state->mutex);
1454 if (signal_pending(current))
1455 break;
1457 set_current_state(TASK_RUNNING);
1458 remove_wait_queue(&info->open_wait, &wait);
1460 state->count++;
1461 info->blocked_open--;
1463 if (signal_pending(current))
1464 return -ERESTARTSYS;
1466 if (!info->tty || tty_hung_up_p(filp))
1467 return -EAGAIN;
1469 return 0;
1472 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1474 struct uart_state *state;
1476 mutex_lock(&port_mutex);
1477 state = drv->state + line;
1478 if (mutex_lock_interruptible(&state->mutex)) {
1479 state = ERR_PTR(-ERESTARTSYS);
1480 goto out;
1483 state->count++;
1484 if (!state->port) {
1485 state->count--;
1486 mutex_unlock(&state->mutex);
1487 state = ERR_PTR(-ENXIO);
1488 goto out;
1491 if (!state->info) {
1492 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1493 if (state->info) {
1494 memset(state->info, 0, sizeof(struct uart_info));
1495 init_waitqueue_head(&state->info->open_wait);
1496 init_waitqueue_head(&state->info->delta_msr_wait);
1499 * Link the info into the other structures.
1501 state->port->info = state->info;
1503 tasklet_init(&state->info->tlet, uart_tasklet_action,
1504 (unsigned long)state);
1505 } else {
1506 state->count--;
1507 mutex_unlock(&state->mutex);
1508 state = ERR_PTR(-ENOMEM);
1512 out:
1513 mutex_unlock(&port_mutex);
1514 return state;
1518 * In 2.4.5, calls to uart_open are serialised by the BKL in
1519 * linux/fs/devices.c:chrdev_open()
1520 * Note that if this fails, then uart_close() _will_ be called.
1522 * In time, we want to scrap the "opening nonpresent ports"
1523 * behaviour and implement an alternative way for setserial
1524 * to set base addresses/ports/types. This will allow us to
1525 * get rid of a certain amount of extra tests.
1527 static int uart_open(struct tty_struct *tty, struct file *filp)
1529 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1530 struct uart_state *state;
1531 int retval, line = tty->index;
1533 BUG_ON(!kernel_locked());
1534 DPRINTK("uart_open(%d) called\n", line);
1537 * tty->driver->num won't change, so we won't fail here with
1538 * tty->driver_data set to something non-NULL (and therefore
1539 * we won't get caught by uart_close()).
1541 retval = -ENODEV;
1542 if (line >= tty->driver->num)
1543 goto fail;
1546 * We take the semaphore inside uart_get to guarantee that we won't
1547 * be re-entered while allocating the info structure, or while we
1548 * request any IRQs that the driver may need. This also has the nice
1549 * side-effect that it delays the action of uart_hangup, so we can
1550 * guarantee that info->tty will always contain something reasonable.
1552 state = uart_get(drv, line);
1553 if (IS_ERR(state)) {
1554 retval = PTR_ERR(state);
1555 goto fail;
1559 * Once we set tty->driver_data here, we are guaranteed that
1560 * uart_close() will decrement the driver module use count.
1561 * Any failures from here onwards should not touch the count.
1563 tty->driver_data = state;
1564 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1565 tty->alt_speed = 0;
1566 state->info->tty = tty;
1569 * If the port is in the middle of closing, bail out now.
1571 if (tty_hung_up_p(filp)) {
1572 retval = -EAGAIN;
1573 state->count--;
1574 mutex_unlock(&state->mutex);
1575 goto fail;
1579 * Make sure the device is in D0 state.
1581 if (state->count == 1)
1582 uart_change_pm(state, 0);
1585 * Start up the serial port.
1587 retval = uart_startup(state, 0);
1590 * If we succeeded, wait until the port is ready.
1592 if (retval == 0)
1593 retval = uart_block_til_ready(filp, state);
1594 mutex_unlock(&state->mutex);
1597 * If this is the first open to succeed, adjust things to suit.
1599 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1600 state->info->flags |= UIF_NORMAL_ACTIVE;
1602 uart_update_termios(state);
1605 fail:
1606 return retval;
1609 static const char *uart_type(struct uart_port *port)
1611 const char *str = NULL;
1613 if (port->ops->type)
1614 str = port->ops->type(port);
1616 if (!str)
1617 str = "unknown";
1619 return str;
1622 #ifdef CONFIG_PROC_FS
1624 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1626 struct uart_state *state = drv->state + i;
1627 struct uart_port *port = state->port;
1628 char stat_buf[32];
1629 unsigned int status;
1630 int ret;
1632 if (!port)
1633 return 0;
1635 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1636 port->line, uart_type(port),
1637 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1638 port->iotype == UPIO_MEM ? port->mapbase :
1639 (unsigned long) port->iobase,
1640 port->irq);
1642 if (port->type == PORT_UNKNOWN) {
1643 strcat(buf, "\n");
1644 return ret + 1;
1647 if(capable(CAP_SYS_ADMIN))
1649 spin_lock_irq(&port->lock);
1650 status = port->ops->get_mctrl(port);
1651 spin_unlock_irq(&port->lock);
1653 ret += sprintf(buf + ret, " tx:%d rx:%d",
1654 port->icount.tx, port->icount.rx);
1655 if (port->icount.frame)
1656 ret += sprintf(buf + ret, " fe:%d",
1657 port->icount.frame);
1658 if (port->icount.parity)
1659 ret += sprintf(buf + ret, " pe:%d",
1660 port->icount.parity);
1661 if (port->icount.brk)
1662 ret += sprintf(buf + ret, " brk:%d",
1663 port->icount.brk);
1664 if (port->icount.overrun)
1665 ret += sprintf(buf + ret, " oe:%d",
1666 port->icount.overrun);
1668 #define INFOBIT(bit,str) \
1669 if (port->mctrl & (bit)) \
1670 strncat(stat_buf, (str), sizeof(stat_buf) - \
1671 strlen(stat_buf) - 2)
1672 #define STATBIT(bit,str) \
1673 if (status & (bit)) \
1674 strncat(stat_buf, (str), sizeof(stat_buf) - \
1675 strlen(stat_buf) - 2)
1677 stat_buf[0] = '\0';
1678 stat_buf[1] = '\0';
1679 INFOBIT(TIOCM_RTS, "|RTS");
1680 STATBIT(TIOCM_CTS, "|CTS");
1681 INFOBIT(TIOCM_DTR, "|DTR");
1682 STATBIT(TIOCM_DSR, "|DSR");
1683 STATBIT(TIOCM_CAR, "|CD");
1684 STATBIT(TIOCM_RNG, "|RI");
1685 if (stat_buf[0])
1686 stat_buf[0] = ' ';
1687 strcat(stat_buf, "\n");
1689 ret += sprintf(buf + ret, stat_buf);
1690 } else {
1691 strcat(buf, "\n");
1692 ret++;
1694 #undef STATBIT
1695 #undef INFOBIT
1696 return ret;
1699 static int uart_read_proc(char *page, char **start, off_t off,
1700 int count, int *eof, void *data)
1702 struct tty_driver *ttydrv = data;
1703 struct uart_driver *drv = ttydrv->driver_state;
1704 int i, len = 0, l;
1705 off_t begin = 0;
1707 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1708 "", "", "");
1709 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1710 l = uart_line_info(page + len, drv, i);
1711 len += l;
1712 if (len + begin > off + count)
1713 goto done;
1714 if (len + begin < off) {
1715 begin += len;
1716 len = 0;
1719 *eof = 1;
1720 done:
1721 if (off >= len + begin)
1722 return 0;
1723 *start = page + (off - begin);
1724 return (count < begin + len - off) ? count : (begin + len - off);
1726 #endif
1728 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1730 * Check whether an invalid uart number has been specified, and
1731 * if so, search for the first available port that does have
1732 * console support.
1734 struct uart_port * __init
1735 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1737 int idx = co->index;
1739 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1740 ports[idx].membase == NULL))
1741 for (idx = 0; idx < nr; idx++)
1742 if (ports[idx].iobase != 0 ||
1743 ports[idx].membase != NULL)
1744 break;
1746 co->index = idx;
1748 return ports + idx;
1752 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1753 * @options: pointer to option string
1754 * @baud: pointer to an 'int' variable for the baud rate.
1755 * @parity: pointer to an 'int' variable for the parity.
1756 * @bits: pointer to an 'int' variable for the number of data bits.
1757 * @flow: pointer to an 'int' variable for the flow control character.
1759 * uart_parse_options decodes a string containing the serial console
1760 * options. The format of the string is <baud><parity><bits><flow>,
1761 * eg: 115200n8r
1763 void __init
1764 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1766 char *s = options;
1768 *baud = simple_strtoul(s, NULL, 10);
1769 while (*s >= '0' && *s <= '9')
1770 s++;
1771 if (*s)
1772 *parity = *s++;
1773 if (*s)
1774 *bits = *s++ - '0';
1775 if (*s)
1776 *flow = *s;
1779 struct baud_rates {
1780 unsigned int rate;
1781 unsigned int cflag;
1784 static const struct baud_rates baud_rates[] = {
1785 { 921600, B921600 },
1786 { 460800, B460800 },
1787 { 230400, B230400 },
1788 { 115200, B115200 },
1789 { 57600, B57600 },
1790 { 38400, B38400 },
1791 { 19200, B19200 },
1792 { 9600, B9600 },
1793 { 4800, B4800 },
1794 { 2400, B2400 },
1795 { 1200, B1200 },
1796 { 0, B38400 }
1800 * uart_set_options - setup the serial console parameters
1801 * @port: pointer to the serial ports uart_port structure
1802 * @co: console pointer
1803 * @baud: baud rate
1804 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1805 * @bits: number of data bits
1806 * @flow: flow control character - 'r' (rts)
1808 int __init
1809 uart_set_options(struct uart_port *port, struct console *co,
1810 int baud, int parity, int bits, int flow)
1812 struct termios termios;
1813 int i;
1816 * Ensure that the serial console lock is initialised
1817 * early.
1819 spin_lock_init(&port->lock);
1821 memset(&termios, 0, sizeof(struct termios));
1823 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1826 * Construct a cflag setting.
1828 for (i = 0; baud_rates[i].rate; i++)
1829 if (baud_rates[i].rate <= baud)
1830 break;
1832 termios.c_cflag |= baud_rates[i].cflag;
1834 if (bits == 7)
1835 termios.c_cflag |= CS7;
1836 else
1837 termios.c_cflag |= CS8;
1839 switch (parity) {
1840 case 'o': case 'O':
1841 termios.c_cflag |= PARODD;
1842 /*fall through*/
1843 case 'e': case 'E':
1844 termios.c_cflag |= PARENB;
1845 break;
1848 if (flow == 'r')
1849 termios.c_cflag |= CRTSCTS;
1851 port->ops->set_termios(port, &termios, NULL);
1852 co->cflag = termios.c_cflag;
1854 return 0;
1856 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1858 static void uart_change_pm(struct uart_state *state, int pm_state)
1860 struct uart_port *port = state->port;
1861 if (port->ops->pm)
1862 port->ops->pm(port, pm_state, state->pm_state);
1863 state->pm_state = pm_state;
1866 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1868 struct uart_state *state = drv->state + port->line;
1870 mutex_lock(&state->mutex);
1872 if (state->info && state->info->flags & UIF_INITIALIZED) {
1873 struct uart_ops *ops = port->ops;
1875 spin_lock_irq(&port->lock);
1876 ops->stop_tx(port);
1877 ops->set_mctrl(port, 0);
1878 ops->stop_rx(port);
1879 spin_unlock_irq(&port->lock);
1882 * Wait for the transmitter to empty.
1884 while (!ops->tx_empty(port)) {
1885 msleep(10);
1888 ops->shutdown(port);
1892 * Disable the console device before suspending.
1894 if (uart_console(port))
1895 console_stop(port->cons);
1897 uart_change_pm(state, 3);
1899 mutex_unlock(&state->mutex);
1901 return 0;
1904 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1906 struct uart_state *state = drv->state + port->line;
1908 mutex_lock(&state->mutex);
1910 uart_change_pm(state, 0);
1913 * Re-enable the console device after suspending.
1915 if (uart_console(port)) {
1916 struct termios termios;
1919 * First try to use the console cflag setting.
1921 memset(&termios, 0, sizeof(struct termios));
1922 termios.c_cflag = port->cons->cflag;
1925 * If that's unset, use the tty termios setting.
1927 if (state->info && state->info->tty && termios.c_cflag == 0)
1928 termios = *state->info->tty->termios;
1930 port->ops->set_termios(port, &termios, NULL);
1931 console_start(port->cons);
1934 if (state->info && state->info->flags & UIF_INITIALIZED) {
1935 struct uart_ops *ops = port->ops;
1936 int ret;
1938 ops->set_mctrl(port, 0);
1939 ret = ops->startup(port);
1940 if (ret == 0) {
1941 uart_change_speed(state, NULL);
1942 spin_lock_irq(&port->lock);
1943 ops->set_mctrl(port, port->mctrl);
1944 ops->start_tx(port);
1945 spin_unlock_irq(&port->lock);
1946 } else {
1948 * Failed to resume - maybe hardware went away?
1949 * Clear the "initialized" flag so we won't try
1950 * to call the low level drivers shutdown method.
1952 state->info->flags &= ~UIF_INITIALIZED;
1953 uart_shutdown(state);
1957 mutex_unlock(&state->mutex);
1959 return 0;
1962 static inline void
1963 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1965 char address[64];
1967 switch (port->iotype) {
1968 case UPIO_PORT:
1969 snprintf(address, sizeof(address),
1970 "I/O 0x%x", port->iobase);
1971 break;
1972 case UPIO_HUB6:
1973 snprintf(address, sizeof(address),
1974 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1975 break;
1976 case UPIO_MEM:
1977 case UPIO_MEM32:
1978 case UPIO_AU:
1979 snprintf(address, sizeof(address),
1980 "MMIO 0x%lx", port->mapbase);
1981 break;
1982 default:
1983 strlcpy(address, "*unknown*", sizeof(address));
1984 break;
1987 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
1988 port->dev ? port->dev->bus_id : "",
1989 port->dev ? ": " : "",
1990 drv->dev_name, port->line, address, port->irq, uart_type(port));
1993 static void
1994 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1995 struct uart_port *port)
1997 unsigned int flags;
2000 * If there isn't a port here, don't do anything further.
2002 if (!port->iobase && !port->mapbase && !port->membase)
2003 return;
2006 * Now do the auto configuration stuff. Note that config_port
2007 * is expected to claim the resources and map the port for us.
2009 flags = UART_CONFIG_TYPE;
2010 if (port->flags & UPF_AUTO_IRQ)
2011 flags |= UART_CONFIG_IRQ;
2012 if (port->flags & UPF_BOOT_AUTOCONF) {
2013 port->type = PORT_UNKNOWN;
2014 port->ops->config_port(port, flags);
2017 if (port->type != PORT_UNKNOWN) {
2018 unsigned long flags;
2020 uart_report_port(drv, port);
2023 * Ensure that the modem control lines are de-activated.
2024 * We probably don't need a spinlock around this, but
2026 spin_lock_irqsave(&port->lock, flags);
2027 port->ops->set_mctrl(port, 0);
2028 spin_unlock_irqrestore(&port->lock, flags);
2031 * Power down all ports by default, except the
2032 * console if we have one.
2034 if (!uart_console(port))
2035 uart_change_pm(state, 3);
2040 * This reverses the effects of uart_configure_port, hanging up the
2041 * port before removal.
2043 static void
2044 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2046 struct uart_port *port = state->port;
2047 struct uart_info *info = state->info;
2049 if (info && info->tty)
2050 tty_vhangup(info->tty);
2052 mutex_lock(&state->mutex);
2054 state->info = NULL;
2057 * Free the port IO and memory resources, if any.
2059 if (port->type != PORT_UNKNOWN)
2060 port->ops->release_port(port);
2063 * Indicate that there isn't a port here anymore.
2065 port->type = PORT_UNKNOWN;
2068 * Kill the tasklet, and free resources.
2070 if (info) {
2071 tasklet_kill(&info->tlet);
2072 kfree(info);
2075 mutex_unlock(&state->mutex);
2078 static struct tty_operations uart_ops = {
2079 .open = uart_open,
2080 .close = uart_close,
2081 .write = uart_write,
2082 .put_char = uart_put_char,
2083 .flush_chars = uart_flush_chars,
2084 .write_room = uart_write_room,
2085 .chars_in_buffer= uart_chars_in_buffer,
2086 .flush_buffer = uart_flush_buffer,
2087 .ioctl = uart_ioctl,
2088 .throttle = uart_throttle,
2089 .unthrottle = uart_unthrottle,
2090 .send_xchar = uart_send_xchar,
2091 .set_termios = uart_set_termios,
2092 .stop = uart_stop,
2093 .start = uart_start,
2094 .hangup = uart_hangup,
2095 .break_ctl = uart_break_ctl,
2096 .wait_until_sent= uart_wait_until_sent,
2097 #ifdef CONFIG_PROC_FS
2098 .read_proc = uart_read_proc,
2099 #endif
2100 .tiocmget = uart_tiocmget,
2101 .tiocmset = uart_tiocmset,
2105 * uart_register_driver - register a driver with the uart core layer
2106 * @drv: low level driver structure
2108 * Register a uart driver with the core driver. We in turn register
2109 * with the tty layer, and initialise the core driver per-port state.
2111 * We have a proc file in /proc/tty/driver which is named after the
2112 * normal driver.
2114 * drv->port should be NULL, and the per-port structures should be
2115 * registered using uart_add_one_port after this call has succeeded.
2117 int uart_register_driver(struct uart_driver *drv)
2119 struct tty_driver *normal = NULL;
2120 int i, retval;
2122 BUG_ON(drv->state);
2125 * Maybe we should be using a slab cache for this, especially if
2126 * we have a large number of ports to handle.
2128 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2129 retval = -ENOMEM;
2130 if (!drv->state)
2131 goto out;
2133 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2135 normal = alloc_tty_driver(drv->nr);
2136 if (!normal)
2137 goto out;
2139 drv->tty_driver = normal;
2141 normal->owner = drv->owner;
2142 normal->driver_name = drv->driver_name;
2143 normal->devfs_name = drv->devfs_name;
2144 normal->name = drv->dev_name;
2145 normal->major = drv->major;
2146 normal->minor_start = drv->minor;
2147 normal->type = TTY_DRIVER_TYPE_SERIAL;
2148 normal->subtype = SERIAL_TYPE_NORMAL;
2149 normal->init_termios = tty_std_termios;
2150 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2151 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2152 normal->driver_state = drv;
2153 tty_set_operations(normal, &uart_ops);
2156 * Initialise the UART state(s).
2158 for (i = 0; i < drv->nr; i++) {
2159 struct uart_state *state = drv->state + i;
2161 state->close_delay = 500; /* .5 seconds */
2162 state->closing_wait = 30000; /* 30 seconds */
2164 mutex_init(&state->mutex);
2167 retval = tty_register_driver(normal);
2168 out:
2169 if (retval < 0) {
2170 put_tty_driver(normal);
2171 kfree(drv->state);
2173 return retval;
2177 * uart_unregister_driver - remove a driver from the uart core layer
2178 * @drv: low level driver structure
2180 * Remove all references to a driver from the core driver. The low
2181 * level driver must have removed all its ports via the
2182 * uart_remove_one_port() if it registered them with uart_add_one_port().
2183 * (ie, drv->port == NULL)
2185 void uart_unregister_driver(struct uart_driver *drv)
2187 struct tty_driver *p = drv->tty_driver;
2188 tty_unregister_driver(p);
2189 put_tty_driver(p);
2190 kfree(drv->state);
2191 drv->tty_driver = NULL;
2194 struct tty_driver *uart_console_device(struct console *co, int *index)
2196 struct uart_driver *p = co->data;
2197 *index = co->index;
2198 return p->tty_driver;
2202 * uart_add_one_port - attach a driver-defined port structure
2203 * @drv: pointer to the uart low level driver structure for this port
2204 * @port: uart port structure to use for this port.
2206 * This allows the driver to register its own uart_port structure
2207 * with the core driver. The main purpose is to allow the low
2208 * level uart drivers to expand uart_port, rather than having yet
2209 * more levels of structures.
2211 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2213 struct uart_state *state;
2214 int ret = 0;
2216 BUG_ON(in_interrupt());
2218 if (port->line >= drv->nr)
2219 return -EINVAL;
2221 state = drv->state + port->line;
2223 mutex_lock(&port_mutex);
2224 if (state->port) {
2225 ret = -EINVAL;
2226 goto out;
2229 state->port = port;
2231 port->cons = drv->cons;
2232 port->info = state->info;
2235 * If this port is a console, then the spinlock is already
2236 * initialised.
2238 if (!uart_console(port))
2239 spin_lock_init(&port->lock);
2241 uart_configure_port(drv, state, port);
2244 * Register the port whether it's detected or not. This allows
2245 * setserial to be used to alter this ports parameters.
2247 tty_register_device(drv->tty_driver, port->line, port->dev);
2250 * If this driver supports console, and it hasn't been
2251 * successfully registered yet, try to re-register it.
2252 * It may be that the port was not available.
2254 if (port->type != PORT_UNKNOWN &&
2255 port->cons && !(port->cons->flags & CON_ENABLED))
2256 register_console(port->cons);
2258 out:
2259 mutex_unlock(&port_mutex);
2261 return ret;
2265 * uart_remove_one_port - detach a driver defined port structure
2266 * @drv: pointer to the uart low level driver structure for this port
2267 * @port: uart port structure for this port
2269 * This unhooks (and hangs up) the specified port structure from the
2270 * core driver. No further calls will be made to the low-level code
2271 * for this port.
2273 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2275 struct uart_state *state = drv->state + port->line;
2277 BUG_ON(in_interrupt());
2279 if (state->port != port)
2280 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2281 state->port, port);
2283 mutex_lock(&port_mutex);
2286 * Remove the devices from devfs
2288 tty_unregister_device(drv->tty_driver, port->line);
2290 uart_unconfigure_port(drv, state);
2291 state->port = NULL;
2292 mutex_unlock(&port_mutex);
2294 return 0;
2298 * Are the two ports equivalent?
2300 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2302 if (port1->iotype != port2->iotype)
2303 return 0;
2305 switch (port1->iotype) {
2306 case UPIO_PORT:
2307 return (port1->iobase == port2->iobase);
2308 case UPIO_HUB6:
2309 return (port1->iobase == port2->iobase) &&
2310 (port1->hub6 == port2->hub6);
2311 case UPIO_MEM:
2312 return (port1->mapbase == port2->mapbase);
2314 return 0;
2316 EXPORT_SYMBOL(uart_match_port);
2318 EXPORT_SYMBOL(uart_write_wakeup);
2319 EXPORT_SYMBOL(uart_register_driver);
2320 EXPORT_SYMBOL(uart_unregister_driver);
2321 EXPORT_SYMBOL(uart_suspend_port);
2322 EXPORT_SYMBOL(uart_resume_port);
2323 EXPORT_SYMBOL(uart_add_one_port);
2324 EXPORT_SYMBOL(uart_remove_one_port);
2326 MODULE_DESCRIPTION("Serial driver core");
2327 MODULE_LICENSE("GPL");