1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver core for serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
10 #include <linux/module.h>
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/gpio/consumer.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/device.h>
22 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
23 #include <linux/serial_core.h>
24 #include <linux/sysrq.h>
25 #include <linux/delay.h>
26 #include <linux/mutex.h>
27 #include <linux/security.h>
29 #include <linux/irq.h>
30 #include <linux/uaccess.h>
33 * This is used to lock changes in serial line configuration.
35 static DEFINE_MUTEX(port_mutex
);
38 * lockdep: port->lock is initialized in two places, but we
39 * want only one lock-class:
41 static struct lock_class_key port_lock_key
;
43 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
45 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
46 struct ktermios
*old_termios
);
47 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
48 static void uart_change_pm(struct uart_state
*state
,
49 enum uart_pm_state pm_state
);
51 static void uart_port_shutdown(struct tty_port
*port
);
53 static int uart_dcd_enabled(struct uart_port
*uport
)
55 return !!(uport
->status
& UPSTAT_DCD_ENABLE
);
58 static inline struct uart_port
*uart_port_ref(struct uart_state
*state
)
60 if (atomic_add_unless(&state
->refcount
, 1, 0))
61 return state
->uart_port
;
65 static inline void uart_port_deref(struct uart_port
*uport
)
67 if (atomic_dec_and_test(&uport
->state
->refcount
))
68 wake_up(&uport
->state
->remove_wait
);
71 #define uart_port_lock(state, flags) \
73 struct uart_port *__uport = uart_port_ref(state); \
75 spin_lock_irqsave(&__uport->lock, flags); \
79 #define uart_port_unlock(uport, flags) \
81 struct uart_port *__uport = uport; \
83 spin_unlock_irqrestore(&__uport->lock, flags); \
84 uart_port_deref(__uport); \
88 static inline struct uart_port
*uart_port_check(struct uart_state
*state
)
90 lockdep_assert_held(&state
->port
.mutex
);
91 return state
->uart_port
;
95 * This routine is used by the interrupt handler to schedule processing in
96 * the software interrupt portion of the driver.
98 void uart_write_wakeup(struct uart_port
*port
)
100 struct uart_state
*state
= port
->state
;
102 * This means you called this function _after_ the port was
103 * closed. No cookie for you.
106 tty_port_tty_wakeup(&state
->port
);
109 static void uart_stop(struct tty_struct
*tty
)
111 struct uart_state
*state
= tty
->driver_data
;
112 struct uart_port
*port
;
115 port
= uart_port_lock(state
, flags
);
117 port
->ops
->stop_tx(port
);
118 uart_port_unlock(port
, flags
);
121 static void __uart_start(struct tty_struct
*tty
)
123 struct uart_state
*state
= tty
->driver_data
;
124 struct uart_port
*port
= state
->uart_port
;
126 if (port
&& !uart_tx_stopped(port
))
127 port
->ops
->start_tx(port
);
130 static void uart_start(struct tty_struct
*tty
)
132 struct uart_state
*state
= tty
->driver_data
;
133 struct uart_port
*port
;
136 port
= uart_port_lock(state
, flags
);
138 uart_port_unlock(port
, flags
);
142 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
147 spin_lock_irqsave(&port
->lock
, flags
);
149 port
->mctrl
= (old
& ~clear
) | set
;
150 if (old
!= port
->mctrl
)
151 port
->ops
->set_mctrl(port
, port
->mctrl
);
152 spin_unlock_irqrestore(&port
->lock
, flags
);
155 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
156 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
158 static void uart_port_dtr_rts(struct uart_port
*uport
, int raise
)
160 int rs485_on
= uport
->rs485_config
&&
161 (uport
->rs485
.flags
& SER_RS485_ENABLED
);
162 int RTS_after_send
= !!(uport
->rs485
.flags
& SER_RS485_RTS_AFTER_SEND
);
165 if (rs485_on
&& !RTS_after_send
) {
166 uart_set_mctrl(uport
, TIOCM_DTR
);
167 uart_clear_mctrl(uport
, TIOCM_RTS
);
169 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
172 unsigned int clear
= TIOCM_DTR
;
174 clear
|= (!rs485_on
|| !RTS_after_send
) ? TIOCM_RTS
: 0;
175 uart_clear_mctrl(uport
, clear
);
180 * Startup the port. This will be called once per open. All calls
181 * will be serialised by the per-port mutex.
183 static int uart_port_startup(struct tty_struct
*tty
, struct uart_state
*state
,
186 struct uart_port
*uport
= uart_port_check(state
);
191 if (uport
->type
== PORT_UNKNOWN
)
195 * Make sure the device is in D0 state.
197 uart_change_pm(state
, UART_PM_STATE_ON
);
200 * Initialise and allocate the transmit and temporary
203 page
= get_zeroed_page(GFP_KERNEL
);
207 uart_port_lock(state
, flags
);
208 if (!state
->xmit
.buf
) {
209 state
->xmit
.buf
= (unsigned char *) page
;
210 uart_circ_clear(&state
->xmit
);
211 uart_port_unlock(uport
, flags
);
213 uart_port_unlock(uport
, flags
);
215 * Do not free() the page under the port lock, see
221 retval
= uport
->ops
->startup(uport
);
223 if (uart_console(uport
) && uport
->cons
->cflag
) {
224 tty
->termios
.c_cflag
= uport
->cons
->cflag
;
225 tty
->termios
.c_ispeed
= uport
->cons
->ispeed
;
226 tty
->termios
.c_ospeed
= uport
->cons
->ospeed
;
227 uport
->cons
->cflag
= 0;
228 uport
->cons
->ispeed
= 0;
229 uport
->cons
->ospeed
= 0;
232 * Initialise the hardware port settings.
234 uart_change_speed(tty
, state
, NULL
);
237 * Setup the RTS and DTR signals once the
238 * port is open and ready to respond.
240 if (init_hw
&& C_BAUD(tty
))
241 uart_port_dtr_rts(uport
, 1);
245 * This is to allow setserial on this port. People may want to set
246 * port/irq/type and then reconfigure the port properly if it failed
249 if (retval
&& capable(CAP_SYS_ADMIN
))
255 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
,
258 struct tty_port
*port
= &state
->port
;
261 if (tty_port_initialized(port
))
264 retval
= uart_port_startup(tty
, state
, init_hw
);
266 set_bit(TTY_IO_ERROR
, &tty
->flags
);
272 * This routine will shutdown a serial port; interrupts are disabled, and
273 * DTR is dropped if the hangup on close termio flag is on. Calls to
274 * uart_shutdown are serialised by the per-port semaphore.
276 * uport == NULL if uart_port has already been removed
278 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
280 struct uart_port
*uport
= uart_port_check(state
);
281 struct tty_port
*port
= &state
->port
;
283 char *xmit_buf
= NULL
;
286 * Set the TTY IO error marker
289 set_bit(TTY_IO_ERROR
, &tty
->flags
);
291 if (tty_port_initialized(port
)) {
292 tty_port_set_initialized(port
, 0);
295 * Turn off DTR and RTS early.
297 if (uport
&& uart_console(uport
) && tty
) {
298 uport
->cons
->cflag
= tty
->termios
.c_cflag
;
299 uport
->cons
->ispeed
= tty
->termios
.c_ispeed
;
300 uport
->cons
->ospeed
= tty
->termios
.c_ospeed
;
303 if (!tty
|| C_HUPCL(tty
))
304 uart_port_dtr_rts(uport
, 0);
306 uart_port_shutdown(port
);
310 * It's possible for shutdown to be called after suspend if we get
311 * a DCD drop (hangup) at just the right time. Clear suspended bit so
312 * we don't try to resume a port that has been shutdown.
314 tty_port_set_suspended(port
, 0);
317 * Do not free() the transmit buffer page under the port lock since
318 * this can create various circular locking scenarios. For instance,
319 * console driver may need to allocate/free a debug object, which
320 * can endup in printk() recursion.
322 uart_port_lock(state
, flags
);
323 xmit_buf
= state
->xmit
.buf
;
324 state
->xmit
.buf
= NULL
;
325 uart_port_unlock(uport
, flags
);
328 free_page((unsigned long)xmit_buf
);
332 * uart_update_timeout - update per-port FIFO timeout.
333 * @port: uart_port structure describing the port
334 * @cflag: termios cflag value
335 * @baud: speed of the port
337 * Set the port FIFO timeout value. The @cflag value should
338 * reflect the actual hardware settings.
341 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
346 size
= tty_get_frame_size(cflag
) * port
->fifosize
;
349 * Figure the timeout to send the above number of bits.
350 * Add .02 seconds of slop
352 port
->timeout
= (HZ
* size
) / baud
+ HZ
/50;
355 EXPORT_SYMBOL(uart_update_timeout
);
358 * uart_get_baud_rate - return baud rate for a particular port
359 * @port: uart_port structure describing the port in question.
360 * @termios: desired termios settings.
361 * @old: old termios (or NULL)
362 * @min: minimum acceptable baud rate
363 * @max: maximum acceptable baud rate
365 * Decode the termios structure into a numeric baud rate,
366 * taking account of the magic 38400 baud rate (with spd_*
367 * flags), and mapping the %B0 rate to 9600 baud.
369 * If the new baud rate is invalid, try the old termios setting.
370 * If it's still invalid, we try 9600 baud.
372 * Update the @termios structure to reflect the baud rate
373 * we're actually going to be using. Don't do this for the case
374 * where B0 is requested ("hang up").
377 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
378 struct ktermios
*old
, unsigned int min
, unsigned int max
)
382 unsigned int altbaud
;
384 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
404 for (try = 0; try < 2; try++) {
405 baud
= tty_termios_baud_rate(termios
);
408 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
411 if (try == 0 && baud
== 38400)
415 * Special case: B0 rate.
422 if (baud
>= min
&& baud
<= max
)
426 * Oops, the quotient was zero. Try again with
427 * the old baud rate if possible.
429 termios
->c_cflag
&= ~CBAUD
;
431 baud
= tty_termios_baud_rate(old
);
433 tty_termios_encode_baud_rate(termios
,
440 * As a last resort, if the range cannot be met then clip to
441 * the nearest chip supported rate.
445 tty_termios_encode_baud_rate(termios
,
448 tty_termios_encode_baud_rate(termios
,
452 /* Should never happen */
457 EXPORT_SYMBOL(uart_get_baud_rate
);
460 * uart_get_divisor - return uart clock divisor
461 * @port: uart_port structure describing the port.
462 * @baud: desired baud rate
464 * Calculate the uart clock divisor for the port.
467 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
472 * Old custom speed handling.
474 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
475 quot
= port
->custom_divisor
;
477 quot
= DIV_ROUND_CLOSEST(port
->uartclk
, 16 * baud
);
482 EXPORT_SYMBOL(uart_get_divisor
);
484 /* Caller holds port mutex */
485 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
486 struct ktermios
*old_termios
)
488 struct uart_port
*uport
= uart_port_check(state
);
489 struct ktermios
*termios
;
493 * If we have no tty, termios, or the port does not exist,
494 * then we can't set the parameters for this port.
496 if (!tty
|| uport
->type
== PORT_UNKNOWN
)
499 termios
= &tty
->termios
;
500 uport
->ops
->set_termios(uport
, termios
, old_termios
);
503 * Set modem status enables based on termios cflag
505 spin_lock_irq(&uport
->lock
);
506 if (termios
->c_cflag
& CRTSCTS
)
507 uport
->status
|= UPSTAT_CTS_ENABLE
;
509 uport
->status
&= ~UPSTAT_CTS_ENABLE
;
511 if (termios
->c_cflag
& CLOCAL
)
512 uport
->status
&= ~UPSTAT_DCD_ENABLE
;
514 uport
->status
|= UPSTAT_DCD_ENABLE
;
516 /* reset sw-assisted CTS flow control based on (possibly) new mode */
517 hw_stopped
= uport
->hw_stopped
;
518 uport
->hw_stopped
= uart_softcts_mode(uport
) &&
519 !(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
);
520 if (uport
->hw_stopped
) {
522 uport
->ops
->stop_tx(uport
);
527 spin_unlock_irq(&uport
->lock
);
530 static int uart_put_char(struct tty_struct
*tty
, unsigned char c
)
532 struct uart_state
*state
= tty
->driver_data
;
533 struct uart_port
*port
;
534 struct circ_buf
*circ
;
539 port
= uart_port_lock(state
, flags
);
541 uart_port_unlock(port
, flags
);
545 if (port
&& uart_circ_chars_free(circ
) != 0) {
546 circ
->buf
[circ
->head
] = c
;
547 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
550 uart_port_unlock(port
, flags
);
554 static void uart_flush_chars(struct tty_struct
*tty
)
559 static int uart_write(struct tty_struct
*tty
,
560 const unsigned char *buf
, int count
)
562 struct uart_state
*state
= tty
->driver_data
;
563 struct uart_port
*port
;
564 struct circ_buf
*circ
;
569 * This means you called this function _after_ the port was
570 * closed. No cookie for you.
577 port
= uart_port_lock(state
, flags
);
580 uart_port_unlock(port
, flags
);
585 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
590 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
591 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
598 uart_port_unlock(port
, flags
);
602 static unsigned int uart_write_room(struct tty_struct
*tty
)
604 struct uart_state
*state
= tty
->driver_data
;
605 struct uart_port
*port
;
609 port
= uart_port_lock(state
, flags
);
610 ret
= uart_circ_chars_free(&state
->xmit
);
611 uart_port_unlock(port
, flags
);
615 static unsigned int uart_chars_in_buffer(struct tty_struct
*tty
)
617 struct uart_state
*state
= tty
->driver_data
;
618 struct uart_port
*port
;
622 port
= uart_port_lock(state
, flags
);
623 ret
= uart_circ_chars_pending(&state
->xmit
);
624 uart_port_unlock(port
, flags
);
628 static void uart_flush_buffer(struct tty_struct
*tty
)
630 struct uart_state
*state
= tty
->driver_data
;
631 struct uart_port
*port
;
635 * This means you called this function _after_ the port was
636 * closed. No cookie for you.
643 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
645 port
= uart_port_lock(state
, flags
);
648 uart_circ_clear(&state
->xmit
);
649 if (port
->ops
->flush_buffer
)
650 port
->ops
->flush_buffer(port
);
651 uart_port_unlock(port
, flags
);
652 tty_port_tty_wakeup(&state
->port
);
656 * This function is used to send a high-priority XON/XOFF character to
659 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
661 struct uart_state
*state
= tty
->driver_data
;
662 struct uart_port
*port
;
665 port
= uart_port_ref(state
);
669 if (port
->ops
->send_xchar
)
670 port
->ops
->send_xchar(port
, ch
);
672 spin_lock_irqsave(&port
->lock
, flags
);
675 port
->ops
->start_tx(port
);
676 spin_unlock_irqrestore(&port
->lock
, flags
);
678 uart_port_deref(port
);
681 static void uart_throttle(struct tty_struct
*tty
)
683 struct uart_state
*state
= tty
->driver_data
;
684 upstat_t mask
= UPSTAT_SYNC_FIFO
;
685 struct uart_port
*port
;
687 port
= uart_port_ref(state
);
692 mask
|= UPSTAT_AUTOXOFF
;
694 mask
|= UPSTAT_AUTORTS
;
696 if (port
->status
& mask
) {
697 port
->ops
->throttle(port
);
698 mask
&= ~port
->status
;
701 if (mask
& UPSTAT_AUTORTS
)
702 uart_clear_mctrl(port
, TIOCM_RTS
);
704 if (mask
& UPSTAT_AUTOXOFF
)
705 uart_send_xchar(tty
, STOP_CHAR(tty
));
707 uart_port_deref(port
);
710 static void uart_unthrottle(struct tty_struct
*tty
)
712 struct uart_state
*state
= tty
->driver_data
;
713 upstat_t mask
= UPSTAT_SYNC_FIFO
;
714 struct uart_port
*port
;
716 port
= uart_port_ref(state
);
721 mask
|= UPSTAT_AUTOXOFF
;
723 mask
|= UPSTAT_AUTORTS
;
725 if (port
->status
& mask
) {
726 port
->ops
->unthrottle(port
);
727 mask
&= ~port
->status
;
730 if (mask
& UPSTAT_AUTORTS
)
731 uart_set_mctrl(port
, TIOCM_RTS
);
733 if (mask
& UPSTAT_AUTOXOFF
)
734 uart_send_xchar(tty
, START_CHAR(tty
));
736 uart_port_deref(port
);
739 static int uart_get_info(struct tty_port
*port
, struct serial_struct
*retinfo
)
741 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
742 struct uart_port
*uport
;
746 * Ensure the state we copy is consistent and no hardware changes
749 mutex_lock(&port
->mutex
);
750 uport
= uart_port_check(state
);
754 retinfo
->type
= uport
->type
;
755 retinfo
->line
= uport
->line
;
756 retinfo
->port
= uport
->iobase
;
757 if (HIGH_BITS_OFFSET
)
758 retinfo
->port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
759 retinfo
->irq
= uport
->irq
;
760 retinfo
->flags
= (__force
int)uport
->flags
;
761 retinfo
->xmit_fifo_size
= uport
->fifosize
;
762 retinfo
->baud_base
= uport
->uartclk
/ 16;
763 retinfo
->close_delay
= jiffies_to_msecs(port
->close_delay
) / 10;
764 retinfo
->closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
765 ASYNC_CLOSING_WAIT_NONE
:
766 jiffies_to_msecs(port
->closing_wait
) / 10;
767 retinfo
->custom_divisor
= uport
->custom_divisor
;
768 retinfo
->hub6
= uport
->hub6
;
769 retinfo
->io_type
= uport
->iotype
;
770 retinfo
->iomem_reg_shift
= uport
->regshift
;
771 retinfo
->iomem_base
= (void *)(unsigned long)uport
->mapbase
;
775 mutex_unlock(&port
->mutex
);
779 static int uart_get_info_user(struct tty_struct
*tty
,
780 struct serial_struct
*ss
)
782 struct uart_state
*state
= tty
->driver_data
;
783 struct tty_port
*port
= &state
->port
;
785 return uart_get_info(port
, ss
) < 0 ? -EIO
: 0;
788 static int uart_set_info(struct tty_struct
*tty
, struct tty_port
*port
,
789 struct uart_state
*state
,
790 struct serial_struct
*new_info
)
792 struct uart_port
*uport
= uart_port_check(state
);
793 unsigned long new_port
;
794 unsigned int change_irq
, change_port
, closing_wait
;
795 unsigned int old_custom_divisor
, close_delay
;
796 upf_t old_flags
, new_flags
;
802 new_port
= new_info
->port
;
803 if (HIGH_BITS_OFFSET
)
804 new_port
+= (unsigned long) new_info
->port_high
<< HIGH_BITS_OFFSET
;
806 new_info
->irq
= irq_canonicalize(new_info
->irq
);
807 close_delay
= msecs_to_jiffies(new_info
->close_delay
* 10);
808 closing_wait
= new_info
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
809 ASYNC_CLOSING_WAIT_NONE
:
810 msecs_to_jiffies(new_info
->closing_wait
* 10);
813 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
814 && new_info
->irq
!= uport
->irq
;
817 * Since changing the 'type' of the port changes its resource
818 * allocations, we should treat type changes the same as
821 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
822 && (new_port
!= uport
->iobase
||
823 (unsigned long)new_info
->iomem_base
!= uport
->mapbase
||
824 new_info
->hub6
!= uport
->hub6
||
825 new_info
->io_type
!= uport
->iotype
||
826 new_info
->iomem_reg_shift
!= uport
->regshift
||
827 new_info
->type
!= uport
->type
);
829 old_flags
= uport
->flags
;
830 new_flags
= (__force upf_t
)new_info
->flags
;
831 old_custom_divisor
= uport
->custom_divisor
;
833 if (!capable(CAP_SYS_ADMIN
)) {
835 if (change_irq
|| change_port
||
836 (new_info
->baud_base
!= uport
->uartclk
/ 16) ||
837 (close_delay
!= port
->close_delay
) ||
838 (closing_wait
!= port
->closing_wait
) ||
839 (new_info
->xmit_fifo_size
&&
840 new_info
->xmit_fifo_size
!= uport
->fifosize
) ||
841 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
843 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
844 (new_flags
& UPF_USR_MASK
));
845 uport
->custom_divisor
= new_info
->custom_divisor
;
849 if (change_irq
|| change_port
) {
850 retval
= security_locked_down(LOCKDOWN_TIOCSSERIAL
);
856 * Ask the low level driver to verify the settings.
858 if (uport
->ops
->verify_port
)
859 retval
= uport
->ops
->verify_port(uport
, new_info
);
861 if ((new_info
->irq
>= nr_irqs
) || (new_info
->irq
< 0) ||
862 (new_info
->baud_base
< 9600))
868 if (change_port
|| change_irq
) {
872 * Make sure that we are the sole user of this port.
874 if (tty_port_users(port
) > 1)
878 * We need to shutdown the serial port at the old
879 * port/type/irq combination.
881 uart_shutdown(tty
, state
);
885 unsigned long old_iobase
, old_mapbase
;
886 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
888 old_iobase
= uport
->iobase
;
889 old_mapbase
= uport
->mapbase
;
890 old_type
= uport
->type
;
891 old_hub6
= uport
->hub6
;
892 old_iotype
= uport
->iotype
;
893 old_shift
= uport
->regshift
;
896 * Free and release old regions
898 if (old_type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
899 uport
->ops
->release_port(uport
);
901 uport
->iobase
= new_port
;
902 uport
->type
= new_info
->type
;
903 uport
->hub6
= new_info
->hub6
;
904 uport
->iotype
= new_info
->io_type
;
905 uport
->regshift
= new_info
->iomem_reg_shift
;
906 uport
->mapbase
= (unsigned long)new_info
->iomem_base
;
909 * Claim and map the new regions
911 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->request_port
) {
912 retval
= uport
->ops
->request_port(uport
);
914 /* Always success - Jean II */
919 * If we fail to request resources for the
920 * new port, try to restore the old settings.
923 uport
->iobase
= old_iobase
;
924 uport
->type
= old_type
;
925 uport
->hub6
= old_hub6
;
926 uport
->iotype
= old_iotype
;
927 uport
->regshift
= old_shift
;
928 uport
->mapbase
= old_mapbase
;
930 if (old_type
!= PORT_UNKNOWN
) {
931 retval
= uport
->ops
->request_port(uport
);
933 * If we failed to restore the old settings,
937 uport
->type
= PORT_UNKNOWN
;
945 /* Added to return the correct error -Ram Gupta */
951 uport
->irq
= new_info
->irq
;
952 if (!(uport
->flags
& UPF_FIXED_PORT
))
953 uport
->uartclk
= new_info
->baud_base
* 16;
954 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
955 (new_flags
& UPF_CHANGE_MASK
);
956 uport
->custom_divisor
= new_info
->custom_divisor
;
957 port
->close_delay
= close_delay
;
958 port
->closing_wait
= closing_wait
;
959 if (new_info
->xmit_fifo_size
)
960 uport
->fifosize
= new_info
->xmit_fifo_size
;
964 if (uport
->type
== PORT_UNKNOWN
)
966 if (tty_port_initialized(port
)) {
967 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
968 old_custom_divisor
!= uport
->custom_divisor
) {
970 * If they're setting up a custom divisor or speed,
971 * instead of clearing it, then bitch about it.
973 if (uport
->flags
& UPF_SPD_MASK
) {
974 dev_notice_ratelimited(uport
->dev
,
975 "%s sets custom speed on %s. This is deprecated.\n",
977 tty_name(port
->tty
));
979 uart_change_speed(tty
, state
, NULL
);
982 retval
= uart_startup(tty
, state
, 1);
984 tty_port_set_initialized(port
, true);
992 static int uart_set_info_user(struct tty_struct
*tty
, struct serial_struct
*ss
)
994 struct uart_state
*state
= tty
->driver_data
;
995 struct tty_port
*port
= &state
->port
;
998 down_write(&tty
->termios_rwsem
);
1000 * This semaphore protects port->count. It is also
1001 * very useful to prevent opens. Also, take the
1002 * port configuration semaphore to make sure that a
1003 * module insertion/removal doesn't change anything
1006 mutex_lock(&port
->mutex
);
1007 retval
= uart_set_info(tty
, port
, state
, ss
);
1008 mutex_unlock(&port
->mutex
);
1009 up_write(&tty
->termios_rwsem
);
1014 * uart_get_lsr_info - get line status register info
1015 * @tty: tty associated with the UART
1016 * @state: UART being queried
1017 * @value: returned modem value
1019 static int uart_get_lsr_info(struct tty_struct
*tty
,
1020 struct uart_state
*state
, unsigned int __user
*value
)
1022 struct uart_port
*uport
= uart_port_check(state
);
1023 unsigned int result
;
1025 result
= uport
->ops
->tx_empty(uport
);
1028 * If we're about to load something into the transmit
1029 * register, we'll pretend the transmitter isn't empty to
1030 * avoid a race condition (depending on when the transmit
1031 * interrupt happens).
1033 if (uport
->x_char
||
1034 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
1035 !uart_tx_stopped(uport
)))
1036 result
&= ~TIOCSER_TEMT
;
1038 return put_user(result
, value
);
1041 static int uart_tiocmget(struct tty_struct
*tty
)
1043 struct uart_state
*state
= tty
->driver_data
;
1044 struct tty_port
*port
= &state
->port
;
1045 struct uart_port
*uport
;
1048 mutex_lock(&port
->mutex
);
1049 uport
= uart_port_check(state
);
1053 if (!tty_io_error(tty
)) {
1054 result
= uport
->mctrl
;
1055 spin_lock_irq(&uport
->lock
);
1056 result
|= uport
->ops
->get_mctrl(uport
);
1057 spin_unlock_irq(&uport
->lock
);
1060 mutex_unlock(&port
->mutex
);
1065 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
1067 struct uart_state
*state
= tty
->driver_data
;
1068 struct tty_port
*port
= &state
->port
;
1069 struct uart_port
*uport
;
1072 mutex_lock(&port
->mutex
);
1073 uport
= uart_port_check(state
);
1077 if (!tty_io_error(tty
)) {
1078 if (uport
->rs485
.flags
& SER_RS485_ENABLED
) {
1080 clear
&= ~TIOCM_RTS
;
1083 uart_update_mctrl(uport
, set
, clear
);
1087 mutex_unlock(&port
->mutex
);
1091 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
1093 struct uart_state
*state
= tty
->driver_data
;
1094 struct tty_port
*port
= &state
->port
;
1095 struct uart_port
*uport
;
1098 mutex_lock(&port
->mutex
);
1099 uport
= uart_port_check(state
);
1103 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->break_ctl
)
1104 uport
->ops
->break_ctl(uport
, break_state
);
1107 mutex_unlock(&port
->mutex
);
1111 static int uart_do_autoconfig(struct tty_struct
*tty
, struct uart_state
*state
)
1113 struct tty_port
*port
= &state
->port
;
1114 struct uart_port
*uport
;
1117 if (!capable(CAP_SYS_ADMIN
))
1121 * Take the per-port semaphore. This prevents count from
1122 * changing, and hence any extra opens of the port while
1123 * we're auto-configuring.
1125 if (mutex_lock_interruptible(&port
->mutex
))
1126 return -ERESTARTSYS
;
1128 uport
= uart_port_check(state
);
1135 if (tty_port_users(port
) == 1) {
1136 uart_shutdown(tty
, state
);
1139 * If we already have a port type configured,
1140 * we must release its resources.
1142 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
1143 uport
->ops
->release_port(uport
);
1145 flags
= UART_CONFIG_TYPE
;
1146 if (uport
->flags
& UPF_AUTO_IRQ
)
1147 flags
|= UART_CONFIG_IRQ
;
1150 * This will claim the ports resources if
1153 uport
->ops
->config_port(uport
, flags
);
1155 ret
= uart_startup(tty
, state
, 1);
1157 tty_port_set_initialized(port
, true);
1162 mutex_unlock(&port
->mutex
);
1166 static void uart_enable_ms(struct uart_port
*uport
)
1169 * Force modem status interrupts on
1171 if (uport
->ops
->enable_ms
)
1172 uport
->ops
->enable_ms(uport
);
1176 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1177 * - mask passed in arg for lines of interest
1178 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1179 * Caller should use TIOCGICOUNT to see which one it was
1181 * FIXME: This wants extracting into a common all driver implementation
1182 * of TIOCMWAIT using tty_port.
1184 static int uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
1186 struct uart_port
*uport
;
1187 struct tty_port
*port
= &state
->port
;
1188 DECLARE_WAITQUEUE(wait
, current
);
1189 struct uart_icount cprev
, cnow
;
1193 * note the counters on entry
1195 uport
= uart_port_ref(state
);
1198 spin_lock_irq(&uport
->lock
);
1199 memcpy(&cprev
, &uport
->icount
, sizeof(struct uart_icount
));
1200 uart_enable_ms(uport
);
1201 spin_unlock_irq(&uport
->lock
);
1203 add_wait_queue(&port
->delta_msr_wait
, &wait
);
1205 spin_lock_irq(&uport
->lock
);
1206 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1207 spin_unlock_irq(&uport
->lock
);
1209 set_current_state(TASK_INTERRUPTIBLE
);
1211 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1212 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1213 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1214 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1221 /* see if a signal did it */
1222 if (signal_pending(current
)) {
1229 __set_current_state(TASK_RUNNING
);
1230 remove_wait_queue(&port
->delta_msr_wait
, &wait
);
1231 uart_port_deref(uport
);
1237 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1238 * Return: write counters to the user passed counter struct
1239 * NB: both 1->0 and 0->1 transitions are counted except for
1240 * RI where only 0->1 is counted.
1242 static int uart_get_icount(struct tty_struct
*tty
,
1243 struct serial_icounter_struct
*icount
)
1245 struct uart_state
*state
= tty
->driver_data
;
1246 struct uart_icount cnow
;
1247 struct uart_port
*uport
;
1249 uport
= uart_port_ref(state
);
1252 spin_lock_irq(&uport
->lock
);
1253 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1254 spin_unlock_irq(&uport
->lock
);
1255 uart_port_deref(uport
);
1257 icount
->cts
= cnow
.cts
;
1258 icount
->dsr
= cnow
.dsr
;
1259 icount
->rng
= cnow
.rng
;
1260 icount
->dcd
= cnow
.dcd
;
1261 icount
->rx
= cnow
.rx
;
1262 icount
->tx
= cnow
.tx
;
1263 icount
->frame
= cnow
.frame
;
1264 icount
->overrun
= cnow
.overrun
;
1265 icount
->parity
= cnow
.parity
;
1266 icount
->brk
= cnow
.brk
;
1267 icount
->buf_overrun
= cnow
.buf_overrun
;
1272 static int uart_get_rs485_config(struct uart_port
*port
,
1273 struct serial_rs485 __user
*rs485
)
1275 unsigned long flags
;
1276 struct serial_rs485 aux
;
1278 spin_lock_irqsave(&port
->lock
, flags
);
1280 spin_unlock_irqrestore(&port
->lock
, flags
);
1282 if (copy_to_user(rs485
, &aux
, sizeof(aux
)))
1288 static int uart_set_rs485_config(struct uart_port
*port
,
1289 struct serial_rs485 __user
*rs485_user
)
1291 struct serial_rs485 rs485
;
1293 unsigned long flags
;
1295 if (!port
->rs485_config
)
1298 if (copy_from_user(&rs485
, rs485_user
, sizeof(*rs485_user
)))
1301 spin_lock_irqsave(&port
->lock
, flags
);
1302 ret
= port
->rs485_config(port
, &rs485
);
1303 spin_unlock_irqrestore(&port
->lock
, flags
);
1307 if (copy_to_user(rs485_user
, &port
->rs485
, sizeof(port
->rs485
)))
1313 static int uart_get_iso7816_config(struct uart_port
*port
,
1314 struct serial_iso7816 __user
*iso7816
)
1316 unsigned long flags
;
1317 struct serial_iso7816 aux
;
1319 if (!port
->iso7816_config
)
1322 spin_lock_irqsave(&port
->lock
, flags
);
1323 aux
= port
->iso7816
;
1324 spin_unlock_irqrestore(&port
->lock
, flags
);
1326 if (copy_to_user(iso7816
, &aux
, sizeof(aux
)))
1332 static int uart_set_iso7816_config(struct uart_port
*port
,
1333 struct serial_iso7816 __user
*iso7816_user
)
1335 struct serial_iso7816 iso7816
;
1337 unsigned long flags
;
1339 if (!port
->iso7816_config
)
1342 if (copy_from_user(&iso7816
, iso7816_user
, sizeof(*iso7816_user
)))
1346 * There are 5 words reserved for future use. Check that userspace
1347 * doesn't put stuff in there to prevent breakages in the future.
1349 for (i
= 0; i
< 5; i
++)
1350 if (iso7816
.reserved
[i
])
1353 spin_lock_irqsave(&port
->lock
, flags
);
1354 ret
= port
->iso7816_config(port
, &iso7816
);
1355 spin_unlock_irqrestore(&port
->lock
, flags
);
1359 if (copy_to_user(iso7816_user
, &port
->iso7816
, sizeof(port
->iso7816
)))
1366 * Called via sys_ioctl. We can use spin_lock_irq() here.
1369 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
1371 struct uart_state
*state
= tty
->driver_data
;
1372 struct tty_port
*port
= &state
->port
;
1373 struct uart_port
*uport
;
1374 void __user
*uarg
= (void __user
*)arg
;
1375 int ret
= -ENOIOCTLCMD
;
1379 * These ioctls don't rely on the hardware to be present.
1383 down_write(&tty
->termios_rwsem
);
1384 ret
= uart_do_autoconfig(tty
, state
);
1385 up_write(&tty
->termios_rwsem
);
1389 if (ret
!= -ENOIOCTLCMD
)
1392 if (tty_io_error(tty
)) {
1398 * The following should only be used when hardware is present.
1402 ret
= uart_wait_modem_status(state
, arg
);
1406 if (ret
!= -ENOIOCTLCMD
)
1409 mutex_lock(&port
->mutex
);
1410 uport
= uart_port_check(state
);
1412 if (!uport
|| tty_io_error(tty
)) {
1418 * All these rely on hardware being present and need to be
1419 * protected against the tty being hung up.
1423 case TIOCSERGETLSR
: /* Get line status register */
1424 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1428 ret
= uart_get_rs485_config(uport
, uarg
);
1432 ret
= uart_set_rs485_config(uport
, uarg
);
1436 ret
= uart_set_iso7816_config(state
->uart_port
, uarg
);
1440 ret
= uart_get_iso7816_config(state
->uart_port
, uarg
);
1443 if (uport
->ops
->ioctl
)
1444 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1448 mutex_unlock(&port
->mutex
);
1453 static void uart_set_ldisc(struct tty_struct
*tty
)
1455 struct uart_state
*state
= tty
->driver_data
;
1456 struct uart_port
*uport
;
1457 struct tty_port
*port
= &state
->port
;
1459 if (!tty_port_initialized(port
))
1462 mutex_lock(&state
->port
.mutex
);
1463 uport
= uart_port_check(state
);
1464 if (uport
&& uport
->ops
->set_ldisc
)
1465 uport
->ops
->set_ldisc(uport
, &tty
->termios
);
1466 mutex_unlock(&state
->port
.mutex
);
1469 static void uart_set_termios(struct tty_struct
*tty
,
1470 struct ktermios
*old_termios
)
1472 struct uart_state
*state
= tty
->driver_data
;
1473 struct uart_port
*uport
;
1474 unsigned int cflag
= tty
->termios
.c_cflag
;
1475 unsigned int iflag_mask
= IGNBRK
|BRKINT
|IGNPAR
|PARMRK
|INPCK
;
1476 bool sw_changed
= false;
1478 mutex_lock(&state
->port
.mutex
);
1479 uport
= uart_port_check(state
);
1484 * Drivers doing software flow control also need to know
1485 * about changes to these input settings.
1487 if (uport
->flags
& UPF_SOFT_FLOW
) {
1488 iflag_mask
|= IXANY
|IXON
|IXOFF
;
1490 tty
->termios
.c_cc
[VSTART
] != old_termios
->c_cc
[VSTART
] ||
1491 tty
->termios
.c_cc
[VSTOP
] != old_termios
->c_cc
[VSTOP
];
1495 * These are the bits that are used to setup various
1496 * flags in the low level driver. We can ignore the Bfoo
1497 * bits in c_cflag; c_[io]speed will always be set
1498 * appropriately by set_termios() in tty_ioctl.c
1500 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1501 tty
->termios
.c_ospeed
== old_termios
->c_ospeed
&&
1502 tty
->termios
.c_ispeed
== old_termios
->c_ispeed
&&
1503 ((tty
->termios
.c_iflag
^ old_termios
->c_iflag
) & iflag_mask
) == 0 &&
1508 uart_change_speed(tty
, state
, old_termios
);
1509 /* reload cflag from termios; port driver may have overridden flags */
1510 cflag
= tty
->termios
.c_cflag
;
1512 /* Handle transition to B0 status */
1513 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1514 uart_clear_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
1515 /* Handle transition away from B0 status */
1516 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1517 unsigned int mask
= TIOCM_DTR
;
1519 if (!(cflag
& CRTSCTS
) || !tty_throttled(tty
))
1521 uart_set_mctrl(uport
, mask
);
1524 mutex_unlock(&state
->port
.mutex
);
1528 * Calls to uart_close() are serialised via the tty_lock in
1529 * drivers/tty/tty_io.c:tty_release()
1530 * drivers/tty/tty_io.c:do_tty_hangup()
1532 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1534 struct uart_state
*state
= tty
->driver_data
;
1537 struct uart_driver
*drv
= tty
->driver
->driver_state
;
1538 struct tty_port
*port
;
1540 state
= drv
->state
+ tty
->index
;
1541 port
= &state
->port
;
1542 spin_lock_irq(&port
->lock
);
1544 spin_unlock_irq(&port
->lock
);
1548 pr_debug("uart_close(%d) called\n", tty
->index
);
1550 tty_port_close(tty
->port
, tty
, filp
);
1553 static void uart_tty_port_shutdown(struct tty_port
*port
)
1555 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1556 struct uart_port
*uport
= uart_port_check(state
);
1560 * At this point, we stop accepting input. To do this, we
1561 * disable the receive line status interrupts.
1563 if (WARN(!uport
, "detached port still initialized!\n"))
1566 spin_lock_irq(&uport
->lock
);
1567 uport
->ops
->stop_rx(uport
);
1568 spin_unlock_irq(&uport
->lock
);
1570 uart_port_shutdown(port
);
1573 * It's possible for shutdown to be called after suspend if we get
1574 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1575 * we don't try to resume a port that has been shutdown.
1577 tty_port_set_suspended(port
, 0);
1580 * Free the transmit buffer.
1582 spin_lock_irq(&uport
->lock
);
1583 buf
= state
->xmit
.buf
;
1584 state
->xmit
.buf
= NULL
;
1585 spin_unlock_irq(&uport
->lock
);
1588 free_page((unsigned long)buf
);
1590 uart_change_pm(state
, UART_PM_STATE_OFF
);
1593 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1595 struct uart_state
*state
= tty
->driver_data
;
1596 struct uart_port
*port
;
1597 unsigned long char_time
, expire
;
1599 port
= uart_port_ref(state
);
1603 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0) {
1604 uart_port_deref(port
);
1609 * Set the check interval to be 1/5 of the estimated time to
1610 * send a single character, and make it at least 1. The check
1611 * interval should also be less than the timeout.
1613 * Note: we have to use pretty tight timings here to satisfy
1616 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1617 char_time
= char_time
/ 5;
1620 if (timeout
&& timeout
< char_time
)
1621 char_time
= timeout
;
1624 * If the transmitter hasn't cleared in twice the approximate
1625 * amount of time to send the entire FIFO, it probably won't
1626 * ever clear. This assumes the UART isn't doing flow
1627 * control, which is currently the case. Hence, if it ever
1628 * takes longer than port->timeout, this is probably due to a
1629 * UART bug of some kind. So, we clamp the timeout parameter at
1632 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1633 timeout
= 2 * port
->timeout
;
1635 expire
= jiffies
+ timeout
;
1637 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1638 port
->line
, jiffies
, expire
);
1641 * Check whether the transmitter is empty every 'char_time'.
1642 * 'timeout' / 'expire' give us the maximum amount of time
1645 while (!port
->ops
->tx_empty(port
)) {
1646 msleep_interruptible(jiffies_to_msecs(char_time
));
1647 if (signal_pending(current
))
1649 if (time_after(jiffies
, expire
))
1652 uart_port_deref(port
);
1656 * Calls to uart_hangup() are serialised by the tty_lock in
1657 * drivers/tty/tty_io.c:do_tty_hangup()
1658 * This runs from a workqueue and can sleep for a _short_ time only.
1660 static void uart_hangup(struct tty_struct
*tty
)
1662 struct uart_state
*state
= tty
->driver_data
;
1663 struct tty_port
*port
= &state
->port
;
1664 struct uart_port
*uport
;
1665 unsigned long flags
;
1667 pr_debug("uart_hangup(%d)\n", tty
->index
);
1669 mutex_lock(&port
->mutex
);
1670 uport
= uart_port_check(state
);
1671 WARN(!uport
, "hangup of detached port!\n");
1673 if (tty_port_active(port
)) {
1674 uart_flush_buffer(tty
);
1675 uart_shutdown(tty
, state
);
1676 spin_lock_irqsave(&port
->lock
, flags
);
1678 spin_unlock_irqrestore(&port
->lock
, flags
);
1679 tty_port_set_active(port
, 0);
1680 tty_port_tty_set(port
, NULL
);
1681 if (uport
&& !uart_console(uport
))
1682 uart_change_pm(state
, UART_PM_STATE_OFF
);
1683 wake_up_interruptible(&port
->open_wait
);
1684 wake_up_interruptible(&port
->delta_msr_wait
);
1686 mutex_unlock(&port
->mutex
);
1689 /* uport == NULL if uart_port has already been removed */
1690 static void uart_port_shutdown(struct tty_port
*port
)
1692 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1693 struct uart_port
*uport
= uart_port_check(state
);
1696 * clear delta_msr_wait queue to avoid mem leaks: we may free
1697 * the irq here so the queue might never be woken up. Note
1698 * that we won't end up waiting on delta_msr_wait again since
1699 * any outstanding file descriptors should be pointing at
1700 * hung_up_tty_fops now.
1702 wake_up_interruptible(&port
->delta_msr_wait
);
1705 * Free the IRQ and disable the port.
1708 uport
->ops
->shutdown(uport
);
1711 * Ensure that the IRQ handler isn't running on another CPU.
1714 synchronize_irq(uport
->irq
);
1717 static int uart_carrier_raised(struct tty_port
*port
)
1719 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1720 struct uart_port
*uport
;
1723 uport
= uart_port_ref(state
);
1725 * Should never observe uport == NULL since checks for hangup should
1726 * abort the tty_port_block_til_ready() loop before checking for carrier
1727 * raised -- but report carrier raised if it does anyway so open will
1728 * continue and not sleep
1730 if (WARN_ON(!uport
))
1732 spin_lock_irq(&uport
->lock
);
1733 uart_enable_ms(uport
);
1734 mctrl
= uport
->ops
->get_mctrl(uport
);
1735 spin_unlock_irq(&uport
->lock
);
1736 uart_port_deref(uport
);
1737 if (mctrl
& TIOCM_CAR
)
1742 static void uart_dtr_rts(struct tty_port
*port
, int raise
)
1744 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1745 struct uart_port
*uport
;
1747 uport
= uart_port_ref(state
);
1750 uart_port_dtr_rts(uport
, raise
);
1751 uart_port_deref(uport
);
1754 static int uart_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1756 struct uart_driver
*drv
= driver
->driver_state
;
1757 struct uart_state
*state
= drv
->state
+ tty
->index
;
1759 tty
->driver_data
= state
;
1761 return tty_standard_install(driver
, tty
);
1765 * Calls to uart_open are serialised by the tty_lock in
1766 * drivers/tty/tty_io.c:tty_open()
1767 * Note that if this fails, then uart_close() _will_ be called.
1769 * In time, we want to scrap the "opening nonpresent ports"
1770 * behaviour and implement an alternative way for setserial
1771 * to set base addresses/ports/types. This will allow us to
1772 * get rid of a certain amount of extra tests.
1774 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1776 struct uart_state
*state
= tty
->driver_data
;
1779 retval
= tty_port_open(&state
->port
, tty
, filp
);
1786 static int uart_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
1788 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1789 struct uart_port
*uport
;
1792 uport
= uart_port_check(state
);
1793 if (!uport
|| uport
->flags
& UPF_DEAD
)
1797 * Start up the serial port.
1799 ret
= uart_startup(tty
, state
, 0);
1801 tty_port_set_active(port
, 1);
1806 static const char *uart_type(struct uart_port
*port
)
1808 const char *str
= NULL
;
1810 if (port
->ops
->type
)
1811 str
= port
->ops
->type(port
);
1819 #ifdef CONFIG_PROC_FS
1821 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1823 struct uart_state
*state
= drv
->state
+ i
;
1824 struct tty_port
*port
= &state
->port
;
1825 enum uart_pm_state pm_state
;
1826 struct uart_port
*uport
;
1828 unsigned int status
;
1831 mutex_lock(&port
->mutex
);
1832 uport
= uart_port_check(state
);
1836 mmio
= uport
->iotype
>= UPIO_MEM
;
1837 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1838 uport
->line
, uart_type(uport
),
1839 mmio
? "mmio:0x" : "port:",
1840 mmio
? (unsigned long long)uport
->mapbase
1841 : (unsigned long long)uport
->iobase
,
1844 if (uport
->type
== PORT_UNKNOWN
) {
1849 if (capable(CAP_SYS_ADMIN
)) {
1850 pm_state
= state
->pm_state
;
1851 if (pm_state
!= UART_PM_STATE_ON
)
1852 uart_change_pm(state
, UART_PM_STATE_ON
);
1853 spin_lock_irq(&uport
->lock
);
1854 status
= uport
->ops
->get_mctrl(uport
);
1855 spin_unlock_irq(&uport
->lock
);
1856 if (pm_state
!= UART_PM_STATE_ON
)
1857 uart_change_pm(state
, pm_state
);
1859 seq_printf(m
, " tx:%d rx:%d",
1860 uport
->icount
.tx
, uport
->icount
.rx
);
1861 if (uport
->icount
.frame
)
1862 seq_printf(m
, " fe:%d", uport
->icount
.frame
);
1863 if (uport
->icount
.parity
)
1864 seq_printf(m
, " pe:%d", uport
->icount
.parity
);
1865 if (uport
->icount
.brk
)
1866 seq_printf(m
, " brk:%d", uport
->icount
.brk
);
1867 if (uport
->icount
.overrun
)
1868 seq_printf(m
, " oe:%d", uport
->icount
.overrun
);
1869 if (uport
->icount
.buf_overrun
)
1870 seq_printf(m
, " bo:%d", uport
->icount
.buf_overrun
);
1872 #define INFOBIT(bit, str) \
1873 if (uport->mctrl & (bit)) \
1874 strncat(stat_buf, (str), sizeof(stat_buf) - \
1875 strlen(stat_buf) - 2)
1876 #define STATBIT(bit, str) \
1877 if (status & (bit)) \
1878 strncat(stat_buf, (str), sizeof(stat_buf) - \
1879 strlen(stat_buf) - 2)
1883 INFOBIT(TIOCM_RTS
, "|RTS");
1884 STATBIT(TIOCM_CTS
, "|CTS");
1885 INFOBIT(TIOCM_DTR
, "|DTR");
1886 STATBIT(TIOCM_DSR
, "|DSR");
1887 STATBIT(TIOCM_CAR
, "|CD");
1888 STATBIT(TIOCM_RNG
, "|RI");
1892 seq_puts(m
, stat_buf
);
1898 mutex_unlock(&port
->mutex
);
1901 static int uart_proc_show(struct seq_file
*m
, void *v
)
1903 struct tty_driver
*ttydrv
= m
->private;
1904 struct uart_driver
*drv
= ttydrv
->driver_state
;
1907 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1908 for (i
= 0; i
< drv
->nr
; i
++)
1909 uart_line_info(m
, drv
, i
);
1914 static inline bool uart_console_enabled(struct uart_port
*port
)
1916 return uart_console(port
) && (port
->cons
->flags
& CON_ENABLED
);
1919 static void uart_port_spin_lock_init(struct uart_port
*port
)
1921 spin_lock_init(&port
->lock
);
1922 lockdep_set_class(&port
->lock
, &port_lock_key
);
1925 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1927 * uart_console_write - write a console message to a serial port
1928 * @port: the port to write the message
1929 * @s: array of characters
1930 * @count: number of characters in string to write
1931 * @putchar: function to write character to port
1933 void uart_console_write(struct uart_port
*port
, const char *s
,
1935 void (*putchar
)(struct uart_port
*, int))
1939 for (i
= 0; i
< count
; i
++, s
++) {
1941 putchar(port
, '\r');
1945 EXPORT_SYMBOL_GPL(uart_console_write
);
1948 * Check whether an invalid uart number has been specified, and
1949 * if so, search for the first available port that does have
1952 struct uart_port
* __init
1953 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1955 int idx
= co
->index
;
1957 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1958 ports
[idx
].membase
== NULL
))
1959 for (idx
= 0; idx
< nr
; idx
++)
1960 if (ports
[idx
].iobase
!= 0 ||
1961 ports
[idx
].membase
!= NULL
)
1970 * uart_parse_earlycon - Parse earlycon options
1971 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1972 * @iotype: ptr for decoded iotype (out)
1973 * @addr: ptr for decoded mapbase/iobase (out)
1974 * @options: ptr for <options> field; NULL if not present (out)
1976 * Decodes earlycon kernel command line parameters of the form
1977 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1978 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1982 * earlycon=<name>,0x<addr>,<options>
1983 * console=<name>,0x<addr>,<options>
1985 * is also accepted; the returned @iotype will be UPIO_MEM.
1987 * Returns 0 on success or -EINVAL on failure
1989 int uart_parse_earlycon(char *p
, unsigned char *iotype
, resource_size_t
*addr
,
1992 if (strncmp(p
, "mmio,", 5) == 0) {
1995 } else if (strncmp(p
, "mmio16,", 7) == 0) {
1996 *iotype
= UPIO_MEM16
;
1998 } else if (strncmp(p
, "mmio32,", 7) == 0) {
1999 *iotype
= UPIO_MEM32
;
2001 } else if (strncmp(p
, "mmio32be,", 9) == 0) {
2002 *iotype
= UPIO_MEM32BE
;
2004 } else if (strncmp(p
, "mmio32native,", 13) == 0) {
2005 *iotype
= IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) ?
2006 UPIO_MEM32BE
: UPIO_MEM32
;
2008 } else if (strncmp(p
, "io,", 3) == 0) {
2009 *iotype
= UPIO_PORT
;
2011 } else if (strncmp(p
, "0x", 2) == 0) {
2018 * Before you replace it with kstrtoull(), think about options separator
2019 * (',') it will not tolerate
2021 *addr
= simple_strtoull(p
, NULL
, 0);
2029 EXPORT_SYMBOL_GPL(uart_parse_earlycon
);
2032 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2033 * @options: pointer to option string
2034 * @baud: pointer to an 'int' variable for the baud rate.
2035 * @parity: pointer to an 'int' variable for the parity.
2036 * @bits: pointer to an 'int' variable for the number of data bits.
2037 * @flow: pointer to an 'int' variable for the flow control character.
2039 * uart_parse_options decodes a string containing the serial console
2040 * options. The format of the string is <baud><parity><bits><flow>,
2044 uart_parse_options(const char *options
, int *baud
, int *parity
,
2045 int *bits
, int *flow
)
2047 const char *s
= options
;
2049 *baud
= simple_strtoul(s
, NULL
, 10);
2050 while (*s
>= '0' && *s
<= '9')
2059 EXPORT_SYMBOL_GPL(uart_parse_options
);
2062 * uart_set_options - setup the serial console parameters
2063 * @port: pointer to the serial ports uart_port structure
2064 * @co: console pointer
2066 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2067 * @bits: number of data bits
2068 * @flow: flow control character - 'r' (rts)
2071 uart_set_options(struct uart_port
*port
, struct console
*co
,
2072 int baud
, int parity
, int bits
, int flow
)
2074 struct ktermios termios
;
2075 static struct ktermios dummy
;
2078 * Ensure that the serial-console lock is initialised early.
2080 * Note that the console-enabled check is needed because of kgdboc,
2081 * which can end up calling uart_set_options() for an already enabled
2082 * console via tty_find_polling_driver() and uart_poll_init().
2084 if (!uart_console_enabled(port
) && !port
->console_reinit
)
2085 uart_port_spin_lock_init(port
);
2087 memset(&termios
, 0, sizeof(struct ktermios
));
2089 termios
.c_cflag
|= CREAD
| HUPCL
| CLOCAL
;
2090 tty_termios_encode_baud_rate(&termios
, baud
, baud
);
2093 termios
.c_cflag
|= CS7
;
2095 termios
.c_cflag
|= CS8
;
2099 termios
.c_cflag
|= PARODD
;
2102 termios
.c_cflag
|= PARENB
;
2107 termios
.c_cflag
|= CRTSCTS
;
2110 * some uarts on other side don't support no flow control.
2111 * So we set * DTR in host uart to make them happy
2113 port
->mctrl
|= TIOCM_DTR
;
2115 port
->ops
->set_termios(port
, &termios
, &dummy
);
2117 * Allow the setting of the UART parameters with a NULL console
2121 co
->cflag
= termios
.c_cflag
;
2122 co
->ispeed
= termios
.c_ispeed
;
2123 co
->ospeed
= termios
.c_ospeed
;
2128 EXPORT_SYMBOL_GPL(uart_set_options
);
2129 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2132 * uart_change_pm - set power state of the port
2134 * @state: port descriptor
2135 * @pm_state: new state
2137 * Locking: port->mutex has to be held
2139 static void uart_change_pm(struct uart_state
*state
,
2140 enum uart_pm_state pm_state
)
2142 struct uart_port
*port
= uart_port_check(state
);
2144 if (state
->pm_state
!= pm_state
) {
2145 if (port
&& port
->ops
->pm
)
2146 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
2147 state
->pm_state
= pm_state
;
2152 struct uart_port
*port
;
2153 struct uart_driver
*driver
;
2156 static int serial_match_port(struct device
*dev
, void *data
)
2158 struct uart_match
*match
= data
;
2159 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
2160 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
2163 return dev
->devt
== devt
; /* Actually, only one tty per port */
2166 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2168 struct uart_state
*state
= drv
->state
+ uport
->line
;
2169 struct tty_port
*port
= &state
->port
;
2170 struct device
*tty_dev
;
2171 struct uart_match match
= {uport
, drv
};
2173 mutex_lock(&port
->mutex
);
2175 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2176 if (tty_dev
&& device_may_wakeup(tty_dev
)) {
2177 enable_irq_wake(uport
->irq
);
2178 put_device(tty_dev
);
2179 mutex_unlock(&port
->mutex
);
2182 put_device(tty_dev
);
2184 /* Nothing to do if the console is not suspending */
2185 if (!console_suspend_enabled
&& uart_console(uport
))
2188 uport
->suspended
= 1;
2190 if (tty_port_initialized(port
)) {
2191 const struct uart_ops
*ops
= uport
->ops
;
2194 tty_port_set_suspended(port
, 1);
2195 tty_port_set_initialized(port
, 0);
2197 spin_lock_irq(&uport
->lock
);
2198 ops
->stop_tx(uport
);
2199 ops
->set_mctrl(uport
, 0);
2200 ops
->stop_rx(uport
);
2201 spin_unlock_irq(&uport
->lock
);
2204 * Wait for the transmitter to empty.
2206 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
2209 dev_err(uport
->dev
, "%s: Unable to drain transmitter\n",
2212 ops
->shutdown(uport
);
2216 * Disable the console device before suspending.
2218 if (uart_console(uport
))
2219 console_stop(uport
->cons
);
2221 uart_change_pm(state
, UART_PM_STATE_OFF
);
2223 mutex_unlock(&port
->mutex
);
2228 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2230 struct uart_state
*state
= drv
->state
+ uport
->line
;
2231 struct tty_port
*port
= &state
->port
;
2232 struct device
*tty_dev
;
2233 struct uart_match match
= {uport
, drv
};
2234 struct ktermios termios
;
2236 mutex_lock(&port
->mutex
);
2238 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2239 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
2240 if (irqd_is_wakeup_set(irq_get_irq_data((uport
->irq
))))
2241 disable_irq_wake(uport
->irq
);
2242 put_device(tty_dev
);
2243 mutex_unlock(&port
->mutex
);
2246 put_device(tty_dev
);
2247 uport
->suspended
= 0;
2250 * Re-enable the console device after suspending.
2252 if (uart_console(uport
)) {
2254 * First try to use the console cflag setting.
2256 memset(&termios
, 0, sizeof(struct ktermios
));
2257 termios
.c_cflag
= uport
->cons
->cflag
;
2258 termios
.c_ispeed
= uport
->cons
->ispeed
;
2259 termios
.c_ospeed
= uport
->cons
->ospeed
;
2262 * If that's unset, use the tty termios setting.
2264 if (port
->tty
&& termios
.c_cflag
== 0)
2265 termios
= port
->tty
->termios
;
2267 if (console_suspend_enabled
)
2268 uart_change_pm(state
, UART_PM_STATE_ON
);
2269 uport
->ops
->set_termios(uport
, &termios
, NULL
);
2270 if (console_suspend_enabled
)
2271 console_start(uport
->cons
);
2274 if (tty_port_suspended(port
)) {
2275 const struct uart_ops
*ops
= uport
->ops
;
2278 uart_change_pm(state
, UART_PM_STATE_ON
);
2279 spin_lock_irq(&uport
->lock
);
2280 ops
->set_mctrl(uport
, 0);
2281 spin_unlock_irq(&uport
->lock
);
2282 if (console_suspend_enabled
|| !uart_console(uport
)) {
2283 /* Protected by port mutex for now */
2284 struct tty_struct
*tty
= port
->tty
;
2286 ret
= ops
->startup(uport
);
2289 uart_change_speed(tty
, state
, NULL
);
2290 spin_lock_irq(&uport
->lock
);
2291 ops
->set_mctrl(uport
, uport
->mctrl
);
2292 ops
->start_tx(uport
);
2293 spin_unlock_irq(&uport
->lock
);
2294 tty_port_set_initialized(port
, 1);
2297 * Failed to resume - maybe hardware went away?
2298 * Clear the "initialized" flag so we won't try
2299 * to call the low level drivers shutdown method.
2301 uart_shutdown(tty
, state
);
2305 tty_port_set_suspended(port
, 0);
2308 mutex_unlock(&port
->mutex
);
2314 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2318 switch (port
->iotype
) {
2320 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2323 snprintf(address
, sizeof(address
),
2324 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2332 snprintf(address
, sizeof(address
),
2333 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2336 strlcpy(address
, "*unknown*", sizeof(address
));
2340 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2341 port
->dev
? dev_name(port
->dev
) : "",
2342 port
->dev
? ": " : "",
2344 address
, port
->irq
, port
->uartclk
/ 16, uart_type(port
));
2346 /* The magic multiplier feature is a bit obscure, so report it too. */
2347 if (port
->flags
& UPF_MAGIC_MULTIPLIER
)
2348 pr_info("%s%s%s extra baud rates supported: %d, %d",
2349 port
->dev
? dev_name(port
->dev
) : "",
2350 port
->dev
? ": " : "",
2352 port
->uartclk
/ 8, port
->uartclk
/ 4);
2356 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2357 struct uart_port
*port
)
2362 * If there isn't a port here, don't do anything further.
2364 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2368 * Now do the auto configuration stuff. Note that config_port
2369 * is expected to claim the resources and map the port for us.
2372 if (port
->flags
& UPF_AUTO_IRQ
)
2373 flags
|= UART_CONFIG_IRQ
;
2374 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2375 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2376 port
->type
= PORT_UNKNOWN
;
2377 flags
|= UART_CONFIG_TYPE
;
2379 port
->ops
->config_port(port
, flags
);
2382 if (port
->type
!= PORT_UNKNOWN
) {
2383 unsigned long flags
;
2385 uart_report_port(drv
, port
);
2387 /* Power up port for set_mctrl() */
2388 uart_change_pm(state
, UART_PM_STATE_ON
);
2391 * Ensure that the modem control lines are de-activated.
2392 * keep the DTR setting that is set in uart_set_options()
2393 * We probably don't need a spinlock around this, but
2395 spin_lock_irqsave(&port
->lock
, flags
);
2396 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2397 spin_unlock_irqrestore(&port
->lock
, flags
);
2400 * If this driver supports console, and it hasn't been
2401 * successfully registered yet, try to re-register it.
2402 * It may be that the port was not available.
2404 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2405 register_console(port
->cons
);
2408 * Power down all ports by default, except the
2409 * console if we have one.
2411 if (!uart_console(port
))
2412 uart_change_pm(state
, UART_PM_STATE_OFF
);
2416 #ifdef CONFIG_CONSOLE_POLL
2418 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2420 struct uart_driver
*drv
= driver
->driver_state
;
2421 struct uart_state
*state
= drv
->state
+ line
;
2422 struct tty_port
*tport
;
2423 struct uart_port
*port
;
2430 tport
= &state
->port
;
2431 mutex_lock(&tport
->mutex
);
2433 port
= uart_port_check(state
);
2434 if (!port
|| !(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
)) {
2439 if (port
->ops
->poll_init
) {
2441 * We don't set initialized as we only initialized the hw,
2442 * e.g. state->xmit is still uninitialized.
2444 if (!tty_port_initialized(tport
))
2445 ret
= port
->ops
->poll_init(port
);
2448 if (!ret
&& options
) {
2449 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2450 ret
= uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2453 mutex_unlock(&tport
->mutex
);
2457 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2459 struct uart_driver
*drv
= driver
->driver_state
;
2460 struct uart_state
*state
= drv
->state
+ line
;
2461 struct uart_port
*port
;
2464 port
= uart_port_ref(state
);
2466 ret
= port
->ops
->poll_get_char(port
);
2467 uart_port_deref(port
);
2473 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2475 struct uart_driver
*drv
= driver
->driver_state
;
2476 struct uart_state
*state
= drv
->state
+ line
;
2477 struct uart_port
*port
;
2479 port
= uart_port_ref(state
);
2484 port
->ops
->poll_put_char(port
, '\r');
2485 port
->ops
->poll_put_char(port
, ch
);
2486 uart_port_deref(port
);
2490 static const struct tty_operations uart_ops
= {
2491 .install
= uart_install
,
2493 .close
= uart_close
,
2494 .write
= uart_write
,
2495 .put_char
= uart_put_char
,
2496 .flush_chars
= uart_flush_chars
,
2497 .write_room
= uart_write_room
,
2498 .chars_in_buffer
= uart_chars_in_buffer
,
2499 .flush_buffer
= uart_flush_buffer
,
2500 .ioctl
= uart_ioctl
,
2501 .throttle
= uart_throttle
,
2502 .unthrottle
= uart_unthrottle
,
2503 .send_xchar
= uart_send_xchar
,
2504 .set_termios
= uart_set_termios
,
2505 .set_ldisc
= uart_set_ldisc
,
2507 .start
= uart_start
,
2508 .hangup
= uart_hangup
,
2509 .break_ctl
= uart_break_ctl
,
2510 .wait_until_sent
= uart_wait_until_sent
,
2511 #ifdef CONFIG_PROC_FS
2512 .proc_show
= uart_proc_show
,
2514 .tiocmget
= uart_tiocmget
,
2515 .tiocmset
= uart_tiocmset
,
2516 .set_serial
= uart_set_info_user
,
2517 .get_serial
= uart_get_info_user
,
2518 .get_icount
= uart_get_icount
,
2519 #ifdef CONFIG_CONSOLE_POLL
2520 .poll_init
= uart_poll_init
,
2521 .poll_get_char
= uart_poll_get_char
,
2522 .poll_put_char
= uart_poll_put_char
,
2526 static const struct tty_port_operations uart_port_ops
= {
2527 .carrier_raised
= uart_carrier_raised
,
2528 .dtr_rts
= uart_dtr_rts
,
2529 .activate
= uart_port_activate
,
2530 .shutdown
= uart_tty_port_shutdown
,
2534 * uart_register_driver - register a driver with the uart core layer
2535 * @drv: low level driver structure
2537 * Register a uart driver with the core driver. We in turn register
2538 * with the tty layer, and initialise the core driver per-port state.
2540 * We have a proc file in /proc/tty/driver which is named after the
2543 * drv->port should be NULL, and the per-port structures should be
2544 * registered using uart_add_one_port after this call has succeeded.
2546 int uart_register_driver(struct uart_driver
*drv
)
2548 struct tty_driver
*normal
;
2549 int i
, retval
= -ENOMEM
;
2554 * Maybe we should be using a slab cache for this, especially if
2555 * we have a large number of ports to handle.
2557 drv
->state
= kcalloc(drv
->nr
, sizeof(struct uart_state
), GFP_KERNEL
);
2561 normal
= tty_alloc_driver(drv
->nr
, TTY_DRIVER_REAL_RAW
|
2562 TTY_DRIVER_DYNAMIC_DEV
);
2563 if (IS_ERR(normal
)) {
2564 retval
= PTR_ERR(normal
);
2568 drv
->tty_driver
= normal
;
2570 normal
->driver_name
= drv
->driver_name
;
2571 normal
->name
= drv
->dev_name
;
2572 normal
->major
= drv
->major
;
2573 normal
->minor_start
= drv
->minor
;
2574 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2575 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2576 normal
->init_termios
= tty_std_termios
;
2577 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2578 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2579 normal
->driver_state
= drv
;
2580 tty_set_operations(normal
, &uart_ops
);
2583 * Initialise the UART state(s).
2585 for (i
= 0; i
< drv
->nr
; i
++) {
2586 struct uart_state
*state
= drv
->state
+ i
;
2587 struct tty_port
*port
= &state
->port
;
2589 tty_port_init(port
);
2590 port
->ops
= &uart_port_ops
;
2593 retval
= tty_register_driver(normal
);
2597 for (i
= 0; i
< drv
->nr
; i
++)
2598 tty_port_destroy(&drv
->state
[i
].port
);
2599 tty_driver_kref_put(normal
);
2607 * uart_unregister_driver - remove a driver from the uart core layer
2608 * @drv: low level driver structure
2610 * Remove all references to a driver from the core driver. The low
2611 * level driver must have removed all its ports via the
2612 * uart_remove_one_port() if it registered them with uart_add_one_port().
2613 * (ie, drv->port == NULL)
2615 void uart_unregister_driver(struct uart_driver
*drv
)
2617 struct tty_driver
*p
= drv
->tty_driver
;
2620 tty_unregister_driver(p
);
2621 tty_driver_kref_put(p
);
2622 for (i
= 0; i
< drv
->nr
; i
++)
2623 tty_port_destroy(&drv
->state
[i
].port
);
2626 drv
->tty_driver
= NULL
;
2629 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2631 struct uart_driver
*p
= co
->data
;
2633 return p
->tty_driver
;
2635 EXPORT_SYMBOL_GPL(uart_console_device
);
2637 static ssize_t
uartclk_show(struct device
*dev
,
2638 struct device_attribute
*attr
, char *buf
)
2640 struct serial_struct tmp
;
2641 struct tty_port
*port
= dev_get_drvdata(dev
);
2643 uart_get_info(port
, &tmp
);
2644 return sprintf(buf
, "%d\n", tmp
.baud_base
* 16);
2647 static ssize_t
type_show(struct device
*dev
,
2648 struct device_attribute
*attr
, char *buf
)
2650 struct serial_struct tmp
;
2651 struct tty_port
*port
= dev_get_drvdata(dev
);
2653 uart_get_info(port
, &tmp
);
2654 return sprintf(buf
, "%d\n", tmp
.type
);
2657 static ssize_t
line_show(struct device
*dev
,
2658 struct device_attribute
*attr
, char *buf
)
2660 struct serial_struct tmp
;
2661 struct tty_port
*port
= dev_get_drvdata(dev
);
2663 uart_get_info(port
, &tmp
);
2664 return sprintf(buf
, "%d\n", tmp
.line
);
2667 static ssize_t
port_show(struct device
*dev
,
2668 struct device_attribute
*attr
, char *buf
)
2670 struct serial_struct tmp
;
2671 struct tty_port
*port
= dev_get_drvdata(dev
);
2672 unsigned long ioaddr
;
2674 uart_get_info(port
, &tmp
);
2676 if (HIGH_BITS_OFFSET
)
2677 ioaddr
|= (unsigned long)tmp
.port_high
<< HIGH_BITS_OFFSET
;
2678 return sprintf(buf
, "0x%lX\n", ioaddr
);
2681 static ssize_t
irq_show(struct device
*dev
,
2682 struct device_attribute
*attr
, char *buf
)
2684 struct serial_struct tmp
;
2685 struct tty_port
*port
= dev_get_drvdata(dev
);
2687 uart_get_info(port
, &tmp
);
2688 return sprintf(buf
, "%d\n", tmp
.irq
);
2691 static ssize_t
flags_show(struct device
*dev
,
2692 struct device_attribute
*attr
, char *buf
)
2694 struct serial_struct tmp
;
2695 struct tty_port
*port
= dev_get_drvdata(dev
);
2697 uart_get_info(port
, &tmp
);
2698 return sprintf(buf
, "0x%X\n", tmp
.flags
);
2701 static ssize_t
xmit_fifo_size_show(struct device
*dev
,
2702 struct device_attribute
*attr
, char *buf
)
2704 struct serial_struct tmp
;
2705 struct tty_port
*port
= dev_get_drvdata(dev
);
2707 uart_get_info(port
, &tmp
);
2708 return sprintf(buf
, "%d\n", tmp
.xmit_fifo_size
);
2711 static ssize_t
close_delay_show(struct device
*dev
,
2712 struct device_attribute
*attr
, char *buf
)
2714 struct serial_struct tmp
;
2715 struct tty_port
*port
= dev_get_drvdata(dev
);
2717 uart_get_info(port
, &tmp
);
2718 return sprintf(buf
, "%d\n", tmp
.close_delay
);
2721 static ssize_t
closing_wait_show(struct device
*dev
,
2722 struct device_attribute
*attr
, char *buf
)
2724 struct serial_struct tmp
;
2725 struct tty_port
*port
= dev_get_drvdata(dev
);
2727 uart_get_info(port
, &tmp
);
2728 return sprintf(buf
, "%d\n", tmp
.closing_wait
);
2731 static ssize_t
custom_divisor_show(struct device
*dev
,
2732 struct device_attribute
*attr
, char *buf
)
2734 struct serial_struct tmp
;
2735 struct tty_port
*port
= dev_get_drvdata(dev
);
2737 uart_get_info(port
, &tmp
);
2738 return sprintf(buf
, "%d\n", tmp
.custom_divisor
);
2741 static ssize_t
io_type_show(struct device
*dev
,
2742 struct device_attribute
*attr
, char *buf
)
2744 struct serial_struct tmp
;
2745 struct tty_port
*port
= dev_get_drvdata(dev
);
2747 uart_get_info(port
, &tmp
);
2748 return sprintf(buf
, "%d\n", tmp
.io_type
);
2751 static ssize_t
iomem_base_show(struct device
*dev
,
2752 struct device_attribute
*attr
, char *buf
)
2754 struct serial_struct tmp
;
2755 struct tty_port
*port
= dev_get_drvdata(dev
);
2757 uart_get_info(port
, &tmp
);
2758 return sprintf(buf
, "0x%lX\n", (unsigned long)tmp
.iomem_base
);
2761 static ssize_t
iomem_reg_shift_show(struct device
*dev
,
2762 struct device_attribute
*attr
, char *buf
)
2764 struct serial_struct tmp
;
2765 struct tty_port
*port
= dev_get_drvdata(dev
);
2767 uart_get_info(port
, &tmp
);
2768 return sprintf(buf
, "%d\n", tmp
.iomem_reg_shift
);
2771 static ssize_t
console_show(struct device
*dev
,
2772 struct device_attribute
*attr
, char *buf
)
2774 struct tty_port
*port
= dev_get_drvdata(dev
);
2775 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
2776 struct uart_port
*uport
;
2777 bool console
= false;
2779 mutex_lock(&port
->mutex
);
2780 uport
= uart_port_check(state
);
2782 console
= uart_console_enabled(uport
);
2783 mutex_unlock(&port
->mutex
);
2785 return sprintf(buf
, "%c\n", console
? 'Y' : 'N');
2788 static ssize_t
console_store(struct device
*dev
,
2789 struct device_attribute
*attr
, const char *buf
, size_t count
)
2791 struct tty_port
*port
= dev_get_drvdata(dev
);
2792 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
2793 struct uart_port
*uport
;
2794 bool oldconsole
, newconsole
;
2797 ret
= kstrtobool(buf
, &newconsole
);
2801 mutex_lock(&port
->mutex
);
2802 uport
= uart_port_check(state
);
2804 oldconsole
= uart_console_enabled(uport
);
2805 if (oldconsole
&& !newconsole
) {
2806 ret
= unregister_console(uport
->cons
);
2807 } else if (!oldconsole
&& newconsole
) {
2808 if (uart_console(uport
)) {
2809 uport
->console_reinit
= 1;
2810 register_console(uport
->cons
);
2818 mutex_unlock(&port
->mutex
);
2820 return ret
< 0 ? ret
: count
;
2823 static DEVICE_ATTR_RO(uartclk
);
2824 static DEVICE_ATTR_RO(type
);
2825 static DEVICE_ATTR_RO(line
);
2826 static DEVICE_ATTR_RO(port
);
2827 static DEVICE_ATTR_RO(irq
);
2828 static DEVICE_ATTR_RO(flags
);
2829 static DEVICE_ATTR_RO(xmit_fifo_size
);
2830 static DEVICE_ATTR_RO(close_delay
);
2831 static DEVICE_ATTR_RO(closing_wait
);
2832 static DEVICE_ATTR_RO(custom_divisor
);
2833 static DEVICE_ATTR_RO(io_type
);
2834 static DEVICE_ATTR_RO(iomem_base
);
2835 static DEVICE_ATTR_RO(iomem_reg_shift
);
2836 static DEVICE_ATTR_RW(console
);
2838 static struct attribute
*tty_dev_attrs
[] = {
2839 &dev_attr_uartclk
.attr
,
2840 &dev_attr_type
.attr
,
2841 &dev_attr_line
.attr
,
2842 &dev_attr_port
.attr
,
2844 &dev_attr_flags
.attr
,
2845 &dev_attr_xmit_fifo_size
.attr
,
2846 &dev_attr_close_delay
.attr
,
2847 &dev_attr_closing_wait
.attr
,
2848 &dev_attr_custom_divisor
.attr
,
2849 &dev_attr_io_type
.attr
,
2850 &dev_attr_iomem_base
.attr
,
2851 &dev_attr_iomem_reg_shift
.attr
,
2852 &dev_attr_console
.attr
,
2856 static const struct attribute_group tty_dev_attr_group
= {
2857 .attrs
= tty_dev_attrs
,
2861 * uart_add_one_port - attach a driver-defined port structure
2862 * @drv: pointer to the uart low level driver structure for this port
2863 * @uport: uart port structure to use for this port.
2865 * Context: task context, might sleep
2867 * This allows the driver to register its own uart_port structure
2868 * with the core driver. The main purpose is to allow the low
2869 * level uart drivers to expand uart_port, rather than having yet
2870 * more levels of structures.
2872 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2874 struct uart_state
*state
;
2875 struct tty_port
*port
;
2877 struct device
*tty_dev
;
2880 if (uport
->line
>= drv
->nr
)
2883 state
= drv
->state
+ uport
->line
;
2884 port
= &state
->port
;
2886 mutex_lock(&port_mutex
);
2887 mutex_lock(&port
->mutex
);
2888 if (state
->uart_port
) {
2893 /* Link the port to the driver state table and vice versa */
2894 atomic_set(&state
->refcount
, 1);
2895 init_waitqueue_head(&state
->remove_wait
);
2896 state
->uart_port
= uport
;
2897 uport
->state
= state
;
2899 state
->pm_state
= UART_PM_STATE_UNDEFINED
;
2900 uport
->cons
= drv
->cons
;
2901 uport
->minor
= drv
->tty_driver
->minor_start
+ uport
->line
;
2902 uport
->name
= kasprintf(GFP_KERNEL
, "%s%d", drv
->dev_name
,
2903 drv
->tty_driver
->name_base
+ uport
->line
);
2910 * If this port is in use as a console then the spinlock is already
2913 if (!uart_console_enabled(uport
))
2914 uart_port_spin_lock_init(uport
);
2916 if (uport
->cons
&& uport
->dev
)
2917 of_console_check(uport
->dev
->of_node
, uport
->cons
->name
, uport
->line
);
2919 tty_port_link_device(port
, drv
->tty_driver
, uport
->line
);
2920 uart_configure_port(drv
, state
, uport
);
2922 port
->console
= uart_console(uport
);
2925 if (uport
->attr_group
)
2928 uport
->tty_groups
= kcalloc(num_groups
, sizeof(*uport
->tty_groups
),
2930 if (!uport
->tty_groups
) {
2934 uport
->tty_groups
[0] = &tty_dev_attr_group
;
2935 if (uport
->attr_group
)
2936 uport
->tty_groups
[1] = uport
->attr_group
;
2939 * Register the port whether it's detected or not. This allows
2940 * setserial to be used to alter this port's parameters.
2942 tty_dev
= tty_port_register_device_attr_serdev(port
, drv
->tty_driver
,
2943 uport
->line
, uport
->dev
, port
, uport
->tty_groups
);
2944 if (!IS_ERR(tty_dev
)) {
2945 device_set_wakeup_capable(tty_dev
, 1);
2947 dev_err(uport
->dev
, "Cannot register tty device on line %d\n",
2952 * Ensure UPF_DEAD is not set.
2954 uport
->flags
&= ~UPF_DEAD
;
2957 mutex_unlock(&port
->mutex
);
2958 mutex_unlock(&port_mutex
);
2964 * uart_remove_one_port - detach a driver defined port structure
2965 * @drv: pointer to the uart low level driver structure for this port
2966 * @uport: uart port structure for this port
2968 * Context: task context, might sleep
2970 * This unhooks (and hangs up) the specified port structure from the
2971 * core driver. No further calls will be made to the low-level code
2974 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2976 struct uart_state
*state
= drv
->state
+ uport
->line
;
2977 struct tty_port
*port
= &state
->port
;
2978 struct uart_port
*uart_port
;
2979 struct tty_struct
*tty
;
2982 mutex_lock(&port_mutex
);
2985 * Mark the port "dead" - this prevents any opens from
2986 * succeeding while we shut down the port.
2988 mutex_lock(&port
->mutex
);
2989 uart_port
= uart_port_check(state
);
2990 if (uart_port
!= uport
)
2991 dev_alert(uport
->dev
, "Removing wrong port: %p != %p\n",
2995 mutex_unlock(&port
->mutex
);
2999 uport
->flags
|= UPF_DEAD
;
3000 mutex_unlock(&port
->mutex
);
3003 * Remove the devices from the tty layer
3005 tty_port_unregister_device(port
, drv
->tty_driver
, uport
->line
);
3007 tty
= tty_port_tty_get(port
);
3009 tty_vhangup(port
->tty
);
3014 * If the port is used as a console, unregister it
3016 if (uart_console(uport
))
3017 unregister_console(uport
->cons
);
3020 * Free the port IO and memory resources, if any.
3022 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
3023 uport
->ops
->release_port(uport
);
3024 kfree(uport
->tty_groups
);
3028 * Indicate that there isn't a port here anymore.
3030 uport
->type
= PORT_UNKNOWN
;
3032 mutex_lock(&port
->mutex
);
3033 WARN_ON(atomic_dec_return(&state
->refcount
) < 0);
3034 wait_event(state
->remove_wait
, !atomic_read(&state
->refcount
));
3035 state
->uart_port
= NULL
;
3036 mutex_unlock(&port
->mutex
);
3038 mutex_unlock(&port_mutex
);
3044 * Are the two ports equivalent?
3046 bool uart_match_port(const struct uart_port
*port1
,
3047 const struct uart_port
*port2
)
3049 if (port1
->iotype
!= port2
->iotype
)
3052 switch (port1
->iotype
) {
3054 return port1
->iobase
== port2
->iobase
;
3056 return port1
->iobase
== port2
->iobase
&&
3057 port1
->hub6
== port2
->hub6
;
3064 return port1
->mapbase
== port2
->mapbase
;
3069 EXPORT_SYMBOL(uart_match_port
);
3072 * uart_handle_dcd_change - handle a change of carrier detect state
3073 * @uport: uart_port structure for the open port
3074 * @status: new carrier detect status, nonzero if active
3076 * Caller must hold uport->lock
3078 void uart_handle_dcd_change(struct uart_port
*uport
, unsigned int status
)
3080 struct tty_port
*port
= &uport
->state
->port
;
3081 struct tty_struct
*tty
= port
->tty
;
3082 struct tty_ldisc
*ld
;
3084 lockdep_assert_held_once(&uport
->lock
);
3087 ld
= tty_ldisc_ref(tty
);
3089 if (ld
->ops
->dcd_change
)
3090 ld
->ops
->dcd_change(tty
, status
);
3091 tty_ldisc_deref(ld
);
3095 uport
->icount
.dcd
++;
3097 if (uart_dcd_enabled(uport
)) {
3099 wake_up_interruptible(&port
->open_wait
);
3104 EXPORT_SYMBOL_GPL(uart_handle_dcd_change
);
3107 * uart_handle_cts_change - handle a change of clear-to-send state
3108 * @uport: uart_port structure for the open port
3109 * @status: new clear to send status, nonzero if active
3111 * Caller must hold uport->lock
3113 void uart_handle_cts_change(struct uart_port
*uport
, unsigned int status
)
3115 lockdep_assert_held_once(&uport
->lock
);
3117 uport
->icount
.cts
++;
3119 if (uart_softcts_mode(uport
)) {
3120 if (uport
->hw_stopped
) {
3122 uport
->hw_stopped
= 0;
3123 uport
->ops
->start_tx(uport
);
3124 uart_write_wakeup(uport
);
3128 uport
->hw_stopped
= 1;
3129 uport
->ops
->stop_tx(uport
);
3135 EXPORT_SYMBOL_GPL(uart_handle_cts_change
);
3138 * uart_insert_char - push a char to the uart layer
3140 * User is responsible to call tty_flip_buffer_push when they are done with
3143 * @port: corresponding port
3144 * @status: state of the serial port RX buffer (LSR for 8250)
3145 * @overrun: mask of overrun bits in @status
3146 * @ch: character to push
3147 * @flag: flag for the character (see TTY_NORMAL and friends)
3149 void uart_insert_char(struct uart_port
*port
, unsigned int status
,
3150 unsigned int overrun
, unsigned int ch
, unsigned int flag
)
3152 struct tty_port
*tport
= &port
->state
->port
;
3154 if ((status
& port
->ignore_status_mask
& ~overrun
) == 0)
3155 if (tty_insert_flip_char(tport
, ch
, flag
) == 0)
3156 ++port
->icount
.buf_overrun
;
3159 * Overrun is special. Since it's reported immediately,
3160 * it doesn't affect the current character.
3162 if (status
& ~port
->ignore_status_mask
& overrun
)
3163 if (tty_insert_flip_char(tport
, 0, TTY_OVERRUN
) == 0)
3164 ++port
->icount
.buf_overrun
;
3166 EXPORT_SYMBOL_GPL(uart_insert_char
);
3168 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3169 static const char sysrq_toggle_seq
[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE
;
3171 static void uart_sysrq_on(struct work_struct
*w
)
3173 int sysrq_toggle_seq_len
= strlen(sysrq_toggle_seq
);
3175 sysrq_toggle_support(1);
3176 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3177 sysrq_toggle_seq_len
, sysrq_toggle_seq
);
3179 static DECLARE_WORK(sysrq_enable_work
, uart_sysrq_on
);
3182 * uart_try_toggle_sysrq - Enables SysRq from serial line
3183 * @port: uart_port structure where char(s) after BREAK met
3184 * @ch: new character in the sequence after received BREAK
3186 * Enables magic SysRq when the required sequence is met on port
3187 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3189 * Returns false if @ch is out of enabling sequence and should be
3190 * handled some other way, true if @ch was consumed.
3192 bool uart_try_toggle_sysrq(struct uart_port
*port
, unsigned int ch
)
3194 int sysrq_toggle_seq_len
= strlen(sysrq_toggle_seq
);
3196 if (!sysrq_toggle_seq_len
)
3199 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq
) >= U8_MAX
);
3200 if (sysrq_toggle_seq
[port
->sysrq_seq
] != ch
) {
3201 port
->sysrq_seq
= 0;
3205 if (++port
->sysrq_seq
< sysrq_toggle_seq_len
) {
3206 port
->sysrq
= jiffies
+ SYSRQ_TIMEOUT
;
3210 schedule_work(&sysrq_enable_work
);
3215 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq
);
3218 EXPORT_SYMBOL(uart_write_wakeup
);
3219 EXPORT_SYMBOL(uart_register_driver
);
3220 EXPORT_SYMBOL(uart_unregister_driver
);
3221 EXPORT_SYMBOL(uart_suspend_port
);
3222 EXPORT_SYMBOL(uart_resume_port
);
3223 EXPORT_SYMBOL(uart_add_one_port
);
3224 EXPORT_SYMBOL(uart_remove_one_port
);
3227 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3228 * @port: uart device's target port
3230 * This function implements the device tree binding described in
3231 * Documentation/devicetree/bindings/serial/rs485.txt.
3233 int uart_get_rs485_mode(struct uart_port
*port
)
3235 struct serial_rs485
*rs485conf
= &port
->rs485
;
3236 struct device
*dev
= port
->dev
;
3240 ret
= device_property_read_u32_array(dev
, "rs485-rts-delay",
3243 rs485conf
->delay_rts_before_send
= rs485_delay
[0];
3244 rs485conf
->delay_rts_after_send
= rs485_delay
[1];
3246 rs485conf
->delay_rts_before_send
= 0;
3247 rs485conf
->delay_rts_after_send
= 0;
3251 * Clear full-duplex and enabled flags, set RTS polarity to active high
3252 * to get to a defined state with the following properties:
3254 rs485conf
->flags
&= ~(SER_RS485_RX_DURING_TX
| SER_RS485_ENABLED
|
3255 SER_RS485_TERMINATE_BUS
|
3256 SER_RS485_RTS_AFTER_SEND
);
3257 rs485conf
->flags
|= SER_RS485_RTS_ON_SEND
;
3259 if (device_property_read_bool(dev
, "rs485-rx-during-tx"))
3260 rs485conf
->flags
|= SER_RS485_RX_DURING_TX
;
3262 if (device_property_read_bool(dev
, "linux,rs485-enabled-at-boot-time"))
3263 rs485conf
->flags
|= SER_RS485_ENABLED
;
3265 if (device_property_read_bool(dev
, "rs485-rts-active-low")) {
3266 rs485conf
->flags
&= ~SER_RS485_RTS_ON_SEND
;
3267 rs485conf
->flags
|= SER_RS485_RTS_AFTER_SEND
;
3271 * Disabling termination by default is the safe choice: Else if many
3272 * bus participants enable it, no communication is possible at all.
3273 * Works fine for short cables and users may enable for longer cables.
3275 port
->rs485_term_gpio
= devm_gpiod_get_optional(dev
, "rs485-term",
3277 if (IS_ERR(port
->rs485_term_gpio
)) {
3278 ret
= PTR_ERR(port
->rs485_term_gpio
);
3279 port
->rs485_term_gpio
= NULL
;
3280 return dev_err_probe(dev
, ret
, "Cannot get rs485-term-gpios\n");
3285 EXPORT_SYMBOL_GPL(uart_get_rs485_mode
);
3287 MODULE_DESCRIPTION("Serial driver core");
3288 MODULE_LICENSE("GPL");