serial: core, remove uart_update_termios
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / tty / serial / serial_core.c
blobd4bd465c4c41ea18574327c4b8f2547cf28452f3
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);
1473 static int uart_carrier_raised(struct tty_port *port)
1475 struct uart_state *state = container_of(port, struct uart_state, port);
1476 struct uart_port *uport = state->uart_port;
1477 int mctrl;
1478 spin_lock_irq(&uport->lock);
1479 uport->ops->enable_ms(uport);
1480 mctrl = uport->ops->get_mctrl(uport);
1481 spin_unlock_irq(&uport->lock);
1482 if (mctrl & TIOCM_CAR)
1483 return 1;
1484 return 0;
1487 static void uart_dtr_rts(struct tty_port *port, int onoff)
1489 struct uart_state *state = container_of(port, struct uart_state, port);
1490 struct uart_port *uport = state->uart_port;
1492 if (onoff)
1493 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1494 else
1495 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1498 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1500 struct uart_state *state;
1501 struct tty_port *port;
1502 int ret = 0;
1504 state = drv->state + line;
1505 port = &state->port;
1506 if (mutex_lock_interruptible(&port->mutex)) {
1507 ret = -ERESTARTSYS;
1508 goto err;
1511 port->count++;
1512 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1513 ret = -ENXIO;
1514 goto err_unlock;
1516 return state;
1518 err_unlock:
1519 port->count--;
1520 mutex_unlock(&port->mutex);
1521 err:
1522 return ERR_PTR(ret);
1526 * calls to uart_open are serialised by the BKL in
1527 * fs/char_dev.c:chrdev_open()
1528 * Note that if this fails, then uart_close() _will_ be called.
1530 * In time, we want to scrap the "opening nonpresent ports"
1531 * behaviour and implement an alternative way for setserial
1532 * to set base addresses/ports/types. This will allow us to
1533 * get rid of a certain amount of extra tests.
1535 static int uart_open(struct tty_struct *tty, struct file *filp)
1537 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1538 struct uart_state *state;
1539 struct tty_port *port;
1540 int retval, line = tty->index;
1542 BUG_ON(!tty_locked());
1543 pr_debug("uart_open(%d) called\n", line);
1546 * tty->driver->num won't change, so we won't fail here with
1547 * tty->driver_data set to something non-NULL (and therefore
1548 * we won't get caught by uart_close()).
1550 retval = -ENODEV;
1551 if (line >= tty->driver->num)
1552 goto fail;
1555 * We take the semaphore inside uart_get to guarantee that we won't
1556 * be re-entered while allocating the state structure, or while we
1557 * request any IRQs that the driver may need. This also has the nice
1558 * side-effect that it delays the action of uart_hangup, so we can
1559 * guarantee that state->port.tty will always contain something
1560 * reasonable.
1562 state = uart_get(drv, line);
1563 if (IS_ERR(state)) {
1564 retval = PTR_ERR(state);
1565 goto fail;
1567 port = &state->port;
1570 * Once we set tty->driver_data here, we are guaranteed that
1571 * uart_close() will decrement the driver module use count.
1572 * Any failures from here onwards should not touch the count.
1574 tty->driver_data = state;
1575 state->uart_port->state = state;
1576 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1577 tty->alt_speed = 0;
1578 tty_port_tty_set(port, tty);
1581 * If the port is in the middle of closing, bail out now.
1583 if (tty_hung_up_p(filp)) {
1584 retval = -EAGAIN;
1585 port->count--;
1586 mutex_unlock(&port->mutex);
1587 goto fail;
1591 * Make sure the device is in D0 state.
1593 if (port->count == 1)
1594 uart_change_pm(state, 0);
1597 * Start up the serial port.
1599 retval = uart_startup(tty, state, 0);
1602 * If we succeeded, wait until the port is ready.
1604 mutex_unlock(&port->mutex);
1605 if (retval == 0)
1606 retval = tty_port_block_til_ready(port, tty, filp);
1608 fail:
1609 return retval;
1612 static const char *uart_type(struct uart_port *port)
1614 const char *str = NULL;
1616 if (port->ops->type)
1617 str = port->ops->type(port);
1619 if (!str)
1620 str = "unknown";
1622 return str;
1625 #ifdef CONFIG_PROC_FS
1627 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1629 struct uart_state *state = drv->state + i;
1630 struct tty_port *port = &state->port;
1631 int pm_state;
1632 struct uart_port *uport = state->uart_port;
1633 char stat_buf[32];
1634 unsigned int status;
1635 int mmio;
1637 if (!uport)
1638 return;
1640 mmio = uport->iotype >= UPIO_MEM;
1641 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1642 uport->line, uart_type(uport),
1643 mmio ? "mmio:0x" : "port:",
1644 mmio ? (unsigned long long)uport->mapbase
1645 : (unsigned long long)uport->iobase,
1646 uport->irq);
1648 if (uport->type == PORT_UNKNOWN) {
1649 seq_putc(m, '\n');
1650 return;
1653 if (capable(CAP_SYS_ADMIN)) {
1654 mutex_lock(&port->mutex);
1655 pm_state = state->pm_state;
1656 if (pm_state)
1657 uart_change_pm(state, 0);
1658 spin_lock_irq(&uport->lock);
1659 status = uport->ops->get_mctrl(uport);
1660 spin_unlock_irq(&uport->lock);
1661 if (pm_state)
1662 uart_change_pm(state, pm_state);
1663 mutex_unlock(&port->mutex);
1665 seq_printf(m, " tx:%d rx:%d",
1666 uport->icount.tx, uport->icount.rx);
1667 if (uport->icount.frame)
1668 seq_printf(m, " fe:%d",
1669 uport->icount.frame);
1670 if (uport->icount.parity)
1671 seq_printf(m, " pe:%d",
1672 uport->icount.parity);
1673 if (uport->icount.brk)
1674 seq_printf(m, " brk:%d",
1675 uport->icount.brk);
1676 if (uport->icount.overrun)
1677 seq_printf(m, " oe:%d",
1678 uport->icount.overrun);
1680 #define INFOBIT(bit, str) \
1681 if (uport->mctrl & (bit)) \
1682 strncat(stat_buf, (str), sizeof(stat_buf) - \
1683 strlen(stat_buf) - 2)
1684 #define STATBIT(bit, str) \
1685 if (status & (bit)) \
1686 strncat(stat_buf, (str), sizeof(stat_buf) - \
1687 strlen(stat_buf) - 2)
1689 stat_buf[0] = '\0';
1690 stat_buf[1] = '\0';
1691 INFOBIT(TIOCM_RTS, "|RTS");
1692 STATBIT(TIOCM_CTS, "|CTS");
1693 INFOBIT(TIOCM_DTR, "|DTR");
1694 STATBIT(TIOCM_DSR, "|DSR");
1695 STATBIT(TIOCM_CAR, "|CD");
1696 STATBIT(TIOCM_RNG, "|RI");
1697 if (stat_buf[0])
1698 stat_buf[0] = ' ';
1700 seq_puts(m, stat_buf);
1702 seq_putc(m, '\n');
1703 #undef STATBIT
1704 #undef INFOBIT
1707 static int uart_proc_show(struct seq_file *m, void *v)
1709 struct tty_driver *ttydrv = m->private;
1710 struct uart_driver *drv = ttydrv->driver_state;
1711 int i;
1713 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1714 "", "", "");
1715 for (i = 0; i < drv->nr; i++)
1716 uart_line_info(m, drv, i);
1717 return 0;
1720 static int uart_proc_open(struct inode *inode, struct file *file)
1722 return single_open(file, uart_proc_show, PDE(inode)->data);
1725 static const struct file_operations uart_proc_fops = {
1726 .owner = THIS_MODULE,
1727 .open = uart_proc_open,
1728 .read = seq_read,
1729 .llseek = seq_lseek,
1730 .release = single_release,
1732 #endif
1734 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1736 * uart_console_write - write a console message to a serial port
1737 * @port: the port to write the message
1738 * @s: array of characters
1739 * @count: number of characters in string to write
1740 * @write: function to write character to port
1742 void uart_console_write(struct uart_port *port, const char *s,
1743 unsigned int count,
1744 void (*putchar)(struct uart_port *, int))
1746 unsigned int i;
1748 for (i = 0; i < count; i++, s++) {
1749 if (*s == '\n')
1750 putchar(port, '\r');
1751 putchar(port, *s);
1754 EXPORT_SYMBOL_GPL(uart_console_write);
1757 * Check whether an invalid uart number has been specified, and
1758 * if so, search for the first available port that does have
1759 * console support.
1761 struct uart_port * __init
1762 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1764 int idx = co->index;
1766 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1767 ports[idx].membase == NULL))
1768 for (idx = 0; idx < nr; idx++)
1769 if (ports[idx].iobase != 0 ||
1770 ports[idx].membase != NULL)
1771 break;
1773 co->index = idx;
1775 return ports + idx;
1779 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1780 * @options: pointer to option string
1781 * @baud: pointer to an 'int' variable for the baud rate.
1782 * @parity: pointer to an 'int' variable for the parity.
1783 * @bits: pointer to an 'int' variable for the number of data bits.
1784 * @flow: pointer to an 'int' variable for the flow control character.
1786 * uart_parse_options decodes a string containing the serial console
1787 * options. The format of the string is <baud><parity><bits><flow>,
1788 * eg: 115200n8r
1790 void
1791 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1793 char *s = options;
1795 *baud = simple_strtoul(s, NULL, 10);
1796 while (*s >= '0' && *s <= '9')
1797 s++;
1798 if (*s)
1799 *parity = *s++;
1800 if (*s)
1801 *bits = *s++ - '0';
1802 if (*s)
1803 *flow = *s;
1805 EXPORT_SYMBOL_GPL(uart_parse_options);
1807 struct baud_rates {
1808 unsigned int rate;
1809 unsigned int cflag;
1812 static const struct baud_rates baud_rates[] = {
1813 { 921600, B921600 },
1814 { 460800, B460800 },
1815 { 230400, B230400 },
1816 { 115200, B115200 },
1817 { 57600, B57600 },
1818 { 38400, B38400 },
1819 { 19200, B19200 },
1820 { 9600, B9600 },
1821 { 4800, B4800 },
1822 { 2400, B2400 },
1823 { 1200, B1200 },
1824 { 0, B38400 }
1828 * uart_set_options - setup the serial console parameters
1829 * @port: pointer to the serial ports uart_port structure
1830 * @co: console pointer
1831 * @baud: baud rate
1832 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1833 * @bits: number of data bits
1834 * @flow: flow control character - 'r' (rts)
1837 uart_set_options(struct uart_port *port, struct console *co,
1838 int baud, int parity, int bits, int flow)
1840 struct ktermios termios;
1841 static struct ktermios dummy;
1842 int i;
1845 * Ensure that the serial console lock is initialised
1846 * early.
1848 spin_lock_init(&port->lock);
1849 lockdep_set_class(&port->lock, &port_lock_key);
1851 memset(&termios, 0, sizeof(struct ktermios));
1853 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1856 * Construct a cflag setting.
1858 for (i = 0; baud_rates[i].rate; i++)
1859 if (baud_rates[i].rate <= baud)
1860 break;
1862 termios.c_cflag |= baud_rates[i].cflag;
1864 if (bits == 7)
1865 termios.c_cflag |= CS7;
1866 else
1867 termios.c_cflag |= CS8;
1869 switch (parity) {
1870 case 'o': case 'O':
1871 termios.c_cflag |= PARODD;
1872 /*fall through*/
1873 case 'e': case 'E':
1874 termios.c_cflag |= PARENB;
1875 break;
1878 if (flow == 'r')
1879 termios.c_cflag |= CRTSCTS;
1882 * some uarts on other side don't support no flow control.
1883 * So we set * DTR in host uart to make them happy
1885 port->mctrl |= TIOCM_DTR;
1887 port->ops->set_termios(port, &termios, &dummy);
1889 * Allow the setting of the UART parameters with a NULL console
1890 * too:
1892 if (co)
1893 co->cflag = termios.c_cflag;
1895 return 0;
1897 EXPORT_SYMBOL_GPL(uart_set_options);
1898 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1900 static void uart_change_pm(struct uart_state *state, int pm_state)
1902 struct uart_port *port = state->uart_port;
1904 if (state->pm_state != pm_state) {
1905 if (port->ops->pm)
1906 port->ops->pm(port, pm_state, state->pm_state);
1907 state->pm_state = pm_state;
1911 struct uart_match {
1912 struct uart_port *port;
1913 struct uart_driver *driver;
1916 static int serial_match_port(struct device *dev, void *data)
1918 struct uart_match *match = data;
1919 struct tty_driver *tty_drv = match->driver->tty_driver;
1920 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1921 match->port->line;
1923 return dev->devt == devt; /* Actually, only one tty per port */
1926 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1928 struct uart_state *state = drv->state + uport->line;
1929 struct tty_port *port = &state->port;
1930 struct device *tty_dev;
1931 struct uart_match match = {uport, drv};
1933 mutex_lock(&port->mutex);
1935 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1936 if (device_may_wakeup(tty_dev)) {
1937 if (!enable_irq_wake(uport->irq))
1938 uport->irq_wake = 1;
1939 put_device(tty_dev);
1940 mutex_unlock(&port->mutex);
1941 return 0;
1943 if (console_suspend_enabled || !uart_console(uport))
1944 uport->suspended = 1;
1946 if (port->flags & ASYNC_INITIALIZED) {
1947 const struct uart_ops *ops = uport->ops;
1948 int tries;
1950 if (console_suspend_enabled || !uart_console(uport)) {
1951 set_bit(ASYNCB_SUSPENDED, &port->flags);
1952 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1954 spin_lock_irq(&uport->lock);
1955 ops->stop_tx(uport);
1956 ops->set_mctrl(uport, 0);
1957 ops->stop_rx(uport);
1958 spin_unlock_irq(&uport->lock);
1962 * Wait for the transmitter to empty.
1964 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1965 msleep(10);
1966 if (!tries)
1967 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1968 "transmitter\n",
1969 uport->dev ? dev_name(uport->dev) : "",
1970 uport->dev ? ": " : "",
1971 drv->dev_name,
1972 drv->tty_driver->name_base + uport->line);
1974 if (console_suspend_enabled || !uart_console(uport))
1975 ops->shutdown(uport);
1979 * Disable the console device before suspending.
1981 if (console_suspend_enabled && uart_console(uport))
1982 console_stop(uport->cons);
1984 if (console_suspend_enabled || !uart_console(uport))
1985 uart_change_pm(state, 3);
1987 mutex_unlock(&port->mutex);
1989 return 0;
1992 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1994 struct uart_state *state = drv->state + uport->line;
1995 struct tty_port *port = &state->port;
1996 struct device *tty_dev;
1997 struct uart_match match = {uport, drv};
1998 struct ktermios termios;
2000 mutex_lock(&port->mutex);
2002 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2003 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2004 if (uport->irq_wake) {
2005 disable_irq_wake(uport->irq);
2006 uport->irq_wake = 0;
2008 mutex_unlock(&port->mutex);
2009 return 0;
2011 uport->suspended = 0;
2014 * Re-enable the console device after suspending.
2016 if (uart_console(uport)) {
2018 * First try to use the console cflag setting.
2020 memset(&termios, 0, sizeof(struct ktermios));
2021 termios.c_cflag = uport->cons->cflag;
2024 * If that's unset, use the tty termios setting.
2026 if (port->tty && port->tty->termios && termios.c_cflag == 0)
2027 termios = *(port->tty->termios);
2029 uport->ops->set_termios(uport, &termios, NULL);
2030 if (console_suspend_enabled)
2031 console_start(uport->cons);
2034 if (port->flags & ASYNC_SUSPENDED) {
2035 const struct uart_ops *ops = uport->ops;
2036 int ret;
2038 uart_change_pm(state, 0);
2039 spin_lock_irq(&uport->lock);
2040 ops->set_mctrl(uport, 0);
2041 spin_unlock_irq(&uport->lock);
2042 if (console_suspend_enabled || !uart_console(uport)) {
2043 /* Protected by port mutex for now */
2044 struct tty_struct *tty = port->tty;
2045 ret = ops->startup(uport);
2046 if (ret == 0) {
2047 if (tty)
2048 uart_change_speed(tty, state, NULL);
2049 spin_lock_irq(&uport->lock);
2050 ops->set_mctrl(uport, uport->mctrl);
2051 ops->start_tx(uport);
2052 spin_unlock_irq(&uport->lock);
2053 set_bit(ASYNCB_INITIALIZED, &port->flags);
2054 } else {
2056 * Failed to resume - maybe hardware went away?
2057 * Clear the "initialized" flag so we won't try
2058 * to call the low level drivers shutdown method.
2060 uart_shutdown(tty, state);
2064 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2067 mutex_unlock(&port->mutex);
2069 return 0;
2072 static inline void
2073 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2075 char address[64];
2077 switch (port->iotype) {
2078 case UPIO_PORT:
2079 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2080 break;
2081 case UPIO_HUB6:
2082 snprintf(address, sizeof(address),
2083 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2084 break;
2085 case UPIO_MEM:
2086 case UPIO_MEM32:
2087 case UPIO_AU:
2088 case UPIO_TSI:
2089 case UPIO_DWAPB:
2090 case UPIO_DWAPB32:
2091 snprintf(address, sizeof(address),
2092 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2093 break;
2094 default:
2095 strlcpy(address, "*unknown*", sizeof(address));
2096 break;
2099 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2100 port->dev ? dev_name(port->dev) : "",
2101 port->dev ? ": " : "",
2102 drv->dev_name,
2103 drv->tty_driver->name_base + port->line,
2104 address, port->irq, uart_type(port));
2107 static void
2108 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2109 struct uart_port *port)
2111 unsigned int flags;
2114 * If there isn't a port here, don't do anything further.
2116 if (!port->iobase && !port->mapbase && !port->membase)
2117 return;
2120 * Now do the auto configuration stuff. Note that config_port
2121 * is expected to claim the resources and map the port for us.
2123 flags = 0;
2124 if (port->flags & UPF_AUTO_IRQ)
2125 flags |= UART_CONFIG_IRQ;
2126 if (port->flags & UPF_BOOT_AUTOCONF) {
2127 if (!(port->flags & UPF_FIXED_TYPE)) {
2128 port->type = PORT_UNKNOWN;
2129 flags |= UART_CONFIG_TYPE;
2131 port->ops->config_port(port, flags);
2134 if (port->type != PORT_UNKNOWN) {
2135 unsigned long flags;
2137 uart_report_port(drv, port);
2139 /* Power up port for set_mctrl() */
2140 uart_change_pm(state, 0);
2143 * Ensure that the modem control lines are de-activated.
2144 * keep the DTR setting that is set in uart_set_options()
2145 * We probably don't need a spinlock around this, but
2147 spin_lock_irqsave(&port->lock, flags);
2148 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2149 spin_unlock_irqrestore(&port->lock, flags);
2152 * If this driver supports console, and it hasn't been
2153 * successfully registered yet, try to re-register it.
2154 * It may be that the port was not available.
2156 if (port->cons && !(port->cons->flags & CON_ENABLED))
2157 register_console(port->cons);
2160 * Power down all ports by default, except the
2161 * console if we have one.
2163 if (!uart_console(port))
2164 uart_change_pm(state, 3);
2168 #ifdef CONFIG_CONSOLE_POLL
2170 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2172 struct uart_driver *drv = driver->driver_state;
2173 struct uart_state *state = drv->state + line;
2174 struct uart_port *port;
2175 int baud = 9600;
2176 int bits = 8;
2177 int parity = 'n';
2178 int flow = 'n';
2180 if (!state || !state->uart_port)
2181 return -1;
2183 port = state->uart_port;
2184 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2185 return -1;
2187 if (options) {
2188 uart_parse_options(options, &baud, &parity, &bits, &flow);
2189 return uart_set_options(port, NULL, baud, parity, bits, flow);
2192 return 0;
2195 static int uart_poll_get_char(struct tty_driver *driver, int line)
2197 struct uart_driver *drv = driver->driver_state;
2198 struct uart_state *state = drv->state + line;
2199 struct uart_port *port;
2201 if (!state || !state->uart_port)
2202 return -1;
2204 port = state->uart_port;
2205 return port->ops->poll_get_char(port);
2208 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2210 struct uart_driver *drv = driver->driver_state;
2211 struct uart_state *state = drv->state + line;
2212 struct uart_port *port;
2214 if (!state || !state->uart_port)
2215 return;
2217 port = state->uart_port;
2218 port->ops->poll_put_char(port, ch);
2220 #endif
2222 static const struct tty_operations uart_ops = {
2223 .open = uart_open,
2224 .close = uart_close,
2225 .write = uart_write,
2226 .put_char = uart_put_char,
2227 .flush_chars = uart_flush_chars,
2228 .write_room = uart_write_room,
2229 .chars_in_buffer= uart_chars_in_buffer,
2230 .flush_buffer = uart_flush_buffer,
2231 .ioctl = uart_ioctl,
2232 .throttle = uart_throttle,
2233 .unthrottle = uart_unthrottle,
2234 .send_xchar = uart_send_xchar,
2235 .set_termios = uart_set_termios,
2236 .set_ldisc = uart_set_ldisc,
2237 .stop = uart_stop,
2238 .start = uart_start,
2239 .hangup = uart_hangup,
2240 .break_ctl = uart_break_ctl,
2241 .wait_until_sent= uart_wait_until_sent,
2242 #ifdef CONFIG_PROC_FS
2243 .proc_fops = &uart_proc_fops,
2244 #endif
2245 .tiocmget = uart_tiocmget,
2246 .tiocmset = uart_tiocmset,
2247 .get_icount = uart_get_icount,
2248 #ifdef CONFIG_CONSOLE_POLL
2249 .poll_init = uart_poll_init,
2250 .poll_get_char = uart_poll_get_char,
2251 .poll_put_char = uart_poll_put_char,
2252 #endif
2255 static const struct tty_port_operations uart_port_ops = {
2256 .carrier_raised = uart_carrier_raised,
2257 .dtr_rts = uart_dtr_rts,
2261 * uart_register_driver - register a driver with the uart core layer
2262 * @drv: low level driver structure
2264 * Register a uart driver with the core driver. We in turn register
2265 * with the tty layer, and initialise the core driver per-port state.
2267 * We have a proc file in /proc/tty/driver which is named after the
2268 * normal driver.
2270 * drv->port should be NULL, and the per-port structures should be
2271 * registered using uart_add_one_port after this call has succeeded.
2273 int uart_register_driver(struct uart_driver *drv)
2275 struct tty_driver *normal;
2276 int i, retval;
2278 BUG_ON(drv->state);
2281 * Maybe we should be using a slab cache for this, especially if
2282 * we have a large number of ports to handle.
2284 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2285 if (!drv->state)
2286 goto out;
2288 normal = alloc_tty_driver(drv->nr);
2289 if (!normal)
2290 goto out_kfree;
2292 drv->tty_driver = normal;
2294 normal->owner = drv->owner;
2295 normal->driver_name = drv->driver_name;
2296 normal->name = drv->dev_name;
2297 normal->major = drv->major;
2298 normal->minor_start = drv->minor;
2299 normal->type = TTY_DRIVER_TYPE_SERIAL;
2300 normal->subtype = SERIAL_TYPE_NORMAL;
2301 normal->init_termios = tty_std_termios;
2302 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2303 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2304 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2305 normal->driver_state = drv;
2306 tty_set_operations(normal, &uart_ops);
2309 * Initialise the UART state(s).
2311 for (i = 0; i < drv->nr; i++) {
2312 struct uart_state *state = drv->state + i;
2313 struct tty_port *port = &state->port;
2315 tty_port_init(port);
2316 port->ops = &uart_port_ops;
2317 port->close_delay = 500; /* .5 seconds */
2318 port->closing_wait = 30000; /* 30 seconds */
2319 tasklet_init(&state->tlet, uart_tasklet_action,
2320 (unsigned long)state);
2323 retval = tty_register_driver(normal);
2324 if (retval >= 0)
2325 return retval;
2327 put_tty_driver(normal);
2328 out_kfree:
2329 kfree(drv->state);
2330 out:
2331 return -ENOMEM;
2335 * uart_unregister_driver - remove a driver from the uart core layer
2336 * @drv: low level driver structure
2338 * Remove all references to a driver from the core driver. The low
2339 * level driver must have removed all its ports via the
2340 * uart_remove_one_port() if it registered them with uart_add_one_port().
2341 * (ie, drv->port == NULL)
2343 void uart_unregister_driver(struct uart_driver *drv)
2345 struct tty_driver *p = drv->tty_driver;
2346 tty_unregister_driver(p);
2347 put_tty_driver(p);
2348 kfree(drv->state);
2349 drv->tty_driver = NULL;
2352 struct tty_driver *uart_console_device(struct console *co, int *index)
2354 struct uart_driver *p = co->data;
2355 *index = co->index;
2356 return p->tty_driver;
2360 * uart_add_one_port - attach a driver-defined port structure
2361 * @drv: pointer to the uart low level driver structure for this port
2362 * @uport: uart port structure to use for this port.
2364 * This allows the driver to register its own uart_port structure
2365 * with the core driver. The main purpose is to allow the low
2366 * level uart drivers to expand uart_port, rather than having yet
2367 * more levels of structures.
2369 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2371 struct uart_state *state;
2372 struct tty_port *port;
2373 int ret = 0;
2374 struct device *tty_dev;
2376 BUG_ON(in_interrupt());
2378 if (uport->line >= drv->nr)
2379 return -EINVAL;
2381 state = drv->state + uport->line;
2382 port = &state->port;
2384 mutex_lock(&port_mutex);
2385 mutex_lock(&port->mutex);
2386 if (state->uart_port) {
2387 ret = -EINVAL;
2388 goto out;
2391 state->uart_port = uport;
2392 state->pm_state = -1;
2394 uport->cons = drv->cons;
2395 uport->state = state;
2398 * If this port is a console, then the spinlock is already
2399 * initialised.
2401 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2402 spin_lock_init(&uport->lock);
2403 lockdep_set_class(&uport->lock, &port_lock_key);
2406 uart_configure_port(drv, state, uport);
2409 * Register the port whether it's detected or not. This allows
2410 * setserial to be used to alter this ports parameters.
2412 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2413 if (likely(!IS_ERR(tty_dev))) {
2414 device_init_wakeup(tty_dev, 1);
2415 device_set_wakeup_enable(tty_dev, 0);
2416 } else
2417 printk(KERN_ERR "Cannot register tty device on line %d\n",
2418 uport->line);
2421 * Ensure UPF_DEAD is not set.
2423 uport->flags &= ~UPF_DEAD;
2425 out:
2426 mutex_unlock(&port->mutex);
2427 mutex_unlock(&port_mutex);
2429 return ret;
2433 * uart_remove_one_port - detach a driver defined port structure
2434 * @drv: pointer to the uart low level driver structure for this port
2435 * @uport: uart port structure for this port
2437 * This unhooks (and hangs up) the specified port structure from the
2438 * core driver. No further calls will be made to the low-level code
2439 * for this port.
2441 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2443 struct uart_state *state = drv->state + uport->line;
2444 struct tty_port *port = &state->port;
2446 BUG_ON(in_interrupt());
2448 if (state->uart_port != uport)
2449 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2450 state->uart_port, uport);
2452 mutex_lock(&port_mutex);
2455 * Mark the port "dead" - this prevents any opens from
2456 * succeeding while we shut down the port.
2458 mutex_lock(&port->mutex);
2459 uport->flags |= UPF_DEAD;
2460 mutex_unlock(&port->mutex);
2463 * Remove the devices from the tty layer
2465 tty_unregister_device(drv->tty_driver, uport->line);
2467 if (port->tty)
2468 tty_vhangup(port->tty);
2471 * Free the port IO and memory resources, if any.
2473 if (uport->type != PORT_UNKNOWN)
2474 uport->ops->release_port(uport);
2477 * Indicate that there isn't a port here anymore.
2479 uport->type = PORT_UNKNOWN;
2482 * Kill the tasklet, and free resources.
2484 tasklet_kill(&state->tlet);
2486 state->uart_port = NULL;
2487 mutex_unlock(&port_mutex);
2489 return 0;
2493 * Are the two ports equivalent?
2495 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2497 if (port1->iotype != port2->iotype)
2498 return 0;
2500 switch (port1->iotype) {
2501 case UPIO_PORT:
2502 return (port1->iobase == port2->iobase);
2503 case UPIO_HUB6:
2504 return (port1->iobase == port2->iobase) &&
2505 (port1->hub6 == port2->hub6);
2506 case UPIO_MEM:
2507 case UPIO_MEM32:
2508 case UPIO_AU:
2509 case UPIO_TSI:
2510 case UPIO_DWAPB:
2511 case UPIO_DWAPB32:
2512 return (port1->mapbase == port2->mapbase);
2514 return 0;
2516 EXPORT_SYMBOL(uart_match_port);
2518 EXPORT_SYMBOL(uart_write_wakeup);
2519 EXPORT_SYMBOL(uart_register_driver);
2520 EXPORT_SYMBOL(uart_unregister_driver);
2521 EXPORT_SYMBOL(uart_suspend_port);
2522 EXPORT_SYMBOL(uart_resume_port);
2523 EXPORT_SYMBOL(uart_add_one_port);
2524 EXPORT_SYMBOL(uart_remove_one_port);
2526 MODULE_DESCRIPTION("Serial driver core");
2527 MODULE_LICENSE("GPL");