2 * Driver core for serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/device.h>
33 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34 #include <linux/serial_core.h>
35 #include <linux/delay.h>
36 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
42 * This is used to lock changes in serial line configuration.
44 static DEFINE_MUTEX(port_mutex
);
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
50 static struct lock_class_key port_lock_key
;
52 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
54 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
55 struct ktermios
*old_termios
);
56 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
);
57 static void uart_change_pm(struct uart_state
*state
,
58 enum uart_pm_state pm_state
);
60 static void uart_port_shutdown(struct tty_port
*port
);
62 static int uart_dcd_enabled(struct uart_port
*uport
)
64 return !!(uport
->status
& UPSTAT_DCD_ENABLE
);
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
71 void uart_write_wakeup(struct uart_port
*port
)
73 struct uart_state
*state
= port
->state
;
75 * This means you called this function _after_ the port was
76 * closed. No cookie for you.
79 tty_wakeup(state
->port
.tty
);
82 static void uart_stop(struct tty_struct
*tty
)
84 struct uart_state
*state
= tty
->driver_data
;
85 struct uart_port
*port
= state
->uart_port
;
88 spin_lock_irqsave(&port
->lock
, flags
);
89 port
->ops
->stop_tx(port
);
90 spin_unlock_irqrestore(&port
->lock
, flags
);
93 static void __uart_start(struct tty_struct
*tty
)
95 struct uart_state
*state
= tty
->driver_data
;
96 struct uart_port
*port
= state
->uart_port
;
98 if (!uart_tx_stopped(port
))
99 port
->ops
->start_tx(port
);
102 static void uart_start(struct tty_struct
*tty
)
104 struct uart_state
*state
= tty
->driver_data
;
105 struct uart_port
*port
= state
->uart_port
;
108 spin_lock_irqsave(&port
->lock
, flags
);
110 spin_unlock_irqrestore(&port
->lock
, flags
);
114 uart_update_mctrl(struct uart_port
*port
, unsigned int set
, unsigned int clear
)
119 spin_lock_irqsave(&port
->lock
, flags
);
121 port
->mctrl
= (old
& ~clear
) | set
;
122 if (old
!= port
->mctrl
)
123 port
->ops
->set_mctrl(port
, port
->mctrl
);
124 spin_unlock_irqrestore(&port
->lock
, flags
);
127 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
128 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
131 * Startup the port. This will be called once per open. All calls
132 * will be serialised by the per-port mutex.
134 static int uart_port_startup(struct tty_struct
*tty
, struct uart_state
*state
,
137 struct uart_port
*uport
= state
->uart_port
;
141 if (uport
->type
== PORT_UNKNOWN
)
145 * Make sure the device is in D0 state.
147 uart_change_pm(state
, UART_PM_STATE_ON
);
150 * Initialise and allocate the transmit and temporary
153 if (!state
->xmit
.buf
) {
154 /* This is protected by the per port mutex */
155 page
= get_zeroed_page(GFP_KERNEL
);
159 state
->xmit
.buf
= (unsigned char *) page
;
160 uart_circ_clear(&state
->xmit
);
163 retval
= uport
->ops
->startup(uport
);
165 if (uart_console(uport
) && uport
->cons
->cflag
) {
166 tty
->termios
.c_cflag
= uport
->cons
->cflag
;
167 uport
->cons
->cflag
= 0;
170 * Initialise the hardware port settings.
172 uart_change_speed(tty
, state
, NULL
);
176 * Setup the RTS and DTR signals once the
177 * port is open and ready to respond.
179 if (tty
->termios
.c_cflag
& CBAUD
)
180 uart_set_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
185 * This is to allow setserial on this port. People may want to set
186 * port/irq/type and then reconfigure the port properly if it failed
189 if (retval
&& capable(CAP_SYS_ADMIN
))
195 static int uart_startup(struct tty_struct
*tty
, struct uart_state
*state
,
198 struct tty_port
*port
= &state
->port
;
201 if (port
->flags
& ASYNC_INITIALIZED
)
205 * Set the TTY IO error marker - we will only clear this
206 * once we have successfully opened the port.
208 set_bit(TTY_IO_ERROR
, &tty
->flags
);
210 retval
= uart_port_startup(tty
, state
, init_hw
);
212 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
213 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
214 } else if (retval
> 0)
221 * This routine will shutdown a serial port; interrupts are disabled, and
222 * DTR is dropped if the hangup on close termio flag is on. Calls to
223 * uart_shutdown are serialised by the per-port semaphore.
225 static void uart_shutdown(struct tty_struct
*tty
, struct uart_state
*state
)
227 struct uart_port
*uport
= state
->uart_port
;
228 struct tty_port
*port
= &state
->port
;
231 * Set the TTY IO error marker
234 set_bit(TTY_IO_ERROR
, &tty
->flags
);
236 if (test_and_clear_bit(ASYNCB_INITIALIZED
, &port
->flags
)) {
238 * Turn off DTR and RTS early.
240 if (uart_console(uport
) && tty
)
241 uport
->cons
->cflag
= tty
->termios
.c_cflag
;
243 if (!tty
|| (tty
->termios
.c_cflag
& HUPCL
))
244 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
246 uart_port_shutdown(port
);
250 * It's possible for shutdown to be called after suspend if we get
251 * a DCD drop (hangup) at just the right time. Clear suspended bit so
252 * we don't try to resume a port that has been shutdown.
254 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
257 * Free the transmit buffer page.
259 if (state
->xmit
.buf
) {
260 free_page((unsigned long)state
->xmit
.buf
);
261 state
->xmit
.buf
= NULL
;
266 * uart_update_timeout - update per-port FIFO timeout.
267 * @port: uart_port structure describing the port
268 * @cflag: termios cflag value
269 * @baud: speed of the port
271 * Set the port FIFO timeout value. The @cflag value should
272 * reflect the actual hardware settings.
275 uart_update_timeout(struct uart_port
*port
, unsigned int cflag
,
280 /* byte size and parity */
281 switch (cflag
& CSIZE
) {
302 * The total number of bits to be transmitted in the fifo.
304 bits
= bits
* port
->fifosize
;
307 * Figure the timeout to send the above number of bits.
308 * Add .02 seconds of slop
310 port
->timeout
= (HZ
* bits
) / baud
+ HZ
/50;
313 EXPORT_SYMBOL(uart_update_timeout
);
316 * uart_get_baud_rate - return baud rate for a particular port
317 * @port: uart_port structure describing the port in question.
318 * @termios: desired termios settings.
319 * @old: old termios (or NULL)
320 * @min: minimum acceptable baud rate
321 * @max: maximum acceptable baud rate
323 * Decode the termios structure into a numeric baud rate,
324 * taking account of the magic 38400 baud rate (with spd_*
325 * flags), and mapping the %B0 rate to 9600 baud.
327 * If the new baud rate is invalid, try the old termios setting.
328 * If it's still invalid, we try 9600 baud.
330 * Update the @termios structure to reflect the baud rate
331 * we're actually going to be using. Don't do this for the case
332 * where B0 is requested ("hang up").
335 uart_get_baud_rate(struct uart_port
*port
, struct ktermios
*termios
,
336 struct ktermios
*old
, unsigned int min
, unsigned int max
)
338 unsigned int try, baud
, altbaud
= 38400;
340 upf_t flags
= port
->flags
& UPF_SPD_MASK
;
342 if (flags
== UPF_SPD_HI
)
344 else if (flags
== UPF_SPD_VHI
)
346 else if (flags
== UPF_SPD_SHI
)
348 else if (flags
== UPF_SPD_WARP
)
351 for (try = 0; try < 2; try++) {
352 baud
= tty_termios_baud_rate(termios
);
355 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
358 if (try == 0 && baud
== 38400)
362 * Special case: B0 rate.
369 if (baud
>= min
&& baud
<= max
)
373 * Oops, the quotient was zero. Try again with
374 * the old baud rate if possible.
376 termios
->c_cflag
&= ~CBAUD
;
378 baud
= tty_termios_baud_rate(old
);
380 tty_termios_encode_baud_rate(termios
,
387 * As a last resort, if the range cannot be met then clip to
388 * the nearest chip supported rate.
392 tty_termios_encode_baud_rate(termios
,
395 tty_termios_encode_baud_rate(termios
,
399 /* Should never happen */
404 EXPORT_SYMBOL(uart_get_baud_rate
);
407 * uart_get_divisor - return uart clock divisor
408 * @port: uart_port structure describing the port.
409 * @baud: desired baud rate
411 * Calculate the uart clock divisor for the port.
414 uart_get_divisor(struct uart_port
*port
, unsigned int baud
)
419 * Old custom speed handling.
421 if (baud
== 38400 && (port
->flags
& UPF_SPD_MASK
) == UPF_SPD_CUST
)
422 quot
= port
->custom_divisor
;
424 quot
= DIV_ROUND_CLOSEST(port
->uartclk
, 16 * baud
);
429 EXPORT_SYMBOL(uart_get_divisor
);
431 /* Caller holds port mutex */
432 static void uart_change_speed(struct tty_struct
*tty
, struct uart_state
*state
,
433 struct ktermios
*old_termios
)
435 struct uart_port
*uport
= state
->uart_port
;
436 struct ktermios
*termios
;
440 * If we have no tty, termios, or the port does not exist,
441 * then we can't set the parameters for this port.
443 if (!tty
|| uport
->type
== PORT_UNKNOWN
)
446 termios
= &tty
->termios
;
447 uport
->ops
->set_termios(uport
, termios
, old_termios
);
450 * Set modem status enables based on termios cflag
452 spin_lock_irq(&uport
->lock
);
453 if (termios
->c_cflag
& CRTSCTS
)
454 uport
->status
|= UPSTAT_CTS_ENABLE
;
456 uport
->status
&= ~UPSTAT_CTS_ENABLE
;
458 if (termios
->c_cflag
& CLOCAL
)
459 uport
->status
&= ~UPSTAT_DCD_ENABLE
;
461 uport
->status
|= UPSTAT_DCD_ENABLE
;
463 /* reset sw-assisted CTS flow control based on (possibly) new mode */
464 hw_stopped
= uport
->hw_stopped
;
465 uport
->hw_stopped
= uart_softcts_mode(uport
) &&
466 !(uport
->ops
->get_mctrl(uport
) & TIOCM_CTS
);
467 if (uport
->hw_stopped
) {
469 uport
->ops
->stop_tx(uport
);
474 spin_unlock_irq(&uport
->lock
);
477 static inline int __uart_put_char(struct uart_port
*port
,
478 struct circ_buf
*circ
, unsigned char c
)
486 spin_lock_irqsave(&port
->lock
, flags
);
487 if (uart_circ_chars_free(circ
) != 0) {
488 circ
->buf
[circ
->head
] = c
;
489 circ
->head
= (circ
->head
+ 1) & (UART_XMIT_SIZE
- 1);
492 spin_unlock_irqrestore(&port
->lock
, flags
);
496 static int uart_put_char(struct tty_struct
*tty
, unsigned char ch
)
498 struct uart_state
*state
= tty
->driver_data
;
500 return __uart_put_char(state
->uart_port
, &state
->xmit
, ch
);
503 static void uart_flush_chars(struct tty_struct
*tty
)
508 static int uart_write(struct tty_struct
*tty
,
509 const unsigned char *buf
, int count
)
511 struct uart_state
*state
= tty
->driver_data
;
512 struct uart_port
*port
;
513 struct circ_buf
*circ
;
518 * This means you called this function _after_ the port was
519 * closed. No cookie for you.
526 port
= state
->uart_port
;
532 spin_lock_irqsave(&port
->lock
, flags
);
534 c
= CIRC_SPACE_TO_END(circ
->head
, circ
->tail
, UART_XMIT_SIZE
);
539 memcpy(circ
->buf
+ circ
->head
, buf
, c
);
540 circ
->head
= (circ
->head
+ c
) & (UART_XMIT_SIZE
- 1);
547 spin_unlock_irqrestore(&port
->lock
, flags
);
552 static int uart_write_room(struct tty_struct
*tty
)
554 struct uart_state
*state
= tty
->driver_data
;
558 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
559 ret
= uart_circ_chars_free(&state
->xmit
);
560 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
564 static int uart_chars_in_buffer(struct tty_struct
*tty
)
566 struct uart_state
*state
= tty
->driver_data
;
570 spin_lock_irqsave(&state
->uart_port
->lock
, flags
);
571 ret
= uart_circ_chars_pending(&state
->xmit
);
572 spin_unlock_irqrestore(&state
->uart_port
->lock
, flags
);
576 static void uart_flush_buffer(struct tty_struct
*tty
)
578 struct uart_state
*state
= tty
->driver_data
;
579 struct uart_port
*port
;
583 * This means you called this function _after_ the port was
584 * closed. No cookie for you.
591 port
= state
->uart_port
;
592 pr_debug("uart_flush_buffer(%d) called\n", tty
->index
);
594 spin_lock_irqsave(&port
->lock
, flags
);
595 uart_circ_clear(&state
->xmit
);
596 if (port
->ops
->flush_buffer
)
597 port
->ops
->flush_buffer(port
);
598 spin_unlock_irqrestore(&port
->lock
, flags
);
603 * This function is used to send a high-priority XON/XOFF character to
606 static void uart_send_xchar(struct tty_struct
*tty
, char ch
)
608 struct uart_state
*state
= tty
->driver_data
;
609 struct uart_port
*port
= state
->uart_port
;
612 if (port
->ops
->send_xchar
)
613 port
->ops
->send_xchar(port
, ch
);
615 spin_lock_irqsave(&port
->lock
, flags
);
618 port
->ops
->start_tx(port
);
619 spin_unlock_irqrestore(&port
->lock
, flags
);
623 static void uart_throttle(struct tty_struct
*tty
)
625 struct uart_state
*state
= tty
->driver_data
;
626 struct uart_port
*port
= state
->uart_port
;
630 mask
|= UPSTAT_AUTOXOFF
;
631 if (tty
->termios
.c_cflag
& CRTSCTS
)
632 mask
|= UPSTAT_AUTORTS
;
634 if (port
->status
& mask
) {
635 port
->ops
->throttle(port
);
636 mask
&= ~port
->status
;
639 if (mask
& UPSTAT_AUTOXOFF
)
640 uart_send_xchar(tty
, STOP_CHAR(tty
));
642 if (mask
& UPSTAT_AUTORTS
)
643 uart_clear_mctrl(port
, TIOCM_RTS
);
646 static void uart_unthrottle(struct tty_struct
*tty
)
648 struct uart_state
*state
= tty
->driver_data
;
649 struct uart_port
*port
= state
->uart_port
;
653 mask
|= UPSTAT_AUTOXOFF
;
654 if (tty
->termios
.c_cflag
& CRTSCTS
)
655 mask
|= UPSTAT_AUTORTS
;
657 if (port
->status
& mask
) {
658 port
->ops
->unthrottle(port
);
659 mask
&= ~port
->status
;
662 if (mask
& UPSTAT_AUTOXOFF
)
663 uart_send_xchar(tty
, START_CHAR(tty
));
665 if (mask
& UPSTAT_AUTORTS
)
666 uart_set_mctrl(port
, TIOCM_RTS
);
669 static void do_uart_get_info(struct tty_port
*port
,
670 struct serial_struct
*retinfo
)
672 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
673 struct uart_port
*uport
= state
->uart_port
;
675 memset(retinfo
, 0, sizeof(*retinfo
));
677 retinfo
->type
= uport
->type
;
678 retinfo
->line
= uport
->line
;
679 retinfo
->port
= uport
->iobase
;
680 if (HIGH_BITS_OFFSET
)
681 retinfo
->port_high
= (long) uport
->iobase
>> HIGH_BITS_OFFSET
;
682 retinfo
->irq
= uport
->irq
;
683 retinfo
->flags
= uport
->flags
;
684 retinfo
->xmit_fifo_size
= uport
->fifosize
;
685 retinfo
->baud_base
= uport
->uartclk
/ 16;
686 retinfo
->close_delay
= jiffies_to_msecs(port
->close_delay
) / 10;
687 retinfo
->closing_wait
= port
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
688 ASYNC_CLOSING_WAIT_NONE
:
689 jiffies_to_msecs(port
->closing_wait
) / 10;
690 retinfo
->custom_divisor
= uport
->custom_divisor
;
691 retinfo
->hub6
= uport
->hub6
;
692 retinfo
->io_type
= uport
->iotype
;
693 retinfo
->iomem_reg_shift
= uport
->regshift
;
694 retinfo
->iomem_base
= (void *)(unsigned long)uport
->mapbase
;
697 static void uart_get_info(struct tty_port
*port
,
698 struct serial_struct
*retinfo
)
700 /* Ensure the state we copy is consistent and no hardware changes
702 mutex_lock(&port
->mutex
);
703 do_uart_get_info(port
, retinfo
);
704 mutex_unlock(&port
->mutex
);
707 static int uart_get_info_user(struct tty_port
*port
,
708 struct serial_struct __user
*retinfo
)
710 struct serial_struct tmp
;
711 uart_get_info(port
, &tmp
);
713 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
718 static int uart_set_info(struct tty_struct
*tty
, struct tty_port
*port
,
719 struct uart_state
*state
,
720 struct serial_struct
*new_info
)
722 struct uart_port
*uport
= state
->uart_port
;
723 unsigned long new_port
;
724 unsigned int change_irq
, change_port
, closing_wait
;
725 unsigned int old_custom_divisor
, close_delay
;
726 upf_t old_flags
, new_flags
;
729 new_port
= new_info
->port
;
730 if (HIGH_BITS_OFFSET
)
731 new_port
+= (unsigned long) new_info
->port_high
<< HIGH_BITS_OFFSET
;
733 new_info
->irq
= irq_canonicalize(new_info
->irq
);
734 close_delay
= msecs_to_jiffies(new_info
->close_delay
* 10);
735 closing_wait
= new_info
->closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
736 ASYNC_CLOSING_WAIT_NONE
:
737 msecs_to_jiffies(new_info
->closing_wait
* 10);
740 change_irq
= !(uport
->flags
& UPF_FIXED_PORT
)
741 && new_info
->irq
!= uport
->irq
;
744 * Since changing the 'type' of the port changes its resource
745 * allocations, we should treat type changes the same as
748 change_port
= !(uport
->flags
& UPF_FIXED_PORT
)
749 && (new_port
!= uport
->iobase
||
750 (unsigned long)new_info
->iomem_base
!= uport
->mapbase
||
751 new_info
->hub6
!= uport
->hub6
||
752 new_info
->io_type
!= uport
->iotype
||
753 new_info
->iomem_reg_shift
!= uport
->regshift
||
754 new_info
->type
!= uport
->type
);
756 old_flags
= uport
->flags
;
757 new_flags
= new_info
->flags
;
758 old_custom_divisor
= uport
->custom_divisor
;
760 if (!capable(CAP_SYS_ADMIN
)) {
762 if (change_irq
|| change_port
||
763 (new_info
->baud_base
!= uport
->uartclk
/ 16) ||
764 (close_delay
!= port
->close_delay
) ||
765 (closing_wait
!= port
->closing_wait
) ||
766 (new_info
->xmit_fifo_size
&&
767 new_info
->xmit_fifo_size
!= uport
->fifosize
) ||
768 (((new_flags
^ old_flags
) & ~UPF_USR_MASK
) != 0))
770 uport
->flags
= ((uport
->flags
& ~UPF_USR_MASK
) |
771 (new_flags
& UPF_USR_MASK
));
772 uport
->custom_divisor
= new_info
->custom_divisor
;
777 * Ask the low level driver to verify the settings.
779 if (uport
->ops
->verify_port
)
780 retval
= uport
->ops
->verify_port(uport
, new_info
);
782 if ((new_info
->irq
>= nr_irqs
) || (new_info
->irq
< 0) ||
783 (new_info
->baud_base
< 9600))
789 if (change_port
|| change_irq
) {
793 * Make sure that we are the sole user of this port.
795 if (tty_port_users(port
) > 1)
799 * We need to shutdown the serial port at the old
800 * port/type/irq combination.
802 uart_shutdown(tty
, state
);
806 unsigned long old_iobase
, old_mapbase
;
807 unsigned int old_type
, old_iotype
, old_hub6
, old_shift
;
809 old_iobase
= uport
->iobase
;
810 old_mapbase
= uport
->mapbase
;
811 old_type
= uport
->type
;
812 old_hub6
= uport
->hub6
;
813 old_iotype
= uport
->iotype
;
814 old_shift
= uport
->regshift
;
817 * Free and release old regions
819 if (old_type
!= PORT_UNKNOWN
)
820 uport
->ops
->release_port(uport
);
822 uport
->iobase
= new_port
;
823 uport
->type
= new_info
->type
;
824 uport
->hub6
= new_info
->hub6
;
825 uport
->iotype
= new_info
->io_type
;
826 uport
->regshift
= new_info
->iomem_reg_shift
;
827 uport
->mapbase
= (unsigned long)new_info
->iomem_base
;
830 * Claim and map the new regions
832 if (uport
->type
!= PORT_UNKNOWN
) {
833 retval
= uport
->ops
->request_port(uport
);
835 /* Always success - Jean II */
840 * If we fail to request resources for the
841 * new port, try to restore the old settings.
844 uport
->iobase
= old_iobase
;
845 uport
->type
= old_type
;
846 uport
->hub6
= old_hub6
;
847 uport
->iotype
= old_iotype
;
848 uport
->regshift
= old_shift
;
849 uport
->mapbase
= old_mapbase
;
851 if (old_type
!= PORT_UNKNOWN
) {
852 retval
= uport
->ops
->request_port(uport
);
854 * If we failed to restore the old settings,
858 uport
->type
= PORT_UNKNOWN
;
866 /* Added to return the correct error -Ram Gupta */
872 uport
->irq
= new_info
->irq
;
873 if (!(uport
->flags
& UPF_FIXED_PORT
))
874 uport
->uartclk
= new_info
->baud_base
* 16;
875 uport
->flags
= (uport
->flags
& ~UPF_CHANGE_MASK
) |
876 (new_flags
& UPF_CHANGE_MASK
);
877 uport
->custom_divisor
= new_info
->custom_divisor
;
878 port
->close_delay
= close_delay
;
879 port
->closing_wait
= closing_wait
;
880 if (new_info
->xmit_fifo_size
)
881 uport
->fifosize
= new_info
->xmit_fifo_size
;
882 port
->low_latency
= (uport
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
886 if (uport
->type
== PORT_UNKNOWN
)
888 if (port
->flags
& ASYNC_INITIALIZED
) {
889 if (((old_flags
^ uport
->flags
) & UPF_SPD_MASK
) ||
890 old_custom_divisor
!= uport
->custom_divisor
) {
892 * If they're setting up a custom divisor or speed,
893 * instead of clearing it, then bitch about it. No
894 * need to rate-limit; it's CAP_SYS_ADMIN only.
896 if (uport
->flags
& UPF_SPD_MASK
) {
899 dev_notice(uport
->dev
,
900 "%s sets custom speed on %s. This is deprecated.\n",
902 tty_name(port
->tty
, buf
));
904 uart_change_speed(tty
, state
, NULL
);
907 retval
= uart_startup(tty
, state
, 1);
912 static int uart_set_info_user(struct tty_struct
*tty
, struct uart_state
*state
,
913 struct serial_struct __user
*newinfo
)
915 struct serial_struct new_serial
;
916 struct tty_port
*port
= &state
->port
;
919 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
923 * This semaphore protects port->count. It is also
924 * very useful to prevent opens. Also, take the
925 * port configuration semaphore to make sure that a
926 * module insertion/removal doesn't change anything
929 mutex_lock(&port
->mutex
);
930 retval
= uart_set_info(tty
, port
, state
, &new_serial
);
931 mutex_unlock(&port
->mutex
);
936 * uart_get_lsr_info - get line status register info
937 * @tty: tty associated with the UART
938 * @state: UART being queried
939 * @value: returned modem value
941 * Note: uart_ioctl protects us against hangups.
943 static int uart_get_lsr_info(struct tty_struct
*tty
,
944 struct uart_state
*state
, unsigned int __user
*value
)
946 struct uart_port
*uport
= state
->uart_port
;
949 result
= uport
->ops
->tx_empty(uport
);
952 * If we're about to load something into the transmit
953 * register, we'll pretend the transmitter isn't empty to
954 * avoid a race condition (depending on when the transmit
955 * interrupt happens).
958 ((uart_circ_chars_pending(&state
->xmit
) > 0) &&
959 !uart_tx_stopped(uport
)))
960 result
&= ~TIOCSER_TEMT
;
962 return put_user(result
, value
);
965 static int uart_tiocmget(struct tty_struct
*tty
)
967 struct uart_state
*state
= tty
->driver_data
;
968 struct tty_port
*port
= &state
->port
;
969 struct uart_port
*uport
= state
->uart_port
;
972 mutex_lock(&port
->mutex
);
973 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
974 result
= uport
->mctrl
;
975 spin_lock_irq(&uport
->lock
);
976 result
|= uport
->ops
->get_mctrl(uport
);
977 spin_unlock_irq(&uport
->lock
);
979 mutex_unlock(&port
->mutex
);
985 uart_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
)
987 struct uart_state
*state
= tty
->driver_data
;
988 struct uart_port
*uport
= state
->uart_port
;
989 struct tty_port
*port
= &state
->port
;
992 mutex_lock(&port
->mutex
);
993 if (!(tty
->flags
& (1 << TTY_IO_ERROR
))) {
994 uart_update_mctrl(uport
, set
, clear
);
997 mutex_unlock(&port
->mutex
);
1001 static int uart_break_ctl(struct tty_struct
*tty
, int break_state
)
1003 struct uart_state
*state
= tty
->driver_data
;
1004 struct tty_port
*port
= &state
->port
;
1005 struct uart_port
*uport
= state
->uart_port
;
1007 mutex_lock(&port
->mutex
);
1009 if (uport
->type
!= PORT_UNKNOWN
)
1010 uport
->ops
->break_ctl(uport
, break_state
);
1012 mutex_unlock(&port
->mutex
);
1016 static int uart_do_autoconfig(struct tty_struct
*tty
,struct uart_state
*state
)
1018 struct uart_port
*uport
= state
->uart_port
;
1019 struct tty_port
*port
= &state
->port
;
1022 if (!capable(CAP_SYS_ADMIN
))
1026 * Take the per-port semaphore. This prevents count from
1027 * changing, and hence any extra opens of the port while
1028 * we're auto-configuring.
1030 if (mutex_lock_interruptible(&port
->mutex
))
1031 return -ERESTARTSYS
;
1034 if (tty_port_users(port
) == 1) {
1035 uart_shutdown(tty
, state
);
1038 * If we already have a port type configured,
1039 * we must release its resources.
1041 if (uport
->type
!= PORT_UNKNOWN
)
1042 uport
->ops
->release_port(uport
);
1044 flags
= UART_CONFIG_TYPE
;
1045 if (uport
->flags
& UPF_AUTO_IRQ
)
1046 flags
|= UART_CONFIG_IRQ
;
1049 * This will claim the ports resources if
1052 uport
->ops
->config_port(uport
, flags
);
1054 ret
= uart_startup(tty
, state
, 1);
1056 mutex_unlock(&port
->mutex
);
1060 static void uart_enable_ms(struct uart_port
*uport
)
1063 * Force modem status interrupts on
1065 if (uport
->ops
->enable_ms
)
1066 uport
->ops
->enable_ms(uport
);
1070 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1071 * - mask passed in arg for lines of interest
1072 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1073 * Caller should use TIOCGICOUNT to see which one it was
1075 * FIXME: This wants extracting into a common all driver implementation
1076 * of TIOCMWAIT using tty_port.
1079 uart_wait_modem_status(struct uart_state
*state
, unsigned long arg
)
1081 struct uart_port
*uport
= state
->uart_port
;
1082 struct tty_port
*port
= &state
->port
;
1083 DECLARE_WAITQUEUE(wait
, current
);
1084 struct uart_icount cprev
, cnow
;
1088 * note the counters on entry
1090 spin_lock_irq(&uport
->lock
);
1091 memcpy(&cprev
, &uport
->icount
, sizeof(struct uart_icount
));
1092 uart_enable_ms(uport
);
1093 spin_unlock_irq(&uport
->lock
);
1095 add_wait_queue(&port
->delta_msr_wait
, &wait
);
1097 spin_lock_irq(&uport
->lock
);
1098 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1099 spin_unlock_irq(&uport
->lock
);
1101 set_current_state(TASK_INTERRUPTIBLE
);
1103 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1104 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1105 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1106 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1113 /* see if a signal did it */
1114 if (signal_pending(current
)) {
1121 __set_current_state(TASK_RUNNING
);
1122 remove_wait_queue(&port
->delta_msr_wait
, &wait
);
1128 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1129 * Return: write counters to the user passed counter struct
1130 * NB: both 1->0 and 0->1 transitions are counted except for
1131 * RI where only 0->1 is counted.
1133 static int uart_get_icount(struct tty_struct
*tty
,
1134 struct serial_icounter_struct
*icount
)
1136 struct uart_state
*state
= tty
->driver_data
;
1137 struct uart_icount cnow
;
1138 struct uart_port
*uport
= state
->uart_port
;
1140 spin_lock_irq(&uport
->lock
);
1141 memcpy(&cnow
, &uport
->icount
, sizeof(struct uart_icount
));
1142 spin_unlock_irq(&uport
->lock
);
1144 icount
->cts
= cnow
.cts
;
1145 icount
->dsr
= cnow
.dsr
;
1146 icount
->rng
= cnow
.rng
;
1147 icount
->dcd
= cnow
.dcd
;
1148 icount
->rx
= cnow
.rx
;
1149 icount
->tx
= cnow
.tx
;
1150 icount
->frame
= cnow
.frame
;
1151 icount
->overrun
= cnow
.overrun
;
1152 icount
->parity
= cnow
.parity
;
1153 icount
->brk
= cnow
.brk
;
1154 icount
->buf_overrun
= cnow
.buf_overrun
;
1159 static int uart_get_rs485_config(struct uart_port
*port
,
1160 struct serial_rs485 __user
*rs485
)
1162 unsigned long flags
;
1163 struct serial_rs485 aux
;
1165 spin_lock_irqsave(&port
->lock
, flags
);
1167 spin_unlock_irqrestore(&port
->lock
, flags
);
1169 if (copy_to_user(rs485
, &aux
, sizeof(aux
)))
1175 static int uart_set_rs485_config(struct uart_port
*port
,
1176 struct serial_rs485 __user
*rs485_user
)
1178 struct serial_rs485 rs485
;
1180 unsigned long flags
;
1182 if (!port
->rs485_config
)
1183 return -ENOIOCTLCMD
;
1185 if (copy_from_user(&rs485
, rs485_user
, sizeof(*rs485_user
)))
1188 spin_lock_irqsave(&port
->lock
, flags
);
1189 ret
= port
->rs485_config(port
, &rs485
);
1190 spin_unlock_irqrestore(&port
->lock
, flags
);
1194 if (copy_to_user(rs485_user
, &port
->rs485
, sizeof(port
->rs485
)))
1201 * Called via sys_ioctl. We can use spin_lock_irq() here.
1204 uart_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1207 struct uart_state
*state
= tty
->driver_data
;
1208 struct tty_port
*port
= &state
->port
;
1209 void __user
*uarg
= (void __user
*)arg
;
1210 int ret
= -ENOIOCTLCMD
;
1214 * These ioctls don't rely on the hardware to be present.
1218 ret
= uart_get_info_user(port
, uarg
);
1222 down_write(&tty
->termios_rwsem
);
1223 ret
= uart_set_info_user(tty
, state
, uarg
);
1224 up_write(&tty
->termios_rwsem
);
1228 down_write(&tty
->termios_rwsem
);
1229 ret
= uart_do_autoconfig(tty
, state
);
1230 up_write(&tty
->termios_rwsem
);
1233 case TIOCSERGWILD
: /* obsolete */
1234 case TIOCSERSWILD
: /* obsolete */
1239 if (ret
!= -ENOIOCTLCMD
)
1242 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1248 * The following should only be used when hardware is present.
1252 ret
= uart_wait_modem_status(state
, arg
);
1256 if (ret
!= -ENOIOCTLCMD
)
1259 mutex_lock(&port
->mutex
);
1261 if (tty
->flags
& (1 << TTY_IO_ERROR
)) {
1267 * All these rely on hardware being present and need to be
1268 * protected against the tty being hung up.
1272 case TIOCSERGETLSR
: /* Get line status register */
1273 ret
= uart_get_lsr_info(tty
, state
, uarg
);
1277 ret
= uart_get_rs485_config(state
->uart_port
, uarg
);
1281 ret
= uart_set_rs485_config(state
->uart_port
, uarg
);
1284 struct uart_port
*uport
= state
->uart_port
;
1285 if (uport
->ops
->ioctl
)
1286 ret
= uport
->ops
->ioctl(uport
, cmd
, arg
);
1291 mutex_unlock(&port
->mutex
);
1296 static void uart_set_ldisc(struct tty_struct
*tty
)
1298 struct uart_state
*state
= tty
->driver_data
;
1299 struct uart_port
*uport
= state
->uart_port
;
1301 if (uport
->ops
->set_ldisc
) {
1302 mutex_lock(&state
->port
.mutex
);
1303 uport
->ops
->set_ldisc(uport
, &tty
->termios
);
1304 mutex_unlock(&state
->port
.mutex
);
1308 static void uart_set_termios(struct tty_struct
*tty
,
1309 struct ktermios
*old_termios
)
1311 struct uart_state
*state
= tty
->driver_data
;
1312 struct uart_port
*uport
= state
->uart_port
;
1313 unsigned int cflag
= tty
->termios
.c_cflag
;
1314 unsigned int iflag_mask
= IGNBRK
|BRKINT
|IGNPAR
|PARMRK
|INPCK
;
1315 bool sw_changed
= false;
1318 * Drivers doing software flow control also need to know
1319 * about changes to these input settings.
1321 if (uport
->flags
& UPF_SOFT_FLOW
) {
1322 iflag_mask
|= IXANY
|IXON
|IXOFF
;
1324 tty
->termios
.c_cc
[VSTART
] != old_termios
->c_cc
[VSTART
] ||
1325 tty
->termios
.c_cc
[VSTOP
] != old_termios
->c_cc
[VSTOP
];
1329 * These are the bits that are used to setup various
1330 * flags in the low level driver. We can ignore the Bfoo
1331 * bits in c_cflag; c_[io]speed will always be set
1332 * appropriately by set_termios() in tty_ioctl.c
1334 if ((cflag
^ old_termios
->c_cflag
) == 0 &&
1335 tty
->termios
.c_ospeed
== old_termios
->c_ospeed
&&
1336 tty
->termios
.c_ispeed
== old_termios
->c_ispeed
&&
1337 ((tty
->termios
.c_iflag
^ old_termios
->c_iflag
) & iflag_mask
) == 0 &&
1342 mutex_lock(&state
->port
.mutex
);
1343 uart_change_speed(tty
, state
, old_termios
);
1344 mutex_unlock(&state
->port
.mutex
);
1345 /* reload cflag from termios; port driver may have overriden flags */
1346 cflag
= tty
->termios
.c_cflag
;
1348 /* Handle transition to B0 status */
1349 if ((old_termios
->c_cflag
& CBAUD
) && !(cflag
& CBAUD
))
1350 uart_clear_mctrl(uport
, TIOCM_RTS
| TIOCM_DTR
);
1351 /* Handle transition away from B0 status */
1352 else if (!(old_termios
->c_cflag
& CBAUD
) && (cflag
& CBAUD
)) {
1353 unsigned int mask
= TIOCM_DTR
;
1354 if (!(cflag
& CRTSCTS
) || !test_bit(TTY_THROTTLED
, &tty
->flags
))
1356 uart_set_mctrl(uport
, mask
);
1361 * Calls to uart_close() are serialised via the tty_lock in
1362 * drivers/tty/tty_io.c:tty_release()
1363 * drivers/tty/tty_io.c:do_tty_hangup()
1364 * This runs from a workqueue and can sleep for a _short_ time only.
1366 static void uart_close(struct tty_struct
*tty
, struct file
*filp
)
1368 struct uart_state
*state
= tty
->driver_data
;
1369 struct tty_port
*port
;
1370 struct uart_port
*uport
;
1371 unsigned long flags
;
1374 struct uart_driver
*drv
= tty
->driver
->driver_state
;
1376 state
= drv
->state
+ tty
->index
;
1377 port
= &state
->port
;
1378 spin_lock_irq(&port
->lock
);
1380 spin_unlock_irq(&port
->lock
);
1384 uport
= state
->uart_port
;
1385 port
= &state
->port
;
1387 pr_debug("uart_close(%d) called\n", uport
? uport
->line
: -1);
1389 if (!port
->count
|| tty_port_close_start(port
, tty
, filp
) == 0)
1393 * At this point, we stop accepting input. To do this, we
1394 * disable the receive line status interrupts.
1396 if (port
->flags
& ASYNC_INITIALIZED
) {
1397 unsigned long flags
;
1398 spin_lock_irqsave(&uport
->lock
, flags
);
1399 uport
->ops
->stop_rx(uport
);
1400 spin_unlock_irqrestore(&uport
->lock
, flags
);
1402 * Before we drop DTR, make sure the UART transmitter
1403 * has completely drained; this is especially
1404 * important if there is a transmit FIFO!
1406 uart_wait_until_sent(tty
, uport
->timeout
);
1409 mutex_lock(&port
->mutex
);
1410 uart_shutdown(tty
, state
);
1411 tty_port_tty_set(port
, NULL
);
1413 spin_lock_irqsave(&port
->lock
, flags
);
1415 if (port
->blocked_open
) {
1416 spin_unlock_irqrestore(&port
->lock
, flags
);
1417 if (port
->close_delay
)
1418 msleep_interruptible(jiffies_to_msecs(port
->close_delay
));
1419 spin_lock_irqsave(&port
->lock
, flags
);
1420 } else if (!uart_console(uport
)) {
1421 spin_unlock_irqrestore(&port
->lock
, flags
);
1422 uart_change_pm(state
, UART_PM_STATE_OFF
);
1423 spin_lock_irqsave(&port
->lock
, flags
);
1427 * Wake up anyone trying to open this port.
1429 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1430 clear_bit(ASYNCB_CLOSING
, &port
->flags
);
1431 spin_unlock_irqrestore(&port
->lock
, flags
);
1432 wake_up_interruptible(&port
->open_wait
);
1433 wake_up_interruptible(&port
->close_wait
);
1435 mutex_unlock(&port
->mutex
);
1437 tty_ldisc_flush(tty
);
1441 static void uart_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1443 struct uart_state
*state
= tty
->driver_data
;
1444 struct uart_port
*port
= state
->uart_port
;
1445 unsigned long char_time
, expire
;
1447 if (port
->type
== PORT_UNKNOWN
|| port
->fifosize
== 0)
1451 * Set the check interval to be 1/5 of the estimated time to
1452 * send a single character, and make it at least 1. The check
1453 * interval should also be less than the timeout.
1455 * Note: we have to use pretty tight timings here to satisfy
1458 char_time
= (port
->timeout
- HZ
/50) / port
->fifosize
;
1459 char_time
= char_time
/ 5;
1462 if (timeout
&& timeout
< char_time
)
1463 char_time
= timeout
;
1466 * If the transmitter hasn't cleared in twice the approximate
1467 * amount of time to send the entire FIFO, it probably won't
1468 * ever clear. This assumes the UART isn't doing flow
1469 * control, which is currently the case. Hence, if it ever
1470 * takes longer than port->timeout, this is probably due to a
1471 * UART bug of some kind. So, we clamp the timeout parameter at
1474 if (timeout
== 0 || timeout
> 2 * port
->timeout
)
1475 timeout
= 2 * port
->timeout
;
1477 expire
= jiffies
+ timeout
;
1479 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1480 port
->line
, jiffies
, expire
);
1483 * Check whether the transmitter is empty every 'char_time'.
1484 * 'timeout' / 'expire' give us the maximum amount of time
1487 while (!port
->ops
->tx_empty(port
)) {
1488 msleep_interruptible(jiffies_to_msecs(char_time
));
1489 if (signal_pending(current
))
1491 if (time_after(jiffies
, expire
))
1497 * Calls to uart_hangup() are serialised by the tty_lock in
1498 * drivers/tty/tty_io.c:do_tty_hangup()
1499 * This runs from a workqueue and can sleep for a _short_ time only.
1501 static void uart_hangup(struct tty_struct
*tty
)
1503 struct uart_state
*state
= tty
->driver_data
;
1504 struct tty_port
*port
= &state
->port
;
1505 unsigned long flags
;
1507 pr_debug("uart_hangup(%d)\n", state
->uart_port
->line
);
1509 mutex_lock(&port
->mutex
);
1510 if (port
->flags
& ASYNC_NORMAL_ACTIVE
) {
1511 uart_flush_buffer(tty
);
1512 uart_shutdown(tty
, state
);
1513 spin_lock_irqsave(&port
->lock
, flags
);
1515 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1516 spin_unlock_irqrestore(&port
->lock
, flags
);
1517 tty_port_tty_set(port
, NULL
);
1518 if (!uart_console(state
->uart_port
))
1519 uart_change_pm(state
, UART_PM_STATE_OFF
);
1520 wake_up_interruptible(&port
->open_wait
);
1521 wake_up_interruptible(&port
->delta_msr_wait
);
1523 mutex_unlock(&port
->mutex
);
1526 static int uart_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
1531 static void uart_port_shutdown(struct tty_port
*port
)
1533 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1534 struct uart_port
*uport
= state
->uart_port
;
1537 * clear delta_msr_wait queue to avoid mem leaks: we may free
1538 * the irq here so the queue might never be woken up. Note
1539 * that we won't end up waiting on delta_msr_wait again since
1540 * any outstanding file descriptors should be pointing at
1541 * hung_up_tty_fops now.
1543 wake_up_interruptible(&port
->delta_msr_wait
);
1546 * Free the IRQ and disable the port.
1548 uport
->ops
->shutdown(uport
);
1551 * Ensure that the IRQ handler isn't running on another CPU.
1553 synchronize_irq(uport
->irq
);
1556 static int uart_carrier_raised(struct tty_port
*port
)
1558 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1559 struct uart_port
*uport
= state
->uart_port
;
1561 spin_lock_irq(&uport
->lock
);
1562 uart_enable_ms(uport
);
1563 mctrl
= uport
->ops
->get_mctrl(uport
);
1564 spin_unlock_irq(&uport
->lock
);
1565 if (mctrl
& TIOCM_CAR
)
1570 static void uart_dtr_rts(struct tty_port
*port
, int onoff
)
1572 struct uart_state
*state
= container_of(port
, struct uart_state
, port
);
1573 struct uart_port
*uport
= state
->uart_port
;
1576 uart_set_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1578 uart_clear_mctrl(uport
, TIOCM_DTR
| TIOCM_RTS
);
1582 * Calls to uart_open are serialised by the tty_lock in
1583 * drivers/tty/tty_io.c:tty_open()
1584 * Note that if this fails, then uart_close() _will_ be called.
1586 * In time, we want to scrap the "opening nonpresent ports"
1587 * behaviour and implement an alternative way for setserial
1588 * to set base addresses/ports/types. This will allow us to
1589 * get rid of a certain amount of extra tests.
1591 static int uart_open(struct tty_struct
*tty
, struct file
*filp
)
1593 struct uart_driver
*drv
= (struct uart_driver
*)tty
->driver
->driver_state
;
1594 int retval
, line
= tty
->index
;
1595 struct uart_state
*state
= drv
->state
+ line
;
1596 struct tty_port
*port
= &state
->port
;
1598 pr_debug("uart_open(%d) called\n", line
);
1600 spin_lock_irq(&port
->lock
);
1602 spin_unlock_irq(&port
->lock
);
1605 * We take the semaphore here to guarantee that we won't be re-entered
1606 * while allocating the state structure, or while we request any IRQs
1607 * that the driver may need. This also has the nice side-effect that
1608 * it delays the action of uart_hangup, so we can guarantee that
1609 * state->port.tty will always contain something reasonable.
1611 if (mutex_lock_interruptible(&port
->mutex
)) {
1612 retval
= -ERESTARTSYS
;
1616 if (!state
->uart_port
|| state
->uart_port
->flags
& UPF_DEAD
) {
1621 tty
->driver_data
= state
;
1622 state
->uart_port
->state
= state
;
1623 state
->port
.low_latency
=
1624 (state
->uart_port
->flags
& UPF_LOW_LATENCY
) ? 1 : 0;
1625 tty_port_tty_set(port
, tty
);
1628 * Start up the serial port.
1630 retval
= uart_startup(tty
, state
, 0);
1633 * If we succeeded, wait until the port is ready.
1635 mutex_unlock(&port
->mutex
);
1637 retval
= tty_port_block_til_ready(port
, tty
, filp
);
1642 mutex_unlock(&port
->mutex
);
1646 static const char *uart_type(struct uart_port
*port
)
1648 const char *str
= NULL
;
1650 if (port
->ops
->type
)
1651 str
= port
->ops
->type(port
);
1659 #ifdef CONFIG_PROC_FS
1661 static void uart_line_info(struct seq_file
*m
, struct uart_driver
*drv
, int i
)
1663 struct uart_state
*state
= drv
->state
+ i
;
1664 struct tty_port
*port
= &state
->port
;
1665 enum uart_pm_state pm_state
;
1666 struct uart_port
*uport
= state
->uart_port
;
1668 unsigned int status
;
1674 mmio
= uport
->iotype
>= UPIO_MEM
;
1675 seq_printf(m
, "%d: uart:%s %s%08llX irq:%d",
1676 uport
->line
, uart_type(uport
),
1677 mmio
? "mmio:0x" : "port:",
1678 mmio
? (unsigned long long)uport
->mapbase
1679 : (unsigned long long)uport
->iobase
,
1682 if (uport
->type
== PORT_UNKNOWN
) {
1687 if (capable(CAP_SYS_ADMIN
)) {
1688 mutex_lock(&port
->mutex
);
1689 pm_state
= state
->pm_state
;
1690 if (pm_state
!= UART_PM_STATE_ON
)
1691 uart_change_pm(state
, UART_PM_STATE_ON
);
1692 spin_lock_irq(&uport
->lock
);
1693 status
= uport
->ops
->get_mctrl(uport
);
1694 spin_unlock_irq(&uport
->lock
);
1695 if (pm_state
!= UART_PM_STATE_ON
)
1696 uart_change_pm(state
, pm_state
);
1697 mutex_unlock(&port
->mutex
);
1699 seq_printf(m
, " tx:%d rx:%d",
1700 uport
->icount
.tx
, uport
->icount
.rx
);
1701 if (uport
->icount
.frame
)
1702 seq_printf(m
, " fe:%d",
1703 uport
->icount
.frame
);
1704 if (uport
->icount
.parity
)
1705 seq_printf(m
, " pe:%d",
1706 uport
->icount
.parity
);
1707 if (uport
->icount
.brk
)
1708 seq_printf(m
, " brk:%d",
1710 if (uport
->icount
.overrun
)
1711 seq_printf(m
, " oe:%d",
1712 uport
->icount
.overrun
);
1714 #define INFOBIT(bit, str) \
1715 if (uport->mctrl & (bit)) \
1716 strncat(stat_buf, (str), sizeof(stat_buf) - \
1717 strlen(stat_buf) - 2)
1718 #define STATBIT(bit, str) \
1719 if (status & (bit)) \
1720 strncat(stat_buf, (str), sizeof(stat_buf) - \
1721 strlen(stat_buf) - 2)
1725 INFOBIT(TIOCM_RTS
, "|RTS");
1726 STATBIT(TIOCM_CTS
, "|CTS");
1727 INFOBIT(TIOCM_DTR
, "|DTR");
1728 STATBIT(TIOCM_DSR
, "|DSR");
1729 STATBIT(TIOCM_CAR
, "|CD");
1730 STATBIT(TIOCM_RNG
, "|RI");
1734 seq_puts(m
, stat_buf
);
1741 static int uart_proc_show(struct seq_file
*m
, void *v
)
1743 struct tty_driver
*ttydrv
= m
->private;
1744 struct uart_driver
*drv
= ttydrv
->driver_state
;
1747 seq_printf(m
, "serinfo:1.0 driver%s%s revision:%s\n",
1749 for (i
= 0; i
< drv
->nr
; i
++)
1750 uart_line_info(m
, drv
, i
);
1754 static int uart_proc_open(struct inode
*inode
, struct file
*file
)
1756 return single_open(file
, uart_proc_show
, PDE_DATA(inode
));
1759 static const struct file_operations uart_proc_fops
= {
1760 .owner
= THIS_MODULE
,
1761 .open
= uart_proc_open
,
1763 .llseek
= seq_lseek
,
1764 .release
= single_release
,
1768 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1770 * uart_console_write - write a console message to a serial port
1771 * @port: the port to write the message
1772 * @s: array of characters
1773 * @count: number of characters in string to write
1774 * @putchar: function to write character to port
1776 void uart_console_write(struct uart_port
*port
, const char *s
,
1778 void (*putchar
)(struct uart_port
*, int))
1782 for (i
= 0; i
< count
; i
++, s
++) {
1784 putchar(port
, '\r');
1788 EXPORT_SYMBOL_GPL(uart_console_write
);
1791 * Check whether an invalid uart number has been specified, and
1792 * if so, search for the first available port that does have
1795 struct uart_port
* __init
1796 uart_get_console(struct uart_port
*ports
, int nr
, struct console
*co
)
1798 int idx
= co
->index
;
1800 if (idx
< 0 || idx
>= nr
|| (ports
[idx
].iobase
== 0 &&
1801 ports
[idx
].membase
== NULL
))
1802 for (idx
= 0; idx
< nr
; idx
++)
1803 if (ports
[idx
].iobase
!= 0 ||
1804 ports
[idx
].membase
!= NULL
)
1813 * uart_parse_earlycon - Parse earlycon options
1814 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1815 * @iotype: ptr for decoded iotype (out)
1816 * @addr: ptr for decoded mapbase/iobase (out)
1817 * @options: ptr for <options> field; NULL if not present (out)
1819 * Decodes earlycon kernel command line parameters of the form
1820 * earlycon=<name>,io|mmio|mmio32,<addr>,<options>
1821 * console=<name>,io|mmio|mmio32,<addr>,<options>
1824 * earlycon=<name>,0x<addr>,<options>
1825 * console=<name>,0x<addr>,<options>
1826 * is also accepted; the returned @iotype will be UPIO_MEM.
1828 * Returns 0 on success or -EINVAL on failure
1830 int uart_parse_earlycon(char *p
, unsigned char *iotype
, unsigned long *addr
,
1833 if (strncmp(p
, "mmio,", 5) == 0) {
1836 } else if (strncmp(p
, "mmio32,", 7) == 0) {
1837 *iotype
= UPIO_MEM32
;
1839 } else if (strncmp(p
, "io,", 3) == 0) {
1840 *iotype
= UPIO_PORT
;
1842 } else if (strncmp(p
, "0x", 2) == 0) {
1848 *addr
= simple_strtoul(p
, NULL
, 0);
1856 EXPORT_SYMBOL_GPL(uart_parse_earlycon
);
1859 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1860 * @options: pointer to option string
1861 * @baud: pointer to an 'int' variable for the baud rate.
1862 * @parity: pointer to an 'int' variable for the parity.
1863 * @bits: pointer to an 'int' variable for the number of data bits.
1864 * @flow: pointer to an 'int' variable for the flow control character.
1866 * uart_parse_options decodes a string containing the serial console
1867 * options. The format of the string is <baud><parity><bits><flow>,
1871 uart_parse_options(char *options
, int *baud
, int *parity
, int *bits
, int *flow
)
1875 *baud
= simple_strtoul(s
, NULL
, 10);
1876 while (*s
>= '0' && *s
<= '9')
1885 EXPORT_SYMBOL_GPL(uart_parse_options
);
1892 static const struct baud_rates baud_rates
[] = {
1893 { 921600, B921600
},
1894 { 460800, B460800
},
1895 { 230400, B230400
},
1896 { 115200, B115200
},
1908 * uart_set_options - setup the serial console parameters
1909 * @port: pointer to the serial ports uart_port structure
1910 * @co: console pointer
1912 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1913 * @bits: number of data bits
1914 * @flow: flow control character - 'r' (rts)
1917 uart_set_options(struct uart_port
*port
, struct console
*co
,
1918 int baud
, int parity
, int bits
, int flow
)
1920 struct ktermios termios
;
1921 static struct ktermios dummy
;
1925 * Ensure that the serial console lock is initialised
1927 * If this port is a console, then the spinlock is already
1930 if (!(uart_console(port
) && (port
->cons
->flags
& CON_ENABLED
))) {
1931 spin_lock_init(&port
->lock
);
1932 lockdep_set_class(&port
->lock
, &port_lock_key
);
1935 memset(&termios
, 0, sizeof(struct ktermios
));
1937 termios
.c_cflag
= CREAD
| HUPCL
| CLOCAL
;
1940 * Construct a cflag setting.
1942 for (i
= 0; baud_rates
[i
].rate
; i
++)
1943 if (baud_rates
[i
].rate
<= baud
)
1946 termios
.c_cflag
|= baud_rates
[i
].cflag
;
1949 termios
.c_cflag
|= CS7
;
1951 termios
.c_cflag
|= CS8
;
1955 termios
.c_cflag
|= PARODD
;
1958 termios
.c_cflag
|= PARENB
;
1963 termios
.c_cflag
|= CRTSCTS
;
1966 * some uarts on other side don't support no flow control.
1967 * So we set * DTR in host uart to make them happy
1969 port
->mctrl
|= TIOCM_DTR
;
1971 port
->ops
->set_termios(port
, &termios
, &dummy
);
1973 * Allow the setting of the UART parameters with a NULL console
1977 co
->cflag
= termios
.c_cflag
;
1981 EXPORT_SYMBOL_GPL(uart_set_options
);
1982 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1985 * uart_change_pm - set power state of the port
1987 * @state: port descriptor
1988 * @pm_state: new state
1990 * Locking: port->mutex has to be held
1992 static void uart_change_pm(struct uart_state
*state
,
1993 enum uart_pm_state pm_state
)
1995 struct uart_port
*port
= state
->uart_port
;
1997 if (state
->pm_state
!= pm_state
) {
1999 port
->ops
->pm(port
, pm_state
, state
->pm_state
);
2000 state
->pm_state
= pm_state
;
2005 struct uart_port
*port
;
2006 struct uart_driver
*driver
;
2009 static int serial_match_port(struct device
*dev
, void *data
)
2011 struct uart_match
*match
= data
;
2012 struct tty_driver
*tty_drv
= match
->driver
->tty_driver
;
2013 dev_t devt
= MKDEV(tty_drv
->major
, tty_drv
->minor_start
) +
2016 return dev
->devt
== devt
; /* Actually, only one tty per port */
2019 int uart_suspend_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2021 struct uart_state
*state
= drv
->state
+ uport
->line
;
2022 struct tty_port
*port
= &state
->port
;
2023 struct device
*tty_dev
;
2024 struct uart_match match
= {uport
, drv
};
2026 mutex_lock(&port
->mutex
);
2028 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2029 if (device_may_wakeup(tty_dev
)) {
2030 if (!enable_irq_wake(uport
->irq
))
2031 uport
->irq_wake
= 1;
2032 put_device(tty_dev
);
2033 mutex_unlock(&port
->mutex
);
2036 put_device(tty_dev
);
2038 /* Nothing to do if the console is not suspending */
2039 if (!console_suspend_enabled
&& uart_console(uport
))
2042 uport
->suspended
= 1;
2044 if (port
->flags
& ASYNC_INITIALIZED
) {
2045 const struct uart_ops
*ops
= uport
->ops
;
2048 set_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2049 clear_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2051 spin_lock_irq(&uport
->lock
);
2052 ops
->stop_tx(uport
);
2053 ops
->set_mctrl(uport
, 0);
2054 ops
->stop_rx(uport
);
2055 spin_unlock_irq(&uport
->lock
);
2058 * Wait for the transmitter to empty.
2060 for (tries
= 3; !ops
->tx_empty(uport
) && tries
; tries
--)
2063 dev_err(uport
->dev
, "%s%d: Unable to drain transmitter\n",
2065 drv
->tty_driver
->name_base
+ uport
->line
);
2067 ops
->shutdown(uport
);
2071 * Disable the console device before suspending.
2073 if (uart_console(uport
))
2074 console_stop(uport
->cons
);
2076 uart_change_pm(state
, UART_PM_STATE_OFF
);
2078 mutex_unlock(&port
->mutex
);
2083 int uart_resume_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2085 struct uart_state
*state
= drv
->state
+ uport
->line
;
2086 struct tty_port
*port
= &state
->port
;
2087 struct device
*tty_dev
;
2088 struct uart_match match
= {uport
, drv
};
2089 struct ktermios termios
;
2091 mutex_lock(&port
->mutex
);
2093 tty_dev
= device_find_child(uport
->dev
, &match
, serial_match_port
);
2094 if (!uport
->suspended
&& device_may_wakeup(tty_dev
)) {
2095 if (uport
->irq_wake
) {
2096 disable_irq_wake(uport
->irq
);
2097 uport
->irq_wake
= 0;
2099 put_device(tty_dev
);
2100 mutex_unlock(&port
->mutex
);
2103 put_device(tty_dev
);
2104 uport
->suspended
= 0;
2107 * Re-enable the console device after suspending.
2109 if (uart_console(uport
)) {
2111 * First try to use the console cflag setting.
2113 memset(&termios
, 0, sizeof(struct ktermios
));
2114 termios
.c_cflag
= uport
->cons
->cflag
;
2117 * If that's unset, use the tty termios setting.
2119 if (port
->tty
&& termios
.c_cflag
== 0)
2120 termios
= port
->tty
->termios
;
2122 if (console_suspend_enabled
)
2123 uart_change_pm(state
, UART_PM_STATE_ON
);
2124 uport
->ops
->set_termios(uport
, &termios
, NULL
);
2125 if (console_suspend_enabled
)
2126 console_start(uport
->cons
);
2129 if (port
->flags
& ASYNC_SUSPENDED
) {
2130 const struct uart_ops
*ops
= uport
->ops
;
2133 uart_change_pm(state
, UART_PM_STATE_ON
);
2134 spin_lock_irq(&uport
->lock
);
2135 ops
->set_mctrl(uport
, 0);
2136 spin_unlock_irq(&uport
->lock
);
2137 if (console_suspend_enabled
|| !uart_console(uport
)) {
2138 /* Protected by port mutex for now */
2139 struct tty_struct
*tty
= port
->tty
;
2140 ret
= ops
->startup(uport
);
2143 uart_change_speed(tty
, state
, NULL
);
2144 spin_lock_irq(&uport
->lock
);
2145 ops
->set_mctrl(uport
, uport
->mctrl
);
2146 ops
->start_tx(uport
);
2147 spin_unlock_irq(&uport
->lock
);
2148 set_bit(ASYNCB_INITIALIZED
, &port
->flags
);
2151 * Failed to resume - maybe hardware went away?
2152 * Clear the "initialized" flag so we won't try
2153 * to call the low level drivers shutdown method.
2155 uart_shutdown(tty
, state
);
2159 clear_bit(ASYNCB_SUSPENDED
, &port
->flags
);
2162 mutex_unlock(&port
->mutex
);
2168 uart_report_port(struct uart_driver
*drv
, struct uart_port
*port
)
2172 switch (port
->iotype
) {
2174 snprintf(address
, sizeof(address
), "I/O 0x%lx", port
->iobase
);
2177 snprintf(address
, sizeof(address
),
2178 "I/O 0x%lx offset 0x%x", port
->iobase
, port
->hub6
);
2185 snprintf(address
, sizeof(address
),
2186 "MMIO 0x%llx", (unsigned long long)port
->mapbase
);
2189 strlcpy(address
, "*unknown*", sizeof(address
));
2193 printk(KERN_INFO
"%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2194 port
->dev
? dev_name(port
->dev
) : "",
2195 port
->dev
? ": " : "",
2197 drv
->tty_driver
->name_base
+ port
->line
,
2198 address
, port
->irq
, port
->uartclk
/ 16, uart_type(port
));
2202 uart_configure_port(struct uart_driver
*drv
, struct uart_state
*state
,
2203 struct uart_port
*port
)
2208 * If there isn't a port here, don't do anything further.
2210 if (!port
->iobase
&& !port
->mapbase
&& !port
->membase
)
2214 * Now do the auto configuration stuff. Note that config_port
2215 * is expected to claim the resources and map the port for us.
2218 if (port
->flags
& UPF_AUTO_IRQ
)
2219 flags
|= UART_CONFIG_IRQ
;
2220 if (port
->flags
& UPF_BOOT_AUTOCONF
) {
2221 if (!(port
->flags
& UPF_FIXED_TYPE
)) {
2222 port
->type
= PORT_UNKNOWN
;
2223 flags
|= UART_CONFIG_TYPE
;
2225 port
->ops
->config_port(port
, flags
);
2228 if (port
->type
!= PORT_UNKNOWN
) {
2229 unsigned long flags
;
2231 uart_report_port(drv
, port
);
2233 /* Power up port for set_mctrl() */
2234 uart_change_pm(state
, UART_PM_STATE_ON
);
2237 * Ensure that the modem control lines are de-activated.
2238 * keep the DTR setting that is set in uart_set_options()
2239 * We probably don't need a spinlock around this, but
2241 spin_lock_irqsave(&port
->lock
, flags
);
2242 port
->ops
->set_mctrl(port
, port
->mctrl
& TIOCM_DTR
);
2243 spin_unlock_irqrestore(&port
->lock
, flags
);
2246 * If this driver supports console, and it hasn't been
2247 * successfully registered yet, try to re-register it.
2248 * It may be that the port was not available.
2250 if (port
->cons
&& !(port
->cons
->flags
& CON_ENABLED
))
2251 register_console(port
->cons
);
2254 * Power down all ports by default, except the
2255 * console if we have one.
2257 if (!uart_console(port
))
2258 uart_change_pm(state
, UART_PM_STATE_OFF
);
2262 #ifdef CONFIG_CONSOLE_POLL
2264 static int uart_poll_init(struct tty_driver
*driver
, int line
, char *options
)
2266 struct uart_driver
*drv
= driver
->driver_state
;
2267 struct uart_state
*state
= drv
->state
+ line
;
2268 struct uart_port
*port
;
2275 if (!state
|| !state
->uart_port
)
2278 port
= state
->uart_port
;
2279 if (!(port
->ops
->poll_get_char
&& port
->ops
->poll_put_char
))
2282 if (port
->ops
->poll_init
) {
2283 struct tty_port
*tport
= &state
->port
;
2286 mutex_lock(&tport
->mutex
);
2288 * We don't set ASYNCB_INITIALIZED as we only initialized the
2289 * hw, e.g. state->xmit is still uninitialized.
2291 if (!test_bit(ASYNCB_INITIALIZED
, &tport
->flags
))
2292 ret
= port
->ops
->poll_init(port
);
2293 mutex_unlock(&tport
->mutex
);
2299 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2300 return uart_set_options(port
, NULL
, baud
, parity
, bits
, flow
);
2306 static int uart_poll_get_char(struct tty_driver
*driver
, int line
)
2308 struct uart_driver
*drv
= driver
->driver_state
;
2309 struct uart_state
*state
= drv
->state
+ line
;
2310 struct uart_port
*port
;
2312 if (!state
|| !state
->uart_port
)
2315 port
= state
->uart_port
;
2316 return port
->ops
->poll_get_char(port
);
2319 static void uart_poll_put_char(struct tty_driver
*driver
, int line
, char ch
)
2321 struct uart_driver
*drv
= driver
->driver_state
;
2322 struct uart_state
*state
= drv
->state
+ line
;
2323 struct uart_port
*port
;
2325 if (!state
|| !state
->uart_port
)
2328 port
= state
->uart_port
;
2331 port
->ops
->poll_put_char(port
, '\r');
2332 port
->ops
->poll_put_char(port
, ch
);
2336 static const struct tty_operations uart_ops
= {
2338 .close
= uart_close
,
2339 .write
= uart_write
,
2340 .put_char
= uart_put_char
,
2341 .flush_chars
= uart_flush_chars
,
2342 .write_room
= uart_write_room
,
2343 .chars_in_buffer
= uart_chars_in_buffer
,
2344 .flush_buffer
= uart_flush_buffer
,
2345 .ioctl
= uart_ioctl
,
2346 .throttle
= uart_throttle
,
2347 .unthrottle
= uart_unthrottle
,
2348 .send_xchar
= uart_send_xchar
,
2349 .set_termios
= uart_set_termios
,
2350 .set_ldisc
= uart_set_ldisc
,
2352 .start
= uart_start
,
2353 .hangup
= uart_hangup
,
2354 .break_ctl
= uart_break_ctl
,
2355 .wait_until_sent
= uart_wait_until_sent
,
2356 #ifdef CONFIG_PROC_FS
2357 .proc_fops
= &uart_proc_fops
,
2359 .tiocmget
= uart_tiocmget
,
2360 .tiocmset
= uart_tiocmset
,
2361 .get_icount
= uart_get_icount
,
2362 #ifdef CONFIG_CONSOLE_POLL
2363 .poll_init
= uart_poll_init
,
2364 .poll_get_char
= uart_poll_get_char
,
2365 .poll_put_char
= uart_poll_put_char
,
2369 static const struct tty_port_operations uart_port_ops
= {
2370 .activate
= uart_port_activate
,
2371 .shutdown
= uart_port_shutdown
,
2372 .carrier_raised
= uart_carrier_raised
,
2373 .dtr_rts
= uart_dtr_rts
,
2377 * uart_register_driver - register a driver with the uart core layer
2378 * @drv: low level driver structure
2380 * Register a uart driver with the core driver. We in turn register
2381 * with the tty layer, and initialise the core driver per-port state.
2383 * We have a proc file in /proc/tty/driver which is named after the
2386 * drv->port should be NULL, and the per-port structures should be
2387 * registered using uart_add_one_port after this call has succeeded.
2389 int uart_register_driver(struct uart_driver
*drv
)
2391 struct tty_driver
*normal
;
2397 * Maybe we should be using a slab cache for this, especially if
2398 * we have a large number of ports to handle.
2400 drv
->state
= kzalloc(sizeof(struct uart_state
) * drv
->nr
, GFP_KERNEL
);
2404 normal
= alloc_tty_driver(drv
->nr
);
2408 drv
->tty_driver
= normal
;
2410 normal
->driver_name
= drv
->driver_name
;
2411 normal
->name
= drv
->dev_name
;
2412 normal
->major
= drv
->major
;
2413 normal
->minor_start
= drv
->minor
;
2414 normal
->type
= TTY_DRIVER_TYPE_SERIAL
;
2415 normal
->subtype
= SERIAL_TYPE_NORMAL
;
2416 normal
->init_termios
= tty_std_termios
;
2417 normal
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2418 normal
->init_termios
.c_ispeed
= normal
->init_termios
.c_ospeed
= 9600;
2419 normal
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
2420 normal
->driver_state
= drv
;
2421 tty_set_operations(normal
, &uart_ops
);
2424 * Initialise the UART state(s).
2426 for (i
= 0; i
< drv
->nr
; i
++) {
2427 struct uart_state
*state
= drv
->state
+ i
;
2428 struct tty_port
*port
= &state
->port
;
2430 tty_port_init(port
);
2431 port
->ops
= &uart_port_ops
;
2434 retval
= tty_register_driver(normal
);
2438 for (i
= 0; i
< drv
->nr
; i
++)
2439 tty_port_destroy(&drv
->state
[i
].port
);
2440 put_tty_driver(normal
);
2448 * uart_unregister_driver - remove a driver from the uart core layer
2449 * @drv: low level driver structure
2451 * Remove all references to a driver from the core driver. The low
2452 * level driver must have removed all its ports via the
2453 * uart_remove_one_port() if it registered them with uart_add_one_port().
2454 * (ie, drv->port == NULL)
2456 void uart_unregister_driver(struct uart_driver
*drv
)
2458 struct tty_driver
*p
= drv
->tty_driver
;
2461 tty_unregister_driver(p
);
2463 for (i
= 0; i
< drv
->nr
; i
++)
2464 tty_port_destroy(&drv
->state
[i
].port
);
2467 drv
->tty_driver
= NULL
;
2470 struct tty_driver
*uart_console_device(struct console
*co
, int *index
)
2472 struct uart_driver
*p
= co
->data
;
2474 return p
->tty_driver
;
2477 static ssize_t
uart_get_attr_uartclk(struct device
*dev
,
2478 struct device_attribute
*attr
, char *buf
)
2480 struct serial_struct tmp
;
2481 struct tty_port
*port
= dev_get_drvdata(dev
);
2483 uart_get_info(port
, &tmp
);
2484 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.baud_base
* 16);
2487 static ssize_t
uart_get_attr_type(struct device
*dev
,
2488 struct device_attribute
*attr
, char *buf
)
2490 struct serial_struct tmp
;
2491 struct tty_port
*port
= dev_get_drvdata(dev
);
2493 uart_get_info(port
, &tmp
);
2494 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.type
);
2496 static ssize_t
uart_get_attr_line(struct device
*dev
,
2497 struct device_attribute
*attr
, char *buf
)
2499 struct serial_struct tmp
;
2500 struct tty_port
*port
= dev_get_drvdata(dev
);
2502 uart_get_info(port
, &tmp
);
2503 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.line
);
2506 static ssize_t
uart_get_attr_port(struct device
*dev
,
2507 struct device_attribute
*attr
, char *buf
)
2509 struct serial_struct tmp
;
2510 struct tty_port
*port
= dev_get_drvdata(dev
);
2511 unsigned long ioaddr
;
2513 uart_get_info(port
, &tmp
);
2515 if (HIGH_BITS_OFFSET
)
2516 ioaddr
|= (unsigned long)tmp
.port_high
<< HIGH_BITS_OFFSET
;
2517 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", ioaddr
);
2520 static ssize_t
uart_get_attr_irq(struct device
*dev
,
2521 struct device_attribute
*attr
, char *buf
)
2523 struct serial_struct tmp
;
2524 struct tty_port
*port
= dev_get_drvdata(dev
);
2526 uart_get_info(port
, &tmp
);
2527 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.irq
);
2530 static ssize_t
uart_get_attr_flags(struct device
*dev
,
2531 struct device_attribute
*attr
, char *buf
)
2533 struct serial_struct tmp
;
2534 struct tty_port
*port
= dev_get_drvdata(dev
);
2536 uart_get_info(port
, &tmp
);
2537 return snprintf(buf
, PAGE_SIZE
, "0x%X\n", tmp
.flags
);
2540 static ssize_t
uart_get_attr_xmit_fifo_size(struct device
*dev
,
2541 struct device_attribute
*attr
, char *buf
)
2543 struct serial_struct tmp
;
2544 struct tty_port
*port
= dev_get_drvdata(dev
);
2546 uart_get_info(port
, &tmp
);
2547 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.xmit_fifo_size
);
2551 static ssize_t
uart_get_attr_close_delay(struct device
*dev
,
2552 struct device_attribute
*attr
, char *buf
)
2554 struct serial_struct tmp
;
2555 struct tty_port
*port
= dev_get_drvdata(dev
);
2557 uart_get_info(port
, &tmp
);
2558 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.close_delay
);
2562 static ssize_t
uart_get_attr_closing_wait(struct device
*dev
,
2563 struct device_attribute
*attr
, char *buf
)
2565 struct serial_struct tmp
;
2566 struct tty_port
*port
= dev_get_drvdata(dev
);
2568 uart_get_info(port
, &tmp
);
2569 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.closing_wait
);
2572 static ssize_t
uart_get_attr_custom_divisor(struct device
*dev
,
2573 struct device_attribute
*attr
, char *buf
)
2575 struct serial_struct tmp
;
2576 struct tty_port
*port
= dev_get_drvdata(dev
);
2578 uart_get_info(port
, &tmp
);
2579 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.custom_divisor
);
2582 static ssize_t
uart_get_attr_io_type(struct device
*dev
,
2583 struct device_attribute
*attr
, char *buf
)
2585 struct serial_struct tmp
;
2586 struct tty_port
*port
= dev_get_drvdata(dev
);
2588 uart_get_info(port
, &tmp
);
2589 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.io_type
);
2592 static ssize_t
uart_get_attr_iomem_base(struct device
*dev
,
2593 struct device_attribute
*attr
, char *buf
)
2595 struct serial_struct tmp
;
2596 struct tty_port
*port
= dev_get_drvdata(dev
);
2598 uart_get_info(port
, &tmp
);
2599 return snprintf(buf
, PAGE_SIZE
, "0x%lX\n", (unsigned long)tmp
.iomem_base
);
2602 static ssize_t
uart_get_attr_iomem_reg_shift(struct device
*dev
,
2603 struct device_attribute
*attr
, char *buf
)
2605 struct serial_struct tmp
;
2606 struct tty_port
*port
= dev_get_drvdata(dev
);
2608 uart_get_info(port
, &tmp
);
2609 return snprintf(buf
, PAGE_SIZE
, "%d\n", tmp
.iomem_reg_shift
);
2612 static DEVICE_ATTR(type
, S_IRUSR
| S_IRGRP
, uart_get_attr_type
, NULL
);
2613 static DEVICE_ATTR(line
, S_IRUSR
| S_IRGRP
, uart_get_attr_line
, NULL
);
2614 static DEVICE_ATTR(port
, S_IRUSR
| S_IRGRP
, uart_get_attr_port
, NULL
);
2615 static DEVICE_ATTR(irq
, S_IRUSR
| S_IRGRP
, uart_get_attr_irq
, NULL
);
2616 static DEVICE_ATTR(flags
, S_IRUSR
| S_IRGRP
, uart_get_attr_flags
, NULL
);
2617 static DEVICE_ATTR(xmit_fifo_size
, S_IRUSR
| S_IRGRP
, uart_get_attr_xmit_fifo_size
, NULL
);
2618 static DEVICE_ATTR(uartclk
, S_IRUSR
| S_IRGRP
, uart_get_attr_uartclk
, NULL
);
2619 static DEVICE_ATTR(close_delay
, S_IRUSR
| S_IRGRP
, uart_get_attr_close_delay
, NULL
);
2620 static DEVICE_ATTR(closing_wait
, S_IRUSR
| S_IRGRP
, uart_get_attr_closing_wait
, NULL
);
2621 static DEVICE_ATTR(custom_divisor
, S_IRUSR
| S_IRGRP
, uart_get_attr_custom_divisor
, NULL
);
2622 static DEVICE_ATTR(io_type
, S_IRUSR
| S_IRGRP
, uart_get_attr_io_type
, NULL
);
2623 static DEVICE_ATTR(iomem_base
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_base
, NULL
);
2624 static DEVICE_ATTR(iomem_reg_shift
, S_IRUSR
| S_IRGRP
, uart_get_attr_iomem_reg_shift
, NULL
);
2626 static struct attribute
*tty_dev_attrs
[] = {
2627 &dev_attr_type
.attr
,
2628 &dev_attr_line
.attr
,
2629 &dev_attr_port
.attr
,
2631 &dev_attr_flags
.attr
,
2632 &dev_attr_xmit_fifo_size
.attr
,
2633 &dev_attr_uartclk
.attr
,
2634 &dev_attr_close_delay
.attr
,
2635 &dev_attr_closing_wait
.attr
,
2636 &dev_attr_custom_divisor
.attr
,
2637 &dev_attr_io_type
.attr
,
2638 &dev_attr_iomem_base
.attr
,
2639 &dev_attr_iomem_reg_shift
.attr
,
2643 static const struct attribute_group tty_dev_attr_group
= {
2644 .attrs
= tty_dev_attrs
,
2648 * uart_add_one_port - attach a driver-defined port structure
2649 * @drv: pointer to the uart low level driver structure for this port
2650 * @uport: uart port structure to use for this port.
2652 * This allows the driver to register its own uart_port structure
2653 * with the core driver. The main purpose is to allow the low
2654 * level uart drivers to expand uart_port, rather than having yet
2655 * more levels of structures.
2657 int uart_add_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2659 struct uart_state
*state
;
2660 struct tty_port
*port
;
2662 struct device
*tty_dev
;
2665 BUG_ON(in_interrupt());
2667 if (uport
->line
>= drv
->nr
)
2670 state
= drv
->state
+ uport
->line
;
2671 port
= &state
->port
;
2673 mutex_lock(&port_mutex
);
2674 mutex_lock(&port
->mutex
);
2675 if (state
->uart_port
) {
2680 /* Link the port to the driver state table and vice versa */
2681 state
->uart_port
= uport
;
2682 uport
->state
= state
;
2684 state
->pm_state
= UART_PM_STATE_UNDEFINED
;
2685 uport
->cons
= drv
->cons
;
2686 uport
->minor
= drv
->tty_driver
->minor_start
+ uport
->line
;
2689 * If this port is a console, then the spinlock is already
2692 if (!(uart_console(uport
) && (uport
->cons
->flags
& CON_ENABLED
))) {
2693 spin_lock_init(&uport
->lock
);
2694 lockdep_set_class(&uport
->lock
, &port_lock_key
);
2696 if (uport
->cons
&& uport
->dev
)
2697 of_console_check(uport
->dev
->of_node
, uport
->cons
->name
, uport
->line
);
2699 uart_configure_port(drv
, state
, uport
);
2702 if (uport
->attr_group
)
2705 uport
->tty_groups
= kcalloc(num_groups
, sizeof(*uport
->tty_groups
),
2707 if (!uport
->tty_groups
) {
2711 uport
->tty_groups
[0] = &tty_dev_attr_group
;
2712 if (uport
->attr_group
)
2713 uport
->tty_groups
[1] = uport
->attr_group
;
2716 * Register the port whether it's detected or not. This allows
2717 * setserial to be used to alter this port's parameters.
2719 tty_dev
= tty_port_register_device_attr(port
, drv
->tty_driver
,
2720 uport
->line
, uport
->dev
, port
, uport
->tty_groups
);
2721 if (likely(!IS_ERR(tty_dev
))) {
2722 device_set_wakeup_capable(tty_dev
, 1);
2724 dev_err(uport
->dev
, "Cannot register tty device on line %d\n",
2729 * Ensure UPF_DEAD is not set.
2731 uport
->flags
&= ~UPF_DEAD
;
2734 mutex_unlock(&port
->mutex
);
2735 mutex_unlock(&port_mutex
);
2741 * uart_remove_one_port - detach a driver defined port structure
2742 * @drv: pointer to the uart low level driver structure for this port
2743 * @uport: uart port structure for this port
2745 * This unhooks (and hangs up) the specified port structure from the
2746 * core driver. No further calls will be made to the low-level code
2749 int uart_remove_one_port(struct uart_driver
*drv
, struct uart_port
*uport
)
2751 struct uart_state
*state
= drv
->state
+ uport
->line
;
2752 struct tty_port
*port
= &state
->port
;
2753 struct tty_struct
*tty
;
2756 BUG_ON(in_interrupt());
2758 if (state
->uart_port
!= uport
)
2759 dev_alert(uport
->dev
, "Removing wrong port: %p != %p\n",
2760 state
->uart_port
, uport
);
2762 mutex_lock(&port_mutex
);
2765 * Mark the port "dead" - this prevents any opens from
2766 * succeeding while we shut down the port.
2768 mutex_lock(&port
->mutex
);
2769 if (!state
->uart_port
) {
2770 mutex_unlock(&port
->mutex
);
2774 uport
->flags
|= UPF_DEAD
;
2775 mutex_unlock(&port
->mutex
);
2778 * Remove the devices from the tty layer
2780 tty_unregister_device(drv
->tty_driver
, uport
->line
);
2782 tty
= tty_port_tty_get(port
);
2784 tty_vhangup(port
->tty
);
2789 * If the port is used as a console, unregister it
2791 if (uart_console(uport
))
2792 unregister_console(uport
->cons
);
2795 * Free the port IO and memory resources, if any.
2797 if (uport
->type
!= PORT_UNKNOWN
)
2798 uport
->ops
->release_port(uport
);
2799 kfree(uport
->tty_groups
);
2802 * Indicate that there isn't a port here anymore.
2804 uport
->type
= PORT_UNKNOWN
;
2806 state
->uart_port
= NULL
;
2808 mutex_unlock(&port_mutex
);
2814 * Are the two ports equivalent?
2816 int uart_match_port(struct uart_port
*port1
, struct uart_port
*port2
)
2818 if (port1
->iotype
!= port2
->iotype
)
2821 switch (port1
->iotype
) {
2823 return (port1
->iobase
== port2
->iobase
);
2825 return (port1
->iobase
== port2
->iobase
) &&
2826 (port1
->hub6
== port2
->hub6
);
2832 return (port1
->mapbase
== port2
->mapbase
);
2836 EXPORT_SYMBOL(uart_match_port
);
2839 * uart_handle_dcd_change - handle a change of carrier detect state
2840 * @uport: uart_port structure for the open port
2841 * @status: new carrier detect status, nonzero if active
2843 * Caller must hold uport->lock
2845 void uart_handle_dcd_change(struct uart_port
*uport
, unsigned int status
)
2847 struct tty_port
*port
= &uport
->state
->port
;
2848 struct tty_struct
*tty
= port
->tty
;
2849 struct tty_ldisc
*ld
;
2851 lockdep_assert_held_once(&uport
->lock
);
2854 ld
= tty_ldisc_ref(tty
);
2856 if (ld
->ops
->dcd_change
)
2857 ld
->ops
->dcd_change(tty
, status
);
2858 tty_ldisc_deref(ld
);
2862 uport
->icount
.dcd
++;
2864 if (uart_dcd_enabled(uport
)) {
2866 wake_up_interruptible(&port
->open_wait
);
2871 EXPORT_SYMBOL_GPL(uart_handle_dcd_change
);
2874 * uart_handle_cts_change - handle a change of clear-to-send state
2875 * @uport: uart_port structure for the open port
2876 * @status: new clear to send status, nonzero if active
2878 * Caller must hold uport->lock
2880 void uart_handle_cts_change(struct uart_port
*uport
, unsigned int status
)
2882 lockdep_assert_held_once(&uport
->lock
);
2884 uport
->icount
.cts
++;
2886 if (uart_softcts_mode(uport
)) {
2887 if (uport
->hw_stopped
) {
2889 uport
->hw_stopped
= 0;
2890 uport
->ops
->start_tx(uport
);
2891 uart_write_wakeup(uport
);
2895 uport
->hw_stopped
= 1;
2896 uport
->ops
->stop_tx(uport
);
2902 EXPORT_SYMBOL_GPL(uart_handle_cts_change
);
2905 * uart_insert_char - push a char to the uart layer
2907 * User is responsible to call tty_flip_buffer_push when they are done with
2910 * @port: corresponding port
2911 * @status: state of the serial port RX buffer (LSR for 8250)
2912 * @overrun: mask of overrun bits in @status
2913 * @ch: character to push
2914 * @flag: flag for the character (see TTY_NORMAL and friends)
2916 void uart_insert_char(struct uart_port
*port
, unsigned int status
,
2917 unsigned int overrun
, unsigned int ch
, unsigned int flag
)
2919 struct tty_port
*tport
= &port
->state
->port
;
2921 if ((status
& port
->ignore_status_mask
& ~overrun
) == 0)
2922 if (tty_insert_flip_char(tport
, ch
, flag
) == 0)
2923 ++port
->icount
.buf_overrun
;
2926 * Overrun is special. Since it's reported immediately,
2927 * it doesn't affect the current character.
2929 if (status
& ~port
->ignore_status_mask
& overrun
)
2930 if (tty_insert_flip_char(tport
, 0, TTY_OVERRUN
) == 0)
2931 ++port
->icount
.buf_overrun
;
2933 EXPORT_SYMBOL_GPL(uart_insert_char
);
2935 EXPORT_SYMBOL(uart_write_wakeup
);
2936 EXPORT_SYMBOL(uart_register_driver
);
2937 EXPORT_SYMBOL(uart_unregister_driver
);
2938 EXPORT_SYMBOL(uart_suspend_port
);
2939 EXPORT_SYMBOL(uart_resume_port
);
2940 EXPORT_SYMBOL(uart_add_one_port
);
2941 EXPORT_SYMBOL(uart_remove_one_port
);
2943 MODULE_DESCRIPTION("Serial driver core");
2944 MODULE_LICENSE("GPL");