serial: core, move termios handling to uart_startup
[zen-stable.git] / drivers / tty / serial / serial_core.c
blob47657cf4f8b953d2dc23ec41ad1adc180ce27d93
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/module.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/serial_core.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
42 * This is used to lock changes in serial line configuration.
44 static DEFINE_MUTEX(port_mutex);
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
50 static struct lock_class_key port_lock_key;
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 #ifdef CONFIG_SERIAL_CORE_CONSOLE
55 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
56 #else
57 #define uart_console(port) (0)
58 #endif
60 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
61 struct ktermios *old_termios);
62 static void __uart_wait_until_sent(struct uart_port *port, 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_state *state = port->state;
73 * This means you called this function _after_ the port was
74 * closed. No cookie for you.
76 BUG_ON(!state);
77 tasklet_schedule(&state->tlet);
80 static void uart_stop(struct tty_struct *tty)
82 struct uart_state *state = tty->driver_data;
83 struct uart_port *port = state->uart_port;
84 unsigned long flags;
86 spin_lock_irqsave(&port->lock, flags);
87 port->ops->stop_tx(port);
88 spin_unlock_irqrestore(&port->lock, flags);
91 static void __uart_start(struct tty_struct *tty)
93 struct uart_state *state = tty->driver_data;
94 struct uart_port *port = state->uart_port;
96 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
97 !tty->stopped && !tty->hw_stopped)
98 port->ops->start_tx(port);
101 static void uart_start(struct tty_struct *tty)
103 struct uart_state *state = tty->driver_data;
104 struct uart_port *port = state->uart_port;
105 unsigned long flags;
107 spin_lock_irqsave(&port->lock, flags);
108 __uart_start(tty);
109 spin_unlock_irqrestore(&port->lock, flags);
112 static void uart_tasklet_action(unsigned long data)
114 struct uart_state *state = (struct uart_state *)data;
115 tty_wakeup(state->port.tty);
118 static inline void
119 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
121 unsigned long flags;
122 unsigned int old;
124 spin_lock_irqsave(&port->lock, flags);
125 old = port->mctrl;
126 port->mctrl = (old & ~clear) | set;
127 if (old != port->mctrl)
128 port->ops->set_mctrl(port, port->mctrl);
129 spin_unlock_irqrestore(&port->lock, flags);
132 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
133 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
136 * Startup the port. This will be called once per open. All calls
137 * will be serialised by the per-port mutex.
139 static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
141 struct uart_port *uport = state->uart_port;
142 struct tty_port *port = &state->port;
143 unsigned long page;
144 int retval = 0;
146 if (port->flags & ASYNC_INITIALIZED)
147 return 0;
150 * Set the TTY IO error marker - we will only clear this
151 * once we have successfully opened the port. Also set
152 * up the tty->alt_speed kludge
154 set_bit(TTY_IO_ERROR, &tty->flags);
156 if (uport->type == PORT_UNKNOWN)
157 return 0;
160 * Initialise and allocate the transmit and temporary
161 * buffer.
163 if (!state->xmit.buf) {
164 /* This is protected by the per port mutex */
165 page = get_zeroed_page(GFP_KERNEL);
166 if (!page)
167 return -ENOMEM;
169 state->xmit.buf = (unsigned char *) page;
170 uart_circ_clear(&state->xmit);
173 retval = uport->ops->startup(uport);
174 if (retval == 0) {
175 if (uart_console(uport) && uport->cons->cflag) {
176 tty->termios->c_cflag = uport->cons->cflag;
177 uport->cons->cflag = 0;
180 * Initialise the hardware port settings.
182 uart_change_speed(tty, state, NULL);
184 if (init_hw) {
186 * Setup the RTS and DTR signals once the
187 * port is open and ready to respond.
189 if (tty->termios->c_cflag & CBAUD)
190 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
193 if (port->flags & ASYNC_CTS_FLOW) {
194 spin_lock_irq(&uport->lock);
195 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
196 tty->hw_stopped = 1;
197 spin_unlock_irq(&uport->lock);
200 set_bit(ASYNCB_INITIALIZED, &port->flags);
202 clear_bit(TTY_IO_ERROR, &tty->flags);
205 if (retval && capable(CAP_SYS_ADMIN))
206 retval = 0;
208 return retval;
212 * This routine will shutdown a serial port; interrupts are disabled, and
213 * DTR is dropped if the hangup on close termio flag is on. Calls to
214 * uart_shutdown are serialised by the per-port semaphore.
216 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
218 struct uart_port *uport = state->uart_port;
219 struct tty_port *port = &state->port;
222 * Set the TTY IO error marker
224 if (tty)
225 set_bit(TTY_IO_ERROR, &tty->flags);
227 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
229 * Turn off DTR and RTS early.
231 if (!tty || (tty->termios->c_cflag & HUPCL))
232 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
235 * clear delta_msr_wait queue to avoid mem leaks: we may free
236 * the irq here so the queue might never be woken up. Note
237 * that we won't end up waiting on delta_msr_wait again since
238 * any outstanding file descriptors should be pointing at
239 * hung_up_tty_fops now.
241 wake_up_interruptible(&port->delta_msr_wait);
244 * Free the IRQ and disable the port.
246 uport->ops->shutdown(uport);
249 * Ensure that the IRQ handler isn't running on another CPU.
251 synchronize_irq(uport->irq);
255 * kill off our tasklet
257 tasklet_kill(&state->tlet);
260 * Free the transmit buffer page.
262 if (state->xmit.buf) {
263 free_page((unsigned long)state->xmit.buf);
264 state->xmit.buf = NULL;
269 * uart_update_timeout - update per-port FIFO timeout.
270 * @port: uart_port structure describing the port
271 * @cflag: termios cflag value
272 * @baud: speed of the port
274 * Set the port FIFO timeout value. The @cflag value should
275 * reflect the actual hardware settings.
277 void
278 uart_update_timeout(struct uart_port *port, unsigned int cflag,
279 unsigned int baud)
281 unsigned int bits;
283 /* byte size and parity */
284 switch (cflag & CSIZE) {
285 case CS5:
286 bits = 7;
287 break;
288 case CS6:
289 bits = 8;
290 break;
291 case CS7:
292 bits = 9;
293 break;
294 default:
295 bits = 10;
296 break; /* CS8 */
299 if (cflag & CSTOPB)
300 bits++;
301 if (cflag & PARENB)
302 bits++;
305 * The total number of bits to be transmitted in the fifo.
307 bits = bits * port->fifosize;
310 * Figure the timeout to send the above number of bits.
311 * Add .02 seconds of slop
313 port->timeout = (HZ * bits) / baud + HZ/50;
316 EXPORT_SYMBOL(uart_update_timeout);
319 * uart_get_baud_rate - return baud rate for a particular port
320 * @port: uart_port structure describing the port in question.
321 * @termios: desired termios settings.
322 * @old: old termios (or NULL)
323 * @min: minimum acceptable baud rate
324 * @max: maximum acceptable baud rate
326 * Decode the termios structure into a numeric baud rate,
327 * taking account of the magic 38400 baud rate (with spd_*
328 * flags), and mapping the %B0 rate to 9600 baud.
330 * If the new baud rate is invalid, try the old termios setting.
331 * If it's still invalid, we try 9600 baud.
333 * Update the @termios structure to reflect the baud rate
334 * we're actually going to be using. Don't do this for the case
335 * where B0 is requested ("hang up").
337 unsigned int
338 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
339 struct ktermios *old, unsigned int min, unsigned int max)
341 unsigned int try, baud, altbaud = 38400;
342 int hung_up = 0;
343 upf_t flags = port->flags & UPF_SPD_MASK;
345 if (flags == UPF_SPD_HI)
346 altbaud = 57600;
347 else if (flags == UPF_SPD_VHI)
348 altbaud = 115200;
349 else if (flags == UPF_SPD_SHI)
350 altbaud = 230400;
351 else if (flags == UPF_SPD_WARP)
352 altbaud = 460800;
354 for (try = 0; try < 2; try++) {
355 baud = tty_termios_baud_rate(termios);
358 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
359 * Die! Die! Die!
361 if (baud == 38400)
362 baud = altbaud;
365 * Special case: B0 rate.
367 if (baud == 0) {
368 hung_up = 1;
369 baud = 9600;
372 if (baud >= min && baud <= max)
373 return baud;
376 * Oops, the quotient was zero. Try again with
377 * the old baud rate if possible.
379 termios->c_cflag &= ~CBAUD;
380 if (old) {
381 baud = tty_termios_baud_rate(old);
382 if (!hung_up)
383 tty_termios_encode_baud_rate(termios,
384 baud, baud);
385 old = NULL;
386 continue;
390 * As a last resort, if the range cannot be met then clip to
391 * the nearest chip supported rate.
393 if (!hung_up) {
394 if (baud <= min)
395 tty_termios_encode_baud_rate(termios,
396 min + 1, min + 1);
397 else
398 tty_termios_encode_baud_rate(termios,
399 max - 1, max - 1);
402 /* Should never happen */
403 WARN_ON(1);
404 return 0;
407 EXPORT_SYMBOL(uart_get_baud_rate);
410 * uart_get_divisor - return uart clock divisor
411 * @port: uart_port structure describing the port.
412 * @baud: desired baud rate
414 * Calculate the uart clock divisor for the port.
416 unsigned int
417 uart_get_divisor(struct uart_port *port, unsigned int baud)
419 unsigned int quot;
422 * Old custom speed handling.
424 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
425 quot = port->custom_divisor;
426 else
427 quot = (port->uartclk + (8 * baud)) / (16 * baud);
429 return quot;
432 EXPORT_SYMBOL(uart_get_divisor);
434 /* FIXME: Consistent locking policy */
435 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
436 struct ktermios *old_termios)
438 struct tty_port *port = &state->port;
439 struct uart_port *uport = state->uart_port;
440 struct ktermios *termios;
443 * If we have no tty, termios, or the port does not exist,
444 * then we can't set the parameters for this port.
446 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
447 return;
449 termios = tty->termios;
452 * Set flags based on termios cflag
454 if (termios->c_cflag & CRTSCTS)
455 set_bit(ASYNCB_CTS_FLOW, &port->flags);
456 else
457 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
459 if (termios->c_cflag & CLOCAL)
460 clear_bit(ASYNCB_CHECK_CD, &port->flags);
461 else
462 set_bit(ASYNCB_CHECK_CD, &port->flags);
464 uport->ops->set_termios(uport, termios, old_termios);
467 static inline int __uart_put_char(struct uart_port *port,
468 struct circ_buf *circ, unsigned char c)
470 unsigned long flags;
471 int ret = 0;
473 if (!circ->buf)
474 return 0;
476 spin_lock_irqsave(&port->lock, flags);
477 if (uart_circ_chars_free(circ) != 0) {
478 circ->buf[circ->head] = c;
479 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
480 ret = 1;
482 spin_unlock_irqrestore(&port->lock, flags);
483 return ret;
486 static int uart_put_char(struct tty_struct *tty, unsigned char ch)
488 struct uart_state *state = tty->driver_data;
490 return __uart_put_char(state->uart_port, &state->xmit, ch);
493 static void uart_flush_chars(struct tty_struct *tty)
495 uart_start(tty);
498 static int uart_write(struct tty_struct *tty,
499 const unsigned char *buf, int count)
501 struct uart_state *state = tty->driver_data;
502 struct uart_port *port;
503 struct circ_buf *circ;
504 unsigned long flags;
505 int c, ret = 0;
508 * This means you called this function _after_ the port was
509 * closed. No cookie for you.
511 if (!state) {
512 WARN_ON(1);
513 return -EL3HLT;
516 port = state->uart_port;
517 circ = &state->xmit;
519 if (!circ->buf)
520 return 0;
522 spin_lock_irqsave(&port->lock, flags);
523 while (1) {
524 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
525 if (count < c)
526 c = count;
527 if (c <= 0)
528 break;
529 memcpy(circ->buf + circ->head, buf, c);
530 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
531 buf += c;
532 count -= c;
533 ret += c;
535 spin_unlock_irqrestore(&port->lock, flags);
537 uart_start(tty);
538 return ret;
541 static int uart_write_room(struct tty_struct *tty)
543 struct uart_state *state = tty->driver_data;
544 unsigned long flags;
545 int ret;
547 spin_lock_irqsave(&state->uart_port->lock, flags);
548 ret = uart_circ_chars_free(&state->xmit);
549 spin_unlock_irqrestore(&state->uart_port->lock, flags);
550 return ret;
553 static int uart_chars_in_buffer(struct tty_struct *tty)
555 struct uart_state *state = tty->driver_data;
556 unsigned long flags;
557 int ret;
559 spin_lock_irqsave(&state->uart_port->lock, flags);
560 ret = uart_circ_chars_pending(&state->xmit);
561 spin_unlock_irqrestore(&state->uart_port->lock, flags);
562 return ret;
565 static void uart_flush_buffer(struct tty_struct *tty)
567 struct uart_state *state = tty->driver_data;
568 struct uart_port *port;
569 unsigned long flags;
572 * This means you called this function _after_ the port was
573 * closed. No cookie for you.
575 if (!state) {
576 WARN_ON(1);
577 return;
580 port = state->uart_port;
581 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
583 spin_lock_irqsave(&port->lock, flags);
584 uart_circ_clear(&state->xmit);
585 if (port->ops->flush_buffer)
586 port->ops->flush_buffer(port);
587 spin_unlock_irqrestore(&port->lock, flags);
588 tty_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->uart_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);
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->uart_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->uart_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,
641 struct serial_struct __user *retinfo)
643 struct uart_port *uport = state->uart_port;
644 struct tty_port *port = &state->port;
645 struct serial_struct tmp;
647 memset(&tmp, 0, sizeof(tmp));
649 /* Ensure the state we copy is consistent and no hardware changes
650 occur as we go */
651 mutex_lock(&port->mutex);
653 tmp.type = uport->type;
654 tmp.line = uport->line;
655 tmp.port = uport->iobase;
656 if (HIGH_BITS_OFFSET)
657 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
658 tmp.irq = uport->irq;
659 tmp.flags = uport->flags;
660 tmp.xmit_fifo_size = uport->fifosize;
661 tmp.baud_base = uport->uartclk / 16;
662 tmp.close_delay = port->close_delay / 10;
663 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
664 ASYNC_CLOSING_WAIT_NONE :
665 port->closing_wait / 10;
666 tmp.custom_divisor = uport->custom_divisor;
667 tmp.hub6 = uport->hub6;
668 tmp.io_type = uport->iotype;
669 tmp.iomem_reg_shift = uport->regshift;
670 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
672 mutex_unlock(&port->mutex);
674 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
675 return -EFAULT;
676 return 0;
679 static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
680 struct serial_struct __user *newinfo)
682 struct serial_struct new_serial;
683 struct uart_port *uport = state->uart_port;
684 struct tty_port *port = &state->port;
685 unsigned long new_port;
686 unsigned int change_irq, change_port, closing_wait;
687 unsigned int old_custom_divisor, close_delay;
688 upf_t old_flags, new_flags;
689 int retval = 0;
691 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
692 return -EFAULT;
694 new_port = new_serial.port;
695 if (HIGH_BITS_OFFSET)
696 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
698 new_serial.irq = irq_canonicalize(new_serial.irq);
699 close_delay = new_serial.close_delay * 10;
700 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
701 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
704 * This semaphore protects port->count. It is also
705 * very useful to prevent opens. Also, take the
706 * port configuration semaphore to make sure that a
707 * module insertion/removal doesn't change anything
708 * under us.
710 mutex_lock(&port->mutex);
712 change_irq = !(uport->flags & UPF_FIXED_PORT)
713 && new_serial.irq != uport->irq;
716 * Since changing the 'type' of the port changes its resource
717 * allocations, we should treat type changes the same as
718 * IO port changes.
720 change_port = !(uport->flags & UPF_FIXED_PORT)
721 && (new_port != uport->iobase ||
722 (unsigned long)new_serial.iomem_base != uport->mapbase ||
723 new_serial.hub6 != uport->hub6 ||
724 new_serial.io_type != uport->iotype ||
725 new_serial.iomem_reg_shift != uport->regshift ||
726 new_serial.type != uport->type);
728 old_flags = uport->flags;
729 new_flags = new_serial.flags;
730 old_custom_divisor = uport->custom_divisor;
732 if (!capable(CAP_SYS_ADMIN)) {
733 retval = -EPERM;
734 if (change_irq || change_port ||
735 (new_serial.baud_base != uport->uartclk / 16) ||
736 (close_delay != port->close_delay) ||
737 (closing_wait != port->closing_wait) ||
738 (new_serial.xmit_fifo_size &&
739 new_serial.xmit_fifo_size != uport->fifosize) ||
740 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
741 goto exit;
742 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
743 (new_flags & UPF_USR_MASK));
744 uport->custom_divisor = new_serial.custom_divisor;
745 goto check_and_exit;
749 * Ask the low level driver to verify the settings.
751 if (uport->ops->verify_port)
752 retval = uport->ops->verify_port(uport, &new_serial);
754 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
755 (new_serial.baud_base < 9600))
756 retval = -EINVAL;
758 if (retval)
759 goto exit;
761 if (change_port || change_irq) {
762 retval = -EBUSY;
765 * Make sure that we are the sole user of this port.
767 if (tty_port_users(port) > 1)
768 goto exit;
771 * We need to shutdown the serial port at the old
772 * port/type/irq combination.
774 uart_shutdown(tty, state);
777 if (change_port) {
778 unsigned long old_iobase, old_mapbase;
779 unsigned int old_type, old_iotype, old_hub6, old_shift;
781 old_iobase = uport->iobase;
782 old_mapbase = uport->mapbase;
783 old_type = uport->type;
784 old_hub6 = uport->hub6;
785 old_iotype = uport->iotype;
786 old_shift = uport->regshift;
789 * Free and release old regions
791 if (old_type != PORT_UNKNOWN)
792 uport->ops->release_port(uport);
794 uport->iobase = new_port;
795 uport->type = new_serial.type;
796 uport->hub6 = new_serial.hub6;
797 uport->iotype = new_serial.io_type;
798 uport->regshift = new_serial.iomem_reg_shift;
799 uport->mapbase = (unsigned long)new_serial.iomem_base;
802 * Claim and map the new regions
804 if (uport->type != PORT_UNKNOWN) {
805 retval = uport->ops->request_port(uport);
806 } else {
807 /* Always success - Jean II */
808 retval = 0;
812 * If we fail to request resources for the
813 * new port, try to restore the old settings.
815 if (retval && old_type != PORT_UNKNOWN) {
816 uport->iobase = old_iobase;
817 uport->type = old_type;
818 uport->hub6 = old_hub6;
819 uport->iotype = old_iotype;
820 uport->regshift = old_shift;
821 uport->mapbase = old_mapbase;
822 retval = uport->ops->request_port(uport);
824 * If we failed to restore the old settings,
825 * we fail like this.
827 if (retval)
828 uport->type = PORT_UNKNOWN;
831 * We failed anyway.
833 retval = -EBUSY;
834 /* Added to return the correct error -Ram Gupta */
835 goto exit;
839 if (change_irq)
840 uport->irq = new_serial.irq;
841 if (!(uport->flags & UPF_FIXED_PORT))
842 uport->uartclk = new_serial.baud_base * 16;
843 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
844 (new_flags & UPF_CHANGE_MASK);
845 uport->custom_divisor = new_serial.custom_divisor;
846 port->close_delay = close_delay;
847 port->closing_wait = closing_wait;
848 if (new_serial.xmit_fifo_size)
849 uport->fifosize = new_serial.xmit_fifo_size;
850 if (port->tty)
851 port->tty->low_latency =
852 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
854 check_and_exit:
855 retval = 0;
856 if (uport->type == PORT_UNKNOWN)
857 goto exit;
858 if (port->flags & ASYNC_INITIALIZED) {
859 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
860 old_custom_divisor != uport->custom_divisor) {
862 * If they're setting up a custom divisor or speed,
863 * instead of clearing it, then bitch about it. No
864 * need to rate-limit; it's CAP_SYS_ADMIN only.
866 if (uport->flags & UPF_SPD_MASK) {
867 char buf[64];
868 printk(KERN_NOTICE
869 "%s sets custom speed on %s. This "
870 "is deprecated.\n", current->comm,
871 tty_name(port->tty, buf));
873 uart_change_speed(tty, state, NULL);
875 } else
876 retval = uart_startup(tty, state, 1);
877 exit:
878 mutex_unlock(&port->mutex);
879 return retval;
883 * uart_get_lsr_info - get line status register info
884 * @tty: tty associated with the UART
885 * @state: UART being queried
886 * @value: returned modem value
888 * Note: uart_ioctl protects us against hangups.
890 static int uart_get_lsr_info(struct tty_struct *tty,
891 struct uart_state *state, unsigned int __user *value)
893 struct uart_port *uport = state->uart_port;
894 unsigned int result;
896 result = uport->ops->tx_empty(uport);
899 * If we're about to load something into the transmit
900 * register, we'll pretend the transmitter isn't empty to
901 * avoid a race condition (depending on when the transmit
902 * interrupt happens).
904 if (uport->x_char ||
905 ((uart_circ_chars_pending(&state->xmit) > 0) &&
906 !tty->stopped && !tty->hw_stopped))
907 result &= ~TIOCSER_TEMT;
909 return put_user(result, value);
912 static int uart_tiocmget(struct tty_struct *tty)
914 struct uart_state *state = tty->driver_data;
915 struct tty_port *port = &state->port;
916 struct uart_port *uport = state->uart_port;
917 int result = -EIO;
919 mutex_lock(&port->mutex);
920 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
921 result = uport->mctrl;
922 spin_lock_irq(&uport->lock);
923 result |= uport->ops->get_mctrl(uport);
924 spin_unlock_irq(&uport->lock);
926 mutex_unlock(&port->mutex);
928 return result;
931 static int
932 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
934 struct uart_state *state = tty->driver_data;
935 struct uart_port *uport = state->uart_port;
936 struct tty_port *port = &state->port;
937 int ret = -EIO;
939 mutex_lock(&port->mutex);
940 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
941 uart_update_mctrl(uport, set, clear);
942 ret = 0;
944 mutex_unlock(&port->mutex);
945 return ret;
948 static int uart_break_ctl(struct tty_struct *tty, int break_state)
950 struct uart_state *state = tty->driver_data;
951 struct tty_port *port = &state->port;
952 struct uart_port *uport = state->uart_port;
954 mutex_lock(&port->mutex);
956 if (uport->type != PORT_UNKNOWN)
957 uport->ops->break_ctl(uport, break_state);
959 mutex_unlock(&port->mutex);
960 return 0;
963 static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
965 struct uart_port *uport = state->uart_port;
966 struct tty_port *port = &state->port;
967 int flags, ret;
969 if (!capable(CAP_SYS_ADMIN))
970 return -EPERM;
973 * Take the per-port semaphore. This prevents count from
974 * changing, and hence any extra opens of the port while
975 * we're auto-configuring.
977 if (mutex_lock_interruptible(&port->mutex))
978 return -ERESTARTSYS;
980 ret = -EBUSY;
981 if (tty_port_users(port) == 1) {
982 uart_shutdown(tty, state);
985 * If we already have a port type configured,
986 * we must release its resources.
988 if (uport->type != PORT_UNKNOWN)
989 uport->ops->release_port(uport);
991 flags = UART_CONFIG_TYPE;
992 if (uport->flags & UPF_AUTO_IRQ)
993 flags |= UART_CONFIG_IRQ;
996 * This will claim the ports resources if
997 * a port is found.
999 uport->ops->config_port(uport, flags);
1001 ret = uart_startup(tty, state, 1);
1003 mutex_unlock(&port->mutex);
1004 return ret;
1008 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1009 * - mask passed in arg for lines of interest
1010 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1011 * Caller should use TIOCGICOUNT to see which one it was
1013 * FIXME: This wants extracting into a common all driver implementation
1014 * of TIOCMWAIT using tty_port.
1016 static int
1017 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1019 struct uart_port *uport = state->uart_port;
1020 struct tty_port *port = &state->port;
1021 DECLARE_WAITQUEUE(wait, current);
1022 struct uart_icount cprev, cnow;
1023 int ret;
1026 * note the counters on entry
1028 spin_lock_irq(&uport->lock);
1029 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1032 * Force modem status interrupts on
1034 uport->ops->enable_ms(uport);
1035 spin_unlock_irq(&uport->lock);
1037 add_wait_queue(&port->delta_msr_wait, &wait);
1038 for (;;) {
1039 spin_lock_irq(&uport->lock);
1040 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1041 spin_unlock_irq(&uport->lock);
1043 set_current_state(TASK_INTERRUPTIBLE);
1045 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1046 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1047 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1048 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1049 ret = 0;
1050 break;
1053 schedule();
1055 /* see if a signal did it */
1056 if (signal_pending(current)) {
1057 ret = -ERESTARTSYS;
1058 break;
1061 cprev = cnow;
1064 current->state = TASK_RUNNING;
1065 remove_wait_queue(&port->delta_msr_wait, &wait);
1067 return ret;
1071 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1072 * Return: write counters to the user passed counter struct
1073 * NB: both 1->0 and 0->1 transitions are counted except for
1074 * RI where only 0->1 is counted.
1076 static int uart_get_icount(struct tty_struct *tty,
1077 struct serial_icounter_struct *icount)
1079 struct uart_state *state = tty->driver_data;
1080 struct uart_icount cnow;
1081 struct uart_port *uport = state->uart_port;
1083 spin_lock_irq(&uport->lock);
1084 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1085 spin_unlock_irq(&uport->lock);
1087 icount->cts = cnow.cts;
1088 icount->dsr = cnow.dsr;
1089 icount->rng = cnow.rng;
1090 icount->dcd = cnow.dcd;
1091 icount->rx = cnow.rx;
1092 icount->tx = cnow.tx;
1093 icount->frame = cnow.frame;
1094 icount->overrun = cnow.overrun;
1095 icount->parity = cnow.parity;
1096 icount->brk = cnow.brk;
1097 icount->buf_overrun = cnow.buf_overrun;
1099 return 0;
1103 * Called via sys_ioctl. We can use spin_lock_irq() here.
1105 static int
1106 uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1107 unsigned long arg)
1109 struct uart_state *state = tty->driver_data;
1110 struct tty_port *port = &state->port;
1111 void __user *uarg = (void __user *)arg;
1112 int ret = -ENOIOCTLCMD;
1116 * These ioctls don't rely on the hardware to be present.
1118 switch (cmd) {
1119 case TIOCGSERIAL:
1120 ret = uart_get_info(state, uarg);
1121 break;
1123 case TIOCSSERIAL:
1124 ret = uart_set_info(tty, state, uarg);
1125 break;
1127 case TIOCSERCONFIG:
1128 ret = uart_do_autoconfig(tty, state);
1129 break;
1131 case TIOCSERGWILD: /* obsolete */
1132 case TIOCSERSWILD: /* obsolete */
1133 ret = 0;
1134 break;
1137 if (ret != -ENOIOCTLCMD)
1138 goto out;
1140 if (tty->flags & (1 << TTY_IO_ERROR)) {
1141 ret = -EIO;
1142 goto out;
1146 * The following should only be used when hardware is present.
1148 switch (cmd) {
1149 case TIOCMIWAIT:
1150 ret = uart_wait_modem_status(state, arg);
1151 break;
1154 if (ret != -ENOIOCTLCMD)
1155 goto out;
1157 mutex_lock(&port->mutex);
1159 if (tty->flags & (1 << TTY_IO_ERROR)) {
1160 ret = -EIO;
1161 goto out_up;
1165 * All these rely on hardware being present and need to be
1166 * protected against the tty being hung up.
1168 switch (cmd) {
1169 case TIOCSERGETLSR: /* Get line status register */
1170 ret = uart_get_lsr_info(tty, state, uarg);
1171 break;
1173 default: {
1174 struct uart_port *uport = state->uart_port;
1175 if (uport->ops->ioctl)
1176 ret = uport->ops->ioctl(uport, cmd, arg);
1177 break;
1180 out_up:
1181 mutex_unlock(&port->mutex);
1182 out:
1183 return ret;
1186 static void uart_set_ldisc(struct tty_struct *tty)
1188 struct uart_state *state = tty->driver_data;
1189 struct uart_port *uport = state->uart_port;
1191 if (uport->ops->set_ldisc)
1192 uport->ops->set_ldisc(uport, tty->termios->c_line);
1195 static void uart_set_termios(struct tty_struct *tty,
1196 struct ktermios *old_termios)
1198 struct uart_state *state = tty->driver_data;
1199 unsigned long flags;
1200 unsigned int cflag = tty->termios->c_cflag;
1204 * These are the bits that are used to setup various
1205 * flags in the low level driver. We can ignore the Bfoo
1206 * bits in c_cflag; c_[io]speed will always be set
1207 * appropriately by set_termios() in tty_ioctl.c
1209 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1210 if ((cflag ^ old_termios->c_cflag) == 0 &&
1211 tty->termios->c_ospeed == old_termios->c_ospeed &&
1212 tty->termios->c_ispeed == old_termios->c_ispeed &&
1213 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1214 return;
1217 uart_change_speed(tty, state, old_termios);
1219 /* Handle transition to B0 status */
1220 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1221 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1222 /* Handle transition away from B0 status */
1223 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1224 unsigned int mask = TIOCM_DTR;
1225 if (!(cflag & CRTSCTS) ||
1226 !test_bit(TTY_THROTTLED, &tty->flags))
1227 mask |= TIOCM_RTS;
1228 uart_set_mctrl(state->uart_port, mask);
1231 /* Handle turning off CRTSCTS */
1232 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1233 spin_lock_irqsave(&state->uart_port->lock, flags);
1234 tty->hw_stopped = 0;
1235 __uart_start(tty);
1236 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1238 /* Handle turning on CRTSCTS */
1239 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1240 spin_lock_irqsave(&state->uart_port->lock, flags);
1241 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1242 tty->hw_stopped = 1;
1243 state->uart_port->ops->stop_tx(state->uart_port);
1245 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1247 #if 0
1249 * No need to wake up processes in open wait, since they
1250 * sample the CLOCAL flag once, and don't recheck it.
1251 * XXX It's not clear whether the current behavior is correct
1252 * or not. Hence, this may change.....
1254 if (!(old_termios->c_cflag & CLOCAL) &&
1255 (tty->termios->c_cflag & CLOCAL))
1256 wake_up_interruptible(&state->uart_port.open_wait);
1257 #endif
1261 * In 2.4.5, calls to this will be serialized via the BKL in
1262 * linux/drivers/char/tty_io.c:tty_release()
1263 * linux/drivers/char/tty_io.c:do_tty_handup()
1265 static void uart_close(struct tty_struct *tty, struct file *filp)
1267 struct uart_state *state = tty->driver_data;
1268 struct tty_port *port;
1269 struct uart_port *uport;
1270 unsigned long flags;
1272 BUG_ON(!tty_locked());
1274 if (!state)
1275 return;
1277 uport = state->uart_port;
1278 port = &state->port;
1280 pr_debug("uart_close(%d) called\n", uport->line);
1282 mutex_lock(&port->mutex);
1283 spin_lock_irqsave(&port->lock, flags);
1285 if (tty_hung_up_p(filp)) {
1286 spin_unlock_irqrestore(&port->lock, flags);
1287 goto done;
1290 if ((tty->count == 1) && (port->count != 1)) {
1292 * Uh, oh. tty->count is 1, which means that the tty
1293 * structure will be freed. port->count should always
1294 * be one in these conditions. If it's greater than
1295 * one, we've got real problems, since it means the
1296 * serial port won't be shutdown.
1298 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1299 "port->count is %d\n", port->count);
1300 port->count = 1;
1302 if (--port->count < 0) {
1303 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1304 tty->name, port->count);
1305 port->count = 0;
1307 if (port->count) {
1308 spin_unlock_irqrestore(&port->lock, flags);
1309 goto done;
1313 * Now we wait for the transmit buffer to clear; and we notify
1314 * the line discipline to only process XON/XOFF characters by
1315 * setting tty->closing.
1317 tty->closing = 1;
1318 spin_unlock_irqrestore(&port->lock, flags);
1320 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
1322 * hack: open-coded tty_wait_until_sent to avoid
1323 * recursive tty_lock
1325 long timeout = msecs_to_jiffies(port->closing_wait);
1326 if (wait_event_interruptible_timeout(tty->write_wait,
1327 !tty_chars_in_buffer(tty), timeout) >= 0)
1328 __uart_wait_until_sent(uport, timeout);
1332 * At this point, we stop accepting input. To do this, we
1333 * disable the receive line status interrupts.
1335 if (port->flags & ASYNC_INITIALIZED) {
1336 unsigned long flags;
1337 spin_lock_irqsave(&uport->lock, flags);
1338 uport->ops->stop_rx(uport);
1339 spin_unlock_irqrestore(&uport->lock, flags);
1341 * Before we drop DTR, make sure the UART transmitter
1342 * has completely drained; this is especially
1343 * important if there is a transmit FIFO!
1345 __uart_wait_until_sent(uport, uport->timeout);
1348 uart_shutdown(tty, state);
1349 uart_flush_buffer(tty);
1351 tty_ldisc_flush(tty);
1353 tty_port_tty_set(port, NULL);
1354 spin_lock_irqsave(&port->lock, flags);
1355 tty->closing = 0;
1357 if (port->blocked_open) {
1358 spin_unlock_irqrestore(&port->lock, flags);
1359 if (port->close_delay)
1360 msleep_interruptible(port->close_delay);
1361 spin_lock_irqsave(&port->lock, flags);
1362 } else if (!uart_console(uport)) {
1363 spin_unlock_irqrestore(&port->lock, flags);
1364 uart_change_pm(state, 3);
1365 spin_lock_irqsave(&port->lock, flags);
1369 * Wake up anyone trying to open this port.
1371 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1372 spin_unlock_irqrestore(&port->lock, flags);
1373 wake_up_interruptible(&port->open_wait);
1375 done:
1376 mutex_unlock(&port->mutex);
1379 static void __uart_wait_until_sent(struct uart_port *port, int timeout)
1381 unsigned long char_time, expire;
1383 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1384 return;
1387 * Set the check interval to be 1/5 of the estimated time to
1388 * send a single character, and make it at least 1. The check
1389 * interval should also be less than the timeout.
1391 * Note: we have to use pretty tight timings here to satisfy
1392 * the NIST-PCTS.
1394 char_time = (port->timeout - HZ/50) / port->fifosize;
1395 char_time = char_time / 5;
1396 if (char_time == 0)
1397 char_time = 1;
1398 if (timeout && timeout < char_time)
1399 char_time = timeout;
1402 * If the transmitter hasn't cleared in twice the approximate
1403 * amount of time to send the entire FIFO, it probably won't
1404 * ever clear. This assumes the UART isn't doing flow
1405 * control, which is currently the case. Hence, if it ever
1406 * takes longer than port->timeout, this is probably due to a
1407 * UART bug of some kind. So, we clamp the timeout parameter at
1408 * 2*port->timeout.
1410 if (timeout == 0 || timeout > 2 * port->timeout)
1411 timeout = 2 * port->timeout;
1413 expire = jiffies + timeout;
1415 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1416 port->line, jiffies, expire);
1419 * Check whether the transmitter is empty every 'char_time'.
1420 * 'timeout' / 'expire' give us the maximum amount of time
1421 * we wait.
1423 while (!port->ops->tx_empty(port)) {
1424 msleep_interruptible(jiffies_to_msecs(char_time));
1425 if (signal_pending(current))
1426 break;
1427 if (time_after(jiffies, expire))
1428 break;
1430 set_current_state(TASK_RUNNING); /* might not be needed */
1433 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1435 struct uart_state *state = tty->driver_data;
1436 struct uart_port *port = state->uart_port;
1438 tty_lock();
1439 __uart_wait_until_sent(port, timeout);
1440 tty_unlock();
1444 * This is called with the BKL held in
1445 * linux/drivers/char/tty_io.c:do_tty_hangup()
1446 * We're called from the eventd thread, so we can sleep for
1447 * a _short_ time only.
1449 static void uart_hangup(struct tty_struct *tty)
1451 struct uart_state *state = tty->driver_data;
1452 struct tty_port *port = &state->port;
1453 unsigned long flags;
1455 BUG_ON(!tty_locked());
1456 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1458 mutex_lock(&port->mutex);
1459 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1460 uart_flush_buffer(tty);
1461 uart_shutdown(tty, state);
1462 spin_lock_irqsave(&port->lock, flags);
1463 port->count = 0;
1464 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1465 spin_unlock_irqrestore(&port->lock, flags);
1466 tty_port_tty_set(port, NULL);
1467 wake_up_interruptible(&port->open_wait);
1468 wake_up_interruptible(&port->delta_msr_wait);
1470 mutex_unlock(&port->mutex);
1474 * uart_update_termios - update the terminal hw settings
1475 * @tty: tty associated with UART
1476 * @state: UART to update
1478 * Copy across the serial console cflag setting into the termios settings
1479 * for the initial open of the port. This allows continuity between the
1480 * kernel settings, and the settings init adopts when it opens the port
1481 * for the first time.
1483 static void uart_update_termios(struct tty_struct *tty,
1484 struct uart_state *state)
1486 struct uart_port *port = state->uart_port;
1489 * If the device failed to grab its irq resources,
1490 * or some other error occurred, don't try to talk
1491 * to the port hardware.
1493 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1495 * And finally enable the RTS and DTR signals.
1497 if (tty->termios->c_cflag & CBAUD)
1498 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1502 static int uart_carrier_raised(struct tty_port *port)
1504 struct uart_state *state = container_of(port, struct uart_state, port);
1505 struct uart_port *uport = state->uart_port;
1506 int mctrl;
1507 spin_lock_irq(&uport->lock);
1508 uport->ops->enable_ms(uport);
1509 mctrl = uport->ops->get_mctrl(uport);
1510 spin_unlock_irq(&uport->lock);
1511 if (mctrl & TIOCM_CAR)
1512 return 1;
1513 return 0;
1516 static void uart_dtr_rts(struct tty_port *port, int onoff)
1518 struct uart_state *state = container_of(port, struct uart_state, port);
1519 struct uart_port *uport = state->uart_port;
1521 if (onoff) {
1522 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1525 * If this is the first open to succeed,
1526 * adjust things to suit.
1528 if (!test_and_set_bit(ASYNCB_NORMAL_ACTIVE, &port->flags))
1529 uart_update_termios(port->tty, state);
1531 else
1532 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1535 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1537 struct uart_state *state;
1538 struct tty_port *port;
1539 int ret = 0;
1541 state = drv->state + line;
1542 port = &state->port;
1543 if (mutex_lock_interruptible(&port->mutex)) {
1544 ret = -ERESTARTSYS;
1545 goto err;
1548 port->count++;
1549 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1550 ret = -ENXIO;
1551 goto err_unlock;
1553 return state;
1555 err_unlock:
1556 port->count--;
1557 mutex_unlock(&port->mutex);
1558 err:
1559 return ERR_PTR(ret);
1563 * calls to uart_open are serialised by the BKL in
1564 * fs/char_dev.c:chrdev_open()
1565 * Note that if this fails, then uart_close() _will_ be called.
1567 * In time, we want to scrap the "opening nonpresent ports"
1568 * behaviour and implement an alternative way for setserial
1569 * to set base addresses/ports/types. This will allow us to
1570 * get rid of a certain amount of extra tests.
1572 static int uart_open(struct tty_struct *tty, struct file *filp)
1574 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1575 struct uart_state *state;
1576 struct tty_port *port;
1577 int retval, line = tty->index;
1579 BUG_ON(!tty_locked());
1580 pr_debug("uart_open(%d) called\n", line);
1583 * tty->driver->num won't change, so we won't fail here with
1584 * tty->driver_data set to something non-NULL (and therefore
1585 * we won't get caught by uart_close()).
1587 retval = -ENODEV;
1588 if (line >= tty->driver->num)
1589 goto fail;
1592 * We take the semaphore inside uart_get to guarantee that we won't
1593 * be re-entered while allocating the state structure, or while we
1594 * request any IRQs that the driver may need. This also has the nice
1595 * side-effect that it delays the action of uart_hangup, so we can
1596 * guarantee that state->port.tty will always contain something
1597 * reasonable.
1599 state = uart_get(drv, line);
1600 if (IS_ERR(state)) {
1601 retval = PTR_ERR(state);
1602 goto fail;
1604 port = &state->port;
1607 * Once we set tty->driver_data here, we are guaranteed that
1608 * uart_close() will decrement the driver module use count.
1609 * Any failures from here onwards should not touch the count.
1611 tty->driver_data = state;
1612 state->uart_port->state = state;
1613 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1614 tty->alt_speed = 0;
1615 tty_port_tty_set(port, tty);
1618 * If the port is in the middle of closing, bail out now.
1620 if (tty_hung_up_p(filp)) {
1621 retval = -EAGAIN;
1622 port->count--;
1623 mutex_unlock(&port->mutex);
1624 goto fail;
1628 * Make sure the device is in D0 state.
1630 if (port->count == 1)
1631 uart_change_pm(state, 0);
1634 * Start up the serial port.
1636 retval = uart_startup(tty, state, 0);
1639 * If we succeeded, wait until the port is ready.
1641 mutex_unlock(&port->mutex);
1642 if (retval == 0)
1643 retval = tty_port_block_til_ready(port, tty, filp);
1645 fail:
1646 return retval;
1649 static const char *uart_type(struct uart_port *port)
1651 const char *str = NULL;
1653 if (port->ops->type)
1654 str = port->ops->type(port);
1656 if (!str)
1657 str = "unknown";
1659 return str;
1662 #ifdef CONFIG_PROC_FS
1664 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1666 struct uart_state *state = drv->state + i;
1667 struct tty_port *port = &state->port;
1668 int pm_state;
1669 struct uart_port *uport = state->uart_port;
1670 char stat_buf[32];
1671 unsigned int status;
1672 int mmio;
1674 if (!uport)
1675 return;
1677 mmio = uport->iotype >= UPIO_MEM;
1678 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1679 uport->line, uart_type(uport),
1680 mmio ? "mmio:0x" : "port:",
1681 mmio ? (unsigned long long)uport->mapbase
1682 : (unsigned long long)uport->iobase,
1683 uport->irq);
1685 if (uport->type == PORT_UNKNOWN) {
1686 seq_putc(m, '\n');
1687 return;
1690 if (capable(CAP_SYS_ADMIN)) {
1691 mutex_lock(&port->mutex);
1692 pm_state = state->pm_state;
1693 if (pm_state)
1694 uart_change_pm(state, 0);
1695 spin_lock_irq(&uport->lock);
1696 status = uport->ops->get_mctrl(uport);
1697 spin_unlock_irq(&uport->lock);
1698 if (pm_state)
1699 uart_change_pm(state, pm_state);
1700 mutex_unlock(&port->mutex);
1702 seq_printf(m, " tx:%d rx:%d",
1703 uport->icount.tx, uport->icount.rx);
1704 if (uport->icount.frame)
1705 seq_printf(m, " fe:%d",
1706 uport->icount.frame);
1707 if (uport->icount.parity)
1708 seq_printf(m, " pe:%d",
1709 uport->icount.parity);
1710 if (uport->icount.brk)
1711 seq_printf(m, " brk:%d",
1712 uport->icount.brk);
1713 if (uport->icount.overrun)
1714 seq_printf(m, " oe:%d",
1715 uport->icount.overrun);
1717 #define INFOBIT(bit, str) \
1718 if (uport->mctrl & (bit)) \
1719 strncat(stat_buf, (str), sizeof(stat_buf) - \
1720 strlen(stat_buf) - 2)
1721 #define STATBIT(bit, str) \
1722 if (status & (bit)) \
1723 strncat(stat_buf, (str), sizeof(stat_buf) - \
1724 strlen(stat_buf) - 2)
1726 stat_buf[0] = '\0';
1727 stat_buf[1] = '\0';
1728 INFOBIT(TIOCM_RTS, "|RTS");
1729 STATBIT(TIOCM_CTS, "|CTS");
1730 INFOBIT(TIOCM_DTR, "|DTR");
1731 STATBIT(TIOCM_DSR, "|DSR");
1732 STATBIT(TIOCM_CAR, "|CD");
1733 STATBIT(TIOCM_RNG, "|RI");
1734 if (stat_buf[0])
1735 stat_buf[0] = ' ';
1737 seq_puts(m, stat_buf);
1739 seq_putc(m, '\n');
1740 #undef STATBIT
1741 #undef INFOBIT
1744 static int uart_proc_show(struct seq_file *m, void *v)
1746 struct tty_driver *ttydrv = m->private;
1747 struct uart_driver *drv = ttydrv->driver_state;
1748 int i;
1750 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1751 "", "", "");
1752 for (i = 0; i < drv->nr; i++)
1753 uart_line_info(m, drv, i);
1754 return 0;
1757 static int uart_proc_open(struct inode *inode, struct file *file)
1759 return single_open(file, uart_proc_show, PDE(inode)->data);
1762 static const struct file_operations uart_proc_fops = {
1763 .owner = THIS_MODULE,
1764 .open = uart_proc_open,
1765 .read = seq_read,
1766 .llseek = seq_lseek,
1767 .release = single_release,
1769 #endif
1771 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1773 * uart_console_write - write a console message to a serial port
1774 * @port: the port to write the message
1775 * @s: array of characters
1776 * @count: number of characters in string to write
1777 * @write: function to write character to port
1779 void uart_console_write(struct uart_port *port, const char *s,
1780 unsigned int count,
1781 void (*putchar)(struct uart_port *, int))
1783 unsigned int i;
1785 for (i = 0; i < count; i++, s++) {
1786 if (*s == '\n')
1787 putchar(port, '\r');
1788 putchar(port, *s);
1791 EXPORT_SYMBOL_GPL(uart_console_write);
1794 * Check whether an invalid uart number has been specified, and
1795 * if so, search for the first available port that does have
1796 * console support.
1798 struct uart_port * __init
1799 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1801 int idx = co->index;
1803 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1804 ports[idx].membase == NULL))
1805 for (idx = 0; idx < nr; idx++)
1806 if (ports[idx].iobase != 0 ||
1807 ports[idx].membase != NULL)
1808 break;
1810 co->index = idx;
1812 return ports + idx;
1816 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1817 * @options: pointer to option string
1818 * @baud: pointer to an 'int' variable for the baud rate.
1819 * @parity: pointer to an 'int' variable for the parity.
1820 * @bits: pointer to an 'int' variable for the number of data bits.
1821 * @flow: pointer to an 'int' variable for the flow control character.
1823 * uart_parse_options decodes a string containing the serial console
1824 * options. The format of the string is <baud><parity><bits><flow>,
1825 * eg: 115200n8r
1827 void
1828 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1830 char *s = options;
1832 *baud = simple_strtoul(s, NULL, 10);
1833 while (*s >= '0' && *s <= '9')
1834 s++;
1835 if (*s)
1836 *parity = *s++;
1837 if (*s)
1838 *bits = *s++ - '0';
1839 if (*s)
1840 *flow = *s;
1842 EXPORT_SYMBOL_GPL(uart_parse_options);
1844 struct baud_rates {
1845 unsigned int rate;
1846 unsigned int cflag;
1849 static const struct baud_rates baud_rates[] = {
1850 { 921600, B921600 },
1851 { 460800, B460800 },
1852 { 230400, B230400 },
1853 { 115200, B115200 },
1854 { 57600, B57600 },
1855 { 38400, B38400 },
1856 { 19200, B19200 },
1857 { 9600, B9600 },
1858 { 4800, B4800 },
1859 { 2400, B2400 },
1860 { 1200, B1200 },
1861 { 0, B38400 }
1865 * uart_set_options - setup the serial console parameters
1866 * @port: pointer to the serial ports uart_port structure
1867 * @co: console pointer
1868 * @baud: baud rate
1869 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1870 * @bits: number of data bits
1871 * @flow: flow control character - 'r' (rts)
1874 uart_set_options(struct uart_port *port, struct console *co,
1875 int baud, int parity, int bits, int flow)
1877 struct ktermios termios;
1878 static struct ktermios dummy;
1879 int i;
1882 * Ensure that the serial console lock is initialised
1883 * early.
1885 spin_lock_init(&port->lock);
1886 lockdep_set_class(&port->lock, &port_lock_key);
1888 memset(&termios, 0, sizeof(struct ktermios));
1890 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1893 * Construct a cflag setting.
1895 for (i = 0; baud_rates[i].rate; i++)
1896 if (baud_rates[i].rate <= baud)
1897 break;
1899 termios.c_cflag |= baud_rates[i].cflag;
1901 if (bits == 7)
1902 termios.c_cflag |= CS7;
1903 else
1904 termios.c_cflag |= CS8;
1906 switch (parity) {
1907 case 'o': case 'O':
1908 termios.c_cflag |= PARODD;
1909 /*fall through*/
1910 case 'e': case 'E':
1911 termios.c_cflag |= PARENB;
1912 break;
1915 if (flow == 'r')
1916 termios.c_cflag |= CRTSCTS;
1919 * some uarts on other side don't support no flow control.
1920 * So we set * DTR in host uart to make them happy
1922 port->mctrl |= TIOCM_DTR;
1924 port->ops->set_termios(port, &termios, &dummy);
1926 * Allow the setting of the UART parameters with a NULL console
1927 * too:
1929 if (co)
1930 co->cflag = termios.c_cflag;
1932 return 0;
1934 EXPORT_SYMBOL_GPL(uart_set_options);
1935 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1937 static void uart_change_pm(struct uart_state *state, int pm_state)
1939 struct uart_port *port = state->uart_port;
1941 if (state->pm_state != pm_state) {
1942 if (port->ops->pm)
1943 port->ops->pm(port, pm_state, state->pm_state);
1944 state->pm_state = pm_state;
1948 struct uart_match {
1949 struct uart_port *port;
1950 struct uart_driver *driver;
1953 static int serial_match_port(struct device *dev, void *data)
1955 struct uart_match *match = data;
1956 struct tty_driver *tty_drv = match->driver->tty_driver;
1957 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1958 match->port->line;
1960 return dev->devt == devt; /* Actually, only one tty per port */
1963 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1965 struct uart_state *state = drv->state + uport->line;
1966 struct tty_port *port = &state->port;
1967 struct device *tty_dev;
1968 struct uart_match match = {uport, drv};
1970 mutex_lock(&port->mutex);
1972 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1973 if (device_may_wakeup(tty_dev)) {
1974 if (!enable_irq_wake(uport->irq))
1975 uport->irq_wake = 1;
1976 put_device(tty_dev);
1977 mutex_unlock(&port->mutex);
1978 return 0;
1980 if (console_suspend_enabled || !uart_console(uport))
1981 uport->suspended = 1;
1983 if (port->flags & ASYNC_INITIALIZED) {
1984 const struct uart_ops *ops = uport->ops;
1985 int tries;
1987 if (console_suspend_enabled || !uart_console(uport)) {
1988 set_bit(ASYNCB_SUSPENDED, &port->flags);
1989 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1991 spin_lock_irq(&uport->lock);
1992 ops->stop_tx(uport);
1993 ops->set_mctrl(uport, 0);
1994 ops->stop_rx(uport);
1995 spin_unlock_irq(&uport->lock);
1999 * Wait for the transmitter to empty.
2001 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2002 msleep(10);
2003 if (!tries)
2004 printk(KERN_ERR "%s%s%s%d: Unable to drain "
2005 "transmitter\n",
2006 uport->dev ? dev_name(uport->dev) : "",
2007 uport->dev ? ": " : "",
2008 drv->dev_name,
2009 drv->tty_driver->name_base + uport->line);
2011 if (console_suspend_enabled || !uart_console(uport))
2012 ops->shutdown(uport);
2016 * Disable the console device before suspending.
2018 if (console_suspend_enabled && uart_console(uport))
2019 console_stop(uport->cons);
2021 if (console_suspend_enabled || !uart_console(uport))
2022 uart_change_pm(state, 3);
2024 mutex_unlock(&port->mutex);
2026 return 0;
2029 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2031 struct uart_state *state = drv->state + uport->line;
2032 struct tty_port *port = &state->port;
2033 struct device *tty_dev;
2034 struct uart_match match = {uport, drv};
2035 struct ktermios termios;
2037 mutex_lock(&port->mutex);
2039 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2040 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2041 if (uport->irq_wake) {
2042 disable_irq_wake(uport->irq);
2043 uport->irq_wake = 0;
2045 mutex_unlock(&port->mutex);
2046 return 0;
2048 uport->suspended = 0;
2051 * Re-enable the console device after suspending.
2053 if (uart_console(uport)) {
2055 * First try to use the console cflag setting.
2057 memset(&termios, 0, sizeof(struct ktermios));
2058 termios.c_cflag = uport->cons->cflag;
2061 * If that's unset, use the tty termios setting.
2063 if (port->tty && port->tty->termios && termios.c_cflag == 0)
2064 termios = *(port->tty->termios);
2066 uport->ops->set_termios(uport, &termios, NULL);
2067 if (console_suspend_enabled)
2068 console_start(uport->cons);
2071 if (port->flags & ASYNC_SUSPENDED) {
2072 const struct uart_ops *ops = uport->ops;
2073 int ret;
2075 uart_change_pm(state, 0);
2076 spin_lock_irq(&uport->lock);
2077 ops->set_mctrl(uport, 0);
2078 spin_unlock_irq(&uport->lock);
2079 if (console_suspend_enabled || !uart_console(uport)) {
2080 /* Protected by port mutex for now */
2081 struct tty_struct *tty = port->tty;
2082 ret = ops->startup(uport);
2083 if (ret == 0) {
2084 if (tty)
2085 uart_change_speed(tty, state, NULL);
2086 spin_lock_irq(&uport->lock);
2087 ops->set_mctrl(uport, uport->mctrl);
2088 ops->start_tx(uport);
2089 spin_unlock_irq(&uport->lock);
2090 set_bit(ASYNCB_INITIALIZED, &port->flags);
2091 } else {
2093 * Failed to resume - maybe hardware went away?
2094 * Clear the "initialized" flag so we won't try
2095 * to call the low level drivers shutdown method.
2097 uart_shutdown(tty, state);
2101 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2104 mutex_unlock(&port->mutex);
2106 return 0;
2109 static inline void
2110 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2112 char address[64];
2114 switch (port->iotype) {
2115 case UPIO_PORT:
2116 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2117 break;
2118 case UPIO_HUB6:
2119 snprintf(address, sizeof(address),
2120 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2121 break;
2122 case UPIO_MEM:
2123 case UPIO_MEM32:
2124 case UPIO_AU:
2125 case UPIO_TSI:
2126 case UPIO_DWAPB:
2127 case UPIO_DWAPB32:
2128 snprintf(address, sizeof(address),
2129 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2130 break;
2131 default:
2132 strlcpy(address, "*unknown*", sizeof(address));
2133 break;
2136 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2137 port->dev ? dev_name(port->dev) : "",
2138 port->dev ? ": " : "",
2139 drv->dev_name,
2140 drv->tty_driver->name_base + port->line,
2141 address, port->irq, uart_type(port));
2144 static void
2145 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2146 struct uart_port *port)
2148 unsigned int flags;
2151 * If there isn't a port here, don't do anything further.
2153 if (!port->iobase && !port->mapbase && !port->membase)
2154 return;
2157 * Now do the auto configuration stuff. Note that config_port
2158 * is expected to claim the resources and map the port for us.
2160 flags = 0;
2161 if (port->flags & UPF_AUTO_IRQ)
2162 flags |= UART_CONFIG_IRQ;
2163 if (port->flags & UPF_BOOT_AUTOCONF) {
2164 if (!(port->flags & UPF_FIXED_TYPE)) {
2165 port->type = PORT_UNKNOWN;
2166 flags |= UART_CONFIG_TYPE;
2168 port->ops->config_port(port, flags);
2171 if (port->type != PORT_UNKNOWN) {
2172 unsigned long flags;
2174 uart_report_port(drv, port);
2176 /* Power up port for set_mctrl() */
2177 uart_change_pm(state, 0);
2180 * Ensure that the modem control lines are de-activated.
2181 * keep the DTR setting that is set in uart_set_options()
2182 * We probably don't need a spinlock around this, but
2184 spin_lock_irqsave(&port->lock, flags);
2185 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2186 spin_unlock_irqrestore(&port->lock, flags);
2189 * If this driver supports console, and it hasn't been
2190 * successfully registered yet, try to re-register it.
2191 * It may be that the port was not available.
2193 if (port->cons && !(port->cons->flags & CON_ENABLED))
2194 register_console(port->cons);
2197 * Power down all ports by default, except the
2198 * console if we have one.
2200 if (!uart_console(port))
2201 uart_change_pm(state, 3);
2205 #ifdef CONFIG_CONSOLE_POLL
2207 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2209 struct uart_driver *drv = driver->driver_state;
2210 struct uart_state *state = drv->state + line;
2211 struct uart_port *port;
2212 int baud = 9600;
2213 int bits = 8;
2214 int parity = 'n';
2215 int flow = 'n';
2217 if (!state || !state->uart_port)
2218 return -1;
2220 port = state->uart_port;
2221 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2222 return -1;
2224 if (options) {
2225 uart_parse_options(options, &baud, &parity, &bits, &flow);
2226 return uart_set_options(port, NULL, baud, parity, bits, flow);
2229 return 0;
2232 static int uart_poll_get_char(struct tty_driver *driver, int line)
2234 struct uart_driver *drv = driver->driver_state;
2235 struct uart_state *state = drv->state + line;
2236 struct uart_port *port;
2238 if (!state || !state->uart_port)
2239 return -1;
2241 port = state->uart_port;
2242 return port->ops->poll_get_char(port);
2245 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2247 struct uart_driver *drv = driver->driver_state;
2248 struct uart_state *state = drv->state + line;
2249 struct uart_port *port;
2251 if (!state || !state->uart_port)
2252 return;
2254 port = state->uart_port;
2255 port->ops->poll_put_char(port, ch);
2257 #endif
2259 static const struct tty_operations uart_ops = {
2260 .open = uart_open,
2261 .close = uart_close,
2262 .write = uart_write,
2263 .put_char = uart_put_char,
2264 .flush_chars = uart_flush_chars,
2265 .write_room = uart_write_room,
2266 .chars_in_buffer= uart_chars_in_buffer,
2267 .flush_buffer = uart_flush_buffer,
2268 .ioctl = uart_ioctl,
2269 .throttle = uart_throttle,
2270 .unthrottle = uart_unthrottle,
2271 .send_xchar = uart_send_xchar,
2272 .set_termios = uart_set_termios,
2273 .set_ldisc = uart_set_ldisc,
2274 .stop = uart_stop,
2275 .start = uart_start,
2276 .hangup = uart_hangup,
2277 .break_ctl = uart_break_ctl,
2278 .wait_until_sent= uart_wait_until_sent,
2279 #ifdef CONFIG_PROC_FS
2280 .proc_fops = &uart_proc_fops,
2281 #endif
2282 .tiocmget = uart_tiocmget,
2283 .tiocmset = uart_tiocmset,
2284 .get_icount = uart_get_icount,
2285 #ifdef CONFIG_CONSOLE_POLL
2286 .poll_init = uart_poll_init,
2287 .poll_get_char = uart_poll_get_char,
2288 .poll_put_char = uart_poll_put_char,
2289 #endif
2292 static const struct tty_port_operations uart_port_ops = {
2293 .carrier_raised = uart_carrier_raised,
2294 .dtr_rts = uart_dtr_rts,
2298 * uart_register_driver - register a driver with the uart core layer
2299 * @drv: low level driver structure
2301 * Register a uart driver with the core driver. We in turn register
2302 * with the tty layer, and initialise the core driver per-port state.
2304 * We have a proc file in /proc/tty/driver which is named after the
2305 * normal driver.
2307 * drv->port should be NULL, and the per-port structures should be
2308 * registered using uart_add_one_port after this call has succeeded.
2310 int uart_register_driver(struct uart_driver *drv)
2312 struct tty_driver *normal;
2313 int i, retval;
2315 BUG_ON(drv->state);
2318 * Maybe we should be using a slab cache for this, especially if
2319 * we have a large number of ports to handle.
2321 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2322 if (!drv->state)
2323 goto out;
2325 normal = alloc_tty_driver(drv->nr);
2326 if (!normal)
2327 goto out_kfree;
2329 drv->tty_driver = normal;
2331 normal->owner = drv->owner;
2332 normal->driver_name = drv->driver_name;
2333 normal->name = drv->dev_name;
2334 normal->major = drv->major;
2335 normal->minor_start = drv->minor;
2336 normal->type = TTY_DRIVER_TYPE_SERIAL;
2337 normal->subtype = SERIAL_TYPE_NORMAL;
2338 normal->init_termios = tty_std_termios;
2339 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2340 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2341 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2342 normal->driver_state = drv;
2343 tty_set_operations(normal, &uart_ops);
2346 * Initialise the UART state(s).
2348 for (i = 0; i < drv->nr; i++) {
2349 struct uart_state *state = drv->state + i;
2350 struct tty_port *port = &state->port;
2352 tty_port_init(port);
2353 port->ops = &uart_port_ops;
2354 port->close_delay = 500; /* .5 seconds */
2355 port->closing_wait = 30000; /* 30 seconds */
2356 tasklet_init(&state->tlet, uart_tasklet_action,
2357 (unsigned long)state);
2360 retval = tty_register_driver(normal);
2361 if (retval >= 0)
2362 return retval;
2364 put_tty_driver(normal);
2365 out_kfree:
2366 kfree(drv->state);
2367 out:
2368 return -ENOMEM;
2372 * uart_unregister_driver - remove a driver from the uart core layer
2373 * @drv: low level driver structure
2375 * Remove all references to a driver from the core driver. The low
2376 * level driver must have removed all its ports via the
2377 * uart_remove_one_port() if it registered them with uart_add_one_port().
2378 * (ie, drv->port == NULL)
2380 void uart_unregister_driver(struct uart_driver *drv)
2382 struct tty_driver *p = drv->tty_driver;
2383 tty_unregister_driver(p);
2384 put_tty_driver(p);
2385 kfree(drv->state);
2386 drv->tty_driver = NULL;
2389 struct tty_driver *uart_console_device(struct console *co, int *index)
2391 struct uart_driver *p = co->data;
2392 *index = co->index;
2393 return p->tty_driver;
2397 * uart_add_one_port - attach a driver-defined port structure
2398 * @drv: pointer to the uart low level driver structure for this port
2399 * @uport: uart port structure to use for this port.
2401 * This allows the driver to register its own uart_port structure
2402 * with the core driver. The main purpose is to allow the low
2403 * level uart drivers to expand uart_port, rather than having yet
2404 * more levels of structures.
2406 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2408 struct uart_state *state;
2409 struct tty_port *port;
2410 int ret = 0;
2411 struct device *tty_dev;
2413 BUG_ON(in_interrupt());
2415 if (uport->line >= drv->nr)
2416 return -EINVAL;
2418 state = drv->state + uport->line;
2419 port = &state->port;
2421 mutex_lock(&port_mutex);
2422 mutex_lock(&port->mutex);
2423 if (state->uart_port) {
2424 ret = -EINVAL;
2425 goto out;
2428 state->uart_port = uport;
2429 state->pm_state = -1;
2431 uport->cons = drv->cons;
2432 uport->state = state;
2435 * If this port is a console, then the spinlock is already
2436 * initialised.
2438 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2439 spin_lock_init(&uport->lock);
2440 lockdep_set_class(&uport->lock, &port_lock_key);
2443 uart_configure_port(drv, state, uport);
2446 * Register the port whether it's detected or not. This allows
2447 * setserial to be used to alter this ports parameters.
2449 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2450 if (likely(!IS_ERR(tty_dev))) {
2451 device_init_wakeup(tty_dev, 1);
2452 device_set_wakeup_enable(tty_dev, 0);
2453 } else
2454 printk(KERN_ERR "Cannot register tty device on line %d\n",
2455 uport->line);
2458 * Ensure UPF_DEAD is not set.
2460 uport->flags &= ~UPF_DEAD;
2462 out:
2463 mutex_unlock(&port->mutex);
2464 mutex_unlock(&port_mutex);
2466 return ret;
2470 * uart_remove_one_port - detach a driver defined port structure
2471 * @drv: pointer to the uart low level driver structure for this port
2472 * @uport: uart port structure for this port
2474 * This unhooks (and hangs up) the specified port structure from the
2475 * core driver. No further calls will be made to the low-level code
2476 * for this port.
2478 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2480 struct uart_state *state = drv->state + uport->line;
2481 struct tty_port *port = &state->port;
2483 BUG_ON(in_interrupt());
2485 if (state->uart_port != uport)
2486 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2487 state->uart_port, uport);
2489 mutex_lock(&port_mutex);
2492 * Mark the port "dead" - this prevents any opens from
2493 * succeeding while we shut down the port.
2495 mutex_lock(&port->mutex);
2496 uport->flags |= UPF_DEAD;
2497 mutex_unlock(&port->mutex);
2500 * Remove the devices from the tty layer
2502 tty_unregister_device(drv->tty_driver, uport->line);
2504 if (port->tty)
2505 tty_vhangup(port->tty);
2508 * Free the port IO and memory resources, if any.
2510 if (uport->type != PORT_UNKNOWN)
2511 uport->ops->release_port(uport);
2514 * Indicate that there isn't a port here anymore.
2516 uport->type = PORT_UNKNOWN;
2519 * Kill the tasklet, and free resources.
2521 tasklet_kill(&state->tlet);
2523 state->uart_port = NULL;
2524 mutex_unlock(&port_mutex);
2526 return 0;
2530 * Are the two ports equivalent?
2532 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2534 if (port1->iotype != port2->iotype)
2535 return 0;
2537 switch (port1->iotype) {
2538 case UPIO_PORT:
2539 return (port1->iobase == port2->iobase);
2540 case UPIO_HUB6:
2541 return (port1->iobase == port2->iobase) &&
2542 (port1->hub6 == port2->hub6);
2543 case UPIO_MEM:
2544 case UPIO_MEM32:
2545 case UPIO_AU:
2546 case UPIO_TSI:
2547 case UPIO_DWAPB:
2548 case UPIO_DWAPB32:
2549 return (port1->mapbase == port2->mapbase);
2551 return 0;
2553 EXPORT_SYMBOL(uart_match_port);
2555 EXPORT_SYMBOL(uart_write_wakeup);
2556 EXPORT_SYMBOL(uart_register_driver);
2557 EXPORT_SYMBOL(uart_unregister_driver);
2558 EXPORT_SYMBOL(uart_suspend_port);
2559 EXPORT_SYMBOL(uart_resume_port);
2560 EXPORT_SYMBOL(uart_add_one_port);
2561 EXPORT_SYMBOL(uart_remove_one_port);
2563 MODULE_DESCRIPTION("Serial driver core");
2564 MODULE_LICENSE("GPL");