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/sched/signal.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/device.h>
34 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
35 #include <linux/serial_core.h>
36 #include <linux/delay.h>
37 #include <linux/mutex.h>
39 #include <linux/irq.h>
40 #include <linux/uaccess.h>
43 * This is used to lock changes in serial line configuration.
45 static DEFINE_MUTEX(port_mutex
);
48 * lockdep: port->lock is initialized in two places, but we
49 * want only one lock-class:
51 static struct lock_class_key port_lock_key
;
53 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
55 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
56 struct ktermios
*old_termios
);
57 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
58 static void uart_change_pm(struct uart_state
*state
,
59 enum uart_pm_state pm_state
);
61 static void uart_port_shutdown(struct tty_port
*port
);
63 static int uart_dcd_enabled(struct uart_port
*uport
)
65 return !!(uport
->status
& UPSTAT_DCD_ENABLE
);
68 static inline struct uart_port
*uart_port_ref(struct uart_state
*state
)
70 if (atomic_add_unless(&state
->refcount
, 1, 0))
71 return state
->uart_port
;
75 static inline void uart_port_deref(struct uart_port
*uport
)
77 if (atomic_dec_and_test(&uport
->state
->refcount
))
78 wake_up(&uport
->state
->remove_wait
);
81 #define uart_port_lock(state, flags) \
83 struct uart_port *__uport = uart_port_ref(state); \
85 spin_lock_irqsave(&__uport->lock, flags); \
89 #define uart_port_unlock(uport, flags) \
91 struct uart_port *__uport = uport; \
93 spin_unlock_irqrestore(&__uport->lock, flags); \
94 uart_port_deref(__uport); \
98 static inline struct uart_port
*uart_port_check(struct uart_state
*state
)
100 lockdep_assert_held(&state
->port
.mutex
);
101 return state
->uart_port
;
105 * This routine is used by the interrupt handler to schedule processing in
106 * the software interrupt portion of the driver.
108 void uart_write_wakeup(struct uart_port
*port
)
110 struct uart_state
*state
= port
->state
;
112 * This means you called this function _after_ the port was
113 * closed. No cookie for you.
116 tty_port_tty_wakeup(&state
->port
);
119 static void uart_stop(struct tty_struct
*tty
)
121 struct uart_state
*state
= tty
->driver_data
;
122 struct uart_port
*port
;
125 port
= uart_port_lock(state
, flags
);
127 port
->ops
->stop_tx(port
);
128 uart_port_unlock(port
, flags
);
131 static void __uart_start(struct tty_struct
*tty
)
133 struct uart_state
*state
= tty
->driver_data
;
134 struct uart_port
*port
= state
->uart_port
;
136 if (port
&& !uart_tx_stopped(port
))
137 port
->ops
->start_tx(port
);
140 static void uart_start(struct tty_struct
*tty
)
142 struct uart_state
*state
= tty
->driver_data
;
143 struct uart_port
*port
;
146 port
= uart_port_lock(state
, flags
);
148 uart_port_unlock(port
, flags
);
152 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
157 spin_lock_irqsave(&port
->lock
, flags
);
159 port
->mctrl
= (old
& ~clear
) | set
;
160 if (old
!= port
->mctrl
)
161 port
->ops
->set_mctrl(port
, port
->mctrl
);
162 spin_unlock_irqrestore(&port
->lock
, flags
);
165 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
166 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
168 static void uart_port_dtr_rts(struct uart_port
*uport
, int raise
)
170 int rs485_on
= uport
->rs485_config
&&
171 (uport
->rs485
.flags
& SER_RS485_ENABLED
);
172 int RTS_after_send
= !!(uport
->rs485
.flags
& SER_RS485_RTS_AFTER_SEND
);
175 if (rs485_on
&& !RTS_after_send
) {
176 uart_set_mctrl(uport
, TIOCM_DTR
);
177 uart_clear_mctrl(uport
, TIOCM_RTS
);
179 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
182 unsigned int clear
= TIOCM_DTR
;
184 clear
|= (!rs485_on
|| !RTS_after_send
) ? TIOCM_RTS
: 0;
185 uart_clear_mctrl(uport
, clear
);
190 * Startup the port. This will be called once per open. All calls
191 * will be serialised by the per-port mutex.
193 static int uart_port_startup(struct tty_struct
*tty
, struct uart_state
*state
,
196 struct uart_port
*uport
= uart_port_check(state
);
200 if (uport
->type
== PORT_UNKNOWN
)
204 * Make sure the device is in D0 state.
206 uart_change_pm(state
, UART_PM_STATE_ON
);
209 * Initialise and allocate the transmit and temporary
212 if (!state
->xmit
.buf
) {
213 /* This is protected by the per port mutex */
214 page
= get_zeroed_page(GFP_KERNEL
);
218 state
->xmit
.buf
= (unsigned char *) page
;
219 uart_circ_clear(&state
->xmit
);
222 retval
= uport
->ops
->startup(uport
);
224 if (uart_console(uport
) && uport
->cons
->cflag
) {
225 tty
->termios
.c_cflag
= uport
->cons
->cflag
;
226 uport
->cons
->cflag
= 0;
229 * Initialise the hardware port settings.
231 uart_change_speed(tty
, state
, NULL
);
234 * Setup the RTS and DTR signals once the
235 * port is open and ready to respond.
237 if (init_hw
&& C_BAUD(tty
))
238 uart_port_dtr_rts(uport
, 1);
242 * This is to allow setserial on this port. People may want to set
243 * port/irq/type and then reconfigure the port properly if it failed
246 if (retval
&& capable(CAP_SYS_ADMIN
))
252 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
,
255 struct tty_port
*port
= &state
->port
;
258 if (tty_port_initialized(port
))
261 retval
= uart_port_startup(tty
, state
, init_hw
);
263 set_bit(TTY_IO_ERROR
, &tty
->flags
);
269 * This routine will shutdown a serial port; interrupts are disabled, and
270 * DTR is dropped if the hangup on close termio flag is on. Calls to
271 * uart_shutdown are serialised by the per-port semaphore.
273 * uport == NULL if uart_port has already been removed
275 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
277 struct uart_port
*uport
= uart_port_check(state
);
278 struct tty_port
*port
= &state
->port
;
281 * Set the TTY IO error marker
284 set_bit(TTY_IO_ERROR
, &tty
->flags
);
286 if (tty_port_initialized(port
)) {
287 tty_port_set_initialized(port
, 0);
290 * Turn off DTR and RTS early.
292 if (uport
&& uart_console(uport
) && tty
)
293 uport
->cons
->cflag
= tty
->termios
.c_cflag
;
295 if (!tty
|| C_HUPCL(tty
))
296 uart_port_dtr_rts(uport
, 0);
298 uart_port_shutdown(port
);
302 * It's possible for shutdown to be called after suspend if we get
303 * a DCD drop (hangup) at just the right time. Clear suspended bit so
304 * we don't try to resume a port that has been shutdown.
306 tty_port_set_suspended(port
, 0);
309 * Free the transmit buffer page.
311 if (state
->xmit
.buf
) {
312 free_page((unsigned long)state
->xmit
.buf
);
313 state
->xmit
.buf
= NULL
;
318 * uart_update_timeout - update per-port FIFO timeout.
319 * @port: uart_port structure describing the port
320 * @cflag: termios cflag value
321 * @baud: speed of the port
323 * Set the port FIFO timeout value. The @cflag value should
324 * reflect the actual hardware settings.
327 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
332 /* byte size and parity */
333 switch (cflag
& CSIZE
) {
354 * The total number of bits to be transmitted in the fifo.
356 bits
= bits
* port
->fifosize
;
359 * Figure the timeout to send the above number of bits.
360 * Add .02 seconds of slop
362 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
365 EXPORT_SYMBOL(uart_update_timeout
);
368 * uart_get_baud_rate - return baud rate for a particular port
369 * @port: uart_port structure describing the port in question.
370 * @termios: desired termios settings.
371 * @old: old termios (or NULL)
372 * @min: minimum acceptable baud rate
373 * @max: maximum acceptable baud rate
375 * Decode the termios structure into a numeric baud rate,
376 * taking account of the magic 38400 baud rate (with spd_*
377 * flags), and mapping the %B0 rate to 9600 baud.
379 * If the new baud rate is invalid, try the old termios setting.
380 * If it's still invalid, we try 9600 baud.
382 * Update the @termios structure to reflect the baud rate
383 * we're actually going to be using. Don't do this for the case
384 * where B0 is requested ("hang up").
387 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
388 struct ktermios
*old
, unsigned int min
, unsigned int max
)
392 unsigned int altbaud
;
394 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
414 for (try = 0; try < 2; try++) {
415 baud
= tty_termios_baud_rate(termios
);
418 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
421 if (try == 0 && baud
== 38400)
425 * Special case: B0 rate.
432 if (baud
>= min
&& baud
<= max
)
436 * Oops, the quotient was zero. Try again with
437 * the old baud rate if possible.
439 termios
->c_cflag
&= ~CBAUD
;
441 baud
= tty_termios_baud_rate(old
);
443 tty_termios_encode_baud_rate(termios
,
450 * As a last resort, if the range cannot be met then clip to
451 * the nearest chip supported rate.
455 tty_termios_encode_baud_rate(termios
,
458 tty_termios_encode_baud_rate(termios
,
462 /* Should never happen */
467 EXPORT_SYMBOL(uart_get_baud_rate
);
470 * uart_get_divisor - return uart clock divisor
471 * @port: uart_port structure describing the port.
472 * @baud: desired baud rate
474 * Calculate the uart clock divisor for the port.
477 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
482 * Old custom speed handling.
484 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
485 quot
= port
->custom_divisor
;
487 quot
= DIV_ROUND_CLOSEST(port
->uartclk
, 16 * baud
);
492 EXPORT_SYMBOL(uart_get_divisor
);
494 /* Caller holds port mutex */
495 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
496 struct ktermios
*old_termios
)
498 struct uart_port
*uport
= uart_port_check(state
);
499 struct ktermios
*termios
;
503 * If we have no tty, termios, or the port does not exist,
504 * then we can't set the parameters for this port.
506 if (!tty
|| uport
->type
== PORT_UNKNOWN
)
509 termios
= &tty
->termios
;
510 uport
->ops
->set_termios(uport
, termios
, old_termios
);
513 * Set modem status enables based on termios cflag
515 spin_lock_irq(&uport
->lock
);
516 if (termios
->c_cflag
& CRTSCTS
)
517 uport
->status
|= UPSTAT_CTS_ENABLE
;
519 uport
->status
&= ~UPSTAT_CTS_ENABLE
;
521 if (termios
->c_cflag
& CLOCAL
)
522 uport
->status
&= ~UPSTAT_DCD_ENABLE
;
524 uport
->status
|= UPSTAT_DCD_ENABLE
;
526 /* reset sw-assisted CTS flow control based on (possibly) new mode */
527 hw_stopped
= uport
->hw_stopped
;
528 uport
->hw_stopped
= uart_softcts_mode(uport
) &&
529 !(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
);
530 if (uport
->hw_stopped
) {
532 uport
->ops
->stop_tx(uport
);
537 spin_unlock_irq(&uport
->lock
);
540 static int uart_put_char(struct tty_struct
*tty
, unsigned char c
)
542 struct uart_state
*state
= tty
->driver_data
;
543 struct uart_port
*port
;
544 struct circ_buf
*circ
;
552 port
= uart_port_lock(state
, flags
);
553 if (port
&& uart_circ_chars_free(circ
) != 0) {
554 circ
->buf
[circ
->head
] = c
;
555 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
558 uart_port_unlock(port
, flags
);
562 static void uart_flush_chars(struct tty_struct
*tty
)
567 static int uart_write(struct tty_struct
*tty
,
568 const unsigned char *buf
, int count
)
570 struct uart_state
*state
= tty
->driver_data
;
571 struct uart_port
*port
;
572 struct circ_buf
*circ
;
577 * This means you called this function _after_ the port was
578 * closed. No cookie for you.
589 port
= uart_port_lock(state
, flags
);
591 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
596 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
597 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
604 uart_port_unlock(port
, flags
);
608 static int uart_write_room(struct tty_struct
*tty
)
610 struct uart_state
*state
= tty
->driver_data
;
611 struct uart_port
*port
;
615 port
= uart_port_lock(state
, flags
);
616 ret
= uart_circ_chars_free(&state
->xmit
);
617 uart_port_unlock(port
, flags
);
621 static int uart_chars_in_buffer(struct tty_struct
*tty
)
623 struct uart_state
*state
= tty
->driver_data
;
624 struct uart_port
*port
;
628 port
= uart_port_lock(state
, flags
);
629 ret
= uart_circ_chars_pending(&state
->xmit
);
630 uart_port_unlock(port
, flags
);
634 static void uart_flush_buffer(struct tty_struct
*tty
)
636 struct uart_state
*state
= tty
->driver_data
;
637 struct uart_port
*port
;
641 * This means you called this function _after_ the port was
642 * closed. No cookie for you.
649 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
651 port
= uart_port_lock(state
, flags
);
654 uart_circ_clear(&state
->xmit
);
655 if (port
->ops
->flush_buffer
)
656 port
->ops
->flush_buffer(port
);
657 uart_port_unlock(port
, flags
);
658 tty_port_tty_wakeup(&state
->port
);
662 * This function is used to send a high-priority XON/XOFF character to
665 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
667 struct uart_state
*state
= tty
->driver_data
;
668 struct uart_port
*port
;
671 port
= uart_port_ref(state
);
675 if (port
->ops
->send_xchar
)
676 port
->ops
->send_xchar(port
, ch
);
678 spin_lock_irqsave(&port
->lock
, flags
);
681 port
->ops
->start_tx(port
);
682 spin_unlock_irqrestore(&port
->lock
, flags
);
684 uart_port_deref(port
);
687 static void uart_throttle(struct tty_struct
*tty
)
689 struct uart_state
*state
= tty
->driver_data
;
690 struct uart_port
*port
;
693 port
= uart_port_ref(state
);
698 mask
|= UPSTAT_AUTOXOFF
;
700 mask
|= UPSTAT_AUTORTS
;
702 if (port
->status
& mask
) {
703 port
->ops
->throttle(port
);
704 mask
&= ~port
->status
;
707 if (mask
& UPSTAT_AUTORTS
)
708 uart_clear_mctrl(port
, TIOCM_RTS
);
710 if (mask
& UPSTAT_AUTOXOFF
)
711 uart_send_xchar(tty
, STOP_CHAR(tty
));
713 uart_port_deref(port
);
716 static void uart_unthrottle(struct tty_struct
*tty
)
718 struct uart_state
*state
= tty
->driver_data
;
719 struct uart_port
*port
;
722 port
= uart_port_ref(state
);
727 mask
|= UPSTAT_AUTOXOFF
;
729 mask
|= UPSTAT_AUTORTS
;
731 if (port
->status
& mask
) {
732 port
->ops
->unthrottle(port
);
733 mask
&= ~port
->status
;
736 if (mask
& UPSTAT_AUTORTS
)
737 uart_set_mctrl(port
, TIOCM_RTS
);
739 if (mask
& UPSTAT_AUTOXOFF
)
740 uart_send_xchar(tty
, START_CHAR(tty
));
742 uart_port_deref(port
);
745 static int uart_get_info(struct tty_port
*port
, struct serial_struct
*retinfo
)
747 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
748 struct uart_port
*uport
;
751 memset(retinfo
, 0, sizeof(*retinfo
));
754 * Ensure the state we copy is consistent and no hardware changes
757 mutex_lock(&port
->mutex
);
758 uport
= uart_port_check(state
);
762 retinfo
->type
= uport
->type
;
763 retinfo
->line
= uport
->line
;
764 retinfo
->port
= uport
->iobase
;
765 if (HIGH_BITS_OFFSET
)
766 retinfo
->port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
767 retinfo
->irq
= uport
->irq
;
768 retinfo
->flags
= (__force
int)uport
->flags
;
769 retinfo
->xmit_fifo_size
= uport
->fifosize
;
770 retinfo
->baud_base
= uport
->uartclk
/ 16;
771 retinfo
->close_delay
= jiffies_to_msecs(port
->close_delay
) / 10;
772 retinfo
->closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
773 ASYNC_CLOSING_WAIT_NONE
:
774 jiffies_to_msecs(port
->closing_wait
) / 10;
775 retinfo
->custom_divisor
= uport
->custom_divisor
;
776 retinfo
->hub6
= uport
->hub6
;
777 retinfo
->io_type
= uport
->iotype
;
778 retinfo
->iomem_reg_shift
= uport
->regshift
;
779 retinfo
->iomem_base
= (void *)(unsigned long)uport
->mapbase
;
783 mutex_unlock(&port
->mutex
);
787 static int uart_get_info_user(struct tty_port
*port
,
788 struct serial_struct __user
*retinfo
)
790 struct serial_struct tmp
;
792 if (uart_get_info(port
, &tmp
) < 0)
795 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
800 static int uart_set_info(struct tty_struct
*tty
, struct tty_port
*port
,
801 struct uart_state
*state
,
802 struct serial_struct
*new_info
)
804 struct uart_port
*uport
= uart_port_check(state
);
805 unsigned long new_port
;
806 unsigned int change_irq
, change_port
, closing_wait
;
807 unsigned int old_custom_divisor
, close_delay
;
808 upf_t old_flags
, new_flags
;
814 new_port
= new_info
->port
;
815 if (HIGH_BITS_OFFSET
)
816 new_port
+= (unsigned long) new_info
->port_high
<< HIGH_BITS_OFFSET
;
818 new_info
->irq
= irq_canonicalize(new_info
->irq
);
819 close_delay
= msecs_to_jiffies(new_info
->close_delay
* 10);
820 closing_wait
= new_info
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
821 ASYNC_CLOSING_WAIT_NONE
:
822 msecs_to_jiffies(new_info
->closing_wait
* 10);
825 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
826 && new_info
->irq
!= uport
->irq
;
829 * Since changing the 'type' of the port changes its resource
830 * allocations, we should treat type changes the same as
833 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
834 && (new_port
!= uport
->iobase
||
835 (unsigned long)new_info
->iomem_base
!= uport
->mapbase
||
836 new_info
->hub6
!= uport
->hub6
||
837 new_info
->io_type
!= uport
->iotype
||
838 new_info
->iomem_reg_shift
!= uport
->regshift
||
839 new_info
->type
!= uport
->type
);
841 old_flags
= uport
->flags
;
842 new_flags
= (__force upf_t
)new_info
->flags
;
843 old_custom_divisor
= uport
->custom_divisor
;
845 if (!capable(CAP_SYS_ADMIN
)) {
847 if (change_irq
|| change_port
||
848 (new_info
->baud_base
!= uport
->uartclk
/ 16) ||
849 (close_delay
!= port
->close_delay
) ||
850 (closing_wait
!= port
->closing_wait
) ||
851 (new_info
->xmit_fifo_size
&&
852 new_info
->xmit_fifo_size
!= uport
->fifosize
) ||
853 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
855 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
856 (new_flags
& UPF_USR_MASK
));
857 uport
->custom_divisor
= new_info
->custom_divisor
;
862 * Ask the low level driver to verify the settings.
864 if (uport
->ops
->verify_port
)
865 retval
= uport
->ops
->verify_port(uport
, new_info
);
867 if ((new_info
->irq
>= nr_irqs
) || (new_info
->irq
< 0) ||
868 (new_info
->baud_base
< 9600))
874 if (change_port
|| change_irq
) {
878 * Make sure that we are the sole user of this port.
880 if (tty_port_users(port
) > 1)
884 * We need to shutdown the serial port at the old
885 * port/type/irq combination.
887 uart_shutdown(tty
, state
);
891 unsigned long old_iobase
, old_mapbase
;
892 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
894 old_iobase
= uport
->iobase
;
895 old_mapbase
= uport
->mapbase
;
896 old_type
= uport
->type
;
897 old_hub6
= uport
->hub6
;
898 old_iotype
= uport
->iotype
;
899 old_shift
= uport
->regshift
;
902 * Free and release old regions
904 if (old_type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
905 uport
->ops
->release_port(uport
);
907 uport
->iobase
= new_port
;
908 uport
->type
= new_info
->type
;
909 uport
->hub6
= new_info
->hub6
;
910 uport
->iotype
= new_info
->io_type
;
911 uport
->regshift
= new_info
->iomem_reg_shift
;
912 uport
->mapbase
= (unsigned long)new_info
->iomem_base
;
915 * Claim and map the new regions
917 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->request_port
) {
918 retval
= uport
->ops
->request_port(uport
);
920 /* Always success - Jean II */
925 * If we fail to request resources for the
926 * new port, try to restore the old settings.
929 uport
->iobase
= old_iobase
;
930 uport
->type
= old_type
;
931 uport
->hub6
= old_hub6
;
932 uport
->iotype
= old_iotype
;
933 uport
->regshift
= old_shift
;
934 uport
->mapbase
= old_mapbase
;
936 if (old_type
!= PORT_UNKNOWN
) {
937 retval
= uport
->ops
->request_port(uport
);
939 * If we failed to restore the old settings,
943 uport
->type
= PORT_UNKNOWN
;
951 /* Added to return the correct error -Ram Gupta */
957 uport
->irq
= new_info
->irq
;
958 if (!(uport
->flags
& UPF_FIXED_PORT
))
959 uport
->uartclk
= new_info
->baud_base
* 16;
960 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
961 (new_flags
& UPF_CHANGE_MASK
);
962 uport
->custom_divisor
= new_info
->custom_divisor
;
963 port
->close_delay
= close_delay
;
964 port
->closing_wait
= closing_wait
;
965 if (new_info
->xmit_fifo_size
)
966 uport
->fifosize
= new_info
->xmit_fifo_size
;
967 port
->low_latency
= (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
971 if (uport
->type
== PORT_UNKNOWN
)
973 if (tty_port_initialized(port
)) {
974 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
975 old_custom_divisor
!= uport
->custom_divisor
) {
977 * If they're setting up a custom divisor or speed,
978 * instead of clearing it, then bitch about it.
980 if (uport
->flags
& UPF_SPD_MASK
) {
981 dev_notice_ratelimited(uport
->dev
,
982 "%s sets custom speed on %s. This is deprecated.\n",
984 tty_name(port
->tty
));
986 uart_change_speed(tty
, state
, NULL
);
989 retval
= uart_startup(tty
, state
, 1);
997 static int uart_set_info_user(struct tty_struct
*tty
, struct uart_state
*state
,
998 struct serial_struct __user
*newinfo
)
1000 struct serial_struct new_serial
;
1001 struct tty_port
*port
= &state
->port
;
1004 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
1008 * This semaphore protects port->count. It is also
1009 * very useful to prevent opens. Also, take the
1010 * port configuration semaphore to make sure that a
1011 * module insertion/removal doesn't change anything
1014 mutex_lock(&port
->mutex
);
1015 retval
= uart_set_info(tty
, port
, state
, &new_serial
);
1016 mutex_unlock(&port
->mutex
);
1021 * uart_get_lsr_info - get line status register info
1022 * @tty: tty associated with the UART
1023 * @state: UART being queried
1024 * @value: returned modem value
1026 static int uart_get_lsr_info(struct tty_struct
*tty
,
1027 struct uart_state
*state
, unsigned int __user
*value
)
1029 struct uart_port
*uport
= uart_port_check(state
);
1030 unsigned int result
;
1032 result
= uport
->ops
->tx_empty(uport
);
1035 * If we're about to load something into the transmit
1036 * register, we'll pretend the transmitter isn't empty to
1037 * avoid a race condition (depending on when the transmit
1038 * interrupt happens).
1040 if (uport
->x_char
||
1041 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
1042 !uart_tx_stopped(uport
)))
1043 result
&= ~TIOCSER_TEMT
;
1045 return put_user(result
, value
);
1048 static int uart_tiocmget(struct tty_struct
*tty
)
1050 struct uart_state
*state
= tty
->driver_data
;
1051 struct tty_port
*port
= &state
->port
;
1052 struct uart_port
*uport
;
1055 mutex_lock(&port
->mutex
);
1056 uport
= uart_port_check(state
);
1060 if (!tty_io_error(tty
)) {
1061 result
= uport
->mctrl
;
1062 spin_lock_irq(&uport
->lock
);
1063 result
|= uport
->ops
->get_mctrl(uport
);
1064 spin_unlock_irq(&uport
->lock
);
1067 mutex_unlock(&port
->mutex
);
1072 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
1074 struct uart_state
*state
= tty
->driver_data
;
1075 struct tty_port
*port
= &state
->port
;
1076 struct uart_port
*uport
;
1079 mutex_lock(&port
->mutex
);
1080 uport
= uart_port_check(state
);
1084 if (!tty_io_error(tty
)) {
1085 uart_update_mctrl(uport
, set
, clear
);
1089 mutex_unlock(&port
->mutex
);
1093 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
1095 struct uart_state
*state
= tty
->driver_data
;
1096 struct tty_port
*port
= &state
->port
;
1097 struct uart_port
*uport
;
1100 mutex_lock(&port
->mutex
);
1101 uport
= uart_port_check(state
);
1105 if (uport
->type
!= PORT_UNKNOWN
)
1106 uport
->ops
->break_ctl(uport
, break_state
);
1109 mutex_unlock(&port
->mutex
);
1113 static int uart_do_autoconfig(struct tty_struct
*tty
,struct uart_state
*state
)
1115 struct tty_port
*port
= &state
->port
;
1116 struct uart_port
*uport
;
1119 if (!capable(CAP_SYS_ADMIN
))
1123 * Take the per-port semaphore. This prevents count from
1124 * changing, and hence any extra opens of the port while
1125 * we're auto-configuring.
1127 if (mutex_lock_interruptible(&port
->mutex
))
1128 return -ERESTARTSYS
;
1130 uport
= uart_port_check(state
);
1137 if (tty_port_users(port
) == 1) {
1138 uart_shutdown(tty
, state
);
1141 * If we already have a port type configured,
1142 * we must release its resources.
1144 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
1145 uport
->ops
->release_port(uport
);
1147 flags
= UART_CONFIG_TYPE
;
1148 if (uport
->flags
& UPF_AUTO_IRQ
)
1149 flags
|= UART_CONFIG_IRQ
;
1152 * This will claim the ports resources if
1155 uport
->ops
->config_port(uport
, flags
);
1157 ret
= uart_startup(tty
, state
, 1);
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
)
1296 return -ENOIOCTLCMD
;
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
)))
1314 * Called via sys_ioctl. We can use spin_lock_irq() here.
1317 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
1319 struct uart_state
*state
= tty
->driver_data
;
1320 struct tty_port
*port
= &state
->port
;
1321 struct uart_port
*uport
;
1322 void __user
*uarg
= (void __user
*)arg
;
1323 int ret
= -ENOIOCTLCMD
;
1327 * These ioctls don't rely on the hardware to be present.
1331 ret
= uart_get_info_user(port
, uarg
);
1335 down_write(&tty
->termios_rwsem
);
1336 ret
= uart_set_info_user(tty
, state
, uarg
);
1337 up_write(&tty
->termios_rwsem
);
1341 down_write(&tty
->termios_rwsem
);
1342 ret
= uart_do_autoconfig(tty
, state
);
1343 up_write(&tty
->termios_rwsem
);
1346 case TIOCSERGWILD
: /* obsolete */
1347 case TIOCSERSWILD
: /* obsolete */
1352 if (ret
!= -ENOIOCTLCMD
)
1355 if (tty_io_error(tty
)) {
1361 * The following should only be used when hardware is present.
1365 ret
= uart_wait_modem_status(state
, arg
);
1369 if (ret
!= -ENOIOCTLCMD
)
1372 mutex_lock(&port
->mutex
);
1373 uport
= uart_port_check(state
);
1375 if (!uport
|| tty_io_error(tty
)) {
1381 * All these rely on hardware being present and need to be
1382 * protected against the tty being hung up.
1386 case TIOCSERGETLSR
: /* Get line status register */
1387 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1391 ret
= uart_get_rs485_config(uport
, uarg
);
1395 ret
= uart_set_rs485_config(uport
, uarg
);
1398 if (uport
->ops
->ioctl
)
1399 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1403 mutex_unlock(&port
->mutex
);
1408 static void uart_set_ldisc(struct tty_struct
*tty
)
1410 struct uart_state
*state
= tty
->driver_data
;
1411 struct uart_port
*uport
;
1413 mutex_lock(&state
->port
.mutex
);
1414 uport
= uart_port_check(state
);
1415 if (uport
&& uport
->ops
->set_ldisc
)
1416 uport
->ops
->set_ldisc(uport
, &tty
->termios
);
1417 mutex_unlock(&state
->port
.mutex
);
1420 static void uart_set_termios(struct tty_struct
*tty
,
1421 struct ktermios
*old_termios
)
1423 struct uart_state
*state
= tty
->driver_data
;
1424 struct uart_port
*uport
;
1425 unsigned int cflag
= tty
->termios
.c_cflag
;
1426 unsigned int iflag_mask
= IGNBRK
|BRKINT
|IGNPAR
|PARMRK
|INPCK
;
1427 bool sw_changed
= false;
1429 mutex_lock(&state
->port
.mutex
);
1430 uport
= uart_port_check(state
);
1435 * Drivers doing software flow control also need to know
1436 * about changes to these input settings.
1438 if (uport
->flags
& UPF_SOFT_FLOW
) {
1439 iflag_mask
|= IXANY
|IXON
|IXOFF
;
1441 tty
->termios
.c_cc
[VSTART
] != old_termios
->c_cc
[VSTART
] ||
1442 tty
->termios
.c_cc
[VSTOP
] != old_termios
->c_cc
[VSTOP
];
1446 * These are the bits that are used to setup various
1447 * flags in the low level driver. We can ignore the Bfoo
1448 * bits in c_cflag; c_[io]speed will always be set
1449 * appropriately by set_termios() in tty_ioctl.c
1451 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1452 tty
->termios
.c_ospeed
== old_termios
->c_ospeed
&&
1453 tty
->termios
.c_ispeed
== old_termios
->c_ispeed
&&
1454 ((tty
->termios
.c_iflag
^ old_termios
->c_iflag
) & iflag_mask
) == 0 &&
1459 uart_change_speed(tty
, state
, old_termios
);
1460 /* reload cflag from termios; port driver may have overriden flags */
1461 cflag
= tty
->termios
.c_cflag
;
1463 /* Handle transition to B0 status */
1464 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1465 uart_clear_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
1466 /* Handle transition away from B0 status */
1467 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1468 unsigned int mask
= TIOCM_DTR
;
1469 if (!(cflag
& CRTSCTS
) || !tty_throttled(tty
))
1471 uart_set_mctrl(uport
, mask
);
1474 mutex_unlock(&state
->port
.mutex
);
1478 * Calls to uart_close() are serialised via the tty_lock in
1479 * drivers/tty/tty_io.c:tty_release()
1480 * drivers/tty/tty_io.c:do_tty_hangup()
1482 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1484 struct uart_state
*state
= tty
->driver_data
;
1485 struct tty_port
*port
;
1488 struct uart_driver
*drv
= tty
->driver
->driver_state
;
1490 state
= drv
->state
+ tty
->index
;
1491 port
= &state
->port
;
1492 spin_lock_irq(&port
->lock
);
1494 spin_unlock_irq(&port
->lock
);
1498 port
= &state
->port
;
1499 pr_debug("uart_close(%d) called\n", tty
->index
);
1501 tty_port_close(tty
->port
, tty
, filp
);
1504 static void uart_tty_port_shutdown(struct tty_port
*port
)
1506 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1507 struct uart_port
*uport
= uart_port_check(state
);
1510 * At this point, we stop accepting input. To do this, we
1511 * disable the receive line status interrupts.
1513 if (WARN(!uport
, "detached port still initialized!\n"))
1516 spin_lock_irq(&uport
->lock
);
1517 uport
->ops
->stop_rx(uport
);
1518 spin_unlock_irq(&uport
->lock
);
1520 uart_port_shutdown(port
);
1523 * It's possible for shutdown to be called after suspend if we get
1524 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1525 * we don't try to resume a port that has been shutdown.
1527 tty_port_set_suspended(port
, 0);
1529 uart_change_pm(state
, UART_PM_STATE_OFF
);
1533 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1535 struct uart_state
*state
= tty
->driver_data
;
1536 struct uart_port
*port
;
1537 unsigned long char_time
, expire
;
1539 port
= uart_port_ref(state
);
1543 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0) {
1544 uart_port_deref(port
);
1549 * Set the check interval to be 1/5 of the estimated time to
1550 * send a single character, and make it at least 1. The check
1551 * interval should also be less than the timeout.
1553 * Note: we have to use pretty tight timings here to satisfy
1556 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1557 char_time
= char_time
/ 5;
1560 if (timeout
&& timeout
< char_time
)
1561 char_time
= timeout
;
1564 * If the transmitter hasn't cleared in twice the approximate
1565 * amount of time to send the entire FIFO, it probably won't
1566 * ever clear. This assumes the UART isn't doing flow
1567 * control, which is currently the case. Hence, if it ever
1568 * takes longer than port->timeout, this is probably due to a
1569 * UART bug of some kind. So, we clamp the timeout parameter at
1572 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1573 timeout
= 2 * port
->timeout
;
1575 expire
= jiffies
+ timeout
;
1577 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1578 port
->line
, jiffies
, expire
);
1581 * Check whether the transmitter is empty every 'char_time'.
1582 * 'timeout' / 'expire' give us the maximum amount of time
1585 while (!port
->ops
->tx_empty(port
)) {
1586 msleep_interruptible(jiffies_to_msecs(char_time
));
1587 if (signal_pending(current
))
1589 if (time_after(jiffies
, expire
))
1592 uart_port_deref(port
);
1596 * Calls to uart_hangup() are serialised by the tty_lock in
1597 * drivers/tty/tty_io.c:do_tty_hangup()
1598 * This runs from a workqueue and can sleep for a _short_ time only.
1600 static void uart_hangup(struct tty_struct
*tty
)
1602 struct uart_state
*state
= tty
->driver_data
;
1603 struct tty_port
*port
= &state
->port
;
1604 struct uart_port
*uport
;
1605 unsigned long flags
;
1607 pr_debug("uart_hangup(%d)\n", tty
->index
);
1609 mutex_lock(&port
->mutex
);
1610 uport
= uart_port_check(state
);
1611 WARN(!uport
, "hangup of detached port!\n");
1613 if (tty_port_active(port
)) {
1614 uart_flush_buffer(tty
);
1615 uart_shutdown(tty
, state
);
1616 spin_lock_irqsave(&port
->lock
, flags
);
1618 spin_unlock_irqrestore(&port
->lock
, flags
);
1619 tty_port_set_active(port
, 0);
1620 tty_port_tty_set(port
, NULL
);
1621 if (uport
&& !uart_console(uport
))
1622 uart_change_pm(state
, UART_PM_STATE_OFF
);
1623 wake_up_interruptible(&port
->open_wait
);
1624 wake_up_interruptible(&port
->delta_msr_wait
);
1626 mutex_unlock(&port
->mutex
);
1629 /* uport == NULL if uart_port has already been removed */
1630 static void uart_port_shutdown(struct tty_port
*port
)
1632 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1633 struct uart_port
*uport
= uart_port_check(state
);
1636 * clear delta_msr_wait queue to avoid mem leaks: we may free
1637 * the irq here so the queue might never be woken up. Note
1638 * that we won't end up waiting on delta_msr_wait again since
1639 * any outstanding file descriptors should be pointing at
1640 * hung_up_tty_fops now.
1642 wake_up_interruptible(&port
->delta_msr_wait
);
1645 * Free the IRQ and disable the port.
1648 uport
->ops
->shutdown(uport
);
1651 * Ensure that the IRQ handler isn't running on another CPU.
1654 synchronize_irq(uport
->irq
);
1657 static int uart_carrier_raised(struct tty_port
*port
)
1659 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1660 struct uart_port
*uport
;
1663 uport
= uart_port_ref(state
);
1665 * Should never observe uport == NULL since checks for hangup should
1666 * abort the tty_port_block_til_ready() loop before checking for carrier
1667 * raised -- but report carrier raised if it does anyway so open will
1668 * continue and not sleep
1670 if (WARN_ON(!uport
))
1672 spin_lock_irq(&uport
->lock
);
1673 uart_enable_ms(uport
);
1674 mctrl
= uport
->ops
->get_mctrl(uport
);
1675 spin_unlock_irq(&uport
->lock
);
1676 uart_port_deref(uport
);
1677 if (mctrl
& TIOCM_CAR
)
1682 static void uart_dtr_rts(struct tty_port
*port
, int raise
)
1684 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1685 struct uart_port
*uport
;
1687 uport
= uart_port_ref(state
);
1690 uart_port_dtr_rts(uport
, raise
);
1691 uart_port_deref(uport
);
1695 * Calls to uart_open are serialised by the tty_lock in
1696 * drivers/tty/tty_io.c:tty_open()
1697 * Note that if this fails, then uart_close() _will_ be called.
1699 * In time, we want to scrap the "opening nonpresent ports"
1700 * behaviour and implement an alternative way for setserial
1701 * to set base addresses/ports/types. This will allow us to
1702 * get rid of a certain amount of extra tests.
1704 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1706 struct uart_driver
*drv
= tty
->driver
->driver_state
;
1707 int retval
, line
= tty
->index
;
1708 struct uart_state
*state
= drv
->state
+ line
;
1710 tty
->driver_data
= state
;
1712 retval
= tty_port_open(&state
->port
, tty
, filp
);
1719 static int uart_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
1721 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1722 struct uart_port
*uport
;
1724 uport
= uart_port_check(state
);
1725 if (!uport
|| uport
->flags
& UPF_DEAD
)
1728 port
->low_latency
= (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1731 * Start up the serial port.
1733 return uart_startup(tty
, state
, 0);
1736 static const char *uart_type(struct uart_port
*port
)
1738 const char *str
= NULL
;
1740 if (port
->ops
->type
)
1741 str
= port
->ops
->type(port
);
1749 #ifdef CONFIG_PROC_FS
1751 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1753 struct uart_state
*state
= drv
->state
+ i
;
1754 struct tty_port
*port
= &state
->port
;
1755 enum uart_pm_state pm_state
;
1756 struct uart_port
*uport
;
1758 unsigned int status
;
1761 mutex_lock(&port
->mutex
);
1762 uport
= uart_port_check(state
);
1766 mmio
= uport
->iotype
>= UPIO_MEM
;
1767 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1768 uport
->line
, uart_type(uport
),
1769 mmio
? "mmio:0x" : "port:",
1770 mmio
? (unsigned long long)uport
->mapbase
1771 : (unsigned long long)uport
->iobase
,
1774 if (uport
->type
== PORT_UNKNOWN
) {
1779 if (capable(CAP_SYS_ADMIN
)) {
1780 pm_state
= state
->pm_state
;
1781 if (pm_state
!= UART_PM_STATE_ON
)
1782 uart_change_pm(state
, UART_PM_STATE_ON
);
1783 spin_lock_irq(&uport
->lock
);
1784 status
= uport
->ops
->get_mctrl(uport
);
1785 spin_unlock_irq(&uport
->lock
);
1786 if (pm_state
!= UART_PM_STATE_ON
)
1787 uart_change_pm(state
, pm_state
);
1789 seq_printf(m
, " tx:%d rx:%d",
1790 uport
->icount
.tx
, uport
->icount
.rx
);
1791 if (uport
->icount
.frame
)
1792 seq_printf(m
, " fe:%d", uport
->icount
.frame
);
1793 if (uport
->icount
.parity
)
1794 seq_printf(m
, " pe:%d", uport
->icount
.parity
);
1795 if (uport
->icount
.brk
)
1796 seq_printf(m
, " brk:%d", uport
->icount
.brk
);
1797 if (uport
->icount
.overrun
)
1798 seq_printf(m
, " oe:%d", uport
->icount
.overrun
);
1800 #define INFOBIT(bit, str) \
1801 if (uport->mctrl & (bit)) \
1802 strncat(stat_buf, (str), sizeof(stat_buf) - \
1803 strlen(stat_buf) - 2)
1804 #define STATBIT(bit, str) \
1805 if (status & (bit)) \
1806 strncat(stat_buf, (str), sizeof(stat_buf) - \
1807 strlen(stat_buf) - 2)
1811 INFOBIT(TIOCM_RTS
, "|RTS");
1812 STATBIT(TIOCM_CTS
, "|CTS");
1813 INFOBIT(TIOCM_DTR
, "|DTR");
1814 STATBIT(TIOCM_DSR
, "|DSR");
1815 STATBIT(TIOCM_CAR
, "|CD");
1816 STATBIT(TIOCM_RNG
, "|RI");
1820 seq_puts(m
, stat_buf
);
1826 mutex_unlock(&port
->mutex
);
1829 static int uart_proc_show(struct seq_file
*m
, void *v
)
1831 struct tty_driver
*ttydrv
= m
->private;
1832 struct uart_driver
*drv
= ttydrv
->driver_state
;
1835 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1836 for (i
= 0; i
< drv
->nr
; i
++)
1837 uart_line_info(m
, drv
, i
);
1841 static int uart_proc_open(struct inode
*inode
, struct file
*file
)
1843 return single_open(file
, uart_proc_show
, PDE_DATA(inode
));
1846 static const struct file_operations uart_proc_fops
= {
1847 .owner
= THIS_MODULE
,
1848 .open
= uart_proc_open
,
1850 .llseek
= seq_lseek
,
1851 .release
= single_release
,
1855 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1857 * uart_console_write - write a console message to a serial port
1858 * @port: the port to write the message
1859 * @s: array of characters
1860 * @count: number of characters in string to write
1861 * @putchar: function to write character to port
1863 void uart_console_write(struct uart_port
*port
, const char *s
,
1865 void (*putchar
)(struct uart_port
*, int))
1869 for (i
= 0; i
< count
; i
++, s
++) {
1871 putchar(port
, '\r');
1875 EXPORT_SYMBOL_GPL(uart_console_write
);
1878 * Check whether an invalid uart number has been specified, and
1879 * if so, search for the first available port that does have
1882 struct uart_port
* __init
1883 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1885 int idx
= co
->index
;
1887 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1888 ports
[idx
].membase
== NULL
))
1889 for (idx
= 0; idx
< nr
; idx
++)
1890 if (ports
[idx
].iobase
!= 0 ||
1891 ports
[idx
].membase
!= NULL
)
1900 * uart_parse_earlycon - Parse earlycon options
1901 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1902 * @iotype: ptr for decoded iotype (out)
1903 * @addr: ptr for decoded mapbase/iobase (out)
1904 * @options: ptr for <options> field; NULL if not present (out)
1906 * Decodes earlycon kernel command line parameters of the form
1907 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1908 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1911 * earlycon=<name>,0x<addr>,<options>
1912 * console=<name>,0x<addr>,<options>
1913 * is also accepted; the returned @iotype will be UPIO_MEM.
1915 * Returns 0 on success or -EINVAL on failure
1917 int uart_parse_earlycon(char *p
, unsigned char *iotype
, resource_size_t
*addr
,
1920 if (strncmp(p
, "mmio,", 5) == 0) {
1923 } else if (strncmp(p
, "mmio16,", 7) == 0) {
1924 *iotype
= UPIO_MEM16
;
1926 } else if (strncmp(p
, "mmio32,", 7) == 0) {
1927 *iotype
= UPIO_MEM32
;
1929 } else if (strncmp(p
, "mmio32be,", 9) == 0) {
1930 *iotype
= UPIO_MEM32BE
;
1932 } else if (strncmp(p
, "mmio32native,", 13) == 0) {
1933 *iotype
= IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) ?
1934 UPIO_MEM32BE
: UPIO_MEM32
;
1936 } else if (strncmp(p
, "io,", 3) == 0) {
1937 *iotype
= UPIO_PORT
;
1939 } else if (strncmp(p
, "0x", 2) == 0) {
1946 * Before you replace it with kstrtoull(), think about options separator
1947 * (',') it will not tolerate
1949 *addr
= simple_strtoull(p
, NULL
, 0);
1957 EXPORT_SYMBOL_GPL(uart_parse_earlycon
);
1960 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1961 * @options: pointer to option string
1962 * @baud: pointer to an 'int' variable for the baud rate.
1963 * @parity: pointer to an 'int' variable for the parity.
1964 * @bits: pointer to an 'int' variable for the number of data bits.
1965 * @flow: pointer to an 'int' variable for the flow control character.
1967 * uart_parse_options decodes a string containing the serial console
1968 * options. The format of the string is <baud><parity><bits><flow>,
1972 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1976 *baud
= simple_strtoul(s
, NULL
, 10);
1977 while (*s
>= '0' && *s
<= '9')
1986 EXPORT_SYMBOL_GPL(uart_parse_options
);
1989 * uart_set_options - setup the serial console parameters
1990 * @port: pointer to the serial ports uart_port structure
1991 * @co: console pointer
1993 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1994 * @bits: number of data bits
1995 * @flow: flow control character - 'r' (rts)
1998 uart_set_options(struct uart_port
*port
, struct console
*co
,
1999 int baud
, int parity
, int bits
, int flow
)
2001 struct ktermios termios
;
2002 static struct ktermios dummy
;
2005 * Ensure that the serial console lock is initialised
2007 * If this port is a console, then the spinlock is already
2010 if (!(uart_console(port
) && (port
->cons
->flags
& CON_ENABLED
))) {
2011 spin_lock_init(&port
->lock
);
2012 lockdep_set_class(&port
->lock
, &port_lock_key
);
2015 memset(&termios
, 0, sizeof(struct ktermios
));
2017 termios
.c_cflag
|= CREAD
| HUPCL
| CLOCAL
;
2018 tty_termios_encode_baud_rate(&termios
, baud
, baud
);
2021 termios
.c_cflag
|= CS7
;
2023 termios
.c_cflag
|= CS8
;
2027 termios
.c_cflag
|= PARODD
;
2030 termios
.c_cflag
|= PARENB
;
2035 termios
.c_cflag
|= CRTSCTS
;
2038 * some uarts on other side don't support no flow control.
2039 * So we set * DTR in host uart to make them happy
2041 port
->mctrl
|= TIOCM_DTR
;
2043 port
->ops
->set_termios(port
, &termios
, &dummy
);
2045 * Allow the setting of the UART parameters with a NULL console
2049 co
->cflag
= termios
.c_cflag
;
2053 EXPORT_SYMBOL_GPL(uart_set_options
);
2054 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2057 * uart_change_pm - set power state of the port
2059 * @state: port descriptor
2060 * @pm_state: new state
2062 * Locking: port->mutex has to be held
2064 static void uart_change_pm(struct uart_state
*state
,
2065 enum uart_pm_state pm_state
)
2067 struct uart_port
*port
= uart_port_check(state
);
2069 if (state
->pm_state
!= pm_state
) {
2070 if (port
&& port
->ops
->pm
)
2071 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
2072 state
->pm_state
= pm_state
;
2077 struct uart_port
*port
;
2078 struct uart_driver
*driver
;
2081 static int serial_match_port(struct device
*dev
, void *data
)
2083 struct uart_match
*match
= data
;
2084 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
2085 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
2088 return dev
->devt
== devt
; /* Actually, only one tty per port */
2091 int uart_suspend_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
};
2098 mutex_lock(&port
->mutex
);
2100 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2101 if (tty_dev
&& device_may_wakeup(tty_dev
)) {
2102 enable_irq_wake(uport
->irq
);
2103 put_device(tty_dev
);
2104 mutex_unlock(&port
->mutex
);
2107 put_device(tty_dev
);
2109 /* Nothing to do if the console is not suspending */
2110 if (!console_suspend_enabled
&& uart_console(uport
))
2113 uport
->suspended
= 1;
2115 if (tty_port_initialized(port
)) {
2116 const struct uart_ops
*ops
= uport
->ops
;
2119 tty_port_set_suspended(port
, 1);
2120 tty_port_set_initialized(port
, 0);
2122 spin_lock_irq(&uport
->lock
);
2123 ops
->stop_tx(uport
);
2124 ops
->set_mctrl(uport
, 0);
2125 ops
->stop_rx(uport
);
2126 spin_unlock_irq(&uport
->lock
);
2129 * Wait for the transmitter to empty.
2131 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
2134 dev_err(uport
->dev
, "%s: Unable to drain transmitter\n",
2137 ops
->shutdown(uport
);
2141 * Disable the console device before suspending.
2143 if (uart_console(uport
))
2144 console_stop(uport
->cons
);
2146 uart_change_pm(state
, UART_PM_STATE_OFF
);
2148 mutex_unlock(&port
->mutex
);
2153 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2155 struct uart_state
*state
= drv
->state
+ uport
->line
;
2156 struct tty_port
*port
= &state
->port
;
2157 struct device
*tty_dev
;
2158 struct uart_match match
= {uport
, drv
};
2159 struct ktermios termios
;
2161 mutex_lock(&port
->mutex
);
2163 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2164 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
2165 if (irqd_is_wakeup_set(irq_get_irq_data((uport
->irq
))))
2166 disable_irq_wake(uport
->irq
);
2167 put_device(tty_dev
);
2168 mutex_unlock(&port
->mutex
);
2171 put_device(tty_dev
);
2172 uport
->suspended
= 0;
2175 * Re-enable the console device after suspending.
2177 if (uart_console(uport
)) {
2179 * First try to use the console cflag setting.
2181 memset(&termios
, 0, sizeof(struct ktermios
));
2182 termios
.c_cflag
= uport
->cons
->cflag
;
2185 * If that's unset, use the tty termios setting.
2187 if (port
->tty
&& termios
.c_cflag
== 0)
2188 termios
= port
->tty
->termios
;
2190 if (console_suspend_enabled
)
2191 uart_change_pm(state
, UART_PM_STATE_ON
);
2192 uport
->ops
->set_termios(uport
, &termios
, NULL
);
2193 if (console_suspend_enabled
)
2194 console_start(uport
->cons
);
2197 if (tty_port_suspended(port
)) {
2198 const struct uart_ops
*ops
= uport
->ops
;
2201 uart_change_pm(state
, UART_PM_STATE_ON
);
2202 spin_lock_irq(&uport
->lock
);
2203 ops
->set_mctrl(uport
, 0);
2204 spin_unlock_irq(&uport
->lock
);
2205 if (console_suspend_enabled
|| !uart_console(uport
)) {
2206 /* Protected by port mutex for now */
2207 struct tty_struct
*tty
= port
->tty
;
2208 ret
= ops
->startup(uport
);
2211 uart_change_speed(tty
, state
, NULL
);
2212 spin_lock_irq(&uport
->lock
);
2213 ops
->set_mctrl(uport
, uport
->mctrl
);
2214 ops
->start_tx(uport
);
2215 spin_unlock_irq(&uport
->lock
);
2216 tty_port_set_initialized(port
, 1);
2219 * Failed to resume - maybe hardware went away?
2220 * Clear the "initialized" flag so we won't try
2221 * to call the low level drivers shutdown method.
2223 uart_shutdown(tty
, state
);
2227 tty_port_set_suspended(port
, 0);
2230 mutex_unlock(&port
->mutex
);
2236 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2240 switch (port
->iotype
) {
2242 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2245 snprintf(address
, sizeof(address
),
2246 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2254 snprintf(address
, sizeof(address
),
2255 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2258 strlcpy(address
, "*unknown*", sizeof(address
));
2262 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2263 port
->dev
? dev_name(port
->dev
) : "",
2264 port
->dev
? ": " : "",
2266 address
, port
->irq
, port
->uartclk
/ 16, uart_type(port
));
2270 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2271 struct uart_port
*port
)
2276 * If there isn't a port here, don't do anything further.
2278 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2282 * Now do the auto configuration stuff. Note that config_port
2283 * is expected to claim the resources and map the port for us.
2286 if (port
->flags
& UPF_AUTO_IRQ
)
2287 flags
|= UART_CONFIG_IRQ
;
2288 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2289 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2290 port
->type
= PORT_UNKNOWN
;
2291 flags
|= UART_CONFIG_TYPE
;
2293 port
->ops
->config_port(port
, flags
);
2296 if (port
->type
!= PORT_UNKNOWN
) {
2297 unsigned long flags
;
2299 uart_report_port(drv
, port
);
2301 /* Power up port for set_mctrl() */
2302 uart_change_pm(state
, UART_PM_STATE_ON
);
2305 * Ensure that the modem control lines are de-activated.
2306 * keep the DTR setting that is set in uart_set_options()
2307 * We probably don't need a spinlock around this, but
2309 spin_lock_irqsave(&port
->lock
, flags
);
2310 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2311 spin_unlock_irqrestore(&port
->lock
, flags
);
2314 * If this driver supports console, and it hasn't been
2315 * successfully registered yet, try to re-register it.
2316 * It may be that the port was not available.
2318 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2319 register_console(port
->cons
);
2322 * Power down all ports by default, except the
2323 * console if we have one.
2325 if (!uart_console(port
))
2326 uart_change_pm(state
, UART_PM_STATE_OFF
);
2330 #ifdef CONFIG_CONSOLE_POLL
2332 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2334 struct uart_driver
*drv
= driver
->driver_state
;
2335 struct uart_state
*state
= drv
->state
+ line
;
2336 struct tty_port
*tport
;
2337 struct uart_port
*port
;
2344 tport
= &state
->port
;
2345 mutex_lock(&tport
->mutex
);
2347 port
= uart_port_check(state
);
2348 if (!port
|| !(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
)) {
2353 if (port
->ops
->poll_init
) {
2355 * We don't set initialized as we only initialized the hw,
2356 * e.g. state->xmit is still uninitialized.
2358 if (!tty_port_initialized(tport
))
2359 ret
= port
->ops
->poll_init(port
);
2362 if (!ret
&& options
) {
2363 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2364 ret
= uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2367 mutex_unlock(&tport
->mutex
);
2371 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2373 struct uart_driver
*drv
= driver
->driver_state
;
2374 struct uart_state
*state
= drv
->state
+ line
;
2375 struct uart_port
*port
;
2378 port
= uart_port_ref(state
);
2380 ret
= port
->ops
->poll_get_char(port
);
2381 uart_port_deref(port
);
2387 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2389 struct uart_driver
*drv
= driver
->driver_state
;
2390 struct uart_state
*state
= drv
->state
+ line
;
2391 struct uart_port
*port
;
2393 port
= uart_port_ref(state
);
2398 port
->ops
->poll_put_char(port
, '\r');
2399 port
->ops
->poll_put_char(port
, ch
);
2400 uart_port_deref(port
);
2404 static const struct tty_operations uart_ops
= {
2406 .close
= uart_close
,
2407 .write
= uart_write
,
2408 .put_char
= uart_put_char
,
2409 .flush_chars
= uart_flush_chars
,
2410 .write_room
= uart_write_room
,
2411 .chars_in_buffer
= uart_chars_in_buffer
,
2412 .flush_buffer
= uart_flush_buffer
,
2413 .ioctl
= uart_ioctl
,
2414 .throttle
= uart_throttle
,
2415 .unthrottle
= uart_unthrottle
,
2416 .send_xchar
= uart_send_xchar
,
2417 .set_termios
= uart_set_termios
,
2418 .set_ldisc
= uart_set_ldisc
,
2420 .start
= uart_start
,
2421 .hangup
= uart_hangup
,
2422 .break_ctl
= uart_break_ctl
,
2423 .wait_until_sent
= uart_wait_until_sent
,
2424 #ifdef CONFIG_PROC_FS
2425 .proc_fops
= &uart_proc_fops
,
2427 .tiocmget
= uart_tiocmget
,
2428 .tiocmset
= uart_tiocmset
,
2429 .get_icount
= uart_get_icount
,
2430 #ifdef CONFIG_CONSOLE_POLL
2431 .poll_init
= uart_poll_init
,
2432 .poll_get_char
= uart_poll_get_char
,
2433 .poll_put_char
= uart_poll_put_char
,
2437 static const struct tty_port_operations uart_port_ops
= {
2438 .carrier_raised
= uart_carrier_raised
,
2439 .dtr_rts
= uart_dtr_rts
,
2440 .activate
= uart_port_activate
,
2441 .shutdown
= uart_tty_port_shutdown
,
2445 * uart_register_driver - register a driver with the uart core layer
2446 * @drv: low level driver structure
2448 * Register a uart driver with the core driver. We in turn register
2449 * with the tty layer, and initialise the core driver per-port state.
2451 * We have a proc file in /proc/tty/driver which is named after the
2454 * drv->port should be NULL, and the per-port structures should be
2455 * registered using uart_add_one_port after this call has succeeded.
2457 int uart_register_driver(struct uart_driver
*drv
)
2459 struct tty_driver
*normal
;
2465 * Maybe we should be using a slab cache for this, especially if
2466 * we have a large number of ports to handle.
2468 drv
->state
= kzalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2472 normal
= alloc_tty_driver(drv
->nr
);
2476 drv
->tty_driver
= normal
;
2478 normal
->driver_name
= drv
->driver_name
;
2479 normal
->name
= drv
->dev_name
;
2480 normal
->major
= drv
->major
;
2481 normal
->minor_start
= drv
->minor
;
2482 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2483 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2484 normal
->init_termios
= tty_std_termios
;
2485 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2486 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2487 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2488 normal
->driver_state
= drv
;
2489 tty_set_operations(normal
, &uart_ops
);
2492 * Initialise the UART state(s).
2494 for (i
= 0; i
< drv
->nr
; i
++) {
2495 struct uart_state
*state
= drv
->state
+ i
;
2496 struct tty_port
*port
= &state
->port
;
2498 tty_port_init(port
);
2499 port
->ops
= &uart_port_ops
;
2502 retval
= tty_register_driver(normal
);
2506 for (i
= 0; i
< drv
->nr
; i
++)
2507 tty_port_destroy(&drv
->state
[i
].port
);
2508 put_tty_driver(normal
);
2516 * uart_unregister_driver - remove a driver from the uart core layer
2517 * @drv: low level driver structure
2519 * Remove all references to a driver from the core driver. The low
2520 * level driver must have removed all its ports via the
2521 * uart_remove_one_port() if it registered them with uart_add_one_port().
2522 * (ie, drv->port == NULL)
2524 void uart_unregister_driver(struct uart_driver
*drv
)
2526 struct tty_driver
*p
= drv
->tty_driver
;
2529 tty_unregister_driver(p
);
2531 for (i
= 0; i
< drv
->nr
; i
++)
2532 tty_port_destroy(&drv
->state
[i
].port
);
2535 drv
->tty_driver
= NULL
;
2538 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2540 struct uart_driver
*p
= co
->data
;
2542 return p
->tty_driver
;
2545 static ssize_t
uart_get_attr_uartclk(struct device
*dev
,
2546 struct device_attribute
*attr
, char *buf
)
2548 struct serial_struct tmp
;
2549 struct tty_port
*port
= dev_get_drvdata(dev
);
2551 uart_get_info(port
, &tmp
);
2552 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.baud_base
* 16);
2555 static ssize_t
uart_get_attr_type(struct device
*dev
,
2556 struct device_attribute
*attr
, char *buf
)
2558 struct serial_struct tmp
;
2559 struct tty_port
*port
= dev_get_drvdata(dev
);
2561 uart_get_info(port
, &tmp
);
2562 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.type
);
2564 static ssize_t
uart_get_attr_line(struct device
*dev
,
2565 struct device_attribute
*attr
, char *buf
)
2567 struct serial_struct tmp
;
2568 struct tty_port
*port
= dev_get_drvdata(dev
);
2570 uart_get_info(port
, &tmp
);
2571 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.line
);
2574 static ssize_t
uart_get_attr_port(struct device
*dev
,
2575 struct device_attribute
*attr
, char *buf
)
2577 struct serial_struct tmp
;
2578 struct tty_port
*port
= dev_get_drvdata(dev
);
2579 unsigned long ioaddr
;
2581 uart_get_info(port
, &tmp
);
2583 if (HIGH_BITS_OFFSET
)
2584 ioaddr
|= (unsigned long)tmp
.port_high
<< HIGH_BITS_OFFSET
;
2585 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", ioaddr
);
2588 static ssize_t
uart_get_attr_irq(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
.irq
);
2598 static ssize_t
uart_get_attr_flags(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%X\n", tmp
.flags
);
2608 static ssize_t
uart_get_attr_xmit_fifo_size(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
.xmit_fifo_size
);
2619 static ssize_t
uart_get_attr_close_delay(struct device
*dev
,
2620 struct device_attribute
*attr
, char *buf
)
2622 struct serial_struct tmp
;
2623 struct tty_port
*port
= dev_get_drvdata(dev
);
2625 uart_get_info(port
, &tmp
);
2626 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.close_delay
);
2630 static ssize_t
uart_get_attr_closing_wait(struct device
*dev
,
2631 struct device_attribute
*attr
, char *buf
)
2633 struct serial_struct tmp
;
2634 struct tty_port
*port
= dev_get_drvdata(dev
);
2636 uart_get_info(port
, &tmp
);
2637 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.closing_wait
);
2640 static ssize_t
uart_get_attr_custom_divisor(struct device
*dev
,
2641 struct device_attribute
*attr
, char *buf
)
2643 struct serial_struct tmp
;
2644 struct tty_port
*port
= dev_get_drvdata(dev
);
2646 uart_get_info(port
, &tmp
);
2647 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.custom_divisor
);
2650 static ssize_t
uart_get_attr_io_type(struct device
*dev
,
2651 struct device_attribute
*attr
, char *buf
)
2653 struct serial_struct tmp
;
2654 struct tty_port
*port
= dev_get_drvdata(dev
);
2656 uart_get_info(port
, &tmp
);
2657 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.io_type
);
2660 static ssize_t
uart_get_attr_iomem_base(struct device
*dev
,
2661 struct device_attribute
*attr
, char *buf
)
2663 struct serial_struct tmp
;
2664 struct tty_port
*port
= dev_get_drvdata(dev
);
2666 uart_get_info(port
, &tmp
);
2667 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", (unsigned long)tmp
.iomem_base
);
2670 static ssize_t
uart_get_attr_iomem_reg_shift(struct device
*dev
,
2671 struct device_attribute
*attr
, char *buf
)
2673 struct serial_struct tmp
;
2674 struct tty_port
*port
= dev_get_drvdata(dev
);
2676 uart_get_info(port
, &tmp
);
2677 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.iomem_reg_shift
);
2680 static DEVICE_ATTR(type
, S_IRUSR
| S_IRGRP
, uart_get_attr_type
, NULL
);
2681 static DEVICE_ATTR(line
, S_IRUSR
| S_IRGRP
, uart_get_attr_line
, NULL
);
2682 static DEVICE_ATTR(port
, S_IRUSR
| S_IRGRP
, uart_get_attr_port
, NULL
);
2683 static DEVICE_ATTR(irq
, S_IRUSR
| S_IRGRP
, uart_get_attr_irq
, NULL
);
2684 static DEVICE_ATTR(flags
, S_IRUSR
| S_IRGRP
, uart_get_attr_flags
, NULL
);
2685 static DEVICE_ATTR(xmit_fifo_size
, S_IRUSR
| S_IRGRP
, uart_get_attr_xmit_fifo_size
, NULL
);
2686 static DEVICE_ATTR(uartclk
, S_IRUSR
| S_IRGRP
, uart_get_attr_uartclk
, NULL
);
2687 static DEVICE_ATTR(close_delay
, S_IRUSR
| S_IRGRP
, uart_get_attr_close_delay
, NULL
);
2688 static DEVICE_ATTR(closing_wait
, S_IRUSR
| S_IRGRP
, uart_get_attr_closing_wait
, NULL
);
2689 static DEVICE_ATTR(custom_divisor
, S_IRUSR
| S_IRGRP
, uart_get_attr_custom_divisor
, NULL
);
2690 static DEVICE_ATTR(io_type
, S_IRUSR
| S_IRGRP
, uart_get_attr_io_type
, NULL
);
2691 static DEVICE_ATTR(iomem_base
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_base
, NULL
);
2692 static DEVICE_ATTR(iomem_reg_shift
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_reg_shift
, NULL
);
2694 static struct attribute
*tty_dev_attrs
[] = {
2695 &dev_attr_type
.attr
,
2696 &dev_attr_line
.attr
,
2697 &dev_attr_port
.attr
,
2699 &dev_attr_flags
.attr
,
2700 &dev_attr_xmit_fifo_size
.attr
,
2701 &dev_attr_uartclk
.attr
,
2702 &dev_attr_close_delay
.attr
,
2703 &dev_attr_closing_wait
.attr
,
2704 &dev_attr_custom_divisor
.attr
,
2705 &dev_attr_io_type
.attr
,
2706 &dev_attr_iomem_base
.attr
,
2707 &dev_attr_iomem_reg_shift
.attr
,
2711 static const struct attribute_group tty_dev_attr_group
= {
2712 .attrs
= tty_dev_attrs
,
2716 * uart_add_one_port - attach a driver-defined port structure
2717 * @drv: pointer to the uart low level driver structure for this port
2718 * @uport: uart port structure to use for this port.
2720 * This allows the driver to register its own uart_port structure
2721 * with the core driver. The main purpose is to allow the low
2722 * level uart drivers to expand uart_port, rather than having yet
2723 * more levels of structures.
2725 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2727 struct uart_state
*state
;
2728 struct tty_port
*port
;
2730 struct device
*tty_dev
;
2733 BUG_ON(in_interrupt());
2735 if (uport
->line
>= drv
->nr
)
2738 state
= drv
->state
+ uport
->line
;
2739 port
= &state
->port
;
2741 mutex_lock(&port_mutex
);
2742 mutex_lock(&port
->mutex
);
2743 if (state
->uart_port
) {
2748 /* Link the port to the driver state table and vice versa */
2749 atomic_set(&state
->refcount
, 1);
2750 init_waitqueue_head(&state
->remove_wait
);
2751 state
->uart_port
= uport
;
2752 uport
->state
= state
;
2754 state
->pm_state
= UART_PM_STATE_UNDEFINED
;
2755 uport
->cons
= drv
->cons
;
2756 uport
->minor
= drv
->tty_driver
->minor_start
+ uport
->line
;
2757 uport
->name
= kasprintf(GFP_KERNEL
, "%s%d", drv
->dev_name
,
2758 drv
->tty_driver
->name_base
+ uport
->line
);
2765 * If this port is a console, then the spinlock is already
2768 if (!(uart_console(uport
) && (uport
->cons
->flags
& CON_ENABLED
))) {
2769 spin_lock_init(&uport
->lock
);
2770 lockdep_set_class(&uport
->lock
, &port_lock_key
);
2772 if (uport
->cons
&& uport
->dev
)
2773 of_console_check(uport
->dev
->of_node
, uport
->cons
->name
, uport
->line
);
2775 uart_configure_port(drv
, state
, uport
);
2777 port
->console
= uart_console(uport
);
2780 if (uport
->attr_group
)
2783 uport
->tty_groups
= kcalloc(num_groups
, sizeof(*uport
->tty_groups
),
2785 if (!uport
->tty_groups
) {
2789 uport
->tty_groups
[0] = &tty_dev_attr_group
;
2790 if (uport
->attr_group
)
2791 uport
->tty_groups
[1] = uport
->attr_group
;
2794 * Register the port whether it's detected or not. This allows
2795 * setserial to be used to alter this port's parameters.
2797 tty_dev
= tty_port_register_device_attr_serdev(port
, drv
->tty_driver
,
2798 uport
->line
, uport
->dev
, port
, uport
->tty_groups
);
2799 if (likely(!IS_ERR(tty_dev
))) {
2800 device_set_wakeup_capable(tty_dev
, 1);
2802 dev_err(uport
->dev
, "Cannot register tty device on line %d\n",
2807 * Ensure UPF_DEAD is not set.
2809 uport
->flags
&= ~UPF_DEAD
;
2812 mutex_unlock(&port
->mutex
);
2813 mutex_unlock(&port_mutex
);
2819 * uart_remove_one_port - detach a driver defined port structure
2820 * @drv: pointer to the uart low level driver structure for this port
2821 * @uport: uart port structure for this port
2823 * This unhooks (and hangs up) the specified port structure from the
2824 * core driver. No further calls will be made to the low-level code
2827 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2829 struct uart_state
*state
= drv
->state
+ uport
->line
;
2830 struct tty_port
*port
= &state
->port
;
2831 struct uart_port
*uart_port
;
2832 struct tty_struct
*tty
;
2835 BUG_ON(in_interrupt());
2837 mutex_lock(&port_mutex
);
2840 * Mark the port "dead" - this prevents any opens from
2841 * succeeding while we shut down the port.
2843 mutex_lock(&port
->mutex
);
2844 uart_port
= uart_port_check(state
);
2845 if (uart_port
!= uport
)
2846 dev_alert(uport
->dev
, "Removing wrong port: %p != %p\n",
2850 mutex_unlock(&port
->mutex
);
2854 uport
->flags
|= UPF_DEAD
;
2855 mutex_unlock(&port
->mutex
);
2858 * Remove the devices from the tty layer
2860 tty_port_unregister_device(port
, drv
->tty_driver
, uport
->line
);
2862 tty
= tty_port_tty_get(port
);
2864 tty_vhangup(port
->tty
);
2869 * If the port is used as a console, unregister it
2871 if (uart_console(uport
))
2872 unregister_console(uport
->cons
);
2875 * Free the port IO and memory resources, if any.
2877 if (uport
->type
!= PORT_UNKNOWN
&& uport
->ops
->release_port
)
2878 uport
->ops
->release_port(uport
);
2879 kfree(uport
->tty_groups
);
2883 * Indicate that there isn't a port here anymore.
2885 uport
->type
= PORT_UNKNOWN
;
2887 mutex_lock(&port
->mutex
);
2888 WARN_ON(atomic_dec_return(&state
->refcount
) < 0);
2889 wait_event(state
->remove_wait
, !atomic_read(&state
->refcount
));
2890 state
->uart_port
= NULL
;
2891 mutex_unlock(&port
->mutex
);
2893 mutex_unlock(&port_mutex
);
2899 * Are the two ports equivalent?
2901 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2903 if (port1
->iotype
!= port2
->iotype
)
2906 switch (port1
->iotype
) {
2908 return (port1
->iobase
== port2
->iobase
);
2910 return (port1
->iobase
== port2
->iobase
) &&
2911 (port1
->hub6
== port2
->hub6
);
2918 return (port1
->mapbase
== port2
->mapbase
);
2922 EXPORT_SYMBOL(uart_match_port
);
2925 * uart_handle_dcd_change - handle a change of carrier detect state
2926 * @uport: uart_port structure for the open port
2927 * @status: new carrier detect status, nonzero if active
2929 * Caller must hold uport->lock
2931 void uart_handle_dcd_change(struct uart_port
*uport
, unsigned int status
)
2933 struct tty_port
*port
= &uport
->state
->port
;
2934 struct tty_struct
*tty
= port
->tty
;
2935 struct tty_ldisc
*ld
;
2937 lockdep_assert_held_once(&uport
->lock
);
2940 ld
= tty_ldisc_ref(tty
);
2942 if (ld
->ops
->dcd_change
)
2943 ld
->ops
->dcd_change(tty
, status
);
2944 tty_ldisc_deref(ld
);
2948 uport
->icount
.dcd
++;
2950 if (uart_dcd_enabled(uport
)) {
2952 wake_up_interruptible(&port
->open_wait
);
2957 EXPORT_SYMBOL_GPL(uart_handle_dcd_change
);
2960 * uart_handle_cts_change - handle a change of clear-to-send state
2961 * @uport: uart_port structure for the open port
2962 * @status: new clear to send status, nonzero if active
2964 * Caller must hold uport->lock
2966 void uart_handle_cts_change(struct uart_port
*uport
, unsigned int status
)
2968 lockdep_assert_held_once(&uport
->lock
);
2970 uport
->icount
.cts
++;
2972 if (uart_softcts_mode(uport
)) {
2973 if (uport
->hw_stopped
) {
2975 uport
->hw_stopped
= 0;
2976 uport
->ops
->start_tx(uport
);
2977 uart_write_wakeup(uport
);
2981 uport
->hw_stopped
= 1;
2982 uport
->ops
->stop_tx(uport
);
2988 EXPORT_SYMBOL_GPL(uart_handle_cts_change
);
2991 * uart_insert_char - push a char to the uart layer
2993 * User is responsible to call tty_flip_buffer_push when they are done with
2996 * @port: corresponding port
2997 * @status: state of the serial port RX buffer (LSR for 8250)
2998 * @overrun: mask of overrun bits in @status
2999 * @ch: character to push
3000 * @flag: flag for the character (see TTY_NORMAL and friends)
3002 void uart_insert_char(struct uart_port
*port
, unsigned int status
,
3003 unsigned int overrun
, unsigned int ch
, unsigned int flag
)
3005 struct tty_port
*tport
= &port
->state
->port
;
3007 if ((status
& port
->ignore_status_mask
& ~overrun
) == 0)
3008 if (tty_insert_flip_char(tport
, ch
, flag
) == 0)
3009 ++port
->icount
.buf_overrun
;
3012 * Overrun is special. Since it's reported immediately,
3013 * it doesn't affect the current character.
3015 if (status
& ~port
->ignore_status_mask
& overrun
)
3016 if (tty_insert_flip_char(tport
, 0, TTY_OVERRUN
) == 0)
3017 ++port
->icount
.buf_overrun
;
3019 EXPORT_SYMBOL_GPL(uart_insert_char
);
3021 EXPORT_SYMBOL(uart_write_wakeup
);
3022 EXPORT_SYMBOL(uart_register_driver
);
3023 EXPORT_SYMBOL(uart_unregister_driver
);
3024 EXPORT_SYMBOL(uart_suspend_port
);
3025 EXPORT_SYMBOL(uart_resume_port
);
3026 EXPORT_SYMBOL(uart_add_one_port
);
3027 EXPORT_SYMBOL(uart_remove_one_port
);
3029 MODULE_DESCRIPTION("Serial driver core");
3030 MODULE_LICENSE("GPL");