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/tty_flip.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/serial_core.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
42 * This is used to lock changes in serial line configuration.
44 static DEFINE_MUTEX(port_mutex
);
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
50 static struct lock_class_key port_lock_key
;
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
55 struct ktermios
*old_termios
);
56 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
57 static void uart_change_pm(struct uart_state
*state
,
58 enum uart_pm_state pm_state
);
60 static void uart_port_shutdown(struct tty_port
*port
);
62 static int uart_dcd_enabled(struct uart_port
*uport
)
64 return !!(uport
->status
& UPSTAT_DCD_ENABLE
);
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
71 void uart_write_wakeup(struct uart_port
*port
)
73 struct uart_state
*state
= port
->state
;
75 * This means you called this function _after_ the port was
76 * closed. No cookie for you.
79 tty_wakeup(state
->port
.tty
);
82 static void uart_stop(struct tty_struct
*tty
)
84 struct uart_state
*state
= tty
->driver_data
;
85 struct uart_port
*port
= state
->uart_port
;
88 spin_lock_irqsave(&port
->lock
, flags
);
89 port
->ops
->stop_tx(port
);
90 spin_unlock_irqrestore(&port
->lock
, flags
);
93 static void __uart_start(struct tty_struct
*tty
)
95 struct uart_state
*state
= tty
->driver_data
;
96 struct uart_port
*port
= state
->uart_port
;
98 if (!uart_tx_stopped(port
))
99 port
->ops
->start_tx(port
);
102 static void uart_start(struct tty_struct
*tty
)
104 struct uart_state
*state
= tty
->driver_data
;
105 struct uart_port
*port
= state
->uart_port
;
108 spin_lock_irqsave(&port
->lock
, flags
);
110 spin_unlock_irqrestore(&port
->lock
, flags
);
114 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
119 spin_lock_irqsave(&port
->lock
, flags
);
121 port
->mctrl
= (old
& ~clear
) | set
;
122 if (old
!= port
->mctrl
)
123 port
->ops
->set_mctrl(port
, port
->mctrl
);
124 spin_unlock_irqrestore(&port
->lock
, flags
);
127 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
128 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
131 * Startup the port. This will be called once per open. All calls
132 * will be serialised by the per-port mutex.
134 static int uart_port_startup(struct tty_struct
*tty
, struct uart_state
*state
,
137 struct uart_port
*uport
= state
->uart_port
;
141 if (uport
->type
== PORT_UNKNOWN
)
145 * Make sure the device is in D0 state.
147 uart_change_pm(state
, UART_PM_STATE_ON
);
150 * Initialise and allocate the transmit and temporary
153 if (!state
->xmit
.buf
) {
154 /* This is protected by the per port mutex */
155 page
= get_zeroed_page(GFP_KERNEL
);
159 state
->xmit
.buf
= (unsigned char *) page
;
160 uart_circ_clear(&state
->xmit
);
163 retval
= uport
->ops
->startup(uport
);
165 if (uart_console(uport
) && uport
->cons
->cflag
) {
166 tty
->termios
.c_cflag
= uport
->cons
->cflag
;
167 uport
->cons
->cflag
= 0;
170 * Initialise the hardware port settings.
172 uart_change_speed(tty
, state
, NULL
);
176 * Setup the RTS and DTR signals once the
177 * port is open and ready to respond.
179 if (tty
->termios
.c_cflag
& CBAUD
)
180 uart_set_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
185 * This is to allow setserial on this port. People may want to set
186 * port/irq/type and then reconfigure the port properly if it failed
189 if (retval
&& capable(CAP_SYS_ADMIN
))
195 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
,
198 struct tty_port
*port
= &state
->port
;
201 if (port
->flags
& ASYNC_INITIALIZED
)
205 * Set the TTY IO error marker - we will only clear this
206 * once we have successfully opened the port.
208 set_bit(TTY_IO_ERROR
, &tty
->flags
);
210 retval
= uart_port_startup(tty
, state
, init_hw
);
212 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
213 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
214 } else if (retval
> 0)
221 * This routine will shutdown a serial port; interrupts are disabled, and
222 * DTR is dropped if the hangup on close termio flag is on. Calls to
223 * uart_shutdown are serialised by the per-port semaphore.
225 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
227 struct uart_port
*uport
= state
->uart_port
;
228 struct tty_port
*port
= &state
->port
;
231 * Set the TTY IO error marker
234 set_bit(TTY_IO_ERROR
, &tty
->flags
);
236 if (test_and_clear_bit(ASYNCB_INITIALIZED
, &port
->flags
)) {
238 * Turn off DTR and RTS early.
240 if (uart_console(uport
) && tty
)
241 uport
->cons
->cflag
= tty
->termios
.c_cflag
;
243 if (!tty
|| (tty
->termios
.c_cflag
& HUPCL
))
244 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
246 uart_port_shutdown(port
);
250 * It's possible for shutdown to be called after suspend if we get
251 * a DCD drop (hangup) at just the right time. Clear suspended bit so
252 * we don't try to resume a port that has been shutdown.
254 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
257 * Free the transmit buffer page.
259 if (state
->xmit
.buf
) {
260 free_page((unsigned long)state
->xmit
.buf
);
261 state
->xmit
.buf
= NULL
;
266 * uart_update_timeout - update per-port FIFO timeout.
267 * @port: uart_port structure describing the port
268 * @cflag: termios cflag value
269 * @baud: speed of the port
271 * Set the port FIFO timeout value. The @cflag value should
272 * reflect the actual hardware settings.
275 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
280 /* byte size and parity */
281 switch (cflag
& CSIZE
) {
302 * The total number of bits to be transmitted in the fifo.
304 bits
= bits
* port
->fifosize
;
307 * Figure the timeout to send the above number of bits.
308 * Add .02 seconds of slop
310 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
313 EXPORT_SYMBOL(uart_update_timeout
);
316 * uart_get_baud_rate - return baud rate for a particular port
317 * @port: uart_port structure describing the port in question.
318 * @termios: desired termios settings.
319 * @old: old termios (or NULL)
320 * @min: minimum acceptable baud rate
321 * @max: maximum acceptable baud rate
323 * Decode the termios structure into a numeric baud rate,
324 * taking account of the magic 38400 baud rate (with spd_*
325 * flags), and mapping the %B0 rate to 9600 baud.
327 * If the new baud rate is invalid, try the old termios setting.
328 * If it's still invalid, we try 9600 baud.
330 * Update the @termios structure to reflect the baud rate
331 * we're actually going to be using. Don't do this for the case
332 * where B0 is requested ("hang up").
335 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
336 struct ktermios
*old
, unsigned int min
, unsigned int max
)
340 unsigned int altbaud
;
342 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
362 for (try = 0; try < 2; try++) {
363 baud
= tty_termios_baud_rate(termios
);
366 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
369 if (try == 0 && baud
== 38400)
373 * Special case: B0 rate.
380 if (baud
>= min
&& baud
<= max
)
384 * Oops, the quotient was zero. Try again with
385 * the old baud rate if possible.
387 termios
->c_cflag
&= ~CBAUD
;
389 baud
= tty_termios_baud_rate(old
);
391 tty_termios_encode_baud_rate(termios
,
398 * As a last resort, if the range cannot be met then clip to
399 * the nearest chip supported rate.
403 tty_termios_encode_baud_rate(termios
,
406 tty_termios_encode_baud_rate(termios
,
410 /* Should never happen */
415 EXPORT_SYMBOL(uart_get_baud_rate
);
418 * uart_get_divisor - return uart clock divisor
419 * @port: uart_port structure describing the port.
420 * @baud: desired baud rate
422 * Calculate the uart clock divisor for the port.
425 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
430 * Old custom speed handling.
432 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
433 quot
= port
->custom_divisor
;
435 quot
= DIV_ROUND_CLOSEST(port
->uartclk
, 16 * baud
);
440 EXPORT_SYMBOL(uart_get_divisor
);
442 /* Caller holds port mutex */
443 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
444 struct ktermios
*old_termios
)
446 struct uart_port
*uport
= state
->uart_port
;
447 struct ktermios
*termios
;
451 * If we have no tty, termios, or the port does not exist,
452 * then we can't set the parameters for this port.
454 if (!tty
|| uport
->type
== PORT_UNKNOWN
)
457 termios
= &tty
->termios
;
458 uport
->ops
->set_termios(uport
, termios
, old_termios
);
461 * Set modem status enables based on termios cflag
463 spin_lock_irq(&uport
->lock
);
464 if (termios
->c_cflag
& CRTSCTS
)
465 uport
->status
|= UPSTAT_CTS_ENABLE
;
467 uport
->status
&= ~UPSTAT_CTS_ENABLE
;
469 if (termios
->c_cflag
& CLOCAL
)
470 uport
->status
&= ~UPSTAT_DCD_ENABLE
;
472 uport
->status
|= UPSTAT_DCD_ENABLE
;
474 /* reset sw-assisted CTS flow control based on (possibly) new mode */
475 hw_stopped
= uport
->hw_stopped
;
476 uport
->hw_stopped
= uart_softcts_mode(uport
) &&
477 !(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
);
478 if (uport
->hw_stopped
) {
480 uport
->ops
->stop_tx(uport
);
485 spin_unlock_irq(&uport
->lock
);
488 static inline int __uart_put_char(struct uart_port
*port
,
489 struct circ_buf
*circ
, unsigned char c
)
497 spin_lock_irqsave(&port
->lock
, flags
);
498 if (uart_circ_chars_free(circ
) != 0) {
499 circ
->buf
[circ
->head
] = c
;
500 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
503 spin_unlock_irqrestore(&port
->lock
, flags
);
507 static int uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
509 struct uart_state
*state
= tty
->driver_data
;
511 return __uart_put_char(state
->uart_port
, &state
->xmit
, ch
);
514 static void uart_flush_chars(struct tty_struct
*tty
)
519 static int uart_write(struct tty_struct
*tty
,
520 const unsigned char *buf
, int count
)
522 struct uart_state
*state
= tty
->driver_data
;
523 struct uart_port
*port
;
524 struct circ_buf
*circ
;
529 * This means you called this function _after_ the port was
530 * closed. No cookie for you.
537 port
= state
->uart_port
;
543 spin_lock_irqsave(&port
->lock
, flags
);
545 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
550 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
551 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
558 spin_unlock_irqrestore(&port
->lock
, flags
);
563 static int uart_write_room(struct tty_struct
*tty
)
565 struct uart_state
*state
= tty
->driver_data
;
569 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
570 ret
= uart_circ_chars_free(&state
->xmit
);
571 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
575 static int uart_chars_in_buffer(struct tty_struct
*tty
)
577 struct uart_state
*state
= tty
->driver_data
;
581 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
582 ret
= uart_circ_chars_pending(&state
->xmit
);
583 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
587 static void uart_flush_buffer(struct tty_struct
*tty
)
589 struct uart_state
*state
= tty
->driver_data
;
590 struct uart_port
*port
;
594 * This means you called this function _after_ the port was
595 * closed. No cookie for you.
602 port
= state
->uart_port
;
603 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
605 spin_lock_irqsave(&port
->lock
, flags
);
606 uart_circ_clear(&state
->xmit
);
607 if (port
->ops
->flush_buffer
)
608 port
->ops
->flush_buffer(port
);
609 spin_unlock_irqrestore(&port
->lock
, flags
);
614 * This function is used to send a high-priority XON/XOFF character to
617 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
619 struct uart_state
*state
= tty
->driver_data
;
620 struct uart_port
*port
= state
->uart_port
;
623 if (port
->ops
->send_xchar
)
624 port
->ops
->send_xchar(port
, ch
);
626 spin_lock_irqsave(&port
->lock
, flags
);
629 port
->ops
->start_tx(port
);
630 spin_unlock_irqrestore(&port
->lock
, flags
);
634 static void uart_throttle(struct tty_struct
*tty
)
636 struct uart_state
*state
= tty
->driver_data
;
637 struct uart_port
*port
= state
->uart_port
;
641 mask
|= UPSTAT_AUTOXOFF
;
642 if (tty
->termios
.c_cflag
& CRTSCTS
)
643 mask
|= UPSTAT_AUTORTS
;
645 if (port
->status
& mask
) {
646 port
->ops
->throttle(port
);
647 mask
&= ~port
->status
;
650 if (mask
& UPSTAT_AUTOXOFF
)
651 uart_send_xchar(tty
, STOP_CHAR(tty
));
653 if (mask
& UPSTAT_AUTORTS
)
654 uart_clear_mctrl(port
, TIOCM_RTS
);
657 static void uart_unthrottle(struct tty_struct
*tty
)
659 struct uart_state
*state
= tty
->driver_data
;
660 struct uart_port
*port
= state
->uart_port
;
664 mask
|= UPSTAT_AUTOXOFF
;
665 if (tty
->termios
.c_cflag
& CRTSCTS
)
666 mask
|= UPSTAT_AUTORTS
;
668 if (port
->status
& mask
) {
669 port
->ops
->unthrottle(port
);
670 mask
&= ~port
->status
;
673 if (mask
& UPSTAT_AUTOXOFF
)
674 uart_send_xchar(tty
, START_CHAR(tty
));
676 if (mask
& UPSTAT_AUTORTS
)
677 uart_set_mctrl(port
, TIOCM_RTS
);
680 static void do_uart_get_info(struct tty_port
*port
,
681 struct serial_struct
*retinfo
)
683 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
684 struct uart_port
*uport
= state
->uart_port
;
686 memset(retinfo
, 0, sizeof(*retinfo
));
688 retinfo
->type
= uport
->type
;
689 retinfo
->line
= uport
->line
;
690 retinfo
->port
= uport
->iobase
;
691 if (HIGH_BITS_OFFSET
)
692 retinfo
->port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
693 retinfo
->irq
= uport
->irq
;
694 retinfo
->flags
= uport
->flags
;
695 retinfo
->xmit_fifo_size
= uport
->fifosize
;
696 retinfo
->baud_base
= uport
->uartclk
/ 16;
697 retinfo
->close_delay
= jiffies_to_msecs(port
->close_delay
) / 10;
698 retinfo
->closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
699 ASYNC_CLOSING_WAIT_NONE
:
700 jiffies_to_msecs(port
->closing_wait
) / 10;
701 retinfo
->custom_divisor
= uport
->custom_divisor
;
702 retinfo
->hub6
= uport
->hub6
;
703 retinfo
->io_type
= uport
->iotype
;
704 retinfo
->iomem_reg_shift
= uport
->regshift
;
705 retinfo
->iomem_base
= (void *)(unsigned long)uport
->mapbase
;
708 static void uart_get_info(struct tty_port
*port
,
709 struct serial_struct
*retinfo
)
711 /* Ensure the state we copy is consistent and no hardware changes
713 mutex_lock(&port
->mutex
);
714 do_uart_get_info(port
, retinfo
);
715 mutex_unlock(&port
->mutex
);
718 static int uart_get_info_user(struct tty_port
*port
,
719 struct serial_struct __user
*retinfo
)
721 struct serial_struct tmp
;
722 uart_get_info(port
, &tmp
);
724 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
729 static int uart_set_info(struct tty_struct
*tty
, struct tty_port
*port
,
730 struct uart_state
*state
,
731 struct serial_struct
*new_info
)
733 struct uart_port
*uport
= state
->uart_port
;
734 unsigned long new_port
;
735 unsigned int change_irq
, change_port
, closing_wait
;
736 unsigned int old_custom_divisor
, close_delay
;
737 upf_t old_flags
, new_flags
;
740 new_port
= new_info
->port
;
741 if (HIGH_BITS_OFFSET
)
742 new_port
+= (unsigned long) new_info
->port_high
<< HIGH_BITS_OFFSET
;
744 new_info
->irq
= irq_canonicalize(new_info
->irq
);
745 close_delay
= msecs_to_jiffies(new_info
->close_delay
* 10);
746 closing_wait
= new_info
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
747 ASYNC_CLOSING_WAIT_NONE
:
748 msecs_to_jiffies(new_info
->closing_wait
* 10);
751 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
752 && new_info
->irq
!= uport
->irq
;
755 * Since changing the 'type' of the port changes its resource
756 * allocations, we should treat type changes the same as
759 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
760 && (new_port
!= uport
->iobase
||
761 (unsigned long)new_info
->iomem_base
!= uport
->mapbase
||
762 new_info
->hub6
!= uport
->hub6
||
763 new_info
->io_type
!= uport
->iotype
||
764 new_info
->iomem_reg_shift
!= uport
->regshift
||
765 new_info
->type
!= uport
->type
);
767 old_flags
= uport
->flags
;
768 new_flags
= new_info
->flags
;
769 old_custom_divisor
= uport
->custom_divisor
;
771 if (!capable(CAP_SYS_ADMIN
)) {
773 if (change_irq
|| change_port
||
774 (new_info
->baud_base
!= uport
->uartclk
/ 16) ||
775 (close_delay
!= port
->close_delay
) ||
776 (closing_wait
!= port
->closing_wait
) ||
777 (new_info
->xmit_fifo_size
&&
778 new_info
->xmit_fifo_size
!= uport
->fifosize
) ||
779 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
781 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
782 (new_flags
& UPF_USR_MASK
));
783 uport
->custom_divisor
= new_info
->custom_divisor
;
788 * Ask the low level driver to verify the settings.
790 if (uport
->ops
->verify_port
)
791 retval
= uport
->ops
->verify_port(uport
, new_info
);
793 if ((new_info
->irq
>= nr_irqs
) || (new_info
->irq
< 0) ||
794 (new_info
->baud_base
< 9600))
800 if (change_port
|| change_irq
) {
804 * Make sure that we are the sole user of this port.
806 if (tty_port_users(port
) > 1)
810 * We need to shutdown the serial port at the old
811 * port/type/irq combination.
813 uart_shutdown(tty
, state
);
817 unsigned long old_iobase
, old_mapbase
;
818 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
820 old_iobase
= uport
->iobase
;
821 old_mapbase
= uport
->mapbase
;
822 old_type
= uport
->type
;
823 old_hub6
= uport
->hub6
;
824 old_iotype
= uport
->iotype
;
825 old_shift
= uport
->regshift
;
828 * Free and release old regions
830 if (old_type
!= PORT_UNKNOWN
)
831 uport
->ops
->release_port(uport
);
833 uport
->iobase
= new_port
;
834 uport
->type
= new_info
->type
;
835 uport
->hub6
= new_info
->hub6
;
836 uport
->iotype
= new_info
->io_type
;
837 uport
->regshift
= new_info
->iomem_reg_shift
;
838 uport
->mapbase
= (unsigned long)new_info
->iomem_base
;
841 * Claim and map the new regions
843 if (uport
->type
!= PORT_UNKNOWN
) {
844 retval
= uport
->ops
->request_port(uport
);
846 /* Always success - Jean II */
851 * If we fail to request resources for the
852 * new port, try to restore the old settings.
855 uport
->iobase
= old_iobase
;
856 uport
->type
= old_type
;
857 uport
->hub6
= old_hub6
;
858 uport
->iotype
= old_iotype
;
859 uport
->regshift
= old_shift
;
860 uport
->mapbase
= old_mapbase
;
862 if (old_type
!= PORT_UNKNOWN
) {
863 retval
= uport
->ops
->request_port(uport
);
865 * If we failed to restore the old settings,
869 uport
->type
= PORT_UNKNOWN
;
877 /* Added to return the correct error -Ram Gupta */
883 uport
->irq
= new_info
->irq
;
884 if (!(uport
->flags
& UPF_FIXED_PORT
))
885 uport
->uartclk
= new_info
->baud_base
* 16;
886 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
887 (new_flags
& UPF_CHANGE_MASK
);
888 uport
->custom_divisor
= new_info
->custom_divisor
;
889 port
->close_delay
= close_delay
;
890 port
->closing_wait
= closing_wait
;
891 if (new_info
->xmit_fifo_size
)
892 uport
->fifosize
= new_info
->xmit_fifo_size
;
893 port
->low_latency
= (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
897 if (uport
->type
== PORT_UNKNOWN
)
899 if (port
->flags
& ASYNC_INITIALIZED
) {
900 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
901 old_custom_divisor
!= uport
->custom_divisor
) {
903 * If they're setting up a custom divisor or speed,
904 * instead of clearing it, then bitch about it. No
905 * need to rate-limit; it's CAP_SYS_ADMIN only.
907 if (uport
->flags
& UPF_SPD_MASK
) {
908 dev_notice(uport
->dev
,
909 "%s sets custom speed on %s. This is deprecated.\n",
911 tty_name(port
->tty
));
913 uart_change_speed(tty
, state
, NULL
);
916 retval
= uart_startup(tty
, state
, 1);
921 static int uart_set_info_user(struct tty_struct
*tty
, struct uart_state
*state
,
922 struct serial_struct __user
*newinfo
)
924 struct serial_struct new_serial
;
925 struct tty_port
*port
= &state
->port
;
928 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
932 * This semaphore protects port->count. It is also
933 * very useful to prevent opens. Also, take the
934 * port configuration semaphore to make sure that a
935 * module insertion/removal doesn't change anything
938 mutex_lock(&port
->mutex
);
939 retval
= uart_set_info(tty
, port
, state
, &new_serial
);
940 mutex_unlock(&port
->mutex
);
945 * uart_get_lsr_info - get line status register info
946 * @tty: tty associated with the UART
947 * @state: UART being queried
948 * @value: returned modem value
950 * Note: uart_ioctl protects us against hangups.
952 static int uart_get_lsr_info(struct tty_struct
*tty
,
953 struct uart_state
*state
, unsigned int __user
*value
)
955 struct uart_port
*uport
= state
->uart_port
;
958 result
= uport
->ops
->tx_empty(uport
);
961 * If we're about to load something into the transmit
962 * register, we'll pretend the transmitter isn't empty to
963 * avoid a race condition (depending on when the transmit
964 * interrupt happens).
967 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
968 !uart_tx_stopped(uport
)))
969 result
&= ~TIOCSER_TEMT
;
971 return put_user(result
, value
);
974 static int uart_tiocmget(struct tty_struct
*tty
)
976 struct uart_state
*state
= tty
->driver_data
;
977 struct tty_port
*port
= &state
->port
;
978 struct uart_port
*uport
= state
->uart_port
;
981 mutex_lock(&port
->mutex
);
982 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
983 result
= uport
->mctrl
;
984 spin_lock_irq(&uport
->lock
);
985 result
|= uport
->ops
->get_mctrl(uport
);
986 spin_unlock_irq(&uport
->lock
);
988 mutex_unlock(&port
->mutex
);
994 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
996 struct uart_state
*state
= tty
->driver_data
;
997 struct uart_port
*uport
= state
->uart_port
;
998 struct tty_port
*port
= &state
->port
;
1001 mutex_lock(&port
->mutex
);
1002 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
1003 uart_update_mctrl(uport
, set
, clear
);
1006 mutex_unlock(&port
->mutex
);
1010 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
1012 struct uart_state
*state
= tty
->driver_data
;
1013 struct tty_port
*port
= &state
->port
;
1014 struct uart_port
*uport
= state
->uart_port
;
1016 mutex_lock(&port
->mutex
);
1018 if (uport
->type
!= PORT_UNKNOWN
)
1019 uport
->ops
->break_ctl(uport
, break_state
);
1021 mutex_unlock(&port
->mutex
);
1025 static int uart_do_autoconfig(struct tty_struct
*tty
,struct uart_state
*state
)
1027 struct uart_port
*uport
= state
->uart_port
;
1028 struct tty_port
*port
= &state
->port
;
1031 if (!capable(CAP_SYS_ADMIN
))
1035 * Take the per-port semaphore. This prevents count from
1036 * changing, and hence any extra opens of the port while
1037 * we're auto-configuring.
1039 if (mutex_lock_interruptible(&port
->mutex
))
1040 return -ERESTARTSYS
;
1043 if (tty_port_users(port
) == 1) {
1044 uart_shutdown(tty
, state
);
1047 * If we already have a port type configured,
1048 * we must release its resources.
1050 if (uport
->type
!= PORT_UNKNOWN
)
1051 uport
->ops
->release_port(uport
);
1053 flags
= UART_CONFIG_TYPE
;
1054 if (uport
->flags
& UPF_AUTO_IRQ
)
1055 flags
|= UART_CONFIG_IRQ
;
1058 * This will claim the ports resources if
1061 uport
->ops
->config_port(uport
, flags
);
1063 ret
= uart_startup(tty
, state
, 1);
1065 mutex_unlock(&port
->mutex
);
1069 static void uart_enable_ms(struct uart_port
*uport
)
1072 * Force modem status interrupts on
1074 if (uport
->ops
->enable_ms
)
1075 uport
->ops
->enable_ms(uport
);
1079 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1080 * - mask passed in arg for lines of interest
1081 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1082 * Caller should use TIOCGICOUNT to see which one it was
1084 * FIXME: This wants extracting into a common all driver implementation
1085 * of TIOCMWAIT using tty_port.
1088 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
1090 struct uart_port
*uport
= state
->uart_port
;
1091 struct tty_port
*port
= &state
->port
;
1092 DECLARE_WAITQUEUE(wait
, current
);
1093 struct uart_icount cprev
, cnow
;
1097 * note the counters on entry
1099 spin_lock_irq(&uport
->lock
);
1100 memcpy(&cprev
, &uport
->icount
, sizeof(struct uart_icount
));
1101 uart_enable_ms(uport
);
1102 spin_unlock_irq(&uport
->lock
);
1104 add_wait_queue(&port
->delta_msr_wait
, &wait
);
1106 spin_lock_irq(&uport
->lock
);
1107 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1108 spin_unlock_irq(&uport
->lock
);
1110 set_current_state(TASK_INTERRUPTIBLE
);
1112 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1113 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1114 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1115 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1122 /* see if a signal did it */
1123 if (signal_pending(current
)) {
1130 __set_current_state(TASK_RUNNING
);
1131 remove_wait_queue(&port
->delta_msr_wait
, &wait
);
1137 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1138 * Return: write counters to the user passed counter struct
1139 * NB: both 1->0 and 0->1 transitions are counted except for
1140 * RI where only 0->1 is counted.
1142 static int uart_get_icount(struct tty_struct
*tty
,
1143 struct serial_icounter_struct
*icount
)
1145 struct uart_state
*state
= tty
->driver_data
;
1146 struct uart_icount cnow
;
1147 struct uart_port
*uport
= state
->uart_port
;
1149 spin_lock_irq(&uport
->lock
);
1150 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1151 spin_unlock_irq(&uport
->lock
);
1153 icount
->cts
= cnow
.cts
;
1154 icount
->dsr
= cnow
.dsr
;
1155 icount
->rng
= cnow
.rng
;
1156 icount
->dcd
= cnow
.dcd
;
1157 icount
->rx
= cnow
.rx
;
1158 icount
->tx
= cnow
.tx
;
1159 icount
->frame
= cnow
.frame
;
1160 icount
->overrun
= cnow
.overrun
;
1161 icount
->parity
= cnow
.parity
;
1162 icount
->brk
= cnow
.brk
;
1163 icount
->buf_overrun
= cnow
.buf_overrun
;
1168 static int uart_get_rs485_config(struct uart_port
*port
,
1169 struct serial_rs485 __user
*rs485
)
1171 unsigned long flags
;
1172 struct serial_rs485 aux
;
1174 spin_lock_irqsave(&port
->lock
, flags
);
1176 spin_unlock_irqrestore(&port
->lock
, flags
);
1178 if (copy_to_user(rs485
, &aux
, sizeof(aux
)))
1184 static int uart_set_rs485_config(struct uart_port
*port
,
1185 struct serial_rs485 __user
*rs485_user
)
1187 struct serial_rs485 rs485
;
1189 unsigned long flags
;
1191 if (!port
->rs485_config
)
1192 return -ENOIOCTLCMD
;
1194 if (copy_from_user(&rs485
, rs485_user
, sizeof(*rs485_user
)))
1197 spin_lock_irqsave(&port
->lock
, flags
);
1198 ret
= port
->rs485_config(port
, &rs485
);
1199 spin_unlock_irqrestore(&port
->lock
, flags
);
1203 if (copy_to_user(rs485_user
, &port
->rs485
, sizeof(port
->rs485
)))
1210 * Called via sys_ioctl. We can use spin_lock_irq() here.
1213 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1216 struct uart_state
*state
= tty
->driver_data
;
1217 struct tty_port
*port
= &state
->port
;
1218 void __user
*uarg
= (void __user
*)arg
;
1219 int ret
= -ENOIOCTLCMD
;
1223 * These ioctls don't rely on the hardware to be present.
1227 ret
= uart_get_info_user(port
, uarg
);
1231 down_write(&tty
->termios_rwsem
);
1232 ret
= uart_set_info_user(tty
, state
, uarg
);
1233 up_write(&tty
->termios_rwsem
);
1237 down_write(&tty
->termios_rwsem
);
1238 ret
= uart_do_autoconfig(tty
, state
);
1239 up_write(&tty
->termios_rwsem
);
1242 case TIOCSERGWILD
: /* obsolete */
1243 case TIOCSERSWILD
: /* obsolete */
1248 if (ret
!= -ENOIOCTLCMD
)
1251 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1257 * The following should only be used when hardware is present.
1261 ret
= uart_wait_modem_status(state
, arg
);
1265 if (ret
!= -ENOIOCTLCMD
)
1268 mutex_lock(&port
->mutex
);
1270 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1276 * All these rely on hardware being present and need to be
1277 * protected against the tty being hung up.
1281 case TIOCSERGETLSR
: /* Get line status register */
1282 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1286 ret
= uart_get_rs485_config(state
->uart_port
, uarg
);
1290 ret
= uart_set_rs485_config(state
->uart_port
, uarg
);
1293 struct uart_port
*uport
= state
->uart_port
;
1294 if (uport
->ops
->ioctl
)
1295 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1300 mutex_unlock(&port
->mutex
);
1305 static void uart_set_ldisc(struct tty_struct
*tty
)
1307 struct uart_state
*state
= tty
->driver_data
;
1308 struct uart_port
*uport
= state
->uart_port
;
1310 if (uport
->ops
->set_ldisc
) {
1311 mutex_lock(&state
->port
.mutex
);
1312 uport
->ops
->set_ldisc(uport
, &tty
->termios
);
1313 mutex_unlock(&state
->port
.mutex
);
1317 static void uart_set_termios(struct tty_struct
*tty
,
1318 struct ktermios
*old_termios
)
1320 struct uart_state
*state
= tty
->driver_data
;
1321 struct uart_port
*uport
= state
->uart_port
;
1322 unsigned int cflag
= tty
->termios
.c_cflag
;
1323 unsigned int iflag_mask
= IGNBRK
|BRKINT
|IGNPAR
|PARMRK
|INPCK
;
1324 bool sw_changed
= false;
1327 * Drivers doing software flow control also need to know
1328 * about changes to these input settings.
1330 if (uport
->flags
& UPF_SOFT_FLOW
) {
1331 iflag_mask
|= IXANY
|IXON
|IXOFF
;
1333 tty
->termios
.c_cc
[VSTART
] != old_termios
->c_cc
[VSTART
] ||
1334 tty
->termios
.c_cc
[VSTOP
] != old_termios
->c_cc
[VSTOP
];
1338 * These are the bits that are used to setup various
1339 * flags in the low level driver. We can ignore the Bfoo
1340 * bits in c_cflag; c_[io]speed will always be set
1341 * appropriately by set_termios() in tty_ioctl.c
1343 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1344 tty
->termios
.c_ospeed
== old_termios
->c_ospeed
&&
1345 tty
->termios
.c_ispeed
== old_termios
->c_ispeed
&&
1346 ((tty
->termios
.c_iflag
^ old_termios
->c_iflag
) & iflag_mask
) == 0 &&
1351 mutex_lock(&state
->port
.mutex
);
1352 uart_change_speed(tty
, state
, old_termios
);
1353 mutex_unlock(&state
->port
.mutex
);
1354 /* reload cflag from termios; port driver may have overriden flags */
1355 cflag
= tty
->termios
.c_cflag
;
1357 /* Handle transition to B0 status */
1358 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1359 uart_clear_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
1360 /* Handle transition away from B0 status */
1361 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1362 unsigned int mask
= TIOCM_DTR
;
1363 if (!(cflag
& CRTSCTS
) || !test_bit(TTY_THROTTLED
, &tty
->flags
))
1365 uart_set_mctrl(uport
, mask
);
1370 * Calls to uart_close() are serialised via the tty_lock in
1371 * drivers/tty/tty_io.c:tty_release()
1372 * drivers/tty/tty_io.c:do_tty_hangup()
1373 * This runs from a workqueue and can sleep for a _short_ time only.
1375 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1377 struct uart_state
*state
= tty
->driver_data
;
1378 struct tty_port
*port
;
1379 struct uart_port
*uport
;
1382 struct uart_driver
*drv
= tty
->driver
->driver_state
;
1384 state
= drv
->state
+ tty
->index
;
1385 port
= &state
->port
;
1386 spin_lock_irq(&port
->lock
);
1388 spin_unlock_irq(&port
->lock
);
1392 uport
= state
->uart_port
;
1393 port
= &state
->port
;
1395 pr_debug("uart_close(%d) called\n", uport
? uport
->line
: -1);
1397 if (!port
->count
|| tty_port_close_start(port
, tty
, filp
) == 0)
1401 * At this point, we stop accepting input. To do this, we
1402 * disable the receive line status interrupts.
1404 if (port
->flags
& ASYNC_INITIALIZED
) {
1405 spin_lock_irq(&uport
->lock
);
1406 uport
->ops
->stop_rx(uport
);
1407 spin_unlock_irq(&uport
->lock
);
1409 * Before we drop DTR, make sure the UART transmitter
1410 * has completely drained; this is especially
1411 * important if there is a transmit FIFO!
1413 uart_wait_until_sent(tty
, uport
->timeout
);
1416 mutex_lock(&port
->mutex
);
1417 uart_shutdown(tty
, state
);
1418 tty_port_tty_set(port
, NULL
);
1420 spin_lock_irq(&port
->lock
);
1422 if (port
->blocked_open
) {
1423 spin_unlock_irq(&port
->lock
);
1424 if (port
->close_delay
)
1425 msleep_interruptible(jiffies_to_msecs(port
->close_delay
));
1426 spin_lock_irq(&port
->lock
);
1427 } else if (!uart_console(uport
)) {
1428 spin_unlock_irq(&port
->lock
);
1429 uart_change_pm(state
, UART_PM_STATE_OFF
);
1430 spin_lock_irq(&port
->lock
);
1434 * Wake up anyone trying to open this port.
1436 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1437 clear_bit(ASYNCB_CLOSING
, &port
->flags
);
1438 spin_unlock_irq(&port
->lock
);
1439 wake_up_interruptible(&port
->open_wait
);
1441 mutex_unlock(&port
->mutex
);
1443 tty_ldisc_flush(tty
);
1447 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1449 struct uart_state
*state
= tty
->driver_data
;
1450 struct uart_port
*port
= state
->uart_port
;
1451 unsigned long char_time
, expire
;
1453 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
1457 * Set the check interval to be 1/5 of the estimated time to
1458 * send a single character, and make it at least 1. The check
1459 * interval should also be less than the timeout.
1461 * Note: we have to use pretty tight timings here to satisfy
1464 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1465 char_time
= char_time
/ 5;
1468 if (timeout
&& timeout
< char_time
)
1469 char_time
= timeout
;
1472 * If the transmitter hasn't cleared in twice the approximate
1473 * amount of time to send the entire FIFO, it probably won't
1474 * ever clear. This assumes the UART isn't doing flow
1475 * control, which is currently the case. Hence, if it ever
1476 * takes longer than port->timeout, this is probably due to a
1477 * UART bug of some kind. So, we clamp the timeout parameter at
1480 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1481 timeout
= 2 * port
->timeout
;
1483 expire
= jiffies
+ timeout
;
1485 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1486 port
->line
, jiffies
, expire
);
1489 * Check whether the transmitter is empty every 'char_time'.
1490 * 'timeout' / 'expire' give us the maximum amount of time
1493 while (!port
->ops
->tx_empty(port
)) {
1494 msleep_interruptible(jiffies_to_msecs(char_time
));
1495 if (signal_pending(current
))
1497 if (time_after(jiffies
, expire
))
1503 * Calls to uart_hangup() are serialised by the tty_lock in
1504 * drivers/tty/tty_io.c:do_tty_hangup()
1505 * This runs from a workqueue and can sleep for a _short_ time only.
1507 static void uart_hangup(struct tty_struct
*tty
)
1509 struct uart_state
*state
= tty
->driver_data
;
1510 struct tty_port
*port
= &state
->port
;
1511 unsigned long flags
;
1513 pr_debug("uart_hangup(%d)\n", state
->uart_port
->line
);
1515 mutex_lock(&port
->mutex
);
1516 if (port
->flags
& ASYNC_NORMAL_ACTIVE
) {
1517 uart_flush_buffer(tty
);
1518 uart_shutdown(tty
, state
);
1519 spin_lock_irqsave(&port
->lock
, flags
);
1521 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1522 spin_unlock_irqrestore(&port
->lock
, flags
);
1523 tty_port_tty_set(port
, NULL
);
1524 if (!uart_console(state
->uart_port
))
1525 uart_change_pm(state
, UART_PM_STATE_OFF
);
1526 wake_up_interruptible(&port
->open_wait
);
1527 wake_up_interruptible(&port
->delta_msr_wait
);
1529 mutex_unlock(&port
->mutex
);
1532 static void uart_port_shutdown(struct tty_port
*port
)
1534 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1535 struct uart_port
*uport
= state
->uart_port
;
1538 * clear delta_msr_wait queue to avoid mem leaks: we may free
1539 * the irq here so the queue might never be woken up. Note
1540 * that we won't end up waiting on delta_msr_wait again since
1541 * any outstanding file descriptors should be pointing at
1542 * hung_up_tty_fops now.
1544 wake_up_interruptible(&port
->delta_msr_wait
);
1547 * Free the IRQ and disable the port.
1549 uport
->ops
->shutdown(uport
);
1552 * Ensure that the IRQ handler isn't running on another CPU.
1554 synchronize_irq(uport
->irq
);
1557 static int uart_carrier_raised(struct tty_port
*port
)
1559 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1560 struct uart_port
*uport
= state
->uart_port
;
1562 spin_lock_irq(&uport
->lock
);
1563 uart_enable_ms(uport
);
1564 mctrl
= uport
->ops
->get_mctrl(uport
);
1565 spin_unlock_irq(&uport
->lock
);
1566 if (mctrl
& TIOCM_CAR
)
1571 static void uart_dtr_rts(struct tty_port
*port
, int onoff
)
1573 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1574 struct uart_port
*uport
= state
->uart_port
;
1577 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1579 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1583 * Calls to uart_open are serialised by the tty_lock in
1584 * drivers/tty/tty_io.c:tty_open()
1585 * Note that if this fails, then uart_close() _will_ be called.
1587 * In time, we want to scrap the "opening nonpresent ports"
1588 * behaviour and implement an alternative way for setserial
1589 * to set base addresses/ports/types. This will allow us to
1590 * get rid of a certain amount of extra tests.
1592 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1594 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1595 int retval
, line
= tty
->index
;
1596 struct uart_state
*state
= drv
->state
+ line
;
1597 struct tty_port
*port
= &state
->port
;
1599 pr_debug("uart_open(%d) called\n", line
);
1601 spin_lock_irq(&port
->lock
);
1603 spin_unlock_irq(&port
->lock
);
1606 * We take the semaphore here to guarantee that we won't be re-entered
1607 * while allocating the state structure, or while we request any IRQs
1608 * that the driver may need. This also has the nice side-effect that
1609 * it delays the action of uart_hangup, so we can guarantee that
1610 * state->port.tty will always contain something reasonable.
1612 if (mutex_lock_interruptible(&port
->mutex
)) {
1613 retval
= -ERESTARTSYS
;
1617 if (!state
->uart_port
|| state
->uart_port
->flags
& UPF_DEAD
) {
1622 tty
->driver_data
= state
;
1623 state
->uart_port
->state
= state
;
1624 state
->port
.low_latency
=
1625 (state
->uart_port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1626 tty_port_tty_set(port
, tty
);
1629 * Start up the serial port.
1631 retval
= uart_startup(tty
, state
, 0);
1634 * If we succeeded, wait until the port is ready.
1636 mutex_unlock(&port
->mutex
);
1638 retval
= tty_port_block_til_ready(port
, tty
, filp
);
1643 mutex_unlock(&port
->mutex
);
1647 static const char *uart_type(struct uart_port
*port
)
1649 const char *str
= NULL
;
1651 if (port
->ops
->type
)
1652 str
= port
->ops
->type(port
);
1660 #ifdef CONFIG_PROC_FS
1662 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1664 struct uart_state
*state
= drv
->state
+ i
;
1665 struct tty_port
*port
= &state
->port
;
1666 enum uart_pm_state pm_state
;
1667 struct uart_port
*uport
= state
->uart_port
;
1669 unsigned int status
;
1675 mmio
= uport
->iotype
>= UPIO_MEM
;
1676 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1677 uport
->line
, uart_type(uport
),
1678 mmio
? "mmio:0x" : "port:",
1679 mmio
? (unsigned long long)uport
->mapbase
1680 : (unsigned long long)uport
->iobase
,
1683 if (uport
->type
== PORT_UNKNOWN
) {
1688 if (capable(CAP_SYS_ADMIN
)) {
1689 mutex_lock(&port
->mutex
);
1690 pm_state
= state
->pm_state
;
1691 if (pm_state
!= UART_PM_STATE_ON
)
1692 uart_change_pm(state
, UART_PM_STATE_ON
);
1693 spin_lock_irq(&uport
->lock
);
1694 status
= uport
->ops
->get_mctrl(uport
);
1695 spin_unlock_irq(&uport
->lock
);
1696 if (pm_state
!= UART_PM_STATE_ON
)
1697 uart_change_pm(state
, pm_state
);
1698 mutex_unlock(&port
->mutex
);
1700 seq_printf(m
, " tx:%d rx:%d",
1701 uport
->icount
.tx
, uport
->icount
.rx
);
1702 if (uport
->icount
.frame
)
1703 seq_printf(m
, " fe:%d",
1704 uport
->icount
.frame
);
1705 if (uport
->icount
.parity
)
1706 seq_printf(m
, " pe:%d",
1707 uport
->icount
.parity
);
1708 if (uport
->icount
.brk
)
1709 seq_printf(m
, " brk:%d",
1711 if (uport
->icount
.overrun
)
1712 seq_printf(m
, " oe:%d",
1713 uport
->icount
.overrun
);
1715 #define INFOBIT(bit, str) \
1716 if (uport->mctrl & (bit)) \
1717 strncat(stat_buf, (str), sizeof(stat_buf) - \
1718 strlen(stat_buf) - 2)
1719 #define STATBIT(bit, str) \
1720 if (status & (bit)) \
1721 strncat(stat_buf, (str), sizeof(stat_buf) - \
1722 strlen(stat_buf) - 2)
1726 INFOBIT(TIOCM_RTS
, "|RTS");
1727 STATBIT(TIOCM_CTS
, "|CTS");
1728 INFOBIT(TIOCM_DTR
, "|DTR");
1729 STATBIT(TIOCM_DSR
, "|DSR");
1730 STATBIT(TIOCM_CAR
, "|CD");
1731 STATBIT(TIOCM_RNG
, "|RI");
1735 seq_puts(m
, stat_buf
);
1742 static int uart_proc_show(struct seq_file
*m
, void *v
)
1744 struct tty_driver
*ttydrv
= m
->private;
1745 struct uart_driver
*drv
= ttydrv
->driver_state
;
1748 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
1750 for (i
= 0; i
< drv
->nr
; i
++)
1751 uart_line_info(m
, drv
, i
);
1755 static int uart_proc_open(struct inode
*inode
, struct file
*file
)
1757 return single_open(file
, uart_proc_show
, PDE_DATA(inode
));
1760 static const struct file_operations uart_proc_fops
= {
1761 .owner
= THIS_MODULE
,
1762 .open
= uart_proc_open
,
1764 .llseek
= seq_lseek
,
1765 .release
= single_release
,
1769 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1771 * uart_console_write - write a console message to a serial port
1772 * @port: the port to write the message
1773 * @s: array of characters
1774 * @count: number of characters in string to write
1775 * @putchar: function to write character to port
1777 void uart_console_write(struct uart_port
*port
, const char *s
,
1779 void (*putchar
)(struct uart_port
*, int))
1783 for (i
= 0; i
< count
; i
++, s
++) {
1785 putchar(port
, '\r');
1789 EXPORT_SYMBOL_GPL(uart_console_write
);
1792 * Check whether an invalid uart number has been specified, and
1793 * if so, search for the first available port that does have
1796 struct uart_port
* __init
1797 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1799 int idx
= co
->index
;
1801 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1802 ports
[idx
].membase
== NULL
))
1803 for (idx
= 0; idx
< nr
; idx
++)
1804 if (ports
[idx
].iobase
!= 0 ||
1805 ports
[idx
].membase
!= NULL
)
1814 * uart_parse_earlycon - Parse earlycon options
1815 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1816 * @iotype: ptr for decoded iotype (out)
1817 * @addr: ptr for decoded mapbase/iobase (out)
1818 * @options: ptr for <options> field; NULL if not present (out)
1820 * Decodes earlycon kernel command line parameters of the form
1821 * earlycon=<name>,io|mmio|mmio32|mmio32be|mmio32native,<addr>,<options>
1822 * console=<name>,io|mmio|mmio32|mmio32be|mmio32native,<addr>,<options>
1825 * earlycon=<name>,0x<addr>,<options>
1826 * console=<name>,0x<addr>,<options>
1827 * is also accepted; the returned @iotype will be UPIO_MEM.
1829 * Returns 0 on success or -EINVAL on failure
1831 int uart_parse_earlycon(char *p
, unsigned char *iotype
, unsigned long *addr
,
1834 if (strncmp(p
, "mmio,", 5) == 0) {
1837 } else if (strncmp(p
, "mmio32,", 7) == 0) {
1838 *iotype
= UPIO_MEM32
;
1840 } else if (strncmp(p
, "mmio32be,", 9) == 0) {
1841 *iotype
= UPIO_MEM32BE
;
1843 } else if (strncmp(p
, "mmio32native,", 13) == 0) {
1844 *iotype
= IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) ?
1845 UPIO_MEM32BE
: UPIO_MEM32
;
1847 } else if (strncmp(p
, "io,", 3) == 0) {
1848 *iotype
= UPIO_PORT
;
1850 } else if (strncmp(p
, "0x", 2) == 0) {
1856 *addr
= simple_strtoul(p
, NULL
, 0);
1864 EXPORT_SYMBOL_GPL(uart_parse_earlycon
);
1867 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1868 * @options: pointer to option string
1869 * @baud: pointer to an 'int' variable for the baud rate.
1870 * @parity: pointer to an 'int' variable for the parity.
1871 * @bits: pointer to an 'int' variable for the number of data bits.
1872 * @flow: pointer to an 'int' variable for the flow control character.
1874 * uart_parse_options decodes a string containing the serial console
1875 * options. The format of the string is <baud><parity><bits><flow>,
1879 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1883 *baud
= simple_strtoul(s
, NULL
, 10);
1884 while (*s
>= '0' && *s
<= '9')
1893 EXPORT_SYMBOL_GPL(uart_parse_options
);
1900 static const struct baud_rates baud_rates
[] = {
1901 { 921600, B921600
},
1902 { 460800, B460800
},
1903 { 230400, B230400
},
1904 { 115200, B115200
},
1916 * uart_set_options - setup the serial console parameters
1917 * @port: pointer to the serial ports uart_port structure
1918 * @co: console pointer
1920 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1921 * @bits: number of data bits
1922 * @flow: flow control character - 'r' (rts)
1925 uart_set_options(struct uart_port
*port
, struct console
*co
,
1926 int baud
, int parity
, int bits
, int flow
)
1928 struct ktermios termios
;
1929 static struct ktermios dummy
;
1933 * Ensure that the serial console lock is initialised
1935 * If this port is a console, then the spinlock is already
1938 if (!(uart_console(port
) && (port
->cons
->flags
& CON_ENABLED
))) {
1939 spin_lock_init(&port
->lock
);
1940 lockdep_set_class(&port
->lock
, &port_lock_key
);
1943 memset(&termios
, 0, sizeof(struct ktermios
));
1945 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1948 * Construct a cflag setting.
1950 for (i
= 0; baud_rates
[i
].rate
; i
++)
1951 if (baud_rates
[i
].rate
<= baud
)
1954 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1957 termios
.c_cflag
|= CS7
;
1959 termios
.c_cflag
|= CS8
;
1963 termios
.c_cflag
|= PARODD
;
1966 termios
.c_cflag
|= PARENB
;
1971 termios
.c_cflag
|= CRTSCTS
;
1974 * some uarts on other side don't support no flow control.
1975 * So we set * DTR in host uart to make them happy
1977 port
->mctrl
|= TIOCM_DTR
;
1979 port
->ops
->set_termios(port
, &termios
, &dummy
);
1981 * Allow the setting of the UART parameters with a NULL console
1985 co
->cflag
= termios
.c_cflag
;
1989 EXPORT_SYMBOL_GPL(uart_set_options
);
1990 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1993 * uart_change_pm - set power state of the port
1995 * @state: port descriptor
1996 * @pm_state: new state
1998 * Locking: port->mutex has to be held
2000 static void uart_change_pm(struct uart_state
*state
,
2001 enum uart_pm_state pm_state
)
2003 struct uart_port
*port
= state
->uart_port
;
2005 if (state
->pm_state
!= pm_state
) {
2007 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
2008 state
->pm_state
= pm_state
;
2013 struct uart_port
*port
;
2014 struct uart_driver
*driver
;
2017 static int serial_match_port(struct device
*dev
, void *data
)
2019 struct uart_match
*match
= data
;
2020 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
2021 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
2024 return dev
->devt
== devt
; /* Actually, only one tty per port */
2027 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2029 struct uart_state
*state
= drv
->state
+ uport
->line
;
2030 struct tty_port
*port
= &state
->port
;
2031 struct device
*tty_dev
;
2032 struct uart_match match
= {uport
, drv
};
2034 mutex_lock(&port
->mutex
);
2036 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2037 if (device_may_wakeup(tty_dev
)) {
2038 if (!enable_irq_wake(uport
->irq
))
2039 uport
->irq_wake
= 1;
2040 put_device(tty_dev
);
2041 mutex_unlock(&port
->mutex
);
2044 put_device(tty_dev
);
2046 /* Nothing to do if the console is not suspending */
2047 if (!console_suspend_enabled
&& uart_console(uport
))
2050 uport
->suspended
= 1;
2052 if (port
->flags
& ASYNC_INITIALIZED
) {
2053 const struct uart_ops
*ops
= uport
->ops
;
2056 set_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2057 clear_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2059 spin_lock_irq(&uport
->lock
);
2060 ops
->stop_tx(uport
);
2061 ops
->set_mctrl(uport
, 0);
2062 ops
->stop_rx(uport
);
2063 spin_unlock_irq(&uport
->lock
);
2066 * Wait for the transmitter to empty.
2068 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
2071 dev_err(uport
->dev
, "%s%d: Unable to drain transmitter\n",
2073 drv
->tty_driver
->name_base
+ uport
->line
);
2075 ops
->shutdown(uport
);
2079 * Disable the console device before suspending.
2081 if (uart_console(uport
))
2082 console_stop(uport
->cons
);
2084 uart_change_pm(state
, UART_PM_STATE_OFF
);
2086 mutex_unlock(&port
->mutex
);
2091 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2093 struct uart_state
*state
= drv
->state
+ uport
->line
;
2094 struct tty_port
*port
= &state
->port
;
2095 struct device
*tty_dev
;
2096 struct uart_match match
= {uport
, drv
};
2097 struct ktermios termios
;
2099 mutex_lock(&port
->mutex
);
2101 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2102 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
2103 if (uport
->irq_wake
) {
2104 disable_irq_wake(uport
->irq
);
2105 uport
->irq_wake
= 0;
2107 put_device(tty_dev
);
2108 mutex_unlock(&port
->mutex
);
2111 put_device(tty_dev
);
2112 uport
->suspended
= 0;
2115 * Re-enable the console device after suspending.
2117 if (uart_console(uport
)) {
2119 * First try to use the console cflag setting.
2121 memset(&termios
, 0, sizeof(struct ktermios
));
2122 termios
.c_cflag
= uport
->cons
->cflag
;
2125 * If that's unset, use the tty termios setting.
2127 if (port
->tty
&& termios
.c_cflag
== 0)
2128 termios
= port
->tty
->termios
;
2130 if (console_suspend_enabled
)
2131 uart_change_pm(state
, UART_PM_STATE_ON
);
2132 uport
->ops
->set_termios(uport
, &termios
, NULL
);
2133 if (console_suspend_enabled
)
2134 console_start(uport
->cons
);
2137 if (port
->flags
& ASYNC_SUSPENDED
) {
2138 const struct uart_ops
*ops
= uport
->ops
;
2141 uart_change_pm(state
, UART_PM_STATE_ON
);
2142 spin_lock_irq(&uport
->lock
);
2143 ops
->set_mctrl(uport
, 0);
2144 spin_unlock_irq(&uport
->lock
);
2145 if (console_suspend_enabled
|| !uart_console(uport
)) {
2146 /* Protected by port mutex for now */
2147 struct tty_struct
*tty
= port
->tty
;
2148 ret
= ops
->startup(uport
);
2151 uart_change_speed(tty
, state
, NULL
);
2152 spin_lock_irq(&uport
->lock
);
2153 ops
->set_mctrl(uport
, uport
->mctrl
);
2154 ops
->start_tx(uport
);
2155 spin_unlock_irq(&uport
->lock
);
2156 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2159 * Failed to resume - maybe hardware went away?
2160 * Clear the "initialized" flag so we won't try
2161 * to call the low level drivers shutdown method.
2163 uart_shutdown(tty
, state
);
2167 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2170 mutex_unlock(&port
->mutex
);
2176 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2180 switch (port
->iotype
) {
2182 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2185 snprintf(address
, sizeof(address
),
2186 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2193 snprintf(address
, sizeof(address
),
2194 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2197 strlcpy(address
, "*unknown*", sizeof(address
));
2201 printk(KERN_INFO
"%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2202 port
->dev
? dev_name(port
->dev
) : "",
2203 port
->dev
? ": " : "",
2205 drv
->tty_driver
->name_base
+ port
->line
,
2206 address
, port
->irq
, port
->uartclk
/ 16, uart_type(port
));
2210 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2211 struct uart_port
*port
)
2216 * If there isn't a port here, don't do anything further.
2218 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2222 * Now do the auto configuration stuff. Note that config_port
2223 * is expected to claim the resources and map the port for us.
2226 if (port
->flags
& UPF_AUTO_IRQ
)
2227 flags
|= UART_CONFIG_IRQ
;
2228 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2229 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2230 port
->type
= PORT_UNKNOWN
;
2231 flags
|= UART_CONFIG_TYPE
;
2233 port
->ops
->config_port(port
, flags
);
2236 if (port
->type
!= PORT_UNKNOWN
) {
2237 unsigned long flags
;
2239 uart_report_port(drv
, port
);
2241 /* Power up port for set_mctrl() */
2242 uart_change_pm(state
, UART_PM_STATE_ON
);
2245 * Ensure that the modem control lines are de-activated.
2246 * keep the DTR setting that is set in uart_set_options()
2247 * We probably don't need a spinlock around this, but
2249 spin_lock_irqsave(&port
->lock
, flags
);
2250 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2251 spin_unlock_irqrestore(&port
->lock
, flags
);
2254 * If this driver supports console, and it hasn't been
2255 * successfully registered yet, try to re-register it.
2256 * It may be that the port was not available.
2258 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2259 register_console(port
->cons
);
2262 * Power down all ports by default, except the
2263 * console if we have one.
2265 if (!uart_console(port
))
2266 uart_change_pm(state
, UART_PM_STATE_OFF
);
2270 #ifdef CONFIG_CONSOLE_POLL
2272 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2274 struct uart_driver
*drv
= driver
->driver_state
;
2275 struct uart_state
*state
= drv
->state
+ line
;
2276 struct uart_port
*port
;
2283 if (!state
|| !state
->uart_port
)
2286 port
= state
->uart_port
;
2287 if (!(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
))
2290 if (port
->ops
->poll_init
) {
2291 struct tty_port
*tport
= &state
->port
;
2294 mutex_lock(&tport
->mutex
);
2296 * We don't set ASYNCB_INITIALIZED as we only initialized the
2297 * hw, e.g. state->xmit is still uninitialized.
2299 if (!test_bit(ASYNCB_INITIALIZED
, &tport
->flags
))
2300 ret
= port
->ops
->poll_init(port
);
2301 mutex_unlock(&tport
->mutex
);
2307 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2308 return uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2314 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2316 struct uart_driver
*drv
= driver
->driver_state
;
2317 struct uart_state
*state
= drv
->state
+ line
;
2318 struct uart_port
*port
;
2320 if (!state
|| !state
->uart_port
)
2323 port
= state
->uart_port
;
2324 return port
->ops
->poll_get_char(port
);
2327 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2329 struct uart_driver
*drv
= driver
->driver_state
;
2330 struct uart_state
*state
= drv
->state
+ line
;
2331 struct uart_port
*port
;
2333 if (!state
|| !state
->uart_port
)
2336 port
= state
->uart_port
;
2339 port
->ops
->poll_put_char(port
, '\r');
2340 port
->ops
->poll_put_char(port
, ch
);
2344 static const struct tty_operations uart_ops
= {
2346 .close
= uart_close
,
2347 .write
= uart_write
,
2348 .put_char
= uart_put_char
,
2349 .flush_chars
= uart_flush_chars
,
2350 .write_room
= uart_write_room
,
2351 .chars_in_buffer
= uart_chars_in_buffer
,
2352 .flush_buffer
= uart_flush_buffer
,
2353 .ioctl
= uart_ioctl
,
2354 .throttle
= uart_throttle
,
2355 .unthrottle
= uart_unthrottle
,
2356 .send_xchar
= uart_send_xchar
,
2357 .set_termios
= uart_set_termios
,
2358 .set_ldisc
= uart_set_ldisc
,
2360 .start
= uart_start
,
2361 .hangup
= uart_hangup
,
2362 .break_ctl
= uart_break_ctl
,
2363 .wait_until_sent
= uart_wait_until_sent
,
2364 #ifdef CONFIG_PROC_FS
2365 .proc_fops
= &uart_proc_fops
,
2367 .tiocmget
= uart_tiocmget
,
2368 .tiocmset
= uart_tiocmset
,
2369 .get_icount
= uart_get_icount
,
2370 #ifdef CONFIG_CONSOLE_POLL
2371 .poll_init
= uart_poll_init
,
2372 .poll_get_char
= uart_poll_get_char
,
2373 .poll_put_char
= uart_poll_put_char
,
2377 static const struct tty_port_operations uart_port_ops
= {
2378 .carrier_raised
= uart_carrier_raised
,
2379 .dtr_rts
= uart_dtr_rts
,
2383 * uart_register_driver - register a driver with the uart core layer
2384 * @drv: low level driver structure
2386 * Register a uart driver with the core driver. We in turn register
2387 * with the tty layer, and initialise the core driver per-port state.
2389 * We have a proc file in /proc/tty/driver which is named after the
2392 * drv->port should be NULL, and the per-port structures should be
2393 * registered using uart_add_one_port after this call has succeeded.
2395 int uart_register_driver(struct uart_driver
*drv
)
2397 struct tty_driver
*normal
;
2403 * Maybe we should be using a slab cache for this, especially if
2404 * we have a large number of ports to handle.
2406 drv
->state
= kzalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2410 normal
= alloc_tty_driver(drv
->nr
);
2414 drv
->tty_driver
= normal
;
2416 normal
->driver_name
= drv
->driver_name
;
2417 normal
->name
= drv
->dev_name
;
2418 normal
->major
= drv
->major
;
2419 normal
->minor_start
= drv
->minor
;
2420 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2421 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2422 normal
->init_termios
= tty_std_termios
;
2423 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2424 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2425 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2426 normal
->driver_state
= drv
;
2427 tty_set_operations(normal
, &uart_ops
);
2430 * Initialise the UART state(s).
2432 for (i
= 0; i
< drv
->nr
; i
++) {
2433 struct uart_state
*state
= drv
->state
+ i
;
2434 struct tty_port
*port
= &state
->port
;
2436 tty_port_init(port
);
2437 port
->ops
= &uart_port_ops
;
2440 retval
= tty_register_driver(normal
);
2444 for (i
= 0; i
< drv
->nr
; i
++)
2445 tty_port_destroy(&drv
->state
[i
].port
);
2446 put_tty_driver(normal
);
2454 * uart_unregister_driver - remove a driver from the uart core layer
2455 * @drv: low level driver structure
2457 * Remove all references to a driver from the core driver. The low
2458 * level driver must have removed all its ports via the
2459 * uart_remove_one_port() if it registered them with uart_add_one_port().
2460 * (ie, drv->port == NULL)
2462 void uart_unregister_driver(struct uart_driver
*drv
)
2464 struct tty_driver
*p
= drv
->tty_driver
;
2467 tty_unregister_driver(p
);
2469 for (i
= 0; i
< drv
->nr
; i
++)
2470 tty_port_destroy(&drv
->state
[i
].port
);
2473 drv
->tty_driver
= NULL
;
2476 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2478 struct uart_driver
*p
= co
->data
;
2480 return p
->tty_driver
;
2483 static ssize_t
uart_get_attr_uartclk(struct device
*dev
,
2484 struct device_attribute
*attr
, char *buf
)
2486 struct serial_struct tmp
;
2487 struct tty_port
*port
= dev_get_drvdata(dev
);
2489 uart_get_info(port
, &tmp
);
2490 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.baud_base
* 16);
2493 static ssize_t
uart_get_attr_type(struct device
*dev
,
2494 struct device_attribute
*attr
, char *buf
)
2496 struct serial_struct tmp
;
2497 struct tty_port
*port
= dev_get_drvdata(dev
);
2499 uart_get_info(port
, &tmp
);
2500 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.type
);
2502 static ssize_t
uart_get_attr_line(struct device
*dev
,
2503 struct device_attribute
*attr
, char *buf
)
2505 struct serial_struct tmp
;
2506 struct tty_port
*port
= dev_get_drvdata(dev
);
2508 uart_get_info(port
, &tmp
);
2509 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.line
);
2512 static ssize_t
uart_get_attr_port(struct device
*dev
,
2513 struct device_attribute
*attr
, char *buf
)
2515 struct serial_struct tmp
;
2516 struct tty_port
*port
= dev_get_drvdata(dev
);
2517 unsigned long ioaddr
;
2519 uart_get_info(port
, &tmp
);
2521 if (HIGH_BITS_OFFSET
)
2522 ioaddr
|= (unsigned long)tmp
.port_high
<< HIGH_BITS_OFFSET
;
2523 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", ioaddr
);
2526 static ssize_t
uart_get_attr_irq(struct device
*dev
,
2527 struct device_attribute
*attr
, char *buf
)
2529 struct serial_struct tmp
;
2530 struct tty_port
*port
= dev_get_drvdata(dev
);
2532 uart_get_info(port
, &tmp
);
2533 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.irq
);
2536 static ssize_t
uart_get_attr_flags(struct device
*dev
,
2537 struct device_attribute
*attr
, char *buf
)
2539 struct serial_struct tmp
;
2540 struct tty_port
*port
= dev_get_drvdata(dev
);
2542 uart_get_info(port
, &tmp
);
2543 return snprintf(buf
, PAGE_SIZE
, "0x%X\n", tmp
.flags
);
2546 static ssize_t
uart_get_attr_xmit_fifo_size(struct device
*dev
,
2547 struct device_attribute
*attr
, char *buf
)
2549 struct serial_struct tmp
;
2550 struct tty_port
*port
= dev_get_drvdata(dev
);
2552 uart_get_info(port
, &tmp
);
2553 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.xmit_fifo_size
);
2557 static ssize_t
uart_get_attr_close_delay(struct device
*dev
,
2558 struct device_attribute
*attr
, char *buf
)
2560 struct serial_struct tmp
;
2561 struct tty_port
*port
= dev_get_drvdata(dev
);
2563 uart_get_info(port
, &tmp
);
2564 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.close_delay
);
2568 static ssize_t
uart_get_attr_closing_wait(struct device
*dev
,
2569 struct device_attribute
*attr
, char *buf
)
2571 struct serial_struct tmp
;
2572 struct tty_port
*port
= dev_get_drvdata(dev
);
2574 uart_get_info(port
, &tmp
);
2575 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.closing_wait
);
2578 static ssize_t
uart_get_attr_custom_divisor(struct device
*dev
,
2579 struct device_attribute
*attr
, char *buf
)
2581 struct serial_struct tmp
;
2582 struct tty_port
*port
= dev_get_drvdata(dev
);
2584 uart_get_info(port
, &tmp
);
2585 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.custom_divisor
);
2588 static ssize_t
uart_get_attr_io_type(struct device
*dev
,
2589 struct device_attribute
*attr
, char *buf
)
2591 struct serial_struct tmp
;
2592 struct tty_port
*port
= dev_get_drvdata(dev
);
2594 uart_get_info(port
, &tmp
);
2595 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.io_type
);
2598 static ssize_t
uart_get_attr_iomem_base(struct device
*dev
,
2599 struct device_attribute
*attr
, char *buf
)
2601 struct serial_struct tmp
;
2602 struct tty_port
*port
= dev_get_drvdata(dev
);
2604 uart_get_info(port
, &tmp
);
2605 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", (unsigned long)tmp
.iomem_base
);
2608 static ssize_t
uart_get_attr_iomem_reg_shift(struct device
*dev
,
2609 struct device_attribute
*attr
, char *buf
)
2611 struct serial_struct tmp
;
2612 struct tty_port
*port
= dev_get_drvdata(dev
);
2614 uart_get_info(port
, &tmp
);
2615 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.iomem_reg_shift
);
2618 static DEVICE_ATTR(type
, S_IRUSR
| S_IRGRP
, uart_get_attr_type
, NULL
);
2619 static DEVICE_ATTR(line
, S_IRUSR
| S_IRGRP
, uart_get_attr_line
, NULL
);
2620 static DEVICE_ATTR(port
, S_IRUSR
| S_IRGRP
, uart_get_attr_port
, NULL
);
2621 static DEVICE_ATTR(irq
, S_IRUSR
| S_IRGRP
, uart_get_attr_irq
, NULL
);
2622 static DEVICE_ATTR(flags
, S_IRUSR
| S_IRGRP
, uart_get_attr_flags
, NULL
);
2623 static DEVICE_ATTR(xmit_fifo_size
, S_IRUSR
| S_IRGRP
, uart_get_attr_xmit_fifo_size
, NULL
);
2624 static DEVICE_ATTR(uartclk
, S_IRUSR
| S_IRGRP
, uart_get_attr_uartclk
, NULL
);
2625 static DEVICE_ATTR(close_delay
, S_IRUSR
| S_IRGRP
, uart_get_attr_close_delay
, NULL
);
2626 static DEVICE_ATTR(closing_wait
, S_IRUSR
| S_IRGRP
, uart_get_attr_closing_wait
, NULL
);
2627 static DEVICE_ATTR(custom_divisor
, S_IRUSR
| S_IRGRP
, uart_get_attr_custom_divisor
, NULL
);
2628 static DEVICE_ATTR(io_type
, S_IRUSR
| S_IRGRP
, uart_get_attr_io_type
, NULL
);
2629 static DEVICE_ATTR(iomem_base
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_base
, NULL
);
2630 static DEVICE_ATTR(iomem_reg_shift
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_reg_shift
, NULL
);
2632 static struct attribute
*tty_dev_attrs
[] = {
2633 &dev_attr_type
.attr
,
2634 &dev_attr_line
.attr
,
2635 &dev_attr_port
.attr
,
2637 &dev_attr_flags
.attr
,
2638 &dev_attr_xmit_fifo_size
.attr
,
2639 &dev_attr_uartclk
.attr
,
2640 &dev_attr_close_delay
.attr
,
2641 &dev_attr_closing_wait
.attr
,
2642 &dev_attr_custom_divisor
.attr
,
2643 &dev_attr_io_type
.attr
,
2644 &dev_attr_iomem_base
.attr
,
2645 &dev_attr_iomem_reg_shift
.attr
,
2649 static const struct attribute_group tty_dev_attr_group
= {
2650 .attrs
= tty_dev_attrs
,
2654 * uart_add_one_port - attach a driver-defined port structure
2655 * @drv: pointer to the uart low level driver structure for this port
2656 * @uport: uart port structure to use for this port.
2658 * This allows the driver to register its own uart_port structure
2659 * with the core driver. The main purpose is to allow the low
2660 * level uart drivers to expand uart_port, rather than having yet
2661 * more levels of structures.
2663 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2665 struct uart_state
*state
;
2666 struct tty_port
*port
;
2668 struct device
*tty_dev
;
2671 BUG_ON(in_interrupt());
2673 if (uport
->line
>= drv
->nr
)
2676 state
= drv
->state
+ uport
->line
;
2677 port
= &state
->port
;
2679 mutex_lock(&port_mutex
);
2680 mutex_lock(&port
->mutex
);
2681 if (state
->uart_port
) {
2686 /* Link the port to the driver state table and vice versa */
2687 state
->uart_port
= uport
;
2688 uport
->state
= state
;
2690 state
->pm_state
= UART_PM_STATE_UNDEFINED
;
2691 uport
->cons
= drv
->cons
;
2692 uport
->minor
= drv
->tty_driver
->minor_start
+ uport
->line
;
2695 * If this port is a console, then the spinlock is already
2698 if (!(uart_console(uport
) && (uport
->cons
->flags
& CON_ENABLED
))) {
2699 spin_lock_init(&uport
->lock
);
2700 lockdep_set_class(&uport
->lock
, &port_lock_key
);
2702 if (uport
->cons
&& uport
->dev
)
2703 of_console_check(uport
->dev
->of_node
, uport
->cons
->name
, uport
->line
);
2705 uart_configure_port(drv
, state
, uport
);
2708 if (uport
->attr_group
)
2711 uport
->tty_groups
= kcalloc(num_groups
, sizeof(*uport
->tty_groups
),
2713 if (!uport
->tty_groups
) {
2717 uport
->tty_groups
[0] = &tty_dev_attr_group
;
2718 if (uport
->attr_group
)
2719 uport
->tty_groups
[1] = uport
->attr_group
;
2722 * Register the port whether it's detected or not. This allows
2723 * setserial to be used to alter this port's parameters.
2725 tty_dev
= tty_port_register_device_attr(port
, drv
->tty_driver
,
2726 uport
->line
, uport
->dev
, port
, uport
->tty_groups
);
2727 if (likely(!IS_ERR(tty_dev
))) {
2728 device_set_wakeup_capable(tty_dev
, 1);
2730 dev_err(uport
->dev
, "Cannot register tty device on line %d\n",
2735 * Ensure UPF_DEAD is not set.
2737 uport
->flags
&= ~UPF_DEAD
;
2740 mutex_unlock(&port
->mutex
);
2741 mutex_unlock(&port_mutex
);
2747 * uart_remove_one_port - detach a driver defined port structure
2748 * @drv: pointer to the uart low level driver structure for this port
2749 * @uport: uart port structure for this port
2751 * This unhooks (and hangs up) the specified port structure from the
2752 * core driver. No further calls will be made to the low-level code
2755 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2757 struct uart_state
*state
= drv
->state
+ uport
->line
;
2758 struct tty_port
*port
= &state
->port
;
2759 struct tty_struct
*tty
;
2762 BUG_ON(in_interrupt());
2764 if (state
->uart_port
!= uport
)
2765 dev_alert(uport
->dev
, "Removing wrong port: %p != %p\n",
2766 state
->uart_port
, uport
);
2768 mutex_lock(&port_mutex
);
2771 * Mark the port "dead" - this prevents any opens from
2772 * succeeding while we shut down the port.
2774 mutex_lock(&port
->mutex
);
2775 if (!state
->uart_port
) {
2776 mutex_unlock(&port
->mutex
);
2780 uport
->flags
|= UPF_DEAD
;
2781 mutex_unlock(&port
->mutex
);
2784 * Remove the devices from the tty layer
2786 tty_unregister_device(drv
->tty_driver
, uport
->line
);
2788 tty
= tty_port_tty_get(port
);
2790 tty_vhangup(port
->tty
);
2795 * If the port is used as a console, unregister it
2797 if (uart_console(uport
))
2798 unregister_console(uport
->cons
);
2801 * Free the port IO and memory resources, if any.
2803 if (uport
->type
!= PORT_UNKNOWN
)
2804 uport
->ops
->release_port(uport
);
2805 kfree(uport
->tty_groups
);
2808 * Indicate that there isn't a port here anymore.
2810 uport
->type
= PORT_UNKNOWN
;
2812 state
->uart_port
= NULL
;
2814 mutex_unlock(&port_mutex
);
2820 * Are the two ports equivalent?
2822 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2824 if (port1
->iotype
!= port2
->iotype
)
2827 switch (port1
->iotype
) {
2829 return (port1
->iobase
== port2
->iobase
);
2831 return (port1
->iobase
== port2
->iobase
) &&
2832 (port1
->hub6
== port2
->hub6
);
2838 return (port1
->mapbase
== port2
->mapbase
);
2842 EXPORT_SYMBOL(uart_match_port
);
2845 * uart_handle_dcd_change - handle a change of carrier detect state
2846 * @uport: uart_port structure for the open port
2847 * @status: new carrier detect status, nonzero if active
2849 * Caller must hold uport->lock
2851 void uart_handle_dcd_change(struct uart_port
*uport
, unsigned int status
)
2853 struct tty_port
*port
= &uport
->state
->port
;
2854 struct tty_struct
*tty
= port
->tty
;
2855 struct tty_ldisc
*ld
;
2857 lockdep_assert_held_once(&uport
->lock
);
2860 ld
= tty_ldisc_ref(tty
);
2862 if (ld
->ops
->dcd_change
)
2863 ld
->ops
->dcd_change(tty
, status
);
2864 tty_ldisc_deref(ld
);
2868 uport
->icount
.dcd
++;
2870 if (uart_dcd_enabled(uport
)) {
2872 wake_up_interruptible(&port
->open_wait
);
2877 EXPORT_SYMBOL_GPL(uart_handle_dcd_change
);
2880 * uart_handle_cts_change - handle a change of clear-to-send state
2881 * @uport: uart_port structure for the open port
2882 * @status: new clear to send status, nonzero if active
2884 * Caller must hold uport->lock
2886 void uart_handle_cts_change(struct uart_port
*uport
, unsigned int status
)
2888 lockdep_assert_held_once(&uport
->lock
);
2890 uport
->icount
.cts
++;
2892 if (uart_softcts_mode(uport
)) {
2893 if (uport
->hw_stopped
) {
2895 uport
->hw_stopped
= 0;
2896 uport
->ops
->start_tx(uport
);
2897 uart_write_wakeup(uport
);
2901 uport
->hw_stopped
= 1;
2902 uport
->ops
->stop_tx(uport
);
2908 EXPORT_SYMBOL_GPL(uart_handle_cts_change
);
2911 * uart_insert_char - push a char to the uart layer
2913 * User is responsible to call tty_flip_buffer_push when they are done with
2916 * @port: corresponding port
2917 * @status: state of the serial port RX buffer (LSR for 8250)
2918 * @overrun: mask of overrun bits in @status
2919 * @ch: character to push
2920 * @flag: flag for the character (see TTY_NORMAL and friends)
2922 void uart_insert_char(struct uart_port
*port
, unsigned int status
,
2923 unsigned int overrun
, unsigned int ch
, unsigned int flag
)
2925 struct tty_port
*tport
= &port
->state
->port
;
2927 if ((status
& port
->ignore_status_mask
& ~overrun
) == 0)
2928 if (tty_insert_flip_char(tport
, ch
, flag
) == 0)
2929 ++port
->icount
.buf_overrun
;
2932 * Overrun is special. Since it's reported immediately,
2933 * it doesn't affect the current character.
2935 if (status
& ~port
->ignore_status_mask
& overrun
)
2936 if (tty_insert_flip_char(tport
, 0, TTY_OVERRUN
) == 0)
2937 ++port
->icount
.buf_overrun
;
2939 EXPORT_SYMBOL_GPL(uart_insert_char
);
2941 EXPORT_SYMBOL(uart_write_wakeup
);
2942 EXPORT_SYMBOL(uart_register_driver
);
2943 EXPORT_SYMBOL(uart_unregister_driver
);
2944 EXPORT_SYMBOL(uart_suspend_port
);
2945 EXPORT_SYMBOL(uart_resume_port
);
2946 EXPORT_SYMBOL(uart_add_one_port
);
2947 EXPORT_SYMBOL(uart_remove_one_port
);
2949 MODULE_DESCRIPTION("Serial driver core");
2950 MODULE_LICENSE("GPL");