xref: /openbmc/linux/drivers/tty/serial/serial_core.c (revision 5e227ef2)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
3ab4382d2SGreg Kroah-Hartman  *  Driver core for serial ports
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6ab4382d2SGreg Kroah-Hartman  *
7ab4382d2SGreg Kroah-Hartman  *  Copyright 1999 ARM Limited
8ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9ab4382d2SGreg Kroah-Hartman  */
10ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
12027d7dacSJiri Slaby #include <linux/tty_flip.h>
13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
14174cd4b1SIngo Molnar #include <linux/sched/signal.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
17167cbce2SJohan Hovold #include <linux/gpio/consumer.h>
18eff37b5eSIlpo Järvinen #include <linux/kernel.h>
19a208ffd2SGrant Likely #include <linux/of.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/device.h>
23ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
24ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
2568af4317SDmitry Safonov #include <linux/sysrq.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h>
2831f6bd7fSIlpo Järvinen #include <linux/math64.h>
29794edf30SDavid Howells #include <linux/security.h>
30ab4382d2SGreg Kroah-Hartman 
31aef3ad10SAndy Shevchenko #include <linux/irq.h>
327c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
33ab4382d2SGreg Kroah-Hartman 
34ab4382d2SGreg Kroah-Hartman /*
35ab4382d2SGreg Kroah-Hartman  * This is used to lock changes in serial line configuration.
36ab4382d2SGreg Kroah-Hartman  */
37ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex);
38ab4382d2SGreg Kroah-Hartman 
39ab4382d2SGreg Kroah-Hartman /*
40ab4382d2SGreg Kroah-Hartman  * lockdep: port->lock is initialized in two places, but we
41ab4382d2SGreg Kroah-Hartman  *          want only one lock-class:
42ab4382d2SGreg Kroah-Hartman  */
43ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key;
44ab4382d2SGreg Kroah-Hartman 
45ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
46ab4382d2SGreg Kroah-Hartman 
470ed12afaSLino Sanfilippo /*
480ed12afaSLino Sanfilippo  * Max time with active RTS before/after data is sent.
490ed12afaSLino Sanfilippo  */
500ed12afaSLino Sanfilippo #define RS485_MAX_RTS_DELAY	100 /* msecs */
510ed12afaSLino Sanfilippo 
526f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
536f538fe3SLinus Walleij 			   enum uart_pm_state pm_state);
54ab4382d2SGreg Kroah-Hartman 
55b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port);
56b922e19dSJiri Slaby 
57299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport)
58299245a1SPeter Hurley {
59d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_DCD_ENABLE);
60299245a1SPeter Hurley }
61299245a1SPeter Hurley 
629ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state)
639ed19428SPeter Hurley {
649ed19428SPeter Hurley 	if (atomic_add_unless(&state->refcount, 1, 0))
659ed19428SPeter Hurley 		return state->uart_port;
669ed19428SPeter Hurley 	return NULL;
679ed19428SPeter Hurley }
689ed19428SPeter Hurley 
699ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport)
709ed19428SPeter Hurley {
71ef510beaSAndy Shevchenko 	if (atomic_dec_and_test(&uport->state->refcount))
729ed19428SPeter Hurley 		wake_up(&uport->state->remove_wait);
739ed19428SPeter Hurley }
749ed19428SPeter Hurley 
759ed19428SPeter Hurley #define uart_port_lock(state, flags)					\
769ed19428SPeter Hurley 	({								\
779ed19428SPeter Hurley 		struct uart_port *__uport = uart_port_ref(state);	\
789ed19428SPeter Hurley 		if (__uport)						\
799ed19428SPeter Hurley 			spin_lock_irqsave(&__uport->lock, flags);	\
809ed19428SPeter Hurley 		__uport;						\
819ed19428SPeter Hurley 	})
829ed19428SPeter Hurley 
839ed19428SPeter Hurley #define uart_port_unlock(uport, flags)					\
849ed19428SPeter Hurley 	({								\
859ed19428SPeter Hurley 		struct uart_port *__uport = uport;			\
86ef510beaSAndy Shevchenko 		if (__uport) {						\
879ed19428SPeter Hurley 			spin_unlock_irqrestore(&__uport->lock, flags);	\
889ed19428SPeter Hurley 			uart_port_deref(__uport);			\
89ef510beaSAndy Shevchenko 		}							\
909ed19428SPeter Hurley 	})
919ed19428SPeter Hurley 
924047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state)
934047b371SPeter Hurley {
947da4b8b7SPeter Hurley 	lockdep_assert_held(&state->port.mutex);
954047b371SPeter Hurley 	return state->uart_port;
964047b371SPeter Hurley }
974047b371SPeter Hurley 
98c4bd17a6SJiri Slaby /**
99c4bd17a6SJiri Slaby  * uart_write_wakeup - schedule write processing
100c4bd17a6SJiri Slaby  * @port: port to be processed
101c4bd17a6SJiri Slaby  *
102c4bd17a6SJiri Slaby  * This routine is used by the interrupt handler to schedule processing in the
103c4bd17a6SJiri Slaby  * software interrupt portion of the driver. A driver is expected to call this
104c4bd17a6SJiri Slaby  * function when the number of characters in the transmit buffer have dropped
105c4bd17a6SJiri Slaby  * below a threshold.
106c4bd17a6SJiri Slaby  *
107c4bd17a6SJiri Slaby  * Locking: @port->lock should be held
108ab4382d2SGreg Kroah-Hartman  */
109ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port)
110ab4382d2SGreg Kroah-Hartman {
111ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = port->state;
112ab4382d2SGreg Kroah-Hartman 	/*
113ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
114ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
115ab4382d2SGreg Kroah-Hartman 	 */
116ab4382d2SGreg Kroah-Hartman 	BUG_ON(!state);
117d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
118ab4382d2SGreg Kroah-Hartman }
11915dc475bSJiri Slaby EXPORT_SYMBOL(uart_write_wakeup);
120ab4382d2SGreg Kroah-Hartman 
121ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty)
122ab4382d2SGreg Kroah-Hartman {
123ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1249ed19428SPeter Hurley 	struct uart_port *port;
125ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
126ab4382d2SGreg Kroah-Hartman 
1279ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
1289ed19428SPeter Hurley 	if (port)
129ab4382d2SGreg Kroah-Hartman 		port->ops->stop_tx(port);
1309ed19428SPeter Hurley 	uart_port_unlock(port, flags);
131ab4382d2SGreg Kroah-Hartman }
132ab4382d2SGreg Kroah-Hartman 
133ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty)
134ab4382d2SGreg Kroah-Hartman {
135ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
136ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
137ab4382d2SGreg Kroah-Hartman 
1389ed19428SPeter Hurley 	if (port && !uart_tx_stopped(port))
139ab4382d2SGreg Kroah-Hartman 		port->ops->start_tx(port);
140ab4382d2SGreg Kroah-Hartman }
141ab4382d2SGreg Kroah-Hartman 
142ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty)
143ab4382d2SGreg Kroah-Hartman {
144ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1459ed19428SPeter Hurley 	struct uart_port *port;
146ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
147ab4382d2SGreg Kroah-Hartman 
1489ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
149ab4382d2SGreg Kroah-Hartman 	__uart_start(tty);
1509ed19428SPeter Hurley 	uart_port_unlock(port, flags);
151ab4382d2SGreg Kroah-Hartman }
152ab4382d2SGreg Kroah-Hartman 
153f4581cabSDenys Vlasenko static void
154ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
155ab4382d2SGreg Kroah-Hartman {
156ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
157ab4382d2SGreg Kroah-Hartman 	unsigned int old;
158ab4382d2SGreg Kroah-Hartman 
159ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
160ab4382d2SGreg Kroah-Hartman 	old = port->mctrl;
161ab4382d2SGreg Kroah-Hartman 	port->mctrl = (old & ~clear) | set;
1627c7f9bc9SLukas Wunner 	if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
163ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl);
164ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
165ab4382d2SGreg Kroah-Hartman }
166ab4382d2SGreg Kroah-Hartman 
167ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
168ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
169ab4382d2SGreg Kroah-Hartman 
1705701cb8bSIlpo Järvinen static void uart_port_dtr_rts(struct uart_port *uport, bool active)
171a6845e1eSRafael Gago {
1725701cb8bSIlpo Järvinen 	if (active)
173a6845e1eSRafael Gago 		uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1742dd8a74fSLukas Wunner 	else
1752dd8a74fSLukas Wunner 		uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
176a6845e1eSRafael Gago }
177a6845e1eSRafael Gago 
1788e90cf29SIlpo Järvinen /* Caller holds port mutex */
179826736a6SIlpo Järvinen static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state,
1808e90cf29SIlpo Järvinen 				      const struct ktermios *old_termios)
1818e90cf29SIlpo Järvinen {
1828e90cf29SIlpo Järvinen 	struct uart_port *uport = uart_port_check(state);
1838e90cf29SIlpo Järvinen 	struct ktermios *termios;
1841690ca51SIlpo Järvinen 	bool old_hw_stopped;
1858e90cf29SIlpo Järvinen 
1868e90cf29SIlpo Järvinen 	/*
1878e90cf29SIlpo Järvinen 	 * If we have no tty, termios, or the port does not exist,
1888e90cf29SIlpo Järvinen 	 * then we can't set the parameters for this port.
1898e90cf29SIlpo Järvinen 	 */
1908e90cf29SIlpo Järvinen 	if (!tty || uport->type == PORT_UNKNOWN)
1918e90cf29SIlpo Järvinen 		return;
1928e90cf29SIlpo Järvinen 
1938e90cf29SIlpo Järvinen 	termios = &tty->termios;
1948e90cf29SIlpo Järvinen 	uport->ops->set_termios(uport, termios, old_termios);
1958e90cf29SIlpo Järvinen 
1968e90cf29SIlpo Järvinen 	/*
1978e90cf29SIlpo Järvinen 	 * Set modem status enables based on termios cflag
1988e90cf29SIlpo Järvinen 	 */
1998e90cf29SIlpo Järvinen 	spin_lock_irq(&uport->lock);
2008e90cf29SIlpo Järvinen 	if (termios->c_cflag & CRTSCTS)
2018e90cf29SIlpo Järvinen 		uport->status |= UPSTAT_CTS_ENABLE;
2028e90cf29SIlpo Järvinen 	else
2038e90cf29SIlpo Järvinen 		uport->status &= ~UPSTAT_CTS_ENABLE;
2048e90cf29SIlpo Järvinen 
2058e90cf29SIlpo Järvinen 	if (termios->c_cflag & CLOCAL)
2068e90cf29SIlpo Järvinen 		uport->status &= ~UPSTAT_DCD_ENABLE;
2078e90cf29SIlpo Järvinen 	else
2088e90cf29SIlpo Järvinen 		uport->status |= UPSTAT_DCD_ENABLE;
2098e90cf29SIlpo Järvinen 
2108e90cf29SIlpo Järvinen 	/* reset sw-assisted CTS flow control based on (possibly) new mode */
2111690ca51SIlpo Järvinen 	old_hw_stopped = uport->hw_stopped;
2128e90cf29SIlpo Järvinen 	uport->hw_stopped = uart_softcts_mode(uport) &&
2138e90cf29SIlpo Järvinen 			    !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
2141690ca51SIlpo Järvinen 	if (uport->hw_stopped != old_hw_stopped) {
2151690ca51SIlpo Järvinen 		if (!old_hw_stopped)
2168e90cf29SIlpo Järvinen 			uport->ops->stop_tx(uport);
2171690ca51SIlpo Järvinen 		else
2188e90cf29SIlpo Järvinen 			__uart_start(tty);
2198e90cf29SIlpo Järvinen 	}
2208e90cf29SIlpo Järvinen 	spin_unlock_irq(&uport->lock);
2218e90cf29SIlpo Järvinen }
2228e90cf29SIlpo Järvinen 
223ab4382d2SGreg Kroah-Hartman /*
224ab4382d2SGreg Kroah-Hartman  * Startup the port.  This will be called once per open.  All calls
225ab4382d2SGreg Kroah-Hartman  * will be serialised by the per-port mutex.
226ab4382d2SGreg Kroah-Hartman  */
227c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
228dcd794c6SIlpo Järvinen 			     bool init_hw)
229ab4382d2SGreg Kroah-Hartman {
2304047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
23118ee37e1SJohan Hovold 	unsigned long flags;
232ab4382d2SGreg Kroah-Hartman 	unsigned long page;
233ab4382d2SGreg Kroah-Hartman 	int retval = 0;
234ab4382d2SGreg Kroah-Hartman 
235ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
236c0d92be6SJiri Slaby 		return 1;
237ab4382d2SGreg Kroah-Hartman 
238ab4382d2SGreg Kroah-Hartman 	/*
2397deb39edSThomas Pfaff 	 * Make sure the device is in D0 state.
2407deb39edSThomas Pfaff 	 */
2417deb39edSThomas Pfaff 	uart_change_pm(state, UART_PM_STATE_ON);
2427deb39edSThomas Pfaff 
2437deb39edSThomas Pfaff 	/*
244ab4382d2SGreg Kroah-Hartman 	 * Initialise and allocate the transmit and temporary
245ab4382d2SGreg Kroah-Hartman 	 * buffer.
246ab4382d2SGreg Kroah-Hartman 	 */
247ab4382d2SGreg Kroah-Hartman 	page = get_zeroed_page(GFP_KERNEL);
248ab4382d2SGreg Kroah-Hartman 	if (!page)
249ab4382d2SGreg Kroah-Hartman 		return -ENOMEM;
250ab4382d2SGreg Kroah-Hartman 
251a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
252a5ba1d95STycho Andersen 	if (!state->xmit.buf) {
253ab4382d2SGreg Kroah-Hartman 		state->xmit.buf = (unsigned char *) page;
254ab4382d2SGreg Kroah-Hartman 		uart_circ_clear(&state->xmit);
255d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
256a5ba1d95STycho Andersen 	} else {
257d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
258d7240214SSergey Senozhatsky 		/*
259d7240214SSergey Senozhatsky 		 * Do not free() the page under the port lock, see
260d7240214SSergey Senozhatsky 		 * uart_shutdown().
261d7240214SSergey Senozhatsky 		 */
262a5ba1d95STycho Andersen 		free_page(page);
263ab4382d2SGreg Kroah-Hartman 	}
264ab4382d2SGreg Kroah-Hartman 
265ab4382d2SGreg Kroah-Hartman 	retval = uport->ops->startup(uport);
266ab4382d2SGreg Kroah-Hartman 	if (retval == 0) {
267c7d7abffSJiri Slaby 		if (uart_console(uport) && uport->cons->cflag) {
268adc8d746SAlan Cox 			tty->termios.c_cflag = uport->cons->cflag;
269027b5717SPali Rohár 			tty->termios.c_ispeed = uport->cons->ispeed;
270027b5717SPali Rohár 			tty->termios.c_ospeed = uport->cons->ospeed;
271c7d7abffSJiri Slaby 			uport->cons->cflag = 0;
272027b5717SPali Rohár 			uport->cons->ispeed = 0;
273027b5717SPali Rohár 			uport->cons->ospeed = 0;
274c7d7abffSJiri Slaby 		}
275ab4382d2SGreg Kroah-Hartman 		/*
276ab4382d2SGreg Kroah-Hartman 		 * Initialise the hardware port settings.
277ab4382d2SGreg Kroah-Hartman 		 */
278826736a6SIlpo Järvinen 		uart_change_line_settings(tty, state, NULL);
279ab4382d2SGreg Kroah-Hartman 
280ab4382d2SGreg Kroah-Hartman 		/*
281ab4382d2SGreg Kroah-Hartman 		 * Setup the RTS and DTR signals once the
282ab4382d2SGreg Kroah-Hartman 		 * port is open and ready to respond.
283ab4382d2SGreg Kroah-Hartman 		 */
2849db276f8SPeter Hurley 		if (init_hw && C_BAUD(tty))
2855d420399SIlpo Järvinen 			uart_port_dtr_rts(uport, true);
286ab4382d2SGreg Kroah-Hartman 	}
287ab4382d2SGreg Kroah-Hartman 
2880055197eSJiri Slaby 	/*
2890055197eSJiri Slaby 	 * This is to allow setserial on this port. People may want to set
2900055197eSJiri Slaby 	 * port/irq/type and then reconfigure the port properly if it failed
2910055197eSJiri Slaby 	 * now.
2920055197eSJiri Slaby 	 */
293ab4382d2SGreg Kroah-Hartman 	if (retval && capable(CAP_SYS_ADMIN))
294c0d92be6SJiri Slaby 		return 1;
295c0d92be6SJiri Slaby 
296c0d92be6SJiri Slaby 	return retval;
297c0d92be6SJiri Slaby }
298c0d92be6SJiri Slaby 
299c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state,
300dcd794c6SIlpo Järvinen 			bool init_hw)
301c0d92be6SJiri Slaby {
302c0d92be6SJiri Slaby 	struct tty_port *port = &state->port;
303c0d92be6SJiri Slaby 	int retval;
304c0d92be6SJiri Slaby 
305d41861caSPeter Hurley 	if (tty_port_initialized(port))
306c0d92be6SJiri Slaby 		return 0;
307c0d92be6SJiri Slaby 
308c0d92be6SJiri Slaby 	retval = uart_port_startup(tty, state, init_hw);
309b3b57646SRob Herring 	if (retval)
310b3b57646SRob Herring 		set_bit(TTY_IO_ERROR, &tty->flags);
311ab4382d2SGreg Kroah-Hartman 
312ab4382d2SGreg Kroah-Hartman 	return retval;
313ab4382d2SGreg Kroah-Hartman }
314ab4382d2SGreg Kroah-Hartman 
315ab4382d2SGreg Kroah-Hartman /*
316ab4382d2SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
317ab4382d2SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.  Calls to
318ab4382d2SGreg Kroah-Hartman  * uart_shutdown are serialised by the per-port semaphore.
319af224ca2SPeter Hurley  *
320af224ca2SPeter Hurley  * uport == NULL if uart_port has already been removed
321ab4382d2SGreg Kroah-Hartman  */
322ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
323ab4382d2SGreg Kroah-Hartman {
3244047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
325ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
32618ee37e1SJohan Hovold 	unsigned long flags;
327d7240214SSergey Senozhatsky 	char *xmit_buf = NULL;
328ab4382d2SGreg Kroah-Hartman 
329ab4382d2SGreg Kroah-Hartman 	/*
330ab4382d2SGreg Kroah-Hartman 	 * Set the TTY IO error marker
331ab4382d2SGreg Kroah-Hartman 	 */
332ab4382d2SGreg Kroah-Hartman 	if (tty)
333ab4382d2SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
334ab4382d2SGreg Kroah-Hartman 
335d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
336515be7baSIlpo Järvinen 		tty_port_set_initialized(port, false);
337d41861caSPeter Hurley 
338ab4382d2SGreg Kroah-Hartman 		/*
339ab4382d2SGreg Kroah-Hartman 		 * Turn off DTR and RTS early.
340ab4382d2SGreg Kroah-Hartman 		 */
341027b5717SPali Rohár 		if (uport && uart_console(uport) && tty) {
342ae84db96SPeter Hurley 			uport->cons->cflag = tty->termios.c_cflag;
343027b5717SPali Rohár 			uport->cons->ispeed = tty->termios.c_ispeed;
344027b5717SPali Rohár 			uport->cons->ospeed = tty->termios.c_ospeed;
345027b5717SPali Rohár 		}
346ae84db96SPeter Hurley 
3479db276f8SPeter Hurley 		if (!tty || C_HUPCL(tty))
3485d420399SIlpo Järvinen 			uart_port_dtr_rts(uport, false);
349ab4382d2SGreg Kroah-Hartman 
350b922e19dSJiri Slaby 		uart_port_shutdown(port);
351ab4382d2SGreg Kroah-Hartman 	}
352ab4382d2SGreg Kroah-Hartman 
353ab4382d2SGreg Kroah-Hartman 	/*
354d208a3bfSDoug Anderson 	 * It's possible for shutdown to be called after suspend if we get
355d208a3bfSDoug Anderson 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
356d208a3bfSDoug Anderson 	 * we don't try to resume a port that has been shutdown.
357ab4382d2SGreg Kroah-Hartman 	 */
35875b20a2aSIlpo Järvinen 	tty_port_set_suspended(port, false);
359ab4382d2SGreg Kroah-Hartman 
360ab4382d2SGreg Kroah-Hartman 	/*
361d7240214SSergey Senozhatsky 	 * Do not free() the transmit buffer page under the port lock since
362d7240214SSergey Senozhatsky 	 * this can create various circular locking scenarios. For instance,
363d7240214SSergey Senozhatsky 	 * console driver may need to allocate/free a debug object, which
364d7240214SSergey Senozhatsky 	 * can endup in printk() recursion.
365ab4382d2SGreg Kroah-Hartman 	 */
366a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
367d7240214SSergey Senozhatsky 	xmit_buf = state->xmit.buf;
368ab4382d2SGreg Kroah-Hartman 	state->xmit.buf = NULL;
369a5ba1d95STycho Andersen 	uart_port_unlock(uport, flags);
370d7240214SSergey Senozhatsky 
371d7240214SSergey Senozhatsky 	free_page((unsigned long)xmit_buf);
372ab4382d2SGreg Kroah-Hartman }
373ab4382d2SGreg Kroah-Hartman 
374ab4382d2SGreg Kroah-Hartman /**
375c4bd17a6SJiri Slaby  * uart_update_timeout - update per-port frame timing information
376ab4382d2SGreg Kroah-Hartman  * @port: uart_port structure describing the port
377ab4382d2SGreg Kroah-Hartman  * @cflag: termios cflag value
378ab4382d2SGreg Kroah-Hartman  * @baud: speed of the port
379ab4382d2SGreg Kroah-Hartman  *
380c4bd17a6SJiri Slaby  * Set the @port frame timing information from which the FIFO timeout value is
381c4bd17a6SJiri Slaby  * derived. The @cflag value should reflect the actual hardware settings as
382c4bd17a6SJiri Slaby  * number of bits, parity, stop bits and baud rate is taken into account here.
383c4bd17a6SJiri Slaby  *
384c4bd17a6SJiri Slaby  * Locking: caller is expected to take @port->lock
385ab4382d2SGreg Kroah-Hartman  */
386ab4382d2SGreg Kroah-Hartman void
387ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag,
388ab4382d2SGreg Kroah-Hartman 		    unsigned int baud)
389ab4382d2SGreg Kroah-Hartman {
39031f6bd7fSIlpo Järvinen 	unsigned int size = tty_get_frame_size(cflag);
39131f6bd7fSIlpo Järvinen 	u64 frame_time;
392ab4382d2SGreg Kroah-Hartman 
39331f6bd7fSIlpo Järvinen 	frame_time = (u64)size * NSEC_PER_SEC;
39431f6bd7fSIlpo Järvinen 	port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
395ab4382d2SGreg Kroah-Hartman }
396ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout);
397ab4382d2SGreg Kroah-Hartman 
398ab4382d2SGreg Kroah-Hartman /**
399ab4382d2SGreg Kroah-Hartman  * uart_get_baud_rate - return baud rate for a particular port
400ab4382d2SGreg Kroah-Hartman  * @port: uart_port structure describing the port in question.
401c4bd17a6SJiri Slaby  * @termios: desired termios settings
402c4bd17a6SJiri Slaby  * @old: old termios (or %NULL)
403ab4382d2SGreg Kroah-Hartman  * @min: minimum acceptable baud rate
404ab4382d2SGreg Kroah-Hartman  * @max: maximum acceptable baud rate
405ab4382d2SGreg Kroah-Hartman  *
406c4bd17a6SJiri Slaby  * Decode the termios structure into a numeric baud rate, taking account of the
407c4bd17a6SJiri Slaby  * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
408c4bd17a6SJiri Slaby  * baud.
409ab4382d2SGreg Kroah-Hartman  *
410c4bd17a6SJiri Slaby  * If the new baud rate is invalid, try the @old termios setting. If it's still
411c4bd17a6SJiri Slaby  * invalid, we try 9600 baud.
412ab4382d2SGreg Kroah-Hartman  *
413c4bd17a6SJiri Slaby  * The @termios structure is updated to reflect the baud rate we're actually
414c4bd17a6SJiri Slaby  * going to be using. Don't do this for the case where B0 is requested ("hang
415c4bd17a6SJiri Slaby  * up").
416c4bd17a6SJiri Slaby  *
417c4bd17a6SJiri Slaby  * Locking: caller dependent
418ab4382d2SGreg Kroah-Hartman  */
419ab4382d2SGreg Kroah-Hartman unsigned int
420ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
421bec5b814SIlpo Järvinen 		   const struct ktermios *old, unsigned int min, unsigned int max)
422ab4382d2SGreg Kroah-Hartman {
423f10a2233SJoakim Nordell 	unsigned int try;
424f10a2233SJoakim Nordell 	unsigned int baud;
425f10a2233SJoakim Nordell 	unsigned int altbaud;
426ab4382d2SGreg Kroah-Hartman 	int hung_up = 0;
427ab4382d2SGreg Kroah-Hartman 	upf_t flags = port->flags & UPF_SPD_MASK;
428ab4382d2SGreg Kroah-Hartman 
429f10a2233SJoakim Nordell 	switch (flags) {
430f10a2233SJoakim Nordell 	case UPF_SPD_HI:
431ab4382d2SGreg Kroah-Hartman 		altbaud = 57600;
432f10a2233SJoakim Nordell 		break;
433f10a2233SJoakim Nordell 	case UPF_SPD_VHI:
434ab4382d2SGreg Kroah-Hartman 		altbaud = 115200;
435f10a2233SJoakim Nordell 		break;
436f10a2233SJoakim Nordell 	case UPF_SPD_SHI:
437ab4382d2SGreg Kroah-Hartman 		altbaud = 230400;
438f10a2233SJoakim Nordell 		break;
439f10a2233SJoakim Nordell 	case UPF_SPD_WARP:
440ab4382d2SGreg Kroah-Hartman 		altbaud = 460800;
441f10a2233SJoakim Nordell 		break;
442f10a2233SJoakim Nordell 	default:
443f10a2233SJoakim Nordell 		altbaud = 38400;
444f10a2233SJoakim Nordell 		break;
445f10a2233SJoakim Nordell 	}
446ab4382d2SGreg Kroah-Hartman 
447ab4382d2SGreg Kroah-Hartman 	for (try = 0; try < 2; try++) {
448ab4382d2SGreg Kroah-Hartman 		baud = tty_termios_baud_rate(termios);
449ab4382d2SGreg Kroah-Hartman 
450ab4382d2SGreg Kroah-Hartman 		/*
451ab4382d2SGreg Kroah-Hartman 		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
452ab4382d2SGreg Kroah-Hartman 		 * Die! Die! Die!
453ab4382d2SGreg Kroah-Hartman 		 */
454547039ecSPeter Hurley 		if (try == 0 && baud == 38400)
455ab4382d2SGreg Kroah-Hartman 			baud = altbaud;
456ab4382d2SGreg Kroah-Hartman 
457ab4382d2SGreg Kroah-Hartman 		/*
458ab4382d2SGreg Kroah-Hartman 		 * Special case: B0 rate.
459ab4382d2SGreg Kroah-Hartman 		 */
460ab4382d2SGreg Kroah-Hartman 		if (baud == 0) {
461ab4382d2SGreg Kroah-Hartman 			hung_up = 1;
462ab4382d2SGreg Kroah-Hartman 			baud = 9600;
463ab4382d2SGreg Kroah-Hartman 		}
464ab4382d2SGreg Kroah-Hartman 
465ab4382d2SGreg Kroah-Hartman 		if (baud >= min && baud <= max)
466ab4382d2SGreg Kroah-Hartman 			return baud;
467ab4382d2SGreg Kroah-Hartman 
468ab4382d2SGreg Kroah-Hartman 		/*
469ab4382d2SGreg Kroah-Hartman 		 * Oops, the quotient was zero.  Try again with
470ab4382d2SGreg Kroah-Hartman 		 * the old baud rate if possible.
471ab4382d2SGreg Kroah-Hartman 		 */
472ab4382d2SGreg Kroah-Hartman 		termios->c_cflag &= ~CBAUD;
473ab4382d2SGreg Kroah-Hartman 		if (old) {
474ab4382d2SGreg Kroah-Hartman 			baud = tty_termios_baud_rate(old);
475ab4382d2SGreg Kroah-Hartman 			if (!hung_up)
476ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
477ab4382d2SGreg Kroah-Hartman 								baud, baud);
478ab4382d2SGreg Kroah-Hartman 			old = NULL;
479ab4382d2SGreg Kroah-Hartman 			continue;
480ab4382d2SGreg Kroah-Hartman 		}
481ab4382d2SGreg Kroah-Hartman 
482ab4382d2SGreg Kroah-Hartman 		/*
483ab4382d2SGreg Kroah-Hartman 		 * As a last resort, if the range cannot be met then clip to
484ab4382d2SGreg Kroah-Hartman 		 * the nearest chip supported rate.
485ab4382d2SGreg Kroah-Hartman 		 */
486ab4382d2SGreg Kroah-Hartman 		if (!hung_up) {
487ab4382d2SGreg Kroah-Hartman 			if (baud <= min)
488ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
489ab4382d2SGreg Kroah-Hartman 							min + 1, min + 1);
490ab4382d2SGreg Kroah-Hartman 			else
491ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
492ab4382d2SGreg Kroah-Hartman 							max - 1, max - 1);
493ab4382d2SGreg Kroah-Hartman 		}
494ab4382d2SGreg Kroah-Hartman 	}
495ab4382d2SGreg Kroah-Hartman 	/* Should never happen */
496ab4382d2SGreg Kroah-Hartman 	WARN_ON(1);
497ab4382d2SGreg Kroah-Hartman 	return 0;
498ab4382d2SGreg Kroah-Hartman }
499ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate);
500ab4382d2SGreg Kroah-Hartman 
501ab4382d2SGreg Kroah-Hartman /**
502ab4382d2SGreg Kroah-Hartman  * uart_get_divisor - return uart clock divisor
503c4bd17a6SJiri Slaby  * @port: uart_port structure describing the port
504ab4382d2SGreg Kroah-Hartman  * @baud: desired baud rate
505ab4382d2SGreg Kroah-Hartman  *
506c4bd17a6SJiri Slaby  * Calculate the divisor (baud_base / baud) for the specified @baud,
507c4bd17a6SJiri Slaby  * appropriately rounded.
508c4bd17a6SJiri Slaby  *
509c4bd17a6SJiri Slaby  * If 38400 baud and custom divisor is selected, return the custom divisor
510c4bd17a6SJiri Slaby  * instead.
511c4bd17a6SJiri Slaby  *
512c4bd17a6SJiri Slaby  * Locking: caller dependent
513ab4382d2SGreg Kroah-Hartman  */
514ab4382d2SGreg Kroah-Hartman unsigned int
515ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud)
516ab4382d2SGreg Kroah-Hartman {
517ab4382d2SGreg Kroah-Hartman 	unsigned int quot;
518ab4382d2SGreg Kroah-Hartman 
519ab4382d2SGreg Kroah-Hartman 	/*
520ab4382d2SGreg Kroah-Hartman 	 * Old custom speed handling.
521ab4382d2SGreg Kroah-Hartman 	 */
522ab4382d2SGreg Kroah-Hartman 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
523ab4382d2SGreg Kroah-Hartman 		quot = port->custom_divisor;
524ab4382d2SGreg Kroah-Hartman 	else
52597d24634SUwe Kleine-König 		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
526ab4382d2SGreg Kroah-Hartman 
527ab4382d2SGreg Kroah-Hartman 	return quot;
528ab4382d2SGreg Kroah-Hartman }
529ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor);
530ab4382d2SGreg Kroah-Hartman 
531f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c)
532ab4382d2SGreg Kroah-Hartman {
533f5291eccSPeter Hurley 	struct uart_state *state = tty->driver_data;
5349ed19428SPeter Hurley 	struct uart_port *port;
535f5291eccSPeter Hurley 	struct circ_buf *circ;
536ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
537ab4382d2SGreg Kroah-Hartman 	int ret = 0;
538ab4382d2SGreg Kroah-Hartman 
539f5291eccSPeter Hurley 	circ = &state->xmit;
5409ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
541aff9cf59SSamir Virmani 	if (!circ->buf) {
542aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
543aff9cf59SSamir Virmani 		return 0;
544aff9cf59SSamir Virmani 	}
545aff9cf59SSamir Virmani 
5469ed19428SPeter Hurley 	if (port && uart_circ_chars_free(circ) != 0) {
547ab4382d2SGreg Kroah-Hartman 		circ->buf[circ->head] = c;
548ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
549ab4382d2SGreg Kroah-Hartman 		ret = 1;
550ab4382d2SGreg Kroah-Hartman 	}
5519ed19428SPeter Hurley 	uart_port_unlock(port, flags);
552ab4382d2SGreg Kroah-Hartman 	return ret;
553ab4382d2SGreg Kroah-Hartman }
554ab4382d2SGreg Kroah-Hartman 
555ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty)
556ab4382d2SGreg Kroah-Hartman {
557ab4382d2SGreg Kroah-Hartman 	uart_start(tty);
558ab4382d2SGreg Kroah-Hartman }
559ab4382d2SGreg Kroah-Hartman 
560ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty,
561ab4382d2SGreg Kroah-Hartman 					const unsigned char *buf, int count)
562ab4382d2SGreg Kroah-Hartman {
563ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
564ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
565ab4382d2SGreg Kroah-Hartman 	struct circ_buf *circ;
566ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
567ab4382d2SGreg Kroah-Hartman 	int c, ret = 0;
568ab4382d2SGreg Kroah-Hartman 
569ab4382d2SGreg Kroah-Hartman 	/*
570ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
571ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
572ab4382d2SGreg Kroah-Hartman 	 */
573ab4382d2SGreg Kroah-Hartman 	if (!state) {
574ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
575ab4382d2SGreg Kroah-Hartman 		return -EL3HLT;
576ab4382d2SGreg Kroah-Hartman 	}
577ab4382d2SGreg Kroah-Hartman 
5789ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
579aff9cf59SSamir Virmani 	circ = &state->xmit;
580aff9cf59SSamir Virmani 	if (!circ->buf) {
581aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
582aff9cf59SSamir Virmani 		return 0;
583aff9cf59SSamir Virmani 	}
584aff9cf59SSamir Virmani 
5859ed19428SPeter Hurley 	while (port) {
586ab4382d2SGreg Kroah-Hartman 		c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
587ab4382d2SGreg Kroah-Hartman 		if (count < c)
588ab4382d2SGreg Kroah-Hartman 			c = count;
589ab4382d2SGreg Kroah-Hartman 		if (c <= 0)
590ab4382d2SGreg Kroah-Hartman 			break;
591ab4382d2SGreg Kroah-Hartman 		memcpy(circ->buf + circ->head, buf, c);
592ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
593ab4382d2SGreg Kroah-Hartman 		buf += c;
594ab4382d2SGreg Kroah-Hartman 		count -= c;
595ab4382d2SGreg Kroah-Hartman 		ret += c;
596ab4382d2SGreg Kroah-Hartman 	}
59764dbee31SPeter Hurley 
59864dbee31SPeter Hurley 	__uart_start(tty);
5999ed19428SPeter Hurley 	uart_port_unlock(port, flags);
600ab4382d2SGreg Kroah-Hartman 	return ret;
601ab4382d2SGreg Kroah-Hartman }
602ab4382d2SGreg Kroah-Hartman 
60303b3b1a2SJiri Slaby static unsigned int uart_write_room(struct tty_struct *tty)
604ab4382d2SGreg Kroah-Hartman {
605ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6069ed19428SPeter Hurley 	struct uart_port *port;
607ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
60803b3b1a2SJiri Slaby 	unsigned int ret;
609ab4382d2SGreg Kroah-Hartman 
6109ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
611ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_free(&state->xmit);
6129ed19428SPeter Hurley 	uart_port_unlock(port, flags);
613ab4382d2SGreg Kroah-Hartman 	return ret;
614ab4382d2SGreg Kroah-Hartman }
615ab4382d2SGreg Kroah-Hartman 
616fff4ef17SJiri Slaby static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
617ab4382d2SGreg Kroah-Hartman {
618ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6199ed19428SPeter Hurley 	struct uart_port *port;
620ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
621fff4ef17SJiri Slaby 	unsigned int ret;
622ab4382d2SGreg Kroah-Hartman 
6239ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
624ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_pending(&state->xmit);
6259ed19428SPeter Hurley 	uart_port_unlock(port, flags);
626ab4382d2SGreg Kroah-Hartman 	return ret;
627ab4382d2SGreg Kroah-Hartman }
628ab4382d2SGreg Kroah-Hartman 
629ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty)
630ab4382d2SGreg Kroah-Hartman {
631ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
632ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
633ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
634ab4382d2SGreg Kroah-Hartman 
635ab4382d2SGreg Kroah-Hartman 	/*
636ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
637ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
638ab4382d2SGreg Kroah-Hartman 	 */
639ab4382d2SGreg Kroah-Hartman 	if (!state) {
640ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
641ab4382d2SGreg Kroah-Hartman 		return;
642ab4382d2SGreg Kroah-Hartman 	}
643ab4382d2SGreg Kroah-Hartman 
644ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
645ab4382d2SGreg Kroah-Hartman 
6469ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6479ed19428SPeter Hurley 	if (!port)
6489ed19428SPeter Hurley 		return;
649ab4382d2SGreg Kroah-Hartman 	uart_circ_clear(&state->xmit);
650ab4382d2SGreg Kroah-Hartman 	if (port->ops->flush_buffer)
651ab4382d2SGreg Kroah-Hartman 		port->ops->flush_buffer(port);
6529ed19428SPeter Hurley 	uart_port_unlock(port, flags);
653d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
654ab4382d2SGreg Kroah-Hartman }
655ab4382d2SGreg Kroah-Hartman 
656ab4382d2SGreg Kroah-Hartman /*
657f58c252eSIlpo Järvinen  * This function performs low-level write of high-priority XON/XOFF
658f58c252eSIlpo Järvinen  * character and accounting for it.
659f58c252eSIlpo Järvinen  *
660f58c252eSIlpo Järvinen  * Requires uart_port to implement .serial_out().
661f58c252eSIlpo Järvinen  */
662f58c252eSIlpo Järvinen void uart_xchar_out(struct uart_port *uport, int offset)
663f58c252eSIlpo Järvinen {
664f58c252eSIlpo Järvinen 	serial_port_out(uport, offset, uport->x_char);
665f58c252eSIlpo Järvinen 	uport->icount.tx++;
666f58c252eSIlpo Järvinen 	uport->x_char = 0;
667f58c252eSIlpo Järvinen }
668f58c252eSIlpo Järvinen EXPORT_SYMBOL_GPL(uart_xchar_out);
669f58c252eSIlpo Järvinen 
670f58c252eSIlpo Järvinen /*
671ab4382d2SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
672ab4382d2SGreg Kroah-Hartman  * the device
673ab4382d2SGreg Kroah-Hartman  */
674ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch)
675ab4382d2SGreg Kroah-Hartman {
676ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6779ed19428SPeter Hurley 	struct uart_port *port;
678ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
679ab4382d2SGreg Kroah-Hartman 
6809ed19428SPeter Hurley 	port = uart_port_ref(state);
6819ed19428SPeter Hurley 	if (!port)
6829ed19428SPeter Hurley 		return;
6839ed19428SPeter Hurley 
684ab4382d2SGreg Kroah-Hartman 	if (port->ops->send_xchar)
685ab4382d2SGreg Kroah-Hartman 		port->ops->send_xchar(port, ch);
686ab4382d2SGreg Kroah-Hartman 	else {
687ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
688c235ccc1SPeter Hurley 		port->x_char = ch;
689c235ccc1SPeter Hurley 		if (ch)
690ab4382d2SGreg Kroah-Hartman 			port->ops->start_tx(port);
691ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
692ab4382d2SGreg Kroah-Hartman 	}
6939ed19428SPeter Hurley 	uart_port_deref(port);
694ab4382d2SGreg Kroah-Hartman }
695ab4382d2SGreg Kroah-Hartman 
696ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty)
697ab4382d2SGreg Kroah-Hartman {
698ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
699c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7009ed19428SPeter Hurley 	struct uart_port *port;
701ab4382d2SGreg Kroah-Hartman 
7029ed19428SPeter Hurley 	port = uart_port_ref(state);
7039ed19428SPeter Hurley 	if (!port)
7049ed19428SPeter Hurley 		return;
7059ed19428SPeter Hurley 
706ab4382d2SGreg Kroah-Hartman 	if (I_IXOFF(tty))
707391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7089db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
709391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7109aba8d5bSRussell King 
711391f93f2SPeter Hurley 	if (port->status & mask) {
7129aba8d5bSRussell King 		port->ops->throttle(port);
713391f93f2SPeter Hurley 		mask &= ~port->status;
7149aba8d5bSRussell King 	}
7159aba8d5bSRussell King 
716391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
7179aba8d5bSRussell King 		uart_clear_mctrl(port, TIOCM_RTS);
718b4749b97SPeter Hurley 
719b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
720b4749b97SPeter Hurley 		uart_send_xchar(tty, STOP_CHAR(tty));
7219ed19428SPeter Hurley 
7229ed19428SPeter Hurley 	uart_port_deref(port);
723ab4382d2SGreg Kroah-Hartman }
724ab4382d2SGreg Kroah-Hartman 
725ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty)
726ab4382d2SGreg Kroah-Hartman {
727ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
728c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7299ed19428SPeter Hurley 	struct uart_port *port;
730ab4382d2SGreg Kroah-Hartman 
7319ed19428SPeter Hurley 	port = uart_port_ref(state);
7329ed19428SPeter Hurley 	if (!port)
7339ed19428SPeter Hurley 		return;
7349ed19428SPeter Hurley 
7359aba8d5bSRussell King 	if (I_IXOFF(tty))
736391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7379db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
738391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7399aba8d5bSRussell King 
740391f93f2SPeter Hurley 	if (port->status & mask) {
7419aba8d5bSRussell King 		port->ops->unthrottle(port);
742391f93f2SPeter Hurley 		mask &= ~port->status;
7439aba8d5bSRussell King 	}
7449aba8d5bSRussell King 
745391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
746ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(port, TIOCM_RTS);
747b4749b97SPeter Hurley 
748b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
749b4749b97SPeter Hurley 		uart_send_xchar(tty, START_CHAR(tty));
7509ed19428SPeter Hurley 
7519ed19428SPeter Hurley 	uart_port_deref(port);
752ab4382d2SGreg Kroah-Hartman }
753ab4382d2SGreg Kroah-Hartman 
7544047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
755ab4382d2SGreg Kroah-Hartman {
7569f109694SAlan Cox 	struct uart_state *state = container_of(port, struct uart_state, port);
7574047b371SPeter Hurley 	struct uart_port *uport;
7584047b371SPeter Hurley 	int ret = -ENODEV;
7597ba2e769SAlan Cox 
7603abe8c76SPeter Hurley 	/*
7613abe8c76SPeter Hurley 	 * Ensure the state we copy is consistent and no hardware changes
7623abe8c76SPeter Hurley 	 * occur as we go
7633abe8c76SPeter Hurley 	 */
7643abe8c76SPeter Hurley 	mutex_lock(&port->mutex);
7654047b371SPeter Hurley 	uport = uart_port_check(state);
7664047b371SPeter Hurley 	if (!uport)
7674047b371SPeter Hurley 		goto out;
7684047b371SPeter Hurley 
7697ba2e769SAlan Cox 	retinfo->type	    = uport->type;
7707ba2e769SAlan Cox 	retinfo->line	    = uport->line;
7717ba2e769SAlan Cox 	retinfo->port	    = uport->iobase;
7727ba2e769SAlan Cox 	if (HIGH_BITS_OFFSET)
7737ba2e769SAlan Cox 		retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
7747ba2e769SAlan Cox 	retinfo->irq		    = uport->irq;
775a17e74c5SAndy Shevchenko 	retinfo->flags	    = (__force int)uport->flags;
7767ba2e769SAlan Cox 	retinfo->xmit_fifo_size  = uport->fifosize;
7777ba2e769SAlan Cox 	retinfo->baud_base	    = uport->uartclk / 16;
7787ba2e769SAlan Cox 	retinfo->close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
7797ba2e769SAlan Cox 	retinfo->closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
7807ba2e769SAlan Cox 				ASYNC_CLOSING_WAIT_NONE :
7817ba2e769SAlan Cox 				jiffies_to_msecs(port->closing_wait) / 10;
7827ba2e769SAlan Cox 	retinfo->custom_divisor  = uport->custom_divisor;
7837ba2e769SAlan Cox 	retinfo->hub6	    = uport->hub6;
7847ba2e769SAlan Cox 	retinfo->io_type         = uport->iotype;
7857ba2e769SAlan Cox 	retinfo->iomem_reg_shift = uport->regshift;
7867ba2e769SAlan Cox 	retinfo->iomem_base      = (void *)(unsigned long)uport->mapbase;
7874047b371SPeter Hurley 
7884047b371SPeter Hurley 	ret = 0;
7894047b371SPeter Hurley out:
790ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
7914047b371SPeter Hurley 	return ret;
7929f109694SAlan Cox }
7939f109694SAlan Cox 
7945099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty,
7955099d234SAl Viro 			 struct serial_struct *ss)
7969f109694SAlan Cox {
7975099d234SAl Viro 	struct uart_state *state = tty->driver_data;
7985099d234SAl Viro 	struct tty_port *port = &state->port;
7993abe8c76SPeter Hurley 
8005099d234SAl Viro 	return uart_get_info(port, ss) < 0 ? -EIO : 0;
801ab4382d2SGreg Kroah-Hartman }
802ab4382d2SGreg Kroah-Hartman 
8037ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
8047ba2e769SAlan Cox 			 struct uart_state *state,
8057ba2e769SAlan Cox 			 struct serial_struct *new_info)
806ab4382d2SGreg Kroah-Hartman {
8074047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
808ab4382d2SGreg Kroah-Hartman 	unsigned long new_port;
809ab4382d2SGreg Kroah-Hartman 	unsigned int change_irq, change_port, closing_wait;
810ab4382d2SGreg Kroah-Hartman 	unsigned int old_custom_divisor, close_delay;
811ab4382d2SGreg Kroah-Hartman 	upf_t old_flags, new_flags;
812ab4382d2SGreg Kroah-Hartman 	int retval = 0;
813ab4382d2SGreg Kroah-Hartman 
8144047b371SPeter Hurley 	if (!uport)
8154047b371SPeter Hurley 		return -EIO;
8164047b371SPeter Hurley 
8177ba2e769SAlan Cox 	new_port = new_info->port;
818ab4382d2SGreg Kroah-Hartman 	if (HIGH_BITS_OFFSET)
8197ba2e769SAlan Cox 		new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
820ab4382d2SGreg Kroah-Hartman 
8217ba2e769SAlan Cox 	new_info->irq = irq_canonicalize(new_info->irq);
8227ba2e769SAlan Cox 	close_delay = msecs_to_jiffies(new_info->close_delay * 10);
8237ba2e769SAlan Cox 	closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
8244cb0fbfdSJiri Slaby 			ASYNC_CLOSING_WAIT_NONE :
8257ba2e769SAlan Cox 			msecs_to_jiffies(new_info->closing_wait * 10);
826ab4382d2SGreg Kroah-Hartman 
827ab4382d2SGreg Kroah-Hartman 
828ab4382d2SGreg Kroah-Hartman 	change_irq  = !(uport->flags & UPF_FIXED_PORT)
8297ba2e769SAlan Cox 		&& new_info->irq != uport->irq;
830ab4382d2SGreg Kroah-Hartman 
831ab4382d2SGreg Kroah-Hartman 	/*
832ab4382d2SGreg Kroah-Hartman 	 * Since changing the 'type' of the port changes its resource
833ab4382d2SGreg Kroah-Hartman 	 * allocations, we should treat type changes the same as
834ab4382d2SGreg Kroah-Hartman 	 * IO port changes.
835ab4382d2SGreg Kroah-Hartman 	 */
836ab4382d2SGreg Kroah-Hartman 	change_port = !(uport->flags & UPF_FIXED_PORT)
837ab4382d2SGreg Kroah-Hartman 		&& (new_port != uport->iobase ||
8387ba2e769SAlan Cox 		    (unsigned long)new_info->iomem_base != uport->mapbase ||
8397ba2e769SAlan Cox 		    new_info->hub6 != uport->hub6 ||
8407ba2e769SAlan Cox 		    new_info->io_type != uport->iotype ||
8417ba2e769SAlan Cox 		    new_info->iomem_reg_shift != uport->regshift ||
8427ba2e769SAlan Cox 		    new_info->type != uport->type);
843ab4382d2SGreg Kroah-Hartman 
844ab4382d2SGreg Kroah-Hartman 	old_flags = uport->flags;
845a17e74c5SAndy Shevchenko 	new_flags = (__force upf_t)new_info->flags;
846ab4382d2SGreg Kroah-Hartman 	old_custom_divisor = uport->custom_divisor;
847ab4382d2SGreg Kroah-Hartman 
848ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
849ab4382d2SGreg Kroah-Hartman 		retval = -EPERM;
850ab4382d2SGreg Kroah-Hartman 		if (change_irq || change_port ||
8517ba2e769SAlan Cox 		    (new_info->baud_base != uport->uartclk / 16) ||
852ab4382d2SGreg Kroah-Hartman 		    (close_delay != port->close_delay) ||
853ab4382d2SGreg Kroah-Hartman 		    (closing_wait != port->closing_wait) ||
8547ba2e769SAlan Cox 		    (new_info->xmit_fifo_size &&
8557ba2e769SAlan Cox 		     new_info->xmit_fifo_size != uport->fifosize) ||
856ab4382d2SGreg Kroah-Hartman 		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
857ab4382d2SGreg Kroah-Hartman 			goto exit;
858ab4382d2SGreg Kroah-Hartman 		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
859ab4382d2SGreg Kroah-Hartman 			       (new_flags & UPF_USR_MASK));
8607ba2e769SAlan Cox 		uport->custom_divisor = new_info->custom_divisor;
861ab4382d2SGreg Kroah-Hartman 		goto check_and_exit;
862ab4382d2SGreg Kroah-Hartman 	}
863ab4382d2SGreg Kroah-Hartman 
8645e722b21SOndrej Mosnacek 	if (change_irq || change_port) {
865794edf30SDavid Howells 		retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
8665e722b21SOndrej Mosnacek 		if (retval)
867794edf30SDavid Howells 			goto exit;
8685e722b21SOndrej Mosnacek 	}
869794edf30SDavid Howells 
870ab4382d2SGreg Kroah-Hartman 	/*
871ab4382d2SGreg Kroah-Hartman 	 * Ask the low level driver to verify the settings.
872ab4382d2SGreg Kroah-Hartman 	 */
873ab4382d2SGreg Kroah-Hartman 	if (uport->ops->verify_port)
8747ba2e769SAlan Cox 		retval = uport->ops->verify_port(uport, new_info);
875ab4382d2SGreg Kroah-Hartman 
8767ba2e769SAlan Cox 	if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
8777ba2e769SAlan Cox 	    (new_info->baud_base < 9600))
878ab4382d2SGreg Kroah-Hartman 		retval = -EINVAL;
879ab4382d2SGreg Kroah-Hartman 
880ab4382d2SGreg Kroah-Hartman 	if (retval)
881ab4382d2SGreg Kroah-Hartman 		goto exit;
882ab4382d2SGreg Kroah-Hartman 
883ab4382d2SGreg Kroah-Hartman 	if (change_port || change_irq) {
884ab4382d2SGreg Kroah-Hartman 		retval = -EBUSY;
885ab4382d2SGreg Kroah-Hartman 
886ab4382d2SGreg Kroah-Hartman 		/*
887ab4382d2SGreg Kroah-Hartman 		 * Make sure that we are the sole user of this port.
888ab4382d2SGreg Kroah-Hartman 		 */
889ab4382d2SGreg Kroah-Hartman 		if (tty_port_users(port) > 1)
890ab4382d2SGreg Kroah-Hartman 			goto exit;
891ab4382d2SGreg Kroah-Hartman 
892ab4382d2SGreg Kroah-Hartman 		/*
893ab4382d2SGreg Kroah-Hartman 		 * We need to shutdown the serial port at the old
894ab4382d2SGreg Kroah-Hartman 		 * port/type/irq combination.
895ab4382d2SGreg Kroah-Hartman 		 */
896ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
897ab4382d2SGreg Kroah-Hartman 	}
898ab4382d2SGreg Kroah-Hartman 
899ab4382d2SGreg Kroah-Hartman 	if (change_port) {
900ab4382d2SGreg Kroah-Hartman 		unsigned long old_iobase, old_mapbase;
901ab4382d2SGreg Kroah-Hartman 		unsigned int old_type, old_iotype, old_hub6, old_shift;
902ab4382d2SGreg Kroah-Hartman 
903ab4382d2SGreg Kroah-Hartman 		old_iobase = uport->iobase;
904ab4382d2SGreg Kroah-Hartman 		old_mapbase = uport->mapbase;
905ab4382d2SGreg Kroah-Hartman 		old_type = uport->type;
906ab4382d2SGreg Kroah-Hartman 		old_hub6 = uport->hub6;
907ab4382d2SGreg Kroah-Hartman 		old_iotype = uport->iotype;
908ab4382d2SGreg Kroah-Hartman 		old_shift = uport->regshift;
909ab4382d2SGreg Kroah-Hartman 
910ab4382d2SGreg Kroah-Hartman 		/*
911ab4382d2SGreg Kroah-Hartman 		 * Free and release old regions
912ab4382d2SGreg Kroah-Hartman 		 */
913a7cfaf16SFabio Estevam 		if (old_type != PORT_UNKNOWN && uport->ops->release_port)
914ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
915ab4382d2SGreg Kroah-Hartman 
916ab4382d2SGreg Kroah-Hartman 		uport->iobase = new_port;
9177ba2e769SAlan Cox 		uport->type = new_info->type;
9187ba2e769SAlan Cox 		uport->hub6 = new_info->hub6;
9197ba2e769SAlan Cox 		uport->iotype = new_info->io_type;
9207ba2e769SAlan Cox 		uport->regshift = new_info->iomem_reg_shift;
9217ba2e769SAlan Cox 		uport->mapbase = (unsigned long)new_info->iomem_base;
922ab4382d2SGreg Kroah-Hartman 
923ab4382d2SGreg Kroah-Hartman 		/*
924ab4382d2SGreg Kroah-Hartman 		 * Claim and map the new regions
925ab4382d2SGreg Kroah-Hartman 		 */
926a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
927ab4382d2SGreg Kroah-Hartman 			retval = uport->ops->request_port(uport);
928ab4382d2SGreg Kroah-Hartman 		} else {
929ab4382d2SGreg Kroah-Hartman 			/* Always success - Jean II */
930ab4382d2SGreg Kroah-Hartman 			retval = 0;
931ab4382d2SGreg Kroah-Hartman 		}
932ab4382d2SGreg Kroah-Hartman 
933ab4382d2SGreg Kroah-Hartman 		/*
934ab4382d2SGreg Kroah-Hartman 		 * If we fail to request resources for the
935ab4382d2SGreg Kroah-Hartman 		 * new port, try to restore the old settings.
936ab4382d2SGreg Kroah-Hartman 		 */
9377deb39edSThomas Pfaff 		if (retval) {
938ab4382d2SGreg Kroah-Hartman 			uport->iobase = old_iobase;
939ab4382d2SGreg Kroah-Hartman 			uport->type = old_type;
940ab4382d2SGreg Kroah-Hartman 			uport->hub6 = old_hub6;
941ab4382d2SGreg Kroah-Hartman 			uport->iotype = old_iotype;
942ab4382d2SGreg Kroah-Hartman 			uport->regshift = old_shift;
943ab4382d2SGreg Kroah-Hartman 			uport->mapbase = old_mapbase;
9447deb39edSThomas Pfaff 
9457deb39edSThomas Pfaff 			if (old_type != PORT_UNKNOWN) {
946ab4382d2SGreg Kroah-Hartman 				retval = uport->ops->request_port(uport);
947ab4382d2SGreg Kroah-Hartman 				/*
948ab4382d2SGreg Kroah-Hartman 				 * If we failed to restore the old settings,
949ab4382d2SGreg Kroah-Hartman 				 * we fail like this.
950ab4382d2SGreg Kroah-Hartman 				 */
951ab4382d2SGreg Kroah-Hartman 				if (retval)
952ab4382d2SGreg Kroah-Hartman 					uport->type = PORT_UNKNOWN;
953ab4382d2SGreg Kroah-Hartman 
954ab4382d2SGreg Kroah-Hartman 				/*
955ab4382d2SGreg Kroah-Hartman 				 * We failed anyway.
956ab4382d2SGreg Kroah-Hartman 				 */
957ab4382d2SGreg Kroah-Hartman 				retval = -EBUSY;
9587deb39edSThomas Pfaff 			}
9597deb39edSThomas Pfaff 
960ab4382d2SGreg Kroah-Hartman 			/* Added to return the correct error -Ram Gupta */
961ab4382d2SGreg Kroah-Hartman 			goto exit;
962ab4382d2SGreg Kroah-Hartman 		}
963ab4382d2SGreg Kroah-Hartman 	}
964ab4382d2SGreg Kroah-Hartman 
965ab4382d2SGreg Kroah-Hartman 	if (change_irq)
9667ba2e769SAlan Cox 		uport->irq      = new_info->irq;
967ab4382d2SGreg Kroah-Hartman 	if (!(uport->flags & UPF_FIXED_PORT))
9687ba2e769SAlan Cox 		uport->uartclk  = new_info->baud_base * 16;
969ab4382d2SGreg Kroah-Hartman 	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
970ab4382d2SGreg Kroah-Hartman 				 (new_flags & UPF_CHANGE_MASK);
9717ba2e769SAlan Cox 	uport->custom_divisor   = new_info->custom_divisor;
972ab4382d2SGreg Kroah-Hartman 	port->close_delay     = close_delay;
973ab4382d2SGreg Kroah-Hartman 	port->closing_wait    = closing_wait;
9747ba2e769SAlan Cox 	if (new_info->xmit_fifo_size)
9757ba2e769SAlan Cox 		uport->fifosize = new_info->xmit_fifo_size;
976ab4382d2SGreg Kroah-Hartman 
977ab4382d2SGreg Kroah-Hartman  check_and_exit:
978ab4382d2SGreg Kroah-Hartman 	retval = 0;
979ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
980ab4382d2SGreg Kroah-Hartman 		goto exit;
981d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
982ab4382d2SGreg Kroah-Hartman 		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
983ab4382d2SGreg Kroah-Hartman 		    old_custom_divisor != uport->custom_divisor) {
984ab4382d2SGreg Kroah-Hartman 			/*
985ab4382d2SGreg Kroah-Hartman 			 * If they're setting up a custom divisor or speed,
986dc2f7271SJohan Hovold 			 * instead of clearing it, then bitch about it.
987ab4382d2SGreg Kroah-Hartman 			 */
988ab4382d2SGreg Kroah-Hartman 			if (uport->flags & UPF_SPD_MASK) {
989dc2f7271SJohan Hovold 				dev_notice_ratelimited(uport->dev,
9902f2dafe7SSudip Mukherjee 				       "%s sets custom speed on %s. This is deprecated.\n",
9912f2dafe7SSudip Mukherjee 				      current->comm,
992429b4749SRasmus Villemoes 				      tty_name(port->tty));
993ab4382d2SGreg Kroah-Hartman 			}
994826736a6SIlpo Järvinen 			uart_change_line_settings(tty, state, NULL);
995ab4382d2SGreg Kroah-Hartman 		}
996b3b57646SRob Herring 	} else {
997dcd794c6SIlpo Järvinen 		retval = uart_startup(tty, state, true);
99844117a1dSSebastian Andrzej Siewior 		if (retval == 0)
99944117a1dSSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1000b3b57646SRob Herring 		if (retval > 0)
1001b3b57646SRob Herring 			retval = 0;
1002b3b57646SRob Herring 	}
1003ab4382d2SGreg Kroah-Hartman  exit:
10047ba2e769SAlan Cox 	return retval;
10057ba2e769SAlan Cox }
10067ba2e769SAlan Cox 
10075099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
10087ba2e769SAlan Cox {
10095099d234SAl Viro 	struct uart_state *state = tty->driver_data;
10107ba2e769SAlan Cox 	struct tty_port *port = &state->port;
10117ba2e769SAlan Cox 	int retval;
10127ba2e769SAlan Cox 
10135099d234SAl Viro 	down_write(&tty->termios_rwsem);
10147ba2e769SAlan Cox 	/*
10157ba2e769SAlan Cox 	 * This semaphore protects port->count.  It is also
10167ba2e769SAlan Cox 	 * very useful to prevent opens.  Also, take the
10177ba2e769SAlan Cox 	 * port configuration semaphore to make sure that a
10187ba2e769SAlan Cox 	 * module insertion/removal doesn't change anything
10197ba2e769SAlan Cox 	 * under us.
10207ba2e769SAlan Cox 	 */
10217ba2e769SAlan Cox 	mutex_lock(&port->mutex);
10225099d234SAl Viro 	retval = uart_set_info(tty, port, state, ss);
1023ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
10245099d234SAl Viro 	up_write(&tty->termios_rwsem);
1025ab4382d2SGreg Kroah-Hartman 	return retval;
1026ab4382d2SGreg Kroah-Hartman }
1027ab4382d2SGreg Kroah-Hartman 
1028ab4382d2SGreg Kroah-Hartman /**
1029ab4382d2SGreg Kroah-Hartman  * uart_get_lsr_info - get line status register info
1030ab4382d2SGreg Kroah-Hartman  * @tty: tty associated with the UART
1031ab4382d2SGreg Kroah-Hartman  * @state: UART being queried
1032ab4382d2SGreg Kroah-Hartman  * @value: returned modem value
1033ab4382d2SGreg Kroah-Hartman  */
1034ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty,
1035ab4382d2SGreg Kroah-Hartman 			struct uart_state *state, unsigned int __user *value)
1036ab4382d2SGreg Kroah-Hartman {
10374047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1038ab4382d2SGreg Kroah-Hartman 	unsigned int result;
1039ab4382d2SGreg Kroah-Hartman 
1040ab4382d2SGreg Kroah-Hartman 	result = uport->ops->tx_empty(uport);
1041ab4382d2SGreg Kroah-Hartman 
1042ab4382d2SGreg Kroah-Hartman 	/*
1043ab4382d2SGreg Kroah-Hartman 	 * If we're about to load something into the transmit
1044ab4382d2SGreg Kroah-Hartman 	 * register, we'll pretend the transmitter isn't empty to
1045ab4382d2SGreg Kroah-Hartman 	 * avoid a race condition (depending on when the transmit
1046ab4382d2SGreg Kroah-Hartman 	 * interrupt happens).
1047ab4382d2SGreg Kroah-Hartman 	 */
1048ab4382d2SGreg Kroah-Hartman 	if (uport->x_char ||
1049ab4382d2SGreg Kroah-Hartman 	    ((uart_circ_chars_pending(&state->xmit) > 0) &&
1050d01f4d18SPeter Hurley 	     !uart_tx_stopped(uport)))
1051ab4382d2SGreg Kroah-Hartman 		result &= ~TIOCSER_TEMT;
1052ab4382d2SGreg Kroah-Hartman 
1053ab4382d2SGreg Kroah-Hartman 	return put_user(result, value);
1054ab4382d2SGreg Kroah-Hartman }
1055ab4382d2SGreg Kroah-Hartman 
105660b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty)
1057ab4382d2SGreg Kroah-Hartman {
1058ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1059ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10604047b371SPeter Hurley 	struct uart_port *uport;
1061ab4382d2SGreg Kroah-Hartman 	int result = -EIO;
1062ab4382d2SGreg Kroah-Hartman 
1063ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10644047b371SPeter Hurley 	uport = uart_port_check(state);
10654047b371SPeter Hurley 	if (!uport)
10664047b371SPeter Hurley 		goto out;
10674047b371SPeter Hurley 
106818900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1069ab4382d2SGreg Kroah-Hartman 		result = uport->mctrl;
1070ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1071ab4382d2SGreg Kroah-Hartman 		result |= uport->ops->get_mctrl(uport);
1072ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1073ab4382d2SGreg Kroah-Hartman 	}
10744047b371SPeter Hurley out:
1075ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1076ab4382d2SGreg Kroah-Hartman 	return result;
1077ab4382d2SGreg Kroah-Hartman }
1078ab4382d2SGreg Kroah-Hartman 
1079ab4382d2SGreg Kroah-Hartman static int
108020b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1081ab4382d2SGreg Kroah-Hartman {
1082ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1083ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10844047b371SPeter Hurley 	struct uart_port *uport;
1085ab4382d2SGreg Kroah-Hartman 	int ret = -EIO;
1086ab4382d2SGreg Kroah-Hartman 
1087ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10884047b371SPeter Hurley 	uport = uart_port_check(state);
10894047b371SPeter Hurley 	if (!uport)
10904047b371SPeter Hurley 		goto out;
10914047b371SPeter Hurley 
109218900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1093ab4382d2SGreg Kroah-Hartman 		uart_update_mctrl(uport, set, clear);
1094ab4382d2SGreg Kroah-Hartman 		ret = 0;
1095ab4382d2SGreg Kroah-Hartman 	}
10964047b371SPeter Hurley out:
1097ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1098ab4382d2SGreg Kroah-Hartman 	return ret;
1099ab4382d2SGreg Kroah-Hartman }
1100ab4382d2SGreg Kroah-Hartman 
1101ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state)
1102ab4382d2SGreg Kroah-Hartman {
1103ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1104ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11054047b371SPeter Hurley 	struct uart_port *uport;
11064047b371SPeter Hurley 	int ret = -EIO;
1107ab4382d2SGreg Kroah-Hartman 
1108ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
11094047b371SPeter Hurley 	uport = uart_port_check(state);
11104047b371SPeter Hurley 	if (!uport)
11114047b371SPeter Hurley 		goto out;
1112ab4382d2SGreg Kroah-Hartman 
11137d73170eSJiangfeng Xiao 	if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1114ab4382d2SGreg Kroah-Hartman 		uport->ops->break_ctl(uport, break_state);
11154047b371SPeter Hurley 	ret = 0;
11164047b371SPeter Hurley out:
1117ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
11184047b371SPeter Hurley 	return ret;
1119ab4382d2SGreg Kroah-Hartman }
1120ab4382d2SGreg Kroah-Hartman 
1121ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1122ab4382d2SGreg Kroah-Hartman {
1123ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11244047b371SPeter Hurley 	struct uart_port *uport;
1125ab4382d2SGreg Kroah-Hartman 	int flags, ret;
1126ab4382d2SGreg Kroah-Hartman 
1127ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
1128ab4382d2SGreg Kroah-Hartman 		return -EPERM;
1129ab4382d2SGreg Kroah-Hartman 
1130ab4382d2SGreg Kroah-Hartman 	/*
1131ab4382d2SGreg Kroah-Hartman 	 * Take the per-port semaphore.  This prevents count from
1132ab4382d2SGreg Kroah-Hartman 	 * changing, and hence any extra opens of the port while
1133ab4382d2SGreg Kroah-Hartman 	 * we're auto-configuring.
1134ab4382d2SGreg Kroah-Hartman 	 */
1135ab4382d2SGreg Kroah-Hartman 	if (mutex_lock_interruptible(&port->mutex))
1136ab4382d2SGreg Kroah-Hartman 		return -ERESTARTSYS;
1137ab4382d2SGreg Kroah-Hartman 
11384047b371SPeter Hurley 	uport = uart_port_check(state);
11394047b371SPeter Hurley 	if (!uport) {
11404047b371SPeter Hurley 		ret = -EIO;
11414047b371SPeter Hurley 		goto out;
11424047b371SPeter Hurley 	}
11434047b371SPeter Hurley 
1144ab4382d2SGreg Kroah-Hartman 	ret = -EBUSY;
1145ab4382d2SGreg Kroah-Hartman 	if (tty_port_users(port) == 1) {
1146ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1147ab4382d2SGreg Kroah-Hartman 
1148ab4382d2SGreg Kroah-Hartman 		/*
1149ab4382d2SGreg Kroah-Hartman 		 * If we already have a port type configured,
1150ab4382d2SGreg Kroah-Hartman 		 * we must release its resources.
1151ab4382d2SGreg Kroah-Hartman 		 */
1152a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1153ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
1154ab4382d2SGreg Kroah-Hartman 
1155ab4382d2SGreg Kroah-Hartman 		flags = UART_CONFIG_TYPE;
1156ab4382d2SGreg Kroah-Hartman 		if (uport->flags & UPF_AUTO_IRQ)
1157ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_IRQ;
1158ab4382d2SGreg Kroah-Hartman 
1159ab4382d2SGreg Kroah-Hartman 		/*
1160ab4382d2SGreg Kroah-Hartman 		 * This will claim the ports resources if
1161ab4382d2SGreg Kroah-Hartman 		 * a port is found.
1162ab4382d2SGreg Kroah-Hartman 		 */
1163ab4382d2SGreg Kroah-Hartman 		uport->ops->config_port(uport, flags);
1164ab4382d2SGreg Kroah-Hartman 
1165dcd794c6SIlpo Järvinen 		ret = uart_startup(tty, state, true);
116671456906SSebastian Andrzej Siewior 		if (ret == 0)
116771456906SSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1168b3b57646SRob Herring 		if (ret > 0)
1169b3b57646SRob Herring 			ret = 0;
1170ab4382d2SGreg Kroah-Hartman 	}
11714047b371SPeter Hurley out:
1172ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1173ab4382d2SGreg Kroah-Hartman 	return ret;
1174ab4382d2SGreg Kroah-Hartman }
1175ab4382d2SGreg Kroah-Hartman 
11761fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport)
11771fdc3106SAlexander Shiyan {
11781fdc3106SAlexander Shiyan 	/*
11791fdc3106SAlexander Shiyan 	 * Force modem status interrupts on
11801fdc3106SAlexander Shiyan 	 */
11811fdc3106SAlexander Shiyan 	if (uport->ops->enable_ms)
11821fdc3106SAlexander Shiyan 		uport->ops->enable_ms(uport);
11831fdc3106SAlexander Shiyan }
11841fdc3106SAlexander Shiyan 
1185ab4382d2SGreg Kroah-Hartman /*
1186ab4382d2SGreg Kroah-Hartman  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1187ab4382d2SGreg Kroah-Hartman  * - mask passed in arg for lines of interest
1188ab4382d2SGreg Kroah-Hartman  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1189ab4382d2SGreg Kroah-Hartman  * Caller should use TIOCGICOUNT to see which one it was
1190ab4382d2SGreg Kroah-Hartman  *
1191ab4382d2SGreg Kroah-Hartman  * FIXME: This wants extracting into a common all driver implementation
1192ab4382d2SGreg Kroah-Hartman  * of TIOCMWAIT using tty_port.
1193ab4382d2SGreg Kroah-Hartman  */
11949ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1195ab4382d2SGreg Kroah-Hartman {
11969ed19428SPeter Hurley 	struct uart_port *uport;
1197ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1198ab4382d2SGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
1199ab4382d2SGreg Kroah-Hartman 	struct uart_icount cprev, cnow;
1200ab4382d2SGreg Kroah-Hartman 	int ret;
1201ab4382d2SGreg Kroah-Hartman 
1202ab4382d2SGreg Kroah-Hartman 	/*
1203ab4382d2SGreg Kroah-Hartman 	 * note the counters on entry
1204ab4382d2SGreg Kroah-Hartman 	 */
12059ed19428SPeter Hurley 	uport = uart_port_ref(state);
12069ed19428SPeter Hurley 	if (!uport)
12079ed19428SPeter Hurley 		return -EIO;
1208ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1209ab4382d2SGreg Kroah-Hartman 	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
12101fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1211ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1212ab4382d2SGreg Kroah-Hartman 
1213ab4382d2SGreg Kroah-Hartman 	add_wait_queue(&port->delta_msr_wait, &wait);
1214ab4382d2SGreg Kroah-Hartman 	for (;;) {
1215ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1216ab4382d2SGreg Kroah-Hartman 		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1217ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1218ab4382d2SGreg Kroah-Hartman 
1219ab4382d2SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
1220ab4382d2SGreg Kroah-Hartman 
1221ab4382d2SGreg Kroah-Hartman 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1222ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1223ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1224ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1225ab4382d2SGreg Kroah-Hartman 			ret = 0;
1226ab4382d2SGreg Kroah-Hartman 			break;
1227ab4382d2SGreg Kroah-Hartman 		}
1228ab4382d2SGreg Kroah-Hartman 
1229ab4382d2SGreg Kroah-Hartman 		schedule();
1230ab4382d2SGreg Kroah-Hartman 
1231ab4382d2SGreg Kroah-Hartman 		/* see if a signal did it */
1232ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current)) {
1233ab4382d2SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
1234ab4382d2SGreg Kroah-Hartman 			break;
1235ab4382d2SGreg Kroah-Hartman 		}
1236ab4382d2SGreg Kroah-Hartman 
1237ab4382d2SGreg Kroah-Hartman 		cprev = cnow;
1238ab4382d2SGreg Kroah-Hartman 	}
123997f9f707SFabian Frederick 	__set_current_state(TASK_RUNNING);
1240ab4382d2SGreg Kroah-Hartman 	remove_wait_queue(&port->delta_msr_wait, &wait);
12419ed19428SPeter Hurley 	uart_port_deref(uport);
1242ab4382d2SGreg Kroah-Hartman 
1243ab4382d2SGreg Kroah-Hartman 	return ret;
1244ab4382d2SGreg Kroah-Hartman }
1245ab4382d2SGreg Kroah-Hartman 
1246ab4382d2SGreg Kroah-Hartman /*
1247ab4382d2SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1248ab4382d2SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1249ab4382d2SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1250ab4382d2SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1251ab4382d2SGreg Kroah-Hartman  */
1252ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty,
1253ab4382d2SGreg Kroah-Hartman 			  struct serial_icounter_struct *icount)
1254ab4382d2SGreg Kroah-Hartman {
1255ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1256ab4382d2SGreg Kroah-Hartman 	struct uart_icount cnow;
12579ed19428SPeter Hurley 	struct uart_port *uport;
1258ab4382d2SGreg Kroah-Hartman 
12599ed19428SPeter Hurley 	uport = uart_port_ref(state);
12609ed19428SPeter Hurley 	if (!uport)
12619ed19428SPeter Hurley 		return -EIO;
1262ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1263ab4382d2SGreg Kroah-Hartman 	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1264ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
12659ed19428SPeter Hurley 	uart_port_deref(uport);
1266ab4382d2SGreg Kroah-Hartman 
1267ab4382d2SGreg Kroah-Hartman 	icount->cts         = cnow.cts;
1268ab4382d2SGreg Kroah-Hartman 	icount->dsr         = cnow.dsr;
1269ab4382d2SGreg Kroah-Hartman 	icount->rng         = cnow.rng;
1270ab4382d2SGreg Kroah-Hartman 	icount->dcd         = cnow.dcd;
1271ab4382d2SGreg Kroah-Hartman 	icount->rx          = cnow.rx;
1272ab4382d2SGreg Kroah-Hartman 	icount->tx          = cnow.tx;
1273ab4382d2SGreg Kroah-Hartman 	icount->frame       = cnow.frame;
1274ab4382d2SGreg Kroah-Hartman 	icount->overrun     = cnow.overrun;
1275ab4382d2SGreg Kroah-Hartman 	icount->parity      = cnow.parity;
1276ab4382d2SGreg Kroah-Hartman 	icount->brk         = cnow.brk;
1277ab4382d2SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1278ab4382d2SGreg Kroah-Hartman 
1279ab4382d2SGreg Kroah-Hartman 	return 0;
1280ab4382d2SGreg Kroah-Hartman }
1281ab4382d2SGreg Kroah-Hartman 
128251ad36baSIlpo Järvinen #define SER_RS485_LEGACY_FLAGS	(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
128351ad36baSIlpo Järvinen 				 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
128451ad36baSIlpo Järvinen 				 SER_RS485_TERMINATE_BUS)
128551ad36baSIlpo Järvinen 
128651ad36baSIlpo Järvinen static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
128751ad36baSIlpo Järvinen {
128851ad36baSIlpo Järvinen 	u32 flags = rs485->flags;
128951ad36baSIlpo Järvinen 
129051ad36baSIlpo Järvinen 	/* Don't return -EINVAL for unsupported legacy flags */
129151ad36baSIlpo Järvinen 	flags &= ~SER_RS485_LEGACY_FLAGS;
129251ad36baSIlpo Järvinen 
129351ad36baSIlpo Järvinen 	/*
129451ad36baSIlpo Järvinen 	 * For any bit outside of the legacy ones that is not supported by
129551ad36baSIlpo Järvinen 	 * the driver, return -EINVAL.
129651ad36baSIlpo Järvinen 	 */
12970139da50SIlpo Järvinen 	if (flags & ~port->rs485_supported.flags)
129851ad36baSIlpo Järvinen 		return -EINVAL;
129951ad36baSIlpo Järvinen 
13004f768e94SIlpo Järvinen 	/* Asking for address w/o addressing mode? */
13014f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDRB) &&
13024f768e94SIlpo Järvinen 	    (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
13034f768e94SIlpo Järvinen 		return -EINVAL;
13044f768e94SIlpo Järvinen 
13054f768e94SIlpo Järvinen 	/* Address given but not enabled? */
13064f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
13074f768e94SIlpo Järvinen 		return -EINVAL;
13084f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
13094f768e94SIlpo Järvinen 		return -EINVAL;
13104f768e94SIlpo Järvinen 
131151ad36baSIlpo Järvinen 	return 0;
131251ad36baSIlpo Järvinen }
131351ad36baSIlpo Järvinen 
1314d8fcd9cfSLino Sanfilippo static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
1315d8fcd9cfSLino Sanfilippo 					      struct serial_rs485 *rs485)
13162dbd0c14SIlpo Järvinen {
13170139da50SIlpo Järvinen 	if (!port->rs485_supported.delay_rts_before_send) {
1318be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_before_send) {
1319be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1320be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay before sending not supported\n",
1321be2e2cb1SIlpo Järvinen 				port->name, port->line);
1322be2e2cb1SIlpo Järvinen 		}
1323be2e2cb1SIlpo Järvinen 		rs485->delay_rts_before_send = 0;
1324be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
13252dbd0c14SIlpo Järvinen 		rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
13262dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13272dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay before sending clamped to %u ms\n",
13282dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_before_send);
13292dbd0c14SIlpo Järvinen 	}
13302dbd0c14SIlpo Järvinen 
13310139da50SIlpo Järvinen 	if (!port->rs485_supported.delay_rts_after_send) {
1332be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_after_send) {
1333be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1334be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay after sending not supported\n",
1335be2e2cb1SIlpo Järvinen 				port->name, port->line);
1336be2e2cb1SIlpo Järvinen 		}
1337be2e2cb1SIlpo Järvinen 		rs485->delay_rts_after_send = 0;
1338be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
13392dbd0c14SIlpo Järvinen 		rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
13402dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13412dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay after sending clamped to %u ms\n",
13422dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_after_send);
13432dbd0c14SIlpo Järvinen 	}
1344d8fcd9cfSLino Sanfilippo }
1345d8fcd9cfSLino Sanfilippo 
1346d8fcd9cfSLino Sanfilippo static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1347d8fcd9cfSLino Sanfilippo {
1348d8fcd9cfSLino Sanfilippo 	u32 supported_flags = port->rs485_supported.flags;
1349d8fcd9cfSLino Sanfilippo 
1350d8fcd9cfSLino Sanfilippo 	if (!(rs485->flags & SER_RS485_ENABLED)) {
1351d8fcd9cfSLino Sanfilippo 		memset(rs485, 0, sizeof(*rs485));
1352d8fcd9cfSLino Sanfilippo 		return;
1353d8fcd9cfSLino Sanfilippo 	}
1354d8fcd9cfSLino Sanfilippo 
1355d8fcd9cfSLino Sanfilippo 	/* Pick sane settings if the user hasn't */
1356d8fcd9cfSLino Sanfilippo 	if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
1357d8fcd9cfSLino Sanfilippo 	    !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1358d8fcd9cfSLino Sanfilippo 	    !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
1359d8fcd9cfSLino Sanfilippo 		dev_warn_ratelimited(port->dev,
1360d8fcd9cfSLino Sanfilippo 			"%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1361d8fcd9cfSLino Sanfilippo 			port->name, port->line);
1362d8fcd9cfSLino Sanfilippo 		rs485->flags |= SER_RS485_RTS_ON_SEND;
1363d8fcd9cfSLino Sanfilippo 		rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1364d8fcd9cfSLino Sanfilippo 		supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
1365d8fcd9cfSLino Sanfilippo 	}
1366be2e2cb1SIlpo Järvinen 
1367be2e2cb1SIlpo Järvinen 	rs485->flags &= supported_flags;
1368be2e2cb1SIlpo Järvinen 
1369d8fcd9cfSLino Sanfilippo 	uart_sanitize_serial_rs485_delays(port, rs485);
1370d8fcd9cfSLino Sanfilippo 
13712dbd0c14SIlpo Järvinen 	/* Return clean padding area to userspace */
13724f768e94SIlpo Järvinen 	memset(rs485->padding0, 0, sizeof(rs485->padding0));
13734f768e94SIlpo Järvinen 	memset(rs485->padding1, 0, sizeof(rs485->padding1));
13742dbd0c14SIlpo Järvinen }
13752dbd0c14SIlpo Järvinen 
137644b27aecSLino Sanfilippo static void uart_set_rs485_termination(struct uart_port *port,
137744b27aecSLino Sanfilippo 				       const struct serial_rs485 *rs485)
137844b27aecSLino Sanfilippo {
137944b27aecSLino Sanfilippo 	if (!(rs485->flags & SER_RS485_ENABLED))
138044b27aecSLino Sanfilippo 		return;
138144b27aecSLino Sanfilippo 
138244b27aecSLino Sanfilippo 	gpiod_set_value_cansleep(port->rs485_term_gpio,
138344b27aecSLino Sanfilippo 				 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
138444b27aecSLino Sanfilippo }
138544b27aecSLino Sanfilippo 
13867c7f9bc9SLukas Wunner static int uart_rs485_config(struct uart_port *port)
13878322b1f5SIlpo Järvinen {
1388be2e2cb1SIlpo Järvinen 	struct serial_rs485 *rs485 = &port->rs485;
1389596a9171SIlpo Järvinen 	int ret;
1390be2e2cb1SIlpo Järvinen 
1391be2e2cb1SIlpo Järvinen 	uart_sanitize_serial_rs485(port, rs485);
139244b27aecSLino Sanfilippo 	uart_set_rs485_termination(port, rs485);
1393be2e2cb1SIlpo Järvinen 
1394ae50bb27SIlpo Järvinen 	ret = port->rs485_config(port, NULL, rs485);
1395596a9171SIlpo Järvinen 	if (ret)
1396596a9171SIlpo Järvinen 		memset(rs485, 0, sizeof(*rs485));
1397596a9171SIlpo Järvinen 
1398596a9171SIlpo Järvinen 	return ret;
13998322b1f5SIlpo Järvinen }
14008322b1f5SIlpo Järvinen 
1401a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port,
1402a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485)
1403a5f276f1SRicardo Ribalda Delgado {
1404bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1405bd737f87SRicardo Ribalda Delgado 	struct serial_rs485 aux;
1406bd737f87SRicardo Ribalda Delgado 
1407bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1408bd737f87SRicardo Ribalda Delgado 	aux = port->rs485;
1409bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1410bd737f87SRicardo Ribalda Delgado 
1411bd737f87SRicardo Ribalda Delgado 	if (copy_to_user(rs485, &aux, sizeof(aux)))
1412a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1413bd737f87SRicardo Ribalda Delgado 
1414a5f276f1SRicardo Ribalda Delgado 	return 0;
1415a5f276f1SRicardo Ribalda Delgado }
1416a5f276f1SRicardo Ribalda Delgado 
1417ae50bb27SIlpo Järvinen static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
1418a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485_user)
1419a5f276f1SRicardo Ribalda Delgado {
1420a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485 rs485;
1421a5f276f1SRicardo Ribalda Delgado 	int ret;
1422bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1423a5f276f1SRicardo Ribalda Delgado 
1424a5f276f1SRicardo Ribalda Delgado 	if (!port->rs485_config)
142579c5966cSJohan Hovold 		return -ENOTTY;
1426a5f276f1SRicardo Ribalda Delgado 
1427a5f276f1SRicardo Ribalda Delgado 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1428a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1429a5f276f1SRicardo Ribalda Delgado 
143051ad36baSIlpo Järvinen 	ret = uart_check_rs485_flags(port, &rs485);
143151ad36baSIlpo Järvinen 	if (ret)
143251ad36baSIlpo Järvinen 		return ret;
14332dbd0c14SIlpo Järvinen 	uart_sanitize_serial_rs485(port, &rs485);
143444b27aecSLino Sanfilippo 	uart_set_rs485_termination(port, &rs485);
14350ed12afaSLino Sanfilippo 
1436bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1437ae50bb27SIlpo Järvinen 	ret = port->rs485_config(port, &tty->termios, &rs485);
14387c7f9bc9SLukas Wunner 	if (!ret) {
14390ed12afaSLino Sanfilippo 		port->rs485 = rs485;
14407c7f9bc9SLukas Wunner 
14417c7f9bc9SLukas Wunner 		/* Reset RTS and other mctrl lines when disabling RS485 */
14427c7f9bc9SLukas Wunner 		if (!(rs485.flags & SER_RS485_ENABLED))
14437c7f9bc9SLukas Wunner 			port->ops->set_mctrl(port, port->mctrl);
14447c7f9bc9SLukas Wunner 	}
1445bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1446a5f276f1SRicardo Ribalda Delgado 	if (ret)
1447a5f276f1SRicardo Ribalda Delgado 		return ret;
1448a5f276f1SRicardo Ribalda Delgado 
1449a5f276f1SRicardo Ribalda Delgado 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1450a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1451a5f276f1SRicardo Ribalda Delgado 
1452a5f276f1SRicardo Ribalda Delgado 	return 0;
1453a5f276f1SRicardo Ribalda Delgado }
1454a5f276f1SRicardo Ribalda Delgado 
1455ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port,
1456ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816)
1457ad8c0eaaSNicolas Ferre {
1458ad8c0eaaSNicolas Ferre 	unsigned long flags;
1459ad8c0eaaSNicolas Ferre 	struct serial_iso7816 aux;
1460ad8c0eaaSNicolas Ferre 
1461ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
146279c5966cSJohan Hovold 		return -ENOTTY;
1463ad8c0eaaSNicolas Ferre 
1464ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1465ad8c0eaaSNicolas Ferre 	aux = port->iso7816;
1466ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1467ad8c0eaaSNicolas Ferre 
1468ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816, &aux, sizeof(aux)))
1469ad8c0eaaSNicolas Ferre 		return -EFAULT;
1470ad8c0eaaSNicolas Ferre 
1471ad8c0eaaSNicolas Ferre 	return 0;
1472ad8c0eaaSNicolas Ferre }
1473ad8c0eaaSNicolas Ferre 
1474ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port,
1475ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816_user)
1476ad8c0eaaSNicolas Ferre {
1477ad8c0eaaSNicolas Ferre 	struct serial_iso7816 iso7816;
1478ad8c0eaaSNicolas Ferre 	int i, ret;
1479ad8c0eaaSNicolas Ferre 	unsigned long flags;
1480ad8c0eaaSNicolas Ferre 
1481ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
148279c5966cSJohan Hovold 		return -ENOTTY;
1483ad8c0eaaSNicolas Ferre 
1484ad8c0eaaSNicolas Ferre 	if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1485ad8c0eaaSNicolas Ferre 		return -EFAULT;
1486ad8c0eaaSNicolas Ferre 
1487ad8c0eaaSNicolas Ferre 	/*
1488ad8c0eaaSNicolas Ferre 	 * There are 5 words reserved for future use. Check that userspace
1489ad8c0eaaSNicolas Ferre 	 * doesn't put stuff in there to prevent breakages in the future.
1490ad8c0eaaSNicolas Ferre 	 */
1491eff37b5eSIlpo Järvinen 	for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++)
1492ad8c0eaaSNicolas Ferre 		if (iso7816.reserved[i])
1493ad8c0eaaSNicolas Ferre 			return -EINVAL;
1494ad8c0eaaSNicolas Ferre 
1495ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1496ad8c0eaaSNicolas Ferre 	ret = port->iso7816_config(port, &iso7816);
1497ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1498ad8c0eaaSNicolas Ferre 	if (ret)
1499ad8c0eaaSNicolas Ferre 		return ret;
1500ad8c0eaaSNicolas Ferre 
1501ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1502ad8c0eaaSNicolas Ferre 		return -EFAULT;
1503ad8c0eaaSNicolas Ferre 
1504ad8c0eaaSNicolas Ferre 	return 0;
1505ad8c0eaaSNicolas Ferre }
1506ad8c0eaaSNicolas Ferre 
1507ab4382d2SGreg Kroah-Hartman /*
1508ab4382d2SGreg Kroah-Hartman  * Called via sys_ioctl.  We can use spin_lock_irq() here.
1509ab4382d2SGreg Kroah-Hartman  */
1510ab4382d2SGreg Kroah-Hartman static int
15114047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1512ab4382d2SGreg Kroah-Hartman {
1513ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1514ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
15154047b371SPeter Hurley 	struct uart_port *uport;
1516ab4382d2SGreg Kroah-Hartman 	void __user *uarg = (void __user *)arg;
1517ab4382d2SGreg Kroah-Hartman 	int ret = -ENOIOCTLCMD;
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 
1520ab4382d2SGreg Kroah-Hartman 	/*
1521ab4382d2SGreg Kroah-Hartman 	 * These ioctls don't rely on the hardware to be present.
1522ab4382d2SGreg Kroah-Hartman 	 */
1523ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1524ab4382d2SGreg Kroah-Hartman 	case TIOCSERCONFIG:
15257c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
1526ab4382d2SGreg Kroah-Hartman 		ret = uart_do_autoconfig(tty, state);
15277c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1528ab4382d2SGreg Kroah-Hartman 		break;
1529ab4382d2SGreg Kroah-Hartman 	}
1530ab4382d2SGreg Kroah-Hartman 
1531ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1532ab4382d2SGreg Kroah-Hartman 		goto out;
1533ab4382d2SGreg Kroah-Hartman 
153418900ca6SPeter Hurley 	if (tty_io_error(tty)) {
1535ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1536ab4382d2SGreg Kroah-Hartman 		goto out;
1537ab4382d2SGreg Kroah-Hartman 	}
1538ab4382d2SGreg Kroah-Hartman 
1539ab4382d2SGreg Kroah-Hartman 	/*
1540ab4382d2SGreg Kroah-Hartman 	 * The following should only be used when hardware is present.
1541ab4382d2SGreg Kroah-Hartman 	 */
1542ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1543ab4382d2SGreg Kroah-Hartman 	case TIOCMIWAIT:
1544ab4382d2SGreg Kroah-Hartman 		ret = uart_wait_modem_status(state, arg);
1545ab4382d2SGreg Kroah-Hartman 		break;
1546ab4382d2SGreg Kroah-Hartman 	}
1547ab4382d2SGreg Kroah-Hartman 
1548ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1549ab4382d2SGreg Kroah-Hartman 		goto out;
1550ab4382d2SGreg Kroah-Hartman 
1551ae50bb27SIlpo Järvinen 	/* rs485_config requires more locking than others */
1552ae50bb27SIlpo Järvinen 	if (cmd == TIOCGRS485)
1553ae50bb27SIlpo Järvinen 		down_write(&tty->termios_rwsem);
1554ae50bb27SIlpo Järvinen 
1555ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
15564047b371SPeter Hurley 	uport = uart_port_check(state);
1557ab4382d2SGreg Kroah-Hartman 
15584047b371SPeter Hurley 	if (!uport || tty_io_error(tty)) {
1559ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1560ab4382d2SGreg Kroah-Hartman 		goto out_up;
1561ab4382d2SGreg Kroah-Hartman 	}
1562ab4382d2SGreg Kroah-Hartman 
1563ab4382d2SGreg Kroah-Hartman 	/*
1564ab4382d2SGreg Kroah-Hartman 	 * All these rely on hardware being present and need to be
1565ab4382d2SGreg Kroah-Hartman 	 * protected against the tty being hung up.
1566ab4382d2SGreg Kroah-Hartman 	 */
1567a9c20a9cSRicardo Ribalda Delgado 
1568ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1569a9c20a9cSRicardo Ribalda Delgado 	case TIOCSERGETLSR: /* Get line status register */
1570a9c20a9cSRicardo Ribalda Delgado 		ret = uart_get_lsr_info(tty, state, uarg);
1571a9c20a9cSRicardo Ribalda Delgado 		break;
1572a9c20a9cSRicardo Ribalda Delgado 
1573a5f276f1SRicardo Ribalda Delgado 	case TIOCGRS485:
15744047b371SPeter Hurley 		ret = uart_get_rs485_config(uport, uarg);
1575a5f276f1SRicardo Ribalda Delgado 		break;
1576a5f276f1SRicardo Ribalda Delgado 
1577a5f276f1SRicardo Ribalda Delgado 	case TIOCSRS485:
1578ae50bb27SIlpo Järvinen 		ret = uart_set_rs485_config(tty, uport, uarg);
1579a5f276f1SRicardo Ribalda Delgado 		break;
1580ad8c0eaaSNicolas Ferre 
1581ad8c0eaaSNicolas Ferre 	case TIOCSISO7816:
1582ad8c0eaaSNicolas Ferre 		ret = uart_set_iso7816_config(state->uart_port, uarg);
1583ad8c0eaaSNicolas Ferre 		break;
1584ad8c0eaaSNicolas Ferre 
1585ad8c0eaaSNicolas Ferre 	case TIOCGISO7816:
1586ad8c0eaaSNicolas Ferre 		ret = uart_get_iso7816_config(state->uart_port, uarg);
1587ad8c0eaaSNicolas Ferre 		break;
15884047b371SPeter Hurley 	default:
1589ab4382d2SGreg Kroah-Hartman 		if (uport->ops->ioctl)
1590ab4382d2SGreg Kroah-Hartman 			ret = uport->ops->ioctl(uport, cmd, arg);
1591ab4382d2SGreg Kroah-Hartman 		break;
1592ab4382d2SGreg Kroah-Hartman 	}
1593ab4382d2SGreg Kroah-Hartman out_up:
1594ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1595ae50bb27SIlpo Järvinen 	if (cmd == TIOCGRS485)
1596ae50bb27SIlpo Järvinen 		up_write(&tty->termios_rwsem);
1597ab4382d2SGreg Kroah-Hartman out:
1598ab4382d2SGreg Kroah-Hartman 	return ret;
1599ab4382d2SGreg Kroah-Hartman }
1600ab4382d2SGreg Kroah-Hartman 
1601ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty)
1602ab4382d2SGreg Kroah-Hartman {
1603ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
16044047b371SPeter Hurley 	struct uart_port *uport;
16052f70e49eSAlexey Kardashevskiy 	struct tty_port *port = &state->port;
16062f70e49eSAlexey Kardashevskiy 
16072f70e49eSAlexey Kardashevskiy 	if (!tty_port_initialized(port))
16082f70e49eSAlexey Kardashevskiy 		return;
1609ab4382d2SGreg Kroah-Hartman 
1610db1b9dfcSPeter Hurley 	mutex_lock(&state->port.mutex);
16114047b371SPeter Hurley 	uport = uart_port_check(state);
16124047b371SPeter Hurley 	if (uport && uport->ops->set_ldisc)
1613732a84a0SPeter Hurley 		uport->ops->set_ldisc(uport, &tty->termios);
1614db1b9dfcSPeter Hurley 	mutex_unlock(&state->port.mutex);
1615db1b9dfcSPeter Hurley }
1616ab4382d2SGreg Kroah-Hartman 
1617ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty,
1618a8c11c15SIlpo Järvinen 			     const struct ktermios *old_termios)
1619ab4382d2SGreg Kroah-Hartman {
1620ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
16214047b371SPeter Hurley 	struct uart_port *uport;
1622adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
16232cbacafdSRussell King 	unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
16242cbacafdSRussell King 	bool sw_changed = false;
1625ab4382d2SGreg Kroah-Hartman 
16264047b371SPeter Hurley 	mutex_lock(&state->port.mutex);
16274047b371SPeter Hurley 	uport = uart_port_check(state);
16284047b371SPeter Hurley 	if (!uport)
16294047b371SPeter Hurley 		goto out;
16304047b371SPeter Hurley 
16312cbacafdSRussell King 	/*
16322cbacafdSRussell King 	 * Drivers doing software flow control also need to know
16332cbacafdSRussell King 	 * about changes to these input settings.
16342cbacafdSRussell King 	 */
16352cbacafdSRussell King 	if (uport->flags & UPF_SOFT_FLOW) {
16362cbacafdSRussell King 		iflag_mask |= IXANY|IXON|IXOFF;
16372cbacafdSRussell King 		sw_changed =
16382cbacafdSRussell King 		   tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
16392cbacafdSRussell King 		   tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
16402cbacafdSRussell King 	}
1641ab4382d2SGreg Kroah-Hartman 
1642ab4382d2SGreg Kroah-Hartman 	/*
1643ab4382d2SGreg Kroah-Hartman 	 * These are the bits that are used to setup various
1644ab4382d2SGreg Kroah-Hartman 	 * flags in the low level driver. We can ignore the Bfoo
1645ab4382d2SGreg Kroah-Hartman 	 * bits in c_cflag; c_[io]speed will always be set
1646ab4382d2SGreg Kroah-Hartman 	 * appropriately by set_termios() in tty_ioctl.c
1647ab4382d2SGreg Kroah-Hartman 	 */
1648ab4382d2SGreg Kroah-Hartman 	if ((cflag ^ old_termios->c_cflag) == 0 &&
1649adc8d746SAlan Cox 	    tty->termios.c_ospeed == old_termios->c_ospeed &&
1650adc8d746SAlan Cox 	    tty->termios.c_ispeed == old_termios->c_ispeed &&
16512cbacafdSRussell King 	    ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
16522cbacafdSRussell King 	    !sw_changed) {
16534047b371SPeter Hurley 		goto out;
1654ab4382d2SGreg Kroah-Hartman 	}
1655ab4382d2SGreg Kroah-Hartman 
1656826736a6SIlpo Järvinen 	uart_change_line_settings(tty, state, old_termios);
1657c7a6b9e4SHariprasad Kelam 	/* reload cflag from termios; port driver may have overridden flags */
1658c18b55fdSPeter Hurley 	cflag = tty->termios.c_cflag;
1659ab4382d2SGreg Kroah-Hartman 
1660ab4382d2SGreg Kroah-Hartman 	/* Handle transition to B0 status */
1661807fccf7SIlpo Järvinen 	if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0))
1662dec94e70SRussell King 		uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1663ab4382d2SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
1664807fccf7SIlpo Järvinen 	else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) {
1665ab4382d2SGreg Kroah-Hartman 		unsigned int mask = TIOCM_DTR;
16664ed71addSTamseel Shams 
166797ef38b8SPeter Hurley 		if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1668ab4382d2SGreg Kroah-Hartman 			mask |= TIOCM_RTS;
1669dec94e70SRussell King 		uart_set_mctrl(uport, mask);
1670ab4382d2SGreg Kroah-Hartman 	}
16714047b371SPeter Hurley out:
16724047b371SPeter Hurley 	mutex_unlock(&state->port.mutex);
1673ab4382d2SGreg Kroah-Hartman }
1674ab4382d2SGreg Kroah-Hartman 
1675ab4382d2SGreg Kroah-Hartman /*
1676ef4f527cSKevin Cernekee  * Calls to uart_close() are serialised via the tty_lock in
1677ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_release()
1678ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1679ab4382d2SGreg Kroah-Hartman  */
1680ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp)
1681ab4382d2SGreg Kroah-Hartman {
1682ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1683ab4382d2SGreg Kroah-Hartman 
168491b32f54SPeter Hurley 	if (!state) {
168591b32f54SPeter Hurley 		struct uart_driver *drv = tty->driver->driver_state;
16869356335fSColin Ian King 		struct tty_port *port;
168791b32f54SPeter Hurley 
168891b32f54SPeter Hurley 		state = drv->state + tty->index;
168991b32f54SPeter Hurley 		port = &state->port;
169091b32f54SPeter Hurley 		spin_lock_irq(&port->lock);
169191b32f54SPeter Hurley 		--port->count;
169291b32f54SPeter Hurley 		spin_unlock_irq(&port->lock);
1693ab4382d2SGreg Kroah-Hartman 		return;
169491b32f54SPeter Hurley 	}
1695ab4382d2SGreg Kroah-Hartman 
169639b3d892SPeter Hurley 	pr_debug("uart_close(%d) called\n", tty->index);
1697ab4382d2SGreg Kroah-Hartman 
1698761ed4a9SRob Herring 	tty_port_close(tty->port, tty, filp);
1699761ed4a9SRob Herring }
1700ab4382d2SGreg Kroah-Hartman 
1701761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port)
1702761ed4a9SRob Herring {
1703761ed4a9SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1704761ed4a9SRob Herring 	struct uart_port *uport = uart_port_check(state);
170500de977fSJohan Hovold 	char *buf;
1706af224ca2SPeter Hurley 
1707ab4382d2SGreg Kroah-Hartman 	/*
1708ab4382d2SGreg Kroah-Hartman 	 * At this point, we stop accepting input.  To do this, we
1709ab4382d2SGreg Kroah-Hartman 	 * disable the receive line status interrupts.
1710ab4382d2SGreg Kroah-Hartman 	 */
1711a5a2b130SAndy Shevchenko 	if (WARN(!uport, "detached port still initialized!\n"))
1712a5a2b130SAndy Shevchenko 		return;
1713761ed4a9SRob Herring 
1714a5a2b130SAndy Shevchenko 	spin_lock_irq(&uport->lock);
1715ab4382d2SGreg Kroah-Hartman 	uport->ops->stop_rx(uport);
17166fb98fb3SPeter Hurley 	spin_unlock_irq(&uport->lock);
1717761ed4a9SRob Herring 
1718761ed4a9SRob Herring 	uart_port_shutdown(port);
1719761ed4a9SRob Herring 
1720ab4382d2SGreg Kroah-Hartman 	/*
1721761ed4a9SRob Herring 	 * It's possible for shutdown to be called after suspend if we get
1722761ed4a9SRob Herring 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
1723761ed4a9SRob Herring 	 * we don't try to resume a port that has been shutdown.
1724ab4382d2SGreg Kroah-Hartman 	 */
172575b20a2aSIlpo Järvinen 	tty_port_set_suspended(port, false);
1726ab4382d2SGreg Kroah-Hartman 
172700de977fSJohan Hovold 	/*
172800de977fSJohan Hovold 	 * Free the transmit buffer.
172900de977fSJohan Hovold 	 */
173000de977fSJohan Hovold 	spin_lock_irq(&uport->lock);
173100de977fSJohan Hovold 	buf = state->xmit.buf;
173200de977fSJohan Hovold 	state->xmit.buf = NULL;
173300de977fSJohan Hovold 	spin_unlock_irq(&uport->lock);
1734ab4382d2SGreg Kroah-Hartman 
173500de977fSJohan Hovold 	free_page((unsigned long)buf);
173600de977fSJohan Hovold 
173700de977fSJohan Hovold 	uart_change_pm(state, UART_PM_STATE_OFF);
1738ab4382d2SGreg Kroah-Hartman }
1739ab4382d2SGreg Kroah-Hartman 
17401f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1741ab4382d2SGreg Kroah-Hartman {
17421f33a51dSJiri Slaby 	struct uart_state *state = tty->driver_data;
17439ed19428SPeter Hurley 	struct uart_port *port;
1744f9008285SIlpo Järvinen 	unsigned long char_time, expire, fifo_timeout;
1745ab4382d2SGreg Kroah-Hartman 
17469ed19428SPeter Hurley 	port = uart_port_ref(state);
1747ef510beaSAndy Shevchenko 	if (!port)
1748ef510beaSAndy Shevchenko 		return;
1749ef510beaSAndy Shevchenko 
1750ef510beaSAndy Shevchenko 	if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
17519ed19428SPeter Hurley 		uart_port_deref(port);
1752ab4382d2SGreg Kroah-Hartman 		return;
17539ed19428SPeter Hurley 	}
1754ab4382d2SGreg Kroah-Hartman 
1755ab4382d2SGreg Kroah-Hartman 	/*
1756ab4382d2SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1757ab4382d2SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1758ab4382d2SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1759ab4382d2SGreg Kroah-Hartman 	 *
1760ab4382d2SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1761ab4382d2SGreg Kroah-Hartman 	 * the NIST-PCTS.
1762ab4382d2SGreg Kroah-Hartman 	 */
176331f6bd7fSIlpo Järvinen 	char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
176431f6bd7fSIlpo Järvinen 
1765ab4382d2SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1766ab4382d2SGreg Kroah-Hartman 		char_time = timeout;
1767ab4382d2SGreg Kroah-Hartman 
1768b0e0bd9dSTomasz Moń 	if (!uart_cts_enabled(port)) {
1769ab4382d2SGreg Kroah-Hartman 		/*
1770ab4382d2SGreg Kroah-Hartman 		 * If the transmitter hasn't cleared in twice the approximate
1771ab4382d2SGreg Kroah-Hartman 		 * amount of time to send the entire FIFO, it probably won't
1772ab4382d2SGreg Kroah-Hartman 		 * ever clear.  This assumes the UART isn't doing flow
1773ab4382d2SGreg Kroah-Hartman 		 * control, which is currently the case.  Hence, if it ever
1774f9008285SIlpo Järvinen 		 * takes longer than FIFO timeout, this is probably due to a
1775ab4382d2SGreg Kroah-Hartman 		 * UART bug of some kind.  So, we clamp the timeout parameter at
1776f9008285SIlpo Järvinen 		 * 2 * FIFO timeout.
1777ab4382d2SGreg Kroah-Hartman 		 */
1778f9008285SIlpo Järvinen 		fifo_timeout = uart_fifo_timeout(port);
1779f9008285SIlpo Järvinen 		if (timeout == 0 || timeout > 2 * fifo_timeout)
1780f9008285SIlpo Järvinen 			timeout = 2 * fifo_timeout;
1781b0e0bd9dSTomasz Moń 	}
1782ab4382d2SGreg Kroah-Hartman 
1783ab4382d2SGreg Kroah-Hartman 	expire = jiffies + timeout;
1784ab4382d2SGreg Kroah-Hartman 
1785ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1786ab4382d2SGreg Kroah-Hartman 		port->line, jiffies, expire);
1787ab4382d2SGreg Kroah-Hartman 
1788ab4382d2SGreg Kroah-Hartman 	/*
1789ab4382d2SGreg Kroah-Hartman 	 * Check whether the transmitter is empty every 'char_time'.
1790ab4382d2SGreg Kroah-Hartman 	 * 'timeout' / 'expire' give us the maximum amount of time
1791ab4382d2SGreg Kroah-Hartman 	 * we wait.
1792ab4382d2SGreg Kroah-Hartman 	 */
1793ab4382d2SGreg Kroah-Hartman 	while (!port->ops->tx_empty(port)) {
1794ab4382d2SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1795ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current))
1796ab4382d2SGreg Kroah-Hartman 			break;
1797b0e0bd9dSTomasz Moń 		if (timeout && time_after(jiffies, expire))
1798ab4382d2SGreg Kroah-Hartman 			break;
1799ab4382d2SGreg Kroah-Hartman 	}
18009ed19428SPeter Hurley 	uart_port_deref(port);
1801ab4382d2SGreg Kroah-Hartman }
1802ab4382d2SGreg Kroah-Hartman 
1803ab4382d2SGreg Kroah-Hartman /*
1804ef4f527cSKevin Cernekee  * Calls to uart_hangup() are serialised by the tty_lock in
1805ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1806ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1807ab4382d2SGreg Kroah-Hartman  */
1808ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty)
1809ab4382d2SGreg Kroah-Hartman {
1810ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1811ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1812af224ca2SPeter Hurley 	struct uart_port *uport;
1813ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1814ab4382d2SGreg Kroah-Hartman 
181539b3d892SPeter Hurley 	pr_debug("uart_hangup(%d)\n", tty->index);
1816ab4382d2SGreg Kroah-Hartman 
1817ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1818af224ca2SPeter Hurley 	uport = uart_port_check(state);
1819af224ca2SPeter Hurley 	WARN(!uport, "hangup of detached port!\n");
1820af224ca2SPeter Hurley 
1821807c8d81SPeter Hurley 	if (tty_port_active(port)) {
1822ab4382d2SGreg Kroah-Hartman 		uart_flush_buffer(tty);
1823ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1824ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
1825ab4382d2SGreg Kroah-Hartman 		port->count = 0;
1826ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
18279b5aa549SIlpo Järvinen 		tty_port_set_active(port, false);
1828ab4382d2SGreg Kroah-Hartman 		tty_port_tty_set(port, NULL);
1829af224ca2SPeter Hurley 		if (uport && !uart_console(uport))
1830bf903c0cSGeert Uytterhoeven 			uart_change_pm(state, UART_PM_STATE_OFF);
1831ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
1832ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->delta_msr_wait);
1833ab4382d2SGreg Kroah-Hartman 	}
1834ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1835ab4382d2SGreg Kroah-Hartman }
1836ab4382d2SGreg Kroah-Hartman 
1837af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */
18380b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port)
18390b1db830SJiri Slaby {
1840b922e19dSJiri Slaby 	struct uart_state *state = container_of(port, struct uart_state, port);
18414047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1842b922e19dSJiri Slaby 
1843b922e19dSJiri Slaby 	/*
1844b922e19dSJiri Slaby 	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1845b922e19dSJiri Slaby 	 * the irq here so the queue might never be woken up.  Note
1846b922e19dSJiri Slaby 	 * that we won't end up waiting on delta_msr_wait again since
1847b922e19dSJiri Slaby 	 * any outstanding file descriptors should be pointing at
1848b922e19dSJiri Slaby 	 * hung_up_tty_fops now.
1849b922e19dSJiri Slaby 	 */
1850b922e19dSJiri Slaby 	wake_up_interruptible(&port->delta_msr_wait);
1851b922e19dSJiri Slaby 
18522765852eSJiri Slaby 	if (uport) {
18532765852eSJiri Slaby 		/* Free the IRQ and disable the port. */
1854b922e19dSJiri Slaby 		uport->ops->shutdown(uport);
1855b922e19dSJiri Slaby 
18562765852eSJiri Slaby 		/* Ensure that the IRQ handler isn't running on another CPU. */
1857b922e19dSJiri Slaby 		synchronize_irq(uport->irq);
18580b1db830SJiri Slaby 	}
18592765852eSJiri Slaby }
18600b1db830SJiri Slaby 
1861b300fb26SIlpo Järvinen static bool uart_carrier_raised(struct tty_port *port)
1862ab4382d2SGreg Kroah-Hartman {
1863ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
18649ed19428SPeter Hurley 	struct uart_port *uport;
1865ab4382d2SGreg Kroah-Hartman 	int mctrl;
18669ed19428SPeter Hurley 
18679ed19428SPeter Hurley 	uport = uart_port_ref(state);
18689ed19428SPeter Hurley 	/*
18699ed19428SPeter Hurley 	 * Should never observe uport == NULL since checks for hangup should
18709ed19428SPeter Hurley 	 * abort the tty_port_block_til_ready() loop before checking for carrier
18719ed19428SPeter Hurley 	 * raised -- but report carrier raised if it does anyway so open will
18729ed19428SPeter Hurley 	 * continue and not sleep
18739ed19428SPeter Hurley 	 */
18749ed19428SPeter Hurley 	if (WARN_ON(!uport))
1875b300fb26SIlpo Järvinen 		return true;
1876ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
18771fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1878ab4382d2SGreg Kroah-Hartman 	mctrl = uport->ops->get_mctrl(uport);
1879ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
18809ed19428SPeter Hurley 	uart_port_deref(uport);
1881b300fb26SIlpo Järvinen 
1882b300fb26SIlpo Järvinen 	return mctrl & TIOCM_CAR;
1883ab4382d2SGreg Kroah-Hartman }
1884ab4382d2SGreg Kroah-Hartman 
18855701cb8bSIlpo Järvinen static void uart_dtr_rts(struct tty_port *port, bool active)
1886ab4382d2SGreg Kroah-Hartman {
1887ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
18889ed19428SPeter Hurley 	struct uart_port *uport;
18899ed19428SPeter Hurley 
18909ed19428SPeter Hurley 	uport = uart_port_ref(state);
18919ed19428SPeter Hurley 	if (!uport)
18929ed19428SPeter Hurley 		return;
18935701cb8bSIlpo Järvinen 	uart_port_dtr_rts(uport, active);
18949ed19428SPeter Hurley 	uart_port_deref(uport);
1895ab4382d2SGreg Kroah-Hartman }
1896ab4382d2SGreg Kroah-Hartman 
18974cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
18984cdd17baSJiri Slaby {
18994cdd17baSJiri Slaby 	struct uart_driver *drv = driver->driver_state;
19004cdd17baSJiri Slaby 	struct uart_state *state = drv->state + tty->index;
19014cdd17baSJiri Slaby 
19024cdd17baSJiri Slaby 	tty->driver_data = state;
19034cdd17baSJiri Slaby 
19044cdd17baSJiri Slaby 	return tty_standard_install(driver, tty);
19054cdd17baSJiri Slaby }
19064cdd17baSJiri Slaby 
1907ab4382d2SGreg Kroah-Hartman /*
1908ef4f527cSKevin Cernekee  * Calls to uart_open are serialised by the tty_lock in
1909ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_open()
1910ab4382d2SGreg Kroah-Hartman  * Note that if this fails, then uart_close() _will_ be called.
1911ab4382d2SGreg Kroah-Hartman  *
1912ab4382d2SGreg Kroah-Hartman  * In time, we want to scrap the "opening nonpresent ports"
1913ab4382d2SGreg Kroah-Hartman  * behaviour and implement an alternative way for setserial
1914ab4382d2SGreg Kroah-Hartman  * to set base addresses/ports/types.  This will allow us to
1915ab4382d2SGreg Kroah-Hartman  * get rid of a certain amount of extra tests.
1916ab4382d2SGreg Kroah-Hartman  */
1917ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp)
1918ab4382d2SGreg Kroah-Hartman {
19194cdd17baSJiri Slaby 	struct uart_state *state = tty->driver_data;
19204cdd17baSJiri Slaby 	int retval;
1921b3b57646SRob Herring 
1922b3b57646SRob Herring 	retval = tty_port_open(&state->port, tty, filp);
1923b3b57646SRob Herring 	if (retval > 0)
1924b3b57646SRob Herring 		retval = 0;
1925b3b57646SRob Herring 
1926b3b57646SRob Herring 	return retval;
1927b3b57646SRob Herring }
1928b3b57646SRob Herring 
1929b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1930b3b57646SRob Herring {
1931b3b57646SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1932b3b57646SRob Herring 	struct uart_port *uport;
193313b18d35SSerge Semin 	int ret;
1934b3b57646SRob Herring 
1935b3b57646SRob Herring 	uport = uart_port_check(state);
1936b3b57646SRob Herring 	if (!uport || uport->flags & UPF_DEAD)
1937b3b57646SRob Herring 		return -ENXIO;
1938b3b57646SRob Herring 
1939ab4382d2SGreg Kroah-Hartman 	/*
1940ab4382d2SGreg Kroah-Hartman 	 * Start up the serial port.
1941ab4382d2SGreg Kroah-Hartman 	 */
1942dcd794c6SIlpo Järvinen 	ret = uart_startup(tty, state, false);
194313b18d35SSerge Semin 	if (ret > 0)
19449b5aa549SIlpo Järvinen 		tty_port_set_active(port, true);
194513b18d35SSerge Semin 
194613b18d35SSerge Semin 	return ret;
1947ab4382d2SGreg Kroah-Hartman }
1948ab4382d2SGreg Kroah-Hartman 
1949ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port)
1950ab4382d2SGreg Kroah-Hartman {
1951ab4382d2SGreg Kroah-Hartman 	const char *str = NULL;
1952ab4382d2SGreg Kroah-Hartman 
1953ab4382d2SGreg Kroah-Hartman 	if (port->ops->type)
1954ab4382d2SGreg Kroah-Hartman 		str = port->ops->type(port);
1955ab4382d2SGreg Kroah-Hartman 
1956ab4382d2SGreg Kroah-Hartman 	if (!str)
1957ab4382d2SGreg Kroah-Hartman 		str = "unknown";
1958ab4382d2SGreg Kroah-Hartman 
1959ab4382d2SGreg Kroah-Hartman 	return str;
1960ab4382d2SGreg Kroah-Hartman }
1961ab4382d2SGreg Kroah-Hartman 
1962ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
1963ab4382d2SGreg Kroah-Hartman 
1964ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1965ab4382d2SGreg Kroah-Hartman {
1966ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + i;
1967ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
19686f538fe3SLinus Walleij 	enum uart_pm_state pm_state;
19694047b371SPeter Hurley 	struct uart_port *uport;
1970ab4382d2SGreg Kroah-Hartman 	char stat_buf[32];
1971ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1972ab4382d2SGreg Kroah-Hartman 	int mmio;
1973ab4382d2SGreg Kroah-Hartman 
19744047b371SPeter Hurley 	mutex_lock(&port->mutex);
19754047b371SPeter Hurley 	uport = uart_port_check(state);
1976ab4382d2SGreg Kroah-Hartman 	if (!uport)
19774047b371SPeter Hurley 		goto out;
1978ab4382d2SGreg Kroah-Hartman 
1979ab4382d2SGreg Kroah-Hartman 	mmio = uport->iotype >= UPIO_MEM;
1980ab4382d2SGreg Kroah-Hartman 	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1981ab4382d2SGreg Kroah-Hartman 			uport->line, uart_type(uport),
1982ab4382d2SGreg Kroah-Hartman 			mmio ? "mmio:0x" : "port:",
1983ab4382d2SGreg Kroah-Hartman 			mmio ? (unsigned long long)uport->mapbase
1984ab4382d2SGreg Kroah-Hartman 			     : (unsigned long long)uport->iobase,
1985ab4382d2SGreg Kroah-Hartman 			uport->irq);
1986ab4382d2SGreg Kroah-Hartman 
1987ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN) {
1988ab4382d2SGreg Kroah-Hartman 		seq_putc(m, '\n');
19894047b371SPeter Hurley 		goto out;
1990ab4382d2SGreg Kroah-Hartman 	}
1991ab4382d2SGreg Kroah-Hartman 
1992ab4382d2SGreg Kroah-Hartman 	if (capable(CAP_SYS_ADMIN)) {
1993ab4382d2SGreg Kroah-Hartman 		pm_state = state->pm_state;
19946f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
19956f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
1996ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1997ab4382d2SGreg Kroah-Hartman 		status = uport->ops->get_mctrl(uport);
1998ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
19996f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
2000ab4382d2SGreg Kroah-Hartman 			uart_change_pm(state, pm_state);
2001ab4382d2SGreg Kroah-Hartman 
2002ab4382d2SGreg Kroah-Hartman 		seq_printf(m, " tx:%d rx:%d",
2003ab4382d2SGreg Kroah-Hartman 				uport->icount.tx, uport->icount.rx);
2004ab4382d2SGreg Kroah-Hartman 		if (uport->icount.frame)
2005968af298SPeter Hurley 			seq_printf(m, " fe:%d",	uport->icount.frame);
2006ab4382d2SGreg Kroah-Hartman 		if (uport->icount.parity)
2007968af298SPeter Hurley 			seq_printf(m, " pe:%d",	uport->icount.parity);
2008ab4382d2SGreg Kroah-Hartman 		if (uport->icount.brk)
2009968af298SPeter Hurley 			seq_printf(m, " brk:%d", uport->icount.brk);
2010ab4382d2SGreg Kroah-Hartman 		if (uport->icount.overrun)
2011968af298SPeter Hurley 			seq_printf(m, " oe:%d", uport->icount.overrun);
20124f794097SJeremy Kerr 		if (uport->icount.buf_overrun)
20134f794097SJeremy Kerr 			seq_printf(m, " bo:%d", uport->icount.buf_overrun);
2014ab4382d2SGreg Kroah-Hartman 
2015ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \
2016ab4382d2SGreg Kroah-Hartman 	if (uport->mctrl & (bit)) \
2017ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
2018ab4382d2SGreg Kroah-Hartman 			strlen(stat_buf) - 2)
2019ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \
2020ab4382d2SGreg Kroah-Hartman 	if (status & (bit)) \
2021ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
2022ab4382d2SGreg Kroah-Hartman 		       strlen(stat_buf) - 2)
2023ab4382d2SGreg Kroah-Hartman 
2024ab4382d2SGreg Kroah-Hartman 		stat_buf[0] = '\0';
2025ab4382d2SGreg Kroah-Hartman 		stat_buf[1] = '\0';
2026ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_RTS, "|RTS");
2027ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CTS, "|CTS");
2028ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_DTR, "|DTR");
2029ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_DSR, "|DSR");
2030ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CAR, "|CD");
2031ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_RNG, "|RI");
2032ab4382d2SGreg Kroah-Hartman 		if (stat_buf[0])
2033ab4382d2SGreg Kroah-Hartman 			stat_buf[0] = ' ';
2034ab4382d2SGreg Kroah-Hartman 
2035ab4382d2SGreg Kroah-Hartman 		seq_puts(m, stat_buf);
2036ab4382d2SGreg Kroah-Hartman 	}
2037ab4382d2SGreg Kroah-Hartman 	seq_putc(m, '\n');
2038ab4382d2SGreg Kroah-Hartman #undef STATBIT
2039ab4382d2SGreg Kroah-Hartman #undef INFOBIT
20404047b371SPeter Hurley out:
20414047b371SPeter Hurley 	mutex_unlock(&port->mutex);
2042ab4382d2SGreg Kroah-Hartman }
2043ab4382d2SGreg Kroah-Hartman 
2044ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v)
2045ab4382d2SGreg Kroah-Hartman {
2046ab4382d2SGreg Kroah-Hartman 	struct tty_driver *ttydrv = m->private;
2047ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = ttydrv->driver_state;
2048ab4382d2SGreg Kroah-Hartman 	int i;
2049ab4382d2SGreg Kroah-Hartman 
2050968af298SPeter Hurley 	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2051ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++)
2052ab4382d2SGreg Kroah-Hartman 		uart_line_info(m, drv, i);
2053ab4382d2SGreg Kroah-Hartman 	return 0;
2054ab4382d2SGreg Kroah-Hartman }
2055ab4382d2SGreg Kroah-Hartman #endif
2056ab4382d2SGreg Kroah-Hartman 
2057e0830dbfSJohan Hovold static void uart_port_spin_lock_init(struct uart_port *port)
2058f743061aSAndy Shevchenko {
2059f743061aSAndy Shevchenko 	spin_lock_init(&port->lock);
2060f743061aSAndy Shevchenko 	lockdep_set_class(&port->lock, &port_lock_key);
2061f743061aSAndy Shevchenko }
2062f743061aSAndy Shevchenko 
2063ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
20641cfe42b7SPeter Hurley /**
2065ab4382d2SGreg Kroah-Hartman  * uart_console_write - write a console message to a serial port
2066ab4382d2SGreg Kroah-Hartman  * @port: the port to write the message
2067ab4382d2SGreg Kroah-Hartman  * @s: array of characters
2068ab4382d2SGreg Kroah-Hartman  * @count: number of characters in string to write
206910afbe34SPeter Hurley  * @putchar: function to write character to port
2070ab4382d2SGreg Kroah-Hartman  */
2071ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s,
2072ab4382d2SGreg Kroah-Hartman 			unsigned int count,
20733f8bab17SJiri Slaby 			void (*putchar)(struct uart_port *, unsigned char))
2074ab4382d2SGreg Kroah-Hartman {
2075ab4382d2SGreg Kroah-Hartman 	unsigned int i;
2076ab4382d2SGreg Kroah-Hartman 
2077ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < count; i++, s++) {
2078ab4382d2SGreg Kroah-Hartman 		if (*s == '\n')
2079ab4382d2SGreg Kroah-Hartman 			putchar(port, '\r');
2080ab4382d2SGreg Kroah-Hartman 		putchar(port, *s);
2081ab4382d2SGreg Kroah-Hartman 	}
2082ab4382d2SGreg Kroah-Hartman }
2083ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write);
2084ab4382d2SGreg Kroah-Hartman 
20859e5f399fSJiri Slaby /**
20869e5f399fSJiri Slaby  * uart_get_console - get uart port for console
20879e5f399fSJiri Slaby  * @ports: ports to search in
20889e5f399fSJiri Slaby  * @nr: number of @ports
20899e5f399fSJiri Slaby  * @co: console to search for
20909e5f399fSJiri Slaby  * Returns: uart_port for the console @co
20919e5f399fSJiri Slaby  *
20929e5f399fSJiri Slaby  * Check whether an invalid uart number has been specified (as @co->index), and
20939e5f399fSJiri Slaby  * if so, search for the first available port that does have console support.
2094ab4382d2SGreg Kroah-Hartman  */
2095ab4382d2SGreg Kroah-Hartman struct uart_port * __init
2096ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co)
2097ab4382d2SGreg Kroah-Hartman {
2098ab4382d2SGreg Kroah-Hartman 	int idx = co->index;
2099ab4382d2SGreg Kroah-Hartman 
2100ab4382d2SGreg Kroah-Hartman 	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2101ab4382d2SGreg Kroah-Hartman 				     ports[idx].membase == NULL))
2102ab4382d2SGreg Kroah-Hartman 		for (idx = 0; idx < nr; idx++)
2103ab4382d2SGreg Kroah-Hartman 			if (ports[idx].iobase != 0 ||
2104ab4382d2SGreg Kroah-Hartman 			    ports[idx].membase != NULL)
2105ab4382d2SGreg Kroah-Hartman 				break;
2106ab4382d2SGreg Kroah-Hartman 
2107ab4382d2SGreg Kroah-Hartman 	co->index = idx;
2108ab4382d2SGreg Kroah-Hartman 
2109ab4382d2SGreg Kroah-Hartman 	return ports + idx;
2110ab4382d2SGreg Kroah-Hartman }
2111ab4382d2SGreg Kroah-Hartman 
2112ab4382d2SGreg Kroah-Hartman /**
211373abaf87SPeter Hurley  * uart_parse_earlycon - Parse earlycon options
211473abaf87SPeter Hurley  * @p:	     ptr to 2nd field (ie., just beyond '<name>,')
211573abaf87SPeter Hurley  * @iotype:  ptr for decoded iotype (out)
211673abaf87SPeter Hurley  * @addr:    ptr for decoded mapbase/iobase (out)
2117987233b3SJiri Slaby  * @options: ptr for <options> field; %NULL if not present (out)
211873abaf87SPeter Hurley  *
2119987233b3SJiri Slaby  * Decodes earlycon kernel command line parameters of the form:
2120987233b3SJiri Slaby  *  * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2121987233b3SJiri Slaby  *  * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
212273abaf87SPeter Hurley  *
2123987233b3SJiri Slaby  * The optional form:
2124987233b3SJiri Slaby  *  * earlycon=<name>,0x<addr>,<options>
2125987233b3SJiri Slaby  *  * console=<name>,0x<addr>,<options>
2126ff30283aSRandy Dunlap  *
2127987233b3SJiri Slaby  * is also accepted; the returned @iotype will be %UPIO_MEM.
2128ff30283aSRandy Dunlap  *
2129987233b3SJiri Slaby  * Returns: 0 on success or -%EINVAL on failure
213073abaf87SPeter Hurley  */
213146e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
213273abaf87SPeter Hurley 			char **options)
213373abaf87SPeter Hurley {
213473abaf87SPeter Hurley 	if (strncmp(p, "mmio,", 5) == 0) {
213573abaf87SPeter Hurley 		*iotype = UPIO_MEM;
213673abaf87SPeter Hurley 		p += 5;
2137bd94c407SMasahiro Yamada 	} else if (strncmp(p, "mmio16,", 7) == 0) {
2138bd94c407SMasahiro Yamada 		*iotype = UPIO_MEM16;
2139bd94c407SMasahiro Yamada 		p += 7;
214073abaf87SPeter Hurley 	} else if (strncmp(p, "mmio32,", 7) == 0) {
214173abaf87SPeter Hurley 		*iotype = UPIO_MEM32;
214273abaf87SPeter Hurley 		p += 7;
21436e63be3fSNoam Camus 	} else if (strncmp(p, "mmio32be,", 9) == 0) {
21446e63be3fSNoam Camus 		*iotype = UPIO_MEM32BE;
21456e63be3fSNoam Camus 		p += 9;
2146d215d809SMax Filippov 	} else if (strncmp(p, "mmio32native,", 13) == 0) {
2147d215d809SMax Filippov 		*iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2148d215d809SMax Filippov 			UPIO_MEM32BE : UPIO_MEM32;
2149d215d809SMax Filippov 		p += 13;
215073abaf87SPeter Hurley 	} else if (strncmp(p, "io,", 3) == 0) {
215173abaf87SPeter Hurley 		*iotype = UPIO_PORT;
215273abaf87SPeter Hurley 		p += 3;
215373abaf87SPeter Hurley 	} else if (strncmp(p, "0x", 2) == 0) {
215473abaf87SPeter Hurley 		*iotype = UPIO_MEM;
215573abaf87SPeter Hurley 	} else {
215673abaf87SPeter Hurley 		return -EINVAL;
215773abaf87SPeter Hurley 	}
215873abaf87SPeter Hurley 
21598b2303deSAlexander Sverdlin 	/*
21608b2303deSAlexander Sverdlin 	 * Before you replace it with kstrtoull(), think about options separator
21618b2303deSAlexander Sverdlin 	 * (',') it will not tolerate
21628b2303deSAlexander Sverdlin 	 */
21638b2303deSAlexander Sverdlin 	*addr = simple_strtoull(p, NULL, 0);
216473abaf87SPeter Hurley 	p = strchr(p, ',');
216573abaf87SPeter Hurley 	if (p)
216673abaf87SPeter Hurley 		p++;
216773abaf87SPeter Hurley 
216873abaf87SPeter Hurley 	*options = p;
216973abaf87SPeter Hurley 	return 0;
217073abaf87SPeter Hurley }
217173abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon);
217273abaf87SPeter Hurley 
217373abaf87SPeter Hurley /**
217402088ca6SGeert Uytterhoeven  * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2175ab4382d2SGreg Kroah-Hartman  * @options: pointer to option string
2176ab4382d2SGreg Kroah-Hartman  * @baud: pointer to an 'int' variable for the baud rate.
2177ab4382d2SGreg Kroah-Hartman  * @parity: pointer to an 'int' variable for the parity.
2178ab4382d2SGreg Kroah-Hartman  * @bits: pointer to an 'int' variable for the number of data bits.
2179ab4382d2SGreg Kroah-Hartman  * @flow: pointer to an 'int' variable for the flow control character.
2180ab4382d2SGreg Kroah-Hartman  *
2181987233b3SJiri Slaby  * uart_parse_options() decodes a string containing the serial console
2182ab4382d2SGreg Kroah-Hartman  * options. The format of the string is <baud><parity><bits><flow>,
2183ab4382d2SGreg Kroah-Hartman  * eg: 115200n8r
2184ab4382d2SGreg Kroah-Hartman  */
2185ab4382d2SGreg Kroah-Hartman void
21860f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity,
21870f646b63SPaul Cercueil 		   int *bits, int *flow)
2188ab4382d2SGreg Kroah-Hartman {
21890f646b63SPaul Cercueil 	const char *s = options;
2190ab4382d2SGreg Kroah-Hartman 
2191ab4382d2SGreg Kroah-Hartman 	*baud = simple_strtoul(s, NULL, 10);
2192ab4382d2SGreg Kroah-Hartman 	while (*s >= '0' && *s <= '9')
2193ab4382d2SGreg Kroah-Hartman 		s++;
2194ab4382d2SGreg Kroah-Hartman 	if (*s)
2195ab4382d2SGreg Kroah-Hartman 		*parity = *s++;
2196ab4382d2SGreg Kroah-Hartman 	if (*s)
2197ab4382d2SGreg Kroah-Hartman 		*bits = *s++ - '0';
2198ab4382d2SGreg Kroah-Hartman 	if (*s)
2199ab4382d2SGreg Kroah-Hartman 		*flow = *s;
2200ab4382d2SGreg Kroah-Hartman }
2201ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options);
2202ab4382d2SGreg Kroah-Hartman 
2203ab4382d2SGreg Kroah-Hartman /**
2204ab4382d2SGreg Kroah-Hartman  * uart_set_options - setup the serial console parameters
2205ab4382d2SGreg Kroah-Hartman  * @port: pointer to the serial ports uart_port structure
2206ab4382d2SGreg Kroah-Hartman  * @co: console pointer
2207ab4382d2SGreg Kroah-Hartman  * @baud: baud rate
2208ab4382d2SGreg Kroah-Hartman  * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2209ab4382d2SGreg Kroah-Hartman  * @bits: number of data bits
2210ab4382d2SGreg Kroah-Hartman  * @flow: flow control character - 'r' (rts)
22113ef5abd9SJohn Ogness  *
22123ef5abd9SJohn Ogness  * Locking: Caller must hold console_list_lock in order to serialize
22133ef5abd9SJohn Ogness  * early initialization of the serial-console lock.
2214ab4382d2SGreg Kroah-Hartman  */
2215ab4382d2SGreg Kroah-Hartman int
2216ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co,
2217ab4382d2SGreg Kroah-Hartman 		 int baud, int parity, int bits, int flow)
2218ab4382d2SGreg Kroah-Hartman {
2219ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2220ab4382d2SGreg Kroah-Hartman 	static struct ktermios dummy;
2221ab4382d2SGreg Kroah-Hartman 
2222e0830dbfSJohan Hovold 	/*
2223e0830dbfSJohan Hovold 	 * Ensure that the serial-console lock is initialised early.
2224e0830dbfSJohan Hovold 	 *
2225452b9b24SJohn Ogness 	 * Note that the console-registered check is needed because
2226452b9b24SJohn Ogness 	 * kgdboc can call uart_set_options() for an already registered
2227e0830dbfSJohan Hovold 	 * console via tty_find_polling_driver() and uart_poll_init().
2228e0830dbfSJohan Hovold 	 */
2229452b9b24SJohn Ogness 	if (!uart_console_registered_locked(port) && !port->console_reinit)
2230d2403cadSAndy Shevchenko 		uart_port_spin_lock_init(port);
2231ab4382d2SGreg Kroah-Hartman 
2232ab4382d2SGreg Kroah-Hartman 	memset(&termios, 0, sizeof(struct ktermios));
2233ab4382d2SGreg Kroah-Hartman 
2234ba47f97aSJeffy Chen 	termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2235ba47f97aSJeffy Chen 	tty_termios_encode_baud_rate(&termios, baud, baud);
2236ab4382d2SGreg Kroah-Hartman 
2237ab4382d2SGreg Kroah-Hartman 	if (bits == 7)
2238ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS7;
2239ab4382d2SGreg Kroah-Hartman 	else
2240ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS8;
2241ab4382d2SGreg Kroah-Hartman 
2242ab4382d2SGreg Kroah-Hartman 	switch (parity) {
2243ab4382d2SGreg Kroah-Hartman 	case 'o': case 'O':
2244ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARODD;
2245df561f66SGustavo A. R. Silva 		fallthrough;
2246ab4382d2SGreg Kroah-Hartman 	case 'e': case 'E':
2247ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARENB;
2248ab4382d2SGreg Kroah-Hartman 		break;
2249ab4382d2SGreg Kroah-Hartman 	}
2250ab4382d2SGreg Kroah-Hartman 
2251ab4382d2SGreg Kroah-Hartman 	if (flow == 'r')
2252ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CRTSCTS;
2253ab4382d2SGreg Kroah-Hartman 
2254ab4382d2SGreg Kroah-Hartman 	/*
2255ab4382d2SGreg Kroah-Hartman 	 * some uarts on other side don't support no flow control.
2256ab4382d2SGreg Kroah-Hartman 	 * So we set * DTR in host uart to make them happy
2257ab4382d2SGreg Kroah-Hartman 	 */
2258ab4382d2SGreg Kroah-Hartman 	port->mctrl |= TIOCM_DTR;
2259ab4382d2SGreg Kroah-Hartman 
2260ab4382d2SGreg Kroah-Hartman 	port->ops->set_termios(port, &termios, &dummy);
2261ab4382d2SGreg Kroah-Hartman 	/*
2262ab4382d2SGreg Kroah-Hartman 	 * Allow the setting of the UART parameters with a NULL console
2263ab4382d2SGreg Kroah-Hartman 	 * too:
2264ab4382d2SGreg Kroah-Hartman 	 */
2265027b5717SPali Rohár 	if (co) {
2266ab4382d2SGreg Kroah-Hartman 		co->cflag = termios.c_cflag;
2267027b5717SPali Rohár 		co->ispeed = termios.c_ispeed;
2268027b5717SPali Rohár 		co->ospeed = termios.c_ospeed;
2269027b5717SPali Rohár 	}
2270ab4382d2SGreg Kroah-Hartman 
2271ab4382d2SGreg Kroah-Hartman 	return 0;
2272ab4382d2SGreg Kroah-Hartman }
2273ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options);
2274ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2275ab4382d2SGreg Kroah-Hartman 
2276cf75525fSJiri Slaby /**
2277cf75525fSJiri Slaby  * uart_change_pm - set power state of the port
2278cf75525fSJiri Slaby  *
2279cf75525fSJiri Slaby  * @state: port descriptor
2280cf75525fSJiri Slaby  * @pm_state: new state
2281cf75525fSJiri Slaby  *
2282cf75525fSJiri Slaby  * Locking: port->mutex has to be held
2283cf75525fSJiri Slaby  */
22846f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
22856f538fe3SLinus Walleij 			   enum uart_pm_state pm_state)
2286ab4382d2SGreg Kroah-Hartman {
22874047b371SPeter Hurley 	struct uart_port *port = uart_port_check(state);
2288ab4382d2SGreg Kroah-Hartman 
2289ab4382d2SGreg Kroah-Hartman 	if (state->pm_state != pm_state) {
22904047b371SPeter Hurley 		if (port && port->ops->pm)
2291ab4382d2SGreg Kroah-Hartman 			port->ops->pm(port, pm_state, state->pm_state);
2292ab4382d2SGreg Kroah-Hartman 		state->pm_state = pm_state;
2293ab4382d2SGreg Kroah-Hartman 	}
2294ab4382d2SGreg Kroah-Hartman }
2295ab4382d2SGreg Kroah-Hartman 
2296ab4382d2SGreg Kroah-Hartman struct uart_match {
2297ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2298ab4382d2SGreg Kroah-Hartman 	struct uart_driver *driver;
2299ab4382d2SGreg Kroah-Hartman };
2300ab4382d2SGreg Kroah-Hartman 
2301ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data)
2302ab4382d2SGreg Kroah-Hartman {
2303ab4382d2SGreg Kroah-Hartman 	struct uart_match *match = data;
2304ab4382d2SGreg Kroah-Hartman 	struct tty_driver *tty_drv = match->driver->tty_driver;
2305ab4382d2SGreg Kroah-Hartman 	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2306ab4382d2SGreg Kroah-Hartman 		match->port->line;
2307ab4382d2SGreg Kroah-Hartman 
2308ab4382d2SGreg Kroah-Hartman 	return dev->devt == devt; /* Actually, only one tty per port */
2309ab4382d2SGreg Kroah-Hartman }
2310ab4382d2SGreg Kroah-Hartman 
2311ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2312ab4382d2SGreg Kroah-Hartman {
2313ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2314ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2315ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2316ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2317ab4382d2SGreg Kroah-Hartman 
2318ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2319ab4382d2SGreg Kroah-Hartman 
2320ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
232188e2582eSLucas Stach 	if (tty_dev && device_may_wakeup(tty_dev)) {
2322aef3ad10SAndy Shevchenko 		enable_irq_wake(uport->irq);
2323ab4382d2SGreg Kroah-Hartman 		put_device(tty_dev);
2324ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2325ab4382d2SGreg Kroah-Hartman 		return 0;
2326ab4382d2SGreg Kroah-Hartman 	}
23275a65dcc0SFederico Vaga 	put_device(tty_dev);
23285a65dcc0SFederico Vaga 
2329c9d2325cSVijaya Krishna Nivarthi 	/*
2330c9d2325cSVijaya Krishna Nivarthi 	 * Nothing to do if the console is not suspending
2331c9d2325cSVijaya Krishna Nivarthi 	 * except stop_rx to prevent any asynchronous data
2332cfab87c2SVijaya Krishna Nivarthi 	 * over RX line. However ensure that we will be
2333cfab87c2SVijaya Krishna Nivarthi 	 * able to Re-start_rx later.
2334c9d2325cSVijaya Krishna Nivarthi 	 */
2335c9d2325cSVijaya Krishna Nivarthi 	if (!console_suspend_enabled && uart_console(uport)) {
2336cfab87c2SVijaya Krishna Nivarthi 		if (uport->ops->start_rx)
2337c9d2325cSVijaya Krishna Nivarthi 			uport->ops->stop_rx(uport);
2338b164c972SPeter Hurley 		goto unlock;
2339c9d2325cSVijaya Krishna Nivarthi 	}
2340b164c972SPeter Hurley 
2341ab4382d2SGreg Kroah-Hartman 	uport->suspended = 1;
2342ab4382d2SGreg Kroah-Hartman 
2343d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
2344ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2345ab4382d2SGreg Kroah-Hartman 		int tries;
234618c9d4a3SAl Cooper 		unsigned int mctrl;
2347ab4382d2SGreg Kroah-Hartman 
234875b20a2aSIlpo Järvinen 		tty_port_set_suspended(port, true);
2349515be7baSIlpo Järvinen 		tty_port_set_initialized(port, false);
2350ab4382d2SGreg Kroah-Hartman 
2351ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2352ab4382d2SGreg Kroah-Hartman 		ops->stop_tx(uport);
23537c7f9bc9SLukas Wunner 		if (!(uport->rs485.flags & SER_RS485_ENABLED))
2354ab4382d2SGreg Kroah-Hartman 			ops->set_mctrl(uport, 0);
235518c9d4a3SAl Cooper 		/* save mctrl so it can be restored on resume */
235618c9d4a3SAl Cooper 		mctrl = uport->mctrl;
235718c9d4a3SAl Cooper 		uport->mctrl = 0;
2358ab4382d2SGreg Kroah-Hartman 		ops->stop_rx(uport);
2359ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2360ab4382d2SGreg Kroah-Hartman 
2361ab4382d2SGreg Kroah-Hartman 		/*
2362ab4382d2SGreg Kroah-Hartman 		 * Wait for the transmitter to empty.
2363ab4382d2SGreg Kroah-Hartman 		 */
2364ab4382d2SGreg Kroah-Hartman 		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2365ab4382d2SGreg Kroah-Hartman 			msleep(10);
2366ab4382d2SGreg Kroah-Hartman 		if (!tries)
2367cade3580SAndy Shevchenko 			dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2368cade3580SAndy Shevchenko 				uport->name);
2369ab4382d2SGreg Kroah-Hartman 
2370ab4382d2SGreg Kroah-Hartman 		ops->shutdown(uport);
237118c9d4a3SAl Cooper 		uport->mctrl = mctrl;
2372ab4382d2SGreg Kroah-Hartman 	}
2373ab4382d2SGreg Kroah-Hartman 
2374ab4382d2SGreg Kroah-Hartman 	/*
2375ab4382d2SGreg Kroah-Hartman 	 * Disable the console device before suspending.
2376ab4382d2SGreg Kroah-Hartman 	 */
2377b164c972SPeter Hurley 	if (uart_console(uport))
2378ab4382d2SGreg Kroah-Hartman 		console_stop(uport->cons);
2379ab4382d2SGreg Kroah-Hartman 
23806f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
2381b164c972SPeter Hurley unlock:
2382ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2383ab4382d2SGreg Kroah-Hartman 
2384ab4382d2SGreg Kroah-Hartman 	return 0;
2385ab4382d2SGreg Kroah-Hartman }
238615dc475bSJiri Slaby EXPORT_SYMBOL(uart_suspend_port);
2387ab4382d2SGreg Kroah-Hartman 
2388ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2389ab4382d2SGreg Kroah-Hartman {
2390ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2391ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2392ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2393ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2394ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2395ab4382d2SGreg Kroah-Hartman 
2396ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2397ab4382d2SGreg Kroah-Hartman 
2398ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2399ab4382d2SGreg Kroah-Hartman 	if (!uport->suspended && device_may_wakeup(tty_dev)) {
2400aef3ad10SAndy Shevchenko 		if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2401ab4382d2SGreg Kroah-Hartman 			disable_irq_wake(uport->irq);
24025a65dcc0SFederico Vaga 		put_device(tty_dev);
2403ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2404ab4382d2SGreg Kroah-Hartman 		return 0;
2405ab4382d2SGreg Kroah-Hartman 	}
24065a65dcc0SFederico Vaga 	put_device(tty_dev);
2407ab4382d2SGreg Kroah-Hartman 	uport->suspended = 0;
2408ab4382d2SGreg Kroah-Hartman 
2409ab4382d2SGreg Kroah-Hartman 	/*
2410ab4382d2SGreg Kroah-Hartman 	 * Re-enable the console device after suspending.
2411ab4382d2SGreg Kroah-Hartman 	 */
24125933a161SYin Kangkai 	if (uart_console(uport)) {
2413ab4382d2SGreg Kroah-Hartman 		/*
2414ab4382d2SGreg Kroah-Hartman 		 * First try to use the console cflag setting.
2415ab4382d2SGreg Kroah-Hartman 		 */
2416ab4382d2SGreg Kroah-Hartman 		memset(&termios, 0, sizeof(struct ktermios));
2417ab4382d2SGreg Kroah-Hartman 		termios.c_cflag = uport->cons->cflag;
2418027b5717SPali Rohár 		termios.c_ispeed = uport->cons->ispeed;
2419027b5717SPali Rohár 		termios.c_ospeed = uport->cons->ospeed;
2420ab4382d2SGreg Kroah-Hartman 
2421ab4382d2SGreg Kroah-Hartman 		/*
2422ab4382d2SGreg Kroah-Hartman 		 * If that's unset, use the tty termios setting.
2423ab4382d2SGreg Kroah-Hartman 		 */
2424adc8d746SAlan Cox 		if (port->tty && termios.c_cflag == 0)
2425adc8d746SAlan Cox 			termios = port->tty->termios;
2426ab4382d2SGreg Kroah-Hartman 
242794abc56fSNing Jiang 		if (console_suspend_enabled)
24286f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2429ab4382d2SGreg Kroah-Hartman 		uport->ops->set_termios(uport, &termios, NULL);
2430cfab87c2SVijaya Krishna Nivarthi 		if (!console_suspend_enabled && uport->ops->start_rx)
2431cfab87c2SVijaya Krishna Nivarthi 			uport->ops->start_rx(uport);
24325933a161SYin Kangkai 		if (console_suspend_enabled)
2433ab4382d2SGreg Kroah-Hartman 			console_start(uport->cons);
2434ab4382d2SGreg Kroah-Hartman 	}
2435ab4382d2SGreg Kroah-Hartman 
243680f02d54SPeter Hurley 	if (tty_port_suspended(port)) {
2437ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2438ab4382d2SGreg Kroah-Hartman 		int ret;
2439ab4382d2SGreg Kroah-Hartman 
24406f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2441ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
24427c7f9bc9SLukas Wunner 		if (!(uport->rs485.flags & SER_RS485_ENABLED))
2443ab4382d2SGreg Kroah-Hartman 			ops->set_mctrl(uport, 0);
2444ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2445ab4382d2SGreg Kroah-Hartman 		if (console_suspend_enabled || !uart_console(uport)) {
2446ab4382d2SGreg Kroah-Hartman 			/* Protected by port mutex for now */
2447ab4382d2SGreg Kroah-Hartman 			struct tty_struct *tty = port->tty;
24484ed71addSTamseel Shams 
2449ab4382d2SGreg Kroah-Hartman 			ret = ops->startup(uport);
2450ab4382d2SGreg Kroah-Hartman 			if (ret == 0) {
2451ab4382d2SGreg Kroah-Hartman 				if (tty)
2452826736a6SIlpo Järvinen 					uart_change_line_settings(tty, state, NULL);
2453ab4382d2SGreg Kroah-Hartman 				spin_lock_irq(&uport->lock);
24547c7f9bc9SLukas Wunner 				if (!(uport->rs485.flags & SER_RS485_ENABLED))
2455ab4382d2SGreg Kroah-Hartman 					ops->set_mctrl(uport, uport->mctrl);
24567c7f9bc9SLukas Wunner 				else
24577c7f9bc9SLukas Wunner 					uart_rs485_config(uport);
2458ab4382d2SGreg Kroah-Hartman 				ops->start_tx(uport);
2459ab4382d2SGreg Kroah-Hartman 				spin_unlock_irq(&uport->lock);
2460515be7baSIlpo Järvinen 				tty_port_set_initialized(port, true);
2461ab4382d2SGreg Kroah-Hartman 			} else {
2462ab4382d2SGreg Kroah-Hartman 				/*
2463ab4382d2SGreg Kroah-Hartman 				 * Failed to resume - maybe hardware went away?
2464ab4382d2SGreg Kroah-Hartman 				 * Clear the "initialized" flag so we won't try
2465ab4382d2SGreg Kroah-Hartman 				 * to call the low level drivers shutdown method.
2466ab4382d2SGreg Kroah-Hartman 				 */
2467ab4382d2SGreg Kroah-Hartman 				uart_shutdown(tty, state);
2468ab4382d2SGreg Kroah-Hartman 			}
2469ab4382d2SGreg Kroah-Hartman 		}
2470ab4382d2SGreg Kroah-Hartman 
247175b20a2aSIlpo Järvinen 		tty_port_set_suspended(port, false);
2472ab4382d2SGreg Kroah-Hartman 	}
2473ab4382d2SGreg Kroah-Hartman 
2474ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman 	return 0;
2477ab4382d2SGreg Kroah-Hartman }
247815dc475bSJiri Slaby EXPORT_SYMBOL(uart_resume_port);
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman static inline void
2481ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port)
2482ab4382d2SGreg Kroah-Hartman {
2483ab4382d2SGreg Kroah-Hartman 	char address[64];
2484ab4382d2SGreg Kroah-Hartman 
2485ab4382d2SGreg Kroah-Hartman 	switch (port->iotype) {
2486ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2487ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2488ab4382d2SGreg Kroah-Hartman 		break;
2489ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2490ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2491ab4382d2SGreg Kroah-Hartman 			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2492ab4382d2SGreg Kroah-Hartman 		break;
2493ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2494bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2495ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
24963ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2497ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2498ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2499ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2500ab4382d2SGreg Kroah-Hartman 			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2501ab4382d2SGreg Kroah-Hartman 		break;
2502ab4382d2SGreg Kroah-Hartman 	default:
2503eb9e109dSWolfram Sang 		strscpy(address, "*unknown*", sizeof(address));
2504ab4382d2SGreg Kroah-Hartman 		break;
2505ab4382d2SGreg Kroah-Hartman 	}
2506ab4382d2SGreg Kroah-Hartman 
2507cade3580SAndy Shevchenko 	pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
250868ed7e1cSJames Bottomley 	       port->dev ? dev_name(port->dev) : "",
250968ed7e1cSJames Bottomley 	       port->dev ? ": " : "",
2510cade3580SAndy Shevchenko 	       port->name,
25117d12b976SKees Cook 	       address, port->irq, port->uartclk / 16, uart_type(port));
2512e7b91932SMaciej W. Rozycki 
2513e7b91932SMaciej W. Rozycki 	/* The magic multiplier feature is a bit obscure, so report it too.  */
2514e7b91932SMaciej W. Rozycki 	if (port->flags & UPF_MAGIC_MULTIPLIER)
2515e7b91932SMaciej W. Rozycki 		pr_info("%s%s%s extra baud rates supported: %d, %d",
2516e7b91932SMaciej W. Rozycki 			port->dev ? dev_name(port->dev) : "",
2517e7b91932SMaciej W. Rozycki 			port->dev ? ": " : "",
2518e7b91932SMaciej W. Rozycki 			port->name,
2519e7b91932SMaciej W. Rozycki 			port->uartclk / 8, port->uartclk / 4);
2520ab4382d2SGreg Kroah-Hartman }
2521ab4382d2SGreg Kroah-Hartman 
2522ab4382d2SGreg Kroah-Hartman static void
2523ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2524ab4382d2SGreg Kroah-Hartman 		    struct uart_port *port)
2525ab4382d2SGreg Kroah-Hartman {
2526ab4382d2SGreg Kroah-Hartman 	unsigned int flags;
2527ab4382d2SGreg Kroah-Hartman 
2528ab4382d2SGreg Kroah-Hartman 	/*
2529ab4382d2SGreg Kroah-Hartman 	 * If there isn't a port here, don't do anything further.
2530ab4382d2SGreg Kroah-Hartman 	 */
2531ab4382d2SGreg Kroah-Hartman 	if (!port->iobase && !port->mapbase && !port->membase)
2532ab4382d2SGreg Kroah-Hartman 		return;
2533ab4382d2SGreg Kroah-Hartman 
2534ab4382d2SGreg Kroah-Hartman 	/*
2535ab4382d2SGreg Kroah-Hartman 	 * Now do the auto configuration stuff.  Note that config_port
2536ab4382d2SGreg Kroah-Hartman 	 * is expected to claim the resources and map the port for us.
2537ab4382d2SGreg Kroah-Hartman 	 */
2538ab4382d2SGreg Kroah-Hartman 	flags = 0;
2539ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_AUTO_IRQ)
2540ab4382d2SGreg Kroah-Hartman 		flags |= UART_CONFIG_IRQ;
2541ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_BOOT_AUTOCONF) {
2542ab4382d2SGreg Kroah-Hartman 		if (!(port->flags & UPF_FIXED_TYPE)) {
2543ab4382d2SGreg Kroah-Hartman 			port->type = PORT_UNKNOWN;
2544ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_TYPE;
2545ab4382d2SGreg Kroah-Hartman 		}
2546ab4382d2SGreg Kroah-Hartman 		port->ops->config_port(port, flags);
2547ab4382d2SGreg Kroah-Hartman 	}
2548ab4382d2SGreg Kroah-Hartman 
2549ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_UNKNOWN) {
2550ab4382d2SGreg Kroah-Hartman 		unsigned long flags;
2551ab4382d2SGreg Kroah-Hartman 
2552ab4382d2SGreg Kroah-Hartman 		uart_report_port(drv, port);
2553ab4382d2SGreg Kroah-Hartman 
2554ab4382d2SGreg Kroah-Hartman 		/* Power up port for set_mctrl() */
25556f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2556ab4382d2SGreg Kroah-Hartman 
2557ab4382d2SGreg Kroah-Hartman 		/*
2558ab4382d2SGreg Kroah-Hartman 		 * Ensure that the modem control lines are de-activated.
2559ab4382d2SGreg Kroah-Hartman 		 * keep the DTR setting that is set in uart_set_options()
2560ab4382d2SGreg Kroah-Hartman 		 * We probably don't need a spinlock around this, but
2561ab4382d2SGreg Kroah-Hartman 		 */
2562ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
256393a770b7SLukas Wunner 		port->mctrl &= TIOCM_DTR;
25647c7f9bc9SLukas Wunner 		if (!(port->rs485.flags & SER_RS485_ENABLED))
256593a770b7SLukas Wunner 			port->ops->set_mctrl(port, port->mctrl);
25667c7f9bc9SLukas Wunner 		else
25677c7f9bc9SLukas Wunner 			uart_rs485_config(port);
2568ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
2569ab4382d2SGreg Kroah-Hartman 
2570ab4382d2SGreg Kroah-Hartman 		/*
2571ab4382d2SGreg Kroah-Hartman 		 * If this driver supports console, and it hasn't been
2572ab4382d2SGreg Kroah-Hartman 		 * successfully registered yet, try to re-register it.
2573ab4382d2SGreg Kroah-Hartman 		 * It may be that the port was not available.
2574ab4382d2SGreg Kroah-Hartman 		 */
2575452b9b24SJohn Ogness 		if (port->cons && !console_is_registered(port->cons))
2576ab4382d2SGreg Kroah-Hartman 			register_console(port->cons);
2577ab4382d2SGreg Kroah-Hartman 
2578ab4382d2SGreg Kroah-Hartman 		/*
2579ab4382d2SGreg Kroah-Hartman 		 * Power down all ports by default, except the
2580ab4382d2SGreg Kroah-Hartman 		 * console if we have one.
2581ab4382d2SGreg Kroah-Hartman 		 */
2582ab4382d2SGreg Kroah-Hartman 		if (!uart_console(port))
25836f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_OFF);
2584ab4382d2SGreg Kroah-Hartman 	}
2585ab4382d2SGreg Kroah-Hartman }
2586ab4382d2SGreg Kroah-Hartman 
2587ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2588ab4382d2SGreg Kroah-Hartman 
2589ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2590ab4382d2SGreg Kroah-Hartman {
2591ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2592ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2593*5e227ef2SDouglas Anderson 	enum uart_pm_state pm_state;
259449c02304SPeter Hurley 	struct tty_port *tport;
2595ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2596ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
2597ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2598ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2599ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
260049c02304SPeter Hurley 	int ret = 0;
2601ab4382d2SGreg Kroah-Hartman 
260249c02304SPeter Hurley 	tport = &state->port;
260349c02304SPeter Hurley 	mutex_lock(&tport->mutex);
260449c02304SPeter Hurley 
26054047b371SPeter Hurley 	port = uart_port_check(state);
26064047b371SPeter Hurley 	if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
260749c02304SPeter Hurley 		ret = -1;
260849c02304SPeter Hurley 		goto out;
260949c02304SPeter Hurley 	}
2610ab4382d2SGreg Kroah-Hartman 
2611*5e227ef2SDouglas Anderson 	pm_state = state->pm_state;
2612*5e227ef2SDouglas Anderson 	uart_change_pm(state, UART_PM_STATE_ON);
2613*5e227ef2SDouglas Anderson 
2614c7f3e708SAnton Vorontsov 	if (port->ops->poll_init) {
2615c7f3e708SAnton Vorontsov 		/*
2616d41861caSPeter Hurley 		 * We don't set initialized as we only initialized the hw,
2617d41861caSPeter Hurley 		 * e.g. state->xmit is still uninitialized.
2618c7f3e708SAnton Vorontsov 		 */
2619d41861caSPeter Hurley 		if (!tty_port_initialized(tport))
2620c7f3e708SAnton Vorontsov 			ret = port->ops->poll_init(port);
2621c7f3e708SAnton Vorontsov 	}
2622c7f3e708SAnton Vorontsov 
262349c02304SPeter Hurley 	if (!ret && options) {
2624ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
26253ef5abd9SJohn Ogness 		console_list_lock();
262649c02304SPeter Hurley 		ret = uart_set_options(port, NULL, baud, parity, bits, flow);
26273ef5abd9SJohn Ogness 		console_list_unlock();
2628ab4382d2SGreg Kroah-Hartman 	}
262949c02304SPeter Hurley out:
2630*5e227ef2SDouglas Anderson 	if (ret)
2631*5e227ef2SDouglas Anderson 		uart_change_pm(state, pm_state);
263249c02304SPeter Hurley 	mutex_unlock(&tport->mutex);
263349c02304SPeter Hurley 	return ret;
2634ab4382d2SGreg Kroah-Hartman }
2635ab4382d2SGreg Kroah-Hartman 
2636ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line)
2637ab4382d2SGreg Kroah-Hartman {
2638ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2639ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2640ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
26419ed19428SPeter Hurley 	int ret = -1;
2642ab4382d2SGreg Kroah-Hartman 
26439ed19428SPeter Hurley 	port = uart_port_ref(state);
2644ef510beaSAndy Shevchenko 	if (port) {
26459ed19428SPeter Hurley 		ret = port->ops->poll_get_char(port);
26469ed19428SPeter Hurley 		uart_port_deref(port);
26479ed19428SPeter Hurley 	}
264822077b09SJiri Slaby 
26499ed19428SPeter Hurley 	return ret;
2650ab4382d2SGreg Kroah-Hartman }
2651ab4382d2SGreg Kroah-Hartman 
2652ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2653ab4382d2SGreg Kroah-Hartman {
2654ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2655ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2656ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2657ab4382d2SGreg Kroah-Hartman 
26589ed19428SPeter Hurley 	port = uart_port_ref(state);
26599ed19428SPeter Hurley 	if (!port)
26609ed19428SPeter Hurley 		return;
2661c7d44a02SDoug Anderson 
2662c7d44a02SDoug Anderson 	if (ch == '\n')
2663c7d44a02SDoug Anderson 		port->ops->poll_put_char(port, '\r');
2664ab4382d2SGreg Kroah-Hartman 	port->ops->poll_put_char(port, ch);
26659ed19428SPeter Hurley 	uart_port_deref(port);
2666ab4382d2SGreg Kroah-Hartman }
2667ab4382d2SGreg Kroah-Hartman #endif
2668ab4382d2SGreg Kroah-Hartman 
2669ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = {
26704cdd17baSJiri Slaby 	.install	= uart_install,
2671ab4382d2SGreg Kroah-Hartman 	.open		= uart_open,
2672ab4382d2SGreg Kroah-Hartman 	.close		= uart_close,
2673ab4382d2SGreg Kroah-Hartman 	.write		= uart_write,
2674ab4382d2SGreg Kroah-Hartman 	.put_char	= uart_put_char,
2675ab4382d2SGreg Kroah-Hartman 	.flush_chars	= uart_flush_chars,
2676ab4382d2SGreg Kroah-Hartman 	.write_room	= uart_write_room,
2677ab4382d2SGreg Kroah-Hartman 	.chars_in_buffer= uart_chars_in_buffer,
2678ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= uart_flush_buffer,
2679ab4382d2SGreg Kroah-Hartman 	.ioctl		= uart_ioctl,
2680ab4382d2SGreg Kroah-Hartman 	.throttle	= uart_throttle,
2681ab4382d2SGreg Kroah-Hartman 	.unthrottle	= uart_unthrottle,
2682ab4382d2SGreg Kroah-Hartman 	.send_xchar	= uart_send_xchar,
2683ab4382d2SGreg Kroah-Hartman 	.set_termios	= uart_set_termios,
2684ab4382d2SGreg Kroah-Hartman 	.set_ldisc	= uart_set_ldisc,
2685ab4382d2SGreg Kroah-Hartman 	.stop		= uart_stop,
2686ab4382d2SGreg Kroah-Hartman 	.start		= uart_start,
2687ab4382d2SGreg Kroah-Hartman 	.hangup		= uart_hangup,
2688ab4382d2SGreg Kroah-Hartman 	.break_ctl	= uart_break_ctl,
2689ab4382d2SGreg Kroah-Hartman 	.wait_until_sent= uart_wait_until_sent,
2690ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
26918a8dcabfSChristoph Hellwig 	.proc_show	= uart_proc_show,
2692ab4382d2SGreg Kroah-Hartman #endif
2693ab4382d2SGreg Kroah-Hartman 	.tiocmget	= uart_tiocmget,
2694ab4382d2SGreg Kroah-Hartman 	.tiocmset	= uart_tiocmset,
26955099d234SAl Viro 	.set_serial	= uart_set_info_user,
26965099d234SAl Viro 	.get_serial	= uart_get_info_user,
2697ab4382d2SGreg Kroah-Hartman 	.get_icount	= uart_get_icount,
2698ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2699ab4382d2SGreg Kroah-Hartman 	.poll_init	= uart_poll_init,
2700ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= uart_poll_get_char,
2701ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= uart_poll_put_char,
2702ab4382d2SGreg Kroah-Hartman #endif
2703ab4382d2SGreg Kroah-Hartman };
2704ab4382d2SGreg Kroah-Hartman 
2705ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = {
2706ab4382d2SGreg Kroah-Hartman 	.carrier_raised = uart_carrier_raised,
2707ab4382d2SGreg Kroah-Hartman 	.dtr_rts	= uart_dtr_rts,
2708b3b57646SRob Herring 	.activate	= uart_port_activate,
2709761ed4a9SRob Herring 	.shutdown	= uart_tty_port_shutdown,
2710ab4382d2SGreg Kroah-Hartman };
2711ab4382d2SGreg Kroah-Hartman 
2712ab4382d2SGreg Kroah-Hartman /**
2713ab4382d2SGreg Kroah-Hartman  * uart_register_driver - register a driver with the uart core layer
2714ab4382d2SGreg Kroah-Hartman  * @drv: low level driver structure
2715ab4382d2SGreg Kroah-Hartman  *
2716c4bd17a6SJiri Slaby  * Register a uart driver with the core driver. We in turn register with the
2717c4bd17a6SJiri Slaby  * tty layer, and initialise the core driver per-port state.
2718ab4382d2SGreg Kroah-Hartman  *
2719c4bd17a6SJiri Slaby  * We have a proc file in /proc/tty/driver which is named after the normal
2720c4bd17a6SJiri Slaby  * driver.
2721ab4382d2SGreg Kroah-Hartman  *
2722c4bd17a6SJiri Slaby  * @drv->port should be %NULL, and the per-port structures should be registered
2723c4bd17a6SJiri Slaby  * using uart_add_one_port() after this call has succeeded.
2724c4bd17a6SJiri Slaby  *
2725c4bd17a6SJiri Slaby  * Locking: none, Interrupts: enabled
2726ab4382d2SGreg Kroah-Hartman  */
2727ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv)
2728ab4382d2SGreg Kroah-Hartman {
2729ab4382d2SGreg Kroah-Hartman 	struct tty_driver *normal;
2730050dfc09SSergey Organov 	int i, retval = -ENOMEM;
2731ab4382d2SGreg Kroah-Hartman 
2732ab4382d2SGreg Kroah-Hartman 	BUG_ON(drv->state);
2733ab4382d2SGreg Kroah-Hartman 
2734ab4382d2SGreg Kroah-Hartman 	/*
2735ab4382d2SGreg Kroah-Hartman 	 * Maybe we should be using a slab cache for this, especially if
2736ab4382d2SGreg Kroah-Hartman 	 * we have a large number of ports to handle.
2737ab4382d2SGreg Kroah-Hartman 	 */
27386396bb22SKees Cook 	drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2739ab4382d2SGreg Kroah-Hartman 	if (!drv->state)
2740ab4382d2SGreg Kroah-Hartman 		goto out;
2741ab4382d2SGreg Kroah-Hartman 
274239b7b42bSJiri Slaby 	normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
274339b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
274439b7b42bSJiri Slaby 	if (IS_ERR(normal)) {
274539b7b42bSJiri Slaby 		retval = PTR_ERR(normal);
2746ab4382d2SGreg Kroah-Hartman 		goto out_kfree;
274739b7b42bSJiri Slaby 	}
2748ab4382d2SGreg Kroah-Hartman 
2749ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = normal;
2750ab4382d2SGreg Kroah-Hartman 
2751ab4382d2SGreg Kroah-Hartman 	normal->driver_name	= drv->driver_name;
2752ab4382d2SGreg Kroah-Hartman 	normal->name		= drv->dev_name;
2753ab4382d2SGreg Kroah-Hartman 	normal->major		= drv->major;
2754ab4382d2SGreg Kroah-Hartman 	normal->minor_start	= drv->minor;
2755ab4382d2SGreg Kroah-Hartman 	normal->type		= TTY_DRIVER_TYPE_SERIAL;
2756ab4382d2SGreg Kroah-Hartman 	normal->subtype		= SERIAL_TYPE_NORMAL;
2757ab4382d2SGreg Kroah-Hartman 	normal->init_termios	= tty_std_termios;
2758ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2759ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2760ab4382d2SGreg Kroah-Hartman 	normal->driver_state    = drv;
2761ab4382d2SGreg Kroah-Hartman 	tty_set_operations(normal, &uart_ops);
2762ab4382d2SGreg Kroah-Hartman 
2763ab4382d2SGreg Kroah-Hartman 	/*
2764ab4382d2SGreg Kroah-Hartman 	 * Initialise the UART state(s).
2765ab4382d2SGreg Kroah-Hartman 	 */
2766ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++) {
2767ab4382d2SGreg Kroah-Hartman 		struct uart_state *state = drv->state + i;
2768ab4382d2SGreg Kroah-Hartman 		struct tty_port *port = &state->port;
2769ab4382d2SGreg Kroah-Hartman 
2770ab4382d2SGreg Kroah-Hartman 		tty_port_init(port);
2771ab4382d2SGreg Kroah-Hartman 		port->ops = &uart_port_ops;
2772ab4382d2SGreg Kroah-Hartman 	}
2773ab4382d2SGreg Kroah-Hartman 
2774ab4382d2SGreg Kroah-Hartman 	retval = tty_register_driver(normal);
2775ab4382d2SGreg Kroah-Hartman 	if (retval >= 0)
2776ab4382d2SGreg Kroah-Hartman 		return retval;
2777ab4382d2SGreg Kroah-Hartman 
2778191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2779191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
27809f90a4ddSJiri Slaby 	tty_driver_kref_put(normal);
2781ab4382d2SGreg Kroah-Hartman out_kfree:
2782ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
2783ab4382d2SGreg Kroah-Hartman out:
2784050dfc09SSergey Organov 	return retval;
2785ab4382d2SGreg Kroah-Hartman }
278615dc475bSJiri Slaby EXPORT_SYMBOL(uart_register_driver);
2787ab4382d2SGreg Kroah-Hartman 
2788ab4382d2SGreg Kroah-Hartman /**
2789ab4382d2SGreg Kroah-Hartman  * uart_unregister_driver - remove a driver from the uart core layer
2790ab4382d2SGreg Kroah-Hartman  * @drv: low level driver structure
2791ab4382d2SGreg Kroah-Hartman  *
2792c4bd17a6SJiri Slaby  * Remove all references to a driver from the core driver. The low level
2793c4bd17a6SJiri Slaby  * driver must have removed all its ports via the uart_remove_one_port() if it
2794c4bd17a6SJiri Slaby  * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
2795c4bd17a6SJiri Slaby  *
2796c4bd17a6SJiri Slaby  * Locking: none, Interrupts: enabled
2797ab4382d2SGreg Kroah-Hartman  */
2798ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv)
2799ab4382d2SGreg Kroah-Hartman {
2800ab4382d2SGreg Kroah-Hartman 	struct tty_driver *p = drv->tty_driver;
2801191c5f10SJiri Slaby 	unsigned int i;
2802191c5f10SJiri Slaby 
2803ab4382d2SGreg Kroah-Hartman 	tty_unregister_driver(p);
28049f90a4ddSJiri Slaby 	tty_driver_kref_put(p);
2805191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2806191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2807ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
28081e66cdedSAlan Cox 	drv->state = NULL;
2809ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = NULL;
2810ab4382d2SGreg Kroah-Hartman }
281115dc475bSJiri Slaby EXPORT_SYMBOL(uart_unregister_driver);
2812ab4382d2SGreg Kroah-Hartman 
2813ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index)
2814ab4382d2SGreg Kroah-Hartman {
2815ab4382d2SGreg Kroah-Hartman 	struct uart_driver *p = co->data;
2816ab4382d2SGreg Kroah-Hartman 	*index = co->index;
2817ab4382d2SGreg Kroah-Hartman 	return p->tty_driver;
2818ab4382d2SGreg Kroah-Hartman }
2819488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device);
2820ab4382d2SGreg Kroah-Hartman 
2821143c02c8SAndy Shevchenko static ssize_t uartclk_show(struct device *dev,
28226915c0e4STomas Hlavacek 	struct device_attribute *attr, char *buf)
28236915c0e4STomas Hlavacek {
2824bebe73e3SAlan Cox 	struct serial_struct tmp;
28256915c0e4STomas Hlavacek 	struct tty_port *port = dev_get_drvdata(dev);
2826b1b79916STomas Hlavacek 
28279f109694SAlan Cox 	uart_get_info(port, &tmp);
28289cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.baud_base * 16);
28296915c0e4STomas Hlavacek }
28306915c0e4STomas Hlavacek 
2831143c02c8SAndy Shevchenko static ssize_t type_show(struct device *dev,
2832373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2833373bac4cSAlan Cox {
2834373bac4cSAlan Cox 	struct serial_struct tmp;
2835373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2836373bac4cSAlan Cox 
2837373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28389cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.type);
2839373bac4cSAlan Cox }
2840143c02c8SAndy Shevchenko 
2841143c02c8SAndy Shevchenko static ssize_t line_show(struct device *dev,
2842373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2843373bac4cSAlan Cox {
2844373bac4cSAlan Cox 	struct serial_struct tmp;
2845373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2846373bac4cSAlan Cox 
2847373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28489cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.line);
2849373bac4cSAlan Cox }
2850373bac4cSAlan Cox 
2851143c02c8SAndy Shevchenko static ssize_t port_show(struct device *dev,
2852373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2853373bac4cSAlan Cox {
2854373bac4cSAlan Cox 	struct serial_struct tmp;
2855373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2856fd985e1dSAndrew Morton 	unsigned long ioaddr;
2857373bac4cSAlan Cox 
2858373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2859fd985e1dSAndrew Morton 	ioaddr = tmp.port;
2860fd985e1dSAndrew Morton 	if (HIGH_BITS_OFFSET)
2861fd985e1dSAndrew Morton 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
28629cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", ioaddr);
2863373bac4cSAlan Cox }
2864373bac4cSAlan Cox 
2865143c02c8SAndy Shevchenko static ssize_t irq_show(struct device *dev,
2866373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2867373bac4cSAlan Cox {
2868373bac4cSAlan Cox 	struct serial_struct tmp;
2869373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2870373bac4cSAlan Cox 
2871373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28729cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.irq);
2873373bac4cSAlan Cox }
2874373bac4cSAlan Cox 
2875143c02c8SAndy Shevchenko static ssize_t flags_show(struct device *dev,
2876373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2877373bac4cSAlan Cox {
2878373bac4cSAlan Cox 	struct serial_struct tmp;
2879373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2880373bac4cSAlan Cox 
2881373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28829cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%X\n", tmp.flags);
2883373bac4cSAlan Cox }
2884373bac4cSAlan Cox 
2885143c02c8SAndy Shevchenko static ssize_t xmit_fifo_size_show(struct device *dev,
2886373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2887373bac4cSAlan Cox {
2888373bac4cSAlan Cox 	struct serial_struct tmp;
2889373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2890373bac4cSAlan Cox 
2891373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28929cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2893373bac4cSAlan Cox }
2894373bac4cSAlan Cox 
2895143c02c8SAndy Shevchenko static ssize_t close_delay_show(struct device *dev,
2896373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2897373bac4cSAlan Cox {
2898373bac4cSAlan Cox 	struct serial_struct tmp;
2899373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2900373bac4cSAlan Cox 
2901373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29029cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.close_delay);
2903373bac4cSAlan Cox }
2904373bac4cSAlan Cox 
2905143c02c8SAndy Shevchenko static ssize_t closing_wait_show(struct device *dev,
2906373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2907373bac4cSAlan Cox {
2908373bac4cSAlan Cox 	struct serial_struct tmp;
2909373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2910373bac4cSAlan Cox 
2911373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29129cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.closing_wait);
2913373bac4cSAlan Cox }
2914373bac4cSAlan Cox 
2915143c02c8SAndy Shevchenko static ssize_t custom_divisor_show(struct device *dev,
2916373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2917373bac4cSAlan Cox {
2918373bac4cSAlan Cox 	struct serial_struct tmp;
2919373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2920373bac4cSAlan Cox 
2921373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29229cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.custom_divisor);
2923373bac4cSAlan Cox }
2924373bac4cSAlan Cox 
2925143c02c8SAndy Shevchenko static ssize_t io_type_show(struct device *dev,
2926373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2927373bac4cSAlan Cox {
2928373bac4cSAlan Cox 	struct serial_struct tmp;
2929373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2930373bac4cSAlan Cox 
2931373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29329cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.io_type);
2933373bac4cSAlan Cox }
2934373bac4cSAlan Cox 
2935143c02c8SAndy Shevchenko static ssize_t iomem_base_show(struct device *dev,
2936373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2937373bac4cSAlan Cox {
2938373bac4cSAlan Cox 	struct serial_struct tmp;
2939373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2940373bac4cSAlan Cox 
2941373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29429cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
2943373bac4cSAlan Cox }
2944373bac4cSAlan Cox 
2945143c02c8SAndy Shevchenko static ssize_t iomem_reg_shift_show(struct device *dev,
2946373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2947373bac4cSAlan Cox {
2948373bac4cSAlan Cox 	struct serial_struct tmp;
2949373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2950373bac4cSAlan Cox 
2951373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29529cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
2953373bac4cSAlan Cox }
2954373bac4cSAlan Cox 
2955a3cb39d2SAndy Shevchenko static ssize_t console_show(struct device *dev,
2956a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, char *buf)
2957a3cb39d2SAndy Shevchenko {
2958a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2959a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2960a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2961a3cb39d2SAndy Shevchenko 	bool console = false;
2962a3cb39d2SAndy Shevchenko 
2963a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2964a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2965a3cb39d2SAndy Shevchenko 	if (uport)
2966452b9b24SJohn Ogness 		console = uart_console_registered(uport);
2967a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
2968a3cb39d2SAndy Shevchenko 
2969a3cb39d2SAndy Shevchenko 	return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2970a3cb39d2SAndy Shevchenko }
2971a3cb39d2SAndy Shevchenko 
2972a3cb39d2SAndy Shevchenko static ssize_t console_store(struct device *dev,
2973a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, const char *buf, size_t count)
2974a3cb39d2SAndy Shevchenko {
2975a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2976a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2977a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2978a3cb39d2SAndy Shevchenko 	bool oldconsole, newconsole;
2979a3cb39d2SAndy Shevchenko 	int ret;
2980a3cb39d2SAndy Shevchenko 
2981a3cb39d2SAndy Shevchenko 	ret = kstrtobool(buf, &newconsole);
2982a3cb39d2SAndy Shevchenko 	if (ret)
2983a3cb39d2SAndy Shevchenko 		return ret;
2984a3cb39d2SAndy Shevchenko 
2985a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2986a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2987a3cb39d2SAndy Shevchenko 	if (uport) {
2988452b9b24SJohn Ogness 		oldconsole = uart_console_registered(uport);
2989a3cb39d2SAndy Shevchenko 		if (oldconsole && !newconsole) {
2990a3cb39d2SAndy Shevchenko 			ret = unregister_console(uport->cons);
2991a3cb39d2SAndy Shevchenko 		} else if (!oldconsole && newconsole) {
2992e0830dbfSJohan Hovold 			if (uart_console(uport)) {
2993e0830dbfSJohan Hovold 				uport->console_reinit = 1;
2994a3cb39d2SAndy Shevchenko 				register_console(uport->cons);
2995e0830dbfSJohan Hovold 			} else {
2996a3cb39d2SAndy Shevchenko 				ret = -ENOENT;
2997a3cb39d2SAndy Shevchenko 			}
2998e0830dbfSJohan Hovold 		}
2999a3cb39d2SAndy Shevchenko 	} else {
3000a3cb39d2SAndy Shevchenko 		ret = -ENXIO;
3001a3cb39d2SAndy Shevchenko 	}
3002a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
3003a3cb39d2SAndy Shevchenko 
3004a3cb39d2SAndy Shevchenko 	return ret < 0 ? ret : count;
3005a3cb39d2SAndy Shevchenko }
3006a3cb39d2SAndy Shevchenko 
3007143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(uartclk);
3008143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(type);
3009143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(line);
3010143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(port);
3011143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(irq);
3012143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(flags);
3013143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(xmit_fifo_size);
3014143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(close_delay);
3015143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(closing_wait);
3016143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(custom_divisor);
3017143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(io_type);
3018143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_base);
3019143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_reg_shift);
3020a3cb39d2SAndy Shevchenko static DEVICE_ATTR_RW(console);
30216915c0e4STomas Hlavacek 
30226915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = {
3023143c02c8SAndy Shevchenko 	&dev_attr_uartclk.attr,
3024373bac4cSAlan Cox 	&dev_attr_type.attr,
3025373bac4cSAlan Cox 	&dev_attr_line.attr,
3026373bac4cSAlan Cox 	&dev_attr_port.attr,
3027373bac4cSAlan Cox 	&dev_attr_irq.attr,
3028373bac4cSAlan Cox 	&dev_attr_flags.attr,
3029373bac4cSAlan Cox 	&dev_attr_xmit_fifo_size.attr,
3030373bac4cSAlan Cox 	&dev_attr_close_delay.attr,
3031373bac4cSAlan Cox 	&dev_attr_closing_wait.attr,
3032373bac4cSAlan Cox 	&dev_attr_custom_divisor.attr,
3033373bac4cSAlan Cox 	&dev_attr_io_type.attr,
3034373bac4cSAlan Cox 	&dev_attr_iomem_base.attr,
3035373bac4cSAlan Cox 	&dev_attr_iomem_reg_shift.attr,
3036a3cb39d2SAndy Shevchenko 	&dev_attr_console.attr,
3037a3cb39d2SAndy Shevchenko 	NULL
30386915c0e4STomas Hlavacek };
30396915c0e4STomas Hlavacek 
3040b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = {
30416915c0e4STomas Hlavacek 	.attrs = tty_dev_attrs,
30426915c0e4STomas Hlavacek };
30436915c0e4STomas Hlavacek 
3044ab4382d2SGreg Kroah-Hartman /**
3045ab4382d2SGreg Kroah-Hartman  * uart_add_one_port - attach a driver-defined port structure
3046ab4382d2SGreg Kroah-Hartman  * @drv: pointer to the uart low level driver structure for this port
3047ab4382d2SGreg Kroah-Hartman  * @uport: uart port structure to use for this port.
3048ab4382d2SGreg Kroah-Hartman  *
3049a157270fSAhmed S. Darwish  * Context: task context, might sleep
3050a157270fSAhmed S. Darwish  *
3051c4bd17a6SJiri Slaby  * This allows the driver @drv to register its own uart_port structure with the
3052c4bd17a6SJiri Slaby  * core driver. The main purpose is to allow the low level uart drivers to
3053c4bd17a6SJiri Slaby  * expand uart_port, rather than having yet more levels of structures.
3054ab4382d2SGreg Kroah-Hartman  */
3055ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
3056ab4382d2SGreg Kroah-Hartman {
3057ab4382d2SGreg Kroah-Hartman 	struct uart_state *state;
3058ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
3059ab4382d2SGreg Kroah-Hartman 	int ret = 0;
3060ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
3061266dcff0SGreg Kroah-Hartman 	int num_groups;
3062ab4382d2SGreg Kroah-Hartman 
3063ab4382d2SGreg Kroah-Hartman 	if (uport->line >= drv->nr)
3064ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
3065ab4382d2SGreg Kroah-Hartman 
3066ab4382d2SGreg Kroah-Hartman 	state = drv->state + uport->line;
3067ab4382d2SGreg Kroah-Hartman 	port = &state->port;
3068ab4382d2SGreg Kroah-Hartman 
3069ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
3070ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
3071ab4382d2SGreg Kroah-Hartman 	if (state->uart_port) {
3072ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
3073ab4382d2SGreg Kroah-Hartman 		goto out;
3074ab4382d2SGreg Kroah-Hartman 	}
3075ab4382d2SGreg Kroah-Hartman 
30762b702b9bSPeter Hurley 	/* Link the port to the driver state table and vice versa */
30779ed19428SPeter Hurley 	atomic_set(&state->refcount, 1);
30789ed19428SPeter Hurley 	init_waitqueue_head(&state->remove_wait);
3079ab4382d2SGreg Kroah-Hartman 	state->uart_port = uport;
3080ab4382d2SGreg Kroah-Hartman 	uport->state = state;
3081ab4382d2SGreg Kroah-Hartman 
30822b702b9bSPeter Hurley 	state->pm_state = UART_PM_STATE_UNDEFINED;
30832b702b9bSPeter Hurley 	uport->cons = drv->cons;
3084959801feSPeter Hurley 	uport->minor = drv->tty_driver->minor_start + uport->line;
3085f7048b15SVignesh R 	uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3086f7048b15SVignesh R 				drv->tty_driver->name_base + uport->line);
3087f7048b15SVignesh R 	if (!uport->name) {
3088f7048b15SVignesh R 		ret = -ENOMEM;
3089f7048b15SVignesh R 		goto out;
3090f7048b15SVignesh R 	}
30912b702b9bSPeter Hurley 
3092fe88c648SJohan Hovold 	/*
3093fe88c648SJohan Hovold 	 * If this port is in use as a console then the spinlock is already
3094fe88c648SJohan Hovold 	 * initialised.
3095fe88c648SJohan Hovold 	 */
3096452b9b24SJohn Ogness 	if (!uart_console_registered(uport))
3097d2403cadSAndy Shevchenko 		uart_port_spin_lock_init(uport);
3098d2403cadSAndy Shevchenko 
3099a208ffd2SGrant Likely 	if (uport->cons && uport->dev)
3100dd8b7a1dSMichal Simek 		of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3101ab4382d2SGreg Kroah-Hartman 
3102fb2b9001SSudip Mukherjee 	tty_port_link_device(port, drv->tty_driver, uport->line);
3103ab4382d2SGreg Kroah-Hartman 	uart_configure_port(drv, state, uport);
3104ab4382d2SGreg Kroah-Hartman 
31054dda864dSGeert Uytterhoeven 	port->console = uart_console(uport);
31064dda864dSGeert Uytterhoeven 
3107266dcff0SGreg Kroah-Hartman 	num_groups = 2;
3108266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3109266dcff0SGreg Kroah-Hartman 		num_groups++;
3110266dcff0SGreg Kroah-Hartman 
3111c2b703b8SYoshihiro YUNOMAE 	uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3112266dcff0SGreg Kroah-Hartman 				    GFP_KERNEL);
3113266dcff0SGreg Kroah-Hartman 	if (!uport->tty_groups) {
3114266dcff0SGreg Kroah-Hartman 		ret = -ENOMEM;
3115266dcff0SGreg Kroah-Hartman 		goto out;
3116266dcff0SGreg Kroah-Hartman 	}
3117266dcff0SGreg Kroah-Hartman 	uport->tty_groups[0] = &tty_dev_attr_group;
3118266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3119266dcff0SGreg Kroah-Hartman 		uport->tty_groups[1] = uport->attr_group;
3120266dcff0SGreg Kroah-Hartman 
3121ab4382d2SGreg Kroah-Hartman 	/*
3122ab4382d2SGreg Kroah-Hartman 	 * Register the port whether it's detected or not.  This allows
3123015355b7SGeert Uytterhoeven 	 * setserial to be used to alter this port's parameters.
3124ab4382d2SGreg Kroah-Hartman 	 */
3125da4c2799SJohan Hovold 	tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3126266dcff0SGreg Kroah-Hartman 			uport->line, uport->dev, port, uport->tty_groups);
3127b289c496SChengguang Xu 	if (!IS_ERR(tty_dev)) {
312877359835SSimon Glass 		device_set_wakeup_capable(tty_dev, 1);
312977359835SSimon Glass 	} else {
31302f2dafe7SSudip Mukherjee 		dev_err(uport->dev, "Cannot register tty device on line %d\n",
3131ab4382d2SGreg Kroah-Hartman 		       uport->line);
313277359835SSimon Glass 	}
3133ab4382d2SGreg Kroah-Hartman 
3134ab4382d2SGreg Kroah-Hartman 	/*
3135ab4382d2SGreg Kroah-Hartman 	 * Ensure UPF_DEAD is not set.
3136ab4382d2SGreg Kroah-Hartman 	 */
3137ab4382d2SGreg Kroah-Hartman 	uport->flags &= ~UPF_DEAD;
3138ab4382d2SGreg Kroah-Hartman 
3139ab4382d2SGreg Kroah-Hartman  out:
3140ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3141ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
3142ab4382d2SGreg Kroah-Hartman 
3143ab4382d2SGreg Kroah-Hartman 	return ret;
3144ab4382d2SGreg Kroah-Hartman }
314515dc475bSJiri Slaby EXPORT_SYMBOL(uart_add_one_port);
3146ab4382d2SGreg Kroah-Hartman 
3147ab4382d2SGreg Kroah-Hartman /**
3148ab4382d2SGreg Kroah-Hartman  * uart_remove_one_port - detach a driver defined port structure
3149ab4382d2SGreg Kroah-Hartman  * @drv: pointer to the uart low level driver structure for this port
3150ab4382d2SGreg Kroah-Hartman  * @uport: uart port structure for this port
3151ab4382d2SGreg Kroah-Hartman  *
3152a157270fSAhmed S. Darwish  * Context: task context, might sleep
3153a157270fSAhmed S. Darwish  *
3154c4bd17a6SJiri Slaby  * This unhooks (and hangs up) the specified port structure from the core
3155c4bd17a6SJiri Slaby  * driver. No further calls will be made to the low-level code for this port.
3156ab4382d2SGreg Kroah-Hartman  */
3157ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
3158ab4382d2SGreg Kroah-Hartman {
3159ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
3160ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
31614047b371SPeter Hurley 	struct uart_port *uart_port;
31624c6d5b4dSGeert Uytterhoeven 	struct tty_struct *tty;
3163b342dd51SChen Gang 	int ret = 0;
3164ab4382d2SGreg Kroah-Hartman 
3165ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
3166ab4382d2SGreg Kroah-Hartman 
3167ab4382d2SGreg Kroah-Hartman 	/*
3168ab4382d2SGreg Kroah-Hartman 	 * Mark the port "dead" - this prevents any opens from
3169ab4382d2SGreg Kroah-Hartman 	 * succeeding while we shut down the port.
3170ab4382d2SGreg Kroah-Hartman 	 */
3171ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
31724047b371SPeter Hurley 	uart_port = uart_port_check(state);
31734047b371SPeter Hurley 	if (uart_port != uport)
31744047b371SPeter Hurley 		dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
31754047b371SPeter Hurley 			  uart_port, uport);
31764047b371SPeter Hurley 
31774047b371SPeter Hurley 	if (!uart_port) {
3178b342dd51SChen Gang 		mutex_unlock(&port->mutex);
3179b342dd51SChen Gang 		ret = -EINVAL;
3180b342dd51SChen Gang 		goto out;
3181b342dd51SChen Gang 	}
3182ab4382d2SGreg Kroah-Hartman 	uport->flags |= UPF_DEAD;
3183ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3184ab4382d2SGreg Kroah-Hartman 
3185ab4382d2SGreg Kroah-Hartman 	/*
3186ab4382d2SGreg Kroah-Hartman 	 * Remove the devices from the tty layer
3187ab4382d2SGreg Kroah-Hartman 	 */
3188da4c2799SJohan Hovold 	tty_port_unregister_device(port, drv->tty_driver, uport->line);
3189ab4382d2SGreg Kroah-Hartman 
31904c6d5b4dSGeert Uytterhoeven 	tty = tty_port_tty_get(port);
31914c6d5b4dSGeert Uytterhoeven 	if (tty) {
3192ab4382d2SGreg Kroah-Hartman 		tty_vhangup(port->tty);
31934c6d5b4dSGeert Uytterhoeven 		tty_kref_put(tty);
31944c6d5b4dSGeert Uytterhoeven 	}
3195ab4382d2SGreg Kroah-Hartman 
3196ab4382d2SGreg Kroah-Hartman 	/*
31975f5c9ae5SGeert Uytterhoeven 	 * If the port is used as a console, unregister it
31985f5c9ae5SGeert Uytterhoeven 	 */
31995f5c9ae5SGeert Uytterhoeven 	if (uart_console(uport))
32005f5c9ae5SGeert Uytterhoeven 		unregister_console(uport->cons);
32015f5c9ae5SGeert Uytterhoeven 
32025f5c9ae5SGeert Uytterhoeven 	/*
3203ab4382d2SGreg Kroah-Hartman 	 * Free the port IO and memory resources, if any.
3204ab4382d2SGreg Kroah-Hartman 	 */
3205a7cfaf16SFabio Estevam 	if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3206ab4382d2SGreg Kroah-Hartman 		uport->ops->release_port(uport);
3207266dcff0SGreg Kroah-Hartman 	kfree(uport->tty_groups);
3208f7048b15SVignesh R 	kfree(uport->name);
3209ab4382d2SGreg Kroah-Hartman 
3210ab4382d2SGreg Kroah-Hartman 	/*
3211ab4382d2SGreg Kroah-Hartman 	 * Indicate that there isn't a port here anymore.
3212ab4382d2SGreg Kroah-Hartman 	 */
3213ab4382d2SGreg Kroah-Hartman 	uport->type = PORT_UNKNOWN;
3214ab4382d2SGreg Kroah-Hartman 
32154047b371SPeter Hurley 	mutex_lock(&port->mutex);
32169ed19428SPeter Hurley 	WARN_ON(atomic_dec_return(&state->refcount) < 0);
32179ed19428SPeter Hurley 	wait_event(state->remove_wait, !atomic_read(&state->refcount));
3218ab4382d2SGreg Kroah-Hartman 	state->uart_port = NULL;
32194047b371SPeter Hurley 	mutex_unlock(&port->mutex);
3220b342dd51SChen Gang out:
3221ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
3222ab4382d2SGreg Kroah-Hartman 
3223b342dd51SChen Gang 	return ret;
3224ab4382d2SGreg Kroah-Hartman }
322515dc475bSJiri Slaby EXPORT_SYMBOL(uart_remove_one_port);
3226ab4382d2SGreg Kroah-Hartman 
3227c4bd17a6SJiri Slaby /**
3228c4bd17a6SJiri Slaby  * uart_match_port - are the two ports equivalent?
3229c4bd17a6SJiri Slaby  * @port1: first port
3230c4bd17a6SJiri Slaby  * @port2: second port
3231c4bd17a6SJiri Slaby  *
3232c4bd17a6SJiri Slaby  * This utility function can be used to determine whether two uart_port
3233c4bd17a6SJiri Slaby  * structures describe the same port.
3234ab4382d2SGreg Kroah-Hartman  */
3235b8be5db5SJiri Slaby bool uart_match_port(const struct uart_port *port1,
3236b8be5db5SJiri Slaby 		const struct uart_port *port2)
3237ab4382d2SGreg Kroah-Hartman {
3238ab4382d2SGreg Kroah-Hartman 	if (port1->iotype != port2->iotype)
3239b8be5db5SJiri Slaby 		return false;
3240ab4382d2SGreg Kroah-Hartman 
3241ab4382d2SGreg Kroah-Hartman 	switch (port1->iotype) {
3242ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
3243b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase;
3244ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
3245b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase &&
3246b8be5db5SJiri Slaby 		       port1->hub6   == port2->hub6;
3247ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
3248bd94c407SMasahiro Yamada 	case UPIO_MEM16:
3249ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
32503ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
3251ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
3252ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
3253b8be5db5SJiri Slaby 		return port1->mapbase == port2->mapbase;
3254ab4382d2SGreg Kroah-Hartman 	}
3255b8be5db5SJiri Slaby 
3256b8be5db5SJiri Slaby 	return false;
3257ab4382d2SGreg Kroah-Hartman }
3258ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port);
3259ab4382d2SGreg Kroah-Hartman 
3260027d7dacSJiri Slaby /**
3261027d7dacSJiri Slaby  * uart_handle_dcd_change - handle a change of carrier detect state
3262027d7dacSJiri Slaby  * @uport: uart_port structure for the open port
32630388a152SIlpo Järvinen  * @active: new carrier detect status
32644d90bb14SPeter Hurley  *
3265987233b3SJiri Slaby  * Caller must hold uport->lock.
3266027d7dacSJiri Slaby  */
32670388a152SIlpo Järvinen void uart_handle_dcd_change(struct uart_port *uport, bool active)
3268027d7dacSJiri Slaby {
326942381572SGeorge Spelvin 	struct tty_port *port = &uport->state->port;
327043eca0aeSAlan Cox 	struct tty_struct *tty = port->tty;
3271c993257bSPeter Hurley 	struct tty_ldisc *ld;
3272027d7dacSJiri Slaby 
32734d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
32744d90bb14SPeter Hurley 
3275c993257bSPeter Hurley 	if (tty) {
3276c993257bSPeter Hurley 		ld = tty_ldisc_ref(tty);
327742381572SGeorge Spelvin 		if (ld) {
327842381572SGeorge Spelvin 			if (ld->ops->dcd_change)
32790388a152SIlpo Järvinen 				ld->ops->dcd_change(tty, active);
328042381572SGeorge Spelvin 			tty_ldisc_deref(ld);
328142381572SGeorge Spelvin 		}
3282c993257bSPeter Hurley 	}
3283027d7dacSJiri Slaby 
3284027d7dacSJiri Slaby 	uport->icount.dcd++;
3285027d7dacSJiri Slaby 
3286299245a1SPeter Hurley 	if (uart_dcd_enabled(uport)) {
32870388a152SIlpo Järvinen 		if (active)
3288027d7dacSJiri Slaby 			wake_up_interruptible(&port->open_wait);
328943eca0aeSAlan Cox 		else if (tty)
329043eca0aeSAlan Cox 			tty_hangup(tty);
3291027d7dacSJiri Slaby 	}
3292027d7dacSJiri Slaby }
3293027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3294027d7dacSJiri Slaby 
3295027d7dacSJiri Slaby /**
3296027d7dacSJiri Slaby  * uart_handle_cts_change - handle a change of clear-to-send state
3297027d7dacSJiri Slaby  * @uport: uart_port structure for the open port
3298968d6457SIlpo Järvinen  * @active: new clear-to-send status
32994d90bb14SPeter Hurley  *
3300987233b3SJiri Slaby  * Caller must hold uport->lock.
3301027d7dacSJiri Slaby  */
3302968d6457SIlpo Järvinen void uart_handle_cts_change(struct uart_port *uport, bool active)
3303027d7dacSJiri Slaby {
33044d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
33054d90bb14SPeter Hurley 
3306027d7dacSJiri Slaby 	uport->icount.cts++;
3307027d7dacSJiri Slaby 
3308391f93f2SPeter Hurley 	if (uart_softcts_mode(uport)) {
3309d01f4d18SPeter Hurley 		if (uport->hw_stopped) {
3310968d6457SIlpo Järvinen 			if (active) {
3311b5def43aSIlpo Järvinen 				uport->hw_stopped = false;
3312027d7dacSJiri Slaby 				uport->ops->start_tx(uport);
3313027d7dacSJiri Slaby 				uart_write_wakeup(uport);
3314027d7dacSJiri Slaby 			}
3315027d7dacSJiri Slaby 		} else {
3316968d6457SIlpo Järvinen 			if (!active) {
3317b5def43aSIlpo Järvinen 				uport->hw_stopped = true;
3318027d7dacSJiri Slaby 				uport->ops->stop_tx(uport);
3319027d7dacSJiri Slaby 			}
3320027d7dacSJiri Slaby 		}
3321391f93f2SPeter Hurley 
3322027d7dacSJiri Slaby 	}
3323027d7dacSJiri Slaby }
3324027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3325027d7dacSJiri Slaby 
3326cf75525fSJiri Slaby /**
3327cf75525fSJiri Slaby  * uart_insert_char - push a char to the uart layer
3328cf75525fSJiri Slaby  *
3329cf75525fSJiri Slaby  * User is responsible to call tty_flip_buffer_push when they are done with
3330cf75525fSJiri Slaby  * insertion.
3331cf75525fSJiri Slaby  *
3332cf75525fSJiri Slaby  * @port: corresponding port
3333cf75525fSJiri Slaby  * @status: state of the serial port RX buffer (LSR for 8250)
3334cf75525fSJiri Slaby  * @overrun: mask of overrun bits in @status
3335cf75525fSJiri Slaby  * @ch: character to push
3336cf75525fSJiri Slaby  * @flag: flag for the character (see TTY_NORMAL and friends)
3337cf75525fSJiri Slaby  */
3338027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status,
3339027d7dacSJiri Slaby 		 unsigned int overrun, unsigned int ch, unsigned int flag)
3340027d7dacSJiri Slaby {
334192a19f9cSJiri Slaby 	struct tty_port *tport = &port->state->port;
3342027d7dacSJiri Slaby 
3343027d7dacSJiri Slaby 	if ((status & port->ignore_status_mask & ~overrun) == 0)
334492a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, ch, flag) == 0)
3345dabfb351SCorbin 			++port->icount.buf_overrun;
3346027d7dacSJiri Slaby 
3347027d7dacSJiri Slaby 	/*
3348027d7dacSJiri Slaby 	 * Overrun is special.  Since it's reported immediately,
3349027d7dacSJiri Slaby 	 * it doesn't affect the current character.
3350027d7dacSJiri Slaby 	 */
3351027d7dacSJiri Slaby 	if (status & ~port->ignore_status_mask & overrun)
335292a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3353dabfb351SCorbin 			++port->icount.buf_overrun;
3354027d7dacSJiri Slaby }
3355027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char);
3356027d7dacSJiri Slaby 
335768af4317SDmitry Safonov #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
335868af4317SDmitry Safonov static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
335968af4317SDmitry Safonov 
336068af4317SDmitry Safonov static void uart_sysrq_on(struct work_struct *w)
336168af4317SDmitry Safonov {
3362b18896ffSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3363b18896ffSAndy Shevchenko 
336468af4317SDmitry Safonov 	sysrq_toggle_support(1);
3365b18896ffSAndy Shevchenko 	pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3366b18896ffSAndy Shevchenko 		sysrq_toggle_seq_len, sysrq_toggle_seq);
336768af4317SDmitry Safonov }
336868af4317SDmitry Safonov static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
336968af4317SDmitry Safonov 
337068af4317SDmitry Safonov /**
337168af4317SDmitry Safonov  * uart_try_toggle_sysrq - Enables SysRq from serial line
337268af4317SDmitry Safonov  * @port: uart_port structure where char(s) after BREAK met
337368af4317SDmitry Safonov  * @ch: new character in the sequence after received BREAK
337468af4317SDmitry Safonov  *
337568af4317SDmitry Safonov  * Enables magic SysRq when the required sequence is met on port
337668af4317SDmitry Safonov  * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
337768af4317SDmitry Safonov  *
3378987233b3SJiri Slaby  * Returns: %false if @ch is out of enabling sequence and should be
3379987233b3SJiri Slaby  * handled some other way, %true if @ch was consumed.
338068af4317SDmitry Safonov  */
338108d54703SJohan Hovold bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
338268af4317SDmitry Safonov {
33832ce5eaceSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
33842ce5eaceSAndy Shevchenko 
33852ce5eaceSAndy Shevchenko 	if (!sysrq_toggle_seq_len)
338668af4317SDmitry Safonov 		return false;
338768af4317SDmitry Safonov 
338868af4317SDmitry Safonov 	BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
338968af4317SDmitry Safonov 	if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
339068af4317SDmitry Safonov 		port->sysrq_seq = 0;
339168af4317SDmitry Safonov 		return false;
339268af4317SDmitry Safonov 	}
339368af4317SDmitry Safonov 
33942ce5eaceSAndy Shevchenko 	if (++port->sysrq_seq < sysrq_toggle_seq_len) {
339568af4317SDmitry Safonov 		port->sysrq = jiffies + SYSRQ_TIMEOUT;
339668af4317SDmitry Safonov 		return true;
339768af4317SDmitry Safonov 	}
339868af4317SDmitry Safonov 
339968af4317SDmitry Safonov 	schedule_work(&sysrq_enable_work);
340068af4317SDmitry Safonov 
340168af4317SDmitry Safonov 	port->sysrq = 0;
340268af4317SDmitry Safonov 	return true;
340368af4317SDmitry Safonov }
340408d54703SJohan Hovold EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
340568af4317SDmitry Safonov #endif
340668af4317SDmitry Safonov 
3407ef838a81SUwe Kleine-König /**
3408743f93f8SLukas Wunner  * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3409a7172561SRandy Dunlap  * @port: uart device's target port
3410ef838a81SUwe Kleine-König  *
3411ef838a81SUwe Kleine-König  * This function implements the device tree binding described in
3412ef838a81SUwe Kleine-König  * Documentation/devicetree/bindings/serial/rs485.txt.
3413ef838a81SUwe Kleine-König  */
3414c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port)
3415ef838a81SUwe Kleine-König {
3416c150c0f3SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
3417c150c0f3SLukas Wunner 	struct device *dev = port->dev;
3418ef838a81SUwe Kleine-König 	u32 rs485_delay[2];
3419ef838a81SUwe Kleine-König 	int ret;
3420163f080eSChristoph Niedermaier 	int rx_during_tx_gpio_flag;
3421ef838a81SUwe Kleine-König 
3422743f93f8SLukas Wunner 	ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3423743f93f8SLukas Wunner 					     rs485_delay, 2);
3424ef838a81SUwe Kleine-König 	if (!ret) {
3425ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = rs485_delay[0];
3426ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = rs485_delay[1];
3427ef838a81SUwe Kleine-König 	} else {
3428ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = 0;
3429ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = 0;
3430ef838a81SUwe Kleine-König 	}
3431ef838a81SUwe Kleine-König 
34324dfd1035SLino Sanfilippo 	uart_sanitize_serial_rs485_delays(port, rs485conf);
34334dfd1035SLino Sanfilippo 
3434ef838a81SUwe Kleine-König 	/*
3435f1e5b618SLukas Wunner 	 * Clear full-duplex and enabled flags, set RTS polarity to active high
3436f1e5b618SLukas Wunner 	 * to get to a defined state with the following properties:
3437ef838a81SUwe Kleine-König 	 */
3438f1e5b618SLukas Wunner 	rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3439d58a2df3SLukas Wunner 			      SER_RS485_TERMINATE_BUS |
3440f1e5b618SLukas Wunner 			      SER_RS485_RTS_AFTER_SEND);
3441f1e5b618SLukas Wunner 	rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3442ef838a81SUwe Kleine-König 
3443743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3444ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
3445ef838a81SUwe Kleine-König 
3446743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3447ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_ENABLED;
3448f1e5b618SLukas Wunner 
3449f1e5b618SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3450f1e5b618SLukas Wunner 		rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3451f1e5b618SLukas Wunner 		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3452f1e5b618SLukas Wunner 	}
3453c150c0f3SLukas Wunner 
3454d58a2df3SLukas Wunner 	/*
3455d58a2df3SLukas Wunner 	 * Disabling termination by default is the safe choice:  Else if many
3456d58a2df3SLukas Wunner 	 * bus participants enable it, no communication is possible at all.
3457d58a2df3SLukas Wunner 	 * Works fine for short cables and users may enable for longer cables.
3458d58a2df3SLukas Wunner 	 */
3459d58a2df3SLukas Wunner 	port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3460d58a2df3SLukas Wunner 							GPIOD_OUT_LOW);
3461d58a2df3SLukas Wunner 	if (IS_ERR(port->rs485_term_gpio)) {
3462d58a2df3SLukas Wunner 		ret = PTR_ERR(port->rs485_term_gpio);
3463d58a2df3SLukas Wunner 		port->rs485_term_gpio = NULL;
346489c65d66SKrzysztof Kozlowski 		return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3465d58a2df3SLukas Wunner 	}
34668bec874fSIlpo Järvinen 	if (port->rs485_term_gpio)
34678bec874fSIlpo Järvinen 		port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
3468d58a2df3SLukas Wunner 
3469163f080eSChristoph Niedermaier 	rx_during_tx_gpio_flag = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
3470163f080eSChristoph Niedermaier 				 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
3471163f080eSChristoph Niedermaier 	port->rs485_rx_during_tx_gpio = devm_gpiod_get_optional(dev,
3472163f080eSChristoph Niedermaier 								"rs485-rx-during-tx",
3473163f080eSChristoph Niedermaier 								rx_during_tx_gpio_flag);
3474163f080eSChristoph Niedermaier 	if (IS_ERR(port->rs485_rx_during_tx_gpio)) {
3475163f080eSChristoph Niedermaier 		ret = PTR_ERR(port->rs485_rx_during_tx_gpio);
3476163f080eSChristoph Niedermaier 		port->rs485_rx_during_tx_gpio = NULL;
3477163f080eSChristoph Niedermaier 		return dev_err_probe(dev, ret, "Cannot get rs485-rx-during-tx-gpios\n");
3478163f080eSChristoph Niedermaier 	}
3479163f080eSChristoph Niedermaier 
3480c150c0f3SLukas Wunner 	return 0;
3481ef838a81SUwe Kleine-König }
3482743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3483ef838a81SUwe Kleine-König 
34844f768e94SIlpo Järvinen /* Compile-time assertions for serial_rs485 layout */
34854f768e94SIlpo Järvinen static_assert(offsetof(struct serial_rs485, padding) ==
34864f768e94SIlpo Järvinen               (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
34874f768e94SIlpo Järvinen static_assert(offsetof(struct serial_rs485, padding1) ==
34884f768e94SIlpo Järvinen 	      offsetof(struct serial_rs485, padding[1]));
34894f768e94SIlpo Järvinen static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
34904f768e94SIlpo Järvinen 	      sizeof(struct serial_rs485));
34914f768e94SIlpo Järvinen 
3492ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core");
3493ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
3494