xref: /openbmc/linux/drivers/tty/serial/serial_core.c (revision 167cbce2)
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>
27794edf30SDavid Howells #include <linux/security.h>
28ab4382d2SGreg Kroah-Hartman 
29aef3ad10SAndy Shevchenko #include <linux/irq.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31ab4382d2SGreg Kroah-Hartman 
32ab4382d2SGreg Kroah-Hartman /*
33ab4382d2SGreg Kroah-Hartman  * This is used to lock changes in serial line configuration.
34ab4382d2SGreg Kroah-Hartman  */
35ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex);
36ab4382d2SGreg Kroah-Hartman 
37ab4382d2SGreg Kroah-Hartman /*
38ab4382d2SGreg Kroah-Hartman  * lockdep: port->lock is initialized in two places, but we
39ab4382d2SGreg Kroah-Hartman  *          want only one lock-class:
40ab4382d2SGreg Kroah-Hartman  */
41ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key;
42ab4382d2SGreg Kroah-Hartman 
43ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
44ab4382d2SGreg Kroah-Hartman 
4568af4317SDmitry Safonov #define SYSRQ_TIMEOUT	(HZ * 5)
4668af4317SDmitry Safonov 
47ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
48ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios);
491f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
506f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
516f538fe3SLinus Walleij 			   enum uart_pm_state pm_state);
52ab4382d2SGreg Kroah-Hartman 
53b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port);
54b922e19dSJiri Slaby 
55299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport)
56299245a1SPeter Hurley {
57d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_DCD_ENABLE);
58299245a1SPeter Hurley }
59299245a1SPeter Hurley 
609ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state)
619ed19428SPeter Hurley {
629ed19428SPeter Hurley 	if (atomic_add_unless(&state->refcount, 1, 0))
639ed19428SPeter Hurley 		return state->uart_port;
649ed19428SPeter Hurley 	return NULL;
659ed19428SPeter Hurley }
669ed19428SPeter Hurley 
679ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport)
689ed19428SPeter Hurley {
69ef510beaSAndy Shevchenko 	if (atomic_dec_and_test(&uport->state->refcount))
709ed19428SPeter Hurley 		wake_up(&uport->state->remove_wait);
719ed19428SPeter Hurley }
729ed19428SPeter Hurley 
739ed19428SPeter Hurley #define uart_port_lock(state, flags)					\
749ed19428SPeter Hurley 	({								\
759ed19428SPeter Hurley 		struct uart_port *__uport = uart_port_ref(state);	\
769ed19428SPeter Hurley 		if (__uport)						\
779ed19428SPeter Hurley 			spin_lock_irqsave(&__uport->lock, flags);	\
789ed19428SPeter Hurley 		__uport;						\
799ed19428SPeter Hurley 	})
809ed19428SPeter Hurley 
819ed19428SPeter Hurley #define uart_port_unlock(uport, flags)					\
829ed19428SPeter Hurley 	({								\
839ed19428SPeter Hurley 		struct uart_port *__uport = uport;			\
84ef510beaSAndy Shevchenko 		if (__uport) {						\
859ed19428SPeter Hurley 			spin_unlock_irqrestore(&__uport->lock, flags);	\
869ed19428SPeter Hurley 			uart_port_deref(__uport);			\
87ef510beaSAndy Shevchenko 		}							\
889ed19428SPeter Hurley 	})
899ed19428SPeter Hurley 
904047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state)
914047b371SPeter Hurley {
927da4b8b7SPeter Hurley 	lockdep_assert_held(&state->port.mutex);
934047b371SPeter Hurley 	return state->uart_port;
944047b371SPeter Hurley }
954047b371SPeter Hurley 
96ab4382d2SGreg Kroah-Hartman /*
97ab4382d2SGreg Kroah-Hartman  * This routine is used by the interrupt handler to schedule processing in
98ab4382d2SGreg Kroah-Hartman  * the software interrupt portion of the driver.
99ab4382d2SGreg Kroah-Hartman  */
100ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port)
101ab4382d2SGreg Kroah-Hartman {
102ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = port->state;
103ab4382d2SGreg Kroah-Hartman 	/*
104ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
105ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
106ab4382d2SGreg Kroah-Hartman 	 */
107ab4382d2SGreg Kroah-Hartman 	BUG_ON(!state);
108d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
109ab4382d2SGreg Kroah-Hartman }
110ab4382d2SGreg Kroah-Hartman 
111ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty)
112ab4382d2SGreg Kroah-Hartman {
113ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1149ed19428SPeter Hurley 	struct uart_port *port;
115ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
116ab4382d2SGreg Kroah-Hartman 
1179ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
1189ed19428SPeter Hurley 	if (port)
119ab4382d2SGreg Kroah-Hartman 		port->ops->stop_tx(port);
1209ed19428SPeter Hurley 	uart_port_unlock(port, flags);
121ab4382d2SGreg Kroah-Hartman }
122ab4382d2SGreg Kroah-Hartman 
123ab4382d2SGreg Kroah-Hartman static void __uart_start(struct tty_struct *tty)
124ab4382d2SGreg Kroah-Hartman {
125ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
126ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
127ab4382d2SGreg Kroah-Hartman 
1289ed19428SPeter Hurley 	if (port && !uart_tx_stopped(port))
129ab4382d2SGreg Kroah-Hartman 		port->ops->start_tx(port);
130ab4382d2SGreg Kroah-Hartman }
131ab4382d2SGreg Kroah-Hartman 
132ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty)
133ab4382d2SGreg Kroah-Hartman {
134ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1359ed19428SPeter Hurley 	struct uart_port *port;
136ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
137ab4382d2SGreg Kroah-Hartman 
1389ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
139ab4382d2SGreg Kroah-Hartman 	__uart_start(tty);
1409ed19428SPeter Hurley 	uart_port_unlock(port, flags);
141ab4382d2SGreg Kroah-Hartman }
142ab4382d2SGreg Kroah-Hartman 
143f4581cabSDenys Vlasenko static void
144ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
145ab4382d2SGreg Kroah-Hartman {
146ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
147ab4382d2SGreg Kroah-Hartman 	unsigned int old;
148ab4382d2SGreg Kroah-Hartman 
149ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
150ab4382d2SGreg Kroah-Hartman 	old = port->mctrl;
151ab4382d2SGreg Kroah-Hartman 	port->mctrl = (old & ~clear) | set;
152ab4382d2SGreg Kroah-Hartman 	if (old != port->mctrl)
153ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl);
154ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
155ab4382d2SGreg Kroah-Hartman }
156ab4382d2SGreg Kroah-Hartman 
157ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
158ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
159ab4382d2SGreg Kroah-Hartman 
160a6845e1eSRafael Gago static void uart_port_dtr_rts(struct uart_port *uport, int raise)
161a6845e1eSRafael Gago {
162a6845e1eSRafael Gago 	int rs485_on = uport->rs485_config &&
163a6845e1eSRafael Gago 		(uport->rs485.flags & SER_RS485_ENABLED);
164a6845e1eSRafael Gago 	int RTS_after_send = !!(uport->rs485.flags & SER_RS485_RTS_AFTER_SEND);
165a6845e1eSRafael Gago 
166a6845e1eSRafael Gago 	if (raise) {
167a6845e1eSRafael Gago 		if (rs485_on && !RTS_after_send) {
168a6845e1eSRafael Gago 			uart_set_mctrl(uport, TIOCM_DTR);
169a6845e1eSRafael Gago 			uart_clear_mctrl(uport, TIOCM_RTS);
170a6845e1eSRafael Gago 		} else {
171a6845e1eSRafael Gago 			uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
172a6845e1eSRafael Gago 		}
173a6845e1eSRafael Gago 	} else {
174a6845e1eSRafael Gago 		unsigned int clear = TIOCM_DTR;
175a6845e1eSRafael Gago 
176a6845e1eSRafael Gago 		clear |= (!rs485_on || !RTS_after_send) ? TIOCM_RTS : 0;
177a6845e1eSRafael Gago 		uart_clear_mctrl(uport, clear);
178a6845e1eSRafael Gago 	}
179a6845e1eSRafael Gago }
180a6845e1eSRafael Gago 
181ab4382d2SGreg Kroah-Hartman /*
182ab4382d2SGreg Kroah-Hartman  * Startup the port.  This will be called once per open.  All calls
183ab4382d2SGreg Kroah-Hartman  * will be serialised by the per-port mutex.
184ab4382d2SGreg Kroah-Hartman  */
185c0d92be6SJiri Slaby static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
186c0d92be6SJiri Slaby 		int init_hw)
187ab4382d2SGreg Kroah-Hartman {
1884047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
189ab4382d2SGreg Kroah-Hartman 	unsigned long page;
190a5ba1d95STycho Andersen 	unsigned long flags = 0;
191ab4382d2SGreg Kroah-Hartman 	int retval = 0;
192ab4382d2SGreg Kroah-Hartman 
193ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
194c0d92be6SJiri Slaby 		return 1;
195ab4382d2SGreg Kroah-Hartman 
196ab4382d2SGreg Kroah-Hartman 	/*
1977deb39edSThomas Pfaff 	 * Make sure the device is in D0 state.
1987deb39edSThomas Pfaff 	 */
1997deb39edSThomas Pfaff 	uart_change_pm(state, UART_PM_STATE_ON);
2007deb39edSThomas Pfaff 
2017deb39edSThomas Pfaff 	/*
202ab4382d2SGreg Kroah-Hartman 	 * Initialise and allocate the transmit and temporary
203ab4382d2SGreg Kroah-Hartman 	 * buffer.
204ab4382d2SGreg Kroah-Hartman 	 */
205ab4382d2SGreg Kroah-Hartman 	page = get_zeroed_page(GFP_KERNEL);
206ab4382d2SGreg Kroah-Hartman 	if (!page)
207ab4382d2SGreg Kroah-Hartman 		return -ENOMEM;
208ab4382d2SGreg Kroah-Hartman 
209a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
210a5ba1d95STycho Andersen 	if (!state->xmit.buf) {
211ab4382d2SGreg Kroah-Hartman 		state->xmit.buf = (unsigned char *) page;
212ab4382d2SGreg Kroah-Hartman 		uart_circ_clear(&state->xmit);
213d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
214a5ba1d95STycho Andersen 	} else {
215d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
216d7240214SSergey Senozhatsky 		/*
217d7240214SSergey Senozhatsky 		 * Do not free() the page under the port lock, see
218d7240214SSergey Senozhatsky 		 * uart_shutdown().
219d7240214SSergey Senozhatsky 		 */
220a5ba1d95STycho Andersen 		free_page(page);
221ab4382d2SGreg Kroah-Hartman 	}
222ab4382d2SGreg Kroah-Hartman 
223ab4382d2SGreg Kroah-Hartman 	retval = uport->ops->startup(uport);
224ab4382d2SGreg Kroah-Hartman 	if (retval == 0) {
225c7d7abffSJiri Slaby 		if (uart_console(uport) && uport->cons->cflag) {
226adc8d746SAlan Cox 			tty->termios.c_cflag = uport->cons->cflag;
227c7d7abffSJiri Slaby 			uport->cons->cflag = 0;
228c7d7abffSJiri Slaby 		}
229ab4382d2SGreg Kroah-Hartman 		/*
230ab4382d2SGreg Kroah-Hartman 		 * Initialise the hardware port settings.
231ab4382d2SGreg Kroah-Hartman 		 */
232ab4382d2SGreg Kroah-Hartman 		uart_change_speed(tty, state, NULL);
233ab4382d2SGreg Kroah-Hartman 
234ab4382d2SGreg Kroah-Hartman 		/*
235ab4382d2SGreg Kroah-Hartman 		 * Setup the RTS and DTR signals once the
236ab4382d2SGreg Kroah-Hartman 		 * port is open and ready to respond.
237ab4382d2SGreg Kroah-Hartman 		 */
2389db276f8SPeter Hurley 		if (init_hw && C_BAUD(tty))
239a6845e1eSRafael Gago 			uart_port_dtr_rts(uport, 1);
240ab4382d2SGreg Kroah-Hartman 	}
241ab4382d2SGreg Kroah-Hartman 
2420055197eSJiri Slaby 	/*
2430055197eSJiri Slaby 	 * This is to allow setserial on this port. People may want to set
2440055197eSJiri Slaby 	 * port/irq/type and then reconfigure the port properly if it failed
2450055197eSJiri Slaby 	 * now.
2460055197eSJiri Slaby 	 */
247ab4382d2SGreg Kroah-Hartman 	if (retval && capable(CAP_SYS_ADMIN))
248c0d92be6SJiri Slaby 		return 1;
249c0d92be6SJiri Slaby 
250c0d92be6SJiri Slaby 	return retval;
251c0d92be6SJiri Slaby }
252c0d92be6SJiri Slaby 
253c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state,
254c0d92be6SJiri Slaby 		int init_hw)
255c0d92be6SJiri Slaby {
256c0d92be6SJiri Slaby 	struct tty_port *port = &state->port;
257c0d92be6SJiri Slaby 	int retval;
258c0d92be6SJiri Slaby 
259d41861caSPeter Hurley 	if (tty_port_initialized(port))
260c0d92be6SJiri Slaby 		return 0;
261c0d92be6SJiri Slaby 
262c0d92be6SJiri Slaby 	retval = uart_port_startup(tty, state, init_hw);
263b3b57646SRob Herring 	if (retval)
264b3b57646SRob Herring 		set_bit(TTY_IO_ERROR, &tty->flags);
265ab4382d2SGreg Kroah-Hartman 
266ab4382d2SGreg Kroah-Hartman 	return retval;
267ab4382d2SGreg Kroah-Hartman }
268ab4382d2SGreg Kroah-Hartman 
269ab4382d2SGreg Kroah-Hartman /*
270ab4382d2SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
271ab4382d2SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.  Calls to
272ab4382d2SGreg Kroah-Hartman  * uart_shutdown are serialised by the per-port semaphore.
273af224ca2SPeter Hurley  *
274af224ca2SPeter Hurley  * uport == NULL if uart_port has already been removed
275ab4382d2SGreg Kroah-Hartman  */
276ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
277ab4382d2SGreg Kroah-Hartman {
2784047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
279ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
280a5ba1d95STycho Andersen 	unsigned long flags = 0;
281d7240214SSergey Senozhatsky 	char *xmit_buf = NULL;
282ab4382d2SGreg Kroah-Hartman 
283ab4382d2SGreg Kroah-Hartman 	/*
284ab4382d2SGreg Kroah-Hartman 	 * Set the TTY IO error marker
285ab4382d2SGreg Kroah-Hartman 	 */
286ab4382d2SGreg Kroah-Hartman 	if (tty)
287ab4382d2SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
288ab4382d2SGreg Kroah-Hartman 
289d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
290d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
291d41861caSPeter Hurley 
292ab4382d2SGreg Kroah-Hartman 		/*
293ab4382d2SGreg Kroah-Hartman 		 * Turn off DTR and RTS early.
294ab4382d2SGreg Kroah-Hartman 		 */
295af224ca2SPeter Hurley 		if (uport && uart_console(uport) && tty)
296ae84db96SPeter Hurley 			uport->cons->cflag = tty->termios.c_cflag;
297ae84db96SPeter Hurley 
2989db276f8SPeter Hurley 		if (!tty || C_HUPCL(tty))
299a6845e1eSRafael Gago 			uart_port_dtr_rts(uport, 0);
300ab4382d2SGreg Kroah-Hartman 
301b922e19dSJiri Slaby 		uart_port_shutdown(port);
302ab4382d2SGreg Kroah-Hartman 	}
303ab4382d2SGreg Kroah-Hartman 
304ab4382d2SGreg Kroah-Hartman 	/*
305d208a3bfSDoug Anderson 	 * It's possible for shutdown to be called after suspend if we get
306d208a3bfSDoug Anderson 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
307d208a3bfSDoug Anderson 	 * we don't try to resume a port that has been shutdown.
308ab4382d2SGreg Kroah-Hartman 	 */
30980f02d54SPeter Hurley 	tty_port_set_suspended(port, 0);
310ab4382d2SGreg Kroah-Hartman 
311ab4382d2SGreg Kroah-Hartman 	/*
312d7240214SSergey Senozhatsky 	 * Do not free() the transmit buffer page under the port lock since
313d7240214SSergey Senozhatsky 	 * this can create various circular locking scenarios. For instance,
314d7240214SSergey Senozhatsky 	 * console driver may need to allocate/free a debug object, which
315d7240214SSergey Senozhatsky 	 * can endup in printk() recursion.
316ab4382d2SGreg Kroah-Hartman 	 */
317a5ba1d95STycho Andersen 	uart_port_lock(state, flags);
318d7240214SSergey Senozhatsky 	xmit_buf = state->xmit.buf;
319ab4382d2SGreg Kroah-Hartman 	state->xmit.buf = NULL;
320a5ba1d95STycho Andersen 	uart_port_unlock(uport, flags);
321d7240214SSergey Senozhatsky 
322d7240214SSergey Senozhatsky 	if (xmit_buf)
323d7240214SSergey Senozhatsky 		free_page((unsigned long)xmit_buf);
324ab4382d2SGreg Kroah-Hartman }
325ab4382d2SGreg Kroah-Hartman 
326ab4382d2SGreg Kroah-Hartman /**
327ab4382d2SGreg Kroah-Hartman  *	uart_update_timeout - update per-port FIFO timeout.
328ab4382d2SGreg Kroah-Hartman  *	@port:  uart_port structure describing the port
329ab4382d2SGreg Kroah-Hartman  *	@cflag: termios cflag value
330ab4382d2SGreg Kroah-Hartman  *	@baud:  speed of the port
331ab4382d2SGreg Kroah-Hartman  *
332ab4382d2SGreg Kroah-Hartman  *	Set the port FIFO timeout value.  The @cflag value should
333ab4382d2SGreg Kroah-Hartman  *	reflect the actual hardware settings.
334ab4382d2SGreg Kroah-Hartman  */
335ab4382d2SGreg Kroah-Hartman void
336ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag,
337ab4382d2SGreg Kroah-Hartman 		    unsigned int baud)
338ab4382d2SGreg Kroah-Hartman {
339ab4382d2SGreg Kroah-Hartman 	unsigned int bits;
340ab4382d2SGreg Kroah-Hartman 
341ab4382d2SGreg Kroah-Hartman 	/* byte size and parity */
342ab4382d2SGreg Kroah-Hartman 	switch (cflag & CSIZE) {
343ab4382d2SGreg Kroah-Hartman 	case CS5:
344ab4382d2SGreg Kroah-Hartman 		bits = 7;
345ab4382d2SGreg Kroah-Hartman 		break;
346ab4382d2SGreg Kroah-Hartman 	case CS6:
347ab4382d2SGreg Kroah-Hartman 		bits = 8;
348ab4382d2SGreg Kroah-Hartman 		break;
349ab4382d2SGreg Kroah-Hartman 	case CS7:
350ab4382d2SGreg Kroah-Hartman 		bits = 9;
351ab4382d2SGreg Kroah-Hartman 		break;
352ab4382d2SGreg Kroah-Hartman 	default:
353ab4382d2SGreg Kroah-Hartman 		bits = 10;
354ab4382d2SGreg Kroah-Hartman 		break; /* CS8 */
355ab4382d2SGreg Kroah-Hartman 	}
356ab4382d2SGreg Kroah-Hartman 
357ab4382d2SGreg Kroah-Hartman 	if (cflag & CSTOPB)
358ab4382d2SGreg Kroah-Hartman 		bits++;
359ab4382d2SGreg Kroah-Hartman 	if (cflag & PARENB)
360ab4382d2SGreg Kroah-Hartman 		bits++;
361ab4382d2SGreg Kroah-Hartman 
362ab4382d2SGreg Kroah-Hartman 	/*
363ab4382d2SGreg Kroah-Hartman 	 * The total number of bits to be transmitted in the fifo.
364ab4382d2SGreg Kroah-Hartman 	 */
365ab4382d2SGreg Kroah-Hartman 	bits = bits * port->fifosize;
366ab4382d2SGreg Kroah-Hartman 
367ab4382d2SGreg Kroah-Hartman 	/*
368ab4382d2SGreg Kroah-Hartman 	 * Figure the timeout to send the above number of bits.
369ab4382d2SGreg Kroah-Hartman 	 * Add .02 seconds of slop
370ab4382d2SGreg Kroah-Hartman 	 */
371ab4382d2SGreg Kroah-Hartman 	port->timeout = (HZ * bits) / baud + HZ/50;
372ab4382d2SGreg Kroah-Hartman }
373ab4382d2SGreg Kroah-Hartman 
374ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout);
375ab4382d2SGreg Kroah-Hartman 
376ab4382d2SGreg Kroah-Hartman /**
377ab4382d2SGreg Kroah-Hartman  *	uart_get_baud_rate - return baud rate for a particular port
378ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port in question.
379ab4382d2SGreg Kroah-Hartman  *	@termios: desired termios settings.
380ab4382d2SGreg Kroah-Hartman  *	@old: old termios (or NULL)
381ab4382d2SGreg Kroah-Hartman  *	@min: minimum acceptable baud rate
382ab4382d2SGreg Kroah-Hartman  *	@max: maximum acceptable baud rate
383ab4382d2SGreg Kroah-Hartman  *
384ab4382d2SGreg Kroah-Hartman  *	Decode the termios structure into a numeric baud rate,
385ab4382d2SGreg Kroah-Hartman  *	taking account of the magic 38400 baud rate (with spd_*
386ab4382d2SGreg Kroah-Hartman  *	flags), and mapping the %B0 rate to 9600 baud.
387ab4382d2SGreg Kroah-Hartman  *
388ab4382d2SGreg Kroah-Hartman  *	If the new baud rate is invalid, try the old termios setting.
389ab4382d2SGreg Kroah-Hartman  *	If it's still invalid, we try 9600 baud.
390ab4382d2SGreg Kroah-Hartman  *
391ab4382d2SGreg Kroah-Hartman  *	Update the @termios structure to reflect the baud rate
392ab4382d2SGreg Kroah-Hartman  *	we're actually going to be using. Don't do this for the case
393ab4382d2SGreg Kroah-Hartman  *	where B0 is requested ("hang up").
394ab4382d2SGreg Kroah-Hartman  */
395ab4382d2SGreg Kroah-Hartman unsigned int
396ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
397ab4382d2SGreg Kroah-Hartman 		   struct ktermios *old, unsigned int min, unsigned int max)
398ab4382d2SGreg Kroah-Hartman {
399f10a2233SJoakim Nordell 	unsigned int try;
400f10a2233SJoakim Nordell 	unsigned int baud;
401f10a2233SJoakim Nordell 	unsigned int altbaud;
402ab4382d2SGreg Kroah-Hartman 	int hung_up = 0;
403ab4382d2SGreg Kroah-Hartman 	upf_t flags = port->flags & UPF_SPD_MASK;
404ab4382d2SGreg Kroah-Hartman 
405f10a2233SJoakim Nordell 	switch (flags) {
406f10a2233SJoakim Nordell 	case UPF_SPD_HI:
407ab4382d2SGreg Kroah-Hartman 		altbaud = 57600;
408f10a2233SJoakim Nordell 		break;
409f10a2233SJoakim Nordell 	case UPF_SPD_VHI:
410ab4382d2SGreg Kroah-Hartman 		altbaud = 115200;
411f10a2233SJoakim Nordell 		break;
412f10a2233SJoakim Nordell 	case UPF_SPD_SHI:
413ab4382d2SGreg Kroah-Hartman 		altbaud = 230400;
414f10a2233SJoakim Nordell 		break;
415f10a2233SJoakim Nordell 	case UPF_SPD_WARP:
416ab4382d2SGreg Kroah-Hartman 		altbaud = 460800;
417f10a2233SJoakim Nordell 		break;
418f10a2233SJoakim Nordell 	default:
419f10a2233SJoakim Nordell 		altbaud = 38400;
420f10a2233SJoakim Nordell 		break;
421f10a2233SJoakim Nordell 	}
422ab4382d2SGreg Kroah-Hartman 
423ab4382d2SGreg Kroah-Hartman 	for (try = 0; try < 2; try++) {
424ab4382d2SGreg Kroah-Hartman 		baud = tty_termios_baud_rate(termios);
425ab4382d2SGreg Kroah-Hartman 
426ab4382d2SGreg Kroah-Hartman 		/*
427ab4382d2SGreg Kroah-Hartman 		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
428ab4382d2SGreg Kroah-Hartman 		 * Die! Die! Die!
429ab4382d2SGreg Kroah-Hartman 		 */
430547039ecSPeter Hurley 		if (try == 0 && baud == 38400)
431ab4382d2SGreg Kroah-Hartman 			baud = altbaud;
432ab4382d2SGreg Kroah-Hartman 
433ab4382d2SGreg Kroah-Hartman 		/*
434ab4382d2SGreg Kroah-Hartman 		 * Special case: B0 rate.
435ab4382d2SGreg Kroah-Hartman 		 */
436ab4382d2SGreg Kroah-Hartman 		if (baud == 0) {
437ab4382d2SGreg Kroah-Hartman 			hung_up = 1;
438ab4382d2SGreg Kroah-Hartman 			baud = 9600;
439ab4382d2SGreg Kroah-Hartman 		}
440ab4382d2SGreg Kroah-Hartman 
441ab4382d2SGreg Kroah-Hartman 		if (baud >= min && baud <= max)
442ab4382d2SGreg Kroah-Hartman 			return baud;
443ab4382d2SGreg Kroah-Hartman 
444ab4382d2SGreg Kroah-Hartman 		/*
445ab4382d2SGreg Kroah-Hartman 		 * Oops, the quotient was zero.  Try again with
446ab4382d2SGreg Kroah-Hartman 		 * the old baud rate if possible.
447ab4382d2SGreg Kroah-Hartman 		 */
448ab4382d2SGreg Kroah-Hartman 		termios->c_cflag &= ~CBAUD;
449ab4382d2SGreg Kroah-Hartman 		if (old) {
450ab4382d2SGreg Kroah-Hartman 			baud = tty_termios_baud_rate(old);
451ab4382d2SGreg Kroah-Hartman 			if (!hung_up)
452ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
453ab4382d2SGreg Kroah-Hartman 								baud, baud);
454ab4382d2SGreg Kroah-Hartman 			old = NULL;
455ab4382d2SGreg Kroah-Hartman 			continue;
456ab4382d2SGreg Kroah-Hartman 		}
457ab4382d2SGreg Kroah-Hartman 
458ab4382d2SGreg Kroah-Hartman 		/*
459ab4382d2SGreg Kroah-Hartman 		 * As a last resort, if the range cannot be met then clip to
460ab4382d2SGreg Kroah-Hartman 		 * the nearest chip supported rate.
461ab4382d2SGreg Kroah-Hartman 		 */
462ab4382d2SGreg Kroah-Hartman 		if (!hung_up) {
463ab4382d2SGreg Kroah-Hartman 			if (baud <= min)
464ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
465ab4382d2SGreg Kroah-Hartman 							min + 1, min + 1);
466ab4382d2SGreg Kroah-Hartman 			else
467ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
468ab4382d2SGreg Kroah-Hartman 							max - 1, max - 1);
469ab4382d2SGreg Kroah-Hartman 		}
470ab4382d2SGreg Kroah-Hartman 	}
471ab4382d2SGreg Kroah-Hartman 	/* Should never happen */
472ab4382d2SGreg Kroah-Hartman 	WARN_ON(1);
473ab4382d2SGreg Kroah-Hartman 	return 0;
474ab4382d2SGreg Kroah-Hartman }
475ab4382d2SGreg Kroah-Hartman 
476ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate);
477ab4382d2SGreg Kroah-Hartman 
478ab4382d2SGreg Kroah-Hartman /**
479ab4382d2SGreg Kroah-Hartman  *	uart_get_divisor - return uart clock divisor
480ab4382d2SGreg Kroah-Hartman  *	@port: uart_port structure describing the port.
481ab4382d2SGreg Kroah-Hartman  *	@baud: desired baud rate
482ab4382d2SGreg Kroah-Hartman  *
483ab4382d2SGreg Kroah-Hartman  *	Calculate the uart clock divisor for the port.
484ab4382d2SGreg Kroah-Hartman  */
485ab4382d2SGreg Kroah-Hartman unsigned int
486ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud)
487ab4382d2SGreg Kroah-Hartman {
488ab4382d2SGreg Kroah-Hartman 	unsigned int quot;
489ab4382d2SGreg Kroah-Hartman 
490ab4382d2SGreg Kroah-Hartman 	/*
491ab4382d2SGreg Kroah-Hartman 	 * Old custom speed handling.
492ab4382d2SGreg Kroah-Hartman 	 */
493ab4382d2SGreg Kroah-Hartman 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
494ab4382d2SGreg Kroah-Hartman 		quot = port->custom_divisor;
495ab4382d2SGreg Kroah-Hartman 	else
49697d24634SUwe Kleine-König 		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
497ab4382d2SGreg Kroah-Hartman 
498ab4382d2SGreg Kroah-Hartman 	return quot;
499ab4382d2SGreg Kroah-Hartman }
500ab4382d2SGreg Kroah-Hartman 
501ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor);
502ab4382d2SGreg Kroah-Hartman 
5037c8ab967SPeter Hurley /* Caller holds port mutex */
504ab4382d2SGreg Kroah-Hartman static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
505ab4382d2SGreg Kroah-Hartman 					struct ktermios *old_termios)
506ab4382d2SGreg Kroah-Hartman {
5074047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
508ab4382d2SGreg Kroah-Hartman 	struct ktermios *termios;
509391f93f2SPeter Hurley 	int hw_stopped;
510ab4382d2SGreg Kroah-Hartman 
511ab4382d2SGreg Kroah-Hartman 	/*
512ab4382d2SGreg Kroah-Hartman 	 * If we have no tty, termios, or the port does not exist,
513ab4382d2SGreg Kroah-Hartman 	 * then we can't set the parameters for this port.
514ab4382d2SGreg Kroah-Hartman 	 */
515adc8d746SAlan Cox 	if (!tty || uport->type == PORT_UNKNOWN)
516ab4382d2SGreg Kroah-Hartman 		return;
517ab4382d2SGreg Kroah-Hartman 
518adc8d746SAlan Cox 	termios = &tty->termios;
519c18b55fdSPeter Hurley 	uport->ops->set_termios(uport, termios, old_termios);
520ab4382d2SGreg Kroah-Hartman 
521ab4382d2SGreg Kroah-Hartman 	/*
522299245a1SPeter Hurley 	 * Set modem status enables based on termios cflag
523ab4382d2SGreg Kroah-Hartman 	 */
524299245a1SPeter Hurley 	spin_lock_irq(&uport->lock);
525ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
526299245a1SPeter Hurley 		uport->status |= UPSTAT_CTS_ENABLE;
527ab4382d2SGreg Kroah-Hartman 	else
528299245a1SPeter Hurley 		uport->status &= ~UPSTAT_CTS_ENABLE;
529ab4382d2SGreg Kroah-Hartman 
530ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CLOCAL)
531299245a1SPeter Hurley 		uport->status &= ~UPSTAT_DCD_ENABLE;
532ab4382d2SGreg Kroah-Hartman 	else
533299245a1SPeter Hurley 		uport->status |= UPSTAT_DCD_ENABLE;
534391f93f2SPeter Hurley 
535391f93f2SPeter Hurley 	/* reset sw-assisted CTS flow control based on (possibly) new mode */
536391f93f2SPeter Hurley 	hw_stopped = uport->hw_stopped;
537391f93f2SPeter Hurley 	uport->hw_stopped = uart_softcts_mode(uport) &&
538391f93f2SPeter Hurley 				!(uport->ops->get_mctrl(uport) & TIOCM_CTS);
539391f93f2SPeter Hurley 	if (uport->hw_stopped) {
540391f93f2SPeter Hurley 		if (!hw_stopped)
541391f93f2SPeter Hurley 			uport->ops->stop_tx(uport);
542391f93f2SPeter Hurley 	} else {
543391f93f2SPeter Hurley 		if (hw_stopped)
544391f93f2SPeter Hurley 			__uart_start(tty);
545391f93f2SPeter Hurley 	}
546299245a1SPeter Hurley 	spin_unlock_irq(&uport->lock);
547ab4382d2SGreg Kroah-Hartman }
548ab4382d2SGreg Kroah-Hartman 
549f5291eccSPeter Hurley static int uart_put_char(struct tty_struct *tty, unsigned char c)
550ab4382d2SGreg Kroah-Hartman {
551f5291eccSPeter Hurley 	struct uart_state *state = tty->driver_data;
5529ed19428SPeter Hurley 	struct uart_port *port;
553f5291eccSPeter Hurley 	struct circ_buf *circ;
554ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
555ab4382d2SGreg Kroah-Hartman 	int ret = 0;
556ab4382d2SGreg Kroah-Hartman 
557f5291eccSPeter Hurley 	circ = &state->xmit;
5589ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
559aff9cf59SSamir Virmani 	if (!circ->buf) {
560aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
561aff9cf59SSamir Virmani 		return 0;
562aff9cf59SSamir Virmani 	}
563aff9cf59SSamir Virmani 
5649ed19428SPeter Hurley 	if (port && uart_circ_chars_free(circ) != 0) {
565ab4382d2SGreg Kroah-Hartman 		circ->buf[circ->head] = c;
566ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
567ab4382d2SGreg Kroah-Hartman 		ret = 1;
568ab4382d2SGreg Kroah-Hartman 	}
5699ed19428SPeter Hurley 	uart_port_unlock(port, flags);
570ab4382d2SGreg Kroah-Hartman 	return ret;
571ab4382d2SGreg Kroah-Hartman }
572ab4382d2SGreg Kroah-Hartman 
573ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty)
574ab4382d2SGreg Kroah-Hartman {
575ab4382d2SGreg Kroah-Hartman 	uart_start(tty);
576ab4382d2SGreg Kroah-Hartman }
577ab4382d2SGreg Kroah-Hartman 
578ab4382d2SGreg Kroah-Hartman static int uart_write(struct tty_struct *tty,
579ab4382d2SGreg Kroah-Hartman 					const unsigned char *buf, int count)
580ab4382d2SGreg Kroah-Hartman {
581ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
582ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
583ab4382d2SGreg Kroah-Hartman 	struct circ_buf *circ;
584ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
585ab4382d2SGreg Kroah-Hartman 	int c, ret = 0;
586ab4382d2SGreg Kroah-Hartman 
587ab4382d2SGreg Kroah-Hartman 	/*
588ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
589ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
590ab4382d2SGreg Kroah-Hartman 	 */
591ab4382d2SGreg Kroah-Hartman 	if (!state) {
592ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
593ab4382d2SGreg Kroah-Hartman 		return -EL3HLT;
594ab4382d2SGreg Kroah-Hartman 	}
595ab4382d2SGreg Kroah-Hartman 
5969ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
597aff9cf59SSamir Virmani 	circ = &state->xmit;
598aff9cf59SSamir Virmani 	if (!circ->buf) {
599aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
600aff9cf59SSamir Virmani 		return 0;
601aff9cf59SSamir Virmani 	}
602aff9cf59SSamir Virmani 
6039ed19428SPeter Hurley 	while (port) {
604ab4382d2SGreg Kroah-Hartman 		c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
605ab4382d2SGreg Kroah-Hartman 		if (count < c)
606ab4382d2SGreg Kroah-Hartman 			c = count;
607ab4382d2SGreg Kroah-Hartman 		if (c <= 0)
608ab4382d2SGreg Kroah-Hartman 			break;
609ab4382d2SGreg Kroah-Hartman 		memcpy(circ->buf + circ->head, buf, c);
610ab4382d2SGreg Kroah-Hartman 		circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
611ab4382d2SGreg Kroah-Hartman 		buf += c;
612ab4382d2SGreg Kroah-Hartman 		count -= c;
613ab4382d2SGreg Kroah-Hartman 		ret += c;
614ab4382d2SGreg Kroah-Hartman 	}
61564dbee31SPeter Hurley 
61664dbee31SPeter Hurley 	__uart_start(tty);
6179ed19428SPeter Hurley 	uart_port_unlock(port, flags);
618ab4382d2SGreg Kroah-Hartman 	return ret;
619ab4382d2SGreg Kroah-Hartman }
620ab4382d2SGreg Kroah-Hartman 
621ab4382d2SGreg Kroah-Hartman static int uart_write_room(struct tty_struct *tty)
622ab4382d2SGreg Kroah-Hartman {
623ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6249ed19428SPeter Hurley 	struct uart_port *port;
625ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
626ab4382d2SGreg Kroah-Hartman 	int ret;
627ab4382d2SGreg Kroah-Hartman 
6289ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
629ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_free(&state->xmit);
6309ed19428SPeter Hurley 	uart_port_unlock(port, flags);
631ab4382d2SGreg Kroah-Hartman 	return ret;
632ab4382d2SGreg Kroah-Hartman }
633ab4382d2SGreg Kroah-Hartman 
634ab4382d2SGreg Kroah-Hartman static int uart_chars_in_buffer(struct tty_struct *tty)
635ab4382d2SGreg Kroah-Hartman {
636ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6379ed19428SPeter Hurley 	struct uart_port *port;
638ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
639ab4382d2SGreg Kroah-Hartman 	int ret;
640ab4382d2SGreg Kroah-Hartman 
6419ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
642ab4382d2SGreg Kroah-Hartman 	ret = uart_circ_chars_pending(&state->xmit);
6439ed19428SPeter Hurley 	uart_port_unlock(port, flags);
644ab4382d2SGreg Kroah-Hartman 	return ret;
645ab4382d2SGreg Kroah-Hartman }
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty)
648ab4382d2SGreg Kroah-Hartman {
649ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
650ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
651ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
652ab4382d2SGreg Kroah-Hartman 
653ab4382d2SGreg Kroah-Hartman 	/*
654ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
655ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
656ab4382d2SGreg Kroah-Hartman 	 */
657ab4382d2SGreg Kroah-Hartman 	if (!state) {
658ab4382d2SGreg Kroah-Hartman 		WARN_ON(1);
659ab4382d2SGreg Kroah-Hartman 		return;
660ab4382d2SGreg Kroah-Hartman 	}
661ab4382d2SGreg Kroah-Hartman 
662ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
663ab4382d2SGreg Kroah-Hartman 
6649ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6659ed19428SPeter Hurley 	if (!port)
6669ed19428SPeter Hurley 		return;
667ab4382d2SGreg Kroah-Hartman 	uart_circ_clear(&state->xmit);
668ab4382d2SGreg Kroah-Hartman 	if (port->ops->flush_buffer)
669ab4382d2SGreg Kroah-Hartman 		port->ops->flush_buffer(port);
6709ed19428SPeter Hurley 	uart_port_unlock(port, flags);
671d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman 
674ab4382d2SGreg Kroah-Hartman /*
675ab4382d2SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
676ab4382d2SGreg Kroah-Hartman  * the device
677ab4382d2SGreg Kroah-Hartman  */
678ab4382d2SGreg Kroah-Hartman static void uart_send_xchar(struct tty_struct *tty, char ch)
679ab4382d2SGreg Kroah-Hartman {
680ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6819ed19428SPeter Hurley 	struct uart_port *port;
682ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
683ab4382d2SGreg Kroah-Hartman 
6849ed19428SPeter Hurley 	port = uart_port_ref(state);
6859ed19428SPeter Hurley 	if (!port)
6869ed19428SPeter Hurley 		return;
6879ed19428SPeter Hurley 
688ab4382d2SGreg Kroah-Hartman 	if (port->ops->send_xchar)
689ab4382d2SGreg Kroah-Hartman 		port->ops->send_xchar(port, ch);
690ab4382d2SGreg Kroah-Hartman 	else {
691ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
692c235ccc1SPeter Hurley 		port->x_char = ch;
693c235ccc1SPeter Hurley 		if (ch)
694ab4382d2SGreg Kroah-Hartman 			port->ops->start_tx(port);
695ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
696ab4382d2SGreg Kroah-Hartman 	}
6979ed19428SPeter Hurley 	uart_port_deref(port);
698ab4382d2SGreg Kroah-Hartman }
699ab4382d2SGreg Kroah-Hartman 
700ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty)
701ab4382d2SGreg Kroah-Hartman {
702ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
703c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7049ed19428SPeter Hurley 	struct uart_port *port;
705ab4382d2SGreg Kroah-Hartman 
7069ed19428SPeter Hurley 	port = uart_port_ref(state);
7079ed19428SPeter Hurley 	if (!port)
7089ed19428SPeter Hurley 		return;
7099ed19428SPeter Hurley 
710ab4382d2SGreg Kroah-Hartman 	if (I_IXOFF(tty))
711391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7129db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
713391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7149aba8d5bSRussell King 
715391f93f2SPeter Hurley 	if (port->status & mask) {
7169aba8d5bSRussell King 		port->ops->throttle(port);
717391f93f2SPeter Hurley 		mask &= ~port->status;
7189aba8d5bSRussell King 	}
7199aba8d5bSRussell King 
720391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
7219aba8d5bSRussell King 		uart_clear_mctrl(port, TIOCM_RTS);
722b4749b97SPeter Hurley 
723b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
724b4749b97SPeter Hurley 		uart_send_xchar(tty, STOP_CHAR(tty));
7259ed19428SPeter Hurley 
7269ed19428SPeter Hurley 	uart_port_deref(port);
727ab4382d2SGreg Kroah-Hartman }
728ab4382d2SGreg Kroah-Hartman 
729ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty)
730ab4382d2SGreg Kroah-Hartman {
731ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
732c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7339ed19428SPeter Hurley 	struct uart_port *port;
734ab4382d2SGreg Kroah-Hartman 
7359ed19428SPeter Hurley 	port = uart_port_ref(state);
7369ed19428SPeter Hurley 	if (!port)
7379ed19428SPeter Hurley 		return;
7389ed19428SPeter Hurley 
7399aba8d5bSRussell King 	if (I_IXOFF(tty))
740391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7419db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
742391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7439aba8d5bSRussell King 
744391f93f2SPeter Hurley 	if (port->status & mask) {
7459aba8d5bSRussell King 		port->ops->unthrottle(port);
746391f93f2SPeter Hurley 		mask &= ~port->status;
7479aba8d5bSRussell King 	}
7489aba8d5bSRussell King 
749391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
750ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(port, TIOCM_RTS);
751b4749b97SPeter Hurley 
752b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
753b4749b97SPeter Hurley 		uart_send_xchar(tty, START_CHAR(tty));
7549ed19428SPeter Hurley 
7559ed19428SPeter Hurley 	uart_port_deref(port);
756ab4382d2SGreg Kroah-Hartman }
757ab4382d2SGreg Kroah-Hartman 
7584047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
759ab4382d2SGreg Kroah-Hartman {
7609f109694SAlan Cox 	struct uart_state *state = container_of(port, struct uart_state, port);
7614047b371SPeter Hurley 	struct uart_port *uport;
7624047b371SPeter Hurley 	int ret = -ENODEV;
7637ba2e769SAlan Cox 
76437cd0c99SFengguang Wu 	memset(retinfo, 0, sizeof(*retinfo));
7657ba2e769SAlan Cox 
7663abe8c76SPeter Hurley 	/*
7673abe8c76SPeter Hurley 	 * Ensure the state we copy is consistent and no hardware changes
7683abe8c76SPeter Hurley 	 * occur as we go
7693abe8c76SPeter Hurley 	 */
7703abe8c76SPeter Hurley 	mutex_lock(&port->mutex);
7714047b371SPeter Hurley 	uport = uart_port_check(state);
7724047b371SPeter Hurley 	if (!uport)
7734047b371SPeter Hurley 		goto out;
7744047b371SPeter Hurley 
7757ba2e769SAlan Cox 	retinfo->type	    = uport->type;
7767ba2e769SAlan Cox 	retinfo->line	    = uport->line;
7777ba2e769SAlan Cox 	retinfo->port	    = uport->iobase;
7787ba2e769SAlan Cox 	if (HIGH_BITS_OFFSET)
7797ba2e769SAlan Cox 		retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
7807ba2e769SAlan Cox 	retinfo->irq		    = uport->irq;
781a17e74c5SAndy Shevchenko 	retinfo->flags	    = (__force int)uport->flags;
7827ba2e769SAlan Cox 	retinfo->xmit_fifo_size  = uport->fifosize;
7837ba2e769SAlan Cox 	retinfo->baud_base	    = uport->uartclk / 16;
7847ba2e769SAlan Cox 	retinfo->close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
7857ba2e769SAlan Cox 	retinfo->closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
7867ba2e769SAlan Cox 				ASYNC_CLOSING_WAIT_NONE :
7877ba2e769SAlan Cox 				jiffies_to_msecs(port->closing_wait) / 10;
7887ba2e769SAlan Cox 	retinfo->custom_divisor  = uport->custom_divisor;
7897ba2e769SAlan Cox 	retinfo->hub6	    = uport->hub6;
7907ba2e769SAlan Cox 	retinfo->io_type         = uport->iotype;
7917ba2e769SAlan Cox 	retinfo->iomem_reg_shift = uport->regshift;
7927ba2e769SAlan Cox 	retinfo->iomem_base      = (void *)(unsigned long)uport->mapbase;
7934047b371SPeter Hurley 
7944047b371SPeter Hurley 	ret = 0;
7954047b371SPeter Hurley out:
796ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
7974047b371SPeter Hurley 	return ret;
7989f109694SAlan Cox }
7999f109694SAlan Cox 
8005099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty,
8015099d234SAl Viro 			 struct serial_struct *ss)
8029f109694SAlan Cox {
8035099d234SAl Viro 	struct uart_state *state = tty->driver_data;
8045099d234SAl Viro 	struct tty_port *port = &state->port;
8053abe8c76SPeter Hurley 
8065099d234SAl Viro 	return uart_get_info(port, ss) < 0 ? -EIO : 0;
807ab4382d2SGreg Kroah-Hartman }
808ab4382d2SGreg Kroah-Hartman 
8097ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
8107ba2e769SAlan Cox 			 struct uart_state *state,
8117ba2e769SAlan Cox 			 struct serial_struct *new_info)
812ab4382d2SGreg Kroah-Hartman {
8134047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
814ab4382d2SGreg Kroah-Hartman 	unsigned long new_port;
815ab4382d2SGreg Kroah-Hartman 	unsigned int change_irq, change_port, closing_wait;
816ab4382d2SGreg Kroah-Hartman 	unsigned int old_custom_divisor, close_delay;
817ab4382d2SGreg Kroah-Hartman 	upf_t old_flags, new_flags;
818ab4382d2SGreg Kroah-Hartman 	int retval = 0;
819ab4382d2SGreg Kroah-Hartman 
8204047b371SPeter Hurley 	if (!uport)
8214047b371SPeter Hurley 		return -EIO;
8224047b371SPeter Hurley 
8237ba2e769SAlan Cox 	new_port = new_info->port;
824ab4382d2SGreg Kroah-Hartman 	if (HIGH_BITS_OFFSET)
8257ba2e769SAlan Cox 		new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
826ab4382d2SGreg Kroah-Hartman 
8277ba2e769SAlan Cox 	new_info->irq = irq_canonicalize(new_info->irq);
8287ba2e769SAlan Cox 	close_delay = msecs_to_jiffies(new_info->close_delay * 10);
8297ba2e769SAlan Cox 	closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
8304cb0fbfdSJiri Slaby 			ASYNC_CLOSING_WAIT_NONE :
8317ba2e769SAlan Cox 			msecs_to_jiffies(new_info->closing_wait * 10);
832ab4382d2SGreg Kroah-Hartman 
833ab4382d2SGreg Kroah-Hartman 
834ab4382d2SGreg Kroah-Hartman 	change_irq  = !(uport->flags & UPF_FIXED_PORT)
8357ba2e769SAlan Cox 		&& new_info->irq != uport->irq;
836ab4382d2SGreg Kroah-Hartman 
837ab4382d2SGreg Kroah-Hartman 	/*
838ab4382d2SGreg Kroah-Hartman 	 * Since changing the 'type' of the port changes its resource
839ab4382d2SGreg Kroah-Hartman 	 * allocations, we should treat type changes the same as
840ab4382d2SGreg Kroah-Hartman 	 * IO port changes.
841ab4382d2SGreg Kroah-Hartman 	 */
842ab4382d2SGreg Kroah-Hartman 	change_port = !(uport->flags & UPF_FIXED_PORT)
843ab4382d2SGreg Kroah-Hartman 		&& (new_port != uport->iobase ||
8447ba2e769SAlan Cox 		    (unsigned long)new_info->iomem_base != uport->mapbase ||
8457ba2e769SAlan Cox 		    new_info->hub6 != uport->hub6 ||
8467ba2e769SAlan Cox 		    new_info->io_type != uport->iotype ||
8477ba2e769SAlan Cox 		    new_info->iomem_reg_shift != uport->regshift ||
8487ba2e769SAlan Cox 		    new_info->type != uport->type);
849ab4382d2SGreg Kroah-Hartman 
850ab4382d2SGreg Kroah-Hartman 	old_flags = uport->flags;
851a17e74c5SAndy Shevchenko 	new_flags = (__force upf_t)new_info->flags;
852ab4382d2SGreg Kroah-Hartman 	old_custom_divisor = uport->custom_divisor;
853ab4382d2SGreg Kroah-Hartman 
854ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
855ab4382d2SGreg Kroah-Hartman 		retval = -EPERM;
856ab4382d2SGreg Kroah-Hartman 		if (change_irq || change_port ||
8577ba2e769SAlan Cox 		    (new_info->baud_base != uport->uartclk / 16) ||
858ab4382d2SGreg Kroah-Hartman 		    (close_delay != port->close_delay) ||
859ab4382d2SGreg Kroah-Hartman 		    (closing_wait != port->closing_wait) ||
8607ba2e769SAlan Cox 		    (new_info->xmit_fifo_size &&
8617ba2e769SAlan Cox 		     new_info->xmit_fifo_size != uport->fifosize) ||
862ab4382d2SGreg Kroah-Hartman 		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
863ab4382d2SGreg Kroah-Hartman 			goto exit;
864ab4382d2SGreg Kroah-Hartman 		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
865ab4382d2SGreg Kroah-Hartman 			       (new_flags & UPF_USR_MASK));
8667ba2e769SAlan Cox 		uport->custom_divisor = new_info->custom_divisor;
867ab4382d2SGreg Kroah-Hartman 		goto check_and_exit;
868ab4382d2SGreg Kroah-Hartman 	}
869ab4382d2SGreg Kroah-Hartman 
870794edf30SDavid Howells 	retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
871794edf30SDavid Howells 	if (retval && (change_irq || change_port))
872794edf30SDavid Howells 		goto exit;
873794edf30SDavid Howells 
874ab4382d2SGreg Kroah-Hartman 	/*
875ab4382d2SGreg Kroah-Hartman 	 * Ask the low level driver to verify the settings.
876ab4382d2SGreg Kroah-Hartman 	 */
877ab4382d2SGreg Kroah-Hartman 	if (uport->ops->verify_port)
8787ba2e769SAlan Cox 		retval = uport->ops->verify_port(uport, new_info);
879ab4382d2SGreg Kroah-Hartman 
8807ba2e769SAlan Cox 	if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
8817ba2e769SAlan Cox 	    (new_info->baud_base < 9600))
882ab4382d2SGreg Kroah-Hartman 		retval = -EINVAL;
883ab4382d2SGreg Kroah-Hartman 
884ab4382d2SGreg Kroah-Hartman 	if (retval)
885ab4382d2SGreg Kroah-Hartman 		goto exit;
886ab4382d2SGreg Kroah-Hartman 
887ab4382d2SGreg Kroah-Hartman 	if (change_port || change_irq) {
888ab4382d2SGreg Kroah-Hartman 		retval = -EBUSY;
889ab4382d2SGreg Kroah-Hartman 
890ab4382d2SGreg Kroah-Hartman 		/*
891ab4382d2SGreg Kroah-Hartman 		 * Make sure that we are the sole user of this port.
892ab4382d2SGreg Kroah-Hartman 		 */
893ab4382d2SGreg Kroah-Hartman 		if (tty_port_users(port) > 1)
894ab4382d2SGreg Kroah-Hartman 			goto exit;
895ab4382d2SGreg Kroah-Hartman 
896ab4382d2SGreg Kroah-Hartman 		/*
897ab4382d2SGreg Kroah-Hartman 		 * We need to shutdown the serial port at the old
898ab4382d2SGreg Kroah-Hartman 		 * port/type/irq combination.
899ab4382d2SGreg Kroah-Hartman 		 */
900ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
901ab4382d2SGreg Kroah-Hartman 	}
902ab4382d2SGreg Kroah-Hartman 
903ab4382d2SGreg Kroah-Hartman 	if (change_port) {
904ab4382d2SGreg Kroah-Hartman 		unsigned long old_iobase, old_mapbase;
905ab4382d2SGreg Kroah-Hartman 		unsigned int old_type, old_iotype, old_hub6, old_shift;
906ab4382d2SGreg Kroah-Hartman 
907ab4382d2SGreg Kroah-Hartman 		old_iobase = uport->iobase;
908ab4382d2SGreg Kroah-Hartman 		old_mapbase = uport->mapbase;
909ab4382d2SGreg Kroah-Hartman 		old_type = uport->type;
910ab4382d2SGreg Kroah-Hartman 		old_hub6 = uport->hub6;
911ab4382d2SGreg Kroah-Hartman 		old_iotype = uport->iotype;
912ab4382d2SGreg Kroah-Hartman 		old_shift = uport->regshift;
913ab4382d2SGreg Kroah-Hartman 
914ab4382d2SGreg Kroah-Hartman 		/*
915ab4382d2SGreg Kroah-Hartman 		 * Free and release old regions
916ab4382d2SGreg Kroah-Hartman 		 */
917a7cfaf16SFabio Estevam 		if (old_type != PORT_UNKNOWN && uport->ops->release_port)
918ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
919ab4382d2SGreg Kroah-Hartman 
920ab4382d2SGreg Kroah-Hartman 		uport->iobase = new_port;
9217ba2e769SAlan Cox 		uport->type = new_info->type;
9227ba2e769SAlan Cox 		uport->hub6 = new_info->hub6;
9237ba2e769SAlan Cox 		uport->iotype = new_info->io_type;
9247ba2e769SAlan Cox 		uport->regshift = new_info->iomem_reg_shift;
9257ba2e769SAlan Cox 		uport->mapbase = (unsigned long)new_info->iomem_base;
926ab4382d2SGreg Kroah-Hartman 
927ab4382d2SGreg Kroah-Hartman 		/*
928ab4382d2SGreg Kroah-Hartman 		 * Claim and map the new regions
929ab4382d2SGreg Kroah-Hartman 		 */
930a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
931ab4382d2SGreg Kroah-Hartman 			retval = uport->ops->request_port(uport);
932ab4382d2SGreg Kroah-Hartman 		} else {
933ab4382d2SGreg Kroah-Hartman 			/* Always success - Jean II */
934ab4382d2SGreg Kroah-Hartman 			retval = 0;
935ab4382d2SGreg Kroah-Hartman 		}
936ab4382d2SGreg Kroah-Hartman 
937ab4382d2SGreg Kroah-Hartman 		/*
938ab4382d2SGreg Kroah-Hartman 		 * If we fail to request resources for the
939ab4382d2SGreg Kroah-Hartman 		 * new port, try to restore the old settings.
940ab4382d2SGreg Kroah-Hartman 		 */
9417deb39edSThomas Pfaff 		if (retval) {
942ab4382d2SGreg Kroah-Hartman 			uport->iobase = old_iobase;
943ab4382d2SGreg Kroah-Hartman 			uport->type = old_type;
944ab4382d2SGreg Kroah-Hartman 			uport->hub6 = old_hub6;
945ab4382d2SGreg Kroah-Hartman 			uport->iotype = old_iotype;
946ab4382d2SGreg Kroah-Hartman 			uport->regshift = old_shift;
947ab4382d2SGreg Kroah-Hartman 			uport->mapbase = old_mapbase;
9487deb39edSThomas Pfaff 
9497deb39edSThomas Pfaff 			if (old_type != PORT_UNKNOWN) {
950ab4382d2SGreg Kroah-Hartman 				retval = uport->ops->request_port(uport);
951ab4382d2SGreg Kroah-Hartman 				/*
952ab4382d2SGreg Kroah-Hartman 				 * If we failed to restore the old settings,
953ab4382d2SGreg Kroah-Hartman 				 * we fail like this.
954ab4382d2SGreg Kroah-Hartman 				 */
955ab4382d2SGreg Kroah-Hartman 				if (retval)
956ab4382d2SGreg Kroah-Hartman 					uport->type = PORT_UNKNOWN;
957ab4382d2SGreg Kroah-Hartman 
958ab4382d2SGreg Kroah-Hartman 				/*
959ab4382d2SGreg Kroah-Hartman 				 * We failed anyway.
960ab4382d2SGreg Kroah-Hartman 				 */
961ab4382d2SGreg Kroah-Hartman 				retval = -EBUSY;
9627deb39edSThomas Pfaff 			}
9637deb39edSThomas Pfaff 
964ab4382d2SGreg Kroah-Hartman 			/* Added to return the correct error -Ram Gupta */
965ab4382d2SGreg Kroah-Hartman 			goto exit;
966ab4382d2SGreg Kroah-Hartman 		}
967ab4382d2SGreg Kroah-Hartman 	}
968ab4382d2SGreg Kroah-Hartman 
969ab4382d2SGreg Kroah-Hartman 	if (change_irq)
9707ba2e769SAlan Cox 		uport->irq      = new_info->irq;
971ab4382d2SGreg Kroah-Hartman 	if (!(uport->flags & UPF_FIXED_PORT))
9727ba2e769SAlan Cox 		uport->uartclk  = new_info->baud_base * 16;
973ab4382d2SGreg Kroah-Hartman 	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
974ab4382d2SGreg Kroah-Hartman 				 (new_flags & UPF_CHANGE_MASK);
9757ba2e769SAlan Cox 	uport->custom_divisor   = new_info->custom_divisor;
976ab4382d2SGreg Kroah-Hartman 	port->close_delay     = close_delay;
977ab4382d2SGreg Kroah-Hartman 	port->closing_wait    = closing_wait;
9787ba2e769SAlan Cox 	if (new_info->xmit_fifo_size)
9797ba2e769SAlan Cox 		uport->fifosize = new_info->xmit_fifo_size;
980d6c53c0eSJiri Slaby 	port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
981ab4382d2SGreg Kroah-Hartman 
982ab4382d2SGreg Kroah-Hartman  check_and_exit:
983ab4382d2SGreg Kroah-Hartman 	retval = 0;
984ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
985ab4382d2SGreg Kroah-Hartman 		goto exit;
986d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
987ab4382d2SGreg Kroah-Hartman 		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
988ab4382d2SGreg Kroah-Hartman 		    old_custom_divisor != uport->custom_divisor) {
989ab4382d2SGreg Kroah-Hartman 			/*
990ab4382d2SGreg Kroah-Hartman 			 * If they're setting up a custom divisor or speed,
991dc2f7271SJohan Hovold 			 * instead of clearing it, then bitch about it.
992ab4382d2SGreg Kroah-Hartman 			 */
993ab4382d2SGreg Kroah-Hartman 			if (uport->flags & UPF_SPD_MASK) {
994dc2f7271SJohan Hovold 				dev_notice_ratelimited(uport->dev,
9952f2dafe7SSudip Mukherjee 				       "%s sets custom speed on %s. This is deprecated.\n",
9962f2dafe7SSudip Mukherjee 				      current->comm,
997429b4749SRasmus Villemoes 				      tty_name(port->tty));
998ab4382d2SGreg Kroah-Hartman 			}
999ab4382d2SGreg Kroah-Hartman 			uart_change_speed(tty, state, NULL);
1000ab4382d2SGreg Kroah-Hartman 		}
1001b3b57646SRob Herring 	} else {
1002ab4382d2SGreg Kroah-Hartman 		retval = uart_startup(tty, state, 1);
100344117a1dSSebastian Andrzej Siewior 		if (retval == 0)
100444117a1dSSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1005b3b57646SRob Herring 		if (retval > 0)
1006b3b57646SRob Herring 			retval = 0;
1007b3b57646SRob Herring 	}
1008ab4382d2SGreg Kroah-Hartman  exit:
10097ba2e769SAlan Cox 	return retval;
10107ba2e769SAlan Cox }
10117ba2e769SAlan Cox 
10125099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
10137ba2e769SAlan Cox {
10145099d234SAl Viro 	struct uart_state *state = tty->driver_data;
10157ba2e769SAlan Cox 	struct tty_port *port = &state->port;
10167ba2e769SAlan Cox 	int retval;
10177ba2e769SAlan Cox 
10185099d234SAl Viro 	down_write(&tty->termios_rwsem);
10197ba2e769SAlan Cox 	/*
10207ba2e769SAlan Cox 	 * This semaphore protects port->count.  It is also
10217ba2e769SAlan Cox 	 * very useful to prevent opens.  Also, take the
10227ba2e769SAlan Cox 	 * port configuration semaphore to make sure that a
10237ba2e769SAlan Cox 	 * module insertion/removal doesn't change anything
10247ba2e769SAlan Cox 	 * under us.
10257ba2e769SAlan Cox 	 */
10267ba2e769SAlan Cox 	mutex_lock(&port->mutex);
10275099d234SAl Viro 	retval = uart_set_info(tty, port, state, ss);
1028ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
10295099d234SAl Viro 	up_write(&tty->termios_rwsem);
1030ab4382d2SGreg Kroah-Hartman 	return retval;
1031ab4382d2SGreg Kroah-Hartman }
1032ab4382d2SGreg Kroah-Hartman 
1033ab4382d2SGreg Kroah-Hartman /**
1034ab4382d2SGreg Kroah-Hartman  *	uart_get_lsr_info	-	get line status register info
1035ab4382d2SGreg Kroah-Hartman  *	@tty: tty associated with the UART
1036ab4382d2SGreg Kroah-Hartman  *	@state: UART being queried
1037ab4382d2SGreg Kroah-Hartman  *	@value: returned modem value
1038ab4382d2SGreg Kroah-Hartman  */
1039ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty,
1040ab4382d2SGreg Kroah-Hartman 			struct uart_state *state, unsigned int __user *value)
1041ab4382d2SGreg Kroah-Hartman {
10424047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1043ab4382d2SGreg Kroah-Hartman 	unsigned int result;
1044ab4382d2SGreg Kroah-Hartman 
1045ab4382d2SGreg Kroah-Hartman 	result = uport->ops->tx_empty(uport);
1046ab4382d2SGreg Kroah-Hartman 
1047ab4382d2SGreg Kroah-Hartman 	/*
1048ab4382d2SGreg Kroah-Hartman 	 * If we're about to load something into the transmit
1049ab4382d2SGreg Kroah-Hartman 	 * register, we'll pretend the transmitter isn't empty to
1050ab4382d2SGreg Kroah-Hartman 	 * avoid a race condition (depending on when the transmit
1051ab4382d2SGreg Kroah-Hartman 	 * interrupt happens).
1052ab4382d2SGreg Kroah-Hartman 	 */
1053ab4382d2SGreg Kroah-Hartman 	if (uport->x_char ||
1054ab4382d2SGreg Kroah-Hartman 	    ((uart_circ_chars_pending(&state->xmit) > 0) &&
1055d01f4d18SPeter Hurley 	     !uart_tx_stopped(uport)))
1056ab4382d2SGreg Kroah-Hartman 		result &= ~TIOCSER_TEMT;
1057ab4382d2SGreg Kroah-Hartman 
1058ab4382d2SGreg Kroah-Hartman 	return put_user(result, value);
1059ab4382d2SGreg Kroah-Hartman }
1060ab4382d2SGreg Kroah-Hartman 
106160b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty)
1062ab4382d2SGreg Kroah-Hartman {
1063ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1064ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10654047b371SPeter Hurley 	struct uart_port *uport;
1066ab4382d2SGreg Kroah-Hartman 	int result = -EIO;
1067ab4382d2SGreg Kroah-Hartman 
1068ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10694047b371SPeter Hurley 	uport = uart_port_check(state);
10704047b371SPeter Hurley 	if (!uport)
10714047b371SPeter Hurley 		goto out;
10724047b371SPeter Hurley 
107318900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1074ab4382d2SGreg Kroah-Hartman 		result = uport->mctrl;
1075ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1076ab4382d2SGreg Kroah-Hartman 		result |= uport->ops->get_mctrl(uport);
1077ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1078ab4382d2SGreg Kroah-Hartman 	}
10794047b371SPeter Hurley out:
1080ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1081ab4382d2SGreg Kroah-Hartman 	return result;
1082ab4382d2SGreg Kroah-Hartman }
1083ab4382d2SGreg Kroah-Hartman 
1084ab4382d2SGreg Kroah-Hartman static int
108520b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1086ab4382d2SGreg Kroah-Hartman {
1087ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1088ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
10894047b371SPeter Hurley 	struct uart_port *uport;
1090ab4382d2SGreg Kroah-Hartman 	int ret = -EIO;
1091ab4382d2SGreg Kroah-Hartman 
1092ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
10934047b371SPeter Hurley 	uport = uart_port_check(state);
10944047b371SPeter Hurley 	if (!uport)
10954047b371SPeter Hurley 		goto out;
10964047b371SPeter Hurley 
109718900ca6SPeter Hurley 	if (!tty_io_error(tty)) {
1098ab4382d2SGreg Kroah-Hartman 		uart_update_mctrl(uport, set, clear);
1099ab4382d2SGreg Kroah-Hartman 		ret = 0;
1100ab4382d2SGreg Kroah-Hartman 	}
11014047b371SPeter Hurley out:
1102ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1103ab4382d2SGreg Kroah-Hartman 	return ret;
1104ab4382d2SGreg Kroah-Hartman }
1105ab4382d2SGreg Kroah-Hartman 
1106ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state)
1107ab4382d2SGreg Kroah-Hartman {
1108ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1109ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11104047b371SPeter Hurley 	struct uart_port *uport;
11114047b371SPeter Hurley 	int ret = -EIO;
1112ab4382d2SGreg Kroah-Hartman 
1113ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
11144047b371SPeter Hurley 	uport = uart_port_check(state);
11154047b371SPeter Hurley 	if (!uport)
11164047b371SPeter Hurley 		goto out;
1117ab4382d2SGreg Kroah-Hartman 
11187d73170eSJiangfeng Xiao 	if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1119ab4382d2SGreg Kroah-Hartman 		uport->ops->break_ctl(uport, break_state);
11204047b371SPeter Hurley 	ret = 0;
11214047b371SPeter Hurley out:
1122ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
11234047b371SPeter Hurley 	return ret;
1124ab4382d2SGreg Kroah-Hartman }
1125ab4382d2SGreg Kroah-Hartman 
1126ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1127ab4382d2SGreg Kroah-Hartman {
1128ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11294047b371SPeter Hurley 	struct uart_port *uport;
1130ab4382d2SGreg Kroah-Hartman 	int flags, ret;
1131ab4382d2SGreg Kroah-Hartman 
1132ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
1133ab4382d2SGreg Kroah-Hartman 		return -EPERM;
1134ab4382d2SGreg Kroah-Hartman 
1135ab4382d2SGreg Kroah-Hartman 	/*
1136ab4382d2SGreg Kroah-Hartman 	 * Take the per-port semaphore.  This prevents count from
1137ab4382d2SGreg Kroah-Hartman 	 * changing, and hence any extra opens of the port while
1138ab4382d2SGreg Kroah-Hartman 	 * we're auto-configuring.
1139ab4382d2SGreg Kroah-Hartman 	 */
1140ab4382d2SGreg Kroah-Hartman 	if (mutex_lock_interruptible(&port->mutex))
1141ab4382d2SGreg Kroah-Hartman 		return -ERESTARTSYS;
1142ab4382d2SGreg Kroah-Hartman 
11434047b371SPeter Hurley 	uport = uart_port_check(state);
11444047b371SPeter Hurley 	if (!uport) {
11454047b371SPeter Hurley 		ret = -EIO;
11464047b371SPeter Hurley 		goto out;
11474047b371SPeter Hurley 	}
11484047b371SPeter Hurley 
1149ab4382d2SGreg Kroah-Hartman 	ret = -EBUSY;
1150ab4382d2SGreg Kroah-Hartman 	if (tty_port_users(port) == 1) {
1151ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1152ab4382d2SGreg Kroah-Hartman 
1153ab4382d2SGreg Kroah-Hartman 		/*
1154ab4382d2SGreg Kroah-Hartman 		 * If we already have a port type configured,
1155ab4382d2SGreg Kroah-Hartman 		 * we must release its resources.
1156ab4382d2SGreg Kroah-Hartman 		 */
1157a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1158ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
1159ab4382d2SGreg Kroah-Hartman 
1160ab4382d2SGreg Kroah-Hartman 		flags = UART_CONFIG_TYPE;
1161ab4382d2SGreg Kroah-Hartman 		if (uport->flags & UPF_AUTO_IRQ)
1162ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_IRQ;
1163ab4382d2SGreg Kroah-Hartman 
1164ab4382d2SGreg Kroah-Hartman 		/*
1165ab4382d2SGreg Kroah-Hartman 		 * This will claim the ports resources if
1166ab4382d2SGreg Kroah-Hartman 		 * a port is found.
1167ab4382d2SGreg Kroah-Hartman 		 */
1168ab4382d2SGreg Kroah-Hartman 		uport->ops->config_port(uport, flags);
1169ab4382d2SGreg Kroah-Hartman 
1170ab4382d2SGreg Kroah-Hartman 		ret = uart_startup(tty, state, 1);
117171456906SSebastian Andrzej Siewior 		if (ret == 0)
117271456906SSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1173b3b57646SRob Herring 		if (ret > 0)
1174b3b57646SRob Herring 			ret = 0;
1175ab4382d2SGreg Kroah-Hartman 	}
11764047b371SPeter Hurley out:
1177ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1178ab4382d2SGreg Kroah-Hartman 	return ret;
1179ab4382d2SGreg Kroah-Hartman }
1180ab4382d2SGreg Kroah-Hartman 
11811fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport)
11821fdc3106SAlexander Shiyan {
11831fdc3106SAlexander Shiyan 	/*
11841fdc3106SAlexander Shiyan 	 * Force modem status interrupts on
11851fdc3106SAlexander Shiyan 	 */
11861fdc3106SAlexander Shiyan 	if (uport->ops->enable_ms)
11871fdc3106SAlexander Shiyan 		uport->ops->enable_ms(uport);
11881fdc3106SAlexander Shiyan }
11891fdc3106SAlexander Shiyan 
1190ab4382d2SGreg Kroah-Hartman /*
1191ab4382d2SGreg Kroah-Hartman  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1192ab4382d2SGreg Kroah-Hartman  * - mask passed in arg for lines of interest
1193ab4382d2SGreg Kroah-Hartman  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1194ab4382d2SGreg Kroah-Hartman  * Caller should use TIOCGICOUNT to see which one it was
1195ab4382d2SGreg Kroah-Hartman  *
1196ab4382d2SGreg Kroah-Hartman  * FIXME: This wants extracting into a common all driver implementation
1197ab4382d2SGreg Kroah-Hartman  * of TIOCMWAIT using tty_port.
1198ab4382d2SGreg Kroah-Hartman  */
11999ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1200ab4382d2SGreg Kroah-Hartman {
12019ed19428SPeter Hurley 	struct uart_port *uport;
1202ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1203ab4382d2SGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
1204ab4382d2SGreg Kroah-Hartman 	struct uart_icount cprev, cnow;
1205ab4382d2SGreg Kroah-Hartman 	int ret;
1206ab4382d2SGreg Kroah-Hartman 
1207ab4382d2SGreg Kroah-Hartman 	/*
1208ab4382d2SGreg Kroah-Hartman 	 * note the counters on entry
1209ab4382d2SGreg Kroah-Hartman 	 */
12109ed19428SPeter Hurley 	uport = uart_port_ref(state);
12119ed19428SPeter Hurley 	if (!uport)
12129ed19428SPeter Hurley 		return -EIO;
1213ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1214ab4382d2SGreg Kroah-Hartman 	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
12151fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1216ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
1217ab4382d2SGreg Kroah-Hartman 
1218ab4382d2SGreg Kroah-Hartman 	add_wait_queue(&port->delta_msr_wait, &wait);
1219ab4382d2SGreg Kroah-Hartman 	for (;;) {
1220ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1221ab4382d2SGreg Kroah-Hartman 		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1222ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
1223ab4382d2SGreg Kroah-Hartman 
1224ab4382d2SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
1225ab4382d2SGreg Kroah-Hartman 
1226ab4382d2SGreg Kroah-Hartman 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1227ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1228ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1229ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1230ab4382d2SGreg Kroah-Hartman 			ret = 0;
1231ab4382d2SGreg Kroah-Hartman 			break;
1232ab4382d2SGreg Kroah-Hartman 		}
1233ab4382d2SGreg Kroah-Hartman 
1234ab4382d2SGreg Kroah-Hartman 		schedule();
1235ab4382d2SGreg Kroah-Hartman 
1236ab4382d2SGreg Kroah-Hartman 		/* see if a signal did it */
1237ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current)) {
1238ab4382d2SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
1239ab4382d2SGreg Kroah-Hartman 			break;
1240ab4382d2SGreg Kroah-Hartman 		}
1241ab4382d2SGreg Kroah-Hartman 
1242ab4382d2SGreg Kroah-Hartman 		cprev = cnow;
1243ab4382d2SGreg Kroah-Hartman 	}
124497f9f707SFabian Frederick 	__set_current_state(TASK_RUNNING);
1245ab4382d2SGreg Kroah-Hartman 	remove_wait_queue(&port->delta_msr_wait, &wait);
12469ed19428SPeter Hurley 	uart_port_deref(uport);
1247ab4382d2SGreg Kroah-Hartman 
1248ab4382d2SGreg Kroah-Hartman 	return ret;
1249ab4382d2SGreg Kroah-Hartman }
1250ab4382d2SGreg Kroah-Hartman 
1251ab4382d2SGreg Kroah-Hartman /*
1252ab4382d2SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1253ab4382d2SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1254ab4382d2SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1255ab4382d2SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1256ab4382d2SGreg Kroah-Hartman  */
1257ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty,
1258ab4382d2SGreg Kroah-Hartman 			  struct serial_icounter_struct *icount)
1259ab4382d2SGreg Kroah-Hartman {
1260ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1261ab4382d2SGreg Kroah-Hartman 	struct uart_icount cnow;
12629ed19428SPeter Hurley 	struct uart_port *uport;
1263ab4382d2SGreg Kroah-Hartman 
12649ed19428SPeter Hurley 	uport = uart_port_ref(state);
12659ed19428SPeter Hurley 	if (!uport)
12669ed19428SPeter Hurley 		return -EIO;
1267ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
1268ab4382d2SGreg Kroah-Hartman 	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1269ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
12709ed19428SPeter Hurley 	uart_port_deref(uport);
1271ab4382d2SGreg Kroah-Hartman 
1272ab4382d2SGreg Kroah-Hartman 	icount->cts         = cnow.cts;
1273ab4382d2SGreg Kroah-Hartman 	icount->dsr         = cnow.dsr;
1274ab4382d2SGreg Kroah-Hartman 	icount->rng         = cnow.rng;
1275ab4382d2SGreg Kroah-Hartman 	icount->dcd         = cnow.dcd;
1276ab4382d2SGreg Kroah-Hartman 	icount->rx          = cnow.rx;
1277ab4382d2SGreg Kroah-Hartman 	icount->tx          = cnow.tx;
1278ab4382d2SGreg Kroah-Hartman 	icount->frame       = cnow.frame;
1279ab4382d2SGreg Kroah-Hartman 	icount->overrun     = cnow.overrun;
1280ab4382d2SGreg Kroah-Hartman 	icount->parity      = cnow.parity;
1281ab4382d2SGreg Kroah-Hartman 	icount->brk         = cnow.brk;
1282ab4382d2SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1283ab4382d2SGreg Kroah-Hartman 
1284ab4382d2SGreg Kroah-Hartman 	return 0;
1285ab4382d2SGreg Kroah-Hartman }
1286ab4382d2SGreg Kroah-Hartman 
1287a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port,
1288a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485)
1289a5f276f1SRicardo Ribalda Delgado {
1290bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1291bd737f87SRicardo Ribalda Delgado 	struct serial_rs485 aux;
1292bd737f87SRicardo Ribalda Delgado 
1293bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1294bd737f87SRicardo Ribalda Delgado 	aux = port->rs485;
1295bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1296bd737f87SRicardo Ribalda Delgado 
1297bd737f87SRicardo Ribalda Delgado 	if (copy_to_user(rs485, &aux, sizeof(aux)))
1298a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1299bd737f87SRicardo Ribalda Delgado 
1300a5f276f1SRicardo Ribalda Delgado 	return 0;
1301a5f276f1SRicardo Ribalda Delgado }
1302a5f276f1SRicardo Ribalda Delgado 
1303a5f276f1SRicardo Ribalda Delgado static int uart_set_rs485_config(struct uart_port *port,
1304a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485_user)
1305a5f276f1SRicardo Ribalda Delgado {
1306a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485 rs485;
1307a5f276f1SRicardo Ribalda Delgado 	int ret;
1308bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1309a5f276f1SRicardo Ribalda Delgado 
1310a5f276f1SRicardo Ribalda Delgado 	if (!port->rs485_config)
1311a5f276f1SRicardo Ribalda Delgado 		return -ENOIOCTLCMD;
1312a5f276f1SRicardo Ribalda Delgado 
1313a5f276f1SRicardo Ribalda Delgado 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1314a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1315a5f276f1SRicardo Ribalda Delgado 
1316bd737f87SRicardo Ribalda Delgado 	spin_lock_irqsave(&port->lock, flags);
1317a5f276f1SRicardo Ribalda Delgado 	ret = port->rs485_config(port, &rs485);
1318bd737f87SRicardo Ribalda Delgado 	spin_unlock_irqrestore(&port->lock, flags);
1319a5f276f1SRicardo Ribalda Delgado 	if (ret)
1320a5f276f1SRicardo Ribalda Delgado 		return ret;
1321a5f276f1SRicardo Ribalda Delgado 
1322a5f276f1SRicardo Ribalda Delgado 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1323a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1324a5f276f1SRicardo Ribalda Delgado 
1325a5f276f1SRicardo Ribalda Delgado 	return 0;
1326a5f276f1SRicardo Ribalda Delgado }
1327a5f276f1SRicardo Ribalda Delgado 
1328ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port,
1329ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816)
1330ad8c0eaaSNicolas Ferre {
1331ad8c0eaaSNicolas Ferre 	unsigned long flags;
1332ad8c0eaaSNicolas Ferre 	struct serial_iso7816 aux;
1333ad8c0eaaSNicolas Ferre 
1334ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
1335ad8c0eaaSNicolas Ferre 		return -ENOIOCTLCMD;
1336ad8c0eaaSNicolas Ferre 
1337ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1338ad8c0eaaSNicolas Ferre 	aux = port->iso7816;
1339ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1340ad8c0eaaSNicolas Ferre 
1341ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816, &aux, sizeof(aux)))
1342ad8c0eaaSNicolas Ferre 		return -EFAULT;
1343ad8c0eaaSNicolas Ferre 
1344ad8c0eaaSNicolas Ferre 	return 0;
1345ad8c0eaaSNicolas Ferre }
1346ad8c0eaaSNicolas Ferre 
1347ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port,
1348ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816_user)
1349ad8c0eaaSNicolas Ferre {
1350ad8c0eaaSNicolas Ferre 	struct serial_iso7816 iso7816;
1351ad8c0eaaSNicolas Ferre 	int i, ret;
1352ad8c0eaaSNicolas Ferre 	unsigned long flags;
1353ad8c0eaaSNicolas Ferre 
1354ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
1355ad8c0eaaSNicolas Ferre 		return -ENOIOCTLCMD;
1356ad8c0eaaSNicolas Ferre 
1357ad8c0eaaSNicolas Ferre 	if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1358ad8c0eaaSNicolas Ferre 		return -EFAULT;
1359ad8c0eaaSNicolas Ferre 
1360ad8c0eaaSNicolas Ferre 	/*
1361ad8c0eaaSNicolas Ferre 	 * There are 5 words reserved for future use. Check that userspace
1362ad8c0eaaSNicolas Ferre 	 * doesn't put stuff in there to prevent breakages in the future.
1363ad8c0eaaSNicolas Ferre 	 */
1364ad8c0eaaSNicolas Ferre 	for (i = 0; i < 5; i++)
1365ad8c0eaaSNicolas Ferre 		if (iso7816.reserved[i])
1366ad8c0eaaSNicolas Ferre 			return -EINVAL;
1367ad8c0eaaSNicolas Ferre 
1368ad8c0eaaSNicolas Ferre 	spin_lock_irqsave(&port->lock, flags);
1369ad8c0eaaSNicolas Ferre 	ret = port->iso7816_config(port, &iso7816);
1370ad8c0eaaSNicolas Ferre 	spin_unlock_irqrestore(&port->lock, flags);
1371ad8c0eaaSNicolas Ferre 	if (ret)
1372ad8c0eaaSNicolas Ferre 		return ret;
1373ad8c0eaaSNicolas Ferre 
1374ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1375ad8c0eaaSNicolas Ferre 		return -EFAULT;
1376ad8c0eaaSNicolas Ferre 
1377ad8c0eaaSNicolas Ferre 	return 0;
1378ad8c0eaaSNicolas Ferre }
1379ad8c0eaaSNicolas Ferre 
1380ab4382d2SGreg Kroah-Hartman /*
1381ab4382d2SGreg Kroah-Hartman  * Called via sys_ioctl.  We can use spin_lock_irq() here.
1382ab4382d2SGreg Kroah-Hartman  */
1383ab4382d2SGreg Kroah-Hartman static int
13844047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1385ab4382d2SGreg Kroah-Hartman {
1386ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1387ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
13884047b371SPeter Hurley 	struct uart_port *uport;
1389ab4382d2SGreg Kroah-Hartman 	void __user *uarg = (void __user *)arg;
1390ab4382d2SGreg Kroah-Hartman 	int ret = -ENOIOCTLCMD;
1391ab4382d2SGreg Kroah-Hartman 
1392ab4382d2SGreg Kroah-Hartman 
1393ab4382d2SGreg Kroah-Hartman 	/*
1394ab4382d2SGreg Kroah-Hartman 	 * These ioctls don't rely on the hardware to be present.
1395ab4382d2SGreg Kroah-Hartman 	 */
1396ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1397ab4382d2SGreg Kroah-Hartman 	case TIOCSERCONFIG:
13987c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
1399ab4382d2SGreg Kroah-Hartman 		ret = uart_do_autoconfig(tty, state);
14007c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1401ab4382d2SGreg Kroah-Hartman 		break;
1402ab4382d2SGreg Kroah-Hartman 	}
1403ab4382d2SGreg Kroah-Hartman 
1404ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1405ab4382d2SGreg Kroah-Hartman 		goto out;
1406ab4382d2SGreg Kroah-Hartman 
140718900ca6SPeter Hurley 	if (tty_io_error(tty)) {
1408ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1409ab4382d2SGreg Kroah-Hartman 		goto out;
1410ab4382d2SGreg Kroah-Hartman 	}
1411ab4382d2SGreg Kroah-Hartman 
1412ab4382d2SGreg Kroah-Hartman 	/*
1413ab4382d2SGreg Kroah-Hartman 	 * The following should only be used when hardware is present.
1414ab4382d2SGreg Kroah-Hartman 	 */
1415ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1416ab4382d2SGreg Kroah-Hartman 	case TIOCMIWAIT:
1417ab4382d2SGreg Kroah-Hartman 		ret = uart_wait_modem_status(state, arg);
1418ab4382d2SGreg Kroah-Hartman 		break;
1419ab4382d2SGreg Kroah-Hartman 	}
1420ab4382d2SGreg Kroah-Hartman 
1421ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1422ab4382d2SGreg Kroah-Hartman 		goto out;
1423ab4382d2SGreg Kroah-Hartman 
1424ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
14254047b371SPeter Hurley 	uport = uart_port_check(state);
1426ab4382d2SGreg Kroah-Hartman 
14274047b371SPeter Hurley 	if (!uport || tty_io_error(tty)) {
1428ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1429ab4382d2SGreg Kroah-Hartman 		goto out_up;
1430ab4382d2SGreg Kroah-Hartman 	}
1431ab4382d2SGreg Kroah-Hartman 
1432ab4382d2SGreg Kroah-Hartman 	/*
1433ab4382d2SGreg Kroah-Hartman 	 * All these rely on hardware being present and need to be
1434ab4382d2SGreg Kroah-Hartman 	 * protected against the tty being hung up.
1435ab4382d2SGreg Kroah-Hartman 	 */
1436a9c20a9cSRicardo Ribalda Delgado 
1437ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1438a9c20a9cSRicardo Ribalda Delgado 	case TIOCSERGETLSR: /* Get line status register */
1439a9c20a9cSRicardo Ribalda Delgado 		ret = uart_get_lsr_info(tty, state, uarg);
1440a9c20a9cSRicardo Ribalda Delgado 		break;
1441a9c20a9cSRicardo Ribalda Delgado 
1442a5f276f1SRicardo Ribalda Delgado 	case TIOCGRS485:
14434047b371SPeter Hurley 		ret = uart_get_rs485_config(uport, uarg);
1444a5f276f1SRicardo Ribalda Delgado 		break;
1445a5f276f1SRicardo Ribalda Delgado 
1446a5f276f1SRicardo Ribalda Delgado 	case TIOCSRS485:
14474047b371SPeter Hurley 		ret = uart_set_rs485_config(uport, uarg);
1448a5f276f1SRicardo Ribalda Delgado 		break;
1449ad8c0eaaSNicolas Ferre 
1450ad8c0eaaSNicolas Ferre 	case TIOCSISO7816:
1451ad8c0eaaSNicolas Ferre 		ret = uart_set_iso7816_config(state->uart_port, uarg);
1452ad8c0eaaSNicolas Ferre 		break;
1453ad8c0eaaSNicolas Ferre 
1454ad8c0eaaSNicolas Ferre 	case TIOCGISO7816:
1455ad8c0eaaSNicolas Ferre 		ret = uart_get_iso7816_config(state->uart_port, uarg);
1456ad8c0eaaSNicolas Ferre 		break;
14574047b371SPeter Hurley 	default:
1458ab4382d2SGreg Kroah-Hartman 		if (uport->ops->ioctl)
1459ab4382d2SGreg Kroah-Hartman 			ret = uport->ops->ioctl(uport, cmd, arg);
1460ab4382d2SGreg Kroah-Hartman 		break;
1461ab4382d2SGreg Kroah-Hartman 	}
1462ab4382d2SGreg Kroah-Hartman out_up:
1463ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1464ab4382d2SGreg Kroah-Hartman out:
1465ab4382d2SGreg Kroah-Hartman 	return ret;
1466ab4382d2SGreg Kroah-Hartman }
1467ab4382d2SGreg Kroah-Hartman 
1468ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty)
1469ab4382d2SGreg Kroah-Hartman {
1470ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
14714047b371SPeter Hurley 	struct uart_port *uport;
1472ab4382d2SGreg Kroah-Hartman 
1473db1b9dfcSPeter Hurley 	mutex_lock(&state->port.mutex);
14744047b371SPeter Hurley 	uport = uart_port_check(state);
14754047b371SPeter Hurley 	if (uport && uport->ops->set_ldisc)
1476732a84a0SPeter Hurley 		uport->ops->set_ldisc(uport, &tty->termios);
1477db1b9dfcSPeter Hurley 	mutex_unlock(&state->port.mutex);
1478db1b9dfcSPeter Hurley }
1479ab4382d2SGreg Kroah-Hartman 
1480ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty,
1481ab4382d2SGreg Kroah-Hartman 						struct ktermios *old_termios)
1482ab4382d2SGreg Kroah-Hartman {
1483ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
14844047b371SPeter Hurley 	struct uart_port *uport;
1485adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
14862cbacafdSRussell King 	unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
14872cbacafdSRussell King 	bool sw_changed = false;
1488ab4382d2SGreg Kroah-Hartman 
14894047b371SPeter Hurley 	mutex_lock(&state->port.mutex);
14904047b371SPeter Hurley 	uport = uart_port_check(state);
14914047b371SPeter Hurley 	if (!uport)
14924047b371SPeter Hurley 		goto out;
14934047b371SPeter Hurley 
14942cbacafdSRussell King 	/*
14952cbacafdSRussell King 	 * Drivers doing software flow control also need to know
14962cbacafdSRussell King 	 * about changes to these input settings.
14972cbacafdSRussell King 	 */
14982cbacafdSRussell King 	if (uport->flags & UPF_SOFT_FLOW) {
14992cbacafdSRussell King 		iflag_mask |= IXANY|IXON|IXOFF;
15002cbacafdSRussell King 		sw_changed =
15012cbacafdSRussell King 		   tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
15022cbacafdSRussell King 		   tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
15032cbacafdSRussell King 	}
1504ab4382d2SGreg Kroah-Hartman 
1505ab4382d2SGreg Kroah-Hartman 	/*
1506ab4382d2SGreg Kroah-Hartman 	 * These are the bits that are used to setup various
1507ab4382d2SGreg Kroah-Hartman 	 * flags in the low level driver. We can ignore the Bfoo
1508ab4382d2SGreg Kroah-Hartman 	 * bits in c_cflag; c_[io]speed will always be set
1509ab4382d2SGreg Kroah-Hartman 	 * appropriately by set_termios() in tty_ioctl.c
1510ab4382d2SGreg Kroah-Hartman 	 */
1511ab4382d2SGreg Kroah-Hartman 	if ((cflag ^ old_termios->c_cflag) == 0 &&
1512adc8d746SAlan Cox 	    tty->termios.c_ospeed == old_termios->c_ospeed &&
1513adc8d746SAlan Cox 	    tty->termios.c_ispeed == old_termios->c_ispeed &&
15142cbacafdSRussell King 	    ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
15152cbacafdSRussell King 	    !sw_changed) {
15164047b371SPeter Hurley 		goto out;
1517ab4382d2SGreg Kroah-Hartman 	}
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 	uart_change_speed(tty, state, old_termios);
1520c7a6b9e4SHariprasad Kelam 	/* reload cflag from termios; port driver may have overridden flags */
1521c18b55fdSPeter Hurley 	cflag = tty->termios.c_cflag;
1522ab4382d2SGreg Kroah-Hartman 
1523ab4382d2SGreg Kroah-Hartman 	/* Handle transition to B0 status */
1524ab4382d2SGreg Kroah-Hartman 	if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1525dec94e70SRussell King 		uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1526ab4382d2SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
1527ab4382d2SGreg Kroah-Hartman 	else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1528ab4382d2SGreg Kroah-Hartman 		unsigned int mask = TIOCM_DTR;
152997ef38b8SPeter Hurley 		if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1530ab4382d2SGreg Kroah-Hartman 			mask |= TIOCM_RTS;
1531dec94e70SRussell King 		uart_set_mctrl(uport, mask);
1532ab4382d2SGreg Kroah-Hartman 	}
15334047b371SPeter Hurley out:
15344047b371SPeter Hurley 	mutex_unlock(&state->port.mutex);
1535ab4382d2SGreg Kroah-Hartman }
1536ab4382d2SGreg Kroah-Hartman 
1537ab4382d2SGreg Kroah-Hartman /*
1538ef4f527cSKevin Cernekee  * Calls to uart_close() are serialised via the tty_lock in
1539ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_release()
1540ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1541ab4382d2SGreg Kroah-Hartman  */
1542ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp)
1543ab4382d2SGreg Kroah-Hartman {
1544ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1545ab4382d2SGreg Kroah-Hartman 
154691b32f54SPeter Hurley 	if (!state) {
154791b32f54SPeter Hurley 		struct uart_driver *drv = tty->driver->driver_state;
15489356335fSColin Ian King 		struct tty_port *port;
154991b32f54SPeter Hurley 
155091b32f54SPeter Hurley 		state = drv->state + tty->index;
155191b32f54SPeter Hurley 		port = &state->port;
155291b32f54SPeter Hurley 		spin_lock_irq(&port->lock);
155391b32f54SPeter Hurley 		--port->count;
155491b32f54SPeter Hurley 		spin_unlock_irq(&port->lock);
1555ab4382d2SGreg Kroah-Hartman 		return;
155691b32f54SPeter Hurley 	}
1557ab4382d2SGreg Kroah-Hartman 
155839b3d892SPeter Hurley 	pr_debug("uart_close(%d) called\n", tty->index);
1559ab4382d2SGreg Kroah-Hartman 
1560761ed4a9SRob Herring 	tty_port_close(tty->port, tty, filp);
1561761ed4a9SRob Herring }
1562ab4382d2SGreg Kroah-Hartman 
1563761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port)
1564761ed4a9SRob Herring {
1565761ed4a9SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1566761ed4a9SRob Herring 	struct uart_port *uport = uart_port_check(state);
1567af224ca2SPeter Hurley 
1568ab4382d2SGreg Kroah-Hartman 	/*
1569ab4382d2SGreg Kroah-Hartman 	 * At this point, we stop accepting input.  To do this, we
1570ab4382d2SGreg Kroah-Hartman 	 * disable the receive line status interrupts.
1571ab4382d2SGreg Kroah-Hartman 	 */
1572a5a2b130SAndy Shevchenko 	if (WARN(!uport, "detached port still initialized!\n"))
1573a5a2b130SAndy Shevchenko 		return;
1574761ed4a9SRob Herring 
1575a5a2b130SAndy Shevchenko 	spin_lock_irq(&uport->lock);
1576ab4382d2SGreg Kroah-Hartman 	uport->ops->stop_rx(uport);
15776fb98fb3SPeter Hurley 	spin_unlock_irq(&uport->lock);
1578761ed4a9SRob Herring 
1579761ed4a9SRob Herring 	uart_port_shutdown(port);
1580761ed4a9SRob Herring 
1581ab4382d2SGreg Kroah-Hartman 	/*
1582761ed4a9SRob Herring 	 * It's possible for shutdown to be called after suspend if we get
1583761ed4a9SRob Herring 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
1584761ed4a9SRob Herring 	 * we don't try to resume a port that has been shutdown.
1585ab4382d2SGreg Kroah-Hartman 	 */
1586761ed4a9SRob Herring 	tty_port_set_suspended(port, 0);
1587ab4382d2SGreg Kroah-Hartman 
15886f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
1589ab4382d2SGreg Kroah-Hartman 
1590ab4382d2SGreg Kroah-Hartman }
1591ab4382d2SGreg Kroah-Hartman 
15921f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1593ab4382d2SGreg Kroah-Hartman {
15941f33a51dSJiri Slaby 	struct uart_state *state = tty->driver_data;
15959ed19428SPeter Hurley 	struct uart_port *port;
1596ab4382d2SGreg Kroah-Hartman 	unsigned long char_time, expire;
1597ab4382d2SGreg Kroah-Hartman 
15989ed19428SPeter Hurley 	port = uart_port_ref(state);
1599ef510beaSAndy Shevchenko 	if (!port)
1600ef510beaSAndy Shevchenko 		return;
1601ef510beaSAndy Shevchenko 
1602ef510beaSAndy Shevchenko 	if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
16039ed19428SPeter Hurley 		uart_port_deref(port);
1604ab4382d2SGreg Kroah-Hartman 		return;
16059ed19428SPeter Hurley 	}
1606ab4382d2SGreg Kroah-Hartman 
1607ab4382d2SGreg Kroah-Hartman 	/*
1608ab4382d2SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1609ab4382d2SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1610ab4382d2SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1611ab4382d2SGreg Kroah-Hartman 	 *
1612ab4382d2SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1613ab4382d2SGreg Kroah-Hartman 	 * the NIST-PCTS.
1614ab4382d2SGreg Kroah-Hartman 	 */
1615ab4382d2SGreg Kroah-Hartman 	char_time = (port->timeout - HZ/50) / port->fifosize;
1616ab4382d2SGreg Kroah-Hartman 	char_time = char_time / 5;
1617ab4382d2SGreg Kroah-Hartman 	if (char_time == 0)
1618ab4382d2SGreg Kroah-Hartman 		char_time = 1;
1619ab4382d2SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1620ab4382d2SGreg Kroah-Hartman 		char_time = timeout;
1621ab4382d2SGreg Kroah-Hartman 
1622ab4382d2SGreg Kroah-Hartman 	/*
1623ab4382d2SGreg Kroah-Hartman 	 * If the transmitter hasn't cleared in twice the approximate
1624ab4382d2SGreg Kroah-Hartman 	 * amount of time to send the entire FIFO, it probably won't
1625ab4382d2SGreg Kroah-Hartman 	 * ever clear.  This assumes the UART isn't doing flow
1626ab4382d2SGreg Kroah-Hartman 	 * control, which is currently the case.  Hence, if it ever
1627ab4382d2SGreg Kroah-Hartman 	 * takes longer than port->timeout, this is probably due to a
1628ab4382d2SGreg Kroah-Hartman 	 * UART bug of some kind.  So, we clamp the timeout parameter at
1629ab4382d2SGreg Kroah-Hartman 	 * 2*port->timeout.
1630ab4382d2SGreg Kroah-Hartman 	 */
1631ab4382d2SGreg Kroah-Hartman 	if (timeout == 0 || timeout > 2 * port->timeout)
1632ab4382d2SGreg Kroah-Hartman 		timeout = 2 * port->timeout;
1633ab4382d2SGreg Kroah-Hartman 
1634ab4382d2SGreg Kroah-Hartman 	expire = jiffies + timeout;
1635ab4382d2SGreg Kroah-Hartman 
1636ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1637ab4382d2SGreg Kroah-Hartman 		port->line, jiffies, expire);
1638ab4382d2SGreg Kroah-Hartman 
1639ab4382d2SGreg Kroah-Hartman 	/*
1640ab4382d2SGreg Kroah-Hartman 	 * Check whether the transmitter is empty every 'char_time'.
1641ab4382d2SGreg Kroah-Hartman 	 * 'timeout' / 'expire' give us the maximum amount of time
1642ab4382d2SGreg Kroah-Hartman 	 * we wait.
1643ab4382d2SGreg Kroah-Hartman 	 */
1644ab4382d2SGreg Kroah-Hartman 	while (!port->ops->tx_empty(port)) {
1645ab4382d2SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1646ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current))
1647ab4382d2SGreg Kroah-Hartman 			break;
1648ab4382d2SGreg Kroah-Hartman 		if (time_after(jiffies, expire))
1649ab4382d2SGreg Kroah-Hartman 			break;
1650ab4382d2SGreg Kroah-Hartman 	}
16519ed19428SPeter Hurley 	uart_port_deref(port);
1652ab4382d2SGreg Kroah-Hartman }
1653ab4382d2SGreg Kroah-Hartman 
1654ab4382d2SGreg Kroah-Hartman /*
1655ef4f527cSKevin Cernekee  * Calls to uart_hangup() are serialised by the tty_lock in
1656ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1657ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1658ab4382d2SGreg Kroah-Hartman  */
1659ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty)
1660ab4382d2SGreg Kroah-Hartman {
1661ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1662ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1663af224ca2SPeter Hurley 	struct uart_port *uport;
1664ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1665ab4382d2SGreg Kroah-Hartman 
166639b3d892SPeter Hurley 	pr_debug("uart_hangup(%d)\n", tty->index);
1667ab4382d2SGreg Kroah-Hartman 
1668ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1669af224ca2SPeter Hurley 	uport = uart_port_check(state);
1670af224ca2SPeter Hurley 	WARN(!uport, "hangup of detached port!\n");
1671af224ca2SPeter Hurley 
1672807c8d81SPeter Hurley 	if (tty_port_active(port)) {
1673ab4382d2SGreg Kroah-Hartman 		uart_flush_buffer(tty);
1674ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1675ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
1676ab4382d2SGreg Kroah-Hartman 		port->count = 0;
1677ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
1678807c8d81SPeter Hurley 		tty_port_set_active(port, 0);
1679ab4382d2SGreg Kroah-Hartman 		tty_port_tty_set(port, NULL);
1680af224ca2SPeter Hurley 		if (uport && !uart_console(uport))
1681bf903c0cSGeert Uytterhoeven 			uart_change_pm(state, UART_PM_STATE_OFF);
1682ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
1683ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->delta_msr_wait);
1684ab4382d2SGreg Kroah-Hartman 	}
1685ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1686ab4382d2SGreg Kroah-Hartman }
1687ab4382d2SGreg Kroah-Hartman 
1688af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */
16890b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port)
16900b1db830SJiri Slaby {
1691b922e19dSJiri Slaby 	struct uart_state *state = container_of(port, struct uart_state, port);
16924047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1693b922e19dSJiri Slaby 
1694b922e19dSJiri Slaby 	/*
1695b922e19dSJiri Slaby 	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1696b922e19dSJiri Slaby 	 * the irq here so the queue might never be woken up.  Note
1697b922e19dSJiri Slaby 	 * that we won't end up waiting on delta_msr_wait again since
1698b922e19dSJiri Slaby 	 * any outstanding file descriptors should be pointing at
1699b922e19dSJiri Slaby 	 * hung_up_tty_fops now.
1700b922e19dSJiri Slaby 	 */
1701b922e19dSJiri Slaby 	wake_up_interruptible(&port->delta_msr_wait);
1702b922e19dSJiri Slaby 
1703b922e19dSJiri Slaby 	/*
1704b922e19dSJiri Slaby 	 * Free the IRQ and disable the port.
1705b922e19dSJiri Slaby 	 */
1706af224ca2SPeter Hurley 	if (uport)
1707b922e19dSJiri Slaby 		uport->ops->shutdown(uport);
1708b922e19dSJiri Slaby 
1709b922e19dSJiri Slaby 	/*
1710b922e19dSJiri Slaby 	 * Ensure that the IRQ handler isn't running on another CPU.
1711b922e19dSJiri Slaby 	 */
1712af224ca2SPeter Hurley 	if (uport)
1713b922e19dSJiri Slaby 		synchronize_irq(uport->irq);
17140b1db830SJiri Slaby }
17150b1db830SJiri Slaby 
1716ab4382d2SGreg Kroah-Hartman static int uart_carrier_raised(struct tty_port *port)
1717ab4382d2SGreg Kroah-Hartman {
1718ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
17199ed19428SPeter Hurley 	struct uart_port *uport;
1720ab4382d2SGreg Kroah-Hartman 	int mctrl;
17219ed19428SPeter Hurley 
17229ed19428SPeter Hurley 	uport = uart_port_ref(state);
17239ed19428SPeter Hurley 	/*
17249ed19428SPeter Hurley 	 * Should never observe uport == NULL since checks for hangup should
17259ed19428SPeter Hurley 	 * abort the tty_port_block_til_ready() loop before checking for carrier
17269ed19428SPeter Hurley 	 * raised -- but report carrier raised if it does anyway so open will
17279ed19428SPeter Hurley 	 * continue and not sleep
17289ed19428SPeter Hurley 	 */
17299ed19428SPeter Hurley 	if (WARN_ON(!uport))
17309ed19428SPeter Hurley 		return 1;
1731ab4382d2SGreg Kroah-Hartman 	spin_lock_irq(&uport->lock);
17321fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1733ab4382d2SGreg Kroah-Hartman 	mctrl = uport->ops->get_mctrl(uport);
1734ab4382d2SGreg Kroah-Hartman 	spin_unlock_irq(&uport->lock);
17359ed19428SPeter Hurley 	uart_port_deref(uport);
1736ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_CAR)
1737ab4382d2SGreg Kroah-Hartman 		return 1;
1738ab4382d2SGreg Kroah-Hartman 	return 0;
1739ab4382d2SGreg Kroah-Hartman }
1740ab4382d2SGreg Kroah-Hartman 
1741a6845e1eSRafael Gago static void uart_dtr_rts(struct tty_port *port, int raise)
1742ab4382d2SGreg Kroah-Hartman {
1743ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
17449ed19428SPeter Hurley 	struct uart_port *uport;
17459ed19428SPeter Hurley 
17469ed19428SPeter Hurley 	uport = uart_port_ref(state);
17479ed19428SPeter Hurley 	if (!uport)
17489ed19428SPeter Hurley 		return;
1749a6845e1eSRafael Gago 	uart_port_dtr_rts(uport, raise);
17509ed19428SPeter Hurley 	uart_port_deref(uport);
1751ab4382d2SGreg Kroah-Hartman }
1752ab4382d2SGreg Kroah-Hartman 
17534cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
17544cdd17baSJiri Slaby {
17554cdd17baSJiri Slaby 	struct uart_driver *drv = driver->driver_state;
17564cdd17baSJiri Slaby 	struct uart_state *state = drv->state + tty->index;
17574cdd17baSJiri Slaby 
17584cdd17baSJiri Slaby 	tty->driver_data = state;
17594cdd17baSJiri Slaby 
17604cdd17baSJiri Slaby 	return tty_standard_install(driver, tty);
17614cdd17baSJiri Slaby }
17624cdd17baSJiri Slaby 
1763ab4382d2SGreg Kroah-Hartman /*
1764ef4f527cSKevin Cernekee  * Calls to uart_open are serialised by the tty_lock in
1765ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_open()
1766ab4382d2SGreg Kroah-Hartman  * Note that if this fails, then uart_close() _will_ be called.
1767ab4382d2SGreg Kroah-Hartman  *
1768ab4382d2SGreg Kroah-Hartman  * In time, we want to scrap the "opening nonpresent ports"
1769ab4382d2SGreg Kroah-Hartman  * behaviour and implement an alternative way for setserial
1770ab4382d2SGreg Kroah-Hartman  * to set base addresses/ports/types.  This will allow us to
1771ab4382d2SGreg Kroah-Hartman  * get rid of a certain amount of extra tests.
1772ab4382d2SGreg Kroah-Hartman  */
1773ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp)
1774ab4382d2SGreg Kroah-Hartman {
17754cdd17baSJiri Slaby 	struct uart_state *state = tty->driver_data;
17764cdd17baSJiri Slaby 	int retval;
1777b3b57646SRob Herring 
1778b3b57646SRob Herring 	retval = tty_port_open(&state->port, tty, filp);
1779b3b57646SRob Herring 	if (retval > 0)
1780b3b57646SRob Herring 		retval = 0;
1781b3b57646SRob Herring 
1782b3b57646SRob Herring 	return retval;
1783b3b57646SRob Herring }
1784b3b57646SRob Herring 
1785b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1786b3b57646SRob Herring {
1787b3b57646SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1788b3b57646SRob Herring 	struct uart_port *uport;
178913b18d35SSerge Semin 	int ret;
1790b3b57646SRob Herring 
1791b3b57646SRob Herring 	uport = uart_port_check(state);
1792b3b57646SRob Herring 	if (!uport || uport->flags & UPF_DEAD)
1793b3b57646SRob Herring 		return -ENXIO;
1794b3b57646SRob Herring 
17954047b371SPeter Hurley 	port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1796ab4382d2SGreg Kroah-Hartman 
1797ab4382d2SGreg Kroah-Hartman 	/*
1798ab4382d2SGreg Kroah-Hartman 	 * Start up the serial port.
1799ab4382d2SGreg Kroah-Hartman 	 */
180013b18d35SSerge Semin 	ret = uart_startup(tty, state, 0);
180113b18d35SSerge Semin 	if (ret > 0)
180213b18d35SSerge Semin 		tty_port_set_active(port, 1);
180313b18d35SSerge Semin 
180413b18d35SSerge Semin 	return ret;
1805ab4382d2SGreg Kroah-Hartman }
1806ab4382d2SGreg Kroah-Hartman 
1807ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port)
1808ab4382d2SGreg Kroah-Hartman {
1809ab4382d2SGreg Kroah-Hartman 	const char *str = NULL;
1810ab4382d2SGreg Kroah-Hartman 
1811ab4382d2SGreg Kroah-Hartman 	if (port->ops->type)
1812ab4382d2SGreg Kroah-Hartman 		str = port->ops->type(port);
1813ab4382d2SGreg Kroah-Hartman 
1814ab4382d2SGreg Kroah-Hartman 	if (!str)
1815ab4382d2SGreg Kroah-Hartman 		str = "unknown";
1816ab4382d2SGreg Kroah-Hartman 
1817ab4382d2SGreg Kroah-Hartman 	return str;
1818ab4382d2SGreg Kroah-Hartman }
1819ab4382d2SGreg Kroah-Hartman 
1820ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
1821ab4382d2SGreg Kroah-Hartman 
1822ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1823ab4382d2SGreg Kroah-Hartman {
1824ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + i;
1825ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
18266f538fe3SLinus Walleij 	enum uart_pm_state pm_state;
18274047b371SPeter Hurley 	struct uart_port *uport;
1828ab4382d2SGreg Kroah-Hartman 	char stat_buf[32];
1829ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1830ab4382d2SGreg Kroah-Hartman 	int mmio;
1831ab4382d2SGreg Kroah-Hartman 
18324047b371SPeter Hurley 	mutex_lock(&port->mutex);
18334047b371SPeter Hurley 	uport = uart_port_check(state);
1834ab4382d2SGreg Kroah-Hartman 	if (!uport)
18354047b371SPeter Hurley 		goto out;
1836ab4382d2SGreg Kroah-Hartman 
1837ab4382d2SGreg Kroah-Hartman 	mmio = uport->iotype >= UPIO_MEM;
1838ab4382d2SGreg Kroah-Hartman 	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1839ab4382d2SGreg Kroah-Hartman 			uport->line, uart_type(uport),
1840ab4382d2SGreg Kroah-Hartman 			mmio ? "mmio:0x" : "port:",
1841ab4382d2SGreg Kroah-Hartman 			mmio ? (unsigned long long)uport->mapbase
1842ab4382d2SGreg Kroah-Hartman 			     : (unsigned long long)uport->iobase,
1843ab4382d2SGreg Kroah-Hartman 			uport->irq);
1844ab4382d2SGreg Kroah-Hartman 
1845ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN) {
1846ab4382d2SGreg Kroah-Hartman 		seq_putc(m, '\n');
18474047b371SPeter Hurley 		goto out;
1848ab4382d2SGreg Kroah-Hartman 	}
1849ab4382d2SGreg Kroah-Hartman 
1850ab4382d2SGreg Kroah-Hartman 	if (capable(CAP_SYS_ADMIN)) {
1851ab4382d2SGreg Kroah-Hartman 		pm_state = state->pm_state;
18526f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
18536f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
1854ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
1855ab4382d2SGreg Kroah-Hartman 		status = uport->ops->get_mctrl(uport);
1856ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
18576f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
1858ab4382d2SGreg Kroah-Hartman 			uart_change_pm(state, pm_state);
1859ab4382d2SGreg Kroah-Hartman 
1860ab4382d2SGreg Kroah-Hartman 		seq_printf(m, " tx:%d rx:%d",
1861ab4382d2SGreg Kroah-Hartman 				uport->icount.tx, uport->icount.rx);
1862ab4382d2SGreg Kroah-Hartman 		if (uport->icount.frame)
1863968af298SPeter Hurley 			seq_printf(m, " fe:%d",	uport->icount.frame);
1864ab4382d2SGreg Kroah-Hartman 		if (uport->icount.parity)
1865968af298SPeter Hurley 			seq_printf(m, " pe:%d",	uport->icount.parity);
1866ab4382d2SGreg Kroah-Hartman 		if (uport->icount.brk)
1867968af298SPeter Hurley 			seq_printf(m, " brk:%d", uport->icount.brk);
1868ab4382d2SGreg Kroah-Hartman 		if (uport->icount.overrun)
1869968af298SPeter Hurley 			seq_printf(m, " oe:%d", uport->icount.overrun);
18704f794097SJeremy Kerr 		if (uport->icount.buf_overrun)
18714f794097SJeremy Kerr 			seq_printf(m, " bo:%d", uport->icount.buf_overrun);
1872ab4382d2SGreg Kroah-Hartman 
1873ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \
1874ab4382d2SGreg Kroah-Hartman 	if (uport->mctrl & (bit)) \
1875ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1876ab4382d2SGreg Kroah-Hartman 			strlen(stat_buf) - 2)
1877ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \
1878ab4382d2SGreg Kroah-Hartman 	if (status & (bit)) \
1879ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
1880ab4382d2SGreg Kroah-Hartman 		       strlen(stat_buf) - 2)
1881ab4382d2SGreg Kroah-Hartman 
1882ab4382d2SGreg Kroah-Hartman 		stat_buf[0] = '\0';
1883ab4382d2SGreg Kroah-Hartman 		stat_buf[1] = '\0';
1884ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_RTS, "|RTS");
1885ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CTS, "|CTS");
1886ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_DTR, "|DTR");
1887ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_DSR, "|DSR");
1888ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CAR, "|CD");
1889ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_RNG, "|RI");
1890ab4382d2SGreg Kroah-Hartman 		if (stat_buf[0])
1891ab4382d2SGreg Kroah-Hartman 			stat_buf[0] = ' ';
1892ab4382d2SGreg Kroah-Hartman 
1893ab4382d2SGreg Kroah-Hartman 		seq_puts(m, stat_buf);
1894ab4382d2SGreg Kroah-Hartman 	}
1895ab4382d2SGreg Kroah-Hartman 	seq_putc(m, '\n');
1896ab4382d2SGreg Kroah-Hartman #undef STATBIT
1897ab4382d2SGreg Kroah-Hartman #undef INFOBIT
18984047b371SPeter Hurley out:
18994047b371SPeter Hurley 	mutex_unlock(&port->mutex);
1900ab4382d2SGreg Kroah-Hartman }
1901ab4382d2SGreg Kroah-Hartman 
1902ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v)
1903ab4382d2SGreg Kroah-Hartman {
1904ab4382d2SGreg Kroah-Hartman 	struct tty_driver *ttydrv = m->private;
1905ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = ttydrv->driver_state;
1906ab4382d2SGreg Kroah-Hartman 	int i;
1907ab4382d2SGreg Kroah-Hartman 
1908968af298SPeter Hurley 	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
1909ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++)
1910ab4382d2SGreg Kroah-Hartman 		uart_line_info(m, drv, i);
1911ab4382d2SGreg Kroah-Hartman 	return 0;
1912ab4382d2SGreg Kroah-Hartman }
1913ab4382d2SGreg Kroah-Hartman #endif
1914ab4382d2SGreg Kroah-Hartman 
19157a49955aSAndy Shevchenko static inline bool uart_console_enabled(struct uart_port *port)
19167a49955aSAndy Shevchenko {
19177a49955aSAndy Shevchenko 	return uart_console(port) && (port->cons->flags & CON_ENABLED);
19187a49955aSAndy Shevchenko }
19197a49955aSAndy Shevchenko 
1920d2403cadSAndy Shevchenko /*
1921d2403cadSAndy Shevchenko  * Ensure that the serial console lock is initialised early.
1922d2403cadSAndy Shevchenko  * If this port is a console, then the spinlock is already initialised.
1923d2403cadSAndy Shevchenko  */
1924d2403cadSAndy Shevchenko static inline void uart_port_spin_lock_init(struct uart_port *port)
1925d2403cadSAndy Shevchenko {
1926a3cb39d2SAndy Shevchenko 	if (uart_console(port))
1927d2403cadSAndy Shevchenko 		return;
1928d2403cadSAndy Shevchenko 
1929d2403cadSAndy Shevchenko 	spin_lock_init(&port->lock);
1930d2403cadSAndy Shevchenko 	lockdep_set_class(&port->lock, &port_lock_key);
1931d2403cadSAndy Shevchenko }
1932d2403cadSAndy Shevchenko 
1933ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
19341cfe42b7SPeter Hurley /**
1935ab4382d2SGreg Kroah-Hartman  *	uart_console_write - write a console message to a serial port
1936ab4382d2SGreg Kroah-Hartman  *	@port: the port to write the message
1937ab4382d2SGreg Kroah-Hartman  *	@s: array of characters
1938ab4382d2SGreg Kroah-Hartman  *	@count: number of characters in string to write
193910afbe34SPeter Hurley  *	@putchar: function to write character to port
1940ab4382d2SGreg Kroah-Hartman  */
1941ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s,
1942ab4382d2SGreg Kroah-Hartman 			unsigned int count,
1943ab4382d2SGreg Kroah-Hartman 			void (*putchar)(struct uart_port *, int))
1944ab4382d2SGreg Kroah-Hartman {
1945ab4382d2SGreg Kroah-Hartman 	unsigned int i;
1946ab4382d2SGreg Kroah-Hartman 
1947ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < count; i++, s++) {
1948ab4382d2SGreg Kroah-Hartman 		if (*s == '\n')
1949ab4382d2SGreg Kroah-Hartman 			putchar(port, '\r');
1950ab4382d2SGreg Kroah-Hartman 		putchar(port, *s);
1951ab4382d2SGreg Kroah-Hartman 	}
1952ab4382d2SGreg Kroah-Hartman }
1953ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write);
1954ab4382d2SGreg Kroah-Hartman 
1955ab4382d2SGreg Kroah-Hartman /*
1956ab4382d2SGreg Kroah-Hartman  *	Check whether an invalid uart number has been specified, and
1957ab4382d2SGreg Kroah-Hartman  *	if so, search for the first available port that does have
1958ab4382d2SGreg Kroah-Hartman  *	console support.
1959ab4382d2SGreg Kroah-Hartman  */
1960ab4382d2SGreg Kroah-Hartman struct uart_port * __init
1961ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co)
1962ab4382d2SGreg Kroah-Hartman {
1963ab4382d2SGreg Kroah-Hartman 	int idx = co->index;
1964ab4382d2SGreg Kroah-Hartman 
1965ab4382d2SGreg Kroah-Hartman 	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1966ab4382d2SGreg Kroah-Hartman 				     ports[idx].membase == NULL))
1967ab4382d2SGreg Kroah-Hartman 		for (idx = 0; idx < nr; idx++)
1968ab4382d2SGreg Kroah-Hartman 			if (ports[idx].iobase != 0 ||
1969ab4382d2SGreg Kroah-Hartman 			    ports[idx].membase != NULL)
1970ab4382d2SGreg Kroah-Hartman 				break;
1971ab4382d2SGreg Kroah-Hartman 
1972ab4382d2SGreg Kroah-Hartman 	co->index = idx;
1973ab4382d2SGreg Kroah-Hartman 
1974ab4382d2SGreg Kroah-Hartman 	return ports + idx;
1975ab4382d2SGreg Kroah-Hartman }
1976ab4382d2SGreg Kroah-Hartman 
1977ab4382d2SGreg Kroah-Hartman /**
197873abaf87SPeter Hurley  *	uart_parse_earlycon - Parse earlycon options
197973abaf87SPeter Hurley  *	@p:	  ptr to 2nd field (ie., just beyond '<name>,')
198073abaf87SPeter Hurley  *	@iotype:  ptr for decoded iotype (out)
198173abaf87SPeter Hurley  *	@addr:    ptr for decoded mapbase/iobase (out)
198273abaf87SPeter Hurley  *	@options: ptr for <options> field; NULL if not present (out)
198373abaf87SPeter Hurley  *
198473abaf87SPeter Hurley  *	Decodes earlycon kernel command line parameters of the form
1985bd94c407SMasahiro Yamada  *	   earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1986bd94c407SMasahiro Yamada  *	   console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
198773abaf87SPeter Hurley  *
198873abaf87SPeter Hurley  *	The optional form
1989ff30283aSRandy Dunlap  *
199073abaf87SPeter Hurley  *	   earlycon=<name>,0x<addr>,<options>
199173abaf87SPeter Hurley  *	   console=<name>,0x<addr>,<options>
1992ff30283aSRandy Dunlap  *
199373abaf87SPeter Hurley  *	is also accepted; the returned @iotype will be UPIO_MEM.
199473abaf87SPeter Hurley  *
19958b2303deSAlexander Sverdlin  *	Returns 0 on success or -EINVAL on failure
199673abaf87SPeter Hurley  */
199746e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
199873abaf87SPeter Hurley 			char **options)
199973abaf87SPeter Hurley {
200073abaf87SPeter Hurley 	if (strncmp(p, "mmio,", 5) == 0) {
200173abaf87SPeter Hurley 		*iotype = UPIO_MEM;
200273abaf87SPeter Hurley 		p += 5;
2003bd94c407SMasahiro Yamada 	} else if (strncmp(p, "mmio16,", 7) == 0) {
2004bd94c407SMasahiro Yamada 		*iotype = UPIO_MEM16;
2005bd94c407SMasahiro Yamada 		p += 7;
200673abaf87SPeter Hurley 	} else if (strncmp(p, "mmio32,", 7) == 0) {
200773abaf87SPeter Hurley 		*iotype = UPIO_MEM32;
200873abaf87SPeter Hurley 		p += 7;
20096e63be3fSNoam Camus 	} else if (strncmp(p, "mmio32be,", 9) == 0) {
20106e63be3fSNoam Camus 		*iotype = UPIO_MEM32BE;
20116e63be3fSNoam Camus 		p += 9;
2012d215d809SMax Filippov 	} else if (strncmp(p, "mmio32native,", 13) == 0) {
2013d215d809SMax Filippov 		*iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2014d215d809SMax Filippov 			UPIO_MEM32BE : UPIO_MEM32;
2015d215d809SMax Filippov 		p += 13;
201673abaf87SPeter Hurley 	} else if (strncmp(p, "io,", 3) == 0) {
201773abaf87SPeter Hurley 		*iotype = UPIO_PORT;
201873abaf87SPeter Hurley 		p += 3;
201973abaf87SPeter Hurley 	} else if (strncmp(p, "0x", 2) == 0) {
202073abaf87SPeter Hurley 		*iotype = UPIO_MEM;
202173abaf87SPeter Hurley 	} else {
202273abaf87SPeter Hurley 		return -EINVAL;
202373abaf87SPeter Hurley 	}
202473abaf87SPeter Hurley 
20258b2303deSAlexander Sverdlin 	/*
20268b2303deSAlexander Sverdlin 	 * Before you replace it with kstrtoull(), think about options separator
20278b2303deSAlexander Sverdlin 	 * (',') it will not tolerate
20288b2303deSAlexander Sverdlin 	 */
20298b2303deSAlexander Sverdlin 	*addr = simple_strtoull(p, NULL, 0);
203073abaf87SPeter Hurley 	p = strchr(p, ',');
203173abaf87SPeter Hurley 	if (p)
203273abaf87SPeter Hurley 		p++;
203373abaf87SPeter Hurley 
203473abaf87SPeter Hurley 	*options = p;
203573abaf87SPeter Hurley 	return 0;
203673abaf87SPeter Hurley }
203773abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon);
203873abaf87SPeter Hurley 
203973abaf87SPeter Hurley /**
204002088ca6SGeert Uytterhoeven  *	uart_parse_options - Parse serial port baud/parity/bits/flow control.
2041ab4382d2SGreg Kroah-Hartman  *	@options: pointer to option string
2042ab4382d2SGreg Kroah-Hartman  *	@baud: pointer to an 'int' variable for the baud rate.
2043ab4382d2SGreg Kroah-Hartman  *	@parity: pointer to an 'int' variable for the parity.
2044ab4382d2SGreg Kroah-Hartman  *	@bits: pointer to an 'int' variable for the number of data bits.
2045ab4382d2SGreg Kroah-Hartman  *	@flow: pointer to an 'int' variable for the flow control character.
2046ab4382d2SGreg Kroah-Hartman  *
2047ab4382d2SGreg Kroah-Hartman  *	uart_parse_options decodes a string containing the serial console
2048ab4382d2SGreg Kroah-Hartman  *	options.  The format of the string is <baud><parity><bits><flow>,
2049ab4382d2SGreg Kroah-Hartman  *	eg: 115200n8r
2050ab4382d2SGreg Kroah-Hartman  */
2051ab4382d2SGreg Kroah-Hartman void
20520f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity,
20530f646b63SPaul Cercueil 		   int *bits, int *flow)
2054ab4382d2SGreg Kroah-Hartman {
20550f646b63SPaul Cercueil 	const char *s = options;
2056ab4382d2SGreg Kroah-Hartman 
2057ab4382d2SGreg Kroah-Hartman 	*baud = simple_strtoul(s, NULL, 10);
2058ab4382d2SGreg Kroah-Hartman 	while (*s >= '0' && *s <= '9')
2059ab4382d2SGreg Kroah-Hartman 		s++;
2060ab4382d2SGreg Kroah-Hartman 	if (*s)
2061ab4382d2SGreg Kroah-Hartman 		*parity = *s++;
2062ab4382d2SGreg Kroah-Hartman 	if (*s)
2063ab4382d2SGreg Kroah-Hartman 		*bits = *s++ - '0';
2064ab4382d2SGreg Kroah-Hartman 	if (*s)
2065ab4382d2SGreg Kroah-Hartman 		*flow = *s;
2066ab4382d2SGreg Kroah-Hartman }
2067ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options);
2068ab4382d2SGreg Kroah-Hartman 
2069ab4382d2SGreg Kroah-Hartman /**
2070ab4382d2SGreg Kroah-Hartman  *	uart_set_options - setup the serial console parameters
2071ab4382d2SGreg Kroah-Hartman  *	@port: pointer to the serial ports uart_port structure
2072ab4382d2SGreg Kroah-Hartman  *	@co: console pointer
2073ab4382d2SGreg Kroah-Hartman  *	@baud: baud rate
2074ab4382d2SGreg Kroah-Hartman  *	@parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2075ab4382d2SGreg Kroah-Hartman  *	@bits: number of data bits
2076ab4382d2SGreg Kroah-Hartman  *	@flow: flow control character - 'r' (rts)
2077ab4382d2SGreg Kroah-Hartman  */
2078ab4382d2SGreg Kroah-Hartman int
2079ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co,
2080ab4382d2SGreg Kroah-Hartman 		 int baud, int parity, int bits, int flow)
2081ab4382d2SGreg Kroah-Hartman {
2082ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2083ab4382d2SGreg Kroah-Hartman 	static struct ktermios dummy;
2084ab4382d2SGreg Kroah-Hartman 
2085d2403cadSAndy Shevchenko 	uart_port_spin_lock_init(port);
2086ab4382d2SGreg Kroah-Hartman 
2087ab4382d2SGreg Kroah-Hartman 	memset(&termios, 0, sizeof(struct ktermios));
2088ab4382d2SGreg Kroah-Hartman 
2089ba47f97aSJeffy Chen 	termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2090ba47f97aSJeffy Chen 	tty_termios_encode_baud_rate(&termios, baud, baud);
2091ab4382d2SGreg Kroah-Hartman 
2092ab4382d2SGreg Kroah-Hartman 	if (bits == 7)
2093ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS7;
2094ab4382d2SGreg Kroah-Hartman 	else
2095ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS8;
2096ab4382d2SGreg Kroah-Hartman 
2097ab4382d2SGreg Kroah-Hartman 	switch (parity) {
2098ab4382d2SGreg Kroah-Hartman 	case 'o': case 'O':
2099ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARODD;
2100ab4382d2SGreg Kroah-Hartman 		/*fall through*/
2101ab4382d2SGreg Kroah-Hartman 	case 'e': case 'E':
2102ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARENB;
2103ab4382d2SGreg Kroah-Hartman 		break;
2104ab4382d2SGreg Kroah-Hartman 	}
2105ab4382d2SGreg Kroah-Hartman 
2106ab4382d2SGreg Kroah-Hartman 	if (flow == 'r')
2107ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CRTSCTS;
2108ab4382d2SGreg Kroah-Hartman 
2109ab4382d2SGreg Kroah-Hartman 	/*
2110ab4382d2SGreg Kroah-Hartman 	 * some uarts on other side don't support no flow control.
2111ab4382d2SGreg Kroah-Hartman 	 * So we set * DTR in host uart to make them happy
2112ab4382d2SGreg Kroah-Hartman 	 */
2113ab4382d2SGreg Kroah-Hartman 	port->mctrl |= TIOCM_DTR;
2114ab4382d2SGreg Kroah-Hartman 
2115ab4382d2SGreg Kroah-Hartman 	port->ops->set_termios(port, &termios, &dummy);
2116ab4382d2SGreg Kroah-Hartman 	/*
2117ab4382d2SGreg Kroah-Hartman 	 * Allow the setting of the UART parameters with a NULL console
2118ab4382d2SGreg Kroah-Hartman 	 * too:
2119ab4382d2SGreg Kroah-Hartman 	 */
2120ab4382d2SGreg Kroah-Hartman 	if (co)
2121ab4382d2SGreg Kroah-Hartman 		co->cflag = termios.c_cflag;
2122ab4382d2SGreg Kroah-Hartman 
2123ab4382d2SGreg Kroah-Hartman 	return 0;
2124ab4382d2SGreg Kroah-Hartman }
2125ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options);
2126ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2127ab4382d2SGreg Kroah-Hartman 
2128cf75525fSJiri Slaby /**
2129cf75525fSJiri Slaby  * uart_change_pm - set power state of the port
2130cf75525fSJiri Slaby  *
2131cf75525fSJiri Slaby  * @state: port descriptor
2132cf75525fSJiri Slaby  * @pm_state: new state
2133cf75525fSJiri Slaby  *
2134cf75525fSJiri Slaby  * Locking: port->mutex has to be held
2135cf75525fSJiri Slaby  */
21366f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
21376f538fe3SLinus Walleij 			   enum uart_pm_state pm_state)
2138ab4382d2SGreg Kroah-Hartman {
21394047b371SPeter Hurley 	struct uart_port *port = uart_port_check(state);
2140ab4382d2SGreg Kroah-Hartman 
2141ab4382d2SGreg Kroah-Hartman 	if (state->pm_state != pm_state) {
21424047b371SPeter Hurley 		if (port && port->ops->pm)
2143ab4382d2SGreg Kroah-Hartman 			port->ops->pm(port, pm_state, state->pm_state);
2144ab4382d2SGreg Kroah-Hartman 		state->pm_state = pm_state;
2145ab4382d2SGreg Kroah-Hartman 	}
2146ab4382d2SGreg Kroah-Hartman }
2147ab4382d2SGreg Kroah-Hartman 
2148ab4382d2SGreg Kroah-Hartman struct uart_match {
2149ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2150ab4382d2SGreg Kroah-Hartman 	struct uart_driver *driver;
2151ab4382d2SGreg Kroah-Hartman };
2152ab4382d2SGreg Kroah-Hartman 
2153ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data)
2154ab4382d2SGreg Kroah-Hartman {
2155ab4382d2SGreg Kroah-Hartman 	struct uart_match *match = data;
2156ab4382d2SGreg Kroah-Hartman 	struct tty_driver *tty_drv = match->driver->tty_driver;
2157ab4382d2SGreg Kroah-Hartman 	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2158ab4382d2SGreg Kroah-Hartman 		match->port->line;
2159ab4382d2SGreg Kroah-Hartman 
2160ab4382d2SGreg Kroah-Hartman 	return dev->devt == devt; /* Actually, only one tty per port */
2161ab4382d2SGreg Kroah-Hartman }
2162ab4382d2SGreg Kroah-Hartman 
2163ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2164ab4382d2SGreg Kroah-Hartman {
2165ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2166ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2167ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2168ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2169ab4382d2SGreg Kroah-Hartman 
2170ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2171ab4382d2SGreg Kroah-Hartman 
2172ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
217388e2582eSLucas Stach 	if (tty_dev && device_may_wakeup(tty_dev)) {
2174aef3ad10SAndy Shevchenko 		enable_irq_wake(uport->irq);
2175ab4382d2SGreg Kroah-Hartman 		put_device(tty_dev);
2176ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2177ab4382d2SGreg Kroah-Hartman 		return 0;
2178ab4382d2SGreg Kroah-Hartman 	}
21795a65dcc0SFederico Vaga 	put_device(tty_dev);
21805a65dcc0SFederico Vaga 
2181b164c972SPeter Hurley 	/* Nothing to do if the console is not suspending */
2182b164c972SPeter Hurley 	if (!console_suspend_enabled && uart_console(uport))
2183b164c972SPeter Hurley 		goto unlock;
2184b164c972SPeter Hurley 
2185ab4382d2SGreg Kroah-Hartman 	uport->suspended = 1;
2186ab4382d2SGreg Kroah-Hartman 
2187d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
2188ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2189ab4382d2SGreg Kroah-Hartman 		int tries;
2190ab4382d2SGreg Kroah-Hartman 
219180f02d54SPeter Hurley 		tty_port_set_suspended(port, 1);
2192d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
2193ab4382d2SGreg Kroah-Hartman 
2194ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2195ab4382d2SGreg Kroah-Hartman 		ops->stop_tx(uport);
2196ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
2197ab4382d2SGreg Kroah-Hartman 		ops->stop_rx(uport);
2198ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2199ab4382d2SGreg Kroah-Hartman 
2200ab4382d2SGreg Kroah-Hartman 		/*
2201ab4382d2SGreg Kroah-Hartman 		 * Wait for the transmitter to empty.
2202ab4382d2SGreg Kroah-Hartman 		 */
2203ab4382d2SGreg Kroah-Hartman 		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2204ab4382d2SGreg Kroah-Hartman 			msleep(10);
2205ab4382d2SGreg Kroah-Hartman 		if (!tries)
2206cade3580SAndy Shevchenko 			dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2207cade3580SAndy Shevchenko 				uport->name);
2208ab4382d2SGreg Kroah-Hartman 
2209ab4382d2SGreg Kroah-Hartman 		ops->shutdown(uport);
2210ab4382d2SGreg Kroah-Hartman 	}
2211ab4382d2SGreg Kroah-Hartman 
2212ab4382d2SGreg Kroah-Hartman 	/*
2213ab4382d2SGreg Kroah-Hartman 	 * Disable the console device before suspending.
2214ab4382d2SGreg Kroah-Hartman 	 */
2215b164c972SPeter Hurley 	if (uart_console(uport))
2216ab4382d2SGreg Kroah-Hartman 		console_stop(uport->cons);
2217ab4382d2SGreg Kroah-Hartman 
22186f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
2219b164c972SPeter Hurley unlock:
2220ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2221ab4382d2SGreg Kroah-Hartman 
2222ab4382d2SGreg Kroah-Hartman 	return 0;
2223ab4382d2SGreg Kroah-Hartman }
2224ab4382d2SGreg Kroah-Hartman 
2225ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2226ab4382d2SGreg Kroah-Hartman {
2227ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2228ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2229ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2230ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2231ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2232ab4382d2SGreg Kroah-Hartman 
2233ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2234ab4382d2SGreg Kroah-Hartman 
2235ab4382d2SGreg Kroah-Hartman 	tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2236ab4382d2SGreg Kroah-Hartman 	if (!uport->suspended && device_may_wakeup(tty_dev)) {
2237aef3ad10SAndy Shevchenko 		if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2238ab4382d2SGreg Kroah-Hartman 			disable_irq_wake(uport->irq);
22395a65dcc0SFederico Vaga 		put_device(tty_dev);
2240ab4382d2SGreg Kroah-Hartman 		mutex_unlock(&port->mutex);
2241ab4382d2SGreg Kroah-Hartman 		return 0;
2242ab4382d2SGreg Kroah-Hartman 	}
22435a65dcc0SFederico Vaga 	put_device(tty_dev);
2244ab4382d2SGreg Kroah-Hartman 	uport->suspended = 0;
2245ab4382d2SGreg Kroah-Hartman 
2246ab4382d2SGreg Kroah-Hartman 	/*
2247ab4382d2SGreg Kroah-Hartman 	 * Re-enable the console device after suspending.
2248ab4382d2SGreg Kroah-Hartman 	 */
22495933a161SYin Kangkai 	if (uart_console(uport)) {
2250ab4382d2SGreg Kroah-Hartman 		/*
2251ab4382d2SGreg Kroah-Hartman 		 * First try to use the console cflag setting.
2252ab4382d2SGreg Kroah-Hartman 		 */
2253ab4382d2SGreg Kroah-Hartman 		memset(&termios, 0, sizeof(struct ktermios));
2254ab4382d2SGreg Kroah-Hartman 		termios.c_cflag = uport->cons->cflag;
2255ab4382d2SGreg Kroah-Hartman 
2256ab4382d2SGreg Kroah-Hartman 		/*
2257ab4382d2SGreg Kroah-Hartman 		 * If that's unset, use the tty termios setting.
2258ab4382d2SGreg Kroah-Hartman 		 */
2259adc8d746SAlan Cox 		if (port->tty && termios.c_cflag == 0)
2260adc8d746SAlan Cox 			termios = port->tty->termios;
2261ab4382d2SGreg Kroah-Hartman 
226294abc56fSNing Jiang 		if (console_suspend_enabled)
22636f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2264ab4382d2SGreg Kroah-Hartman 		uport->ops->set_termios(uport, &termios, NULL);
22655933a161SYin Kangkai 		if (console_suspend_enabled)
2266ab4382d2SGreg Kroah-Hartman 			console_start(uport->cons);
2267ab4382d2SGreg Kroah-Hartman 	}
2268ab4382d2SGreg Kroah-Hartman 
226980f02d54SPeter Hurley 	if (tty_port_suspended(port)) {
2270ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2271ab4382d2SGreg Kroah-Hartman 		int ret;
2272ab4382d2SGreg Kroah-Hartman 
22736f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2274ab4382d2SGreg Kroah-Hartman 		spin_lock_irq(&uport->lock);
2275ab4382d2SGreg Kroah-Hartman 		ops->set_mctrl(uport, 0);
2276ab4382d2SGreg Kroah-Hartman 		spin_unlock_irq(&uport->lock);
2277ab4382d2SGreg Kroah-Hartman 		if (console_suspend_enabled || !uart_console(uport)) {
2278ab4382d2SGreg Kroah-Hartman 			/* Protected by port mutex for now */
2279ab4382d2SGreg Kroah-Hartman 			struct tty_struct *tty = port->tty;
2280ab4382d2SGreg Kroah-Hartman 			ret = ops->startup(uport);
2281ab4382d2SGreg Kroah-Hartman 			if (ret == 0) {
2282ab4382d2SGreg Kroah-Hartman 				if (tty)
2283ab4382d2SGreg Kroah-Hartman 					uart_change_speed(tty, state, NULL);
2284ab4382d2SGreg Kroah-Hartman 				spin_lock_irq(&uport->lock);
2285ab4382d2SGreg Kroah-Hartman 				ops->set_mctrl(uport, uport->mctrl);
2286ab4382d2SGreg Kroah-Hartman 				ops->start_tx(uport);
2287ab4382d2SGreg Kroah-Hartman 				spin_unlock_irq(&uport->lock);
2288d41861caSPeter Hurley 				tty_port_set_initialized(port, 1);
2289ab4382d2SGreg Kroah-Hartman 			} else {
2290ab4382d2SGreg Kroah-Hartman 				/*
2291ab4382d2SGreg Kroah-Hartman 				 * Failed to resume - maybe hardware went away?
2292ab4382d2SGreg Kroah-Hartman 				 * Clear the "initialized" flag so we won't try
2293ab4382d2SGreg Kroah-Hartman 				 * to call the low level drivers shutdown method.
2294ab4382d2SGreg Kroah-Hartman 				 */
2295ab4382d2SGreg Kroah-Hartman 				uart_shutdown(tty, state);
2296ab4382d2SGreg Kroah-Hartman 			}
2297ab4382d2SGreg Kroah-Hartman 		}
2298ab4382d2SGreg Kroah-Hartman 
229980f02d54SPeter Hurley 		tty_port_set_suspended(port, 0);
2300ab4382d2SGreg Kroah-Hartman 	}
2301ab4382d2SGreg Kroah-Hartman 
2302ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2303ab4382d2SGreg Kroah-Hartman 
2304ab4382d2SGreg Kroah-Hartman 	return 0;
2305ab4382d2SGreg Kroah-Hartman }
2306ab4382d2SGreg Kroah-Hartman 
2307ab4382d2SGreg Kroah-Hartman static inline void
2308ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port)
2309ab4382d2SGreg Kroah-Hartman {
2310ab4382d2SGreg Kroah-Hartman 	char address[64];
2311ab4382d2SGreg Kroah-Hartman 
2312ab4382d2SGreg Kroah-Hartman 	switch (port->iotype) {
2313ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2314ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2315ab4382d2SGreg Kroah-Hartman 		break;
2316ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2317ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2318ab4382d2SGreg Kroah-Hartman 			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2319ab4382d2SGreg Kroah-Hartman 		break;
2320ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2321bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2322ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
23233ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2324ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2325ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2326ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2327ab4382d2SGreg Kroah-Hartman 			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2328ab4382d2SGreg Kroah-Hartman 		break;
2329ab4382d2SGreg Kroah-Hartman 	default:
2330ab4382d2SGreg Kroah-Hartman 		strlcpy(address, "*unknown*", sizeof(address));
2331ab4382d2SGreg Kroah-Hartman 		break;
2332ab4382d2SGreg Kroah-Hartman 	}
2333ab4382d2SGreg Kroah-Hartman 
2334cade3580SAndy Shevchenko 	pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
233568ed7e1cSJames Bottomley 	       port->dev ? dev_name(port->dev) : "",
233668ed7e1cSJames Bottomley 	       port->dev ? ": " : "",
2337cade3580SAndy Shevchenko 	       port->name,
23387d12b976SKees Cook 	       address, port->irq, port->uartclk / 16, uart_type(port));
2339ab4382d2SGreg Kroah-Hartman }
2340ab4382d2SGreg Kroah-Hartman 
2341ab4382d2SGreg Kroah-Hartman static void
2342ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2343ab4382d2SGreg Kroah-Hartman 		    struct uart_port *port)
2344ab4382d2SGreg Kroah-Hartman {
2345ab4382d2SGreg Kroah-Hartman 	unsigned int flags;
2346ab4382d2SGreg Kroah-Hartman 
2347ab4382d2SGreg Kroah-Hartman 	/*
2348ab4382d2SGreg Kroah-Hartman 	 * If there isn't a port here, don't do anything further.
2349ab4382d2SGreg Kroah-Hartman 	 */
2350ab4382d2SGreg Kroah-Hartman 	if (!port->iobase && !port->mapbase && !port->membase)
2351ab4382d2SGreg Kroah-Hartman 		return;
2352ab4382d2SGreg Kroah-Hartman 
2353ab4382d2SGreg Kroah-Hartman 	/*
2354ab4382d2SGreg Kroah-Hartman 	 * Now do the auto configuration stuff.  Note that config_port
2355ab4382d2SGreg Kroah-Hartman 	 * is expected to claim the resources and map the port for us.
2356ab4382d2SGreg Kroah-Hartman 	 */
2357ab4382d2SGreg Kroah-Hartman 	flags = 0;
2358ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_AUTO_IRQ)
2359ab4382d2SGreg Kroah-Hartman 		flags |= UART_CONFIG_IRQ;
2360ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_BOOT_AUTOCONF) {
2361ab4382d2SGreg Kroah-Hartman 		if (!(port->flags & UPF_FIXED_TYPE)) {
2362ab4382d2SGreg Kroah-Hartman 			port->type = PORT_UNKNOWN;
2363ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_TYPE;
2364ab4382d2SGreg Kroah-Hartman 		}
2365ab4382d2SGreg Kroah-Hartman 		port->ops->config_port(port, flags);
2366ab4382d2SGreg Kroah-Hartman 	}
2367ab4382d2SGreg Kroah-Hartman 
2368ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_UNKNOWN) {
2369ab4382d2SGreg Kroah-Hartman 		unsigned long flags;
2370ab4382d2SGreg Kroah-Hartman 
2371ab4382d2SGreg Kroah-Hartman 		uart_report_port(drv, port);
2372ab4382d2SGreg Kroah-Hartman 
2373ab4382d2SGreg Kroah-Hartman 		/* Power up port for set_mctrl() */
23746f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2375ab4382d2SGreg Kroah-Hartman 
2376ab4382d2SGreg Kroah-Hartman 		/*
2377ab4382d2SGreg Kroah-Hartman 		 * Ensure that the modem control lines are de-activated.
2378ab4382d2SGreg Kroah-Hartman 		 * keep the DTR setting that is set in uart_set_options()
2379ab4382d2SGreg Kroah-Hartman 		 * We probably don't need a spinlock around this, but
2380ab4382d2SGreg Kroah-Hartman 		 */
2381ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
2382ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2383ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
2384ab4382d2SGreg Kroah-Hartman 
2385ab4382d2SGreg Kroah-Hartman 		/*
2386ab4382d2SGreg Kroah-Hartman 		 * If this driver supports console, and it hasn't been
2387ab4382d2SGreg Kroah-Hartman 		 * successfully registered yet, try to re-register it.
2388ab4382d2SGreg Kroah-Hartman 		 * It may be that the port was not available.
2389ab4382d2SGreg Kroah-Hartman 		 */
2390ab4382d2SGreg Kroah-Hartman 		if (port->cons && !(port->cons->flags & CON_ENABLED))
2391ab4382d2SGreg Kroah-Hartman 			register_console(port->cons);
2392ab4382d2SGreg Kroah-Hartman 
2393ab4382d2SGreg Kroah-Hartman 		/*
2394ab4382d2SGreg Kroah-Hartman 		 * Power down all ports by default, except the
2395ab4382d2SGreg Kroah-Hartman 		 * console if we have one.
2396ab4382d2SGreg Kroah-Hartman 		 */
2397ab4382d2SGreg Kroah-Hartman 		if (!uart_console(port))
23986f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_OFF);
2399ab4382d2SGreg Kroah-Hartman 	}
2400ab4382d2SGreg Kroah-Hartman }
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2403ab4382d2SGreg Kroah-Hartman 
2404ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2405ab4382d2SGreg Kroah-Hartman {
2406ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2407ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
240849c02304SPeter Hurley 	struct tty_port *tport;
2409ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2410ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
2411ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2412ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2413ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
241449c02304SPeter Hurley 	int ret = 0;
2415ab4382d2SGreg Kroah-Hartman 
241649c02304SPeter Hurley 	tport = &state->port;
241749c02304SPeter Hurley 	mutex_lock(&tport->mutex);
241849c02304SPeter Hurley 
24194047b371SPeter Hurley 	port = uart_port_check(state);
24204047b371SPeter Hurley 	if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
242149c02304SPeter Hurley 		ret = -1;
242249c02304SPeter Hurley 		goto out;
242349c02304SPeter Hurley 	}
2424ab4382d2SGreg Kroah-Hartman 
2425c7f3e708SAnton Vorontsov 	if (port->ops->poll_init) {
2426c7f3e708SAnton Vorontsov 		/*
2427d41861caSPeter Hurley 		 * We don't set initialized as we only initialized the hw,
2428d41861caSPeter Hurley 		 * e.g. state->xmit is still uninitialized.
2429c7f3e708SAnton Vorontsov 		 */
2430d41861caSPeter Hurley 		if (!tty_port_initialized(tport))
2431c7f3e708SAnton Vorontsov 			ret = port->ops->poll_init(port);
2432c7f3e708SAnton Vorontsov 	}
2433c7f3e708SAnton Vorontsov 
243449c02304SPeter Hurley 	if (!ret && options) {
2435ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
243649c02304SPeter Hurley 		ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2437ab4382d2SGreg Kroah-Hartman 	}
243849c02304SPeter Hurley out:
243949c02304SPeter Hurley 	mutex_unlock(&tport->mutex);
244049c02304SPeter Hurley 	return ret;
2441ab4382d2SGreg Kroah-Hartman }
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line)
2444ab4382d2SGreg Kroah-Hartman {
2445ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2446ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2447ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
24489ed19428SPeter Hurley 	int ret = -1;
2449ab4382d2SGreg Kroah-Hartman 
24509ed19428SPeter Hurley 	port = uart_port_ref(state);
2451ef510beaSAndy Shevchenko 	if (port) {
24529ed19428SPeter Hurley 		ret = port->ops->poll_get_char(port);
24539ed19428SPeter Hurley 		uart_port_deref(port);
24549ed19428SPeter Hurley 	}
245522077b09SJiri Slaby 
24569ed19428SPeter Hurley 	return ret;
2457ab4382d2SGreg Kroah-Hartman }
2458ab4382d2SGreg Kroah-Hartman 
2459ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2460ab4382d2SGreg Kroah-Hartman {
2461ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2462ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2463ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2464ab4382d2SGreg Kroah-Hartman 
24659ed19428SPeter Hurley 	port = uart_port_ref(state);
24669ed19428SPeter Hurley 	if (!port)
24679ed19428SPeter Hurley 		return;
2468c7d44a02SDoug Anderson 
2469c7d44a02SDoug Anderson 	if (ch == '\n')
2470c7d44a02SDoug Anderson 		port->ops->poll_put_char(port, '\r');
2471ab4382d2SGreg Kroah-Hartman 	port->ops->poll_put_char(port, ch);
24729ed19428SPeter Hurley 	uart_port_deref(port);
2473ab4382d2SGreg Kroah-Hartman }
2474ab4382d2SGreg Kroah-Hartman #endif
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = {
24774cdd17baSJiri Slaby 	.install	= uart_install,
2478ab4382d2SGreg Kroah-Hartman 	.open		= uart_open,
2479ab4382d2SGreg Kroah-Hartman 	.close		= uart_close,
2480ab4382d2SGreg Kroah-Hartman 	.write		= uart_write,
2481ab4382d2SGreg Kroah-Hartman 	.put_char	= uart_put_char,
2482ab4382d2SGreg Kroah-Hartman 	.flush_chars	= uart_flush_chars,
2483ab4382d2SGreg Kroah-Hartman 	.write_room	= uart_write_room,
2484ab4382d2SGreg Kroah-Hartman 	.chars_in_buffer= uart_chars_in_buffer,
2485ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= uart_flush_buffer,
2486ab4382d2SGreg Kroah-Hartman 	.ioctl		= uart_ioctl,
2487ab4382d2SGreg Kroah-Hartman 	.throttle	= uart_throttle,
2488ab4382d2SGreg Kroah-Hartman 	.unthrottle	= uart_unthrottle,
2489ab4382d2SGreg Kroah-Hartman 	.send_xchar	= uart_send_xchar,
2490ab4382d2SGreg Kroah-Hartman 	.set_termios	= uart_set_termios,
2491ab4382d2SGreg Kroah-Hartman 	.set_ldisc	= uart_set_ldisc,
2492ab4382d2SGreg Kroah-Hartman 	.stop		= uart_stop,
2493ab4382d2SGreg Kroah-Hartman 	.start		= uart_start,
2494ab4382d2SGreg Kroah-Hartman 	.hangup		= uart_hangup,
2495ab4382d2SGreg Kroah-Hartman 	.break_ctl	= uart_break_ctl,
2496ab4382d2SGreg Kroah-Hartman 	.wait_until_sent= uart_wait_until_sent,
2497ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
24988a8dcabfSChristoph Hellwig 	.proc_show	= uart_proc_show,
2499ab4382d2SGreg Kroah-Hartman #endif
2500ab4382d2SGreg Kroah-Hartman 	.tiocmget	= uart_tiocmget,
2501ab4382d2SGreg Kroah-Hartman 	.tiocmset	= uart_tiocmset,
25025099d234SAl Viro 	.set_serial	= uart_set_info_user,
25035099d234SAl Viro 	.get_serial	= uart_get_info_user,
2504ab4382d2SGreg Kroah-Hartman 	.get_icount	= uart_get_icount,
2505ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2506ab4382d2SGreg Kroah-Hartman 	.poll_init	= uart_poll_init,
2507ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= uart_poll_get_char,
2508ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= uart_poll_put_char,
2509ab4382d2SGreg Kroah-Hartman #endif
2510ab4382d2SGreg Kroah-Hartman };
2511ab4382d2SGreg Kroah-Hartman 
2512ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = {
2513ab4382d2SGreg Kroah-Hartman 	.carrier_raised = uart_carrier_raised,
2514ab4382d2SGreg Kroah-Hartman 	.dtr_rts	= uart_dtr_rts,
2515b3b57646SRob Herring 	.activate	= uart_port_activate,
2516761ed4a9SRob Herring 	.shutdown	= uart_tty_port_shutdown,
2517ab4382d2SGreg Kroah-Hartman };
2518ab4382d2SGreg Kroah-Hartman 
2519ab4382d2SGreg Kroah-Hartman /**
2520ab4382d2SGreg Kroah-Hartman  *	uart_register_driver - register a driver with the uart core layer
2521ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2522ab4382d2SGreg Kroah-Hartman  *
2523ab4382d2SGreg Kroah-Hartman  *	Register a uart driver with the core driver.  We in turn register
2524ab4382d2SGreg Kroah-Hartman  *	with the tty layer, and initialise the core driver per-port state.
2525ab4382d2SGreg Kroah-Hartman  *
2526ab4382d2SGreg Kroah-Hartman  *	We have a proc file in /proc/tty/driver which is named after the
2527ab4382d2SGreg Kroah-Hartman  *	normal driver.
2528ab4382d2SGreg Kroah-Hartman  *
2529ab4382d2SGreg Kroah-Hartman  *	drv->port should be NULL, and the per-port structures should be
2530ab4382d2SGreg Kroah-Hartman  *	registered using uart_add_one_port after this call has succeeded.
2531ab4382d2SGreg Kroah-Hartman  */
2532ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv)
2533ab4382d2SGreg Kroah-Hartman {
2534ab4382d2SGreg Kroah-Hartman 	struct tty_driver *normal;
2535050dfc09SSergey Organov 	int i, retval = -ENOMEM;
2536ab4382d2SGreg Kroah-Hartman 
2537ab4382d2SGreg Kroah-Hartman 	BUG_ON(drv->state);
2538ab4382d2SGreg Kroah-Hartman 
2539ab4382d2SGreg Kroah-Hartman 	/*
2540ab4382d2SGreg Kroah-Hartman 	 * Maybe we should be using a slab cache for this, especially if
2541ab4382d2SGreg Kroah-Hartman 	 * we have a large number of ports to handle.
2542ab4382d2SGreg Kroah-Hartman 	 */
25436396bb22SKees Cook 	drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2544ab4382d2SGreg Kroah-Hartman 	if (!drv->state)
2545ab4382d2SGreg Kroah-Hartman 		goto out;
2546ab4382d2SGreg Kroah-Hartman 
2547ab4382d2SGreg Kroah-Hartman 	normal = alloc_tty_driver(drv->nr);
2548ab4382d2SGreg Kroah-Hartman 	if (!normal)
2549ab4382d2SGreg Kroah-Hartman 		goto out_kfree;
2550ab4382d2SGreg Kroah-Hartman 
2551ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = normal;
2552ab4382d2SGreg Kroah-Hartman 
2553ab4382d2SGreg Kroah-Hartman 	normal->driver_name	= drv->driver_name;
2554ab4382d2SGreg Kroah-Hartman 	normal->name		= drv->dev_name;
2555ab4382d2SGreg Kroah-Hartman 	normal->major		= drv->major;
2556ab4382d2SGreg Kroah-Hartman 	normal->minor_start	= drv->minor;
2557ab4382d2SGreg Kroah-Hartman 	normal->type		= TTY_DRIVER_TYPE_SERIAL;
2558ab4382d2SGreg Kroah-Hartman 	normal->subtype		= SERIAL_TYPE_NORMAL;
2559ab4382d2SGreg Kroah-Hartman 	normal->init_termios	= tty_std_termios;
2560ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2561ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2562ab4382d2SGreg Kroah-Hartman 	normal->flags		= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2563ab4382d2SGreg Kroah-Hartman 	normal->driver_state    = drv;
2564ab4382d2SGreg Kroah-Hartman 	tty_set_operations(normal, &uart_ops);
2565ab4382d2SGreg Kroah-Hartman 
2566ab4382d2SGreg Kroah-Hartman 	/*
2567ab4382d2SGreg Kroah-Hartman 	 * Initialise the UART state(s).
2568ab4382d2SGreg Kroah-Hartman 	 */
2569ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++) {
2570ab4382d2SGreg Kroah-Hartman 		struct uart_state *state = drv->state + i;
2571ab4382d2SGreg Kroah-Hartman 		struct tty_port *port = &state->port;
2572ab4382d2SGreg Kroah-Hartman 
2573ab4382d2SGreg Kroah-Hartman 		tty_port_init(port);
2574ab4382d2SGreg Kroah-Hartman 		port->ops = &uart_port_ops;
2575ab4382d2SGreg Kroah-Hartman 	}
2576ab4382d2SGreg Kroah-Hartman 
2577ab4382d2SGreg Kroah-Hartman 	retval = tty_register_driver(normal);
2578ab4382d2SGreg Kroah-Hartman 	if (retval >= 0)
2579ab4382d2SGreg Kroah-Hartman 		return retval;
2580ab4382d2SGreg Kroah-Hartman 
2581191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2582191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2583ab4382d2SGreg Kroah-Hartman 	put_tty_driver(normal);
2584ab4382d2SGreg Kroah-Hartman out_kfree:
2585ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
2586ab4382d2SGreg Kroah-Hartman out:
2587050dfc09SSergey Organov 	return retval;
2588ab4382d2SGreg Kroah-Hartman }
2589ab4382d2SGreg Kroah-Hartman 
2590ab4382d2SGreg Kroah-Hartman /**
2591ab4382d2SGreg Kroah-Hartman  *	uart_unregister_driver - remove a driver from the uart core layer
2592ab4382d2SGreg Kroah-Hartman  *	@drv: low level driver structure
2593ab4382d2SGreg Kroah-Hartman  *
2594ab4382d2SGreg Kroah-Hartman  *	Remove all references to a driver from the core driver.  The low
2595ab4382d2SGreg Kroah-Hartman  *	level driver must have removed all its ports via the
2596ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port() if it registered them with uart_add_one_port().
2597ab4382d2SGreg Kroah-Hartman  *	(ie, drv->port == NULL)
2598ab4382d2SGreg Kroah-Hartman  */
2599ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv)
2600ab4382d2SGreg Kroah-Hartman {
2601ab4382d2SGreg Kroah-Hartman 	struct tty_driver *p = drv->tty_driver;
2602191c5f10SJiri Slaby 	unsigned int i;
2603191c5f10SJiri Slaby 
2604ab4382d2SGreg Kroah-Hartman 	tty_unregister_driver(p);
2605ab4382d2SGreg Kroah-Hartman 	put_tty_driver(p);
2606191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2607191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2608ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
26091e66cdedSAlan Cox 	drv->state = NULL;
2610ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = NULL;
2611ab4382d2SGreg Kroah-Hartman }
2612ab4382d2SGreg Kroah-Hartman 
2613ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index)
2614ab4382d2SGreg Kroah-Hartman {
2615ab4382d2SGreg Kroah-Hartman 	struct uart_driver *p = co->data;
2616ab4382d2SGreg Kroah-Hartman 	*index = co->index;
2617ab4382d2SGreg Kroah-Hartman 	return p->tty_driver;
2618ab4382d2SGreg Kroah-Hartman }
2619488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device);
2620ab4382d2SGreg Kroah-Hartman 
2621143c02c8SAndy Shevchenko static ssize_t uartclk_show(struct device *dev,
26226915c0e4STomas Hlavacek 	struct device_attribute *attr, char *buf)
26236915c0e4STomas Hlavacek {
2624bebe73e3SAlan Cox 	struct serial_struct tmp;
26256915c0e4STomas Hlavacek 	struct tty_port *port = dev_get_drvdata(dev);
2626b1b79916STomas Hlavacek 
26279f109694SAlan Cox 	uart_get_info(port, &tmp);
2628bebe73e3SAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
26296915c0e4STomas Hlavacek }
26306915c0e4STomas Hlavacek 
2631143c02c8SAndy Shevchenko static ssize_t type_show(struct device *dev,
2632373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2633373bac4cSAlan Cox {
2634373bac4cSAlan Cox 	struct serial_struct tmp;
2635373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2636373bac4cSAlan Cox 
2637373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2638373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2639373bac4cSAlan Cox }
2640143c02c8SAndy Shevchenko 
2641143c02c8SAndy Shevchenko static ssize_t line_show(struct device *dev,
2642373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2643373bac4cSAlan Cox {
2644373bac4cSAlan Cox 	struct serial_struct tmp;
2645373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2646373bac4cSAlan Cox 
2647373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2648373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2649373bac4cSAlan Cox }
2650373bac4cSAlan Cox 
2651143c02c8SAndy Shevchenko static ssize_t port_show(struct device *dev,
2652373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2653373bac4cSAlan Cox {
2654373bac4cSAlan Cox 	struct serial_struct tmp;
2655373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2656fd985e1dSAndrew Morton 	unsigned long ioaddr;
2657373bac4cSAlan Cox 
2658373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2659fd985e1dSAndrew Morton 	ioaddr = tmp.port;
2660fd985e1dSAndrew Morton 	if (HIGH_BITS_OFFSET)
2661fd985e1dSAndrew Morton 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2662fd985e1dSAndrew Morton 	return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2663373bac4cSAlan Cox }
2664373bac4cSAlan Cox 
2665143c02c8SAndy Shevchenko static ssize_t irq_show(struct device *dev,
2666373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2667373bac4cSAlan Cox {
2668373bac4cSAlan Cox 	struct serial_struct tmp;
2669373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2670373bac4cSAlan Cox 
2671373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2672373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2673373bac4cSAlan Cox }
2674373bac4cSAlan Cox 
2675143c02c8SAndy Shevchenko static ssize_t flags_show(struct device *dev,
2676373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2677373bac4cSAlan Cox {
2678373bac4cSAlan Cox 	struct serial_struct tmp;
2679373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2680373bac4cSAlan Cox 
2681373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2682373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2683373bac4cSAlan Cox }
2684373bac4cSAlan Cox 
2685143c02c8SAndy Shevchenko static ssize_t xmit_fifo_size_show(struct device *dev,
2686373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2687373bac4cSAlan Cox {
2688373bac4cSAlan Cox 	struct serial_struct tmp;
2689373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2690373bac4cSAlan Cox 
2691373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2692373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2693373bac4cSAlan Cox }
2694373bac4cSAlan Cox 
2695143c02c8SAndy Shevchenko static ssize_t close_delay_show(struct device *dev,
2696373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2697373bac4cSAlan Cox {
2698373bac4cSAlan Cox 	struct serial_struct tmp;
2699373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2700373bac4cSAlan Cox 
2701373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2702373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2703373bac4cSAlan Cox }
2704373bac4cSAlan Cox 
2705143c02c8SAndy Shevchenko static ssize_t closing_wait_show(struct device *dev,
2706373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2707373bac4cSAlan Cox {
2708373bac4cSAlan Cox 	struct serial_struct tmp;
2709373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2710373bac4cSAlan Cox 
2711373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2712373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2713373bac4cSAlan Cox }
2714373bac4cSAlan Cox 
2715143c02c8SAndy Shevchenko static ssize_t custom_divisor_show(struct device *dev,
2716373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2717373bac4cSAlan Cox {
2718373bac4cSAlan Cox 	struct serial_struct tmp;
2719373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2720373bac4cSAlan Cox 
2721373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2722373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2723373bac4cSAlan Cox }
2724373bac4cSAlan Cox 
2725143c02c8SAndy Shevchenko static ssize_t io_type_show(struct device *dev,
2726373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2727373bac4cSAlan Cox {
2728373bac4cSAlan Cox 	struct serial_struct tmp;
2729373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2730373bac4cSAlan Cox 
2731373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2732373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2733373bac4cSAlan Cox }
2734373bac4cSAlan Cox 
2735143c02c8SAndy Shevchenko static ssize_t iomem_base_show(struct device *dev,
2736373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2737373bac4cSAlan Cox {
2738373bac4cSAlan Cox 	struct serial_struct tmp;
2739373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2740373bac4cSAlan Cox 
2741373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2742373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2743373bac4cSAlan Cox }
2744373bac4cSAlan Cox 
2745143c02c8SAndy Shevchenko static ssize_t iomem_reg_shift_show(struct device *dev,
2746373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2747373bac4cSAlan Cox {
2748373bac4cSAlan Cox 	struct serial_struct tmp;
2749373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2750373bac4cSAlan Cox 
2751373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2752373bac4cSAlan Cox 	return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2753373bac4cSAlan Cox }
2754373bac4cSAlan Cox 
2755a3cb39d2SAndy Shevchenko static ssize_t console_show(struct device *dev,
2756a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, char *buf)
2757a3cb39d2SAndy Shevchenko {
2758a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2759a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2760a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2761a3cb39d2SAndy Shevchenko 	bool console = false;
2762a3cb39d2SAndy Shevchenko 
2763a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2764a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2765a3cb39d2SAndy Shevchenko 	if (uport)
2766a3cb39d2SAndy Shevchenko 		console = uart_console_enabled(uport);
2767a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
2768a3cb39d2SAndy Shevchenko 
2769a3cb39d2SAndy Shevchenko 	return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2770a3cb39d2SAndy Shevchenko }
2771a3cb39d2SAndy Shevchenko 
2772a3cb39d2SAndy Shevchenko static ssize_t console_store(struct device *dev,
2773a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, const char *buf, size_t count)
2774a3cb39d2SAndy Shevchenko {
2775a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
2776a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
2777a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
2778a3cb39d2SAndy Shevchenko 	bool oldconsole, newconsole;
2779a3cb39d2SAndy Shevchenko 	int ret;
2780a3cb39d2SAndy Shevchenko 
2781a3cb39d2SAndy Shevchenko 	ret = kstrtobool(buf, &newconsole);
2782a3cb39d2SAndy Shevchenko 	if (ret)
2783a3cb39d2SAndy Shevchenko 		return ret;
2784a3cb39d2SAndy Shevchenko 
2785a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
2786a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
2787a3cb39d2SAndy Shevchenko 	if (uport) {
2788a3cb39d2SAndy Shevchenko 		oldconsole = uart_console_enabled(uport);
2789a3cb39d2SAndy Shevchenko 		if (oldconsole && !newconsole) {
2790a3cb39d2SAndy Shevchenko 			ret = unregister_console(uport->cons);
2791a3cb39d2SAndy Shevchenko 		} else if (!oldconsole && newconsole) {
2792a3cb39d2SAndy Shevchenko 			if (uart_console(uport))
2793a3cb39d2SAndy Shevchenko 				register_console(uport->cons);
2794a3cb39d2SAndy Shevchenko 			else
2795a3cb39d2SAndy Shevchenko 				ret = -ENOENT;
2796a3cb39d2SAndy Shevchenko 		}
2797a3cb39d2SAndy Shevchenko 	} else {
2798a3cb39d2SAndy Shevchenko 		ret = -ENXIO;
2799a3cb39d2SAndy Shevchenko 	}
2800a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
2801a3cb39d2SAndy Shevchenko 
2802a3cb39d2SAndy Shevchenko 	return ret < 0 ? ret : count;
2803a3cb39d2SAndy Shevchenko }
2804a3cb39d2SAndy Shevchenko 
2805143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(uartclk);
2806143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(type);
2807143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(line);
2808143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(port);
2809143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(irq);
2810143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(flags);
2811143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(xmit_fifo_size);
2812143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(close_delay);
2813143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(closing_wait);
2814143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(custom_divisor);
2815143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(io_type);
2816143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_base);
2817143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_reg_shift);
2818a3cb39d2SAndy Shevchenko static DEVICE_ATTR_RW(console);
28196915c0e4STomas Hlavacek 
28206915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = {
2821143c02c8SAndy Shevchenko 	&dev_attr_uartclk.attr,
2822373bac4cSAlan Cox 	&dev_attr_type.attr,
2823373bac4cSAlan Cox 	&dev_attr_line.attr,
2824373bac4cSAlan Cox 	&dev_attr_port.attr,
2825373bac4cSAlan Cox 	&dev_attr_irq.attr,
2826373bac4cSAlan Cox 	&dev_attr_flags.attr,
2827373bac4cSAlan Cox 	&dev_attr_xmit_fifo_size.attr,
2828373bac4cSAlan Cox 	&dev_attr_close_delay.attr,
2829373bac4cSAlan Cox 	&dev_attr_closing_wait.attr,
2830373bac4cSAlan Cox 	&dev_attr_custom_divisor.attr,
2831373bac4cSAlan Cox 	&dev_attr_io_type.attr,
2832373bac4cSAlan Cox 	&dev_attr_iomem_base.attr,
2833373bac4cSAlan Cox 	&dev_attr_iomem_reg_shift.attr,
2834a3cb39d2SAndy Shevchenko 	&dev_attr_console.attr,
2835a3cb39d2SAndy Shevchenko 	NULL
28366915c0e4STomas Hlavacek };
28376915c0e4STomas Hlavacek 
2838b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = {
28396915c0e4STomas Hlavacek 	.attrs = tty_dev_attrs,
28406915c0e4STomas Hlavacek };
28416915c0e4STomas Hlavacek 
2842ab4382d2SGreg Kroah-Hartman /**
2843ab4382d2SGreg Kroah-Hartman  *	uart_add_one_port - attach a driver-defined port structure
2844ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
2845ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure to use for this port.
2846ab4382d2SGreg Kroah-Hartman  *
2847ab4382d2SGreg Kroah-Hartman  *	This allows the driver to register its own uart_port structure
2848ab4382d2SGreg Kroah-Hartman  *	with the core driver.  The main purpose is to allow the low
2849ab4382d2SGreg Kroah-Hartman  *	level uart drivers to expand uart_port, rather than having yet
2850ab4382d2SGreg Kroah-Hartman  *	more levels of structures.
2851ab4382d2SGreg Kroah-Hartman  */
2852ab4382d2SGreg Kroah-Hartman int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2853ab4382d2SGreg Kroah-Hartman {
2854ab4382d2SGreg Kroah-Hartman 	struct uart_state *state;
2855ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
2856ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2857ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2858266dcff0SGreg Kroah-Hartman 	int num_groups;
2859ab4382d2SGreg Kroah-Hartman 
2860ab4382d2SGreg Kroah-Hartman 	BUG_ON(in_interrupt());
2861ab4382d2SGreg Kroah-Hartman 
2862ab4382d2SGreg Kroah-Hartman 	if (uport->line >= drv->nr)
2863ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
2864ab4382d2SGreg Kroah-Hartman 
2865ab4382d2SGreg Kroah-Hartman 	state = drv->state + uport->line;
2866ab4382d2SGreg Kroah-Hartman 	port = &state->port;
2867ab4382d2SGreg Kroah-Hartman 
2868ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
2869ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2870ab4382d2SGreg Kroah-Hartman 	if (state->uart_port) {
2871ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2872ab4382d2SGreg Kroah-Hartman 		goto out;
2873ab4382d2SGreg Kroah-Hartman 	}
2874ab4382d2SGreg Kroah-Hartman 
28752b702b9bSPeter Hurley 	/* Link the port to the driver state table and vice versa */
28769ed19428SPeter Hurley 	atomic_set(&state->refcount, 1);
28779ed19428SPeter Hurley 	init_waitqueue_head(&state->remove_wait);
2878ab4382d2SGreg Kroah-Hartman 	state->uart_port = uport;
2879ab4382d2SGreg Kroah-Hartman 	uport->state = state;
2880ab4382d2SGreg Kroah-Hartman 
28812b702b9bSPeter Hurley 	state->pm_state = UART_PM_STATE_UNDEFINED;
28822b702b9bSPeter Hurley 	uport->cons = drv->cons;
2883959801feSPeter Hurley 	uport->minor = drv->tty_driver->minor_start + uport->line;
2884f7048b15SVignesh R 	uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
2885f7048b15SVignesh R 				drv->tty_driver->name_base + uport->line);
2886f7048b15SVignesh R 	if (!uport->name) {
2887f7048b15SVignesh R 		ret = -ENOMEM;
2888f7048b15SVignesh R 		goto out;
2889f7048b15SVignesh R 	}
28902b702b9bSPeter Hurley 
2891d2403cadSAndy Shevchenko 	uart_port_spin_lock_init(uport);
2892d2403cadSAndy Shevchenko 
2893a208ffd2SGrant Likely 	if (uport->cons && uport->dev)
2894dd8b7a1dSMichal Simek 		of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
2895ab4382d2SGreg Kroah-Hartman 
2896fb2b9001SSudip Mukherjee 	tty_port_link_device(port, drv->tty_driver, uport->line);
2897ab4382d2SGreg Kroah-Hartman 	uart_configure_port(drv, state, uport);
2898ab4382d2SGreg Kroah-Hartman 
28994dda864dSGeert Uytterhoeven 	port->console = uart_console(uport);
29004dda864dSGeert Uytterhoeven 
2901266dcff0SGreg Kroah-Hartman 	num_groups = 2;
2902266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
2903266dcff0SGreg Kroah-Hartman 		num_groups++;
2904266dcff0SGreg Kroah-Hartman 
2905c2b703b8SYoshihiro YUNOMAE 	uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
2906266dcff0SGreg Kroah-Hartman 				    GFP_KERNEL);
2907266dcff0SGreg Kroah-Hartman 	if (!uport->tty_groups) {
2908266dcff0SGreg Kroah-Hartman 		ret = -ENOMEM;
2909266dcff0SGreg Kroah-Hartman 		goto out;
2910266dcff0SGreg Kroah-Hartman 	}
2911266dcff0SGreg Kroah-Hartman 	uport->tty_groups[0] = &tty_dev_attr_group;
2912266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
2913266dcff0SGreg Kroah-Hartman 		uport->tty_groups[1] = uport->attr_group;
2914266dcff0SGreg Kroah-Hartman 
2915ab4382d2SGreg Kroah-Hartman 	/*
2916ab4382d2SGreg Kroah-Hartman 	 * Register the port whether it's detected or not.  This allows
2917015355b7SGeert Uytterhoeven 	 * setserial to be used to alter this port's parameters.
2918ab4382d2SGreg Kroah-Hartman 	 */
2919da4c2799SJohan Hovold 	tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
2920266dcff0SGreg Kroah-Hartman 			uport->line, uport->dev, port, uport->tty_groups);
2921b289c496SChengguang Xu 	if (!IS_ERR(tty_dev)) {
292277359835SSimon Glass 		device_set_wakeup_capable(tty_dev, 1);
292377359835SSimon Glass 	} else {
29242f2dafe7SSudip Mukherjee 		dev_err(uport->dev, "Cannot register tty device on line %d\n",
2925ab4382d2SGreg Kroah-Hartman 		       uport->line);
292677359835SSimon Glass 	}
2927ab4382d2SGreg Kroah-Hartman 
2928ab4382d2SGreg Kroah-Hartman 	/*
2929ab4382d2SGreg Kroah-Hartman 	 * Ensure UPF_DEAD is not set.
2930ab4382d2SGreg Kroah-Hartman 	 */
2931ab4382d2SGreg Kroah-Hartman 	uport->flags &= ~UPF_DEAD;
2932ab4382d2SGreg Kroah-Hartman 
2933ab4382d2SGreg Kroah-Hartman  out:
2934ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2935ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
2936ab4382d2SGreg Kroah-Hartman 
2937ab4382d2SGreg Kroah-Hartman 	return ret;
2938ab4382d2SGreg Kroah-Hartman }
2939ab4382d2SGreg Kroah-Hartman 
2940ab4382d2SGreg Kroah-Hartman /**
2941ab4382d2SGreg Kroah-Hartman  *	uart_remove_one_port - detach a driver defined port structure
2942ab4382d2SGreg Kroah-Hartman  *	@drv: pointer to the uart low level driver structure for this port
2943ab4382d2SGreg Kroah-Hartman  *	@uport: uart port structure for this port
2944ab4382d2SGreg Kroah-Hartman  *
2945ab4382d2SGreg Kroah-Hartman  *	This unhooks (and hangs up) the specified port structure from the
2946ab4382d2SGreg Kroah-Hartman  *	core driver.  No further calls will be made to the low-level code
2947ab4382d2SGreg Kroah-Hartman  *	for this port.
2948ab4382d2SGreg Kroah-Hartman  */
2949ab4382d2SGreg Kroah-Hartman int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2950ab4382d2SGreg Kroah-Hartman {
2951ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2952ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
29534047b371SPeter Hurley 	struct uart_port *uart_port;
29544c6d5b4dSGeert Uytterhoeven 	struct tty_struct *tty;
2955b342dd51SChen Gang 	int ret = 0;
2956ab4382d2SGreg Kroah-Hartman 
2957ab4382d2SGreg Kroah-Hartman 	BUG_ON(in_interrupt());
2958ab4382d2SGreg Kroah-Hartman 
2959ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port_mutex);
2960ab4382d2SGreg Kroah-Hartman 
2961ab4382d2SGreg Kroah-Hartman 	/*
2962ab4382d2SGreg Kroah-Hartman 	 * Mark the port "dead" - this prevents any opens from
2963ab4382d2SGreg Kroah-Hartman 	 * succeeding while we shut down the port.
2964ab4382d2SGreg Kroah-Hartman 	 */
2965ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
29664047b371SPeter Hurley 	uart_port = uart_port_check(state);
29674047b371SPeter Hurley 	if (uart_port != uport)
29684047b371SPeter Hurley 		dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
29694047b371SPeter Hurley 			  uart_port, uport);
29704047b371SPeter Hurley 
29714047b371SPeter Hurley 	if (!uart_port) {
2972b342dd51SChen Gang 		mutex_unlock(&port->mutex);
2973b342dd51SChen Gang 		ret = -EINVAL;
2974b342dd51SChen Gang 		goto out;
2975b342dd51SChen Gang 	}
2976ab4382d2SGreg Kroah-Hartman 	uport->flags |= UPF_DEAD;
2977ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
2978ab4382d2SGreg Kroah-Hartman 
2979ab4382d2SGreg Kroah-Hartman 	/*
2980ab4382d2SGreg Kroah-Hartman 	 * Remove the devices from the tty layer
2981ab4382d2SGreg Kroah-Hartman 	 */
2982da4c2799SJohan Hovold 	tty_port_unregister_device(port, drv->tty_driver, uport->line);
2983ab4382d2SGreg Kroah-Hartman 
29844c6d5b4dSGeert Uytterhoeven 	tty = tty_port_tty_get(port);
29854c6d5b4dSGeert Uytterhoeven 	if (tty) {
2986ab4382d2SGreg Kroah-Hartman 		tty_vhangup(port->tty);
29874c6d5b4dSGeert Uytterhoeven 		tty_kref_put(tty);
29884c6d5b4dSGeert Uytterhoeven 	}
2989ab4382d2SGreg Kroah-Hartman 
2990ab4382d2SGreg Kroah-Hartman 	/*
29915f5c9ae5SGeert Uytterhoeven 	 * If the port is used as a console, unregister it
29925f5c9ae5SGeert Uytterhoeven 	 */
29935f5c9ae5SGeert Uytterhoeven 	if (uart_console(uport))
29945f5c9ae5SGeert Uytterhoeven 		unregister_console(uport->cons);
29955f5c9ae5SGeert Uytterhoeven 
29965f5c9ae5SGeert Uytterhoeven 	/*
2997ab4382d2SGreg Kroah-Hartman 	 * Free the port IO and memory resources, if any.
2998ab4382d2SGreg Kroah-Hartman 	 */
2999a7cfaf16SFabio Estevam 	if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3000ab4382d2SGreg Kroah-Hartman 		uport->ops->release_port(uport);
3001266dcff0SGreg Kroah-Hartman 	kfree(uport->tty_groups);
3002f7048b15SVignesh R 	kfree(uport->name);
3003ab4382d2SGreg Kroah-Hartman 
3004ab4382d2SGreg Kroah-Hartman 	/*
3005ab4382d2SGreg Kroah-Hartman 	 * Indicate that there isn't a port here anymore.
3006ab4382d2SGreg Kroah-Hartman 	 */
3007ab4382d2SGreg Kroah-Hartman 	uport->type = PORT_UNKNOWN;
3008ab4382d2SGreg Kroah-Hartman 
30094047b371SPeter Hurley 	mutex_lock(&port->mutex);
30109ed19428SPeter Hurley 	WARN_ON(atomic_dec_return(&state->refcount) < 0);
30119ed19428SPeter Hurley 	wait_event(state->remove_wait, !atomic_read(&state->refcount));
3012ab4382d2SGreg Kroah-Hartman 	state->uart_port = NULL;
30134047b371SPeter Hurley 	mutex_unlock(&port->mutex);
3014b342dd51SChen Gang out:
3015ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port_mutex);
3016ab4382d2SGreg Kroah-Hartman 
3017b342dd51SChen Gang 	return ret;
3018ab4382d2SGreg Kroah-Hartman }
3019ab4382d2SGreg Kroah-Hartman 
3020ab4382d2SGreg Kroah-Hartman /*
3021ab4382d2SGreg Kroah-Hartman  *	Are the two ports equivalent?
3022ab4382d2SGreg Kroah-Hartman  */
3023ab4382d2SGreg Kroah-Hartman int uart_match_port(struct uart_port *port1, struct uart_port *port2)
3024ab4382d2SGreg Kroah-Hartman {
3025ab4382d2SGreg Kroah-Hartman 	if (port1->iotype != port2->iotype)
3026ab4382d2SGreg Kroah-Hartman 		return 0;
3027ab4382d2SGreg Kroah-Hartman 
3028ab4382d2SGreg Kroah-Hartman 	switch (port1->iotype) {
3029ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
3030ab4382d2SGreg Kroah-Hartman 		return (port1->iobase == port2->iobase);
3031ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
3032ab4382d2SGreg Kroah-Hartman 		return (port1->iobase == port2->iobase) &&
3033ab4382d2SGreg Kroah-Hartman 		       (port1->hub6   == port2->hub6);
3034ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
3035bd94c407SMasahiro Yamada 	case UPIO_MEM16:
3036ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
30373ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
3038ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
3039ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
3040ab4382d2SGreg Kroah-Hartman 		return (port1->mapbase == port2->mapbase);
3041ab4382d2SGreg Kroah-Hartman 	}
3042ab4382d2SGreg Kroah-Hartman 	return 0;
3043ab4382d2SGreg Kroah-Hartman }
3044ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port);
3045ab4382d2SGreg Kroah-Hartman 
3046027d7dacSJiri Slaby /**
3047027d7dacSJiri Slaby  *	uart_handle_dcd_change - handle a change of carrier detect state
3048027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
3049027d7dacSJiri Slaby  *	@status: new carrier detect status, nonzero if active
30504d90bb14SPeter Hurley  *
30514d90bb14SPeter Hurley  *	Caller must hold uport->lock
3052027d7dacSJiri Slaby  */
3053027d7dacSJiri Slaby void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
3054027d7dacSJiri Slaby {
305542381572SGeorge Spelvin 	struct tty_port *port = &uport->state->port;
305643eca0aeSAlan Cox 	struct tty_struct *tty = port->tty;
3057c993257bSPeter Hurley 	struct tty_ldisc *ld;
3058027d7dacSJiri Slaby 
30594d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
30604d90bb14SPeter Hurley 
3061c993257bSPeter Hurley 	if (tty) {
3062c993257bSPeter Hurley 		ld = tty_ldisc_ref(tty);
306342381572SGeorge Spelvin 		if (ld) {
306442381572SGeorge Spelvin 			if (ld->ops->dcd_change)
3065593fb1aeSGeorge Spelvin 				ld->ops->dcd_change(tty, status);
306642381572SGeorge Spelvin 			tty_ldisc_deref(ld);
306742381572SGeorge Spelvin 		}
3068c993257bSPeter Hurley 	}
3069027d7dacSJiri Slaby 
3070027d7dacSJiri Slaby 	uport->icount.dcd++;
3071027d7dacSJiri Slaby 
3072299245a1SPeter Hurley 	if (uart_dcd_enabled(uport)) {
3073027d7dacSJiri Slaby 		if (status)
3074027d7dacSJiri Slaby 			wake_up_interruptible(&port->open_wait);
307543eca0aeSAlan Cox 		else if (tty)
307643eca0aeSAlan Cox 			tty_hangup(tty);
3077027d7dacSJiri Slaby 	}
3078027d7dacSJiri Slaby }
3079027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3080027d7dacSJiri Slaby 
3081027d7dacSJiri Slaby /**
3082027d7dacSJiri Slaby  *	uart_handle_cts_change - handle a change of clear-to-send state
3083027d7dacSJiri Slaby  *	@uport: uart_port structure for the open port
3084027d7dacSJiri Slaby  *	@status: new clear to send status, nonzero if active
30854d90bb14SPeter Hurley  *
30864d90bb14SPeter Hurley  *	Caller must hold uport->lock
3087027d7dacSJiri Slaby  */
3088027d7dacSJiri Slaby void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3089027d7dacSJiri Slaby {
30904d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
30914d90bb14SPeter Hurley 
3092027d7dacSJiri Slaby 	uport->icount.cts++;
3093027d7dacSJiri Slaby 
3094391f93f2SPeter Hurley 	if (uart_softcts_mode(uport)) {
3095d01f4d18SPeter Hurley 		if (uport->hw_stopped) {
3096027d7dacSJiri Slaby 			if (status) {
3097d01f4d18SPeter Hurley 				uport->hw_stopped = 0;
3098027d7dacSJiri Slaby 				uport->ops->start_tx(uport);
3099027d7dacSJiri Slaby 				uart_write_wakeup(uport);
3100027d7dacSJiri Slaby 			}
3101027d7dacSJiri Slaby 		} else {
3102027d7dacSJiri Slaby 			if (!status) {
3103d01f4d18SPeter Hurley 				uport->hw_stopped = 1;
3104027d7dacSJiri Slaby 				uport->ops->stop_tx(uport);
3105027d7dacSJiri Slaby 			}
3106027d7dacSJiri Slaby 		}
3107391f93f2SPeter Hurley 
3108027d7dacSJiri Slaby 	}
3109027d7dacSJiri Slaby }
3110027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3111027d7dacSJiri Slaby 
3112cf75525fSJiri Slaby /**
3113cf75525fSJiri Slaby  * uart_insert_char - push a char to the uart layer
3114cf75525fSJiri Slaby  *
3115cf75525fSJiri Slaby  * User is responsible to call tty_flip_buffer_push when they are done with
3116cf75525fSJiri Slaby  * insertion.
3117cf75525fSJiri Slaby  *
3118cf75525fSJiri Slaby  * @port: corresponding port
3119cf75525fSJiri Slaby  * @status: state of the serial port RX buffer (LSR for 8250)
3120cf75525fSJiri Slaby  * @overrun: mask of overrun bits in @status
3121cf75525fSJiri Slaby  * @ch: character to push
3122cf75525fSJiri Slaby  * @flag: flag for the character (see TTY_NORMAL and friends)
3123cf75525fSJiri Slaby  */
3124027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status,
3125027d7dacSJiri Slaby 		 unsigned int overrun, unsigned int ch, unsigned int flag)
3126027d7dacSJiri Slaby {
312792a19f9cSJiri Slaby 	struct tty_port *tport = &port->state->port;
3128027d7dacSJiri Slaby 
3129027d7dacSJiri Slaby 	if ((status & port->ignore_status_mask & ~overrun) == 0)
313092a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, ch, flag) == 0)
3131dabfb351SCorbin 			++port->icount.buf_overrun;
3132027d7dacSJiri Slaby 
3133027d7dacSJiri Slaby 	/*
3134027d7dacSJiri Slaby 	 * Overrun is special.  Since it's reported immediately,
3135027d7dacSJiri Slaby 	 * it doesn't affect the current character.
3136027d7dacSJiri Slaby 	 */
3137027d7dacSJiri Slaby 	if (status & ~port->ignore_status_mask & overrun)
313892a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3139dabfb351SCorbin 			++port->icount.buf_overrun;
3140027d7dacSJiri Slaby }
3141027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char);
3142027d7dacSJiri Slaby 
314368af4317SDmitry Safonov #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
314468af4317SDmitry Safonov static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
314568af4317SDmitry Safonov 
314668af4317SDmitry Safonov static void uart_sysrq_on(struct work_struct *w)
314768af4317SDmitry Safonov {
3148b18896ffSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3149b18896ffSAndy Shevchenko 
315068af4317SDmitry Safonov 	sysrq_toggle_support(1);
3151b18896ffSAndy Shevchenko 	pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3152b18896ffSAndy Shevchenko 		sysrq_toggle_seq_len, sysrq_toggle_seq);
315368af4317SDmitry Safonov }
315468af4317SDmitry Safonov static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
315568af4317SDmitry Safonov 
315668af4317SDmitry Safonov /**
315768af4317SDmitry Safonov  *	uart_try_toggle_sysrq - Enables SysRq from serial line
315868af4317SDmitry Safonov  *	@port: uart_port structure where char(s) after BREAK met
315968af4317SDmitry Safonov  *	@ch: new character in the sequence after received BREAK
316068af4317SDmitry Safonov  *
316168af4317SDmitry Safonov  *	Enables magic SysRq when the required sequence is met on port
316268af4317SDmitry Safonov  *	(see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
316368af4317SDmitry Safonov  *
316468af4317SDmitry Safonov  *	Returns false if @ch is out of enabling sequence and should be
316568af4317SDmitry Safonov  *	handled some other way, true if @ch was consumed.
316668af4317SDmitry Safonov  */
316768af4317SDmitry Safonov static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
316868af4317SDmitry Safonov {
31692ce5eaceSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
31702ce5eaceSAndy Shevchenko 
31712ce5eaceSAndy Shevchenko 	if (!sysrq_toggle_seq_len)
317268af4317SDmitry Safonov 		return false;
317368af4317SDmitry Safonov 
317468af4317SDmitry Safonov 	BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
317568af4317SDmitry Safonov 	if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
317668af4317SDmitry Safonov 		port->sysrq_seq = 0;
317768af4317SDmitry Safonov 		return false;
317868af4317SDmitry Safonov 	}
317968af4317SDmitry Safonov 
31802ce5eaceSAndy Shevchenko 	if (++port->sysrq_seq < sysrq_toggle_seq_len) {
318168af4317SDmitry Safonov 		port->sysrq = jiffies + SYSRQ_TIMEOUT;
318268af4317SDmitry Safonov 		return true;
318368af4317SDmitry Safonov 	}
318468af4317SDmitry Safonov 
318568af4317SDmitry Safonov 	schedule_work(&sysrq_enable_work);
318668af4317SDmitry Safonov 
318768af4317SDmitry Safonov 	port->sysrq = 0;
318868af4317SDmitry Safonov 	return true;
318968af4317SDmitry Safonov }
319068af4317SDmitry Safonov #else
319168af4317SDmitry Safonov static inline bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
319268af4317SDmitry Safonov {
319368af4317SDmitry Safonov 	return false;
319468af4317SDmitry Safonov }
319568af4317SDmitry Safonov #endif
319668af4317SDmitry Safonov 
31978e20fc39SDmitry Safonov int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
31988e20fc39SDmitry Safonov {
31998e20fc39SDmitry Safonov 	if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL))
32008e20fc39SDmitry Safonov 		return 0;
32018e20fc39SDmitry Safonov 
32028e20fc39SDmitry Safonov 	if (!port->has_sysrq || !port->sysrq)
32038e20fc39SDmitry Safonov 		return 0;
32048e20fc39SDmitry Safonov 
32058e20fc39SDmitry Safonov 	if (ch && time_before(jiffies, port->sysrq)) {
320668af4317SDmitry Safonov 		if (sysrq_mask()) {
32078e20fc39SDmitry Safonov 			handle_sysrq(ch);
32088e20fc39SDmitry Safonov 			port->sysrq = 0;
32098e20fc39SDmitry Safonov 			return 1;
32108e20fc39SDmitry Safonov 		}
321168af4317SDmitry Safonov 		if (uart_try_toggle_sysrq(port, ch))
321268af4317SDmitry Safonov 			return 1;
321368af4317SDmitry Safonov 	}
32148e20fc39SDmitry Safonov 	port->sysrq = 0;
32158e20fc39SDmitry Safonov 
32168e20fc39SDmitry Safonov 	return 0;
32178e20fc39SDmitry Safonov }
32188e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_handle_sysrq_char);
32198e20fc39SDmitry Safonov 
32208e20fc39SDmitry Safonov int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch)
32218e20fc39SDmitry Safonov {
32228e20fc39SDmitry Safonov 	if (!IS_ENABLED(CONFIG_MAGIC_SYSRQ_SERIAL))
32238e20fc39SDmitry Safonov 		return 0;
32248e20fc39SDmitry Safonov 
32258e20fc39SDmitry Safonov 	if (!port->has_sysrq || !port->sysrq)
32268e20fc39SDmitry Safonov 		return 0;
32278e20fc39SDmitry Safonov 
32288e20fc39SDmitry Safonov 	if (ch && time_before(jiffies, port->sysrq)) {
322968af4317SDmitry Safonov 		if (sysrq_mask()) {
32308e20fc39SDmitry Safonov 			port->sysrq_ch = ch;
32318e20fc39SDmitry Safonov 			port->sysrq = 0;
32328e20fc39SDmitry Safonov 			return 1;
32338e20fc39SDmitry Safonov 		}
323468af4317SDmitry Safonov 		if (uart_try_toggle_sysrq(port, ch))
323568af4317SDmitry Safonov 			return 1;
323668af4317SDmitry Safonov 	}
32378e20fc39SDmitry Safonov 	port->sysrq = 0;
32388e20fc39SDmitry Safonov 
32398e20fc39SDmitry Safonov 	return 0;
32408e20fc39SDmitry Safonov }
32418e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_prepare_sysrq_char);
32428e20fc39SDmitry Safonov 
3243da9a5aa3SAndy Shevchenko void uart_unlock_and_check_sysrq(struct uart_port *port, unsigned long flags)
3244da9a5aa3SAndy Shevchenko __releases(&port->lock)
32458e20fc39SDmitry Safonov {
3246da9a5aa3SAndy Shevchenko 	if (port->has_sysrq) {
3247da9a5aa3SAndy Shevchenko 		int sysrq_ch = port->sysrq_ch;
32488e20fc39SDmitry Safonov 
32498e20fc39SDmitry Safonov 		port->sysrq_ch = 0;
3250da9a5aa3SAndy Shevchenko 		spin_unlock_irqrestore(&port->lock, flags);
32518e20fc39SDmitry Safonov 		if (sysrq_ch)
32528e20fc39SDmitry Safonov 			handle_sysrq(sysrq_ch);
3253da9a5aa3SAndy Shevchenko 	} else {
3254da9a5aa3SAndy Shevchenko 		spin_unlock_irqrestore(&port->lock, flags);
3255da9a5aa3SAndy Shevchenko 	}
32568e20fc39SDmitry Safonov }
32578e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_unlock_and_check_sysrq);
32588e20fc39SDmitry Safonov 
32598e20fc39SDmitry Safonov /*
32608e20fc39SDmitry Safonov  * We do the SysRQ and SAK checking like this...
32618e20fc39SDmitry Safonov  */
32628e20fc39SDmitry Safonov int uart_handle_break(struct uart_port *port)
32638e20fc39SDmitry Safonov {
32648e20fc39SDmitry Safonov 	struct uart_state *state = port->state;
32658e20fc39SDmitry Safonov 
32668e20fc39SDmitry Safonov 	if (port->handle_break)
32678e20fc39SDmitry Safonov 		port->handle_break(port);
32688e20fc39SDmitry Safonov 
3269e140ef36SAndy Shevchenko 	if (port->has_sysrq && uart_console(port)) {
32708e20fc39SDmitry Safonov 		if (!port->sysrq) {
327168af4317SDmitry Safonov 			port->sysrq = jiffies + SYSRQ_TIMEOUT;
32728e20fc39SDmitry Safonov 			return 1;
32738e20fc39SDmitry Safonov 		}
32748e20fc39SDmitry Safonov 		port->sysrq = 0;
32758e20fc39SDmitry Safonov 	}
32768e20fc39SDmitry Safonov 
32778e20fc39SDmitry Safonov 	if (port->flags & UPF_SAK)
32788e20fc39SDmitry Safonov 		do_SAK(state->port.tty);
32798e20fc39SDmitry Safonov 	return 0;
32808e20fc39SDmitry Safonov }
32818e20fc39SDmitry Safonov EXPORT_SYMBOL_GPL(uart_handle_break);
32828e20fc39SDmitry Safonov 
3283ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_write_wakeup);
3284ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_register_driver);
3285ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_unregister_driver);
3286ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_suspend_port);
3287ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_resume_port);
3288ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_add_one_port);
3289ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_remove_one_port);
3290ab4382d2SGreg Kroah-Hartman 
3291ef838a81SUwe Kleine-König /**
3292743f93f8SLukas Wunner  * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3293743f93f8SLukas Wunner  * @dev: uart device
3294ef838a81SUwe Kleine-König  * @rs485conf: output parameter
3295ef838a81SUwe Kleine-König  *
3296ef838a81SUwe Kleine-König  * This function implements the device tree binding described in
3297ef838a81SUwe Kleine-König  * Documentation/devicetree/bindings/serial/rs485.txt.
3298ef838a81SUwe Kleine-König  */
3299c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port)
3300ef838a81SUwe Kleine-König {
3301c150c0f3SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
3302c150c0f3SLukas Wunner 	struct device *dev = port->dev;
3303ef838a81SUwe Kleine-König 	u32 rs485_delay[2];
3304ef838a81SUwe Kleine-König 	int ret;
3305ef838a81SUwe Kleine-König 
3306743f93f8SLukas Wunner 	ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3307743f93f8SLukas Wunner 					     rs485_delay, 2);
3308ef838a81SUwe Kleine-König 	if (!ret) {
3309ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = rs485_delay[0];
3310ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = rs485_delay[1];
3311ef838a81SUwe Kleine-König 	} else {
3312ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = 0;
3313ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = 0;
3314ef838a81SUwe Kleine-König 	}
3315ef838a81SUwe Kleine-König 
3316ef838a81SUwe Kleine-König 	/*
3317f1e5b618SLukas Wunner 	 * Clear full-duplex and enabled flags, set RTS polarity to active high
3318f1e5b618SLukas Wunner 	 * to get to a defined state with the following properties:
3319ef838a81SUwe Kleine-König 	 */
3320f1e5b618SLukas Wunner 	rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3321d58a2df3SLukas Wunner 			      SER_RS485_TERMINATE_BUS |
3322f1e5b618SLukas Wunner 			      SER_RS485_RTS_AFTER_SEND);
3323f1e5b618SLukas Wunner 	rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3324ef838a81SUwe Kleine-König 
3325743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3326ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
3327ef838a81SUwe Kleine-König 
3328743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3329ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_ENABLED;
3330f1e5b618SLukas Wunner 
3331f1e5b618SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3332f1e5b618SLukas Wunner 		rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3333f1e5b618SLukas Wunner 		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3334f1e5b618SLukas Wunner 	}
3335c150c0f3SLukas Wunner 
3336d58a2df3SLukas Wunner 	/*
3337d58a2df3SLukas Wunner 	 * Disabling termination by default is the safe choice:  Else if many
3338d58a2df3SLukas Wunner 	 * bus participants enable it, no communication is possible at all.
3339d58a2df3SLukas Wunner 	 * Works fine for short cables and users may enable for longer cables.
3340d58a2df3SLukas Wunner 	 */
3341d58a2df3SLukas Wunner 	port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3342d58a2df3SLukas Wunner 							GPIOD_OUT_LOW);
3343d58a2df3SLukas Wunner 	if (IS_ERR(port->rs485_term_gpio)) {
3344d58a2df3SLukas Wunner 		ret = PTR_ERR(port->rs485_term_gpio);
3345d58a2df3SLukas Wunner 		port->rs485_term_gpio = NULL;
3346d58a2df3SLukas Wunner 		if (ret != -EPROBE_DEFER)
3347d58a2df3SLukas Wunner 			dev_err(dev, "Cannot get rs485-term-gpios\n");
3348d58a2df3SLukas Wunner 		return ret;
3349d58a2df3SLukas Wunner 	}
3350d58a2df3SLukas Wunner 
3351c150c0f3SLukas Wunner 	return 0;
3352ef838a81SUwe Kleine-König }
3353743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3354ef838a81SUwe Kleine-König 
3355ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core");
3356ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
3357