2 * Driver core for serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/device.h>
31 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
32 #include <linux/serial_core.h>
33 #include <linux/delay.h>
34 #include <linux/mutex.h>
37 #include <asm/uaccess.h>
40 * This is used to lock changes in serial line configuration.
42 static DEFINE_MUTEX(port_mutex
);
45 * lockdep: port->lock is initialized in two places, but we
46 * want only one lock-class:
48 static struct lock_class_key port_lock_key
;
50 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
52 #ifdef CONFIG_SERIAL_CORE_CONSOLE
53 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
55 #define uart_console(port) (0)
58 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
59 struct ktermios
*old_termios
);
60 static void __uart_wait_until_sent(struct uart_port
*port
, int timeout
);
61 static void uart_change_pm(struct uart_state
*state
, int pm_state
);
64 * This routine is used by the interrupt handler to schedule processing in
65 * the software interrupt portion of the driver.
67 void uart_write_wakeup(struct uart_port
*port
)
69 struct uart_state
*state
= port
->state
;
71 * This means you called this function _after_ the port was
72 * closed. No cookie for you.
75 tasklet_schedule(&state
->tlet
);
78 static void uart_stop(struct tty_struct
*tty
)
80 struct uart_state
*state
= tty
->driver_data
;
81 struct uart_port
*port
= state
->uart_port
;
84 spin_lock_irqsave(&port
->lock
, flags
);
85 port
->ops
->stop_tx(port
);
86 spin_unlock_irqrestore(&port
->lock
, flags
);
89 static void __uart_start(struct tty_struct
*tty
)
91 struct uart_state
*state
= tty
->driver_data
;
92 struct uart_port
*port
= state
->uart_port
;
94 if (!uart_circ_empty(&state
->xmit
) && state
->xmit
.buf
&&
95 !tty
->stopped
&& !tty
->hw_stopped
)
96 port
->ops
->start_tx(port
);
99 static void uart_start(struct tty_struct
*tty
)
101 struct uart_state
*state
= tty
->driver_data
;
102 struct uart_port
*port
= state
->uart_port
;
105 spin_lock_irqsave(&port
->lock
, flags
);
107 spin_unlock_irqrestore(&port
->lock
, flags
);
110 static void uart_tasklet_action(unsigned long data
)
112 struct uart_state
*state
= (struct uart_state
*)data
;
113 tty_wakeup(state
->port
.tty
);
117 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
122 spin_lock_irqsave(&port
->lock
, flags
);
124 port
->mctrl
= (old
& ~clear
) | set
;
125 if (old
!= port
->mctrl
)
126 port
->ops
->set_mctrl(port
, port
->mctrl
);
127 spin_unlock_irqrestore(&port
->lock
, flags
);
130 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
131 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
134 * Startup the port. This will be called once per open. All calls
135 * will be serialised by the per-port mutex.
137 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
, int init_hw
)
139 struct uart_port
*uport
= state
->uart_port
;
140 struct tty_port
*port
= &state
->port
;
144 if (port
->flags
& ASYNC_INITIALIZED
)
148 * Set the TTY IO error marker - we will only clear this
149 * once we have successfully opened the port. Also set
150 * up the tty->alt_speed kludge
152 set_bit(TTY_IO_ERROR
, &tty
->flags
);
154 if (uport
->type
== PORT_UNKNOWN
)
158 * Initialise and allocate the transmit and temporary
161 if (!state
->xmit
.buf
) {
162 /* This is protected by the per port mutex */
163 page
= get_zeroed_page(GFP_KERNEL
);
167 state
->xmit
.buf
= (unsigned char *) page
;
168 uart_circ_clear(&state
->xmit
);
171 retval
= uport
->ops
->startup(uport
);
173 if (uart_console(uport
) && uport
->cons
->cflag
) {
174 tty
->termios
->c_cflag
= uport
->cons
->cflag
;
175 uport
->cons
->cflag
= 0;
178 * Initialise the hardware port settings.
180 uart_change_speed(tty
, state
, NULL
);
184 * Setup the RTS and DTR signals once the
185 * port is open and ready to respond.
187 if (tty
->termios
->c_cflag
& CBAUD
)
188 uart_set_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
191 if (port
->flags
& ASYNC_CTS_FLOW
) {
192 spin_lock_irq(&uport
->lock
);
193 if (!(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
))
195 spin_unlock_irq(&uport
->lock
);
198 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
200 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
203 if (retval
&& capable(CAP_SYS_ADMIN
))
210 * This routine will shutdown a serial port; interrupts are disabled, and
211 * DTR is dropped if the hangup on close termio flag is on. Calls to
212 * uart_shutdown are serialised by the per-port semaphore.
214 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
216 struct uart_port
*uport
= state
->uart_port
;
217 struct tty_port
*port
= &state
->port
;
220 * Set the TTY IO error marker
223 set_bit(TTY_IO_ERROR
, &tty
->flags
);
225 if (test_and_clear_bit(ASYNCB_INITIALIZED
, &port
->flags
)) {
227 * Turn off DTR and RTS early.
229 if (!tty
|| (tty
->termios
->c_cflag
& HUPCL
))
230 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
233 * clear delta_msr_wait queue to avoid mem leaks: we may free
234 * the irq here so the queue might never be woken up. Note
235 * that we won't end up waiting on delta_msr_wait again since
236 * any outstanding file descriptors should be pointing at
237 * hung_up_tty_fops now.
239 wake_up_interruptible(&port
->delta_msr_wait
);
242 * Free the IRQ and disable the port.
244 uport
->ops
->shutdown(uport
);
247 * Ensure that the IRQ handler isn't running on another CPU.
249 synchronize_irq(uport
->irq
);
253 * kill off our tasklet
255 tasklet_kill(&state
->tlet
);
258 * Free the transmit buffer page.
260 if (state
->xmit
.buf
) {
261 free_page((unsigned long)state
->xmit
.buf
);
262 state
->xmit
.buf
= NULL
;
267 * uart_update_timeout - update per-port FIFO timeout.
268 * @port: uart_port structure describing the port
269 * @cflag: termios cflag value
270 * @baud: speed of the port
272 * Set the port FIFO timeout value. The @cflag value should
273 * reflect the actual hardware settings.
276 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
281 /* byte size and parity */
282 switch (cflag
& CSIZE
) {
303 * The total number of bits to be transmitted in the fifo.
305 bits
= bits
* port
->fifosize
;
308 * Figure the timeout to send the above number of bits.
309 * Add .02 seconds of slop
311 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
314 EXPORT_SYMBOL(uart_update_timeout
);
317 * uart_get_baud_rate - return baud rate for a particular port
318 * @port: uart_port structure describing the port in question.
319 * @termios: desired termios settings.
320 * @old: old termios (or NULL)
321 * @min: minimum acceptable baud rate
322 * @max: maximum acceptable baud rate
324 * Decode the termios structure into a numeric baud rate,
325 * taking account of the magic 38400 baud rate (with spd_*
326 * flags), and mapping the %B0 rate to 9600 baud.
328 * If the new baud rate is invalid, try the old termios setting.
329 * If it's still invalid, we try 9600 baud.
331 * Update the @termios structure to reflect the baud rate
332 * we're actually going to be using. Don't do this for the case
333 * where B0 is requested ("hang up").
336 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
337 struct ktermios
*old
, unsigned int min
, unsigned int max
)
339 unsigned int try, baud
, altbaud
= 38400;
341 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
343 if (flags
== UPF_SPD_HI
)
345 else if (flags
== UPF_SPD_VHI
)
347 else if (flags
== UPF_SPD_SHI
)
349 else if (flags
== UPF_SPD_WARP
)
352 for (try = 0; try < 2; try++) {
353 baud
= tty_termios_baud_rate(termios
);
356 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
363 * Special case: B0 rate.
370 if (baud
>= min
&& baud
<= max
)
374 * Oops, the quotient was zero. Try again with
375 * the old baud rate if possible.
377 termios
->c_cflag
&= ~CBAUD
;
379 baud
= tty_termios_baud_rate(old
);
381 tty_termios_encode_baud_rate(termios
,
388 * As a last resort, if the range cannot be met then clip to
389 * the nearest chip supported rate.
393 tty_termios_encode_baud_rate(termios
,
396 tty_termios_encode_baud_rate(termios
,
400 /* Should never happen */
405 EXPORT_SYMBOL(uart_get_baud_rate
);
408 * uart_get_divisor - return uart clock divisor
409 * @port: uart_port structure describing the port.
410 * @baud: desired baud rate
412 * Calculate the uart clock divisor for the port.
415 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
420 * Old custom speed handling.
422 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
423 quot
= port
->custom_divisor
;
425 quot
= (port
->uartclk
+ (8 * baud
)) / (16 * baud
);
430 EXPORT_SYMBOL(uart_get_divisor
);
432 /* FIXME: Consistent locking policy */
433 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
434 struct ktermios
*old_termios
)
436 struct tty_port
*port
= &state
->port
;
437 struct uart_port
*uport
= state
->uart_port
;
438 struct ktermios
*termios
;
441 * If we have no tty, termios, or the port does not exist,
442 * then we can't set the parameters for this port.
444 if (!tty
|| !tty
->termios
|| uport
->type
== PORT_UNKNOWN
)
447 termios
= tty
->termios
;
450 * Set flags based on termios cflag
452 if (termios
->c_cflag
& CRTSCTS
)
453 set_bit(ASYNCB_CTS_FLOW
, &port
->flags
);
455 clear_bit(ASYNCB_CTS_FLOW
, &port
->flags
);
457 if (termios
->c_cflag
& CLOCAL
)
458 clear_bit(ASYNCB_CHECK_CD
, &port
->flags
);
460 set_bit(ASYNCB_CHECK_CD
, &port
->flags
);
462 uport
->ops
->set_termios(uport
, termios
, old_termios
);
465 static inline int __uart_put_char(struct uart_port
*port
,
466 struct circ_buf
*circ
, unsigned char c
)
474 spin_lock_irqsave(&port
->lock
, flags
);
475 if (uart_circ_chars_free(circ
) != 0) {
476 circ
->buf
[circ
->head
] = c
;
477 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
480 spin_unlock_irqrestore(&port
->lock
, flags
);
484 static int uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
486 struct uart_state
*state
= tty
->driver_data
;
488 return __uart_put_char(state
->uart_port
, &state
->xmit
, ch
);
491 static void uart_flush_chars(struct tty_struct
*tty
)
496 static int uart_write(struct tty_struct
*tty
,
497 const unsigned char *buf
, int count
)
499 struct uart_state
*state
= tty
->driver_data
;
500 struct uart_port
*port
;
501 struct circ_buf
*circ
;
506 * This means you called this function _after_ the port was
507 * closed. No cookie for you.
514 port
= state
->uart_port
;
520 spin_lock_irqsave(&port
->lock
, flags
);
522 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
527 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
528 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
533 spin_unlock_irqrestore(&port
->lock
, flags
);
539 static int uart_write_room(struct tty_struct
*tty
)
541 struct uart_state
*state
= tty
->driver_data
;
545 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
546 ret
= uart_circ_chars_free(&state
->xmit
);
547 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
551 static int uart_chars_in_buffer(struct tty_struct
*tty
)
553 struct uart_state
*state
= tty
->driver_data
;
557 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
558 ret
= uart_circ_chars_pending(&state
->xmit
);
559 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
563 static void uart_flush_buffer(struct tty_struct
*tty
)
565 struct uart_state
*state
= tty
->driver_data
;
566 struct uart_port
*port
;
570 * This means you called this function _after_ the port was
571 * closed. No cookie for you.
578 port
= state
->uart_port
;
579 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
581 spin_lock_irqsave(&port
->lock
, flags
);
582 uart_circ_clear(&state
->xmit
);
583 if (port
->ops
->flush_buffer
)
584 port
->ops
->flush_buffer(port
);
585 spin_unlock_irqrestore(&port
->lock
, flags
);
590 * This function is used to send a high-priority XON/XOFF character to
593 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
595 struct uart_state
*state
= tty
->driver_data
;
596 struct uart_port
*port
= state
->uart_port
;
599 if (port
->ops
->send_xchar
)
600 port
->ops
->send_xchar(port
, ch
);
604 spin_lock_irqsave(&port
->lock
, flags
);
605 port
->ops
->start_tx(port
);
606 spin_unlock_irqrestore(&port
->lock
, flags
);
611 static void uart_throttle(struct tty_struct
*tty
)
613 struct uart_state
*state
= tty
->driver_data
;
616 uart_send_xchar(tty
, STOP_CHAR(tty
));
618 if (tty
->termios
->c_cflag
& CRTSCTS
)
619 uart_clear_mctrl(state
->uart_port
, TIOCM_RTS
);
622 static void uart_unthrottle(struct tty_struct
*tty
)
624 struct uart_state
*state
= tty
->driver_data
;
625 struct uart_port
*port
= state
->uart_port
;
631 uart_send_xchar(tty
, START_CHAR(tty
));
634 if (tty
->termios
->c_cflag
& CRTSCTS
)
635 uart_set_mctrl(port
, TIOCM_RTS
);
638 static int uart_get_info(struct uart_state
*state
,
639 struct serial_struct __user
*retinfo
)
641 struct uart_port
*uport
= state
->uart_port
;
642 struct tty_port
*port
= &state
->port
;
643 struct serial_struct tmp
;
645 memset(&tmp
, 0, sizeof(tmp
));
647 /* Ensure the state we copy is consistent and no hardware changes
649 mutex_lock(&port
->mutex
);
651 tmp
.type
= uport
->type
;
652 tmp
.line
= uport
->line
;
653 tmp
.port
= uport
->iobase
;
654 if (HIGH_BITS_OFFSET
)
655 tmp
.port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
656 tmp
.irq
= uport
->irq
;
657 tmp
.flags
= uport
->flags
;
658 tmp
.xmit_fifo_size
= uport
->fifosize
;
659 tmp
.baud_base
= uport
->uartclk
/ 16;
660 tmp
.close_delay
= port
->close_delay
/ 10;
661 tmp
.closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
662 ASYNC_CLOSING_WAIT_NONE
:
663 port
->closing_wait
/ 10;
664 tmp
.custom_divisor
= uport
->custom_divisor
;
665 tmp
.hub6
= uport
->hub6
;
666 tmp
.io_type
= uport
->iotype
;
667 tmp
.iomem_reg_shift
= uport
->regshift
;
668 tmp
.iomem_base
= (void *)(unsigned long)uport
->mapbase
;
670 mutex_unlock(&port
->mutex
);
672 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
677 static int uart_set_info(struct tty_struct
*tty
, struct uart_state
*state
,
678 struct serial_struct __user
*newinfo
)
680 struct serial_struct new_serial
;
681 struct uart_port
*uport
= state
->uart_port
;
682 struct tty_port
*port
= &state
->port
;
683 unsigned long new_port
;
684 unsigned int change_irq
, change_port
, closing_wait
;
685 unsigned int old_custom_divisor
, close_delay
;
686 upf_t old_flags
, new_flags
;
689 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
692 new_port
= new_serial
.port
;
693 if (HIGH_BITS_OFFSET
)
694 new_port
+= (unsigned long) new_serial
.port_high
<< HIGH_BITS_OFFSET
;
696 new_serial
.irq
= irq_canonicalize(new_serial
.irq
);
697 close_delay
= new_serial
.close_delay
* 10;
698 closing_wait
= new_serial
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
699 ASYNC_CLOSING_WAIT_NONE
: new_serial
.closing_wait
* 10;
702 * This semaphore protects port->count. It is also
703 * very useful to prevent opens. Also, take the
704 * port configuration semaphore to make sure that a
705 * module insertion/removal doesn't change anything
708 mutex_lock(&port
->mutex
);
710 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
711 && new_serial
.irq
!= uport
->irq
;
714 * Since changing the 'type' of the port changes its resource
715 * allocations, we should treat type changes the same as
718 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
719 && (new_port
!= uport
->iobase
||
720 (unsigned long)new_serial
.iomem_base
!= uport
->mapbase
||
721 new_serial
.hub6
!= uport
->hub6
||
722 new_serial
.io_type
!= uport
->iotype
||
723 new_serial
.iomem_reg_shift
!= uport
->regshift
||
724 new_serial
.type
!= uport
->type
);
726 old_flags
= uport
->flags
;
727 new_flags
= new_serial
.flags
;
728 old_custom_divisor
= uport
->custom_divisor
;
730 if (!capable(CAP_SYS_ADMIN
)) {
732 if (change_irq
|| change_port
||
733 (new_serial
.baud_base
!= uport
->uartclk
/ 16) ||
734 (close_delay
!= port
->close_delay
) ||
735 (closing_wait
!= port
->closing_wait
) ||
736 (new_serial
.xmit_fifo_size
&&
737 new_serial
.xmit_fifo_size
!= uport
->fifosize
) ||
738 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
740 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
741 (new_flags
& UPF_USR_MASK
));
742 uport
->custom_divisor
= new_serial
.custom_divisor
;
747 * Ask the low level driver to verify the settings.
749 if (uport
->ops
->verify_port
)
750 retval
= uport
->ops
->verify_port(uport
, &new_serial
);
752 if ((new_serial
.irq
>= nr_irqs
) || (new_serial
.irq
< 0) ||
753 (new_serial
.baud_base
< 9600))
759 if (change_port
|| change_irq
) {
763 * Make sure that we are the sole user of this port.
765 if (tty_port_users(port
) > 1)
769 * We need to shutdown the serial port at the old
770 * port/type/irq combination.
772 uart_shutdown(tty
, state
);
776 unsigned long old_iobase
, old_mapbase
;
777 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
779 old_iobase
= uport
->iobase
;
780 old_mapbase
= uport
->mapbase
;
781 old_type
= uport
->type
;
782 old_hub6
= uport
->hub6
;
783 old_iotype
= uport
->iotype
;
784 old_shift
= uport
->regshift
;
787 * Free and release old regions
789 if (old_type
!= PORT_UNKNOWN
)
790 uport
->ops
->release_port(uport
);
792 uport
->iobase
= new_port
;
793 uport
->type
= new_serial
.type
;
794 uport
->hub6
= new_serial
.hub6
;
795 uport
->iotype
= new_serial
.io_type
;
796 uport
->regshift
= new_serial
.iomem_reg_shift
;
797 uport
->mapbase
= (unsigned long)new_serial
.iomem_base
;
800 * Claim and map the new regions
802 if (uport
->type
!= PORT_UNKNOWN
) {
803 retval
= uport
->ops
->request_port(uport
);
805 /* Always success - Jean II */
810 * If we fail to request resources for the
811 * new port, try to restore the old settings.
813 if (retval
&& old_type
!= PORT_UNKNOWN
) {
814 uport
->iobase
= old_iobase
;
815 uport
->type
= old_type
;
816 uport
->hub6
= old_hub6
;
817 uport
->iotype
= old_iotype
;
818 uport
->regshift
= old_shift
;
819 uport
->mapbase
= old_mapbase
;
820 retval
= uport
->ops
->request_port(uport
);
822 * If we failed to restore the old settings,
826 uport
->type
= PORT_UNKNOWN
;
832 /* Added to return the correct error -Ram Gupta */
838 uport
->irq
= new_serial
.irq
;
839 if (!(uport
->flags
& UPF_FIXED_PORT
))
840 uport
->uartclk
= new_serial
.baud_base
* 16;
841 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
842 (new_flags
& UPF_CHANGE_MASK
);
843 uport
->custom_divisor
= new_serial
.custom_divisor
;
844 port
->close_delay
= close_delay
;
845 port
->closing_wait
= closing_wait
;
846 if (new_serial
.xmit_fifo_size
)
847 uport
->fifosize
= new_serial
.xmit_fifo_size
;
849 port
->tty
->low_latency
=
850 (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
854 if (uport
->type
== PORT_UNKNOWN
)
856 if (port
->flags
& ASYNC_INITIALIZED
) {
857 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
858 old_custom_divisor
!= uport
->custom_divisor
) {
860 * If they're setting up a custom divisor or speed,
861 * instead of clearing it, then bitch about it. No
862 * need to rate-limit; it's CAP_SYS_ADMIN only.
864 if (uport
->flags
& UPF_SPD_MASK
) {
867 "%s sets custom speed on %s. This "
868 "is deprecated.\n", current
->comm
,
869 tty_name(port
->tty
, buf
));
871 uart_change_speed(tty
, state
, NULL
);
874 retval
= uart_startup(tty
, state
, 1);
876 mutex_unlock(&port
->mutex
);
881 * uart_get_lsr_info - get line status register info
882 * @tty: tty associated with the UART
883 * @state: UART being queried
884 * @value: returned modem value
886 * Note: uart_ioctl protects us against hangups.
888 static int uart_get_lsr_info(struct tty_struct
*tty
,
889 struct uart_state
*state
, unsigned int __user
*value
)
891 struct uart_port
*uport
= state
->uart_port
;
894 result
= uport
->ops
->tx_empty(uport
);
897 * If we're about to load something into the transmit
898 * register, we'll pretend the transmitter isn't empty to
899 * avoid a race condition (depending on when the transmit
900 * interrupt happens).
903 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
904 !tty
->stopped
&& !tty
->hw_stopped
))
905 result
&= ~TIOCSER_TEMT
;
907 return put_user(result
, value
);
910 static int uart_tiocmget(struct tty_struct
*tty
)
912 struct uart_state
*state
= tty
->driver_data
;
913 struct tty_port
*port
= &state
->port
;
914 struct uart_port
*uport
= state
->uart_port
;
917 mutex_lock(&port
->mutex
);
918 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
919 result
= uport
->mctrl
;
920 spin_lock_irq(&uport
->lock
);
921 result
|= uport
->ops
->get_mctrl(uport
);
922 spin_unlock_irq(&uport
->lock
);
924 mutex_unlock(&port
->mutex
);
930 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
932 struct uart_state
*state
= tty
->driver_data
;
933 struct uart_port
*uport
= state
->uart_port
;
934 struct tty_port
*port
= &state
->port
;
937 mutex_lock(&port
->mutex
);
938 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
939 uart_update_mctrl(uport
, set
, clear
);
942 mutex_unlock(&port
->mutex
);
946 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
948 struct uart_state
*state
= tty
->driver_data
;
949 struct tty_port
*port
= &state
->port
;
950 struct uart_port
*uport
= state
->uart_port
;
952 mutex_lock(&port
->mutex
);
954 if (uport
->type
!= PORT_UNKNOWN
)
955 uport
->ops
->break_ctl(uport
, break_state
);
957 mutex_unlock(&port
->mutex
);
961 static int uart_do_autoconfig(struct tty_struct
*tty
,struct uart_state
*state
)
963 struct uart_port
*uport
= state
->uart_port
;
964 struct tty_port
*port
= &state
->port
;
967 if (!capable(CAP_SYS_ADMIN
))
971 * Take the per-port semaphore. This prevents count from
972 * changing, and hence any extra opens of the port while
973 * we're auto-configuring.
975 if (mutex_lock_interruptible(&port
->mutex
))
979 if (tty_port_users(port
) == 1) {
980 uart_shutdown(tty
, state
);
983 * If we already have a port type configured,
984 * we must release its resources.
986 if (uport
->type
!= PORT_UNKNOWN
)
987 uport
->ops
->release_port(uport
);
989 flags
= UART_CONFIG_TYPE
;
990 if (uport
->flags
& UPF_AUTO_IRQ
)
991 flags
|= UART_CONFIG_IRQ
;
994 * This will claim the ports resources if
997 uport
->ops
->config_port(uport
, flags
);
999 ret
= uart_startup(tty
, state
, 1);
1001 mutex_unlock(&port
->mutex
);
1006 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1007 * - mask passed in arg for lines of interest
1008 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1009 * Caller should use TIOCGICOUNT to see which one it was
1011 * FIXME: This wants extracting into a common all driver implementation
1012 * of TIOCMWAIT using tty_port.
1015 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
1017 struct uart_port
*uport
= state
->uart_port
;
1018 struct tty_port
*port
= &state
->port
;
1019 DECLARE_WAITQUEUE(wait
, current
);
1020 struct uart_icount cprev
, cnow
;
1024 * note the counters on entry
1026 spin_lock_irq(&uport
->lock
);
1027 memcpy(&cprev
, &uport
->icount
, sizeof(struct uart_icount
));
1030 * Force modem status interrupts on
1032 uport
->ops
->enable_ms(uport
);
1033 spin_unlock_irq(&uport
->lock
);
1035 add_wait_queue(&port
->delta_msr_wait
, &wait
);
1037 spin_lock_irq(&uport
->lock
);
1038 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1039 spin_unlock_irq(&uport
->lock
);
1041 set_current_state(TASK_INTERRUPTIBLE
);
1043 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1044 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1045 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1046 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1053 /* see if a signal did it */
1054 if (signal_pending(current
)) {
1062 current
->state
= TASK_RUNNING
;
1063 remove_wait_queue(&port
->delta_msr_wait
, &wait
);
1069 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1070 * Return: write counters to the user passed counter struct
1071 * NB: both 1->0 and 0->1 transitions are counted except for
1072 * RI where only 0->1 is counted.
1074 static int uart_get_icount(struct tty_struct
*tty
,
1075 struct serial_icounter_struct
*icount
)
1077 struct uart_state
*state
= tty
->driver_data
;
1078 struct uart_icount cnow
;
1079 struct uart_port
*uport
= state
->uart_port
;
1081 spin_lock_irq(&uport
->lock
);
1082 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1083 spin_unlock_irq(&uport
->lock
);
1085 icount
->cts
= cnow
.cts
;
1086 icount
->dsr
= cnow
.dsr
;
1087 icount
->rng
= cnow
.rng
;
1088 icount
->dcd
= cnow
.dcd
;
1089 icount
->rx
= cnow
.rx
;
1090 icount
->tx
= cnow
.tx
;
1091 icount
->frame
= cnow
.frame
;
1092 icount
->overrun
= cnow
.overrun
;
1093 icount
->parity
= cnow
.parity
;
1094 icount
->brk
= cnow
.brk
;
1095 icount
->buf_overrun
= cnow
.buf_overrun
;
1101 * Called via sys_ioctl. We can use spin_lock_irq() here.
1104 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1107 struct uart_state
*state
= tty
->driver_data
;
1108 struct tty_port
*port
= &state
->port
;
1109 void __user
*uarg
= (void __user
*)arg
;
1110 int ret
= -ENOIOCTLCMD
;
1114 * These ioctls don't rely on the hardware to be present.
1118 ret
= uart_get_info(state
, uarg
);
1122 ret
= uart_set_info(tty
, state
, uarg
);
1126 ret
= uart_do_autoconfig(tty
, state
);
1129 case TIOCSERGWILD
: /* obsolete */
1130 case TIOCSERSWILD
: /* obsolete */
1135 if (ret
!= -ENOIOCTLCMD
)
1138 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1144 * The following should only be used when hardware is present.
1148 ret
= uart_wait_modem_status(state
, arg
);
1152 if (ret
!= -ENOIOCTLCMD
)
1155 mutex_lock(&port
->mutex
);
1157 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1163 * All these rely on hardware being present and need to be
1164 * protected against the tty being hung up.
1167 case TIOCSERGETLSR
: /* Get line status register */
1168 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1172 struct uart_port
*uport
= state
->uart_port
;
1173 if (uport
->ops
->ioctl
)
1174 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1179 mutex_unlock(&port
->mutex
);
1184 static void uart_set_ldisc(struct tty_struct
*tty
)
1186 struct uart_state
*state
= tty
->driver_data
;
1187 struct uart_port
*uport
= state
->uart_port
;
1189 if (uport
->ops
->set_ldisc
)
1190 uport
->ops
->set_ldisc(uport
, tty
->termios
->c_line
);
1193 static void uart_set_termios(struct tty_struct
*tty
,
1194 struct ktermios
*old_termios
)
1196 struct uart_state
*state
= tty
->driver_data
;
1197 unsigned long flags
;
1198 unsigned int cflag
= tty
->termios
->c_cflag
;
1202 * These are the bits that are used to setup various
1203 * flags in the low level driver. We can ignore the Bfoo
1204 * bits in c_cflag; c_[io]speed will always be set
1205 * appropriately by set_termios() in tty_ioctl.c
1207 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1208 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1209 tty
->termios
->c_ospeed
== old_termios
->c_ospeed
&&
1210 tty
->termios
->c_ispeed
== old_termios
->c_ispeed
&&
1211 RELEVANT_IFLAG(tty
->termios
->c_iflag
^ old_termios
->c_iflag
) == 0) {
1215 uart_change_speed(tty
, state
, old_termios
);
1217 /* Handle transition to B0 status */
1218 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1219 uart_clear_mctrl(state
->uart_port
, TIOCM_RTS
| TIOCM_DTR
);
1220 /* Handle transition away from B0 status */
1221 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1222 unsigned int mask
= TIOCM_DTR
;
1223 if (!(cflag
& CRTSCTS
) ||
1224 !test_bit(TTY_THROTTLED
, &tty
->flags
))
1226 uart_set_mctrl(state
->uart_port
, mask
);
1229 /* Handle turning off CRTSCTS */
1230 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
1231 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
1232 tty
->hw_stopped
= 0;
1234 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
1236 /* Handle turning on CRTSCTS */
1237 else if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
1238 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
1239 if (!(state
->uart_port
->ops
->get_mctrl(state
->uart_port
) & TIOCM_CTS
)) {
1240 tty
->hw_stopped
= 1;
1241 state
->uart_port
->ops
->stop_tx(state
->uart_port
);
1243 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
1248 * In 2.4.5, calls to this will be serialized via the BKL in
1249 * linux/drivers/char/tty_io.c:tty_release()
1250 * linux/drivers/char/tty_io.c:do_tty_handup()
1252 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1254 struct uart_state
*state
= tty
->driver_data
;
1255 struct tty_port
*port
;
1256 struct uart_port
*uport
;
1257 unsigned long flags
;
1259 BUG_ON(!tty_locked());
1264 uport
= state
->uart_port
;
1265 port
= &state
->port
;
1267 pr_debug("uart_close(%d) called\n", uport
->line
);
1269 mutex_lock(&port
->mutex
);
1270 spin_lock_irqsave(&port
->lock
, flags
);
1272 if (tty_hung_up_p(filp
)) {
1273 spin_unlock_irqrestore(&port
->lock
, flags
);
1277 if ((tty
->count
== 1) && (port
->count
!= 1)) {
1279 * Uh, oh. tty->count is 1, which means that the tty
1280 * structure will be freed. port->count should always
1281 * be one in these conditions. If it's greater than
1282 * one, we've got real problems, since it means the
1283 * serial port won't be shutdown.
1285 printk(KERN_ERR
"uart_close: bad serial port count; tty->count is 1, "
1286 "port->count is %d\n", port
->count
);
1289 if (--port
->count
< 0) {
1290 printk(KERN_ERR
"uart_close: bad serial port count for %s: %d\n",
1291 tty
->name
, port
->count
);
1295 spin_unlock_irqrestore(&port
->lock
, flags
);
1300 * Now we wait for the transmit buffer to clear; and we notify
1301 * the line discipline to only process XON/XOFF characters by
1302 * setting tty->closing.
1305 spin_unlock_irqrestore(&port
->lock
, flags
);
1307 if (port
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
) {
1309 * hack: open-coded tty_wait_until_sent to avoid
1310 * recursive tty_lock
1312 long timeout
= msecs_to_jiffies(port
->closing_wait
);
1313 if (wait_event_interruptible_timeout(tty
->write_wait
,
1314 !tty_chars_in_buffer(tty
), timeout
) >= 0)
1315 __uart_wait_until_sent(uport
, timeout
);
1319 * At this point, we stop accepting input. To do this, we
1320 * disable the receive line status interrupts.
1322 if (port
->flags
& ASYNC_INITIALIZED
) {
1323 unsigned long flags
;
1324 spin_lock_irqsave(&uport
->lock
, flags
);
1325 uport
->ops
->stop_rx(uport
);
1326 spin_unlock_irqrestore(&uport
->lock
, flags
);
1328 * Before we drop DTR, make sure the UART transmitter
1329 * has completely drained; this is especially
1330 * important if there is a transmit FIFO!
1332 __uart_wait_until_sent(uport
, uport
->timeout
);
1335 uart_shutdown(tty
, state
);
1336 uart_flush_buffer(tty
);
1338 tty_ldisc_flush(tty
);
1340 tty_port_tty_set(port
, NULL
);
1341 spin_lock_irqsave(&port
->lock
, flags
);
1344 if (port
->blocked_open
) {
1345 spin_unlock_irqrestore(&port
->lock
, flags
);
1346 if (port
->close_delay
)
1347 msleep_interruptible(port
->close_delay
);
1348 spin_lock_irqsave(&port
->lock
, flags
);
1349 } else if (!uart_console(uport
)) {
1350 spin_unlock_irqrestore(&port
->lock
, flags
);
1351 uart_change_pm(state
, 3);
1352 spin_lock_irqsave(&port
->lock
, flags
);
1356 * Wake up anyone trying to open this port.
1358 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1359 spin_unlock_irqrestore(&port
->lock
, flags
);
1360 wake_up_interruptible(&port
->open_wait
);
1363 mutex_unlock(&port
->mutex
);
1366 static void __uart_wait_until_sent(struct uart_port
*port
, int timeout
)
1368 unsigned long char_time
, expire
;
1370 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
1374 * Set the check interval to be 1/5 of the estimated time to
1375 * send a single character, and make it at least 1. The check
1376 * interval should also be less than the timeout.
1378 * Note: we have to use pretty tight timings here to satisfy
1381 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1382 char_time
= char_time
/ 5;
1385 if (timeout
&& timeout
< char_time
)
1386 char_time
= timeout
;
1389 * If the transmitter hasn't cleared in twice the approximate
1390 * amount of time to send the entire FIFO, it probably won't
1391 * ever clear. This assumes the UART isn't doing flow
1392 * control, which is currently the case. Hence, if it ever
1393 * takes longer than port->timeout, this is probably due to a
1394 * UART bug of some kind. So, we clamp the timeout parameter at
1397 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1398 timeout
= 2 * port
->timeout
;
1400 expire
= jiffies
+ timeout
;
1402 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1403 port
->line
, jiffies
, expire
);
1406 * Check whether the transmitter is empty every 'char_time'.
1407 * 'timeout' / 'expire' give us the maximum amount of time
1410 while (!port
->ops
->tx_empty(port
)) {
1411 msleep_interruptible(jiffies_to_msecs(char_time
));
1412 if (signal_pending(current
))
1414 if (time_after(jiffies
, expire
))
1419 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1421 struct uart_state
*state
= tty
->driver_data
;
1422 struct uart_port
*port
= state
->uart_port
;
1425 __uart_wait_until_sent(port
, timeout
);
1430 * This is called with the BKL held in
1431 * linux/drivers/char/tty_io.c:do_tty_hangup()
1432 * We're called from the eventd thread, so we can sleep for
1433 * a _short_ time only.
1435 static void uart_hangup(struct tty_struct
*tty
)
1437 struct uart_state
*state
= tty
->driver_data
;
1438 struct tty_port
*port
= &state
->port
;
1439 unsigned long flags
;
1441 BUG_ON(!tty_locked());
1442 pr_debug("uart_hangup(%d)\n", state
->uart_port
->line
);
1444 mutex_lock(&port
->mutex
);
1445 if (port
->flags
& ASYNC_NORMAL_ACTIVE
) {
1446 uart_flush_buffer(tty
);
1447 uart_shutdown(tty
, state
);
1448 spin_lock_irqsave(&port
->lock
, flags
);
1450 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1451 spin_unlock_irqrestore(&port
->lock
, flags
);
1452 tty_port_tty_set(port
, NULL
);
1453 wake_up_interruptible(&port
->open_wait
);
1454 wake_up_interruptible(&port
->delta_msr_wait
);
1456 mutex_unlock(&port
->mutex
);
1459 static int uart_carrier_raised(struct tty_port
*port
)
1461 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1462 struct uart_port
*uport
= state
->uart_port
;
1464 spin_lock_irq(&uport
->lock
);
1465 uport
->ops
->enable_ms(uport
);
1466 mctrl
= uport
->ops
->get_mctrl(uport
);
1467 spin_unlock_irq(&uport
->lock
);
1468 if (mctrl
& TIOCM_CAR
)
1473 static void uart_dtr_rts(struct tty_port
*port
, int onoff
)
1475 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1476 struct uart_port
*uport
= state
->uart_port
;
1479 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1481 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1484 static struct uart_state
*uart_get(struct uart_driver
*drv
, int line
)
1486 struct uart_state
*state
;
1487 struct tty_port
*port
;
1490 state
= drv
->state
+ line
;
1491 port
= &state
->port
;
1492 if (mutex_lock_interruptible(&port
->mutex
)) {
1498 if (!state
->uart_port
|| state
->uart_port
->flags
& UPF_DEAD
) {
1506 mutex_unlock(&port
->mutex
);
1508 return ERR_PTR(ret
);
1512 * calls to uart_open are serialised by the BKL in
1513 * fs/char_dev.c:chrdev_open()
1514 * Note that if this fails, then uart_close() _will_ be called.
1516 * In time, we want to scrap the "opening nonpresent ports"
1517 * behaviour and implement an alternative way for setserial
1518 * to set base addresses/ports/types. This will allow us to
1519 * get rid of a certain amount of extra tests.
1521 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1523 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1524 struct uart_state
*state
;
1525 struct tty_port
*port
;
1526 int retval
, line
= tty
->index
;
1528 BUG_ON(!tty_locked());
1529 pr_debug("uart_open(%d) called\n", line
);
1532 * We take the semaphore inside uart_get to guarantee that we won't
1533 * be re-entered while allocating the state structure, or while we
1534 * request any IRQs that the driver may need. This also has the nice
1535 * side-effect that it delays the action of uart_hangup, so we can
1536 * guarantee that state->port.tty will always contain something
1539 state
= uart_get(drv
, line
);
1540 if (IS_ERR(state
)) {
1541 retval
= PTR_ERR(state
);
1544 port
= &state
->port
;
1547 * Once we set tty->driver_data here, we are guaranteed that
1548 * uart_close() will decrement the driver module use count.
1549 * Any failures from here onwards should not touch the count.
1551 tty
->driver_data
= state
;
1552 state
->uart_port
->state
= state
;
1553 tty
->low_latency
= (state
->uart_port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1555 tty_port_tty_set(port
, tty
);
1558 * If the port is in the middle of closing, bail out now.
1560 if (tty_hung_up_p(filp
)) {
1563 mutex_unlock(&port
->mutex
);
1568 * Make sure the device is in D0 state.
1570 if (port
->count
== 1)
1571 uart_change_pm(state
, 0);
1574 * Start up the serial port.
1576 retval
= uart_startup(tty
, state
, 0);
1579 * If we succeeded, wait until the port is ready.
1581 mutex_unlock(&port
->mutex
);
1583 retval
= tty_port_block_til_ready(port
, tty
, filp
);
1589 static const char *uart_type(struct uart_port
*port
)
1591 const char *str
= NULL
;
1593 if (port
->ops
->type
)
1594 str
= port
->ops
->type(port
);
1602 #ifdef CONFIG_PROC_FS
1604 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1606 struct uart_state
*state
= drv
->state
+ i
;
1607 struct tty_port
*port
= &state
->port
;
1609 struct uart_port
*uport
= state
->uart_port
;
1611 unsigned int status
;
1617 mmio
= uport
->iotype
>= UPIO_MEM
;
1618 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1619 uport
->line
, uart_type(uport
),
1620 mmio
? "mmio:0x" : "port:",
1621 mmio
? (unsigned long long)uport
->mapbase
1622 : (unsigned long long)uport
->iobase
,
1625 if (uport
->type
== PORT_UNKNOWN
) {
1630 if (capable(CAP_SYS_ADMIN
)) {
1631 mutex_lock(&port
->mutex
);
1632 pm_state
= state
->pm_state
;
1634 uart_change_pm(state
, 0);
1635 spin_lock_irq(&uport
->lock
);
1636 status
= uport
->ops
->get_mctrl(uport
);
1637 spin_unlock_irq(&uport
->lock
);
1639 uart_change_pm(state
, pm_state
);
1640 mutex_unlock(&port
->mutex
);
1642 seq_printf(m
, " tx:%d rx:%d",
1643 uport
->icount
.tx
, uport
->icount
.rx
);
1644 if (uport
->icount
.frame
)
1645 seq_printf(m
, " fe:%d",
1646 uport
->icount
.frame
);
1647 if (uport
->icount
.parity
)
1648 seq_printf(m
, " pe:%d",
1649 uport
->icount
.parity
);
1650 if (uport
->icount
.brk
)
1651 seq_printf(m
, " brk:%d",
1653 if (uport
->icount
.overrun
)
1654 seq_printf(m
, " oe:%d",
1655 uport
->icount
.overrun
);
1657 #define INFOBIT(bit, str) \
1658 if (uport->mctrl & (bit)) \
1659 strncat(stat_buf, (str), sizeof(stat_buf) - \
1660 strlen(stat_buf) - 2)
1661 #define STATBIT(bit, str) \
1662 if (status & (bit)) \
1663 strncat(stat_buf, (str), sizeof(stat_buf) - \
1664 strlen(stat_buf) - 2)
1668 INFOBIT(TIOCM_RTS
, "|RTS");
1669 STATBIT(TIOCM_CTS
, "|CTS");
1670 INFOBIT(TIOCM_DTR
, "|DTR");
1671 STATBIT(TIOCM_DSR
, "|DSR");
1672 STATBIT(TIOCM_CAR
, "|CD");
1673 STATBIT(TIOCM_RNG
, "|RI");
1677 seq_puts(m
, stat_buf
);
1684 static int uart_proc_show(struct seq_file
*m
, void *v
)
1686 struct tty_driver
*ttydrv
= m
->private;
1687 struct uart_driver
*drv
= ttydrv
->driver_state
;
1690 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
1692 for (i
= 0; i
< drv
->nr
; i
++)
1693 uart_line_info(m
, drv
, i
);
1697 static int uart_proc_open(struct inode
*inode
, struct file
*file
)
1699 return single_open(file
, uart_proc_show
, PDE(inode
)->data
);
1702 static const struct file_operations uart_proc_fops
= {
1703 .owner
= THIS_MODULE
,
1704 .open
= uart_proc_open
,
1706 .llseek
= seq_lseek
,
1707 .release
= single_release
,
1711 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1713 * uart_console_write - write a console message to a serial port
1714 * @port: the port to write the message
1715 * @s: array of characters
1716 * @count: number of characters in string to write
1717 * @write: function to write character to port
1719 void uart_console_write(struct uart_port
*port
, const char *s
,
1721 void (*putchar
)(struct uart_port
*, int))
1725 for (i
= 0; i
< count
; i
++, s
++) {
1727 putchar(port
, '\r');
1731 EXPORT_SYMBOL_GPL(uart_console_write
);
1734 * Check whether an invalid uart number has been specified, and
1735 * if so, search for the first available port that does have
1738 struct uart_port
* __init
1739 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1741 int idx
= co
->index
;
1743 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1744 ports
[idx
].membase
== NULL
))
1745 for (idx
= 0; idx
< nr
; idx
++)
1746 if (ports
[idx
].iobase
!= 0 ||
1747 ports
[idx
].membase
!= NULL
)
1756 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1757 * @options: pointer to option string
1758 * @baud: pointer to an 'int' variable for the baud rate.
1759 * @parity: pointer to an 'int' variable for the parity.
1760 * @bits: pointer to an 'int' variable for the number of data bits.
1761 * @flow: pointer to an 'int' variable for the flow control character.
1763 * uart_parse_options decodes a string containing the serial console
1764 * options. The format of the string is <baud><parity><bits><flow>,
1768 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1772 *baud
= simple_strtoul(s
, NULL
, 10);
1773 while (*s
>= '0' && *s
<= '9')
1782 EXPORT_SYMBOL_GPL(uart_parse_options
);
1789 static const struct baud_rates baud_rates
[] = {
1790 { 921600, B921600
},
1791 { 460800, B460800
},
1792 { 230400, B230400
},
1793 { 115200, B115200
},
1805 * uart_set_options - setup the serial console parameters
1806 * @port: pointer to the serial ports uart_port structure
1807 * @co: console pointer
1809 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1810 * @bits: number of data bits
1811 * @flow: flow control character - 'r' (rts)
1814 uart_set_options(struct uart_port
*port
, struct console
*co
,
1815 int baud
, int parity
, int bits
, int flow
)
1817 struct ktermios termios
;
1818 static struct ktermios dummy
;
1822 * Ensure that the serial console lock is initialised
1825 spin_lock_init(&port
->lock
);
1826 lockdep_set_class(&port
->lock
, &port_lock_key
);
1828 memset(&termios
, 0, sizeof(struct ktermios
));
1830 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1833 * Construct a cflag setting.
1835 for (i
= 0; baud_rates
[i
].rate
; i
++)
1836 if (baud_rates
[i
].rate
<= baud
)
1839 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1842 termios
.c_cflag
|= CS7
;
1844 termios
.c_cflag
|= CS8
;
1848 termios
.c_cflag
|= PARODD
;
1851 termios
.c_cflag
|= PARENB
;
1856 termios
.c_cflag
|= CRTSCTS
;
1859 * some uarts on other side don't support no flow control.
1860 * So we set * DTR in host uart to make them happy
1862 port
->mctrl
|= TIOCM_DTR
;
1864 port
->ops
->set_termios(port
, &termios
, &dummy
);
1866 * Allow the setting of the UART parameters with a NULL console
1870 co
->cflag
= termios
.c_cflag
;
1874 EXPORT_SYMBOL_GPL(uart_set_options
);
1875 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1877 static void uart_change_pm(struct uart_state
*state
, int pm_state
)
1879 struct uart_port
*port
= state
->uart_port
;
1881 if (state
->pm_state
!= pm_state
) {
1883 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
1884 state
->pm_state
= pm_state
;
1889 struct uart_port
*port
;
1890 struct uart_driver
*driver
;
1893 static int serial_match_port(struct device
*dev
, void *data
)
1895 struct uart_match
*match
= data
;
1896 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
1897 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
1900 return dev
->devt
== devt
; /* Actually, only one tty per port */
1903 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*uport
)
1905 struct uart_state
*state
= drv
->state
+ uport
->line
;
1906 struct tty_port
*port
= &state
->port
;
1907 struct device
*tty_dev
;
1908 struct uart_match match
= {uport
, drv
};
1910 mutex_lock(&port
->mutex
);
1912 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
1913 if (device_may_wakeup(tty_dev
)) {
1914 if (!enable_irq_wake(uport
->irq
))
1915 uport
->irq_wake
= 1;
1916 put_device(tty_dev
);
1917 mutex_unlock(&port
->mutex
);
1920 if (console_suspend_enabled
|| !uart_console(uport
))
1921 uport
->suspended
= 1;
1923 if (port
->flags
& ASYNC_INITIALIZED
) {
1924 const struct uart_ops
*ops
= uport
->ops
;
1927 if (console_suspend_enabled
|| !uart_console(uport
)) {
1928 set_bit(ASYNCB_SUSPENDED
, &port
->flags
);
1929 clear_bit(ASYNCB_INITIALIZED
, &port
->flags
);
1931 spin_lock_irq(&uport
->lock
);
1932 ops
->stop_tx(uport
);
1933 ops
->set_mctrl(uport
, 0);
1934 ops
->stop_rx(uport
);
1935 spin_unlock_irq(&uport
->lock
);
1939 * Wait for the transmitter to empty.
1941 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
1944 printk(KERN_ERR
"%s%s%s%d: Unable to drain "
1946 uport
->dev
? dev_name(uport
->dev
) : "",
1947 uport
->dev
? ": " : "",
1949 drv
->tty_driver
->name_base
+ uport
->line
);
1951 if (console_suspend_enabled
|| !uart_console(uport
))
1952 ops
->shutdown(uport
);
1956 * Disable the console device before suspending.
1958 if (console_suspend_enabled
&& uart_console(uport
))
1959 console_stop(uport
->cons
);
1961 if (console_suspend_enabled
|| !uart_console(uport
))
1962 uart_change_pm(state
, 3);
1964 mutex_unlock(&port
->mutex
);
1969 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
1971 struct uart_state
*state
= drv
->state
+ uport
->line
;
1972 struct tty_port
*port
= &state
->port
;
1973 struct device
*tty_dev
;
1974 struct uart_match match
= {uport
, drv
};
1975 struct ktermios termios
;
1977 mutex_lock(&port
->mutex
);
1979 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
1980 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
1981 if (uport
->irq_wake
) {
1982 disable_irq_wake(uport
->irq
);
1983 uport
->irq_wake
= 0;
1985 mutex_unlock(&port
->mutex
);
1988 uport
->suspended
= 0;
1991 * Re-enable the console device after suspending.
1993 if (uart_console(uport
)) {
1995 * First try to use the console cflag setting.
1997 memset(&termios
, 0, sizeof(struct ktermios
));
1998 termios
.c_cflag
= uport
->cons
->cflag
;
2001 * If that's unset, use the tty termios setting.
2003 if (port
->tty
&& port
->tty
->termios
&& termios
.c_cflag
== 0)
2004 termios
= *(port
->tty
->termios
);
2006 if (console_suspend_enabled
)
2007 uart_change_pm(state
, 0);
2008 uport
->ops
->set_termios(uport
, &termios
, NULL
);
2009 if (console_suspend_enabled
)
2010 console_start(uport
->cons
);
2013 if (port
->flags
& ASYNC_SUSPENDED
) {
2014 const struct uart_ops
*ops
= uport
->ops
;
2017 uart_change_pm(state
, 0);
2018 spin_lock_irq(&uport
->lock
);
2019 ops
->set_mctrl(uport
, 0);
2020 spin_unlock_irq(&uport
->lock
);
2021 if (console_suspend_enabled
|| !uart_console(uport
)) {
2022 /* Protected by port mutex for now */
2023 struct tty_struct
*tty
= port
->tty
;
2024 ret
= ops
->startup(uport
);
2027 uart_change_speed(tty
, state
, NULL
);
2028 spin_lock_irq(&uport
->lock
);
2029 ops
->set_mctrl(uport
, uport
->mctrl
);
2030 ops
->start_tx(uport
);
2031 spin_unlock_irq(&uport
->lock
);
2032 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2035 * Failed to resume - maybe hardware went away?
2036 * Clear the "initialized" flag so we won't try
2037 * to call the low level drivers shutdown method.
2039 uart_shutdown(tty
, state
);
2043 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2046 mutex_unlock(&port
->mutex
);
2052 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2056 switch (port
->iotype
) {
2058 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2061 snprintf(address
, sizeof(address
),
2062 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2070 snprintf(address
, sizeof(address
),
2071 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2074 strlcpy(address
, "*unknown*", sizeof(address
));
2078 printk(KERN_INFO
"%s%s%s%d at %s (irq = %d) is a %s\n",
2079 port
->dev
? dev_name(port
->dev
) : "",
2080 port
->dev
? ": " : "",
2082 drv
->tty_driver
->name_base
+ port
->line
,
2083 address
, port
->irq
, uart_type(port
));
2087 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2088 struct uart_port
*port
)
2093 * If there isn't a port here, don't do anything further.
2095 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2099 * Now do the auto configuration stuff. Note that config_port
2100 * is expected to claim the resources and map the port for us.
2103 if (port
->flags
& UPF_AUTO_IRQ
)
2104 flags
|= UART_CONFIG_IRQ
;
2105 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2106 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2107 port
->type
= PORT_UNKNOWN
;
2108 flags
|= UART_CONFIG_TYPE
;
2110 port
->ops
->config_port(port
, flags
);
2113 if (port
->type
!= PORT_UNKNOWN
) {
2114 unsigned long flags
;
2116 uart_report_port(drv
, port
);
2118 /* Power up port for set_mctrl() */
2119 uart_change_pm(state
, 0);
2122 * Ensure that the modem control lines are de-activated.
2123 * keep the DTR setting that is set in uart_set_options()
2124 * We probably don't need a spinlock around this, but
2126 spin_lock_irqsave(&port
->lock
, flags
);
2127 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2128 spin_unlock_irqrestore(&port
->lock
, flags
);
2131 * If this driver supports console, and it hasn't been
2132 * successfully registered yet, try to re-register it.
2133 * It may be that the port was not available.
2135 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2136 register_console(port
->cons
);
2139 * Power down all ports by default, except the
2140 * console if we have one.
2142 if (!uart_console(port
))
2143 uart_change_pm(state
, 3);
2147 #ifdef CONFIG_CONSOLE_POLL
2149 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2151 struct uart_driver
*drv
= driver
->driver_state
;
2152 struct uart_state
*state
= drv
->state
+ line
;
2153 struct uart_port
*port
;
2159 if (!state
|| !state
->uart_port
)
2162 port
= state
->uart_port
;
2163 if (!(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
))
2167 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2168 return uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2174 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2176 struct uart_driver
*drv
= driver
->driver_state
;
2177 struct uart_state
*state
= drv
->state
+ line
;
2178 struct uart_port
*port
;
2180 if (!state
|| !state
->uart_port
)
2183 port
= state
->uart_port
;
2184 return port
->ops
->poll_get_char(port
);
2187 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2189 struct uart_driver
*drv
= driver
->driver_state
;
2190 struct uart_state
*state
= drv
->state
+ line
;
2191 struct uart_port
*port
;
2193 if (!state
|| !state
->uart_port
)
2196 port
= state
->uart_port
;
2197 port
->ops
->poll_put_char(port
, ch
);
2201 static const struct tty_operations uart_ops
= {
2203 .close
= uart_close
,
2204 .write
= uart_write
,
2205 .put_char
= uart_put_char
,
2206 .flush_chars
= uart_flush_chars
,
2207 .write_room
= uart_write_room
,
2208 .chars_in_buffer
= uart_chars_in_buffer
,
2209 .flush_buffer
= uart_flush_buffer
,
2210 .ioctl
= uart_ioctl
,
2211 .throttle
= uart_throttle
,
2212 .unthrottle
= uart_unthrottle
,
2213 .send_xchar
= uart_send_xchar
,
2214 .set_termios
= uart_set_termios
,
2215 .set_ldisc
= uart_set_ldisc
,
2217 .start
= uart_start
,
2218 .hangup
= uart_hangup
,
2219 .break_ctl
= uart_break_ctl
,
2220 .wait_until_sent
= uart_wait_until_sent
,
2221 #ifdef CONFIG_PROC_FS
2222 .proc_fops
= &uart_proc_fops
,
2224 .tiocmget
= uart_tiocmget
,
2225 .tiocmset
= uart_tiocmset
,
2226 .get_icount
= uart_get_icount
,
2227 #ifdef CONFIG_CONSOLE_POLL
2228 .poll_init
= uart_poll_init
,
2229 .poll_get_char
= uart_poll_get_char
,
2230 .poll_put_char
= uart_poll_put_char
,
2234 static const struct tty_port_operations uart_port_ops
= {
2235 .carrier_raised
= uart_carrier_raised
,
2236 .dtr_rts
= uart_dtr_rts
,
2240 * uart_register_driver - register a driver with the uart core layer
2241 * @drv: low level driver structure
2243 * Register a uart driver with the core driver. We in turn register
2244 * with the tty layer, and initialise the core driver per-port state.
2246 * We have a proc file in /proc/tty/driver which is named after the
2249 * drv->port should be NULL, and the per-port structures should be
2250 * registered using uart_add_one_port after this call has succeeded.
2252 int uart_register_driver(struct uart_driver
*drv
)
2254 struct tty_driver
*normal
;
2260 * Maybe we should be using a slab cache for this, especially if
2261 * we have a large number of ports to handle.
2263 drv
->state
= kzalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2267 normal
= alloc_tty_driver(drv
->nr
);
2271 drv
->tty_driver
= normal
;
2273 normal
->owner
= drv
->owner
;
2274 normal
->driver_name
= drv
->driver_name
;
2275 normal
->name
= drv
->dev_name
;
2276 normal
->major
= drv
->major
;
2277 normal
->minor_start
= drv
->minor
;
2278 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2279 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2280 normal
->init_termios
= tty_std_termios
;
2281 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2282 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2283 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2284 normal
->driver_state
= drv
;
2285 tty_set_operations(normal
, &uart_ops
);
2288 * Initialise the UART state(s).
2290 for (i
= 0; i
< drv
->nr
; i
++) {
2291 struct uart_state
*state
= drv
->state
+ i
;
2292 struct tty_port
*port
= &state
->port
;
2294 tty_port_init(port
);
2295 port
->ops
= &uart_port_ops
;
2296 port
->close_delay
= 500; /* .5 seconds */
2297 port
->closing_wait
= 30000; /* 30 seconds */
2298 tasklet_init(&state
->tlet
, uart_tasklet_action
,
2299 (unsigned long)state
);
2302 retval
= tty_register_driver(normal
);
2306 put_tty_driver(normal
);
2314 * uart_unregister_driver - remove a driver from the uart core layer
2315 * @drv: low level driver structure
2317 * Remove all references to a driver from the core driver. The low
2318 * level driver must have removed all its ports via the
2319 * uart_remove_one_port() if it registered them with uart_add_one_port().
2320 * (ie, drv->port == NULL)
2322 void uart_unregister_driver(struct uart_driver
*drv
)
2324 struct tty_driver
*p
= drv
->tty_driver
;
2325 tty_unregister_driver(p
);
2328 drv
->tty_driver
= NULL
;
2331 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2333 struct uart_driver
*p
= co
->data
;
2335 return p
->tty_driver
;
2339 * uart_add_one_port - attach a driver-defined port structure
2340 * @drv: pointer to the uart low level driver structure for this port
2341 * @uport: uart port structure to use for this port.
2343 * This allows the driver to register its own uart_port structure
2344 * with the core driver. The main purpose is to allow the low
2345 * level uart drivers to expand uart_port, rather than having yet
2346 * more levels of structures.
2348 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2350 struct uart_state
*state
;
2351 struct tty_port
*port
;
2353 struct device
*tty_dev
;
2355 BUG_ON(in_interrupt());
2357 if (uport
->line
>= drv
->nr
)
2360 state
= drv
->state
+ uport
->line
;
2361 port
= &state
->port
;
2363 mutex_lock(&port_mutex
);
2364 mutex_lock(&port
->mutex
);
2365 if (state
->uart_port
) {
2370 state
->uart_port
= uport
;
2371 state
->pm_state
= -1;
2373 uport
->cons
= drv
->cons
;
2374 uport
->state
= state
;
2377 * If this port is a console, then the spinlock is already
2380 if (!(uart_console(uport
) && (uport
->cons
->flags
& CON_ENABLED
))) {
2381 spin_lock_init(&uport
->lock
);
2382 lockdep_set_class(&uport
->lock
, &port_lock_key
);
2385 uart_configure_port(drv
, state
, uport
);
2388 * Register the port whether it's detected or not. This allows
2389 * setserial to be used to alter this ports parameters.
2391 tty_dev
= tty_register_device(drv
->tty_driver
, uport
->line
, uport
->dev
);
2392 if (likely(!IS_ERR(tty_dev
))) {
2393 device_init_wakeup(tty_dev
, 1);
2394 device_set_wakeup_enable(tty_dev
, 0);
2396 printk(KERN_ERR
"Cannot register tty device on line %d\n",
2400 * Ensure UPF_DEAD is not set.
2402 uport
->flags
&= ~UPF_DEAD
;
2405 mutex_unlock(&port
->mutex
);
2406 mutex_unlock(&port_mutex
);
2412 * uart_remove_one_port - detach a driver defined port structure
2413 * @drv: pointer to the uart low level driver structure for this port
2414 * @uport: uart port structure for this port
2416 * This unhooks (and hangs up) the specified port structure from the
2417 * core driver. No further calls will be made to the low-level code
2420 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2422 struct uart_state
*state
= drv
->state
+ uport
->line
;
2423 struct tty_port
*port
= &state
->port
;
2425 BUG_ON(in_interrupt());
2427 if (state
->uart_port
!= uport
)
2428 printk(KERN_ALERT
"Removing wrong port: %p != %p\n",
2429 state
->uart_port
, uport
);
2431 mutex_lock(&port_mutex
);
2434 * Mark the port "dead" - this prevents any opens from
2435 * succeeding while we shut down the port.
2437 mutex_lock(&port
->mutex
);
2438 uport
->flags
|= UPF_DEAD
;
2439 mutex_unlock(&port
->mutex
);
2442 * Remove the devices from the tty layer
2444 tty_unregister_device(drv
->tty_driver
, uport
->line
);
2447 tty_vhangup(port
->tty
);
2450 * Free the port IO and memory resources, if any.
2452 if (uport
->type
!= PORT_UNKNOWN
)
2453 uport
->ops
->release_port(uport
);
2456 * Indicate that there isn't a port here anymore.
2458 uport
->type
= PORT_UNKNOWN
;
2461 * Kill the tasklet, and free resources.
2463 tasklet_kill(&state
->tlet
);
2465 state
->uart_port
= NULL
;
2466 mutex_unlock(&port_mutex
);
2472 * Are the two ports equivalent?
2474 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2476 if (port1
->iotype
!= port2
->iotype
)
2479 switch (port1
->iotype
) {
2481 return (port1
->iobase
== port2
->iobase
);
2483 return (port1
->iobase
== port2
->iobase
) &&
2484 (port1
->hub6
== port2
->hub6
);
2491 return (port1
->mapbase
== port2
->mapbase
);
2495 EXPORT_SYMBOL(uart_match_port
);
2497 EXPORT_SYMBOL(uart_write_wakeup
);
2498 EXPORT_SYMBOL(uart_register_driver
);
2499 EXPORT_SYMBOL(uart_unregister_driver
);
2500 EXPORT_SYMBOL(uart_suspend_port
);
2501 EXPORT_SYMBOL(uart_resume_port
);
2502 EXPORT_SYMBOL(uart_add_one_port
);
2503 EXPORT_SYMBOL(uart_remove_one_port
);
2505 MODULE_DESCRIPTION("Serial driver core");
2506 MODULE_LICENSE("GPL");