xref: /openbmc/linux/drivers/tty/serial/serial_core.c (revision f9008285)
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>
18a208ffd2SGrant Likely #include <linux/of.h>
19ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/device.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
23ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
2468af4317SDmitry Safonov #include <linux/sysrq.h>
25ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h>
2731f6bd7fSIlpo Järvinen #include <linux/math64.h>
28794edf30SDavid Howells #include <linux/security.h>
29ab4382d2SGreg Kroah-Hartman 
30aef3ad10SAndy Shevchenko #include <linux/irq.h>
317c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
32ab4382d2SGreg Kroah-Hartman 
33ab4382d2SGreg Kroah-Hartman /*
34ab4382d2SGreg Kroah-Hartman  * This is used to lock changes in serial line configuration.
35ab4382d2SGreg Kroah-Hartman  */
36ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex);
37ab4382d2SGreg Kroah-Hartman 
38ab4382d2SGreg Kroah-Hartman /*
39ab4382d2SGreg Kroah-Hartman  * lockdep: port->lock is initialized in two places, but we
40ab4382d2SGreg Kroah-Hartman  *          want only one lock-class:
41ab4382d2SGreg Kroah-Hartman  */
42ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key;
43ab4382d2SGreg Kroah-Hartman 
44ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
45ab4382d2SGreg Kroah-Hartman 
460ed12afaSLino Sanfilippo /*
470ed12afaSLino Sanfilippo  * Max time with active RTS before/after data is sent.
480ed12afaSLino Sanfilippo  */
490ed12afaSLino Sanfilippo #define RS485_MAX_RTS_DELAY	100 /* msecs */
500ed12afaSLino Sanfilippo 
51ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
52ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios);
531f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
546f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
556f538fe3SLinus Walleij 			   enum uart_pm_state pm_state);
56ab4382d2SGreg Kroah-Hartman 
57b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port);
58b922e19dSJiri Slaby 
59299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport)
60299245a1SPeter Hurley {
61d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_DCD_ENABLE);
62299245a1SPeter Hurley }
63299245a1SPeter Hurley 
649ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state)
659ed19428SPeter Hurley {
669ed19428SPeter Hurley 	if (atomic_add_unless(&state->refcount, 1, 0))
679ed19428SPeter Hurley 		return state->uart_port;
689ed19428SPeter Hurley 	return NULL;
699ed19428SPeter Hurley }
709ed19428SPeter Hurley 
719ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport)
729ed19428SPeter Hurley {
73ef510beaSAndy Shevchenko 	if (atomic_dec_and_test(&uport->state->refcount))
749ed19428SPeter Hurley 		wake_up(&uport->state->remove_wait);
759ed19428SPeter Hurley }
769ed19428SPeter Hurley 
779ed19428SPeter Hurley #define uart_port_lock(state, flags)					\
789ed19428SPeter Hurley 	({								\
799ed19428SPeter Hurley 		struct uart_port *__uport = uart_port_ref(state);	\
809ed19428SPeter Hurley 		if (__uport)						\
819ed19428SPeter Hurley 			spin_lock_irqsave(&__uport->lock, flags);	\
829ed19428SPeter Hurley 		__uport;						\
839ed19428SPeter Hurley 	})
849ed19428SPeter Hurley 
859ed19428SPeter Hurley #define uart_port_unlock(uport, flags)					\
869ed19428SPeter Hurley 	({								\
879ed19428SPeter Hurley 		struct uart_port *__uport = uport;			\
88ef510beaSAndy Shevchenko 		if (__uport) {						\
899ed19428SPeter Hurley 			spin_unlock_irqrestore(&__uport->lock, flags);	\
909ed19428SPeter Hurley 			uart_port_deref(__uport);			\
91ef510beaSAndy Shevchenko 		}							\
929ed19428SPeter Hurley 	})
939ed19428SPeter Hurley 
944047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state)
954047b371SPeter Hurley {
967da4b8b7SPeter Hurley 	lockdep_assert_held(&state->port.mutex);
974047b371SPeter Hurley 	return state->uart_port;
984047b371SPeter Hurley }
994047b371SPeter Hurley 
100ab4382d2SGreg Kroah-Hartman /*
101ab4382d2SGreg Kroah-Hartman  * This routine is used by the interrupt handler to schedule processing in
102ab4382d2SGreg Kroah-Hartman  * the software interrupt portion of the driver.
103ab4382d2SGreg Kroah-Hartman  */
104ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port)
105ab4382d2SGreg Kroah-Hartman {
106ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = port->state;
107ab4382d2SGreg Kroah-Hartman 	/*
108ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
109ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
110ab4382d2SGreg Kroah-Hartman 	 */
111ab4382d2SGreg Kroah-Hartman 	BUG_ON(!state);
112d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
113ab4382d2SGreg Kroah-Hartman }
11415dc475bSJiri Slaby EXPORT_SYMBOL(uart_write_wakeup);
115ab4382d2SGreg Kroah-Hartman 
116ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty)
117ab4382d2SGreg Kroah-Hartman {
118ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1199ed19428SPeter Hurley 	struct uart_port *port;
120ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
121ab4382d2SGreg Kroah-Hartman 
1229ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
1239ed19428SPeter Hurley 	if (port)
124ab4382d2SGreg Kroah-Hartman 		port->ops->stop_tx(port);
1259ed19428SPeter Hurley 	uart_port_unlock(port, flags);
126ab4382d2SGreg Kroah-Hartman }
127ab4382d2SGreg Kroah-Hartman 
128ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty)
129ab4382d2SGreg Kroah-Hartman {
130ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
131ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
132ab4382d2SGreg Kroah-Hartman 
1339ed19428SPeter Hurley 	if (port && !uart_tx_stopped(port))
134ab4382d2SGreg Kroah-Hartman 		port->ops->start_tx(port);
135ab4382d2SGreg Kroah-Hartman }
136ab4382d2SGreg Kroah-Hartman 
137ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty)
138ab4382d2SGreg Kroah-Hartman {
139ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1409ed19428SPeter Hurley 	struct uart_port *port;
141ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
142ab4382d2SGreg Kroah-Hartman 
1439ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
144ab4382d2SGreg Kroah-Hartman 	__uart_start(tty);
1459ed19428SPeter Hurley 	uart_port_unlock(port, flags);
146ab4382d2SGreg Kroah-Hartman }
147ab4382d2SGreg Kroah-Hartman 
148f4581cabSDenys Vlasenko static void
149ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
150ab4382d2SGreg Kroah-Hartman {
151ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
152ab4382d2SGreg Kroah-Hartman 	unsigned int old;
153ab4382d2SGreg Kroah-Hartman 
1542dd8a74fSLukas Wunner 	if (port->rs485.flags & SER_RS485_ENABLED) {
1552dd8a74fSLukas Wunner 		set &= ~TIOCM_RTS;
1562dd8a74fSLukas Wunner 		clear &= ~TIOCM_RTS;
1572dd8a74fSLukas Wunner 	}
1582dd8a74fSLukas Wunner 
159ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
160ab4382d2SGreg Kroah-Hartman 	old = port->mctrl;
161ab4382d2SGreg Kroah-Hartman 	port->mctrl = (old & ~clear) | set;
162ab4382d2SGreg Kroah-Hartman 	if (old != port->mctrl)
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 
170a6845e1eSRafael Gago static void uart_port_dtr_rts(struct uart_port *uport, int raise)
171a6845e1eSRafael Gago {
1722dd8a74fSLukas Wunner 	if (raise)
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 
178ab4382d2SGreg Kroah-Hartman /*
179ab4382d2SGreg Kroah-Hartman  * Startup the port.  This will be called once per open.  All calls
180ab4382d2SGreg Kroah-Hartman  * will be serialised by the per-port mutex.
181ab4382d2SGreg Kroah-Hartman  */
182c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
183c0d92be6SJiri Slaby 		int init_hw)
184ab4382d2SGreg Kroah-Hartman {
1854047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
18618ee37e1SJohan Hovold 	unsigned long flags;
187ab4382d2SGreg Kroah-Hartman 	unsigned long page;
188ab4382d2SGreg Kroah-Hartman 	int retval = 0;
189ab4382d2SGreg Kroah-Hartman 
190ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
191c0d92be6SJiri Slaby 		return 1;
192ab4382d2SGreg Kroah-Hartman 
193ab4382d2SGreg Kroah-Hartman 	/*
1947deb39edSThomas Pfaff 	 * Make sure the device is in D0 state.
1957deb39edSThomas Pfaff 	 */
1967deb39edSThomas Pfaff 	uart_change_pm(state, UART_PM_STATE_ON);
1977deb39edSThomas Pfaff 
1987deb39edSThomas Pfaff 	/*
199ab4382d2SGreg Kroah-Hartman 	 * Initialise and allocate the transmit and temporary
200ab4382d2SGreg Kroah-Hartman 	 * buffer.
201ab4382d2SGreg Kroah-Hartman 	 */
202ab4382d2SGreg Kroah-Hartman 	page = get_zeroed_page(GFP_KERNEL);
203ab4382d2SGreg Kroah-Hartman 	if (!page)
204ab4382d2SGreg Kroah-Hartman 		return -ENOMEM;
205ab4382d2SGreg Kroah-Hartman 
206a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
207a5ba1d95STycho Andersen 	if (!state->xmit.buf) {
208ab4382d2SGreg Kroah-Hartman 		state->xmit.buf = (unsigned char *) page;
209ab4382d2SGreg Kroah-Hartman 		uart_circ_clear(&state->xmit);
210d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
211a5ba1d95STycho Andersen 	} else {
212d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
213d7240214SSergey Senozhatsky 		/*
214d7240214SSergey Senozhatsky 		 * Do not free() the page under the port lock, see
215d7240214SSergey Senozhatsky 		 * uart_shutdown().
216d7240214SSergey Senozhatsky 		 */
217a5ba1d95STycho Andersen 		free_page(page);
218ab4382d2SGreg Kroah-Hartman 	}
219ab4382d2SGreg Kroah-Hartman 
220ab4382d2SGreg Kroah-Hartman 	retval = uport->ops->startup(uport);
221ab4382d2SGreg Kroah-Hartman 	if (retval == 0) {
222c7d7abffSJiri Slaby 		if (uart_console(uport) && uport->cons->cflag) {
223adc8d746SAlan Cox 			tty->termios.c_cflag = uport->cons->cflag;
224027b5717SPali Rohár 			tty->termios.c_ispeed = uport->cons->ispeed;
225027b5717SPali Rohár 			tty->termios.c_ospeed = uport->cons->ospeed;
226c7d7abffSJiri Slaby 			uport->cons->cflag = 0;
227027b5717SPali Rohár 			uport->cons->ispeed = 0;
228027b5717SPali Rohár 			uport->cons->ospeed = 0;
229c7d7abffSJiri Slaby 		}
230ab4382d2SGreg Kroah-Hartman 		/*
231ab4382d2SGreg Kroah-Hartman 		 * Initialise the hardware port settings.
232ab4382d2SGreg Kroah-Hartman 		 */
233ab4382d2SGreg Kroah-Hartman 		uart_change_speed(tty, state, NULL);
234ab4382d2SGreg Kroah-Hartman 
235ab4382d2SGreg Kroah-Hartman 		/*
236ab4382d2SGreg Kroah-Hartman 		 * Setup the RTS and DTR signals once the
237ab4382d2SGreg Kroah-Hartman 		 * port is open and ready to respond.
238ab4382d2SGreg Kroah-Hartman 		 */
2399db276f8SPeter Hurley 		if (init_hw && C_BAUD(tty))
240a6845e1eSRafael Gago 			uart_port_dtr_rts(uport, 1);
241ab4382d2SGreg Kroah-Hartman 	}
242ab4382d2SGreg Kroah-Hartman 
2430055197eSJiri Slaby 	/*
2440055197eSJiri Slaby 	 * This is to allow setserial on this port. People may want to set
2450055197eSJiri Slaby 	 * port/irq/type and then reconfigure the port properly if it failed
2460055197eSJiri Slaby 	 * now.
2470055197eSJiri Slaby 	 */
248ab4382d2SGreg Kroah-Hartman 	if (retval && capable(CAP_SYS_ADMIN))
249c0d92be6SJiri Slaby 		return 1;
250c0d92be6SJiri Slaby 
251c0d92be6SJiri Slaby 	return retval;
252c0d92be6SJiri Slaby }
253c0d92be6SJiri Slaby 
254c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state,
255c0d92be6SJiri Slaby 		int init_hw)
256c0d92be6SJiri Slaby {
257c0d92be6SJiri Slaby 	struct tty_port *port = &state->port;
258c0d92be6SJiri Slaby 	int retval;
259c0d92be6SJiri Slaby 
260d41861caSPeter Hurley 	if (tty_port_initialized(port))
261c0d92be6SJiri Slaby 		return 0;
262c0d92be6SJiri Slaby 
263c0d92be6SJiri Slaby 	retval = uart_port_startup(tty, state, init_hw);
264b3b57646SRob Herring 	if (retval)
265b3b57646SRob Herring 		set_bit(TTY_IO_ERROR, &tty->flags);
266ab4382d2SGreg Kroah-Hartman 
267ab4382d2SGreg Kroah-Hartman 	return retval;
268ab4382d2SGreg Kroah-Hartman }
269ab4382d2SGreg Kroah-Hartman 
270ab4382d2SGreg Kroah-Hartman /*
271ab4382d2SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
272ab4382d2SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.  Calls to
273ab4382d2SGreg Kroah-Hartman  * uart_shutdown are serialised by the per-port semaphore.
274af224ca2SPeter Hurley  *
275af224ca2SPeter Hurley  * uport == NULL if uart_port has already been removed
276ab4382d2SGreg Kroah-Hartman  */
277ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
278ab4382d2SGreg Kroah-Hartman {
2794047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
280ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
28118ee37e1SJohan Hovold 	unsigned long flags;
282d7240214SSergey Senozhatsky 	char *xmit_buf = NULL;
283ab4382d2SGreg Kroah-Hartman 
284ab4382d2SGreg Kroah-Hartman 	/*
285ab4382d2SGreg Kroah-Hartman 	 * Set the TTY IO error marker
286ab4382d2SGreg Kroah-Hartman 	 */
287ab4382d2SGreg Kroah-Hartman 	if (tty)
288ab4382d2SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
289ab4382d2SGreg Kroah-Hartman 
290d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
291d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
292d41861caSPeter Hurley 
293ab4382d2SGreg Kroah-Hartman 		/*
294ab4382d2SGreg Kroah-Hartman 		 * Turn off DTR and RTS early.
295ab4382d2SGreg Kroah-Hartman 		 */
296027b5717SPali Rohár 		if (uport && uart_console(uport) && tty) {
297ae84db96SPeter Hurley 			uport->cons->cflag = tty->termios.c_cflag;
298027b5717SPali Rohár 			uport->cons->ispeed = tty->termios.c_ispeed;
299027b5717SPali Rohár 			uport->cons->ospeed = tty->termios.c_ospeed;
300027b5717SPali Rohár 		}
301ae84db96SPeter Hurley 
3029db276f8SPeter Hurley 		if (!tty || C_HUPCL(tty))
303a6845e1eSRafael Gago 			uart_port_dtr_rts(uport, 0);
304ab4382d2SGreg Kroah-Hartman 
305b922e19dSJiri Slaby 		uart_port_shutdown(port);
306ab4382d2SGreg Kroah-Hartman 	}
307ab4382d2SGreg Kroah-Hartman 
308ab4382d2SGreg Kroah-Hartman 	/*
309d208a3bfSDoug Anderson 	 * It's possible for shutdown to be called after suspend if we get
310d208a3bfSDoug Anderson 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
311d208a3bfSDoug Anderson 	 * we don't try to resume a port that has been shutdown.
312ab4382d2SGreg Kroah-Hartman 	 */
31380f02d54SPeter Hurley 	tty_port_set_suspended(port, 0);
314ab4382d2SGreg Kroah-Hartman 
315ab4382d2SGreg Kroah-Hartman 	/*
316d7240214SSergey Senozhatsky 	 * Do not free() the transmit buffer page under the port lock since
317d7240214SSergey Senozhatsky 	 * this can create various circular locking scenarios. For instance,
318d7240214SSergey Senozhatsky 	 * console driver may need to allocate/free a debug object, which
319d7240214SSergey Senozhatsky 	 * can endup in printk() recursion.
320ab4382d2SGreg Kroah-Hartman 	 */
321a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
322d7240214SSergey Senozhatsky 	xmit_buf = state->xmit.buf;
323ab4382d2SGreg Kroah-Hartman 	state->xmit.buf = NULL;
324a5ba1d95STycho Andersen 	uart_port_unlock(uport, flags);
325d7240214SSergey Senozhatsky 
326d7240214SSergey Senozhatsky 	free_page((unsigned long)xmit_buf);
327ab4382d2SGreg Kroah-Hartman }
328ab4382d2SGreg Kroah-Hartman 
329ab4382d2SGreg Kroah-Hartman /**
330*f9008285SIlpo Järvinen  *	uart_update_timeout - update per-port frame timing information.
331ab4382d2SGreg Kroah-Hartman  *	@port:  uart_port structure describing the port
332ab4382d2SGreg Kroah-Hartman  *	@cflag: termios cflag value
333ab4382d2SGreg Kroah-Hartman  *	@baud:  speed of the port
334ab4382d2SGreg Kroah-Hartman  *
335*f9008285SIlpo Järvinen  *	Set the port frame timing information from which the FIFO timeout
336*f9008285SIlpo Järvinen  *	value is derived. The @cflag value should reflect the actual hardware
337*f9008285SIlpo Järvinen  *	settings.
338ab4382d2SGreg Kroah-Hartman  */
339ab4382d2SGreg Kroah-Hartman void
340ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag,
341ab4382d2SGreg Kroah-Hartman 		    unsigned int baud)
342ab4382d2SGreg Kroah-Hartman {
34331f6bd7fSIlpo Järvinen 	unsigned int size = tty_get_frame_size(cflag);
34431f6bd7fSIlpo Järvinen 	u64 frame_time;
345ab4382d2SGreg Kroah-Hartman 
34631f6bd7fSIlpo Järvinen 	frame_time = (u64)size * NSEC_PER_SEC;
34731f6bd7fSIlpo Järvinen 	port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
348ab4382d2SGreg Kroah-Hartman }
349ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout);
350ab4382d2SGreg Kroah-Hartman 
351ab4382d2SGreg Kroah-Hartman /**
352ab4382d2SGreg Kroah-Hartman  *	uart_get_baud_rate - return baud rate for a particular port
353ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port in question.
354ab4382d2SGreg Kroah-Hartman  *	@termios: desired termios settings.
355ab4382d2SGreg Kroah-Hartman  *	@old: old termios (or NULL)
356ab4382d2SGreg Kroah-Hartman  *	@min: minimum acceptable baud rate
357ab4382d2SGreg Kroah-Hartman  *	@max: maximum acceptable baud rate
358ab4382d2SGreg Kroah-Hartman  *
359ab4382d2SGreg Kroah-Hartman  *	Decode the termios structure into a numeric baud rate,
360ab4382d2SGreg Kroah-Hartman  *	taking account of the magic 38400 baud rate (with spd_*
361ab4382d2SGreg Kroah-Hartman  *	flags), and mapping the %B0 rate to 9600 baud.
362ab4382d2SGreg Kroah-Hartman  *
363ab4382d2SGreg Kroah-Hartman  *	If the new baud rate is invalid, try the old termios setting.
364ab4382d2SGreg Kroah-Hartman  *	If it's still invalid, we try 9600 baud.
365ab4382d2SGreg Kroah-Hartman  *
366ab4382d2SGreg Kroah-Hartman  *	Update the @termios structure to reflect the baud rate
367ab4382d2SGreg Kroah-Hartman  *	we're actually going to be using. Don't do this for the case
368ab4382d2SGreg Kroah-Hartman  *	where B0 is requested ("hang up").
369ab4382d2SGreg Kroah-Hartman  */
370ab4382d2SGreg Kroah-Hartman unsigned int
371ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
372ab4382d2SGreg Kroah-Hartman 		   struct ktermios *old, unsigned int min, unsigned int max)
373ab4382d2SGreg Kroah-Hartman {
374f10a2233SJoakim Nordell 	unsigned int try;
375f10a2233SJoakim Nordell 	unsigned int baud;
376f10a2233SJoakim Nordell 	unsigned int altbaud;
377ab4382d2SGreg Kroah-Hartman 	int hung_up = 0;
378ab4382d2SGreg Kroah-Hartman 	upf_t flags = port->flags & UPF_SPD_MASK;
379ab4382d2SGreg Kroah-Hartman 
380f10a2233SJoakim Nordell 	switch (flags) {
381f10a2233SJoakim Nordell 	case UPF_SPD_HI:
382ab4382d2SGreg Kroah-Hartman 		altbaud = 57600;
383f10a2233SJoakim Nordell 		break;
384f10a2233SJoakim Nordell 	case UPF_SPD_VHI:
385ab4382d2SGreg Kroah-Hartman 		altbaud = 115200;
386f10a2233SJoakim Nordell 		break;
387f10a2233SJoakim Nordell 	case UPF_SPD_SHI:
388ab4382d2SGreg Kroah-Hartman 		altbaud = 230400;
389f10a2233SJoakim Nordell 		break;
390f10a2233SJoakim Nordell 	case UPF_SPD_WARP:
391ab4382d2SGreg Kroah-Hartman 		altbaud = 460800;
392f10a2233SJoakim Nordell 		break;
393f10a2233SJoakim Nordell 	default:
394f10a2233SJoakim Nordell 		altbaud = 38400;
395f10a2233SJoakim Nordell 		break;
396f10a2233SJoakim Nordell 	}
397ab4382d2SGreg Kroah-Hartman 
398ab4382d2SGreg Kroah-Hartman 	for (try = 0; try < 2; try++) {
399ab4382d2SGreg Kroah-Hartman 		baud = tty_termios_baud_rate(termios);
400ab4382d2SGreg Kroah-Hartman 
401ab4382d2SGreg Kroah-Hartman 		/*
402ab4382d2SGreg Kroah-Hartman 		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
403ab4382d2SGreg Kroah-Hartman 		 * Die! Die! Die!
404ab4382d2SGreg Kroah-Hartman 		 */
405547039ecSPeter Hurley 		if (try == 0 && baud == 38400)
406ab4382d2SGreg Kroah-Hartman 			baud = altbaud;
407ab4382d2SGreg Kroah-Hartman 
408ab4382d2SGreg Kroah-Hartman 		/*
409ab4382d2SGreg Kroah-Hartman 		 * Special case: B0 rate.
410ab4382d2SGreg Kroah-Hartman 		 */
411ab4382d2SGreg Kroah-Hartman 		if (baud == 0) {
412ab4382d2SGreg Kroah-Hartman 			hung_up = 1;
413ab4382d2SGreg Kroah-Hartman 			baud = 9600;
414ab4382d2SGreg Kroah-Hartman 		}
415ab4382d2SGreg Kroah-Hartman 
416ab4382d2SGreg Kroah-Hartman 		if (baud >= min && baud <= max)
417ab4382d2SGreg Kroah-Hartman 			return baud;
418ab4382d2SGreg Kroah-Hartman 
419ab4382d2SGreg Kroah-Hartman 		/*
420ab4382d2SGreg Kroah-Hartman 		 * Oops, the quotient was zero.  Try again with
421ab4382d2SGreg Kroah-Hartman 		 * the old baud rate if possible.
422ab4382d2SGreg Kroah-Hartman 		 */
423ab4382d2SGreg Kroah-Hartman 		termios->c_cflag &= ~CBAUD;
424ab4382d2SGreg Kroah-Hartman 		if (old) {
425ab4382d2SGreg Kroah-Hartman 			baud = tty_termios_baud_rate(old);
426ab4382d2SGreg Kroah-Hartman 			if (!hung_up)
427ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
428ab4382d2SGreg Kroah-Hartman 								baud, baud);
429ab4382d2SGreg Kroah-Hartman 			old = NULL;
430ab4382d2SGreg Kroah-Hartman 			continue;
431ab4382d2SGreg Kroah-Hartman 		}
432ab4382d2SGreg Kroah-Hartman 
433ab4382d2SGreg Kroah-Hartman 		/*
434ab4382d2SGreg Kroah-Hartman 		 * As a last resort, if the range cannot be met then clip to
435ab4382d2SGreg Kroah-Hartman 		 * the nearest chip supported rate.
436ab4382d2SGreg Kroah-Hartman 		 */
437ab4382d2SGreg Kroah-Hartman 		if (!hung_up) {
438ab4382d2SGreg Kroah-Hartman 			if (baud <= min)
439ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
440ab4382d2SGreg Kroah-Hartman 							min + 1, min + 1);
441ab4382d2SGreg Kroah-Hartman 			else
442ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
443ab4382d2SGreg Kroah-Hartman 							max - 1, max - 1);
444ab4382d2SGreg Kroah-Hartman 		}
445ab4382d2SGreg Kroah-Hartman 	}
446ab4382d2SGreg Kroah-Hartman 	/* Should never happen */
447ab4382d2SGreg Kroah-Hartman 	WARN_ON(1);
448ab4382d2SGreg Kroah-Hartman 	return 0;
449ab4382d2SGreg Kroah-Hartman }
450ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate);
451ab4382d2SGreg Kroah-Hartman 
452ab4382d2SGreg Kroah-Hartman /**
453ab4382d2SGreg Kroah-Hartman  *	uart_get_divisor - return uart clock divisor
454ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port.
455ab4382d2SGreg Kroah-Hartman  *	@baud: desired baud rate
456ab4382d2SGreg Kroah-Hartman  *
457ab4382d2SGreg Kroah-Hartman  *	Calculate the uart clock divisor for the port.
458ab4382d2SGreg Kroah-Hartman  */
459ab4382d2SGreg Kroah-Hartman unsigned int
460ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud)
461ab4382d2SGreg Kroah-Hartman {
462ab4382d2SGreg Kroah-Hartman 	unsigned int quot;
463ab4382d2SGreg Kroah-Hartman 
464ab4382d2SGreg Kroah-Hartman 	/*
465ab4382d2SGreg Kroah-Hartman 	 * Old custom speed handling.
466ab4382d2SGreg Kroah-Hartman 	 */
467ab4382d2SGreg Kroah-Hartman 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
468ab4382d2SGreg Kroah-Hartman 		quot = port->custom_divisor;
469ab4382d2SGreg Kroah-Hartman 	else
47097d24634SUwe Kleine-König 		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
471ab4382d2SGreg Kroah-Hartman 
472ab4382d2SGreg Kroah-Hartman 	return quot;
473ab4382d2SGreg Kroah-Hartman }
474ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor);
475ab4382d2SGreg Kroah-Hartman 
4767c8ab967SPeter Hurley /* Caller holds port mutex */
477ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
478ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios)
479ab4382d2SGreg Kroah-Hartman {
4804047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
481ab4382d2SGreg Kroah-Hartman 	struct ktermios *termios;
482391f93f2SPeter Hurley 	int hw_stopped;
483ab4382d2SGreg Kroah-Hartman 
484ab4382d2SGreg Kroah-Hartman 	/*
485ab4382d2SGreg Kroah-Hartman 	 * If we have no tty, termios, or the port does not exist,
486ab4382d2SGreg Kroah-Hartman 	 * then we can't set the parameters for this port.
487ab4382d2SGreg Kroah-Hartman 	 */
488adc8d746SAlan Cox 	if (!tty || uport->type == PORT_UNKNOWN)
489ab4382d2SGreg Kroah-Hartman 		return;
490ab4382d2SGreg Kroah-Hartman 
491adc8d746SAlan Cox 	termios = &tty->termios;
492c18b55fdSPeter Hurley 	uport->ops->set_termios(uport, termios, old_termios);
493ab4382d2SGreg Kroah-Hartman 
494ab4382d2SGreg Kroah-Hartman 	/*
495299245a1SPeter Hurley 	 * Set modem status enables based on termios cflag
496ab4382d2SGreg Kroah-Hartman 	 */
497299245a1SPeter Hurley 	spin_lock_irq(&uport->lock);
498ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
499299245a1SPeter Hurley 		uport->status |= UPSTAT_CTS_ENABLE;
500ab4382d2SGreg Kroah-Hartman 	else
501299245a1SPeter Hurley 		uport->status &= ~UPSTAT_CTS_ENABLE;
502ab4382d2SGreg Kroah-Hartman 
503ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CLOCAL)
504299245a1SPeter Hurley 		uport->status &= ~UPSTAT_DCD_ENABLE;
505ab4382d2SGreg Kroah-Hartman 	else
506299245a1SPeter Hurley 		uport->status |= UPSTAT_DCD_ENABLE;
507391f93f2SPeter Hurley 
508391f93f2SPeter Hurley 	/* reset sw-assisted CTS flow control based on (possibly) new mode */
509391f93f2SPeter Hurley 	hw_stopped = uport->hw_stopped;
510391f93f2SPeter Hurley 	uport->hw_stopped = uart_softcts_mode(uport) &&
511391f93f2SPeter Hurley 				!(uport->ops->get_mctrl(uport) & TIOCM_CTS);
512391f93f2SPeter Hurley 	if (uport->hw_stopped) {
513391f93f2SPeter Hurley 		if (!hw_stopped)
514391f93f2SPeter Hurley 			uport->ops->stop_tx(uport);
515391f93f2SPeter Hurley 	} else {
516391f93f2SPeter Hurley 		if (hw_stopped)
517391f93f2SPeter Hurley 			__uart_start(tty);
518391f93f2SPeter Hurley 	}
519299245a1SPeter Hurley 	spin_unlock_irq(&uport->lock);
520ab4382d2SGreg Kroah-Hartman }
521ab4382d2SGreg Kroah-Hartman 
522f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c)
523ab4382d2SGreg Kroah-Hartman {
524f5291eccSPeter Hurley 	struct uart_state *state = tty->driver_data;
5259ed19428SPeter Hurley 	struct uart_port *port;
526f5291eccSPeter Hurley 	struct circ_buf *circ;
527ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
528ab4382d2SGreg Kroah-Hartman 	int ret = 0;
529ab4382d2SGreg Kroah-Hartman 
530f5291eccSPeter Hurley 	circ = &state->xmit;
5319ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
532aff9cf59SSamir Virmani 	if (!circ->buf) {
533aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
534aff9cf59SSamir Virmani 		return 0;
535aff9cf59SSamir Virmani 	}
536aff9cf59SSamir Virmani 
5379ed19428SPeter Hurley 	if (port && uart_circ_chars_free(circ) != 0) {
538ab4382d2SGreg Kroah-Hartman 		circ->buf[circ->head] = c;
539ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
540ab4382d2SGreg Kroah-Hartman 		ret = 1;
541ab4382d2SGreg Kroah-Hartman 	}
5429ed19428SPeter Hurley 	uart_port_unlock(port, flags);
543ab4382d2SGreg Kroah-Hartman 	return ret;
544ab4382d2SGreg Kroah-Hartman }
545ab4382d2SGreg Kroah-Hartman 
546ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty)
547ab4382d2SGreg Kroah-Hartman {
548ab4382d2SGreg Kroah-Hartman 	uart_start(tty);
549ab4382d2SGreg Kroah-Hartman }
550ab4382d2SGreg Kroah-Hartman 
551ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty,
552ab4382d2SGreg Kroah-Hartman 					const unsigned char *buf, int count)
553ab4382d2SGreg Kroah-Hartman {
554ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
555ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
556ab4382d2SGreg Kroah-Hartman 	struct circ_buf *circ;
557ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
558ab4382d2SGreg Kroah-Hartman 	int c, ret = 0;
559ab4382d2SGreg Kroah-Hartman 
560ab4382d2SGreg Kroah-Hartman 	/*
561ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
562ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
563ab4382d2SGreg Kroah-Hartman 	 */
564ab4382d2SGreg Kroah-Hartman 	if (!state) {
565ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
566ab4382d2SGreg Kroah-Hartman 		return -EL3HLT;
567ab4382d2SGreg Kroah-Hartman 	}
568ab4382d2SGreg Kroah-Hartman 
5699ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
570aff9cf59SSamir Virmani 	circ = &state->xmit;
571aff9cf59SSamir Virmani 	if (!circ->buf) {
572aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
573aff9cf59SSamir Virmani 		return 0;
574aff9cf59SSamir Virmani 	}
575aff9cf59SSamir Virmani 
5769ed19428SPeter Hurley 	while (port) {
577ab4382d2SGreg Kroah-Hartman 		c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
578ab4382d2SGreg Kroah-Hartman 		if (count < c)
579ab4382d2SGreg Kroah-Hartman 			c = count;
580ab4382d2SGreg Kroah-Hartman 		if (c <= 0)
581ab4382d2SGreg Kroah-Hartman 			break;
582ab4382d2SGreg Kroah-Hartman 		memcpy(circ->buf + circ->head, buf, c);
583ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
584ab4382d2SGreg Kroah-Hartman 		buf += c;
585ab4382d2SGreg Kroah-Hartman 		count -= c;
586ab4382d2SGreg Kroah-Hartman 		ret += c;
587ab4382d2SGreg Kroah-Hartman 	}
58864dbee31SPeter Hurley 
58964dbee31SPeter Hurley 	__uart_start(tty);
5909ed19428SPeter Hurley 	uart_port_unlock(port, flags);
591ab4382d2SGreg Kroah-Hartman 	return ret;
592ab4382d2SGreg Kroah-Hartman }
593ab4382d2SGreg Kroah-Hartman 
59403b3b1a2SJiri Slaby static unsigned int uart_write_room(struct tty_struct *tty)
595ab4382d2SGreg Kroah-Hartman {
596ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
5979ed19428SPeter Hurley 	struct uart_port *port;
598ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
59903b3b1a2SJiri Slaby 	unsigned int ret;
600ab4382d2SGreg Kroah-Hartman 
6019ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
602ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_free(&state->xmit);
6039ed19428SPeter Hurley 	uart_port_unlock(port, flags);
604ab4382d2SGreg Kroah-Hartman 	return ret;
605ab4382d2SGreg Kroah-Hartman }
606ab4382d2SGreg Kroah-Hartman 
607fff4ef17SJiri Slaby static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
608ab4382d2SGreg Kroah-Hartman {
609ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6109ed19428SPeter Hurley 	struct uart_port *port;
611ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
612fff4ef17SJiri Slaby 	unsigned int ret;
613ab4382d2SGreg Kroah-Hartman 
6149ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
615ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_pending(&state->xmit);
6169ed19428SPeter Hurley 	uart_port_unlock(port, flags);
617ab4382d2SGreg Kroah-Hartman 	return ret;
618ab4382d2SGreg Kroah-Hartman }
619ab4382d2SGreg Kroah-Hartman 
620ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty)
621ab4382d2SGreg Kroah-Hartman {
622ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
623ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
624ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
625ab4382d2SGreg Kroah-Hartman 
626ab4382d2SGreg Kroah-Hartman 	/*
627ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
628ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
629ab4382d2SGreg Kroah-Hartman 	 */
630ab4382d2SGreg Kroah-Hartman 	if (!state) {
631ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
632ab4382d2SGreg Kroah-Hartman 		return;
633ab4382d2SGreg Kroah-Hartman 	}
634ab4382d2SGreg Kroah-Hartman 
635ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
636ab4382d2SGreg Kroah-Hartman 
6379ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6389ed19428SPeter Hurley 	if (!port)
6399ed19428SPeter Hurley 		return;
640ab4382d2SGreg Kroah-Hartman 	uart_circ_clear(&state->xmit);
641ab4382d2SGreg Kroah-Hartman 	if (port->ops->flush_buffer)
642ab4382d2SGreg Kroah-Hartman 		port->ops->flush_buffer(port);
6439ed19428SPeter Hurley 	uart_port_unlock(port, flags);
644d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
645ab4382d2SGreg Kroah-Hartman }
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman /*
648f58c252eSIlpo Järvinen  * This function performs low-level write of high-priority XON/XOFF
649f58c252eSIlpo Järvinen  * character and accounting for it.
650f58c252eSIlpo Järvinen  *
651f58c252eSIlpo Järvinen  * Requires uart_port to implement .serial_out().
652f58c252eSIlpo Järvinen  */
653f58c252eSIlpo Järvinen void uart_xchar_out(struct uart_port *uport, int offset)
654f58c252eSIlpo Järvinen {
655f58c252eSIlpo Järvinen 	serial_port_out(uport, offset, uport->x_char);
656f58c252eSIlpo Järvinen 	uport->icount.tx++;
657f58c252eSIlpo Järvinen 	uport->x_char = 0;
658f58c252eSIlpo Järvinen }
659f58c252eSIlpo Järvinen EXPORT_SYMBOL_GPL(uart_xchar_out);
660f58c252eSIlpo Järvinen 
661f58c252eSIlpo Järvinen /*
662ab4382d2SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
663ab4382d2SGreg Kroah-Hartman  * the device
664ab4382d2SGreg Kroah-Hartman  */
665ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch)
666ab4382d2SGreg Kroah-Hartman {
667ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6689ed19428SPeter Hurley 	struct uart_port *port;
669ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
670ab4382d2SGreg Kroah-Hartman 
6719ed19428SPeter Hurley 	port = uart_port_ref(state);
6729ed19428SPeter Hurley 	if (!port)
6739ed19428SPeter Hurley 		return;
6749ed19428SPeter Hurley 
675ab4382d2SGreg Kroah-Hartman 	if (port->ops->send_xchar)
676ab4382d2SGreg Kroah-Hartman 		port->ops->send_xchar(port, ch);
677ab4382d2SGreg Kroah-Hartman 	else {
678ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
679c235ccc1SPeter Hurley 		port->x_char = ch;
680c235ccc1SPeter Hurley 		if (ch)
681ab4382d2SGreg Kroah-Hartman 			port->ops->start_tx(port);
682ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
683ab4382d2SGreg Kroah-Hartman 	}
6849ed19428SPeter Hurley 	uart_port_deref(port);
685ab4382d2SGreg Kroah-Hartman }
686ab4382d2SGreg Kroah-Hartman 
687ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty)
688ab4382d2SGreg Kroah-Hartman {
689ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
690c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
6919ed19428SPeter Hurley 	struct uart_port *port;
692ab4382d2SGreg Kroah-Hartman 
6939ed19428SPeter Hurley 	port = uart_port_ref(state);
6949ed19428SPeter Hurley 	if (!port)
6959ed19428SPeter Hurley 		return;
6969ed19428SPeter Hurley 
697ab4382d2SGreg Kroah-Hartman 	if (I_IXOFF(tty))
698391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
6999db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
700391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7019aba8d5bSRussell King 
702391f93f2SPeter Hurley 	if (port->status & mask) {
7039aba8d5bSRussell King 		port->ops->throttle(port);
704391f93f2SPeter Hurley 		mask &= ~port->status;
7059aba8d5bSRussell King 	}
7069aba8d5bSRussell King 
707391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
7089aba8d5bSRussell King 		uart_clear_mctrl(port, TIOCM_RTS);
709b4749b97SPeter Hurley 
710b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
711b4749b97SPeter Hurley 		uart_send_xchar(tty, STOP_CHAR(tty));
7129ed19428SPeter Hurley 
7139ed19428SPeter Hurley 	uart_port_deref(port);
714ab4382d2SGreg Kroah-Hartman }
715ab4382d2SGreg Kroah-Hartman 
716ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty)
717ab4382d2SGreg Kroah-Hartman {
718ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
719c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7209ed19428SPeter Hurley 	struct uart_port *port;
721ab4382d2SGreg Kroah-Hartman 
7229ed19428SPeter Hurley 	port = uart_port_ref(state);
7239ed19428SPeter Hurley 	if (!port)
7249ed19428SPeter Hurley 		return;
7259ed19428SPeter Hurley 
7269aba8d5bSRussell King 	if (I_IXOFF(tty))
727391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7289db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
729391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7309aba8d5bSRussell King 
731391f93f2SPeter Hurley 	if (port->status & mask) {
7329aba8d5bSRussell King 		port->ops->unthrottle(port);
733391f93f2SPeter Hurley 		mask &= ~port->status;
7349aba8d5bSRussell King 	}
7359aba8d5bSRussell King 
736391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
737ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(port, TIOCM_RTS);
738b4749b97SPeter Hurley 
739b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
740b4749b97SPeter Hurley 		uart_send_xchar(tty, START_CHAR(tty));
7419ed19428SPeter Hurley 
7429ed19428SPeter Hurley 	uart_port_deref(port);
743ab4382d2SGreg Kroah-Hartman }
744ab4382d2SGreg Kroah-Hartman 
7454047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
746ab4382d2SGreg Kroah-Hartman {
7479f109694SAlan Cox 	struct uart_state *state = container_of(port, struct uart_state, port);
7484047b371SPeter Hurley 	struct uart_port *uport;
7494047b371SPeter Hurley 	int ret = -ENODEV;
7507ba2e769SAlan Cox 
7513abe8c76SPeter Hurley 	/*
7523abe8c76SPeter Hurley 	 * Ensure the state we copy is consistent and no hardware changes
7533abe8c76SPeter Hurley 	 * occur as we go
7543abe8c76SPeter Hurley 	 */
7553abe8c76SPeter Hurley 	mutex_lock(&port->mutex);
7564047b371SPeter Hurley 	uport = uart_port_check(state);
7574047b371SPeter Hurley 	if (!uport)
7584047b371SPeter Hurley 		goto out;
7594047b371SPeter Hurley 
7607ba2e769SAlan Cox 	retinfo->type	    = uport->type;
7617ba2e769SAlan Cox 	retinfo->line	    = uport->line;
7627ba2e769SAlan Cox 	retinfo->port	    = uport->iobase;
7637ba2e769SAlan Cox 	if (HIGH_BITS_OFFSET)
7647ba2e769SAlan Cox 		retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
7657ba2e769SAlan Cox 	retinfo->irq		    = uport->irq;
766a17e74c5SAndy Shevchenko 	retinfo->flags	    = (__force int)uport->flags;
7677ba2e769SAlan Cox 	retinfo->xmit_fifo_size  = uport->fifosize;
7687ba2e769SAlan Cox 	retinfo->baud_base	    = uport->uartclk / 16;
7697ba2e769SAlan Cox 	retinfo->close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
7707ba2e769SAlan Cox 	retinfo->closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
7717ba2e769SAlan Cox 				ASYNC_CLOSING_WAIT_NONE :
7727ba2e769SAlan Cox 				jiffies_to_msecs(port->closing_wait) / 10;
7737ba2e769SAlan Cox 	retinfo->custom_divisor  = uport->custom_divisor;
7747ba2e769SAlan Cox 	retinfo->hub6	    = uport->hub6;
7757ba2e769SAlan Cox 	retinfo->io_type         = uport->iotype;
7767ba2e769SAlan Cox 	retinfo->iomem_reg_shift = uport->regshift;
7777ba2e769SAlan Cox 	retinfo->iomem_base      = (void *)(unsigned long)uport->mapbase;
7784047b371SPeter Hurley 
7794047b371SPeter Hurley 	ret = 0;
7804047b371SPeter Hurley out:
781ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
7824047b371SPeter Hurley 	return ret;
7839f109694SAlan Cox }
7849f109694SAlan Cox 
7855099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty,
7865099d234SAl Viro 			 struct serial_struct *ss)
7879f109694SAlan Cox {
7885099d234SAl Viro 	struct uart_state *state = tty->driver_data;
7895099d234SAl Viro 	struct tty_port *port = &state->port;
7903abe8c76SPeter Hurley 
7915099d234SAl Viro 	return uart_get_info(port, ss) < 0 ? -EIO : 0;
792ab4382d2SGreg Kroah-Hartman }
793ab4382d2SGreg Kroah-Hartman 
7947ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
7957ba2e769SAlan Cox 			 struct uart_state *state,
7967ba2e769SAlan Cox 			 struct serial_struct *new_info)
797ab4382d2SGreg Kroah-Hartman {
7984047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
799ab4382d2SGreg Kroah-Hartman 	unsigned long new_port;
800ab4382d2SGreg Kroah-Hartman 	unsigned int change_irq, change_port, closing_wait;
801ab4382d2SGreg Kroah-Hartman 	unsigned int old_custom_divisor, close_delay;
802ab4382d2SGreg Kroah-Hartman 	upf_t old_flags, new_flags;
803ab4382d2SGreg Kroah-Hartman 	int retval = 0;
804ab4382d2SGreg Kroah-Hartman 
8054047b371SPeter Hurley 	if (!uport)
8064047b371SPeter Hurley 		return -EIO;
8074047b371SPeter Hurley 
8087ba2e769SAlan Cox 	new_port = new_info->port;
809ab4382d2SGreg Kroah-Hartman 	if (HIGH_BITS_OFFSET)
8107ba2e769SAlan Cox 		new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
811ab4382d2SGreg Kroah-Hartman 
8127ba2e769SAlan Cox 	new_info->irq = irq_canonicalize(new_info->irq);
8137ba2e769SAlan Cox 	close_delay = msecs_to_jiffies(new_info->close_delay * 10);
8147ba2e769SAlan Cox 	closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
8154cb0fbfdSJiri Slaby 			ASYNC_CLOSING_WAIT_NONE :
8167ba2e769SAlan Cox 			msecs_to_jiffies(new_info->closing_wait * 10);
817ab4382d2SGreg Kroah-Hartman 
818ab4382d2SGreg Kroah-Hartman 
819ab4382d2SGreg Kroah-Hartman 	change_irq  = !(uport->flags & UPF_FIXED_PORT)
8207ba2e769SAlan Cox 		&& new_info->irq != uport->irq;
821ab4382d2SGreg Kroah-Hartman 
822ab4382d2SGreg Kroah-Hartman 	/*
823ab4382d2SGreg Kroah-Hartman 	 * Since changing the 'type' of the port changes its resource
824ab4382d2SGreg Kroah-Hartman 	 * allocations, we should treat type changes the same as
825ab4382d2SGreg Kroah-Hartman 	 * IO port changes.
826ab4382d2SGreg Kroah-Hartman 	 */
827ab4382d2SGreg Kroah-Hartman 	change_port = !(uport->flags & UPF_FIXED_PORT)
828ab4382d2SGreg Kroah-Hartman 		&& (new_port != uport->iobase ||
8297ba2e769SAlan Cox 		    (unsigned long)new_info->iomem_base != uport->mapbase ||
8307ba2e769SAlan Cox 		    new_info->hub6 != uport->hub6 ||
8317ba2e769SAlan Cox 		    new_info->io_type != uport->iotype ||
8327ba2e769SAlan Cox 		    new_info->iomem_reg_shift != uport->regshift ||
8337ba2e769SAlan Cox 		    new_info->type != uport->type);
834ab4382d2SGreg Kroah-Hartman 
835ab4382d2SGreg Kroah-Hartman 	old_flags = uport->flags;
836a17e74c5SAndy Shevchenko 	new_flags = (__force upf_t)new_info->flags;
837ab4382d2SGreg Kroah-Hartman 	old_custom_divisor = uport->custom_divisor;
838ab4382d2SGreg Kroah-Hartman 
839ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
840ab4382d2SGreg Kroah-Hartman 		retval = -EPERM;
841ab4382d2SGreg Kroah-Hartman 		if (change_irq || change_port ||
8427ba2e769SAlan Cox 		    (new_info->baud_base != uport->uartclk / 16) ||
843ab4382d2SGreg Kroah-Hartman 		    (close_delay != port->close_delay) ||
844ab4382d2SGreg Kroah-Hartman 		    (closing_wait != port->closing_wait) ||
8457ba2e769SAlan Cox 		    (new_info->xmit_fifo_size &&
8467ba2e769SAlan Cox 		     new_info->xmit_fifo_size != uport->fifosize) ||
847ab4382d2SGreg Kroah-Hartman 		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
848ab4382d2SGreg Kroah-Hartman 			goto exit;
849ab4382d2SGreg Kroah-Hartman 		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
850ab4382d2SGreg Kroah-Hartman 			       (new_flags & UPF_USR_MASK));
8517ba2e769SAlan Cox 		uport->custom_divisor = new_info->custom_divisor;
852ab4382d2SGreg Kroah-Hartman 		goto check_and_exit;
853ab4382d2SGreg Kroah-Hartman 	}
854ab4382d2SGreg Kroah-Hartman 
8555e722b21SOndrej Mosnacek 	if (change_irq || change_port) {
856794edf30SDavid Howells 		retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
8575e722b21SOndrej Mosnacek 		if (retval)
858794edf30SDavid Howells 			goto exit;
8595e722b21SOndrej Mosnacek 	}
860794edf30SDavid Howells 
861ab4382d2SGreg Kroah-Hartman 	/*
862ab4382d2SGreg Kroah-Hartman 	 * Ask the low level driver to verify the settings.
863ab4382d2SGreg Kroah-Hartman 	 */
864ab4382d2SGreg Kroah-Hartman 	if (uport->ops->verify_port)
8657ba2e769SAlan Cox 		retval = uport->ops->verify_port(uport, new_info);
866ab4382d2SGreg Kroah-Hartman 
8677ba2e769SAlan Cox 	if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
8687ba2e769SAlan Cox 	    (new_info->baud_base < 9600))
869ab4382d2SGreg Kroah-Hartman 		retval = -EINVAL;
870ab4382d2SGreg Kroah-Hartman 
871ab4382d2SGreg Kroah-Hartman 	if (retval)
872ab4382d2SGreg Kroah-Hartman 		goto exit;
873ab4382d2SGreg Kroah-Hartman 
874ab4382d2SGreg Kroah-Hartman 	if (change_port || change_irq) {
875ab4382d2SGreg Kroah-Hartman 		retval = -EBUSY;
876ab4382d2SGreg Kroah-Hartman 
877ab4382d2SGreg Kroah-Hartman 		/*
878ab4382d2SGreg Kroah-Hartman 		 * Make sure that we are the sole user of this port.
879ab4382d2SGreg Kroah-Hartman 		 */
880ab4382d2SGreg Kroah-Hartman 		if (tty_port_users(port) > 1)
881ab4382d2SGreg Kroah-Hartman 			goto exit;
882ab4382d2SGreg Kroah-Hartman 
883ab4382d2SGreg Kroah-Hartman 		/*
884ab4382d2SGreg Kroah-Hartman 		 * We need to shutdown the serial port at the old
885ab4382d2SGreg Kroah-Hartman 		 * port/type/irq combination.
886ab4382d2SGreg Kroah-Hartman 		 */
887ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
888ab4382d2SGreg Kroah-Hartman 	}
889ab4382d2SGreg Kroah-Hartman 
890ab4382d2SGreg Kroah-Hartman 	if (change_port) {
891ab4382d2SGreg Kroah-Hartman 		unsigned long old_iobase, old_mapbase;
892ab4382d2SGreg Kroah-Hartman 		unsigned int old_type, old_iotype, old_hub6, old_shift;
893ab4382d2SGreg Kroah-Hartman 
894ab4382d2SGreg Kroah-Hartman 		old_iobase = uport->iobase;
895ab4382d2SGreg Kroah-Hartman 		old_mapbase = uport->mapbase;
896ab4382d2SGreg Kroah-Hartman 		old_type = uport->type;
897ab4382d2SGreg Kroah-Hartman 		old_hub6 = uport->hub6;
898ab4382d2SGreg Kroah-Hartman 		old_iotype = uport->iotype;
899ab4382d2SGreg Kroah-Hartman 		old_shift = uport->regshift;
900ab4382d2SGreg Kroah-Hartman 
901ab4382d2SGreg Kroah-Hartman 		/*
902ab4382d2SGreg Kroah-Hartman 		 * Free and release old regions
903ab4382d2SGreg Kroah-Hartman 		 */
904a7cfaf16SFabio Estevam 		if (old_type != PORT_UNKNOWN && uport->ops->release_port)
905ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
906ab4382d2SGreg Kroah-Hartman 
907ab4382d2SGreg Kroah-Hartman 		uport->iobase = new_port;
9087ba2e769SAlan Cox 		uport->type = new_info->type;
9097ba2e769SAlan Cox 		uport->hub6 = new_info->hub6;
9107ba2e769SAlan Cox 		uport->iotype = new_info->io_type;
9117ba2e769SAlan Cox 		uport->regshift = new_info->iomem_reg_shift;
9127ba2e769SAlan Cox 		uport->mapbase = (unsigned long)new_info->iomem_base;
913ab4382d2SGreg Kroah-Hartman 
914ab4382d2SGreg Kroah-Hartman 		/*
915ab4382d2SGreg Kroah-Hartman 		 * Claim and map the new regions
916ab4382d2SGreg Kroah-Hartman 		 */
917a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
918ab4382d2SGreg Kroah-Hartman 			retval = uport->ops->request_port(uport);
919ab4382d2SGreg Kroah-Hartman 		} else {
920ab4382d2SGreg Kroah-Hartman 			/* Always success - Jean II */
921ab4382d2SGreg Kroah-Hartman 			retval = 0;
922ab4382d2SGreg Kroah-Hartman 		}
923ab4382d2SGreg Kroah-Hartman 
924ab4382d2SGreg Kroah-Hartman 		/*
925ab4382d2SGreg Kroah-Hartman 		 * If we fail to request resources for the
926ab4382d2SGreg Kroah-Hartman 		 * new port, try to restore the old settings.
927ab4382d2SGreg Kroah-Hartman 		 */
9287deb39edSThomas Pfaff 		if (retval) {
929ab4382d2SGreg Kroah-Hartman 			uport->iobase = old_iobase;
930ab4382d2SGreg Kroah-Hartman 			uport->type = old_type;
931ab4382d2SGreg Kroah-Hartman 			uport->hub6 = old_hub6;
932ab4382d2SGreg Kroah-Hartman 			uport->iotype = old_iotype;
933ab4382d2SGreg Kroah-Hartman 			uport->regshift = old_shift;
934ab4382d2SGreg Kroah-Hartman 			uport->mapbase = old_mapbase;
9357deb39edSThomas Pfaff 
9367deb39edSThomas Pfaff 			if (old_type != PORT_UNKNOWN) {
937ab4382d2SGreg Kroah-Hartman 				retval = uport->ops->request_port(uport);
938ab4382d2SGreg Kroah-Hartman 				/*
939ab4382d2SGreg Kroah-Hartman 				 * If we failed to restore the old settings,
940ab4382d2SGreg Kroah-Hartman 				 * we fail like this.
941ab4382d2SGreg Kroah-Hartman 				 */
942ab4382d2SGreg Kroah-Hartman 				if (retval)
943ab4382d2SGreg Kroah-Hartman 					uport->type = PORT_UNKNOWN;
944ab4382d2SGreg Kroah-Hartman 
945ab4382d2SGreg Kroah-Hartman 				/*
946ab4382d2SGreg Kroah-Hartman 				 * We failed anyway.
947ab4382d2SGreg Kroah-Hartman 				 */
948ab4382d2SGreg Kroah-Hartman 				retval = -EBUSY;
9497deb39edSThomas Pfaff 			}
9507deb39edSThomas Pfaff 
951ab4382d2SGreg Kroah-Hartman 			/* Added to return the correct error -Ram Gupta */
952ab4382d2SGreg Kroah-Hartman 			goto exit;
953ab4382d2SGreg Kroah-Hartman 		}
954ab4382d2SGreg Kroah-Hartman 	}
955ab4382d2SGreg Kroah-Hartman 
956ab4382d2SGreg Kroah-Hartman 	if (change_irq)
9577ba2e769SAlan Cox 		uport->irq      = new_info->irq;
958ab4382d2SGreg Kroah-Hartman 	if (!(uport->flags & UPF_FIXED_PORT))
9597ba2e769SAlan Cox 		uport->uartclk  = new_info->baud_base * 16;
960ab4382d2SGreg Kroah-Hartman 	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
961ab4382d2SGreg Kroah-Hartman 				 (new_flags & UPF_CHANGE_MASK);
9627ba2e769SAlan Cox 	uport->custom_divisor   = new_info->custom_divisor;
963ab4382d2SGreg Kroah-Hartman 	port->close_delay     = close_delay;
964ab4382d2SGreg Kroah-Hartman 	port->closing_wait    = closing_wait;
9657ba2e769SAlan Cox 	if (new_info->xmit_fifo_size)
9667ba2e769SAlan Cox 		uport->fifosize = new_info->xmit_fifo_size;
967ab4382d2SGreg Kroah-Hartman 
968ab4382d2SGreg Kroah-Hartman  check_and_exit:
969ab4382d2SGreg Kroah-Hartman 	retval = 0;
970ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
971ab4382d2SGreg Kroah-Hartman 		goto exit;
972d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
973ab4382d2SGreg Kroah-Hartman 		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
974ab4382d2SGreg Kroah-Hartman 		    old_custom_divisor != uport->custom_divisor) {
975ab4382d2SGreg Kroah-Hartman 			/*
976ab4382d2SGreg Kroah-Hartman 			 * If they're setting up a custom divisor or speed,
977dc2f7271SJohan Hovold 			 * instead of clearing it, then bitch about it.
978ab4382d2SGreg Kroah-Hartman 			 */
979ab4382d2SGreg Kroah-Hartman 			if (uport->flags & UPF_SPD_MASK) {
980dc2f7271SJohan Hovold 				dev_notice_ratelimited(uport->dev,
9812f2dafe7SSudip Mukherjee 				       "%s sets custom speed on %s. This is deprecated.\n",
9822f2dafe7SSudip Mukherjee 				      current->comm,
983429b4749SRasmus Villemoes 				      tty_name(port->tty));
984ab4382d2SGreg Kroah-Hartman 			}
985ab4382d2SGreg Kroah-Hartman 			uart_change_speed(tty, state, NULL);
986ab4382d2SGreg Kroah-Hartman 		}
987b3b57646SRob Herring 	} else {
988ab4382d2SGreg Kroah-Hartman 		retval = uart_startup(tty, state, 1);
98944117a1dSSebastian Andrzej Siewior 		if (retval == 0)
99044117a1dSSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
991b3b57646SRob Herring 		if (retval > 0)
992b3b57646SRob Herring 			retval = 0;
993b3b57646SRob Herring 	}
994ab4382d2SGreg Kroah-Hartman  exit:
9957ba2e769SAlan Cox 	return retval;
9967ba2e769SAlan Cox }
9977ba2e769SAlan Cox 
9985099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
9997ba2e769SAlan Cox {
10005099d234SAl Viro 	struct uart_state *state = tty->driver_data;
10017ba2e769SAlan Cox 	struct tty_port *port = &state->port;
10027ba2e769SAlan Cox 	int retval;
10037ba2e769SAlan Cox 
10045099d234SAl Viro 	down_write(&tty->termios_rwsem);
10057ba2e769SAlan Cox 	/*
10067ba2e769SAlan Cox 	 * This semaphore protects port->count.  It is also
10077ba2e769SAlan Cox 	 * very useful to prevent opens.  Also, take the
10087ba2e769SAlan Cox 	 * port configuration semaphore to make sure that a
10097ba2e769SAlan Cox 	 * module insertion/removal doesn't change anything
10107ba2e769SAlan Cox 	 * under us.
10117ba2e769SAlan Cox 	 */
10127ba2e769SAlan Cox 	mutex_lock(&port->mutex);
10135099d234SAl Viro 	retval = uart_set_info(tty, port, state, ss);
1014ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
10155099d234SAl Viro 	up_write(&tty->termios_rwsem);
1016ab4382d2SGreg Kroah-Hartman 	return retval;
1017ab4382d2SGreg Kroah-Hartman }
1018ab4382d2SGreg Kroah-Hartman 
1019ab4382d2SGreg Kroah-Hartman /**
1020ab4382d2SGreg Kroah-Hartman  *	uart_get_lsr_info	-	get line status register info
1021ab4382d2SGreg Kroah-Hartman  *	@tty: tty associated with the UART
1022ab4382d2SGreg Kroah-Hartman  *	@state: UART being queried
1023ab4382d2SGreg Kroah-Hartman  *	@value: returned modem value
1024ab4382d2SGreg Kroah-Hartman  */
1025ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty,
1026ab4382d2SGreg Kroah-Hartman 			struct uart_state *state, unsigned int __user *value)
1027ab4382d2SGreg Kroah-Hartman {
10284047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1029ab4382d2SGreg Kroah-Hartman 	unsigned int result;
1030ab4382d2SGreg Kroah-Hartman 
1031ab4382d2SGreg Kroah-Hartman 	result = uport->ops->tx_empty(uport);
1032ab4382d2SGreg Kroah-Hartman 
1033ab4382d2SGreg Kroah-Hartman 	/*
1034ab4382d2SGreg Kroah-Hartman 	 * If we're about to load something into the transmit
1035ab4382d2SGreg Kroah-Hartman 	 * register, we'll pretend the transmitter isn't empty to
1036ab4382d2SGreg Kroah-Hartman 	 * avoid a race condition (depending on when the transmit
1037ab4382d2SGreg Kroah-Hartman 	 * interrupt happens).
1038ab4382d2SGreg Kroah-Hartman 	 */
1039ab4382d2SGreg Kroah-Hartman 	if (uport->x_char ||
1040ab4382d2SGreg Kroah-Hartman 	    ((uart_circ_chars_pending(&state->xmit) > 0) &&
1041d01f4d18SPeter Hurley 	     !uart_tx_stopped(uport)))
1042ab4382d2SGreg Kroah-Hartman 		result &= ~TIOCSER_TEMT;
1043ab4382d2SGreg Kroah-Hartman 
1044ab4382d2SGreg Kroah-Hartman 	return put_user(result, value);
1045ab4382d2SGreg Kroah-Hartman }
1046ab4382d2SGreg Kroah-Hartman 
104760b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty)
1048ab4382d2SGreg Kroah-Hartman {
1049ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1050ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10514047b371SPeter Hurley 	struct uart_port *uport;
1052ab4382d2SGreg Kroah-Hartman 	int result = -EIO;
1053ab4382d2SGreg Kroah-Hartman 
1054ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10554047b371SPeter Hurley 	uport = uart_port_check(state);
10564047b371SPeter Hurley 	if (!uport)
10574047b371SPeter Hurley 		goto out;
10584047b371SPeter Hurley 
105918900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1060ab4382d2SGreg Kroah-Hartman 		result = uport->mctrl;
1061ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1062ab4382d2SGreg Kroah-Hartman 		result |= uport->ops->get_mctrl(uport);
1063ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1064ab4382d2SGreg Kroah-Hartman 	}
10654047b371SPeter Hurley out:
1066ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1067ab4382d2SGreg Kroah-Hartman 	return result;
1068ab4382d2SGreg Kroah-Hartman }
1069ab4382d2SGreg Kroah-Hartman 
1070ab4382d2SGreg Kroah-Hartman static int
107120b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1072ab4382d2SGreg Kroah-Hartman {
1073ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1074ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10754047b371SPeter Hurley 	struct uart_port *uport;
1076ab4382d2SGreg Kroah-Hartman 	int ret = -EIO;
1077ab4382d2SGreg Kroah-Hartman 
1078ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10794047b371SPeter Hurley 	uport = uart_port_check(state);
10804047b371SPeter Hurley 	if (!uport)
10814047b371SPeter Hurley 		goto out;
10824047b371SPeter Hurley 
108318900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1084ab4382d2SGreg Kroah-Hartman 		uart_update_mctrl(uport, set, clear);
1085ab4382d2SGreg Kroah-Hartman 		ret = 0;
1086ab4382d2SGreg Kroah-Hartman 	}
10874047b371SPeter Hurley out:
1088ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1089ab4382d2SGreg Kroah-Hartman 	return ret;
1090ab4382d2SGreg Kroah-Hartman }
1091ab4382d2SGreg Kroah-Hartman 
1092ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state)
1093ab4382d2SGreg Kroah-Hartman {
1094ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1095ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10964047b371SPeter Hurley 	struct uart_port *uport;
10974047b371SPeter Hurley 	int ret = -EIO;
1098ab4382d2SGreg Kroah-Hartman 
1099ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
11004047b371SPeter Hurley 	uport = uart_port_check(state);
11014047b371SPeter Hurley 	if (!uport)
11024047b371SPeter Hurley 		goto out;
1103ab4382d2SGreg Kroah-Hartman 
11047d73170eSJiangfeng Xiao 	if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1105ab4382d2SGreg Kroah-Hartman 		uport->ops->break_ctl(uport, break_state);
11064047b371SPeter Hurley 	ret = 0;
11074047b371SPeter Hurley out:
1108ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
11094047b371SPeter Hurley 	return ret;
1110ab4382d2SGreg Kroah-Hartman }
1111ab4382d2SGreg Kroah-Hartman 
1112ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1113ab4382d2SGreg Kroah-Hartman {
1114ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11154047b371SPeter Hurley 	struct uart_port *uport;
1116ab4382d2SGreg Kroah-Hartman 	int flags, ret;
1117ab4382d2SGreg Kroah-Hartman 
1118ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
1119ab4382d2SGreg Kroah-Hartman 		return -EPERM;
1120ab4382d2SGreg Kroah-Hartman 
1121ab4382d2SGreg Kroah-Hartman 	/*
1122ab4382d2SGreg Kroah-Hartman 	 * Take the per-port semaphore.  This prevents count from
1123ab4382d2SGreg Kroah-Hartman 	 * changing, and hence any extra opens of the port while
1124ab4382d2SGreg Kroah-Hartman 	 * we're auto-configuring.
1125ab4382d2SGreg Kroah-Hartman 	 */
1126ab4382d2SGreg Kroah-Hartman 	if (mutex_lock_interruptible(&port->mutex))
1127ab4382d2SGreg Kroah-Hartman 		return -ERESTARTSYS;
1128ab4382d2SGreg Kroah-Hartman 
11294047b371SPeter Hurley 	uport = uart_port_check(state);
11304047b371SPeter Hurley 	if (!uport) {
11314047b371SPeter Hurley 		ret = -EIO;
11324047b371SPeter Hurley 		goto out;
11334047b371SPeter Hurley 	}
11344047b371SPeter Hurley 
1135ab4382d2SGreg Kroah-Hartman 	ret = -EBUSY;
1136ab4382d2SGreg Kroah-Hartman 	if (tty_port_users(port) == 1) {
1137ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1138ab4382d2SGreg Kroah-Hartman 
1139ab4382d2SGreg Kroah-Hartman 		/*
1140ab4382d2SGreg Kroah-Hartman 		 * If we already have a port type configured,
1141ab4382d2SGreg Kroah-Hartman 		 * we must release its resources.
1142ab4382d2SGreg Kroah-Hartman 		 */
1143a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1144ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
1145ab4382d2SGreg Kroah-Hartman 
1146ab4382d2SGreg Kroah-Hartman 		flags = UART_CONFIG_TYPE;
1147ab4382d2SGreg Kroah-Hartman 		if (uport->flags & UPF_AUTO_IRQ)
1148ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_IRQ;
1149ab4382d2SGreg Kroah-Hartman 
1150ab4382d2SGreg Kroah-Hartman 		/*
1151ab4382d2SGreg Kroah-Hartman 		 * This will claim the ports resources if
1152ab4382d2SGreg Kroah-Hartman 		 * a port is found.
1153ab4382d2SGreg Kroah-Hartman 		 */
1154ab4382d2SGreg Kroah-Hartman 		uport->ops->config_port(uport, flags);
1155ab4382d2SGreg Kroah-Hartman 
1156ab4382d2SGreg Kroah-Hartman 		ret = uart_startup(tty, state, 1);
115771456906SSebastian Andrzej Siewior 		if (ret == 0)
115871456906SSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1159b3b57646SRob Herring 		if (ret > 0)
1160b3b57646SRob Herring 			ret = 0;
1161ab4382d2SGreg Kroah-Hartman 	}
11624047b371SPeter Hurley out:
1163ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1164ab4382d2SGreg Kroah-Hartman 	return ret;
1165ab4382d2SGreg Kroah-Hartman }
1166ab4382d2SGreg Kroah-Hartman 
11671fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport)
11681fdc3106SAlexander Shiyan {
11691fdc3106SAlexander Shiyan 	/*
11701fdc3106SAlexander Shiyan 	 * Force modem status interrupts on
11711fdc3106SAlexander Shiyan 	 */
11721fdc3106SAlexander Shiyan 	if (uport->ops->enable_ms)
11731fdc3106SAlexander Shiyan 		uport->ops->enable_ms(uport);
11741fdc3106SAlexander Shiyan }
11751fdc3106SAlexander Shiyan 
1176ab4382d2SGreg Kroah-Hartman /*
1177ab4382d2SGreg Kroah-Hartman  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1178ab4382d2SGreg Kroah-Hartman  * - mask passed in arg for lines of interest
1179ab4382d2SGreg Kroah-Hartman  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1180ab4382d2SGreg Kroah-Hartman  * Caller should use TIOCGICOUNT to see which one it was
1181ab4382d2SGreg Kroah-Hartman  *
1182ab4382d2SGreg Kroah-Hartman  * FIXME: This wants extracting into a common all driver implementation
1183ab4382d2SGreg Kroah-Hartman  * of TIOCMWAIT using tty_port.
1184ab4382d2SGreg Kroah-Hartman  */
11859ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1186ab4382d2SGreg Kroah-Hartman {
11879ed19428SPeter Hurley 	struct uart_port *uport;
1188ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1189ab4382d2SGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
1190ab4382d2SGreg Kroah-Hartman 	struct uart_icount cprev, cnow;
1191ab4382d2SGreg Kroah-Hartman 	int ret;
1192ab4382d2SGreg Kroah-Hartman 
1193ab4382d2SGreg Kroah-Hartman 	/*
1194ab4382d2SGreg Kroah-Hartman 	 * note the counters on entry
1195ab4382d2SGreg Kroah-Hartman 	 */
11969ed19428SPeter Hurley 	uport = uart_port_ref(state);
11979ed19428SPeter Hurley 	if (!uport)
11989ed19428SPeter Hurley 		return -EIO;
1199ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1200ab4382d2SGreg Kroah-Hartman 	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
12011fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1202ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1203ab4382d2SGreg Kroah-Hartman 
1204ab4382d2SGreg Kroah-Hartman 	add_wait_queue(&port->delta_msr_wait, &wait);
1205ab4382d2SGreg Kroah-Hartman 	for (;;) {
1206ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1207ab4382d2SGreg Kroah-Hartman 		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1208ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1209ab4382d2SGreg Kroah-Hartman 
1210ab4382d2SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
1211ab4382d2SGreg Kroah-Hartman 
1212ab4382d2SGreg Kroah-Hartman 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1213ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1214ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1215ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1216ab4382d2SGreg Kroah-Hartman 			ret = 0;
1217ab4382d2SGreg Kroah-Hartman 			break;
1218ab4382d2SGreg Kroah-Hartman 		}
1219ab4382d2SGreg Kroah-Hartman 
1220ab4382d2SGreg Kroah-Hartman 		schedule();
1221ab4382d2SGreg Kroah-Hartman 
1222ab4382d2SGreg Kroah-Hartman 		/* see if a signal did it */
1223ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current)) {
1224ab4382d2SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
1225ab4382d2SGreg Kroah-Hartman 			break;
1226ab4382d2SGreg Kroah-Hartman 		}
1227ab4382d2SGreg Kroah-Hartman 
1228ab4382d2SGreg Kroah-Hartman 		cprev = cnow;
1229ab4382d2SGreg Kroah-Hartman 	}
123097f9f707SFabian Frederick 	__set_current_state(TASK_RUNNING);
1231ab4382d2SGreg Kroah-Hartman 	remove_wait_queue(&port->delta_msr_wait, &wait);
12329ed19428SPeter Hurley 	uart_port_deref(uport);
1233ab4382d2SGreg Kroah-Hartman 
1234ab4382d2SGreg Kroah-Hartman 	return ret;
1235ab4382d2SGreg Kroah-Hartman }
1236ab4382d2SGreg Kroah-Hartman 
1237ab4382d2SGreg Kroah-Hartman /*
1238ab4382d2SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1239ab4382d2SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1240ab4382d2SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1241ab4382d2SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1242ab4382d2SGreg Kroah-Hartman  */
1243ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty,
1244ab4382d2SGreg Kroah-Hartman 			  struct serial_icounter_struct *icount)
1245ab4382d2SGreg Kroah-Hartman {
1246ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1247ab4382d2SGreg Kroah-Hartman 	struct uart_icount cnow;
12489ed19428SPeter Hurley 	struct uart_port *uport;
1249ab4382d2SGreg Kroah-Hartman 
12509ed19428SPeter Hurley 	uport = uart_port_ref(state);
12519ed19428SPeter Hurley 	if (!uport)
12529ed19428SPeter Hurley 		return -EIO;
1253ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1254ab4382d2SGreg Kroah-Hartman 	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1255ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
12569ed19428SPeter Hurley 	uart_port_deref(uport);
1257ab4382d2SGreg Kroah-Hartman 
1258ab4382d2SGreg Kroah-Hartman 	icount->cts         = cnow.cts;
1259ab4382d2SGreg Kroah-Hartman 	icount->dsr         = cnow.dsr;
1260ab4382d2SGreg Kroah-Hartman 	icount->rng         = cnow.rng;
1261ab4382d2SGreg Kroah-Hartman 	icount->dcd         = cnow.dcd;
1262ab4382d2SGreg Kroah-Hartman 	icount->rx          = cnow.rx;
1263ab4382d2SGreg Kroah-Hartman 	icount->tx          = cnow.tx;
1264ab4382d2SGreg Kroah-Hartman 	icount->frame       = cnow.frame;
1265ab4382d2SGreg Kroah-Hartman 	icount->overrun     = cnow.overrun;
1266ab4382d2SGreg Kroah-Hartman 	icount->parity      = cnow.parity;
1267ab4382d2SGreg Kroah-Hartman 	icount->brk         = cnow.brk;
1268ab4382d2SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1269ab4382d2SGreg Kroah-Hartman 
1270ab4382d2SGreg Kroah-Hartman 	return 0;
1271ab4382d2SGreg Kroah-Hartman }
1272ab4382d2SGreg Kroah-Hartman 
127351ad36baSIlpo Järvinen #define SER_RS485_LEGACY_FLAGS	(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
127451ad36baSIlpo Järvinen 				 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
127551ad36baSIlpo Järvinen 				 SER_RS485_TERMINATE_BUS)
127651ad36baSIlpo Järvinen 
127751ad36baSIlpo Järvinen static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
127851ad36baSIlpo Järvinen {
127951ad36baSIlpo Järvinen 	u32 flags = rs485->flags;
128051ad36baSIlpo Järvinen 
128151ad36baSIlpo Järvinen 	/* Don't return -EINVAL for unsupported legacy flags */
128251ad36baSIlpo Järvinen 	flags &= ~SER_RS485_LEGACY_FLAGS;
128351ad36baSIlpo Järvinen 
128451ad36baSIlpo Järvinen 	/*
128551ad36baSIlpo Järvinen 	 * For any bit outside of the legacy ones that is not supported by
128651ad36baSIlpo Järvinen 	 * the driver, return -EINVAL.
128751ad36baSIlpo Järvinen 	 */
128851ad36baSIlpo Järvinen 	if (flags & ~port->rs485_supported->flags)
128951ad36baSIlpo Järvinen 		return -EINVAL;
129051ad36baSIlpo Järvinen 
129151ad36baSIlpo Järvinen 	return 0;
129251ad36baSIlpo Järvinen }
129351ad36baSIlpo Järvinen 
12942dbd0c14SIlpo Järvinen static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
12952dbd0c14SIlpo Järvinen {
1296be2e2cb1SIlpo Järvinen 	u32 supported_flags = port->rs485_supported->flags;
1297be2e2cb1SIlpo Järvinen 
1298596a9171SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ENABLED)) {
1299596a9171SIlpo Järvinen 		memset(rs485, 0, sizeof(*rs485));
1300596a9171SIlpo Järvinen 		return;
1301596a9171SIlpo Järvinen 	}
1302596a9171SIlpo Järvinen 
13032dbd0c14SIlpo Järvinen 	/* pick sane settings if the user hasn't */
1304be2e2cb1SIlpo Järvinen 	if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
1305be2e2cb1SIlpo Järvinen 	    !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
13062dbd0c14SIlpo Järvinen 	    !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
13072dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13082dbd0c14SIlpo Järvinen 			"%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
13092dbd0c14SIlpo Järvinen 			port->name, port->line);
13102dbd0c14SIlpo Järvinen 		rs485->flags |= SER_RS485_RTS_ON_SEND;
13112dbd0c14SIlpo Järvinen 		rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1312be2e2cb1SIlpo Järvinen 		supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
13132dbd0c14SIlpo Järvinen 	}
13142dbd0c14SIlpo Järvinen 
1315be2e2cb1SIlpo Järvinen 	if (!port->rs485_supported->delay_rts_before_send) {
1316be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_before_send) {
1317be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1318be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay before sending not supported\n",
1319be2e2cb1SIlpo Järvinen 				port->name, port->line);
1320be2e2cb1SIlpo Järvinen 		}
1321be2e2cb1SIlpo Järvinen 		rs485->delay_rts_before_send = 0;
1322be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
13232dbd0c14SIlpo Järvinen 		rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
13242dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13252dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay before sending clamped to %u ms\n",
13262dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_before_send);
13272dbd0c14SIlpo Järvinen 	}
13282dbd0c14SIlpo Järvinen 
1329be2e2cb1SIlpo Järvinen 	if (!port->rs485_supported->delay_rts_after_send) {
1330be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_after_send) {
1331be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1332be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay after sending not supported\n",
1333be2e2cb1SIlpo Järvinen 				port->name, port->line);
1334be2e2cb1SIlpo Järvinen 		}
1335be2e2cb1SIlpo Järvinen 		rs485->delay_rts_after_send = 0;
1336be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
13372dbd0c14SIlpo Järvinen 		rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
13382dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13392dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay after sending clamped to %u ms\n",
13402dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_after_send);
13412dbd0c14SIlpo Järvinen 	}
1342be2e2cb1SIlpo Järvinen 
1343be2e2cb1SIlpo Järvinen 	rs485->flags &= supported_flags;
1344be2e2cb1SIlpo Järvinen 
13452dbd0c14SIlpo Järvinen 	/* Return clean padding area to userspace */
13462dbd0c14SIlpo Järvinen 	memset(rs485->padding, 0, sizeof(rs485->padding));
13472dbd0c14SIlpo Järvinen }
13482dbd0c14SIlpo Järvinen 
13498322b1f5SIlpo Järvinen int uart_rs485_config(struct uart_port *port)
13508322b1f5SIlpo Järvinen {
1351be2e2cb1SIlpo Järvinen 	struct serial_rs485 *rs485 = &port->rs485;
1352596a9171SIlpo Järvinen 	int ret;
1353be2e2cb1SIlpo Järvinen 
1354be2e2cb1SIlpo Järvinen 	uart_sanitize_serial_rs485(port, rs485);
1355be2e2cb1SIlpo Järvinen 
1356596a9171SIlpo Järvinen 	ret = port->rs485_config(port, rs485);
1357596a9171SIlpo Järvinen 	if (ret)
1358596a9171SIlpo Järvinen 		memset(rs485, 0, sizeof(*rs485));
1359596a9171SIlpo Järvinen 
1360596a9171SIlpo Järvinen 	return ret;
13618322b1f5SIlpo Järvinen }
13628322b1f5SIlpo Järvinen EXPORT_SYMBOL_GPL(uart_rs485_config);
13638322b1f5SIlpo Järvinen 
1364a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port,
1365a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485)
1366a5f276f1SRicardo Ribalda Delgado {
1367bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1368bd737f87SRicardo Ribalda Delgado 	struct serial_rs485 aux;
1369bd737f87SRicardo Ribalda Delgado 
1370bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1371bd737f87SRicardo Ribalda Delgado 	aux = port->rs485;
1372bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1373bd737f87SRicardo Ribalda Delgado 
1374bd737f87SRicardo Ribalda Delgado 	if (copy_to_user(rs485, &aux, sizeof(aux)))
1375a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1376bd737f87SRicardo Ribalda Delgado 
1377a5f276f1SRicardo Ribalda Delgado 	return 0;
1378a5f276f1SRicardo Ribalda Delgado }
1379a5f276f1SRicardo Ribalda Delgado 
1380a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port,
1381a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485_user)
1382a5f276f1SRicardo Ribalda Delgado {
1383a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485 rs485;
1384a5f276f1SRicardo Ribalda Delgado 	int ret;
1385bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1386a5f276f1SRicardo Ribalda Delgado 
1387a5f276f1SRicardo Ribalda Delgado 	if (!port->rs485_config)
138879c5966cSJohan Hovold 		return -ENOTTY;
1389a5f276f1SRicardo Ribalda Delgado 
1390a5f276f1SRicardo Ribalda Delgado 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1391a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1392a5f276f1SRicardo Ribalda Delgado 
139351ad36baSIlpo Järvinen 	ret = uart_check_rs485_flags(port, &rs485);
139451ad36baSIlpo Järvinen 	if (ret)
139551ad36baSIlpo Järvinen 		return ret;
13962dbd0c14SIlpo Järvinen 	uart_sanitize_serial_rs485(port, &rs485);
13970ed12afaSLino Sanfilippo 
1398bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1399a5f276f1SRicardo Ribalda Delgado 	ret = port->rs485_config(port, &rs485);
14000ed12afaSLino Sanfilippo 	if (!ret)
14010ed12afaSLino Sanfilippo 		port->rs485 = rs485;
1402bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1403a5f276f1SRicardo Ribalda Delgado 	if (ret)
1404a5f276f1SRicardo Ribalda Delgado 		return ret;
1405a5f276f1SRicardo Ribalda Delgado 
1406a5f276f1SRicardo Ribalda Delgado 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1407a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1408a5f276f1SRicardo Ribalda Delgado 
1409a5f276f1SRicardo Ribalda Delgado 	return 0;
1410a5f276f1SRicardo Ribalda Delgado }
1411a5f276f1SRicardo Ribalda Delgado 
1412ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port,
1413ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816)
1414ad8c0eaaSNicolas Ferre {
1415ad8c0eaaSNicolas Ferre 	unsigned long flags;
1416ad8c0eaaSNicolas Ferre 	struct serial_iso7816 aux;
1417ad8c0eaaSNicolas Ferre 
1418ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
141979c5966cSJohan Hovold 		return -ENOTTY;
1420ad8c0eaaSNicolas Ferre 
1421ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1422ad8c0eaaSNicolas Ferre 	aux = port->iso7816;
1423ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1424ad8c0eaaSNicolas Ferre 
1425ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816, &aux, sizeof(aux)))
1426ad8c0eaaSNicolas Ferre 		return -EFAULT;
1427ad8c0eaaSNicolas Ferre 
1428ad8c0eaaSNicolas Ferre 	return 0;
1429ad8c0eaaSNicolas Ferre }
1430ad8c0eaaSNicolas Ferre 
1431ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port,
1432ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816_user)
1433ad8c0eaaSNicolas Ferre {
1434ad8c0eaaSNicolas Ferre 	struct serial_iso7816 iso7816;
1435ad8c0eaaSNicolas Ferre 	int i, ret;
1436ad8c0eaaSNicolas Ferre 	unsigned long flags;
1437ad8c0eaaSNicolas Ferre 
1438ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
143979c5966cSJohan Hovold 		return -ENOTTY;
1440ad8c0eaaSNicolas Ferre 
1441ad8c0eaaSNicolas Ferre 	if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1442ad8c0eaaSNicolas Ferre 		return -EFAULT;
1443ad8c0eaaSNicolas Ferre 
1444ad8c0eaaSNicolas Ferre 	/*
1445ad8c0eaaSNicolas Ferre 	 * There are 5 words reserved for future use. Check that userspace
1446ad8c0eaaSNicolas Ferre 	 * doesn't put stuff in there to prevent breakages in the future.
1447ad8c0eaaSNicolas Ferre 	 */
1448ad8c0eaaSNicolas Ferre 	for (i = 0; i < 5; i++)
1449ad8c0eaaSNicolas Ferre 		if (iso7816.reserved[i])
1450ad8c0eaaSNicolas Ferre 			return -EINVAL;
1451ad8c0eaaSNicolas Ferre 
1452ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1453ad8c0eaaSNicolas Ferre 	ret = port->iso7816_config(port, &iso7816);
1454ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1455ad8c0eaaSNicolas Ferre 	if (ret)
1456ad8c0eaaSNicolas Ferre 		return ret;
1457ad8c0eaaSNicolas Ferre 
1458ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1459ad8c0eaaSNicolas Ferre 		return -EFAULT;
1460ad8c0eaaSNicolas Ferre 
1461ad8c0eaaSNicolas Ferre 	return 0;
1462ad8c0eaaSNicolas Ferre }
1463ad8c0eaaSNicolas Ferre 
1464ab4382d2SGreg Kroah-Hartman /*
1465ab4382d2SGreg Kroah-Hartman  * Called via sys_ioctl.  We can use spin_lock_irq() here.
1466ab4382d2SGreg Kroah-Hartman  */
1467ab4382d2SGreg Kroah-Hartman static int
14684047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1469ab4382d2SGreg Kroah-Hartman {
1470ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1471ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
14724047b371SPeter Hurley 	struct uart_port *uport;
1473ab4382d2SGreg Kroah-Hartman 	void __user *uarg = (void __user *)arg;
1474ab4382d2SGreg Kroah-Hartman 	int ret = -ENOIOCTLCMD;
1475ab4382d2SGreg Kroah-Hartman 
1476ab4382d2SGreg Kroah-Hartman 
1477ab4382d2SGreg Kroah-Hartman 	/*
1478ab4382d2SGreg Kroah-Hartman 	 * These ioctls don't rely on the hardware to be present.
1479ab4382d2SGreg Kroah-Hartman 	 */
1480ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1481ab4382d2SGreg Kroah-Hartman 	case TIOCSERCONFIG:
14827c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
1483ab4382d2SGreg Kroah-Hartman 		ret = uart_do_autoconfig(tty, state);
14847c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1485ab4382d2SGreg Kroah-Hartman 		break;
1486ab4382d2SGreg Kroah-Hartman 	}
1487ab4382d2SGreg Kroah-Hartman 
1488ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1489ab4382d2SGreg Kroah-Hartman 		goto out;
1490ab4382d2SGreg Kroah-Hartman 
149118900ca6SPeter Hurley 	if (tty_io_error(tty)) {
1492ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1493ab4382d2SGreg Kroah-Hartman 		goto out;
1494ab4382d2SGreg Kroah-Hartman 	}
1495ab4382d2SGreg Kroah-Hartman 
1496ab4382d2SGreg Kroah-Hartman 	/*
1497ab4382d2SGreg Kroah-Hartman 	 * The following should only be used when hardware is present.
1498ab4382d2SGreg Kroah-Hartman 	 */
1499ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1500ab4382d2SGreg Kroah-Hartman 	case TIOCMIWAIT:
1501ab4382d2SGreg Kroah-Hartman 		ret = uart_wait_modem_status(state, arg);
1502ab4382d2SGreg Kroah-Hartman 		break;
1503ab4382d2SGreg Kroah-Hartman 	}
1504ab4382d2SGreg Kroah-Hartman 
1505ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1506ab4382d2SGreg Kroah-Hartman 		goto out;
1507ab4382d2SGreg Kroah-Hartman 
1508ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
15094047b371SPeter Hurley 	uport = uart_port_check(state);
1510ab4382d2SGreg Kroah-Hartman 
15114047b371SPeter Hurley 	if (!uport || tty_io_error(tty)) {
1512ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1513ab4382d2SGreg Kroah-Hartman 		goto out_up;
1514ab4382d2SGreg Kroah-Hartman 	}
1515ab4382d2SGreg Kroah-Hartman 
1516ab4382d2SGreg Kroah-Hartman 	/*
1517ab4382d2SGreg Kroah-Hartman 	 * All these rely on hardware being present and need to be
1518ab4382d2SGreg Kroah-Hartman 	 * protected against the tty being hung up.
1519ab4382d2SGreg Kroah-Hartman 	 */
1520a9c20a9cSRicardo Ribalda Delgado 
1521ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1522a9c20a9cSRicardo Ribalda Delgado 	case TIOCSERGETLSR: /* Get line status register */
1523a9c20a9cSRicardo Ribalda Delgado 		ret = uart_get_lsr_info(tty, state, uarg);
1524a9c20a9cSRicardo Ribalda Delgado 		break;
1525a9c20a9cSRicardo Ribalda Delgado 
1526a5f276f1SRicardo Ribalda Delgado 	case TIOCGRS485:
15274047b371SPeter Hurley 		ret = uart_get_rs485_config(uport, uarg);
1528a5f276f1SRicardo Ribalda Delgado 		break;
1529a5f276f1SRicardo Ribalda Delgado 
1530a5f276f1SRicardo Ribalda Delgado 	case TIOCSRS485:
15314047b371SPeter Hurley 		ret = uart_set_rs485_config(uport, uarg);
1532a5f276f1SRicardo Ribalda Delgado 		break;
1533ad8c0eaaSNicolas Ferre 
1534ad8c0eaaSNicolas Ferre 	case TIOCSISO7816:
1535ad8c0eaaSNicolas Ferre 		ret = uart_set_iso7816_config(state->uart_port, uarg);
1536ad8c0eaaSNicolas Ferre 		break;
1537ad8c0eaaSNicolas Ferre 
1538ad8c0eaaSNicolas Ferre 	case TIOCGISO7816:
1539ad8c0eaaSNicolas Ferre 		ret = uart_get_iso7816_config(state->uart_port, uarg);
1540ad8c0eaaSNicolas Ferre 		break;
15414047b371SPeter Hurley 	default:
1542ab4382d2SGreg Kroah-Hartman 		if (uport->ops->ioctl)
1543ab4382d2SGreg Kroah-Hartman 			ret = uport->ops->ioctl(uport, cmd, arg);
1544ab4382d2SGreg Kroah-Hartman 		break;
1545ab4382d2SGreg Kroah-Hartman 	}
1546ab4382d2SGreg Kroah-Hartman out_up:
1547ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1548ab4382d2SGreg Kroah-Hartman out:
1549ab4382d2SGreg Kroah-Hartman 	return ret;
1550ab4382d2SGreg Kroah-Hartman }
1551ab4382d2SGreg Kroah-Hartman 
1552ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty)
1553ab4382d2SGreg Kroah-Hartman {
1554ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
15554047b371SPeter Hurley 	struct uart_port *uport;
15562f70e49eSAlexey Kardashevskiy 	struct tty_port *port = &state->port;
15572f70e49eSAlexey Kardashevskiy 
15582f70e49eSAlexey Kardashevskiy 	if (!tty_port_initialized(port))
15592f70e49eSAlexey Kardashevskiy 		return;
1560ab4382d2SGreg Kroah-Hartman 
1561db1b9dfcSPeter Hurley 	mutex_lock(&state->port.mutex);
15624047b371SPeter Hurley 	uport = uart_port_check(state);
15634047b371SPeter Hurley 	if (uport && uport->ops->set_ldisc)
1564732a84a0SPeter Hurley 		uport->ops->set_ldisc(uport, &tty->termios);
1565db1b9dfcSPeter Hurley 	mutex_unlock(&state->port.mutex);
1566db1b9dfcSPeter Hurley }
1567ab4382d2SGreg Kroah-Hartman 
1568ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty,
1569ab4382d2SGreg Kroah-Hartman 						struct ktermios *old_termios)
1570ab4382d2SGreg Kroah-Hartman {
1571ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
15724047b371SPeter Hurley 	struct uart_port *uport;
1573adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
15742cbacafdSRussell King 	unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
15752cbacafdSRussell King 	bool sw_changed = false;
1576ab4382d2SGreg Kroah-Hartman 
15774047b371SPeter Hurley 	mutex_lock(&state->port.mutex);
15784047b371SPeter Hurley 	uport = uart_port_check(state);
15794047b371SPeter Hurley 	if (!uport)
15804047b371SPeter Hurley 		goto out;
15814047b371SPeter Hurley 
15822cbacafdSRussell King 	/*
15832cbacafdSRussell King 	 * Drivers doing software flow control also need to know
15842cbacafdSRussell King 	 * about changes to these input settings.
15852cbacafdSRussell King 	 */
15862cbacafdSRussell King 	if (uport->flags & UPF_SOFT_FLOW) {
15872cbacafdSRussell King 		iflag_mask |= IXANY|IXON|IXOFF;
15882cbacafdSRussell King 		sw_changed =
15892cbacafdSRussell King 		   tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
15902cbacafdSRussell King 		   tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
15912cbacafdSRussell King 	}
1592ab4382d2SGreg Kroah-Hartman 
1593ab4382d2SGreg Kroah-Hartman 	/*
1594ab4382d2SGreg Kroah-Hartman 	 * These are the bits that are used to setup various
1595ab4382d2SGreg Kroah-Hartman 	 * flags in the low level driver. We can ignore the Bfoo
1596ab4382d2SGreg Kroah-Hartman 	 * bits in c_cflag; c_[io]speed will always be set
1597ab4382d2SGreg Kroah-Hartman 	 * appropriately by set_termios() in tty_ioctl.c
1598ab4382d2SGreg Kroah-Hartman 	 */
1599ab4382d2SGreg Kroah-Hartman 	if ((cflag ^ old_termios->c_cflag) == 0 &&
1600adc8d746SAlan Cox 	    tty->termios.c_ospeed == old_termios->c_ospeed &&
1601adc8d746SAlan Cox 	    tty->termios.c_ispeed == old_termios->c_ispeed &&
16022cbacafdSRussell King 	    ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
16032cbacafdSRussell King 	    !sw_changed) {
16044047b371SPeter Hurley 		goto out;
1605ab4382d2SGreg Kroah-Hartman 	}
1606ab4382d2SGreg Kroah-Hartman 
1607ab4382d2SGreg Kroah-Hartman 	uart_change_speed(tty, state, old_termios);
1608c7a6b9e4SHariprasad Kelam 	/* reload cflag from termios; port driver may have overridden flags */
1609c18b55fdSPeter Hurley 	cflag = tty->termios.c_cflag;
1610ab4382d2SGreg Kroah-Hartman 
1611ab4382d2SGreg Kroah-Hartman 	/* Handle transition to B0 status */
1612ab4382d2SGreg Kroah-Hartman 	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1613dec94e70SRussell King 		uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1614ab4382d2SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
1615ab4382d2SGreg Kroah-Hartman 	else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1616ab4382d2SGreg Kroah-Hartman 		unsigned int mask = TIOCM_DTR;
16174ed71addSTamseel Shams 
161897ef38b8SPeter Hurley 		if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1619ab4382d2SGreg Kroah-Hartman 			mask |= TIOCM_RTS;
1620dec94e70SRussell King 		uart_set_mctrl(uport, mask);
1621ab4382d2SGreg Kroah-Hartman 	}
16224047b371SPeter Hurley out:
16234047b371SPeter Hurley 	mutex_unlock(&state->port.mutex);
1624ab4382d2SGreg Kroah-Hartman }
1625ab4382d2SGreg Kroah-Hartman 
1626ab4382d2SGreg Kroah-Hartman /*
1627ef4f527cSKevin Cernekee  * Calls to uart_close() are serialised via the tty_lock in
1628ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_release()
1629ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1630ab4382d2SGreg Kroah-Hartman  */
1631ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp)
1632ab4382d2SGreg Kroah-Hartman {
1633ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1634ab4382d2SGreg Kroah-Hartman 
163591b32f54SPeter Hurley 	if (!state) {
163691b32f54SPeter Hurley 		struct uart_driver *drv = tty->driver->driver_state;
16379356335fSColin Ian King 		struct tty_port *port;
163891b32f54SPeter Hurley 
163991b32f54SPeter Hurley 		state = drv->state + tty->index;
164091b32f54SPeter Hurley 		port = &state->port;
164191b32f54SPeter Hurley 		spin_lock_irq(&port->lock);
164291b32f54SPeter Hurley 		--port->count;
164391b32f54SPeter Hurley 		spin_unlock_irq(&port->lock);
1644ab4382d2SGreg Kroah-Hartman 		return;
164591b32f54SPeter Hurley 	}
1646ab4382d2SGreg Kroah-Hartman 
164739b3d892SPeter Hurley 	pr_debug("uart_close(%d) called\n", tty->index);
1648ab4382d2SGreg Kroah-Hartman 
1649761ed4a9SRob Herring 	tty_port_close(tty->port, tty, filp);
1650761ed4a9SRob Herring }
1651ab4382d2SGreg Kroah-Hartman 
1652761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port)
1653761ed4a9SRob Herring {
1654761ed4a9SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1655761ed4a9SRob Herring 	struct uart_port *uport = uart_port_check(state);
165600de977fSJohan Hovold 	char *buf;
1657af224ca2SPeter Hurley 
1658ab4382d2SGreg Kroah-Hartman 	/*
1659ab4382d2SGreg Kroah-Hartman 	 * At this point, we stop accepting input.  To do this, we
1660ab4382d2SGreg Kroah-Hartman 	 * disable the receive line status interrupts.
1661ab4382d2SGreg Kroah-Hartman 	 */
1662a5a2b130SAndy Shevchenko 	if (WARN(!uport, "detached port still initialized!\n"))
1663a5a2b130SAndy Shevchenko 		return;
1664761ed4a9SRob Herring 
1665a5a2b130SAndy Shevchenko 	spin_lock_irq(&uport->lock);
1666ab4382d2SGreg Kroah-Hartman 	uport->ops->stop_rx(uport);
16676fb98fb3SPeter Hurley 	spin_unlock_irq(&uport->lock);
1668761ed4a9SRob Herring 
1669761ed4a9SRob Herring 	uart_port_shutdown(port);
1670761ed4a9SRob Herring 
1671ab4382d2SGreg Kroah-Hartman 	/*
1672761ed4a9SRob Herring 	 * It's possible for shutdown to be called after suspend if we get
1673761ed4a9SRob Herring 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
1674761ed4a9SRob Herring 	 * we don't try to resume a port that has been shutdown.
1675ab4382d2SGreg Kroah-Hartman 	 */
1676761ed4a9SRob Herring 	tty_port_set_suspended(port, 0);
1677ab4382d2SGreg Kroah-Hartman 
167800de977fSJohan Hovold 	/*
167900de977fSJohan Hovold 	 * Free the transmit buffer.
168000de977fSJohan Hovold 	 */
168100de977fSJohan Hovold 	spin_lock_irq(&uport->lock);
168200de977fSJohan Hovold 	buf = state->xmit.buf;
168300de977fSJohan Hovold 	state->xmit.buf = NULL;
168400de977fSJohan Hovold 	spin_unlock_irq(&uport->lock);
1685ab4382d2SGreg Kroah-Hartman 
168600de977fSJohan Hovold 	free_page((unsigned long)buf);
168700de977fSJohan Hovold 
168800de977fSJohan Hovold 	uart_change_pm(state, UART_PM_STATE_OFF);
1689ab4382d2SGreg Kroah-Hartman }
1690ab4382d2SGreg Kroah-Hartman 
16911f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1692ab4382d2SGreg Kroah-Hartman {
16931f33a51dSJiri Slaby 	struct uart_state *state = tty->driver_data;
16949ed19428SPeter Hurley 	struct uart_port *port;
1695*f9008285SIlpo Järvinen 	unsigned long char_time, expire, fifo_timeout;
1696ab4382d2SGreg Kroah-Hartman 
16979ed19428SPeter Hurley 	port = uart_port_ref(state);
1698ef510beaSAndy Shevchenko 	if (!port)
1699ef510beaSAndy Shevchenko 		return;
1700ef510beaSAndy Shevchenko 
1701ef510beaSAndy Shevchenko 	if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
17029ed19428SPeter Hurley 		uart_port_deref(port);
1703ab4382d2SGreg Kroah-Hartman 		return;
17049ed19428SPeter Hurley 	}
1705ab4382d2SGreg Kroah-Hartman 
1706ab4382d2SGreg Kroah-Hartman 	/*
1707ab4382d2SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1708ab4382d2SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1709ab4382d2SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1710ab4382d2SGreg Kroah-Hartman 	 *
1711ab4382d2SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1712ab4382d2SGreg Kroah-Hartman 	 * the NIST-PCTS.
1713ab4382d2SGreg Kroah-Hartman 	 */
171431f6bd7fSIlpo Järvinen 	char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
171531f6bd7fSIlpo Järvinen 
1716ab4382d2SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1717ab4382d2SGreg Kroah-Hartman 		char_time = timeout;
1718ab4382d2SGreg Kroah-Hartman 
1719b0e0bd9dSTomasz Moń 	if (!uart_cts_enabled(port)) {
1720ab4382d2SGreg Kroah-Hartman 		/*
1721ab4382d2SGreg Kroah-Hartman 		 * If the transmitter hasn't cleared in twice the approximate
1722ab4382d2SGreg Kroah-Hartman 		 * amount of time to send the entire FIFO, it probably won't
1723ab4382d2SGreg Kroah-Hartman 		 * ever clear.  This assumes the UART isn't doing flow
1724ab4382d2SGreg Kroah-Hartman 		 * control, which is currently the case.  Hence, if it ever
1725*f9008285SIlpo Järvinen 		 * takes longer than FIFO timeout, this is probably due to a
1726ab4382d2SGreg Kroah-Hartman 		 * UART bug of some kind.  So, we clamp the timeout parameter at
1727*f9008285SIlpo Järvinen 		 * 2 * FIFO timeout.
1728ab4382d2SGreg Kroah-Hartman 		 */
1729*f9008285SIlpo Järvinen 		fifo_timeout = uart_fifo_timeout(port);
1730*f9008285SIlpo Järvinen 		if (timeout == 0 || timeout > 2 * fifo_timeout)
1731*f9008285SIlpo Järvinen 			timeout = 2 * fifo_timeout;
1732b0e0bd9dSTomasz Moń 	}
1733ab4382d2SGreg Kroah-Hartman 
1734ab4382d2SGreg Kroah-Hartman 	expire = jiffies + timeout;
1735ab4382d2SGreg Kroah-Hartman 
1736ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1737ab4382d2SGreg Kroah-Hartman 		port->line, jiffies, expire);
1738ab4382d2SGreg Kroah-Hartman 
1739ab4382d2SGreg Kroah-Hartman 	/*
1740ab4382d2SGreg Kroah-Hartman 	 * Check whether the transmitter is empty every 'char_time'.
1741ab4382d2SGreg Kroah-Hartman 	 * 'timeout' / 'expire' give us the maximum amount of time
1742ab4382d2SGreg Kroah-Hartman 	 * we wait.
1743ab4382d2SGreg Kroah-Hartman 	 */
1744ab4382d2SGreg Kroah-Hartman 	while (!port->ops->tx_empty(port)) {
1745ab4382d2SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1746ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current))
1747ab4382d2SGreg Kroah-Hartman 			break;
1748b0e0bd9dSTomasz Moń 		if (timeout && time_after(jiffies, expire))
1749ab4382d2SGreg Kroah-Hartman 			break;
1750ab4382d2SGreg Kroah-Hartman 	}
17519ed19428SPeter Hurley 	uart_port_deref(port);
1752ab4382d2SGreg Kroah-Hartman }
1753ab4382d2SGreg Kroah-Hartman 
1754ab4382d2SGreg Kroah-Hartman /*
1755ef4f527cSKevin Cernekee  * Calls to uart_hangup() are serialised by the tty_lock in
1756ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1757ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1758ab4382d2SGreg Kroah-Hartman  */
1759ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty)
1760ab4382d2SGreg Kroah-Hartman {
1761ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1762ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1763af224ca2SPeter Hurley 	struct uart_port *uport;
1764ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1765ab4382d2SGreg Kroah-Hartman 
176639b3d892SPeter Hurley 	pr_debug("uart_hangup(%d)\n", tty->index);
1767ab4382d2SGreg Kroah-Hartman 
1768ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1769af224ca2SPeter Hurley 	uport = uart_port_check(state);
1770af224ca2SPeter Hurley 	WARN(!uport, "hangup of detached port!\n");
1771af224ca2SPeter Hurley 
1772807c8d81SPeter Hurley 	if (tty_port_active(port)) {
1773ab4382d2SGreg Kroah-Hartman 		uart_flush_buffer(tty);
1774ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1775ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
1776ab4382d2SGreg Kroah-Hartman 		port->count = 0;
1777ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
1778807c8d81SPeter Hurley 		tty_port_set_active(port, 0);
1779ab4382d2SGreg Kroah-Hartman 		tty_port_tty_set(port, NULL);
1780af224ca2SPeter Hurley 		if (uport && !uart_console(uport))
1781bf903c0cSGeert Uytterhoeven 			uart_change_pm(state, UART_PM_STATE_OFF);
1782ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
1783ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->delta_msr_wait);
1784ab4382d2SGreg Kroah-Hartman 	}
1785ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1786ab4382d2SGreg Kroah-Hartman }
1787ab4382d2SGreg Kroah-Hartman 
1788af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */
17890b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port)
17900b1db830SJiri Slaby {
1791b922e19dSJiri Slaby 	struct uart_state *state = container_of(port, struct uart_state, port);
17924047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1793b922e19dSJiri Slaby 
1794b922e19dSJiri Slaby 	/*
1795b922e19dSJiri Slaby 	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1796b922e19dSJiri Slaby 	 * the irq here so the queue might never be woken up.  Note
1797b922e19dSJiri Slaby 	 * that we won't end up waiting on delta_msr_wait again since
1798b922e19dSJiri Slaby 	 * any outstanding file descriptors should be pointing at
1799b922e19dSJiri Slaby 	 * hung_up_tty_fops now.
1800b922e19dSJiri Slaby 	 */
1801b922e19dSJiri Slaby 	wake_up_interruptible(&port->delta_msr_wait);
1802b922e19dSJiri Slaby 
18032765852eSJiri Slaby 	if (uport) {
18042765852eSJiri Slaby 		/* Free the IRQ and disable the port. */
1805b922e19dSJiri Slaby 		uport->ops->shutdown(uport);
1806b922e19dSJiri Slaby 
18072765852eSJiri Slaby 		/* Ensure that the IRQ handler isn't running on another CPU. */
1808b922e19dSJiri Slaby 		synchronize_irq(uport->irq);
18090b1db830SJiri Slaby 	}
18102765852eSJiri Slaby }
18110b1db830SJiri Slaby 
1812ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port)
1813ab4382d2SGreg Kroah-Hartman {
1814ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
18159ed19428SPeter Hurley 	struct uart_port *uport;
1816ab4382d2SGreg Kroah-Hartman 	int mctrl;
18179ed19428SPeter Hurley 
18189ed19428SPeter Hurley 	uport = uart_port_ref(state);
18199ed19428SPeter Hurley 	/*
18209ed19428SPeter Hurley 	 * Should never observe uport == NULL since checks for hangup should
18219ed19428SPeter Hurley 	 * abort the tty_port_block_til_ready() loop before checking for carrier
18229ed19428SPeter Hurley 	 * raised -- but report carrier raised if it does anyway so open will
18239ed19428SPeter Hurley 	 * continue and not sleep
18249ed19428SPeter Hurley 	 */
18259ed19428SPeter Hurley 	if (WARN_ON(!uport))
18269ed19428SPeter Hurley 		return 1;
1827ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
18281fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1829ab4382d2SGreg Kroah-Hartman 	mctrl = uport->ops->get_mctrl(uport);
1830ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
18319ed19428SPeter Hurley 	uart_port_deref(uport);
1832ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_CAR)
1833ab4382d2SGreg Kroah-Hartman 		return 1;
1834ab4382d2SGreg Kroah-Hartman 	return 0;
1835ab4382d2SGreg Kroah-Hartman }
1836ab4382d2SGreg Kroah-Hartman 
1837a6845e1eSRafael Gago static void uart_dtr_rts(struct tty_port *port, int raise)
1838ab4382d2SGreg Kroah-Hartman {
1839ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
18409ed19428SPeter Hurley 	struct uart_port *uport;
18419ed19428SPeter Hurley 
18429ed19428SPeter Hurley 	uport = uart_port_ref(state);
18439ed19428SPeter Hurley 	if (!uport)
18449ed19428SPeter Hurley 		return;
1845a6845e1eSRafael Gago 	uart_port_dtr_rts(uport, raise);
18469ed19428SPeter Hurley 	uart_port_deref(uport);
1847ab4382d2SGreg Kroah-Hartman }
1848ab4382d2SGreg Kroah-Hartman 
18494cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
18504cdd17baSJiri Slaby {
18514cdd17baSJiri Slaby 	struct uart_driver *drv = driver->driver_state;
18524cdd17baSJiri Slaby 	struct uart_state *state = drv->state + tty->index;
18534cdd17baSJiri Slaby 
18544cdd17baSJiri Slaby 	tty->driver_data = state;
18554cdd17baSJiri Slaby 
18564cdd17baSJiri Slaby 	return tty_standard_install(driver, tty);
18574cdd17baSJiri Slaby }
18584cdd17baSJiri Slaby 
1859ab4382d2SGreg Kroah-Hartman /*
1860ef4f527cSKevin Cernekee  * Calls to uart_open are serialised by the tty_lock in
1861ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_open()
1862ab4382d2SGreg Kroah-Hartman  * Note that if this fails, then uart_close() _will_ be called.
1863ab4382d2SGreg Kroah-Hartman  *
1864ab4382d2SGreg Kroah-Hartman  * In time, we want to scrap the "opening nonpresent ports"
1865ab4382d2SGreg Kroah-Hartman  * behaviour and implement an alternative way for setserial
1866ab4382d2SGreg Kroah-Hartman  * to set base addresses/ports/types.  This will allow us to
1867ab4382d2SGreg Kroah-Hartman  * get rid of a certain amount of extra tests.
1868ab4382d2SGreg Kroah-Hartman  */
1869ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp)
1870ab4382d2SGreg Kroah-Hartman {
18714cdd17baSJiri Slaby 	struct uart_state *state = tty->driver_data;
18724cdd17baSJiri Slaby 	int retval;
1873b3b57646SRob Herring 
1874b3b57646SRob Herring 	retval = tty_port_open(&state->port, tty, filp);
1875b3b57646SRob Herring 	if (retval > 0)
1876b3b57646SRob Herring 		retval = 0;
1877b3b57646SRob Herring 
1878b3b57646SRob Herring 	return retval;
1879b3b57646SRob Herring }
1880b3b57646SRob Herring 
1881b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1882b3b57646SRob Herring {
1883b3b57646SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1884b3b57646SRob Herring 	struct uart_port *uport;
188513b18d35SSerge Semin 	int ret;
1886b3b57646SRob Herring 
1887b3b57646SRob Herring 	uport = uart_port_check(state);
1888b3b57646SRob Herring 	if (!uport || uport->flags & UPF_DEAD)
1889b3b57646SRob Herring 		return -ENXIO;
1890b3b57646SRob Herring 
1891ab4382d2SGreg Kroah-Hartman 	/*
1892ab4382d2SGreg Kroah-Hartman 	 * Start up the serial port.
1893ab4382d2SGreg Kroah-Hartman 	 */
189413b18d35SSerge Semin 	ret = uart_startup(tty, state, 0);
189513b18d35SSerge Semin 	if (ret > 0)
189613b18d35SSerge Semin 		tty_port_set_active(port, 1);
189713b18d35SSerge Semin 
189813b18d35SSerge Semin 	return ret;
1899ab4382d2SGreg Kroah-Hartman }
1900ab4382d2SGreg Kroah-Hartman 
1901ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port)
1902ab4382d2SGreg Kroah-Hartman {
1903ab4382d2SGreg Kroah-Hartman 	const char *str = NULL;
1904ab4382d2SGreg Kroah-Hartman 
1905ab4382d2SGreg Kroah-Hartman 	if (port->ops->type)
1906ab4382d2SGreg Kroah-Hartman 		str = port->ops->type(port);
1907ab4382d2SGreg Kroah-Hartman 
1908ab4382d2SGreg Kroah-Hartman 	if (!str)
1909ab4382d2SGreg Kroah-Hartman 		str = "unknown";
1910ab4382d2SGreg Kroah-Hartman 
1911ab4382d2SGreg Kroah-Hartman 	return str;
1912ab4382d2SGreg Kroah-Hartman }
1913ab4382d2SGreg Kroah-Hartman 
1914ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
1915ab4382d2SGreg Kroah-Hartman 
1916ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1917ab4382d2SGreg Kroah-Hartman {
1918ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + i;
1919ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
19206f538fe3SLinus Walleij 	enum uart_pm_state pm_state;
19214047b371SPeter Hurley 	struct uart_port *uport;
1922ab4382d2SGreg Kroah-Hartman 	char stat_buf[32];
1923ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1924ab4382d2SGreg Kroah-Hartman 	int mmio;
1925ab4382d2SGreg Kroah-Hartman 
19264047b371SPeter Hurley 	mutex_lock(&port->mutex);
19274047b371SPeter Hurley 	uport = uart_port_check(state);
1928ab4382d2SGreg Kroah-Hartman 	if (!uport)
19294047b371SPeter Hurley 		goto out;
1930ab4382d2SGreg Kroah-Hartman 
1931ab4382d2SGreg Kroah-Hartman 	mmio = uport->iotype >= UPIO_MEM;
1932ab4382d2SGreg Kroah-Hartman 	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1933ab4382d2SGreg Kroah-Hartman 			uport->line, uart_type(uport),
1934ab4382d2SGreg Kroah-Hartman 			mmio ? "mmio:0x" : "port:",
1935ab4382d2SGreg Kroah-Hartman 			mmio ? (unsigned long long)uport->mapbase
1936ab4382d2SGreg Kroah-Hartman 			     : (unsigned long long)uport->iobase,
1937ab4382d2SGreg Kroah-Hartman 			uport->irq);
1938ab4382d2SGreg Kroah-Hartman 
1939ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN) {
1940ab4382d2SGreg Kroah-Hartman 		seq_putc(m, '\n');
19414047b371SPeter Hurley 		goto out;
1942ab4382d2SGreg Kroah-Hartman 	}
1943ab4382d2SGreg Kroah-Hartman 
1944ab4382d2SGreg Kroah-Hartman 	if (capable(CAP_SYS_ADMIN)) {
1945ab4382d2SGreg Kroah-Hartman 		pm_state = state->pm_state;
19466f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
19476f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
1948ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1949ab4382d2SGreg Kroah-Hartman 		status = uport->ops->get_mctrl(uport);
1950ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
19516f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
1952ab4382d2SGreg Kroah-Hartman 			uart_change_pm(state, pm_state);
1953ab4382d2SGreg Kroah-Hartman 
1954ab4382d2SGreg Kroah-Hartman 		seq_printf(m, " tx:%d rx:%d",
1955ab4382d2SGreg Kroah-Hartman 				uport->icount.tx, uport->icount.rx);
1956ab4382d2SGreg Kroah-Hartman 		if (uport->icount.frame)
1957968af298SPeter Hurley 			seq_printf(m, " fe:%d",	uport->icount.frame);
1958ab4382d2SGreg Kroah-Hartman 		if (uport->icount.parity)
1959968af298SPeter Hurley 			seq_printf(m, " pe:%d",	uport->icount.parity);
1960ab4382d2SGreg Kroah-Hartman 		if (uport->icount.brk)
1961968af298SPeter Hurley 			seq_printf(m, " brk:%d", uport->icount.brk);
1962ab4382d2SGreg Kroah-Hartman 		if (uport->icount.overrun)
1963968af298SPeter Hurley 			seq_printf(m, " oe:%d", uport->icount.overrun);
19644f794097SJeremy Kerr 		if (uport->icount.buf_overrun)
19654f794097SJeremy Kerr 			seq_printf(m, " bo:%d", uport->icount.buf_overrun);
1966ab4382d2SGreg Kroah-Hartman 
1967ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \
1968ab4382d2SGreg Kroah-Hartman 	if (uport->mctrl & (bit)) \
1969ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1970ab4382d2SGreg Kroah-Hartman 			strlen(stat_buf) - 2)
1971ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \
1972ab4382d2SGreg Kroah-Hartman 	if (status & (bit)) \
1973ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1974ab4382d2SGreg Kroah-Hartman 		       strlen(stat_buf) - 2)
1975ab4382d2SGreg Kroah-Hartman 
1976ab4382d2SGreg Kroah-Hartman 		stat_buf[0] = '\0';
1977ab4382d2SGreg Kroah-Hartman 		stat_buf[1] = '\0';
1978ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_RTS, "|RTS");
1979ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CTS, "|CTS");
1980ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_DTR, "|DTR");
1981ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_DSR, "|DSR");
1982ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CAR, "|CD");
1983ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_RNG, "|RI");
1984ab4382d2SGreg Kroah-Hartman 		if (stat_buf[0])
1985ab4382d2SGreg Kroah-Hartman 			stat_buf[0] = ' ';
1986ab4382d2SGreg Kroah-Hartman 
1987ab4382d2SGreg Kroah-Hartman 		seq_puts(m, stat_buf);
1988ab4382d2SGreg Kroah-Hartman 	}
1989ab4382d2SGreg Kroah-Hartman 	seq_putc(m, '\n');
1990ab4382d2SGreg Kroah-Hartman #undef STATBIT
1991ab4382d2SGreg Kroah-Hartman #undef INFOBIT
19924047b371SPeter Hurley out:
19934047b371SPeter Hurley 	mutex_unlock(&port->mutex);
1994ab4382d2SGreg Kroah-Hartman }
1995ab4382d2SGreg Kroah-Hartman 
1996ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v)
1997ab4382d2SGreg Kroah-Hartman {
1998ab4382d2SGreg Kroah-Hartman 	struct tty_driver *ttydrv = m->private;
1999ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = ttydrv->driver_state;
2000ab4382d2SGreg Kroah-Hartman 	int i;
2001ab4382d2SGreg Kroah-Hartman 
2002968af298SPeter Hurley 	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2003ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++)
2004ab4382d2SGreg Kroah-Hartman 		uart_line_info(m, drv, i);
2005ab4382d2SGreg Kroah-Hartman 	return 0;
2006ab4382d2SGreg Kroah-Hartman }
2007ab4382d2SGreg Kroah-Hartman #endif
2008ab4382d2SGreg Kroah-Hartman 
20097a49955aSAndy Shevchenko static inline bool uart_console_enabled(struct uart_port *port)
20107a49955aSAndy Shevchenko {
20117a49955aSAndy Shevchenko 	return uart_console(port) && (port->cons->flags & CON_ENABLED);
20127a49955aSAndy Shevchenko }
20137a49955aSAndy Shevchenko 
2014e0830dbfSJohan Hovold static void uart_port_spin_lock_init(struct uart_port *port)
2015f743061aSAndy Shevchenko {
2016f743061aSAndy Shevchenko 	spin_lock_init(&port->lock);
2017f743061aSAndy Shevchenko 	lockdep_set_class(&port->lock, &port_lock_key);
2018f743061aSAndy Shevchenko }
2019f743061aSAndy Shevchenko 
2020ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
20211cfe42b7SPeter Hurley /**
2022ab4382d2SGreg Kroah-Hartman  *	uart_console_write - write a console message to a serial port
2023ab4382d2SGreg Kroah-Hartman  *	@port: the port to write the message
2024ab4382d2SGreg Kroah-Hartman  *	@s: array of characters
2025ab4382d2SGreg Kroah-Hartman  *	@count: number of characters in string to write
202610afbe34SPeter Hurley  *	@putchar: function to write character to port
2027ab4382d2SGreg Kroah-Hartman  */
2028ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s,
2029ab4382d2SGreg Kroah-Hartman 			unsigned int count,
20303f8bab17SJiri Slaby 			void (*putchar)(struct uart_port *, unsigned char))
2031ab4382d2SGreg Kroah-Hartman {
2032ab4382d2SGreg Kroah-Hartman 	unsigned int i;
2033ab4382d2SGreg Kroah-Hartman 
2034ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < count; i++, s++) {
2035ab4382d2SGreg Kroah-Hartman 		if (*s == '\n')
2036ab4382d2SGreg Kroah-Hartman 			putchar(port, '\r');
2037ab4382d2SGreg Kroah-Hartman 		putchar(port, *s);
2038ab4382d2SGreg Kroah-Hartman 	}
2039ab4382d2SGreg Kroah-Hartman }
2040ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write);
2041ab4382d2SGreg Kroah-Hartman 
2042ab4382d2SGreg Kroah-Hartman /*
2043ab4382d2SGreg Kroah-Hartman  *	Check whether an invalid uart number has been specified, and
2044ab4382d2SGreg Kroah-Hartman  *	if so, search for the first available port that does have
2045ab4382d2SGreg Kroah-Hartman  *	console support.
2046ab4382d2SGreg Kroah-Hartman  */
2047ab4382d2SGreg Kroah-Hartman struct uart_port * __init
2048ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co)
2049ab4382d2SGreg Kroah-Hartman {
2050ab4382d2SGreg Kroah-Hartman 	int idx = co->index;
2051ab4382d2SGreg Kroah-Hartman 
2052ab4382d2SGreg Kroah-Hartman 	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2053ab4382d2SGreg Kroah-Hartman 				     ports[idx].membase == NULL))
2054ab4382d2SGreg Kroah-Hartman 		for (idx = 0; idx < nr; idx++)
2055ab4382d2SGreg Kroah-Hartman 			if (ports[idx].iobase != 0 ||
2056ab4382d2SGreg Kroah-Hartman 			    ports[idx].membase != NULL)
2057ab4382d2SGreg Kroah-Hartman 				break;
2058ab4382d2SGreg Kroah-Hartman 
2059ab4382d2SGreg Kroah-Hartman 	co->index = idx;
2060ab4382d2SGreg Kroah-Hartman 
2061ab4382d2SGreg Kroah-Hartman 	return ports + idx;
2062ab4382d2SGreg Kroah-Hartman }
2063ab4382d2SGreg Kroah-Hartman 
2064ab4382d2SGreg Kroah-Hartman /**
206573abaf87SPeter Hurley  *	uart_parse_earlycon - Parse earlycon options
206673abaf87SPeter Hurley  *	@p:	  ptr to 2nd field (ie., just beyond '<name>,')
206773abaf87SPeter Hurley  *	@iotype:  ptr for decoded iotype (out)
206873abaf87SPeter Hurley  *	@addr:    ptr for decoded mapbase/iobase (out)
206973abaf87SPeter Hurley  *	@options: ptr for <options> field; NULL if not present (out)
207073abaf87SPeter Hurley  *
207173abaf87SPeter Hurley  *	Decodes earlycon kernel command line parameters of the form
2072bd94c407SMasahiro Yamada  *	   earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2073bd94c407SMasahiro Yamada  *	   console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
207473abaf87SPeter Hurley  *
207573abaf87SPeter Hurley  *	The optional form
2076ff30283aSRandy Dunlap  *
207773abaf87SPeter Hurley  *	   earlycon=<name>,0x<addr>,<options>
207873abaf87SPeter Hurley  *	   console=<name>,0x<addr>,<options>
2079ff30283aSRandy Dunlap  *
208073abaf87SPeter Hurley  *	is also accepted; the returned @iotype will be UPIO_MEM.
208173abaf87SPeter Hurley  *
20828b2303deSAlexander Sverdlin  *	Returns 0 on success or -EINVAL on failure
208373abaf87SPeter Hurley  */
208446e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
208573abaf87SPeter Hurley 			char **options)
208673abaf87SPeter Hurley {
208773abaf87SPeter Hurley 	if (strncmp(p, "mmio,", 5) == 0) {
208873abaf87SPeter Hurley 		*iotype = UPIO_MEM;
208973abaf87SPeter Hurley 		p += 5;
2090bd94c407SMasahiro Yamada 	} else if (strncmp(p, "mmio16,", 7) == 0) {
2091bd94c407SMasahiro Yamada 		*iotype = UPIO_MEM16;
2092bd94c407SMasahiro Yamada 		p += 7;
209373abaf87SPeter Hurley 	} else if (strncmp(p, "mmio32,", 7) == 0) {
209473abaf87SPeter Hurley 		*iotype = UPIO_MEM32;
209573abaf87SPeter Hurley 		p += 7;
20966e63be3fSNoam Camus 	} else if (strncmp(p, "mmio32be,", 9) == 0) {
20976e63be3fSNoam Camus 		*iotype = UPIO_MEM32BE;
20986e63be3fSNoam Camus 		p += 9;
2099d215d809SMax Filippov 	} else if (strncmp(p, "mmio32native,", 13) == 0) {
2100d215d809SMax Filippov 		*iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2101d215d809SMax Filippov 			UPIO_MEM32BE : UPIO_MEM32;
2102d215d809SMax Filippov 		p += 13;
210373abaf87SPeter Hurley 	} else if (strncmp(p, "io,", 3) == 0) {
210473abaf87SPeter Hurley 		*iotype = UPIO_PORT;
210573abaf87SPeter Hurley 		p += 3;
210673abaf87SPeter Hurley 	} else if (strncmp(p, "0x", 2) == 0) {
210773abaf87SPeter Hurley 		*iotype = UPIO_MEM;
210873abaf87SPeter Hurley 	} else {
210973abaf87SPeter Hurley 		return -EINVAL;
211073abaf87SPeter Hurley 	}
211173abaf87SPeter Hurley 
21128b2303deSAlexander Sverdlin 	/*
21138b2303deSAlexander Sverdlin 	 * Before you replace it with kstrtoull(), think about options separator
21148b2303deSAlexander Sverdlin 	 * (',') it will not tolerate
21158b2303deSAlexander Sverdlin 	 */
21168b2303deSAlexander Sverdlin 	*addr = simple_strtoull(p, NULL, 0);
211773abaf87SPeter Hurley 	p = strchr(p, ',');
211873abaf87SPeter Hurley 	if (p)
211973abaf87SPeter Hurley 		p++;
212073abaf87SPeter Hurley 
212173abaf87SPeter Hurley 	*options = p;
212273abaf87SPeter Hurley 	return 0;
212373abaf87SPeter Hurley }
212473abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon);
212573abaf87SPeter Hurley 
212673abaf87SPeter Hurley /**
212702088ca6SGeert Uytterhoeven  *	uart_parse_options - Parse serial port baud/parity/bits/flow control.
2128ab4382d2SGreg Kroah-Hartman  *	@options: pointer to option string
2129ab4382d2SGreg Kroah-Hartman  *	@baud: pointer to an 'int' variable for the baud rate.
2130ab4382d2SGreg Kroah-Hartman  *	@parity: pointer to an 'int' variable for the parity.
2131ab4382d2SGreg Kroah-Hartman  *	@bits: pointer to an 'int' variable for the number of data bits.
2132ab4382d2SGreg Kroah-Hartman  *	@flow: pointer to an 'int' variable for the flow control character.
2133ab4382d2SGreg Kroah-Hartman  *
2134ab4382d2SGreg Kroah-Hartman  *	uart_parse_options decodes a string containing the serial console
2135ab4382d2SGreg Kroah-Hartman  *	options.  The format of the string is <baud><parity><bits><flow>,
2136ab4382d2SGreg Kroah-Hartman  *	eg: 115200n8r
2137ab4382d2SGreg Kroah-Hartman  */
2138ab4382d2SGreg Kroah-Hartman void
21390f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity,
21400f646b63SPaul Cercueil 		   int *bits, int *flow)
2141ab4382d2SGreg Kroah-Hartman {
21420f646b63SPaul Cercueil 	const char *s = options;
2143ab4382d2SGreg Kroah-Hartman 
2144ab4382d2SGreg Kroah-Hartman 	*baud = simple_strtoul(s, NULL, 10);
2145ab4382d2SGreg Kroah-Hartman 	while (*s >= '0' && *s <= '9')
2146ab4382d2SGreg Kroah-Hartman 		s++;
2147ab4382d2SGreg Kroah-Hartman 	if (*s)
2148ab4382d2SGreg Kroah-Hartman 		*parity = *s++;
2149ab4382d2SGreg Kroah-Hartman 	if (*s)
2150ab4382d2SGreg Kroah-Hartman 		*bits = *s++ - '0';
2151ab4382d2SGreg Kroah-Hartman 	if (*s)
2152ab4382d2SGreg Kroah-Hartman 		*flow = *s;
2153ab4382d2SGreg Kroah-Hartman }
2154ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options);
2155ab4382d2SGreg Kroah-Hartman 
2156ab4382d2SGreg Kroah-Hartman /**
2157ab4382d2SGreg Kroah-Hartman  *	uart_set_options - setup the serial console parameters
2158ab4382d2SGreg Kroah-Hartman  *	@port: pointer to the serial ports uart_port structure
2159ab4382d2SGreg Kroah-Hartman  *	@co: console pointer
2160ab4382d2SGreg Kroah-Hartman  *	@baud: baud rate
2161ab4382d2SGreg Kroah-Hartman  *	@parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2162ab4382d2SGreg Kroah-Hartman  *	@bits: number of data bits
2163ab4382d2SGreg Kroah-Hartman  *	@flow: flow control character - 'r' (rts)
2164ab4382d2SGreg Kroah-Hartman  */
2165ab4382d2SGreg Kroah-Hartman int
2166ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co,
2167ab4382d2SGreg Kroah-Hartman 		 int baud, int parity, int bits, int flow)
2168ab4382d2SGreg Kroah-Hartman {
2169ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2170ab4382d2SGreg Kroah-Hartman 	static struct ktermios dummy;
2171ab4382d2SGreg Kroah-Hartman 
2172e0830dbfSJohan Hovold 	/*
2173e0830dbfSJohan Hovold 	 * Ensure that the serial-console lock is initialised early.
2174e0830dbfSJohan Hovold 	 *
2175e0830dbfSJohan Hovold 	 * Note that the console-enabled check is needed because of kgdboc,
2176e0830dbfSJohan Hovold 	 * which can end up calling uart_set_options() for an already enabled
2177e0830dbfSJohan Hovold 	 * console via tty_find_polling_driver() and uart_poll_init().
2178e0830dbfSJohan Hovold 	 */
2179e0830dbfSJohan Hovold 	if (!uart_console_enabled(port) && !port->console_reinit)
2180d2403cadSAndy Shevchenko 		uart_port_spin_lock_init(port);
2181ab4382d2SGreg Kroah-Hartman 
2182ab4382d2SGreg Kroah-Hartman 	memset(&termios, 0, sizeof(struct ktermios));
2183ab4382d2SGreg Kroah-Hartman 
2184ba47f97aSJeffy Chen 	termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2185ba47f97aSJeffy Chen 	tty_termios_encode_baud_rate(&termios, baud, baud);
2186ab4382d2SGreg Kroah-Hartman 
2187ab4382d2SGreg Kroah-Hartman 	if (bits == 7)
2188ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS7;
2189ab4382d2SGreg Kroah-Hartman 	else
2190ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS8;
2191ab4382d2SGreg Kroah-Hartman 
2192ab4382d2SGreg Kroah-Hartman 	switch (parity) {
2193ab4382d2SGreg Kroah-Hartman 	case 'o': case 'O':
2194ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARODD;
2195df561f66SGustavo A. R. Silva 		fallthrough;
2196ab4382d2SGreg Kroah-Hartman 	case 'e': case 'E':
2197ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARENB;
2198ab4382d2SGreg Kroah-Hartman 		break;
2199ab4382d2SGreg Kroah-Hartman 	}
2200ab4382d2SGreg Kroah-Hartman 
2201ab4382d2SGreg Kroah-Hartman 	if (flow == 'r')
2202ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CRTSCTS;
2203ab4382d2SGreg Kroah-Hartman 
2204ab4382d2SGreg Kroah-Hartman 	/*
2205ab4382d2SGreg Kroah-Hartman 	 * some uarts on other side don't support no flow control.
2206ab4382d2SGreg Kroah-Hartman 	 * So we set * DTR in host uart to make them happy
2207ab4382d2SGreg Kroah-Hartman 	 */
2208ab4382d2SGreg Kroah-Hartman 	port->mctrl |= TIOCM_DTR;
2209ab4382d2SGreg Kroah-Hartman 
2210ab4382d2SGreg Kroah-Hartman 	port->ops->set_termios(port, &termios, &dummy);
2211ab4382d2SGreg Kroah-Hartman 	/*
2212ab4382d2SGreg Kroah-Hartman 	 * Allow the setting of the UART parameters with a NULL console
2213ab4382d2SGreg Kroah-Hartman 	 * too:
2214ab4382d2SGreg Kroah-Hartman 	 */
2215027b5717SPali Rohár 	if (co) {
2216ab4382d2SGreg Kroah-Hartman 		co->cflag = termios.c_cflag;
2217027b5717SPali Rohár 		co->ispeed = termios.c_ispeed;
2218027b5717SPali Rohár 		co->ospeed = termios.c_ospeed;
2219027b5717SPali Rohár 	}
2220ab4382d2SGreg Kroah-Hartman 
2221ab4382d2SGreg Kroah-Hartman 	return 0;
2222ab4382d2SGreg Kroah-Hartman }
2223ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options);
2224ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2225ab4382d2SGreg Kroah-Hartman 
2226cf75525fSJiri Slaby /**
2227cf75525fSJiri Slaby  * uart_change_pm - set power state of the port
2228cf75525fSJiri Slaby  *
2229cf75525fSJiri Slaby  * @state: port descriptor
2230cf75525fSJiri Slaby  * @pm_state: new state
2231cf75525fSJiri Slaby  *
2232cf75525fSJiri Slaby  * Locking: port->mutex has to be held
2233cf75525fSJiri Slaby  */
22346f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
22356f538fe3SLinus Walleij 			   enum uart_pm_state pm_state)
2236ab4382d2SGreg Kroah-Hartman {
22374047b371SPeter Hurley 	struct uart_port *port = uart_port_check(state);
2238ab4382d2SGreg Kroah-Hartman 
2239ab4382d2SGreg Kroah-Hartman 	if (state->pm_state != pm_state) {
22404047b371SPeter Hurley 		if (port && port->ops->pm)
2241ab4382d2SGreg Kroah-Hartman 			port->ops->pm(port, pm_state, state->pm_state);
2242ab4382d2SGreg Kroah-Hartman 		state->pm_state = pm_state;
2243ab4382d2SGreg Kroah-Hartman 	}
2244ab4382d2SGreg Kroah-Hartman }
2245ab4382d2SGreg Kroah-Hartman 
2246ab4382d2SGreg Kroah-Hartman struct uart_match {
2247ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2248ab4382d2SGreg Kroah-Hartman 	struct uart_driver *driver;
2249ab4382d2SGreg Kroah-Hartman };
2250ab4382d2SGreg Kroah-Hartman 
2251ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data)
2252ab4382d2SGreg Kroah-Hartman {
2253ab4382d2SGreg Kroah-Hartman 	struct uart_match *match = data;
2254ab4382d2SGreg Kroah-Hartman 	struct tty_driver *tty_drv = match->driver->tty_driver;
2255ab4382d2SGreg Kroah-Hartman 	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2256ab4382d2SGreg Kroah-Hartman 		match->port->line;
2257ab4382d2SGreg Kroah-Hartman 
2258ab4382d2SGreg Kroah-Hartman 	return dev->devt == devt; /* Actually, only one tty per port */
2259ab4382d2SGreg Kroah-Hartman }
2260ab4382d2SGreg Kroah-Hartman 
2261ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2262ab4382d2SGreg Kroah-Hartman {
2263ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2264ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2265ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2266ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2267ab4382d2SGreg Kroah-Hartman 
2268ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2269ab4382d2SGreg Kroah-Hartman 
2270ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
227188e2582eSLucas Stach 	if (tty_dev && device_may_wakeup(tty_dev)) {
2272aef3ad10SAndy Shevchenko 		enable_irq_wake(uport->irq);
2273ab4382d2SGreg Kroah-Hartman 		put_device(tty_dev);
2274ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2275ab4382d2SGreg Kroah-Hartman 		return 0;
2276ab4382d2SGreg Kroah-Hartman 	}
22775a65dcc0SFederico Vaga 	put_device(tty_dev);
22785a65dcc0SFederico Vaga 
2279c9d2325cSVijaya Krishna Nivarthi 	/*
2280c9d2325cSVijaya Krishna Nivarthi 	 * Nothing to do if the console is not suspending
2281c9d2325cSVijaya Krishna Nivarthi 	 * except stop_rx to prevent any asynchronous data
2282cfab87c2SVijaya Krishna Nivarthi 	 * over RX line. However ensure that we will be
2283cfab87c2SVijaya Krishna Nivarthi 	 * able to Re-start_rx later.
2284c9d2325cSVijaya Krishna Nivarthi 	 */
2285c9d2325cSVijaya Krishna Nivarthi 	if (!console_suspend_enabled && uart_console(uport)) {
2286cfab87c2SVijaya Krishna Nivarthi 		if (uport->ops->start_rx)
2287c9d2325cSVijaya Krishna Nivarthi 			uport->ops->stop_rx(uport);
2288b164c972SPeter Hurley 		goto unlock;
2289c9d2325cSVijaya Krishna Nivarthi 	}
2290b164c972SPeter Hurley 
2291ab4382d2SGreg Kroah-Hartman 	uport->suspended = 1;
2292ab4382d2SGreg Kroah-Hartman 
2293d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
2294ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2295ab4382d2SGreg Kroah-Hartman 		int tries;
229618c9d4a3SAl Cooper 		unsigned int mctrl;
2297ab4382d2SGreg Kroah-Hartman 
229880f02d54SPeter Hurley 		tty_port_set_suspended(port, 1);
2299d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
2300ab4382d2SGreg Kroah-Hartman 
2301ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2302ab4382d2SGreg Kroah-Hartman 		ops->stop_tx(uport);
2303ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
230418c9d4a3SAl Cooper 		/* save mctrl so it can be restored on resume */
230518c9d4a3SAl Cooper 		mctrl = uport->mctrl;
230618c9d4a3SAl Cooper 		uport->mctrl = 0;
2307ab4382d2SGreg Kroah-Hartman 		ops->stop_rx(uport);
2308ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2309ab4382d2SGreg Kroah-Hartman 
2310ab4382d2SGreg Kroah-Hartman 		/*
2311ab4382d2SGreg Kroah-Hartman 		 * Wait for the transmitter to empty.
2312ab4382d2SGreg Kroah-Hartman 		 */
2313ab4382d2SGreg Kroah-Hartman 		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2314ab4382d2SGreg Kroah-Hartman 			msleep(10);
2315ab4382d2SGreg Kroah-Hartman 		if (!tries)
2316cade3580SAndy Shevchenko 			dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2317cade3580SAndy Shevchenko 				uport->name);
2318ab4382d2SGreg Kroah-Hartman 
2319ab4382d2SGreg Kroah-Hartman 		ops->shutdown(uport);
232018c9d4a3SAl Cooper 		uport->mctrl = mctrl;
2321ab4382d2SGreg Kroah-Hartman 	}
2322ab4382d2SGreg Kroah-Hartman 
2323ab4382d2SGreg Kroah-Hartman 	/*
2324ab4382d2SGreg Kroah-Hartman 	 * Disable the console device before suspending.
2325ab4382d2SGreg Kroah-Hartman 	 */
2326b164c972SPeter Hurley 	if (uart_console(uport))
2327ab4382d2SGreg Kroah-Hartman 		console_stop(uport->cons);
2328ab4382d2SGreg Kroah-Hartman 
23296f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
2330b164c972SPeter Hurley unlock:
2331ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2332ab4382d2SGreg Kroah-Hartman 
2333ab4382d2SGreg Kroah-Hartman 	return 0;
2334ab4382d2SGreg Kroah-Hartman }
233515dc475bSJiri Slaby EXPORT_SYMBOL(uart_suspend_port);
2336ab4382d2SGreg Kroah-Hartman 
2337ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2338ab4382d2SGreg Kroah-Hartman {
2339ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2340ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2341ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2342ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2343ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2344ab4382d2SGreg Kroah-Hartman 
2345ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2346ab4382d2SGreg Kroah-Hartman 
2347ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2348ab4382d2SGreg Kroah-Hartman 	if (!uport->suspended && device_may_wakeup(tty_dev)) {
2349aef3ad10SAndy Shevchenko 		if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2350ab4382d2SGreg Kroah-Hartman 			disable_irq_wake(uport->irq);
23515a65dcc0SFederico Vaga 		put_device(tty_dev);
2352ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2353ab4382d2SGreg Kroah-Hartman 		return 0;
2354ab4382d2SGreg Kroah-Hartman 	}
23555a65dcc0SFederico Vaga 	put_device(tty_dev);
2356ab4382d2SGreg Kroah-Hartman 	uport->suspended = 0;
2357ab4382d2SGreg Kroah-Hartman 
2358ab4382d2SGreg Kroah-Hartman 	/*
2359ab4382d2SGreg Kroah-Hartman 	 * Re-enable the console device after suspending.
2360ab4382d2SGreg Kroah-Hartman 	 */
23615933a161SYin Kangkai 	if (uart_console(uport)) {
2362ab4382d2SGreg Kroah-Hartman 		/*
2363ab4382d2SGreg Kroah-Hartman 		 * First try to use the console cflag setting.
2364ab4382d2SGreg Kroah-Hartman 		 */
2365ab4382d2SGreg Kroah-Hartman 		memset(&termios, 0, sizeof(struct ktermios));
2366ab4382d2SGreg Kroah-Hartman 		termios.c_cflag = uport->cons->cflag;
2367027b5717SPali Rohár 		termios.c_ispeed = uport->cons->ispeed;
2368027b5717SPali Rohár 		termios.c_ospeed = uport->cons->ospeed;
2369ab4382d2SGreg Kroah-Hartman 
2370ab4382d2SGreg Kroah-Hartman 		/*
2371ab4382d2SGreg Kroah-Hartman 		 * If that's unset, use the tty termios setting.
2372ab4382d2SGreg Kroah-Hartman 		 */
2373adc8d746SAlan Cox 		if (port->tty && termios.c_cflag == 0)
2374adc8d746SAlan Cox 			termios = port->tty->termios;
2375ab4382d2SGreg Kroah-Hartman 
237694abc56fSNing Jiang 		if (console_suspend_enabled)
23776f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2378ab4382d2SGreg Kroah-Hartman 		uport->ops->set_termios(uport, &termios, NULL);
2379cfab87c2SVijaya Krishna Nivarthi 		if (!console_suspend_enabled && uport->ops->start_rx)
2380cfab87c2SVijaya Krishna Nivarthi 			uport->ops->start_rx(uport);
23815933a161SYin Kangkai 		if (console_suspend_enabled)
2382ab4382d2SGreg Kroah-Hartman 			console_start(uport->cons);
2383ab4382d2SGreg Kroah-Hartman 	}
2384ab4382d2SGreg Kroah-Hartman 
238580f02d54SPeter Hurley 	if (tty_port_suspended(port)) {
2386ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2387ab4382d2SGreg Kroah-Hartman 		int ret;
2388ab4382d2SGreg Kroah-Hartman 
23896f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2390ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2391ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
2392ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2393ab4382d2SGreg Kroah-Hartman 		if (console_suspend_enabled || !uart_console(uport)) {
2394ab4382d2SGreg Kroah-Hartman 			/* Protected by port mutex for now */
2395ab4382d2SGreg Kroah-Hartman 			struct tty_struct *tty = port->tty;
23964ed71addSTamseel Shams 
2397ab4382d2SGreg Kroah-Hartman 			ret = ops->startup(uport);
2398ab4382d2SGreg Kroah-Hartman 			if (ret == 0) {
2399ab4382d2SGreg Kroah-Hartman 				if (tty)
2400ab4382d2SGreg Kroah-Hartman 					uart_change_speed(tty, state, NULL);
2401ab4382d2SGreg Kroah-Hartman 				spin_lock_irq(&uport->lock);
2402ab4382d2SGreg Kroah-Hartman 				ops->set_mctrl(uport, uport->mctrl);
2403ab4382d2SGreg Kroah-Hartman 				ops->start_tx(uport);
2404ab4382d2SGreg Kroah-Hartman 				spin_unlock_irq(&uport->lock);
2405d41861caSPeter Hurley 				tty_port_set_initialized(port, 1);
2406ab4382d2SGreg Kroah-Hartman 			} else {
2407ab4382d2SGreg Kroah-Hartman 				/*
2408ab4382d2SGreg Kroah-Hartman 				 * Failed to resume - maybe hardware went away?
2409ab4382d2SGreg Kroah-Hartman 				 * Clear the "initialized" flag so we won't try
2410ab4382d2SGreg Kroah-Hartman 				 * to call the low level drivers shutdown method.
2411ab4382d2SGreg Kroah-Hartman 				 */
2412ab4382d2SGreg Kroah-Hartman 				uart_shutdown(tty, state);
2413ab4382d2SGreg Kroah-Hartman 			}
2414ab4382d2SGreg Kroah-Hartman 		}
2415ab4382d2SGreg Kroah-Hartman 
241680f02d54SPeter Hurley 		tty_port_set_suspended(port, 0);
2417ab4382d2SGreg Kroah-Hartman 	}
2418ab4382d2SGreg Kroah-Hartman 
2419ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2420ab4382d2SGreg Kroah-Hartman 
2421ab4382d2SGreg Kroah-Hartman 	return 0;
2422ab4382d2SGreg Kroah-Hartman }
242315dc475bSJiri Slaby EXPORT_SYMBOL(uart_resume_port);
2424ab4382d2SGreg Kroah-Hartman 
2425ab4382d2SGreg Kroah-Hartman static inline void
2426ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port)
2427ab4382d2SGreg Kroah-Hartman {
2428ab4382d2SGreg Kroah-Hartman 	char address[64];
2429ab4382d2SGreg Kroah-Hartman 
2430ab4382d2SGreg Kroah-Hartman 	switch (port->iotype) {
2431ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2432ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2433ab4382d2SGreg Kroah-Hartman 		break;
2434ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2435ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2436ab4382d2SGreg Kroah-Hartman 			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2437ab4382d2SGreg Kroah-Hartman 		break;
2438ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2439bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2440ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
24413ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2442ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2443ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2444ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2445ab4382d2SGreg Kroah-Hartman 			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2446ab4382d2SGreg Kroah-Hartman 		break;
2447ab4382d2SGreg Kroah-Hartman 	default:
2448ab4382d2SGreg Kroah-Hartman 		strlcpy(address, "*unknown*", sizeof(address));
2449ab4382d2SGreg Kroah-Hartman 		break;
2450ab4382d2SGreg Kroah-Hartman 	}
2451ab4382d2SGreg Kroah-Hartman 
2452cade3580SAndy Shevchenko 	pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
245368ed7e1cSJames Bottomley 	       port->dev ? dev_name(port->dev) : "",
245468ed7e1cSJames Bottomley 	       port->dev ? ": " : "",
2455cade3580SAndy Shevchenko 	       port->name,
24567d12b976SKees Cook 	       address, port->irq, port->uartclk / 16, uart_type(port));
2457e7b91932SMaciej W. Rozycki 
2458e7b91932SMaciej W. Rozycki 	/* The magic multiplier feature is a bit obscure, so report it too.  */
2459e7b91932SMaciej W. Rozycki 	if (port->flags & UPF_MAGIC_MULTIPLIER)
2460e7b91932SMaciej W. Rozycki 		pr_info("%s%s%s extra baud rates supported: %d, %d",
2461e7b91932SMaciej W. Rozycki 			port->dev ? dev_name(port->dev) : "",
2462e7b91932SMaciej W. Rozycki 			port->dev ? ": " : "",
2463e7b91932SMaciej W. Rozycki 			port->name,
2464e7b91932SMaciej W. Rozycki 			port->uartclk / 8, port->uartclk / 4);
2465ab4382d2SGreg Kroah-Hartman }
2466ab4382d2SGreg Kroah-Hartman 
2467ab4382d2SGreg Kroah-Hartman static void
2468ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2469ab4382d2SGreg Kroah-Hartman 		    struct uart_port *port)
2470ab4382d2SGreg Kroah-Hartman {
2471ab4382d2SGreg Kroah-Hartman 	unsigned int flags;
2472ab4382d2SGreg Kroah-Hartman 
2473ab4382d2SGreg Kroah-Hartman 	/*
2474ab4382d2SGreg Kroah-Hartman 	 * If there isn't a port here, don't do anything further.
2475ab4382d2SGreg Kroah-Hartman 	 */
2476ab4382d2SGreg Kroah-Hartman 	if (!port->iobase && !port->mapbase && !port->membase)
2477ab4382d2SGreg Kroah-Hartman 		return;
2478ab4382d2SGreg Kroah-Hartman 
2479ab4382d2SGreg Kroah-Hartman 	/*
2480ab4382d2SGreg Kroah-Hartman 	 * Now do the auto configuration stuff.  Note that config_port
2481ab4382d2SGreg Kroah-Hartman 	 * is expected to claim the resources and map the port for us.
2482ab4382d2SGreg Kroah-Hartman 	 */
2483ab4382d2SGreg Kroah-Hartman 	flags = 0;
2484ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_AUTO_IRQ)
2485ab4382d2SGreg Kroah-Hartman 		flags |= UART_CONFIG_IRQ;
2486ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_BOOT_AUTOCONF) {
2487ab4382d2SGreg Kroah-Hartman 		if (!(port->flags & UPF_FIXED_TYPE)) {
2488ab4382d2SGreg Kroah-Hartman 			port->type = PORT_UNKNOWN;
2489ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_TYPE;
2490ab4382d2SGreg Kroah-Hartman 		}
2491ab4382d2SGreg Kroah-Hartman 		port->ops->config_port(port, flags);
2492ab4382d2SGreg Kroah-Hartman 	}
2493ab4382d2SGreg Kroah-Hartman 
2494ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_UNKNOWN) {
2495ab4382d2SGreg Kroah-Hartman 		unsigned long flags;
2496ab4382d2SGreg Kroah-Hartman 
2497ab4382d2SGreg Kroah-Hartman 		uart_report_port(drv, port);
2498ab4382d2SGreg Kroah-Hartman 
2499ab4382d2SGreg Kroah-Hartman 		/* Power up port for set_mctrl() */
25006f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2501ab4382d2SGreg Kroah-Hartman 
2502ab4382d2SGreg Kroah-Hartman 		/*
2503ab4382d2SGreg Kroah-Hartman 		 * Ensure that the modem control lines are de-activated.
2504ab4382d2SGreg Kroah-Hartman 		 * keep the DTR setting that is set in uart_set_options()
2505ab4382d2SGreg Kroah-Hartman 		 * We probably don't need a spinlock around this, but
2506ab4382d2SGreg Kroah-Hartman 		 */
2507ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
250893a770b7SLukas Wunner 		port->mctrl &= TIOCM_DTR;
25092dd8a74fSLukas Wunner 		if (port->rs485.flags & SER_RS485_ENABLED &&
25102dd8a74fSLukas Wunner 		    !(port->rs485.flags & SER_RS485_RTS_AFTER_SEND))
25112dd8a74fSLukas Wunner 			port->mctrl |= TIOCM_RTS;
251293a770b7SLukas Wunner 		port->ops->set_mctrl(port, port->mctrl);
2513ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
2514ab4382d2SGreg Kroah-Hartman 
2515ab4382d2SGreg Kroah-Hartman 		/*
2516ab4382d2SGreg Kroah-Hartman 		 * If this driver supports console, and it hasn't been
2517ab4382d2SGreg Kroah-Hartman 		 * successfully registered yet, try to re-register it.
2518ab4382d2SGreg Kroah-Hartman 		 * It may be that the port was not available.
2519ab4382d2SGreg Kroah-Hartman 		 */
2520ab4382d2SGreg Kroah-Hartman 		if (port->cons && !(port->cons->flags & CON_ENABLED))
2521ab4382d2SGreg Kroah-Hartman 			register_console(port->cons);
2522ab4382d2SGreg Kroah-Hartman 
2523ab4382d2SGreg Kroah-Hartman 		/*
2524ab4382d2SGreg Kroah-Hartman 		 * Power down all ports by default, except the
2525ab4382d2SGreg Kroah-Hartman 		 * console if we have one.
2526ab4382d2SGreg Kroah-Hartman 		 */
2527ab4382d2SGreg Kroah-Hartman 		if (!uart_console(port))
25286f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_OFF);
2529ab4382d2SGreg Kroah-Hartman 	}
2530ab4382d2SGreg Kroah-Hartman }
2531ab4382d2SGreg Kroah-Hartman 
2532ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2533ab4382d2SGreg Kroah-Hartman 
2534ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2535ab4382d2SGreg Kroah-Hartman {
2536ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2537ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
253849c02304SPeter Hurley 	struct tty_port *tport;
2539ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2540ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
2541ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2542ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2543ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
254449c02304SPeter Hurley 	int ret = 0;
2545ab4382d2SGreg Kroah-Hartman 
254649c02304SPeter Hurley 	tport = &state->port;
254749c02304SPeter Hurley 	mutex_lock(&tport->mutex);
254849c02304SPeter Hurley 
25494047b371SPeter Hurley 	port = uart_port_check(state);
25504047b371SPeter Hurley 	if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
255149c02304SPeter Hurley 		ret = -1;
255249c02304SPeter Hurley 		goto out;
255349c02304SPeter Hurley 	}
2554ab4382d2SGreg Kroah-Hartman 
2555c7f3e708SAnton Vorontsov 	if (port->ops->poll_init) {
2556c7f3e708SAnton Vorontsov 		/*
2557d41861caSPeter Hurley 		 * We don't set initialized as we only initialized the hw,
2558d41861caSPeter Hurley 		 * e.g. state->xmit is still uninitialized.
2559c7f3e708SAnton Vorontsov 		 */
2560d41861caSPeter Hurley 		if (!tty_port_initialized(tport))
2561c7f3e708SAnton Vorontsov 			ret = port->ops->poll_init(port);
2562c7f3e708SAnton Vorontsov 	}
2563c7f3e708SAnton Vorontsov 
256449c02304SPeter Hurley 	if (!ret && options) {
2565ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
256649c02304SPeter Hurley 		ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2567ab4382d2SGreg Kroah-Hartman 	}
256849c02304SPeter Hurley out:
256949c02304SPeter Hurley 	mutex_unlock(&tport->mutex);
257049c02304SPeter Hurley 	return ret;
2571ab4382d2SGreg Kroah-Hartman }
2572ab4382d2SGreg Kroah-Hartman 
2573ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line)
2574ab4382d2SGreg Kroah-Hartman {
2575ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2576ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2577ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
25789ed19428SPeter Hurley 	int ret = -1;
2579ab4382d2SGreg Kroah-Hartman 
25809ed19428SPeter Hurley 	port = uart_port_ref(state);
2581ef510beaSAndy Shevchenko 	if (port) {
25829ed19428SPeter Hurley 		ret = port->ops->poll_get_char(port);
25839ed19428SPeter Hurley 		uart_port_deref(port);
25849ed19428SPeter Hurley 	}
258522077b09SJiri Slaby 
25869ed19428SPeter Hurley 	return ret;
2587ab4382d2SGreg Kroah-Hartman }
2588ab4382d2SGreg Kroah-Hartman 
2589ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2590ab4382d2SGreg Kroah-Hartman {
2591ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2592ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2593ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2594ab4382d2SGreg Kroah-Hartman 
25959ed19428SPeter Hurley 	port = uart_port_ref(state);
25969ed19428SPeter Hurley 	if (!port)
25979ed19428SPeter Hurley 		return;
2598c7d44a02SDoug Anderson 
2599c7d44a02SDoug Anderson 	if (ch == '\n')
2600c7d44a02SDoug Anderson 		port->ops->poll_put_char(port, '\r');
2601ab4382d2SGreg Kroah-Hartman 	port->ops->poll_put_char(port, ch);
26029ed19428SPeter Hurley 	uart_port_deref(port);
2603ab4382d2SGreg Kroah-Hartman }
2604ab4382d2SGreg Kroah-Hartman #endif
2605ab4382d2SGreg Kroah-Hartman 
2606ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = {
26074cdd17baSJiri Slaby 	.install	= uart_install,
2608ab4382d2SGreg Kroah-Hartman 	.open		= uart_open,
2609ab4382d2SGreg Kroah-Hartman 	.close		= uart_close,
2610ab4382d2SGreg Kroah-Hartman 	.write		= uart_write,
2611ab4382d2SGreg Kroah-Hartman 	.put_char	= uart_put_char,
2612ab4382d2SGreg Kroah-Hartman 	.flush_chars	= uart_flush_chars,
2613ab4382d2SGreg Kroah-Hartman 	.write_room	= uart_write_room,
2614ab4382d2SGreg Kroah-Hartman 	.chars_in_buffer= uart_chars_in_buffer,
2615ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= uart_flush_buffer,
2616ab4382d2SGreg Kroah-Hartman 	.ioctl		= uart_ioctl,
2617ab4382d2SGreg Kroah-Hartman 	.throttle	= uart_throttle,
2618ab4382d2SGreg Kroah-Hartman 	.unthrottle	= uart_unthrottle,
2619ab4382d2SGreg Kroah-Hartman 	.send_xchar	= uart_send_xchar,
2620ab4382d2SGreg Kroah-Hartman 	.set_termios	= uart_set_termios,
2621ab4382d2SGreg Kroah-Hartman 	.set_ldisc	= uart_set_ldisc,
2622ab4382d2SGreg Kroah-Hartman 	.stop		= uart_stop,
2623ab4382d2SGreg Kroah-Hartman 	.start		= uart_start,
2624ab4382d2SGreg Kroah-Hartman 	.hangup		= uart_hangup,
2625ab4382d2SGreg Kroah-Hartman 	.break_ctl	= uart_break_ctl,
2626ab4382d2SGreg Kroah-Hartman 	.wait_until_sent= uart_wait_until_sent,
2627ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
26288a8dcabfSChristoph Hellwig 	.proc_show	= uart_proc_show,
2629ab4382d2SGreg Kroah-Hartman #endif
2630ab4382d2SGreg Kroah-Hartman 	.tiocmget	= uart_tiocmget,
2631ab4382d2SGreg Kroah-Hartman 	.tiocmset	= uart_tiocmset,
26325099d234SAl Viro 	.set_serial	= uart_set_info_user,
26335099d234SAl Viro 	.get_serial	= uart_get_info_user,
2634ab4382d2SGreg Kroah-Hartman 	.get_icount	= uart_get_icount,
2635ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2636ab4382d2SGreg Kroah-Hartman 	.poll_init	= uart_poll_init,
2637ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= uart_poll_get_char,
2638ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= uart_poll_put_char,
2639ab4382d2SGreg Kroah-Hartman #endif
2640ab4382d2SGreg Kroah-Hartman };
2641ab4382d2SGreg Kroah-Hartman 
2642ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = {
2643ab4382d2SGreg Kroah-Hartman 	.carrier_raised = uart_carrier_raised,
2644ab4382d2SGreg Kroah-Hartman 	.dtr_rts	= uart_dtr_rts,
2645b3b57646SRob Herring 	.activate	= uart_port_activate,
2646761ed4a9SRob Herring 	.shutdown	= uart_tty_port_shutdown,
2647ab4382d2SGreg Kroah-Hartman };
2648ab4382d2SGreg Kroah-Hartman 
2649ab4382d2SGreg Kroah-Hartman /**
2650ab4382d2SGreg Kroah-Hartman  *	uart_register_driver - register a driver with the uart core layer
2651ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2652ab4382d2SGreg Kroah-Hartman  *
2653ab4382d2SGreg Kroah-Hartman  *	Register a uart driver with the core driver.  We in turn register
2654ab4382d2SGreg Kroah-Hartman  *	with the tty layer, and initialise the core driver per-port state.
2655ab4382d2SGreg Kroah-Hartman  *
2656ab4382d2SGreg Kroah-Hartman  *	We have a proc file in /proc/tty/driver which is named after the
2657ab4382d2SGreg Kroah-Hartman  *	normal driver.
2658ab4382d2SGreg Kroah-Hartman  *
2659ab4382d2SGreg Kroah-Hartman  *	drv->port should be NULL, and the per-port structures should be
2660ab4382d2SGreg Kroah-Hartman  *	registered using uart_add_one_port after this call has succeeded.
2661ab4382d2SGreg Kroah-Hartman  */
2662ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv)
2663ab4382d2SGreg Kroah-Hartman {
2664ab4382d2SGreg Kroah-Hartman 	struct tty_driver *normal;
2665050dfc09SSergey Organov 	int i, retval = -ENOMEM;
2666ab4382d2SGreg Kroah-Hartman 
2667ab4382d2SGreg Kroah-Hartman 	BUG_ON(drv->state);
2668ab4382d2SGreg Kroah-Hartman 
2669ab4382d2SGreg Kroah-Hartman 	/*
2670ab4382d2SGreg Kroah-Hartman 	 * Maybe we should be using a slab cache for this, especially if
2671ab4382d2SGreg Kroah-Hartman 	 * we have a large number of ports to handle.
2672ab4382d2SGreg Kroah-Hartman 	 */
26736396bb22SKees Cook 	drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2674ab4382d2SGreg Kroah-Hartman 	if (!drv->state)
2675ab4382d2SGreg Kroah-Hartman 		goto out;
2676ab4382d2SGreg Kroah-Hartman 
267739b7b42bSJiri Slaby 	normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
267839b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
267939b7b42bSJiri Slaby 	if (IS_ERR(normal)) {
268039b7b42bSJiri Slaby 		retval = PTR_ERR(normal);
2681ab4382d2SGreg Kroah-Hartman 		goto out_kfree;
268239b7b42bSJiri Slaby 	}
2683ab4382d2SGreg Kroah-Hartman 
2684ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = normal;
2685ab4382d2SGreg Kroah-Hartman 
2686ab4382d2SGreg Kroah-Hartman 	normal->driver_name	= drv->driver_name;
2687ab4382d2SGreg Kroah-Hartman 	normal->name		= drv->dev_name;
2688ab4382d2SGreg Kroah-Hartman 	normal->major		= drv->major;
2689ab4382d2SGreg Kroah-Hartman 	normal->minor_start	= drv->minor;
2690ab4382d2SGreg Kroah-Hartman 	normal->type		= TTY_DRIVER_TYPE_SERIAL;
2691ab4382d2SGreg Kroah-Hartman 	normal->subtype		= SERIAL_TYPE_NORMAL;
2692ab4382d2SGreg Kroah-Hartman 	normal->init_termios	= tty_std_termios;
2693ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2694ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2695ab4382d2SGreg Kroah-Hartman 	normal->driver_state    = drv;
2696ab4382d2SGreg Kroah-Hartman 	tty_set_operations(normal, &uart_ops);
2697ab4382d2SGreg Kroah-Hartman 
2698ab4382d2SGreg Kroah-Hartman 	/*
2699ab4382d2SGreg Kroah-Hartman 	 * Initialise the UART state(s).
2700ab4382d2SGreg Kroah-Hartman 	 */
2701ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++) {
2702ab4382d2SGreg Kroah-Hartman 		struct uart_state *state = drv->state + i;
2703ab4382d2SGreg Kroah-Hartman 		struct tty_port *port = &state->port;
2704ab4382d2SGreg Kroah-Hartman 
2705ab4382d2SGreg Kroah-Hartman 		tty_port_init(port);
2706ab4382d2SGreg Kroah-Hartman 		port->ops = &uart_port_ops;
2707ab4382d2SGreg Kroah-Hartman 	}
2708ab4382d2SGreg Kroah-Hartman 
2709ab4382d2SGreg Kroah-Hartman 	retval = tty_register_driver(normal);
2710ab4382d2SGreg Kroah-Hartman 	if (retval >= 0)
2711ab4382d2SGreg Kroah-Hartman 		return retval;
2712ab4382d2SGreg Kroah-Hartman 
2713191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2714191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
27159f90a4ddSJiri Slaby 	tty_driver_kref_put(normal);
2716ab4382d2SGreg Kroah-Hartman out_kfree:
2717ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
2718ab4382d2SGreg Kroah-Hartman out:
2719050dfc09SSergey Organov 	return retval;
2720ab4382d2SGreg Kroah-Hartman }
272115dc475bSJiri Slaby EXPORT_SYMBOL(uart_register_driver);
2722ab4382d2SGreg Kroah-Hartman 
2723ab4382d2SGreg Kroah-Hartman /**
2724ab4382d2SGreg Kroah-Hartman  *	uart_unregister_driver - remove a driver from the uart core layer
2725ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2726ab4382d2SGreg Kroah-Hartman  *
2727ab4382d2SGreg Kroah-Hartman  *	Remove all references to a driver from the core driver.  The low
2728ab4382d2SGreg Kroah-Hartman  *	level driver must have removed all its ports via the
2729ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port() if it registered them with uart_add_one_port().
2730ab4382d2SGreg Kroah-Hartman  *	(ie, drv->port == NULL)
2731ab4382d2SGreg Kroah-Hartman  */
2732ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv)
2733ab4382d2SGreg Kroah-Hartman {
2734ab4382d2SGreg Kroah-Hartman 	struct tty_driver *p = drv->tty_driver;
2735191c5f10SJiri Slaby 	unsigned int i;
2736191c5f10SJiri Slaby 
2737ab4382d2SGreg Kroah-Hartman 	tty_unregister_driver(p);
27389f90a4ddSJiri Slaby 	tty_driver_kref_put(p);
2739191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2740191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2741ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
27421e66cdedSAlan Cox 	drv->state = NULL;
2743ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = NULL;
2744ab4382d2SGreg Kroah-Hartman }
274515dc475bSJiri Slaby EXPORT_SYMBOL(uart_unregister_driver);
2746ab4382d2SGreg Kroah-Hartman 
2747ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index)
2748ab4382d2SGreg Kroah-Hartman {
2749ab4382d2SGreg Kroah-Hartman 	struct uart_driver *p = co->data;
2750ab4382d2SGreg Kroah-Hartman 	*index = co->index;
2751ab4382d2SGreg Kroah-Hartman 	return p->tty_driver;
2752ab4382d2SGreg Kroah-Hartman }
2753488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device);
2754ab4382d2SGreg Kroah-Hartman 
2755143c02c8SAndy Shevchenko static ssize_t uartclk_show(struct device *dev,
27566915c0e4STomas Hlavacek 	struct device_attribute *attr, char *buf)
27576915c0e4STomas Hlavacek {
2758bebe73e3SAlan Cox 	struct serial_struct tmp;
27596915c0e4STomas Hlavacek 	struct tty_port *port = dev_get_drvdata(dev);
2760b1b79916STomas Hlavacek 
27619f109694SAlan Cox 	uart_get_info(port, &tmp);
27629cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.baud_base * 16);
27636915c0e4STomas Hlavacek }
27646915c0e4STomas Hlavacek 
2765143c02c8SAndy Shevchenko static ssize_t type_show(struct device *dev,
2766373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2767373bac4cSAlan Cox {
2768373bac4cSAlan Cox 	struct serial_struct tmp;
2769373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2770373bac4cSAlan Cox 
2771373bac4cSAlan Cox 	uart_get_info(port, &tmp);
27729cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.type);
2773373bac4cSAlan Cox }
2774143c02c8SAndy Shevchenko 
2775143c02c8SAndy Shevchenko static ssize_t line_show(struct device *dev,
2776373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2777373bac4cSAlan Cox {
2778373bac4cSAlan Cox 	struct serial_struct tmp;
2779373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2780373bac4cSAlan Cox 
2781373bac4cSAlan Cox 	uart_get_info(port, &tmp);
27829cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.line);
2783373bac4cSAlan Cox }
2784373bac4cSAlan Cox 
2785143c02c8SAndy Shevchenko static ssize_t port_show(struct device *dev,
2786373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2787373bac4cSAlan Cox {
2788373bac4cSAlan Cox 	struct serial_struct tmp;
2789373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2790fd985e1dSAndrew Morton 	unsigned long ioaddr;
2791373bac4cSAlan Cox 
2792373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2793fd985e1dSAndrew Morton 	ioaddr = tmp.port;
2794fd985e1dSAndrew Morton 	if (HIGH_BITS_OFFSET)
2795fd985e1dSAndrew Morton 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
27969cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", ioaddr);
2797373bac4cSAlan Cox }
2798373bac4cSAlan Cox 
2799143c02c8SAndy Shevchenko static ssize_t irq_show(struct device *dev,
2800373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2801373bac4cSAlan Cox {
2802373bac4cSAlan Cox 	struct serial_struct tmp;
2803373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2804373bac4cSAlan Cox 
2805373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28069cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.irq);
2807373bac4cSAlan Cox }
2808373bac4cSAlan Cox 
2809143c02c8SAndy Shevchenko static ssize_t flags_show(struct device *dev,
2810373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2811373bac4cSAlan Cox {
2812373bac4cSAlan Cox 	struct serial_struct tmp;
2813373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2814373bac4cSAlan Cox 
2815373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28169cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%X\n", tmp.flags);
2817373bac4cSAlan Cox }
2818373bac4cSAlan Cox 
2819143c02c8SAndy Shevchenko static ssize_t xmit_fifo_size_show(struct device *dev,
2820373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2821373bac4cSAlan Cox {
2822373bac4cSAlan Cox 	struct serial_struct tmp;
2823373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2824373bac4cSAlan Cox 
2825373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28269cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2827373bac4cSAlan Cox }
2828373bac4cSAlan Cox 
2829143c02c8SAndy Shevchenko static ssize_t close_delay_show(struct device *dev,
2830373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2831373bac4cSAlan Cox {
2832373bac4cSAlan Cox 	struct serial_struct tmp;
2833373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2834373bac4cSAlan Cox 
2835373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28369cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.close_delay);
2837373bac4cSAlan Cox }
2838373bac4cSAlan Cox 
2839143c02c8SAndy Shevchenko static ssize_t closing_wait_show(struct device *dev,
2840373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2841373bac4cSAlan Cox {
2842373bac4cSAlan Cox 	struct serial_struct tmp;
2843373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2844373bac4cSAlan Cox 
2845373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28469cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.closing_wait);
2847373bac4cSAlan Cox }
2848373bac4cSAlan Cox 
2849143c02c8SAndy Shevchenko static ssize_t custom_divisor_show(struct device *dev,
2850373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2851373bac4cSAlan Cox {
2852373bac4cSAlan Cox 	struct serial_struct tmp;
2853373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2854373bac4cSAlan Cox 
2855373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28569cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.custom_divisor);
2857373bac4cSAlan Cox }
2858373bac4cSAlan Cox 
2859143c02c8SAndy Shevchenko static ssize_t io_type_show(struct device *dev,
2860373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2861373bac4cSAlan Cox {
2862373bac4cSAlan Cox 	struct serial_struct tmp;
2863373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2864373bac4cSAlan Cox 
2865373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28669cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.io_type);
2867373bac4cSAlan Cox }
2868373bac4cSAlan Cox 
2869143c02c8SAndy Shevchenko static ssize_t iomem_base_show(struct device *dev,
2870373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2871373bac4cSAlan Cox {
2872373bac4cSAlan Cox 	struct serial_struct tmp;
2873373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2874373bac4cSAlan Cox 
2875373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28769cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
2877373bac4cSAlan Cox }
2878373bac4cSAlan Cox 
2879143c02c8SAndy Shevchenko static ssize_t iomem_reg_shift_show(struct device *dev,
2880373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2881373bac4cSAlan Cox {
2882373bac4cSAlan Cox 	struct serial_struct tmp;
2883373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2884373bac4cSAlan Cox 
2885373bac4cSAlan Cox 	uart_get_info(port, &tmp);
28869cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
2887373bac4cSAlan Cox }
2888373bac4cSAlan Cox 
2889a3cb39d2SAndy Shevchenko static ssize_t console_show(struct device *dev,
2890a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, char *buf)
2891a3cb39d2SAndy Shevchenko {
2892a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2893a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2894a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2895a3cb39d2SAndy Shevchenko 	bool console = false;
2896a3cb39d2SAndy Shevchenko 
2897a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2898a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2899a3cb39d2SAndy Shevchenko 	if (uport)
2900a3cb39d2SAndy Shevchenko 		console = uart_console_enabled(uport);
2901a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
2902a3cb39d2SAndy Shevchenko 
2903a3cb39d2SAndy Shevchenko 	return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2904a3cb39d2SAndy Shevchenko }
2905a3cb39d2SAndy Shevchenko 
2906a3cb39d2SAndy Shevchenko static ssize_t console_store(struct device *dev,
2907a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, const char *buf, size_t count)
2908a3cb39d2SAndy Shevchenko {
2909a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2910a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2911a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2912a3cb39d2SAndy Shevchenko 	bool oldconsole, newconsole;
2913a3cb39d2SAndy Shevchenko 	int ret;
2914a3cb39d2SAndy Shevchenko 
2915a3cb39d2SAndy Shevchenko 	ret = kstrtobool(buf, &newconsole);
2916a3cb39d2SAndy Shevchenko 	if (ret)
2917a3cb39d2SAndy Shevchenko 		return ret;
2918a3cb39d2SAndy Shevchenko 
2919a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2920a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2921a3cb39d2SAndy Shevchenko 	if (uport) {
2922a3cb39d2SAndy Shevchenko 		oldconsole = uart_console_enabled(uport);
2923a3cb39d2SAndy Shevchenko 		if (oldconsole && !newconsole) {
2924a3cb39d2SAndy Shevchenko 			ret = unregister_console(uport->cons);
2925a3cb39d2SAndy Shevchenko 		} else if (!oldconsole && newconsole) {
2926e0830dbfSJohan Hovold 			if (uart_console(uport)) {
2927e0830dbfSJohan Hovold 				uport->console_reinit = 1;
2928a3cb39d2SAndy Shevchenko 				register_console(uport->cons);
2929e0830dbfSJohan Hovold 			} else {
2930a3cb39d2SAndy Shevchenko 				ret = -ENOENT;
2931a3cb39d2SAndy Shevchenko 			}
2932e0830dbfSJohan Hovold 		}
2933a3cb39d2SAndy Shevchenko 	} else {
2934a3cb39d2SAndy Shevchenko 		ret = -ENXIO;
2935a3cb39d2SAndy Shevchenko 	}
2936a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
2937a3cb39d2SAndy Shevchenko 
2938a3cb39d2SAndy Shevchenko 	return ret < 0 ? ret : count;
2939a3cb39d2SAndy Shevchenko }
2940a3cb39d2SAndy Shevchenko 
2941143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(uartclk);
2942143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(type);
2943143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(line);
2944143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(port);
2945143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(irq);
2946143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(flags);
2947143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(xmit_fifo_size);
2948143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(close_delay);
2949143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(closing_wait);
2950143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(custom_divisor);
2951143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(io_type);
2952143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_base);
2953143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_reg_shift);
2954a3cb39d2SAndy Shevchenko static DEVICE_ATTR_RW(console);
29556915c0e4STomas Hlavacek 
29566915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = {
2957143c02c8SAndy Shevchenko 	&dev_attr_uartclk.attr,
2958373bac4cSAlan Cox 	&dev_attr_type.attr,
2959373bac4cSAlan Cox 	&dev_attr_line.attr,
2960373bac4cSAlan Cox 	&dev_attr_port.attr,
2961373bac4cSAlan Cox 	&dev_attr_irq.attr,
2962373bac4cSAlan Cox 	&dev_attr_flags.attr,
2963373bac4cSAlan Cox 	&dev_attr_xmit_fifo_size.attr,
2964373bac4cSAlan Cox 	&dev_attr_close_delay.attr,
2965373bac4cSAlan Cox 	&dev_attr_closing_wait.attr,
2966373bac4cSAlan Cox 	&dev_attr_custom_divisor.attr,
2967373bac4cSAlan Cox 	&dev_attr_io_type.attr,
2968373bac4cSAlan Cox 	&dev_attr_iomem_base.attr,
2969373bac4cSAlan Cox 	&dev_attr_iomem_reg_shift.attr,
2970a3cb39d2SAndy Shevchenko 	&dev_attr_console.attr,
2971a3cb39d2SAndy Shevchenko 	NULL
29726915c0e4STomas Hlavacek };
29736915c0e4STomas Hlavacek 
2974b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = {
29756915c0e4STomas Hlavacek 	.attrs = tty_dev_attrs,
29766915c0e4STomas Hlavacek };
29776915c0e4STomas Hlavacek 
2978ab4382d2SGreg Kroah-Hartman /**
2979ab4382d2SGreg Kroah-Hartman  *	uart_add_one_port - attach a driver-defined port structure
2980ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
2981ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure to use for this port.
2982ab4382d2SGreg Kroah-Hartman  *
2983a157270fSAhmed S. Darwish  *	Context: task context, might sleep
2984a157270fSAhmed S. Darwish  *
2985ab4382d2SGreg Kroah-Hartman  *	This allows the driver to register its own uart_port structure
2986ab4382d2SGreg Kroah-Hartman  *	with the core driver.  The main purpose is to allow the low
2987ab4382d2SGreg Kroah-Hartman  *	level uart drivers to expand uart_port, rather than having yet
2988ab4382d2SGreg Kroah-Hartman  *	more levels of structures.
2989ab4382d2SGreg Kroah-Hartman  */
2990ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2991ab4382d2SGreg Kroah-Hartman {
2992ab4382d2SGreg Kroah-Hartman 	struct uart_state *state;
2993ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
2994ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2995ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2996266dcff0SGreg Kroah-Hartman 	int num_groups;
2997ab4382d2SGreg Kroah-Hartman 
2998ab4382d2SGreg Kroah-Hartman 	if (uport->line >= drv->nr)
2999ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
3000ab4382d2SGreg Kroah-Hartman 
3001ab4382d2SGreg Kroah-Hartman 	state = drv->state + uport->line;
3002ab4382d2SGreg Kroah-Hartman 	port = &state->port;
3003ab4382d2SGreg Kroah-Hartman 
3004ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
3005ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
3006ab4382d2SGreg Kroah-Hartman 	if (state->uart_port) {
3007ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
3008ab4382d2SGreg Kroah-Hartman 		goto out;
3009ab4382d2SGreg Kroah-Hartman 	}
3010ab4382d2SGreg Kroah-Hartman 
30112b702b9bSPeter Hurley 	/* Link the port to the driver state table and vice versa */
30129ed19428SPeter Hurley 	atomic_set(&state->refcount, 1);
30139ed19428SPeter Hurley 	init_waitqueue_head(&state->remove_wait);
3014ab4382d2SGreg Kroah-Hartman 	state->uart_port = uport;
3015ab4382d2SGreg Kroah-Hartman 	uport->state = state;
3016ab4382d2SGreg Kroah-Hartman 
30172b702b9bSPeter Hurley 	state->pm_state = UART_PM_STATE_UNDEFINED;
30182b702b9bSPeter Hurley 	uport->cons = drv->cons;
3019959801feSPeter Hurley 	uport->minor = drv->tty_driver->minor_start + uport->line;
3020f7048b15SVignesh R 	uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3021f7048b15SVignesh R 				drv->tty_driver->name_base + uport->line);
3022f7048b15SVignesh R 	if (!uport->name) {
3023f7048b15SVignesh R 		ret = -ENOMEM;
3024f7048b15SVignesh R 		goto out;
3025f7048b15SVignesh R 	}
30262b702b9bSPeter Hurley 
3027fe88c648SJohan Hovold 	/*
3028fe88c648SJohan Hovold 	 * If this port is in use as a console then the spinlock is already
3029fe88c648SJohan Hovold 	 * initialised.
3030fe88c648SJohan Hovold 	 */
3031fe88c648SJohan Hovold 	if (!uart_console_enabled(uport))
3032d2403cadSAndy Shevchenko 		uart_port_spin_lock_init(uport);
3033d2403cadSAndy Shevchenko 
3034a208ffd2SGrant Likely 	if (uport->cons && uport->dev)
3035dd8b7a1dSMichal Simek 		of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3036ab4382d2SGreg Kroah-Hartman 
3037fb2b9001SSudip Mukherjee 	tty_port_link_device(port, drv->tty_driver, uport->line);
3038ab4382d2SGreg Kroah-Hartman 	uart_configure_port(drv, state, uport);
3039ab4382d2SGreg Kroah-Hartman 
30404dda864dSGeert Uytterhoeven 	port->console = uart_console(uport);
30414dda864dSGeert Uytterhoeven 
3042266dcff0SGreg Kroah-Hartman 	num_groups = 2;
3043266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3044266dcff0SGreg Kroah-Hartman 		num_groups++;
3045266dcff0SGreg Kroah-Hartman 
3046c2b703b8SYoshihiro YUNOMAE 	uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3047266dcff0SGreg Kroah-Hartman 				    GFP_KERNEL);
3048266dcff0SGreg Kroah-Hartman 	if (!uport->tty_groups) {
3049266dcff0SGreg Kroah-Hartman 		ret = -ENOMEM;
3050266dcff0SGreg Kroah-Hartman 		goto out;
3051266dcff0SGreg Kroah-Hartman 	}
3052266dcff0SGreg Kroah-Hartman 	uport->tty_groups[0] = &tty_dev_attr_group;
3053266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3054266dcff0SGreg Kroah-Hartman 		uport->tty_groups[1] = uport->attr_group;
3055266dcff0SGreg Kroah-Hartman 
3056ab4382d2SGreg Kroah-Hartman 	/*
3057ab4382d2SGreg Kroah-Hartman 	 * Register the port whether it's detected or not.  This allows
3058015355b7SGeert Uytterhoeven 	 * setserial to be used to alter this port's parameters.
3059ab4382d2SGreg Kroah-Hartman 	 */
3060da4c2799SJohan Hovold 	tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3061266dcff0SGreg Kroah-Hartman 			uport->line, uport->dev, port, uport->tty_groups);
3062b289c496SChengguang Xu 	if (!IS_ERR(tty_dev)) {
306377359835SSimon Glass 		device_set_wakeup_capable(tty_dev, 1);
306477359835SSimon Glass 	} else {
30652f2dafe7SSudip Mukherjee 		dev_err(uport->dev, "Cannot register tty device on line %d\n",
3066ab4382d2SGreg Kroah-Hartman 		       uport->line);
306777359835SSimon Glass 	}
3068ab4382d2SGreg Kroah-Hartman 
3069ab4382d2SGreg Kroah-Hartman 	/*
3070ab4382d2SGreg Kroah-Hartman 	 * Ensure UPF_DEAD is not set.
3071ab4382d2SGreg Kroah-Hartman 	 */
3072ab4382d2SGreg Kroah-Hartman 	uport->flags &= ~UPF_DEAD;
3073ab4382d2SGreg Kroah-Hartman 
3074ab4382d2SGreg Kroah-Hartman  out:
3075ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3076ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
3077ab4382d2SGreg Kroah-Hartman 
3078ab4382d2SGreg Kroah-Hartman 	return ret;
3079ab4382d2SGreg Kroah-Hartman }
308015dc475bSJiri Slaby EXPORT_SYMBOL(uart_add_one_port);
3081ab4382d2SGreg Kroah-Hartman 
3082ab4382d2SGreg Kroah-Hartman /**
3083ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port - detach a driver defined port structure
3084ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
3085ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure for this port
3086ab4382d2SGreg Kroah-Hartman  *
3087a157270fSAhmed S. Darwish  *	Context: task context, might sleep
3088a157270fSAhmed S. Darwish  *
3089ab4382d2SGreg Kroah-Hartman  *	This unhooks (and hangs up) the specified port structure from the
3090ab4382d2SGreg Kroah-Hartman  *	core driver.  No further calls will be made to the low-level code
3091ab4382d2SGreg Kroah-Hartman  *	for this port.
3092ab4382d2SGreg Kroah-Hartman  */
3093ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
3094ab4382d2SGreg Kroah-Hartman {
3095ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
3096ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
30974047b371SPeter Hurley 	struct uart_port *uart_port;
30984c6d5b4dSGeert Uytterhoeven 	struct tty_struct *tty;
3099b342dd51SChen Gang 	int ret = 0;
3100ab4382d2SGreg Kroah-Hartman 
3101ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
3102ab4382d2SGreg Kroah-Hartman 
3103ab4382d2SGreg Kroah-Hartman 	/*
3104ab4382d2SGreg Kroah-Hartman 	 * Mark the port "dead" - this prevents any opens from
3105ab4382d2SGreg Kroah-Hartman 	 * succeeding while we shut down the port.
3106ab4382d2SGreg Kroah-Hartman 	 */
3107ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
31084047b371SPeter Hurley 	uart_port = uart_port_check(state);
31094047b371SPeter Hurley 	if (uart_port != uport)
31104047b371SPeter Hurley 		dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
31114047b371SPeter Hurley 			  uart_port, uport);
31124047b371SPeter Hurley 
31134047b371SPeter Hurley 	if (!uart_port) {
3114b342dd51SChen Gang 		mutex_unlock(&port->mutex);
3115b342dd51SChen Gang 		ret = -EINVAL;
3116b342dd51SChen Gang 		goto out;
3117b342dd51SChen Gang 	}
3118ab4382d2SGreg Kroah-Hartman 	uport->flags |= UPF_DEAD;
3119ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3120ab4382d2SGreg Kroah-Hartman 
3121ab4382d2SGreg Kroah-Hartman 	/*
3122ab4382d2SGreg Kroah-Hartman 	 * Remove the devices from the tty layer
3123ab4382d2SGreg Kroah-Hartman 	 */
3124da4c2799SJohan Hovold 	tty_port_unregister_device(port, drv->tty_driver, uport->line);
3125ab4382d2SGreg Kroah-Hartman 
31264c6d5b4dSGeert Uytterhoeven 	tty = tty_port_tty_get(port);
31274c6d5b4dSGeert Uytterhoeven 	if (tty) {
3128ab4382d2SGreg Kroah-Hartman 		tty_vhangup(port->tty);
31294c6d5b4dSGeert Uytterhoeven 		tty_kref_put(tty);
31304c6d5b4dSGeert Uytterhoeven 	}
3131ab4382d2SGreg Kroah-Hartman 
3132ab4382d2SGreg Kroah-Hartman 	/*
31335f5c9ae5SGeert Uytterhoeven 	 * If the port is used as a console, unregister it
31345f5c9ae5SGeert Uytterhoeven 	 */
31355f5c9ae5SGeert Uytterhoeven 	if (uart_console(uport))
31365f5c9ae5SGeert Uytterhoeven 		unregister_console(uport->cons);
31375f5c9ae5SGeert Uytterhoeven 
31385f5c9ae5SGeert Uytterhoeven 	/*
3139ab4382d2SGreg Kroah-Hartman 	 * Free the port IO and memory resources, if any.
3140ab4382d2SGreg Kroah-Hartman 	 */
3141a7cfaf16SFabio Estevam 	if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3142ab4382d2SGreg Kroah-Hartman 		uport->ops->release_port(uport);
3143266dcff0SGreg Kroah-Hartman 	kfree(uport->tty_groups);
3144f7048b15SVignesh R 	kfree(uport->name);
3145ab4382d2SGreg Kroah-Hartman 
3146ab4382d2SGreg Kroah-Hartman 	/*
3147ab4382d2SGreg Kroah-Hartman 	 * Indicate that there isn't a port here anymore.
3148ab4382d2SGreg Kroah-Hartman 	 */
3149ab4382d2SGreg Kroah-Hartman 	uport->type = PORT_UNKNOWN;
3150ab4382d2SGreg Kroah-Hartman 
31514047b371SPeter Hurley 	mutex_lock(&port->mutex);
31529ed19428SPeter Hurley 	WARN_ON(atomic_dec_return(&state->refcount) < 0);
31539ed19428SPeter Hurley 	wait_event(state->remove_wait, !atomic_read(&state->refcount));
3154ab4382d2SGreg Kroah-Hartman 	state->uart_port = NULL;
31554047b371SPeter Hurley 	mutex_unlock(&port->mutex);
3156b342dd51SChen Gang out:
3157ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
3158ab4382d2SGreg Kroah-Hartman 
3159b342dd51SChen Gang 	return ret;
3160ab4382d2SGreg Kroah-Hartman }
316115dc475bSJiri Slaby EXPORT_SYMBOL(uart_remove_one_port);
3162ab4382d2SGreg Kroah-Hartman 
3163ab4382d2SGreg Kroah-Hartman /*
3164ab4382d2SGreg Kroah-Hartman  *	Are the two ports equivalent?
3165ab4382d2SGreg Kroah-Hartman  */
3166b8be5db5SJiri Slaby bool uart_match_port(const struct uart_port *port1,
3167b8be5db5SJiri Slaby 		const struct uart_port *port2)
3168ab4382d2SGreg Kroah-Hartman {
3169ab4382d2SGreg Kroah-Hartman 	if (port1->iotype != port2->iotype)
3170b8be5db5SJiri Slaby 		return false;
3171ab4382d2SGreg Kroah-Hartman 
3172ab4382d2SGreg Kroah-Hartman 	switch (port1->iotype) {
3173ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
3174b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase;
3175ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
3176b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase &&
3177b8be5db5SJiri Slaby 		       port1->hub6   == port2->hub6;
3178ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
3179bd94c407SMasahiro Yamada 	case UPIO_MEM16:
3180ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
31813ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
3182ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
3183ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
3184b8be5db5SJiri Slaby 		return port1->mapbase == port2->mapbase;
3185ab4382d2SGreg Kroah-Hartman 	}
3186b8be5db5SJiri Slaby 
3187b8be5db5SJiri Slaby 	return false;
3188ab4382d2SGreg Kroah-Hartman }
3189ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port);
3190ab4382d2SGreg Kroah-Hartman 
3191027d7dacSJiri Slaby /**
3192027d7dacSJiri Slaby  *	uart_handle_dcd_change - handle a change of carrier detect state
3193027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
3194027d7dacSJiri Slaby  *	@status: new carrier detect status, nonzero if active
31954d90bb14SPeter Hurley  *
31964d90bb14SPeter Hurley  *	Caller must hold uport->lock
3197027d7dacSJiri Slaby  */
3198027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
3199027d7dacSJiri Slaby {
320042381572SGeorge Spelvin 	struct tty_port *port = &uport->state->port;
320143eca0aeSAlan Cox 	struct tty_struct *tty = port->tty;
3202c993257bSPeter Hurley 	struct tty_ldisc *ld;
3203027d7dacSJiri Slaby 
32044d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
32054d90bb14SPeter Hurley 
3206c993257bSPeter Hurley 	if (tty) {
3207c993257bSPeter Hurley 		ld = tty_ldisc_ref(tty);
320842381572SGeorge Spelvin 		if (ld) {
320942381572SGeorge Spelvin 			if (ld->ops->dcd_change)
3210593fb1aeSGeorge Spelvin 				ld->ops->dcd_change(tty, status);
321142381572SGeorge Spelvin 			tty_ldisc_deref(ld);
321242381572SGeorge Spelvin 		}
3213c993257bSPeter Hurley 	}
3214027d7dacSJiri Slaby 
3215027d7dacSJiri Slaby 	uport->icount.dcd++;
3216027d7dacSJiri Slaby 
3217299245a1SPeter Hurley 	if (uart_dcd_enabled(uport)) {
3218027d7dacSJiri Slaby 		if (status)
3219027d7dacSJiri Slaby 			wake_up_interruptible(&port->open_wait);
322043eca0aeSAlan Cox 		else if (tty)
322143eca0aeSAlan Cox 			tty_hangup(tty);
3222027d7dacSJiri Slaby 	}
3223027d7dacSJiri Slaby }
3224027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3225027d7dacSJiri Slaby 
3226027d7dacSJiri Slaby /**
3227027d7dacSJiri Slaby  *	uart_handle_cts_change - handle a change of clear-to-send state
3228027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
3229027d7dacSJiri Slaby  *	@status: new clear to send status, nonzero if active
32304d90bb14SPeter Hurley  *
32314d90bb14SPeter Hurley  *	Caller must hold uport->lock
3232027d7dacSJiri Slaby  */
3233027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3234027d7dacSJiri Slaby {
32354d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
32364d90bb14SPeter Hurley 
3237027d7dacSJiri Slaby 	uport->icount.cts++;
3238027d7dacSJiri Slaby 
3239391f93f2SPeter Hurley 	if (uart_softcts_mode(uport)) {
3240d01f4d18SPeter Hurley 		if (uport->hw_stopped) {
3241027d7dacSJiri Slaby 			if (status) {
3242d01f4d18SPeter Hurley 				uport->hw_stopped = 0;
3243027d7dacSJiri Slaby 				uport->ops->start_tx(uport);
3244027d7dacSJiri Slaby 				uart_write_wakeup(uport);
3245027d7dacSJiri Slaby 			}
3246027d7dacSJiri Slaby 		} else {
3247027d7dacSJiri Slaby 			if (!status) {
3248d01f4d18SPeter Hurley 				uport->hw_stopped = 1;
3249027d7dacSJiri Slaby 				uport->ops->stop_tx(uport);
3250027d7dacSJiri Slaby 			}
3251027d7dacSJiri Slaby 		}
3252391f93f2SPeter Hurley 
3253027d7dacSJiri Slaby 	}
3254027d7dacSJiri Slaby }
3255027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3256027d7dacSJiri Slaby 
3257cf75525fSJiri Slaby /**
3258cf75525fSJiri Slaby  * uart_insert_char - push a char to the uart layer
3259cf75525fSJiri Slaby  *
3260cf75525fSJiri Slaby  * User is responsible to call tty_flip_buffer_push when they are done with
3261cf75525fSJiri Slaby  * insertion.
3262cf75525fSJiri Slaby  *
3263cf75525fSJiri Slaby  * @port: corresponding port
3264cf75525fSJiri Slaby  * @status: state of the serial port RX buffer (LSR for 8250)
3265cf75525fSJiri Slaby  * @overrun: mask of overrun bits in @status
3266cf75525fSJiri Slaby  * @ch: character to push
3267cf75525fSJiri Slaby  * @flag: flag for the character (see TTY_NORMAL and friends)
3268cf75525fSJiri Slaby  */
3269027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status,
3270027d7dacSJiri Slaby 		 unsigned int overrun, unsigned int ch, unsigned int flag)
3271027d7dacSJiri Slaby {
327292a19f9cSJiri Slaby 	struct tty_port *tport = &port->state->port;
3273027d7dacSJiri Slaby 
3274027d7dacSJiri Slaby 	if ((status & port->ignore_status_mask & ~overrun) == 0)
327592a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, ch, flag) == 0)
3276dabfb351SCorbin 			++port->icount.buf_overrun;
3277027d7dacSJiri Slaby 
3278027d7dacSJiri Slaby 	/*
3279027d7dacSJiri Slaby 	 * Overrun is special.  Since it's reported immediately,
3280027d7dacSJiri Slaby 	 * it doesn't affect the current character.
3281027d7dacSJiri Slaby 	 */
3282027d7dacSJiri Slaby 	if (status & ~port->ignore_status_mask & overrun)
328392a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3284dabfb351SCorbin 			++port->icount.buf_overrun;
3285027d7dacSJiri Slaby }
3286027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char);
3287027d7dacSJiri Slaby 
328868af4317SDmitry Safonov #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
328968af4317SDmitry Safonov static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
329068af4317SDmitry Safonov 
329168af4317SDmitry Safonov static void uart_sysrq_on(struct work_struct *w)
329268af4317SDmitry Safonov {
3293b18896ffSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3294b18896ffSAndy Shevchenko 
329568af4317SDmitry Safonov 	sysrq_toggle_support(1);
3296b18896ffSAndy Shevchenko 	pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3297b18896ffSAndy Shevchenko 		sysrq_toggle_seq_len, sysrq_toggle_seq);
329868af4317SDmitry Safonov }
329968af4317SDmitry Safonov static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
330068af4317SDmitry Safonov 
330168af4317SDmitry Safonov /**
330268af4317SDmitry Safonov  *	uart_try_toggle_sysrq - Enables SysRq from serial line
330368af4317SDmitry Safonov  *	@port: uart_port structure where char(s) after BREAK met
330468af4317SDmitry Safonov  *	@ch: new character in the sequence after received BREAK
330568af4317SDmitry Safonov  *
330668af4317SDmitry Safonov  *	Enables magic SysRq when the required sequence is met on port
330768af4317SDmitry Safonov  *	(see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
330868af4317SDmitry Safonov  *
330968af4317SDmitry Safonov  *	Returns false if @ch is out of enabling sequence and should be
331068af4317SDmitry Safonov  *	handled some other way, true if @ch was consumed.
331168af4317SDmitry Safonov  */
331208d54703SJohan Hovold bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
331368af4317SDmitry Safonov {
33142ce5eaceSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
33152ce5eaceSAndy Shevchenko 
33162ce5eaceSAndy Shevchenko 	if (!sysrq_toggle_seq_len)
331768af4317SDmitry Safonov 		return false;
331868af4317SDmitry Safonov 
331968af4317SDmitry Safonov 	BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
332068af4317SDmitry Safonov 	if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
332168af4317SDmitry Safonov 		port->sysrq_seq = 0;
332268af4317SDmitry Safonov 		return false;
332368af4317SDmitry Safonov 	}
332468af4317SDmitry Safonov 
33252ce5eaceSAndy Shevchenko 	if (++port->sysrq_seq < sysrq_toggle_seq_len) {
332668af4317SDmitry Safonov 		port->sysrq = jiffies + SYSRQ_TIMEOUT;
332768af4317SDmitry Safonov 		return true;
332868af4317SDmitry Safonov 	}
332968af4317SDmitry Safonov 
333068af4317SDmitry Safonov 	schedule_work(&sysrq_enable_work);
333168af4317SDmitry Safonov 
333268af4317SDmitry Safonov 	port->sysrq = 0;
333368af4317SDmitry Safonov 	return true;
333468af4317SDmitry Safonov }
333508d54703SJohan Hovold EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
333668af4317SDmitry Safonov #endif
333768af4317SDmitry Safonov 
3338ef838a81SUwe Kleine-König /**
3339743f93f8SLukas Wunner  * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3340a7172561SRandy Dunlap  * @port: uart device's target port
3341ef838a81SUwe Kleine-König  *
3342ef838a81SUwe Kleine-König  * This function implements the device tree binding described in
3343ef838a81SUwe Kleine-König  * Documentation/devicetree/bindings/serial/rs485.txt.
3344ef838a81SUwe Kleine-König  */
3345c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port)
3346ef838a81SUwe Kleine-König {
3347c150c0f3SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
3348c150c0f3SLukas Wunner 	struct device *dev = port->dev;
3349ef838a81SUwe Kleine-König 	u32 rs485_delay[2];
3350ef838a81SUwe Kleine-König 	int ret;
3351ef838a81SUwe Kleine-König 
3352743f93f8SLukas Wunner 	ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3353743f93f8SLukas Wunner 					     rs485_delay, 2);
3354ef838a81SUwe Kleine-König 	if (!ret) {
3355ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = rs485_delay[0];
3356ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = rs485_delay[1];
3357ef838a81SUwe Kleine-König 	} else {
3358ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = 0;
3359ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = 0;
3360ef838a81SUwe Kleine-König 	}
3361ef838a81SUwe Kleine-König 
3362ef838a81SUwe Kleine-König 	/*
3363f1e5b618SLukas Wunner 	 * Clear full-duplex and enabled flags, set RTS polarity to active high
3364f1e5b618SLukas Wunner 	 * to get to a defined state with the following properties:
3365ef838a81SUwe Kleine-König 	 */
3366f1e5b618SLukas Wunner 	rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3367d58a2df3SLukas Wunner 			      SER_RS485_TERMINATE_BUS |
3368f1e5b618SLukas Wunner 			      SER_RS485_RTS_AFTER_SEND);
3369f1e5b618SLukas Wunner 	rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3370ef838a81SUwe Kleine-König 
3371743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3372ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
3373ef838a81SUwe Kleine-König 
3374743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3375ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_ENABLED;
3376f1e5b618SLukas Wunner 
3377f1e5b618SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3378f1e5b618SLukas Wunner 		rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3379f1e5b618SLukas Wunner 		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3380f1e5b618SLukas Wunner 	}
3381c150c0f3SLukas Wunner 
3382d58a2df3SLukas Wunner 	/*
3383d58a2df3SLukas Wunner 	 * Disabling termination by default is the safe choice:  Else if many
3384d58a2df3SLukas Wunner 	 * bus participants enable it, no communication is possible at all.
3385d58a2df3SLukas Wunner 	 * Works fine for short cables and users may enable for longer cables.
3386d58a2df3SLukas Wunner 	 */
3387d58a2df3SLukas Wunner 	port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3388d58a2df3SLukas Wunner 							GPIOD_OUT_LOW);
3389d58a2df3SLukas Wunner 	if (IS_ERR(port->rs485_term_gpio)) {
3390d58a2df3SLukas Wunner 		ret = PTR_ERR(port->rs485_term_gpio);
3391d58a2df3SLukas Wunner 		port->rs485_term_gpio = NULL;
339289c65d66SKrzysztof Kozlowski 		return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3393d58a2df3SLukas Wunner 	}
3394d58a2df3SLukas Wunner 
3395c150c0f3SLukas Wunner 	return 0;
3396ef838a81SUwe Kleine-König }
3397743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3398ef838a81SUwe Kleine-König 
3399ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core");
3400ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
3401