Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / tty / serial / serial_core.c
blob5c04cb9f2390f36fa3643129cc5ab5983ed4fa3d
1 /*
2 * Driver core for serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/device.h>
31 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
32 #include <linux/serial_core.h>
33 #include <linux/delay.h>
34 #include <linux/mutex.h>
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
40 * This is used to lock changes in serial line configuration.
42 static DEFINE_MUTEX(port_mutex);
45 * lockdep: port->lock is initialized in two places, but we
46 * want only one lock-class:
48 static struct lock_class_key port_lock_key;
50 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
52 #ifdef CONFIG_SERIAL_CORE_CONSOLE
53 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
54 #else
55 #define uart_console(port) (0)
56 #endif
58 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
59 struct ktermios *old_termios);
60 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
61 static void uart_change_pm(struct uart_state *state, int pm_state);
64 * This routine is used by the interrupt handler to schedule processing in
65 * the software interrupt portion of the driver.
67 void uart_write_wakeup(struct uart_port *port)
69 struct uart_state *state = port->state;
71 * This means you called this function _after_ the port was
72 * closed. No cookie for you.
74 BUG_ON(!state);
75 tty_wakeup(state->port.tty);
78 static void uart_stop(struct tty_struct *tty)
80 struct uart_state *state = tty->driver_data;
81 struct uart_port *port = state->uart_port;
82 unsigned long flags;
84 spin_lock_irqsave(&port->lock, flags);
85 port->ops->stop_tx(port);
86 spin_unlock_irqrestore(&port->lock, flags);
89 static void __uart_start(struct tty_struct *tty)
91 struct uart_state *state = tty->driver_data;
92 struct uart_port *port = state->uart_port;
94 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
95 !tty->stopped && !tty->hw_stopped)
96 port->ops->start_tx(port);
99 static void uart_start(struct tty_struct *tty)
101 struct uart_state *state = tty->driver_data;
102 struct uart_port *port = state->uart_port;
103 unsigned long flags;
105 spin_lock_irqsave(&port->lock, flags);
106 __uart_start(tty);
107 spin_unlock_irqrestore(&port->lock, flags);
110 static inline void
111 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
113 unsigned long flags;
114 unsigned int old;
116 spin_lock_irqsave(&port->lock, flags);
117 old = port->mctrl;
118 port->mctrl = (old & ~clear) | set;
119 if (old != port->mctrl)
120 port->ops->set_mctrl(port, port->mctrl);
121 spin_unlock_irqrestore(&port->lock, flags);
124 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
125 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
128 * Startup the port. This will be called once per open. All calls
129 * will be serialised by the per-port mutex.
131 static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
133 struct uart_port *uport = state->uart_port;
134 struct tty_port *port = &state->port;
135 unsigned long page;
136 int retval = 0;
138 if (port->flags & ASYNC_INITIALIZED)
139 return 0;
142 * Set the TTY IO error marker - we will only clear this
143 * once we have successfully opened the port. Also set
144 * up the tty->alt_speed kludge
146 set_bit(TTY_IO_ERROR, &tty->flags);
148 if (uport->type == PORT_UNKNOWN)
149 return 0;
152 * Initialise and allocate the transmit and temporary
153 * buffer.
155 if (!state->xmit.buf) {
156 /* This is protected by the per port mutex */
157 page = get_zeroed_page(GFP_KERNEL);
158 if (!page)
159 return -ENOMEM;
161 state->xmit.buf = (unsigned char *) page;
162 uart_circ_clear(&state->xmit);
165 retval = uport->ops->startup(uport);
166 if (retval == 0) {
167 if (uart_console(uport) && uport->cons->cflag) {
168 tty->termios->c_cflag = uport->cons->cflag;
169 uport->cons->cflag = 0;
172 * Initialise the hardware port settings.
174 uart_change_speed(tty, state, NULL);
176 if (init_hw) {
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (tty->termios->c_cflag & CBAUD)
182 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
185 if (port->flags & ASYNC_CTS_FLOW) {
186 spin_lock_irq(&uport->lock);
187 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
188 tty->hw_stopped = 1;
189 spin_unlock_irq(&uport->lock);
192 set_bit(ASYNCB_INITIALIZED, &port->flags);
194 clear_bit(TTY_IO_ERROR, &tty->flags);
198 * This is to allow setserial on this port. People may want to set
199 * port/irq/type and then reconfigure the port properly if it failed
200 * now.
202 if (retval && capable(CAP_SYS_ADMIN))
203 retval = 0;
205 return retval;
209 * This routine will shutdown a serial port; interrupts are disabled, and
210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore.
213 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
215 struct uart_port *uport = state->uart_port;
216 struct tty_port *port = &state->port;
219 * Set the TTY IO error marker
221 if (tty)
222 set_bit(TTY_IO_ERROR, &tty->flags);
224 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
226 * Turn off DTR and RTS early.
228 if (!tty || (tty->termios->c_cflag & HUPCL))
229 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
232 * clear delta_msr_wait queue to avoid mem leaks: we may free
233 * the irq here so the queue might never be woken up. Note
234 * that we won't end up waiting on delta_msr_wait again since
235 * any outstanding file descriptors should be pointing at
236 * hung_up_tty_fops now.
238 wake_up_interruptible(&port->delta_msr_wait);
241 * Free the IRQ and disable the port.
243 uport->ops->shutdown(uport);
246 * Ensure that the IRQ handler isn't running on another CPU.
248 synchronize_irq(uport->irq);
252 * Free the transmit buffer page.
254 if (state->xmit.buf) {
255 free_page((unsigned long)state->xmit.buf);
256 state->xmit.buf = NULL;
261 * uart_update_timeout - update per-port FIFO timeout.
262 * @port: uart_port structure describing the port
263 * @cflag: termios cflag value
264 * @baud: speed of the port
266 * Set the port FIFO timeout value. The @cflag value should
267 * reflect the actual hardware settings.
269 void
270 uart_update_timeout(struct uart_port *port, unsigned int cflag,
271 unsigned int baud)
273 unsigned int bits;
275 /* byte size and parity */
276 switch (cflag & CSIZE) {
277 case CS5:
278 bits = 7;
279 break;
280 case CS6:
281 bits = 8;
282 break;
283 case CS7:
284 bits = 9;
285 break;
286 default:
287 bits = 10;
288 break; /* CS8 */
291 if (cflag & CSTOPB)
292 bits++;
293 if (cflag & PARENB)
294 bits++;
297 * The total number of bits to be transmitted in the fifo.
299 bits = bits * port->fifosize;
302 * Figure the timeout to send the above number of bits.
303 * Add .02 seconds of slop
305 port->timeout = (HZ * bits) / baud + HZ/50;
308 EXPORT_SYMBOL(uart_update_timeout);
311 * uart_get_baud_rate - return baud rate for a particular port
312 * @port: uart_port structure describing the port in question.
313 * @termios: desired termios settings.
314 * @old: old termios (or NULL)
315 * @min: minimum acceptable baud rate
316 * @max: maximum acceptable baud rate
318 * Decode the termios structure into a numeric baud rate,
319 * taking account of the magic 38400 baud rate (with spd_*
320 * flags), and mapping the %B0 rate to 9600 baud.
322 * If the new baud rate is invalid, try the old termios setting.
323 * If it's still invalid, we try 9600 baud.
325 * Update the @termios structure to reflect the baud rate
326 * we're actually going to be using. Don't do this for the case
327 * where B0 is requested ("hang up").
329 unsigned int
330 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
331 struct ktermios *old, unsigned int min, unsigned int max)
333 unsigned int try, baud, altbaud = 38400;
334 int hung_up = 0;
335 upf_t flags = port->flags & UPF_SPD_MASK;
337 if (flags == UPF_SPD_HI)
338 altbaud = 57600;
339 else if (flags == UPF_SPD_VHI)
340 altbaud = 115200;
341 else if (flags == UPF_SPD_SHI)
342 altbaud = 230400;
343 else 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 hung_up = 1;
361 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 baud = tty_termios_baud_rate(old);
374 if (!hung_up)
375 tty_termios_encode_baud_rate(termios,
376 baud, baud);
377 old = NULL;
378 continue;
382 * As a last resort, if the range cannot be met then clip to
383 * the nearest chip supported rate.
385 if (!hung_up) {
386 if (baud <= min)
387 tty_termios_encode_baud_rate(termios,
388 min + 1, min + 1);
389 else
390 tty_termios_encode_baud_rate(termios,
391 max - 1, max - 1);
394 /* Should never happen */
395 WARN_ON(1);
396 return 0;
399 EXPORT_SYMBOL(uart_get_baud_rate);
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
406 * Calculate the uart clock divisor for the port.
408 unsigned int
409 uart_get_divisor(struct uart_port *port, unsigned int baud)
411 unsigned int quot;
414 * Old custom speed handling.
416 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
417 quot = port->custom_divisor;
418 else
419 quot = (port->uartclk + (8 * baud)) / (16 * baud);
421 return quot;
424 EXPORT_SYMBOL(uart_get_divisor);
426 /* FIXME: Consistent locking policy */
427 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
428 struct ktermios *old_termios)
430 struct tty_port *port = &state->port;
431 struct uart_port *uport = state->uart_port;
432 struct ktermios *termios;
435 * If we have no tty, termios, or the port does not exist,
436 * then we can't set the parameters for this port.
438 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
439 return;
441 termios = tty->termios;
444 * Set flags based on termios cflag
446 if (termios->c_cflag & CRTSCTS)
447 set_bit(ASYNCB_CTS_FLOW, &port->flags);
448 else
449 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
451 if (termios->c_cflag & CLOCAL)
452 clear_bit(ASYNCB_CHECK_CD, &port->flags);
453 else
454 set_bit(ASYNCB_CHECK_CD, &port->flags);
456 uport->ops->set_termios(uport, termios, old_termios);
459 static inline int __uart_put_char(struct uart_port *port,
460 struct circ_buf *circ, unsigned char c)
462 unsigned long flags;
463 int ret = 0;
465 if (!circ->buf)
466 return 0;
468 spin_lock_irqsave(&port->lock, flags);
469 if (uart_circ_chars_free(circ) != 0) {
470 circ->buf[circ->head] = c;
471 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
472 ret = 1;
474 spin_unlock_irqrestore(&port->lock, flags);
475 return ret;
478 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
480 struct uart_state *state = tty->driver_data;
482 return __uart_put_char(state->uart_port, &state->xmit, ch);
485 static void uart_flush_chars(struct tty_struct *tty)
487 uart_start(tty);
490 static int uart_write(struct tty_struct *tty,
491 const unsigned char *buf, int count)
493 struct uart_state *state = tty->driver_data;
494 struct uart_port *port;
495 struct circ_buf *circ;
496 unsigned long flags;
497 int c, ret = 0;
500 * This means you called this function _after_ the port was
501 * closed. No cookie for you.
503 if (!state) {
504 WARN_ON(1);
505 return -EL3HLT;
508 port = state->uart_port;
509 circ = &state->xmit;
511 if (!circ->buf)
512 return 0;
514 spin_lock_irqsave(&port->lock, flags);
515 while (1) {
516 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
517 if (count < c)
518 c = count;
519 if (c <= 0)
520 break;
521 memcpy(circ->buf + circ->head, buf, c);
522 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
523 buf += c;
524 count -= c;
525 ret += c;
527 spin_unlock_irqrestore(&port->lock, flags);
529 uart_start(tty);
530 return ret;
533 static int uart_write_room(struct tty_struct *tty)
535 struct uart_state *state = tty->driver_data;
536 unsigned long flags;
537 int ret;
539 spin_lock_irqsave(&state->uart_port->lock, flags);
540 ret = uart_circ_chars_free(&state->xmit);
541 spin_unlock_irqrestore(&state->uart_port->lock, flags);
542 return ret;
545 static int uart_chars_in_buffer(struct tty_struct *tty)
547 struct uart_state *state = tty->driver_data;
548 unsigned long flags;
549 int ret;
551 spin_lock_irqsave(&state->uart_port->lock, flags);
552 ret = uart_circ_chars_pending(&state->xmit);
553 spin_unlock_irqrestore(&state->uart_port->lock, flags);
554 return ret;
557 static void uart_flush_buffer(struct tty_struct *tty)
559 struct uart_state *state = tty->driver_data;
560 struct uart_port *port;
561 unsigned long flags;
564 * This means you called this function _after_ the port was
565 * closed. No cookie for you.
567 if (!state) {
568 WARN_ON(1);
569 return;
572 port = state->uart_port;
573 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
575 spin_lock_irqsave(&port->lock, flags);
576 uart_circ_clear(&state->xmit);
577 if (port->ops->flush_buffer)
578 port->ops->flush_buffer(port);
579 spin_unlock_irqrestore(&port->lock, flags);
580 tty_wakeup(tty);
584 * This function is used to send a high-priority XON/XOFF character to
585 * the device
587 static void uart_send_xchar(struct tty_struct *tty, char ch)
589 struct uart_state *state = tty->driver_data;
590 struct uart_port *port = state->uart_port;
591 unsigned long flags;
593 if (port->ops->send_xchar)
594 port->ops->send_xchar(port, ch);
595 else {
596 port->x_char = ch;
597 if (ch) {
598 spin_lock_irqsave(&port->lock, flags);
599 port->ops->start_tx(port);
600 spin_unlock_irqrestore(&port->lock, flags);
605 static void uart_throttle(struct tty_struct *tty)
607 struct uart_state *state = tty->driver_data;
609 if (I_IXOFF(tty))
610 uart_send_xchar(tty, STOP_CHAR(tty));
612 if (tty->termios->c_cflag & CRTSCTS)
613 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
616 static void uart_unthrottle(struct tty_struct *tty)
618 struct uart_state *state = tty->driver_data;
619 struct uart_port *port = state->uart_port;
621 if (I_IXOFF(tty)) {
622 if (port->x_char)
623 port->x_char = 0;
624 else
625 uart_send_xchar(tty, START_CHAR(tty));
628 if (tty->termios->c_cflag & CRTSCTS)
629 uart_set_mctrl(port, TIOCM_RTS);
632 static int uart_get_info(struct uart_state *state,
633 struct serial_struct __user *retinfo)
635 struct uart_port *uport = state->uart_port;
636 struct tty_port *port = &state->port;
637 struct serial_struct tmp;
639 memset(&tmp, 0, sizeof(tmp));
641 /* Ensure the state we copy is consistent and no hardware changes
642 occur as we go */
643 mutex_lock(&port->mutex);
645 tmp.type = uport->type;
646 tmp.line = uport->line;
647 tmp.port = uport->iobase;
648 if (HIGH_BITS_OFFSET)
649 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
650 tmp.irq = uport->irq;
651 tmp.flags = uport->flags;
652 tmp.xmit_fifo_size = uport->fifosize;
653 tmp.baud_base = uport->uartclk / 16;
654 tmp.close_delay = port->close_delay / 10;
655 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
656 ASYNC_CLOSING_WAIT_NONE :
657 port->closing_wait / 10;
658 tmp.custom_divisor = uport->custom_divisor;
659 tmp.hub6 = uport->hub6;
660 tmp.io_type = uport->iotype;
661 tmp.iomem_reg_shift = uport->regshift;
662 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
664 mutex_unlock(&port->mutex);
666 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
667 return -EFAULT;
668 return 0;
671 static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
672 struct serial_struct __user *newinfo)
674 struct serial_struct new_serial;
675 struct uart_port *uport = state->uart_port;
676 struct tty_port *port = &state->port;
677 unsigned long new_port;
678 unsigned int change_irq, change_port, closing_wait;
679 unsigned int old_custom_divisor, close_delay;
680 upf_t old_flags, new_flags;
681 int retval = 0;
683 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
684 return -EFAULT;
686 new_port = new_serial.port;
687 if (HIGH_BITS_OFFSET)
688 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
690 new_serial.irq = irq_canonicalize(new_serial.irq);
691 close_delay = new_serial.close_delay * 10;
692 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
693 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
696 * This semaphore protects port->count. It is also
697 * very useful to prevent opens. Also, take the
698 * port configuration semaphore to make sure that a
699 * module insertion/removal doesn't change anything
700 * under us.
702 mutex_lock(&port->mutex);
704 change_irq = !(uport->flags & UPF_FIXED_PORT)
705 && new_serial.irq != uport->irq;
708 * Since changing the 'type' of the port changes its resource
709 * allocations, we should treat type changes the same as
710 * IO port changes.
712 change_port = !(uport->flags & UPF_FIXED_PORT)
713 && (new_port != uport->iobase ||
714 (unsigned long)new_serial.iomem_base != uport->mapbase ||
715 new_serial.hub6 != uport->hub6 ||
716 new_serial.io_type != uport->iotype ||
717 new_serial.iomem_reg_shift != uport->regshift ||
718 new_serial.type != uport->type);
720 old_flags = uport->flags;
721 new_flags = new_serial.flags;
722 old_custom_divisor = uport->custom_divisor;
724 if (!capable(CAP_SYS_ADMIN)) {
725 retval = -EPERM;
726 if (change_irq || change_port ||
727 (new_serial.baud_base != uport->uartclk / 16) ||
728 (close_delay != port->close_delay) ||
729 (closing_wait != port->closing_wait) ||
730 (new_serial.xmit_fifo_size &&
731 new_serial.xmit_fifo_size != uport->fifosize) ||
732 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
733 goto exit;
734 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
735 (new_flags & UPF_USR_MASK));
736 uport->custom_divisor = new_serial.custom_divisor;
737 goto check_and_exit;
741 * Ask the low level driver to verify the settings.
743 if (uport->ops->verify_port)
744 retval = uport->ops->verify_port(uport, &new_serial);
746 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
747 (new_serial.baud_base < 9600))
748 retval = -EINVAL;
750 if (retval)
751 goto exit;
753 if (change_port || change_irq) {
754 retval = -EBUSY;
757 * Make sure that we are the sole user of this port.
759 if (tty_port_users(port) > 1)
760 goto exit;
763 * We need to shutdown the serial port at the old
764 * port/type/irq combination.
766 uart_shutdown(tty, state);
769 if (change_port) {
770 unsigned long old_iobase, old_mapbase;
771 unsigned int old_type, old_iotype, old_hub6, old_shift;
773 old_iobase = uport->iobase;
774 old_mapbase = uport->mapbase;
775 old_type = uport->type;
776 old_hub6 = uport->hub6;
777 old_iotype = uport->iotype;
778 old_shift = uport->regshift;
781 * Free and release old regions
783 if (old_type != PORT_UNKNOWN)
784 uport->ops->release_port(uport);
786 uport->iobase = new_port;
787 uport->type = new_serial.type;
788 uport->hub6 = new_serial.hub6;
789 uport->iotype = new_serial.io_type;
790 uport->regshift = new_serial.iomem_reg_shift;
791 uport->mapbase = (unsigned long)new_serial.iomem_base;
794 * Claim and map the new regions
796 if (uport->type != PORT_UNKNOWN) {
797 retval = uport->ops->request_port(uport);
798 } else {
799 /* Always success - Jean II */
800 retval = 0;
804 * If we fail to request resources for the
805 * new port, try to restore the old settings.
807 if (retval && old_type != PORT_UNKNOWN) {
808 uport->iobase = old_iobase;
809 uport->type = old_type;
810 uport->hub6 = old_hub6;
811 uport->iotype = old_iotype;
812 uport->regshift = old_shift;
813 uport->mapbase = old_mapbase;
814 retval = uport->ops->request_port(uport);
816 * If we failed to restore the old settings,
817 * we fail like this.
819 if (retval)
820 uport->type = PORT_UNKNOWN;
823 * We failed anyway.
825 retval = -EBUSY;
826 /* Added to return the correct error -Ram Gupta */
827 goto exit;
831 if (change_irq)
832 uport->irq = new_serial.irq;
833 if (!(uport->flags & UPF_FIXED_PORT))
834 uport->uartclk = new_serial.baud_base * 16;
835 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
836 (new_flags & UPF_CHANGE_MASK);
837 uport->custom_divisor = new_serial.custom_divisor;
838 port->close_delay = close_delay;
839 port->closing_wait = closing_wait;
840 if (new_serial.xmit_fifo_size)
841 uport->fifosize = new_serial.xmit_fifo_size;
842 if (port->tty)
843 port->tty->low_latency =
844 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
846 check_and_exit:
847 retval = 0;
848 if (uport->type == PORT_UNKNOWN)
849 goto exit;
850 if (port->flags & ASYNC_INITIALIZED) {
851 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
852 old_custom_divisor != uport->custom_divisor) {
854 * If they're setting up a custom divisor or speed,
855 * instead of clearing it, then bitch about it. No
856 * need to rate-limit; it's CAP_SYS_ADMIN only.
858 if (uport->flags & UPF_SPD_MASK) {
859 char buf[64];
860 printk(KERN_NOTICE
861 "%s sets custom speed on %s. This "
862 "is deprecated.\n", current->comm,
863 tty_name(port->tty, buf));
865 uart_change_speed(tty, state, NULL);
867 } else
868 retval = uart_startup(tty, state, 1);
869 exit:
870 mutex_unlock(&port->mutex);
871 return retval;
875 * uart_get_lsr_info - get line status register info
876 * @tty: tty associated with the UART
877 * @state: UART being queried
878 * @value: returned modem value
880 * Note: uart_ioctl protects us against hangups.
882 static int uart_get_lsr_info(struct tty_struct *tty,
883 struct uart_state *state, unsigned int __user *value)
885 struct uart_port *uport = state->uart_port;
886 unsigned int result;
888 result = uport->ops->tx_empty(uport);
891 * If we're about to load something into the transmit
892 * register, we'll pretend the transmitter isn't empty to
893 * avoid a race condition (depending on when the transmit
894 * interrupt happens).
896 if (uport->x_char ||
897 ((uart_circ_chars_pending(&state->xmit) > 0) &&
898 !tty->stopped && !tty->hw_stopped))
899 result &= ~TIOCSER_TEMT;
901 return put_user(result, value);
904 static int uart_tiocmget(struct tty_struct *tty)
906 struct uart_state *state = tty->driver_data;
907 struct tty_port *port = &state->port;
908 struct uart_port *uport = state->uart_port;
909 int result = -EIO;
911 mutex_lock(&port->mutex);
912 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
913 result = uport->mctrl;
914 spin_lock_irq(&uport->lock);
915 result |= uport->ops->get_mctrl(uport);
916 spin_unlock_irq(&uport->lock);
918 mutex_unlock(&port->mutex);
920 return result;
923 static int
924 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
926 struct uart_state *state = tty->driver_data;
927 struct uart_port *uport = state->uart_port;
928 struct tty_port *port = &state->port;
929 int ret = -EIO;
931 mutex_lock(&port->mutex);
932 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
933 uart_update_mctrl(uport, set, clear);
934 ret = 0;
936 mutex_unlock(&port->mutex);
937 return ret;
940 static int uart_break_ctl(struct tty_struct *tty, int break_state)
942 struct uart_state *state = tty->driver_data;
943 struct tty_port *port = &state->port;
944 struct uart_port *uport = state->uart_port;
946 mutex_lock(&port->mutex);
948 if (uport->type != PORT_UNKNOWN)
949 uport->ops->break_ctl(uport, break_state);
951 mutex_unlock(&port->mutex);
952 return 0;
955 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
957 struct uart_port *uport = state->uart_port;
958 struct tty_port *port = &state->port;
959 int flags, ret;
961 if (!capable(CAP_SYS_ADMIN))
962 return -EPERM;
965 * Take the per-port semaphore. This prevents count from
966 * changing, and hence any extra opens of the port while
967 * we're auto-configuring.
969 if (mutex_lock_interruptible(&port->mutex))
970 return -ERESTARTSYS;
972 ret = -EBUSY;
973 if (tty_port_users(port) == 1) {
974 uart_shutdown(tty, state);
977 * If we already have a port type configured,
978 * we must release its resources.
980 if (uport->type != PORT_UNKNOWN)
981 uport->ops->release_port(uport);
983 flags = UART_CONFIG_TYPE;
984 if (uport->flags & UPF_AUTO_IRQ)
985 flags |= UART_CONFIG_IRQ;
988 * This will claim the ports resources if
989 * a port is found.
991 uport->ops->config_port(uport, flags);
993 ret = uart_startup(tty, state, 1);
995 mutex_unlock(&port->mutex);
996 return ret;
1000 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1001 * - mask passed in arg for lines of interest
1002 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1003 * Caller should use TIOCGICOUNT to see which one it was
1005 * FIXME: This wants extracting into a common all driver implementation
1006 * of TIOCMWAIT using tty_port.
1008 static int
1009 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1011 struct uart_port *uport = state->uart_port;
1012 struct tty_port *port = &state->port;
1013 DECLARE_WAITQUEUE(wait, current);
1014 struct uart_icount cprev, cnow;
1015 int ret;
1018 * note the counters on entry
1020 spin_lock_irq(&uport->lock);
1021 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1024 * Force modem status interrupts on
1026 uport->ops->enable_ms(uport);
1027 spin_unlock_irq(&uport->lock);
1029 add_wait_queue(&port->delta_msr_wait, &wait);
1030 for (;;) {
1031 spin_lock_irq(&uport->lock);
1032 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1033 spin_unlock_irq(&uport->lock);
1035 set_current_state(TASK_INTERRUPTIBLE);
1037 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1038 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1039 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1040 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1041 ret = 0;
1042 break;
1045 schedule();
1047 /* see if a signal did it */
1048 if (signal_pending(current)) {
1049 ret = -ERESTARTSYS;
1050 break;
1053 cprev = cnow;
1056 current->state = TASK_RUNNING;
1057 remove_wait_queue(&port->delta_msr_wait, &wait);
1059 return ret;
1063 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1064 * Return: write counters to the user passed counter struct
1065 * NB: both 1->0 and 0->1 transitions are counted except for
1066 * RI where only 0->1 is counted.
1068 static int uart_get_icount(struct tty_struct *tty,
1069 struct serial_icounter_struct *icount)
1071 struct uart_state *state = tty->driver_data;
1072 struct uart_icount cnow;
1073 struct uart_port *uport = state->uart_port;
1075 spin_lock_irq(&uport->lock);
1076 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1077 spin_unlock_irq(&uport->lock);
1079 icount->cts = cnow.cts;
1080 icount->dsr = cnow.dsr;
1081 icount->rng = cnow.rng;
1082 icount->dcd = cnow.dcd;
1083 icount->rx = cnow.rx;
1084 icount->tx = cnow.tx;
1085 icount->frame = cnow.frame;
1086 icount->overrun = cnow.overrun;
1087 icount->parity = cnow.parity;
1088 icount->brk = cnow.brk;
1089 icount->buf_overrun = cnow.buf_overrun;
1091 return 0;
1095 * Called via sys_ioctl. We can use spin_lock_irq() here.
1097 static int
1098 uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1099 unsigned long arg)
1101 struct uart_state *state = tty->driver_data;
1102 struct tty_port *port = &state->port;
1103 void __user *uarg = (void __user *)arg;
1104 int ret = -ENOIOCTLCMD;
1108 * These ioctls don't rely on the hardware to be present.
1110 switch (cmd) {
1111 case TIOCGSERIAL:
1112 ret = uart_get_info(state, uarg);
1113 break;
1115 case TIOCSSERIAL:
1116 ret = uart_set_info(tty, state, uarg);
1117 break;
1119 case TIOCSERCONFIG:
1120 ret = uart_do_autoconfig(tty, state);
1121 break;
1123 case TIOCSERGWILD: /* obsolete */
1124 case TIOCSERSWILD: /* obsolete */
1125 ret = 0;
1126 break;
1129 if (ret != -ENOIOCTLCMD)
1130 goto out;
1132 if (tty->flags & (1 << TTY_IO_ERROR)) {
1133 ret = -EIO;
1134 goto out;
1138 * The following should only be used when hardware is present.
1140 switch (cmd) {
1141 case TIOCMIWAIT:
1142 ret = uart_wait_modem_status(state, arg);
1143 break;
1146 if (ret != -ENOIOCTLCMD)
1147 goto out;
1149 mutex_lock(&port->mutex);
1151 if (tty->flags & (1 << TTY_IO_ERROR)) {
1152 ret = -EIO;
1153 goto out_up;
1157 * All these rely on hardware being present and need to be
1158 * protected against the tty being hung up.
1160 switch (cmd) {
1161 case TIOCSERGETLSR: /* Get line status register */
1162 ret = uart_get_lsr_info(tty, state, uarg);
1163 break;
1165 default: {
1166 struct uart_port *uport = state->uart_port;
1167 if (uport->ops->ioctl)
1168 ret = uport->ops->ioctl(uport, cmd, arg);
1169 break;
1172 out_up:
1173 mutex_unlock(&port->mutex);
1174 out:
1175 return ret;
1178 static void uart_set_ldisc(struct tty_struct *tty)
1180 struct uart_state *state = tty->driver_data;
1181 struct uart_port *uport = state->uart_port;
1183 if (uport->ops->set_ldisc)
1184 uport->ops->set_ldisc(uport, tty->termios->c_line);
1187 static void uart_set_termios(struct tty_struct *tty,
1188 struct ktermios *old_termios)
1190 struct uart_state *state = tty->driver_data;
1191 unsigned long flags;
1192 unsigned int cflag = tty->termios->c_cflag;
1196 * These are the bits that are used to setup various
1197 * flags in the low level driver. We can ignore the Bfoo
1198 * bits in c_cflag; c_[io]speed will always be set
1199 * appropriately by set_termios() in tty_ioctl.c
1201 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1202 if ((cflag ^ old_termios->c_cflag) == 0 &&
1203 tty->termios->c_ospeed == old_termios->c_ospeed &&
1204 tty->termios->c_ispeed == old_termios->c_ispeed &&
1205 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1206 return;
1209 uart_change_speed(tty, state, old_termios);
1211 /* Handle transition to B0 status */
1212 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1213 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1214 /* Handle transition away from B0 status */
1215 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1216 unsigned int mask = TIOCM_DTR;
1217 if (!(cflag & CRTSCTS) ||
1218 !test_bit(TTY_THROTTLED, &tty->flags))
1219 mask |= TIOCM_RTS;
1220 uart_set_mctrl(state->uart_port, mask);
1223 /* Handle turning off CRTSCTS */
1224 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1225 spin_lock_irqsave(&state->uart_port->lock, flags);
1226 tty->hw_stopped = 0;
1227 __uart_start(tty);
1228 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1230 /* Handle turning on CRTSCTS */
1231 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1232 spin_lock_irqsave(&state->uart_port->lock, flags);
1233 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1234 tty->hw_stopped = 1;
1235 state->uart_port->ops->stop_tx(state->uart_port);
1237 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1242 * In 2.4.5, calls to this will be serialized via the BKL in
1243 * linux/drivers/char/tty_io.c:tty_release()
1244 * linux/drivers/char/tty_io.c:do_tty_handup()
1246 static void uart_close(struct tty_struct *tty, struct file *filp)
1248 struct uart_state *state = tty->driver_data;
1249 struct tty_port *port;
1250 struct uart_port *uport;
1251 unsigned long flags;
1253 if (!state)
1254 return;
1256 uport = state->uart_port;
1257 port = &state->port;
1259 pr_debug("uart_close(%d) called\n", uport->line);
1261 spin_lock_irqsave(&port->lock, flags);
1263 if (tty_hung_up_p(filp)) {
1264 spin_unlock_irqrestore(&port->lock, flags);
1265 goto done;
1268 if ((tty->count == 1) && (port->count != 1)) {
1270 * Uh, oh. tty->count is 1, which means that the tty
1271 * structure will be freed. port->count should always
1272 * be one in these conditions. If it's greater than
1273 * one, we've got real problems, since it means the
1274 * serial port won't be shutdown.
1276 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1277 "port->count is %d\n", port->count);
1278 port->count = 1;
1280 if (--port->count < 0) {
1281 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1282 tty->name, port->count);
1283 port->count = 0;
1285 if (port->count) {
1286 spin_unlock_irqrestore(&port->lock, flags);
1287 goto done;
1291 * Now we wait for the transmit buffer to clear; and we notify
1292 * the line discipline to only process XON/XOFF characters by
1293 * setting tty->closing.
1295 set_bit(ASYNCB_CLOSING, &port->flags);
1296 tty->closing = 1;
1297 spin_unlock_irqrestore(&port->lock, flags);
1299 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1300 tty_wait_until_sent_from_close(tty,
1301 msecs_to_jiffies(port->closing_wait));
1304 * At this point, we stop accepting input. To do this, we
1305 * disable the receive line status interrupts.
1307 if (port->flags & ASYNC_INITIALIZED) {
1308 unsigned long flags;
1309 spin_lock_irqsave(&uport->lock, flags);
1310 uport->ops->stop_rx(uport);
1311 spin_unlock_irqrestore(&uport->lock, flags);
1313 * Before we drop DTR, make sure the UART transmitter
1314 * has completely drained; this is especially
1315 * important if there is a transmit FIFO!
1317 uart_wait_until_sent(tty, uport->timeout);
1320 mutex_lock(&port->mutex);
1321 uart_shutdown(tty, state);
1322 uart_flush_buffer(tty);
1324 tty_ldisc_flush(tty);
1326 tty_port_tty_set(port, NULL);
1327 spin_lock_irqsave(&port->lock, flags);
1328 tty->closing = 0;
1330 if (port->blocked_open) {
1331 spin_unlock_irqrestore(&port->lock, flags);
1332 if (port->close_delay)
1333 msleep_interruptible(port->close_delay);
1334 spin_lock_irqsave(&port->lock, flags);
1335 } else if (!uart_console(uport)) {
1336 spin_unlock_irqrestore(&port->lock, flags);
1337 uart_change_pm(state, 3);
1338 spin_lock_irqsave(&port->lock, flags);
1342 * Wake up anyone trying to open this port.
1344 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1345 clear_bit(ASYNCB_CLOSING, &port->flags);
1346 spin_unlock_irqrestore(&port->lock, flags);
1347 wake_up_interruptible(&port->open_wait);
1348 wake_up_interruptible(&port->close_wait);
1350 done:
1351 mutex_unlock(&port->mutex);
1354 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1356 struct uart_state *state = tty->driver_data;
1357 struct uart_port *port = state->uart_port;
1358 unsigned long char_time, expire;
1360 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1361 return;
1364 * Set the check interval to be 1/5 of the estimated time to
1365 * send a single character, and make it at least 1. The check
1366 * interval should also be less than the timeout.
1368 * Note: we have to use pretty tight timings here to satisfy
1369 * the NIST-PCTS.
1371 char_time = (port->timeout - HZ/50) / port->fifosize;
1372 char_time = char_time / 5;
1373 if (char_time == 0)
1374 char_time = 1;
1375 if (timeout && timeout < char_time)
1376 char_time = timeout;
1379 * If the transmitter hasn't cleared in twice the approximate
1380 * amount of time to send the entire FIFO, it probably won't
1381 * ever clear. This assumes the UART isn't doing flow
1382 * control, which is currently the case. Hence, if it ever
1383 * takes longer than port->timeout, this is probably due to a
1384 * UART bug of some kind. So, we clamp the timeout parameter at
1385 * 2*port->timeout.
1387 if (timeout == 0 || timeout > 2 * port->timeout)
1388 timeout = 2 * port->timeout;
1390 expire = jiffies + timeout;
1392 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1393 port->line, jiffies, expire);
1396 * Check whether the transmitter is empty every 'char_time'.
1397 * 'timeout' / 'expire' give us the maximum amount of time
1398 * we wait.
1400 while (!port->ops->tx_empty(port)) {
1401 msleep_interruptible(jiffies_to_msecs(char_time));
1402 if (signal_pending(current))
1403 break;
1404 if (time_after(jiffies, expire))
1405 break;
1410 * This is called with the BKL held in
1411 * linux/drivers/char/tty_io.c:do_tty_hangup()
1412 * We're called from the eventd thread, so we can sleep for
1413 * a _short_ time only.
1415 static void uart_hangup(struct tty_struct *tty)
1417 struct uart_state *state = tty->driver_data;
1418 struct tty_port *port = &state->port;
1419 unsigned long flags;
1421 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1423 mutex_lock(&port->mutex);
1424 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1425 uart_flush_buffer(tty);
1426 uart_shutdown(tty, state);
1427 spin_lock_irqsave(&port->lock, flags);
1428 port->count = 0;
1429 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1430 spin_unlock_irqrestore(&port->lock, flags);
1431 tty_port_tty_set(port, NULL);
1432 wake_up_interruptible(&port->open_wait);
1433 wake_up_interruptible(&port->delta_msr_wait);
1435 mutex_unlock(&port->mutex);
1438 static int uart_carrier_raised(struct tty_port *port)
1440 struct uart_state *state = container_of(port, struct uart_state, port);
1441 struct uart_port *uport = state->uart_port;
1442 int mctrl;
1443 spin_lock_irq(&uport->lock);
1444 uport->ops->enable_ms(uport);
1445 mctrl = uport->ops->get_mctrl(uport);
1446 spin_unlock_irq(&uport->lock);
1447 if (mctrl & TIOCM_CAR)
1448 return 1;
1449 return 0;
1452 static void uart_dtr_rts(struct tty_port *port, int onoff)
1454 struct uart_state *state = container_of(port, struct uart_state, port);
1455 struct uart_port *uport = state->uart_port;
1457 if (onoff)
1458 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1459 else
1460 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1463 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1465 struct uart_state *state;
1466 struct tty_port *port;
1467 int ret = 0;
1469 state = drv->state + line;
1470 port = &state->port;
1471 if (mutex_lock_interruptible(&port->mutex)) {
1472 ret = -ERESTARTSYS;
1473 goto err;
1476 port->count++;
1477 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1478 ret = -ENXIO;
1479 goto err_unlock;
1481 return state;
1483 err_unlock:
1484 port->count--;
1485 mutex_unlock(&port->mutex);
1486 err:
1487 return ERR_PTR(ret);
1491 * calls to uart_open are serialised by the BKL in
1492 * fs/char_dev.c:chrdev_open()
1493 * Note that if this fails, then uart_close() _will_ be called.
1495 * In time, we want to scrap the "opening nonpresent ports"
1496 * behaviour and implement an alternative way for setserial
1497 * to set base addresses/ports/types. This will allow us to
1498 * get rid of a certain amount of extra tests.
1500 static int uart_open(struct tty_struct *tty, struct file *filp)
1502 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1503 struct uart_state *state;
1504 struct tty_port *port;
1505 int retval, line = tty->index;
1507 pr_debug("uart_open(%d) called\n", line);
1510 * We take the semaphore inside uart_get to guarantee that we won't
1511 * be re-entered while allocating the state structure, or while we
1512 * request any IRQs that the driver may need. This also has the nice
1513 * side-effect that it delays the action of uart_hangup, so we can
1514 * guarantee that state->port.tty will always contain something
1515 * reasonable.
1517 state = uart_get(drv, line);
1518 if (IS_ERR(state)) {
1519 retval = PTR_ERR(state);
1520 goto fail;
1522 port = &state->port;
1525 * Once we set tty->driver_data here, we are guaranteed that
1526 * uart_close() will decrement the driver module use count.
1527 * Any failures from here onwards should not touch the count.
1529 tty->driver_data = state;
1530 state->uart_port->state = state;
1531 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1532 tty->alt_speed = 0;
1533 tty_port_tty_set(port, tty);
1536 * If the port is in the middle of closing, bail out now.
1538 if (tty_hung_up_p(filp)) {
1539 retval = -EAGAIN;
1540 port->count--;
1541 mutex_unlock(&port->mutex);
1542 goto fail;
1546 * Make sure the device is in D0 state.
1548 if (port->count == 1)
1549 uart_change_pm(state, 0);
1552 * Start up the serial port.
1554 retval = uart_startup(tty, state, 0);
1557 * If we succeeded, wait until the port is ready.
1559 mutex_unlock(&port->mutex);
1560 if (retval == 0)
1561 retval = tty_port_block_til_ready(port, tty, filp);
1563 fail:
1564 return retval;
1567 static const char *uart_type(struct uart_port *port)
1569 const char *str = NULL;
1571 if (port->ops->type)
1572 str = port->ops->type(port);
1574 if (!str)
1575 str = "unknown";
1577 return str;
1580 #ifdef CONFIG_PROC_FS
1582 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1584 struct uart_state *state = drv->state + i;
1585 struct tty_port *port = &state->port;
1586 int pm_state;
1587 struct uart_port *uport = state->uart_port;
1588 char stat_buf[32];
1589 unsigned int status;
1590 int mmio;
1592 if (!uport)
1593 return;
1595 mmio = uport->iotype >= UPIO_MEM;
1596 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1597 uport->line, uart_type(uport),
1598 mmio ? "mmio:0x" : "port:",
1599 mmio ? (unsigned long long)uport->mapbase
1600 : (unsigned long long)uport->iobase,
1601 uport->irq);
1603 if (uport->type == PORT_UNKNOWN) {
1604 seq_putc(m, '\n');
1605 return;
1608 if (capable(CAP_SYS_ADMIN)) {
1609 mutex_lock(&port->mutex);
1610 pm_state = state->pm_state;
1611 if (pm_state)
1612 uart_change_pm(state, 0);
1613 spin_lock_irq(&uport->lock);
1614 status = uport->ops->get_mctrl(uport);
1615 spin_unlock_irq(&uport->lock);
1616 if (pm_state)
1617 uart_change_pm(state, pm_state);
1618 mutex_unlock(&port->mutex);
1620 seq_printf(m, " tx:%d rx:%d",
1621 uport->icount.tx, uport->icount.rx);
1622 if (uport->icount.frame)
1623 seq_printf(m, " fe:%d",
1624 uport->icount.frame);
1625 if (uport->icount.parity)
1626 seq_printf(m, " pe:%d",
1627 uport->icount.parity);
1628 if (uport->icount.brk)
1629 seq_printf(m, " brk:%d",
1630 uport->icount.brk);
1631 if (uport->icount.overrun)
1632 seq_printf(m, " oe:%d",
1633 uport->icount.overrun);
1635 #define INFOBIT(bit, str) \
1636 if (uport->mctrl & (bit)) \
1637 strncat(stat_buf, (str), sizeof(stat_buf) - \
1638 strlen(stat_buf) - 2)
1639 #define STATBIT(bit, str) \
1640 if (status & (bit)) \
1641 strncat(stat_buf, (str), sizeof(stat_buf) - \
1642 strlen(stat_buf) - 2)
1644 stat_buf[0] = '\0';
1645 stat_buf[1] = '\0';
1646 INFOBIT(TIOCM_RTS, "|RTS");
1647 STATBIT(TIOCM_CTS, "|CTS");
1648 INFOBIT(TIOCM_DTR, "|DTR");
1649 STATBIT(TIOCM_DSR, "|DSR");
1650 STATBIT(TIOCM_CAR, "|CD");
1651 STATBIT(TIOCM_RNG, "|RI");
1652 if (stat_buf[0])
1653 stat_buf[0] = ' ';
1655 seq_puts(m, stat_buf);
1657 seq_putc(m, '\n');
1658 #undef STATBIT
1659 #undef INFOBIT
1662 static int uart_proc_show(struct seq_file *m, void *v)
1664 struct tty_driver *ttydrv = m->private;
1665 struct uart_driver *drv = ttydrv->driver_state;
1666 int i;
1668 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1669 "", "", "");
1670 for (i = 0; i < drv->nr; i++)
1671 uart_line_info(m, drv, i);
1672 return 0;
1675 static int uart_proc_open(struct inode *inode, struct file *file)
1677 return single_open(file, uart_proc_show, PDE(inode)->data);
1680 static const struct file_operations uart_proc_fops = {
1681 .owner = THIS_MODULE,
1682 .open = uart_proc_open,
1683 .read = seq_read,
1684 .llseek = seq_lseek,
1685 .release = single_release,
1687 #endif
1689 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1691 * uart_console_write - write a console message to a serial port
1692 * @port: the port to write the message
1693 * @s: array of characters
1694 * @count: number of characters in string to write
1695 * @write: function to write character to port
1697 void uart_console_write(struct uart_port *port, const char *s,
1698 unsigned int count,
1699 void (*putchar)(struct uart_port *, int))
1701 unsigned int i;
1703 for (i = 0; i < count; i++, s++) {
1704 if (*s == '\n')
1705 putchar(port, '\r');
1706 putchar(port, *s);
1709 EXPORT_SYMBOL_GPL(uart_console_write);
1712 * Check whether an invalid uart number has been specified, and
1713 * if so, search for the first available port that does have
1714 * console support.
1716 struct uart_port * __init
1717 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1719 int idx = co->index;
1721 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1722 ports[idx].membase == NULL))
1723 for (idx = 0; idx < nr; idx++)
1724 if (ports[idx].iobase != 0 ||
1725 ports[idx].membase != NULL)
1726 break;
1728 co->index = idx;
1730 return ports + idx;
1734 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1735 * @options: pointer to option string
1736 * @baud: pointer to an 'int' variable for the baud rate.
1737 * @parity: pointer to an 'int' variable for the parity.
1738 * @bits: pointer to an 'int' variable for the number of data bits.
1739 * @flow: pointer to an 'int' variable for the flow control character.
1741 * uart_parse_options decodes a string containing the serial console
1742 * options. The format of the string is <baud><parity><bits><flow>,
1743 * eg: 115200n8r
1745 void
1746 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1748 char *s = options;
1750 *baud = simple_strtoul(s, NULL, 10);
1751 while (*s >= '0' && *s <= '9')
1752 s++;
1753 if (*s)
1754 *parity = *s++;
1755 if (*s)
1756 *bits = *s++ - '0';
1757 if (*s)
1758 *flow = *s;
1760 EXPORT_SYMBOL_GPL(uart_parse_options);
1762 struct baud_rates {
1763 unsigned int rate;
1764 unsigned int cflag;
1767 static const struct baud_rates baud_rates[] = {
1768 { 921600, B921600 },
1769 { 460800, B460800 },
1770 { 230400, B230400 },
1771 { 115200, B115200 },
1772 { 57600, B57600 },
1773 { 38400, B38400 },
1774 { 19200, B19200 },
1775 { 9600, B9600 },
1776 { 4800, B4800 },
1777 { 2400, B2400 },
1778 { 1200, B1200 },
1779 { 0, B38400 }
1783 * uart_set_options - setup the serial console parameters
1784 * @port: pointer to the serial ports uart_port structure
1785 * @co: console pointer
1786 * @baud: baud rate
1787 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1788 * @bits: number of data bits
1789 * @flow: flow control character - 'r' (rts)
1792 uart_set_options(struct uart_port *port, struct console *co,
1793 int baud, int parity, int bits, int flow)
1795 struct ktermios termios;
1796 static struct ktermios dummy;
1797 int i;
1800 * Ensure that the serial console lock is initialised
1801 * early.
1803 spin_lock_init(&port->lock);
1804 lockdep_set_class(&port->lock, &port_lock_key);
1806 memset(&termios, 0, sizeof(struct ktermios));
1808 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1811 * Construct a cflag setting.
1813 for (i = 0; baud_rates[i].rate; i++)
1814 if (baud_rates[i].rate <= baud)
1815 break;
1817 termios.c_cflag |= baud_rates[i].cflag;
1819 if (bits == 7)
1820 termios.c_cflag |= CS7;
1821 else
1822 termios.c_cflag |= CS8;
1824 switch (parity) {
1825 case 'o': case 'O':
1826 termios.c_cflag |= PARODD;
1827 /*fall through*/
1828 case 'e': case 'E':
1829 termios.c_cflag |= PARENB;
1830 break;
1833 if (flow == 'r')
1834 termios.c_cflag |= CRTSCTS;
1837 * some uarts on other side don't support no flow control.
1838 * So we set * DTR in host uart to make them happy
1840 port->mctrl |= TIOCM_DTR;
1842 port->ops->set_termios(port, &termios, &dummy);
1844 * Allow the setting of the UART parameters with a NULL console
1845 * too:
1847 if (co)
1848 co->cflag = termios.c_cflag;
1850 return 0;
1852 EXPORT_SYMBOL_GPL(uart_set_options);
1853 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1855 static void uart_change_pm(struct uart_state *state, int pm_state)
1857 struct uart_port *port = state->uart_port;
1859 if (state->pm_state != pm_state) {
1860 if (port->ops->pm)
1861 port->ops->pm(port, pm_state, state->pm_state);
1862 state->pm_state = pm_state;
1866 struct uart_match {
1867 struct uart_port *port;
1868 struct uart_driver *driver;
1871 static int serial_match_port(struct device *dev, void *data)
1873 struct uart_match *match = data;
1874 struct tty_driver *tty_drv = match->driver->tty_driver;
1875 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1876 match->port->line;
1878 return dev->devt == devt; /* Actually, only one tty per port */
1881 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1883 struct uart_state *state = drv->state + uport->line;
1884 struct tty_port *port = &state->port;
1885 struct device *tty_dev;
1886 struct uart_match match = {uport, drv};
1888 mutex_lock(&port->mutex);
1890 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1891 if (device_may_wakeup(tty_dev)) {
1892 if (!enable_irq_wake(uport->irq))
1893 uport->irq_wake = 1;
1894 put_device(tty_dev);
1895 mutex_unlock(&port->mutex);
1896 return 0;
1898 if (console_suspend_enabled || !uart_console(uport))
1899 uport->suspended = 1;
1901 if (port->flags & ASYNC_INITIALIZED) {
1902 const struct uart_ops *ops = uport->ops;
1903 int tries;
1905 if (console_suspend_enabled || !uart_console(uport)) {
1906 set_bit(ASYNCB_SUSPENDED, &port->flags);
1907 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1909 spin_lock_irq(&uport->lock);
1910 ops->stop_tx(uport);
1911 ops->set_mctrl(uport, 0);
1912 ops->stop_rx(uport);
1913 spin_unlock_irq(&uport->lock);
1917 * Wait for the transmitter to empty.
1919 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1920 msleep(10);
1921 if (!tries)
1922 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1923 "transmitter\n",
1924 uport->dev ? dev_name(uport->dev) : "",
1925 uport->dev ? ": " : "",
1926 drv->dev_name,
1927 drv->tty_driver->name_base + uport->line);
1929 if (console_suspend_enabled || !uart_console(uport))
1930 ops->shutdown(uport);
1934 * Disable the console device before suspending.
1936 if (console_suspend_enabled && uart_console(uport))
1937 console_stop(uport->cons);
1939 if (console_suspend_enabled || !uart_console(uport))
1940 uart_change_pm(state, 3);
1942 mutex_unlock(&port->mutex);
1944 return 0;
1947 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1949 struct uart_state *state = drv->state + uport->line;
1950 struct tty_port *port = &state->port;
1951 struct device *tty_dev;
1952 struct uart_match match = {uport, drv};
1953 struct ktermios termios;
1955 mutex_lock(&port->mutex);
1957 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1958 if (!uport->suspended && device_may_wakeup(tty_dev)) {
1959 if (uport->irq_wake) {
1960 disable_irq_wake(uport->irq);
1961 uport->irq_wake = 0;
1963 mutex_unlock(&port->mutex);
1964 return 0;
1966 uport->suspended = 0;
1969 * Re-enable the console device after suspending.
1971 if (uart_console(uport)) {
1973 * First try to use the console cflag setting.
1975 memset(&termios, 0, sizeof(struct ktermios));
1976 termios.c_cflag = uport->cons->cflag;
1979 * If that's unset, use the tty termios setting.
1981 if (port->tty && port->tty->termios && termios.c_cflag == 0)
1982 termios = *(port->tty->termios);
1984 uport->ops->set_termios(uport, &termios, NULL);
1985 if (console_suspend_enabled)
1986 console_start(uport->cons);
1989 if (port->flags & ASYNC_SUSPENDED) {
1990 const struct uart_ops *ops = uport->ops;
1991 int ret;
1993 uart_change_pm(state, 0);
1994 spin_lock_irq(&uport->lock);
1995 ops->set_mctrl(uport, 0);
1996 spin_unlock_irq(&uport->lock);
1997 if (console_suspend_enabled || !uart_console(uport)) {
1998 /* Protected by port mutex for now */
1999 struct tty_struct *tty = port->tty;
2000 ret = ops->startup(uport);
2001 if (ret == 0) {
2002 if (tty)
2003 uart_change_speed(tty, state, NULL);
2004 spin_lock_irq(&uport->lock);
2005 ops->set_mctrl(uport, uport->mctrl);
2006 ops->start_tx(uport);
2007 spin_unlock_irq(&uport->lock);
2008 set_bit(ASYNCB_INITIALIZED, &port->flags);
2009 } else {
2011 * Failed to resume - maybe hardware went away?
2012 * Clear the "initialized" flag so we won't try
2013 * to call the low level drivers shutdown method.
2015 uart_shutdown(tty, state);
2019 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2022 mutex_unlock(&port->mutex);
2024 return 0;
2027 static inline void
2028 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2030 char address[64];
2032 switch (port->iotype) {
2033 case UPIO_PORT:
2034 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2035 break;
2036 case UPIO_HUB6:
2037 snprintf(address, sizeof(address),
2038 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2039 break;
2040 case UPIO_MEM:
2041 case UPIO_MEM32:
2042 case UPIO_AU:
2043 case UPIO_TSI:
2044 snprintf(address, sizeof(address),
2045 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2046 break;
2047 default:
2048 strlcpy(address, "*unknown*", sizeof(address));
2049 break;
2052 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2053 port->dev ? dev_name(port->dev) : "",
2054 port->dev ? ": " : "",
2055 drv->dev_name,
2056 drv->tty_driver->name_base + port->line,
2057 address, port->irq, uart_type(port));
2060 static void
2061 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2062 struct uart_port *port)
2064 unsigned int flags;
2067 * If there isn't a port here, don't do anything further.
2069 if (!port->iobase && !port->mapbase && !port->membase)
2070 return;
2073 * Now do the auto configuration stuff. Note that config_port
2074 * is expected to claim the resources and map the port for us.
2076 flags = 0;
2077 if (port->flags & UPF_AUTO_IRQ)
2078 flags |= UART_CONFIG_IRQ;
2079 if (port->flags & UPF_BOOT_AUTOCONF) {
2080 if (!(port->flags & UPF_FIXED_TYPE)) {
2081 port->type = PORT_UNKNOWN;
2082 flags |= UART_CONFIG_TYPE;
2084 port->ops->config_port(port, flags);
2087 if (port->type != PORT_UNKNOWN) {
2088 unsigned long flags;
2090 uart_report_port(drv, port);
2092 /* Power up port for set_mctrl() */
2093 uart_change_pm(state, 0);
2096 * Ensure that the modem control lines are de-activated.
2097 * keep the DTR setting that is set in uart_set_options()
2098 * We probably don't need a spinlock around this, but
2100 spin_lock_irqsave(&port->lock, flags);
2101 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2102 spin_unlock_irqrestore(&port->lock, flags);
2105 * If this driver supports console, and it hasn't been
2106 * successfully registered yet, try to re-register it.
2107 * It may be that the port was not available.
2109 if (port->cons && !(port->cons->flags & CON_ENABLED))
2110 register_console(port->cons);
2113 * Power down all ports by default, except the
2114 * console if we have one.
2116 if (!uart_console(port))
2117 uart_change_pm(state, 3);
2121 #ifdef CONFIG_CONSOLE_POLL
2123 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2125 struct uart_driver *drv = driver->driver_state;
2126 struct uart_state *state = drv->state + line;
2127 struct uart_port *port;
2128 int baud = 9600;
2129 int bits = 8;
2130 int parity = 'n';
2131 int flow = 'n';
2133 if (!state || !state->uart_port)
2134 return -1;
2136 port = state->uart_port;
2137 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2138 return -1;
2140 if (options) {
2141 uart_parse_options(options, &baud, &parity, &bits, &flow);
2142 return uart_set_options(port, NULL, baud, parity, bits, flow);
2145 return 0;
2148 static int uart_poll_get_char(struct tty_driver *driver, int line)
2150 struct uart_driver *drv = driver->driver_state;
2151 struct uart_state *state = drv->state + line;
2152 struct uart_port *port;
2154 if (!state || !state->uart_port)
2155 return -1;
2157 port = state->uart_port;
2158 return port->ops->poll_get_char(port);
2161 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2163 struct uart_driver *drv = driver->driver_state;
2164 struct uart_state *state = drv->state + line;
2165 struct uart_port *port;
2167 if (!state || !state->uart_port)
2168 return;
2170 port = state->uart_port;
2171 port->ops->poll_put_char(port, ch);
2173 #endif
2175 static const struct tty_operations uart_ops = {
2176 .open = uart_open,
2177 .close = uart_close,
2178 .write = uart_write,
2179 .put_char = uart_put_char,
2180 .flush_chars = uart_flush_chars,
2181 .write_room = uart_write_room,
2182 .chars_in_buffer= uart_chars_in_buffer,
2183 .flush_buffer = uart_flush_buffer,
2184 .ioctl = uart_ioctl,
2185 .throttle = uart_throttle,
2186 .unthrottle = uart_unthrottle,
2187 .send_xchar = uart_send_xchar,
2188 .set_termios = uart_set_termios,
2189 .set_ldisc = uart_set_ldisc,
2190 .stop = uart_stop,
2191 .start = uart_start,
2192 .hangup = uart_hangup,
2193 .break_ctl = uart_break_ctl,
2194 .wait_until_sent= uart_wait_until_sent,
2195 #ifdef CONFIG_PROC_FS
2196 .proc_fops = &uart_proc_fops,
2197 #endif
2198 .tiocmget = uart_tiocmget,
2199 .tiocmset = uart_tiocmset,
2200 .get_icount = uart_get_icount,
2201 #ifdef CONFIG_CONSOLE_POLL
2202 .poll_init = uart_poll_init,
2203 .poll_get_char = uart_poll_get_char,
2204 .poll_put_char = uart_poll_put_char,
2205 #endif
2208 static const struct tty_port_operations uart_port_ops = {
2209 .carrier_raised = uart_carrier_raised,
2210 .dtr_rts = uart_dtr_rts,
2214 * uart_register_driver - register a driver with the uart core layer
2215 * @drv: low level driver structure
2217 * Register a uart driver with the core driver. We in turn register
2218 * with the tty layer, and initialise the core driver per-port state.
2220 * We have a proc file in /proc/tty/driver which is named after the
2221 * normal driver.
2223 * drv->port should be NULL, and the per-port structures should be
2224 * registered using uart_add_one_port after this call has succeeded.
2226 int uart_register_driver(struct uart_driver *drv)
2228 struct tty_driver *normal;
2229 int i, retval;
2231 BUG_ON(drv->state);
2234 * Maybe we should be using a slab cache for this, especially if
2235 * we have a large number of ports to handle.
2237 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2238 if (!drv->state)
2239 goto out;
2241 normal = alloc_tty_driver(drv->nr);
2242 if (!normal)
2243 goto out_kfree;
2245 drv->tty_driver = normal;
2247 normal->owner = drv->owner;
2248 normal->driver_name = drv->driver_name;
2249 normal->name = drv->dev_name;
2250 normal->major = drv->major;
2251 normal->minor_start = drv->minor;
2252 normal->type = TTY_DRIVER_TYPE_SERIAL;
2253 normal->subtype = SERIAL_TYPE_NORMAL;
2254 normal->init_termios = tty_std_termios;
2255 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2256 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2257 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2258 normal->driver_state = drv;
2259 tty_set_operations(normal, &uart_ops);
2262 * Initialise the UART state(s).
2264 for (i = 0; i < drv->nr; i++) {
2265 struct uart_state *state = drv->state + i;
2266 struct tty_port *port = &state->port;
2268 tty_port_init(port);
2269 port->ops = &uart_port_ops;
2270 port->close_delay = 500; /* .5 seconds */
2271 port->closing_wait = 30000; /* 30 seconds */
2274 retval = tty_register_driver(normal);
2275 if (retval >= 0)
2276 return retval;
2278 put_tty_driver(normal);
2279 out_kfree:
2280 kfree(drv->state);
2281 out:
2282 return -ENOMEM;
2286 * uart_unregister_driver - remove a driver from the uart core layer
2287 * @drv: low level driver structure
2289 * Remove all references to a driver from the core driver. The low
2290 * level driver must have removed all its ports via the
2291 * uart_remove_one_port() if it registered them with uart_add_one_port().
2292 * (ie, drv->port == NULL)
2294 void uart_unregister_driver(struct uart_driver *drv)
2296 struct tty_driver *p = drv->tty_driver;
2297 tty_unregister_driver(p);
2298 put_tty_driver(p);
2299 kfree(drv->state);
2300 drv->tty_driver = NULL;
2303 struct tty_driver *uart_console_device(struct console *co, int *index)
2305 struct uart_driver *p = co->data;
2306 *index = co->index;
2307 return p->tty_driver;
2311 * uart_add_one_port - attach a driver-defined port structure
2312 * @drv: pointer to the uart low level driver structure for this port
2313 * @uport: uart port structure to use for this port.
2315 * This allows the driver to register its own uart_port structure
2316 * with the core driver. The main purpose is to allow the low
2317 * level uart drivers to expand uart_port, rather than having yet
2318 * more levels of structures.
2320 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2322 struct uart_state *state;
2323 struct tty_port *port;
2324 int ret = 0;
2325 struct device *tty_dev;
2327 BUG_ON(in_interrupt());
2329 if (uport->line >= drv->nr)
2330 return -EINVAL;
2332 state = drv->state + uport->line;
2333 port = &state->port;
2335 mutex_lock(&port_mutex);
2336 mutex_lock(&port->mutex);
2337 if (state->uart_port) {
2338 ret = -EINVAL;
2339 goto out;
2342 state->uart_port = uport;
2343 state->pm_state = -1;
2345 uport->cons = drv->cons;
2346 uport->state = state;
2349 * If this port is a console, then the spinlock is already
2350 * initialised.
2352 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2353 spin_lock_init(&uport->lock);
2354 lockdep_set_class(&uport->lock, &port_lock_key);
2357 uart_configure_port(drv, state, uport);
2360 * Register the port whether it's detected or not. This allows
2361 * setserial to be used to alter this ports parameters.
2363 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2364 if (likely(!IS_ERR(tty_dev))) {
2365 device_init_wakeup(tty_dev, 1);
2366 device_set_wakeup_enable(tty_dev, 0);
2367 } else
2368 printk(KERN_ERR "Cannot register tty device on line %d\n",
2369 uport->line);
2372 * Ensure UPF_DEAD is not set.
2374 uport->flags &= ~UPF_DEAD;
2376 out:
2377 mutex_unlock(&port->mutex);
2378 mutex_unlock(&port_mutex);
2380 return ret;
2384 * uart_remove_one_port - detach a driver defined port structure
2385 * @drv: pointer to the uart low level driver structure for this port
2386 * @uport: uart port structure for this port
2388 * This unhooks (and hangs up) the specified port structure from the
2389 * core driver. No further calls will be made to the low-level code
2390 * for this port.
2392 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2394 struct uart_state *state = drv->state + uport->line;
2395 struct tty_port *port = &state->port;
2397 BUG_ON(in_interrupt());
2399 if (state->uart_port != uport)
2400 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2401 state->uart_port, uport);
2403 mutex_lock(&port_mutex);
2406 * Mark the port "dead" - this prevents any opens from
2407 * succeeding while we shut down the port.
2409 mutex_lock(&port->mutex);
2410 uport->flags |= UPF_DEAD;
2411 mutex_unlock(&port->mutex);
2414 * Remove the devices from the tty layer
2416 tty_unregister_device(drv->tty_driver, uport->line);
2418 if (port->tty)
2419 tty_vhangup(port->tty);
2422 * Free the port IO and memory resources, if any.
2424 if (uport->type != PORT_UNKNOWN)
2425 uport->ops->release_port(uport);
2428 * Indicate that there isn't a port here anymore.
2430 uport->type = PORT_UNKNOWN;
2432 state->uart_port = NULL;
2433 mutex_unlock(&port_mutex);
2435 return 0;
2439 * Are the two ports equivalent?
2441 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2443 if (port1->iotype != port2->iotype)
2444 return 0;
2446 switch (port1->iotype) {
2447 case UPIO_PORT:
2448 return (port1->iobase == port2->iobase);
2449 case UPIO_HUB6:
2450 return (port1->iobase == port2->iobase) &&
2451 (port1->hub6 == port2->hub6);
2452 case UPIO_MEM:
2453 case UPIO_MEM32:
2454 case UPIO_AU:
2455 case UPIO_TSI:
2456 return (port1->mapbase == port2->mapbase);
2458 return 0;
2460 EXPORT_SYMBOL(uart_match_port);
2462 EXPORT_SYMBOL(uart_write_wakeup);
2463 EXPORT_SYMBOL(uart_register_driver);
2464 EXPORT_SYMBOL(uart_unregister_driver);
2465 EXPORT_SYMBOL(uart_suspend_port);
2466 EXPORT_SYMBOL(uart_resume_port);
2467 EXPORT_SYMBOL(uart_add_one_port);
2468 EXPORT_SYMBOL(uart_remove_one_port);
2470 MODULE_DESCRIPTION("Serial driver core");
2471 MODULE_LICENSE("GPL");