xref: /openbmc/linux/drivers/tty/serial/serial_core.c (revision ba47f97a)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver core for serial ports
3ab4382d2SGreg Kroah-Hartman  *
4ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5ab4382d2SGreg Kroah-Hartman  *
6ab4382d2SGreg Kroah-Hartman  *  Copyright 1999 ARM Limited
7ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
8ab4382d2SGreg Kroah-Hartman  *
9ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
10ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
11ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
12ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
13ab4382d2SGreg Kroah-Hartman  *
14ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
15ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
18ab4382d2SGreg Kroah-Hartman  *
19ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
20ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
21ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22ab4382d2SGreg Kroah-Hartman  */
23ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
25027d7dacSJiri Slaby #include <linux/tty_flip.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
29a208ffd2SGrant Likely #include <linux/of.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/device.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
34ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
36ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h>
37ab4382d2SGreg Kroah-Hartman 
38ab4382d2SGreg Kroah-Hartman #include <asm/irq.h>
39ab4382d2SGreg Kroah-Hartman #include <asm/uaccess.h>
40ab4382d2SGreg Kroah-Hartman 
41ab4382d2SGreg Kroah-Hartman /*
42ab4382d2SGreg Kroah-Hartman  * This is used to lock changes in serial line configuration.
43ab4382d2SGreg Kroah-Hartman  */
44ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex);
45ab4382d2SGreg Kroah-Hartman 
46ab4382d2SGreg Kroah-Hartman /*
47ab4382d2SGreg Kroah-Hartman  * lockdep: port->lock is initialized in two places, but we
48ab4382d2SGreg Kroah-Hartman  *          want only one lock-class:
49ab4382d2SGreg Kroah-Hartman  */
50ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key;
51ab4382d2SGreg Kroah-Hartman 
52ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
55ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios);
561f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
576f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
586f538fe3SLinus Walleij 			   enum uart_pm_state pm_state);
59ab4382d2SGreg Kroah-Hartman 
60b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port);
61b922e19dSJiri Slaby 
62299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport)
63299245a1SPeter Hurley {
64d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_DCD_ENABLE);
65299245a1SPeter Hurley }
66299245a1SPeter Hurley 
67ab4382d2SGreg Kroah-Hartman /*
68ab4382d2SGreg Kroah-Hartman  * This routine is used by the interrupt handler to schedule processing in
69ab4382d2SGreg Kroah-Hartman  * the software interrupt portion of the driver.
70ab4382d2SGreg Kroah-Hartman  */
71ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port)
72ab4382d2SGreg Kroah-Hartman {
73ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = port->state;
74ab4382d2SGreg Kroah-Hartman 	/*
75ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
76ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
77ab4382d2SGreg Kroah-Hartman 	 */
78ab4382d2SGreg Kroah-Hartman 	BUG_ON(!state);
796a3e492bSJiri Slaby 	tty_wakeup(state->port.tty);
80ab4382d2SGreg Kroah-Hartman }
81ab4382d2SGreg Kroah-Hartman 
82ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty)
83ab4382d2SGreg Kroah-Hartman {
84ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
85ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
86ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
89ab4382d2SGreg Kroah-Hartman 	port->ops->stop_tx(port);
90ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
91ab4382d2SGreg Kroah-Hartman }
92ab4382d2SGreg Kroah-Hartman 
93ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty)
94ab4382d2SGreg Kroah-Hartman {
95ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
96ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
97ab4382d2SGreg Kroah-Hartman 
98d01f4d18SPeter Hurley 	if (!uart_tx_stopped(port))
99ab4382d2SGreg Kroah-Hartman 		port->ops->start_tx(port);
100ab4382d2SGreg Kroah-Hartman }
101ab4382d2SGreg Kroah-Hartman 
102ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty)
103ab4382d2SGreg Kroah-Hartman {
104ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
105ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
106ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
107ab4382d2SGreg Kroah-Hartman 
108ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
109ab4382d2SGreg Kroah-Hartman 	__uart_start(tty);
110ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
111ab4382d2SGreg Kroah-Hartman }
112ab4382d2SGreg Kroah-Hartman 
113f4581cabSDenys Vlasenko static void
114ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
115ab4382d2SGreg Kroah-Hartman {
116ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
117ab4382d2SGreg Kroah-Hartman 	unsigned int old;
118ab4382d2SGreg Kroah-Hartman 
119ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
120ab4382d2SGreg Kroah-Hartman 	old = port->mctrl;
121ab4382d2SGreg Kroah-Hartman 	port->mctrl = (old & ~clear) | set;
122ab4382d2SGreg Kroah-Hartman 	if (old != port->mctrl)
123ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl);
124ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
125ab4382d2SGreg Kroah-Hartman }
126ab4382d2SGreg Kroah-Hartman 
127ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
128ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
129ab4382d2SGreg Kroah-Hartman 
130ab4382d2SGreg Kroah-Hartman /*
131ab4382d2SGreg Kroah-Hartman  * Startup the port.  This will be called once per open.  All calls
132ab4382d2SGreg Kroah-Hartman  * will be serialised by the per-port mutex.
133ab4382d2SGreg Kroah-Hartman  */
134c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
135c0d92be6SJiri Slaby 		int init_hw)
136ab4382d2SGreg Kroah-Hartman {
137ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
138ab4382d2SGreg Kroah-Hartman 	unsigned long page;
139ab4382d2SGreg Kroah-Hartman 	int retval = 0;
140ab4382d2SGreg Kroah-Hartman 
141ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
142c0d92be6SJiri Slaby 		return 1;
143ab4382d2SGreg Kroah-Hartman 
144ab4382d2SGreg Kroah-Hartman 	/*
1457deb39edSThomas Pfaff 	 * Make sure the device is in D0 state.
1467deb39edSThomas Pfaff 	 */
1477deb39edSThomas Pfaff 	uart_change_pm(state, UART_PM_STATE_ON);
1487deb39edSThomas Pfaff 
1497deb39edSThomas Pfaff 	/*
150ab4382d2SGreg Kroah-Hartman 	 * Initialise and allocate the transmit and temporary
151ab4382d2SGreg Kroah-Hartman 	 * buffer.
152ab4382d2SGreg Kroah-Hartman 	 */
153ab4382d2SGreg Kroah-Hartman 	if (!state->xmit.buf) {
154ab4382d2SGreg Kroah-Hartman 		/* This is protected by the per port mutex */
155ab4382d2SGreg Kroah-Hartman 		page = get_zeroed_page(GFP_KERNEL);
156ab4382d2SGreg Kroah-Hartman 		if (!page)
157ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
158ab4382d2SGreg Kroah-Hartman 
159ab4382d2SGreg Kroah-Hartman 		state->xmit.buf = (unsigned char *) page;
160ab4382d2SGreg Kroah-Hartman 		uart_circ_clear(&state->xmit);
161ab4382d2SGreg Kroah-Hartman 	}
162ab4382d2SGreg Kroah-Hartman 
163ab4382d2SGreg Kroah-Hartman 	retval = uport->ops->startup(uport);
164ab4382d2SGreg Kroah-Hartman 	if (retval == 0) {
165c7d7abffSJiri Slaby 		if (uart_console(uport) && uport->cons->cflag) {
166adc8d746SAlan Cox 			tty->termios.c_cflag = uport->cons->cflag;
167c7d7abffSJiri Slaby 			uport->cons->cflag = 0;
168c7d7abffSJiri Slaby 		}
169ab4382d2SGreg Kroah-Hartman 		/*
170ab4382d2SGreg Kroah-Hartman 		 * Initialise the hardware port settings.
171ab4382d2SGreg Kroah-Hartman 		 */
172ab4382d2SGreg Kroah-Hartman 		uart_change_speed(tty, state, NULL);
173ab4382d2SGreg Kroah-Hartman 
174ab4382d2SGreg Kroah-Hartman 		/*
175ab4382d2SGreg Kroah-Hartman 		 * Setup the RTS and DTR signals once the
176ab4382d2SGreg Kroah-Hartman 		 * port is open and ready to respond.
177ab4382d2SGreg Kroah-Hartman 		 */
1789db276f8SPeter Hurley 		if (init_hw && C_BAUD(tty))
179ab4382d2SGreg Kroah-Hartman 			uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
180ab4382d2SGreg Kroah-Hartman 	}
181ab4382d2SGreg Kroah-Hartman 
1820055197eSJiri Slaby 	/*
1830055197eSJiri Slaby 	 * This is to allow setserial on this port. People may want to set
1840055197eSJiri Slaby 	 * port/irq/type and then reconfigure the port properly if it failed
1850055197eSJiri Slaby 	 * now.
1860055197eSJiri Slaby 	 */
187ab4382d2SGreg Kroah-Hartman 	if (retval && capable(CAP_SYS_ADMIN))
188c0d92be6SJiri Slaby 		return 1;
189c0d92be6SJiri Slaby 
190c0d92be6SJiri Slaby 	return retval;
191c0d92be6SJiri Slaby }
192c0d92be6SJiri Slaby 
193c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state,
194c0d92be6SJiri Slaby 		int init_hw)
195c0d92be6SJiri Slaby {
196c0d92be6SJiri Slaby 	struct tty_port *port = &state->port;
197c0d92be6SJiri Slaby 	int retval;
198c0d92be6SJiri Slaby 
199c0d92be6SJiri Slaby 	if (port->flags & ASYNC_INITIALIZED)
200c0d92be6SJiri Slaby 		return 0;
201c0d92be6SJiri Slaby 
202c0d92be6SJiri Slaby 	/*
203c0d92be6SJiri Slaby 	 * Set the TTY IO error marker - we will only clear this
204c0d92be6SJiri Slaby 	 * once we have successfully opened the port.
205c0d92be6SJiri Slaby 	 */
206c0d92be6SJiri Slaby 	set_bit(TTY_IO_ERROR, &tty->flags);
207c0d92be6SJiri Slaby 
208c0d92be6SJiri Slaby 	retval = uart_port_startup(tty, state, init_hw);
209c0d92be6SJiri Slaby 	if (!retval) {
210c0d92be6SJiri Slaby 		set_bit(ASYNCB_INITIALIZED, &port->flags);
211c0d92be6SJiri Slaby 		clear_bit(TTY_IO_ERROR, &tty->flags);
212c0d92be6SJiri Slaby 	} else if (retval > 0)
213ab4382d2SGreg Kroah-Hartman 		retval = 0;
214ab4382d2SGreg Kroah-Hartman 
215ab4382d2SGreg Kroah-Hartman 	return retval;
216ab4382d2SGreg Kroah-Hartman }
217ab4382d2SGreg Kroah-Hartman 
218ab4382d2SGreg Kroah-Hartman /*
219ab4382d2SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
220ab4382d2SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.  Calls to
221ab4382d2SGreg Kroah-Hartman  * uart_shutdown are serialised by the per-port semaphore.
222ab4382d2SGreg Kroah-Hartman  */
223ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
224ab4382d2SGreg Kroah-Hartman {
225ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
226ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
227ab4382d2SGreg Kroah-Hartman 
228ab4382d2SGreg Kroah-Hartman 	/*
229ab4382d2SGreg Kroah-Hartman 	 * Set the TTY IO error marker
230ab4382d2SGreg Kroah-Hartman 	 */
231ab4382d2SGreg Kroah-Hartman 	if (tty)
232ab4382d2SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
233ab4382d2SGreg Kroah-Hartman 
234ab4382d2SGreg Kroah-Hartman 	if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
235ab4382d2SGreg Kroah-Hartman 		/*
236ab4382d2SGreg Kroah-Hartman 		 * Turn off DTR and RTS early.
237ab4382d2SGreg Kroah-Hartman 		 */
238ae84db96SPeter Hurley 		if (uart_console(uport) && tty)
239ae84db96SPeter Hurley 			uport->cons->cflag = tty->termios.c_cflag;
240ae84db96SPeter Hurley 
2419db276f8SPeter Hurley 		if (!tty || C_HUPCL(tty))
242ab4382d2SGreg Kroah-Hartman 			uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
243ab4382d2SGreg Kroah-Hartman 
244b922e19dSJiri Slaby 		uart_port_shutdown(port);
245ab4382d2SGreg Kroah-Hartman 	}
246ab4382d2SGreg Kroah-Hartman 
247ab4382d2SGreg Kroah-Hartman 	/*
248d208a3bfSDoug Anderson 	 * It's possible for shutdown to be called after suspend if we get
249d208a3bfSDoug Anderson 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
250d208a3bfSDoug Anderson 	 * we don't try to resume a port that has been shutdown.
251ab4382d2SGreg Kroah-Hartman 	 */
252d208a3bfSDoug Anderson 	clear_bit(ASYNCB_SUSPENDED, &port->flags);
253ab4382d2SGreg Kroah-Hartman 
254ab4382d2SGreg Kroah-Hartman 	/*
255ab4382d2SGreg Kroah-Hartman 	 * Free the transmit buffer page.
256ab4382d2SGreg Kroah-Hartman 	 */
257ab4382d2SGreg Kroah-Hartman 	if (state->xmit.buf) {
258ab4382d2SGreg Kroah-Hartman 		free_page((unsigned long)state->xmit.buf);
259ab4382d2SGreg Kroah-Hartman 		state->xmit.buf = NULL;
260ab4382d2SGreg Kroah-Hartman 	}
261ab4382d2SGreg Kroah-Hartman }
262ab4382d2SGreg Kroah-Hartman 
263ab4382d2SGreg Kroah-Hartman /**
264ab4382d2SGreg Kroah-Hartman  *	uart_update_timeout - update per-port FIFO timeout.
265ab4382d2SGreg Kroah-Hartman  *	@port:  uart_port structure describing the port
266ab4382d2SGreg Kroah-Hartman  *	@cflag: termios cflag value
267ab4382d2SGreg Kroah-Hartman  *	@baud:  speed of the port
268ab4382d2SGreg Kroah-Hartman  *
269ab4382d2SGreg Kroah-Hartman  *	Set the port FIFO timeout value.  The @cflag value should
270ab4382d2SGreg Kroah-Hartman  *	reflect the actual hardware settings.
271ab4382d2SGreg Kroah-Hartman  */
272ab4382d2SGreg Kroah-Hartman void
273ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag,
274ab4382d2SGreg Kroah-Hartman 		    unsigned int baud)
275ab4382d2SGreg Kroah-Hartman {
276ab4382d2SGreg Kroah-Hartman 	unsigned int bits;
277ab4382d2SGreg Kroah-Hartman 
278ab4382d2SGreg Kroah-Hartman 	/* byte size and parity */
279ab4382d2SGreg Kroah-Hartman 	switch (cflag & CSIZE) {
280ab4382d2SGreg Kroah-Hartman 	case CS5:
281ab4382d2SGreg Kroah-Hartman 		bits = 7;
282ab4382d2SGreg Kroah-Hartman 		break;
283ab4382d2SGreg Kroah-Hartman 	case CS6:
284ab4382d2SGreg Kroah-Hartman 		bits = 8;
285ab4382d2SGreg Kroah-Hartman 		break;
286ab4382d2SGreg Kroah-Hartman 	case CS7:
287ab4382d2SGreg Kroah-Hartman 		bits = 9;
288ab4382d2SGreg Kroah-Hartman 		break;
289ab4382d2SGreg Kroah-Hartman 	default:
290ab4382d2SGreg Kroah-Hartman 		bits = 10;
291ab4382d2SGreg Kroah-Hartman 		break; /* CS8 */
292ab4382d2SGreg Kroah-Hartman 	}
293ab4382d2SGreg Kroah-Hartman 
294ab4382d2SGreg Kroah-Hartman 	if (cflag & CSTOPB)
295ab4382d2SGreg Kroah-Hartman 		bits++;
296ab4382d2SGreg Kroah-Hartman 	if (cflag & PARENB)
297ab4382d2SGreg Kroah-Hartman 		bits++;
298ab4382d2SGreg Kroah-Hartman 
299ab4382d2SGreg Kroah-Hartman 	/*
300ab4382d2SGreg Kroah-Hartman 	 * The total number of bits to be transmitted in the fifo.
301ab4382d2SGreg Kroah-Hartman 	 */
302ab4382d2SGreg Kroah-Hartman 	bits = bits * port->fifosize;
303ab4382d2SGreg Kroah-Hartman 
304ab4382d2SGreg Kroah-Hartman 	/*
305ab4382d2SGreg Kroah-Hartman 	 * Figure the timeout to send the above number of bits.
306ab4382d2SGreg Kroah-Hartman 	 * Add .02 seconds of slop
307ab4382d2SGreg Kroah-Hartman 	 */
308ab4382d2SGreg Kroah-Hartman 	port->timeout = (HZ * bits) / baud + HZ/50;
309ab4382d2SGreg Kroah-Hartman }
310ab4382d2SGreg Kroah-Hartman 
311ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout);
312ab4382d2SGreg Kroah-Hartman 
313ab4382d2SGreg Kroah-Hartman /**
314ab4382d2SGreg Kroah-Hartman  *	uart_get_baud_rate - return baud rate for a particular port
315ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port in question.
316ab4382d2SGreg Kroah-Hartman  *	@termios: desired termios settings.
317ab4382d2SGreg Kroah-Hartman  *	@old: old termios (or NULL)
318ab4382d2SGreg Kroah-Hartman  *	@min: minimum acceptable baud rate
319ab4382d2SGreg Kroah-Hartman  *	@max: maximum acceptable baud rate
320ab4382d2SGreg Kroah-Hartman  *
321ab4382d2SGreg Kroah-Hartman  *	Decode the termios structure into a numeric baud rate,
322ab4382d2SGreg Kroah-Hartman  *	taking account of the magic 38400 baud rate (with spd_*
323ab4382d2SGreg Kroah-Hartman  *	flags), and mapping the %B0 rate to 9600 baud.
324ab4382d2SGreg Kroah-Hartman  *
325ab4382d2SGreg Kroah-Hartman  *	If the new baud rate is invalid, try the old termios setting.
326ab4382d2SGreg Kroah-Hartman  *	If it's still invalid, we try 9600 baud.
327ab4382d2SGreg Kroah-Hartman  *
328ab4382d2SGreg Kroah-Hartman  *	Update the @termios structure to reflect the baud rate
329ab4382d2SGreg Kroah-Hartman  *	we're actually going to be using. Don't do this for the case
330ab4382d2SGreg Kroah-Hartman  *	where B0 is requested ("hang up").
331ab4382d2SGreg Kroah-Hartman  */
332ab4382d2SGreg Kroah-Hartman unsigned int
333ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
334ab4382d2SGreg Kroah-Hartman 		   struct ktermios *old, unsigned int min, unsigned int max)
335ab4382d2SGreg Kroah-Hartman {
336f10a2233SJoakim Nordell 	unsigned int try;
337f10a2233SJoakim Nordell 	unsigned int baud;
338f10a2233SJoakim Nordell 	unsigned int altbaud;
339ab4382d2SGreg Kroah-Hartman 	int hung_up = 0;
340ab4382d2SGreg Kroah-Hartman 	upf_t flags = port->flags & UPF_SPD_MASK;
341ab4382d2SGreg Kroah-Hartman 
342f10a2233SJoakim Nordell 	switch (flags) {
343f10a2233SJoakim Nordell 	case UPF_SPD_HI:
344ab4382d2SGreg Kroah-Hartman 		altbaud = 57600;
345f10a2233SJoakim Nordell 		break;
346f10a2233SJoakim Nordell 	case UPF_SPD_VHI:
347ab4382d2SGreg Kroah-Hartman 		altbaud = 115200;
348f10a2233SJoakim Nordell 		break;
349f10a2233SJoakim Nordell 	case UPF_SPD_SHI:
350ab4382d2SGreg Kroah-Hartman 		altbaud = 230400;
351f10a2233SJoakim Nordell 		break;
352f10a2233SJoakim Nordell 	case UPF_SPD_WARP:
353ab4382d2SGreg Kroah-Hartman 		altbaud = 460800;
354f10a2233SJoakim Nordell 		break;
355f10a2233SJoakim Nordell 	default:
356f10a2233SJoakim Nordell 		altbaud = 38400;
357f10a2233SJoakim Nordell 		break;
358f10a2233SJoakim Nordell 	}
359ab4382d2SGreg Kroah-Hartman 
360ab4382d2SGreg Kroah-Hartman 	for (try = 0; try < 2; try++) {
361ab4382d2SGreg Kroah-Hartman 		baud = tty_termios_baud_rate(termios);
362ab4382d2SGreg Kroah-Hartman 
363ab4382d2SGreg Kroah-Hartman 		/*
364ab4382d2SGreg Kroah-Hartman 		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
365ab4382d2SGreg Kroah-Hartman 		 * Die! Die! Die!
366ab4382d2SGreg Kroah-Hartman 		 */
367547039ecSPeter Hurley 		if (try == 0 && baud == 38400)
368ab4382d2SGreg Kroah-Hartman 			baud = altbaud;
369ab4382d2SGreg Kroah-Hartman 
370ab4382d2SGreg Kroah-Hartman 		/*
371ab4382d2SGreg Kroah-Hartman 		 * Special case: B0 rate.
372ab4382d2SGreg Kroah-Hartman 		 */
373ab4382d2SGreg Kroah-Hartman 		if (baud == 0) {
374ab4382d2SGreg Kroah-Hartman 			hung_up = 1;
375ab4382d2SGreg Kroah-Hartman 			baud = 9600;
376ab4382d2SGreg Kroah-Hartman 		}
377ab4382d2SGreg Kroah-Hartman 
378ab4382d2SGreg Kroah-Hartman 		if (baud >= min && baud <= max)
379ab4382d2SGreg Kroah-Hartman 			return baud;
380ab4382d2SGreg Kroah-Hartman 
381ab4382d2SGreg Kroah-Hartman 		/*
382ab4382d2SGreg Kroah-Hartman 		 * Oops, the quotient was zero.  Try again with
383ab4382d2SGreg Kroah-Hartman 		 * the old baud rate if possible.
384ab4382d2SGreg Kroah-Hartman 		 */
385ab4382d2SGreg Kroah-Hartman 		termios->c_cflag &= ~CBAUD;
386ab4382d2SGreg Kroah-Hartman 		if (old) {
387ab4382d2SGreg Kroah-Hartman 			baud = tty_termios_baud_rate(old);
388ab4382d2SGreg Kroah-Hartman 			if (!hung_up)
389ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
390ab4382d2SGreg Kroah-Hartman 								baud, baud);
391ab4382d2SGreg Kroah-Hartman 			old = NULL;
392ab4382d2SGreg Kroah-Hartman 			continue;
393ab4382d2SGreg Kroah-Hartman 		}
394ab4382d2SGreg Kroah-Hartman 
395ab4382d2SGreg Kroah-Hartman 		/*
396ab4382d2SGreg Kroah-Hartman 		 * As a last resort, if the range cannot be met then clip to
397ab4382d2SGreg Kroah-Hartman 		 * the nearest chip supported rate.
398ab4382d2SGreg Kroah-Hartman 		 */
399ab4382d2SGreg Kroah-Hartman 		if (!hung_up) {
400ab4382d2SGreg Kroah-Hartman 			if (baud <= min)
401ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
402ab4382d2SGreg Kroah-Hartman 							min + 1, min + 1);
403ab4382d2SGreg Kroah-Hartman 			else
404ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
405ab4382d2SGreg Kroah-Hartman 							max - 1, max - 1);
406ab4382d2SGreg Kroah-Hartman 		}
407ab4382d2SGreg Kroah-Hartman 	}
408ab4382d2SGreg Kroah-Hartman 	/* Should never happen */
409ab4382d2SGreg Kroah-Hartman 	WARN_ON(1);
410ab4382d2SGreg Kroah-Hartman 	return 0;
411ab4382d2SGreg Kroah-Hartman }
412ab4382d2SGreg Kroah-Hartman 
413ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate);
414ab4382d2SGreg Kroah-Hartman 
415ab4382d2SGreg Kroah-Hartman /**
416ab4382d2SGreg Kroah-Hartman  *	uart_get_divisor - return uart clock divisor
417ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port.
418ab4382d2SGreg Kroah-Hartman  *	@baud: desired baud rate
419ab4382d2SGreg Kroah-Hartman  *
420ab4382d2SGreg Kroah-Hartman  *	Calculate the uart clock divisor for the port.
421ab4382d2SGreg Kroah-Hartman  */
422ab4382d2SGreg Kroah-Hartman unsigned int
423ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud)
424ab4382d2SGreg Kroah-Hartman {
425ab4382d2SGreg Kroah-Hartman 	unsigned int quot;
426ab4382d2SGreg Kroah-Hartman 
427ab4382d2SGreg Kroah-Hartman 	/*
428ab4382d2SGreg Kroah-Hartman 	 * Old custom speed handling.
429ab4382d2SGreg Kroah-Hartman 	 */
430ab4382d2SGreg Kroah-Hartman 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
431ab4382d2SGreg Kroah-Hartman 		quot = port->custom_divisor;
432ab4382d2SGreg Kroah-Hartman 	else
43397d24634SUwe Kleine-König 		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
434ab4382d2SGreg Kroah-Hartman 
435ab4382d2SGreg Kroah-Hartman 	return quot;
436ab4382d2SGreg Kroah-Hartman }
437ab4382d2SGreg Kroah-Hartman 
438ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor);
439ab4382d2SGreg Kroah-Hartman 
4407c8ab967SPeter Hurley /* Caller holds port mutex */
441ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
442ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios)
443ab4382d2SGreg Kroah-Hartman {
444ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
445ab4382d2SGreg Kroah-Hartman 	struct ktermios *termios;
446391f93f2SPeter Hurley 	int hw_stopped;
447ab4382d2SGreg Kroah-Hartman 
448ab4382d2SGreg Kroah-Hartman 	/*
449ab4382d2SGreg Kroah-Hartman 	 * If we have no tty, termios, or the port does not exist,
450ab4382d2SGreg Kroah-Hartman 	 * then we can't set the parameters for this port.
451ab4382d2SGreg Kroah-Hartman 	 */
452adc8d746SAlan Cox 	if (!tty || uport->type == PORT_UNKNOWN)
453ab4382d2SGreg Kroah-Hartman 		return;
454ab4382d2SGreg Kroah-Hartman 
455adc8d746SAlan Cox 	termios = &tty->termios;
456c18b55fdSPeter Hurley 	uport->ops->set_termios(uport, termios, old_termios);
457ab4382d2SGreg Kroah-Hartman 
458ab4382d2SGreg Kroah-Hartman 	/*
459299245a1SPeter Hurley 	 * Set modem status enables based on termios cflag
460ab4382d2SGreg Kroah-Hartman 	 */
461299245a1SPeter Hurley 	spin_lock_irq(&uport->lock);
462ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
463299245a1SPeter Hurley 		uport->status |= UPSTAT_CTS_ENABLE;
464ab4382d2SGreg Kroah-Hartman 	else
465299245a1SPeter Hurley 		uport->status &= ~UPSTAT_CTS_ENABLE;
466ab4382d2SGreg Kroah-Hartman 
467ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CLOCAL)
468299245a1SPeter Hurley 		uport->status &= ~UPSTAT_DCD_ENABLE;
469ab4382d2SGreg Kroah-Hartman 	else
470299245a1SPeter Hurley 		uport->status |= UPSTAT_DCD_ENABLE;
471391f93f2SPeter Hurley 
472391f93f2SPeter Hurley 	/* reset sw-assisted CTS flow control based on (possibly) new mode */
473391f93f2SPeter Hurley 	hw_stopped = uport->hw_stopped;
474391f93f2SPeter Hurley 	uport->hw_stopped = uart_softcts_mode(uport) &&
475391f93f2SPeter Hurley 				!(uport->ops->get_mctrl(uport) & TIOCM_CTS);
476391f93f2SPeter Hurley 	if (uport->hw_stopped) {
477391f93f2SPeter Hurley 		if (!hw_stopped)
478391f93f2SPeter Hurley 			uport->ops->stop_tx(uport);
479391f93f2SPeter Hurley 	} else {
480391f93f2SPeter Hurley 		if (hw_stopped)
481391f93f2SPeter Hurley 			__uart_start(tty);
482391f93f2SPeter Hurley 	}
483299245a1SPeter Hurley 	spin_unlock_irq(&uport->lock);
484ab4382d2SGreg Kroah-Hartman }
485ab4382d2SGreg Kroah-Hartman 
486f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c)
487ab4382d2SGreg Kroah-Hartman {
488f5291eccSPeter Hurley 	struct uart_state *state = tty->driver_data;
489f5291eccSPeter Hurley 	struct uart_port *port = state->uart_port;
490f5291eccSPeter Hurley 	struct circ_buf *circ;
491ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
492ab4382d2SGreg Kroah-Hartman 	int ret = 0;
493ab4382d2SGreg Kroah-Hartman 
494f5291eccSPeter Hurley 	circ = &state->xmit;
495ab4382d2SGreg Kroah-Hartman 	if (!circ->buf)
496ab4382d2SGreg Kroah-Hartman 		return 0;
497ab4382d2SGreg Kroah-Hartman 
498ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
499ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_free(circ) != 0) {
500ab4382d2SGreg Kroah-Hartman 		circ->buf[circ->head] = c;
501ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
502ab4382d2SGreg Kroah-Hartman 		ret = 1;
503ab4382d2SGreg Kroah-Hartman 	}
504ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
505ab4382d2SGreg Kroah-Hartman 	return ret;
506ab4382d2SGreg Kroah-Hartman }
507ab4382d2SGreg Kroah-Hartman 
508ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty)
509ab4382d2SGreg Kroah-Hartman {
510ab4382d2SGreg Kroah-Hartman 	uart_start(tty);
511ab4382d2SGreg Kroah-Hartman }
512ab4382d2SGreg Kroah-Hartman 
513ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty,
514ab4382d2SGreg Kroah-Hartman 					const unsigned char *buf, int count)
515ab4382d2SGreg Kroah-Hartman {
516ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
517ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
518ab4382d2SGreg Kroah-Hartman 	struct circ_buf *circ;
519ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
520ab4382d2SGreg Kroah-Hartman 	int c, ret = 0;
521ab4382d2SGreg Kroah-Hartman 
522ab4382d2SGreg Kroah-Hartman 	/*
523ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
524ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
525ab4382d2SGreg Kroah-Hartman 	 */
526ab4382d2SGreg Kroah-Hartman 	if (!state) {
527ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
528ab4382d2SGreg Kroah-Hartman 		return -EL3HLT;
529ab4382d2SGreg Kroah-Hartman 	}
530ab4382d2SGreg Kroah-Hartman 
531ab4382d2SGreg Kroah-Hartman 	port = state->uart_port;
532ab4382d2SGreg Kroah-Hartman 	circ = &state->xmit;
533ab4382d2SGreg Kroah-Hartman 
534ab4382d2SGreg Kroah-Hartman 	if (!circ->buf)
535ab4382d2SGreg Kroah-Hartman 		return 0;
536ab4382d2SGreg Kroah-Hartman 
537ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
538ab4382d2SGreg Kroah-Hartman 	while (1) {
539ab4382d2SGreg Kroah-Hartman 		c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
540ab4382d2SGreg Kroah-Hartman 		if (count < c)
541ab4382d2SGreg Kroah-Hartman 			c = count;
542ab4382d2SGreg Kroah-Hartman 		if (c <= 0)
543ab4382d2SGreg Kroah-Hartman 			break;
544ab4382d2SGreg Kroah-Hartman 		memcpy(circ->buf + circ->head, buf, c);
545ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
546ab4382d2SGreg Kroah-Hartman 		buf += c;
547ab4382d2SGreg Kroah-Hartman 		count -= c;
548ab4382d2SGreg Kroah-Hartman 		ret += c;
549ab4382d2SGreg Kroah-Hartman 	}
55064dbee31SPeter Hurley 
55164dbee31SPeter Hurley 	__uart_start(tty);
552ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
553ab4382d2SGreg Kroah-Hartman 
554ab4382d2SGreg Kroah-Hartman 	return ret;
555ab4382d2SGreg Kroah-Hartman }
556ab4382d2SGreg Kroah-Hartman 
557ab4382d2SGreg Kroah-Hartman static int uart_write_room(struct tty_struct *tty)
558ab4382d2SGreg Kroah-Hartman {
559ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
560ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
561ab4382d2SGreg Kroah-Hartman 	int ret;
562ab4382d2SGreg Kroah-Hartman 
563ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&state->uart_port->lock, flags);
564ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_free(&state->xmit);
565ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&state->uart_port->lock, flags);
566ab4382d2SGreg Kroah-Hartman 	return ret;
567ab4382d2SGreg Kroah-Hartman }
568ab4382d2SGreg Kroah-Hartman 
569ab4382d2SGreg Kroah-Hartman static int uart_chars_in_buffer(struct tty_struct *tty)
570ab4382d2SGreg Kroah-Hartman {
571ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
572ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
573ab4382d2SGreg Kroah-Hartman 	int ret;
574ab4382d2SGreg Kroah-Hartman 
575ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&state->uart_port->lock, flags);
576ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_pending(&state->xmit);
577ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&state->uart_port->lock, flags);
578ab4382d2SGreg Kroah-Hartman 	return ret;
579ab4382d2SGreg Kroah-Hartman }
580ab4382d2SGreg Kroah-Hartman 
581ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty)
582ab4382d2SGreg Kroah-Hartman {
583ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
584ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
585ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
586ab4382d2SGreg Kroah-Hartman 
587ab4382d2SGreg Kroah-Hartman 	/*
588ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
589ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
590ab4382d2SGreg Kroah-Hartman 	 */
591ab4382d2SGreg Kroah-Hartman 	if (!state) {
592ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
593ab4382d2SGreg Kroah-Hartman 		return;
594ab4382d2SGreg Kroah-Hartman 	}
595ab4382d2SGreg Kroah-Hartman 
596ab4382d2SGreg Kroah-Hartman 	port = state->uart_port;
597ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
598ab4382d2SGreg Kroah-Hartman 
599ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
600ab4382d2SGreg Kroah-Hartman 	uart_circ_clear(&state->xmit);
601ab4382d2SGreg Kroah-Hartman 	if (port->ops->flush_buffer)
602ab4382d2SGreg Kroah-Hartman 		port->ops->flush_buffer(port);
603ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
604ab4382d2SGreg Kroah-Hartman 	tty_wakeup(tty);
605ab4382d2SGreg Kroah-Hartman }
606ab4382d2SGreg Kroah-Hartman 
607ab4382d2SGreg Kroah-Hartman /*
608ab4382d2SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
609ab4382d2SGreg Kroah-Hartman  * the device
610ab4382d2SGreg Kroah-Hartman  */
611ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch)
612ab4382d2SGreg Kroah-Hartman {
613ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
614ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
615ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
616ab4382d2SGreg Kroah-Hartman 
617ab4382d2SGreg Kroah-Hartman 	if (port->ops->send_xchar)
618ab4382d2SGreg Kroah-Hartman 		port->ops->send_xchar(port, ch);
619ab4382d2SGreg Kroah-Hartman 	else {
620ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
621c235ccc1SPeter Hurley 		port->x_char = ch;
622c235ccc1SPeter Hurley 		if (ch)
623ab4382d2SGreg Kroah-Hartman 			port->ops->start_tx(port);
624ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
625ab4382d2SGreg Kroah-Hartman 	}
626ab4382d2SGreg Kroah-Hartman }
627ab4382d2SGreg Kroah-Hartman 
628ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty)
629ab4382d2SGreg Kroah-Hartman {
630ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6319aba8d5bSRussell King 	struct uart_port *port = state->uart_port;
632391f93f2SPeter Hurley 	upstat_t mask = 0;
633ab4382d2SGreg Kroah-Hartman 
634ab4382d2SGreg Kroah-Hartman 	if (I_IXOFF(tty))
635391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
6369db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
637391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
6389aba8d5bSRussell King 
639391f93f2SPeter Hurley 	if (port->status & mask) {
6409aba8d5bSRussell King 		port->ops->throttle(port);
641391f93f2SPeter Hurley 		mask &= ~port->status;
6429aba8d5bSRussell King 	}
6439aba8d5bSRussell King 
644391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
6459aba8d5bSRussell King 		uart_clear_mctrl(port, TIOCM_RTS);
646b4749b97SPeter Hurley 
647b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
648b4749b97SPeter Hurley 		uart_send_xchar(tty, STOP_CHAR(tty));
649ab4382d2SGreg Kroah-Hartman }
650ab4382d2SGreg Kroah-Hartman 
651ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty)
652ab4382d2SGreg Kroah-Hartman {
653ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
654ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
655391f93f2SPeter Hurley 	upstat_t mask = 0;
656ab4382d2SGreg Kroah-Hartman 
6579aba8d5bSRussell King 	if (I_IXOFF(tty))
658391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
6599db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
660391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
6619aba8d5bSRussell King 
662391f93f2SPeter Hurley 	if (port->status & mask) {
6639aba8d5bSRussell King 		port->ops->unthrottle(port);
664391f93f2SPeter Hurley 		mask &= ~port->status;
6659aba8d5bSRussell King 	}
6669aba8d5bSRussell King 
667391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
668ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(port, TIOCM_RTS);
669b4749b97SPeter Hurley 
670b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
671b4749b97SPeter Hurley 		uart_send_xchar(tty, START_CHAR(tty));
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman 
6743abe8c76SPeter Hurley static void uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
675ab4382d2SGreg Kroah-Hartman {
6769f109694SAlan Cox 	struct uart_state *state = container_of(port, struct uart_state, port);
677ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
6787ba2e769SAlan Cox 
67937cd0c99SFengguang Wu 	memset(retinfo, 0, sizeof(*retinfo));
6807ba2e769SAlan Cox 
6813abe8c76SPeter Hurley 	/*
6823abe8c76SPeter Hurley 	 * Ensure the state we copy is consistent and no hardware changes
6833abe8c76SPeter Hurley 	 * occur as we go
6843abe8c76SPeter Hurley 	 */
6853abe8c76SPeter Hurley 	mutex_lock(&port->mutex);
6867ba2e769SAlan Cox 	retinfo->type	    = uport->type;
6877ba2e769SAlan Cox 	retinfo->line	    = uport->line;
6887ba2e769SAlan Cox 	retinfo->port	    = uport->iobase;
6897ba2e769SAlan Cox 	if (HIGH_BITS_OFFSET)
6907ba2e769SAlan Cox 		retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
6917ba2e769SAlan Cox 	retinfo->irq		    = uport->irq;
6927ba2e769SAlan Cox 	retinfo->flags	    = uport->flags;
6937ba2e769SAlan Cox 	retinfo->xmit_fifo_size  = uport->fifosize;
6947ba2e769SAlan Cox 	retinfo->baud_base	    = uport->uartclk / 16;
6957ba2e769SAlan Cox 	retinfo->close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
6967ba2e769SAlan Cox 	retinfo->closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
6977ba2e769SAlan Cox 				ASYNC_CLOSING_WAIT_NONE :
6987ba2e769SAlan Cox 				jiffies_to_msecs(port->closing_wait) / 10;
6997ba2e769SAlan Cox 	retinfo->custom_divisor  = uport->custom_divisor;
7007ba2e769SAlan Cox 	retinfo->hub6	    = uport->hub6;
7017ba2e769SAlan Cox 	retinfo->io_type         = uport->iotype;
7027ba2e769SAlan Cox 	retinfo->iomem_reg_shift = uport->regshift;
7037ba2e769SAlan Cox 	retinfo->iomem_base      = (void *)(unsigned long)uport->mapbase;
704ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
7059f109694SAlan Cox }
7069f109694SAlan Cox 
7079f109694SAlan Cox static int uart_get_info_user(struct tty_port *port,
7089f109694SAlan Cox 			 struct serial_struct __user *retinfo)
7099f109694SAlan Cox {
7109f109694SAlan Cox 	struct serial_struct tmp;
7113abe8c76SPeter Hurley 
7129f109694SAlan Cox 	uart_get_info(port, &tmp);
713ab4382d2SGreg Kroah-Hartman 
714ab4382d2SGreg Kroah-Hartman 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
715ab4382d2SGreg Kroah-Hartman 		return -EFAULT;
716ab4382d2SGreg Kroah-Hartman 	return 0;
717ab4382d2SGreg Kroah-Hartman }
718ab4382d2SGreg Kroah-Hartman 
7197ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
7207ba2e769SAlan Cox 			 struct uart_state *state,
7217ba2e769SAlan Cox 			 struct serial_struct *new_info)
722ab4382d2SGreg Kroah-Hartman {
723ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
724ab4382d2SGreg Kroah-Hartman 	unsigned long new_port;
725ab4382d2SGreg Kroah-Hartman 	unsigned int change_irq, change_port, closing_wait;
726ab4382d2SGreg Kroah-Hartman 	unsigned int old_custom_divisor, close_delay;
727ab4382d2SGreg Kroah-Hartman 	upf_t old_flags, new_flags;
728ab4382d2SGreg Kroah-Hartman 	int retval = 0;
729ab4382d2SGreg Kroah-Hartman 
7307ba2e769SAlan Cox 	new_port = new_info->port;
731ab4382d2SGreg Kroah-Hartman 	if (HIGH_BITS_OFFSET)
7327ba2e769SAlan Cox 		new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
733ab4382d2SGreg Kroah-Hartman 
7347ba2e769SAlan Cox 	new_info->irq = irq_canonicalize(new_info->irq);
7357ba2e769SAlan Cox 	close_delay = msecs_to_jiffies(new_info->close_delay * 10);
7367ba2e769SAlan Cox 	closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
7374cb0fbfdSJiri Slaby 			ASYNC_CLOSING_WAIT_NONE :
7387ba2e769SAlan Cox 			msecs_to_jiffies(new_info->closing_wait * 10);
739ab4382d2SGreg Kroah-Hartman 
740ab4382d2SGreg Kroah-Hartman 
741ab4382d2SGreg Kroah-Hartman 	change_irq  = !(uport->flags & UPF_FIXED_PORT)
7427ba2e769SAlan Cox 		&& new_info->irq != uport->irq;
743ab4382d2SGreg Kroah-Hartman 
744ab4382d2SGreg Kroah-Hartman 	/*
745ab4382d2SGreg Kroah-Hartman 	 * Since changing the 'type' of the port changes its resource
746ab4382d2SGreg Kroah-Hartman 	 * allocations, we should treat type changes the same as
747ab4382d2SGreg Kroah-Hartman 	 * IO port changes.
748ab4382d2SGreg Kroah-Hartman 	 */
749ab4382d2SGreg Kroah-Hartman 	change_port = !(uport->flags & UPF_FIXED_PORT)
750ab4382d2SGreg Kroah-Hartman 		&& (new_port != uport->iobase ||
7517ba2e769SAlan Cox 		    (unsigned long)new_info->iomem_base != uport->mapbase ||
7527ba2e769SAlan Cox 		    new_info->hub6 != uport->hub6 ||
7537ba2e769SAlan Cox 		    new_info->io_type != uport->iotype ||
7547ba2e769SAlan Cox 		    new_info->iomem_reg_shift != uport->regshift ||
7557ba2e769SAlan Cox 		    new_info->type != uport->type);
756ab4382d2SGreg Kroah-Hartman 
757ab4382d2SGreg Kroah-Hartman 	old_flags = uport->flags;
7587ba2e769SAlan Cox 	new_flags = new_info->flags;
759ab4382d2SGreg Kroah-Hartman 	old_custom_divisor = uport->custom_divisor;
760ab4382d2SGreg Kroah-Hartman 
761ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
762ab4382d2SGreg Kroah-Hartman 		retval = -EPERM;
763ab4382d2SGreg Kroah-Hartman 		if (change_irq || change_port ||
7647ba2e769SAlan Cox 		    (new_info->baud_base != uport->uartclk / 16) ||
765ab4382d2SGreg Kroah-Hartman 		    (close_delay != port->close_delay) ||
766ab4382d2SGreg Kroah-Hartman 		    (closing_wait != port->closing_wait) ||
7677ba2e769SAlan Cox 		    (new_info->xmit_fifo_size &&
7687ba2e769SAlan Cox 		     new_info->xmit_fifo_size != uport->fifosize) ||
769ab4382d2SGreg Kroah-Hartman 		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
770ab4382d2SGreg Kroah-Hartman 			goto exit;
771ab4382d2SGreg Kroah-Hartman 		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
772ab4382d2SGreg Kroah-Hartman 			       (new_flags & UPF_USR_MASK));
7737ba2e769SAlan Cox 		uport->custom_divisor = new_info->custom_divisor;
774ab4382d2SGreg Kroah-Hartman 		goto check_and_exit;
775ab4382d2SGreg Kroah-Hartman 	}
776ab4382d2SGreg Kroah-Hartman 
777ab4382d2SGreg Kroah-Hartman 	/*
778ab4382d2SGreg Kroah-Hartman 	 * Ask the low level driver to verify the settings.
779ab4382d2SGreg Kroah-Hartman 	 */
780ab4382d2SGreg Kroah-Hartman 	if (uport->ops->verify_port)
7817ba2e769SAlan Cox 		retval = uport->ops->verify_port(uport, new_info);
782ab4382d2SGreg Kroah-Hartman 
7837ba2e769SAlan Cox 	if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
7847ba2e769SAlan Cox 	    (new_info->baud_base < 9600))
785ab4382d2SGreg Kroah-Hartman 		retval = -EINVAL;
786ab4382d2SGreg Kroah-Hartman 
787ab4382d2SGreg Kroah-Hartman 	if (retval)
788ab4382d2SGreg Kroah-Hartman 		goto exit;
789ab4382d2SGreg Kroah-Hartman 
790ab4382d2SGreg Kroah-Hartman 	if (change_port || change_irq) {
791ab4382d2SGreg Kroah-Hartman 		retval = -EBUSY;
792ab4382d2SGreg Kroah-Hartman 
793ab4382d2SGreg Kroah-Hartman 		/*
794ab4382d2SGreg Kroah-Hartman 		 * Make sure that we are the sole user of this port.
795ab4382d2SGreg Kroah-Hartman 		 */
796ab4382d2SGreg Kroah-Hartman 		if (tty_port_users(port) > 1)
797ab4382d2SGreg Kroah-Hartman 			goto exit;
798ab4382d2SGreg Kroah-Hartman 
799ab4382d2SGreg Kroah-Hartman 		/*
800ab4382d2SGreg Kroah-Hartman 		 * We need to shutdown the serial port at the old
801ab4382d2SGreg Kroah-Hartman 		 * port/type/irq combination.
802ab4382d2SGreg Kroah-Hartman 		 */
803ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
804ab4382d2SGreg Kroah-Hartman 	}
805ab4382d2SGreg Kroah-Hartman 
806ab4382d2SGreg Kroah-Hartman 	if (change_port) {
807ab4382d2SGreg Kroah-Hartman 		unsigned long old_iobase, old_mapbase;
808ab4382d2SGreg Kroah-Hartman 		unsigned int old_type, old_iotype, old_hub6, old_shift;
809ab4382d2SGreg Kroah-Hartman 
810ab4382d2SGreg Kroah-Hartman 		old_iobase = uport->iobase;
811ab4382d2SGreg Kroah-Hartman 		old_mapbase = uport->mapbase;
812ab4382d2SGreg Kroah-Hartman 		old_type = uport->type;
813ab4382d2SGreg Kroah-Hartman 		old_hub6 = uport->hub6;
814ab4382d2SGreg Kroah-Hartman 		old_iotype = uport->iotype;
815ab4382d2SGreg Kroah-Hartman 		old_shift = uport->regshift;
816ab4382d2SGreg Kroah-Hartman 
817ab4382d2SGreg Kroah-Hartman 		/*
818ab4382d2SGreg Kroah-Hartman 		 * Free and release old regions
819ab4382d2SGreg Kroah-Hartman 		 */
820ab4382d2SGreg Kroah-Hartman 		if (old_type != PORT_UNKNOWN)
821ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
822ab4382d2SGreg Kroah-Hartman 
823ab4382d2SGreg Kroah-Hartman 		uport->iobase = new_port;
8247ba2e769SAlan Cox 		uport->type = new_info->type;
8257ba2e769SAlan Cox 		uport->hub6 = new_info->hub6;
8267ba2e769SAlan Cox 		uport->iotype = new_info->io_type;
8277ba2e769SAlan Cox 		uport->regshift = new_info->iomem_reg_shift;
8287ba2e769SAlan Cox 		uport->mapbase = (unsigned long)new_info->iomem_base;
829ab4382d2SGreg Kroah-Hartman 
830ab4382d2SGreg Kroah-Hartman 		/*
831ab4382d2SGreg Kroah-Hartman 		 * Claim and map the new regions
832ab4382d2SGreg Kroah-Hartman 		 */
833ab4382d2SGreg Kroah-Hartman 		if (uport->type != PORT_UNKNOWN) {
834ab4382d2SGreg Kroah-Hartman 			retval = uport->ops->request_port(uport);
835ab4382d2SGreg Kroah-Hartman 		} else {
836ab4382d2SGreg Kroah-Hartman 			/* Always success - Jean II */
837ab4382d2SGreg Kroah-Hartman 			retval = 0;
838ab4382d2SGreg Kroah-Hartman 		}
839ab4382d2SGreg Kroah-Hartman 
840ab4382d2SGreg Kroah-Hartman 		/*
841ab4382d2SGreg Kroah-Hartman 		 * If we fail to request resources for the
842ab4382d2SGreg Kroah-Hartman 		 * new port, try to restore the old settings.
843ab4382d2SGreg Kroah-Hartman 		 */
8447deb39edSThomas Pfaff 		if (retval) {
845ab4382d2SGreg Kroah-Hartman 			uport->iobase = old_iobase;
846ab4382d2SGreg Kroah-Hartman 			uport->type = old_type;
847ab4382d2SGreg Kroah-Hartman 			uport->hub6 = old_hub6;
848ab4382d2SGreg Kroah-Hartman 			uport->iotype = old_iotype;
849ab4382d2SGreg Kroah-Hartman 			uport->regshift = old_shift;
850ab4382d2SGreg Kroah-Hartman 			uport->mapbase = old_mapbase;
8517deb39edSThomas Pfaff 
8527deb39edSThomas Pfaff 			if (old_type != PORT_UNKNOWN) {
853ab4382d2SGreg Kroah-Hartman 				retval = uport->ops->request_port(uport);
854ab4382d2SGreg Kroah-Hartman 				/*
855ab4382d2SGreg Kroah-Hartman 				 * If we failed to restore the old settings,
856ab4382d2SGreg Kroah-Hartman 				 * we fail like this.
857ab4382d2SGreg Kroah-Hartman 				 */
858ab4382d2SGreg Kroah-Hartman 				if (retval)
859ab4382d2SGreg Kroah-Hartman 					uport->type = PORT_UNKNOWN;
860ab4382d2SGreg Kroah-Hartman 
861ab4382d2SGreg Kroah-Hartman 				/*
862ab4382d2SGreg Kroah-Hartman 				 * We failed anyway.
863ab4382d2SGreg Kroah-Hartman 				 */
864ab4382d2SGreg Kroah-Hartman 				retval = -EBUSY;
8657deb39edSThomas Pfaff 			}
8667deb39edSThomas Pfaff 
867ab4382d2SGreg Kroah-Hartman 			/* Added to return the correct error -Ram Gupta */
868ab4382d2SGreg Kroah-Hartman 			goto exit;
869ab4382d2SGreg Kroah-Hartman 		}
870ab4382d2SGreg Kroah-Hartman 	}
871ab4382d2SGreg Kroah-Hartman 
872ab4382d2SGreg Kroah-Hartman 	if (change_irq)
8737ba2e769SAlan Cox 		uport->irq      = new_info->irq;
874ab4382d2SGreg Kroah-Hartman 	if (!(uport->flags & UPF_FIXED_PORT))
8757ba2e769SAlan Cox 		uport->uartclk  = new_info->baud_base * 16;
876ab4382d2SGreg Kroah-Hartman 	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
877ab4382d2SGreg Kroah-Hartman 				 (new_flags & UPF_CHANGE_MASK);
8787ba2e769SAlan Cox 	uport->custom_divisor   = new_info->custom_divisor;
879ab4382d2SGreg Kroah-Hartman 	port->close_delay     = close_delay;
880ab4382d2SGreg Kroah-Hartman 	port->closing_wait    = closing_wait;
8817ba2e769SAlan Cox 	if (new_info->xmit_fifo_size)
8827ba2e769SAlan Cox 		uport->fifosize = new_info->xmit_fifo_size;
883d6c53c0eSJiri Slaby 	port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
884ab4382d2SGreg Kroah-Hartman 
885ab4382d2SGreg Kroah-Hartman  check_and_exit:
886ab4382d2SGreg Kroah-Hartman 	retval = 0;
887ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
888ab4382d2SGreg Kroah-Hartman 		goto exit;
889ab4382d2SGreg Kroah-Hartman 	if (port->flags & ASYNC_INITIALIZED) {
890ab4382d2SGreg Kroah-Hartman 		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
891ab4382d2SGreg Kroah-Hartman 		    old_custom_divisor != uport->custom_divisor) {
892ab4382d2SGreg Kroah-Hartman 			/*
893ab4382d2SGreg Kroah-Hartman 			 * If they're setting up a custom divisor or speed,
894ab4382d2SGreg Kroah-Hartman 			 * instead of clearing it, then bitch about it. No
895ab4382d2SGreg Kroah-Hartman 			 * need to rate-limit; it's CAP_SYS_ADMIN only.
896ab4382d2SGreg Kroah-Hartman 			 */
897ab4382d2SGreg Kroah-Hartman 			if (uport->flags & UPF_SPD_MASK) {
8982f2dafe7SSudip Mukherjee 				dev_notice(uport->dev,
8992f2dafe7SSudip Mukherjee 				       "%s sets custom speed on %s. This is deprecated.\n",
9002f2dafe7SSudip Mukherjee 				      current->comm,
901429b4749SRasmus Villemoes 				      tty_name(port->tty));
902ab4382d2SGreg Kroah-Hartman 			}
903ab4382d2SGreg Kroah-Hartman 			uart_change_speed(tty, state, NULL);
904ab4382d2SGreg Kroah-Hartman 		}
905ab4382d2SGreg Kroah-Hartman 	} else
906ab4382d2SGreg Kroah-Hartman 		retval = uart_startup(tty, state, 1);
907ab4382d2SGreg Kroah-Hartman  exit:
9087ba2e769SAlan Cox 	return retval;
9097ba2e769SAlan Cox }
9107ba2e769SAlan Cox 
9117ba2e769SAlan Cox static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
9127ba2e769SAlan Cox 			 struct serial_struct __user *newinfo)
9137ba2e769SAlan Cox {
9147ba2e769SAlan Cox 	struct serial_struct new_serial;
9157ba2e769SAlan Cox 	struct tty_port *port = &state->port;
9167ba2e769SAlan Cox 	int retval;
9177ba2e769SAlan Cox 
9187ba2e769SAlan Cox 	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
9197ba2e769SAlan Cox 		return -EFAULT;
9207ba2e769SAlan Cox 
9217ba2e769SAlan Cox 	/*
9227ba2e769SAlan Cox 	 * This semaphore protects port->count.  It is also
9237ba2e769SAlan Cox 	 * very useful to prevent opens.  Also, take the
9247ba2e769SAlan Cox 	 * port configuration semaphore to make sure that a
9257ba2e769SAlan Cox 	 * module insertion/removal doesn't change anything
9267ba2e769SAlan Cox 	 * under us.
9277ba2e769SAlan Cox 	 */
9287ba2e769SAlan Cox 	mutex_lock(&port->mutex);
9297ba2e769SAlan Cox 	retval = uart_set_info(tty, port, state, &new_serial);
930ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
931ab4382d2SGreg Kroah-Hartman 	return retval;
932ab4382d2SGreg Kroah-Hartman }
933ab4382d2SGreg Kroah-Hartman 
934ab4382d2SGreg Kroah-Hartman /**
935ab4382d2SGreg Kroah-Hartman  *	uart_get_lsr_info	-	get line status register info
936ab4382d2SGreg Kroah-Hartman  *	@tty: tty associated with the UART
937ab4382d2SGreg Kroah-Hartman  *	@state: UART being queried
938ab4382d2SGreg Kroah-Hartman  *	@value: returned modem value
939ab4382d2SGreg Kroah-Hartman  *
940ab4382d2SGreg Kroah-Hartman  *	Note: uart_ioctl protects us against hangups.
941ab4382d2SGreg Kroah-Hartman  */
942ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty,
943ab4382d2SGreg Kroah-Hartman 			struct uart_state *state, unsigned int __user *value)
944ab4382d2SGreg Kroah-Hartman {
945ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
946ab4382d2SGreg Kroah-Hartman 	unsigned int result;
947ab4382d2SGreg Kroah-Hartman 
948ab4382d2SGreg Kroah-Hartman 	result = uport->ops->tx_empty(uport);
949ab4382d2SGreg Kroah-Hartman 
950ab4382d2SGreg Kroah-Hartman 	/*
951ab4382d2SGreg Kroah-Hartman 	 * If we're about to load something into the transmit
952ab4382d2SGreg Kroah-Hartman 	 * register, we'll pretend the transmitter isn't empty to
953ab4382d2SGreg Kroah-Hartman 	 * avoid a race condition (depending on when the transmit
954ab4382d2SGreg Kroah-Hartman 	 * interrupt happens).
955ab4382d2SGreg Kroah-Hartman 	 */
956ab4382d2SGreg Kroah-Hartman 	if (uport->x_char ||
957ab4382d2SGreg Kroah-Hartman 	    ((uart_circ_chars_pending(&state->xmit) > 0) &&
958d01f4d18SPeter Hurley 	     !uart_tx_stopped(uport)))
959ab4382d2SGreg Kroah-Hartman 		result &= ~TIOCSER_TEMT;
960ab4382d2SGreg Kroah-Hartman 
961ab4382d2SGreg Kroah-Hartman 	return put_user(result, value);
962ab4382d2SGreg Kroah-Hartman }
963ab4382d2SGreg Kroah-Hartman 
96460b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty)
965ab4382d2SGreg Kroah-Hartman {
966ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
967ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
968ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
969ab4382d2SGreg Kroah-Hartman 	int result = -EIO;
970ab4382d2SGreg Kroah-Hartman 
971ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
97260b33c13SAlan Cox 	if (!(tty->flags & (1 << TTY_IO_ERROR))) {
973ab4382d2SGreg Kroah-Hartman 		result = uport->mctrl;
974ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
975ab4382d2SGreg Kroah-Hartman 		result |= uport->ops->get_mctrl(uport);
976ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
977ab4382d2SGreg Kroah-Hartman 	}
978ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
979ab4382d2SGreg Kroah-Hartman 
980ab4382d2SGreg Kroah-Hartman 	return result;
981ab4382d2SGreg Kroah-Hartman }
982ab4382d2SGreg Kroah-Hartman 
983ab4382d2SGreg Kroah-Hartman static int
98420b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
985ab4382d2SGreg Kroah-Hartman {
986ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
987ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
988ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
989ab4382d2SGreg Kroah-Hartman 	int ret = -EIO;
990ab4382d2SGreg Kroah-Hartman 
991ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
99220b9d177SAlan Cox 	if (!(tty->flags & (1 << TTY_IO_ERROR))) {
993ab4382d2SGreg Kroah-Hartman 		uart_update_mctrl(uport, set, clear);
994ab4382d2SGreg Kroah-Hartman 		ret = 0;
995ab4382d2SGreg Kroah-Hartman 	}
996ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
997ab4382d2SGreg Kroah-Hartman 	return ret;
998ab4382d2SGreg Kroah-Hartman }
999ab4382d2SGreg Kroah-Hartman 
1000ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state)
1001ab4382d2SGreg Kroah-Hartman {
1002ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1003ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1004ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1005ab4382d2SGreg Kroah-Hartman 
1006ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1007ab4382d2SGreg Kroah-Hartman 
1008ab4382d2SGreg Kroah-Hartman 	if (uport->type != PORT_UNKNOWN)
1009ab4382d2SGreg Kroah-Hartman 		uport->ops->break_ctl(uport, break_state);
1010ab4382d2SGreg Kroah-Hartman 
1011ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1012ab4382d2SGreg Kroah-Hartman 	return 0;
1013ab4382d2SGreg Kroah-Hartman }
1014ab4382d2SGreg Kroah-Hartman 
1015ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1016ab4382d2SGreg Kroah-Hartman {
1017ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1018ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1019ab4382d2SGreg Kroah-Hartman 	int flags, ret;
1020ab4382d2SGreg Kroah-Hartman 
1021ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
1022ab4382d2SGreg Kroah-Hartman 		return -EPERM;
1023ab4382d2SGreg Kroah-Hartman 
1024ab4382d2SGreg Kroah-Hartman 	/*
1025ab4382d2SGreg Kroah-Hartman 	 * Take the per-port semaphore.  This prevents count from
1026ab4382d2SGreg Kroah-Hartman 	 * changing, and hence any extra opens of the port while
1027ab4382d2SGreg Kroah-Hartman 	 * we're auto-configuring.
1028ab4382d2SGreg Kroah-Hartman 	 */
1029ab4382d2SGreg Kroah-Hartman 	if (mutex_lock_interruptible(&port->mutex))
1030ab4382d2SGreg Kroah-Hartman 		return -ERESTARTSYS;
1031ab4382d2SGreg Kroah-Hartman 
1032ab4382d2SGreg Kroah-Hartman 	ret = -EBUSY;
1033ab4382d2SGreg Kroah-Hartman 	if (tty_port_users(port) == 1) {
1034ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1035ab4382d2SGreg Kroah-Hartman 
1036ab4382d2SGreg Kroah-Hartman 		/*
1037ab4382d2SGreg Kroah-Hartman 		 * If we already have a port type configured,
1038ab4382d2SGreg Kroah-Hartman 		 * we must release its resources.
1039ab4382d2SGreg Kroah-Hartman 		 */
1040ab4382d2SGreg Kroah-Hartman 		if (uport->type != PORT_UNKNOWN)
1041ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
1042ab4382d2SGreg Kroah-Hartman 
1043ab4382d2SGreg Kroah-Hartman 		flags = UART_CONFIG_TYPE;
1044ab4382d2SGreg Kroah-Hartman 		if (uport->flags & UPF_AUTO_IRQ)
1045ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_IRQ;
1046ab4382d2SGreg Kroah-Hartman 
1047ab4382d2SGreg Kroah-Hartman 		/*
1048ab4382d2SGreg Kroah-Hartman 		 * This will claim the ports resources if
1049ab4382d2SGreg Kroah-Hartman 		 * a port is found.
1050ab4382d2SGreg Kroah-Hartman 		 */
1051ab4382d2SGreg Kroah-Hartman 		uport->ops->config_port(uport, flags);
1052ab4382d2SGreg Kroah-Hartman 
1053ab4382d2SGreg Kroah-Hartman 		ret = uart_startup(tty, state, 1);
1054ab4382d2SGreg Kroah-Hartman 	}
1055ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1056ab4382d2SGreg Kroah-Hartman 	return ret;
1057ab4382d2SGreg Kroah-Hartman }
1058ab4382d2SGreg Kroah-Hartman 
10591fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport)
10601fdc3106SAlexander Shiyan {
10611fdc3106SAlexander Shiyan 	/*
10621fdc3106SAlexander Shiyan 	 * Force modem status interrupts on
10631fdc3106SAlexander Shiyan 	 */
10641fdc3106SAlexander Shiyan 	if (uport->ops->enable_ms)
10651fdc3106SAlexander Shiyan 		uport->ops->enable_ms(uport);
10661fdc3106SAlexander Shiyan }
10671fdc3106SAlexander Shiyan 
1068ab4382d2SGreg Kroah-Hartman /*
1069ab4382d2SGreg Kroah-Hartman  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1070ab4382d2SGreg Kroah-Hartman  * - mask passed in arg for lines of interest
1071ab4382d2SGreg Kroah-Hartman  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1072ab4382d2SGreg Kroah-Hartman  * Caller should use TIOCGICOUNT to see which one it was
1073ab4382d2SGreg Kroah-Hartman  *
1074ab4382d2SGreg Kroah-Hartman  * FIXME: This wants extracting into a common all driver implementation
1075ab4382d2SGreg Kroah-Hartman  * of TIOCMWAIT using tty_port.
1076ab4382d2SGreg Kroah-Hartman  */
1077ab4382d2SGreg Kroah-Hartman static int
1078ab4382d2SGreg Kroah-Hartman uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1079ab4382d2SGreg Kroah-Hartman {
1080ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1081ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1082ab4382d2SGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
1083ab4382d2SGreg Kroah-Hartman 	struct uart_icount cprev, cnow;
1084ab4382d2SGreg Kroah-Hartman 	int ret;
1085ab4382d2SGreg Kroah-Hartman 
1086ab4382d2SGreg Kroah-Hartman 	/*
1087ab4382d2SGreg Kroah-Hartman 	 * note the counters on entry
1088ab4382d2SGreg Kroah-Hartman 	 */
1089ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1090ab4382d2SGreg Kroah-Hartman 	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
10911fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1092ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1093ab4382d2SGreg Kroah-Hartman 
1094ab4382d2SGreg Kroah-Hartman 	add_wait_queue(&port->delta_msr_wait, &wait);
1095ab4382d2SGreg Kroah-Hartman 	for (;;) {
1096ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1097ab4382d2SGreg Kroah-Hartman 		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1098ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1099ab4382d2SGreg Kroah-Hartman 
1100ab4382d2SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
1101ab4382d2SGreg Kroah-Hartman 
1102ab4382d2SGreg Kroah-Hartman 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1103ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1104ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1105ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1106ab4382d2SGreg Kroah-Hartman 			ret = 0;
1107ab4382d2SGreg Kroah-Hartman 			break;
1108ab4382d2SGreg Kroah-Hartman 		}
1109ab4382d2SGreg Kroah-Hartman 
1110ab4382d2SGreg Kroah-Hartman 		schedule();
1111ab4382d2SGreg Kroah-Hartman 
1112ab4382d2SGreg Kroah-Hartman 		/* see if a signal did it */
1113ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current)) {
1114ab4382d2SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
1115ab4382d2SGreg Kroah-Hartman 			break;
1116ab4382d2SGreg Kroah-Hartman 		}
1117ab4382d2SGreg Kroah-Hartman 
1118ab4382d2SGreg Kroah-Hartman 		cprev = cnow;
1119ab4382d2SGreg Kroah-Hartman 	}
112097f9f707SFabian Frederick 	__set_current_state(TASK_RUNNING);
1121ab4382d2SGreg Kroah-Hartman 	remove_wait_queue(&port->delta_msr_wait, &wait);
1122ab4382d2SGreg Kroah-Hartman 
1123ab4382d2SGreg Kroah-Hartman 	return ret;
1124ab4382d2SGreg Kroah-Hartman }
1125ab4382d2SGreg Kroah-Hartman 
1126ab4382d2SGreg Kroah-Hartman /*
1127ab4382d2SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1128ab4382d2SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1129ab4382d2SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1130ab4382d2SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1131ab4382d2SGreg Kroah-Hartman  */
1132ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty,
1133ab4382d2SGreg Kroah-Hartman 			  struct serial_icounter_struct *icount)
1134ab4382d2SGreg Kroah-Hartman {
1135ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1136ab4382d2SGreg Kroah-Hartman 	struct uart_icount cnow;
1137ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1138ab4382d2SGreg Kroah-Hartman 
1139ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1140ab4382d2SGreg Kroah-Hartman 	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1141ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1142ab4382d2SGreg Kroah-Hartman 
1143ab4382d2SGreg Kroah-Hartman 	icount->cts         = cnow.cts;
1144ab4382d2SGreg Kroah-Hartman 	icount->dsr         = cnow.dsr;
1145ab4382d2SGreg Kroah-Hartman 	icount->rng         = cnow.rng;
1146ab4382d2SGreg Kroah-Hartman 	icount->dcd         = cnow.dcd;
1147ab4382d2SGreg Kroah-Hartman 	icount->rx          = cnow.rx;
1148ab4382d2SGreg Kroah-Hartman 	icount->tx          = cnow.tx;
1149ab4382d2SGreg Kroah-Hartman 	icount->frame       = cnow.frame;
1150ab4382d2SGreg Kroah-Hartman 	icount->overrun     = cnow.overrun;
1151ab4382d2SGreg Kroah-Hartman 	icount->parity      = cnow.parity;
1152ab4382d2SGreg Kroah-Hartman 	icount->brk         = cnow.brk;
1153ab4382d2SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1154ab4382d2SGreg Kroah-Hartman 
1155ab4382d2SGreg Kroah-Hartman 	return 0;
1156ab4382d2SGreg Kroah-Hartman }
1157ab4382d2SGreg Kroah-Hartman 
1158a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port,
1159a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485)
1160a5f276f1SRicardo Ribalda Delgado {
1161bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1162bd737f87SRicardo Ribalda Delgado 	struct serial_rs485 aux;
1163bd737f87SRicardo Ribalda Delgado 
1164bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1165bd737f87SRicardo Ribalda Delgado 	aux = port->rs485;
1166bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1167bd737f87SRicardo Ribalda Delgado 
1168bd737f87SRicardo Ribalda Delgado 	if (copy_to_user(rs485, &aux, sizeof(aux)))
1169a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1170bd737f87SRicardo Ribalda Delgado 
1171a5f276f1SRicardo Ribalda Delgado 	return 0;
1172a5f276f1SRicardo Ribalda Delgado }
1173a5f276f1SRicardo Ribalda Delgado 
1174a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port,
1175a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485_user)
1176a5f276f1SRicardo Ribalda Delgado {
1177a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485 rs485;
1178a5f276f1SRicardo Ribalda Delgado 	int ret;
1179bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1180a5f276f1SRicardo Ribalda Delgado 
1181a5f276f1SRicardo Ribalda Delgado 	if (!port->rs485_config)
1182a5f276f1SRicardo Ribalda Delgado 		return -ENOIOCTLCMD;
1183a5f276f1SRicardo Ribalda Delgado 
1184a5f276f1SRicardo Ribalda Delgado 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1185a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1186a5f276f1SRicardo Ribalda Delgado 
1187bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1188a5f276f1SRicardo Ribalda Delgado 	ret = port->rs485_config(port, &rs485);
1189bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1190a5f276f1SRicardo Ribalda Delgado 	if (ret)
1191a5f276f1SRicardo Ribalda Delgado 		return ret;
1192a5f276f1SRicardo Ribalda Delgado 
1193a5f276f1SRicardo Ribalda Delgado 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1194a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1195a5f276f1SRicardo Ribalda Delgado 
1196a5f276f1SRicardo Ribalda Delgado 	return 0;
1197a5f276f1SRicardo Ribalda Delgado }
1198a5f276f1SRicardo Ribalda Delgado 
1199ab4382d2SGreg Kroah-Hartman /*
1200ab4382d2SGreg Kroah-Hartman  * Called via sys_ioctl.  We can use spin_lock_irq() here.
1201ab4382d2SGreg Kroah-Hartman  */
1202ab4382d2SGreg Kroah-Hartman static int
12036caa76b7SAlan Cox uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1204ab4382d2SGreg Kroah-Hartman 	   unsigned long arg)
1205ab4382d2SGreg Kroah-Hartman {
1206ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1207ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1208ab4382d2SGreg Kroah-Hartman 	void __user *uarg = (void __user *)arg;
1209ab4382d2SGreg Kroah-Hartman 	int ret = -ENOIOCTLCMD;
1210ab4382d2SGreg Kroah-Hartman 
1211ab4382d2SGreg Kroah-Hartman 
1212ab4382d2SGreg Kroah-Hartman 	/*
1213ab4382d2SGreg Kroah-Hartman 	 * These ioctls don't rely on the hardware to be present.
1214ab4382d2SGreg Kroah-Hartman 	 */
1215ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1216ab4382d2SGreg Kroah-Hartman 	case TIOCGSERIAL:
12179f109694SAlan Cox 		ret = uart_get_info_user(port, uarg);
1218ab4382d2SGreg Kroah-Hartman 		break;
1219ab4382d2SGreg Kroah-Hartman 
1220ab4382d2SGreg Kroah-Hartman 	case TIOCSSERIAL:
12217c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
12227ba2e769SAlan Cox 		ret = uart_set_info_user(tty, state, uarg);
12237c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1224ab4382d2SGreg Kroah-Hartman 		break;
1225ab4382d2SGreg Kroah-Hartman 
1226ab4382d2SGreg Kroah-Hartman 	case TIOCSERCONFIG:
12277c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
1228ab4382d2SGreg Kroah-Hartman 		ret = uart_do_autoconfig(tty, state);
12297c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1230ab4382d2SGreg Kroah-Hartman 		break;
1231ab4382d2SGreg Kroah-Hartman 
1232ab4382d2SGreg Kroah-Hartman 	case TIOCSERGWILD: /* obsolete */
1233ab4382d2SGreg Kroah-Hartman 	case TIOCSERSWILD: /* obsolete */
1234ab4382d2SGreg Kroah-Hartman 		ret = 0;
1235ab4382d2SGreg Kroah-Hartman 		break;
1236ab4382d2SGreg Kroah-Hartman 	}
1237ab4382d2SGreg Kroah-Hartman 
1238ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1239ab4382d2SGreg Kroah-Hartman 		goto out;
1240ab4382d2SGreg Kroah-Hartman 
1241ab4382d2SGreg Kroah-Hartman 	if (tty->flags & (1 << TTY_IO_ERROR)) {
1242ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1243ab4382d2SGreg Kroah-Hartman 		goto out;
1244ab4382d2SGreg Kroah-Hartman 	}
1245ab4382d2SGreg Kroah-Hartman 
1246ab4382d2SGreg Kroah-Hartman 	/*
1247ab4382d2SGreg Kroah-Hartman 	 * The following should only be used when hardware is present.
1248ab4382d2SGreg Kroah-Hartman 	 */
1249ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1250ab4382d2SGreg Kroah-Hartman 	case TIOCMIWAIT:
1251ab4382d2SGreg Kroah-Hartman 		ret = uart_wait_modem_status(state, arg);
1252ab4382d2SGreg Kroah-Hartman 		break;
1253ab4382d2SGreg Kroah-Hartman 	}
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1256ab4382d2SGreg Kroah-Hartman 		goto out;
1257ab4382d2SGreg Kroah-Hartman 
1258ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1259ab4382d2SGreg Kroah-Hartman 
12606caa76b7SAlan Cox 	if (tty->flags & (1 << TTY_IO_ERROR)) {
1261ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1262ab4382d2SGreg Kroah-Hartman 		goto out_up;
1263ab4382d2SGreg Kroah-Hartman 	}
1264ab4382d2SGreg Kroah-Hartman 
1265ab4382d2SGreg Kroah-Hartman 	/*
1266ab4382d2SGreg Kroah-Hartman 	 * All these rely on hardware being present and need to be
1267ab4382d2SGreg Kroah-Hartman 	 * protected against the tty being hung up.
1268ab4382d2SGreg Kroah-Hartman 	 */
1269a9c20a9cSRicardo Ribalda Delgado 
1270ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1271a9c20a9cSRicardo Ribalda Delgado 	case TIOCSERGETLSR: /* Get line status register */
1272a9c20a9cSRicardo Ribalda Delgado 		ret = uart_get_lsr_info(tty, state, uarg);
1273a9c20a9cSRicardo Ribalda Delgado 		break;
1274a9c20a9cSRicardo Ribalda Delgado 
1275a5f276f1SRicardo Ribalda Delgado 	case TIOCGRS485:
1276a5f276f1SRicardo Ribalda Delgado 		ret = uart_get_rs485_config(state->uart_port, uarg);
1277a5f276f1SRicardo Ribalda Delgado 		break;
1278a5f276f1SRicardo Ribalda Delgado 
1279a5f276f1SRicardo Ribalda Delgado 	case TIOCSRS485:
1280a5f276f1SRicardo Ribalda Delgado 		ret = uart_set_rs485_config(state->uart_port, uarg);
1281a5f276f1SRicardo Ribalda Delgado 		break;
1282ab4382d2SGreg Kroah-Hartman 	default: {
1283ab4382d2SGreg Kroah-Hartman 		struct uart_port *uport = state->uart_port;
1284ab4382d2SGreg Kroah-Hartman 		if (uport->ops->ioctl)
1285ab4382d2SGreg Kroah-Hartman 			ret = uport->ops->ioctl(uport, cmd, arg);
1286ab4382d2SGreg Kroah-Hartman 		break;
1287ab4382d2SGreg Kroah-Hartman 	}
1288ab4382d2SGreg Kroah-Hartman 	}
1289ab4382d2SGreg Kroah-Hartman out_up:
1290ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1291ab4382d2SGreg Kroah-Hartman out:
1292ab4382d2SGreg Kroah-Hartman 	return ret;
1293ab4382d2SGreg Kroah-Hartman }
1294ab4382d2SGreg Kroah-Hartman 
1295ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty)
1296ab4382d2SGreg Kroah-Hartman {
1297ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1298ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1299ab4382d2SGreg Kroah-Hartman 
1300db1b9dfcSPeter Hurley 	if (uport->ops->set_ldisc) {
1301db1b9dfcSPeter Hurley 		mutex_lock(&state->port.mutex);
1302732a84a0SPeter Hurley 		uport->ops->set_ldisc(uport, &tty->termios);
1303db1b9dfcSPeter Hurley 		mutex_unlock(&state->port.mutex);
1304db1b9dfcSPeter Hurley 	}
1305ab4382d2SGreg Kroah-Hartman }
1306ab4382d2SGreg Kroah-Hartman 
1307ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty,
1308ab4382d2SGreg Kroah-Hartman 						struct ktermios *old_termios)
1309ab4382d2SGreg Kroah-Hartman {
1310ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1311dec94e70SRussell King 	struct uart_port *uport = state->uart_port;
1312adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
13132cbacafdSRussell King 	unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
13142cbacafdSRussell King 	bool sw_changed = false;
1315ab4382d2SGreg Kroah-Hartman 
13162cbacafdSRussell King 	/*
13172cbacafdSRussell King 	 * Drivers doing software flow control also need to know
13182cbacafdSRussell King 	 * about changes to these input settings.
13192cbacafdSRussell King 	 */
13202cbacafdSRussell King 	if (uport->flags & UPF_SOFT_FLOW) {
13212cbacafdSRussell King 		iflag_mask |= IXANY|IXON|IXOFF;
13222cbacafdSRussell King 		sw_changed =
13232cbacafdSRussell King 		   tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
13242cbacafdSRussell King 		   tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
13252cbacafdSRussell King 	}
1326ab4382d2SGreg Kroah-Hartman 
1327ab4382d2SGreg Kroah-Hartman 	/*
1328ab4382d2SGreg Kroah-Hartman 	 * These are the bits that are used to setup various
1329ab4382d2SGreg Kroah-Hartman 	 * flags in the low level driver. We can ignore the Bfoo
1330ab4382d2SGreg Kroah-Hartman 	 * bits in c_cflag; c_[io]speed will always be set
1331ab4382d2SGreg Kroah-Hartman 	 * appropriately by set_termios() in tty_ioctl.c
1332ab4382d2SGreg Kroah-Hartman 	 */
1333ab4382d2SGreg Kroah-Hartman 	if ((cflag ^ old_termios->c_cflag) == 0 &&
1334adc8d746SAlan Cox 	    tty->termios.c_ospeed == old_termios->c_ospeed &&
1335adc8d746SAlan Cox 	    tty->termios.c_ispeed == old_termios->c_ispeed &&
13362cbacafdSRussell King 	    ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
13372cbacafdSRussell King 	    !sw_changed) {
1338ab4382d2SGreg Kroah-Hartman 		return;
1339ab4382d2SGreg Kroah-Hartman 	}
1340ab4382d2SGreg Kroah-Hartman 
13417c8ab967SPeter Hurley 	mutex_lock(&state->port.mutex);
1342ab4382d2SGreg Kroah-Hartman 	uart_change_speed(tty, state, old_termios);
13437c8ab967SPeter Hurley 	mutex_unlock(&state->port.mutex);
1344c18b55fdSPeter Hurley 	/* reload cflag from termios; port driver may have overriden flags */
1345c18b55fdSPeter Hurley 	cflag = tty->termios.c_cflag;
1346ab4382d2SGreg Kroah-Hartman 
1347ab4382d2SGreg Kroah-Hartman 	/* Handle transition to B0 status */
1348ab4382d2SGreg Kroah-Hartman 	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1349dec94e70SRussell King 		uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1350ab4382d2SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
1351ab4382d2SGreg Kroah-Hartman 	else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1352ab4382d2SGreg Kroah-Hartman 		unsigned int mask = TIOCM_DTR;
135399abf3b9SPeter Hurley 		if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
1354ab4382d2SGreg Kroah-Hartman 			mask |= TIOCM_RTS;
1355dec94e70SRussell King 		uart_set_mctrl(uport, mask);
1356ab4382d2SGreg Kroah-Hartman 	}
1357ab4382d2SGreg Kroah-Hartman }
1358ab4382d2SGreg Kroah-Hartman 
1359ab4382d2SGreg Kroah-Hartman /*
1360ef4f527cSKevin Cernekee  * Calls to uart_close() are serialised via the tty_lock in
1361ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_release()
1362ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1363ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1364ab4382d2SGreg Kroah-Hartman  */
1365ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp)
1366ab4382d2SGreg Kroah-Hartman {
1367ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1368ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
1369ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport;
1370ab4382d2SGreg Kroah-Hartman 
137191b32f54SPeter Hurley 	if (!state) {
137291b32f54SPeter Hurley 		struct uart_driver *drv = tty->driver->driver_state;
137391b32f54SPeter Hurley 
137491b32f54SPeter Hurley 		state = drv->state + tty->index;
137591b32f54SPeter Hurley 		port = &state->port;
137691b32f54SPeter Hurley 		spin_lock_irq(&port->lock);
137791b32f54SPeter Hurley 		--port->count;
137891b32f54SPeter Hurley 		spin_unlock_irq(&port->lock);
1379ab4382d2SGreg Kroah-Hartman 		return;
138091b32f54SPeter Hurley 	}
1381ab4382d2SGreg Kroah-Hartman 
1382ab4382d2SGreg Kroah-Hartman 	uport = state->uart_port;
1383ab4382d2SGreg Kroah-Hartman 	port = &state->port;
138439b3d892SPeter Hurley 	pr_debug("uart_close(%d) called\n", tty->index);
1385ab4382d2SGreg Kroah-Hartman 
13864ed94cd4SGeert Uytterhoeven 	if (!port->count || tty_port_close_start(port, tty, filp) == 0)
138755956216SNobuhiro Iwamatsu 		return;
1388ab4382d2SGreg Kroah-Hartman 
1389ab4382d2SGreg Kroah-Hartman 	/*
1390ab4382d2SGreg Kroah-Hartman 	 * At this point, we stop accepting input.  To do this, we
1391ab4382d2SGreg Kroah-Hartman 	 * disable the receive line status interrupts.
1392ab4382d2SGreg Kroah-Hartman 	 */
1393ab4382d2SGreg Kroah-Hartman 	if (port->flags & ASYNC_INITIALIZED) {
13946fb98fb3SPeter Hurley 		spin_lock_irq(&uport->lock);
1395ab4382d2SGreg Kroah-Hartman 		uport->ops->stop_rx(uport);
13966fb98fb3SPeter Hurley 		spin_unlock_irq(&uport->lock);
1397ab4382d2SGreg Kroah-Hartman 		/*
1398ab4382d2SGreg Kroah-Hartman 		 * Before we drop DTR, make sure the UART transmitter
1399ab4382d2SGreg Kroah-Hartman 		 * has completely drained; this is especially
1400ab4382d2SGreg Kroah-Hartman 		 * important if there is a transmit FIFO!
1401ab4382d2SGreg Kroah-Hartman 		 */
14021f33a51dSJiri Slaby 		uart_wait_until_sent(tty, uport->timeout);
1403ab4382d2SGreg Kroah-Hartman 	}
1404ab4382d2SGreg Kroah-Hartman 
1405bafb0bd2SJiri Slaby 	mutex_lock(&port->mutex);
1406ab4382d2SGreg Kroah-Hartman 	uart_shutdown(tty, state);
1407ab4382d2SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
1408e144c58cSPeter Hurley 
14096fb98fb3SPeter Hurley 	spin_lock_irq(&port->lock);
1410ab4382d2SGreg Kroah-Hartman 
1411ab4382d2SGreg Kroah-Hartman 	if (port->blocked_open) {
14126fb98fb3SPeter Hurley 		spin_unlock_irq(&port->lock);
1413ab4382d2SGreg Kroah-Hartman 		if (port->close_delay)
141474866e75SPeter Hurley 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
14156fb98fb3SPeter Hurley 		spin_lock_irq(&port->lock);
1416ab4382d2SGreg Kroah-Hartman 	} else if (!uart_console(uport)) {
14176fb98fb3SPeter Hurley 		spin_unlock_irq(&port->lock);
14186f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_OFF);
14196fb98fb3SPeter Hurley 		spin_lock_irq(&port->lock);
1420ab4382d2SGreg Kroah-Hartman 	}
1421ab4382d2SGreg Kroah-Hartman 
1422ab4382d2SGreg Kroah-Hartman 	/*
1423ab4382d2SGreg Kroah-Hartman 	 * Wake up anyone trying to open this port.
1424ab4382d2SGreg Kroah-Hartman 	 */
1425ab4382d2SGreg Kroah-Hartman 	clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
14266fb98fb3SPeter Hurley 	spin_unlock_irq(&port->lock);
1427ab4382d2SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
1428ab4382d2SGreg Kroah-Hartman 
1429ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
14302e758910SPeter Hurley 
14312e758910SPeter Hurley 	tty_ldisc_flush(tty);
1432e144c58cSPeter Hurley 	tty->closing = 0;
1433ab4382d2SGreg Kroah-Hartman }
1434ab4382d2SGreg Kroah-Hartman 
14351f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1436ab4382d2SGreg Kroah-Hartman {
14371f33a51dSJiri Slaby 	struct uart_state *state = tty->driver_data;
14381f33a51dSJiri Slaby 	struct uart_port *port = state->uart_port;
1439ab4382d2SGreg Kroah-Hartman 	unsigned long char_time, expire;
1440ab4382d2SGreg Kroah-Hartman 
1441ab4382d2SGreg Kroah-Hartman 	if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1442ab4382d2SGreg Kroah-Hartman 		return;
1443ab4382d2SGreg Kroah-Hartman 
1444ab4382d2SGreg Kroah-Hartman 	/*
1445ab4382d2SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1446ab4382d2SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1447ab4382d2SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1448ab4382d2SGreg Kroah-Hartman 	 *
1449ab4382d2SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1450ab4382d2SGreg Kroah-Hartman 	 * the NIST-PCTS.
1451ab4382d2SGreg Kroah-Hartman 	 */
1452ab4382d2SGreg Kroah-Hartman 	char_time = (port->timeout - HZ/50) / port->fifosize;
1453ab4382d2SGreg Kroah-Hartman 	char_time = char_time / 5;
1454ab4382d2SGreg Kroah-Hartman 	if (char_time == 0)
1455ab4382d2SGreg Kroah-Hartman 		char_time = 1;
1456ab4382d2SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1457ab4382d2SGreg Kroah-Hartman 		char_time = timeout;
1458ab4382d2SGreg Kroah-Hartman 
1459ab4382d2SGreg Kroah-Hartman 	/*
1460ab4382d2SGreg Kroah-Hartman 	 * If the transmitter hasn't cleared in twice the approximate
1461ab4382d2SGreg Kroah-Hartman 	 * amount of time to send the entire FIFO, it probably won't
1462ab4382d2SGreg Kroah-Hartman 	 * ever clear.  This assumes the UART isn't doing flow
1463ab4382d2SGreg Kroah-Hartman 	 * control, which is currently the case.  Hence, if it ever
1464ab4382d2SGreg Kroah-Hartman 	 * takes longer than port->timeout, this is probably due to a
1465ab4382d2SGreg Kroah-Hartman 	 * UART bug of some kind.  So, we clamp the timeout parameter at
1466ab4382d2SGreg Kroah-Hartman 	 * 2*port->timeout.
1467ab4382d2SGreg Kroah-Hartman 	 */
1468ab4382d2SGreg Kroah-Hartman 	if (timeout == 0 || timeout > 2 * port->timeout)
1469ab4382d2SGreg Kroah-Hartman 		timeout = 2 * port->timeout;
1470ab4382d2SGreg Kroah-Hartman 
1471ab4382d2SGreg Kroah-Hartman 	expire = jiffies + timeout;
1472ab4382d2SGreg Kroah-Hartman 
1473ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1474ab4382d2SGreg Kroah-Hartman 		port->line, jiffies, expire);
1475ab4382d2SGreg Kroah-Hartman 
1476ab4382d2SGreg Kroah-Hartman 	/*
1477ab4382d2SGreg Kroah-Hartman 	 * Check whether the transmitter is empty every 'char_time'.
1478ab4382d2SGreg Kroah-Hartman 	 * 'timeout' / 'expire' give us the maximum amount of time
1479ab4382d2SGreg Kroah-Hartman 	 * we wait.
1480ab4382d2SGreg Kroah-Hartman 	 */
1481ab4382d2SGreg Kroah-Hartman 	while (!port->ops->tx_empty(port)) {
1482ab4382d2SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1483ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current))
1484ab4382d2SGreg Kroah-Hartman 			break;
1485ab4382d2SGreg Kroah-Hartman 		if (time_after(jiffies, expire))
1486ab4382d2SGreg Kroah-Hartman 			break;
1487ab4382d2SGreg Kroah-Hartman 	}
1488ab4382d2SGreg Kroah-Hartman }
1489ab4382d2SGreg Kroah-Hartman 
1490ab4382d2SGreg Kroah-Hartman /*
1491ef4f527cSKevin Cernekee  * Calls to uart_hangup() are serialised by the tty_lock in
1492ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1493ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1494ab4382d2SGreg Kroah-Hartman  */
1495ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty)
1496ab4382d2SGreg Kroah-Hartman {
1497ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1498ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1499ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1500ab4382d2SGreg Kroah-Hartman 
150139b3d892SPeter Hurley 	pr_debug("uart_hangup(%d)\n", tty->index);
1502ab4382d2SGreg Kroah-Hartman 
1503ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1504ab4382d2SGreg Kroah-Hartman 	if (port->flags & ASYNC_NORMAL_ACTIVE) {
1505ab4382d2SGreg Kroah-Hartman 		uart_flush_buffer(tty);
1506ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1507ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
1508ab4382d2SGreg Kroah-Hartman 		port->count = 0;
1509ab4382d2SGreg Kroah-Hartman 		clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1510ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
1511ab4382d2SGreg Kroah-Hartman 		tty_port_tty_set(port, NULL);
1512bf903c0cSGeert Uytterhoeven 		if (!uart_console(state->uart_port))
1513bf903c0cSGeert Uytterhoeven 			uart_change_pm(state, UART_PM_STATE_OFF);
1514ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
1515ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->delta_msr_wait);
1516ab4382d2SGreg Kroah-Hartman 	}
1517ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1518ab4382d2SGreg Kroah-Hartman }
1519ab4382d2SGreg Kroah-Hartman 
15200b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port)
15210b1db830SJiri Slaby {
1522b922e19dSJiri Slaby 	struct uart_state *state = container_of(port, struct uart_state, port);
1523b922e19dSJiri Slaby 	struct uart_port *uport = state->uart_port;
1524b922e19dSJiri Slaby 
1525b922e19dSJiri Slaby 	/*
1526b922e19dSJiri Slaby 	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1527b922e19dSJiri Slaby 	 * the irq here so the queue might never be woken up.  Note
1528b922e19dSJiri Slaby 	 * that we won't end up waiting on delta_msr_wait again since
1529b922e19dSJiri Slaby 	 * any outstanding file descriptors should be pointing at
1530b922e19dSJiri Slaby 	 * hung_up_tty_fops now.
1531b922e19dSJiri Slaby 	 */
1532b922e19dSJiri Slaby 	wake_up_interruptible(&port->delta_msr_wait);
1533b922e19dSJiri Slaby 
1534b922e19dSJiri Slaby 	/*
1535b922e19dSJiri Slaby 	 * Free the IRQ and disable the port.
1536b922e19dSJiri Slaby 	 */
1537b922e19dSJiri Slaby 	uport->ops->shutdown(uport);
1538b922e19dSJiri Slaby 
1539b922e19dSJiri Slaby 	/*
1540b922e19dSJiri Slaby 	 * Ensure that the IRQ handler isn't running on another CPU.
1541b922e19dSJiri Slaby 	 */
1542b922e19dSJiri Slaby 	synchronize_irq(uport->irq);
15430b1db830SJiri Slaby }
15440b1db830SJiri Slaby 
1545ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port)
1546ab4382d2SGreg Kroah-Hartman {
1547ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
1548ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1549ab4382d2SGreg Kroah-Hartman 	int mctrl;
1550ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
15511fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1552ab4382d2SGreg Kroah-Hartman 	mctrl = uport->ops->get_mctrl(uport);
1553ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1554ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_CAR)
1555ab4382d2SGreg Kroah-Hartman 		return 1;
1556ab4382d2SGreg Kroah-Hartman 	return 0;
1557ab4382d2SGreg Kroah-Hartman }
1558ab4382d2SGreg Kroah-Hartman 
1559ab4382d2SGreg Kroah-Hartman static void uart_dtr_rts(struct tty_port *port, int onoff)
1560ab4382d2SGreg Kroah-Hartman {
1561ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
1562ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1563ab4382d2SGreg Kroah-Hartman 
15646f5c24adSJiri Slaby 	if (onoff)
1565ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1566ab4382d2SGreg Kroah-Hartman 	else
1567ab4382d2SGreg Kroah-Hartman 		uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1568ab4382d2SGreg Kroah-Hartman }
1569ab4382d2SGreg Kroah-Hartman 
1570ab4382d2SGreg Kroah-Hartman /*
1571ef4f527cSKevin Cernekee  * Calls to uart_open are serialised by the tty_lock in
1572ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_open()
1573ab4382d2SGreg Kroah-Hartman  * Note that if this fails, then uart_close() _will_ be called.
1574ab4382d2SGreg Kroah-Hartman  *
1575ab4382d2SGreg Kroah-Hartman  * In time, we want to scrap the "opening nonpresent ports"
1576ab4382d2SGreg Kroah-Hartman  * behaviour and implement an alternative way for setserial
1577ab4382d2SGreg Kroah-Hartman  * to set base addresses/ports/types.  This will allow us to
1578ab4382d2SGreg Kroah-Hartman  * get rid of a certain amount of extra tests.
1579ab4382d2SGreg Kroah-Hartman  */
1580ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp)
1581ab4382d2SGreg Kroah-Hartman {
15825e388027SPeter Hurley 	struct uart_driver *drv = tty->driver->driver_state;
1583ab4382d2SGreg Kroah-Hartman 	int retval, line = tty->index;
15841c7b13c4SJiri Slaby 	struct uart_state *state = drv->state + line;
15851c7b13c4SJiri Slaby 	struct tty_port *port = &state->port;
1586ab4382d2SGreg Kroah-Hartman 
1587ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_open(%d) called\n", line);
1588ab4382d2SGreg Kroah-Hartman 
158991b32f54SPeter Hurley 	spin_lock_irq(&port->lock);
159091b32f54SPeter Hurley 	++port->count;
159191b32f54SPeter Hurley 	spin_unlock_irq(&port->lock);
159291b32f54SPeter Hurley 
1593ab4382d2SGreg Kroah-Hartman 	/*
15941c7b13c4SJiri Slaby 	 * We take the semaphore here to guarantee that we won't be re-entered
15951c7b13c4SJiri Slaby 	 * while allocating the state structure, or while we request any IRQs
15961c7b13c4SJiri Slaby 	 * that the driver may need.  This also has the nice side-effect that
15971c7b13c4SJiri Slaby 	 * it delays the action of uart_hangup, so we can guarantee that
15981c7b13c4SJiri Slaby 	 * state->port.tty will always contain something reasonable.
1599ab4382d2SGreg Kroah-Hartman 	 */
16001c7b13c4SJiri Slaby 	if (mutex_lock_interruptible(&port->mutex)) {
16011c7b13c4SJiri Slaby 		retval = -ERESTARTSYS;
16021c7b13c4SJiri Slaby 		goto end;
1603ab4382d2SGreg Kroah-Hartman 	}
16041c7b13c4SJiri Slaby 
16051c7b13c4SJiri Slaby 	if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
16061c7b13c4SJiri Slaby 		retval = -ENXIO;
160791b32f54SPeter Hurley 		goto err_unlock;
16081c7b13c4SJiri Slaby 	}
1609ab4382d2SGreg Kroah-Hartman 
1610ab4382d2SGreg Kroah-Hartman 	tty->driver_data = state;
1611ab4382d2SGreg Kroah-Hartman 	state->uart_port->state = state;
1612d6c53c0eSJiri Slaby 	state->port.low_latency =
1613d6c53c0eSJiri Slaby 		(state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1614ab4382d2SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
1615ab4382d2SGreg Kroah-Hartman 
1616ab4382d2SGreg Kroah-Hartman 	/*
1617ab4382d2SGreg Kroah-Hartman 	 * Start up the serial port.
1618ab4382d2SGreg Kroah-Hartman 	 */
1619ab4382d2SGreg Kroah-Hartman 	retval = uart_startup(tty, state, 0);
1620ab4382d2SGreg Kroah-Hartman 
1621ab4382d2SGreg Kroah-Hartman 	/*
1622ab4382d2SGreg Kroah-Hartman 	 * If we succeeded, wait until the port is ready.
1623ab4382d2SGreg Kroah-Hartman 	 */
1624f9d1083dSPeter Hurley err_unlock:
1625ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1626ab4382d2SGreg Kroah-Hartman 	if (retval == 0)
1627ab4382d2SGreg Kroah-Hartman 		retval = tty_port_block_til_ready(port, tty, filp);
16281c7b13c4SJiri Slaby end:
1629ab4382d2SGreg Kroah-Hartman 	return retval;
1630ab4382d2SGreg Kroah-Hartman }
1631ab4382d2SGreg Kroah-Hartman 
1632ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port)
1633ab4382d2SGreg Kroah-Hartman {
1634ab4382d2SGreg Kroah-Hartman 	const char *str = NULL;
1635ab4382d2SGreg Kroah-Hartman 
1636ab4382d2SGreg Kroah-Hartman 	if (port->ops->type)
1637ab4382d2SGreg Kroah-Hartman 		str = port->ops->type(port);
1638ab4382d2SGreg Kroah-Hartman 
1639ab4382d2SGreg Kroah-Hartman 	if (!str)
1640ab4382d2SGreg Kroah-Hartman 		str = "unknown";
1641ab4382d2SGreg Kroah-Hartman 
1642ab4382d2SGreg Kroah-Hartman 	return str;
1643ab4382d2SGreg Kroah-Hartman }
1644ab4382d2SGreg Kroah-Hartman 
1645ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
1646ab4382d2SGreg Kroah-Hartman 
1647ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1648ab4382d2SGreg Kroah-Hartman {
1649ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + i;
1650ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
16516f538fe3SLinus Walleij 	enum uart_pm_state pm_state;
1652ab4382d2SGreg Kroah-Hartman 	struct uart_port *uport = state->uart_port;
1653ab4382d2SGreg Kroah-Hartman 	char stat_buf[32];
1654ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1655ab4382d2SGreg Kroah-Hartman 	int mmio;
1656ab4382d2SGreg Kroah-Hartman 
1657ab4382d2SGreg Kroah-Hartman 	if (!uport)
1658ab4382d2SGreg Kroah-Hartman 		return;
1659ab4382d2SGreg Kroah-Hartman 
1660ab4382d2SGreg Kroah-Hartman 	mmio = uport->iotype >= UPIO_MEM;
1661ab4382d2SGreg Kroah-Hartman 	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1662ab4382d2SGreg Kroah-Hartman 			uport->line, uart_type(uport),
1663ab4382d2SGreg Kroah-Hartman 			mmio ? "mmio:0x" : "port:",
1664ab4382d2SGreg Kroah-Hartman 			mmio ? (unsigned long long)uport->mapbase
1665ab4382d2SGreg Kroah-Hartman 			     : (unsigned long long)uport->iobase,
1666ab4382d2SGreg Kroah-Hartman 			uport->irq);
1667ab4382d2SGreg Kroah-Hartman 
1668ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN) {
1669ab4382d2SGreg Kroah-Hartman 		seq_putc(m, '\n');
1670ab4382d2SGreg Kroah-Hartman 		return;
1671ab4382d2SGreg Kroah-Hartman 	}
1672ab4382d2SGreg Kroah-Hartman 
1673ab4382d2SGreg Kroah-Hartman 	if (capable(CAP_SYS_ADMIN)) {
1674ab4382d2SGreg Kroah-Hartman 		mutex_lock(&port->mutex);
1675ab4382d2SGreg Kroah-Hartman 		pm_state = state->pm_state;
16766f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
16776f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
1678ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1679ab4382d2SGreg Kroah-Hartman 		status = uport->ops->get_mctrl(uport);
1680ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
16816f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
1682ab4382d2SGreg Kroah-Hartman 			uart_change_pm(state, pm_state);
1683ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
1684ab4382d2SGreg Kroah-Hartman 
1685ab4382d2SGreg Kroah-Hartman 		seq_printf(m, " tx:%d rx:%d",
1686ab4382d2SGreg Kroah-Hartman 				uport->icount.tx, uport->icount.rx);
1687ab4382d2SGreg Kroah-Hartman 		if (uport->icount.frame)
1688968af298SPeter Hurley 			seq_printf(m, " fe:%d",	uport->icount.frame);
1689ab4382d2SGreg Kroah-Hartman 		if (uport->icount.parity)
1690968af298SPeter Hurley 			seq_printf(m, " pe:%d",	uport->icount.parity);
1691ab4382d2SGreg Kroah-Hartman 		if (uport->icount.brk)
1692968af298SPeter Hurley 			seq_printf(m, " brk:%d", uport->icount.brk);
1693ab4382d2SGreg Kroah-Hartman 		if (uport->icount.overrun)
1694968af298SPeter Hurley 			seq_printf(m, " oe:%d", uport->icount.overrun);
1695ab4382d2SGreg Kroah-Hartman 
1696ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \
1697ab4382d2SGreg Kroah-Hartman 	if (uport->mctrl & (bit)) \
1698ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1699ab4382d2SGreg Kroah-Hartman 			strlen(stat_buf) - 2)
1700ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \
1701ab4382d2SGreg Kroah-Hartman 	if (status & (bit)) \
1702ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1703ab4382d2SGreg Kroah-Hartman 		       strlen(stat_buf) - 2)
1704ab4382d2SGreg Kroah-Hartman 
1705ab4382d2SGreg Kroah-Hartman 		stat_buf[0] = '\0';
1706ab4382d2SGreg Kroah-Hartman 		stat_buf[1] = '\0';
1707ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_RTS, "|RTS");
1708ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CTS, "|CTS");
1709ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_DTR, "|DTR");
1710ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_DSR, "|DSR");
1711ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CAR, "|CD");
1712ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_RNG, "|RI");
1713ab4382d2SGreg Kroah-Hartman 		if (stat_buf[0])
1714ab4382d2SGreg Kroah-Hartman 			stat_buf[0] = ' ';
1715ab4382d2SGreg Kroah-Hartman 
1716ab4382d2SGreg Kroah-Hartman 		seq_puts(m, stat_buf);
1717ab4382d2SGreg Kroah-Hartman 	}
1718ab4382d2SGreg Kroah-Hartman 	seq_putc(m, '\n');
1719ab4382d2SGreg Kroah-Hartman #undef STATBIT
1720ab4382d2SGreg Kroah-Hartman #undef INFOBIT
1721ab4382d2SGreg Kroah-Hartman }
1722ab4382d2SGreg Kroah-Hartman 
1723ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v)
1724ab4382d2SGreg Kroah-Hartman {
1725ab4382d2SGreg Kroah-Hartman 	struct tty_driver *ttydrv = m->private;
1726ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = ttydrv->driver_state;
1727ab4382d2SGreg Kroah-Hartman 	int i;
1728ab4382d2SGreg Kroah-Hartman 
1729968af298SPeter Hurley 	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1730ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++)
1731ab4382d2SGreg Kroah-Hartman 		uart_line_info(m, drv, i);
1732ab4382d2SGreg Kroah-Hartman 	return 0;
1733ab4382d2SGreg Kroah-Hartman }
1734ab4382d2SGreg Kroah-Hartman 
1735ab4382d2SGreg Kroah-Hartman static int uart_proc_open(struct inode *inode, struct file *file)
1736ab4382d2SGreg Kroah-Hartman {
1737d9dda78bSAl Viro 	return single_open(file, uart_proc_show, PDE_DATA(inode));
1738ab4382d2SGreg Kroah-Hartman }
1739ab4382d2SGreg Kroah-Hartman 
1740ab4382d2SGreg Kroah-Hartman static const struct file_operations uart_proc_fops = {
1741ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
1742ab4382d2SGreg Kroah-Hartman 	.open		= uart_proc_open,
1743ab4382d2SGreg Kroah-Hartman 	.read		= seq_read,
1744ab4382d2SGreg Kroah-Hartman 	.llseek		= seq_lseek,
1745ab4382d2SGreg Kroah-Hartman 	.release	= single_release,
1746ab4382d2SGreg Kroah-Hartman };
1747ab4382d2SGreg Kroah-Hartman #endif
1748ab4382d2SGreg Kroah-Hartman 
1749ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
17501cfe42b7SPeter Hurley /**
1751ab4382d2SGreg Kroah-Hartman  *	uart_console_write - write a console message to a serial port
1752ab4382d2SGreg Kroah-Hartman  *	@port: the port to write the message
1753ab4382d2SGreg Kroah-Hartman  *	@s: array of characters
1754ab4382d2SGreg Kroah-Hartman  *	@count: number of characters in string to write
175510afbe34SPeter Hurley  *	@putchar: function to write character to port
1756ab4382d2SGreg Kroah-Hartman  */
1757ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s,
1758ab4382d2SGreg Kroah-Hartman 			unsigned int count,
1759ab4382d2SGreg Kroah-Hartman 			void (*putchar)(struct uart_port *, int))
1760ab4382d2SGreg Kroah-Hartman {
1761ab4382d2SGreg Kroah-Hartman 	unsigned int i;
1762ab4382d2SGreg Kroah-Hartman 
1763ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < count; i++, s++) {
1764ab4382d2SGreg Kroah-Hartman 		if (*s == '\n')
1765ab4382d2SGreg Kroah-Hartman 			putchar(port, '\r');
1766ab4382d2SGreg Kroah-Hartman 		putchar(port, *s);
1767ab4382d2SGreg Kroah-Hartman 	}
1768ab4382d2SGreg Kroah-Hartman }
1769ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write);
1770ab4382d2SGreg Kroah-Hartman 
1771ab4382d2SGreg Kroah-Hartman /*
1772ab4382d2SGreg Kroah-Hartman  *	Check whether an invalid uart number has been specified, and
1773ab4382d2SGreg Kroah-Hartman  *	if so, search for the first available port that does have
1774ab4382d2SGreg Kroah-Hartman  *	console support.
1775ab4382d2SGreg Kroah-Hartman  */
1776ab4382d2SGreg Kroah-Hartman struct uart_port * __init
1777ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co)
1778ab4382d2SGreg Kroah-Hartman {
1779ab4382d2SGreg Kroah-Hartman 	int idx = co->index;
1780ab4382d2SGreg Kroah-Hartman 
1781ab4382d2SGreg Kroah-Hartman 	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1782ab4382d2SGreg Kroah-Hartman 				     ports[idx].membase == NULL))
1783ab4382d2SGreg Kroah-Hartman 		for (idx = 0; idx < nr; idx++)
1784ab4382d2SGreg Kroah-Hartman 			if (ports[idx].iobase != 0 ||
1785ab4382d2SGreg Kroah-Hartman 			    ports[idx].membase != NULL)
1786ab4382d2SGreg Kroah-Hartman 				break;
1787ab4382d2SGreg Kroah-Hartman 
1788ab4382d2SGreg Kroah-Hartman 	co->index = idx;
1789ab4382d2SGreg Kroah-Hartman 
1790ab4382d2SGreg Kroah-Hartman 	return ports + idx;
1791ab4382d2SGreg Kroah-Hartman }
1792ab4382d2SGreg Kroah-Hartman 
1793ab4382d2SGreg Kroah-Hartman /**
179473abaf87SPeter Hurley  *	uart_parse_earlycon - Parse earlycon options
179573abaf87SPeter Hurley  *	@p:	  ptr to 2nd field (ie., just beyond '<name>,')
179673abaf87SPeter Hurley  *	@iotype:  ptr for decoded iotype (out)
179773abaf87SPeter Hurley  *	@addr:    ptr for decoded mapbase/iobase (out)
179873abaf87SPeter Hurley  *	@options: ptr for <options> field; NULL if not present (out)
179973abaf87SPeter Hurley  *
180073abaf87SPeter Hurley  *	Decodes earlycon kernel command line parameters of the form
1801bd94c407SMasahiro Yamada  *	   earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1802bd94c407SMasahiro Yamada  *	   console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
180373abaf87SPeter Hurley  *
180473abaf87SPeter Hurley  *	The optional form
180573abaf87SPeter Hurley  *	   earlycon=<name>,0x<addr>,<options>
180673abaf87SPeter Hurley  *	   console=<name>,0x<addr>,<options>
180773abaf87SPeter Hurley  *	is also accepted; the returned @iotype will be UPIO_MEM.
180873abaf87SPeter Hurley  *
180973abaf87SPeter Hurley  *	Returns 0 on success or -EINVAL on failure
181073abaf87SPeter Hurley  */
181173abaf87SPeter Hurley int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
181273abaf87SPeter Hurley 			char **options)
181373abaf87SPeter Hurley {
181473abaf87SPeter Hurley 	if (strncmp(p, "mmio,", 5) == 0) {
181573abaf87SPeter Hurley 		*iotype = UPIO_MEM;
181673abaf87SPeter Hurley 		p += 5;
1817bd94c407SMasahiro Yamada 	} else if (strncmp(p, "mmio16,", 7) == 0) {
1818bd94c407SMasahiro Yamada 		*iotype = UPIO_MEM16;
1819bd94c407SMasahiro Yamada 		p += 7;
182073abaf87SPeter Hurley 	} else if (strncmp(p, "mmio32,", 7) == 0) {
182173abaf87SPeter Hurley 		*iotype = UPIO_MEM32;
182273abaf87SPeter Hurley 		p += 7;
18236e63be3fSNoam Camus 	} else if (strncmp(p, "mmio32be,", 9) == 0) {
18246e63be3fSNoam Camus 		*iotype = UPIO_MEM32BE;
18256e63be3fSNoam Camus 		p += 9;
1826d215d809SMax Filippov 	} else if (strncmp(p, "mmio32native,", 13) == 0) {
1827d215d809SMax Filippov 		*iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1828d215d809SMax Filippov 			UPIO_MEM32BE : UPIO_MEM32;
1829d215d809SMax Filippov 		p += 13;
183073abaf87SPeter Hurley 	} else if (strncmp(p, "io,", 3) == 0) {
183173abaf87SPeter Hurley 		*iotype = UPIO_PORT;
183273abaf87SPeter Hurley 		p += 3;
183373abaf87SPeter Hurley 	} else if (strncmp(p, "0x", 2) == 0) {
183473abaf87SPeter Hurley 		*iotype = UPIO_MEM;
183573abaf87SPeter Hurley 	} else {
183673abaf87SPeter Hurley 		return -EINVAL;
183773abaf87SPeter Hurley 	}
183873abaf87SPeter Hurley 
183973abaf87SPeter Hurley 	*addr = simple_strtoul(p, NULL, 0);
184073abaf87SPeter Hurley 	p = strchr(p, ',');
184173abaf87SPeter Hurley 	if (p)
184273abaf87SPeter Hurley 		p++;
184373abaf87SPeter Hurley 
184473abaf87SPeter Hurley 	*options = p;
184573abaf87SPeter Hurley 	return 0;
184673abaf87SPeter Hurley }
184773abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon);
184873abaf87SPeter Hurley 
184973abaf87SPeter Hurley /**
185002088ca6SGeert Uytterhoeven  *	uart_parse_options - Parse serial port baud/parity/bits/flow control.
1851ab4382d2SGreg Kroah-Hartman  *	@options: pointer to option string
1852ab4382d2SGreg Kroah-Hartman  *	@baud: pointer to an 'int' variable for the baud rate.
1853ab4382d2SGreg Kroah-Hartman  *	@parity: pointer to an 'int' variable for the parity.
1854ab4382d2SGreg Kroah-Hartman  *	@bits: pointer to an 'int' variable for the number of data bits.
1855ab4382d2SGreg Kroah-Hartman  *	@flow: pointer to an 'int' variable for the flow control character.
1856ab4382d2SGreg Kroah-Hartman  *
1857ab4382d2SGreg Kroah-Hartman  *	uart_parse_options decodes a string containing the serial console
1858ab4382d2SGreg Kroah-Hartman  *	options.  The format of the string is <baud><parity><bits><flow>,
1859ab4382d2SGreg Kroah-Hartman  *	eg: 115200n8r
1860ab4382d2SGreg Kroah-Hartman  */
1861ab4382d2SGreg Kroah-Hartman void
1862ab4382d2SGreg Kroah-Hartman uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1863ab4382d2SGreg Kroah-Hartman {
1864ab4382d2SGreg Kroah-Hartman 	char *s = options;
1865ab4382d2SGreg Kroah-Hartman 
1866ab4382d2SGreg Kroah-Hartman 	*baud = simple_strtoul(s, NULL, 10);
1867ab4382d2SGreg Kroah-Hartman 	while (*s >= '0' && *s <= '9')
1868ab4382d2SGreg Kroah-Hartman 		s++;
1869ab4382d2SGreg Kroah-Hartman 	if (*s)
1870ab4382d2SGreg Kroah-Hartman 		*parity = *s++;
1871ab4382d2SGreg Kroah-Hartman 	if (*s)
1872ab4382d2SGreg Kroah-Hartman 		*bits = *s++ - '0';
1873ab4382d2SGreg Kroah-Hartman 	if (*s)
1874ab4382d2SGreg Kroah-Hartman 		*flow = *s;
1875ab4382d2SGreg Kroah-Hartman }
1876ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options);
1877ab4382d2SGreg Kroah-Hartman 
1878ab4382d2SGreg Kroah-Hartman /**
1879ab4382d2SGreg Kroah-Hartman  *	uart_set_options - setup the serial console parameters
1880ab4382d2SGreg Kroah-Hartman  *	@port: pointer to the serial ports uart_port structure
1881ab4382d2SGreg Kroah-Hartman  *	@co: console pointer
1882ab4382d2SGreg Kroah-Hartman  *	@baud: baud rate
1883ab4382d2SGreg Kroah-Hartman  *	@parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1884ab4382d2SGreg Kroah-Hartman  *	@bits: number of data bits
1885ab4382d2SGreg Kroah-Hartman  *	@flow: flow control character - 'r' (rts)
1886ab4382d2SGreg Kroah-Hartman  */
1887ab4382d2SGreg Kroah-Hartman int
1888ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co,
1889ab4382d2SGreg Kroah-Hartman 		 int baud, int parity, int bits, int flow)
1890ab4382d2SGreg Kroah-Hartman {
1891ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
1892ab4382d2SGreg Kroah-Hartman 	static struct ktermios dummy;
1893ab4382d2SGreg Kroah-Hartman 
1894ab4382d2SGreg Kroah-Hartman 	/*
1895ab4382d2SGreg Kroah-Hartman 	 * Ensure that the serial console lock is initialised
1896ab4382d2SGreg Kroah-Hartman 	 * early.
189742b6a1baSRandy Witt 	 * If this port is a console, then the spinlock is already
189842b6a1baSRandy Witt 	 * initialised.
1899ab4382d2SGreg Kroah-Hartman 	 */
190042b6a1baSRandy Witt 	if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1901ab4382d2SGreg Kroah-Hartman 		spin_lock_init(&port->lock);
1902ab4382d2SGreg Kroah-Hartman 		lockdep_set_class(&port->lock, &port_lock_key);
190342b6a1baSRandy Witt 	}
1904ab4382d2SGreg Kroah-Hartman 
1905ab4382d2SGreg Kroah-Hartman 	memset(&termios, 0, sizeof(struct ktermios));
1906ab4382d2SGreg Kroah-Hartman 
1907ba47f97aSJeffy Chen 	termios.c_cflag |= CREAD | HUPCL | CLOCAL;
1908ba47f97aSJeffy Chen 	tty_termios_encode_baud_rate(&termios, baud, baud);
1909ab4382d2SGreg Kroah-Hartman 
1910ab4382d2SGreg Kroah-Hartman 	if (bits == 7)
1911ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS7;
1912ab4382d2SGreg Kroah-Hartman 	else
1913ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS8;
1914ab4382d2SGreg Kroah-Hartman 
1915ab4382d2SGreg Kroah-Hartman 	switch (parity) {
1916ab4382d2SGreg Kroah-Hartman 	case 'o': case 'O':
1917ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARODD;
1918ab4382d2SGreg Kroah-Hartman 		/*fall through*/
1919ab4382d2SGreg Kroah-Hartman 	case 'e': case 'E':
1920ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARENB;
1921ab4382d2SGreg Kroah-Hartman 		break;
1922ab4382d2SGreg Kroah-Hartman 	}
1923ab4382d2SGreg Kroah-Hartman 
1924ab4382d2SGreg Kroah-Hartman 	if (flow == 'r')
1925ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CRTSCTS;
1926ab4382d2SGreg Kroah-Hartman 
1927ab4382d2SGreg Kroah-Hartman 	/*
1928ab4382d2SGreg Kroah-Hartman 	 * some uarts on other side don't support no flow control.
1929ab4382d2SGreg Kroah-Hartman 	 * So we set * DTR in host uart to make them happy
1930ab4382d2SGreg Kroah-Hartman 	 */
1931ab4382d2SGreg Kroah-Hartman 	port->mctrl |= TIOCM_DTR;
1932ab4382d2SGreg Kroah-Hartman 
1933ab4382d2SGreg Kroah-Hartman 	port->ops->set_termios(port, &termios, &dummy);
1934ab4382d2SGreg Kroah-Hartman 	/*
1935ab4382d2SGreg Kroah-Hartman 	 * Allow the setting of the UART parameters with a NULL console
1936ab4382d2SGreg Kroah-Hartman 	 * too:
1937ab4382d2SGreg Kroah-Hartman 	 */
1938ab4382d2SGreg Kroah-Hartman 	if (co)
1939ab4382d2SGreg Kroah-Hartman 		co->cflag = termios.c_cflag;
1940ab4382d2SGreg Kroah-Hartman 
1941ab4382d2SGreg Kroah-Hartman 	return 0;
1942ab4382d2SGreg Kroah-Hartman }
1943ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options);
1944ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */
1945ab4382d2SGreg Kroah-Hartman 
1946cf75525fSJiri Slaby /**
1947cf75525fSJiri Slaby  * uart_change_pm - set power state of the port
1948cf75525fSJiri Slaby  *
1949cf75525fSJiri Slaby  * @state: port descriptor
1950cf75525fSJiri Slaby  * @pm_state: new state
1951cf75525fSJiri Slaby  *
1952cf75525fSJiri Slaby  * Locking: port->mutex has to be held
1953cf75525fSJiri Slaby  */
19546f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
19556f538fe3SLinus Walleij 			   enum uart_pm_state pm_state)
1956ab4382d2SGreg Kroah-Hartman {
1957ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
1958ab4382d2SGreg Kroah-Hartman 
1959ab4382d2SGreg Kroah-Hartman 	if (state->pm_state != pm_state) {
1960ab4382d2SGreg Kroah-Hartman 		if (port->ops->pm)
1961ab4382d2SGreg Kroah-Hartman 			port->ops->pm(port, pm_state, state->pm_state);
1962ab4382d2SGreg Kroah-Hartman 		state->pm_state = pm_state;
1963ab4382d2SGreg Kroah-Hartman 	}
1964ab4382d2SGreg Kroah-Hartman }
1965ab4382d2SGreg Kroah-Hartman 
1966ab4382d2SGreg Kroah-Hartman struct uart_match {
1967ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
1968ab4382d2SGreg Kroah-Hartman 	struct uart_driver *driver;
1969ab4382d2SGreg Kroah-Hartman };
1970ab4382d2SGreg Kroah-Hartman 
1971ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data)
1972ab4382d2SGreg Kroah-Hartman {
1973ab4382d2SGreg Kroah-Hartman 	struct uart_match *match = data;
1974ab4382d2SGreg Kroah-Hartman 	struct tty_driver *tty_drv = match->driver->tty_driver;
1975ab4382d2SGreg Kroah-Hartman 	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1976ab4382d2SGreg Kroah-Hartman 		match->port->line;
1977ab4382d2SGreg Kroah-Hartman 
1978ab4382d2SGreg Kroah-Hartman 	return dev->devt == devt; /* Actually, only one tty per port */
1979ab4382d2SGreg Kroah-Hartman }
1980ab4382d2SGreg Kroah-Hartman 
1981ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1982ab4382d2SGreg Kroah-Hartman {
1983ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
1984ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1985ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
1986ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
1987ab4382d2SGreg Kroah-Hartman 
1988ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1989ab4382d2SGreg Kroah-Hartman 
1990ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1991ab4382d2SGreg Kroah-Hartman 	if (device_may_wakeup(tty_dev)) {
1992ab4382d2SGreg Kroah-Hartman 		if (!enable_irq_wake(uport->irq))
1993ab4382d2SGreg Kroah-Hartman 			uport->irq_wake = 1;
1994ab4382d2SGreg Kroah-Hartman 		put_device(tty_dev);
1995ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
1996ab4382d2SGreg Kroah-Hartman 		return 0;
1997ab4382d2SGreg Kroah-Hartman 	}
19985a65dcc0SFederico Vaga 	put_device(tty_dev);
19995a65dcc0SFederico Vaga 
2000b164c972SPeter Hurley 	/* Nothing to do if the console is not suspending */
2001b164c972SPeter Hurley 	if (!console_suspend_enabled && uart_console(uport))
2002b164c972SPeter Hurley 		goto unlock;
2003b164c972SPeter Hurley 
2004ab4382d2SGreg Kroah-Hartman 	uport->suspended = 1;
2005ab4382d2SGreg Kroah-Hartman 
2006ab4382d2SGreg Kroah-Hartman 	if (port->flags & ASYNC_INITIALIZED) {
2007ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2008ab4382d2SGreg Kroah-Hartman 		int tries;
2009ab4382d2SGreg Kroah-Hartman 
2010ab4382d2SGreg Kroah-Hartman 		set_bit(ASYNCB_SUSPENDED, &port->flags);
2011ab4382d2SGreg Kroah-Hartman 		clear_bit(ASYNCB_INITIALIZED, &port->flags);
2012ab4382d2SGreg Kroah-Hartman 
2013ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2014ab4382d2SGreg Kroah-Hartman 		ops->stop_tx(uport);
2015ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
2016ab4382d2SGreg Kroah-Hartman 		ops->stop_rx(uport);
2017ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2018ab4382d2SGreg Kroah-Hartman 
2019ab4382d2SGreg Kroah-Hartman 		/*
2020ab4382d2SGreg Kroah-Hartman 		 * Wait for the transmitter to empty.
2021ab4382d2SGreg Kroah-Hartman 		 */
2022ab4382d2SGreg Kroah-Hartman 		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2023ab4382d2SGreg Kroah-Hartman 			msleep(10);
2024ab4382d2SGreg Kroah-Hartman 		if (!tries)
20252f2dafe7SSudip Mukherjee 			dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2026ab4382d2SGreg Kroah-Hartman 				drv->dev_name,
2027ab4382d2SGreg Kroah-Hartman 				drv->tty_driver->name_base + uport->line);
2028ab4382d2SGreg Kroah-Hartman 
2029ab4382d2SGreg Kroah-Hartman 		ops->shutdown(uport);
2030ab4382d2SGreg Kroah-Hartman 	}
2031ab4382d2SGreg Kroah-Hartman 
2032ab4382d2SGreg Kroah-Hartman 	/*
2033ab4382d2SGreg Kroah-Hartman 	 * Disable the console device before suspending.
2034ab4382d2SGreg Kroah-Hartman 	 */
2035b164c972SPeter Hurley 	if (uart_console(uport))
2036ab4382d2SGreg Kroah-Hartman 		console_stop(uport->cons);
2037ab4382d2SGreg Kroah-Hartman 
20386f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
2039b164c972SPeter Hurley unlock:
2040ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2041ab4382d2SGreg Kroah-Hartman 
2042ab4382d2SGreg Kroah-Hartman 	return 0;
2043ab4382d2SGreg Kroah-Hartman }
2044ab4382d2SGreg Kroah-Hartman 
2045ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2046ab4382d2SGreg Kroah-Hartman {
2047ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2048ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2049ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2050ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2051ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2052ab4382d2SGreg Kroah-Hartman 
2053ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2054ab4382d2SGreg Kroah-Hartman 
2055ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2056ab4382d2SGreg Kroah-Hartman 	if (!uport->suspended && device_may_wakeup(tty_dev)) {
2057ab4382d2SGreg Kroah-Hartman 		if (uport->irq_wake) {
2058ab4382d2SGreg Kroah-Hartman 			disable_irq_wake(uport->irq);
2059ab4382d2SGreg Kroah-Hartman 			uport->irq_wake = 0;
2060ab4382d2SGreg Kroah-Hartman 		}
20615a65dcc0SFederico Vaga 		put_device(tty_dev);
2062ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2063ab4382d2SGreg Kroah-Hartman 		return 0;
2064ab4382d2SGreg Kroah-Hartman 	}
20655a65dcc0SFederico Vaga 	put_device(tty_dev);
2066ab4382d2SGreg Kroah-Hartman 	uport->suspended = 0;
2067ab4382d2SGreg Kroah-Hartman 
2068ab4382d2SGreg Kroah-Hartman 	/*
2069ab4382d2SGreg Kroah-Hartman 	 * Re-enable the console device after suspending.
2070ab4382d2SGreg Kroah-Hartman 	 */
20715933a161SYin Kangkai 	if (uart_console(uport)) {
2072ab4382d2SGreg Kroah-Hartman 		/*
2073ab4382d2SGreg Kroah-Hartman 		 * First try to use the console cflag setting.
2074ab4382d2SGreg Kroah-Hartman 		 */
2075ab4382d2SGreg Kroah-Hartman 		memset(&termios, 0, sizeof(struct ktermios));
2076ab4382d2SGreg Kroah-Hartman 		termios.c_cflag = uport->cons->cflag;
2077ab4382d2SGreg Kroah-Hartman 
2078ab4382d2SGreg Kroah-Hartman 		/*
2079ab4382d2SGreg Kroah-Hartman 		 * If that's unset, use the tty termios setting.
2080ab4382d2SGreg Kroah-Hartman 		 */
2081adc8d746SAlan Cox 		if (port->tty && termios.c_cflag == 0)
2082adc8d746SAlan Cox 			termios = port->tty->termios;
2083ab4382d2SGreg Kroah-Hartman 
208494abc56fSNing Jiang 		if (console_suspend_enabled)
20856f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2086ab4382d2SGreg Kroah-Hartman 		uport->ops->set_termios(uport, &termios, NULL);
20875933a161SYin Kangkai 		if (console_suspend_enabled)
2088ab4382d2SGreg Kroah-Hartman 			console_start(uport->cons);
2089ab4382d2SGreg Kroah-Hartman 	}
2090ab4382d2SGreg Kroah-Hartman 
2091ab4382d2SGreg Kroah-Hartman 	if (port->flags & ASYNC_SUSPENDED) {
2092ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2093ab4382d2SGreg Kroah-Hartman 		int ret;
2094ab4382d2SGreg Kroah-Hartman 
20956f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2096ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2097ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
2098ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2099ab4382d2SGreg Kroah-Hartman 		if (console_suspend_enabled || !uart_console(uport)) {
2100ab4382d2SGreg Kroah-Hartman 			/* Protected by port mutex for now */
2101ab4382d2SGreg Kroah-Hartman 			struct tty_struct *tty = port->tty;
2102ab4382d2SGreg Kroah-Hartman 			ret = ops->startup(uport);
2103ab4382d2SGreg Kroah-Hartman 			if (ret == 0) {
2104ab4382d2SGreg Kroah-Hartman 				if (tty)
2105ab4382d2SGreg Kroah-Hartman 					uart_change_speed(tty, state, NULL);
2106ab4382d2SGreg Kroah-Hartman 				spin_lock_irq(&uport->lock);
2107ab4382d2SGreg Kroah-Hartman 				ops->set_mctrl(uport, uport->mctrl);
2108ab4382d2SGreg Kroah-Hartman 				ops->start_tx(uport);
2109ab4382d2SGreg Kroah-Hartman 				spin_unlock_irq(&uport->lock);
2110ab4382d2SGreg Kroah-Hartman 				set_bit(ASYNCB_INITIALIZED, &port->flags);
2111ab4382d2SGreg Kroah-Hartman 			} else {
2112ab4382d2SGreg Kroah-Hartman 				/*
2113ab4382d2SGreg Kroah-Hartman 				 * Failed to resume - maybe hardware went away?
2114ab4382d2SGreg Kroah-Hartman 				 * Clear the "initialized" flag so we won't try
2115ab4382d2SGreg Kroah-Hartman 				 * to call the low level drivers shutdown method.
2116ab4382d2SGreg Kroah-Hartman 				 */
2117ab4382d2SGreg Kroah-Hartman 				uart_shutdown(tty, state);
2118ab4382d2SGreg Kroah-Hartman 			}
2119ab4382d2SGreg Kroah-Hartman 		}
2120ab4382d2SGreg Kroah-Hartman 
2121ab4382d2SGreg Kroah-Hartman 		clear_bit(ASYNCB_SUSPENDED, &port->flags);
2122ab4382d2SGreg Kroah-Hartman 	}
2123ab4382d2SGreg Kroah-Hartman 
2124ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2125ab4382d2SGreg Kroah-Hartman 
2126ab4382d2SGreg Kroah-Hartman 	return 0;
2127ab4382d2SGreg Kroah-Hartman }
2128ab4382d2SGreg Kroah-Hartman 
2129ab4382d2SGreg Kroah-Hartman static inline void
2130ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port)
2131ab4382d2SGreg Kroah-Hartman {
2132ab4382d2SGreg Kroah-Hartman 	char address[64];
2133ab4382d2SGreg Kroah-Hartman 
2134ab4382d2SGreg Kroah-Hartman 	switch (port->iotype) {
2135ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2136ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2137ab4382d2SGreg Kroah-Hartman 		break;
2138ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2139ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2140ab4382d2SGreg Kroah-Hartman 			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2141ab4382d2SGreg Kroah-Hartman 		break;
2142ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2143bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2144ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
21453ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2146ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2147ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2148ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2149ab4382d2SGreg Kroah-Hartman 			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2150ab4382d2SGreg Kroah-Hartman 		break;
2151ab4382d2SGreg Kroah-Hartman 	default:
2152ab4382d2SGreg Kroah-Hartman 		strlcpy(address, "*unknown*", sizeof(address));
2153ab4382d2SGreg Kroah-Hartman 		break;
2154ab4382d2SGreg Kroah-Hartman 	}
2155ab4382d2SGreg Kroah-Hartman 
215668ed7e1cSJames Bottomley 	printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
215768ed7e1cSJames Bottomley 	       port->dev ? dev_name(port->dev) : "",
215868ed7e1cSJames Bottomley 	       port->dev ? ": " : "",
2159ab4382d2SGreg Kroah-Hartman 	       drv->dev_name,
2160ab4382d2SGreg Kroah-Hartman 	       drv->tty_driver->name_base + port->line,
21617d12b976SKees Cook 	       address, port->irq, port->uartclk / 16, uart_type(port));
2162ab4382d2SGreg Kroah-Hartman }
2163ab4382d2SGreg Kroah-Hartman 
2164ab4382d2SGreg Kroah-Hartman static void
2165ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2166ab4382d2SGreg Kroah-Hartman 		    struct uart_port *port)
2167ab4382d2SGreg Kroah-Hartman {
2168ab4382d2SGreg Kroah-Hartman 	unsigned int flags;
2169ab4382d2SGreg Kroah-Hartman 
2170ab4382d2SGreg Kroah-Hartman 	/*
2171ab4382d2SGreg Kroah-Hartman 	 * If there isn't a port here, don't do anything further.
2172ab4382d2SGreg Kroah-Hartman 	 */
2173ab4382d2SGreg Kroah-Hartman 	if (!port->iobase && !port->mapbase && !port->membase)
2174ab4382d2SGreg Kroah-Hartman 		return;
2175ab4382d2SGreg Kroah-Hartman 
2176ab4382d2SGreg Kroah-Hartman 	/*
2177ab4382d2SGreg Kroah-Hartman 	 * Now do the auto configuration stuff.  Note that config_port
2178ab4382d2SGreg Kroah-Hartman 	 * is expected to claim the resources and map the port for us.
2179ab4382d2SGreg Kroah-Hartman 	 */
2180ab4382d2SGreg Kroah-Hartman 	flags = 0;
2181ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_AUTO_IRQ)
2182ab4382d2SGreg Kroah-Hartman 		flags |= UART_CONFIG_IRQ;
2183ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_BOOT_AUTOCONF) {
2184ab4382d2SGreg Kroah-Hartman 		if (!(port->flags & UPF_FIXED_TYPE)) {
2185ab4382d2SGreg Kroah-Hartman 			port->type = PORT_UNKNOWN;
2186ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_TYPE;
2187ab4382d2SGreg Kroah-Hartman 		}
2188ab4382d2SGreg Kroah-Hartman 		port->ops->config_port(port, flags);
2189ab4382d2SGreg Kroah-Hartman 	}
2190ab4382d2SGreg Kroah-Hartman 
2191ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_UNKNOWN) {
2192ab4382d2SGreg Kroah-Hartman 		unsigned long flags;
2193ab4382d2SGreg Kroah-Hartman 
2194ab4382d2SGreg Kroah-Hartman 		uart_report_port(drv, port);
2195ab4382d2SGreg Kroah-Hartman 
2196ab4382d2SGreg Kroah-Hartman 		/* Power up port for set_mctrl() */
21976f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2198ab4382d2SGreg Kroah-Hartman 
2199ab4382d2SGreg Kroah-Hartman 		/*
2200ab4382d2SGreg Kroah-Hartman 		 * Ensure that the modem control lines are de-activated.
2201ab4382d2SGreg Kroah-Hartman 		 * keep the DTR setting that is set in uart_set_options()
2202ab4382d2SGreg Kroah-Hartman 		 * We probably don't need a spinlock around this, but
2203ab4382d2SGreg Kroah-Hartman 		 */
2204ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
2205ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2206ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
2207ab4382d2SGreg Kroah-Hartman 
2208ab4382d2SGreg Kroah-Hartman 		/*
2209ab4382d2SGreg Kroah-Hartman 		 * If this driver supports console, and it hasn't been
2210ab4382d2SGreg Kroah-Hartman 		 * successfully registered yet, try to re-register it.
2211ab4382d2SGreg Kroah-Hartman 		 * It may be that the port was not available.
2212ab4382d2SGreg Kroah-Hartman 		 */
2213ab4382d2SGreg Kroah-Hartman 		if (port->cons && !(port->cons->flags & CON_ENABLED))
2214ab4382d2SGreg Kroah-Hartman 			register_console(port->cons);
2215ab4382d2SGreg Kroah-Hartman 
2216ab4382d2SGreg Kroah-Hartman 		/*
2217ab4382d2SGreg Kroah-Hartman 		 * Power down all ports by default, except the
2218ab4382d2SGreg Kroah-Hartman 		 * console if we have one.
2219ab4382d2SGreg Kroah-Hartman 		 */
2220ab4382d2SGreg Kroah-Hartman 		if (!uart_console(port))
22216f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_OFF);
2222ab4382d2SGreg Kroah-Hartman 	}
2223ab4382d2SGreg Kroah-Hartman }
2224ab4382d2SGreg Kroah-Hartman 
2225ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2226ab4382d2SGreg Kroah-Hartman 
2227ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2228ab4382d2SGreg Kroah-Hartman {
2229ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2230ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2231ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2232ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
2233ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2234ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2235ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2236c7f3e708SAnton Vorontsov 	int ret;
2237ab4382d2SGreg Kroah-Hartman 
2238ab4382d2SGreg Kroah-Hartman 	if (!state || !state->uart_port)
2239ab4382d2SGreg Kroah-Hartman 		return -1;
2240ab4382d2SGreg Kroah-Hartman 
2241ab4382d2SGreg Kroah-Hartman 	port = state->uart_port;
2242ab4382d2SGreg Kroah-Hartman 	if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2243ab4382d2SGreg Kroah-Hartman 		return -1;
2244ab4382d2SGreg Kroah-Hartman 
2245c7f3e708SAnton Vorontsov 	if (port->ops->poll_init) {
2246c7f3e708SAnton Vorontsov 		struct tty_port *tport = &state->port;
2247c7f3e708SAnton Vorontsov 
2248c7f3e708SAnton Vorontsov 		ret = 0;
2249c7f3e708SAnton Vorontsov 		mutex_lock(&tport->mutex);
2250c7f3e708SAnton Vorontsov 		/*
2251c7f3e708SAnton Vorontsov 		 * We don't set ASYNCB_INITIALIZED as we only initialized the
2252c7f3e708SAnton Vorontsov 		 * hw, e.g. state->xmit is still uninitialized.
2253c7f3e708SAnton Vorontsov 		 */
2254c7f3e708SAnton Vorontsov 		if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
2255c7f3e708SAnton Vorontsov 			ret = port->ops->poll_init(port);
2256c7f3e708SAnton Vorontsov 		mutex_unlock(&tport->mutex);
2257c7f3e708SAnton Vorontsov 		if (ret)
2258c7f3e708SAnton Vorontsov 			return ret;
2259c7f3e708SAnton Vorontsov 	}
2260c7f3e708SAnton Vorontsov 
2261ab4382d2SGreg Kroah-Hartman 	if (options) {
2262ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2263ab4382d2SGreg Kroah-Hartman 		return uart_set_options(port, NULL, baud, parity, bits, flow);
2264ab4382d2SGreg Kroah-Hartman 	}
2265ab4382d2SGreg Kroah-Hartman 
2266ab4382d2SGreg Kroah-Hartman 	return 0;
2267ab4382d2SGreg Kroah-Hartman }
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line)
2270ab4382d2SGreg Kroah-Hartman {
2271ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2272ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2273ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2274ab4382d2SGreg Kroah-Hartman 
2275ab4382d2SGreg Kroah-Hartman 	if (!state || !state->uart_port)
2276ab4382d2SGreg Kroah-Hartman 		return -1;
2277ab4382d2SGreg Kroah-Hartman 
2278ab4382d2SGreg Kroah-Hartman 	port = state->uart_port;
2279ab4382d2SGreg Kroah-Hartman 	return port->ops->poll_get_char(port);
2280ab4382d2SGreg Kroah-Hartman }
2281ab4382d2SGreg Kroah-Hartman 
2282ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2283ab4382d2SGreg Kroah-Hartman {
2284ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2285ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2286ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2287ab4382d2SGreg Kroah-Hartman 
2288ab4382d2SGreg Kroah-Hartman 	if (!state || !state->uart_port)
2289ab4382d2SGreg Kroah-Hartman 		return;
2290ab4382d2SGreg Kroah-Hartman 
2291ab4382d2SGreg Kroah-Hartman 	port = state->uart_port;
2292c7d44a02SDoug Anderson 
2293c7d44a02SDoug Anderson 	if (ch == '\n')
2294c7d44a02SDoug Anderson 		port->ops->poll_put_char(port, '\r');
2295ab4382d2SGreg Kroah-Hartman 	port->ops->poll_put_char(port, ch);
2296ab4382d2SGreg Kroah-Hartman }
2297ab4382d2SGreg Kroah-Hartman #endif
2298ab4382d2SGreg Kroah-Hartman 
2299ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = {
2300ab4382d2SGreg Kroah-Hartman 	.open		= uart_open,
2301ab4382d2SGreg Kroah-Hartman 	.close		= uart_close,
2302ab4382d2SGreg Kroah-Hartman 	.write		= uart_write,
2303ab4382d2SGreg Kroah-Hartman 	.put_char	= uart_put_char,
2304ab4382d2SGreg Kroah-Hartman 	.flush_chars	= uart_flush_chars,
2305ab4382d2SGreg Kroah-Hartman 	.write_room	= uart_write_room,
2306ab4382d2SGreg Kroah-Hartman 	.chars_in_buffer= uart_chars_in_buffer,
2307ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= uart_flush_buffer,
2308ab4382d2SGreg Kroah-Hartman 	.ioctl		= uart_ioctl,
2309ab4382d2SGreg Kroah-Hartman 	.throttle	= uart_throttle,
2310ab4382d2SGreg Kroah-Hartman 	.unthrottle	= uart_unthrottle,
2311ab4382d2SGreg Kroah-Hartman 	.send_xchar	= uart_send_xchar,
2312ab4382d2SGreg Kroah-Hartman 	.set_termios	= uart_set_termios,
2313ab4382d2SGreg Kroah-Hartman 	.set_ldisc	= uart_set_ldisc,
2314ab4382d2SGreg Kroah-Hartman 	.stop		= uart_stop,
2315ab4382d2SGreg Kroah-Hartman 	.start		= uart_start,
2316ab4382d2SGreg Kroah-Hartman 	.hangup		= uart_hangup,
2317ab4382d2SGreg Kroah-Hartman 	.break_ctl	= uart_break_ctl,
2318ab4382d2SGreg Kroah-Hartman 	.wait_until_sent= uart_wait_until_sent,
2319ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
2320ab4382d2SGreg Kroah-Hartman 	.proc_fops	= &uart_proc_fops,
2321ab4382d2SGreg Kroah-Hartman #endif
2322ab4382d2SGreg Kroah-Hartman 	.tiocmget	= uart_tiocmget,
2323ab4382d2SGreg Kroah-Hartman 	.tiocmset	= uart_tiocmset,
2324ab4382d2SGreg Kroah-Hartman 	.get_icount	= uart_get_icount,
2325ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2326ab4382d2SGreg Kroah-Hartman 	.poll_init	= uart_poll_init,
2327ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= uart_poll_get_char,
2328ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= uart_poll_put_char,
2329ab4382d2SGreg Kroah-Hartman #endif
2330ab4382d2SGreg Kroah-Hartman };
2331ab4382d2SGreg Kroah-Hartman 
2332ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = {
2333ab4382d2SGreg Kroah-Hartman 	.carrier_raised = uart_carrier_raised,
2334ab4382d2SGreg Kroah-Hartman 	.dtr_rts	= uart_dtr_rts,
2335ab4382d2SGreg Kroah-Hartman };
2336ab4382d2SGreg Kroah-Hartman 
2337ab4382d2SGreg Kroah-Hartman /**
2338ab4382d2SGreg Kroah-Hartman  *	uart_register_driver - register a driver with the uart core layer
2339ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2340ab4382d2SGreg Kroah-Hartman  *
2341ab4382d2SGreg Kroah-Hartman  *	Register a uart driver with the core driver.  We in turn register
2342ab4382d2SGreg Kroah-Hartman  *	with the tty layer, and initialise the core driver per-port state.
2343ab4382d2SGreg Kroah-Hartman  *
2344ab4382d2SGreg Kroah-Hartman  *	We have a proc file in /proc/tty/driver which is named after the
2345ab4382d2SGreg Kroah-Hartman  *	normal driver.
2346ab4382d2SGreg Kroah-Hartman  *
2347ab4382d2SGreg Kroah-Hartman  *	drv->port should be NULL, and the per-port structures should be
2348ab4382d2SGreg Kroah-Hartman  *	registered using uart_add_one_port after this call has succeeded.
2349ab4382d2SGreg Kroah-Hartman  */
2350ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv)
2351ab4382d2SGreg Kroah-Hartman {
2352ab4382d2SGreg Kroah-Hartman 	struct tty_driver *normal;
2353ab4382d2SGreg Kroah-Hartman 	int i, retval;
2354ab4382d2SGreg Kroah-Hartman 
2355ab4382d2SGreg Kroah-Hartman 	BUG_ON(drv->state);
2356ab4382d2SGreg Kroah-Hartman 
2357ab4382d2SGreg Kroah-Hartman 	/*
2358ab4382d2SGreg Kroah-Hartman 	 * Maybe we should be using a slab cache for this, especially if
2359ab4382d2SGreg Kroah-Hartman 	 * we have a large number of ports to handle.
2360ab4382d2SGreg Kroah-Hartman 	 */
2361ab4382d2SGreg Kroah-Hartman 	drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2362ab4382d2SGreg Kroah-Hartman 	if (!drv->state)
2363ab4382d2SGreg Kroah-Hartman 		goto out;
2364ab4382d2SGreg Kroah-Hartman 
2365ab4382d2SGreg Kroah-Hartman 	normal = alloc_tty_driver(drv->nr);
2366ab4382d2SGreg Kroah-Hartman 	if (!normal)
2367ab4382d2SGreg Kroah-Hartman 		goto out_kfree;
2368ab4382d2SGreg Kroah-Hartman 
2369ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = normal;
2370ab4382d2SGreg Kroah-Hartman 
2371ab4382d2SGreg Kroah-Hartman 	normal->driver_name	= drv->driver_name;
2372ab4382d2SGreg Kroah-Hartman 	normal->name		= drv->dev_name;
2373ab4382d2SGreg Kroah-Hartman 	normal->major		= drv->major;
2374ab4382d2SGreg Kroah-Hartman 	normal->minor_start	= drv->minor;
2375ab4382d2SGreg Kroah-Hartman 	normal->type		= TTY_DRIVER_TYPE_SERIAL;
2376ab4382d2SGreg Kroah-Hartman 	normal->subtype		= SERIAL_TYPE_NORMAL;
2377ab4382d2SGreg Kroah-Hartman 	normal->init_termios	= tty_std_termios;
2378ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2379ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2380ab4382d2SGreg Kroah-Hartman 	normal->flags		= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2381ab4382d2SGreg Kroah-Hartman 	normal->driver_state    = drv;
2382ab4382d2SGreg Kroah-Hartman 	tty_set_operations(normal, &uart_ops);
2383ab4382d2SGreg Kroah-Hartman 
2384ab4382d2SGreg Kroah-Hartman 	/*
2385ab4382d2SGreg Kroah-Hartman 	 * Initialise the UART state(s).
2386ab4382d2SGreg Kroah-Hartman 	 */
2387ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++) {
2388ab4382d2SGreg Kroah-Hartman 		struct uart_state *state = drv->state + i;
2389ab4382d2SGreg Kroah-Hartman 		struct tty_port *port = &state->port;
2390ab4382d2SGreg Kroah-Hartman 
2391ab4382d2SGreg Kroah-Hartman 		tty_port_init(port);
2392ab4382d2SGreg Kroah-Hartman 		port->ops = &uart_port_ops;
2393ab4382d2SGreg Kroah-Hartman 	}
2394ab4382d2SGreg Kroah-Hartman 
2395ab4382d2SGreg Kroah-Hartman 	retval = tty_register_driver(normal);
2396ab4382d2SGreg Kroah-Hartman 	if (retval >= 0)
2397ab4382d2SGreg Kroah-Hartman 		return retval;
2398ab4382d2SGreg Kroah-Hartman 
2399191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2400191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2401ab4382d2SGreg Kroah-Hartman 	put_tty_driver(normal);
2402ab4382d2SGreg Kroah-Hartman out_kfree:
2403ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
2404ab4382d2SGreg Kroah-Hartman out:
2405ab4382d2SGreg Kroah-Hartman 	return -ENOMEM;
2406ab4382d2SGreg Kroah-Hartman }
2407ab4382d2SGreg Kroah-Hartman 
2408ab4382d2SGreg Kroah-Hartman /**
2409ab4382d2SGreg Kroah-Hartman  *	uart_unregister_driver - remove a driver from the uart core layer
2410ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2411ab4382d2SGreg Kroah-Hartman  *
2412ab4382d2SGreg Kroah-Hartman  *	Remove all references to a driver from the core driver.  The low
2413ab4382d2SGreg Kroah-Hartman  *	level driver must have removed all its ports via the
2414ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port() if it registered them with uart_add_one_port().
2415ab4382d2SGreg Kroah-Hartman  *	(ie, drv->port == NULL)
2416ab4382d2SGreg Kroah-Hartman  */
2417ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv)
2418ab4382d2SGreg Kroah-Hartman {
2419ab4382d2SGreg Kroah-Hartman 	struct tty_driver *p = drv->tty_driver;
2420191c5f10SJiri Slaby 	unsigned int i;
2421191c5f10SJiri Slaby 
2422ab4382d2SGreg Kroah-Hartman 	tty_unregister_driver(p);
2423ab4382d2SGreg Kroah-Hartman 	put_tty_driver(p);
2424191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2425191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2426ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
24271e66cdedSAlan Cox 	drv->state = NULL;
2428ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = NULL;
2429ab4382d2SGreg Kroah-Hartman }
2430ab4382d2SGreg Kroah-Hartman 
2431ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index)
2432ab4382d2SGreg Kroah-Hartman {
2433ab4382d2SGreg Kroah-Hartman 	struct uart_driver *p = co->data;
2434ab4382d2SGreg Kroah-Hartman 	*index = co->index;
2435ab4382d2SGreg Kroah-Hartman 	return p->tty_driver;
2436ab4382d2SGreg Kroah-Hartman }
2437ab4382d2SGreg Kroah-Hartman 
24386915c0e4STomas Hlavacek static ssize_t uart_get_attr_uartclk(struct device *dev,
24396915c0e4STomas Hlavacek 	struct device_attribute *attr, char *buf)
24406915c0e4STomas Hlavacek {
2441bebe73e3SAlan Cox 	struct serial_struct tmp;
24426915c0e4STomas Hlavacek 	struct tty_port *port = dev_get_drvdata(dev);
2443b1b79916STomas Hlavacek 
24449f109694SAlan Cox 	uart_get_info(port, &tmp);
2445bebe73e3SAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
24466915c0e4STomas Hlavacek }
24476915c0e4STomas Hlavacek 
2448373bac4cSAlan Cox static ssize_t uart_get_attr_type(struct device *dev,
2449373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2450373bac4cSAlan Cox {
2451373bac4cSAlan Cox 	struct serial_struct tmp;
2452373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2453373bac4cSAlan Cox 
2454373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2455373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2456373bac4cSAlan Cox }
2457373bac4cSAlan Cox static ssize_t uart_get_attr_line(struct device *dev,
2458373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2459373bac4cSAlan Cox {
2460373bac4cSAlan Cox 	struct serial_struct tmp;
2461373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2462373bac4cSAlan Cox 
2463373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2464373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2465373bac4cSAlan Cox }
2466373bac4cSAlan Cox 
2467373bac4cSAlan Cox static ssize_t uart_get_attr_port(struct device *dev,
2468373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2469373bac4cSAlan Cox {
2470373bac4cSAlan Cox 	struct serial_struct tmp;
2471373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2472fd985e1dSAndrew Morton 	unsigned long ioaddr;
2473373bac4cSAlan Cox 
2474373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2475fd985e1dSAndrew Morton 	ioaddr = tmp.port;
2476fd985e1dSAndrew Morton 	if (HIGH_BITS_OFFSET)
2477fd985e1dSAndrew Morton 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2478fd985e1dSAndrew Morton 	return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2479373bac4cSAlan Cox }
2480373bac4cSAlan Cox 
2481373bac4cSAlan Cox static ssize_t uart_get_attr_irq(struct device *dev,
2482373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2483373bac4cSAlan Cox {
2484373bac4cSAlan Cox 	struct serial_struct tmp;
2485373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2486373bac4cSAlan Cox 
2487373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2488373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2489373bac4cSAlan Cox }
2490373bac4cSAlan Cox 
2491373bac4cSAlan Cox static ssize_t uart_get_attr_flags(struct device *dev,
2492373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2493373bac4cSAlan Cox {
2494373bac4cSAlan Cox 	struct serial_struct tmp;
2495373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2496373bac4cSAlan Cox 
2497373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2498373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2499373bac4cSAlan Cox }
2500373bac4cSAlan Cox 
2501373bac4cSAlan Cox static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2502373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2503373bac4cSAlan Cox {
2504373bac4cSAlan Cox 	struct serial_struct tmp;
2505373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2506373bac4cSAlan Cox 
2507373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2508373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2509373bac4cSAlan Cox }
2510373bac4cSAlan Cox 
2511373bac4cSAlan Cox 
2512373bac4cSAlan Cox static ssize_t uart_get_attr_close_delay(struct device *dev,
2513373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2514373bac4cSAlan Cox {
2515373bac4cSAlan Cox 	struct serial_struct tmp;
2516373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2517373bac4cSAlan Cox 
2518373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2519373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2520373bac4cSAlan Cox }
2521373bac4cSAlan Cox 
2522373bac4cSAlan Cox 
2523373bac4cSAlan Cox static ssize_t uart_get_attr_closing_wait(struct device *dev,
2524373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2525373bac4cSAlan Cox {
2526373bac4cSAlan Cox 	struct serial_struct tmp;
2527373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2528373bac4cSAlan Cox 
2529373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2530373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2531373bac4cSAlan Cox }
2532373bac4cSAlan Cox 
2533373bac4cSAlan Cox static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2534373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2535373bac4cSAlan Cox {
2536373bac4cSAlan Cox 	struct serial_struct tmp;
2537373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2538373bac4cSAlan Cox 
2539373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2540373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2541373bac4cSAlan Cox }
2542373bac4cSAlan Cox 
2543373bac4cSAlan Cox static ssize_t uart_get_attr_io_type(struct device *dev,
2544373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2545373bac4cSAlan Cox {
2546373bac4cSAlan Cox 	struct serial_struct tmp;
2547373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2548373bac4cSAlan Cox 
2549373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2550373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2551373bac4cSAlan Cox }
2552373bac4cSAlan Cox 
2553373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_base(struct device *dev,
2554373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2555373bac4cSAlan Cox {
2556373bac4cSAlan Cox 	struct serial_struct tmp;
2557373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2558373bac4cSAlan Cox 
2559373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2560373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2561373bac4cSAlan Cox }
2562373bac4cSAlan Cox 
2563373bac4cSAlan Cox static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2564373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2565373bac4cSAlan Cox {
2566373bac4cSAlan Cox 	struct serial_struct tmp;
2567373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2568373bac4cSAlan Cox 
2569373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2570373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2571373bac4cSAlan Cox }
2572373bac4cSAlan Cox 
2573373bac4cSAlan Cox static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2574373bac4cSAlan Cox static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2575373bac4cSAlan Cox static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2576373bac4cSAlan Cox static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2577373bac4cSAlan Cox static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2578373bac4cSAlan Cox static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
25796915c0e4STomas Hlavacek static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2580373bac4cSAlan Cox static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2581373bac4cSAlan Cox static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2582373bac4cSAlan Cox static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2583373bac4cSAlan Cox static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2584373bac4cSAlan Cox static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2585373bac4cSAlan Cox static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
25866915c0e4STomas Hlavacek 
25876915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = {
2588373bac4cSAlan Cox 	&dev_attr_type.attr,
2589373bac4cSAlan Cox 	&dev_attr_line.attr,
2590373bac4cSAlan Cox 	&dev_attr_port.attr,
2591373bac4cSAlan Cox 	&dev_attr_irq.attr,
2592373bac4cSAlan Cox 	&dev_attr_flags.attr,
2593373bac4cSAlan Cox 	&dev_attr_xmit_fifo_size.attr,
25946915c0e4STomas Hlavacek 	&dev_attr_uartclk.attr,
2595373bac4cSAlan Cox 	&dev_attr_close_delay.attr,
2596373bac4cSAlan Cox 	&dev_attr_closing_wait.attr,
2597373bac4cSAlan Cox 	&dev_attr_custom_divisor.attr,
2598373bac4cSAlan Cox 	&dev_attr_io_type.attr,
2599373bac4cSAlan Cox 	&dev_attr_iomem_base.attr,
2600373bac4cSAlan Cox 	&dev_attr_iomem_reg_shift.attr,
26016915c0e4STomas Hlavacek 	NULL,
26026915c0e4STomas Hlavacek 	};
26036915c0e4STomas Hlavacek 
2604b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = {
26056915c0e4STomas Hlavacek 	.attrs = tty_dev_attrs,
26066915c0e4STomas Hlavacek 	};
26076915c0e4STomas Hlavacek 
2608ab4382d2SGreg Kroah-Hartman /**
2609ab4382d2SGreg Kroah-Hartman  *	uart_add_one_port - attach a driver-defined port structure
2610ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
2611ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure to use for this port.
2612ab4382d2SGreg Kroah-Hartman  *
2613ab4382d2SGreg Kroah-Hartman  *	This allows the driver to register its own uart_port structure
2614ab4382d2SGreg Kroah-Hartman  *	with the core driver.  The main purpose is to allow the low
2615ab4382d2SGreg Kroah-Hartman  *	level uart drivers to expand uart_port, rather than having yet
2616ab4382d2SGreg Kroah-Hartman  *	more levels of structures.
2617ab4382d2SGreg Kroah-Hartman  */
2618ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2619ab4382d2SGreg Kroah-Hartman {
2620ab4382d2SGreg Kroah-Hartman 	struct uart_state *state;
2621ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
2622ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2623ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2624266dcff0SGreg Kroah-Hartman 	int num_groups;
2625ab4382d2SGreg Kroah-Hartman 
2626ab4382d2SGreg Kroah-Hartman 	BUG_ON(in_interrupt());
2627ab4382d2SGreg Kroah-Hartman 
2628ab4382d2SGreg Kroah-Hartman 	if (uport->line >= drv->nr)
2629ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
2630ab4382d2SGreg Kroah-Hartman 
2631ab4382d2SGreg Kroah-Hartman 	state = drv->state + uport->line;
2632ab4382d2SGreg Kroah-Hartman 	port = &state->port;
2633ab4382d2SGreg Kroah-Hartman 
2634ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
2635ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2636ab4382d2SGreg Kroah-Hartman 	if (state->uart_port) {
2637ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2638ab4382d2SGreg Kroah-Hartman 		goto out;
2639ab4382d2SGreg Kroah-Hartman 	}
2640ab4382d2SGreg Kroah-Hartman 
26412b702b9bSPeter Hurley 	/* Link the port to the driver state table and vice versa */
2642ab4382d2SGreg Kroah-Hartman 	state->uart_port = uport;
2643ab4382d2SGreg Kroah-Hartman 	uport->state = state;
2644ab4382d2SGreg Kroah-Hartman 
26452b702b9bSPeter Hurley 	state->pm_state = UART_PM_STATE_UNDEFINED;
26462b702b9bSPeter Hurley 	uport->cons = drv->cons;
2647959801feSPeter Hurley 	uport->minor = drv->tty_driver->minor_start + uport->line;
26482b702b9bSPeter Hurley 
2649ab4382d2SGreg Kroah-Hartman 	/*
2650ab4382d2SGreg Kroah-Hartman 	 * If this port is a console, then the spinlock is already
2651ab4382d2SGreg Kroah-Hartman 	 * initialised.
2652ab4382d2SGreg Kroah-Hartman 	 */
2653ab4382d2SGreg Kroah-Hartman 	if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2654ab4382d2SGreg Kroah-Hartman 		spin_lock_init(&uport->lock);
2655ab4382d2SGreg Kroah-Hartman 		lockdep_set_class(&uport->lock, &port_lock_key);
2656ab4382d2SGreg Kroah-Hartman 	}
2657a208ffd2SGrant Likely 	if (uport->cons && uport->dev)
2658a208ffd2SGrant Likely 		of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2659ab4382d2SGreg Kroah-Hartman 
2660ab4382d2SGreg Kroah-Hartman 	uart_configure_port(drv, state, uport);
2661ab4382d2SGreg Kroah-Hartman 
2662266dcff0SGreg Kroah-Hartman 	num_groups = 2;
2663266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
2664266dcff0SGreg Kroah-Hartman 		num_groups++;
2665266dcff0SGreg Kroah-Hartman 
2666c2b703b8SYoshihiro YUNOMAE 	uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2667266dcff0SGreg Kroah-Hartman 				    GFP_KERNEL);
2668266dcff0SGreg Kroah-Hartman 	if (!uport->tty_groups) {
2669266dcff0SGreg Kroah-Hartman 		ret = -ENOMEM;
2670266dcff0SGreg Kroah-Hartman 		goto out;
2671266dcff0SGreg Kroah-Hartman 	}
2672266dcff0SGreg Kroah-Hartman 	uport->tty_groups[0] = &tty_dev_attr_group;
2673266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
2674266dcff0SGreg Kroah-Hartman 		uport->tty_groups[1] = uport->attr_group;
2675266dcff0SGreg Kroah-Hartman 
2676ab4382d2SGreg Kroah-Hartman 	/*
2677ab4382d2SGreg Kroah-Hartman 	 * Register the port whether it's detected or not.  This allows
2678015355b7SGeert Uytterhoeven 	 * setserial to be used to alter this port's parameters.
2679ab4382d2SGreg Kroah-Hartman 	 */
2680b1b79916STomas Hlavacek 	tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
2681266dcff0SGreg Kroah-Hartman 			uport->line, uport->dev, port, uport->tty_groups);
2682ab4382d2SGreg Kroah-Hartman 	if (likely(!IS_ERR(tty_dev))) {
268377359835SSimon Glass 		device_set_wakeup_capable(tty_dev, 1);
268477359835SSimon Glass 	} else {
26852f2dafe7SSudip Mukherjee 		dev_err(uport->dev, "Cannot register tty device on line %d\n",
2686ab4382d2SGreg Kroah-Hartman 		       uport->line);
268777359835SSimon Glass 	}
2688ab4382d2SGreg Kroah-Hartman 
2689ab4382d2SGreg Kroah-Hartman 	/*
2690ab4382d2SGreg Kroah-Hartman 	 * Ensure UPF_DEAD is not set.
2691ab4382d2SGreg Kroah-Hartman 	 */
2692ab4382d2SGreg Kroah-Hartman 	uport->flags &= ~UPF_DEAD;
2693ab4382d2SGreg Kroah-Hartman 
2694ab4382d2SGreg Kroah-Hartman  out:
2695ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2696ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
2697ab4382d2SGreg Kroah-Hartman 
2698ab4382d2SGreg Kroah-Hartman 	return ret;
2699ab4382d2SGreg Kroah-Hartman }
2700ab4382d2SGreg Kroah-Hartman 
2701ab4382d2SGreg Kroah-Hartman /**
2702ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port - detach a driver defined port structure
2703ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
2704ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure for this port
2705ab4382d2SGreg Kroah-Hartman  *
2706ab4382d2SGreg Kroah-Hartman  *	This unhooks (and hangs up) the specified port structure from the
2707ab4382d2SGreg Kroah-Hartman  *	core driver.  No further calls will be made to the low-level code
2708ab4382d2SGreg Kroah-Hartman  *	for this port.
2709ab4382d2SGreg Kroah-Hartman  */
2710ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2711ab4382d2SGreg Kroah-Hartman {
2712ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2713ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
27144c6d5b4dSGeert Uytterhoeven 	struct tty_struct *tty;
2715b342dd51SChen Gang 	int ret = 0;
2716ab4382d2SGreg Kroah-Hartman 
2717ab4382d2SGreg Kroah-Hartman 	BUG_ON(in_interrupt());
2718ab4382d2SGreg Kroah-Hartman 
2719ab4382d2SGreg Kroah-Hartman 	if (state->uart_port != uport)
27202f2dafe7SSudip Mukherjee 		dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2721ab4382d2SGreg Kroah-Hartman 			state->uart_port, uport);
2722ab4382d2SGreg Kroah-Hartman 
2723ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
2724ab4382d2SGreg Kroah-Hartman 
2725ab4382d2SGreg Kroah-Hartman 	/*
2726ab4382d2SGreg Kroah-Hartman 	 * Mark the port "dead" - this prevents any opens from
2727ab4382d2SGreg Kroah-Hartman 	 * succeeding while we shut down the port.
2728ab4382d2SGreg Kroah-Hartman 	 */
2729ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2730b342dd51SChen Gang 	if (!state->uart_port) {
2731b342dd51SChen Gang 		mutex_unlock(&port->mutex);
2732b342dd51SChen Gang 		ret = -EINVAL;
2733b342dd51SChen Gang 		goto out;
2734b342dd51SChen Gang 	}
2735ab4382d2SGreg Kroah-Hartman 	uport->flags |= UPF_DEAD;
2736ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2737ab4382d2SGreg Kroah-Hartman 
2738ab4382d2SGreg Kroah-Hartman 	/*
2739ab4382d2SGreg Kroah-Hartman 	 * Remove the devices from the tty layer
2740ab4382d2SGreg Kroah-Hartman 	 */
2741ab4382d2SGreg Kroah-Hartman 	tty_unregister_device(drv->tty_driver, uport->line);
2742ab4382d2SGreg Kroah-Hartman 
27434c6d5b4dSGeert Uytterhoeven 	tty = tty_port_tty_get(port);
27444c6d5b4dSGeert Uytterhoeven 	if (tty) {
2745ab4382d2SGreg Kroah-Hartman 		tty_vhangup(port->tty);
27464c6d5b4dSGeert Uytterhoeven 		tty_kref_put(tty);
27474c6d5b4dSGeert Uytterhoeven 	}
2748ab4382d2SGreg Kroah-Hartman 
2749ab4382d2SGreg Kroah-Hartman 	/*
27505f5c9ae5SGeert Uytterhoeven 	 * If the port is used as a console, unregister it
27515f5c9ae5SGeert Uytterhoeven 	 */
27525f5c9ae5SGeert Uytterhoeven 	if (uart_console(uport))
27535f5c9ae5SGeert Uytterhoeven 		unregister_console(uport->cons);
27545f5c9ae5SGeert Uytterhoeven 
27555f5c9ae5SGeert Uytterhoeven 	/*
2756ab4382d2SGreg Kroah-Hartman 	 * Free the port IO and memory resources, if any.
2757ab4382d2SGreg Kroah-Hartman 	 */
2758ab4382d2SGreg Kroah-Hartman 	if (uport->type != PORT_UNKNOWN)
2759ab4382d2SGreg Kroah-Hartman 		uport->ops->release_port(uport);
2760266dcff0SGreg Kroah-Hartman 	kfree(uport->tty_groups);
2761ab4382d2SGreg Kroah-Hartman 
2762ab4382d2SGreg Kroah-Hartman 	/*
2763ab4382d2SGreg Kroah-Hartman 	 * Indicate that there isn't a port here anymore.
2764ab4382d2SGreg Kroah-Hartman 	 */
2765ab4382d2SGreg Kroah-Hartman 	uport->type = PORT_UNKNOWN;
2766ab4382d2SGreg Kroah-Hartman 
2767ab4382d2SGreg Kroah-Hartman 	state->uart_port = NULL;
2768b342dd51SChen Gang out:
2769ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
2770ab4382d2SGreg Kroah-Hartman 
2771b342dd51SChen Gang 	return ret;
2772ab4382d2SGreg Kroah-Hartman }
2773ab4382d2SGreg Kroah-Hartman 
2774ab4382d2SGreg Kroah-Hartman /*
2775ab4382d2SGreg Kroah-Hartman  *	Are the two ports equivalent?
2776ab4382d2SGreg Kroah-Hartman  */
2777ab4382d2SGreg Kroah-Hartman int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2778ab4382d2SGreg Kroah-Hartman {
2779ab4382d2SGreg Kroah-Hartman 	if (port1->iotype != port2->iotype)
2780ab4382d2SGreg Kroah-Hartman 		return 0;
2781ab4382d2SGreg Kroah-Hartman 
2782ab4382d2SGreg Kroah-Hartman 	switch (port1->iotype) {
2783ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2784ab4382d2SGreg Kroah-Hartman 		return (port1->iobase == port2->iobase);
2785ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2786ab4382d2SGreg Kroah-Hartman 		return (port1->iobase == port2->iobase) &&
2787ab4382d2SGreg Kroah-Hartman 		       (port1->hub6   == port2->hub6);
2788ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2789bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2790ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
27913ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2792ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2793ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2794ab4382d2SGreg Kroah-Hartman 		return (port1->mapbase == port2->mapbase);
2795ab4382d2SGreg Kroah-Hartman 	}
2796ab4382d2SGreg Kroah-Hartman 	return 0;
2797ab4382d2SGreg Kroah-Hartman }
2798ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port);
2799ab4382d2SGreg Kroah-Hartman 
2800027d7dacSJiri Slaby /**
2801027d7dacSJiri Slaby  *	uart_handle_dcd_change - handle a change of carrier detect state
2802027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
2803027d7dacSJiri Slaby  *	@status: new carrier detect status, nonzero if active
28044d90bb14SPeter Hurley  *
28054d90bb14SPeter Hurley  *	Caller must hold uport->lock
2806027d7dacSJiri Slaby  */
2807027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2808027d7dacSJiri Slaby {
280942381572SGeorge Spelvin 	struct tty_port *port = &uport->state->port;
281043eca0aeSAlan Cox 	struct tty_struct *tty = port->tty;
2811c993257bSPeter Hurley 	struct tty_ldisc *ld;
2812027d7dacSJiri Slaby 
28134d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
28144d90bb14SPeter Hurley 
2815c993257bSPeter Hurley 	if (tty) {
2816c993257bSPeter Hurley 		ld = tty_ldisc_ref(tty);
281742381572SGeorge Spelvin 		if (ld) {
281842381572SGeorge Spelvin 			if (ld->ops->dcd_change)
2819593fb1aeSGeorge Spelvin 				ld->ops->dcd_change(tty, status);
282042381572SGeorge Spelvin 			tty_ldisc_deref(ld);
282142381572SGeorge Spelvin 		}
2822c993257bSPeter Hurley 	}
2823027d7dacSJiri Slaby 
2824027d7dacSJiri Slaby 	uport->icount.dcd++;
2825027d7dacSJiri Slaby 
2826299245a1SPeter Hurley 	if (uart_dcd_enabled(uport)) {
2827027d7dacSJiri Slaby 		if (status)
2828027d7dacSJiri Slaby 			wake_up_interruptible(&port->open_wait);
282943eca0aeSAlan Cox 		else if (tty)
283043eca0aeSAlan Cox 			tty_hangup(tty);
2831027d7dacSJiri Slaby 	}
2832027d7dacSJiri Slaby }
2833027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2834027d7dacSJiri Slaby 
2835027d7dacSJiri Slaby /**
2836027d7dacSJiri Slaby  *	uart_handle_cts_change - handle a change of clear-to-send state
2837027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
2838027d7dacSJiri Slaby  *	@status: new clear to send status, nonzero if active
28394d90bb14SPeter Hurley  *
28404d90bb14SPeter Hurley  *	Caller must hold uport->lock
2841027d7dacSJiri Slaby  */
2842027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2843027d7dacSJiri Slaby {
28444d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
28454d90bb14SPeter Hurley 
2846027d7dacSJiri Slaby 	uport->icount.cts++;
2847027d7dacSJiri Slaby 
2848391f93f2SPeter Hurley 	if (uart_softcts_mode(uport)) {
2849d01f4d18SPeter Hurley 		if (uport->hw_stopped) {
2850027d7dacSJiri Slaby 			if (status) {
2851d01f4d18SPeter Hurley 				uport->hw_stopped = 0;
2852027d7dacSJiri Slaby 				uport->ops->start_tx(uport);
2853027d7dacSJiri Slaby 				uart_write_wakeup(uport);
2854027d7dacSJiri Slaby 			}
2855027d7dacSJiri Slaby 		} else {
2856027d7dacSJiri Slaby 			if (!status) {
2857d01f4d18SPeter Hurley 				uport->hw_stopped = 1;
2858027d7dacSJiri Slaby 				uport->ops->stop_tx(uport);
2859027d7dacSJiri Slaby 			}
2860027d7dacSJiri Slaby 		}
2861391f93f2SPeter Hurley 
2862027d7dacSJiri Slaby 	}
2863027d7dacSJiri Slaby }
2864027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2865027d7dacSJiri Slaby 
2866cf75525fSJiri Slaby /**
2867cf75525fSJiri Slaby  * uart_insert_char - push a char to the uart layer
2868cf75525fSJiri Slaby  *
2869cf75525fSJiri Slaby  * User is responsible to call tty_flip_buffer_push when they are done with
2870cf75525fSJiri Slaby  * insertion.
2871cf75525fSJiri Slaby  *
2872cf75525fSJiri Slaby  * @port: corresponding port
2873cf75525fSJiri Slaby  * @status: state of the serial port RX buffer (LSR for 8250)
2874cf75525fSJiri Slaby  * @overrun: mask of overrun bits in @status
2875cf75525fSJiri Slaby  * @ch: character to push
2876cf75525fSJiri Slaby  * @flag: flag for the character (see TTY_NORMAL and friends)
2877cf75525fSJiri Slaby  */
2878027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status,
2879027d7dacSJiri Slaby 		 unsigned int overrun, unsigned int ch, unsigned int flag)
2880027d7dacSJiri Slaby {
288192a19f9cSJiri Slaby 	struct tty_port *tport = &port->state->port;
2882027d7dacSJiri Slaby 
2883027d7dacSJiri Slaby 	if ((status & port->ignore_status_mask & ~overrun) == 0)
288492a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, ch, flag) == 0)
2885dabfb351SCorbin 			++port->icount.buf_overrun;
2886027d7dacSJiri Slaby 
2887027d7dacSJiri Slaby 	/*
2888027d7dacSJiri Slaby 	 * Overrun is special.  Since it's reported immediately,
2889027d7dacSJiri Slaby 	 * it doesn't affect the current character.
2890027d7dacSJiri Slaby 	 */
2891027d7dacSJiri Slaby 	if (status & ~port->ignore_status_mask & overrun)
289292a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
2893dabfb351SCorbin 			++port->icount.buf_overrun;
2894027d7dacSJiri Slaby }
2895027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char);
2896027d7dacSJiri Slaby 
2897ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_write_wakeup);
2898ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_register_driver);
2899ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_unregister_driver);
2900ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_suspend_port);
2901ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_resume_port);
2902ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_add_one_port);
2903ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_remove_one_port);
2904ab4382d2SGreg Kroah-Hartman 
2905ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core");
2906ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2907