serial_core.c revision d30ccf08e8e01060288587f52a78c3ca0fcfc1fc
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
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is used by the interrupt handler to schedule processing in
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the software interrupt portion of the driver.
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid uart_write_wakeup(struct uart_port *port)
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
70ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_state *state = port->state;
71d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	/*
72d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * This means you called this function _after_ the port was
73d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 * closed.  No cookie for you.
74d5f735e52fb41e032b0db08aa20c02dbb9cd0db3Pavel Machek	 */
75ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	BUG_ON(!state);
766a3e492b6daaf7ec4dc41e51d87d2aae8ff886f2Jiri Slaby	tty_wakeup(state->port.tty);
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_stop(struct tty_struct *tty)
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
82ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
86b129a8ccd53f74c43e4c83c8e0031a4990040830Russell King	port->ops->stop_tx(port);
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __uart_start(struct tty_struct *tty)
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
93ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
95ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    !tty->stopped && !tty->hw_stopped)
97b129a8ccd53f74c43e4c83c8e0031a4990040830Russell King		port->ops->start_tx(port);
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_start(struct tty_struct *tty)
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = tty->driver_data;
103ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__uart_start(tty);
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int old;
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&port->lock, flags);
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	old = port->mctrl;
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	port->mctrl = (old & ~clear) | set;
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (old != port->mctrl)
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->ops->set_mctrl(port, port->mctrl);
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(&port->lock, flags);
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
125a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
126a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Startup the port.  This will be called once per open.  All calls
130df4f4dd429870f435f8d5d9d561db029a29f063bAlan Cox * will be serialised by the per-port mutex.
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
132192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13446d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct uart_port *uport = state->uart_port;
13546d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	struct tty_port *port = &state->port;
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long page;
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval = 0;
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
139ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_INITIALIZED)
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the TTY IO error marker - we will only clear this
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * once we have successfully opened the port.  Also set
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * up the tty->alt_speed kludge
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
147192251352f912bccfb942ea35801d2357f11f592Alan Cox	set_bit(TTY_IO_ERROR, &tty->flags);
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14946d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	if (uport->type == PORT_UNKNOWN)
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Initialise and allocate the transmit and temporary
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * buffer.
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
156ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state->xmit.buf) {
157df4f4dd429870f435f8d5d9d561db029a29f063bAlan Cox		/* This is protected by the per port mutex */
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page = get_zeroed_page(GFP_KERNEL);
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!page)
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -ENOMEM;
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
162ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		state->xmit.buf = (unsigned char *) page;
163ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox		uart_circ_clear(&state->xmit);
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16646d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox	retval = uport->ops->startup(uport);
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval == 0) {
168c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		if (uart_console(uport) && uport->cons->cflag) {
169c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby			tty->termios->c_cflag = uport->cons->cflag;
170c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby			uport->cons->cflag = 0;
171c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		}
172c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		/*
173c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		 * Initialise the hardware port settings.
174c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		 */
175c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		uart_change_speed(tty, state, NULL);
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
177c7d7abff40c27f82fe78b1091ab3fad69b2546f9Jiri Slaby		if (init_hw) {
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * Setup the RTS and DTR signals once the
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * port is open and ready to respond.
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
182192251352f912bccfb942ea35801d2357f11f592Alan Cox			if (tty->termios->c_cflag & CBAUD)
18346d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox				uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
186ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		if (port->flags & ASYNC_CTS_FLOW) {
18746d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			spin_lock_irq(&uport->lock);
18846d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
189192251352f912bccfb942ea35801d2357f11f592Alan Cox				tty->hw_stopped = 1;
19046d57a449aa13d9c6adcc9d1dbc7b9a0ecfb69d8Alan Cox			spin_unlock_irq(&uport->lock);
1910dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King		}
1920dd7a1aed7c34a39917c4faf75b4230c169e809bRussell King
193ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		set_bit(ASYNCB_INITIALIZED, &port->flags);
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
195192251352f912bccfb942ea35801d2357f11f592Alan Cox		clear_bit(TTY_IO_ERROR, &tty->flags);
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1980055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	/*
1990055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * This is to allow setserial on this port. People may want to set
2000055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * port/irq/type and then reconfigure the port properly if it failed
2010055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 * now.
2020055197e984e5fbe6f48f37fc50dd30254915493Jiri Slaby	 */
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval && capable(CAP_SYS_ADMIN))
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = 0;
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine will shutdown a serial port; interrupts are disabled, and
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * DTR is dropped if the hangup on close termio flag is on.  Calls to
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * uart_shutdown are serialised by the per-port semaphore.
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
214192251352f912bccfb942ea35801d2357f11f592Alan Coxstatic void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
216ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_port *uport = state->uart_port;
217bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	struct tty_port *port = &state->port;
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
220ee31b337852ca8a65840702544ff5c64d37740f5Russell King	 * Set the TTY IO error marker
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
222f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox	if (tty)
223f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox		set_bit(TTY_IO_ERROR, &tty->flags);
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
225bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox	if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
226ee31b337852ca8a65840702544ff5c64d37740f5Russell King		/*
227ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * Turn off DTR and RTS early.
228ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 */
229f751928e0ddf54ea4fe5546f35e99efc5b5d9938Alan Cox		if (!tty || (tty->termios->c_cflag & HUPCL))
230ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
231ee31b337852ca8a65840702544ff5c64d37740f5Russell King
232ee31b337852ca8a65840702544ff5c64d37740f5Russell King		/*
233ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * clear delta_msr_wait queue to avoid mem leaks: we may free
234ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * the irq here so the queue might never be woken up.  Note
235ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * that we won't end up waiting on delta_msr_wait again since
236ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * any outstanding file descriptors should be pointing at
237ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * hung_up_tty_fops now.
238ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 */
239bdc04e3174e18f475289fa8f4144f66686326b7eAlan Cox		wake_up_interruptible(&port->delta_msr_wait);
240ee31b337852ca8a65840702544ff5c64d37740f5Russell King
241ee31b337852ca8a65840702544ff5c64d37740f5Russell King		/*
242ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * Free the IRQ and disable the port.
243ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 */
244ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		uport->ops->shutdown(uport);
245ee31b337852ca8a65840702544ff5c64d37740f5Russell King
246ee31b337852ca8a65840702544ff5c64d37740f5Russell King		/*
247ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 * Ensure that the IRQ handler isn't running on another CPU.
248ee31b337852ca8a65840702544ff5c64d37740f5Russell King		 */
249ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		synchronize_irq(uport->irq);
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
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		quot = (port->uartclk + (8 * baud)) / (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
1408de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic int uart_carrier_raised(struct tty_port *port)
1409de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox{
1410de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_state *state = container_of(port, struct uart_state, port);
1411de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_port *uport = state->uart_port;
1412de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	int mctrl;
1413de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	spin_lock_irq(&uport->lock);
1414de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	uport->ops->enable_ms(uport);
1415de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	mctrl = uport->ops->get_mctrl(uport);
1416de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	spin_unlock_irq(&uport->lock);
1417de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	if (mctrl & TIOCM_CAR)
1418de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		return 1;
1419de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	return 0;
1420de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox}
1421de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
1422de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic void uart_dtr_rts(struct tty_port *port, int onoff)
1423de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox{
1424de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_state *state = container_of(port, struct uart_state, port);
1425de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	struct uart_port *uport = state->uart_port;
142624fcc7c8cd0fcabcf37d455abe3501b3196fcf64Alan Cox
14276f5c24ad0f7619502199185a026a228174a27e68Jiri Slaby	if (onoff)
1428de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1429de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	else
1430de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1431de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox}
1432de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
14331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct uart_state *uart_get(struct uart_driver *drv, int line)
14341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state;
1436a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port;
143768ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	int ret = 0;
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	state = drv->state + line;
1440a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	port = &state->port;
1441a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (mutex_lock_interruptible(&port->mutex)) {
144268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King		ret = -ERESTARTSYS;
144368ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King		goto err;
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1446a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	port->count++;
1447ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
144868ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King		ret = -ENXIO;
144968ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King		goto err_unlock;
14501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return state;
145268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
145368ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King err_unlock:
1454a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	port->count--;
1455a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
145668ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King err:
145768ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	return ERR_PTR(ret);
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1461922f9cfa79b52c85b6002d96cb0eefd13437c58cDenis Cheng * calls to uart_open are serialised by the BKL in
1462922f9cfa79b52c85b6002d96cb0eefd13437c58cDenis Cheng *   fs/char_dev.c:chrdev_open()
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note that if this fails, then uart_close() _will_ be called.
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * In time, we want to scrap the "opening nonpresent ports"
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * behaviour and implement an alternative way for setserial
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to set base addresses/ports/types.  This will allow us to
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * get rid of a certain amount of extra tests.
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int uart_open(struct tty_struct *tty, struct file *filp)
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state;
147491312cdb4fcd832341e425f74f49938e0503c929Alan Cox	struct tty_port *port;
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval, line = tty->index;
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1477eb3a1e1145ca8f12372c7c96aa0702d86a9002a9Jiri Slaby	pr_debug("uart_open(%d) called\n", line);
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * We take the semaphore inside uart_get to guarantee that we won't
1481ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	 * be re-entered while allocating the state structure, or while we
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * request any IRQs that the driver may need.  This also has the nice
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * side-effect that it delays the action of uart_hangup, so we can
1484ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	 * guarantee that state->port.tty will always contain something
1485ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	 * reasonable.
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	state = uart_get(drv, line);
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(state)) {
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = PTR_ERR(state);
14901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto fail;
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
149291312cdb4fcd832341e425f74f49938e0503c929Alan Cox	port = &state->port;
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Once we set tty->driver_data here, we are guaranteed that
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * uart_close() will decrement the driver module use count.
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Any failures from here onwards should not touch the count.
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty->driver_data = state;
1500ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	state->uart_port->state = state;
1501ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty->alt_speed = 0;
15037b01478f97a671c97fad9254aa91892209b018b5Alan Cox	tty_port_tty_set(port, tty);
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If the port is in the middle of closing, bail out now.
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tty_hung_up_p(filp)) {
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		retval = -EAGAIN;
151091312cdb4fcd832341e425f74f49938e0503c929Alan Cox		port->count--;
1511a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto fail;
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Make sure the device is in D0 state.
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
151891312cdb4fcd832341e425f74f49938e0503c929Alan Cox	if (port->count == 1)
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_change_pm(state, 0);
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Start up the serial port.
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1524192251352f912bccfb942ea35801d2357f11f592Alan Cox	retval = uart_startup(tty, state, 0);
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If we succeeded, wait until the port is ready.
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
152961cd8a21d8a9fb4b11111270cf2d3aa919c20624Alan Cox	mutex_unlock(&port->mutex);
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval == 0)
153174c2107759dc6efaa1b9127014be58a742a1e7acAlan Cox		retval = tty_port_block_til_ready(port, tty, filp);
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1533a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Coxfail:
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *uart_type(struct uart_port *port)
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *str = NULL;
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->ops->type)
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		str = port->ops->type(port);
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!str)
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		str = "unknown";
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return str;
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PROC_FS
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1552d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state = drv->state + i;
1555a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
15563689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis	int pm_state;
1557a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct uart_port *uport = state->uart_port;
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char stat_buf[32];
15591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int status;
1560d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	int mmio;
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1562a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (!uport)
1563d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		return;
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1565a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mmio = uport->iotype >= UPIO_MEM;
1566d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1567a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			uport->line, uart_type(uport),
15686c6a2334a1e8af7c3eaab992732825fa9ade77cfSergei Shtylyov			mmio ? "mmio:0x" : "port:",
1569a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			mmio ? (unsigned long long)uport->mapbase
1570a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			     : (unsigned long long)uport->iobase,
1571a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			uport->irq);
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1573a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->type == PORT_UNKNOWN) {
1574d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_putc(m, '\n');
1575d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		return;
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1578a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox	if (capable(CAP_SYS_ADMIN)) {
1579a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_lock(&port->mutex);
15803689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		pm_state = state->pm_state;
15813689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		if (pm_state)
15823689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis			uart_change_pm(state, 0);
1583a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_lock_irq(&uport->lock);
1584a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		status = uport->ops->get_mctrl(uport);
1585a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_unlock_irq(&uport->lock);
15863689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		if (pm_state)
15873689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis			uart_change_pm(state, pm_state);
1588a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1590d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_printf(m, " tx:%d rx:%d",
1591a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.tx, uport->icount.rx);
1592a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.frame)
1593d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " fe:%d",
1594a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.frame);
1595a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.parity)
1596d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " pe:%d",
1597a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.parity);
1598a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.brk)
1599d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " brk:%d",
1600a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.brk);
1601a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		if (uport->icount.overrun)
1602d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan			seq_printf(m, " oe:%d",
1603a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox				uport->icount.overrun);
1604a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
1605a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define INFOBIT(bit, str) \
1606a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->mctrl & (bit)) \
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		strncat(stat_buf, (str), sizeof(stat_buf) - \
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			strlen(stat_buf) - 2)
1609a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox#define STATBIT(bit, str) \
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (status & (bit)) \
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		strncat(stat_buf, (str), sizeof(stat_buf) - \
16121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       strlen(stat_buf) - 2)
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stat_buf[0] = '\0';
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stat_buf[1] = '\0';
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		INFOBIT(TIOCM_RTS, "|RTS");
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_CTS, "|CTS");
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		INFOBIT(TIOCM_DTR, "|DTR");
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_DSR, "|DSR");
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_CAR, "|CD");
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		STATBIT(TIOCM_RNG, "|RI");
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stat_buf[0])
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			stat_buf[0] = ' ';
1624a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox
1625d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		seq_puts(m, stat_buf);
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1627d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_putc(m, '\n');
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef STATBIT
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef INFOBIT
16301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1632d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic int uart_proc_show(struct seq_file *m, void *v)
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1634833bb3046b6cb320e775ea2160ddca87d53260d5Alexey Dobriyan	struct tty_driver *ttydrv = m->private;
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *drv = ttydrv->driver_state;
1636d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	int i;
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1638d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			"", "", "");
1640d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	for (i = 0; i < drv->nr; i++)
1641d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan		uart_line_info(m, drv, i);
1642d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	return 0;
16431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1644d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan
1645d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic int uart_proc_open(struct inode *inode, struct file *file)
1646d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan{
1647d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	return single_open(file, uart_proc_show, PDE(inode)->data);
1648d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan}
1649d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan
1650d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyanstatic const struct file_operations uart_proc_fops = {
1651d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.owner		= THIS_MODULE,
1652d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.open		= uart_proc_open,
1653d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.read		= seq_read,
1654d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.llseek		= seq_lseek,
1655d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.release	= single_release,
1656d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan};
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16594a1b5502d426df09b9ba1cbcc74fd09702a74cd8Andrew Morton#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
16601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1661d358788f3f30113e49882187d794832905e42592Russell King *	uart_console_write - write a console message to a serial port
1662d358788f3f30113e49882187d794832905e42592Russell King *	@port: the port to write the message
1663d358788f3f30113e49882187d794832905e42592Russell King *	@s: array of characters
1664d358788f3f30113e49882187d794832905e42592Russell King *	@count: number of characters in string to write
1665d358788f3f30113e49882187d794832905e42592Russell King *	@write: function to write character to port
1666d358788f3f30113e49882187d794832905e42592Russell King */
1667d358788f3f30113e49882187d794832905e42592Russell Kingvoid uart_console_write(struct uart_port *port, const char *s,
1668d358788f3f30113e49882187d794832905e42592Russell King			unsigned int count,
1669d358788f3f30113e49882187d794832905e42592Russell King			void (*putchar)(struct uart_port *, int))
1670d358788f3f30113e49882187d794832905e42592Russell King{
1671d358788f3f30113e49882187d794832905e42592Russell King	unsigned int i;
1672d358788f3f30113e49882187d794832905e42592Russell King
1673d358788f3f30113e49882187d794832905e42592Russell King	for (i = 0; i < count; i++, s++) {
1674d358788f3f30113e49882187d794832905e42592Russell King		if (*s == '\n')
1675d358788f3f30113e49882187d794832905e42592Russell King			putchar(port, '\r');
1676d358788f3f30113e49882187d794832905e42592Russell King		putchar(port, *s);
1677d358788f3f30113e49882187d794832905e42592Russell King	}
1678d358788f3f30113e49882187d794832905e42592Russell King}
1679d358788f3f30113e49882187d794832905e42592Russell KingEXPORT_SYMBOL_GPL(uart_console_write);
1680d358788f3f30113e49882187d794832905e42592Russell King
1681d358788f3f30113e49882187d794832905e42592Russell King/*
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Check whether an invalid uart number has been specified, and
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	if so, search for the first available port that does have
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	console support.
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
16861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct uart_port * __init
16871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_get_console(struct uart_port *ports, int nr, struct console *co)
16881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int idx = co->index;
16901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
16921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				     ports[idx].membase == NULL))
16931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (idx = 0; idx < nr; idx++)
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ports[idx].iobase != 0 ||
16951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    ports[idx].membase != NULL)
16961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
16971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	co->index = idx;
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ports + idx;
17011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_parse_options - Parse serial port baud/parity/bits/flow contro.
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@options: pointer to option string
17061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud: pointer to an 'int' variable for the baud rate.
17071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@parity: pointer to an 'int' variable for the parity.
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@bits: pointer to an 'int' variable for the number of data bits.
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@flow: pointer to an 'int' variable for the flow control character.
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
17111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_parse_options decodes a string containing the serial console
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	options.  The format of the string is <baud><parity><bits><flow>,
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	eg: 115200n8r
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1715f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselvoid
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *s = options;
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*baud = simple_strtoul(s, NULL, 10);
17211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (*s >= '0' && *s <= '9')
17221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		s++;
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*parity = *s++;
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*bits = *s++ - '0';
17271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*s)
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*flow = *s;
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1730f2d937f3bf00665ccf048b3b6616ef95859b0945Jason WesselEXPORT_SYMBOL_GPL(uart_parse_options);
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct baud_rates {
17331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int rate;
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int cflag;
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1737cb3592be272d83011051dc49f4326355c01f1e1fArjan van de Venstatic const struct baud_rates baud_rates[] = {
17381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 921600, B921600 },
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 460800, B460800 },
17401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 230400, B230400 },
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 115200, B115200 },
17421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  57600, B57600  },
17431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  38400, B38400  },
17441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{  19200, B19200  },
17451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   9600, B9600   },
17461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   4800, B4800   },
17471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   2400, B2400   },
17481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{   1200, B1200   },
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{      0, B38400  }
17501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
17531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_set_options - setup the serial console parameters
17541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@port: pointer to the serial ports uart_port structure
17551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@co: console pointer
17561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@baud: baud rate
17571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
17581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@bits: number of data bits
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@flow: flow control character - 'r' (rts)
17601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1761f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselint
17621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_set_options(struct uart_port *port, struct console *co,
17631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 int baud, int parity, int bits, int flow)
17641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1765606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	struct ktermios termios;
1766149b36eae2ab6aa6056664f4bc461f3d3affc9c1Alan Cox	static struct ktermios dummy;
17671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
17681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1769976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	/*
1770976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * Ensure that the serial console lock is initialised
1771976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * early.
1772976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 */
1773976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	spin_lock_init(&port->lock);
177413e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar	lockdep_set_class(&port->lock, &port_lock_key);
1775976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King
1776606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	memset(&termios, 0, sizeof(struct ktermios));
17771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	termios.c_cflag = CREAD | HUPCL | CLOCAL;
17791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
17811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Construct a cflag setting.
17821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
17831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; baud_rates[i].rate; i++)
17841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (baud_rates[i].rate <= baud)
17851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
17861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	termios.c_cflag |= baud_rates[i].cflag;
17881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (bits == 7)
17901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CS7;
17911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
17921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CS8;
17931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (parity) {
17951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 'o': case 'O':
17961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= PARODD;
17971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*fall through*/
17981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 'e': case 'E':
17991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= PARENB;
18001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (flow == 'r')
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		termios.c_cflag |= CRTSCTS;
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
180679492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	/*
180779492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 * some uarts on other side don't support no flow control.
180879492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 * So we set * DTR in host uart to make them happy
180979492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	 */
181079492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu	port->mctrl |= TIOCM_DTR;
181179492689e40d4f4d3d8a7262781d56fb295b4b86Yinghai Lu
1812149b36eae2ab6aa6056664f4bc461f3d3affc9c1Alan Cox	port->ops->set_termios(port, &termios, &dummy);
1813f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	/*
1814f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 * Allow the setting of the UART parameters with a NULL console
1815f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 * too:
1816f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	 */
1817f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (co)
1818f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		co->cflag = termios.c_cflag;
18191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
18211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1822f2d937f3bf00665ccf048b3b6616ef95859b0945Jason WesselEXPORT_SYMBOL_GPL(uart_set_options);
18231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_SERIAL_CORE_CONSOLE */
18241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void uart_change_pm(struct uart_state *state, int pm_state)
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1827ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	struct uart_port *port = state->uart_port;
18281281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor
18291281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor	if (state->pm_state != pm_state) {
18301281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor		if (port->ops->pm)
18311281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor			port->ops->pm(port, pm_state, state->pm_state);
18321281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor		state->pm_state = pm_state;
18331281e36027a9119356bd93b5e7853c72c35dd462Andrew Victor	}
18341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1836b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetskistruct uart_match {
1837b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_port *port;
1838b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_driver *driver;
1839b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski};
1840b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1841b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetskistatic int serial_match_port(struct device *dev, void *data)
1842b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski{
1843b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct uart_match *match = data;
18447ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski	struct tty_driver *tty_drv = match->driver->tty_driver;
18457ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
18467ca796f492a11f9408e661c8f22cd8c4f486b8e5Guennadi Liakhovetski		match->port->line;
1847b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1848b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	return dev->devt == devt; /* Actually, only one tty per port */
1849b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski}
1850b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1851ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Coxint uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
18521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1853ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_state *state = drv->state + uport->line;
1854ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct tty_port *port = &state->port;
1855b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct device *tty_dev;
1856ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_match match = {uport, drv};
18571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1858a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
18591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1860ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1861b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	if (device_may_wakeup(tty_dev)) {
18623f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		if (!enable_irq_wake(uport->irq))
18633f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			uport->irq_wake = 1;
1864b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		put_device(tty_dev);
1865a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
1866b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		return 0;
1867b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	}
18684547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled || !uart_console(uport))
18694547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		uport->suspended = 1;
1870b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
1871ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_INITIALIZED) {
1872ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		const struct uart_ops *ops = uport->ops;
1873c8c6bfa39d6bd7347f43937c8767ae145b61bcb4Russell King		int tries;
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18754547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport)) {
18764547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			set_bit(ASYNCB_SUSPENDED, &port->flags);
18774547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			clear_bit(ASYNCB_INITIALIZED, &port->flags);
1878a6b93a908508810c5d51dd9b390283345af6f2d9Russell King
18794547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			spin_lock_irq(&uport->lock);
18804547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->stop_tx(uport);
18814547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->set_mctrl(uport, 0);
18824547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->stop_rx(uport);
18834547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			spin_unlock_irq(&uport->lock);
18844547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		}
18851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
18871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Wait for the transmitter to empty.
18881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1889ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
18901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			msleep(10);
1891c8c6bfa39d6bd7347f43937c8767ae145b61bcb4Russell King		if (!tries)
1892a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox			printk(KERN_ERR "%s%s%s%d: Unable to drain "
1893a46c9994242978ab001299cc9c906b9a3eedadccAlan Cox					"transmitter\n",
1894ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       uport->dev ? dev_name(uport->dev) : "",
1895ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       uport->dev ? ": " : "",
18968440838bc5337243917f13bc14ea2445da5e0197David S. Miller			       drv->dev_name,
1897ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox			       drv->tty_driver->name_base + uport->line);
18981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18994547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport))
19004547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ops->shutdown(uport);
19011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
19041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Disable the console device before suspending.
19051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
19064547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled && uart_console(uport))
1907ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		console_stop(uport->cons);
19081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19094547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec	if (console_suspend_enabled || !uart_console(uport))
19104547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		uart_change_pm(state, 3);
19111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1912a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
19131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
19151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1917ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Coxint uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
19181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1919ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_state *state = drv->state + uport->line;
1920ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct tty_port *port = &state->port;
192103a74dcc7eebe6edd778317e82fafdf71e68488cArjan van de Ven	struct device *tty_dev;
1922ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	struct uart_match match = {uport, drv};
1923ba15ab0e8de0d4439a91342ad52d55ca9e313f3dDeepak Saxena	struct ktermios termios;
19241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1925a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
19261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1927ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1928ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (!uport->suspended && device_may_wakeup(tty_dev)) {
19293f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		if (uport->irq_wake) {
19303f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			disable_irq_wake(uport->irq);
19313f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R			uport->irq_wake = 0;
19323f960dbb9dfe29ff283810624c4340c79fde87f5Govindraj.R		}
1933a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		mutex_unlock(&port->mutex);
1934b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		return 0;
1935b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	}
1936ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	uport->suspended = 0;
1937b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski
19381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
19391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Re-enable the console device after suspending.
19401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
19415933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai	if (uart_console(uport)) {
1942891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		/*
1943891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 * First try to use the console cflag setting.
1944891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 */
1945891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		memset(&termios, 0, sizeof(struct ktermios));
1946891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		termios.c_cflag = uport->cons->cflag;
1947891b9dd10764352926e1e107756aa229dfa2c210Jason Wang
1948891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		/*
1949891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 * If that's unset, use the tty termios setting.
1950891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		 */
1951891b9dd10764352926e1e107756aa229dfa2c210Jason Wang		if (port->tty && port->tty->termios && termios.c_cflag == 0)
1952891b9dd10764352926e1e107756aa229dfa2c210Jason Wang			termios = *(port->tty->termios);
1953891b9dd10764352926e1e107756aa229dfa2c210Jason Wang
195494abc56f4d90f289ea32a0a11d3577fcd8cb28fbNing Jiang		if (console_suspend_enabled)
195594abc56f4d90f289ea32a0a11d3577fcd8cb28fbNing Jiang			uart_change_pm(state, 0);
1956ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		uport->ops->set_termios(uport, &termios, NULL);
19575933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai		if (console_suspend_enabled)
19585933a161abcb8d83a2c145177f48027c3c0a8995Yin Kangkai			console_start(uport->cons);
19591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1961ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox	if (port->flags & ASYNC_SUSPENDED) {
1962ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		const struct uart_ops *ops = uport->ops;
1963ee31b337852ca8a65840702544ff5c64d37740f5Russell King		int ret;
19641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19659d778a69370cc1b643b13648df971c83ff5654efRussell King		uart_change_pm(state, 0);
1966ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		spin_lock_irq(&uport->lock);
1967ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		ops->set_mctrl(uport, 0);
1968ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		spin_unlock_irq(&uport->lock);
19694547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec		if (console_suspend_enabled || !uart_console(uport)) {
1970192251352f912bccfb942ea35801d2357f11f592Alan Cox			/* Protected by port mutex for now */
1971192251352f912bccfb942ea35801d2357f11f592Alan Cox			struct tty_struct *tty = port->tty;
19724547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			ret = ops->startup(uport);
19734547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			if (ret == 0) {
1974192251352f912bccfb942ea35801d2357f11f592Alan Cox				if (tty)
1975192251352f912bccfb942ea35801d2357f11f592Alan Cox					uart_change_speed(tty, state, NULL);
19764547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				spin_lock_irq(&uport->lock);
19774547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				ops->set_mctrl(uport, uport->mctrl);
19784547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				ops->start_tx(uport);
19794547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				spin_unlock_irq(&uport->lock);
19804547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				set_bit(ASYNCB_INITIALIZED, &port->flags);
19814547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			} else {
19824547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				/*
19834547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * Failed to resume - maybe hardware went away?
19844547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * Clear the "initialized" flag so we won't try
19854547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 * to call the low level drivers shutdown method.
19864547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec				 */
1987192251352f912bccfb942ea35801d2357f11f592Alan Cox				uart_shutdown(tty, state);
19884547be7809a3b775ce750ec7f8b5748954741523Stanislav Brabec			}
1989ee31b337852ca8a65840702544ff5c64d37740f5Russell King		}
1990a6b93a908508810c5d51dd9b390283345af6f2d9Russell King
1991ccce6debb62d94964e3878f978a56b0f3e32d94fAlan Cox		clear_bit(ASYNCB_SUSPENDED, &port->flags);
19921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1994a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
19951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
19971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void
20001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_report_port(struct uart_driver *drv, struct uart_port *port)
20011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
200230b7a3bc133c5b4a723163be35157ed709fca91cRussell King	char address[64];
200330b7a3bc133c5b4a723163be35157ed709fca91cRussell King
20041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (port->iotype) {
20051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_PORT:
20069bde10a4b8c54804236d3d6b4b75e98825a921e7Andrew Morton		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
20071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_HUB6:
200930b7a3bc133c5b4a723163be35157ed709fca91cRussell King		snprintf(address, sizeof(address),
20109bde10a4b8c54804236d3d6b4b75e98825a921e7Andrew Morton			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
20111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM:
20131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM32:
201421c614a7899046ab108b3d327d76c33443a8ebf2Pantelis Antoniou	case UPIO_AU:
20153be91ec7388bae3cf1bfb4febcee5ab6c65f409fZang Roy-r	case UPIO_TSI:
201630b7a3bc133c5b4a723163be35157ed709fca91cRussell King		snprintf(address, sizeof(address),
20174f640efb3170dbcf99a37a3cc99060647b95428cJosh Boyer			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
201830b7a3bc133c5b4a723163be35157ed709fca91cRussell King		break;
201930b7a3bc133c5b4a723163be35157ed709fca91cRussell King	default:
202030b7a3bc133c5b4a723163be35157ed709fca91cRussell King		strlcpy(address, "*unknown*", sizeof(address));
20211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
20221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
202330b7a3bc133c5b4a723163be35157ed709fca91cRussell King
20240cf669d5c5d08eb827df9867429df21cf030eba6Russell King	printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
20254bfe090b0a29258eeeb026a09a96cf5b5838ac63Kay Sievers	       port->dev ? dev_name(port->dev) : "",
20260cf669d5c5d08eb827df9867429df21cf030eba6Russell King	       port->dev ? ": " : "",
20278440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       drv->dev_name,
20288440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       drv->tty_driver->name_base + port->line,
20298440838bc5337243917f13bc14ea2445da5e0197David S. Miller	       address, port->irq, uart_type(port));
20301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
20331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuart_configure_port(struct uart_driver *drv, struct uart_state *state,
20341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    struct uart_port *port)
20351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int flags;
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If there isn't a port here, don't do anything further.
20401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!port->iobase && !port->mapbase && !port->membase)
20421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
20431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Now do the auto configuration stuff.  Note that config_port
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * is expected to claim the resources and map the port for us.
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20488e23fcc89c8091790903927449f8efb9b4e23960David Daney	flags = 0;
20491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->flags & UPF_AUTO_IRQ)
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		flags |= UART_CONFIG_IRQ;
20511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->flags & UPF_BOOT_AUTOCONF) {
20528e23fcc89c8091790903927449f8efb9b4e23960David Daney		if (!(port->flags & UPF_FIXED_TYPE)) {
20538e23fcc89c8091790903927449f8efb9b4e23960David Daney			port->type = PORT_UNKNOWN;
20548e23fcc89c8091790903927449f8efb9b4e23960David Daney			flags |= UART_CONFIG_TYPE;
20558e23fcc89c8091790903927449f8efb9b4e23960David Daney		}
20561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->ops->config_port(port, flags);
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->type != PORT_UNKNOWN) {
20601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long flags;
20611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		uart_report_port(drv, port);
20631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20643689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		/* Power up port for set_mctrl() */
20653689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis		uart_change_pm(state, 0);
20663689a0ec60bc8f56cc372c1dfa0d89dab48f7c9cGeorge G. Davis
20671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
20681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Ensure that the modem control lines are de-activated.
2069c3e4642be734ce3d2c7398246d8cbced3a039f54Yinghai Lu		 * keep the DTR setting that is set in uart_set_options()
20701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * We probably don't need a spinlock around this, but
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
20721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(&port->lock, flags);
2073c3e4642be734ce3d2c7398246d8cbced3a039f54Yinghai Lu		port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
20741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(&port->lock, flags);
20751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
207797d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * If this driver supports console, and it hasn't been
207897d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * successfully registered yet, try to re-register it.
207997d97224ff361e08777fb33e0fd193ca877dac28Russell King		 * It may be that the port was not available.
208097d97224ff361e08777fb33e0fd193ca877dac28Russell King		 */
208197d97224ff361e08777fb33e0fd193ca877dac28Russell King		if (port->cons && !(port->cons->flags & CON_ENABLED))
208297d97224ff361e08777fb33e0fd193ca877dac28Russell King			register_console(port->cons);
208397d97224ff361e08777fb33e0fd193ca877dac28Russell King
208497d97224ff361e08777fb33e0fd193ca877dac28Russell King		/*
20851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Power down all ports by default, except the
20861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * console if we have one.
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
20881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!uart_console(port))
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			uart_change_pm(state, 3);
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2093f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#ifdef CONFIG_CONSOLE_POLL
2094f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2095f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic int uart_poll_init(struct tty_driver *driver, int line, char *options)
2096f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2097f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2098f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2099f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2100f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int baud = 9600;
2101f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int bits = 8;
2102f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int parity = 'n';
2103f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	int flow = 'n';
2104f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2105ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2106f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2107f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2108ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2109f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2110f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2111f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2112f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	if (options) {
2113f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		uart_parse_options(options, &baud, &parity, &bits, &flow);
2114f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return uart_set_options(port, NULL, baud, parity, bits, flow);
2115f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	}
2116f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2117f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	return 0;
2118f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2119f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2120f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic int uart_poll_get_char(struct tty_driver *driver, int line)
2121f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2122f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2123f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2124f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2125f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2126ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2127f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return -1;
2128f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2129ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2130f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	return port->ops->poll_get_char(port);
2131f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2132f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2133f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wesselstatic void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2134f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel{
2135f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_driver *drv = driver->driver_state;
2136f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_state *state = drv->state + line;
2137f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	struct uart_port *port;
2138f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2139ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (!state || !state->uart_port)
2140f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel		return;
2141f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2142ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	port = state->uart_port;
2143f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	port->ops->poll_put_char(port, ch);
2144f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel}
2145f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#endif
2146f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel
2147b68e31d0ebbcc909d1941f9f230c9d062a3a13d3Jeff Dikestatic const struct tty_operations uart_ops = {
21481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.open		= uart_open,
21491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.close		= uart_close,
21501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write		= uart_write,
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.put_char	= uart_put_char,
21521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flush_chars	= uart_flush_chars,
21531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_room	= uart_write_room,
21541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.chars_in_buffer= uart_chars_in_buffer,
21551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flush_buffer	= uart_flush_buffer,
21561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ioctl		= uart_ioctl,
21571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.throttle	= uart_throttle,
21581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.unthrottle	= uart_unthrottle,
21591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.send_xchar	= uart_send_xchar,
21601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.set_termios	= uart_set_termios,
216164e9159f5d2c4edf5fa6425031e556f8fddaf7e6Alan Cox	.set_ldisc	= uart_set_ldisc,
21621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.stop		= uart_stop,
21631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.start		= uart_start,
21641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.hangup		= uart_hangup,
21651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.break_ctl	= uart_break_ctl,
21661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.wait_until_sent= uart_wait_until_sent,
21671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PROC_FS
2168d196a949ba0fb85121c0dc0720b13380d802dbd6Alexey Dobriyan	.proc_fops	= &uart_proc_fops,
21691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
21701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmget	= uart_tiocmget,
21711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmset	= uart_tiocmset,
2172d281da7ff6f70efca0553c288bb883e8605b3862Alan Cox	.get_icount	= uart_get_icount,
2173f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#ifdef CONFIG_CONSOLE_POLL
2174f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_init	= uart_poll_init,
2175f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_get_char	= uart_poll_get_char,
2176f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel	.poll_put_char	= uart_poll_put_char,
2177f2d937f3bf00665ccf048b3b6616ef95859b0945Jason Wessel#endif
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2180de0c8cb314cc737c47a00de33cd6246accf94192Alan Coxstatic const struct tty_port_operations uart_port_ops = {
2181de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	.carrier_raised = uart_carrier_raised,
2182de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox	.dtr_rts	= uart_dtr_rts,
2183de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox};
2184de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_register_driver - register a driver with the uart core layer
21871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: low level driver structure
21881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
21891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Register a uart driver with the core driver.  We in turn register
21901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	with the tty layer, and initialise the core driver per-port state.
21911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
21921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	We have a proc file in /proc/tty/driver which is named after the
21931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	normal driver.
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
21951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	drv->port should be NULL, and the per-port structures should be
21961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	registered using uart_add_one_port after this call has succeeded.
21971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
21981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint uart_register_driver(struct uart_driver *drv)
21991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22009e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	struct tty_driver *normal;
22011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, retval;
22021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(drv->state);
22041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Maybe we should be using a slab cache for this, especially if
22071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * we have a large number of ports to handle.
22081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22098f31bb39ec2a5622974666c72257e74c22492602Burman Yan	drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
22101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!drv->state)
22111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
22121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22139e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	normal = alloc_tty_driver(drv->nr);
22141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!normal)
22159e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa		goto out_kfree;
22161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	drv->tty_driver = normal;
22181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->owner		= drv->owner;
22201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->driver_name	= drv->driver_name;
22211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->name		= drv->dev_name;
22221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->major		= drv->major;
22231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->minor_start	= drv->minor;
22241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->type		= TTY_DRIVER_TYPE_SERIAL;
22251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->subtype		= SERIAL_TYPE_NORMAL;
22261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->init_termios	= tty_std_termios;
22271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2228606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2229331b831983f9d706f4a40d08a996d5c2c7a6ea7bGreg Kroah-Hartman	normal->flags		= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
22301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	normal->driver_state    = drv;
22311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_set_operations(normal, &uart_ops);
22321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Initialise the UART state(s).
22351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < drv->nr; i++) {
22371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct uart_state *state = drv->state + i;
2238a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		struct tty_port *port = &state->port;
22391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2240a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		tty_port_init(port);
2241de0c8cb314cc737c47a00de33cd6246accf94192Alan Cox		port->ops = &uart_port_ops;
22424cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby		port->close_delay     = HZ / 2;	/* .5 seconds */
22434cb0fbfdc81f29a414583bd05a9e324f8f19984dJiri Slaby		port->closing_wait    = 30 * HZ;/* 30 seconds */
22441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
22451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = tty_register_driver(normal);
22479e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	if (retval >= 0)
22489e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa		return retval;
22499e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa
22509e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	put_tty_driver(normal);
22519e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosaout_kfree:
22529e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	kfree(drv->state);
22539e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosaout:
22549e845abfc8a8973373821aa05302794fd254514bAndré Goddard Rosa	return -ENOMEM;
22551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
22581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_unregister_driver - remove a driver from the uart core layer
22591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: low level driver structure
22601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Remove all references to a driver from the core driver.  The low
22621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	level driver must have removed all its ports via the
22631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_remove_one_port() if it registered them with uart_add_one_port().
22641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	(ie, drv->port == NULL)
22651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
22661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid uart_unregister_driver(struct uart_driver *drv)
22671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct tty_driver *p = drv->tty_driver;
22691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_unregister_driver(p);
22701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_tty_driver(p);
22711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(drv->state);
22721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	drv->tty_driver = NULL;
22731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct tty_driver *uart_console_device(struct console *co, int *index)
22761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_driver *p = co->data;
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*index = co->index;
22791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p->tty_driver;
22801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
22831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_add_one_port - attach a driver-defined port structure
22841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: pointer to the uart low level driver structure for this port
22851b9894f342a39601bb0420b7b8c7e445670c1b51Randy Dunlap *	@uport: uart port structure to use for this port.
22861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This allows the driver to register its own uart_port structure
22881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	with the core driver.  The main purpose is to allow the low
22891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	level uart drivers to expand uart_port, rather than having yet
22901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	more levels of structures.
22911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2292a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Coxint uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
22931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct uart_state *state;
2295a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port;
22961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
2297b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	struct device *tty_dev;
22981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(in_interrupt());
23001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2301a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->line >= drv->nr)
23021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
23031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2304a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	state = drv->state + uport->line;
2305a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	port = &state->port;
23061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2307f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_lock(&port_mutex);
2308a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
2309ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	if (state->uart_port) {
23101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EINVAL;
23111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
23121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
23131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2314a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	state->uart_port = uport;
231597d97224ff361e08777fb33e0fd193ca877dac28Russell King	state->pm_state = -1;
23161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2317a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->cons = drv->cons;
2318a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->state = state;
23191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2320976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	/*
2321976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * If this port is a console, then the spinlock is already
2322976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 * initialised.
2323976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King	 */
2324a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2325a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		spin_lock_init(&uport->lock);
2326a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		lockdep_set_class(&uport->lock, &port_lock_key);
232713e83599d282ddfd544600df9db5ab343ac4662fIngo Molnar	}
2328976ecd12b8144d066a23fe97c6fbfc1ac8470af7Russell King
2329a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uart_configure_port(drv, state, uport);
23301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
23321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Register the port whether it's detected or not.  This allows
23331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * setserial to be used to alter this ports parameters.
23341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2335a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2336b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	if (likely(!IS_ERR(tty_dev))) {
233774081f8667d73ad59961cf63be5f0e9d6a87c8a3Alan Stern		device_init_wakeup(tty_dev, 1);
2338b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		device_set_wakeup_enable(tty_dev, 0);
2339b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski	} else
2340b3b708fa2780cd2b5d8266a8f0c3a1cab364d4d2Guennadi Liakhovetski		printk(KERN_ERR "Cannot register tty device on line %d\n",
2341a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		       uport->line);
23421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
234468ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Ensure UPF_DEAD is not set.
234568ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2346a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->flags &= ~UPF_DEAD;
234768ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
23481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds out:
2349a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
2350f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_unlock(&port_mutex);
23511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
23531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
23561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	uart_remove_one_port - detach a driver defined port structure
23571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	@drv: pointer to the uart low level driver structure for this port
23581b9894f342a39601bb0420b7b8c7e445670c1b51Randy Dunlap *	@uport: uart port structure for this port
23591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This unhooks (and hangs up) the specified port structure from the
23611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	core driver.  No further calls will be made to the low-level code
23621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	for this port.
23631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2364a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Coxint uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
23651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2366a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct uart_state *state = drv->state + uport->line;
2367a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	struct tty_port *port = &state->port;
23681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(in_interrupt());
23701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2371a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (state->uart_port != uport)
23721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2373a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox			state->uart_port, uport);
23741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2375f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_lock(&port_mutex);
23761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
237868ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Mark the port "dead" - this prevents any opens from
237968ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * succeeding while we shut down the port.
238068ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2381a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_lock(&port->mutex);
2382a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->flags |= UPF_DEAD;
2383a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	mutex_unlock(&port->mutex);
238468ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
238568ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
2386aa4148cfc7b3b93eeaf755a7d14f10afaffe9a96Greg Kroah-Hartman	 * Remove the devices from the tty layer
23871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2388a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	tty_unregister_device(drv->tty_driver, uport->line);
23891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2390a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (port->tty)
2391a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		tty_vhangup(port->tty);
239268ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
239368ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
239468ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Free the port IO and memory resources, if any.
239568ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2396a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	if (uport->type != PORT_UNKNOWN)
2397a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox		uport->ops->release_port(uport);
239868ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
239968ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	/*
240068ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 * Indicate that there isn't a port here anymore.
240168ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King	 */
2402a2bceae065ed8c4f552b35c4dde4cc2db05ce9e3Alan Cox	uport->type = PORT_UNKNOWN;
240368ac64cd3fd89fdaa091701f6ab98a9065e9b1b5Russell King
2404ebd2c8f6d2ec4012c267ecb95e72a57b8355a705Alan Cox	state->uart_port = NULL;
2405f392ecfa12de9a2baf72789b00557bac040d6171Arjan van de Ven	mutex_unlock(&port_mutex);
24061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
24081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
24091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
24111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	Are the two ports equivalent?
24121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
24131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint uart_match_port(struct uart_port *port1, struct uart_port *port2)
24141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
24151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port1->iotype != port2->iotype)
24161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
24171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (port1->iotype) {
24191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_PORT:
24201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (port1->iobase == port2->iobase);
24211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_HUB6:
24221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (port1->iobase == port2->iobase) &&
24231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       (port1->hub6   == port2->hub6);
24241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case UPIO_MEM:
2425d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_MEM32:
2426d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_AU:
2427d21b55d30b46c0b43821a2980e4998965fa37e33Sergei Shtylyov	case UPIO_TSI:
24281624f003349b49050f42c7d9f5407dfc05efb912Benjamin Herrenschmidt		return (port1->mapbase == port2->mapbase);
24291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
24301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
24311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
24321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_match_port);
24331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2434027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby/**
2435027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	uart_handle_dcd_change - handle a change of carrier detect state
2436027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@uport: uart_port structure for the open port
2437027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@status: new carrier detect status, nonzero if active
2438027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby */
2439027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2440027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2441027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct uart_state *state = uport->state;
2442027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_port *port = &state->port;
2443027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_ldisc *ld = tty_ldisc_ref(port->tty);
2444027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct pps_event_time ts;
2445027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2446027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld && ld->ops->dcd_change)
2447027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		pps_get_ts(&ts);
2448027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2449027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	uport->icount.dcd++;
2450027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby#ifdef CONFIG_HARD_PPS
2451027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if ((uport->flags & UPF_HARDPPS_CD) && status)
2452027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		hardpps();
2453027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby#endif
2454027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2455027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (port->flags & ASYNC_CHECK_CD) {
2456027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		if (status)
2457027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			wake_up_interruptible(&port->open_wait);
2458027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		else if (port->tty)
2459027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			tty_hangup(port->tty);
2460027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	}
2461027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2462027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld && ld->ops->dcd_change)
2463027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		ld->ops->dcd_change(port->tty, status, &ts);
2464027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (ld)
2465027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_ldisc_deref(ld);
2466027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2467027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2468027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2469027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby/**
2470027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	uart_handle_cts_change - handle a change of clear-to-send state
2471027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@uport: uart_port structure for the open port
2472027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby *	@status: new clear to send status, nonzero if active
2473027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby */
2474027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2475027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2476027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_port *port = &uport->state->port;
2477027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_struct *tty = port->tty;
2478027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2479027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	uport->icount.cts++;
2480027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2481027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (port->flags & ASYNC_CTS_FLOW) {
2482027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		if (tty->hw_stopped) {
2483027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			if (status) {
2484027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				tty->hw_stopped = 0;
2485027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uport->ops->start_tx(uport);
2486027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uart_write_wakeup(uport);
2487027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			}
2488027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		} else {
2489027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			if (!status) {
2490027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				tty->hw_stopped = 1;
2491027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby				uport->ops->stop_tx(uport);
2492027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby			}
2493027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		}
2494027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	}
2495027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2496027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_handle_cts_change);
2497027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2498027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slabyvoid uart_insert_char(struct uart_port *port, unsigned int status,
2499027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		 unsigned int overrun, unsigned int ch, unsigned int flag)
2500027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby{
2501027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	struct tty_struct *tty = port->state->port.tty;
2502027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2503027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if ((status & port->ignore_status_mask & ~overrun) == 0)
2504027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_insert_flip_char(tty, ch, flag);
2505027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
2506027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	/*
2507027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 * Overrun is special.  Since it's reported immediately,
2508027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 * it doesn't affect the current character.
2509027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	 */
2510027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby	if (status & ~port->ignore_status_mask & overrun)
2511027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2512027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby}
2513027d7dacf73273dbe07a75b2ef5579616f17272cJiri SlabyEXPORT_SYMBOL_GPL(uart_insert_char);
2514027d7dacf73273dbe07a75b2ef5579616f17272cJiri Slaby
25151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_write_wakeup);
25161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_register_driver);
25171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_unregister_driver);
25181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_suspend_port);
25191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_resume_port);
25201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_add_one_port);
25211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(uart_remove_one_port);
25221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("Serial driver core");
25241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
2525