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 tty_struct
*tty
, 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 tty_wakeup(state
->port
.tty
);
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
);
111 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
116 spin_lock_irqsave(&port
->lock
, flags
);
118 port
->mctrl
= (old
& ~clear
) | set
;
119 if (old
!= port
->mctrl
)
120 port
->ops
->set_mctrl(port
, port
->mctrl
);
121 spin_unlock_irqrestore(&port
->lock
, flags
);
124 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
125 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
128 * Startup the port. This will be called once per open. All calls
129 * will be serialised by the per-port mutex.
131 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
, int init_hw
)
133 struct uart_port
*uport
= state
->uart_port
;
134 struct tty_port
*port
= &state
->port
;
138 if (port
->flags
& ASYNC_INITIALIZED
)
142 * Set the TTY IO error marker - we will only clear this
143 * once we have successfully opened the port. Also set
144 * up the tty->alt_speed kludge
146 set_bit(TTY_IO_ERROR
, &tty
->flags
);
148 if (uport
->type
== PORT_UNKNOWN
)
152 * Initialise and allocate the transmit and temporary
155 if (!state
->xmit
.buf
) {
156 /* This is protected by the per port mutex */
157 page
= get_zeroed_page(GFP_KERNEL
);
161 state
->xmit
.buf
= (unsigned char *) page
;
162 uart_circ_clear(&state
->xmit
);
165 retval
= uport
->ops
->startup(uport
);
167 if (uart_console(uport
) && uport
->cons
->cflag
) {
168 tty
->termios
->c_cflag
= uport
->cons
->cflag
;
169 uport
->cons
->cflag
= 0;
172 * Initialise the hardware port settings.
174 uart_change_speed(tty
, state
, NULL
);
178 * Setup the RTS and DTR signals once the
179 * port is open and ready to respond.
181 if (tty
->termios
->c_cflag
& CBAUD
)
182 uart_set_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
185 if (port
->flags
& ASYNC_CTS_FLOW
) {
186 spin_lock_irq(&uport
->lock
);
187 if (!(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
))
189 spin_unlock_irq(&uport
->lock
);
192 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
194 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
198 * This is to allow setserial on this port. People may want to set
199 * port/irq/type and then reconfigure the port properly if it failed
202 if (retval
&& capable(CAP_SYS_ADMIN
))
209 * This routine will shutdown a serial port; interrupts are disabled, and
210 * DTR is dropped if the hangup on close termio flag is on. Calls to
211 * uart_shutdown are serialised by the per-port semaphore.
213 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
215 struct uart_port
*uport
= state
->uart_port
;
216 struct tty_port
*port
= &state
->port
;
219 * Set the TTY IO error marker
222 set_bit(TTY_IO_ERROR
, &tty
->flags
);
224 if (test_and_clear_bit(ASYNCB_INITIALIZED
, &port
->flags
)) {
226 * Turn off DTR and RTS early.
228 if (!tty
|| (tty
->termios
->c_cflag
& HUPCL
))
229 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
232 * clear delta_msr_wait queue to avoid mem leaks: we may free
233 * the irq here so the queue might never be woken up. Note
234 * that we won't end up waiting on delta_msr_wait again since
235 * any outstanding file descriptors should be pointing at
236 * hung_up_tty_fops now.
238 wake_up_interruptible(&port
->delta_msr_wait
);
241 * Free the IRQ and disable the port.
243 uport
->ops
->shutdown(uport
);
246 * Ensure that the IRQ handler isn't running on another CPU.
248 synchronize_irq(uport
->irq
);
252 * Free the transmit buffer page.
254 if (state
->xmit
.buf
) {
255 free_page((unsigned long)state
->xmit
.buf
);
256 state
->xmit
.buf
= NULL
;
261 * uart_update_timeout - update per-port FIFO timeout.
262 * @port: uart_port structure describing the port
263 * @cflag: termios cflag value
264 * @baud: speed of the port
266 * Set the port FIFO timeout value. The @cflag value should
267 * reflect the actual hardware settings.
270 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
275 /* byte size and parity */
276 switch (cflag
& CSIZE
) {
297 * The total number of bits to be transmitted in the fifo.
299 bits
= bits
* port
->fifosize
;
302 * Figure the timeout to send the above number of bits.
303 * Add .02 seconds of slop
305 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
308 EXPORT_SYMBOL(uart_update_timeout
);
311 * uart_get_baud_rate - return baud rate for a particular port
312 * @port: uart_port structure describing the port in question.
313 * @termios: desired termios settings.
314 * @old: old termios (or NULL)
315 * @min: minimum acceptable baud rate
316 * @max: maximum acceptable baud rate
318 * Decode the termios structure into a numeric baud rate,
319 * taking account of the magic 38400 baud rate (with spd_*
320 * flags), and mapping the %B0 rate to 9600 baud.
322 * If the new baud rate is invalid, try the old termios setting.
323 * If it's still invalid, we try 9600 baud.
325 * Update the @termios structure to reflect the baud rate
326 * we're actually going to be using. Don't do this for the case
327 * where B0 is requested ("hang up").
330 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
331 struct ktermios
*old
, unsigned int min
, unsigned int max
)
333 unsigned int try, baud
, altbaud
= 38400;
335 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
337 if (flags
== UPF_SPD_HI
)
339 else if (flags
== UPF_SPD_VHI
)
341 else if (flags
== UPF_SPD_SHI
)
343 else if (flags
== UPF_SPD_WARP
)
346 for (try = 0; try < 2; try++) {
347 baud
= tty_termios_baud_rate(termios
);
350 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Special case: B0 rate.
364 if (baud
>= min
&& baud
<= max
)
368 * Oops, the quotient was zero. Try again with
369 * the old baud rate if possible.
371 termios
->c_cflag
&= ~CBAUD
;
373 baud
= tty_termios_baud_rate(old
);
375 tty_termios_encode_baud_rate(termios
,
382 * As a last resort, if the range cannot be met then clip to
383 * the nearest chip supported rate.
387 tty_termios_encode_baud_rate(termios
,
390 tty_termios_encode_baud_rate(termios
,
394 /* Should never happen */
399 EXPORT_SYMBOL(uart_get_baud_rate
);
402 * uart_get_divisor - return uart clock divisor
403 * @port: uart_port structure describing the port.
404 * @baud: desired baud rate
406 * Calculate the uart clock divisor for the port.
409 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
414 * Old custom speed handling.
416 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
417 quot
= port
->custom_divisor
;
419 quot
= (port
->uartclk
+ (8 * baud
)) / (16 * baud
);
424 EXPORT_SYMBOL(uart_get_divisor
);
426 /* FIXME: Consistent locking policy */
427 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
428 struct ktermios
*old_termios
)
430 struct tty_port
*port
= &state
->port
;
431 struct uart_port
*uport
= state
->uart_port
;
432 struct ktermios
*termios
;
435 * If we have no tty, termios, or the port does not exist,
436 * then we can't set the parameters for this port.
438 if (!tty
|| !tty
->termios
|| uport
->type
== PORT_UNKNOWN
)
441 termios
= tty
->termios
;
444 * Set flags based on termios cflag
446 if (termios
->c_cflag
& CRTSCTS
)
447 set_bit(ASYNCB_CTS_FLOW
, &port
->flags
);
449 clear_bit(ASYNCB_CTS_FLOW
, &port
->flags
);
451 if (termios
->c_cflag
& CLOCAL
)
452 clear_bit(ASYNCB_CHECK_CD
, &port
->flags
);
454 set_bit(ASYNCB_CHECK_CD
, &port
->flags
);
456 uport
->ops
->set_termios(uport
, termios
, old_termios
);
459 static inline int __uart_put_char(struct uart_port
*port
,
460 struct circ_buf
*circ
, unsigned char c
)
468 spin_lock_irqsave(&port
->lock
, flags
);
469 if (uart_circ_chars_free(circ
) != 0) {
470 circ
->buf
[circ
->head
] = c
;
471 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
474 spin_unlock_irqrestore(&port
->lock
, flags
);
478 static int uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
480 struct uart_state
*state
= tty
->driver_data
;
482 return __uart_put_char(state
->uart_port
, &state
->xmit
, ch
);
485 static void uart_flush_chars(struct tty_struct
*tty
)
490 static int uart_write(struct tty_struct
*tty
,
491 const unsigned char *buf
, int count
)
493 struct uart_state
*state
= tty
->driver_data
;
494 struct uart_port
*port
;
495 struct circ_buf
*circ
;
500 * This means you called this function _after_ the port was
501 * closed. No cookie for you.
508 port
= state
->uart_port
;
514 spin_lock_irqsave(&port
->lock
, flags
);
516 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
521 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
522 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
527 spin_unlock_irqrestore(&port
->lock
, flags
);
533 static int uart_write_room(struct tty_struct
*tty
)
535 struct uart_state
*state
= tty
->driver_data
;
539 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
540 ret
= uart_circ_chars_free(&state
->xmit
);
541 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
545 static int uart_chars_in_buffer(struct tty_struct
*tty
)
547 struct uart_state
*state
= tty
->driver_data
;
551 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
552 ret
= uart_circ_chars_pending(&state
->xmit
);
553 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
557 static void uart_flush_buffer(struct tty_struct
*tty
)
559 struct uart_state
*state
= tty
->driver_data
;
560 struct uart_port
*port
;
564 * This means you called this function _after_ the port was
565 * closed. No cookie for you.
572 port
= state
->uart_port
;
573 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
575 spin_lock_irqsave(&port
->lock
, flags
);
576 uart_circ_clear(&state
->xmit
);
577 if (port
->ops
->flush_buffer
)
578 port
->ops
->flush_buffer(port
);
579 spin_unlock_irqrestore(&port
->lock
, flags
);
584 * This function is used to send a high-priority XON/XOFF character to
587 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
589 struct uart_state
*state
= tty
->driver_data
;
590 struct uart_port
*port
= state
->uart_port
;
593 if (port
->ops
->send_xchar
)
594 port
->ops
->send_xchar(port
, ch
);
598 spin_lock_irqsave(&port
->lock
, flags
);
599 port
->ops
->start_tx(port
);
600 spin_unlock_irqrestore(&port
->lock
, flags
);
605 static void uart_throttle(struct tty_struct
*tty
)
607 struct uart_state
*state
= tty
->driver_data
;
610 uart_send_xchar(tty
, STOP_CHAR(tty
));
612 if (tty
->termios
->c_cflag
& CRTSCTS
)
613 uart_clear_mctrl(state
->uart_port
, TIOCM_RTS
);
616 static void uart_unthrottle(struct tty_struct
*tty
)
618 struct uart_state
*state
= tty
->driver_data
;
619 struct uart_port
*port
= state
->uart_port
;
625 uart_send_xchar(tty
, START_CHAR(tty
));
628 if (tty
->termios
->c_cflag
& CRTSCTS
)
629 uart_set_mctrl(port
, TIOCM_RTS
);
632 static int uart_get_info(struct uart_state
*state
,
633 struct serial_struct __user
*retinfo
)
635 struct uart_port
*uport
= state
->uart_port
;
636 struct tty_port
*port
= &state
->port
;
637 struct serial_struct tmp
;
639 memset(&tmp
, 0, sizeof(tmp
));
641 /* Ensure the state we copy is consistent and no hardware changes
643 mutex_lock(&port
->mutex
);
645 tmp
.type
= uport
->type
;
646 tmp
.line
= uport
->line
;
647 tmp
.port
= uport
->iobase
;
648 if (HIGH_BITS_OFFSET
)
649 tmp
.port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
650 tmp
.irq
= uport
->irq
;
651 tmp
.flags
= uport
->flags
;
652 tmp
.xmit_fifo_size
= uport
->fifosize
;
653 tmp
.baud_base
= uport
->uartclk
/ 16;
654 tmp
.close_delay
= port
->close_delay
/ 10;
655 tmp
.closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
656 ASYNC_CLOSING_WAIT_NONE
:
657 port
->closing_wait
/ 10;
658 tmp
.custom_divisor
= uport
->custom_divisor
;
659 tmp
.hub6
= uport
->hub6
;
660 tmp
.io_type
= uport
->iotype
;
661 tmp
.iomem_reg_shift
= uport
->regshift
;
662 tmp
.iomem_base
= (void *)(unsigned long)uport
->mapbase
;
664 mutex_unlock(&port
->mutex
);
666 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
671 static int uart_set_info(struct tty_struct
*tty
, struct uart_state
*state
,
672 struct serial_struct __user
*newinfo
)
674 struct serial_struct new_serial
;
675 struct uart_port
*uport
= state
->uart_port
;
676 struct tty_port
*port
= &state
->port
;
677 unsigned long new_port
;
678 unsigned int change_irq
, change_port
, closing_wait
;
679 unsigned int old_custom_divisor
, close_delay
;
680 upf_t old_flags
, new_flags
;
683 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
686 new_port
= new_serial
.port
;
687 if (HIGH_BITS_OFFSET
)
688 new_port
+= (unsigned long) new_serial
.port_high
<< HIGH_BITS_OFFSET
;
690 new_serial
.irq
= irq_canonicalize(new_serial
.irq
);
691 close_delay
= new_serial
.close_delay
* 10;
692 closing_wait
= new_serial
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
693 ASYNC_CLOSING_WAIT_NONE
: new_serial
.closing_wait
* 10;
696 * This semaphore protects port->count. It is also
697 * very useful to prevent opens. Also, take the
698 * port configuration semaphore to make sure that a
699 * module insertion/removal doesn't change anything
702 mutex_lock(&port
->mutex
);
704 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
705 && new_serial
.irq
!= uport
->irq
;
708 * Since changing the 'type' of the port changes its resource
709 * allocations, we should treat type changes the same as
712 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
713 && (new_port
!= uport
->iobase
||
714 (unsigned long)new_serial
.iomem_base
!= uport
->mapbase
||
715 new_serial
.hub6
!= uport
->hub6
||
716 new_serial
.io_type
!= uport
->iotype
||
717 new_serial
.iomem_reg_shift
!= uport
->regshift
||
718 new_serial
.type
!= uport
->type
);
720 old_flags
= uport
->flags
;
721 new_flags
= new_serial
.flags
;
722 old_custom_divisor
= uport
->custom_divisor
;
724 if (!capable(CAP_SYS_ADMIN
)) {
726 if (change_irq
|| change_port
||
727 (new_serial
.baud_base
!= uport
->uartclk
/ 16) ||
728 (close_delay
!= port
->close_delay
) ||
729 (closing_wait
!= port
->closing_wait
) ||
730 (new_serial
.xmit_fifo_size
&&
731 new_serial
.xmit_fifo_size
!= uport
->fifosize
) ||
732 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
734 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
735 (new_flags
& UPF_USR_MASK
));
736 uport
->custom_divisor
= new_serial
.custom_divisor
;
741 * Ask the low level driver to verify the settings.
743 if (uport
->ops
->verify_port
)
744 retval
= uport
->ops
->verify_port(uport
, &new_serial
);
746 if ((new_serial
.irq
>= nr_irqs
) || (new_serial
.irq
< 0) ||
747 (new_serial
.baud_base
< 9600))
753 if (change_port
|| change_irq
) {
757 * Make sure that we are the sole user of this port.
759 if (tty_port_users(port
) > 1)
763 * We need to shutdown the serial port at the old
764 * port/type/irq combination.
766 uart_shutdown(tty
, state
);
770 unsigned long old_iobase
, old_mapbase
;
771 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
773 old_iobase
= uport
->iobase
;
774 old_mapbase
= uport
->mapbase
;
775 old_type
= uport
->type
;
776 old_hub6
= uport
->hub6
;
777 old_iotype
= uport
->iotype
;
778 old_shift
= uport
->regshift
;
781 * Free and release old regions
783 if (old_type
!= PORT_UNKNOWN
)
784 uport
->ops
->release_port(uport
);
786 uport
->iobase
= new_port
;
787 uport
->type
= new_serial
.type
;
788 uport
->hub6
= new_serial
.hub6
;
789 uport
->iotype
= new_serial
.io_type
;
790 uport
->regshift
= new_serial
.iomem_reg_shift
;
791 uport
->mapbase
= (unsigned long)new_serial
.iomem_base
;
794 * Claim and map the new regions
796 if (uport
->type
!= PORT_UNKNOWN
) {
797 retval
= uport
->ops
->request_port(uport
);
799 /* Always success - Jean II */
804 * If we fail to request resources for the
805 * new port, try to restore the old settings.
807 if (retval
&& old_type
!= PORT_UNKNOWN
) {
808 uport
->iobase
= old_iobase
;
809 uport
->type
= old_type
;
810 uport
->hub6
= old_hub6
;
811 uport
->iotype
= old_iotype
;
812 uport
->regshift
= old_shift
;
813 uport
->mapbase
= old_mapbase
;
814 retval
= uport
->ops
->request_port(uport
);
816 * If we failed to restore the old settings,
820 uport
->type
= PORT_UNKNOWN
;
826 /* Added to return the correct error -Ram Gupta */
832 uport
->irq
= new_serial
.irq
;
833 if (!(uport
->flags
& UPF_FIXED_PORT
))
834 uport
->uartclk
= new_serial
.baud_base
* 16;
835 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
836 (new_flags
& UPF_CHANGE_MASK
);
837 uport
->custom_divisor
= new_serial
.custom_divisor
;
838 port
->close_delay
= close_delay
;
839 port
->closing_wait
= closing_wait
;
840 if (new_serial
.xmit_fifo_size
)
841 uport
->fifosize
= new_serial
.xmit_fifo_size
;
843 port
->tty
->low_latency
=
844 (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
848 if (uport
->type
== PORT_UNKNOWN
)
850 if (port
->flags
& ASYNC_INITIALIZED
) {
851 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
852 old_custom_divisor
!= uport
->custom_divisor
) {
854 * If they're setting up a custom divisor or speed,
855 * instead of clearing it, then bitch about it. No
856 * need to rate-limit; it's CAP_SYS_ADMIN only.
858 if (uport
->flags
& UPF_SPD_MASK
) {
861 "%s sets custom speed on %s. This "
862 "is deprecated.\n", current
->comm
,
863 tty_name(port
->tty
, buf
));
865 uart_change_speed(tty
, state
, NULL
);
868 retval
= uart_startup(tty
, state
, 1);
870 mutex_unlock(&port
->mutex
);
875 * uart_get_lsr_info - get line status register info
876 * @tty: tty associated with the UART
877 * @state: UART being queried
878 * @value: returned modem value
880 * Note: uart_ioctl protects us against hangups.
882 static int uart_get_lsr_info(struct tty_struct
*tty
,
883 struct uart_state
*state
, unsigned int __user
*value
)
885 struct uart_port
*uport
= state
->uart_port
;
888 result
= uport
->ops
->tx_empty(uport
);
891 * If we're about to load something into the transmit
892 * register, we'll pretend the transmitter isn't empty to
893 * avoid a race condition (depending on when the transmit
894 * interrupt happens).
897 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
898 !tty
->stopped
&& !tty
->hw_stopped
))
899 result
&= ~TIOCSER_TEMT
;
901 return put_user(result
, value
);
904 static int uart_tiocmget(struct tty_struct
*tty
)
906 struct uart_state
*state
= tty
->driver_data
;
907 struct tty_port
*port
= &state
->port
;
908 struct uart_port
*uport
= state
->uart_port
;
911 mutex_lock(&port
->mutex
);
912 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
913 result
= uport
->mctrl
;
914 spin_lock_irq(&uport
->lock
);
915 result
|= uport
->ops
->get_mctrl(uport
);
916 spin_unlock_irq(&uport
->lock
);
918 mutex_unlock(&port
->mutex
);
924 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
926 struct uart_state
*state
= tty
->driver_data
;
927 struct uart_port
*uport
= state
->uart_port
;
928 struct tty_port
*port
= &state
->port
;
931 mutex_lock(&port
->mutex
);
932 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
933 uart_update_mctrl(uport
, set
, clear
);
936 mutex_unlock(&port
->mutex
);
940 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
942 struct uart_state
*state
= tty
->driver_data
;
943 struct tty_port
*port
= &state
->port
;
944 struct uart_port
*uport
= state
->uart_port
;
946 mutex_lock(&port
->mutex
);
948 if (uport
->type
!= PORT_UNKNOWN
)
949 uport
->ops
->break_ctl(uport
, break_state
);
951 mutex_unlock(&port
->mutex
);
955 static int uart_do_autoconfig(struct tty_struct
*tty
,struct uart_state
*state
)
957 struct uart_port
*uport
= state
->uart_port
;
958 struct tty_port
*port
= &state
->port
;
961 if (!capable(CAP_SYS_ADMIN
))
965 * Take the per-port semaphore. This prevents count from
966 * changing, and hence any extra opens of the port while
967 * we're auto-configuring.
969 if (mutex_lock_interruptible(&port
->mutex
))
973 if (tty_port_users(port
) == 1) {
974 uart_shutdown(tty
, state
);
977 * If we already have a port type configured,
978 * we must release its resources.
980 if (uport
->type
!= PORT_UNKNOWN
)
981 uport
->ops
->release_port(uport
);
983 flags
= UART_CONFIG_TYPE
;
984 if (uport
->flags
& UPF_AUTO_IRQ
)
985 flags
|= UART_CONFIG_IRQ
;
988 * This will claim the ports resources if
991 uport
->ops
->config_port(uport
, flags
);
993 ret
= uart_startup(tty
, state
, 1);
995 mutex_unlock(&port
->mutex
);
1000 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1001 * - mask passed in arg for lines of interest
1002 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1003 * Caller should use TIOCGICOUNT to see which one it was
1005 * FIXME: This wants extracting into a common all driver implementation
1006 * of TIOCMWAIT using tty_port.
1009 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
1011 struct uart_port
*uport
= state
->uart_port
;
1012 struct tty_port
*port
= &state
->port
;
1013 DECLARE_WAITQUEUE(wait
, current
);
1014 struct uart_icount cprev
, cnow
;
1018 * note the counters on entry
1020 spin_lock_irq(&uport
->lock
);
1021 memcpy(&cprev
, &uport
->icount
, sizeof(struct uart_icount
));
1024 * Force modem status interrupts on
1026 uport
->ops
->enable_ms(uport
);
1027 spin_unlock_irq(&uport
->lock
);
1029 add_wait_queue(&port
->delta_msr_wait
, &wait
);
1031 spin_lock_irq(&uport
->lock
);
1032 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1033 spin_unlock_irq(&uport
->lock
);
1035 set_current_state(TASK_INTERRUPTIBLE
);
1037 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1038 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1039 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1040 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1047 /* see if a signal did it */
1048 if (signal_pending(current
)) {
1056 current
->state
= TASK_RUNNING
;
1057 remove_wait_queue(&port
->delta_msr_wait
, &wait
);
1063 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1064 * Return: write counters to the user passed counter struct
1065 * NB: both 1->0 and 0->1 transitions are counted except for
1066 * RI where only 0->1 is counted.
1068 static int uart_get_icount(struct tty_struct
*tty
,
1069 struct serial_icounter_struct
*icount
)
1071 struct uart_state
*state
= tty
->driver_data
;
1072 struct uart_icount cnow
;
1073 struct uart_port
*uport
= state
->uart_port
;
1075 spin_lock_irq(&uport
->lock
);
1076 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1077 spin_unlock_irq(&uport
->lock
);
1079 icount
->cts
= cnow
.cts
;
1080 icount
->dsr
= cnow
.dsr
;
1081 icount
->rng
= cnow
.rng
;
1082 icount
->dcd
= cnow
.dcd
;
1083 icount
->rx
= cnow
.rx
;
1084 icount
->tx
= cnow
.tx
;
1085 icount
->frame
= cnow
.frame
;
1086 icount
->overrun
= cnow
.overrun
;
1087 icount
->parity
= cnow
.parity
;
1088 icount
->brk
= cnow
.brk
;
1089 icount
->buf_overrun
= cnow
.buf_overrun
;
1095 * Called via sys_ioctl. We can use spin_lock_irq() here.
1098 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1101 struct uart_state
*state
= tty
->driver_data
;
1102 struct tty_port
*port
= &state
->port
;
1103 void __user
*uarg
= (void __user
*)arg
;
1104 int ret
= -ENOIOCTLCMD
;
1108 * These ioctls don't rely on the hardware to be present.
1112 ret
= uart_get_info(state
, uarg
);
1116 ret
= uart_set_info(tty
, state
, uarg
);
1120 ret
= uart_do_autoconfig(tty
, state
);
1123 case TIOCSERGWILD
: /* obsolete */
1124 case TIOCSERSWILD
: /* obsolete */
1129 if (ret
!= -ENOIOCTLCMD
)
1132 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1138 * The following should only be used when hardware is present.
1142 ret
= uart_wait_modem_status(state
, arg
);
1146 if (ret
!= -ENOIOCTLCMD
)
1149 mutex_lock(&port
->mutex
);
1151 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1157 * All these rely on hardware being present and need to be
1158 * protected against the tty being hung up.
1161 case TIOCSERGETLSR
: /* Get line status register */
1162 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1166 struct uart_port
*uport
= state
->uart_port
;
1167 if (uport
->ops
->ioctl
)
1168 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1173 mutex_unlock(&port
->mutex
);
1178 static void uart_set_ldisc(struct tty_struct
*tty
)
1180 struct uart_state
*state
= tty
->driver_data
;
1181 struct uart_port
*uport
= state
->uart_port
;
1183 if (uport
->ops
->set_ldisc
)
1184 uport
->ops
->set_ldisc(uport
, tty
->termios
->c_line
);
1187 static void uart_set_termios(struct tty_struct
*tty
,
1188 struct ktermios
*old_termios
)
1190 struct uart_state
*state
= tty
->driver_data
;
1191 unsigned long flags
;
1192 unsigned int cflag
= tty
->termios
->c_cflag
;
1196 * These are the bits that are used to setup various
1197 * flags in the low level driver. We can ignore the Bfoo
1198 * bits in c_cflag; c_[io]speed will always be set
1199 * appropriately by set_termios() in tty_ioctl.c
1201 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1202 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1203 tty
->termios
->c_ospeed
== old_termios
->c_ospeed
&&
1204 tty
->termios
->c_ispeed
== old_termios
->c_ispeed
&&
1205 RELEVANT_IFLAG(tty
->termios
->c_iflag
^ old_termios
->c_iflag
) == 0) {
1209 uart_change_speed(tty
, state
, old_termios
);
1211 /* Handle transition to B0 status */
1212 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1213 uart_clear_mctrl(state
->uart_port
, TIOCM_RTS
| TIOCM_DTR
);
1214 /* Handle transition away from B0 status */
1215 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1216 unsigned int mask
= TIOCM_DTR
;
1217 if (!(cflag
& CRTSCTS
) ||
1218 !test_bit(TTY_THROTTLED
, &tty
->flags
))
1220 uart_set_mctrl(state
->uart_port
, mask
);
1223 /* Handle turning off CRTSCTS */
1224 if ((old_termios
->c_cflag
& CRTSCTS
) && !(cflag
& CRTSCTS
)) {
1225 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
1226 tty
->hw_stopped
= 0;
1228 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
1230 /* Handle turning on CRTSCTS */
1231 else if (!(old_termios
->c_cflag
& CRTSCTS
) && (cflag
& CRTSCTS
)) {
1232 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
1233 if (!(state
->uart_port
->ops
->get_mctrl(state
->uart_port
) & TIOCM_CTS
)) {
1234 tty
->hw_stopped
= 1;
1235 state
->uart_port
->ops
->stop_tx(state
->uart_port
);
1237 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
1242 * In 2.4.5, calls to this will be serialized via the BKL in
1243 * linux/drivers/char/tty_io.c:tty_release()
1244 * linux/drivers/char/tty_io.c:do_tty_handup()
1246 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1248 struct uart_state
*state
= tty
->driver_data
;
1249 struct tty_port
*port
;
1250 struct uart_port
*uport
;
1251 unsigned long flags
;
1256 uport
= state
->uart_port
;
1257 port
= &state
->port
;
1259 pr_debug("uart_close(%d) called\n", uport
->line
);
1261 spin_lock_irqsave(&port
->lock
, flags
);
1263 if (tty_hung_up_p(filp
)) {
1264 spin_unlock_irqrestore(&port
->lock
, flags
);
1268 if ((tty
->count
== 1) && (port
->count
!= 1)) {
1270 * Uh, oh. tty->count is 1, which means that the tty
1271 * structure will be freed. port->count should always
1272 * be one in these conditions. If it's greater than
1273 * one, we've got real problems, since it means the
1274 * serial port won't be shutdown.
1276 printk(KERN_ERR
"uart_close: bad serial port count; tty->count is 1, "
1277 "port->count is %d\n", port
->count
);
1280 if (--port
->count
< 0) {
1281 printk(KERN_ERR
"uart_close: bad serial port count for %s: %d\n",
1282 tty
->name
, port
->count
);
1286 spin_unlock_irqrestore(&port
->lock
, flags
);
1291 * Now we wait for the transmit buffer to clear; and we notify
1292 * the line discipline to only process XON/XOFF characters by
1293 * setting tty->closing.
1295 set_bit(ASYNCB_CLOSING
, &port
->flags
);
1297 spin_unlock_irqrestore(&port
->lock
, flags
);
1299 if (port
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1300 tty_wait_until_sent_from_close(tty
,
1301 msecs_to_jiffies(port
->closing_wait
));
1304 * At this point, we stop accepting input. To do this, we
1305 * disable the receive line status interrupts.
1307 if (port
->flags
& ASYNC_INITIALIZED
) {
1308 unsigned long flags
;
1309 spin_lock_irqsave(&uport
->lock
, flags
);
1310 uport
->ops
->stop_rx(uport
);
1311 spin_unlock_irqrestore(&uport
->lock
, flags
);
1313 * Before we drop DTR, make sure the UART transmitter
1314 * has completely drained; this is especially
1315 * important if there is a transmit FIFO!
1317 uart_wait_until_sent(tty
, uport
->timeout
);
1320 mutex_lock(&port
->mutex
);
1321 uart_shutdown(tty
, state
);
1322 uart_flush_buffer(tty
);
1324 tty_ldisc_flush(tty
);
1326 tty_port_tty_set(port
, NULL
);
1327 spin_lock_irqsave(&port
->lock
, flags
);
1330 if (port
->blocked_open
) {
1331 spin_unlock_irqrestore(&port
->lock
, flags
);
1332 if (port
->close_delay
)
1333 msleep_interruptible(port
->close_delay
);
1334 spin_lock_irqsave(&port
->lock
, flags
);
1335 } else if (!uart_console(uport
)) {
1336 spin_unlock_irqrestore(&port
->lock
, flags
);
1337 uart_change_pm(state
, 3);
1338 spin_lock_irqsave(&port
->lock
, flags
);
1342 * Wake up anyone trying to open this port.
1344 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1345 clear_bit(ASYNCB_CLOSING
, &port
->flags
);
1346 spin_unlock_irqrestore(&port
->lock
, flags
);
1347 wake_up_interruptible(&port
->open_wait
);
1348 wake_up_interruptible(&port
->close_wait
);
1351 mutex_unlock(&port
->mutex
);
1354 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1356 struct uart_state
*state
= tty
->driver_data
;
1357 struct uart_port
*port
= state
->uart_port
;
1358 unsigned long char_time
, expire
;
1360 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
1364 * Set the check interval to be 1/5 of the estimated time to
1365 * send a single character, and make it at least 1. The check
1366 * interval should also be less than the timeout.
1368 * Note: we have to use pretty tight timings here to satisfy
1371 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1372 char_time
= char_time
/ 5;
1375 if (timeout
&& timeout
< char_time
)
1376 char_time
= timeout
;
1379 * If the transmitter hasn't cleared in twice the approximate
1380 * amount of time to send the entire FIFO, it probably won't
1381 * ever clear. This assumes the UART isn't doing flow
1382 * control, which is currently the case. Hence, if it ever
1383 * takes longer than port->timeout, this is probably due to a
1384 * UART bug of some kind. So, we clamp the timeout parameter at
1387 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1388 timeout
= 2 * port
->timeout
;
1390 expire
= jiffies
+ timeout
;
1392 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1393 port
->line
, jiffies
, expire
);
1396 * Check whether the transmitter is empty every 'char_time'.
1397 * 'timeout' / 'expire' give us the maximum amount of time
1400 while (!port
->ops
->tx_empty(port
)) {
1401 msleep_interruptible(jiffies_to_msecs(char_time
));
1402 if (signal_pending(current
))
1404 if (time_after(jiffies
, expire
))
1410 * This is called with the BKL held in
1411 * linux/drivers/char/tty_io.c:do_tty_hangup()
1412 * We're called from the eventd thread, so we can sleep for
1413 * a _short_ time only.
1415 static void uart_hangup(struct tty_struct
*tty
)
1417 struct uart_state
*state
= tty
->driver_data
;
1418 struct tty_port
*port
= &state
->port
;
1419 unsigned long flags
;
1421 pr_debug("uart_hangup(%d)\n", state
->uart_port
->line
);
1423 mutex_lock(&port
->mutex
);
1424 if (port
->flags
& ASYNC_NORMAL_ACTIVE
) {
1425 uart_flush_buffer(tty
);
1426 uart_shutdown(tty
, state
);
1427 spin_lock_irqsave(&port
->lock
, flags
);
1429 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1430 spin_unlock_irqrestore(&port
->lock
, flags
);
1431 tty_port_tty_set(port
, NULL
);
1432 wake_up_interruptible(&port
->open_wait
);
1433 wake_up_interruptible(&port
->delta_msr_wait
);
1435 mutex_unlock(&port
->mutex
);
1438 static int uart_carrier_raised(struct tty_port
*port
)
1440 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1441 struct uart_port
*uport
= state
->uart_port
;
1443 spin_lock_irq(&uport
->lock
);
1444 uport
->ops
->enable_ms(uport
);
1445 mctrl
= uport
->ops
->get_mctrl(uport
);
1446 spin_unlock_irq(&uport
->lock
);
1447 if (mctrl
& TIOCM_CAR
)
1452 static void uart_dtr_rts(struct tty_port
*port
, int onoff
)
1454 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1455 struct uart_port
*uport
= state
->uart_port
;
1458 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1460 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1463 static struct uart_state
*uart_get(struct uart_driver
*drv
, int line
)
1465 struct uart_state
*state
;
1466 struct tty_port
*port
;
1469 state
= drv
->state
+ line
;
1470 port
= &state
->port
;
1471 if (mutex_lock_interruptible(&port
->mutex
)) {
1477 if (!state
->uart_port
|| state
->uart_port
->flags
& UPF_DEAD
) {
1485 mutex_unlock(&port
->mutex
);
1487 return ERR_PTR(ret
);
1491 * calls to uart_open are serialised by the BKL in
1492 * fs/char_dev.c:chrdev_open()
1493 * Note that if this fails, then uart_close() _will_ be called.
1495 * In time, we want to scrap the "opening nonpresent ports"
1496 * behaviour and implement an alternative way for setserial
1497 * to set base addresses/ports/types. This will allow us to
1498 * get rid of a certain amount of extra tests.
1500 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1502 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1503 struct uart_state
*state
;
1504 struct tty_port
*port
;
1505 int retval
, line
= tty
->index
;
1507 pr_debug("uart_open(%d) called\n", line
);
1510 * We take the semaphore inside uart_get to guarantee that we won't
1511 * be re-entered while allocating the state structure, or while we
1512 * request any IRQs that the driver may need. This also has the nice
1513 * side-effect that it delays the action of uart_hangup, so we can
1514 * guarantee that state->port.tty will always contain something
1517 state
= uart_get(drv
, line
);
1518 if (IS_ERR(state
)) {
1519 retval
= PTR_ERR(state
);
1522 port
= &state
->port
;
1525 * Once we set tty->driver_data here, we are guaranteed that
1526 * uart_close() will decrement the driver module use count.
1527 * Any failures from here onwards should not touch the count.
1529 tty
->driver_data
= state
;
1530 state
->uart_port
->state
= state
;
1531 tty
->low_latency
= (state
->uart_port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1533 tty_port_tty_set(port
, tty
);
1536 * If the port is in the middle of closing, bail out now.
1538 if (tty_hung_up_p(filp
)) {
1541 mutex_unlock(&port
->mutex
);
1546 * Make sure the device is in D0 state.
1548 if (port
->count
== 1)
1549 uart_change_pm(state
, 0);
1552 * Start up the serial port.
1554 retval
= uart_startup(tty
, state
, 0);
1557 * If we succeeded, wait until the port is ready.
1559 mutex_unlock(&port
->mutex
);
1561 retval
= tty_port_block_til_ready(port
, tty
, filp
);
1567 static const char *uart_type(struct uart_port
*port
)
1569 const char *str
= NULL
;
1571 if (port
->ops
->type
)
1572 str
= port
->ops
->type(port
);
1580 #ifdef CONFIG_PROC_FS
1582 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1584 struct uart_state
*state
= drv
->state
+ i
;
1585 struct tty_port
*port
= &state
->port
;
1587 struct uart_port
*uport
= state
->uart_port
;
1589 unsigned int status
;
1595 mmio
= uport
->iotype
>= UPIO_MEM
;
1596 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1597 uport
->line
, uart_type(uport
),
1598 mmio
? "mmio:0x" : "port:",
1599 mmio
? (unsigned long long)uport
->mapbase
1600 : (unsigned long long)uport
->iobase
,
1603 if (uport
->type
== PORT_UNKNOWN
) {
1608 if (capable(CAP_SYS_ADMIN
)) {
1609 mutex_lock(&port
->mutex
);
1610 pm_state
= state
->pm_state
;
1612 uart_change_pm(state
, 0);
1613 spin_lock_irq(&uport
->lock
);
1614 status
= uport
->ops
->get_mctrl(uport
);
1615 spin_unlock_irq(&uport
->lock
);
1617 uart_change_pm(state
, pm_state
);
1618 mutex_unlock(&port
->mutex
);
1620 seq_printf(m
, " tx:%d rx:%d",
1621 uport
->icount
.tx
, uport
->icount
.rx
);
1622 if (uport
->icount
.frame
)
1623 seq_printf(m
, " fe:%d",
1624 uport
->icount
.frame
);
1625 if (uport
->icount
.parity
)
1626 seq_printf(m
, " pe:%d",
1627 uport
->icount
.parity
);
1628 if (uport
->icount
.brk
)
1629 seq_printf(m
, " brk:%d",
1631 if (uport
->icount
.overrun
)
1632 seq_printf(m
, " oe:%d",
1633 uport
->icount
.overrun
);
1635 #define INFOBIT(bit, str) \
1636 if (uport->mctrl & (bit)) \
1637 strncat(stat_buf, (str), sizeof(stat_buf) - \
1638 strlen(stat_buf) - 2)
1639 #define STATBIT(bit, str) \
1640 if (status & (bit)) \
1641 strncat(stat_buf, (str), sizeof(stat_buf) - \
1642 strlen(stat_buf) - 2)
1646 INFOBIT(TIOCM_RTS
, "|RTS");
1647 STATBIT(TIOCM_CTS
, "|CTS");
1648 INFOBIT(TIOCM_DTR
, "|DTR");
1649 STATBIT(TIOCM_DSR
, "|DSR");
1650 STATBIT(TIOCM_CAR
, "|CD");
1651 STATBIT(TIOCM_RNG
, "|RI");
1655 seq_puts(m
, stat_buf
);
1662 static int uart_proc_show(struct seq_file
*m
, void *v
)
1664 struct tty_driver
*ttydrv
= m
->private;
1665 struct uart_driver
*drv
= ttydrv
->driver_state
;
1668 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
1670 for (i
= 0; i
< drv
->nr
; i
++)
1671 uart_line_info(m
, drv
, i
);
1675 static int uart_proc_open(struct inode
*inode
, struct file
*file
)
1677 return single_open(file
, uart_proc_show
, PDE(inode
)->data
);
1680 static const struct file_operations uart_proc_fops
= {
1681 .owner
= THIS_MODULE
,
1682 .open
= uart_proc_open
,
1684 .llseek
= seq_lseek
,
1685 .release
= single_release
,
1689 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1691 * uart_console_write - write a console message to a serial port
1692 * @port: the port to write the message
1693 * @s: array of characters
1694 * @count: number of characters in string to write
1695 * @write: function to write character to port
1697 void uart_console_write(struct uart_port
*port
, const char *s
,
1699 void (*putchar
)(struct uart_port
*, int))
1703 for (i
= 0; i
< count
; i
++, s
++) {
1705 putchar(port
, '\r');
1709 EXPORT_SYMBOL_GPL(uart_console_write
);
1712 * Check whether an invalid uart number has been specified, and
1713 * if so, search for the first available port that does have
1716 struct uart_port
* __init
1717 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1719 int idx
= co
->index
;
1721 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1722 ports
[idx
].membase
== NULL
))
1723 for (idx
= 0; idx
< nr
; idx
++)
1724 if (ports
[idx
].iobase
!= 0 ||
1725 ports
[idx
].membase
!= NULL
)
1734 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1735 * @options: pointer to option string
1736 * @baud: pointer to an 'int' variable for the baud rate.
1737 * @parity: pointer to an 'int' variable for the parity.
1738 * @bits: pointer to an 'int' variable for the number of data bits.
1739 * @flow: pointer to an 'int' variable for the flow control character.
1741 * uart_parse_options decodes a string containing the serial console
1742 * options. The format of the string is <baud><parity><bits><flow>,
1746 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1750 *baud
= simple_strtoul(s
, NULL
, 10);
1751 while (*s
>= '0' && *s
<= '9')
1760 EXPORT_SYMBOL_GPL(uart_parse_options
);
1767 static const struct baud_rates baud_rates
[] = {
1768 { 921600, B921600
},
1769 { 460800, B460800
},
1770 { 230400, B230400
},
1771 { 115200, B115200
},
1783 * uart_set_options - setup the serial console parameters
1784 * @port: pointer to the serial ports uart_port structure
1785 * @co: console pointer
1787 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1788 * @bits: number of data bits
1789 * @flow: flow control character - 'r' (rts)
1792 uart_set_options(struct uart_port
*port
, struct console
*co
,
1793 int baud
, int parity
, int bits
, int flow
)
1795 struct ktermios termios
;
1796 static struct ktermios dummy
;
1800 * Ensure that the serial console lock is initialised
1803 spin_lock_init(&port
->lock
);
1804 lockdep_set_class(&port
->lock
, &port_lock_key
);
1806 memset(&termios
, 0, sizeof(struct ktermios
));
1808 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1811 * Construct a cflag setting.
1813 for (i
= 0; baud_rates
[i
].rate
; i
++)
1814 if (baud_rates
[i
].rate
<= baud
)
1817 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1820 termios
.c_cflag
|= CS7
;
1822 termios
.c_cflag
|= CS8
;
1826 termios
.c_cflag
|= PARODD
;
1829 termios
.c_cflag
|= PARENB
;
1834 termios
.c_cflag
|= CRTSCTS
;
1837 * some uarts on other side don't support no flow control.
1838 * So we set * DTR in host uart to make them happy
1840 port
->mctrl
|= TIOCM_DTR
;
1842 port
->ops
->set_termios(port
, &termios
, &dummy
);
1844 * Allow the setting of the UART parameters with a NULL console
1848 co
->cflag
= termios
.c_cflag
;
1852 EXPORT_SYMBOL_GPL(uart_set_options
);
1853 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1855 static void uart_change_pm(struct uart_state
*state
, int pm_state
)
1857 struct uart_port
*port
= state
->uart_port
;
1859 if (state
->pm_state
!= pm_state
) {
1861 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
1862 state
->pm_state
= pm_state
;
1867 struct uart_port
*port
;
1868 struct uart_driver
*driver
;
1871 static int serial_match_port(struct device
*dev
, void *data
)
1873 struct uart_match
*match
= data
;
1874 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
1875 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
1878 return dev
->devt
== devt
; /* Actually, only one tty per port */
1881 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*uport
)
1883 struct uart_state
*state
= drv
->state
+ uport
->line
;
1884 struct tty_port
*port
= &state
->port
;
1885 struct device
*tty_dev
;
1886 struct uart_match match
= {uport
, drv
};
1888 mutex_lock(&port
->mutex
);
1890 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
1891 if (device_may_wakeup(tty_dev
)) {
1892 if (!enable_irq_wake(uport
->irq
))
1893 uport
->irq_wake
= 1;
1894 put_device(tty_dev
);
1895 mutex_unlock(&port
->mutex
);
1898 if (console_suspend_enabled
|| !uart_console(uport
))
1899 uport
->suspended
= 1;
1901 if (port
->flags
& ASYNC_INITIALIZED
) {
1902 const struct uart_ops
*ops
= uport
->ops
;
1905 if (console_suspend_enabled
|| !uart_console(uport
)) {
1906 set_bit(ASYNCB_SUSPENDED
, &port
->flags
);
1907 clear_bit(ASYNCB_INITIALIZED
, &port
->flags
);
1909 spin_lock_irq(&uport
->lock
);
1910 ops
->stop_tx(uport
);
1911 ops
->set_mctrl(uport
, 0);
1912 ops
->stop_rx(uport
);
1913 spin_unlock_irq(&uport
->lock
);
1917 * Wait for the transmitter to empty.
1919 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
1922 printk(KERN_ERR
"%s%s%s%d: Unable to drain "
1924 uport
->dev
? dev_name(uport
->dev
) : "",
1925 uport
->dev
? ": " : "",
1927 drv
->tty_driver
->name_base
+ uport
->line
);
1929 if (console_suspend_enabled
|| !uart_console(uport
))
1930 ops
->shutdown(uport
);
1934 * Disable the console device before suspending.
1936 if (console_suspend_enabled
&& uart_console(uport
))
1937 console_stop(uport
->cons
);
1939 if (console_suspend_enabled
|| !uart_console(uport
))
1940 uart_change_pm(state
, 3);
1942 mutex_unlock(&port
->mutex
);
1947 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
1949 struct uart_state
*state
= drv
->state
+ uport
->line
;
1950 struct tty_port
*port
= &state
->port
;
1951 struct device
*tty_dev
;
1952 struct uart_match match
= {uport
, drv
};
1953 struct ktermios termios
;
1955 mutex_lock(&port
->mutex
);
1957 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
1958 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
1959 if (uport
->irq_wake
) {
1960 disable_irq_wake(uport
->irq
);
1961 uport
->irq_wake
= 0;
1963 mutex_unlock(&port
->mutex
);
1966 uport
->suspended
= 0;
1969 * Re-enable the console device after suspending.
1971 if (uart_console(uport
)) {
1973 * First try to use the console cflag setting.
1975 memset(&termios
, 0, sizeof(struct ktermios
));
1976 termios
.c_cflag
= uport
->cons
->cflag
;
1979 * If that's unset, use the tty termios setting.
1981 if (port
->tty
&& port
->tty
->termios
&& termios
.c_cflag
== 0)
1982 termios
= *(port
->tty
->termios
);
1984 uport
->ops
->set_termios(uport
, &termios
, NULL
);
1985 if (console_suspend_enabled
)
1986 console_start(uport
->cons
);
1989 if (port
->flags
& ASYNC_SUSPENDED
) {
1990 const struct uart_ops
*ops
= uport
->ops
;
1993 uart_change_pm(state
, 0);
1994 spin_lock_irq(&uport
->lock
);
1995 ops
->set_mctrl(uport
, 0);
1996 spin_unlock_irq(&uport
->lock
);
1997 if (console_suspend_enabled
|| !uart_console(uport
)) {
1998 /* Protected by port mutex for now */
1999 struct tty_struct
*tty
= port
->tty
;
2000 ret
= ops
->startup(uport
);
2003 uart_change_speed(tty
, state
, NULL
);
2004 spin_lock_irq(&uport
->lock
);
2005 ops
->set_mctrl(uport
, uport
->mctrl
);
2006 ops
->start_tx(uport
);
2007 spin_unlock_irq(&uport
->lock
);
2008 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2011 * Failed to resume - maybe hardware went away?
2012 * Clear the "initialized" flag so we won't try
2013 * to call the low level drivers shutdown method.
2015 uart_shutdown(tty
, state
);
2019 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2022 mutex_unlock(&port
->mutex
);
2028 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2032 switch (port
->iotype
) {
2034 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2037 snprintf(address
, sizeof(address
),
2038 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2044 snprintf(address
, sizeof(address
),
2045 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2048 strlcpy(address
, "*unknown*", sizeof(address
));
2052 printk(KERN_INFO
"%s%s%s%d at %s (irq = %d) is a %s\n",
2053 port
->dev
? dev_name(port
->dev
) : "",
2054 port
->dev
? ": " : "",
2056 drv
->tty_driver
->name_base
+ port
->line
,
2057 address
, port
->irq
, uart_type(port
));
2061 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2062 struct uart_port
*port
)
2067 * If there isn't a port here, don't do anything further.
2069 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2073 * Now do the auto configuration stuff. Note that config_port
2074 * is expected to claim the resources and map the port for us.
2077 if (port
->flags
& UPF_AUTO_IRQ
)
2078 flags
|= UART_CONFIG_IRQ
;
2079 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2080 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2081 port
->type
= PORT_UNKNOWN
;
2082 flags
|= UART_CONFIG_TYPE
;
2084 port
->ops
->config_port(port
, flags
);
2087 if (port
->type
!= PORT_UNKNOWN
) {
2088 unsigned long flags
;
2090 uart_report_port(drv
, port
);
2092 /* Power up port for set_mctrl() */
2093 uart_change_pm(state
, 0);
2096 * Ensure that the modem control lines are de-activated.
2097 * keep the DTR setting that is set in uart_set_options()
2098 * We probably don't need a spinlock around this, but
2100 spin_lock_irqsave(&port
->lock
, flags
);
2101 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2102 spin_unlock_irqrestore(&port
->lock
, flags
);
2105 * If this driver supports console, and it hasn't been
2106 * successfully registered yet, try to re-register it.
2107 * It may be that the port was not available.
2109 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2110 register_console(port
->cons
);
2113 * Power down all ports by default, except the
2114 * console if we have one.
2116 if (!uart_console(port
))
2117 uart_change_pm(state
, 3);
2121 #ifdef CONFIG_CONSOLE_POLL
2123 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2125 struct uart_driver
*drv
= driver
->driver_state
;
2126 struct uart_state
*state
= drv
->state
+ line
;
2127 struct uart_port
*port
;
2133 if (!state
|| !state
->uart_port
)
2136 port
= state
->uart_port
;
2137 if (!(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
))
2141 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2142 return uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2148 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2150 struct uart_driver
*drv
= driver
->driver_state
;
2151 struct uart_state
*state
= drv
->state
+ line
;
2152 struct uart_port
*port
;
2154 if (!state
|| !state
->uart_port
)
2157 port
= state
->uart_port
;
2158 return port
->ops
->poll_get_char(port
);
2161 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2163 struct uart_driver
*drv
= driver
->driver_state
;
2164 struct uart_state
*state
= drv
->state
+ line
;
2165 struct uart_port
*port
;
2167 if (!state
|| !state
->uart_port
)
2170 port
= state
->uart_port
;
2171 port
->ops
->poll_put_char(port
, ch
);
2175 static const struct tty_operations uart_ops
= {
2177 .close
= uart_close
,
2178 .write
= uart_write
,
2179 .put_char
= uart_put_char
,
2180 .flush_chars
= uart_flush_chars
,
2181 .write_room
= uart_write_room
,
2182 .chars_in_buffer
= uart_chars_in_buffer
,
2183 .flush_buffer
= uart_flush_buffer
,
2184 .ioctl
= uart_ioctl
,
2185 .throttle
= uart_throttle
,
2186 .unthrottle
= uart_unthrottle
,
2187 .send_xchar
= uart_send_xchar
,
2188 .set_termios
= uart_set_termios
,
2189 .set_ldisc
= uart_set_ldisc
,
2191 .start
= uart_start
,
2192 .hangup
= uart_hangup
,
2193 .break_ctl
= uart_break_ctl
,
2194 .wait_until_sent
= uart_wait_until_sent
,
2195 #ifdef CONFIG_PROC_FS
2196 .proc_fops
= &uart_proc_fops
,
2198 .tiocmget
= uart_tiocmget
,
2199 .tiocmset
= uart_tiocmset
,
2200 .get_icount
= uart_get_icount
,
2201 #ifdef CONFIG_CONSOLE_POLL
2202 .poll_init
= uart_poll_init
,
2203 .poll_get_char
= uart_poll_get_char
,
2204 .poll_put_char
= uart_poll_put_char
,
2208 static const struct tty_port_operations uart_port_ops
= {
2209 .carrier_raised
= uart_carrier_raised
,
2210 .dtr_rts
= uart_dtr_rts
,
2214 * uart_register_driver - register a driver with the uart core layer
2215 * @drv: low level driver structure
2217 * Register a uart driver with the core driver. We in turn register
2218 * with the tty layer, and initialise the core driver per-port state.
2220 * We have a proc file in /proc/tty/driver which is named after the
2223 * drv->port should be NULL, and the per-port structures should be
2224 * registered using uart_add_one_port after this call has succeeded.
2226 int uart_register_driver(struct uart_driver
*drv
)
2228 struct tty_driver
*normal
;
2234 * Maybe we should be using a slab cache for this, especially if
2235 * we have a large number of ports to handle.
2237 drv
->state
= kzalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2241 normal
= alloc_tty_driver(drv
->nr
);
2245 drv
->tty_driver
= normal
;
2247 normal
->owner
= drv
->owner
;
2248 normal
->driver_name
= drv
->driver_name
;
2249 normal
->name
= drv
->dev_name
;
2250 normal
->major
= drv
->major
;
2251 normal
->minor_start
= drv
->minor
;
2252 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2253 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2254 normal
->init_termios
= tty_std_termios
;
2255 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2256 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2257 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2258 normal
->driver_state
= drv
;
2259 tty_set_operations(normal
, &uart_ops
);
2262 * Initialise the UART state(s).
2264 for (i
= 0; i
< drv
->nr
; i
++) {
2265 struct uart_state
*state
= drv
->state
+ i
;
2266 struct tty_port
*port
= &state
->port
;
2268 tty_port_init(port
);
2269 port
->ops
= &uart_port_ops
;
2270 port
->close_delay
= 500; /* .5 seconds */
2271 port
->closing_wait
= 30000; /* 30 seconds */
2274 retval
= tty_register_driver(normal
);
2278 put_tty_driver(normal
);
2286 * uart_unregister_driver - remove a driver from the uart core layer
2287 * @drv: low level driver structure
2289 * Remove all references to a driver from the core driver. The low
2290 * level driver must have removed all its ports via the
2291 * uart_remove_one_port() if it registered them with uart_add_one_port().
2292 * (ie, drv->port == NULL)
2294 void uart_unregister_driver(struct uart_driver
*drv
)
2296 struct tty_driver
*p
= drv
->tty_driver
;
2297 tty_unregister_driver(p
);
2300 drv
->tty_driver
= NULL
;
2303 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2305 struct uart_driver
*p
= co
->data
;
2307 return p
->tty_driver
;
2311 * uart_add_one_port - attach a driver-defined port structure
2312 * @drv: pointer to the uart low level driver structure for this port
2313 * @uport: uart port structure to use for this port.
2315 * This allows the driver to register its own uart_port structure
2316 * with the core driver. The main purpose is to allow the low
2317 * level uart drivers to expand uart_port, rather than having yet
2318 * more levels of structures.
2320 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2322 struct uart_state
*state
;
2323 struct tty_port
*port
;
2325 struct device
*tty_dev
;
2327 BUG_ON(in_interrupt());
2329 if (uport
->line
>= drv
->nr
)
2332 state
= drv
->state
+ uport
->line
;
2333 port
= &state
->port
;
2335 mutex_lock(&port_mutex
);
2336 mutex_lock(&port
->mutex
);
2337 if (state
->uart_port
) {
2342 state
->uart_port
= uport
;
2343 state
->pm_state
= -1;
2345 uport
->cons
= drv
->cons
;
2346 uport
->state
= state
;
2349 * If this port is a console, then the spinlock is already
2352 if (!(uart_console(uport
) && (uport
->cons
->flags
& CON_ENABLED
))) {
2353 spin_lock_init(&uport
->lock
);
2354 lockdep_set_class(&uport
->lock
, &port_lock_key
);
2357 uart_configure_port(drv
, state
, uport
);
2360 * Register the port whether it's detected or not. This allows
2361 * setserial to be used to alter this ports parameters.
2363 tty_dev
= tty_register_device(drv
->tty_driver
, uport
->line
, uport
->dev
);
2364 if (likely(!IS_ERR(tty_dev
))) {
2365 device_init_wakeup(tty_dev
, 1);
2366 device_set_wakeup_enable(tty_dev
, 0);
2368 printk(KERN_ERR
"Cannot register tty device on line %d\n",
2372 * Ensure UPF_DEAD is not set.
2374 uport
->flags
&= ~UPF_DEAD
;
2377 mutex_unlock(&port
->mutex
);
2378 mutex_unlock(&port_mutex
);
2384 * uart_remove_one_port - detach a driver defined port structure
2385 * @drv: pointer to the uart low level driver structure for this port
2386 * @uport: uart port structure for this port
2388 * This unhooks (and hangs up) the specified port structure from the
2389 * core driver. No further calls will be made to the low-level code
2392 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2394 struct uart_state
*state
= drv
->state
+ uport
->line
;
2395 struct tty_port
*port
= &state
->port
;
2397 BUG_ON(in_interrupt());
2399 if (state
->uart_port
!= uport
)
2400 printk(KERN_ALERT
"Removing wrong port: %p != %p\n",
2401 state
->uart_port
, uport
);
2403 mutex_lock(&port_mutex
);
2406 * Mark the port "dead" - this prevents any opens from
2407 * succeeding while we shut down the port.
2409 mutex_lock(&port
->mutex
);
2410 uport
->flags
|= UPF_DEAD
;
2411 mutex_unlock(&port
->mutex
);
2414 * Remove the devices from the tty layer
2416 tty_unregister_device(drv
->tty_driver
, uport
->line
);
2419 tty_vhangup(port
->tty
);
2422 * Free the port IO and memory resources, if any.
2424 if (uport
->type
!= PORT_UNKNOWN
)
2425 uport
->ops
->release_port(uport
);
2428 * Indicate that there isn't a port here anymore.
2430 uport
->type
= PORT_UNKNOWN
;
2432 state
->uart_port
= NULL
;
2433 mutex_unlock(&port_mutex
);
2439 * Are the two ports equivalent?
2441 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2443 if (port1
->iotype
!= port2
->iotype
)
2446 switch (port1
->iotype
) {
2448 return (port1
->iobase
== port2
->iobase
);
2450 return (port1
->iobase
== port2
->iobase
) &&
2451 (port1
->hub6
== port2
->hub6
);
2456 return (port1
->mapbase
== port2
->mapbase
);
2460 EXPORT_SYMBOL(uart_match_port
);
2462 EXPORT_SYMBOL(uart_write_wakeup
);
2463 EXPORT_SYMBOL(uart_register_driver
);
2464 EXPORT_SYMBOL(uart_unregister_driver
);
2465 EXPORT_SYMBOL(uart_suspend_port
);
2466 EXPORT_SYMBOL(uart_resume_port
);
2467 EXPORT_SYMBOL(uart_add_one_port
);
2468 EXPORT_SYMBOL(uart_remove_one_port
);
2470 MODULE_DESCRIPTION("Serial driver core");
2471 MODULE_LICENSE("GPL");