MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / serial / serial_core.c
blob8804e363bbf23221b1e53d5499d10cea5f54adce
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/serial_core.h>
31 #include <linux/smp_lock.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/delay.h>
35 #include <linux/mutex.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #if 1 // add by Victor Yu. 02-09-2007
40 #include <asm/arch/moxa.h>
41 #include <asm/arch/gpio.h>
42 #include <asm/io.h>
43 #include <linux/kd.h>
44 #endif
46 #undef DEBUG
47 #ifdef DEBUG
48 #define DPRINTK(x...) printk(x)
49 #else
50 #define DPRINTK(x...) do { } while (0)
51 #endif
54 * This is used to lock changes in serial line configuration.
56 static DEFINE_MUTEX(port_mutex);
59 * lockdep: port->lock is initialized in two places, but we
60 * want only one lock-class:
62 static struct lock_class_key port_lock_key;
64 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
66 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
68 #ifdef CONFIG_SERIAL_CORE_CONSOLE
69 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
70 #else
71 #define uart_console(port) (0)
72 #endif
74 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
75 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
76 static void uart_change_pm(struct uart_state *state, int pm_state);
79 * This routine is used by the interrupt handler to schedule processing in
80 * the software interrupt portion of the driver.
82 void uart_write_wakeup(struct uart_port *port)
84 struct uart_info *info = port->info;
86 * This means you called this function _after_ the port was
87 * closed. No cookie for you.
89 BUG_ON(!info);
90 tasklet_schedule(&info->tlet);
93 static void uart_stop(struct tty_struct *tty)
95 struct uart_state *state = tty->driver_data;
96 struct uart_port *port = state->port;
97 unsigned long flags;
99 spin_lock_irqsave(&port->lock, flags);
100 port->ops->stop_tx(port);
101 spin_unlock_irqrestore(&port->lock, flags);
104 static void __uart_start(struct tty_struct *tty)
106 struct uart_state *state = tty->driver_data;
107 struct uart_port *port = state->port;
109 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
110 !tty->stopped && !tty->hw_stopped)
111 port->ops->start_tx(port);
114 static void uart_start(struct tty_struct *tty)
116 struct uart_state *state = tty->driver_data;
117 struct uart_port *port = state->port;
118 unsigned long flags;
120 spin_lock_irqsave(&port->lock, flags);
121 __uart_start(tty);
122 spin_unlock_irqrestore(&port->lock, flags);
125 static void uart_tasklet_action(unsigned long data)
127 struct uart_state *state = (struct uart_state *)data;
128 tty_wakeup(state->info->tty);
131 static inline void
132 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
134 unsigned long flags;
135 unsigned int old;
137 spin_lock_irqsave(&port->lock, flags);
138 old = port->mctrl;
139 port->mctrl = (old & ~clear) | set;
140 if (old != port->mctrl)
141 port->ops->set_mctrl(port, port->mctrl);
142 spin_unlock_irqrestore(&port->lock, flags);
145 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
146 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
149 * Startup the port. This will be called once per open. All calls
150 * will be serialised by the per-port semaphore.
152 static int uart_startup(struct uart_state *state, int init_hw)
154 struct uart_info *info = state->info;
155 struct uart_port *port = state->port;
156 unsigned long page;
157 int retval = 0;
159 if (info->flags & UIF_INITIALIZED)
160 return 0;
163 * Set the TTY IO error marker - we will only clear this
164 * once we have successfully opened the port. Also set
165 * up the tty->alt_speed kludge
167 set_bit(TTY_IO_ERROR, &info->tty->flags);
169 if (port->type == PORT_UNKNOWN)
170 return 0;
173 * Initialise and allocate the transmit and temporary
174 * buffer.
176 if (!info->xmit.buf) {
177 page = get_zeroed_page(GFP_KERNEL);
178 if (!page)
179 return -ENOMEM;
181 info->xmit.buf = (unsigned char *) page;
182 uart_circ_clear(&info->xmit);
185 retval = port->ops->startup(port);
186 if (retval == 0) {
187 if (init_hw) {
189 * Initialise the hardware port settings.
191 uart_change_speed(state, NULL);
194 * Setup the RTS and DTR signals once the
195 * port is open and ready to respond.
197 if (info->tty->termios->c_cflag & CBAUD)
198 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
201 if (info->flags & UIF_CTS_FLOW) {
202 spin_lock_irq(&port->lock);
203 if (!(port->ops->get_mctrl(port) & TIOCM_CTS))
204 info->tty->hw_stopped = 1;
205 spin_unlock_irq(&port->lock);
208 info->flags |= UIF_INITIALIZED;
210 clear_bit(TTY_IO_ERROR, &info->tty->flags);
213 if (retval && capable(CAP_SYS_ADMIN))
214 retval = 0;
216 return retval;
220 * This routine will shutdown a serial port; interrupts are disabled, and
221 * DTR is dropped if the hangup on close termio flag is on. Calls to
222 * uart_shutdown are serialised by the per-port semaphore.
224 static void uart_shutdown(struct uart_state *state)
226 struct uart_info *info = state->info;
227 struct uart_port *port = state->port;
230 * Set the TTY IO error marker
232 if (info->tty)
233 set_bit(TTY_IO_ERROR, &info->tty->flags);
235 if (info->flags & UIF_INITIALIZED) {
236 info->flags &= ~UIF_INITIALIZED;
239 * Turn off DTR and RTS early.
241 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
242 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
245 * clear delta_msr_wait queue to avoid mem leaks: we may free
246 * the irq here so the queue might never be woken up. Note
247 * that we won't end up waiting on delta_msr_wait again since
248 * any outstanding file descriptors should be pointing at
249 * hung_up_tty_fops now.
251 wake_up_interruptible(&info->delta_msr_wait);
254 * Free the IRQ and disable the port.
256 port->ops->shutdown(port);
259 * Ensure that the IRQ handler isn't running on another CPU.
261 synchronize_irq(port->irq);
265 * kill off our tasklet
267 tasklet_kill(&info->tlet);
270 * Free the transmit buffer page.
272 if (info->xmit.buf) {
273 free_page((unsigned long)info->xmit.buf);
274 info->xmit.buf = NULL;
279 * uart_update_timeout - update per-port FIFO timeout.
280 * @port: uart_port structure describing the port
281 * @cflag: termios cflag value
282 * @baud: speed of the port
284 * Set the port FIFO timeout value. The @cflag value should
285 * reflect the actual hardware settings.
287 void
288 uart_update_timeout(struct uart_port *port, unsigned int cflag,
289 unsigned int baud)
291 unsigned int bits;
293 /* byte size and parity */
294 switch (cflag & CSIZE) {
295 case CS5:
296 bits = 7;
297 break;
298 case CS6:
299 bits = 8;
300 break;
301 case CS7:
302 bits = 9;
303 break;
304 default:
305 bits = 10;
306 break; // CS8
309 if (cflag & CSTOPB)
310 bits++;
311 if (cflag & PARENB)
312 bits++;
315 * The total number of bits to be transmitted in the fifo.
317 bits = bits * port->fifosize;
320 * Figure the timeout to send the above number of bits.
321 * Add .02 seconds of slop
323 port->timeout = (HZ * bits) / baud + HZ/50;
326 EXPORT_SYMBOL(uart_update_timeout);
329 * uart_get_baud_rate - return baud rate for a particular port
330 * @port: uart_port structure describing the port in question.
331 * @termios: desired termios settings.
332 * @old: old termios (or NULL)
333 * @min: minimum acceptable baud rate
334 * @max: maximum acceptable baud rate
336 * Decode the termios structure into a numeric baud rate,
337 * taking account of the magic 38400 baud rate (with spd_*
338 * flags), and mapping the %B0 rate to 9600 baud.
340 * If the new baud rate is invalid, try the old termios setting.
341 * If it's still invalid, we try 9600 baud.
343 * Update the @termios structure to reflect the baud rate
344 * we're actually going to be using.
346 unsigned int
347 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
348 struct termios *old, unsigned int min, unsigned int max)
350 unsigned int try, baud, altbaud = 38400;
351 upf_t flags = port->flags & UPF_SPD_MASK;
353 if (flags == UPF_SPD_HI)
354 altbaud = 57600;
355 if (flags == UPF_SPD_VHI)
356 altbaud = 115200;
357 if (flags == UPF_SPD_SHI)
358 altbaud = 230400;
359 if (flags == UPF_SPD_WARP)
360 altbaud = 460800;
362 for (try = 0; try < 2; try++) {
363 baud = tty_termios_baud_rate(termios);
366 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
367 * Die! Die! Die!
369 if (baud == 38400)
370 baud = altbaud;
373 * Special case: B0 rate.
375 if (baud == 0)
376 baud = 9600;
378 if (baud >= min && baud <= max)
379 return baud;
382 * Oops, the quotient was zero. Try again with
383 * the old baud rate if possible.
385 termios->c_cflag &= ~CBAUD;
386 if (old) {
387 termios->c_cflag |= old->c_cflag & CBAUD;
388 old = NULL;
389 continue;
393 * As a last resort, if the quotient is zero,
394 * default to 9600 bps
396 termios->c_cflag |= B9600;
399 return 0;
402 EXPORT_SYMBOL(uart_get_baud_rate);
405 * uart_get_divisor - return uart clock divisor
406 * @port: uart_port structure describing the port.
407 * @baud: desired baud rate
409 * Calculate the uart clock divisor for the port.
411 unsigned int
412 uart_get_divisor(struct uart_port *port, unsigned int baud)
414 unsigned int quot;
417 * Old custom speed handling.
419 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
420 quot = port->custom_divisor;
421 else
422 quot = (port->uartclk + (8 * baud)) / (16 * baud);
424 return quot;
427 EXPORT_SYMBOL(uart_get_divisor);
429 static void
430 uart_change_speed(struct uart_state *state, struct termios *old_termios)
432 struct tty_struct *tty = state->info->tty;
433 struct uart_port *port = state->port;
434 struct termios *termios;
437 * If we have no tty, termios, or the port does not exist,
438 * then we can't set the parameters for this port.
440 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
441 return;
443 termios = tty->termios;
446 * Set flags based on termios cflag
448 if (termios->c_cflag & CRTSCTS)
449 state->info->flags |= UIF_CTS_FLOW;
450 else
451 state->info->flags &= ~UIF_CTS_FLOW;
453 if (termios->c_cflag & CLOCAL)
454 state->info->flags &= ~UIF_CHECK_CD;
455 else
456 state->info->flags |= UIF_CHECK_CD;
458 port->ops->set_termios(port, termios, old_termios);
461 static inline void
462 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
464 unsigned long flags;
466 if (!circ->buf)
467 return;
469 spin_lock_irqsave(&port->lock, flags);
470 if (uart_circ_chars_free(circ) != 0) {
471 circ->buf[circ->head] = c;
472 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
474 spin_unlock_irqrestore(&port->lock, flags);
477 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
479 struct uart_state *state = tty->driver_data;
481 __uart_put_char(state->port, &state->info->xmit, ch);
484 static void uart_flush_chars(struct tty_struct *tty)
486 uart_start(tty);
489 static int
490 uart_write(struct tty_struct *tty, const unsigned char *buf, int count)
492 struct uart_state *state = tty->driver_data;
493 struct uart_port *port;
494 struct circ_buf *circ;
495 unsigned long flags;
496 int c, ret = 0;
499 * This means you called this function _after_ the port was
500 * closed. No cookie for you.
502 if (!state || !state->info) {
503 WARN_ON(1);
504 return -EL3HLT;
507 port = state->port;
508 circ = &state->info->xmit;
510 if (!circ->buf)
511 return 0;
513 spin_lock_irqsave(&port->lock, flags);
514 while (1) {
515 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
516 if (count < c)
517 c = count;
518 if (c <= 0)
519 break;
520 memcpy(circ->buf + circ->head, buf, c);
521 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
522 buf += c;
523 count -= c;
524 ret += c;
526 spin_unlock_irqrestore(&port->lock, flags);
528 uart_start(tty);
529 return ret;
532 static int uart_write_room(struct tty_struct *tty)
534 struct uart_state *state = tty->driver_data;
536 return uart_circ_chars_free(&state->info->xmit);
539 static int uart_chars_in_buffer(struct tty_struct *tty)
541 struct uart_state *state = tty->driver_data;
543 return uart_circ_chars_pending(&state->info->xmit);
546 static void uart_flush_buffer(struct tty_struct *tty)
548 struct uart_state *state = tty->driver_data;
549 struct uart_port *port = state->port;
550 unsigned long flags;
553 * This means you called this function _after_ the port was
554 * closed. No cookie for you.
556 if (!state || !state->info) {
557 WARN_ON(1);
558 return;
561 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
563 spin_lock_irqsave(&port->lock, flags);
564 uart_circ_clear(&state->info->xmit);
565 spin_unlock_irqrestore(&port->lock, flags);
566 tty_wakeup(tty);
570 * This function is used to send a high-priority XON/XOFF character to
571 * the device
573 static void uart_send_xchar(struct tty_struct *tty, char ch)
575 struct uart_state *state = tty->driver_data;
576 struct uart_port *port = state->port;
577 unsigned long flags;
579 if (port->ops->send_xchar)
580 port->ops->send_xchar(port, ch);
581 else {
582 port->x_char = ch;
583 if (ch) {
584 spin_lock_irqsave(&port->lock, flags);
585 port->ops->start_tx(port);
586 spin_unlock_irqrestore(&port->lock, flags);
591 static void uart_throttle(struct tty_struct *tty)
593 struct uart_state *state = tty->driver_data;
595 if (I_IXOFF(tty))
596 uart_send_xchar(tty, STOP_CHAR(tty));
598 if (tty->termios->c_cflag & CRTSCTS)
599 uart_clear_mctrl(state->port, TIOCM_RTS);
602 static void uart_unthrottle(struct tty_struct *tty)
604 struct uart_state *state = tty->driver_data;
605 struct uart_port *port = state->port;
607 if (I_IXOFF(tty)) {
608 if (port->x_char)
609 port->x_char = 0;
610 else
611 uart_send_xchar(tty, START_CHAR(tty));
614 if (tty->termios->c_cflag & CRTSCTS)
615 uart_set_mctrl(port, TIOCM_RTS);
618 static int uart_get_info(struct uart_state *state,
619 struct serial_struct __user *retinfo)
621 struct uart_port *port = state->port;
622 struct serial_struct tmp;
624 memset(&tmp, 0, sizeof(tmp));
625 tmp.type = port->type;
626 tmp.line = port->line;
627 tmp.port = port->iobase;
628 if (HIGH_BITS_OFFSET)
629 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
630 tmp.irq = port->irq;
631 tmp.flags = port->flags;
632 tmp.xmit_fifo_size = port->fifosize;
633 tmp.baud_base = port->uartclk / 16;
634 tmp.close_delay = state->close_delay / 10;
635 tmp.closing_wait = state->closing_wait == USF_CLOSING_WAIT_NONE ?
636 ASYNC_CLOSING_WAIT_NONE :
637 state->closing_wait / 10;
638 tmp.custom_divisor = port->custom_divisor;
639 tmp.hub6 = port->hub6;
640 tmp.io_type = port->iotype;
641 tmp.iomem_reg_shift = port->regshift;
642 tmp.iomem_base = (void *)port->mapbase;
644 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
645 return -EFAULT;
646 return 0;
649 static int uart_set_info(struct uart_state *state,
650 struct serial_struct __user *newinfo)
652 struct serial_struct new_serial;
653 struct uart_port *port = state->port;
654 unsigned long new_port;
655 unsigned int change_irq, change_port, closing_wait;
656 unsigned int old_custom_divisor, close_delay;
657 upf_t old_flags, new_flags;
658 int retval = 0;
660 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
661 return -EFAULT;
663 new_port = new_serial.port;
664 if (HIGH_BITS_OFFSET)
665 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
667 new_serial.irq = irq_canonicalize(new_serial.irq);
668 close_delay = new_serial.close_delay * 10;
669 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
670 USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
673 * This semaphore protects state->count. It is also
674 * very useful to prevent opens. Also, take the
675 * port configuration semaphore to make sure that a
676 * module insertion/removal doesn't change anything
677 * under us.
679 mutex_lock(&state->mutex);
681 change_irq = new_serial.irq != port->irq;
684 * Since changing the 'type' of the port changes its resource
685 * allocations, we should treat type changes the same as
686 * IO port changes.
688 change_port = new_port != port->iobase ||
689 (unsigned long)new_serial.iomem_base != port->mapbase ||
690 new_serial.hub6 != port->hub6 ||
691 new_serial.io_type != port->iotype ||
692 new_serial.iomem_reg_shift != port->regshift ||
693 new_serial.type != port->type;
695 old_flags = port->flags;
696 new_flags = new_serial.flags;
697 old_custom_divisor = port->custom_divisor;
699 if (!capable(CAP_SYS_ADMIN)) {
700 retval = -EPERM;
701 if (change_irq || change_port ||
702 (new_serial.baud_base != port->uartclk / 16) ||
703 (close_delay != state->close_delay) ||
704 (closing_wait != state->closing_wait) ||
705 (new_serial.xmit_fifo_size &&
706 new_serial.xmit_fifo_size != port->fifosize) ||
707 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
708 goto exit;
709 port->flags = ((port->flags & ~UPF_USR_MASK) |
710 (new_flags & UPF_USR_MASK));
711 port->custom_divisor = new_serial.custom_divisor;
712 goto check_and_exit;
716 * Ask the low level driver to verify the settings.
718 if (port->ops->verify_port)
719 retval = port->ops->verify_port(port, &new_serial);
721 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
722 (new_serial.baud_base < 9600))
723 retval = -EINVAL;
725 if (retval)
726 goto exit;
728 if (change_port || change_irq) {
729 retval = -EBUSY;
732 * Make sure that we are the sole user of this port.
734 if (uart_users(state) > 1)
735 goto exit;
738 * We need to shutdown the serial port at the old
739 * port/type/irq combination.
741 uart_shutdown(state);
744 if (change_port) {
745 unsigned long old_iobase, old_mapbase;
746 unsigned int old_type, old_iotype, old_hub6, old_shift;
748 old_iobase = port->iobase;
749 old_mapbase = port->mapbase;
750 old_type = port->type;
751 old_hub6 = port->hub6;
752 old_iotype = port->iotype;
753 old_shift = port->regshift;
756 * Free and release old regions
758 if (old_type != PORT_UNKNOWN)
759 port->ops->release_port(port);
761 port->iobase = new_port;
762 port->type = new_serial.type;
763 port->hub6 = new_serial.hub6;
764 port->iotype = new_serial.io_type;
765 port->regshift = new_serial.iomem_reg_shift;
766 port->mapbase = (unsigned long)new_serial.iomem_base;
769 * Claim and map the new regions
771 if (port->type != PORT_UNKNOWN) {
772 retval = port->ops->request_port(port);
773 } else {
774 /* Always success - Jean II */
775 retval = 0;
779 * If we fail to request resources for the
780 * new port, try to restore the old settings.
782 if (retval && old_type != PORT_UNKNOWN) {
783 port->iobase = old_iobase;
784 port->type = old_type;
785 port->hub6 = old_hub6;
786 port->iotype = old_iotype;
787 port->regshift = old_shift;
788 port->mapbase = old_mapbase;
789 retval = port->ops->request_port(port);
791 * If we failed to restore the old settings,
792 * we fail like this.
794 if (retval)
795 port->type = PORT_UNKNOWN;
798 * We failed anyway.
800 retval = -EBUSY;
801 goto exit; // Added to return the correct error -Ram Gupta
805 port->irq = new_serial.irq;
806 port->uartclk = new_serial.baud_base * 16;
807 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
808 (new_flags & UPF_CHANGE_MASK);
809 port->custom_divisor = new_serial.custom_divisor;
810 state->close_delay = close_delay;
811 state->closing_wait = closing_wait;
812 if (new_serial.xmit_fifo_size)
813 port->fifosize = new_serial.xmit_fifo_size;
814 if (state->info->tty)
815 state->info->tty->low_latency =
816 (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
818 check_and_exit:
819 retval = 0;
820 if (port->type == PORT_UNKNOWN)
821 goto exit;
822 if (state->info->flags & UIF_INITIALIZED) {
823 if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
824 old_custom_divisor != port->custom_divisor) {
826 * If they're setting up a custom divisor or speed,
827 * instead of clearing it, then bitch about it. No
828 * need to rate-limit; it's CAP_SYS_ADMIN only.
830 if (port->flags & UPF_SPD_MASK) {
831 char buf[64];
832 printk(KERN_NOTICE
833 "%s sets custom speed on %s. This "
834 "is deprecated.\n", current->comm,
835 tty_name(state->info->tty, buf));
837 uart_change_speed(state, NULL);
839 } else
840 retval = uart_startup(state, 1);
841 exit:
842 mutex_unlock(&state->mutex);
843 return retval;
848 * uart_get_lsr_info - get line status register info.
849 * Note: uart_ioctl protects us against hangups.
851 static int uart_get_lsr_info(struct uart_state *state,
852 unsigned int __user *value)
854 struct uart_port *port = state->port;
855 unsigned int result;
857 result = port->ops->tx_empty(port);
860 * If we're about to load something into the transmit
861 * register, we'll pretend the transmitter isn't empty to
862 * avoid a race condition (depending on when the transmit
863 * interrupt happens).
865 if (port->x_char ||
866 ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
867 !state->info->tty->stopped && !state->info->tty->hw_stopped))
868 result &= ~TIOCSER_TEMT;
870 return put_user(result, value);
873 static int uart_tiocmget(struct tty_struct *tty, struct file *file)
875 struct uart_state *state = tty->driver_data;
876 struct uart_port *port = state->port;
877 int result = -EIO;
879 mutex_lock(&state->mutex);
880 if ((!file || !tty_hung_up_p(file)) &&
881 !(tty->flags & (1 << TTY_IO_ERROR))) {
882 result = port->mctrl;
884 spin_lock_irq(&port->lock);
885 result |= port->ops->get_mctrl(port);
886 spin_unlock_irq(&port->lock);
888 mutex_unlock(&state->mutex);
890 return result;
893 static int
894 uart_tiocmset(struct tty_struct *tty, struct file *file,
895 unsigned int set, unsigned int clear)
897 struct uart_state *state = tty->driver_data;
898 struct uart_port *port = state->port;
899 int ret = -EIO;
901 mutex_lock(&state->mutex);
902 if ((!file || !tty_hung_up_p(file)) &&
903 !(tty->flags & (1 << TTY_IO_ERROR))) {
904 uart_update_mctrl(port, set, clear);
905 ret = 0;
907 mutex_unlock(&state->mutex);
908 return ret;
911 static void uart_break_ctl(struct tty_struct *tty, int break_state)
913 struct uart_state *state = tty->driver_data;
914 struct uart_port *port = state->port;
916 BUG_ON(!kernel_locked());
918 mutex_lock(&state->mutex);
920 if (port->type != PORT_UNKNOWN)
921 port->ops->break_ctl(port, break_state);
923 mutex_unlock(&state->mutex);
926 static int uart_do_autoconfig(struct uart_state *state)
928 struct uart_port *port = state->port;
929 int flags, ret;
931 if (!capable(CAP_SYS_ADMIN))
932 return -EPERM;
935 * Take the per-port semaphore. This prevents count from
936 * changing, and hence any extra opens of the port while
937 * we're auto-configuring.
939 if (mutex_lock_interruptible(&state->mutex))
940 return -ERESTARTSYS;
942 ret = -EBUSY;
943 if (uart_users(state) == 1) {
944 uart_shutdown(state);
947 * If we already have a port type configured,
948 * we must release its resources.
950 if (port->type != PORT_UNKNOWN)
951 port->ops->release_port(port);
953 flags = UART_CONFIG_TYPE;
954 if (port->flags & UPF_AUTO_IRQ)
955 flags |= UART_CONFIG_IRQ;
958 * This will claim the ports resources if
959 * a port is found.
961 port->ops->config_port(port, flags);
963 ret = uart_startup(state, 1);
965 mutex_unlock(&state->mutex);
966 return ret;
970 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
971 * - mask passed in arg for lines of interest
972 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
973 * Caller should use TIOCGICOUNT to see which one it was
975 static int
976 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
978 struct uart_port *port = state->port;
979 DECLARE_WAITQUEUE(wait, current);
980 struct uart_icount cprev, cnow;
981 int ret;
984 * note the counters on entry
986 spin_lock_irq(&port->lock);
987 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
990 * Force modem status interrupts on
992 port->ops->enable_ms(port);
993 spin_unlock_irq(&port->lock);
995 add_wait_queue(&state->info->delta_msr_wait, &wait);
996 for (;;) {
997 spin_lock_irq(&port->lock);
998 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
999 spin_unlock_irq(&port->lock);
1001 set_current_state(TASK_INTERRUPTIBLE);
1003 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1004 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1005 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1006 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1007 ret = 0;
1008 break;
1011 schedule();
1013 /* see if a signal did it */
1014 if (signal_pending(current)) {
1015 ret = -ERESTARTSYS;
1016 break;
1019 cprev = cnow;
1022 current->state = TASK_RUNNING;
1023 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1025 return ret;
1029 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1030 * Return: write counters to the user passed counter struct
1031 * NB: both 1->0 and 0->1 transitions are counted except for
1032 * RI where only 0->1 is counted.
1034 static int uart_get_count(struct uart_state *state,
1035 struct serial_icounter_struct __user *icnt)
1037 struct serial_icounter_struct icount;
1038 struct uart_icount cnow;
1039 struct uart_port *port = state->port;
1041 spin_lock_irq(&port->lock);
1042 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1043 spin_unlock_irq(&port->lock);
1045 icount.cts = cnow.cts;
1046 icount.dsr = cnow.dsr;
1047 icount.rng = cnow.rng;
1048 icount.dcd = cnow.dcd;
1049 icount.rx = cnow.rx;
1050 icount.tx = cnow.tx;
1051 icount.frame = cnow.frame;
1052 icount.overrun = cnow.overrun;
1053 icount.parity = cnow.parity;
1054 icount.brk = cnow.brk;
1055 icount.buf_overrun = cnow.buf_overrun;
1057 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1060 #if 1 // add by Victor Yu. 02-09-2007
1061 #define PIO(x) (1<<x)
1062 #if 1 // add by Victor Yu. 07-26-2007
1063 #if defined(CONFIG_ARCH_W311_TEST)
1064 #define SW_READY_LED PIO(4)
1065 #define BEEPER_IO PIO(27)
1066 #else
1067 #define SW_READY_LED PIO(27)
1068 #define BEEPER_IO PIO(24)
1069 #endif
1070 static int readyledflag=0;
1071 #endif
1072 static void beep_sound(unsigned long arg)
1074 unsigned long ms,ss ;
1075 static spinlock_t beeplock;
1077 ms = arg >> 16;
1078 if ( HZ <= 1000 )
1079 ms = ms / (1000/HZ);
1080 else
1081 ms = (ms * 1000) / (1000000/HZ);
1082 if ( ms <= 0 )
1083 ms = 1;
1085 if ( readyledflag == 0 ) { // add by Victor Yu. 07-26-2007
1086 // enable beeper GPIO output
1087 mcpu_gpio_mp_set(BEEPER_IO);
1088 mcpu_gpio_inout(BEEPER_IO, MCPU_GPIO_OUTPUT);
1090 /* light ready led after jump into user space */
1091 mcpu_gpio_mp_set(SW_READY_LED);
1092 mcpu_gpio_inout(SW_READY_LED, MCPU_GPIO_OUTPUT);
1093 mcpu_gpio_set(SW_READY_LED, MCPU_GPIO_LOW);
1094 readyledflag = 1;
1097 spin_lock(&beeplock);
1098 /* sound on */
1099 mcpu_gpio_set(BEEPER_IO, MCPU_GPIO_HIGH);
1100 ss = ms;
1101 ms += jiffies ;
1102 while( !time_after(jiffies,ms)) {
1103 set_current_state(TASK_INTERRUPTIBLE);
1104 schedule_timeout(ss);
1105 if (signal_pending(current))
1106 break;
1107 ss = ms - jiffies;
1109 /* sound off */
1110 mcpu_gpio_set(BEEPER_IO, MCPU_GPIO_LOW);
1111 spin_unlock(&beeplock);
1113 #endif
1116 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1118 static int
1119 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
1120 unsigned long arg)
1122 struct uart_state *state = tty->driver_data;
1123 void __user *uarg = (void __user *)arg;
1124 int ret = -ENOIOCTLCMD;
1126 BUG_ON(!kernel_locked());
1129 * These ioctls don't rely on the hardware to be present.
1131 switch (cmd) {
1132 #if 1 // add by Victor Yu. 02-09-2007
1133 case KDMKTONE:
1134 beep_sound(arg) ;
1135 ret = 0;
1136 break;
1137 #endif
1138 case TIOCGSERIAL:
1139 ret = uart_get_info(state, uarg);
1140 break;
1142 case TIOCSSERIAL:
1143 ret = uart_set_info(state, uarg);
1144 break;
1146 case TIOCSERCONFIG:
1147 ret = uart_do_autoconfig(state);
1148 break;
1150 case TIOCSERGWILD: /* obsolete */
1151 case TIOCSERSWILD: /* obsolete */
1152 ret = 0;
1153 break;
1156 if (ret != -ENOIOCTLCMD)
1157 goto out;
1159 if (tty->flags & (1 << TTY_IO_ERROR)) {
1160 ret = -EIO;
1161 goto out;
1165 * The following should only be used when hardware is present.
1167 switch (cmd) {
1168 case TIOCMIWAIT:
1169 ret = uart_wait_modem_status(state, arg);
1170 break;
1172 case TIOCGICOUNT:
1173 ret = uart_get_count(state, uarg);
1174 break;
1177 if (ret != -ENOIOCTLCMD)
1178 goto out;
1180 mutex_lock(&state->mutex);
1182 if (tty_hung_up_p(filp)) {
1183 ret = -EIO;
1184 goto out_up;
1188 * All these rely on hardware being present and need to be
1189 * protected against the tty being hung up.
1191 switch (cmd) {
1192 case TIOCSERGETLSR: /* Get line status register */
1193 ret = uart_get_lsr_info(state, uarg);
1194 break;
1196 default: {
1197 struct uart_port *port = state->port;
1198 if (port->ops->ioctl)
1199 ret = port->ops->ioctl(port, cmd, arg);
1200 break;
1203 out_up:
1204 mutex_unlock(&state->mutex);
1205 out:
1206 return ret;
1209 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1211 struct uart_state *state = tty->driver_data;
1212 unsigned long flags;
1213 unsigned int cflag = tty->termios->c_cflag;
1215 BUG_ON(!kernel_locked());
1218 * These are the bits that are used to setup various
1219 * flags in the low level driver.
1221 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1223 if ((cflag ^ old_termios->c_cflag) == 0 &&
1224 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1225 return;
1227 uart_change_speed(state, old_termios);
1229 /* Handle transition to B0 status */
1230 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1231 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1233 /* Handle transition away from B0 status */
1234 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1235 unsigned int mask = TIOCM_DTR;
1236 if (!(cflag & CRTSCTS) ||
1237 !test_bit(TTY_THROTTLED, &tty->flags))
1238 mask |= TIOCM_RTS;
1239 uart_set_mctrl(state->port, mask);
1242 /* Handle turning off CRTSCTS */
1243 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1244 spin_lock_irqsave(&state->port->lock, flags);
1245 tty->hw_stopped = 0;
1246 __uart_start(tty);
1247 spin_unlock_irqrestore(&state->port->lock, flags);
1250 /* Handle turning on CRTSCTS */
1251 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1252 spin_lock_irqsave(&state->port->lock, flags);
1253 if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1254 tty->hw_stopped = 1;
1255 state->port->ops->stop_tx(state->port);
1257 spin_unlock_irqrestore(&state->port->lock, flags);
1260 #if 0
1262 * No need to wake up processes in open wait, since they
1263 * sample the CLOCAL flag once, and don't recheck it.
1264 * XXX It's not clear whether the current behavior is correct
1265 * or not. Hence, this may change.....
1267 if (!(old_termios->c_cflag & CLOCAL) &&
1268 (tty->termios->c_cflag & CLOCAL))
1269 wake_up_interruptible(&state->info->open_wait);
1270 #endif
1274 * In 2.4.5, calls to this will be serialized via the BKL in
1275 * linux/drivers/char/tty_io.c:tty_release()
1276 * linux/drivers/char/tty_io.c:do_tty_handup()
1278 static void uart_close(struct tty_struct *tty, struct file *filp)
1280 struct uart_state *state = tty->driver_data;
1281 struct uart_port *port;
1283 BUG_ON(!kernel_locked());
1285 if (!state || !state->port)
1286 return;
1288 port = state->port;
1290 DPRINTK("uart_close(%d) called\n", port->line);
1292 mutex_lock(&state->mutex);
1294 if (tty_hung_up_p(filp))
1295 goto done;
1297 if ((tty->count == 1) && (state->count != 1)) {
1299 * Uh, oh. tty->count is 1, which means that the tty
1300 * structure will be freed. state->count should always
1301 * be one in these conditions. If it's greater than
1302 * one, we've got real problems, since it means the
1303 * serial port won't be shutdown.
1305 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1306 "state->count is %d\n", state->count);
1307 state->count = 1;
1309 if (--state->count < 0) {
1310 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1311 tty->name, state->count);
1312 state->count = 0;
1314 if (state->count)
1315 goto done;
1318 * Now we wait for the transmit buffer to clear; and we notify
1319 * the line discipline to only process XON/XOFF characters by
1320 * setting tty->closing.
1322 tty->closing = 1;
1324 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1325 tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
1328 * At this point, we stop accepting input. To do this, we
1329 * disable the receive line status interrupts.
1331 if (state->info->flags & UIF_INITIALIZED) {
1332 unsigned long flags;
1333 spin_lock_irqsave(&port->lock, flags);
1334 port->ops->stop_rx(port);
1335 spin_unlock_irqrestore(&port->lock, flags);
1337 * Before we drop DTR, make sure the UART transmitter
1338 * has completely drained; this is especially
1339 * important if there is a transmit FIFO!
1341 uart_wait_until_sent(tty, port->timeout);
1344 uart_shutdown(state);
1345 uart_flush_buffer(tty);
1347 tty_ldisc_flush(tty);
1349 tty->closing = 0;
1350 state->info->tty = NULL;
1352 if (state->info->blocked_open) {
1353 if (state->close_delay)
1354 msleep_interruptible(state->close_delay);
1355 } else if (!uart_console(port)) {
1356 uart_change_pm(state, 3);
1360 * Wake up anyone trying to open this port.
1362 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1363 wake_up_interruptible(&state->info->open_wait);
1365 done:
1366 mutex_unlock(&state->mutex);
1369 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1371 struct uart_state *state = tty->driver_data;
1372 struct uart_port *port = state->port;
1373 unsigned long char_time, expire;
1375 BUG_ON(!kernel_locked());
1377 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1378 return;
1381 * Set the check interval to be 1/5 of the estimated time to
1382 * send a single character, and make it at least 1. The check
1383 * interval should also be less than the timeout.
1385 * Note: we have to use pretty tight timings here to satisfy
1386 * the NIST-PCTS.
1388 char_time = (port->timeout - HZ/50) / port->fifosize;
1389 char_time = char_time / 5;
1390 if (char_time == 0)
1391 char_time = 1;
1392 if (timeout && timeout < char_time)
1393 char_time = timeout;
1396 * If the transmitter hasn't cleared in twice the approximate
1397 * amount of time to send the entire FIFO, it probably won't
1398 * ever clear. This assumes the UART isn't doing flow
1399 * control, which is currently the case. Hence, if it ever
1400 * takes longer than port->timeout, this is probably due to a
1401 * UART bug of some kind. So, we clamp the timeout parameter at
1402 * 2*port->timeout.
1404 if (timeout == 0 || timeout > 2 * port->timeout)
1405 timeout = 2 * port->timeout;
1407 expire = jiffies + timeout;
1409 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1410 port->line, jiffies, expire);
1413 * Check whether the transmitter is empty every 'char_time'.
1414 * 'timeout' / 'expire' give us the maximum amount of time
1415 * we wait.
1417 while (!port->ops->tx_empty(port)) {
1418 msleep_interruptible(jiffies_to_msecs(char_time));
1419 if (signal_pending(current))
1420 break;
1421 if (time_after(jiffies, expire))
1422 break;
1424 set_current_state(TASK_RUNNING); /* might not be needed */
1428 * This is called with the BKL held in
1429 * linux/drivers/char/tty_io.c:do_tty_hangup()
1430 * We're called from the eventd thread, so we can sleep for
1431 * a _short_ time only.
1433 static void uart_hangup(struct tty_struct *tty)
1435 struct uart_state *state = tty->driver_data;
1437 BUG_ON(!kernel_locked());
1438 DPRINTK("uart_hangup(%d)\n", state->port->line);
1440 mutex_lock(&state->mutex);
1441 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1442 uart_flush_buffer(tty);
1443 uart_shutdown(state);
1444 state->count = 0;
1445 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1446 state->info->tty = NULL;
1447 wake_up_interruptible(&state->info->open_wait);
1448 wake_up_interruptible(&state->info->delta_msr_wait);
1450 mutex_unlock(&state->mutex);
1454 * Copy across the serial console cflag setting into the termios settings
1455 * for the initial open of the port. This allows continuity between the
1456 * kernel settings, and the settings init adopts when it opens the port
1457 * for the first time.
1459 static void uart_update_termios(struct uart_state *state)
1461 struct tty_struct *tty = state->info->tty;
1462 struct uart_port *port = state->port;
1464 if (uart_console(port) && port->cons->cflag) {
1465 tty->termios->c_cflag = port->cons->cflag;
1466 port->cons->cflag = 0;
1470 * If the device failed to grab its irq resources,
1471 * or some other error occurred, don't try to talk
1472 * to the port hardware.
1474 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1476 * Make termios settings take effect.
1478 uart_change_speed(state, NULL);
1481 * And finally enable the RTS and DTR signals.
1483 if (tty->termios->c_cflag & CBAUD)
1484 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1489 * Block the open until the port is ready. We must be called with
1490 * the per-port semaphore held.
1492 static int
1493 uart_block_til_ready(struct file *filp, struct uart_state *state)
1495 DECLARE_WAITQUEUE(wait, current);
1496 struct uart_info *info = state->info;
1497 struct uart_port *port = state->port;
1498 unsigned int mctrl;
1500 info->blocked_open++;
1501 state->count--;
1503 add_wait_queue(&info->open_wait, &wait);
1504 while (1) {
1505 set_current_state(TASK_INTERRUPTIBLE);
1508 * If we have been hung up, tell userspace/restart open.
1510 if (tty_hung_up_p(filp) || info->tty == NULL)
1511 break;
1514 * If the port has been closed, tell userspace/restart open.
1516 if (!(info->flags & UIF_INITIALIZED))
1517 break;
1520 * If non-blocking mode is set, or CLOCAL mode is set,
1521 * we don't want to wait for the modem status lines to
1522 * indicate that the port is ready.
1524 * Also, if the port is not enabled/configured, we want
1525 * to allow the open to succeed here. Note that we will
1526 * have set TTY_IO_ERROR for a non-existant port.
1528 if ((filp->f_flags & O_NONBLOCK) ||
1529 (info->tty->termios->c_cflag & CLOCAL) ||
1530 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1531 break;
1535 * Set DTR to allow modem to know we're waiting. Do
1536 * not set RTS here - we want to make sure we catch
1537 * the data from the modem.
1539 if (info->tty->termios->c_cflag & CBAUD)
1540 uart_set_mctrl(port, TIOCM_DTR);
1543 * and wait for the carrier to indicate that the
1544 * modem is ready for us.
1546 spin_lock_irq(&port->lock);
1547 port->ops->enable_ms(port);
1548 mctrl = port->ops->get_mctrl(port);
1549 spin_unlock_irq(&port->lock);
1550 if (mctrl & TIOCM_CAR)
1551 break;
1553 mutex_unlock(&state->mutex);
1554 schedule();
1555 mutex_lock(&state->mutex);
1557 if (signal_pending(current))
1558 break;
1560 set_current_state(TASK_RUNNING);
1561 remove_wait_queue(&info->open_wait, &wait);
1563 state->count++;
1564 info->blocked_open--;
1566 if (signal_pending(current))
1567 return -ERESTARTSYS;
1569 if (!info->tty || tty_hung_up_p(filp))
1570 return -EAGAIN;
1572 return 0;
1575 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1577 struct uart_state *state;
1578 int ret = 0;
1580 state = drv->state + line;
1581 if (mutex_lock_interruptible(&state->mutex)) {
1582 ret = -ERESTARTSYS;
1583 goto err;
1586 state->count++;
1587 if (!state->port || state->port->flags & UPF_DEAD) {
1588 ret = -ENXIO;
1589 goto err_unlock;
1592 if (!state->info) {
1593 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1594 if (state->info) {
1595 memset(state->info, 0, sizeof(struct uart_info));
1596 init_waitqueue_head(&state->info->open_wait);
1597 init_waitqueue_head(&state->info->delta_msr_wait);
1600 * Link the info into the other structures.
1602 state->port->info = state->info;
1604 tasklet_init(&state->info->tlet, uart_tasklet_action,
1605 (unsigned long)state);
1606 } else {
1607 ret = -ENOMEM;
1608 goto err_unlock;
1611 return state;
1613 err_unlock:
1614 state->count--;
1615 mutex_unlock(&state->mutex);
1616 err:
1617 return ERR_PTR(ret);
1621 * In 2.4.5, calls to uart_open are serialised by the BKL in
1622 * linux/fs/devices.c:chrdev_open()
1623 * Note that if this fails, then uart_close() _will_ be called.
1625 * In time, we want to scrap the "opening nonpresent ports"
1626 * behaviour and implement an alternative way for setserial
1627 * to set base addresses/ports/types. This will allow us to
1628 * get rid of a certain amount of extra tests.
1630 static int uart_open(struct tty_struct *tty, struct file *filp)
1632 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1633 struct uart_state *state;
1634 int retval, line = tty->index;
1636 BUG_ON(!kernel_locked());
1637 DPRINTK("uart_open(%d) called\n", line);
1640 * tty->driver->num won't change, so we won't fail here with
1641 * tty->driver_data set to something non-NULL (and therefore
1642 * we won't get caught by uart_close()).
1644 retval = -ENODEV;
1645 if (line >= tty->driver->num)
1646 goto fail;
1649 * We take the semaphore inside uart_get to guarantee that we won't
1650 * be re-entered while allocating the info structure, or while we
1651 * request any IRQs that the driver may need. This also has the nice
1652 * side-effect that it delays the action of uart_hangup, so we can
1653 * guarantee that info->tty will always contain something reasonable.
1655 state = uart_get(drv, line);
1656 if (IS_ERR(state)) {
1657 retval = PTR_ERR(state);
1658 goto fail;
1662 * Once we set tty->driver_data here, we are guaranteed that
1663 * uart_close() will decrement the driver module use count.
1664 * Any failures from here onwards should not touch the count.
1666 tty->driver_data = state;
1667 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1668 tty->alt_speed = 0;
1669 state->info->tty = tty;
1672 * If the port is in the middle of closing, bail out now.
1674 if (tty_hung_up_p(filp)) {
1675 retval = -EAGAIN;
1676 state->count--;
1677 mutex_unlock(&state->mutex);
1678 goto fail;
1682 * Make sure the device is in D0 state.
1684 if (state->count == 1)
1685 uart_change_pm(state, 0);
1688 * Start up the serial port.
1690 retval = uart_startup(state, 0);
1693 * If we succeeded, wait until the port is ready.
1695 if (retval == 0)
1696 retval = uart_block_til_ready(filp, state);
1697 mutex_unlock(&state->mutex);
1700 * If this is the first open to succeed, adjust things to suit.
1702 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1703 state->info->flags |= UIF_NORMAL_ACTIVE;
1705 uart_update_termios(state);
1708 fail:
1709 return retval;
1712 static const char *uart_type(struct uart_port *port)
1714 const char *str = NULL;
1716 if (port->ops->type)
1717 str = port->ops->type(port);
1719 if (!str)
1720 str = "unknown";
1722 return str;
1725 #ifdef CONFIG_PROC_FS
1727 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1729 struct uart_state *state = drv->state + i;
1730 struct uart_port *port = state->port;
1731 char stat_buf[32];
1732 unsigned int status;
1733 int mmio, ret;
1735 if (!port)
1736 return 0;
1738 mmio = port->iotype >= UPIO_MEM;
1739 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1740 port->line, uart_type(port),
1741 mmio ? "mmio:0x" : "port:",
1742 mmio ? port->mapbase : (unsigned long) port->iobase,
1743 port->irq);
1745 if (port->type == PORT_UNKNOWN) {
1746 strcat(buf, "\n");
1747 return ret + 1;
1750 if(capable(CAP_SYS_ADMIN))
1752 spin_lock_irq(&port->lock);
1753 status = port->ops->get_mctrl(port);
1754 spin_unlock_irq(&port->lock);
1756 ret += sprintf(buf + ret, " tx:%d rx:%d",
1757 port->icount.tx, port->icount.rx);
1758 if (port->icount.frame)
1759 ret += sprintf(buf + ret, " fe:%d",
1760 port->icount.frame);
1761 if (port->icount.parity)
1762 ret += sprintf(buf + ret, " pe:%d",
1763 port->icount.parity);
1764 if (port->icount.brk)
1765 ret += sprintf(buf + ret, " brk:%d",
1766 port->icount.brk);
1767 if (port->icount.overrun)
1768 ret += sprintf(buf + ret, " oe:%d",
1769 port->icount.overrun);
1771 #define INFOBIT(bit,str) \
1772 if (port->mctrl & (bit)) \
1773 strncat(stat_buf, (str), sizeof(stat_buf) - \
1774 strlen(stat_buf) - 2)
1775 #define STATBIT(bit,str) \
1776 if (status & (bit)) \
1777 strncat(stat_buf, (str), sizeof(stat_buf) - \
1778 strlen(stat_buf) - 2)
1780 stat_buf[0] = '\0';
1781 stat_buf[1] = '\0';
1782 INFOBIT(TIOCM_RTS, "|RTS");
1783 STATBIT(TIOCM_CTS, "|CTS");
1784 INFOBIT(TIOCM_DTR, "|DTR");
1785 STATBIT(TIOCM_DSR, "|DSR");
1786 STATBIT(TIOCM_CAR, "|CD");
1787 STATBIT(TIOCM_RNG, "|RI");
1788 if (stat_buf[0])
1789 stat_buf[0] = ' ';
1790 strcat(stat_buf, "\n");
1792 ret += sprintf(buf + ret, stat_buf);
1793 } else {
1794 strcat(buf, "\n");
1795 ret++;
1797 #undef STATBIT
1798 #undef INFOBIT
1799 return ret;
1802 static int uart_read_proc(char *page, char **start, off_t off,
1803 int count, int *eof, void *data)
1805 struct tty_driver *ttydrv = data;
1806 struct uart_driver *drv = ttydrv->driver_state;
1807 int i, len = 0, l;
1808 off_t begin = 0;
1810 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1811 "", "", "");
1812 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1813 l = uart_line_info(page + len, drv, i);
1814 len += l;
1815 if (len + begin > off + count)
1816 goto done;
1817 if (len + begin < off) {
1818 begin += len;
1819 len = 0;
1822 *eof = 1;
1823 done:
1824 if (off >= len + begin)
1825 return 0;
1826 *start = page + (off - begin);
1827 return (count < begin + len - off) ? count : (begin + len - off);
1829 #endif
1831 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1833 * uart_console_write - write a console message to a serial port
1834 * @port: the port to write the message
1835 * @s: array of characters
1836 * @count: number of characters in string to write
1837 * @write: function to write character to port
1839 void uart_console_write(struct uart_port *port, const char *s,
1840 unsigned int count,
1841 void (*putchar)(struct uart_port *, int))
1843 unsigned int i;
1845 for (i = 0; i < count; i++, s++) {
1846 if (*s == '\n')
1847 putchar(port, '\r');
1848 putchar(port, *s);
1851 EXPORT_SYMBOL_GPL(uart_console_write);
1854 * Check whether an invalid uart number has been specified, and
1855 * if so, search for the first available port that does have
1856 * console support.
1858 struct uart_port * __init
1859 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1861 int idx = co->index;
1863 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1864 ports[idx].membase == NULL))
1865 for (idx = 0; idx < nr; idx++)
1866 if (ports[idx].iobase != 0 ||
1867 ports[idx].membase != NULL)
1868 break;
1870 co->index = idx;
1872 return ports + idx;
1876 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1877 * @options: pointer to option string
1878 * @baud: pointer to an 'int' variable for the baud rate.
1879 * @parity: pointer to an 'int' variable for the parity.
1880 * @bits: pointer to an 'int' variable for the number of data bits.
1881 * @flow: pointer to an 'int' variable for the flow control character.
1883 * uart_parse_options decodes a string containing the serial console
1884 * options. The format of the string is <baud><parity><bits><flow>,
1885 * eg: 115200n8r
1887 void __init
1888 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1890 char *s = options;
1892 *baud = simple_strtoul(s, NULL, 10);
1893 while (*s >= '0' && *s <= '9')
1894 s++;
1895 if (*s)
1896 *parity = *s++;
1897 if (*s)
1898 *bits = *s++ - '0';
1899 if (*s)
1900 *flow = *s;
1903 struct baud_rates {
1904 unsigned int rate;
1905 unsigned int cflag;
1908 static const struct baud_rates baud_rates[] = {
1909 { 921600, B921600 },
1910 { 460800, B460800 },
1911 { 230400, B230400 },
1912 { 115200, B115200 },
1913 { 57600, B57600 },
1914 { 38400, B38400 },
1915 { 19200, B19200 },
1916 { 9600, B9600 },
1917 { 4800, B4800 },
1918 { 2400, B2400 },
1919 { 1200, B1200 },
1920 { 0, B38400 }
1924 * uart_set_options - setup the serial console parameters
1925 * @port: pointer to the serial ports uart_port structure
1926 * @co: console pointer
1927 * @baud: baud rate
1928 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1929 * @bits: number of data bits
1930 * @flow: flow control character - 'r' (rts)
1932 int __init
1933 uart_set_options(struct uart_port *port, struct console *co,
1934 int baud, int parity, int bits, int flow)
1936 struct termios termios;
1937 int i;
1940 * Ensure that the serial console lock is initialised
1941 * early.
1943 spin_lock_init(&port->lock);
1944 lockdep_set_class(&port->lock, &port_lock_key);
1946 memset(&termios, 0, sizeof(struct termios));
1948 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1951 * Construct a cflag setting.
1953 for (i = 0; baud_rates[i].rate; i++)
1954 if (baud_rates[i].rate <= baud)
1955 break;
1957 termios.c_cflag |= baud_rates[i].cflag;
1959 if (bits == 7)
1960 termios.c_cflag |= CS7;
1961 else
1962 termios.c_cflag |= CS8;
1964 switch (parity) {
1965 case 'o': case 'O':
1966 termios.c_cflag |= PARODD;
1967 /*fall through*/
1968 case 'e': case 'E':
1969 termios.c_cflag |= PARENB;
1970 break;
1973 if (flow == 'r')
1974 termios.c_cflag |= CRTSCTS;
1976 port->ops->set_termios(port, &termios, NULL);
1977 co->cflag = termios.c_cflag;
1979 return 0;
1981 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1983 static void uart_change_pm(struct uart_state *state, int pm_state)
1985 struct uart_port *port = state->port;
1987 if (state->pm_state != pm_state) {
1988 if (port->ops->pm)
1989 port->ops->pm(port, pm_state, state->pm_state);
1990 state->pm_state = pm_state;
1994 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1996 struct uart_state *state = drv->state + port->line;
1998 mutex_lock(&state->mutex);
2000 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
2001 if (uart_console(port)) {
2002 mutex_unlock(&state->mutex);
2003 return 0;
2005 #endif
2007 if (state->info && state->info->flags & UIF_INITIALIZED) {
2008 const struct uart_ops *ops = port->ops;
2010 state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
2011 | UIF_SUSPENDED;
2013 spin_lock_irq(&port->lock);
2014 ops->stop_tx(port);
2015 ops->set_mctrl(port, 0);
2016 ops->stop_rx(port);
2017 spin_unlock_irq(&port->lock);
2020 * Wait for the transmitter to empty.
2022 while (!ops->tx_empty(port)) {
2023 msleep(10);
2026 ops->shutdown(port);
2030 * Disable the console device before suspending.
2032 if (uart_console(port))
2033 console_stop(port->cons);
2035 uart_change_pm(state, 3);
2037 mutex_unlock(&state->mutex);
2039 return 0;
2042 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
2044 struct uart_state *state = drv->state + port->line;
2046 mutex_lock(&state->mutex);
2048 #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
2049 if (uart_console(port)) {
2050 mutex_unlock(&state->mutex);
2051 return 0;
2053 #endif
2055 uart_change_pm(state, 0);
2058 * Re-enable the console device after suspending.
2060 if (uart_console(port)) {
2061 struct termios termios;
2064 * First try to use the console cflag setting.
2066 memset(&termios, 0, sizeof(struct termios));
2067 termios.c_cflag = port->cons->cflag;
2070 * If that's unset, use the tty termios setting.
2072 if (state->info && state->info->tty && termios.c_cflag == 0)
2073 termios = *state->info->tty->termios;
2075 port->ops->set_termios(port, &termios, NULL);
2076 console_start(port->cons);
2079 if (state->info && state->info->flags & UIF_SUSPENDED) {
2080 const struct uart_ops *ops = port->ops;
2081 int ret;
2083 ops->set_mctrl(port, 0);
2084 ret = ops->startup(port);
2085 if (ret == 0) {
2086 uart_change_speed(state, NULL);
2087 spin_lock_irq(&port->lock);
2088 ops->set_mctrl(port, port->mctrl);
2089 ops->start_tx(port);
2090 spin_unlock_irq(&port->lock);
2091 state->info->flags |= UIF_INITIALIZED;
2092 } else {
2094 * Failed to resume - maybe hardware went away?
2095 * Clear the "initialized" flag so we won't try
2096 * to call the low level drivers shutdown method.
2098 uart_shutdown(state);
2101 state->info->flags &= ~UIF_SUSPENDED;
2104 mutex_unlock(&state->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),
2117 "I/O 0x%x", port->iobase);
2118 break;
2119 case UPIO_HUB6:
2120 snprintf(address, sizeof(address),
2121 "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
2122 break;
2123 case UPIO_MEM:
2124 case UPIO_MEM32:
2125 case UPIO_AU:
2126 case UPIO_TSI:
2127 snprintf(address, sizeof(address),
2128 "MMIO 0x%lx", port->mapbase);
2129 break;
2130 default:
2131 strlcpy(address, "*unknown*", sizeof(address));
2132 break;
2135 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2136 port->dev ? port->dev->bus_id : "",
2137 port->dev ? ": " : "",
2138 drv->dev_name, port->line, address, port->irq, uart_type(port));
2141 static void
2142 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2143 struct uart_port *port)
2145 unsigned int flags;
2148 * If there isn't a port here, don't do anything further.
2150 if (!port->iobase && !port->mapbase && !port->membase)
2151 return;
2154 * Now do the auto configuration stuff. Note that config_port
2155 * is expected to claim the resources and map the port for us.
2157 flags = UART_CONFIG_TYPE;
2158 if (port->flags & UPF_AUTO_IRQ)
2159 flags |= UART_CONFIG_IRQ;
2160 if (port->flags & UPF_BOOT_AUTOCONF) {
2161 port->type = PORT_UNKNOWN;
2162 port->ops->config_port(port, flags);
2165 if (port->type != PORT_UNKNOWN) {
2166 unsigned long flags;
2168 uart_report_port(drv, port);
2171 * Ensure that the modem control lines are de-activated.
2172 * We probably don't need a spinlock around this, but
2174 spin_lock_irqsave(&port->lock, flags);
2175 port->ops->set_mctrl(port, 0);
2176 spin_unlock_irqrestore(&port->lock, flags);
2179 * Power down all ports by default, except the
2180 * console if we have one.
2182 if (!uart_console(port))
2183 uart_change_pm(state, 3);
2187 static const struct tty_operations uart_ops = {
2188 .open = uart_open,
2189 .close = uart_close,
2190 .write = uart_write,
2191 .put_char = uart_put_char,
2192 .flush_chars = uart_flush_chars,
2193 .write_room = uart_write_room,
2194 .chars_in_buffer= uart_chars_in_buffer,
2195 .flush_buffer = uart_flush_buffer,
2196 .ioctl = uart_ioctl,
2197 .throttle = uart_throttle,
2198 .unthrottle = uart_unthrottle,
2199 .send_xchar = uart_send_xchar,
2200 .set_termios = uart_set_termios,
2201 .stop = uart_stop,
2202 .start = uart_start,
2203 .hangup = uart_hangup,
2204 .break_ctl = uart_break_ctl,
2205 .wait_until_sent= uart_wait_until_sent,
2206 #ifdef CONFIG_PROC_FS
2207 .read_proc = uart_read_proc,
2208 #endif
2209 .tiocmget = uart_tiocmget,
2210 .tiocmset = uart_tiocmset,
2214 * uart_register_driver - register a driver with the uart core layer
2215 * @drv: low level driver structure
2217 * Register a uart driver with the core driver. We in turn register
2218 * with the tty layer, and initialise the core driver per-port state.
2220 * We have a proc file in /proc/tty/driver which is named after the
2221 * normal driver.
2223 * drv->port should be NULL, and the per-port structures should be
2224 * registered using uart_add_one_port after this call has succeeded.
2226 int uart_register_driver(struct uart_driver *drv)
2228 struct tty_driver *normal = NULL;
2229 int i, retval;
2231 BUG_ON(drv->state);
2234 * Maybe we should be using a slab cache for this, especially if
2235 * we have a large number of ports to handle.
2237 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2238 retval = -ENOMEM;
2239 if (!drv->state)
2240 goto out;
2242 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2244 normal = alloc_tty_driver(drv->nr);
2245 if (!normal)
2246 goto out;
2248 drv->tty_driver = normal;
2250 normal->owner = drv->owner;
2251 normal->driver_name = drv->driver_name;
2252 normal->name = drv->dev_name;
2253 normal->major = drv->major;
2254 normal->minor_start = drv->minor;
2255 normal->type = TTY_DRIVER_TYPE_SERIAL;
2256 normal->subtype = SERIAL_TYPE_NORMAL;
2257 normal->init_termios = tty_std_termios;
2258 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2259 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2260 normal->driver_state = drv;
2261 tty_set_operations(normal, &uart_ops);
2264 * Initialise the UART state(s).
2266 for (i = 0; i < drv->nr; i++) {
2267 struct uart_state *state = drv->state + i;
2269 state->close_delay = 500; /* .5 seconds */
2270 state->closing_wait = 30000; /* 30 seconds */
2272 mutex_init(&state->mutex);
2275 retval = tty_register_driver(normal);
2276 out:
2277 if (retval < 0) {
2278 put_tty_driver(normal);
2279 kfree(drv->state);
2281 return retval;
2285 * uart_unregister_driver - remove a driver from the uart core layer
2286 * @drv: low level driver structure
2288 * Remove all references to a driver from the core driver. The low
2289 * level driver must have removed all its ports via the
2290 * uart_remove_one_port() if it registered them with uart_add_one_port().
2291 * (ie, drv->port == NULL)
2293 void uart_unregister_driver(struct uart_driver *drv)
2295 struct tty_driver *p = drv->tty_driver;
2296 tty_unregister_driver(p);
2297 put_tty_driver(p);
2298 kfree(drv->state);
2299 drv->tty_driver = NULL;
2302 struct tty_driver *uart_console_device(struct console *co, int *index)
2304 struct uart_driver *p = co->data;
2305 *index = co->index;
2306 return p->tty_driver;
2310 * uart_add_one_port - attach a driver-defined port structure
2311 * @drv: pointer to the uart low level driver structure for this port
2312 * @port: uart port structure to use for this port.
2314 * This allows the driver to register its own uart_port structure
2315 * with the core driver. The main purpose is to allow the low
2316 * level uart drivers to expand uart_port, rather than having yet
2317 * more levels of structures.
2319 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2321 struct uart_state *state;
2322 int ret = 0;
2324 BUG_ON(in_interrupt());
2326 if (port->line >= drv->nr)
2327 return -EINVAL;
2329 state = drv->state + port->line;
2331 mutex_lock(&port_mutex);
2332 mutex_lock(&state->mutex);
2333 if (state->port) {
2334 ret = -EINVAL;
2335 goto out;
2338 state->port = port;
2340 port->cons = drv->cons;
2341 port->info = state->info;
2344 * If this port is a console, then the spinlock is already
2345 * initialised.
2347 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2348 spin_lock_init(&port->lock);
2349 lockdep_set_class(&port->lock, &port_lock_key);
2352 uart_configure_port(drv, state, port);
2355 * Register the port whether it's detected or not. This allows
2356 * setserial to be used to alter this ports parameters.
2358 tty_register_device(drv->tty_driver, port->line, port->dev);
2361 * If this driver supports console, and it hasn't been
2362 * successfully registered yet, try to re-register it.
2363 * It may be that the port was not available.
2365 if (port->type != PORT_UNKNOWN &&
2366 port->cons && !(port->cons->flags & CON_ENABLED))
2367 register_console(port->cons);
2370 * Ensure UPF_DEAD is not set.
2372 port->flags &= ~UPF_DEAD;
2374 out:
2375 mutex_unlock(&state->mutex);
2376 mutex_unlock(&port_mutex);
2378 return ret;
2382 * uart_remove_one_port - detach a driver defined port structure
2383 * @drv: pointer to the uart low level driver structure for this port
2384 * @port: uart port structure for this port
2386 * This unhooks (and hangs up) the specified port structure from the
2387 * core driver. No further calls will be made to the low-level code
2388 * for this port.
2390 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2392 struct uart_state *state = drv->state + port->line;
2393 struct uart_info *info;
2395 BUG_ON(in_interrupt());
2397 if (state->port != port)
2398 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2399 state->port, port);
2401 mutex_lock(&port_mutex);
2404 * Mark the port "dead" - this prevents any opens from
2405 * succeeding while we shut down the port.
2407 mutex_lock(&state->mutex);
2408 port->flags |= UPF_DEAD;
2409 mutex_unlock(&state->mutex);
2412 * Remove the devices from the tty layer
2414 tty_unregister_device(drv->tty_driver, port->line);
2416 info = state->info;
2417 if (info && info->tty)
2418 tty_vhangup(info->tty);
2421 * All users of this port should now be disconnected from
2422 * this driver, and the port shut down. We should be the
2423 * only thread fiddling with this port from now on.
2425 state->info = NULL;
2428 * Free the port IO and memory resources, if any.
2430 if (port->type != PORT_UNKNOWN)
2431 port->ops->release_port(port);
2434 * Indicate that there isn't a port here anymore.
2436 port->type = PORT_UNKNOWN;
2439 * Kill the tasklet, and free resources.
2441 if (info) {
2442 tasklet_kill(&info->tlet);
2443 kfree(info);
2446 state->port = NULL;
2447 mutex_unlock(&port_mutex);
2449 return 0;
2453 * Are the two ports equivalent?
2455 int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2457 if (port1->iotype != port2->iotype)
2458 return 0;
2460 switch (port1->iotype) {
2461 case UPIO_PORT:
2462 return (port1->iobase == port2->iobase);
2463 case UPIO_HUB6:
2464 return (port1->iobase == port2->iobase) &&
2465 (port1->hub6 == port2->hub6);
2466 case UPIO_MEM:
2467 case UPIO_MEM32:
2468 case UPIO_AU:
2469 case UPIO_TSI:
2470 return (port1->mapbase == port2->mapbase);
2472 return 0;
2474 EXPORT_SYMBOL(uart_match_port);
2476 EXPORT_SYMBOL(uart_write_wakeup);
2477 EXPORT_SYMBOL(uart_register_driver);
2478 EXPORT_SYMBOL(uart_unregister_driver);
2479 EXPORT_SYMBOL(uart_suspend_port);
2480 EXPORT_SYMBOL(uart_resume_port);
2481 EXPORT_SYMBOL(uart_add_one_port);
2482 EXPORT_SYMBOL(uart_remove_one_port);
2484 MODULE_DESCRIPTION("Serial driver core");
2485 MODULE_LICENSE("GPL");