11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Driver core for serial ports
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright 1999 ARM Limited
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it under the terms of the GNU General Public License as published by
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the Free Software Foundation; either version 2 of the License, or
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (at your option) any later version.
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful,
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * GNU General Public License for more details.
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * along with this program; if not, write to the Free Software
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty.h>
25027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby#include <linux/tty_flip.h>
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/console.h>
29d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan#include <linux/proc_fs.h>
30d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan#include <linux/seq_file.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/device.h>
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
33ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox#include <linux/serial_core.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/delay.h>
35f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven#include <linux/mutex.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/irq.h>
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This is used to lock changes in serial line configuration.
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
43f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Venstatic DEFINE_MUTEX(port_mutex);
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4513e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar/*
4613e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar * lockdep: port->lock is initialized in two places, but we
4713e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar *          want only one lock-class:
4813e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar */
4913e83599d282ddfd544600df9db5ab343ac4662fIngo Molnarstatic struct lock_class_key port_lock_key;
5013e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_SERIAL_CORE_CONSOLE
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define uart_console(port)	((port)->cons && (port)->cons->index == (port)->line)
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#else
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define uart_console(port)	(0)
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
59192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
60a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox					struct ktermios *old_termios);
611f33a51d9771b34be3cb6f7fb96a325e17bbac7bJiri Slabystatic void uart_wait_until_sent(struct tty_struct *tty, int timeout);
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_change_pm(struct uart_state *state, int pm_state);
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
64b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slabystatic void uart_port_shutdown(struct tty_port *port);
65b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is used by the interrupt handler to schedule processing in
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the software interrupt portion of the driver.
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid uart_write_wakeup(struct uart_port *port)
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
72ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_state *state = port->state;
73d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	/*
74d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * This means you called this function _after_ the port was
75d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * closed.  No cookie for you.
76d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 */
77ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	BUG_ON(!state);
786a3e492b6daaf7ec4dc41e51d87d2aae8ff886f2Jiri Slaby	tty_wakeup(state->port.tty);
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_stop(struct tty_struct *tty)
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
84ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
88b129a8ccd53f74c43e4c83c8e0031a4990040830Russell King	port->ops->stop_tx(port);
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __uart_start(struct tty_struct *tty)
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
95ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
975188eeeaa673e45a22e9433bb596fef1a16e56d6San Mehat	if (port->ops->wake_peer)
985188eeeaa673e45a22e9433bb596fef1a16e56d6San Mehat		port->ops->wake_peer(port);
995188eeeaa673e45a22e9433bb596fef1a16e56d6San Mehat
100ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    !tty->stopped && !tty->hw_stopped)
102b129a8ccd53f74c43e4c83c8e0031a4990040830Russell King		port->ops->start_tx(port);
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_start(struct tty_struct *tty)
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
108ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__uart_start(tty);
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int old;
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	old = port->mctrl;
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	port->mctrl = (old & ~clear) | set;
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (old != port->mctrl)
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->ops->set_mctrl(port, port->mctrl);
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
130a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
131a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Startup the port.  This will be called once per open.  All calls
135df4f4dd429870f435f8d5d9d561db029a29f063bAlan Cox * will be serialised by the per-port mutex.
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
137c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slabystatic int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
138c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		int init_hw)
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
14146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct tty_port *port = &state->port;
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long page;
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval = 0;
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->type == PORT_UNKNOWN)
146c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		return 1;
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Initialise and allocate the transmit and temporary
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * buffer.
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
152ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state->xmit.buf) {
153df4f4dd429870f435f8d5d9d561db029a29f063bAlan Cox		/* This is protected by the per port mutex */
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page = get_zeroed_page(GFP_KERNEL);
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!page)
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -ENOMEM;
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
158ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		state->xmit.buf = (unsigned char *) page;
159ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		uart_circ_clear(&state->xmit);
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	retval = uport->ops->startup(uport);
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval == 0) {
164c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		if (uart_console(uport) && uport->cons->cflag) {
165c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby			tty->termios->c_cflag = uport->cons->cflag;
166c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby			uport->cons->cflag = 0;
167c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		}
168c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		/*
169c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		 * Initialise the hardware port settings.
170c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		 */
171c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		uart_change_speed(tty, state, NULL);
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
173c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		if (init_hw) {
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * Setup the RTS and DTR signals once the
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * port is open and ready to respond.
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
178192251352f912bccfb942ea35801d2357f11f592Alan Cox			if (tty->termios->c_cflag & CBAUD)
17946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox				uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
182ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		if (port->flags & ASYNC_CTS_FLOW) {
18346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			spin_lock_irq(&uport->lock);
18446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
185192251352f912bccfb942ea35801d2357f11f592Alan Cox				tty->hw_stopped = 1;
18646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			spin_unlock_irq(&uport->lock);
1870dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King		}
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1900055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	/*
1910055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * This is to allow setserial on this port. People may want to set
1920055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * port/irq/type and then reconfigure the port properly if it failed
1930055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * now.
1940055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 */
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval && capable(CAP_SYS_ADMIN))
196c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		return 1;
197c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby
198c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	return retval;
199c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby}
200c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby
201c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slabystatic int uart_startup(struct tty_struct *tty, struct uart_state *state,
202c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		int init_hw)
203c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby{
204c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	struct tty_port *port = &state->port;
205c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	int retval;
206c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby
207c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	if (port->flags & ASYNC_INITIALIZED)
208c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		return 0;
209c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby
210c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	/*
211c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	 * Set the TTY IO error marker - we will only clear this
212c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	 * once we have successfully opened the port.
213c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	 */
214c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	set_bit(TTY_IO_ERROR, &tty->flags);
215c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby
216c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	retval = uart_port_startup(tty, state, init_hw);
217c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	if (!retval) {
218c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		set_bit(ASYNCB_INITIALIZED, &port->flags);
219c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby		clear_bit(TTY_IO_ERROR, &tty->flags);
220c0d92be6bc4fbbf6402fde2c0bc08fc4d2b58430Jiri Slaby	} else if (retval > 0)
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = 0;
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine will shutdown a serial port; interrupts are disabled, and
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * DTR is dropped if the hangup on close termio flag is on.  Calls to
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * uart_shutdown are serialised by the per-port semaphore.
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
231192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
233ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_port *uport = state->uart_port;
234bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	struct tty_port *port = &state->port;
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
237ee31b337852ca8a65840702544ff5c64d37740f5Russell King	 * Set the TTY IO error marker
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
239f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox	if (tty)
240f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox		set_bit(TTY_IO_ERROR, &tty->flags);
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
242bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
243ee31b337852ca8a65840702544ff5c64d37740f5Russell King		/*
244ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * Turn off DTR and RTS early.
245ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 */
246f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox		if (!tty || (tty->termios->c_cflag & HUPCL))
247ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
248ee31b337852ca8a65840702544ff5c64d37740f5Russell King
249b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby		uart_port_shutdown(port);
250ee31b337852ca8a65840702544ff5c64d37740f5Russell King	}
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
253d208a3bf77f902283894f546b6b5383202cf7882Doug Anderson	 * It's possible for shutdown to be called after suspend if we get
254d208a3bf77f902283894f546b6b5383202cf7882Doug Anderson	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
255d208a3bf77f902283894f546b6b5383202cf7882Doug Anderson	 * we don't try to resume a port that has been shutdown.
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
257d208a3bf77f902283894f546b6b5383202cf7882Doug Anderson	clear_bit(ASYNCB_SUSPENDED, &port->flags);
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Free the transmit buffer page.
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
262ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (state->xmit.buf) {
263ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		free_page((unsigned long)state->xmit.buf);
264ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		state->xmit.buf = NULL;
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_update_timeout - update per-port FIFO timeout.
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@port:  uart_port structure describing the port
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@cflag: termios cflag value
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud:  speed of the port
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Set the port FIFO timeout value.  The @cflag value should
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	reflect the actual hardware settings.
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_update_timeout(struct uart_port *port, unsigned int cflag,
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    unsigned int baud)
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int bits;
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* byte size and parity */
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cflag & CSIZE) {
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CS5:
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits = 7;
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CS6:
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits = 8;
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CS7:
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits = 9;
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits = 10;
296a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox		break; /* CS8 */
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cflag & CSTOPB)
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits++;
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cflag & PARENB)
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bits++;
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The total number of bits to be transmitted in the fifo.
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	bits = bits * port->fifosize;
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Figure the timeout to send the above number of bits.
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Add .02 seconds of slop
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	port->timeout = (HZ * bits) / baud + HZ/50;
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_update_timeout);
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_get_baud_rate - return baud rate for a particular port
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@port: uart_port structure describing the port in question.
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@termios: desired termios settings.
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@old: old termios (or NULL)
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@min: minimum acceptable baud rate
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@max: maximum acceptable baud rate
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Decode the termios structure into a numeric baud rate,
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	taking account of the magic 38400 baud rate (with spd_*
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	flags), and mapping the %B0 rate to 9600 baud.
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	If the new baud rate is invalid, try the old termios setting.
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	If it's still invalid, we try 9600 baud.
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Update the @termios structure to reflect the baud rate
334eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox *	we're actually going to be using. Don't do this for the case
335eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox *	where B0 is requested ("hang up").
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned int
338606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Coxuart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
339606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox		   struct ktermios *old, unsigned int min, unsigned int max)
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int try, baud, altbaud = 38400;
342eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox	int hung_up = 0;
3430077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King	upf_t flags = port->flags & UPF_SPD_MASK;
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (flags == UPF_SPD_HI)
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		altbaud = 57600;
34782cb7ba10deafe17686bf22ce4a7a303a77a197fAndré Goddard Rosa	else if (flags == UPF_SPD_VHI)
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		altbaud = 115200;
34982cb7ba10deafe17686bf22ce4a7a303a77a197fAndré Goddard Rosa	else if (flags == UPF_SPD_SHI)
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		altbaud = 230400;
35182cb7ba10deafe17686bf22ce4a7a303a77a197fAndré Goddard Rosa	else if (flags == UPF_SPD_WARP)
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		altbaud = 460800;
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (try = 0; try < 2; try++) {
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		baud = tty_termios_baud_rate(termios);
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Die! Die! Die!
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (baud == 38400)
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			baud = altbaud;
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Special case: B0 rate.
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
367eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox		if (baud == 0) {
368eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox			hung_up = 1;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			baud = 9600;
370eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox		}
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (baud >= min && baud <= max)
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return baud;
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Oops, the quotient was zero.  Try again with
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * the old baud rate if possible.
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios->c_cflag &= ~CBAUD;
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (old) {
3816d4d67beb963de8865499781b8523e5b683819c3Alan Cox			baud = tty_termios_baud_rate(old);
382eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox			if (!hung_up)
383eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox				tty_termios_encode_baud_rate(termios,
384eb424fd21c0931e998156225f2a0910167c3e16cAlan Cox								baud, baud);
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			old = NULL;
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
39016ae2a877bf4179737921235e85ceffd7b79354fAlan Cox		 * As a last resort, if the range cannot be met then clip to
39116ae2a877bf4179737921235e85ceffd7b79354fAlan Cox		 * the nearest chip supported rate.
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
39316ae2a877bf4179737921235e85ceffd7b79354fAlan Cox		if (!hung_up) {
39416ae2a877bf4179737921235e85ceffd7b79354fAlan Cox			if (baud <= min)
39516ae2a877bf4179737921235e85ceffd7b79354fAlan Cox				tty_termios_encode_baud_rate(termios,
39616ae2a877bf4179737921235e85ceffd7b79354fAlan Cox							min + 1, min + 1);
39716ae2a877bf4179737921235e85ceffd7b79354fAlan Cox			else
39816ae2a877bf4179737921235e85ceffd7b79354fAlan Cox				tty_termios_encode_baud_rate(termios,
39916ae2a877bf4179737921235e85ceffd7b79354fAlan Cox							max - 1, max - 1);
40016ae2a877bf4179737921235e85ceffd7b79354fAlan Cox		}
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
40216ae2a877bf4179737921235e85ceffd7b79354fAlan Cox	/* Should never happen */
40316ae2a877bf4179737921235e85ceffd7b79354fAlan Cox	WARN_ON(1);
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_get_baud_rate);
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_get_divisor - return uart clock divisor
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@port: uart_port structure describing the port.
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud: desired baud rate
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Calculate the uart clock divisor for the port.
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned int
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_get_divisor(struct uart_port *port, unsigned int baud)
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int quot;
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Old custom speed handling.
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		quot = port->custom_divisor;
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
42797d24634daff8b83dae21cfde68553ff4997e558Uwe Kleine-König		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return quot;
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_get_divisor);
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
43423d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Cox/* FIXME: Consistent locking policy */
435192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
436192251352f912bccfb942ea35801d2357f11f592Alan Cox					struct ktermios *old_termios)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
438ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct tty_port *port = &state->port;
439ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_port *uport = state->uart_port;
440606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	struct ktermios *termios;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If we have no tty, termios, or the port does not exist,
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * then we can't set the parameters for this port.
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
446ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	termios = tty->termios;
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set flags based on termios cflag
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (termios->c_cflag & CRTSCTS)
455ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		set_bit(ASYNCB_CTS_FLOW, &port->flags);
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
457ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		clear_bit(ASYNCB_CTS_FLOW, &port->flags);
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (termios->c_cflag & CLOCAL)
460ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		clear_bit(ASYNCB_CHECK_CD, &port->flags);
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
462ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		set_bit(ASYNCB_CHECK_CD, &port->flags);
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
464ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	uport->ops->set_termios(uport, termios, old_termios);
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
467192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic inline int __uart_put_char(struct uart_port *port,
468192251352f912bccfb942ea35801d2357f11f592Alan Cox				struct circ_buf *circ, unsigned char c)
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
47123d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Cox	int ret = 0;
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!circ->buf)
47423d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Cox		return 0;
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (uart_circ_chars_free(circ) != 0) {
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		circ->buf[circ->head] = c;
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
48023d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Cox		ret = 1;
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
48323d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Cox	return ret;
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
48623d22cea85ba9114a59a32ca8dfb1e2aef52a278Alan Coxstatic int uart_put_char(struct tty_struct *tty, unsigned char ch)
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
490ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	return __uart_put_char(state->uart_port, &state->xmit, ch);
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_flush_chars(struct tty_struct *tty)
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	uart_start(tty);
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
498192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic int uart_write(struct tty_struct *tty,
499192251352f912bccfb942ea35801d2357f11f592Alan Cox					const unsigned char *buf, int count)
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
502d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	struct uart_port *port;
503d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	struct circ_buf *circ;
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int c, ret = 0;
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
507d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	/*
508d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * This means you called this function _after_ the port was
509d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * closed.  No cookie for you.
510d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 */
511f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox	if (!state) {
512d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek		WARN_ON(1);
513d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek		return -EL3HLT;
514d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	}
515d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek
516ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
517ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	circ = &state->xmit;
518d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!circ->buf)
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (1) {
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (count < c)
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			c = count;
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (c <= 0)
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memcpy(circ->buf + circ->head, buf, c);
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf += c;
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count -= c;
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret += c;
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	uart_start(tty);
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int uart_write_room(struct tty_struct *tty)
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
544f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	unsigned long flags;
545f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	int ret;
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
547ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	spin_lock_irqsave(&state->uart_port->lock, flags);
548ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	ret = uart_circ_chars_free(&state->xmit);
549ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	spin_unlock_irqrestore(&state->uart_port->lock, flags);
550f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	return ret;
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int uart_chars_in_buffer(struct tty_struct *tty)
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
556f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	unsigned long flags;
557f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	int ret;
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
559ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	spin_lock_irqsave(&state->uart_port->lock, flags);
560ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	ret = uart_circ_chars_pending(&state->xmit);
561ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	spin_unlock_irqrestore(&state->uart_port->lock, flags);
562f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	return ret;
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_flush_buffer(struct tty_struct *tty)
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
56855d7b68996a5064f011d681bca412b6281d2f711Tetsuo Handa	struct uart_port *port;
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
571d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	/*
572d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * This means you called this function _after_ the port was
573d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * closed.  No cookie for you.
574d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 */
575f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox	if (!state) {
576d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek		WARN_ON(1);
577d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek		return;
578d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	}
579d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek
580ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
581eb3a1e1145ca8f12372c7c96aa0702d86a9002a9Jiri Slaby	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
584ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	uart_circ_clear(&state->xmit);
5856bb0e3a59a089e23eecc0af3b6f6012b2a9affbaHaavard Skinnemoen	if (port->ops->flush_buffer)
5866bb0e3a59a089e23eecc0af3b6f6012b2a9affbaHaavard Skinnemoen		port->ops->flush_buffer(port);
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_wakeup(tty);
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This function is used to send a high-priority XON/XOFF character to
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the device
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_send_xchar(struct tty_struct *tty, char ch)
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
598ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->ops->send_xchar)
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->ops->send_xchar(port, ch);
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else {
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->x_char = ch;
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ch) {
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spin_lock_irqsave(&port->lock, flags);
607b129a8ccd53f74c43e4c83c8e0031a4990040830Russell King			port->ops->start_tx(port);
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spin_unlock_irqrestore(&port->lock, flags);
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_throttle(struct tty_struct *tty)
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (I_IXOFF(tty))
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_send_xchar(tty, STOP_CHAR(tty));
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tty->termios->c_cflag & CRTSCTS)
621ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		uart_clear_mctrl(state->uart_port, TIOCM_RTS);
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_unthrottle(struct tty_struct *tty)
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
627ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (I_IXOFF(tty)) {
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (port->x_char)
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			port->x_char = 0;
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			uart_send_xchar(tty, START_CHAR(tty));
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tty->termios->c_cflag & CRTSCTS)
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_set_mctrl(port, TIOCM_RTS);
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int uart_get_info(struct uart_state *state,
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 struct serial_struct __user *retinfo)
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
643a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct uart_port *uport = state->uart_port;
644a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct serial_struct tmp;
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&tmp, 0, sizeof(tmp));
648f34d7a5b7010b82fe97da95496b9971435530062Alan Cox
649f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	/* Ensure the state we copy is consistent and no hardware changes
650f34d7a5b7010b82fe97da95496b9971435530062Alan Cox	   occur as we go */
651a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
652f34d7a5b7010b82fe97da95496b9971435530062Alan Cox
653a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.type	    = uport->type;
654a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.line	    = uport->line;
655a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.port	    = uport->iobase;
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (HIGH_BITS_OFFSET)
657a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
658a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.irq		    = uport->irq;
659a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.flags	    = uport->flags;
660a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.xmit_fifo_size  = uport->fifosize;
661a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.baud_base	    = uport->uartclk / 16;
6624cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby	tmp.close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
663016af53a6de6837e5be3da68901083ea85ebb4daAlan Cox	tmp.closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ASYNC_CLOSING_WAIT_NONE :
6654cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby				jiffies_to_msecs(port->closing_wait) / 10;
666a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.custom_divisor  = uport->custom_divisor;
667a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.hub6	    = uport->hub6;
668a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.io_type         = uport->iotype;
669a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.iomem_reg_shift = uport->regshift;
670a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tmp.iomem_base      = (void *)(unsigned long)uport->mapbase;
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
672a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
673f34d7a5b7010b82fe97da95496b9971435530062Alan Cox
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
679192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic int uart_set_info(struct tty_struct *tty, struct uart_state *state,
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 struct serial_struct __user *newinfo)
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct serial_struct new_serial;
68346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
68446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct tty_port *port = &state->port;
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long new_port;
6860077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King	unsigned int change_irq, change_port, closing_wait;
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int old_custom_divisor, close_delay;
6880077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King	upf_t old_flags, new_flags;
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval = 0;
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_port = new_serial.port;
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (HIGH_BITS_OFFSET)
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_serial.irq = irq_canonicalize(new_serial.irq);
6994cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby	close_delay = msecs_to_jiffies(new_serial.close_delay * 10);
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
7014cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby			ASYNC_CLOSING_WAIT_NONE :
7024cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby			msecs_to_jiffies(new_serial.closing_wait * 10);
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
70591312cdb4fcd832341e425f74f49938e0503c929Alan Cox	 * This semaphore protects port->count.  It is also
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * very useful to prevent opens.  Also, take the
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * port configuration semaphore to make sure that a
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * module insertion/removal doesn't change anything
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * under us.
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
711a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
71346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	change_irq  = !(uport->flags & UPF_FIXED_PORT)
71446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		&& new_serial.irq != uport->irq;
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Since changing the 'type' of the port changes its resource
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * allocations, we should treat type changes the same as
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * IO port changes.
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
72146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	change_port = !(uport->flags & UPF_FIXED_PORT)
72246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		&& (new_port != uport->iobase ||
72346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    (unsigned long)new_serial.iomem_base != uport->mapbase ||
72446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    new_serial.hub6 != uport->hub6 ||
72546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    new_serial.io_type != uport->iotype ||
72646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    new_serial.iomem_reg_shift != uport->regshift ||
72746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    new_serial.type != uport->type);
72846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox
72946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	old_flags = uport->flags;
7300077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King	new_flags = new_serial.flags;
73146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	old_custom_divisor = uport->custom_divisor;
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!capable(CAP_SYS_ADMIN)) {
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = -EPERM;
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (change_irq || change_port ||
73646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    (new_serial.baud_base != uport->uartclk / 16) ||
73746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    (close_delay != port->close_delay) ||
73846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    (closing_wait != port->closing_wait) ||
739947deee8904b3c2edc7f59ab6e6242499e4dc434Russell King		    (new_serial.xmit_fifo_size &&
74046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		     new_serial.xmit_fifo_size != uport->fifosize) ||
7410077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit;
74346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
7440077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King			       (new_flags & UPF_USR_MASK));
74546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->custom_divisor = new_serial.custom_divisor;
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto check_and_exit;
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Ask the low level driver to verify the settings.
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
75246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->ops->verify_port)
75346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		retval = uport->ops->verify_port(uport, &new_serial);
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
755a62c41337356989387d15020dc0f0288aaacfa44Yinghai Lu	if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (new_serial.baud_base < 9600))
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = -EINVAL;
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval)
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (change_port || change_irq) {
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = -EBUSY;
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Make sure that we are the sole user of this port.
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
768b58d13a0216d4e0753668214f23e1d2c24c30f8cAlan Cox		if (tty_port_users(port) > 1)
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit;
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * We need to shutdown the serial port at the old
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * port/type/irq combination.
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
775192251352f912bccfb942ea35801d2357f11f592Alan Cox		uart_shutdown(tty, state);
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (change_port) {
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long old_iobase, old_mapbase;
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned int old_type, old_iotype, old_hub6, old_shift;
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
78246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_iobase = uport->iobase;
78346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_mapbase = uport->mapbase;
78446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_type = uport->type;
78546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_hub6 = uport->hub6;
78646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_iotype = uport->iotype;
78746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		old_shift = uport->regshift;
7881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
7901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Free and release old regions
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
7921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (old_type != PORT_UNKNOWN)
79346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->ops->release_port(uport);
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
79546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->iobase = new_port;
79646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->type = new_serial.type;
79746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->hub6 = new_serial.hub6;
79846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->iotype = new_serial.io_type;
79946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->regshift = new_serial.iomem_reg_shift;
80046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->mapbase = (unsigned long)new_serial.iomem_base;
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
8031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Claim and map the new regions
8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
80546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (uport->type != PORT_UNKNOWN) {
80646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			retval = uport->ops->request_port(uport);
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* Always success - Jean II */
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			retval = 0;
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If we fail to request resources for the
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * new port, try to restore the old settings.
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (retval && old_type != PORT_UNKNOWN) {
81746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->iobase = old_iobase;
81846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->type = old_type;
81946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->hub6 = old_hub6;
82046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->iotype = old_iotype;
82146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->regshift = old_shift;
82246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->mapbase = old_mapbase;
82346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			retval = uport->ops->request_port(uport);
8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * If we failed to restore the old settings,
8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * we fail like this.
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (retval)
82946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox				uport->type = PORT_UNKNOWN;
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * We failed anyway.
8331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
8341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			retval = -EBUSY;
835a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			/* Added to return the correct error -Ram Gupta */
836a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			goto exit;
8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
8381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
840abb4a2390737867353ebafc012d45f2b03f3f944David Gibson	if (change_irq)
84146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->irq      = new_serial.irq;
84246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (!(uport->flags & UPF_FIXED_PORT))
84346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->uartclk  = new_serial.baud_base * 16;
84446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
8450077d45e46fe2af3aaee5813c99268afcd0e7c0eRussell King				 (new_flags & UPF_CHANGE_MASK);
84646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	uport->custom_divisor   = new_serial.custom_divisor;
84746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	port->close_delay     = close_delay;
84846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	port->closing_wait    = closing_wait;
849947deee8904b3c2edc7f59ab6e6242499e4dc434Russell King	if (new_serial.xmit_fifo_size)
85046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->fifosize = new_serial.xmit_fifo_size;
85146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (port->tty)
85246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		port->tty->low_latency =
85346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			(uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds check_and_exit:
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = 0;
85746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->type == PORT_UNKNOWN)
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
859ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_INITIALIZED) {
86046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
86146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		    old_custom_divisor != uport->custom_divisor) {
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * If they're setting up a custom divisor or speed,
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * instead of clearing it, then bitch about it. No
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * need to rate-limit; it's CAP_SYS_ADMIN only.
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
86746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			if (uport->flags & UPF_SPD_MASK) {
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				char buf[64];
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				printk(KERN_NOTICE
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				       "%s sets custom speed on %s. This "
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				       "is deprecated.\n", current->comm,
87246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox				       tty_name(port->tty, buf));
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
874192251352f912bccfb942ea35801d2357f11f592Alan Cox			uart_change_speed(tty, state, NULL);
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else
877192251352f912bccfb942ea35801d2357f11f592Alan Cox		retval = uart_startup(tty, state, 1);
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds exit:
879a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
883192251352f912bccfb942ea35801d2357f11f592Alan Cox/**
884192251352f912bccfb942ea35801d2357f11f592Alan Cox *	uart_get_lsr_info	-	get line status register info
885192251352f912bccfb942ea35801d2357f11f592Alan Cox *	@tty: tty associated with the UART
886192251352f912bccfb942ea35801d2357f11f592Alan Cox *	@state: UART being queried
887192251352f912bccfb942ea35801d2357f11f592Alan Cox *	@value: returned modem value
888192251352f912bccfb942ea35801d2357f11f592Alan Cox *
889192251352f912bccfb942ea35801d2357f11f592Alan Cox *	Note: uart_ioctl protects us against hangups.
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
891192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic int uart_get_lsr_info(struct tty_struct *tty,
892192251352f912bccfb942ea35801d2357f11f592Alan Cox			struct uart_state *state, unsigned int __user *value)
8931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
89446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int result;
8961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
89746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	result = uport->ops->tx_empty(uport);
8981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
9001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If we're about to load something into the transmit
9011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * register, we'll pretend the transmitter isn't empty to
9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * avoid a race condition (depending on when the transmit
9031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * interrupt happens).
9041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
90546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->x_char ||
906ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	    ((uart_circ_chars_pending(&state->xmit) > 0) &&
907192251352f912bccfb942ea35801d2357f11f592Alan Cox	     !tty->stopped && !tty->hw_stopped))
9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		result &= ~TIOCSER_TEMT;
909a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return put_user(result, value);
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
91360b33c133ca0b7c0b6072c87234b63fee6e80558Alan Coxstatic int uart_tiocmget(struct tty_struct *tty)
9141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
916a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
91746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result = -EIO;
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
920a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
92160b33c133ca0b7c0b6072c87234b63fee6e80558Alan Cox	if (!(tty->flags & (1 << TTY_IO_ERROR))) {
92246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		result = uport->mctrl;
92346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		spin_lock_irq(&uport->lock);
92446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		result |= uport->ops->get_mctrl(uport);
92546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		spin_unlock_irq(&uport->lock);
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
927a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result;
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
93320b9d17715017ae4dd4ec87fabc36d33b9de708eAlan Coxuart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
93646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
937a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = -EIO;
9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
940a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
94120b9d17715017ae4dd4ec87fabc36d33b9de708eAlan Cox	if (!(tty->flags & (1 << TTY_IO_ERROR))) {
94246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uart_update_mctrl(uport, set, clear);
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = 0;
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
945a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9499e98966c7bb94355689478bc84cc3e0c190f977eAlan Coxstatic int uart_break_ctl(struct tty_struct *tty, int break_state)
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
952a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
95346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
955a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
95746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->type != PORT_UNKNOWN)
95846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->ops->break_ctl(uport, break_state);
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
960a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
9619e98966c7bb94355689478bc84cc3e0c190f977eAlan Cox	return 0;
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
964192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
96646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
967a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int flags, ret;
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!capable(CAP_SYS_ADMIN))
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EPERM;
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Take the per-port semaphore.  This prevents count from
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * changing, and hence any extra opens of the port while
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * we're auto-configuring.
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
978a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (mutex_lock_interruptible(&port->mutex))
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ERESTARTSYS;
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = -EBUSY;
982b58d13a0216d4e0753668214f23e1d2c24c30f8cAlan Cox	if (tty_port_users(port) == 1) {
983192251352f912bccfb942ea35801d2357f11f592Alan Cox		uart_shutdown(tty, state);
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If we already have a port type configured,
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * we must release its resources.
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
98946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (uport->type != PORT_UNKNOWN)
99046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			uport->ops->release_port(uport);
9911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		flags = UART_CONFIG_TYPE;
99346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (uport->flags & UPF_AUTO_IRQ)
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			flags |= UART_CONFIG_IRQ;
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * This will claim the ports resources if
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * a port is found.
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
100046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->ops->config_port(uport, flags);
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1002192251352f912bccfb942ea35801d2357f11f592Alan Cox		ret = uart_startup(tty, state, 1);
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1004a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * - mask passed in arg for lines of interest
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
10121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Caller should use TIOCGICOUNT to see which one it was
1013bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox *
1014bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox * FIXME: This wants extracting into a common all driver implementation
1015bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox * of TIOCMWAIT using tty_port.
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_wait_modem_status(struct uart_state *state, unsigned long arg)
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
102046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
1021bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	struct tty_port *port = &state->port;
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DECLARE_WAITQUEUE(wait, current);
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_icount cprev, cnow;
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
10251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * note the counters on entry
10281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
102946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	spin_lock_irq(&uport->lock);
103046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Force modem status interrupts on
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
103546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	uport->ops->enable_ms(uport);
103646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	spin_unlock_irq(&uport->lock);
10371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1038bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	add_wait_queue(&port->delta_msr_wait, &wait);
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (;;) {
104046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		spin_lock_irq(&uport->lock);
104146d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
104246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		spin_unlock_irq(&uport->lock);
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		set_current_state(TASK_INTERRUPTIBLE);
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1050a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			ret = 0;
1051a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			break;
10521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		schedule();
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* see if a signal did it */
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (signal_pending(current)) {
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -ERESTARTSYS;
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cprev = cnow;
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	current->state = TASK_RUNNING;
1066bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	remove_wait_queue(&port->delta_msr_wait, &wait);
10671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
10731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Return: write counters to the user passed counter struct
10741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * NB: both 1->0 and 0->1 transitions are counted except for
10751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     RI where only 0->1 is counted.
10761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1077d281da7ff6f70efca0553c288bb883e8605b3862Alan Coxstatic int uart_get_icount(struct tty_struct *tty,
1078d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox			  struct serial_icounter_struct *icount)
10791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1080d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	struct uart_state *state = tty->driver_data;
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_icount cnow;
108246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
108446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	spin_lock_irq(&uport->lock);
108546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
108646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	spin_unlock_irq(&uport->lock);
10871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1088d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->cts         = cnow.cts;
1089d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->dsr         = cnow.dsr;
1090d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->rng         = cnow.rng;
1091d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->dcd         = cnow.dcd;
1092d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->rx          = cnow.rx;
1093d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->tx          = cnow.tx;
1094d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->frame       = cnow.frame;
1095d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->overrun     = cnow.overrun;
1096d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->parity      = cnow.parity;
1097d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->brk         = cnow.brk;
1098d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	icount->buf_overrun = cnow.buf_overrun;
1099d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox
1100d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	return 0;
11011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1104e52384426064bca0669a954736206adca7595d48Alan Cox * Called via sys_ioctl.  We can use spin_lock_irq() here.
11051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
11061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
11076caa76b7786891b42b66a0e61e2c2fff2c884620Alan Coxuart_ioctl(struct tty_struct *tty, unsigned int cmd,
11081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   unsigned long arg)
11091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
1111a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
11121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	void __user *uarg = (void __user *)arg;
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = -ENOIOCTLCMD;
11141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * These ioctls don't rely on the hardware to be present.
11181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
11201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCGSERIAL:
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = uart_get_info(state, uarg);
11221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCSSERIAL:
1125192251352f912bccfb942ea35801d2357f11f592Alan Cox		ret = uart_set_info(tty, state, uarg);
11261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCSERCONFIG:
1129192251352f912bccfb942ea35801d2357f11f592Alan Cox		ret = uart_do_autoconfig(tty, state);
11301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCSERGWILD: /* obsolete */
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCSERSWILD: /* obsolete */
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = 0;
11351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret != -ENOIOCTLCMD)
11391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
11401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tty->flags & (1 << TTY_IO_ERROR)) {
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EIO;
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
11441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The following should only be used when hardware is present.
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
11501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCMIWAIT:
11511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = uart_wait_modem_status(state, arg);
11521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret != -ENOIOCTLCMD)
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
11571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1158a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
11591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11606caa76b7786891b42b66a0e61e2c2fff2c884620Alan Cox	if (tty->flags & (1 << TTY_IO_ERROR)) {
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EIO;
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_up;
11631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * All these rely on hardware being present and need to be
11671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * protected against the tty being hung up.
11681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
11701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCSERGETLSR: /* Get line status register */
1171192251352f912bccfb942ea35801d2357f11f592Alan Cox		ret = uart_get_lsr_info(tty, state, uarg);
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default: {
117546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		struct uart_port *uport = state->uart_port;
117646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (uport->ops->ioctl)
117746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			ret = uport->ops->ioctl(uport, cmd, arg);
11781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1181f34d7a5b7010b82fe97da95496b9971435530062Alan Coxout_up:
1182a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
1183f34d7a5b7010b82fe97da95496b9971435530062Alan Coxout:
11841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
11851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1187edeb280e49d38a5330db25463ef45f5466b0058aLinus Torvaldsstatic void uart_set_ldisc(struct tty_struct *tty)
118864e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox{
118964e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox	struct uart_state *state = tty->driver_data;
119046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
119164e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox
119246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->ops->set_ldisc)
1193d87d9b7d19f04b16c4406d3c0feeca10090e0adaAlan Cox		uport->ops->set_ldisc(uport, tty->termios->c_line);
119464e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox}
119564e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox
1196a46c9994242978ab001299cc9c906b9a3eedadccAlan Coxstatic void uart_set_termios(struct tty_struct *tty,
1197a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox						struct ktermios *old_termios)
11981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
12001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
12011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int cflag = tty->termios->c_cflag;
12021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
12051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * These are the bits that are used to setup various
120620620d688ac6ff8ea01a873e46febf5a6a7909f1David Woodhouse	 * flags in the low level driver. We can ignore the Bfoo
120720620d688ac6ff8ea01a873e46febf5a6a7909f1David Woodhouse	 * bits in c_cflag; c_[io]speed will always be set
120820620d688ac6ff8ea01a873e46febf5a6a7909f1David Woodhouse	 * appropriately by set_termios() in tty_ioctl.c
12091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
12101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define RELEVANT_IFLAG(iflag)	((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
12111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((cflag ^ old_termios->c_cflag) == 0 &&
121220620d688ac6ff8ea01a873e46febf5a6a7909f1David Woodhouse	    tty->termios->c_ospeed == old_termios->c_ospeed &&
121320620d688ac6ff8ea01a873e46febf5a6a7909f1David Woodhouse	    tty->termios->c_ispeed == old_termios->c_ispeed &&
1214e52384426064bca0669a954736206adca7595d48Alan Cox	    RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
12151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
1216e52384426064bca0669a954736206adca7595d48Alan Cox	}
12171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1218192251352f912bccfb942ea35801d2357f11f592Alan Cox	uart_change_speed(tty, state, old_termios);
12191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Handle transition to B0 status */
12211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1222ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
12231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Handle transition away from B0 status */
122482cb7ba10deafe17686bf22ce4a7a303a77a197fAndré Goddard Rosa	else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
12251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned int mask = TIOCM_DTR;
12261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!(cflag & CRTSCTS) ||
12271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    !test_bit(TTY_THROTTLED, &tty->flags))
12281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			mask |= TIOCM_RTS;
1229ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		uart_set_mctrl(state->uart_port, mask);
12301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Handle turning off CRTSCTS */
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1234ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		spin_lock_irqsave(&state->uart_port->lock, flags);
12351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		tty->hw_stopped = 0;
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		__uart_start(tty);
1237ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		spin_unlock_irqrestore(&state->uart_port->lock, flags);
12381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12390dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King	/* Handle turning on CRTSCTS */
124082cb7ba10deafe17686bf22ce4a7a303a77a197fAndré Goddard Rosa	else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1241ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		spin_lock_irqsave(&state->uart_port->lock, flags);
1242ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
12430dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King			tty->hw_stopped = 1;
1244ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox			state->uart_port->ops->stop_tx(state->uart_port);
12450dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King		}
1246ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		spin_unlock_irqrestore(&state->uart_port->lock, flags);
12470dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King	}
12481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * In 2.4.5, calls to this will be serialized via the BKL in
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  linux/drivers/char/tty_io.c:tty_release()
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  linux/drivers/char/tty_io.c:do_tty_handup()
12541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_close(struct tty_struct *tty, struct file *filp)
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
125846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct tty_port *port;
125946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport;
126061cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	unsigned long flags;
1261a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
1262eea7e17e0eb23729d58368420659f8e7c357d82eLinus Torvalds	if (!state)
1263eea7e17e0eb23729d58368420659f8e7c357d82eLinus Torvalds		return;
1264eea7e17e0eb23729d58368420659f8e7c357d82eLinus Torvalds
126546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	uport = state->uart_port;
126646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	port = &state->port;
12671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
126846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	pr_debug("uart_close(%d) called\n", uport->line);
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1270d30ccf08e8e01060288587f52a78c3ca0fcfc1fcJiri Slaby	if (tty_port_close_start(port, tty, filp) == 0)
127155956216f4b42fefaee70060b054359d63d2afa5Nobuhiro Iwamatsu		return;
12721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
12741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * At this point, we stop accepting input.  To do this, we
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * disable the receive line status interrupts.
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1277ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_INITIALIZED) {
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long flags;
1279eea7e17e0eb23729d58368420659f8e7c357d82eLinus Torvalds		spin_lock_irqsave(&uport->lock, flags);
128046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		uport->ops->stop_rx(uport);
1281eea7e17e0eb23729d58368420659f8e7c357d82eLinus Torvalds		spin_unlock_irqrestore(&uport->lock, flags);
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Before we drop DTR, make sure the UART transmitter
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * has completely drained; this is especially
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * important if there is a transmit FIFO!
12861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
12871f33a51d9771b34be3cb6f7fb96a325e17bbac7bJiri Slaby		uart_wait_until_sent(tty, uport->timeout);
12881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1290bafb0bd24d7e0e0124318625b239a55d58c757a2Jiri Slaby	mutex_lock(&port->mutex);
1291192251352f912bccfb942ea35801d2357f11f592Alan Cox	uart_shutdown(tty, state);
12921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	uart_flush_buffer(tty);
12931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1294a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox	tty_ldisc_flush(tty);
1295a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
12967b01478f97a671c97fad9254aa91892209b018b5Alan Cox	tty_port_tty_set(port, NULL);
129761cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	spin_lock_irqsave(&port->lock, flags);
129861cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	tty->closing = 0;
12991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
130046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (port->blocked_open) {
130161cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_unlock_irqrestore(&port->lock, flags);
130246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		if (port->close_delay)
13034cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby			msleep_interruptible(
13044cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby					jiffies_to_msecs(port->close_delay));
130561cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_lock_irqsave(&port->lock, flags);
130646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	} else if (!uart_console(uport)) {
130761cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_unlock_irqrestore(&port->lock, flags);
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_change_pm(state, 3);
130961cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_lock_irqsave(&port->lock, flags);
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Wake up anyone trying to open this port.
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1315ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1316426929f8d3514d7f727b8c464d1eeeaf74b21519Jiri Slaby	clear_bit(ASYNCB_CLOSING, &port->flags);
131761cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	spin_unlock_irqrestore(&port->lock, flags);
131846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	wake_up_interruptible(&port->open_wait);
1319426929f8d3514d7f727b8c464d1eeeaf74b21519Jiri Slaby	wake_up_interruptible(&port->close_wait);
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1321a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
13221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13241f33a51d9771b34be3cb6f7fb96a325e17bbac7bJiri Slabystatic void uart_wait_until_sent(struct tty_struct *tty, int timeout)
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13261f33a51d9771b34be3cb6f7fb96a325e17bbac7bJiri Slaby	struct uart_state *state = tty->driver_data;
13271f33a51d9771b34be3cb6f7fb96a325e17bbac7bJiri Slaby	struct uart_port *port = state->uart_port;
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long char_time, expire;
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->type == PORT_UNKNOWN || port->fifosize == 0)
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the check interval to be 1/5 of the estimated time to
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * send a single character, and make it at least 1.  The check
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * interval should also be less than the timeout.
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
13381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Note: we have to use pretty tight timings here to satisfy
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the NIST-PCTS.
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char_time = (port->timeout - HZ/50) / port->fifosize;
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char_time = char_time / 5;
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (char_time == 0)
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char_time = 1;
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (timeout && timeout < char_time)
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char_time = timeout;
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
13491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If the transmitter hasn't cleared in twice the approximate
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * amount of time to send the entire FIFO, it probably won't
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * ever clear.  This assumes the UART isn't doing flow
13521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * control, which is currently the case.  Hence, if it ever
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * takes longer than port->timeout, this is probably due to a
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * UART bug of some kind.  So, we clamp the timeout parameter at
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 2*port->timeout.
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (timeout == 0 || timeout > 2 * port->timeout)
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		timeout = 2 * port->timeout;
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	expire = jiffies + timeout;
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1362eb3a1e1145ca8f12372c7c96aa0702d86a9002a9Jiri Slaby	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1363a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox		port->line, jiffies, expire);
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
13661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Check whether the transmitter is empty every 'char_time'.
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 'timeout' / 'expire' give us the maximum amount of time
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * we wait.
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (!port->ops->tx_empty(port)) {
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		msleep_interruptible(jiffies_to_msecs(char_time));
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (signal_pending(current))
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (time_after(jiffies, expire))
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1377203652192634c1fce5e79df0a8ff2fabfaefd3abArnd Bergmann}
1378203652192634c1fce5e79df0a8ff2fabfaefd3abArnd Bergmann
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This is called with the BKL held in
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  linux/drivers/char/tty_io.c:do_tty_hangup()
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We're called from the eventd thread, so we can sleep for
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a _short_ time only.
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_hangup(struct tty_struct *tty)
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
138846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct tty_port *port = &state->port;
138961cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	unsigned long flags;
13901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1391ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	pr_debug("uart_hangup(%d)\n", state->uart_port->line);
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1393a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
1394ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_NORMAL_ACTIVE) {
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_flush_buffer(tty);
1396192251352f912bccfb942ea35801d2357f11f592Alan Cox		uart_shutdown(tty, state);
139761cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_lock_irqsave(&port->lock, flags);
139891312cdb4fcd832341e425f74f49938e0503c929Alan Cox		port->count = 0;
1399ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
140061cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox		spin_unlock_irqrestore(&port->lock, flags);
14017b01478f97a671c97fad9254aa91892209b018b5Alan Cox		tty_port_tty_set(port, NULL);
140246d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox		wake_up_interruptible(&port->open_wait);
1403bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox		wake_up_interruptible(&port->delta_msr_wait);
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1405a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14080b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slabystatic int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
14090b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby{
14100b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby	return 0;
14110b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby}
14120b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby
14130b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slabystatic void uart_port_shutdown(struct tty_port *port)
14140b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby{
1415b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	struct uart_state *state = container_of(port, struct uart_state, port);
1416b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	struct uart_port *uport = state->uart_port;
1417b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby
1418b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	/*
1419b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1420b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * the irq here so the queue might never be woken up.  Note
1421b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * that we won't end up waiting on delta_msr_wait again since
1422b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * any outstanding file descriptors should be pointing at
1423b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * hung_up_tty_fops now.
1424b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 */
1425b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	wake_up_interruptible(&port->delta_msr_wait);
1426b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby
1427b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	/*
1428b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * Free the IRQ and disable the port.
1429b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 */
1430b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	uport->ops->shutdown(uport);
1431b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby
1432b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	/*
1433b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 * Ensure that the IRQ handler isn't running on another CPU.
1434b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	 */
1435b922e19d03a680d732b61dc8e82d9948f6f8b6c7Jiri Slaby	synchronize_irq(uport->irq);
14360b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby}
14370b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby
1438de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic int uart_carrier_raised(struct tty_port *port)
1439de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox{
1440de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_state *state = container_of(port, struct uart_state, port);
1441de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_port *uport = state->uart_port;
1442de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	int mctrl;
1443de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	spin_lock_irq(&uport->lock);
1444de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	uport->ops->enable_ms(uport);
1445de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	mctrl = uport->ops->get_mctrl(uport);
1446de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	spin_unlock_irq(&uport->lock);
1447de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	if (mctrl & TIOCM_CAR)
1448de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		return 1;
1449de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	return 0;
1450de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox}
1451de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
1452de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic void uart_dtr_rts(struct tty_port *port, int onoff)
1453de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox{
1454de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_state *state = container_of(port, struct uart_state, port);
1455de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_port *uport = state->uart_port;
145624fcc7c8cd0fcabcf37d455abe3501b3196fcf64Alan Cox
14576f5c24ad0f7619502199185a026a228174a27e68Jiri Slaby	if (onoff)
1458de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1459de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	else
1460de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1461de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox}
1462de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1464922f9cfa79b52c85b6002d96cb0eefd13437c58cDenis Cheng * calls to uart_open are serialised by the BKL in
1465922f9cfa79b52c85b6002d96cb0eefd13437c58cDenis Cheng *   fs/char_dev.c:chrdev_open()
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note that if this fails, then uart_close() _will_ be called.
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * In time, we want to scrap the "opening nonpresent ports"
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * behaviour and implement an alternative way for setserial
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to set base addresses/ports/types.  This will allow us to
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * get rid of a certain amount of extra tests.
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int uart_open(struct tty_struct *tty, struct file *filp)
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval, line = tty->index;
14771c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	struct uart_state *state = drv->state + line;
14781c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	struct tty_port *port = &state->port;
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1480eb3a1e1145ca8f12372c7c96aa0702d86a9002a9Jiri Slaby	pr_debug("uart_open(%d) called\n", line);
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
14831c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	 * We take the semaphore here to guarantee that we won't be re-entered
14841c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	 * while allocating the state structure, or while we request any IRQs
14851c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	 * that the driver may need.  This also has the nice side-effect that
14861c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	 * it delays the action of uart_hangup, so we can guarantee that
14871c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	 * state->port.tty will always contain something reasonable.
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
14891c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	if (mutex_lock_interruptible(&port->mutex)) {
14901c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby		retval = -ERESTARTSYS;
14911c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby		goto end;
14921c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	}
14931c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby
14941c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	port->count++;
14951c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
14961c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby		retval = -ENXIO;
14971c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby		goto err_dec_count;
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Once we set tty->driver_data here, we are guaranteed that
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * uart_close() will decrement the driver module use count.
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Any failures from here onwards should not touch the count.
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty->driver_data = state;
1506ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	state->uart_port->state = state;
1507ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
15087b01478f97a671c97fad9254aa91892209b018b5Alan Cox	tty_port_tty_set(port, tty);
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If the port is in the middle of closing, bail out now.
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tty_hung_up_p(filp)) {
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = -EAGAIN;
15151c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby		goto err_dec_count;
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Make sure the device is in D0 state.
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
152191312cdb4fcd832341e425f74f49938e0503c929Alan Cox	if (port->count == 1)
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_change_pm(state, 0);
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Start up the serial port.
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1527192251352f912bccfb942ea35801d2357f11f592Alan Cox	retval = uart_startup(tty, state, 0);
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If we succeeded, wait until the port is ready.
15311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
153261cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	mutex_unlock(&port->mutex);
15331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval == 0)
153474c2107759dc6efaa1b9127014be58a742a1e7acAlan Cox		retval = tty_port_block_til_ready(port, tty, filp);
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15361c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slabyend:
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
15381c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slabyerr_dec_count:
15391c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	port->count--;
15401c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	mutex_unlock(&port->mutex);
15411c7b13c4886f5cfaf02fb1052f65ef1a2fe88b9aJiri Slaby	goto end;
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *uart_type(struct uart_port *port)
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *str = NULL;
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->ops->type)
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		str = port->ops->type(port);
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!str)
15521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		str = "unknown";
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return str;
15551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PROC_FS
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1559d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
15601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = drv->state + i;
1562a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
15633689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis	int pm_state;
1564a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct uart_port *uport = state->uart_port;
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char stat_buf[32];
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int status;
1567d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	int mmio;
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1569a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (!uport)
1570d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		return;
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1572a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mmio = uport->iotype >= UPIO_MEM;
1573d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1574a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			uport->line, uart_type(uport),
15756c6a2334a1e8af7c3eaab992732825fa9ade77cfSergei Shtylyov			mmio ? "mmio:0x" : "port:",
1576a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			mmio ? (unsigned long long)uport->mapbase
1577a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			     : (unsigned long long)uport->iobase,
1578a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			uport->irq);
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1580a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->type == PORT_UNKNOWN) {
1581d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_putc(m, '\n');
1582d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		return;
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1585a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox	if (capable(CAP_SYS_ADMIN)) {
1586a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_lock(&port->mutex);
15873689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		pm_state = state->pm_state;
15883689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		if (pm_state)
15893689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis			uart_change_pm(state, 0);
1590a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_lock_irq(&uport->lock);
1591a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		status = uport->ops->get_mctrl(uport);
1592a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_unlock_irq(&uport->lock);
15933689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		if (pm_state)
15943689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis			uart_change_pm(state, pm_state);
1595a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1597d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_printf(m, " tx:%d rx:%d",
1598a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.tx, uport->icount.rx);
1599a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.frame)
1600d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " fe:%d",
1601a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.frame);
1602a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.parity)
1603d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " pe:%d",
1604a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.parity);
1605a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.brk)
1606d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " brk:%d",
1607a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.brk);
1608a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.overrun)
1609d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " oe:%d",
1610a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.overrun);
1611a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
1612a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define INFOBIT(bit, str) \
1613a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->mctrl & (bit)) \
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		strncat(stat_buf, (str), sizeof(stat_buf) - \
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			strlen(stat_buf) - 2)
1616a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define STATBIT(bit, str) \
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (status & (bit)) \
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		strncat(stat_buf, (str), sizeof(stat_buf) - \
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       strlen(stat_buf) - 2)
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stat_buf[0] = '\0';
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stat_buf[1] = '\0';
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		INFOBIT(TIOCM_RTS, "|RTS");
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_CTS, "|CTS");
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		INFOBIT(TIOCM_DTR, "|DTR");
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_DSR, "|DSR");
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_CAR, "|CD");
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_RNG, "|RI");
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stat_buf[0])
16301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			stat_buf[0] = ' ';
1631a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
1632d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_puts(m, stat_buf);
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1634d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_putc(m, '\n');
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef STATBIT
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef INFOBIT
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1639d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic int uart_proc_show(struct seq_file *m, void *v)
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1641833bb3046b6cb320e775ea2160ddca87d53260d5Alexey Dobriyan	struct tty_driver *ttydrv = m->private;
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *drv = ttydrv->driver_state;
1643d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	int i;
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1645d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			"", "", "");
1647d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	for (i = 0; i < drv->nr; i++)
1648d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		uart_line_info(m, drv, i);
1649d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	return 0;
16501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1651d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan
1652d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic int uart_proc_open(struct inode *inode, struct file *file)
1653d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan{
1654d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	return single_open(file, uart_proc_show, PDE(inode)->data);
1655d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan}
1656d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan
1657d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic const struct file_operations uart_proc_fops = {
1658d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.owner		= THIS_MODULE,
1659d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.open		= uart_proc_open,
1660d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.read		= seq_read,
1661d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.llseek		= seq_lseek,
1662d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.release	= single_release,
1663d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan};
16641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16664a1b5502d426df09b9ba1cbcc74fd09702a74cd8Andrew Morton#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1668d358788f3f30113e49882187d794832905e42592Russell King *	uart_console_write - write a console message to a serial port
1669d358788f3f30113e49882187d794832905e42592Russell King *	@port: the port to write the message
1670d358788f3f30113e49882187d794832905e42592Russell King *	@s: array of characters
1671d358788f3f30113e49882187d794832905e42592Russell King *	@count: number of characters in string to write
1672d358788f3f30113e49882187d794832905e42592Russell King *	@write: function to write character to port
1673d358788f3f30113e49882187d794832905e42592Russell King */
1674d358788f3f30113e49882187d794832905e42592Russell Kingvoid uart_console_write(struct uart_port *port, const char *s,
1675d358788f3f30113e49882187d794832905e42592Russell King			unsigned int count,
1676d358788f3f30113e49882187d794832905e42592Russell King			void (*putchar)(struct uart_port *, int))
1677d358788f3f30113e49882187d794832905e42592Russell King{
1678d358788f3f30113e49882187d794832905e42592Russell King	unsigned int i;
1679d358788f3f30113e49882187d794832905e42592Russell King
1680d358788f3f30113e49882187d794832905e42592Russell King	for (i = 0; i < count; i++, s++) {
1681d358788f3f30113e49882187d794832905e42592Russell King		if (*s == '\n')
1682d358788f3f30113e49882187d794832905e42592Russell King			putchar(port, '\r');
1683d358788f3f30113e49882187d794832905e42592Russell King		putchar(port, *s);
1684d358788f3f30113e49882187d794832905e42592Russell King	}
1685d358788f3f30113e49882187d794832905e42592Russell King}
1686d358788f3f30113e49882187d794832905e42592Russell KingEXPORT_SYMBOL_GPL(uart_console_write);
1687d358788f3f30113e49882187d794832905e42592Russell King
1688d358788f3f30113e49882187d794832905e42592Russell King/*
16891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Check whether an invalid uart number has been specified, and
16901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	if so, search for the first available port that does have
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	console support.
16921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
16931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct uart_port * __init
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_get_console(struct uart_port *ports, int nr, struct console *co)
16951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int idx = co->index;
16971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				     ports[idx].membase == NULL))
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (idx = 0; idx < nr; idx++)
17011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ports[idx].iobase != 0 ||
17021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    ports[idx].membase != NULL)
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	co->index = idx;
17061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ports + idx;
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
17111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_parse_options - Parse serial port baud/parity/bits/flow contro.
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@options: pointer to option string
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud: pointer to an 'int' variable for the baud rate.
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@parity: pointer to an 'int' variable for the parity.
17151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@bits: pointer to an 'int' variable for the number of data bits.
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@flow: pointer to an 'int' variable for the flow control character.
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_parse_options decodes a string containing the serial console
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	options.  The format of the string is <baud><parity><bits><flow>,
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	eg: 115200n8r
17211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1722f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselvoid
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
17241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *s = options;
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*baud = simple_strtoul(s, NULL, 10);
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (*s >= '0' && *s <= '9')
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		s++;
17301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*parity = *s++;
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*bits = *s++ - '0';
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*flow = *s;
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1737f2d937f3bf00665ccf048b3b6616ef95859b0945Jason WesselEXPORT_SYMBOL_GPL(uart_parse_options);
17381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct baud_rates {
17401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int rate;
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int cflag;
17421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1744cb3592be272d83011051dc49f4326355c01f1e1fArjan van de Venstatic const struct baud_rates baud_rates[] = {
17451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 921600, B921600 },
17461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 460800, B460800 },
17471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 230400, B230400 },
17481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 115200, B115200 },
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  57600, B57600  },
17501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  38400, B38400  },
17511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  19200, B19200  },
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   9600, B9600   },
17531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   4800, B4800   },
17541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   2400, B2400   },
17551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   1200, B1200   },
17561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{      0, B38400  }
17571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
17601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_set_options - setup the serial console parameters
17611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@port: pointer to the serial ports uart_port structure
17621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@co: console pointer
17631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud: baud rate
17641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
17651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@bits: number of data bits
17661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@flow: flow control character - 'r' (rts)
17671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1768f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselint
17691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_set_options(struct uart_port *port, struct console *co,
17701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 int baud, int parity, int bits, int flow)
17711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1772606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	struct ktermios termios;
1773149b36eae2ab6aa6056664f4bc461f3d3affc9c1Alan Cox	static struct ktermios dummy;
17741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
17751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1776976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	/*
1777976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * Ensure that the serial console lock is initialised
1778976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * early.
1779976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 */
1780976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	spin_lock_init(&port->lock);
178113e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar	lockdep_set_class(&port->lock, &port_lock_key);
1782976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King
1783606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	memset(&termios, 0, sizeof(struct ktermios));
17841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	termios.c_cflag = CREAD | HUPCL | CLOCAL;
17861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
17881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Construct a cflag setting.
17891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
17901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; baud_rates[i].rate; i++)
17911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (baud_rates[i].rate <= baud)
17921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
17931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	termios.c_cflag |= baud_rates[i].cflag;
17951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (bits == 7)
17971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CS7;
17981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
17991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CS8;
18001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (parity) {
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 'o': case 'O':
18031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= PARODD;
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*fall through*/
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 'e': case 'E':
18061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= PARENB;
18071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
18081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
18091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (flow == 'r')
18111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CRTSCTS;
18121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
181379492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	/*
181479492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 * some uarts on other side don't support no flow control.
181579492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 * So we set * DTR in host uart to make them happy
181679492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 */
181779492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	port->mctrl |= TIOCM_DTR;
181879492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu
1819149b36eae2ab6aa6056664f4bc461f3d3affc9c1Alan Cox	port->ops->set_termios(port, &termios, &dummy);
1820f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	/*
1821f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 * Allow the setting of the UART parameters with a NULL console
1822f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 * too:
1823f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 */
1824f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (co)
1825f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		co->cflag = termios.c_cflag;
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
18281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1829f2d937f3bf00665ccf048b3b6616ef95859b0945Jason WesselEXPORT_SYMBOL_GPL(uart_set_options);
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_SERIAL_CORE_CONSOLE */
18311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1832cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby/**
1833cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * uart_change_pm - set power state of the port
1834cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby *
1835cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @state: port descriptor
1836cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @pm_state: new state
1837cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby *
1838cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * Locking: port->mutex has to be held
1839cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby */
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_change_pm(struct uart_state *state, int pm_state)
18411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1842ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
18431281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor
18441281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor	if (state->pm_state != pm_state) {
18451281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor		if (port->ops->pm)
18461281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor			port->ops->pm(port, pm_state, state->pm_state);
18471281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor		state->pm_state = pm_state;
18481281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor	}
18491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1851b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetskistruct uart_match {
1852b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_port *port;
1853b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_driver *driver;
1854b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski};
1855b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1856b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetskistatic int serial_match_port(struct device *dev, void *data)
1857b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski{
1858b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_match *match = data;
18597ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski	struct tty_driver *tty_drv = match->driver->tty_driver;
18607ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
18617ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski		match->port->line;
1862b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1863b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	return dev->devt == devt; /* Actually, only one tty per port */
1864b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski}
1865b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1866ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Coxint uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1868ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_state *state = drv->state + uport->line;
1869ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct tty_port *port = &state->port;
1870b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct device *tty_dev;
1871ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_match match = {uport, drv};
18721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1873a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1875ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1876b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	if (device_may_wakeup(tty_dev)) {
18773f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		if (!enable_irq_wake(uport->irq))
18783f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			uport->irq_wake = 1;
1879b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		put_device(tty_dev);
1880a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
1881b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		return 0;
1882b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	}
18834547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled || !uart_console(uport))
18844547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		uport->suspended = 1;
1885b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1886ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_INITIALIZED) {
1887ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		const struct uart_ops *ops = uport->ops;
1888c8c6bfa39d6bd7347f43937c8767ae145b61bcb4Russell King		int tries;
18891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18904547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport)) {
18914547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			set_bit(ASYNCB_SUSPENDED, &port->flags);
18924547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			clear_bit(ASYNCB_INITIALIZED, &port->flags);
1893a6b93a908508810c5d51dd9b390283345af6f2d9Russell King
18944547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			spin_lock_irq(&uport->lock);
18954547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->stop_tx(uport);
18964547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->set_mctrl(uport, 0);
18974547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->stop_rx(uport);
18984547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			spin_unlock_irq(&uport->lock);
18994547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		}
19001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
19021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Wait for the transmitter to empty.
19031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1904ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
19051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			msleep(10);
1906c8c6bfa39d6bd7347f43937c8767ae145b61bcb4Russell King		if (!tries)
1907a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			printk(KERN_ERR "%s%s%s%d: Unable to drain "
1908a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox					"transmitter\n",
1909ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       uport->dev ? dev_name(uport->dev) : "",
1910ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       uport->dev ? ": " : "",
19118440838bc5337243917f13bc14ea2445da5e0197David S. Miller			       drv->dev_name,
1912ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       drv->tty_driver->name_base + uport->line);
19131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19144547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport))
19154547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->shutdown(uport);
19161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
19191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Disable the console device before suspending.
19201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
19214547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled && uart_console(uport))
1922ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		console_stop(uport->cons);
19231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19244547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled || !uart_console(uport))
19254547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		uart_change_pm(state, 3);
19261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1927a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
19281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
19301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1932ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Coxint uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
19331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1934ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_state *state = drv->state + uport->line;
1935ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct tty_port *port = &state->port;
193603a74dcc7eebe6edd778317e82fafdf71e68488cArjan van de Ven	struct device *tty_dev;
1937ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_match match = {uport, drv};
1938ba15ab0e8de0d4439a91342ad52d55ca9e313f3dDeepak Saxena	struct ktermios termios;
19391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1940a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
19411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1942ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1943ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (!uport->suspended && device_may_wakeup(tty_dev)) {
19443f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		if (uport->irq_wake) {
19453f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			disable_irq_wake(uport->irq);
19463f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			uport->irq_wake = 0;
19473f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		}
1948a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
1949b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		return 0;
1950b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	}
1951ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	uport->suspended = 0;
1952b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
19531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
19541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Re-enable the console device after suspending.
19551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
19565933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai	if (uart_console(uport)) {
1957891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		/*
1958891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 * First try to use the console cflag setting.
1959891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 */
1960891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		memset(&termios, 0, sizeof(struct ktermios));
1961891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		termios.c_cflag = uport->cons->cflag;
1962891b9dd10764352926e1e107756aa229dfa2c210Jason Wang
1963891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		/*
1964891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 * If that's unset, use the tty termios setting.
1965891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 */
1966891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		if (port->tty && port->tty->termios && termios.c_cflag == 0)
1967891b9dd10764352926e1e107756aa229dfa2c210Jason Wang			termios = *(port->tty->termios);
1968891b9dd10764352926e1e107756aa229dfa2c210Jason Wang
196994abc56f4d90f289ea32a0a11d3577fcd8cb28fbNing Jiang		if (console_suspend_enabled)
197094abc56f4d90f289ea32a0a11d3577fcd8cb28fbNing Jiang			uart_change_pm(state, 0);
1971ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		uport->ops->set_termios(uport, &termios, NULL);
19725933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai		if (console_suspend_enabled)
19735933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai			console_start(uport->cons);
19741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1976ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_SUSPENDED) {
1977ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		const struct uart_ops *ops = uport->ops;
1978ee31b337852ca8a65840702544ff5c64d37740f5Russell King		int ret;
19791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19809d778a69370cc1b643b13648df971c83ff5654efRussell King		uart_change_pm(state, 0);
1981ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		spin_lock_irq(&uport->lock);
1982ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		ops->set_mctrl(uport, 0);
1983ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		spin_unlock_irq(&uport->lock);
19844547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport)) {
1985192251352f912bccfb942ea35801d2357f11f592Alan Cox			/* Protected by port mutex for now */
1986192251352f912bccfb942ea35801d2357f11f592Alan Cox			struct tty_struct *tty = port->tty;
19874547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ret = ops->startup(uport);
19884547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			if (ret == 0) {
1989192251352f912bccfb942ea35801d2357f11f592Alan Cox				if (tty)
1990192251352f912bccfb942ea35801d2357f11f592Alan Cox					uart_change_speed(tty, state, NULL);
19914547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				spin_lock_irq(&uport->lock);
19924547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				ops->set_mctrl(uport, uport->mctrl);
19934547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				ops->start_tx(uport);
19944547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				spin_unlock_irq(&uport->lock);
19954547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				set_bit(ASYNCB_INITIALIZED, &port->flags);
19964547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			} else {
19974547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				/*
19984547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * Failed to resume - maybe hardware went away?
19994547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * Clear the "initialized" flag so we won't try
20004547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * to call the low level drivers shutdown method.
20014547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 */
2002192251352f912bccfb942ea35801d2357f11f592Alan Cox				uart_shutdown(tty, state);
20034547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			}
2004ee31b337852ca8a65840702544ff5c64d37740f5Russell King		}
2005a6b93a908508810c5d51dd9b390283345af6f2d9Russell King
2006ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		clear_bit(ASYNCB_SUSPENDED, &port->flags);
20071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2009a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
20101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
20121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void
20151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_report_port(struct uart_driver *drv, struct uart_port *port)
20161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
201730b7a3bc133c5b4a723163be35157ed709fca91cRussell King	char address[64];
201830b7a3bc133c5b4a723163be35157ed709fca91cRussell King
20191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (port->iotype) {
20201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_PORT:
20219bde10a4b8c54804236d3d6b4b75e98825a921e7Andrew Morton		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
20221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_HUB6:
202430b7a3bc133c5b4a723163be35157ed709fca91cRussell King		snprintf(address, sizeof(address),
20259bde10a4b8c54804236d3d6b4b75e98825a921e7Andrew Morton			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
20261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM:
20281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM32:
202921c614a7899046ab108b3d327d76c33443a8ebf2Pantelis Antoniou	case UPIO_AU:
20303be91ec7388bae3cf1bfb4febcee5ab6c65f409fZang Roy-r	case UPIO_TSI:
203130b7a3bc133c5b4a723163be35157ed709fca91cRussell King		snprintf(address, sizeof(address),
20324f640efb3170dbcf99a37a3cc99060647b95428cJosh Boyer			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
203330b7a3bc133c5b4a723163be35157ed709fca91cRussell King		break;
203430b7a3bc133c5b4a723163be35157ed709fca91cRussell King	default:
203530b7a3bc133c5b4a723163be35157ed709fca91cRussell King		strlcpy(address, "*unknown*", sizeof(address));
20361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
203830b7a3bc133c5b4a723163be35157ed709fca91cRussell King
20390cf669d5c5d08eb827df9867429df21cf030eba6Russell King	printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
20404bfe090b0a29258eeeb026a09a96cf5b5838ac63Kay Sievers	       port->dev ? dev_name(port->dev) : "",
20410cf669d5c5d08eb827df9867429df21cf030eba6Russell King	       port->dev ? ": " : "",
20428440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       drv->dev_name,
20438440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       drv->tty_driver->name_base + port->line,
20448440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       address, port->irq, uart_type(port));
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
20481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_configure_port(struct uart_driver *drv, struct uart_state *state,
20491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    struct uart_port *port)
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int flags;
20521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If there isn't a port here, don't do anything further.
20551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!port->iobase && !port->mapbase && !port->membase)
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Now do the auto configuration stuff.  Note that config_port
20611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * is expected to claim the resources and map the port for us.
20621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20638e23fcc89c8091790903927449f8efb9b4e23960David Daney	flags = 0;
20641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->flags & UPF_AUTO_IRQ)
20651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		flags |= UART_CONFIG_IRQ;
20661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->flags & UPF_BOOT_AUTOCONF) {
20678e23fcc89c8091790903927449f8efb9b4e23960David Daney		if (!(port->flags & UPF_FIXED_TYPE)) {
20688e23fcc89c8091790903927449f8efb9b4e23960David Daney			port->type = PORT_UNKNOWN;
20698e23fcc89c8091790903927449f8efb9b4e23960David Daney			flags |= UART_CONFIG_TYPE;
20708e23fcc89c8091790903927449f8efb9b4e23960David Daney		}
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->ops->config_port(port, flags);
20721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->type != PORT_UNKNOWN) {
20751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long flags;
20761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_report_port(drv, port);
20781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20793689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		/* Power up port for set_mctrl() */
20803689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		uart_change_pm(state, 0);
20813689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Ensure that the modem control lines are de-activated.
2084c3e4642be734ce3d2c7398246d8cbced3a039f54Yinghai Lu		 * keep the DTR setting that is set in uart_set_options()
20851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * We probably don't need a spinlock around this, but
20861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(&port->lock, flags);
2088c3e4642be734ce3d2c7398246d8cbced3a039f54Yinghai Lu		port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(&port->lock, flags);
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
209297d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * If this driver supports console, and it hasn't been
209397d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * successfully registered yet, try to re-register it.
209497d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * It may be that the port was not available.
209597d97224ff361e08777fb33e0fd193ca877dac28Russell King		 */
209697d97224ff361e08777fb33e0fd193ca877dac28Russell King		if (port->cons && !(port->cons->flags & CON_ENABLED))
209797d97224ff361e08777fb33e0fd193ca877dac28Russell King			register_console(port->cons);
209897d97224ff361e08777fb33e0fd193ca877dac28Russell King
209997d97224ff361e08777fb33e0fd193ca877dac28Russell King		/*
21001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Power down all ports by default, except the
21011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * console if we have one.
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
21031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!uart_console(port))
21041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			uart_change_pm(state, 3);
21051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2108f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#ifdef CONFIG_CONSOLE_POLL
2109f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2110f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic int uart_poll_init(struct tty_driver *driver, int line, char *options)
2111f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2112f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2113f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2114f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2115f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int baud = 9600;
2116f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int bits = 8;
2117f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int parity = 'n';
2118f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int flow = 'n';
2119f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2120ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2121f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2122f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2123ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2124f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2125f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2126f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2127f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (options) {
2128f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		uart_parse_options(options, &baud, &parity, &bits, &flow);
2129f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return uart_set_options(port, NULL, baud, parity, bits, flow);
2130f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	}
2131f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2132f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	return 0;
2133f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2134f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2135f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic int uart_poll_get_char(struct tty_driver *driver, int line)
2136f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2137f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2138f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2139f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2140f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2141ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2142f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2143f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2144ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2145f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	return port->ops->poll_get_char(port);
2146f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2147f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2148f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2149f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2150f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2151f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2152f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2153f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2154ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2155f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return;
2156f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2157ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2158f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	port->ops->poll_put_char(port, ch);
2159f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2160f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#endif
2161f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2162b68e31d0ebbcc909d1941f9f230c9d062a3a13d3Jeff Dikestatic const struct tty_operations uart_ops = {
21631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.open		= uart_open,
21641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.close		= uart_close,
21651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write		= uart_write,
21661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.put_char	= uart_put_char,
21671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flush_chars	= uart_flush_chars,
21681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_room	= uart_write_room,
21691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.chars_in_buffer= uart_chars_in_buffer,
21701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flush_buffer	= uart_flush_buffer,
21711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ioctl		= uart_ioctl,
21721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.throttle	= uart_throttle,
21731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.unthrottle	= uart_unthrottle,
21741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.send_xchar	= uart_send_xchar,
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.set_termios	= uart_set_termios,
217664e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox	.set_ldisc	= uart_set_ldisc,
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.stop		= uart_stop,
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.start		= uart_start,
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.hangup		= uart_hangup,
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.break_ctl	= uart_break_ctl,
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.wait_until_sent= uart_wait_until_sent,
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PROC_FS
2183d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.proc_fops	= &uart_proc_fops,
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmget	= uart_tiocmget,
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmset	= uart_tiocmset,
2187d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	.get_icount	= uart_get_icount,
2188f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#ifdef CONFIG_CONSOLE_POLL
2189f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_init	= uart_poll_init,
2190f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_get_char	= uart_poll_get_char,
2191f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_put_char	= uart_poll_put_char,
2192f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#endif
21931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2195de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic const struct tty_port_operations uart_port_ops = {
21960b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby	.activate	= uart_port_activate,
21970b1db83081599615cf7b254aebc14a2d8f6ca056Jiri Slaby	.shutdown	= uart_port_shutdown,
2198de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	.carrier_raised = uart_carrier_raised,
2199de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	.dtr_rts	= uart_dtr_rts,
2200de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox};
2201de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
22021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
22031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_register_driver - register a driver with the uart core layer
22041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: low level driver structure
22051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Register a uart driver with the core driver.  We in turn register
22071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	with the tty layer, and initialise the core driver per-port state.
22081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	We have a proc file in /proc/tty/driver which is named after the
22101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	normal driver.
22111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	drv->port should be NULL, and the per-port structures should be
22131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	registered using uart_add_one_port after this call has succeeded.
22141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
22151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint uart_register_driver(struct uart_driver *drv)
22161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22179e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	struct tty_driver *normal;
22181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, retval;
22191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(drv->state);
22211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Maybe we should be using a slab cache for this, especially if
22241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * we have a large number of ports to handle.
22251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22268f31bb39ec2a5622974666c72257e74c22492602Burman Yan	drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
22271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!drv->state)
22281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
22291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22309e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	normal = alloc_tty_driver(drv->nr);
22311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!normal)
22329e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa		goto out_kfree;
22331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	drv->tty_driver = normal;
22351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->driver_name	= drv->driver_name;
22371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->name		= drv->dev_name;
22381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->major		= drv->major;
22391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->minor_start	= drv->minor;
22401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->type		= TTY_DRIVER_TYPE_SERIAL;
22411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->subtype		= SERIAL_TYPE_NORMAL;
22421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->init_termios	= tty_std_termios;
22431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2244606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2245331b831983f9d706f4a40d08a996d5c2c7a6ea7bGreg Kroah-Hartman	normal->flags		= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
22461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->driver_state    = drv;
22471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_set_operations(normal, &uart_ops);
22481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Initialise the UART state(s).
22511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < drv->nr; i++) {
22531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct uart_state *state = drv->state + i;
2254a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		struct tty_port *port = &state->port;
22551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2256a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		tty_port_init(port);
2257de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		port->ops = &uart_port_ops;
22584cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby		port->close_delay     = HZ / 2;	/* .5 seconds */
22594cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby		port->closing_wait    = 30 * HZ;/* 30 seconds */
22601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
22611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = tty_register_driver(normal);
22639e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	if (retval >= 0)
22649e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa		return retval;
22659e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa
22669e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	put_tty_driver(normal);
22679e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosaout_kfree:
22689e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	kfree(drv->state);
22699e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosaout:
22709e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	return -ENOMEM;
22711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_unregister_driver - remove a driver from the uart core layer
22751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: low level driver structure
22761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Remove all references to a driver from the core driver.  The low
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	level driver must have removed all its ports via the
22791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_remove_one_port() if it registered them with uart_add_one_port().
22801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	(ie, drv->port == NULL)
22811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
22821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid uart_unregister_driver(struct uart_driver *drv)
22831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct tty_driver *p = drv->tty_driver;
22851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_unregister_driver(p);
22861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_tty_driver(p);
22871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(drv->state);
2288d0afed68fb1d586034be7a7e038488e83c37a89fAlan Cox	drv->state = NULL;
22891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	drv->tty_driver = NULL;
22901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct tty_driver *uart_console_device(struct console *co, int *index)
22931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *p = co->data;
22951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*index = co->index;
22961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p->tty_driver;
22971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
23001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_add_one_port - attach a driver-defined port structure
23011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: pointer to the uart low level driver structure for this port
23021b9894f342a39601bb0420b7b8c7e445670c1b51Randy Dunlap *	@uport: uart port structure to use for this port.
23031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This allows the driver to register its own uart_port structure
23051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	with the core driver.  The main purpose is to allow the low
23061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	level uart drivers to expand uart_port, rather than having yet
23071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	more levels of structures.
23081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2309a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Coxint uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
23101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
23111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state;
2312a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port;
23131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
2314b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct device *tty_dev;
23151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(in_interrupt());
23171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2318a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->line >= drv->nr)
23191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
23201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2321a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	state = drv->state + uport->line;
2322a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	port = &state->port;
23231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2324f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_lock(&port_mutex);
2325a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
2326ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (state->uart_port) {
23271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EINVAL;
23281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
23291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
23301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2331a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	state->uart_port = uport;
233297d97224ff361e08777fb33e0fd193ca877dac28Russell King	state->pm_state = -1;
23331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2334a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->cons = drv->cons;
2335a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->state = state;
23361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2337976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	/*
2338976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * If this port is a console, then the spinlock is already
2339976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * initialised.
2340976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 */
2341a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2342a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_lock_init(&uport->lock);
2343a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		lockdep_set_class(&uport->lock, &port_lock_key);
234413e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar	}
2345976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King
2346a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uart_configure_port(drv, state, uport);
23471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
23491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Register the port whether it's detected or not.  This allows
23501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * setserial to be used to alter this ports parameters.
23511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2352a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2353b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	if (likely(!IS_ERR(tty_dev))) {
2354773598357c0baf03081cf87f2b444f97744faf1eSimon Glass		device_set_wakeup_capable(tty_dev, 1);
2355773598357c0baf03081cf87f2b444f97744faf1eSimon Glass	} else {
2356b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		printk(KERN_ERR "Cannot register tty device on line %d\n",
2357a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		       uport->line);
2358773598357c0baf03081cf87f2b444f97744faf1eSimon Glass	}
23591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
236168ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Ensure UPF_DEAD is not set.
236268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2363a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->flags &= ~UPF_DEAD;
236468ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
23651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds out:
2366a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
2367f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_unlock(&port_mutex);
23681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
23701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
23731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_remove_one_port - detach a driver defined port structure
23741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: pointer to the uart low level driver structure for this port
23751b9894f342a39601bb0420b7b8c7e445670c1b51Randy Dunlap *	@uport: uart port structure for this port
23761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This unhooks (and hangs up) the specified port structure from the
23781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	core driver.  No further calls will be made to the low-level code
23791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	for this port.
23801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2381a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Coxint uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
23821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2383a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct uart_state *state = drv->state + uport->line;
2384a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
23851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(in_interrupt());
23871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2388a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (state->uart_port != uport)
23891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2390a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			state->uart_port, uport);
23911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2392f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_lock(&port_mutex);
23931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
239568ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Mark the port "dead" - this prevents any opens from
239668ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * succeeding while we shut down the port.
239768ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2398a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
2399a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->flags |= UPF_DEAD;
2400a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
240168ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
240268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
2403aa4148cfc7b3b93eeaf755a7d14f10afaffe9a96Greg Kroah-Hartman	 * Remove the devices from the tty layer
24041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2405a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tty_unregister_device(drv->tty_driver, uport->line);
24061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2407a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (port->tty)
2408a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		tty_vhangup(port->tty);
240968ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
241068ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
241168ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Free the port IO and memory resources, if any.
241268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2413a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->type != PORT_UNKNOWN)
2414a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		uport->ops->release_port(uport);
241568ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
241668ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
241768ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Indicate that there isn't a port here anymore.
241868ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2419a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->type = PORT_UNKNOWN;
242068ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
2421ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	state->uart_port = NULL;
2422f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_unlock(&port_mutex);
24231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
24251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
24261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
24281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Are the two ports equivalent?
24291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
24301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint uart_match_port(struct uart_port *port1, struct uart_port *port2)
24311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
24321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port1->iotype != port2->iotype)
24331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
24341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (port1->iotype) {
24361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_PORT:
24371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (port1->iobase == port2->iobase);
24381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_HUB6:
24391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (port1->iobase == port2->iobase) &&
24401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       (port1->hub6   == port2->hub6);
24411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM:
2442d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_MEM32:
2443d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_AU:
2444d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_TSI:
24451624f003349b49050f42c7d9f5407dfc05efb912Benjamin Herrenschmidt		return (port1->mapbase == port2->mapbase);
24461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
24471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
24481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
24491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_match_port);
24501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2451027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby/**
2452027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	uart_handle_dcd_change - handle a change of carrier detect state
2453027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@uport: uart_port structure for the open port
2454027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@status: new carrier detect status, nonzero if active
2455027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby */
2456027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2457027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2458027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct uart_state *state = uport->state;
2459027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_port *port = &state->port;
2460027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_ldisc *ld = tty_ldisc_ref(port->tty);
2461027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct pps_event_time ts;
2462027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2463027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld && ld->ops->dcd_change)
2464027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		pps_get_ts(&ts);
2465027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2466027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	uport->icount.dcd++;
2467027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby#ifdef CONFIG_HARD_PPS
2468027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if ((uport->flags & UPF_HARDPPS_CD) && status)
2469027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		hardpps();
2470027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby#endif
2471027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2472027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (port->flags & ASYNC_CHECK_CD) {
2473027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		if (status)
2474027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			wake_up_interruptible(&port->open_wait);
2475027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		else if (port->tty)
2476027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			tty_hangup(port->tty);
2477027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	}
2478027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2479027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld && ld->ops->dcd_change)
2480027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		ld->ops->dcd_change(port->tty, status, &ts);
2481027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld)
2482027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_ldisc_deref(ld);
2483027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2484027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2485027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2486027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby/**
2487027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	uart_handle_cts_change - handle a change of clear-to-send state
2488027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@uport: uart_port structure for the open port
2489027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@status: new clear to send status, nonzero if active
2490027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby */
2491027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2492027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2493027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_port *port = &uport->state->port;
2494027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_struct *tty = port->tty;
2495027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2496027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	uport->icount.cts++;
2497027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2498027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (port->flags & ASYNC_CTS_FLOW) {
2499027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		if (tty->hw_stopped) {
2500027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			if (status) {
2501027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				tty->hw_stopped = 0;
2502027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uport->ops->start_tx(uport);
2503027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uart_write_wakeup(uport);
2504027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			}
2505027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		} else {
2506027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			if (!status) {
2507027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				tty->hw_stopped = 1;
2508027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uport->ops->stop_tx(uport);
2509027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			}
2510027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		}
2511027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	}
2512027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2513027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_handle_cts_change);
2514027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2515cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby/**
2516cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * uart_insert_char - push a char to the uart layer
2517cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby *
2518cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * User is responsible to call tty_flip_buffer_push when they are done with
2519cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * insertion.
2520cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby *
2521cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @port: corresponding port
2522cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @status: state of the serial port RX buffer (LSR for 8250)
2523cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @overrun: mask of overrun bits in @status
2524cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @ch: character to push
2525cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby * @flag: flag for the character (see TTY_NORMAL and friends)
2526cf75525f374dd5c49d705d9c8c3757f9aa3e9fd2Jiri Slaby */
2527027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_insert_char(struct uart_port *port, unsigned int status,
2528027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		 unsigned int overrun, unsigned int ch, unsigned int flag)
2529027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2530027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_struct *tty = port->state->port.tty;
2531027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2532027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if ((status & port->ignore_status_mask & ~overrun) == 0)
2533027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_insert_flip_char(tty, ch, flag);
2534027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2535027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	/*
2536027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 * Overrun is special.  Since it's reported immediately,
2537027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 * it doesn't affect the current character.
2538027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 */
2539027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (status & ~port->ignore_status_mask & overrun)
2540027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2541027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2542027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_insert_char);
2543027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
25441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_write_wakeup);
25451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_register_driver);
25461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_unregister_driver);
25471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_suspend_port);
25481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_resume_port);
25491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_add_one_port);
25501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_remove_one_port);
25511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("Serial driver core");
25531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
2554