23 #include <linux/module.h>
24 #include <linux/tty.h>
26 #include <linux/slab.h>
31 #include <linux/device.h>
32 #include <linux/serial.h>
33 #include <linux/serial_core.h>
38 #include <asm/uaccess.h>
51 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53 #ifdef CONFIG_SERIAL_CORE_CONSOLE
54 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
56 #define uart_console(port) (0)
61 static void uart_wait_until_sent(
struct tty_struct *tty,
int timeout);
88 port->
ops->stop_tx(port);
89 spin_unlock_irqrestore(&port->
lock, flags);
92 static void __uart_start(
struct tty_struct *tty)
99 port->
ops->start_tx(port);
102 static void uart_start(
struct tty_struct *tty)
110 spin_unlock_irqrestore(&port->
lock, flags);
114 uart_update_mctrl(
struct uart_port *port,
unsigned int set,
unsigned int clear)
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)
149 if (!state->
xmit.buf) {
155 state->
xmit.buf = (
unsigned char *) page;
159 retval = uport->
ops->startup(uport);
162 tty->termios.c_cflag = uport->
cons->cflag;
163 uport->
cons->cflag = 0;
168 uart_change_speed(tty, state,
NULL);
175 if (tty->termios.c_cflag &
CBAUD)
179 if (tty_port_cts_enabled(port)) {
180 spin_lock_irq(&uport->
lock);
183 spin_unlock_irq(&uport->
lock);
213 retval = uart_port_startup(tty, state, init_hw);
217 }
else if (retval > 0)
243 if (!tty || (tty->termios.c_cflag &
HUPCL))
246 uart_port_shutdown(port);
259 if (state->
xmit.buf) {
281 switch (cflag &
CSIZE) {
338 unsigned int try,
baud, altbaud = 38400;
351 for (
try = 0;
try < 2;
try++) {
369 if (baud >= min && baud <= max)
446 termios = &tty->termios;
461 uport->
ops->set_termios(uport, termios, old_termios);
464 static inline int __uart_put_char(
struct uart_port *port,
479 spin_unlock_irqrestore(&port->
lock, flags);
483 static int uart_put_char(
struct tty_struct *tty,
unsigned char ch)
490 static void uart_flush_chars(
struct tty_struct *tty)
496 const unsigned char *
buf,
int count)
532 spin_unlock_irqrestore(&port->
lock, flags);
538 static int uart_write_room(
struct tty_struct *tty)
546 spin_unlock_irqrestore(&state->
uart_port->lock, flags);
550 static int uart_chars_in_buffer(
struct tty_struct *tty)
558 spin_unlock_irqrestore(&state->
uart_port->lock, flags);
562 static void uart_flush_buffer(
struct tty_struct *tty)
582 if (port->
ops->flush_buffer)
583 port->
ops->flush_buffer(port);
584 spin_unlock_irqrestore(&port->
lock, flags);
592 static void uart_send_xchar(
struct tty_struct *tty,
char ch)
598 if (port->
ops->send_xchar)
599 port->
ops->send_xchar(port, ch);
604 port->
ops->start_tx(port);
605 spin_unlock_irqrestore(&port->
lock, flags);
610 static void uart_throttle(
struct tty_struct *tty)
617 if (tty->termios.c_cflag &
CRTSCTS)
621 static void uart_unthrottle(
struct tty_struct *tty)
633 if (tty->termios.c_cflag &
CRTSCTS)
637 static void uart_get_info(
struct tty_port *port,
643 memset(retinfo, 0,
sizeof(*retinfo));
650 retinfo->
irq = uport->
irq;
665 static int uart_get_info_user(
struct uart_state *state,
674 uart_get_info(port, state, &
tmp);
687 unsigned long new_port;
688 unsigned int change_irq, change_port, closing_wait;
689 unsigned int old_custom_divisor, close_delay;
690 upf_t old_flags, new_flags;
693 new_port = new_info->
port;
705 && new_info->
irq != uport->
irq;
713 && (new_port != uport->
iobase ||
720 old_flags = uport->
flags;
721 new_flags = new_info->
flags;
726 if (change_irq || change_port ||
743 if (uport->
ops->verify_port)
744 retval = uport->
ops->verify_port(uport, new_info);
753 if (change_port || change_irq) {
759 if (tty_port_users(port) > 1)
766 uart_shutdown(tty, state);
770 unsigned long old_iobase, old_mapbase;
771 unsigned int old_type, old_iotype, old_hub6, old_shift;
773 old_iobase = uport->
iobase;
775 old_type = uport->
type;
776 old_hub6 = uport->
hub6;
777 old_iotype = uport->
iotype;
784 uport->
ops->release_port(uport);
797 retval = uport->
ops->request_port(uport);
808 uport->
iobase = old_iobase;
809 uport->
type = old_type;
810 uport->
hub6 = old_hub6;
811 uport->
iotype = old_iotype;
814 retval = uport->
ops->request_port(uport);
832 uport->
irq = new_info->
irq;
843 port->
tty->low_latency =
861 "%s sets custom speed on %s. This "
862 "is deprecated.\n",
current->comm,
865 uart_change_speed(tty, state,
NULL);
868 retval = uart_startup(tty, state, 1);
891 retval = uart_set_info(tty, port, state, &new_serial);
904 static int uart_get_lsr_info(
struct tty_struct *tty,
910 result = uport->
ops->tx_empty(uport);
926 static int uart_tiocmget(
struct tty_struct *tty)
935 result = uport->
mctrl;
936 spin_lock_irq(&uport->
lock);
937 result |= uport->
ops->get_mctrl(uport);
938 spin_unlock_irq(&uport->
lock);
946 uart_tiocmset(
struct tty_struct *tty,
unsigned int set,
unsigned int clear)
955 uart_update_mctrl(uport,
set, clear);
962 static int uart_break_ctl(
struct tty_struct *tty,
int break_state)
971 uport->
ops->break_ctl(uport, break_state);
995 if (tty_port_users(port) == 1) {
996 uart_shutdown(tty, state);
1003 uport->
ops->release_port(uport);
1013 uport->
ops->config_port(uport, flags);
1015 ret = uart_startup(tty, state, 1);
1031 uart_wait_modem_status(
struct uart_state *state,
unsigned long arg)
1042 spin_lock_irq(&uport->
lock);
1048 uport->
ops->enable_ms(uport);
1049 spin_unlock_irq(&uport->
lock);
1053 spin_lock_irq(&uport->
lock);
1055 spin_unlock_irq(&uport->
lock);
1059 if (((arg &
TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1060 ((arg &
TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1061 ((arg &
TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1062 ((arg &
TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1070 if (signal_pending(
current)) {
1090 static int uart_get_icount(
struct tty_struct *tty,
1097 spin_lock_irq(&uport->
lock);
1099 spin_unlock_irq(&uport->
lock);
1101 icount->
cts = cnow.cts;
1102 icount->
dsr = cnow.dsr;
1103 icount->
rng = cnow.rng;
1104 icount->
dcd = cnow.dcd;
1105 icount->
rx = cnow.rx;
1106 icount->
tx = cnow.tx;
1107 icount->
frame = cnow.frame;
1108 icount->
overrun = cnow.overrun;
1109 icount->
parity = cnow.parity;
1110 icount->
brk = cnow.brk;
1134 ret = uart_get_info_user(state, uarg);
1138 ret = uart_set_info_user(tty, state, uarg);
1142 ret = uart_do_autoconfig(tty, state);
1164 ret = uart_wait_modem_status(state, arg);
1184 ret = uart_get_lsr_info(tty, state, uarg);
1189 if (uport->
ops->ioctl)
1190 ret = uport->
ops->ioctl(uport, cmd, arg);
1200 static void uart_set_ldisc(
struct tty_struct *tty)
1205 if (uport->
ops->set_ldisc)
1206 uport->
ops->set_ldisc(uport, tty->termios.c_line);
1209 static void uart_set_termios(
struct tty_struct *tty,
1213 unsigned long flags;
1214 unsigned int cflag = tty->termios.c_cflag;
1223 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1224 if ((cflag ^ old_termios->
c_cflag) == 0 &&
1225 tty->termios.c_ospeed == old_termios->
c_ospeed &&
1226 tty->termios.c_ispeed == old_termios->
c_ispeed &&
1231 uart_change_speed(tty, state, old_termios);
1237 else if (!(old_termios->
c_cflag & CBAUD) && (cflag & CBAUD)) {
1246 if ((old_termios->
c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1250 spin_unlock_irqrestore(&state->
uart_port->lock, flags);
1253 else if (!(old_termios->
c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1259 spin_unlock_irqrestore(&state->
uart_port->lock, flags);
1268 static void uart_close(
struct tty_struct *tty,
struct file *filp)
1273 unsigned long flags;
1279 port = &state->
port;
1291 unsigned long flags;
1293 uport->
ops->stop_rx(uport);
1294 spin_unlock_irqrestore(&uport->
lock, flags);
1300 uart_wait_until_sent(tty, uport->
timeout);
1304 uart_shutdown(tty, state);
1305 uart_flush_buffer(tty);
1314 spin_unlock_irqrestore(&port->
lock, flags);
1320 spin_unlock_irqrestore(&port->
lock, flags);
1321 uart_change_pm(state, 3);
1330 spin_unlock_irqrestore(&port->
lock, flags);
1341 unsigned long char_time, expire;
1355 char_time = char_time / 5;
1358 if (timeout && timeout < char_time)
1370 if (timeout == 0 || timeout > 2 * port->
timeout)
1375 pr_debug(
"uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1376 port->
line, jiffies, expire);
1383 while (!port->
ops->tx_empty(port)) {
1398 static void uart_hangup(
struct tty_struct *tty)
1402 unsigned long flags;
1408 uart_flush_buffer(tty);
1409 uart_shutdown(tty, state);
1413 spin_unlock_irqrestore(&port->
lock, flags);
1426 static void uart_port_shutdown(
struct tty_port *port)
1443 uport->
ops->shutdown(uport);
1451 static int uart_carrier_raised(
struct tty_port *port)
1456 spin_lock_irq(&uport->
lock);
1457 uport->
ops->enable_ms(uport);
1458 mctrl = uport->
ops->get_mctrl(uport);
1459 spin_unlock_irq(&uport->
lock);
1534 if (port->
count == 1)
1535 uart_change_pm(state, 0);
1540 retval = uart_startup(tty, state, 0);
1557 static const char *uart_type(
struct uart_port *port)
1561 if (port->
ops->type)
1562 str = port->
ops->type(port);
1570 #ifdef CONFIG_PROC_FS
1587 uport->
line, uart_type(uport),
1588 mmio ?
"mmio:0x" :
"port:",
1589 mmio ? (
unsigned long long)uport->
mapbase
1590 : (
unsigned long long)uport->
iobase,
1602 uart_change_pm(state, 0);
1603 spin_lock_irq(&uport->
lock);
1604 status = uport->
ops->get_mctrl(uport);
1605 spin_unlock_irq(&uport->
lock);
1607 uart_change_pm(state, pm_state);
1615 if (uport->
icount.parity)
1621 if (uport->
icount.overrun)
1625 #define INFOBIT(bit, str) \
1626 if (uport->mctrl & (bit)) \
1627 strncat(stat_buf, (str), sizeof(stat_buf) - \
1628 strlen(stat_buf) - 2)
1629 #define STATBIT(bit, str) \
1630 if (status & (bit)) \
1631 strncat(stat_buf, (str), sizeof(stat_buf) - \
1632 strlen(stat_buf) - 2)
1637 STATBIT(TIOCM_CTS,
"|CTS");
1639 STATBIT(TIOCM_DSR,
"|DSR");
1641 STATBIT(TIOCM_RNG,
"|RI");
1652 static int uart_proc_show(
struct seq_file *m,
void *
v)
1658 seq_printf(m,
"serinfo:1.0 driver%s%s revision:%s\n",
1660 for (i = 0; i < drv->
nr; i++)
1661 uart_line_info(m, drv, i);
1672 .open = uart_proc_open,
1679 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1693 for (i = 0; i <
count; i++, s++) {
1711 if (idx < 0 || idx >= nr || (ports[idx].
iobase == 0 &&
1713 for (idx = 0; idx <
nr; idx++)
1714 if (ports[idx].
iobase != 0 ||
1741 while (*s >=
'0' && *s <=
'9')
1757 static const struct baud_rates baud_rates[] = {
1783 int baud,
int parity,
int bits,
int flow)
1803 for (i = 0; baud_rates[
i].rate; i++)
1804 if (baud_rates[i].
rate <= baud)
1807 termios.
c_cflag |= baud_rates[
i].cflag;
1832 port->
ops->set_termios(port, &termios, &
dummy);
1853 static void uart_change_pm(
struct uart_state *state,
int pm_state)
1869 static int serial_match_port(
struct device *
dev,
void *
data)
1889 if (device_may_wakeup(tty_dev)) {
1890 if (!enable_irq_wake(uport->
irq))
1907 spin_lock_irq(&uport->
lock);
1911 spin_unlock_irq(&uport->
lock);
1917 for (tries = 3; !ops->
tx_empty(uport) && tries; tries--)
1922 uport->
dev ? dev_name(uport->
dev) :
"",
1923 uport->
dev ?
": " :
"",
1938 uart_change_pm(state, 3);
1956 if (!uport->
suspended && device_may_wakeup(tty_dev)) {
1958 disable_irq_wake(uport->
irq);
1980 termios = port->
tty->termios;
1983 uart_change_pm(state, 0);
1984 uport->
ops->set_termios(uport, &termios,
NULL);
1993 uart_change_pm(state, 0);
1994 spin_lock_irq(&uport->
lock);
1996 spin_unlock_irq(&uport->
lock);
2003 uart_change_speed(tty, state,
NULL);
2004 spin_lock_irq(&uport->
lock);
2007 spin_unlock_irq(&uport->
lock);
2015 uart_shutdown(tty, state);
2038 "I/O 0x%lx offset 0x%x", port->
iobase, port->
hub6);
2045 "MMIO 0x%llx", (
unsigned long long)port->
mapbase);
2048 strlcpy(address,
"*unknown*",
sizeof(address));
2053 port->
dev ? dev_name(port->
dev) :
"",
2054 port->
dev ?
": " :
"",
2057 address, port->
irq, uart_type(port));
2084 port->
ops->config_port(port, flags);
2088 unsigned long flags;
2090 uart_report_port(drv, port);
2093 uart_change_pm(state, 0);
2102 spin_unlock_irqrestore(&port->
lock, flags);
2117 uart_change_pm(state, 3);
2121 #ifdef CONFIG_CONSOLE_POLL
2138 if (!(port->
ops->poll_get_char && port->
ops->poll_put_char))
2141 if (port->
ops->poll_init) {
2151 ret = port->
ops->poll_init(port);
2165 static int uart_poll_get_char(
struct tty_driver *driver,
int line)
2175 return port->
ops->poll_get_char(port);
2178 static void uart_poll_put_char(
struct tty_driver *driver,
int line,
char ch)
2188 port->
ops->poll_put_char(port, ch);
2194 .close = uart_close,
2195 .write = uart_write,
2196 .put_char = uart_put_char,
2197 .flush_chars = uart_flush_chars,
2198 .write_room = uart_write_room,
2199 .chars_in_buffer= uart_chars_in_buffer,
2200 .flush_buffer = uart_flush_buffer,
2201 .ioctl = uart_ioctl,
2202 .throttle = uart_throttle,
2203 .unthrottle = uart_unthrottle,
2204 .send_xchar = uart_send_xchar,
2205 .set_termios = uart_set_termios,
2206 .set_ldisc = uart_set_ldisc,
2208 .start = uart_start,
2209 .hangup = uart_hangup,
2210 .break_ctl = uart_break_ctl,
2211 .wait_until_sent= uart_wait_until_sent,
2212 #ifdef CONFIG_PROC_FS
2213 .proc_fops = &uart_proc_fops,
2215 .tiocmget = uart_tiocmget,
2216 .tiocmset = uart_tiocmset,
2217 .get_icount = uart_get_icount,
2218 #ifdef CONFIG_CONSOLE_POLL
2219 .poll_init = uart_poll_init,
2220 .poll_get_char = uart_poll_get_char,
2221 .poll_put_char = uart_poll_put_char,
2226 .activate = uart_port_activate,
2227 .shutdown = uart_port_shutdown,
2228 .carrier_raised = uart_carrier_raised,
2229 .dtr_rts = uart_dtr_rts,
2260 normal = alloc_tty_driver(drv->
nr);
2282 for (i = 0; i < drv->
nr; i++) {
2287 port->
ops = &uart_port_ops;
2345 static struct attribute *tty_dev_attrs[] = {
2346 &dev_attr_uartclk.attr,
2351 .attrs = tty_dev_attrs,
2355 &tty_dev_attr_group,
2378 if (uport->
line >= drv->
nr)
2382 port = &state->
port;
2406 uart_configure_port(drv, state, uport);
2413 uport->
line, uport->
dev, port, tty_dev_attr_groups);
2414 if (
likely(!IS_ERR(tty_dev))) {
2475 uport->
ops->release_port(uport);
2527 if (ld && ld->
ops->dcd_change)
2531 #ifdef CONFIG_HARD_PPS
2543 if (ld && ld->
ops->dcd_change)
2544 ld->
ops->dcd_change(tty, status, &ts);
2562 if (tty_port_cts_enabled(port)) {
2566 uport->
ops->start_tx(uport);
2572 uport->
ops->stop_tx(uport);
2592 unsigned int overrun,
unsigned int ch,
unsigned int flag)
2597 if (tty_insert_flip_char(tty, ch, flag) == 0)
2598 ++port->
icount.buf_overrun;
2605 if (tty_insert_flip_char(tty, 0,
TTY_OVERRUN) == 0)
2606 ++port->
icount.buf_overrun;