Merge with 2.5.75.
[linux-2.6/linux-mips.git] / drivers / serial / core.c
blob17c43594f9c480f4f45b2b36df0290eaecd190bb
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 */
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
39 #undef DEBUG
40 #ifdef DEBUG
41 #define DPRINTK(x...) printk(x)
42 #else
43 #define DPRINTK(x...) do { } while (0)
44 #endif
47 * This is used to lock changes in serial line configuration.
49 static DECLARE_MUTEX(port_sem);
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
55 #ifdef CONFIG_SERIAL_CORE_CONSOLE
56 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
57 #else
58 #define uart_console(port) (0)
59 #endif
61 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
62 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
63 static void uart_change_pm(struct uart_state *state, int pm_state);
66 * This routine is used by the interrupt handler to schedule processing in
67 * the software interrupt portion of the driver.
69 void uart_write_wakeup(struct uart_port *port)
71 struct uart_info *info = port->info;
72 tasklet_schedule(&info->tlet);
75 static void uart_stop(struct tty_struct *tty)
77 struct uart_state *state = tty->driver_data;
78 struct uart_port *port = state->port;
79 unsigned long flags;
81 spin_lock_irqsave(&port->lock, flags);
82 port->ops->stop_tx(port, 1);
83 spin_unlock_irqrestore(&port->lock, flags);
86 static void __uart_start(struct tty_struct *tty)
88 struct uart_state *state = tty->driver_data;
89 struct uart_port *port = state->port;
91 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
92 !tty->stopped && !tty->hw_stopped)
93 port->ops->start_tx(port, 1);
96 static void uart_start(struct tty_struct *tty)
98 struct uart_state *state = tty->driver_data;
99 struct uart_port *port = state->port;
100 unsigned long flags;
102 spin_lock_irqsave(&port->lock, flags);
103 __uart_start(tty);
104 spin_unlock_irqrestore(&port->lock, flags);
107 static void uart_tasklet_action(unsigned long data)
109 struct uart_state *state = (struct uart_state *)data;
110 struct tty_struct *tty;
112 tty = state->info->tty;
113 if (tty) {
114 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
115 tty->ldisc.write_wakeup)
116 tty->ldisc.write_wakeup(tty);
117 wake_up_interruptible(&tty->write_wait);
121 static inline void
122 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
124 unsigned long flags;
125 unsigned int old;
127 spin_lock_irqsave(&port->lock, flags);
128 old = port->mctrl;
129 port->mctrl = (old & ~clear) | set;
130 if (old != port->mctrl)
131 port->ops->set_mctrl(port, port->mctrl);
132 spin_unlock_irqrestore(&port->lock, flags);
135 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
136 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
139 * Startup the port. This will be called once per open. All calls
140 * will be serialised by the per-port semaphore.
142 static int uart_startup(struct uart_state *state, int init_hw)
144 struct uart_info *info = state->info;
145 struct uart_port *port = state->port;
146 unsigned long page;
147 int retval = 0;
149 if (info->flags & UIF_INITIALIZED)
150 return 0;
153 * Set the TTY IO error marker - we will only clear this
154 * once we have successfully opened the port. Also set
155 * up the tty->alt_speed kludge
157 if (info->tty)
158 set_bit(TTY_IO_ERROR, &info->tty->flags);
160 if (port->type == PORT_UNKNOWN)
161 return 0;
164 * Initialise and allocate the transmit and temporary
165 * buffer.
167 if (!info->xmit.buf) {
168 page = get_zeroed_page(GFP_KERNEL);
169 if (!page)
170 return -ENOMEM;
172 info->xmit.buf = (unsigned char *) page;
173 info->tmpbuf = info->xmit.buf + UART_XMIT_SIZE;
174 init_MUTEX(&info->tmpbuf_sem);
175 uart_circ_clear(&info->xmit);
178 port->mctrl = 0;
180 retval = port->ops->startup(port);
181 if (retval == 0) {
182 if (init_hw) {
184 * Initialise the hardware port settings.
186 uart_change_speed(state, NULL);
189 * Setup the RTS and DTR signals once the
190 * port is open and ready to respond.
192 if (info->tty->termios->c_cflag & CBAUD)
193 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
196 info->flags |= UIF_INITIALIZED;
198 clear_bit(TTY_IO_ERROR, &info->tty->flags);
201 if (retval && capable(CAP_SYS_ADMIN))
202 retval = 0;
204 return retval;
208 * This routine will shutdown a serial port; interrupts are disabled, and
209 * DTR is dropped if the hangup on close termio flag is on. Calls to
210 * uart_shutdown are serialised by the per-port semaphore.
212 static void uart_shutdown(struct uart_state *state)
214 struct uart_info *info = state->info;
215 struct uart_port *port = state->port;
217 if (!(info->flags & UIF_INITIALIZED))
218 return;
221 * Turn off DTR and RTS early.
223 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
224 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
227 * clear delta_msr_wait queue to avoid mem leaks: we may free
228 * the irq here so the queue might never be woken up. Note
229 * that we won't end up waiting on delta_msr_wait again since
230 * any outstanding file descriptors should be pointing at
231 * hung_up_tty_fops now.
233 wake_up_interruptible(&info->delta_msr_wait);
236 * Free the IRQ and disable the port.
238 port->ops->shutdown(port);
241 * Ensure that the IRQ handler isn't running on another CPU.
243 synchronize_irq(port->irq);
246 * Free the transmit buffer page.
248 if (info->xmit.buf) {
249 free_page((unsigned long)info->xmit.buf);
250 info->xmit.buf = NULL;
251 info->tmpbuf = NULL;
255 * kill off our tasklet
257 tasklet_kill(&info->tlet);
258 if (info->tty)
259 set_bit(TTY_IO_ERROR, &info->tty->flags);
261 info->flags &= ~UIF_INITIALIZED;
265 * uart_update_timeout - update per-port FIFO timeout.
266 * @port: uart_port structure describing the port.
267 * @cflag: termios cflag value
268 * @quot: uart clock divisor quotient
270 * Set the port FIFO timeout value. The @cflag value should
271 * reflect the actual hardware settings.
273 void
274 uart_update_timeout(struct uart_port *port, unsigned int cflag,
275 unsigned int baud)
277 unsigned int bits;
279 /* byte size and parity */
280 switch (cflag & CSIZE) {
281 case CS5:
282 bits = 7;
283 break;
284 case CS6:
285 bits = 8;
286 break;
287 case CS7:
288 bits = 9;
289 break;
290 default:
291 bits = 10;
292 break; // CS8
295 if (cflag & CSTOPB)
296 bits++;
297 if (cflag & PARENB)
298 bits++;
301 * The total number of bits to be transmitted in the fifo.
303 bits = bits * port->fifosize;
306 * Figure the timeout to send the above number of bits.
307 * Add .02 seconds of slop
309 port->timeout = (HZ * bits) / baud + HZ/50;
312 EXPORT_SYMBOL(uart_update_timeout);
315 * uart_get_baud_rate - return baud rate for a particular port
316 * @port: uart_port structure describing the port in question.
317 * @termios: desired termios settings.
318 * @old: old termios (or NULL)
319 * @min: minimum acceptable baud rate
320 * @max: maximum acceptable baud rate
322 * Decode the termios structure into a numeric baud rate,
323 * taking account of the magic 38400 baud rate (with spd_*
324 * flags), and mapping the %B0 rate to 9600 baud.
326 * If the new baud rate is invalid, try the old termios setting.
327 * If it's still invalid, we try 9600 baud.
329 * Update the @termios structure to reflect the baud rate
330 * we're actually going to be using.
332 unsigned int
333 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
334 struct termios *old, unsigned int min, unsigned int max)
336 unsigned int try, baud, altbaud = 38400;
337 unsigned int flags = port->flags & UPF_SPD_MASK;
339 if (flags == UPF_SPD_HI)
340 altbaud = 57600;
341 if (flags == UPF_SPD_VHI)
342 altbaud = 115200;
343 if (flags == UPF_SPD_SHI)
344 altbaud = 230400;
345 if (flags == UPF_SPD_WARP)
346 altbaud = 460800;
348 for (try = 0; try < 2; try++) {
349 baud = tty_termios_baud_rate(termios);
352 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
353 * Die! Die! Die!
355 if (baud == 38400)
356 baud = altbaud;
359 * Special case: B0 rate.
361 if (baud == 0)
362 baud = 9600;
364 if (baud >= min && baud <= max)
365 return baud;
368 * Oops, the quotient was zero. Try again with
369 * the old baud rate if possible.
371 termios->c_cflag &= ~CBAUD;
372 if (old) {
373 termios->c_cflag |= old->c_cflag & CBAUD;
374 old = NULL;
375 continue;
379 * As a last resort, if the quotient is zero,
380 * default to 9600 bps
382 termios->c_cflag |= B9600;
385 return 0;
388 EXPORT_SYMBOL(uart_get_baud_rate);
391 * uart_get_divisor - return uart clock divisor
392 * @port: uart_port structure describing the port.
393 * @baud: desired baud rate
395 * Calculate the uart clock divisor for the port.
397 unsigned int
398 uart_get_divisor(struct uart_port *port, unsigned int baud)
400 unsigned int quot;
403 * Old custom speed handling.
405 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
406 quot = port->custom_divisor;
407 else
408 quot = port->uartclk / (16 * baud);
410 return quot;
413 EXPORT_SYMBOL(uart_get_divisor);
415 static void
416 uart_change_speed(struct uart_state *state, struct termios *old_termios)
418 struct tty_struct *tty = state->info->tty;
419 struct uart_port *port = state->port;
420 struct termios *termios;
423 * If we have no tty, termios, or the port does not exist,
424 * then we can't set the parameters for this port.
426 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
427 return;
429 termios = tty->termios;
432 * Set flags based on termios cflag
434 if (termios->c_cflag & CRTSCTS)
435 state->info->flags |= UIF_CTS_FLOW;
436 else
437 state->info->flags &= ~UIF_CTS_FLOW;
439 if (termios->c_cflag & CLOCAL)
440 state->info->flags &= ~UIF_CHECK_CD;
441 else
442 state->info->flags |= UIF_CHECK_CD;
444 port->ops->set_termios(port, termios, old_termios);
447 static inline void
448 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
450 unsigned long flags;
452 if (!circ->buf)
453 return;
455 spin_lock_irqsave(&port->lock, flags);
456 if (uart_circ_chars_free(circ) != 0) {
457 circ->buf[circ->head] = c;
458 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
460 spin_unlock_irqrestore(&port->lock, flags);
463 static inline int
464 __uart_user_write(struct uart_port *port, struct circ_buf *circ,
465 const unsigned char *buf, int count)
467 unsigned long flags;
468 int c, ret = 0;
470 if (down_interruptible(&port->info->tmpbuf_sem))
471 return -EINTR;
473 while (1) {
474 int c1;
475 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
476 if (count < c)
477 c = count;
478 if (c <= 0)
479 break;
481 c -= copy_from_user(port->info->tmpbuf, buf, c);
482 if (!c) {
483 if (!ret)
484 ret = -EFAULT;
485 break;
487 spin_lock_irqsave(&port->lock, flags);
488 c1 = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
489 if (c1 < c)
490 c = c1;
491 memcpy(circ->buf + circ->head, port->info->tmpbuf, c);
492 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
493 spin_unlock_irqrestore(&port->lock, flags);
494 buf += c;
495 count -= c;
496 ret += c;
498 up(&port->info->tmpbuf_sem);
500 return ret;
503 static inline int
504 __uart_kern_write(struct uart_port *port, struct circ_buf *circ,
505 const unsigned char *buf, int count)
507 unsigned long flags;
508 int c, ret = 0;
510 spin_lock_irqsave(&port->lock, flags);
511 while (1) {
512 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
513 if (count < c)
514 c = count;
515 if (c <= 0)
516 break;
517 memcpy(circ->buf + circ->head, buf, c);
518 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
519 buf += c;
520 count -= c;
521 ret += c;
523 spin_unlock_irqrestore(&port->lock, flags);
525 return ret;
528 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
530 struct uart_state *state = tty->driver_data;
532 if (tty)
533 __uart_put_char(state->port, &state->info->xmit, ch);
536 static void uart_flush_chars(struct tty_struct *tty)
538 uart_start(tty);
541 static int
542 uart_write(struct tty_struct *tty, int from_user, const unsigned char * buf,
543 int count)
545 struct uart_state *state = tty->driver_data;
546 int ret;
548 if (!tty || !state->info->xmit.buf)
549 return 0;
551 if (from_user)
552 ret = __uart_user_write(state->port, &state->info->xmit, buf, count);
553 else
554 ret = __uart_kern_write(state->port, &state->info->xmit, buf, count);
556 uart_start(tty);
557 return ret;
560 static int uart_write_room(struct tty_struct *tty)
562 struct uart_state *state = tty->driver_data;
564 return uart_circ_chars_free(&state->info->xmit);
567 static int uart_chars_in_buffer(struct tty_struct *tty)
569 struct uart_state *state = tty->driver_data;
571 return uart_circ_chars_pending(&state->info->xmit);
574 static void uart_flush_buffer(struct tty_struct *tty)
576 struct uart_state *state = tty->driver_data;
577 struct uart_port *port = state->port;
578 unsigned long flags;
580 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
582 spin_lock_irqsave(&port->lock, flags);
583 uart_circ_clear(&state->info->xmit);
584 spin_unlock_irqrestore(&port->lock, flags);
585 wake_up_interruptible(&tty->write_wait);
586 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
587 tty->ldisc.write_wakeup)
588 (tty->ldisc.write_wakeup)(tty);
592 * This function is used to send a high-priority XON/XOFF character to
593 * the device
595 static void uart_send_xchar(struct tty_struct *tty, char ch)
597 struct uart_state *state = tty->driver_data;
598 struct uart_port *port = state->port;
599 unsigned long flags;
601 if (port->ops->send_xchar)
602 port->ops->send_xchar(port, ch);
603 else {
604 port->x_char = ch;
605 if (ch) {
606 spin_lock_irqsave(&port->lock, flags);
607 port->ops->start_tx(port, 0);
608 spin_unlock_irqrestore(&port->lock, flags);
613 static void uart_throttle(struct tty_struct *tty)
615 struct uart_state *state = tty->driver_data;
617 if (I_IXOFF(tty))
618 uart_send_xchar(tty, STOP_CHAR(tty));
620 if (tty->termios->c_cflag & CRTSCTS)
621 uart_clear_mctrl(state->port, TIOCM_RTS);
624 static void uart_unthrottle(struct tty_struct *tty)
626 struct uart_state *state = tty->driver_data;
627 struct uart_port *port = state->port;
629 if (I_IXOFF(tty)) {
630 if (port->x_char)
631 port->x_char = 0;
632 else
633 uart_send_xchar(tty, START_CHAR(tty));
636 if (tty->termios->c_cflag & CRTSCTS)
637 uart_set_mctrl(port, TIOCM_RTS);
640 static int uart_get_info(struct uart_state *state, struct serial_struct *retinfo)
642 struct uart_port *port = state->port;
643 struct serial_struct tmp;
645 memset(&tmp, 0, sizeof(tmp));
646 tmp.type = port->type;
647 tmp.line = port->line;
648 tmp.port = port->iobase;
649 if (HIGH_BITS_OFFSET)
650 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
651 tmp.irq = port->irq;
652 tmp.flags = port->flags;
653 tmp.xmit_fifo_size = port->fifosize;
654 tmp.baud_base = port->uartclk / 16;
655 tmp.close_delay = state->close_delay;
656 tmp.closing_wait = state->closing_wait;
657 tmp.custom_divisor = port->custom_divisor;
658 tmp.hub6 = port->hub6;
659 tmp.io_type = port->iotype;
660 tmp.iomem_reg_shift = port->regshift;
661 tmp.iomem_base = (void *)port->mapbase;
663 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
664 return -EFAULT;
665 return 0;
668 static int
669 uart_set_info(struct uart_state *state, struct serial_struct *newinfo)
671 struct serial_struct new_serial;
672 struct uart_port *port = state->port;
673 unsigned long new_port;
674 unsigned int change_irq, change_port, old_flags;
675 unsigned int old_custom_divisor;
676 int retval = 0;
678 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
679 return -EFAULT;
681 new_port = new_serial.port;
682 if (HIGH_BITS_OFFSET)
683 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
685 new_serial.irq = irq_canonicalize(new_serial.irq);
688 * This semaphore protects state->count. It is also
689 * very useful to prevent opens. Also, take the
690 * port configuration semaphore to make sure that a
691 * module insertion/removal doesn't change anything
692 * under us.
694 down(&state->sem);
696 change_irq = new_serial.irq != port->irq;
699 * Since changing the 'type' of the port changes its resource
700 * allocations, we should treat type changes the same as
701 * IO port changes.
703 change_port = new_port != port->iobase ||
704 (unsigned long)new_serial.iomem_base != port->mapbase ||
705 new_serial.hub6 != port->hub6 ||
706 new_serial.io_type != port->iotype ||
707 new_serial.iomem_reg_shift != port->regshift ||
708 new_serial.type != port->type;
710 old_flags = port->flags;
711 old_custom_divisor = port->custom_divisor;
713 if (!capable(CAP_SYS_ADMIN)) {
714 retval = -EPERM;
715 if (change_irq || change_port ||
716 (new_serial.baud_base != port->uartclk / 16) ||
717 (new_serial.close_delay != state->close_delay) ||
718 (new_serial.closing_wait != state->closing_wait) ||
719 (new_serial.xmit_fifo_size != port->fifosize) ||
720 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
721 goto exit;
722 port->flags = ((port->flags & ~UPF_USR_MASK) |
723 (new_serial.flags & UPF_USR_MASK));
724 port->custom_divisor = new_serial.custom_divisor;
725 goto check_and_exit;
729 * Ask the low level driver to verify the settings.
731 if (port->ops->verify_port)
732 retval = port->ops->verify_port(port, &new_serial);
734 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
735 (new_serial.baud_base < 9600))
736 retval = -EINVAL;
738 if (retval)
739 goto exit;
741 if (change_port || change_irq) {
742 retval = -EBUSY;
745 * Make sure that we are the sole user of this port.
747 if (uart_users(state) > 1)
748 goto exit;
751 * We need to shutdown the serial port at the old
752 * port/type/irq combination.
754 uart_shutdown(state);
757 if (change_port) {
758 unsigned long old_iobase, old_mapbase;
759 unsigned int old_type, old_iotype, old_hub6, old_shift;
761 old_iobase = port->iobase;
762 old_mapbase = port->mapbase;
763 old_type = port->type;
764 old_hub6 = port->hub6;
765 old_iotype = port->iotype;
766 old_shift = port->regshift;
769 * Free and release old regions
771 if (old_type != PORT_UNKNOWN)
772 port->ops->release_port(port);
774 port->iobase = new_port;
775 port->type = new_serial.type;
776 port->hub6 = new_serial.hub6;
777 port->iotype = new_serial.io_type;
778 port->regshift = new_serial.iomem_reg_shift;
779 port->mapbase = (unsigned long)new_serial.iomem_base;
782 * Claim and map the new regions
784 if (port->type != PORT_UNKNOWN) {
785 retval = port->ops->request_port(port);
786 } else {
787 /* Always success - Jean II */
788 retval = 0;
792 * If we fail to request resources for the
793 * new port, try to restore the old settings.
795 if (retval && old_type != PORT_UNKNOWN) {
796 port->iobase = old_iobase;
797 port->type = old_type;
798 port->hub6 = old_hub6;
799 port->iotype = old_iotype;
800 port->regshift = old_shift;
801 port->mapbase = old_mapbase;
802 retval = port->ops->request_port(port);
804 * If we failed to restore the old settings,
805 * we fail like this.
807 if (retval)
808 port->type = PORT_UNKNOWN;
811 * We failed anyway.
813 retval = -EBUSY;
817 port->irq = new_serial.irq;
818 port->uartclk = new_serial.baud_base * 16;
819 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
820 (new_serial.flags & UPF_CHANGE_MASK);
821 port->custom_divisor = new_serial.custom_divisor;
822 state->close_delay = new_serial.close_delay * HZ / 100;
823 state->closing_wait = new_serial.closing_wait * HZ / 100;
824 port->fifosize = new_serial.xmit_fifo_size;
825 if (state->info->tty)
826 state->info->tty->low_latency =
827 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
829 check_and_exit:
830 retval = 0;
831 if (port->type == PORT_UNKNOWN)
832 goto exit;
833 if (state->info->flags & UIF_INITIALIZED) {
834 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
835 old_custom_divisor != port->custom_divisor) {
836 /* If they're setting up a custom divisor or speed,
837 * instead of clearing it, then bitch about it. No
838 * need to rate-limit; it's CAP_SYS_ADMIN only. */
839 if (port->flags & UPF_SPD_MASK) {
840 printk(KERN_NOTICE "%s sets custom speed on %s%d. This is deprecated.\n",
841 current->comm, state->info->tty->driver->name,
842 state->port->line);
844 uart_change_speed(state, NULL);
846 } else
847 retval = uart_startup(state, 1);
848 exit:
849 up(&state->sem);
850 return retval;
855 * uart_get_lsr_info - get line status register info.
856 * Note: uart_ioctl protects us against hangups.
858 static int uart_get_lsr_info(struct uart_state *state, unsigned int *value)
860 struct uart_port *port = state->port;
861 unsigned int result;
863 result = port->ops->tx_empty(port);
866 * If we're about to load something into the transmit
867 * register, we'll pretend the transmitter isn't empty to
868 * avoid a race condition (depending on when the transmit
869 * interrupt happens).
871 if (port->x_char ||
872 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
873 !state->info->tty->stopped && !state->info->tty->hw_stopped))
874 result &= ~TIOCSER_TEMT;
876 return put_user(result, value);
879 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
881 struct uart_state *state = tty->driver_data;
882 struct uart_port *port = state->port;
883 int result = -EIO;
885 down(&state->sem);
886 if ((!file || !tty_hung_up_p(file)) &&
887 !(tty->flags & (1 << TTY_IO_ERROR))) {
888 result = port->mctrl;
889 result |= port->ops->get_mctrl(port);
891 up(&state->sem);
893 return result;
896 static int
897 uart_tiocmset(struct tty_struct *tty, struct file *file,
898 unsigned int set, unsigned int clear)
900 struct uart_state *state = tty->driver_data;
901 struct uart_port *port = state->port;
902 int ret = -EIO;
904 down(&state->sem);
905 if ((!file || !tty_hung_up_p(file)) &&
906 !(tty->flags & (1 << TTY_IO_ERROR))) {
907 uart_update_mctrl(port, set, clear);
908 ret = 0;
910 up(&state->sem);
911 return ret;
914 static void uart_break_ctl(struct tty_struct *tty, int break_state)
916 struct uart_state *state = tty->driver_data;
917 struct uart_port *port = state->port;
919 BUG_ON(!kernel_locked());
921 down(&state->sem);
923 if (port->type != PORT_UNKNOWN)
924 port->ops->break_ctl(port, break_state);
926 up(&state->sem);
929 static int uart_do_autoconfig(struct uart_state *state)
931 struct uart_port *port = state->port;
932 int flags, ret;
934 if (!capable(CAP_SYS_ADMIN))
935 return -EPERM;
938 * Take the per-port semaphore. This prevents count from
939 * changing, and hence any extra opens of the port while
940 * we're auto-configuring.
942 if (down_interruptible(&state->sem))
943 return -ERESTARTSYS;
945 ret = -EBUSY;
946 if (uart_users(state) == 1) {
947 uart_shutdown(state);
950 * If we already have a port type configured,
951 * we must release its resources.
953 if (port->type != PORT_UNKNOWN)
954 port->ops->release_port(port);
956 flags = UART_CONFIG_TYPE;
957 if (port->flags & UPF_AUTO_IRQ)
958 flags |= UART_CONFIG_IRQ;
961 * This will claim the ports resources if
962 * a port is found.
964 port->ops->config_port(port, flags);
966 ret = uart_startup(state, 1);
968 up(&state->sem);
969 return ret;
973 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
974 * - mask passed in arg for lines of interest
975 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
976 * Caller should use TIOCGICOUNT to see which one it was
978 static int
979 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
981 struct uart_port *port = state->port;
982 DECLARE_WAITQUEUE(wait, current);
983 struct uart_icount cprev, cnow;
984 int ret;
987 * note the counters on entry
989 spin_lock_irq(&port->lock);
990 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
993 * Force modem status interrupts on
995 port->ops->enable_ms(port);
996 spin_unlock_irq(&port->lock);
998 add_wait_queue(&state->info->delta_msr_wait, &wait);
999 for (;;) {
1000 spin_lock_irq(&port->lock);
1001 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1002 spin_unlock_irq(&port->lock);
1004 set_current_state(TASK_INTERRUPTIBLE);
1006 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1007 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1008 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1009 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1010 ret = 0;
1011 break;
1014 schedule();
1016 /* see if a signal did it */
1017 if (signal_pending(current)) {
1018 ret = -ERESTARTSYS;
1019 break;
1022 cprev = cnow;
1025 current->state = TASK_RUNNING;
1026 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1028 return ret;
1032 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1033 * Return: write counters to the user passed counter struct
1034 * NB: both 1->0 and 0->1 transitions are counted except for
1035 * RI where only 0->1 is counted.
1037 static int
1038 uart_get_count(struct uart_state *state, struct serial_icounter_struct *icnt)
1040 struct serial_icounter_struct icount;
1041 struct uart_icount cnow;
1042 struct uart_port *port = state->port;
1044 spin_lock_irq(&port->lock);
1045 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1046 spin_unlock_irq(&port->lock);
1048 icount.cts = cnow.cts;
1049 icount.dsr = cnow.dsr;
1050 icount.rng = cnow.rng;
1051 icount.dcd = cnow.dcd;
1052 icount.rx = cnow.rx;
1053 icount.tx = cnow.tx;
1054 icount.frame = cnow.frame;
1055 icount.overrun = cnow.overrun;
1056 icount.parity = cnow.parity;
1057 icount.brk = cnow.brk;
1058 icount.buf_overrun = cnow.buf_overrun;
1060 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1064 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1066 static int
1067 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1068 unsigned long arg)
1070 struct uart_state *state = tty->driver_data;
1071 int ret = -ENOIOCTLCMD;
1073 BUG_ON(!kernel_locked());
1076 * These ioctls don't rely on the hardware to be present.
1078 switch (cmd) {
1079 case TIOCGSERIAL:
1080 ret = uart_get_info(state, (struct serial_struct *)arg);
1081 break;
1083 case TIOCSSERIAL:
1084 ret = uart_set_info(state, (struct serial_struct *)arg);
1085 break;
1087 case TIOCSERCONFIG:
1088 ret = uart_do_autoconfig(state);
1089 break;
1091 case TIOCSERGWILD: /* obsolete */
1092 case TIOCSERSWILD: /* obsolete */
1093 ret = 0;
1094 break;
1097 if (ret != -ENOIOCTLCMD)
1098 goto out;
1100 if (tty->flags & (1 << TTY_IO_ERROR)) {
1101 ret = -EIO;
1102 goto out;
1106 * The following should only be used when hardware is present.
1108 switch (cmd) {
1109 case TIOCMIWAIT:
1110 ret = uart_wait_modem_status(state, arg);
1111 break;
1113 case TIOCGICOUNT:
1114 ret = uart_get_count(state, (struct serial_icounter_struct *)arg);
1115 break;
1118 if (ret != -ENOIOCTLCMD)
1119 goto out;
1121 down(&state->sem);
1123 if (tty_hung_up_p(filp)) {
1124 ret = -EIO;
1125 goto out_up;
1129 * All these rely on hardware being present and need to be
1130 * protected against the tty being hung up.
1132 switch (cmd) {
1133 case TIOCSERGETLSR: /* Get line status register */
1134 ret = uart_get_lsr_info(state, (unsigned int *)arg);
1135 break;
1137 default: {
1138 struct uart_port *port = state->port;
1139 if (port->ops->ioctl)
1140 ret = port->ops->ioctl(port, cmd, arg);
1141 break;
1144 out_up:
1145 up(&state->sem);
1146 out:
1147 return ret;
1150 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1152 struct uart_state *state = tty->driver_data;
1153 unsigned long flags;
1154 unsigned int cflag = tty->termios->c_cflag;
1156 BUG_ON(!kernel_locked());
1159 * These are the bits that are used to setup various
1160 * flags in the low level driver.
1162 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1164 if ((cflag ^ old_termios->c_cflag) == 0 &&
1165 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1166 return;
1168 uart_change_speed(state, old_termios);
1170 /* Handle transition to B0 status */
1171 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1172 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1174 /* Handle transition away from B0 status */
1175 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1176 unsigned int mask = TIOCM_DTR;
1177 if (!(cflag & CRTSCTS) ||
1178 !test_bit(TTY_THROTTLED, &tty->flags))
1179 mask |= TIOCM_RTS;
1180 uart_set_mctrl(state->port, mask);
1183 /* Handle turning off CRTSCTS */
1184 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1185 spin_lock_irqsave(&state->port->lock, flags);
1186 tty->hw_stopped = 0;
1187 __uart_start(tty);
1188 spin_unlock_irqrestore(&state->port->lock, flags);
1191 #if 0
1193 * No need to wake up processes in open wait, since they
1194 * sample the CLOCAL flag once, and don't recheck it.
1195 * XXX It's not clear whether the current behavior is correct
1196 * or not. Hence, this may change.....
1198 if (!(old_termios->c_cflag & CLOCAL) &&
1199 (tty->termios->c_cflag & CLOCAL))
1200 wake_up_interruptible(&state->info->open_wait);
1201 #endif
1205 * In 2.4.5, calls to this will be serialized via the BKL in
1206 * linux/drivers/char/tty_io.c:tty_release()
1207 * linux/drivers/char/tty_io.c:do_tty_handup()
1209 static void uart_close(struct tty_struct *tty, struct file *filp)
1211 struct uart_state *state = tty->driver_data;
1212 struct uart_port *port = state->port;
1214 BUG_ON(!kernel_locked());
1215 DPRINTK("uart_close(%d) called\n", port->line);
1217 down(&state->sem);
1219 if (tty_hung_up_p(filp))
1220 goto done;
1222 if ((tty->count == 1) && (state->count != 1)) {
1224 * Uh, oh. tty->count is 1, which means that the tty
1225 * structure will be freed. state->count should always
1226 * be one in these conditions. If it's greater than
1227 * one, we've got real problems, since it means the
1228 * serial port won't be shutdown.
1230 printk("uart_close: bad serial port count; tty->count is 1, "
1231 "state->count is %d\n", state->count);
1232 state->count = 1;
1234 if (--state->count < 0) {
1235 printk("rs_close: bad serial port count for %s: %d\n",
1236 tty->name, state->count);
1237 state->count = 0;
1239 if (state->count)
1240 goto done;
1243 * Now we wait for the transmit buffer to clear; and we notify
1244 * the line discipline to only process XON/XOFF characters by
1245 * setting tty->closing.
1247 tty->closing = 1;
1249 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1250 tty_wait_until_sent(tty, state->closing_wait);
1253 * At this point, we stop accepting input. To do this, we
1254 * disable the receive line status interrupts.
1256 if (state->info->flags & UIF_INITIALIZED) {
1257 unsigned long flags;
1258 spin_lock_irqsave(&port->lock, flags);
1259 port->ops->stop_rx(port);
1260 spin_unlock_irqrestore(&port->lock, flags);
1262 * Before we drop DTR, make sure the UART transmitter
1263 * has completely drained; this is especially
1264 * important if there is a transmit FIFO!
1266 uart_wait_until_sent(tty, port->timeout);
1269 uart_shutdown(state);
1270 uart_flush_buffer(tty);
1271 if (tty->ldisc.flush_buffer)
1272 tty->ldisc.flush_buffer(tty);
1273 tty->closing = 0;
1274 state->info->tty = NULL;
1276 if (state->info->blocked_open) {
1277 if (state->close_delay) {
1278 set_current_state(TASK_INTERRUPTIBLE);
1279 schedule_timeout(state->close_delay);
1280 set_current_state(TASK_RUNNING);
1282 } else if (!uart_console(port)) {
1283 uart_change_pm(state, 3);
1287 * Wake up anyone trying to open this port.
1289 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1290 wake_up_interruptible(&state->info->open_wait);
1292 done:
1293 up(&state->sem);
1296 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1298 struct uart_state *state = tty->driver_data;
1299 struct uart_port *port = state->port;
1300 unsigned long char_time, expire;
1302 BUG_ON(!kernel_locked());
1304 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1305 return;
1308 * Set the check interval to be 1/5 of the estimated time to
1309 * send a single character, and make it at least 1. The check
1310 * interval should also be less than the timeout.
1312 * Note: we have to use pretty tight timings here to satisfy
1313 * the NIST-PCTS.
1315 char_time = (port->timeout - HZ/50) / port->fifosize;
1316 char_time = char_time / 5;
1317 if (char_time == 0)
1318 char_time = 1;
1319 if (timeout && timeout < char_time)
1320 char_time = timeout;
1323 * If the transmitter hasn't cleared in twice the approximate
1324 * amount of time to send the entire FIFO, it probably won't
1325 * ever clear. This assumes the UART isn't doing flow
1326 * control, which is currently the case. Hence, if it ever
1327 * takes longer than port->timeout, this is probably due to a
1328 * UART bug of some kind. So, we clamp the timeout parameter at
1329 * 2*port->timeout.
1331 if (timeout == 0 || timeout > 2 * port->timeout)
1332 timeout = 2 * port->timeout;
1334 expire = jiffies + timeout;
1336 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1337 port->line, jiffies, expire);
1340 * Check whether the transmitter is empty every 'char_time'.
1341 * 'timeout' / 'expire' give us the maximum amount of time
1342 * we wait.
1344 while (!port->ops->tx_empty(port)) {
1345 set_current_state(TASK_INTERRUPTIBLE);
1346 schedule_timeout(char_time);
1347 if (signal_pending(current))
1348 break;
1349 if (time_after(jiffies, expire))
1350 break;
1352 set_current_state(TASK_RUNNING); /* might not be needed */
1356 * This is called with the BKL held in
1357 * linux/drivers/char/tty_io.c:do_tty_hangup()
1358 * We're called from the eventd thread, so we can sleep for
1359 * a _short_ time only.
1361 static void uart_hangup(struct tty_struct *tty)
1363 struct uart_state *state = tty->driver_data;
1365 BUG_ON(!kernel_locked());
1366 DPRINTK("uart_hangup(%d)\n", state->port->line);
1368 down(&state->sem);
1369 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1370 uart_flush_buffer(tty);
1371 uart_shutdown(state);
1372 state->count = 0;
1373 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1374 state->info->tty = NULL;
1375 wake_up_interruptible(&state->info->open_wait);
1376 wake_up_interruptible(&state->info->delta_msr_wait);
1378 up(&state->sem);
1382 * Copy across the serial console cflag setting into the termios settings
1383 * for the initial open of the port. This allows continuity between the
1384 * kernel settings, and the settings init adopts when it opens the port
1385 * for the first time.
1387 static void uart_update_termios(struct uart_state *state)
1389 struct tty_struct *tty = state->info->tty;
1390 struct uart_port *port = state->port;
1392 if (uart_console(port) && port->cons->cflag) {
1393 tty->termios->c_cflag = port->cons->cflag;
1394 port->cons->cflag = 0;
1398 * If the device failed to grab its irq resources,
1399 * or some other error occurred, don't try to talk
1400 * to the port hardware.
1402 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1404 * Make termios settings take effect.
1406 uart_change_speed(state, NULL);
1409 * And finally enable the RTS and DTR signals.
1411 if (tty->termios->c_cflag & CBAUD)
1412 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1417 * Block the open until the port is ready. We must be called with
1418 * the per-port semaphore held.
1420 static int
1421 uart_block_til_ready(struct file *filp, struct uart_state *state)
1423 DECLARE_WAITQUEUE(wait, current);
1424 struct uart_info *info = state->info;
1425 struct uart_port *port = state->port;
1427 info->blocked_open++;
1428 state->count--;
1430 add_wait_queue(&info->open_wait, &wait);
1431 while (1) {
1432 set_current_state(TASK_INTERRUPTIBLE);
1435 * If we have been hung up, tell userspace/restart open.
1437 if (tty_hung_up_p(filp) || info->tty == NULL)
1438 break;
1441 * If the port has been closed, tell userspace/restart open.
1443 if (!(info->flags & UIF_INITIALIZED))
1444 break;
1447 * If non-blocking mode is set, or CLOCAL mode is set,
1448 * we don't want to wait for the modem status lines to
1449 * indicate that the port is ready.
1451 * Also, if the port is not enabled/configured, we want
1452 * to allow the open to succeed here. Note that we will
1453 * have set TTY_IO_ERROR for a non-existant port.
1455 if ((filp->f_flags & O_NONBLOCK) ||
1456 (info->tty->termios->c_cflag & CLOCAL) ||
1457 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1458 break;
1462 * Set DTR to allow modem to know we're waiting. Do
1463 * not set RTS here - we want to make sure we catch
1464 * the data from the modem.
1466 if (info->tty->termios->c_cflag & CBAUD)
1467 uart_set_mctrl(port, TIOCM_DTR);
1470 * and wait for the carrier to indicate that the
1471 * modem is ready for us.
1473 if (port->ops->get_mctrl(port) & TIOCM_CAR)
1474 break;
1476 up(&state->sem);
1477 schedule();
1478 down(&state->sem);
1480 if (signal_pending(current))
1481 break;
1483 set_current_state(TASK_RUNNING);
1484 remove_wait_queue(&info->open_wait, &wait);
1486 state->count++;
1487 info->blocked_open--;
1489 if (signal_pending(current))
1490 return -ERESTARTSYS;
1492 if (!info->tty || tty_hung_up_p(filp))
1493 return -EAGAIN;
1495 return 0;
1498 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1500 struct uart_state *state;
1502 down(&port_sem);
1503 state = drv->state + line;
1504 if (down_interruptible(&state->sem)) {
1505 state = ERR_PTR(-ERESTARTSYS);
1506 goto out;
1509 state->count++;
1510 if (!state->port) {
1511 state->count--;
1512 up(&state->sem);
1513 state = ERR_PTR(-ENXIO);
1514 goto out;
1517 if (!state->info) {
1518 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1519 if (state->info) {
1520 memset(state->info, 0, sizeof(struct uart_info));
1521 init_waitqueue_head(&state->info->open_wait);
1522 init_waitqueue_head(&state->info->delta_msr_wait);
1525 * Link the info into the other structures.
1527 state->port->info = state->info;
1529 tasklet_init(&state->info->tlet, uart_tasklet_action,
1530 (unsigned long)state);
1531 } else {
1532 state->count--;
1533 up(&state->sem);
1534 state = ERR_PTR(-ENOMEM);
1538 out:
1539 up(&port_sem);
1540 return state;
1544 * In 2.4.5, calls to uart_open are serialised by the BKL in
1545 * linux/fs/devices.c:chrdev_open()
1546 * Note that if this fails, then uart_close() _will_ be called.
1548 * In time, we want to scrap the "opening nonpresent ports"
1549 * behaviour and implement an alternative way for setserial
1550 * to set base addresses/ports/types. This will allow us to
1551 * get rid of a certain amount of extra tests.
1553 static int uart_open(struct tty_struct *tty, struct file *filp)
1555 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1556 struct uart_state *state;
1557 int retval, line = tty->index;
1559 BUG_ON(!kernel_locked());
1560 DPRINTK("uart_open(%d) called\n", line);
1563 * tty->driver->num won't change, so we won't fail here with
1564 * tty->driver_data set to something non-NULL (and therefore
1565 * we won't get caught by uart_close()).
1567 retval = -ENODEV;
1568 if (line >= tty->driver->num)
1569 goto fail;
1572 * We take the semaphore inside uart_get to guarantee that we won't
1573 * be re-entered while allocating the info structure, or while we
1574 * request any IRQs that the driver may need. This also has the nice
1575 * side-effect that it delays the action of uart_hangup, so we can
1576 * guarantee that info->tty will always contain something reasonable.
1578 state = uart_get(drv, line);
1579 if (IS_ERR(state)) {
1580 retval = PTR_ERR(state);
1581 goto fail;
1585 * Once we set tty->driver_data here, we are guaranteed that
1586 * uart_close() will decrement the driver module use count.
1587 * Any failures from here onwards should not touch the count.
1589 tty->driver_data = state;
1590 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1591 tty->alt_speed = 0;
1592 state->info->tty = tty;
1595 * If the port is in the middle of closing, bail out now.
1597 if (tty_hung_up_p(filp)) {
1598 retval = -EAGAIN;
1599 state->count--;
1600 up(&state->sem);
1601 goto fail;
1605 * Make sure the device is in D0 state.
1607 if (state->count == 1)
1608 uart_change_pm(state, 0);
1611 * Start up the serial port.
1613 retval = uart_startup(state, 0);
1616 * If we succeeded, wait until the port is ready.
1618 if (retval == 0)
1619 retval = uart_block_til_ready(filp, state);
1620 up(&state->sem);
1623 * If this is the first open to succeed, adjust things to suit.
1625 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1626 state->info->flags |= UIF_NORMAL_ACTIVE;
1628 uart_update_termios(state);
1631 fail:
1632 return retval;
1635 static const char *uart_type(struct uart_port *port)
1637 const char *str = NULL;
1639 if (port->ops->type)
1640 str = port->ops->type(port);
1642 if (!str)
1643 str = "unknown";
1645 return str;
1648 #ifdef CONFIG_PROC_FS
1650 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1652 struct uart_state *state = drv->state + i;
1653 struct uart_port *port = state->port;
1654 char stat_buf[32];
1655 unsigned int status;
1656 int ret;
1658 if (!port)
1659 return 0;
1661 ret = sprintf(buf, "%d: uart:%s port:%08X irq:%d",
1662 port->line, uart_type(port),
1663 port->iobase, port->irq);
1665 if (port->type == PORT_UNKNOWN) {
1666 strcat(buf, "\n");
1667 return ret + 1;
1670 status = port->ops->get_mctrl(port);
1672 ret += sprintf(buf + ret, " tx:%d rx:%d",
1673 port->icount.tx, port->icount.rx);
1674 if (port->icount.frame)
1675 ret += sprintf(buf + ret, " fe:%d",
1676 port->icount.frame);
1677 if (port->icount.parity)
1678 ret += sprintf(buf + ret, " pe:%d",
1679 port->icount.parity);
1680 if (port->icount.brk)
1681 ret += sprintf(buf + ret, " brk:%d",
1682 port->icount.brk);
1683 if (port->icount.overrun)
1684 ret += sprintf(buf + ret, " oe:%d",
1685 port->icount.overrun);
1687 #define INFOBIT(bit,str) \
1688 if (port->mctrl & (bit)) \
1689 strncat(stat_buf, (str), sizeof(stat_buf) - \
1690 strlen(stat_buf) - 2)
1691 #define STATBIT(bit,str) \
1692 if (status & (bit)) \
1693 strncat(stat_buf, (str), sizeof(stat_buf) - \
1694 strlen(stat_buf) - 2)
1696 stat_buf[0] = '\0';
1697 stat_buf[1] = '\0';
1698 INFOBIT(TIOCM_RTS, "|RTS");
1699 STATBIT(TIOCM_CTS, "|CTS");
1700 INFOBIT(TIOCM_DTR, "|DTR");
1701 STATBIT(TIOCM_DSR, "|DSR");
1702 STATBIT(TIOCM_CAR, "|CD");
1703 STATBIT(TIOCM_RNG, "|RI");
1704 if (stat_buf[0])
1705 stat_buf[0] = ' ';
1706 strcat(stat_buf, "\n");
1708 ret += sprintf(buf + ret, stat_buf);
1709 return ret;
1712 static int uart_read_proc(char *page, char **start, off_t off,
1713 int count, int *eof, void *data)
1715 struct tty_driver *ttydrv = data;
1716 struct uart_driver *drv = ttydrv->driver_state;
1717 int i, len = 0, l;
1718 off_t begin = 0;
1720 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1721 "", "", "");
1722 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1723 l = uart_line_info(page + len, drv, i);
1724 len += l;
1725 if (len + begin > off + count)
1726 goto done;
1727 if (len + begin < off) {
1728 begin += len;
1729 len = 0;
1732 *eof = 1;
1733 done:
1734 if (off >= len + begin)
1735 return 0;
1736 *start = page + (off - begin);
1737 return (count < begin + len - off) ? count : (begin + len - off);
1739 #endif
1741 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1743 * Check whether an invalid uart number has been specified, and
1744 * if so, search for the first available port that does have
1745 * console support.
1747 struct uart_port * __init
1748 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1750 int idx = co->index;
1752 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1753 ports[idx].membase == NULL))
1754 for (idx = 0; idx < nr; idx++)
1755 if (ports[idx].iobase != 0 ||
1756 ports[idx].membase != NULL)
1757 break;
1759 co->index = idx;
1761 return ports + idx;
1765 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1766 * @options: pointer to option string
1767 * @baud: pointer to an 'int' variable for the baud rate.
1768 * @parity: pointer to an 'int' variable for the parity.
1769 * @bits: pointer to an 'int' variable for the number of data bits.
1770 * @flow: pointer to an 'int' variable for the flow control character.
1772 * uart_parse_options decodes a string containing the serial console
1773 * options. The format of the string is <baud><parity><bits><flow>,
1774 * eg: 115200n8r
1776 void __init
1777 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1779 char *s = options;
1781 *baud = simple_strtoul(s, NULL, 10);
1782 while (*s >= '0' && *s <= '9')
1783 s++;
1784 if (*s)
1785 *parity = *s++;
1786 if (*s)
1787 *bits = *s++ - '0';
1788 if (*s)
1789 *flow = *s;
1792 struct baud_rates {
1793 unsigned int rate;
1794 unsigned int cflag;
1797 static struct baud_rates baud_rates[] = {
1798 { 921600, B921600 },
1799 { 460800, B460800 },
1800 { 230400, B230400 },
1801 { 115200, B115200 },
1802 { 57600, B57600 },
1803 { 38400, B38400 },
1804 { 19200, B19200 },
1805 { 9600, B9600 },
1806 { 4800, B4800 },
1807 { 2400, B2400 },
1808 { 1200, B1200 },
1809 { 0, B38400 }
1813 * uart_set_options - setup the serial console parameters
1814 * @port: pointer to the serial ports uart_port structure
1815 * @co: console pointer
1816 * @baud: baud rate
1817 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1818 * @bits: number of data bits
1819 * @flow: flow control character - 'r' (rts)
1821 int __init
1822 uart_set_options(struct uart_port *port, struct console *co,
1823 int baud, int parity, int bits, int flow)
1825 struct termios termios;
1826 int i;
1828 memset(&termios, 0, sizeof(struct termios));
1830 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1833 * Construct a cflag setting.
1835 for (i = 0; baud_rates[i].rate; i++)
1836 if (baud_rates[i].rate <= baud)
1837 break;
1839 termios.c_cflag |= baud_rates[i].cflag;
1841 if (bits == 7)
1842 termios.c_cflag |= CS7;
1843 else
1844 termios.c_cflag |= CS8;
1846 switch (parity) {
1847 case 'o': case 'O':
1848 termios.c_cflag |= PARODD;
1849 /*fall through*/
1850 case 'e': case 'E':
1851 termios.c_cflag |= PARENB;
1852 break;
1855 if (flow == 'r')
1856 termios.c_cflag |= CRTSCTS;
1858 port->ops->set_termios(port, &termios, NULL);
1859 co->cflag = termios.c_cflag;
1861 return 0;
1863 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1865 static void uart_change_pm(struct uart_state *state, int pm_state)
1867 struct uart_port *port = state->port;
1868 if (port->ops->pm)
1869 port->ops->pm(port, pm_state, state->pm_state);
1870 state->pm_state = pm_state;
1873 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port, u32 level)
1875 struct uart_state *state = drv->state + port->line;
1877 down(&state->sem);
1879 switch (level) {
1880 case SUSPEND_SAVE_STATE:
1881 if (state->info && state->info->flags & UIF_INITIALIZED) {
1882 struct uart_ops *ops = port->ops;
1884 spin_lock_irq(&port->lock);
1885 ops->stop_tx(port, 0);
1886 ops->set_mctrl(port, 0);
1887 ops->stop_rx(port);
1888 spin_unlock_irq(&port->lock);
1891 * Wait for the transmitter to empty.
1893 while (!ops->tx_empty(port)) {
1894 set_current_state(TASK_UNINTERRUPTIBLE);
1895 schedule_timeout(10*HZ/1000);
1897 set_current_state(TASK_RUNNING);
1899 ops->shutdown(port);
1901 break;
1903 case SUSPEND_POWER_DOWN:
1905 * Disable the console device before suspending.
1907 if (uart_console(port))
1908 port->cons->flags &= ~CON_ENABLED;
1910 uart_change_pm(state, 3);
1911 break;
1914 up(&state->sem);
1916 return 0;
1919 int uart_resume_port(struct uart_driver *drv, struct uart_port *port, u32 level)
1921 struct uart_state *state = drv->state + port->line;
1923 down(&state->sem);
1925 switch (level) {
1926 case RESUME_POWER_ON:
1927 uart_change_pm(state, 0);
1930 * Re-enable the console device after suspending.
1932 if (uart_console(port)) {
1933 uart_change_speed(state, NULL);
1934 port->cons->flags |= CON_ENABLED;
1936 break;
1938 case RESUME_RESTORE_STATE:
1939 if (state->info && state->info->flags & UIF_INITIALIZED) {
1940 struct uart_ops *ops = port->ops;
1942 ops->set_mctrl(port, 0);
1943 ops->startup(port);
1944 uart_change_speed(state, NULL);
1945 spin_lock_irq(&port->lock);
1946 ops->set_mctrl(port, port->mctrl);
1947 ops->start_tx(port, 0);
1948 spin_unlock_irq(&port->lock);
1950 break;
1953 up(&state->sem);
1955 return 0;
1958 static inline void
1959 uart_report_port(struct uart_driver *drv, struct uart_port *port)
1961 printk("%s%d", drv->dev_name, port->line);
1962 printk(" at ");
1963 switch (port->iotype) {
1964 case UPIO_PORT:
1965 printk("I/O 0x%x", port->iobase);
1966 break;
1967 case UPIO_HUB6:
1968 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1969 break;
1970 case UPIO_MEM:
1971 printk("MMIO 0x%lx", port->mapbase);
1972 break;
1974 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
1977 static void
1978 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
1979 struct uart_port *port)
1981 unsigned int flags;
1984 * If there isn't a port here, don't do anything further.
1986 if (!port->iobase && !port->mapbase && !port->membase)
1987 return;
1990 * Now do the auto configuration stuff. Note that config_port
1991 * is expected to claim the resources and map the port for us.
1993 flags = UART_CONFIG_TYPE;
1994 if (port->flags & UPF_AUTO_IRQ)
1995 flags |= UART_CONFIG_IRQ;
1996 if (port->flags & UPF_BOOT_AUTOCONF) {
1997 port->type = PORT_UNKNOWN;
1998 port->ops->config_port(port, flags);
2001 if (port->type != PORT_UNKNOWN) {
2002 unsigned long flags;
2004 uart_report_port(drv, port);
2007 * Ensure that the modem control lines are de-activated.
2008 * We probably don't need a spinlock around this, but
2010 spin_lock_irqsave(&port->lock, flags);
2011 port->ops->set_mctrl(port, 0);
2012 spin_unlock_irqrestore(&port->lock, flags);
2015 * Power down all ports by default, except the
2016 * console if we have one.
2018 if (!uart_console(port))
2019 uart_change_pm(state, 3);
2024 * This reverses the effects of uart_configure_port, hanging up the
2025 * port before removal.
2027 static void
2028 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2030 struct uart_port *port = state->port;
2031 struct uart_info *info = state->info;
2033 if (info && info->tty)
2034 tty_vhangup(info->tty);
2036 down(&state->sem);
2038 state->info = NULL;
2041 * Free the port IO and memory resources, if any.
2043 if (port->type != PORT_UNKNOWN)
2044 port->ops->release_port(port);
2047 * Indicate that there isn't a port here anymore.
2049 port->type = PORT_UNKNOWN;
2052 * Kill the tasklet, and free resources.
2054 if (info) {
2055 tasklet_kill(&info->tlet);
2056 kfree(info);
2059 up(&state->sem);
2062 static struct tty_operations uart_ops = {
2063 .open = uart_open,
2064 .close = uart_close,
2065 .write = uart_write,
2066 .put_char = uart_put_char,
2067 .flush_chars = uart_flush_chars,
2068 .write_room = uart_write_room,
2069 .chars_in_buffer= uart_chars_in_buffer,
2070 .flush_buffer = uart_flush_buffer,
2071 .ioctl = uart_ioctl,
2072 .throttle = uart_throttle,
2073 .unthrottle = uart_unthrottle,
2074 .send_xchar = uart_send_xchar,
2075 .set_termios = uart_set_termios,
2076 .stop = uart_stop,
2077 .start = uart_start,
2078 .hangup = uart_hangup,
2079 .break_ctl = uart_break_ctl,
2080 .wait_until_sent= uart_wait_until_sent,
2081 #ifdef CONFIG_PROC_FS
2082 .read_proc = uart_read_proc,
2083 #endif
2084 .tiocmget = uart_tiocmget,
2085 .tiocmset = uart_tiocmset,
2089 * uart_register_driver - register a driver with the uart core layer
2090 * @drv: low level driver structure
2092 * Register a uart driver with the core driver. We in turn register
2093 * with the tty layer, and initialise the core driver per-port state.
2095 * We have a proc file in /proc/tty/driver which is named after the
2096 * normal driver.
2098 * drv->port should be NULL, and the per-port structures should be
2099 * registered using uart_add_one_port after this call has succeeded.
2101 int uart_register_driver(struct uart_driver *drv)
2103 struct tty_driver *normal = NULL;
2104 int i, retval;
2106 BUG_ON(drv->state);
2109 * Maybe we should be using a slab cache for this, especially if
2110 * we have a large number of ports to handle.
2112 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2113 retval = -ENOMEM;
2114 if (!drv->state)
2115 goto out;
2117 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2119 normal = alloc_tty_driver(drv->nr);
2120 if (!normal)
2121 goto out;
2123 drv->tty_driver = normal;
2125 normal->owner = drv->owner;
2126 normal->driver_name = drv->driver_name;
2127 normal->devfs_name = drv->devfs_name;
2128 normal->name = drv->dev_name;
2129 normal->major = drv->major;
2130 normal->minor_start = drv->minor;
2131 normal->type = TTY_DRIVER_TYPE_SERIAL;
2132 normal->subtype = SERIAL_TYPE_NORMAL;
2133 normal->init_termios = tty_std_termios;
2134 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2135 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2136 normal->driver_state = drv;
2137 tty_set_operations(normal, &uart_ops);
2140 * Initialise the UART state(s).
2142 for (i = 0; i < drv->nr; i++) {
2143 struct uart_state *state = drv->state + i;
2145 state->close_delay = 5 * HZ / 10;
2146 state->closing_wait = 30 * HZ;
2148 init_MUTEX(&state->sem);
2151 retval = tty_register_driver(normal);
2152 out:
2153 if (retval < 0) {
2154 put_tty_driver(normal);
2155 kfree(drv->state);
2157 return retval;
2161 * uart_unregister_driver - remove a driver from the uart core layer
2162 * @drv: low level driver structure
2164 * Remove all references to a driver from the core driver. The low
2165 * level driver must have removed all its ports via the
2166 * uart_remove_one_port() if it registered them with uart_add_one_port().
2167 * (ie, drv->port == NULL)
2169 void uart_unregister_driver(struct uart_driver *drv)
2171 struct tty_driver *p = drv->tty_driver;
2172 tty_unregister_driver(p);
2173 put_tty_driver(p);
2174 kfree(drv->state);
2175 drv->tty_driver = NULL;
2178 struct tty_driver *uart_console_device(struct console *co, int *index)
2180 struct uart_driver *p = co->data;
2181 *index = co->index;
2182 return p->tty_driver;
2186 * uart_add_one_port - attach a driver-defined port structure
2187 * @drv: pointer to the uart low level driver structure for this port
2188 * @port: uart port structure to use for this port.
2190 * This allows the driver to register its own uart_port structure
2191 * with the core driver. The main purpose is to allow the low
2192 * level uart drivers to expand uart_port, rather than having yet
2193 * more levels of structures.
2195 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2197 struct uart_state *state;
2198 int ret = 0;
2200 BUG_ON(in_interrupt());
2202 if (port->line >= drv->nr)
2203 return -EINVAL;
2205 state = drv->state + port->line;
2207 down(&port_sem);
2208 if (state->port) {
2209 ret = -EINVAL;
2210 goto out;
2213 state->port = port;
2215 spin_lock_init(&port->lock);
2216 port->cons = drv->cons;
2217 port->info = state->info;
2219 uart_configure_port(drv, state, port);
2222 * Register the port whether it's detected or not. This allows
2223 * setserial to be used to alter this ports parameters.
2225 tty_register_device(drv->tty_driver, port->line, NULL);
2227 out:
2228 up(&port_sem);
2230 return ret;
2234 * uart_remove_one_port - detach a driver defined port structure
2235 * @drv: pointer to the uart low level driver structure for this port
2236 * @port: uart port structure for this port
2238 * This unhooks (and hangs up) the specified port structure from the
2239 * core driver. No further calls will be made to the low-level code
2240 * for this port.
2242 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2244 struct uart_state *state = drv->state + port->line;
2246 BUG_ON(in_interrupt());
2248 if (state->port != port)
2249 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2250 state->port, port);
2252 down(&port_sem);
2255 * Remove the devices from devfs
2257 tty_unregister_device(drv->tty_driver, port->line);
2259 uart_unconfigure_port(drv, state);
2260 state->port = NULL;
2261 up(&port_sem);
2263 return 0;
2267 * Are the two ports equivalent?
2269 static int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2271 if (port1->iotype != port2->iotype)
2272 return 0;
2274 switch (port1->iotype) {
2275 case UPIO_PORT:
2276 return (port1->iobase == port2->iobase);
2277 case UPIO_HUB6:
2278 return (port1->iobase == port2->iobase) &&
2279 (port1->hub6 == port2->hub6);
2280 case UPIO_MEM:
2281 return (port1->membase == port2->membase);
2283 return 0;
2287 * Try to find an unused uart_state slot for a port.
2289 static struct uart_state *
2290 uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2292 int i;
2295 * First, find a port entry which matches. Note: if we do
2296 * find a matching entry, and it has a non-zero use count,
2297 * then we can't register the port.
2299 for (i = 0; i < drv->nr; i++)
2300 if (uart_match_port(drv->state[i].port, port))
2301 return &drv->state[i];
2304 * We didn't find a matching entry, so look for the first
2305 * free entry. We look for one which hasn't been previously
2306 * used (indicated by zero iobase).
2308 for (i = 0; i < drv->nr; i++)
2309 if (drv->state[i].port->type == PORT_UNKNOWN &&
2310 drv->state[i].port->iobase == 0 &&
2311 drv->state[i].count == 0)
2312 return &drv->state[i];
2315 * That also failed. Last resort is to find any currently
2316 * entry which doesn't have a real port associated with it.
2318 for (i = 0; i < drv->nr; i++)
2319 if (drv->state[i].port->type == PORT_UNKNOWN &&
2320 drv->state[i].count == 0)
2321 return &drv->state[i];
2323 return NULL;
2327 * uart_register_port: register uart settings with a port
2328 * @drv: pointer to the uart low level driver structure for this port
2329 * @port: uart port structure describing the port
2331 * Register UART settings with the specified low level driver. Detect
2332 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2333 * IRQ if UPF_AUTO_IRQ is set.
2335 * We try to pick the same port for the same IO base address, so that
2336 * when a modem is plugged in, unplugged and plugged back in, it gets
2337 * allocated the same port.
2339 * Returns negative error, or positive line number.
2341 int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2343 struct uart_state *state;
2344 int ret;
2346 down(&port_sem);
2348 state = uart_find_match_or_unused(drv, port);
2350 if (state) {
2352 * Ok, we've found a line that we can use.
2354 * If we find a port that matches this one, and it appears
2355 * to be in-use (even if it doesn't have a type) we shouldn't
2356 * alter it underneath itself - the port may be open and
2357 * trying to do useful work.
2359 if (uart_users(state) != 0) {
2360 ret = -EBUSY;
2361 goto out;
2365 * If the port is already initialised, don't touch it.
2367 if (state->port->type == PORT_UNKNOWN) {
2368 state->port->iobase = port->iobase;
2369 state->port->membase = port->membase;
2370 state->port->irq = port->irq;
2371 state->port->uartclk = port->uartclk;
2372 state->port->fifosize = port->fifosize;
2373 state->port->regshift = port->regshift;
2374 state->port->iotype = port->iotype;
2375 state->port->flags = port->flags;
2376 state->port->line = state - drv->state;
2377 state->port->mapbase = port->mapbase;
2379 uart_configure_port(drv, state, state->port);
2382 ret = state->port->line;
2383 } else
2384 ret = -ENOSPC;
2385 out:
2386 up(&port_sem);
2387 return ret;
2391 * uart_unregister_port - de-allocate a port
2392 * @drv: pointer to the uart low level driver structure for this port
2393 * @line: line index previously returned from uart_register_port()
2395 * Hang up the specified line associated with the low level driver,
2396 * and mark the port as unused.
2398 void uart_unregister_port(struct uart_driver *drv, int line)
2400 struct uart_state *state;
2402 if (line < 0 || line >= drv->nr) {
2403 printk(KERN_ERR "Attempt to unregister ");
2404 printk("%s%d", drv->dev_name, line);
2405 printk("\n");
2406 return;
2409 state = drv->state + line;
2411 down(&port_sem);
2412 uart_unconfigure_port(drv, state);
2413 up(&port_sem);
2416 EXPORT_SYMBOL(uart_write_wakeup);
2417 EXPORT_SYMBOL(uart_register_driver);
2418 EXPORT_SYMBOL(uart_unregister_driver);
2419 EXPORT_SYMBOL(uart_suspend_port);
2420 EXPORT_SYMBOL(uart_resume_port);
2421 EXPORT_SYMBOL(uart_register_port);
2422 EXPORT_SYMBOL(uart_unregister_port);
2423 EXPORT_SYMBOL(uart_add_one_port);
2424 EXPORT_SYMBOL(uart_remove_one_port);
2426 MODULE_DESCRIPTION("Serial driver core");
2427 MODULE_LICENSE("GPL");