MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / serial / serial_core.c
blob1a9bec961dab72a6350303baa22c1864569cdd8a
1 /*
2 * linux/drivers/char/core.c
4 * Driver core for serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/tty.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/console.h>
31 #include <linux/serial_core.h>
32 #include <linux/smp_lock.h>
33 #include <linux/device.h>
34 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
39 #undef DEBUG
40 #ifdef DEBUG
41 #define DPRINTK(x...) printk(x)
42 #else
43 #define DPRINTK(x...) do { } while (0)
44 #endif
47 * This is used to lock changes in serial line configuration.
49 static DECLARE_MUTEX(port_sem);
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 #define uart_users(state) ((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
55 #ifdef CONFIG_SERIAL_CORE_CONSOLE
56 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
57 #else
58 #define uart_console(port) (0)
59 #endif
61 static void uart_change_speed(struct uart_state *state, struct termios *old_termios);
62 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
63 static void uart_change_pm(struct uart_state *state, int pm_state);
66 * This routine is used by the interrupt handler to schedule processing in
67 * the software interrupt portion of the driver.
69 void uart_write_wakeup(struct uart_port *port)
71 struct uart_info *info = port->info;
72 tasklet_schedule(&info->tlet);
75 static void uart_stop(struct tty_struct *tty)
77 struct uart_state *state = tty->driver_data;
78 struct uart_port *port = state->port;
79 unsigned long flags;
81 spin_lock_irqsave(&port->lock, flags);
82 port->ops->stop_tx(port, 1);
83 spin_unlock_irqrestore(&port->lock, flags);
86 static void __uart_start(struct tty_struct *tty)
88 struct uart_state *state = tty->driver_data;
89 struct uart_port *port = state->port;
91 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
92 !tty->stopped && !tty->hw_stopped)
93 port->ops->start_tx(port, 1);
96 static void uart_start(struct tty_struct *tty)
98 struct uart_state *state = tty->driver_data;
99 struct uart_port *port = state->port;
100 unsigned long flags;
102 spin_lock_irqsave(&port->lock, flags);
103 __uart_start(tty);
104 spin_unlock_irqrestore(&port->lock, flags);
107 static void uart_tasklet_action(unsigned long data)
109 struct uart_state *state = (struct uart_state *)data;
110 tty_wakeup(state->info->tty);
113 static inline void
114 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
116 unsigned long flags;
117 unsigned int old;
119 spin_lock_irqsave(&port->lock, flags);
120 old = port->mctrl;
121 port->mctrl = (old & ~clear) | set;
122 if (old != port->mctrl)
123 port->ops->set_mctrl(port, port->mctrl);
124 spin_unlock_irqrestore(&port->lock, flags);
127 #define uart_set_mctrl(port,set) uart_update_mctrl(port,set,0)
128 #define uart_clear_mctrl(port,clear) uart_update_mctrl(port,0,clear)
131 * Startup the port. This will be called once per open. All calls
132 * will be serialised by the per-port semaphore.
134 static int uart_startup(struct uart_state *state, int init_hw)
136 struct uart_info *info = state->info;
137 struct uart_port *port = state->port;
138 unsigned long page;
139 int retval = 0;
141 if (info->flags & UIF_INITIALIZED)
142 return 0;
145 * Set the TTY IO error marker - we will only clear this
146 * once we have successfully opened the port. Also set
147 * up the tty->alt_speed kludge
149 if (info->tty)
150 set_bit(TTY_IO_ERROR, &info->tty->flags);
152 if (port->type == PORT_UNKNOWN)
153 return 0;
156 * Initialise and allocate the transmit and temporary
157 * buffer.
159 if (!info->xmit.buf) {
160 page = get_zeroed_page(GFP_KERNEL);
161 if (!page)
162 return -ENOMEM;
164 info->xmit.buf = (unsigned char *) page;
165 info->tmpbuf = info->xmit.buf + UART_XMIT_SIZE;
166 init_MUTEX(&info->tmpbuf_sem);
167 uart_circ_clear(&info->xmit);
170 retval = port->ops->startup(port);
171 if (retval == 0) {
172 if (init_hw) {
174 * Initialise the hardware port settings.
176 uart_change_speed(state, NULL);
179 * Setup the RTS and DTR signals once the
180 * port is open and ready to respond.
182 if (info->tty->termios->c_cflag & CBAUD)
183 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
186 info->flags |= UIF_INITIALIZED;
188 clear_bit(TTY_IO_ERROR, &info->tty->flags);
191 if (retval && capable(CAP_SYS_ADMIN))
192 retval = 0;
194 return retval;
198 * This routine will shutdown a serial port; interrupts are disabled, and
199 * DTR is dropped if the hangup on close termio flag is on. Calls to
200 * uart_shutdown are serialised by the per-port semaphore.
202 static void uart_shutdown(struct uart_state *state)
204 struct uart_info *info = state->info;
205 struct uart_port *port = state->port;
207 if (!(info->flags & UIF_INITIALIZED))
208 return;
211 * Turn off DTR and RTS early.
213 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
214 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
217 * clear delta_msr_wait queue to avoid mem leaks: we may free
218 * the irq here so the queue might never be woken up. Note
219 * that we won't end up waiting on delta_msr_wait again since
220 * any outstanding file descriptors should be pointing at
221 * hung_up_tty_fops now.
223 wake_up_interruptible(&info->delta_msr_wait);
226 * Free the IRQ and disable the port.
228 port->ops->shutdown(port);
231 * Ensure that the IRQ handler isn't running on another CPU.
233 synchronize_irq(port->irq);
236 * Free the transmit buffer page.
238 if (info->xmit.buf) {
239 free_page((unsigned long)info->xmit.buf);
240 info->xmit.buf = NULL;
241 info->tmpbuf = NULL;
245 * kill off our tasklet
247 tasklet_kill(&info->tlet);
248 if (info->tty)
249 set_bit(TTY_IO_ERROR, &info->tty->flags);
251 info->flags &= ~UIF_INITIALIZED;
255 * uart_update_timeout - update per-port FIFO timeout.
256 * @port: uart_port structure describing the port
257 * @cflag: termios cflag value
258 * @baud: speed of the port
260 * Set the port FIFO timeout value. The @cflag value should
261 * reflect the actual hardware settings.
263 void
264 uart_update_timeout(struct uart_port *port, unsigned int cflag,
265 unsigned int baud)
267 unsigned int bits;
269 /* byte size and parity */
270 switch (cflag & CSIZE) {
271 case CS5:
272 bits = 7;
273 break;
274 case CS6:
275 bits = 8;
276 break;
277 case CS7:
278 bits = 9;
279 break;
280 default:
281 bits = 10;
282 break; // CS8
285 if (cflag & CSTOPB)
286 bits++;
287 if (cflag & PARENB)
288 bits++;
291 * The total number of bits to be transmitted in the fifo.
293 bits = bits * port->fifosize;
296 * Figure the timeout to send the above number of bits.
297 * Add .02 seconds of slop
299 port->timeout = (HZ * bits) / baud + HZ/50;
302 EXPORT_SYMBOL(uart_update_timeout);
305 * uart_get_baud_rate - return baud rate for a particular port
306 * @port: uart_port structure describing the port in question.
307 * @termios: desired termios settings.
308 * @old: old termios (or NULL)
309 * @min: minimum acceptable baud rate
310 * @max: maximum acceptable baud rate
312 * Decode the termios structure into a numeric baud rate,
313 * taking account of the magic 38400 baud rate (with spd_*
314 * flags), and mapping the %B0 rate to 9600 baud.
316 * If the new baud rate is invalid, try the old termios setting.
317 * If it's still invalid, we try 9600 baud.
319 * Update the @termios structure to reflect the baud rate
320 * we're actually going to be using.
322 unsigned int
323 uart_get_baud_rate(struct uart_port *port, struct termios *termios,
324 struct termios *old, unsigned int min, unsigned int max)
326 unsigned int try, baud, altbaud = 38400;
327 unsigned int flags = port->flags & UPF_SPD_MASK;
329 if (flags == UPF_SPD_HI)
330 altbaud = 57600;
331 if (flags == UPF_SPD_VHI)
332 altbaud = 115200;
333 if (flags == UPF_SPD_SHI)
334 altbaud = 230400;
335 if (flags == UPF_SPD_WARP)
336 altbaud = 460800;
338 for (try = 0; try < 2; try++) {
339 baud = tty_termios_baud_rate(termios);
342 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
343 * Die! Die! Die!
345 if (baud == 38400)
346 baud = altbaud;
349 * Special case: B0 rate.
351 if (baud == 0)
352 baud = 9600;
354 if (baud >= min && baud <= max)
355 return baud;
358 * Oops, the quotient was zero. Try again with
359 * the old baud rate if possible.
361 termios->c_cflag &= ~CBAUD;
362 if (old) {
363 termios->c_cflag |= old->c_cflag & CBAUD;
364 old = NULL;
365 continue;
369 * As a last resort, if the quotient is zero,
370 * default to 9600 bps
372 termios->c_cflag |= B9600;
375 return 0;
378 EXPORT_SYMBOL(uart_get_baud_rate);
381 * uart_get_divisor - return uart clock divisor
382 * @port: uart_port structure describing the port.
383 * @baud: desired baud rate
385 * Calculate the uart clock divisor for the port.
387 unsigned int
388 uart_get_divisor(struct uart_port *port, unsigned int baud)
390 unsigned int quot;
393 * Old custom speed handling.
395 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
396 quot = port->custom_divisor;
397 else
398 quot = (port->uartclk + (8 * baud)) / (16 * baud);
400 return quot;
403 EXPORT_SYMBOL(uart_get_divisor);
405 static void
406 uart_change_speed(struct uart_state *state, struct termios *old_termios)
408 struct tty_struct *tty = state->info->tty;
409 struct uart_port *port = state->port;
410 struct termios *termios;
413 * If we have no tty, termios, or the port does not exist,
414 * then we can't set the parameters for this port.
416 if (!tty || !tty->termios || port->type == PORT_UNKNOWN)
417 return;
419 termios = tty->termios;
422 * Set flags based on termios cflag
424 if (termios->c_cflag & CRTSCTS)
425 state->info->flags |= UIF_CTS_FLOW;
426 else
427 state->info->flags &= ~UIF_CTS_FLOW;
429 if (termios->c_cflag & CLOCAL)
430 state->info->flags &= ~UIF_CHECK_CD;
431 else
432 state->info->flags |= UIF_CHECK_CD;
434 port->ops->set_termios(port, termios, old_termios);
437 static inline void
438 __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c)
440 unsigned long flags;
442 if (!circ->buf)
443 return;
445 spin_lock_irqsave(&port->lock, flags);
446 if (uart_circ_chars_free(circ) != 0) {
447 circ->buf[circ->head] = c;
448 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
450 spin_unlock_irqrestore(&port->lock, flags);
453 static inline int
454 __uart_user_write(struct uart_port *port, struct circ_buf *circ,
455 const unsigned char __user *buf, int count)
457 unsigned long flags;
458 int c, ret = 0;
460 if (down_interruptible(&port->info->tmpbuf_sem))
461 return -EINTR;
463 while (1) {
464 int c1;
465 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
466 if (count < c)
467 c = count;
468 if (c <= 0)
469 break;
471 c -= copy_from_user(port->info->tmpbuf, buf, c);
472 if (!c) {
473 if (!ret)
474 ret = -EFAULT;
475 break;
477 spin_lock_irqsave(&port->lock, flags);
478 c1 = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
479 if (c1 < c)
480 c = c1;
481 memcpy(circ->buf + circ->head, port->info->tmpbuf, c);
482 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
483 spin_unlock_irqrestore(&port->lock, flags);
484 buf += c;
485 count -= c;
486 ret += c;
488 up(&port->info->tmpbuf_sem);
490 return ret;
493 static inline int
494 __uart_kern_write(struct uart_port *port, struct circ_buf *circ,
495 const unsigned char *buf, int count)
497 unsigned long flags;
498 int c, ret = 0;
500 spin_lock_irqsave(&port->lock, flags);
501 while (1) {
502 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
503 if (count < c)
504 c = count;
505 if (c <= 0)
506 break;
507 memcpy(circ->buf + circ->head, buf, c);
508 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
509 buf += c;
510 count -= c;
511 ret += c;
513 spin_unlock_irqrestore(&port->lock, flags);
515 return ret;
518 static void uart_put_char(struct tty_struct *tty, unsigned char ch)
520 struct uart_state *state = tty->driver_data;
522 __uart_put_char(state->port, &state->info->xmit, ch);
525 static void uart_flush_chars(struct tty_struct *tty)
527 uart_start(tty);
530 static int
531 uart_write(struct tty_struct *tty, int from_user, const unsigned char * buf,
532 int count)
534 struct uart_state *state = tty->driver_data;
535 int ret;
537 if (!state->info->xmit.buf)
538 return 0;
540 if (from_user)
541 ret = __uart_user_write(state->port, &state->info->xmit,
542 (const unsigned char __user *)buf, count);
543 else
544 ret = __uart_kern_write(state->port, &state->info->xmit,
545 buf, count);
547 uart_start(tty);
548 return ret;
551 static int uart_write_room(struct tty_struct *tty)
553 struct uart_state *state = tty->driver_data;
555 return uart_circ_chars_free(&state->info->xmit);
558 static int uart_chars_in_buffer(struct tty_struct *tty)
560 struct uart_state *state = tty->driver_data;
562 return uart_circ_chars_pending(&state->info->xmit);
565 static void uart_flush_buffer(struct tty_struct *tty)
567 struct uart_state *state = tty->driver_data;
568 struct uart_port *port = state->port;
569 unsigned long flags;
571 DPRINTK("uart_flush_buffer(%d) called\n", tty->index);
573 spin_lock_irqsave(&port->lock, flags);
574 uart_circ_clear(&state->info->xmit);
575 spin_unlock_irqrestore(&port->lock, flags);
576 tty_wakeup(tty);
580 * This function is used to send a high-priority XON/XOFF character to
581 * the device
583 static void uart_send_xchar(struct tty_struct *tty, char ch)
585 struct uart_state *state = tty->driver_data;
586 struct uart_port *port = state->port;
587 unsigned long flags;
589 if (port->ops->send_xchar)
590 port->ops->send_xchar(port, ch);
591 else {
592 port->x_char = ch;
593 if (ch) {
594 spin_lock_irqsave(&port->lock, flags);
595 port->ops->start_tx(port, 0);
596 spin_unlock_irqrestore(&port->lock, flags);
601 static void uart_throttle(struct tty_struct *tty)
603 struct uart_state *state = tty->driver_data;
605 if (I_IXOFF(tty))
606 uart_send_xchar(tty, STOP_CHAR(tty));
608 if (tty->termios->c_cflag & CRTSCTS)
609 uart_clear_mctrl(state->port, TIOCM_RTS);
612 static void uart_unthrottle(struct tty_struct *tty)
614 struct uart_state *state = tty->driver_data;
615 struct uart_port *port = state->port;
617 if (I_IXOFF(tty)) {
618 if (port->x_char)
619 port->x_char = 0;
620 else
621 uart_send_xchar(tty, START_CHAR(tty));
624 if (tty->termios->c_cflag & CRTSCTS)
625 uart_set_mctrl(port, TIOCM_RTS);
628 static int uart_get_info(struct uart_state *state,
629 struct serial_struct __user *retinfo)
631 struct uart_port *port = state->port;
632 struct serial_struct tmp;
634 memset(&tmp, 0, sizeof(tmp));
635 tmp.type = port->type;
636 tmp.line = port->line;
637 tmp.port = port->iobase;
638 if (HIGH_BITS_OFFSET)
639 tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
640 tmp.irq = port->irq;
641 tmp.flags = port->flags;
642 tmp.xmit_fifo_size = port->fifosize;
643 tmp.baud_base = port->uartclk / 16;
644 tmp.close_delay = state->close_delay;
645 tmp.closing_wait = state->closing_wait;
646 tmp.custom_divisor = port->custom_divisor;
647 tmp.hub6 = port->hub6;
648 tmp.io_type = port->iotype;
649 tmp.iomem_reg_shift = port->regshift;
650 tmp.iomem_base = (void *)port->mapbase;
652 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
653 return -EFAULT;
654 return 0;
657 static int uart_set_info(struct uart_state *state,
658 struct serial_struct __user *newinfo)
660 struct serial_struct new_serial;
661 struct uart_port *port = state->port;
662 unsigned long new_port;
663 unsigned int change_irq, change_port, old_flags;
664 unsigned int old_custom_divisor;
665 int retval = 0;
667 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
668 return -EFAULT;
670 new_port = new_serial.port;
671 if (HIGH_BITS_OFFSET)
672 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
674 new_serial.irq = irq_canonicalize(new_serial.irq);
677 * This semaphore protects state->count. It is also
678 * very useful to prevent opens. Also, take the
679 * port configuration semaphore to make sure that a
680 * module insertion/removal doesn't change anything
681 * under us.
683 down(&state->sem);
685 change_irq = new_serial.irq != port->irq;
688 * Since changing the 'type' of the port changes its resource
689 * allocations, we should treat type changes the same as
690 * IO port changes.
692 change_port = new_port != port->iobase ||
693 (unsigned long)new_serial.iomem_base != port->mapbase ||
694 new_serial.hub6 != port->hub6 ||
695 new_serial.io_type != port->iotype ||
696 new_serial.iomem_reg_shift != port->regshift ||
697 new_serial.type != port->type;
699 old_flags = port->flags;
700 old_custom_divisor = port->custom_divisor;
702 if (!capable(CAP_SYS_ADMIN)) {
703 retval = -EPERM;
704 if (change_irq || change_port ||
705 (new_serial.baud_base != port->uartclk / 16) ||
706 (new_serial.close_delay != state->close_delay) ||
707 (new_serial.closing_wait != state->closing_wait) ||
708 (new_serial.xmit_fifo_size != port->fifosize) ||
709 (((new_serial.flags ^ old_flags) & ~UPF_USR_MASK) != 0))
710 goto exit;
711 port->flags = ((port->flags & ~UPF_USR_MASK) |
712 (new_serial.flags & UPF_USR_MASK));
713 port->custom_divisor = new_serial.custom_divisor;
714 goto check_and_exit;
718 * Ask the low level driver to verify the settings.
720 if (port->ops->verify_port)
721 retval = port->ops->verify_port(port, &new_serial);
723 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
724 (new_serial.baud_base < 9600))
725 retval = -EINVAL;
727 if (retval)
728 goto exit;
730 if (change_port || change_irq) {
731 retval = -EBUSY;
734 * Make sure that we are the sole user of this port.
736 if (uart_users(state) > 1)
737 goto exit;
740 * We need to shutdown the serial port at the old
741 * port/type/irq combination.
743 uart_shutdown(state);
746 if (change_port) {
747 unsigned long old_iobase, old_mapbase;
748 unsigned int old_type, old_iotype, old_hub6, old_shift;
750 old_iobase = port->iobase;
751 old_mapbase = port->mapbase;
752 old_type = port->type;
753 old_hub6 = port->hub6;
754 old_iotype = port->iotype;
755 old_shift = port->regshift;
758 * Free and release old regions
760 if (old_type != PORT_UNKNOWN)
761 port->ops->release_port(port);
763 port->iobase = new_port;
764 port->type = new_serial.type;
765 port->hub6 = new_serial.hub6;
766 port->iotype = new_serial.io_type;
767 port->regshift = new_serial.iomem_reg_shift;
768 port->mapbase = (unsigned long)new_serial.iomem_base;
771 * Claim and map the new regions
773 if (port->type != PORT_UNKNOWN) {
774 retval = port->ops->request_port(port);
775 } else {
776 /* Always success - Jean II */
777 retval = 0;
781 * If we fail to request resources for the
782 * new port, try to restore the old settings.
784 if (retval && old_type != PORT_UNKNOWN) {
785 port->iobase = old_iobase;
786 port->type = old_type;
787 port->hub6 = old_hub6;
788 port->iotype = old_iotype;
789 port->regshift = old_shift;
790 port->mapbase = old_mapbase;
791 retval = port->ops->request_port(port);
793 * If we failed to restore the old settings,
794 * we fail like this.
796 if (retval)
797 port->type = PORT_UNKNOWN;
800 * We failed anyway.
802 retval = -EBUSY;
806 port->irq = new_serial.irq;
807 port->uartclk = new_serial.baud_base * 16;
808 port->flags = (port->flags & ~UPF_CHANGE_MASK) |
809 (new_serial.flags & UPF_CHANGE_MASK);
810 port->custom_divisor = new_serial.custom_divisor;
811 state->close_delay = new_serial.close_delay * HZ / 100;
812 state->closing_wait = new_serial.closing_wait * HZ / 100;
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 up(&state->sem);
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 down(&state->sem);
880 if ((!file || !tty_hung_up_p(file)) &&
881 !(tty->flags & (1 << TTY_IO_ERROR))) {
882 result = port->mctrl;
883 result |= port->ops->get_mctrl(port);
885 up(&state->sem);
887 return result;
890 static int
891 uart_tiocmset(struct tty_struct *tty, struct file *file,
892 unsigned int set, unsigned int clear)
894 struct uart_state *state = tty->driver_data;
895 struct uart_port *port = state->port;
896 int ret = -EIO;
898 down(&state->sem);
899 if ((!file || !tty_hung_up_p(file)) &&
900 !(tty->flags & (1 << TTY_IO_ERROR))) {
901 uart_update_mctrl(port, set, clear);
902 ret = 0;
904 up(&state->sem);
905 return ret;
908 static void uart_break_ctl(struct tty_struct *tty, int break_state)
910 struct uart_state *state = tty->driver_data;
911 struct uart_port *port = state->port;
913 BUG_ON(!kernel_locked());
915 down(&state->sem);
917 if (port->type != PORT_UNKNOWN)
918 port->ops->break_ctl(port, break_state);
920 up(&state->sem);
923 static int uart_do_autoconfig(struct uart_state *state)
925 struct uart_port *port = state->port;
926 int flags, ret;
928 if (!capable(CAP_SYS_ADMIN))
929 return -EPERM;
932 * Take the per-port semaphore. This prevents count from
933 * changing, and hence any extra opens of the port while
934 * we're auto-configuring.
936 if (down_interruptible(&state->sem))
937 return -ERESTARTSYS;
939 ret = -EBUSY;
940 if (uart_users(state) == 1) {
941 uart_shutdown(state);
944 * If we already have a port type configured,
945 * we must release its resources.
947 if (port->type != PORT_UNKNOWN)
948 port->ops->release_port(port);
950 flags = UART_CONFIG_TYPE;
951 if (port->flags & UPF_AUTO_IRQ)
952 flags |= UART_CONFIG_IRQ;
955 * This will claim the ports resources if
956 * a port is found.
958 port->ops->config_port(port, flags);
960 ret = uart_startup(state, 1);
962 up(&state->sem);
963 return ret;
967 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
968 * - mask passed in arg for lines of interest
969 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
970 * Caller should use TIOCGICOUNT to see which one it was
972 static int
973 uart_wait_modem_status(struct uart_state *state, unsigned long arg)
975 struct uart_port *port = state->port;
976 DECLARE_WAITQUEUE(wait, current);
977 struct uart_icount cprev, cnow;
978 int ret;
981 * note the counters on entry
983 spin_lock_irq(&port->lock);
984 memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
987 * Force modem status interrupts on
989 port->ops->enable_ms(port);
990 spin_unlock_irq(&port->lock);
992 add_wait_queue(&state->info->delta_msr_wait, &wait);
993 for (;;) {
994 spin_lock_irq(&port->lock);
995 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
996 spin_unlock_irq(&port->lock);
998 set_current_state(TASK_INTERRUPTIBLE);
1000 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1001 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1002 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1003 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1004 ret = 0;
1005 break;
1008 schedule();
1010 /* see if a signal did it */
1011 if (signal_pending(current)) {
1012 ret = -ERESTARTSYS;
1013 break;
1016 cprev = cnow;
1019 current->state = TASK_RUNNING;
1020 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1022 return ret;
1026 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1027 * Return: write counters to the user passed counter struct
1028 * NB: both 1->0 and 0->1 transitions are counted except for
1029 * RI where only 0->1 is counted.
1031 static int uart_get_count(struct uart_state *state,
1032 struct serial_icounter_struct __user *icnt)
1034 struct serial_icounter_struct icount;
1035 struct uart_icount cnow;
1036 struct uart_port *port = state->port;
1038 spin_lock_irq(&port->lock);
1039 memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
1040 spin_unlock_irq(&port->lock);
1042 icount.cts = cnow.cts;
1043 icount.dsr = cnow.dsr;
1044 icount.rng = cnow.rng;
1045 icount.dcd = cnow.dcd;
1046 icount.rx = cnow.rx;
1047 icount.tx = cnow.tx;
1048 icount.frame = cnow.frame;
1049 icount.overrun = cnow.overrun;
1050 icount.parity = cnow.parity;
1051 icount.brk = cnow.brk;
1052 icount.buf_overrun = cnow.buf_overrun;
1054 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1058 * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
1060 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 02-22-2006
1061 #include <linux/signal.h>
1062 #include <linux/sched.h>
1063 #include <linux/timer.h>
1064 #include <linux/kd.h>
1065 #include <linux/delay.h>
1066 #include <asm/arch/gpio.h>
1067 //#if defined(CONFIG_ARCH_W311)
1068 //#define BEEPER_GPIO (1<<27)
1069 //#else
1070 #define BEEPER_GPIO (1<<24)
1071 //#endif
1072 static spinlock_t beeperlock;
1073 #endif
1074 static int
1075 uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
1077 struct uart_state *state = tty->driver_data;
1078 void __user *uarg = (void __user *)arg;
1079 int ret = -ENOIOCTLCMD;
1081 BUG_ON(!kernel_locked());
1084 * These ioctls don't rely on the hardware to be present.
1086 switch (cmd) {
1087 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 02-22-2006
1088 case KDMKTONE :
1090 unsigned long ms, endms;
1091 ms = arg >> 16;
1092 if ( HZ <= 1000 )
1093 ms = ms / (1000/HZ);
1094 else
1095 ms = (ms * 1000) / (1000000/HZ);
1096 if ( ms <= 0 )
1097 ms = 1;
1098 spin_lock(&beeperlock);
1099 mcpu_gpio_mp_set(BEEPER_GPIO);
1100 mcpu_gpio_inout(BEEPER_GPIO, MCPU_GPIO_OUTPUT);
1101 mcpu_gpio_set(BEEPER_GPIO, MCPU_GPIO_HIGH);
1102 endms = ms + jiffies;
1103 while ( ms && !time_after(jiffies, endms) ) {
1104 set_current_state(TASK_INTERRUPTIBLE);
1105 ms = schedule_timeout(ms);
1107 mcpu_gpio_set(BEEPER_GPIO, MCPU_GPIO_LOW);
1108 spin_unlock(&beeperlock);
1109 ret = 0;
1110 break;
1112 #endif
1113 case TIOCGSERIAL:
1114 ret = uart_get_info(state, uarg);
1115 break;
1117 case TIOCSSERIAL:
1118 ret = uart_set_info(state, uarg);
1119 break;
1121 case TIOCSERCONFIG:
1122 ret = uart_do_autoconfig(state);
1123 break;
1125 case TIOCSERGWILD: /* obsolete */
1126 case TIOCSERSWILD: /* obsolete */
1127 ret = 0;
1128 break;
1131 if (ret != -ENOIOCTLCMD)
1132 goto out;
1134 if (tty->flags & (1 << TTY_IO_ERROR)) {
1135 ret = -EIO;
1136 goto out;
1140 * The following should only be used when hardware is present.
1142 switch (cmd) {
1143 case TIOCMIWAIT:
1144 ret = uart_wait_modem_status(state, arg);
1145 break;
1147 case TIOCGICOUNT:
1148 ret = uart_get_count(state, uarg);
1149 break;
1152 if (ret != -ENOIOCTLCMD)
1153 goto out;
1155 down(&state->sem);
1157 if (tty_hung_up_p(filp)) {
1158 ret = -EIO;
1159 goto out_up;
1163 * All these rely on hardware being present and need to be
1164 * protected against the tty being hung up.
1166 switch (cmd) {
1167 case TIOCSERGETLSR: /* Get line status register */
1168 ret = uart_get_lsr_info(state, uarg);
1169 break;
1171 default: {
1172 struct uart_port *port = state->port;
1173 if (port->ops->ioctl)
1174 ret = port->ops->ioctl(port, cmd, arg);
1175 break;
1178 out_up:
1179 up(&state->sem);
1180 out:
1181 return ret;
1184 static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1186 struct uart_state *state = tty->driver_data;
1187 unsigned long flags;
1188 unsigned int cflag = tty->termios->c_cflag;
1190 BUG_ON(!kernel_locked());
1193 * These are the bits that are used to setup various
1194 * flags in the low level driver.
1196 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1198 if ((cflag ^ old_termios->c_cflag) == 0 &&
1199 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1200 return;
1202 uart_change_speed(state, old_termios);
1204 /* Handle transition to B0 status */
1205 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1206 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1208 /* Handle transition away from B0 status */
1209 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1210 unsigned int mask = TIOCM_DTR;
1211 if (!(cflag & CRTSCTS) ||
1212 !test_bit(TTY_THROTTLED, &tty->flags))
1213 mask |= TIOCM_RTS;
1214 uart_set_mctrl(state->port, mask);
1217 /* Handle turning off CRTSCTS */
1218 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1219 spin_lock_irqsave(&state->port->lock, flags);
1220 tty->hw_stopped = 0;
1221 __uart_start(tty);
1222 spin_unlock_irqrestore(&state->port->lock, flags);
1225 #if 0
1227 * No need to wake up processes in open wait, since they
1228 * sample the CLOCAL flag once, and don't recheck it.
1229 * XXX It's not clear whether the current behavior is correct
1230 * or not. Hence, this may change.....
1232 if (!(old_termios->c_cflag & CLOCAL) &&
1233 (tty->termios->c_cflag & CLOCAL))
1234 wake_up_interruptible(&state->info->open_wait);
1235 #endif
1239 * In 2.4.5, calls to this will be serialized via the BKL in
1240 * linux/drivers/char/tty_io.c:tty_release()
1241 * linux/drivers/char/tty_io.c:do_tty_handup()
1243 static void uart_close(struct tty_struct *tty, struct file *filp)
1245 struct uart_state *state = tty->driver_data;
1246 struct uart_port *port;
1248 BUG_ON(!kernel_locked());
1250 if (!state || !state->port)
1251 return;
1253 port = state->port;
1255 DPRINTK("uart_close(%d) called\n", port->line);
1257 down(&state->sem);
1259 if (tty_hung_up_p(filp))
1260 goto done;
1262 if ((tty->count == 1) && (state->count != 1)) {
1264 * Uh, oh. tty->count is 1, which means that the tty
1265 * structure will be freed. state->count should always
1266 * be one in these conditions. If it's greater than
1267 * one, we've got real problems, since it means the
1268 * serial port won't be shutdown.
1270 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1271 "state->count is %d\n", state->count);
1272 state->count = 1;
1274 if (--state->count < 0) {
1275 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1276 tty->name, state->count);
1277 state->count = 0;
1279 if (state->count)
1280 goto done;
1283 * Now we wait for the transmit buffer to clear; and we notify
1284 * the line discipline to only process XON/XOFF characters by
1285 * setting tty->closing.
1287 tty->closing = 1;
1289 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1290 tty_wait_until_sent(tty, state->closing_wait);
1293 * At this point, we stop accepting input. To do this, we
1294 * disable the receive line status interrupts.
1296 if (state->info->flags & UIF_INITIALIZED) {
1297 unsigned long flags;
1298 spin_lock_irqsave(&port->lock, flags);
1299 port->ops->stop_rx(port);
1300 spin_unlock_irqrestore(&port->lock, flags);
1302 * Before we drop DTR, make sure the UART transmitter
1303 * has completely drained; this is especially
1304 * important if there is a transmit FIFO!
1306 uart_wait_until_sent(tty, port->timeout);
1309 uart_shutdown(state);
1310 uart_flush_buffer(tty);
1312 tty_ldisc_flush(tty);
1314 tty->closing = 0;
1315 state->info->tty = NULL;
1317 if (state->info->blocked_open) {
1318 if (state->close_delay) {
1319 set_current_state(TASK_INTERRUPTIBLE);
1320 schedule_timeout(state->close_delay);
1322 } else if (!uart_console(port)) {
1323 uart_change_pm(state, 3);
1327 * Wake up anyone trying to open this port.
1329 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1330 wake_up_interruptible(&state->info->open_wait);
1332 done:
1333 up(&state->sem);
1336 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1338 struct uart_state *state = tty->driver_data;
1339 struct uart_port *port = state->port;
1340 unsigned long char_time, expire;
1342 BUG_ON(!kernel_locked());
1344 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1345 return;
1348 * Set the check interval to be 1/5 of the estimated time to
1349 * send a single character, and make it at least 1. The check
1350 * interval should also be less than the timeout.
1352 * Note: we have to use pretty tight timings here to satisfy
1353 * the NIST-PCTS.
1355 char_time = (port->timeout - HZ/50) / port->fifosize;
1356 char_time = char_time / 5;
1357 if (char_time == 0)
1358 char_time = 1;
1359 if (timeout && timeout < char_time)
1360 char_time = timeout;
1363 * If the transmitter hasn't cleared in twice the approximate
1364 * amount of time to send the entire FIFO, it probably won't
1365 * ever clear. This assumes the UART isn't doing flow
1366 * control, which is currently the case. Hence, if it ever
1367 * takes longer than port->timeout, this is probably due to a
1368 * UART bug of some kind. So, we clamp the timeout parameter at
1369 * 2*port->timeout.
1371 if (timeout == 0 || timeout > 2 * port->timeout)
1372 timeout = 2 * port->timeout;
1374 expire = jiffies + timeout;
1376 DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1377 port->line, jiffies, expire);
1380 * Check whether the transmitter is empty every 'char_time'.
1381 * 'timeout' / 'expire' give us the maximum amount of time
1382 * we wait.
1384 while (!port->ops->tx_empty(port)) {
1385 set_current_state(TASK_INTERRUPTIBLE);
1386 schedule_timeout(char_time);
1387 if (signal_pending(current))
1388 break;
1389 if (time_after(jiffies, expire))
1390 break;
1392 set_current_state(TASK_RUNNING); /* might not be needed */
1396 * This is called with the BKL held in
1397 * linux/drivers/char/tty_io.c:do_tty_hangup()
1398 * We're called from the eventd thread, so we can sleep for
1399 * a _short_ time only.
1401 static void uart_hangup(struct tty_struct *tty)
1403 struct uart_state *state = tty->driver_data;
1405 BUG_ON(!kernel_locked());
1406 DPRINTK("uart_hangup(%d)\n", state->port->line);
1408 down(&state->sem);
1409 if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1410 uart_flush_buffer(tty);
1411 uart_shutdown(state);
1412 state->count = 0;
1413 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1414 state->info->tty = NULL;
1415 wake_up_interruptible(&state->info->open_wait);
1416 wake_up_interruptible(&state->info->delta_msr_wait);
1418 up(&state->sem);
1422 * Copy across the serial console cflag setting into the termios settings
1423 * for the initial open of the port. This allows continuity between the
1424 * kernel settings, and the settings init adopts when it opens the port
1425 * for the first time.
1427 static void uart_update_termios(struct uart_state *state)
1429 struct tty_struct *tty = state->info->tty;
1430 struct uart_port *port = state->port;
1432 if (uart_console(port) && port->cons->cflag) {
1433 tty->termios->c_cflag = port->cons->cflag;
1434 port->cons->cflag = 0;
1438 * If the device failed to grab its irq resources,
1439 * or some other error occurred, don't try to talk
1440 * to the port hardware.
1442 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1444 * Make termios settings take effect.
1446 uart_change_speed(state, NULL);
1449 * And finally enable the RTS and DTR signals.
1451 if (tty->termios->c_cflag & CBAUD)
1452 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1457 * Block the open until the port is ready. We must be called with
1458 * the per-port semaphore held.
1460 static int
1461 uart_block_til_ready(struct file *filp, struct uart_state *state)
1463 DECLARE_WAITQUEUE(wait, current);
1464 struct uart_info *info = state->info;
1465 struct uart_port *port = state->port;
1467 info->blocked_open++;
1468 state->count--;
1470 add_wait_queue(&info->open_wait, &wait);
1471 while (1) {
1472 set_current_state(TASK_INTERRUPTIBLE);
1475 * If we have been hung up, tell userspace/restart open.
1477 if (tty_hung_up_p(filp) || info->tty == NULL)
1478 break;
1481 * If the port has been closed, tell userspace/restart open.
1483 if (!(info->flags & UIF_INITIALIZED))
1484 break;
1487 * If non-blocking mode is set, or CLOCAL mode is set,
1488 * we don't want to wait for the modem status lines to
1489 * indicate that the port is ready.
1491 * Also, if the port is not enabled/configured, we want
1492 * to allow the open to succeed here. Note that we will
1493 * have set TTY_IO_ERROR for a non-existant port.
1495 if ((filp->f_flags & O_NONBLOCK) ||
1496 (info->tty->termios->c_cflag & CLOCAL) ||
1497 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1498 break;
1502 * Set DTR to allow modem to know we're waiting. Do
1503 * not set RTS here - we want to make sure we catch
1504 * the data from the modem.
1506 if (info->tty->termios->c_cflag & CBAUD)
1507 uart_set_mctrl(port, TIOCM_DTR);
1510 * and wait for the carrier to indicate that the
1511 * modem is ready for us.
1513 if (port->ops->get_mctrl(port) & TIOCM_CAR)
1514 break;
1516 up(&state->sem);
1517 schedule();
1518 down(&state->sem);
1520 if (signal_pending(current))
1521 break;
1523 set_current_state(TASK_RUNNING);
1524 remove_wait_queue(&info->open_wait, &wait);
1526 state->count++;
1527 info->blocked_open--;
1529 if (signal_pending(current))
1530 return -ERESTARTSYS;
1532 if (!info->tty || tty_hung_up_p(filp))
1533 return -EAGAIN;
1535 return 0;
1538 static struct uart_state *uart_get(struct uart_driver *drv, int line)
1540 struct uart_state *state;
1542 down(&port_sem);
1543 state = drv->state + line;
1544 if (down_interruptible(&state->sem)) {
1545 state = ERR_PTR(-ERESTARTSYS);
1546 goto out;
1549 state->count++;
1550 if (!state->port) {
1551 state->count--;
1552 up(&state->sem);
1553 state = ERR_PTR(-ENXIO);
1554 goto out;
1557 if (!state->info) {
1558 state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
1559 if (state->info) {
1560 memset(state->info, 0, sizeof(struct uart_info));
1561 init_waitqueue_head(&state->info->open_wait);
1562 init_waitqueue_head(&state->info->delta_msr_wait);
1565 * Link the info into the other structures.
1567 state->port->info = state->info;
1569 tasklet_init(&state->info->tlet, uart_tasklet_action,
1570 (unsigned long)state);
1571 } else {
1572 state->count--;
1573 up(&state->sem);
1574 state = ERR_PTR(-ENOMEM);
1578 out:
1579 up(&port_sem);
1580 return state;
1584 * In 2.4.5, calls to uart_open are serialised by the BKL in
1585 * linux/fs/devices.c:chrdev_open()
1586 * Note that if this fails, then uart_close() _will_ be called.
1588 * In time, we want to scrap the "opening nonpresent ports"
1589 * behaviour and implement an alternative way for setserial
1590 * to set base addresses/ports/types. This will allow us to
1591 * get rid of a certain amount of extra tests.
1593 static int uart_open(struct tty_struct *tty, struct file *filp)
1595 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1596 struct uart_state *state;
1597 int retval, line = tty->index;
1599 BUG_ON(!kernel_locked());
1600 DPRINTK("uart_open(%d) called\n", line);
1603 * tty->driver->num won't change, so we won't fail here with
1604 * tty->driver_data set to something non-NULL (and therefore
1605 * we won't get caught by uart_close()).
1607 retval = -ENODEV;
1608 if (line >= tty->driver->num)
1609 goto fail;
1612 * We take the semaphore inside uart_get to guarantee that we won't
1613 * be re-entered while allocating the info structure, or while we
1614 * request any IRQs that the driver may need. This also has the nice
1615 * side-effect that it delays the action of uart_hangup, so we can
1616 * guarantee that info->tty will always contain something reasonable.
1618 state = uart_get(drv, line);
1619 if (IS_ERR(state)) {
1620 retval = PTR_ERR(state);
1621 goto fail;
1625 * Once we set tty->driver_data here, we are guaranteed that
1626 * uart_close() will decrement the driver module use count.
1627 * Any failures from here onwards should not touch the count.
1629 tty->driver_data = state;
1630 tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1631 tty->alt_speed = 0;
1632 state->info->tty = tty;
1635 * If the port is in the middle of closing, bail out now.
1637 if (tty_hung_up_p(filp)) {
1638 retval = -EAGAIN;
1639 state->count--;
1640 up(&state->sem);
1641 goto fail;
1645 * Make sure the device is in D0 state.
1647 if (state->count == 1)
1648 uart_change_pm(state, 0);
1651 * Start up the serial port.
1653 retval = uart_startup(state, 0);
1656 * If we succeeded, wait until the port is ready.
1658 if (retval == 0)
1659 retval = uart_block_til_ready(filp, state);
1660 up(&state->sem);
1663 * If this is the first open to succeed, adjust things to suit.
1665 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1666 state->info->flags |= UIF_NORMAL_ACTIVE;
1668 uart_update_termios(state);
1671 fail:
1672 return retval;
1675 static const char *uart_type(struct uart_port *port)
1677 const char *str = NULL;
1679 if (port->ops->type)
1680 str = port->ops->type(port);
1682 if (!str)
1683 str = "unknown";
1685 return str;
1688 #ifdef CONFIG_PROC_FS
1690 static int uart_line_info(char *buf, struct uart_driver *drv, int i)
1692 struct uart_state *state = drv->state + i;
1693 struct uart_port *port = state->port;
1694 char stat_buf[32];
1695 unsigned int status;
1696 int ret;
1698 if (!port)
1699 return 0;
1701 ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
1702 port->line, uart_type(port),
1703 port->iotype == UPIO_MEM ? "mmio:0x" : "port:",
1704 port->iotype == UPIO_MEM ? port->mapbase :
1705 (unsigned long) port->iobase,
1706 port->irq);
1708 if (port->type == PORT_UNKNOWN) {
1709 strcat(buf, "\n");
1710 return ret + 1;
1713 if(capable(CAP_SYS_ADMIN))
1715 status = port->ops->get_mctrl(port);
1717 ret += sprintf(buf + ret, " tx:%d rx:%d",
1718 port->icount.tx, port->icount.rx);
1719 if (port->icount.frame)
1720 ret += sprintf(buf + ret, " fe:%d",
1721 port->icount.frame);
1722 if (port->icount.parity)
1723 ret += sprintf(buf + ret, " pe:%d",
1724 port->icount.parity);
1725 if (port->icount.brk)
1726 ret += sprintf(buf + ret, " brk:%d",
1727 port->icount.brk);
1728 if (port->icount.overrun)
1729 ret += sprintf(buf + ret, " oe:%d",
1730 port->icount.overrun);
1732 #define INFOBIT(bit,str) \
1733 if (port->mctrl & (bit)) \
1734 strncat(stat_buf, (str), sizeof(stat_buf) - \
1735 strlen(stat_buf) - 2)
1736 #define STATBIT(bit,str) \
1737 if (status & (bit)) \
1738 strncat(stat_buf, (str), sizeof(stat_buf) - \
1739 strlen(stat_buf) - 2)
1741 stat_buf[0] = '\0';
1742 stat_buf[1] = '\0';
1743 INFOBIT(TIOCM_RTS, "|RTS");
1744 STATBIT(TIOCM_CTS, "|CTS");
1745 INFOBIT(TIOCM_DTR, "|DTR");
1746 STATBIT(TIOCM_DSR, "|DSR");
1747 STATBIT(TIOCM_CAR, "|CD");
1748 STATBIT(TIOCM_RNG, "|RI");
1749 if (stat_buf[0])
1750 stat_buf[0] = ' ';
1751 strcat(stat_buf, "\n");
1753 ret += sprintf(buf + ret, stat_buf);
1754 } else {
1755 strcat(buf, "\n");
1756 ret++;
1758 #undef STATBIT
1759 #undef INFOBIT
1760 return ret;
1763 static int uart_read_proc(char *page, char **start, off_t off,
1764 int count, int *eof, void *data)
1766 struct tty_driver *ttydrv = data;
1767 struct uart_driver *drv = ttydrv->driver_state;
1768 int i, len = 0, l;
1769 off_t begin = 0;
1771 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
1772 "", "", "");
1773 for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
1774 l = uart_line_info(page + len, drv, i);
1775 len += l;
1776 if (len + begin > off + count)
1777 goto done;
1778 if (len + begin < off) {
1779 begin += len;
1780 len = 0;
1783 *eof = 1;
1784 done:
1785 if (off >= len + begin)
1786 return 0;
1787 *start = page + (off - begin);
1788 return (count < begin + len - off) ? count : (begin + len - off);
1790 #endif
1792 #ifdef CONFIG_SERIAL_CORE_CONSOLE
1794 * Check whether an invalid uart number has been specified, and
1795 * if so, search for the first available port that does have
1796 * console support.
1798 struct uart_port * __init
1799 uart_get_console(struct uart_port *ports, int nr, struct console *co)
1801 int idx = co->index;
1803 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1804 ports[idx].membase == NULL))
1805 for (idx = 0; idx < nr; idx++)
1806 if (ports[idx].iobase != 0 ||
1807 ports[idx].membase != NULL)
1808 break;
1810 co->index = idx;
1812 return ports + idx;
1816 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1817 * @options: pointer to option string
1818 * @baud: pointer to an 'int' variable for the baud rate.
1819 * @parity: pointer to an 'int' variable for the parity.
1820 * @bits: pointer to an 'int' variable for the number of data bits.
1821 * @flow: pointer to an 'int' variable for the flow control character.
1823 * uart_parse_options decodes a string containing the serial console
1824 * options. The format of the string is <baud><parity><bits><flow>,
1825 * eg: 115200n8r
1827 void __init
1828 uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1830 char *s = options;
1832 *baud = simple_strtoul(s, NULL, 10);
1833 while (*s >= '0' && *s <= '9')
1834 s++;
1835 if (*s)
1836 *parity = *s++;
1837 if (*s)
1838 *bits = *s++ - '0';
1839 if (*s)
1840 *flow = *s;
1843 struct baud_rates {
1844 unsigned int rate;
1845 unsigned int cflag;
1848 static struct baud_rates baud_rates[] = {
1849 { 921600, B921600 },
1850 { 460800, B460800 },
1851 { 230400, B230400 },
1852 { 115200, B115200 },
1853 { 57600, B57600 },
1854 { 38400, B38400 },
1855 { 19200, B19200 },
1856 { 9600, B9600 },
1857 { 4800, B4800 },
1858 { 2400, B2400 },
1859 { 1200, B1200 },
1860 { 0, B38400 }
1864 * uart_set_options - setup the serial console parameters
1865 * @port: pointer to the serial ports uart_port structure
1866 * @co: console pointer
1867 * @baud: baud rate
1868 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1869 * @bits: number of data bits
1870 * @flow: flow control character - 'r' (rts)
1872 int __init
1873 uart_set_options(struct uart_port *port, struct console *co,
1874 int baud, int parity, int bits, int flow)
1876 struct termios termios;
1877 int i;
1879 memset(&termios, 0, sizeof(struct termios));
1881 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1884 * Construct a cflag setting.
1886 for (i = 0; baud_rates[i].rate; i++)
1887 if (baud_rates[i].rate <= baud)
1888 break;
1890 termios.c_cflag |= baud_rates[i].cflag;
1892 if (bits == 7)
1893 termios.c_cflag |= CS7;
1894 else
1895 termios.c_cflag |= CS8;
1897 switch (parity) {
1898 case 'o': case 'O':
1899 termios.c_cflag |= PARODD;
1900 /*fall through*/
1901 case 'e': case 'E':
1902 termios.c_cflag |= PARENB;
1903 break;
1906 if (flow == 'r')
1907 termios.c_cflag |= CRTSCTS;
1909 port->ops->set_termios(port, &termios, NULL);
1910 co->cflag = termios.c_cflag;
1912 return 0;
1914 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1916 static void uart_change_pm(struct uart_state *state, int pm_state)
1918 struct uart_port *port = state->port;
1919 if (port->ops->pm)
1920 port->ops->pm(port, pm_state, state->pm_state);
1921 state->pm_state = pm_state;
1924 int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
1926 struct uart_state *state = drv->state + port->line;
1928 down(&state->sem);
1930 if (state->info && state->info->flags & UIF_INITIALIZED) {
1931 struct uart_ops *ops = port->ops;
1933 spin_lock_irq(&port->lock);
1934 ops->stop_tx(port, 0);
1935 ops->set_mctrl(port, 0);
1936 ops->stop_rx(port);
1937 spin_unlock_irq(&port->lock);
1940 * Wait for the transmitter to empty.
1942 while (!ops->tx_empty(port)) {
1943 set_current_state(TASK_UNINTERRUPTIBLE);
1944 schedule_timeout(10*HZ/1000);
1946 set_current_state(TASK_RUNNING);
1948 ops->shutdown(port);
1952 * Disable the console device before suspending.
1954 if (uart_console(port))
1955 console_stop(port->cons);
1957 uart_change_pm(state, 3);
1959 up(&state->sem);
1961 return 0;
1964 int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
1966 struct uart_state *state = drv->state + port->line;
1968 down(&state->sem);
1970 uart_change_pm(state, 0);
1973 * Re-enable the console device after suspending.
1975 if (uart_console(port)) {
1976 uart_change_speed(state, NULL);
1977 console_start(port->cons);
1980 if (state->info && state->info->flags & UIF_INITIALIZED) {
1981 struct uart_ops *ops = port->ops;
1983 ops->set_mctrl(port, 0);
1984 ops->startup(port);
1985 uart_change_speed(state, NULL);
1986 spin_lock_irq(&port->lock);
1987 ops->set_mctrl(port, port->mctrl);
1988 ops->start_tx(port, 0);
1989 spin_unlock_irq(&port->lock);
1992 up(&state->sem);
1994 return 0;
1997 static inline void
1998 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2000 printk("%s%d", drv->dev_name, port->line);
2001 printk(" at ");
2002 switch (port->iotype) {
2003 case UPIO_PORT:
2004 printk("I/O 0x%x", port->iobase);
2005 break;
2006 case UPIO_HUB6:
2007 printk("I/O 0x%x offset 0x%x", port->iobase, port->hub6);
2008 break;
2009 case UPIO_MEM:
2010 case UPIO_MEM32:
2011 printk("MMIO 0x%lx", port->mapbase);
2012 break;
2014 printk(" (irq = %d) is a %s\n", port->irq, uart_type(port));
2017 static void
2018 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2019 struct uart_port *port)
2021 unsigned int flags;
2024 * If there isn't a port here, don't do anything further.
2026 if (!port->iobase && !port->mapbase && !port->membase)
2027 return;
2030 * Now do the auto configuration stuff. Note that config_port
2031 * is expected to claim the resources and map the port for us.
2033 flags = UART_CONFIG_TYPE;
2034 if (port->flags & UPF_AUTO_IRQ)
2035 flags |= UART_CONFIG_IRQ;
2036 if (port->flags & UPF_BOOT_AUTOCONF) {
2037 port->type = PORT_UNKNOWN;
2038 port->ops->config_port(port, flags);
2041 if (port->type != PORT_UNKNOWN) {
2042 unsigned long flags;
2044 uart_report_port(drv, port);
2047 * Ensure that the modem control lines are de-activated.
2048 * We probably don't need a spinlock around this, but
2050 spin_lock_irqsave(&port->lock, flags);
2051 port->ops->set_mctrl(port, 0);
2052 spin_unlock_irqrestore(&port->lock, flags);
2055 * Power down all ports by default, except the
2056 * console if we have one.
2058 if (!uart_console(port))
2059 uart_change_pm(state, 3);
2064 * This reverses the effects of uart_configure_port, hanging up the
2065 * port before removal.
2067 static void
2068 uart_unconfigure_port(struct uart_driver *drv, struct uart_state *state)
2070 struct uart_port *port = state->port;
2071 struct uart_info *info = state->info;
2073 if (info && info->tty)
2074 tty_vhangup(info->tty);
2076 down(&state->sem);
2078 state->info = NULL;
2081 * Free the port IO and memory resources, if any.
2083 if (port->type != PORT_UNKNOWN)
2084 port->ops->release_port(port);
2087 * Indicate that there isn't a port here anymore.
2089 port->type = PORT_UNKNOWN;
2092 * Kill the tasklet, and free resources.
2094 if (info) {
2095 tasklet_kill(&info->tlet);
2096 kfree(info);
2099 up(&state->sem);
2102 static struct tty_operations uart_ops = {
2103 .open = uart_open,
2104 .close = uart_close,
2105 .write = uart_write,
2106 .put_char = uart_put_char,
2107 .flush_chars = uart_flush_chars,
2108 .write_room = uart_write_room,
2109 .chars_in_buffer= uart_chars_in_buffer,
2110 .flush_buffer = uart_flush_buffer,
2111 .ioctl = uart_ioctl,
2112 .throttle = uart_throttle,
2113 .unthrottle = uart_unthrottle,
2114 .send_xchar = uart_send_xchar,
2115 .set_termios = uart_set_termios,
2116 .stop = uart_stop,
2117 .start = uart_start,
2118 .hangup = uart_hangup,
2119 .break_ctl = uart_break_ctl,
2120 .wait_until_sent= uart_wait_until_sent,
2121 #ifdef CONFIG_PROC_FS
2122 .read_proc = uart_read_proc,
2123 #endif
2124 .tiocmget = uart_tiocmget,
2125 .tiocmset = uart_tiocmset,
2129 * uart_register_driver - register a driver with the uart core layer
2130 * @drv: low level driver structure
2132 * Register a uart driver with the core driver. We in turn register
2133 * with the tty layer, and initialise the core driver per-port state.
2135 * We have a proc file in /proc/tty/driver which is named after the
2136 * normal driver.
2138 * drv->port should be NULL, and the per-port structures should be
2139 * registered using uart_add_one_port after this call has succeeded.
2141 int uart_register_driver(struct uart_driver *drv)
2143 struct tty_driver *normal = NULL;
2144 int i, retval;
2146 BUG_ON(drv->state);
2149 * Maybe we should be using a slab cache for this, especially if
2150 * we have a large number of ports to handle.
2152 drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2153 retval = -ENOMEM;
2154 if (!drv->state)
2155 goto out;
2157 memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
2159 normal = alloc_tty_driver(drv->nr);
2160 if (!normal)
2161 goto out;
2163 drv->tty_driver = normal;
2165 normal->owner = drv->owner;
2166 normal->driver_name = drv->driver_name;
2167 normal->devfs_name = drv->devfs_name;
2168 normal->name = drv->dev_name;
2169 normal->major = drv->major;
2170 normal->minor_start = drv->minor;
2171 normal->type = TTY_DRIVER_TYPE_SERIAL;
2172 normal->subtype = SERIAL_TYPE_NORMAL;
2173 normal->init_termios = tty_std_termios;
2174 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2175 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2176 normal->driver_state = drv;
2177 tty_set_operations(normal, &uart_ops);
2180 * Initialise the UART state(s).
2182 for (i = 0; i < drv->nr; i++) {
2183 struct uart_state *state = drv->state + i;
2185 state->close_delay = 5 * HZ / 10;
2186 state->closing_wait = 30 * HZ;
2188 init_MUTEX(&state->sem);
2191 retval = tty_register_driver(normal);
2192 out:
2193 if (retval < 0) {
2194 put_tty_driver(normal);
2195 kfree(drv->state);
2197 return retval;
2201 * uart_unregister_driver - remove a driver from the uart core layer
2202 * @drv: low level driver structure
2204 * Remove all references to a driver from the core driver. The low
2205 * level driver must have removed all its ports via the
2206 * uart_remove_one_port() if it registered them with uart_add_one_port().
2207 * (ie, drv->port == NULL)
2209 void uart_unregister_driver(struct uart_driver *drv)
2211 struct tty_driver *p = drv->tty_driver;
2212 tty_unregister_driver(p);
2213 put_tty_driver(p);
2214 kfree(drv->state);
2215 drv->tty_driver = NULL;
2218 struct tty_driver *uart_console_device(struct console *co, int *index)
2220 struct uart_driver *p = co->data;
2221 *index = co->index;
2222 return p->tty_driver;
2226 * uart_add_one_port - attach a driver-defined port structure
2227 * @drv: pointer to the uart low level driver structure for this port
2228 * @port: uart port structure to use for this port.
2230 * This allows the driver to register its own uart_port structure
2231 * with the core driver. The main purpose is to allow the low
2232 * level uart drivers to expand uart_port, rather than having yet
2233 * more levels of structures.
2235 int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
2237 struct uart_state *state;
2238 int ret = 0;
2240 BUG_ON(in_interrupt());
2242 if (port->line >= drv->nr)
2243 return -EINVAL;
2245 state = drv->state + port->line;
2247 down(&port_sem);
2248 if (state->port) {
2249 ret = -EINVAL;
2250 goto out;
2253 state->port = port;
2255 spin_lock_init(&port->lock);
2256 port->cons = drv->cons;
2257 port->info = state->info;
2259 uart_configure_port(drv, state, port);
2262 * Register the port whether it's detected or not. This allows
2263 * setserial to be used to alter this ports parameters.
2265 tty_register_device(drv->tty_driver, port->line, port->dev);
2267 out:
2268 up(&port_sem);
2270 return ret;
2274 * uart_remove_one_port - detach a driver defined port structure
2275 * @drv: pointer to the uart low level driver structure for this port
2276 * @port: uart port structure for this port
2278 * This unhooks (and hangs up) the specified port structure from the
2279 * core driver. No further calls will be made to the low-level code
2280 * for this port.
2282 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
2284 struct uart_state *state = drv->state + port->line;
2286 BUG_ON(in_interrupt());
2288 if (state->port != port)
2289 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2290 state->port, port);
2292 down(&port_sem);
2295 * Remove the devices from devfs
2297 tty_unregister_device(drv->tty_driver, port->line);
2299 uart_unconfigure_port(drv, state);
2300 state->port = NULL;
2301 up(&port_sem);
2303 return 0;
2307 * Are the two ports equivalent?
2309 static int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2311 if (port1->iotype != port2->iotype)
2312 return 0;
2314 switch (port1->iotype) {
2315 case UPIO_PORT:
2316 return (port1->iobase == port2->iobase);
2317 case UPIO_HUB6:
2318 return (port1->iobase == port2->iobase) &&
2319 (port1->hub6 == port2->hub6);
2320 case UPIO_MEM:
2321 return (port1->membase == port2->membase);
2323 return 0;
2327 * Try to find an unused uart_state slot for a port.
2329 static struct uart_state *
2330 uart_find_match_or_unused(struct uart_driver *drv, struct uart_port *port)
2332 int i;
2335 * First, find a port entry which matches. Note: if we do
2336 * find a matching entry, and it has a non-zero use count,
2337 * then we can't register the port.
2339 for (i = 0; i < drv->nr; i++)
2340 if (uart_match_port(drv->state[i].port, port))
2341 return &drv->state[i];
2344 * We didn't find a matching entry, so look for the first
2345 * free entry. We look for one which hasn't been previously
2346 * used (indicated by zero iobase).
2348 for (i = 0; i < drv->nr; i++)
2349 if (drv->state[i].port->type == PORT_UNKNOWN &&
2350 drv->state[i].port->iobase == 0 &&
2351 drv->state[i].count == 0)
2352 return &drv->state[i];
2355 * That also failed. Last resort is to find any currently
2356 * entry which doesn't have a real port associated with it.
2358 for (i = 0; i < drv->nr; i++)
2359 if (drv->state[i].port->type == PORT_UNKNOWN &&
2360 drv->state[i].count == 0)
2361 return &drv->state[i];
2363 return NULL;
2367 * uart_register_port: register uart settings with a port
2368 * @drv: pointer to the uart low level driver structure for this port
2369 * @port: uart port structure describing the port
2371 * Register UART settings with the specified low level driver. Detect
2372 * the type of the port if UPF_BOOT_AUTOCONF is set, and detect the
2373 * IRQ if UPF_AUTO_IRQ is set.
2375 * We try to pick the same port for the same IO base address, so that
2376 * when a modem is plugged in, unplugged and plugged back in, it gets
2377 * allocated the same port.
2379 * Returns negative error, or positive line number.
2381 int uart_register_port(struct uart_driver *drv, struct uart_port *port)
2383 struct uart_state *state;
2384 int ret;
2386 down(&port_sem);
2388 state = uart_find_match_or_unused(drv, port);
2390 if (state) {
2392 * Ok, we've found a line that we can use.
2394 * If we find a port that matches this one, and it appears
2395 * to be in-use (even if it doesn't have a type) we shouldn't
2396 * alter it underneath itself - the port may be open and
2397 * trying to do useful work.
2399 if (uart_users(state) != 0) {
2400 ret = -EBUSY;
2401 goto out;
2405 * If the port is already initialised, don't touch it.
2407 if (state->port->type == PORT_UNKNOWN) {
2408 state->port->iobase = port->iobase;
2409 state->port->membase = port->membase;
2410 state->port->irq = port->irq;
2411 state->port->uartclk = port->uartclk;
2412 state->port->fifosize = port->fifosize;
2413 state->port->regshift = port->regshift;
2414 state->port->iotype = port->iotype;
2415 state->port->flags = port->flags;
2416 state->port->line = state - drv->state;
2417 state->port->mapbase = port->mapbase;
2419 uart_configure_port(drv, state, state->port);
2422 ret = state->port->line;
2423 } else
2424 ret = -ENOSPC;
2425 out:
2426 up(&port_sem);
2427 return ret;
2431 * uart_unregister_port - de-allocate a port
2432 * @drv: pointer to the uart low level driver structure for this port
2433 * @line: line index previously returned from uart_register_port()
2435 * Hang up the specified line associated with the low level driver,
2436 * and mark the port as unused.
2438 void uart_unregister_port(struct uart_driver *drv, int line)
2440 struct uart_state *state;
2442 if (line < 0 || line >= drv->nr) {
2443 printk(KERN_ERR "Attempt to unregister ");
2444 printk("%s%d", drv->dev_name, line);
2445 printk("\n");
2446 return;
2449 state = drv->state + line;
2451 down(&port_sem);
2452 uart_unconfigure_port(drv, state);
2453 up(&port_sem);
2456 EXPORT_SYMBOL(uart_write_wakeup);
2457 EXPORT_SYMBOL(uart_register_driver);
2458 EXPORT_SYMBOL(uart_unregister_driver);
2459 EXPORT_SYMBOL(uart_suspend_port);
2460 EXPORT_SYMBOL(uart_resume_port);
2461 EXPORT_SYMBOL(uart_register_port);
2462 EXPORT_SYMBOL(uart_unregister_port);
2463 EXPORT_SYMBOL(uart_add_one_port);
2464 EXPORT_SYMBOL(uart_remove_one_port);
2466 MODULE_DESCRIPTION("Serial driver core");
2467 MODULE_LICENSE("GPL");