xref: /linux/drivers/tty/serial/serial_core.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
3ab4382d2SGreg Kroah-Hartman  *  Driver core for serial ports
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6ab4382d2SGreg Kroah-Hartman  *
7ab4382d2SGreg Kroah-Hartman  *  Copyright 1999 ARM Limited
8ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9ab4382d2SGreg Kroah-Hartman  */
10ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
12027d7dacSJiri Slaby #include <linux/tty_flip.h>
13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
14174cd4b1SIngo Molnar #include <linux/sched/signal.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
17167cbce2SJohan Hovold #include <linux/gpio/consumer.h>
18eff37b5eSIlpo Järvinen #include <linux/kernel.h>
19a208ffd2SGrant Likely #include <linux/of.h>
2084a9582fSTony Lindgren #include <linux/pm_runtime.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/proc_fs.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/seq_file.h>
23ab4382d2SGreg Kroah-Hartman #include <linux/device.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
25ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
2668af4317SDmitry Safonov #include <linux/sysrq.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/mutex.h>
2931f6bd7fSIlpo Järvinen #include <linux/math64.h>
30794edf30SDavid Howells #include <linux/security.h>
31ab4382d2SGreg Kroah-Hartman 
32aef3ad10SAndy Shevchenko #include <linux/irq.h>
337c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
34ab4382d2SGreg Kroah-Hartman 
3584a9582fSTony Lindgren #include "serial_base.h"
3684a9582fSTony Lindgren 
37ab4382d2SGreg Kroah-Hartman /*
38ab4382d2SGreg Kroah-Hartman  * This is used to lock changes in serial line configuration.
39ab4382d2SGreg Kroah-Hartman  */
40ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(port_mutex);
41ab4382d2SGreg Kroah-Hartman 
42ab4382d2SGreg Kroah-Hartman /*
43ab4382d2SGreg Kroah-Hartman  * lockdep: port->lock is initialized in two places, but we
44ab4382d2SGreg Kroah-Hartman  *          want only one lock-class:
45ab4382d2SGreg Kroah-Hartman  */
46ab4382d2SGreg Kroah-Hartman static struct lock_class_key port_lock_key;
47ab4382d2SGreg Kroah-Hartman 
48ab4382d2SGreg Kroah-Hartman #define HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
49ab4382d2SGreg Kroah-Hartman 
500ed12afaSLino Sanfilippo /*
510ed12afaSLino Sanfilippo  * Max time with active RTS before/after data is sent.
520ed12afaSLino Sanfilippo  */
530ed12afaSLino Sanfilippo #define RS485_MAX_RTS_DELAY	100 /* msecs */
540ed12afaSLino Sanfilippo 
556f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
566f538fe3SLinus Walleij 			   enum uart_pm_state pm_state);
57ab4382d2SGreg Kroah-Hartman 
58b922e19dSJiri Slaby static void uart_port_shutdown(struct tty_port *port);
59b922e19dSJiri Slaby 
uart_dcd_enabled(struct uart_port * uport)60299245a1SPeter Hurley static int uart_dcd_enabled(struct uart_port *uport)
61299245a1SPeter Hurley {
62d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_DCD_ENABLE);
63299245a1SPeter Hurley }
64299245a1SPeter Hurley 
uart_port_ref(struct uart_state * state)659ed19428SPeter Hurley static inline struct uart_port *uart_port_ref(struct uart_state *state)
669ed19428SPeter Hurley {
679ed19428SPeter Hurley 	if (atomic_add_unless(&state->refcount, 1, 0))
689ed19428SPeter Hurley 		return state->uart_port;
699ed19428SPeter Hurley 	return NULL;
709ed19428SPeter Hurley }
719ed19428SPeter Hurley 
uart_port_deref(struct uart_port * uport)729ed19428SPeter Hurley static inline void uart_port_deref(struct uart_port *uport)
739ed19428SPeter Hurley {
74ef510beaSAndy Shevchenko 	if (atomic_dec_and_test(&uport->state->refcount))
759ed19428SPeter Hurley 		wake_up(&uport->state->remove_wait);
769ed19428SPeter Hurley }
779ed19428SPeter Hurley 
789ed19428SPeter Hurley #define uart_port_lock(state, flags)					\
799ed19428SPeter Hurley 	({								\
809ed19428SPeter Hurley 		struct uart_port *__uport = uart_port_ref(state);	\
819ed19428SPeter Hurley 		if (__uport)						\
82559c7ff4SThomas Gleixner 			uart_port_lock_irqsave(__uport, &flags);	\
839ed19428SPeter Hurley 		__uport;						\
849ed19428SPeter Hurley 	})
859ed19428SPeter Hurley 
869ed19428SPeter Hurley #define uart_port_unlock(uport, flags)					\
879ed19428SPeter Hurley 	({								\
889ed19428SPeter Hurley 		struct uart_port *__uport = uport;			\
89ef510beaSAndy Shevchenko 		if (__uport) {						\
90559c7ff4SThomas Gleixner 			uart_port_unlock_irqrestore(__uport, flags);	\
919ed19428SPeter Hurley 			uart_port_deref(__uport);			\
92ef510beaSAndy Shevchenko 		}							\
939ed19428SPeter Hurley 	})
949ed19428SPeter Hurley 
uart_port_check(struct uart_state * state)954047b371SPeter Hurley static inline struct uart_port *uart_port_check(struct uart_state *state)
964047b371SPeter Hurley {
977da4b8b7SPeter Hurley 	lockdep_assert_held(&state->port.mutex);
984047b371SPeter Hurley 	return state->uart_port;
994047b371SPeter Hurley }
1004047b371SPeter Hurley 
101c4bd17a6SJiri Slaby /**
102c4bd17a6SJiri Slaby  * uart_write_wakeup - schedule write processing
103c4bd17a6SJiri Slaby  * @port: port to be processed
104c4bd17a6SJiri Slaby  *
105c4bd17a6SJiri Slaby  * This routine is used by the interrupt handler to schedule processing in the
106c4bd17a6SJiri Slaby  * software interrupt portion of the driver. A driver is expected to call this
107c4bd17a6SJiri Slaby  * function when the number of characters in the transmit buffer have dropped
108c4bd17a6SJiri Slaby  * below a threshold.
109c4bd17a6SJiri Slaby  *
110c4bd17a6SJiri Slaby  * Locking: @port->lock should be held
111ab4382d2SGreg Kroah-Hartman  */
uart_write_wakeup(struct uart_port * port)112ab4382d2SGreg Kroah-Hartman void uart_write_wakeup(struct uart_port *port)
113ab4382d2SGreg Kroah-Hartman {
114ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = port->state;
115ab4382d2SGreg Kroah-Hartman 	/*
116ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
117ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
118ab4382d2SGreg Kroah-Hartman 	 */
119ab4382d2SGreg Kroah-Hartman 	BUG_ON(!state);
120d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
121ab4382d2SGreg Kroah-Hartman }
12215dc475bSJiri Slaby EXPORT_SYMBOL(uart_write_wakeup);
123ab4382d2SGreg Kroah-Hartman 
uart_stop(struct tty_struct * tty)124ab4382d2SGreg Kroah-Hartman static void uart_stop(struct tty_struct *tty)
125ab4382d2SGreg Kroah-Hartman {
126ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1279ed19428SPeter Hurley 	struct uart_port *port;
128ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
129ab4382d2SGreg Kroah-Hartman 
1309ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
1319ed19428SPeter Hurley 	if (port)
132ab4382d2SGreg Kroah-Hartman 		port->ops->stop_tx(port);
1339ed19428SPeter Hurley 	uart_port_unlock(port, flags);
134ab4382d2SGreg Kroah-Hartman }
135ab4382d2SGreg Kroah-Hartman 
__uart_start(struct uart_state * state)1361225541cSJiri Slaby static void __uart_start(struct uart_state *state)
137ab4382d2SGreg Kroah-Hartman {
138ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = state->uart_port;
13984a9582fSTony Lindgren 	struct serial_port_device *port_dev;
14084a9582fSTony Lindgren 	int err;
141ab4382d2SGreg Kroah-Hartman 
14284a9582fSTony Lindgren 	if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port))
14384a9582fSTony Lindgren 		return;
14484a9582fSTony Lindgren 
14584a9582fSTony Lindgren 	port_dev = port->port_dev;
14684a9582fSTony Lindgren 
14784a9582fSTony Lindgren 	/* Increment the runtime PM usage count for the active check below */
14884a9582fSTony Lindgren 	err = pm_runtime_get(&port_dev->dev);
1496f699743STony Lindgren 	if (err < 0 && err != -EINPROGRESS) {
15084a9582fSTony Lindgren 		pm_runtime_put_noidle(&port_dev->dev);
15184a9582fSTony Lindgren 		return;
15284a9582fSTony Lindgren 	}
15384a9582fSTony Lindgren 
15484a9582fSTony Lindgren 	/*
15584a9582fSTony Lindgren 	 * Start TX if enabled, and kick runtime PM. If the device is not
15684a9582fSTony Lindgren 	 * enabled, serial_port_runtime_resume() calls start_tx() again
15784a9582fSTony Lindgren 	 * after enabling the device.
15884a9582fSTony Lindgren 	 */
15955559805STony Lindgren 	if (!pm_runtime_enabled(port->dev) || pm_runtime_active(&port_dev->dev))
160ab4382d2SGreg Kroah-Hartman 		port->ops->start_tx(port);
16184a9582fSTony Lindgren 	pm_runtime_mark_last_busy(&port_dev->dev);
16284a9582fSTony Lindgren 	pm_runtime_put_autosuspend(&port_dev->dev);
163ab4382d2SGreg Kroah-Hartman }
164ab4382d2SGreg Kroah-Hartman 
uart_start(struct tty_struct * tty)165ab4382d2SGreg Kroah-Hartman static void uart_start(struct tty_struct *tty)
166ab4382d2SGreg Kroah-Hartman {
167ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1689ed19428SPeter Hurley 	struct uart_port *port;
169ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
170ab4382d2SGreg Kroah-Hartman 
1719ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
1721225541cSJiri Slaby 	__uart_start(state);
1739ed19428SPeter Hurley 	uart_port_unlock(port, flags);
174ab4382d2SGreg Kroah-Hartman }
175ab4382d2SGreg Kroah-Hartman 
176f4581cabSDenys Vlasenko static void
uart_update_mctrl(struct uart_port * port,unsigned int set,unsigned int clear)177ab4382d2SGreg Kroah-Hartman uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
178ab4382d2SGreg Kroah-Hartman {
179ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
180ab4382d2SGreg Kroah-Hartman 	unsigned int old;
181ab4382d2SGreg Kroah-Hartman 
182559c7ff4SThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
183ab4382d2SGreg Kroah-Hartman 	old = port->mctrl;
184ab4382d2SGreg Kroah-Hartman 	port->mctrl = (old & ~clear) | set;
1857c7f9bc9SLukas Wunner 	if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
186ab4382d2SGreg Kroah-Hartman 		port->ops->set_mctrl(port, port->mctrl);
187559c7ff4SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
188ab4382d2SGreg Kroah-Hartman }
189ab4382d2SGreg Kroah-Hartman 
190ab4382d2SGreg Kroah-Hartman #define uart_set_mctrl(port, set)	uart_update_mctrl(port, set, 0)
191ab4382d2SGreg Kroah-Hartman #define uart_clear_mctrl(port, clear)	uart_update_mctrl(port, 0, clear)
192ab4382d2SGreg Kroah-Hartman 
uart_port_dtr_rts(struct uart_port * uport,bool active)1935701cb8bSIlpo Järvinen static void uart_port_dtr_rts(struct uart_port *uport, bool active)
194a6845e1eSRafael Gago {
1955701cb8bSIlpo Järvinen 	if (active)
196a6845e1eSRafael Gago 		uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1972dd8a74fSLukas Wunner 	else
1982dd8a74fSLukas Wunner 		uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
199a6845e1eSRafael Gago }
200a6845e1eSRafael Gago 
2018e90cf29SIlpo Järvinen /* Caller holds port mutex */
uart_change_line_settings(struct tty_struct * tty,struct uart_state * state,const struct ktermios * old_termios)202826736a6SIlpo Järvinen static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state,
2038e90cf29SIlpo Järvinen 				      const struct ktermios *old_termios)
2048e90cf29SIlpo Järvinen {
2058e90cf29SIlpo Järvinen 	struct uart_port *uport = uart_port_check(state);
2068e90cf29SIlpo Järvinen 	struct ktermios *termios;
2071690ca51SIlpo Järvinen 	bool old_hw_stopped;
2088e90cf29SIlpo Järvinen 
2098e90cf29SIlpo Järvinen 	/*
2108e90cf29SIlpo Järvinen 	 * If we have no tty, termios, or the port does not exist,
2118e90cf29SIlpo Järvinen 	 * then we can't set the parameters for this port.
2128e90cf29SIlpo Järvinen 	 */
2138e90cf29SIlpo Järvinen 	if (!tty || uport->type == PORT_UNKNOWN)
2148e90cf29SIlpo Järvinen 		return;
2158e90cf29SIlpo Järvinen 
2168e90cf29SIlpo Järvinen 	termios = &tty->termios;
2178e90cf29SIlpo Järvinen 	uport->ops->set_termios(uport, termios, old_termios);
2188e90cf29SIlpo Järvinen 
2198e90cf29SIlpo Järvinen 	/*
2208e90cf29SIlpo Järvinen 	 * Set modem status enables based on termios cflag
2218e90cf29SIlpo Järvinen 	 */
222559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
2238e90cf29SIlpo Järvinen 	if (termios->c_cflag & CRTSCTS)
2248e90cf29SIlpo Järvinen 		uport->status |= UPSTAT_CTS_ENABLE;
2258e90cf29SIlpo Järvinen 	else
2268e90cf29SIlpo Järvinen 		uport->status &= ~UPSTAT_CTS_ENABLE;
2278e90cf29SIlpo Järvinen 
2288e90cf29SIlpo Järvinen 	if (termios->c_cflag & CLOCAL)
2298e90cf29SIlpo Järvinen 		uport->status &= ~UPSTAT_DCD_ENABLE;
2308e90cf29SIlpo Järvinen 	else
2318e90cf29SIlpo Järvinen 		uport->status |= UPSTAT_DCD_ENABLE;
2328e90cf29SIlpo Järvinen 
2338e90cf29SIlpo Järvinen 	/* reset sw-assisted CTS flow control based on (possibly) new mode */
2341690ca51SIlpo Järvinen 	old_hw_stopped = uport->hw_stopped;
2358e90cf29SIlpo Järvinen 	uport->hw_stopped = uart_softcts_mode(uport) &&
2368e90cf29SIlpo Järvinen 			    !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
2371690ca51SIlpo Järvinen 	if (uport->hw_stopped != old_hw_stopped) {
2381690ca51SIlpo Järvinen 		if (!old_hw_stopped)
2398e90cf29SIlpo Järvinen 			uport->ops->stop_tx(uport);
2401690ca51SIlpo Järvinen 		else
2411225541cSJiri Slaby 			__uart_start(state);
2428e90cf29SIlpo Järvinen 	}
243559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
2448e90cf29SIlpo Järvinen }
2458e90cf29SIlpo Järvinen 
uart_alloc_xmit_buf(struct tty_port * port)246abcd8632SAndy Shevchenko static int uart_alloc_xmit_buf(struct tty_port *port)
247ab4382d2SGreg Kroah-Hartman {
248abcd8632SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
249abcd8632SAndy Shevchenko 	struct uart_port *uport;
25018ee37e1SJohan Hovold 	unsigned long flags;
251ab4382d2SGreg Kroah-Hartman 	unsigned long page;
2527deb39edSThomas Pfaff 
2537deb39edSThomas Pfaff 	/*
254ab4382d2SGreg Kroah-Hartman 	 * Initialise and allocate the transmit and temporary
255ab4382d2SGreg Kroah-Hartman 	 * buffer.
256ab4382d2SGreg Kroah-Hartman 	 */
257ab4382d2SGreg Kroah-Hartman 	page = get_zeroed_page(GFP_KERNEL);
258ab4382d2SGreg Kroah-Hartman 	if (!page)
259ab4382d2SGreg Kroah-Hartman 		return -ENOMEM;
260ab4382d2SGreg Kroah-Hartman 
261abcd8632SAndy Shevchenko 	uport = uart_port_lock(state, flags);
2621788cf6aSJiri Slaby (SUSE) 	if (!state->port.xmit_buf) {
2631788cf6aSJiri Slaby (SUSE) 		state->port.xmit_buf = (unsigned char *)page;
2641788cf6aSJiri Slaby (SUSE) 		kfifo_init(&state->port.xmit_fifo, state->port.xmit_buf,
2651788cf6aSJiri Slaby (SUSE) 				PAGE_SIZE);
266d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
267a5ba1d95STycho Andersen 	} else {
268d7240214SSergey Senozhatsky 		uart_port_unlock(uport, flags);
269d7240214SSergey Senozhatsky 		/*
270d7240214SSergey Senozhatsky 		 * Do not free() the page under the port lock, see
271abcd8632SAndy Shevchenko 		 * uart_free_xmit_buf().
272d7240214SSergey Senozhatsky 		 */
273a5ba1d95STycho Andersen 		free_page(page);
274ab4382d2SGreg Kroah-Hartman 	}
275ab4382d2SGreg Kroah-Hartman 
276abcd8632SAndy Shevchenko 	return 0;
277abcd8632SAndy Shevchenko }
278abcd8632SAndy Shevchenko 
uart_free_xmit_buf(struct tty_port * port)279abcd8632SAndy Shevchenko static void uart_free_xmit_buf(struct tty_port *port)
280abcd8632SAndy Shevchenko {
281abcd8632SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
282abcd8632SAndy Shevchenko 	struct uart_port *uport;
283abcd8632SAndy Shevchenko 	unsigned long flags;
284abcd8632SAndy Shevchenko 	char *xmit_buf;
285abcd8632SAndy Shevchenko 
286abcd8632SAndy Shevchenko 	/*
287abcd8632SAndy Shevchenko 	 * Do not free() the transmit buffer page under the port lock since
288abcd8632SAndy Shevchenko 	 * this can create various circular locking scenarios. For instance,
289abcd8632SAndy Shevchenko 	 * console driver may need to allocate/free a debug object, which
290abcd8632SAndy Shevchenko 	 * can end up in printk() recursion.
291abcd8632SAndy Shevchenko 	 */
292abcd8632SAndy Shevchenko 	uport = uart_port_lock(state, flags);
293abcd8632SAndy Shevchenko 	xmit_buf = port->xmit_buf;
294abcd8632SAndy Shevchenko 	port->xmit_buf = NULL;
295abcd8632SAndy Shevchenko 	INIT_KFIFO(port->xmit_fifo);
296abcd8632SAndy Shevchenko 	uart_port_unlock(uport, flags);
297abcd8632SAndy Shevchenko 
298abcd8632SAndy Shevchenko 	free_page((unsigned long)xmit_buf);
299abcd8632SAndy Shevchenko }
300abcd8632SAndy Shevchenko 
301abcd8632SAndy Shevchenko /*
302abcd8632SAndy Shevchenko  * Startup the port.  This will be called once per open.  All calls
303abcd8632SAndy Shevchenko  * will be serialised by the per-port mutex.
304abcd8632SAndy Shevchenko  */
uart_port_startup(struct tty_struct * tty,struct uart_state * state,bool init_hw)305abcd8632SAndy Shevchenko static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
306abcd8632SAndy Shevchenko 			     bool init_hw)
307abcd8632SAndy Shevchenko {
308abcd8632SAndy Shevchenko 	struct uart_port *uport = uart_port_check(state);
309abcd8632SAndy Shevchenko 	int retval;
310abcd8632SAndy Shevchenko 
311abcd8632SAndy Shevchenko 	if (uport->type == PORT_UNKNOWN)
312abcd8632SAndy Shevchenko 		return 1;
313abcd8632SAndy Shevchenko 
314abcd8632SAndy Shevchenko 	/*
315abcd8632SAndy Shevchenko 	 * Make sure the device is in D0 state.
316abcd8632SAndy Shevchenko 	 */
317abcd8632SAndy Shevchenko 	uart_change_pm(state, UART_PM_STATE_ON);
318abcd8632SAndy Shevchenko 
319abcd8632SAndy Shevchenko 	retval = uart_alloc_xmit_buf(&state->port);
320abcd8632SAndy Shevchenko 	if (retval)
321abcd8632SAndy Shevchenko 		return retval;
322abcd8632SAndy Shevchenko 
323ab4382d2SGreg Kroah-Hartman 	retval = uport->ops->startup(uport);
324ab4382d2SGreg Kroah-Hartman 	if (retval == 0) {
325c7d7abffSJiri Slaby 		if (uart_console(uport) && uport->cons->cflag) {
326adc8d746SAlan Cox 			tty->termios.c_cflag = uport->cons->cflag;
327027b5717SPali Rohár 			tty->termios.c_ispeed = uport->cons->ispeed;
328027b5717SPali Rohár 			tty->termios.c_ospeed = uport->cons->ospeed;
329c7d7abffSJiri Slaby 			uport->cons->cflag = 0;
330027b5717SPali Rohár 			uport->cons->ispeed = 0;
331027b5717SPali Rohár 			uport->cons->ospeed = 0;
332c7d7abffSJiri Slaby 		}
333ab4382d2SGreg Kroah-Hartman 		/*
334ab4382d2SGreg Kroah-Hartman 		 * Initialise the hardware port settings.
335ab4382d2SGreg Kroah-Hartman 		 */
336826736a6SIlpo Järvinen 		uart_change_line_settings(tty, state, NULL);
337ab4382d2SGreg Kroah-Hartman 
338ab4382d2SGreg Kroah-Hartman 		/*
339ab4382d2SGreg Kroah-Hartman 		 * Setup the RTS and DTR signals once the
340ab4382d2SGreg Kroah-Hartman 		 * port is open and ready to respond.
341ab4382d2SGreg Kroah-Hartman 		 */
3429db276f8SPeter Hurley 		if (init_hw && C_BAUD(tty))
3435d420399SIlpo Järvinen 			uart_port_dtr_rts(uport, true);
344ab4382d2SGreg Kroah-Hartman 	}
345ab4382d2SGreg Kroah-Hartman 
3460055197eSJiri Slaby 	/*
3470055197eSJiri Slaby 	 * This is to allow setserial on this port. People may want to set
3480055197eSJiri Slaby 	 * port/irq/type and then reconfigure the port properly if it failed
3490055197eSJiri Slaby 	 * now.
3500055197eSJiri Slaby 	 */
351ab4382d2SGreg Kroah-Hartman 	if (retval && capable(CAP_SYS_ADMIN))
352c0d92be6SJiri Slaby 		return 1;
353c0d92be6SJiri Slaby 
354c0d92be6SJiri Slaby 	return retval;
355c0d92be6SJiri Slaby }
356c0d92be6SJiri Slaby 
uart_startup(struct tty_struct * tty,struct uart_state * state,bool init_hw)357c0d92be6SJiri Slaby static int uart_startup(struct tty_struct *tty, struct uart_state *state,
358dcd794c6SIlpo Järvinen 			bool init_hw)
359c0d92be6SJiri Slaby {
360c0d92be6SJiri Slaby 	struct tty_port *port = &state->port;
3611aa4ad4eSTony Lindgren 	struct uart_port *uport;
362c0d92be6SJiri Slaby 	int retval;
363c0d92be6SJiri Slaby 
364d41861caSPeter Hurley 	if (tty_port_initialized(port))
3651aa4ad4eSTony Lindgren 		goto out_base_port_startup;
366c0d92be6SJiri Slaby 
367c0d92be6SJiri Slaby 	retval = uart_port_startup(tty, state, init_hw);
3681aa4ad4eSTony Lindgren 	if (retval) {
369b3b57646SRob Herring 		set_bit(TTY_IO_ERROR, &tty->flags);
370ab4382d2SGreg Kroah-Hartman 		return retval;
371ab4382d2SGreg Kroah-Hartman 	}
372ab4382d2SGreg Kroah-Hartman 
3731aa4ad4eSTony Lindgren out_base_port_startup:
3741aa4ad4eSTony Lindgren 	uport = uart_port_check(state);
3751aa4ad4eSTony Lindgren 	if (!uport)
3761aa4ad4eSTony Lindgren 		return -EIO;
3771aa4ad4eSTony Lindgren 
3781aa4ad4eSTony Lindgren 	serial_base_port_startup(uport);
3791aa4ad4eSTony Lindgren 
3801aa4ad4eSTony Lindgren 	return 0;
3811aa4ad4eSTony Lindgren }
3821aa4ad4eSTony Lindgren 
383ab4382d2SGreg Kroah-Hartman /*
384ab4382d2SGreg Kroah-Hartman  * This routine will shutdown a serial port; interrupts are disabled, and
385ab4382d2SGreg Kroah-Hartman  * DTR is dropped if the hangup on close termio flag is on.  Calls to
386ab4382d2SGreg Kroah-Hartman  * uart_shutdown are serialised by the per-port semaphore.
387af224ca2SPeter Hurley  *
388af224ca2SPeter Hurley  * uport == NULL if uart_port has already been removed
389ab4382d2SGreg Kroah-Hartman  */
uart_shutdown(struct tty_struct * tty,struct uart_state * state)390ab4382d2SGreg Kroah-Hartman static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
391ab4382d2SGreg Kroah-Hartman {
3924047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
393ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
394ab4382d2SGreg Kroah-Hartman 
395ab4382d2SGreg Kroah-Hartman 	/*
396ab4382d2SGreg Kroah-Hartman 	 * Set the TTY IO error marker
397ab4382d2SGreg Kroah-Hartman 	 */
398ab4382d2SGreg Kroah-Hartman 	if (tty)
399ab4382d2SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
400ab4382d2SGreg Kroah-Hartman 
4011aa4ad4eSTony Lindgren 	if (uport)
4021aa4ad4eSTony Lindgren 		serial_base_port_shutdown(uport);
4031aa4ad4eSTony Lindgren 
404d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
405515be7baSIlpo Järvinen 		tty_port_set_initialized(port, false);
406d41861caSPeter Hurley 
407ab4382d2SGreg Kroah-Hartman 		/*
408ab4382d2SGreg Kroah-Hartman 		 * Turn off DTR and RTS early.
409ab4382d2SGreg Kroah-Hartman 		 */
410602babaaSJiri Slaby (SUSE) 		if (uport) {
411602babaaSJiri Slaby (SUSE) 			if (uart_console(uport) && tty) {
412ae84db96SPeter Hurley 				uport->cons->cflag = tty->termios.c_cflag;
413027b5717SPali Rohár 				uport->cons->ispeed = tty->termios.c_ispeed;
414027b5717SPali Rohár 				uport->cons->ospeed = tty->termios.c_ospeed;
415027b5717SPali Rohár 			}
416ae84db96SPeter Hurley 
4179db276f8SPeter Hurley 			if (!tty || C_HUPCL(tty))
4185d420399SIlpo Järvinen 				uart_port_dtr_rts(uport, false);
419602babaaSJiri Slaby (SUSE) 		}
420ab4382d2SGreg Kroah-Hartman 
421b922e19dSJiri Slaby 		uart_port_shutdown(port);
422ab4382d2SGreg Kroah-Hartman 	}
423ab4382d2SGreg Kroah-Hartman 
424ab4382d2SGreg Kroah-Hartman 	/*
425d208a3bfSDoug Anderson 	 * It's possible for shutdown to be called after suspend if we get
426d208a3bfSDoug Anderson 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
427d208a3bfSDoug Anderson 	 * we don't try to resume a port that has been shutdown.
428ab4382d2SGreg Kroah-Hartman 	 */
42975b20a2aSIlpo Järvinen 	tty_port_set_suspended(port, false);
430ab4382d2SGreg Kroah-Hartman 
431abcd8632SAndy Shevchenko 	uart_free_xmit_buf(port);
432ab4382d2SGreg Kroah-Hartman }
433ab4382d2SGreg Kroah-Hartman 
434ab4382d2SGreg Kroah-Hartman /**
435c4bd17a6SJiri Slaby  * uart_update_timeout - update per-port frame timing information
436ab4382d2SGreg Kroah-Hartman  * @port: uart_port structure describing the port
437ab4382d2SGreg Kroah-Hartman  * @cflag: termios cflag value
438ab4382d2SGreg Kroah-Hartman  * @baud: speed of the port
439ab4382d2SGreg Kroah-Hartman  *
440c4bd17a6SJiri Slaby  * Set the @port frame timing information from which the FIFO timeout value is
441c4bd17a6SJiri Slaby  * derived. The @cflag value should reflect the actual hardware settings as
442c4bd17a6SJiri Slaby  * number of bits, parity, stop bits and baud rate is taken into account here.
443c4bd17a6SJiri Slaby  *
444c4bd17a6SJiri Slaby  * Locking: caller is expected to take @port->lock
445ab4382d2SGreg Kroah-Hartman  */
446ab4382d2SGreg Kroah-Hartman void
uart_update_timeout(struct uart_port * port,unsigned int cflag,unsigned int baud)447ab4382d2SGreg Kroah-Hartman uart_update_timeout(struct uart_port *port, unsigned int cflag,
448ab4382d2SGreg Kroah-Hartman 		    unsigned int baud)
449ab4382d2SGreg Kroah-Hartman {
450d4303e0bSVamshi Gajjela 	u64 temp = tty_get_frame_size(cflag);
451ab4382d2SGreg Kroah-Hartman 
452d4303e0bSVamshi Gajjela 	temp *= NSEC_PER_SEC;
453d4303e0bSVamshi Gajjela 	port->frame_time = (unsigned int)DIV64_U64_ROUND_UP(temp, baud);
454ab4382d2SGreg Kroah-Hartman }
455ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_update_timeout);
456ab4382d2SGreg Kroah-Hartman 
457ab4382d2SGreg Kroah-Hartman /**
458ab4382d2SGreg Kroah-Hartman  * uart_get_baud_rate - return baud rate for a particular port
459ab4382d2SGreg Kroah-Hartman  * @port: uart_port structure describing the port in question.
460c4bd17a6SJiri Slaby  * @termios: desired termios settings
461c4bd17a6SJiri Slaby  * @old: old termios (or %NULL)
462ab4382d2SGreg Kroah-Hartman  * @min: minimum acceptable baud rate
463ab4382d2SGreg Kroah-Hartman  * @max: maximum acceptable baud rate
464ab4382d2SGreg Kroah-Hartman  *
465c4bd17a6SJiri Slaby  * Decode the termios structure into a numeric baud rate, taking account of the
466c4bd17a6SJiri Slaby  * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
467c4bd17a6SJiri Slaby  * baud.
468ab4382d2SGreg Kroah-Hartman  *
469c4bd17a6SJiri Slaby  * If the new baud rate is invalid, try the @old termios setting. If it's still
47023bf72faSMax Filippov  * invalid, we try 9600 baud. If that is also invalid 0 is returned.
471ab4382d2SGreg Kroah-Hartman  *
472c4bd17a6SJiri Slaby  * The @termios structure is updated to reflect the baud rate we're actually
473c4bd17a6SJiri Slaby  * going to be using. Don't do this for the case where B0 is requested ("hang
474c4bd17a6SJiri Slaby  * up").
475c4bd17a6SJiri Slaby  *
476c4bd17a6SJiri Slaby  * Locking: caller dependent
477ab4382d2SGreg Kroah-Hartman  */
478ab4382d2SGreg Kroah-Hartman unsigned int
uart_get_baud_rate(struct uart_port * port,struct ktermios * termios,const struct ktermios * old,unsigned int min,unsigned int max)479ab4382d2SGreg Kroah-Hartman uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
480bec5b814SIlpo Järvinen 		   const struct ktermios *old, unsigned int min, unsigned int max)
481ab4382d2SGreg Kroah-Hartman {
482f10a2233SJoakim Nordell 	unsigned int try;
483f10a2233SJoakim Nordell 	unsigned int baud;
484f10a2233SJoakim Nordell 	unsigned int altbaud;
485ab4382d2SGreg Kroah-Hartman 	int hung_up = 0;
486ab4382d2SGreg Kroah-Hartman 	upf_t flags = port->flags & UPF_SPD_MASK;
487ab4382d2SGreg Kroah-Hartman 
488f10a2233SJoakim Nordell 	switch (flags) {
489f10a2233SJoakim Nordell 	case UPF_SPD_HI:
490ab4382d2SGreg Kroah-Hartman 		altbaud = 57600;
491f10a2233SJoakim Nordell 		break;
492f10a2233SJoakim Nordell 	case UPF_SPD_VHI:
493ab4382d2SGreg Kroah-Hartman 		altbaud = 115200;
494f10a2233SJoakim Nordell 		break;
495f10a2233SJoakim Nordell 	case UPF_SPD_SHI:
496ab4382d2SGreg Kroah-Hartman 		altbaud = 230400;
497f10a2233SJoakim Nordell 		break;
498f10a2233SJoakim Nordell 	case UPF_SPD_WARP:
499ab4382d2SGreg Kroah-Hartman 		altbaud = 460800;
500f10a2233SJoakim Nordell 		break;
501f10a2233SJoakim Nordell 	default:
502f10a2233SJoakim Nordell 		altbaud = 38400;
503f10a2233SJoakim Nordell 		break;
504f10a2233SJoakim Nordell 	}
505ab4382d2SGreg Kroah-Hartman 
506ab4382d2SGreg Kroah-Hartman 	for (try = 0; try < 2; try++) {
507ab4382d2SGreg Kroah-Hartman 		baud = tty_termios_baud_rate(termios);
508ab4382d2SGreg Kroah-Hartman 
509ab4382d2SGreg Kroah-Hartman 		/*
510ab4382d2SGreg Kroah-Hartman 		 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
511ab4382d2SGreg Kroah-Hartman 		 * Die! Die! Die!
512ab4382d2SGreg Kroah-Hartman 		 */
513547039ecSPeter Hurley 		if (try == 0 && baud == 38400)
514ab4382d2SGreg Kroah-Hartman 			baud = altbaud;
515ab4382d2SGreg Kroah-Hartman 
516ab4382d2SGreg Kroah-Hartman 		/*
517ab4382d2SGreg Kroah-Hartman 		 * Special case: B0 rate.
518ab4382d2SGreg Kroah-Hartman 		 */
519ab4382d2SGreg Kroah-Hartman 		if (baud == 0) {
520ab4382d2SGreg Kroah-Hartman 			hung_up = 1;
521ab4382d2SGreg Kroah-Hartman 			baud = 9600;
522ab4382d2SGreg Kroah-Hartman 		}
523ab4382d2SGreg Kroah-Hartman 
524ab4382d2SGreg Kroah-Hartman 		if (baud >= min && baud <= max)
525ab4382d2SGreg Kroah-Hartman 			return baud;
526ab4382d2SGreg Kroah-Hartman 
527ab4382d2SGreg Kroah-Hartman 		/*
528ab4382d2SGreg Kroah-Hartman 		 * Oops, the quotient was zero.  Try again with
529ab4382d2SGreg Kroah-Hartman 		 * the old baud rate if possible.
530ab4382d2SGreg Kroah-Hartman 		 */
531ab4382d2SGreg Kroah-Hartman 		termios->c_cflag &= ~CBAUD;
532ab4382d2SGreg Kroah-Hartman 		if (old) {
533ab4382d2SGreg Kroah-Hartman 			baud = tty_termios_baud_rate(old);
534ab4382d2SGreg Kroah-Hartman 			if (!hung_up)
535ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
536ab4382d2SGreg Kroah-Hartman 								baud, baud);
537ab4382d2SGreg Kroah-Hartman 			old = NULL;
538ab4382d2SGreg Kroah-Hartman 			continue;
539ab4382d2SGreg Kroah-Hartman 		}
540ab4382d2SGreg Kroah-Hartman 
541ab4382d2SGreg Kroah-Hartman 		/*
542ab4382d2SGreg Kroah-Hartman 		 * As a last resort, if the range cannot be met then clip to
543ab4382d2SGreg Kroah-Hartman 		 * the nearest chip supported rate.
544ab4382d2SGreg Kroah-Hartman 		 */
545ab4382d2SGreg Kroah-Hartman 		if (!hung_up) {
546ab4382d2SGreg Kroah-Hartman 			if (baud <= min)
547ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
548ab4382d2SGreg Kroah-Hartman 							min + 1, min + 1);
549ab4382d2SGreg Kroah-Hartman 			else
550ab4382d2SGreg Kroah-Hartman 				tty_termios_encode_baud_rate(termios,
551ab4382d2SGreg Kroah-Hartman 							max - 1, max - 1);
552ab4382d2SGreg Kroah-Hartman 		}
553ab4382d2SGreg Kroah-Hartman 	}
554ab4382d2SGreg Kroah-Hartman 	return 0;
555ab4382d2SGreg Kroah-Hartman }
556ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_baud_rate);
557ab4382d2SGreg Kroah-Hartman 
558ab4382d2SGreg Kroah-Hartman /**
559ab4382d2SGreg Kroah-Hartman  * uart_get_divisor - return uart clock divisor
560c4bd17a6SJiri Slaby  * @port: uart_port structure describing the port
561ab4382d2SGreg Kroah-Hartman  * @baud: desired baud rate
562ab4382d2SGreg Kroah-Hartman  *
563c4bd17a6SJiri Slaby  * Calculate the divisor (baud_base / baud) for the specified @baud,
564c4bd17a6SJiri Slaby  * appropriately rounded.
565c4bd17a6SJiri Slaby  *
566c4bd17a6SJiri Slaby  * If 38400 baud and custom divisor is selected, return the custom divisor
567c4bd17a6SJiri Slaby  * instead.
568c4bd17a6SJiri Slaby  *
569c4bd17a6SJiri Slaby  * Locking: caller dependent
570ab4382d2SGreg Kroah-Hartman  */
571ab4382d2SGreg Kroah-Hartman unsigned int
uart_get_divisor(struct uart_port * port,unsigned int baud)572ab4382d2SGreg Kroah-Hartman uart_get_divisor(struct uart_port *port, unsigned int baud)
573ab4382d2SGreg Kroah-Hartman {
574ab4382d2SGreg Kroah-Hartman 	unsigned int quot;
575ab4382d2SGreg Kroah-Hartman 
576ab4382d2SGreg Kroah-Hartman 	/*
577ab4382d2SGreg Kroah-Hartman 	 * Old custom speed handling.
578ab4382d2SGreg Kroah-Hartman 	 */
579ab4382d2SGreg Kroah-Hartman 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
580ab4382d2SGreg Kroah-Hartman 		quot = port->custom_divisor;
581ab4382d2SGreg Kroah-Hartman 	else
58297d24634SUwe Kleine-König 		quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
583ab4382d2SGreg Kroah-Hartman 
584ab4382d2SGreg Kroah-Hartman 	return quot;
585ab4382d2SGreg Kroah-Hartman }
586ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_get_divisor);
587ab4382d2SGreg Kroah-Hartman 
uart_put_char(struct tty_struct * tty,u8 c)588dcaafbe6SJiri Slaby (SUSE) static int uart_put_char(struct tty_struct *tty, u8 c)
589ab4382d2SGreg Kroah-Hartman {
590f5291eccSPeter Hurley 	struct uart_state *state = tty->driver_data;
5919ed19428SPeter Hurley 	struct uart_port *port;
592ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
593ab4382d2SGreg Kroah-Hartman 	int ret = 0;
594ab4382d2SGreg Kroah-Hartman 
5959ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
5965f1149d2SJiri Slaby (SUSE) 	if (!state->port.xmit_buf) {
597aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
598aff9cf59SSamir Virmani 		return 0;
599aff9cf59SSamir Virmani 	}
600aff9cf59SSamir Virmani 
6011788cf6aSJiri Slaby (SUSE) 	if (port)
6021788cf6aSJiri Slaby (SUSE) 		ret = kfifo_put(&state->port.xmit_fifo, c);
6039ed19428SPeter Hurley 	uart_port_unlock(port, flags);
604ab4382d2SGreg Kroah-Hartman 	return ret;
605ab4382d2SGreg Kroah-Hartman }
606ab4382d2SGreg Kroah-Hartman 
uart_flush_chars(struct tty_struct * tty)607ab4382d2SGreg Kroah-Hartman static void uart_flush_chars(struct tty_struct *tty)
608ab4382d2SGreg Kroah-Hartman {
609ab4382d2SGreg Kroah-Hartman 	uart_start(tty);
610ab4382d2SGreg Kroah-Hartman }
611ab4382d2SGreg Kroah-Hartman 
uart_write(struct tty_struct * tty,const u8 * buf,size_t count)61295713967SJiri Slaby (SUSE) static ssize_t uart_write(struct tty_struct *tty, const u8 *buf, size_t count)
613ab4382d2SGreg Kroah-Hartman {
614ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
615ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
616ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
6171788cf6aSJiri Slaby (SUSE) 	int ret = 0;
618ab4382d2SGreg Kroah-Hartman 
619ab4382d2SGreg Kroah-Hartman 	/*
620ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
621ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
622ab4382d2SGreg Kroah-Hartman 	 */
623602c8021SJiri Slaby 	if (WARN_ON(!state))
624ab4382d2SGreg Kroah-Hartman 		return -EL3HLT;
625ab4382d2SGreg Kroah-Hartman 
6269ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
627ae01e52dSTetsuo Handa 	if (!state->port.xmit_buf) {
628aff9cf59SSamir Virmani 		uart_port_unlock(port, flags);
629aff9cf59SSamir Virmani 		return 0;
630aff9cf59SSamir Virmani 	}
631aff9cf59SSamir Virmani 
6321788cf6aSJiri Slaby (SUSE) 	if (port)
6331788cf6aSJiri Slaby (SUSE) 		ret = kfifo_in(&state->port.xmit_fifo, buf, count);
63464dbee31SPeter Hurley 
6351225541cSJiri Slaby 	__uart_start(state);
6369ed19428SPeter Hurley 	uart_port_unlock(port, flags);
637ab4382d2SGreg Kroah-Hartman 	return ret;
638ab4382d2SGreg Kroah-Hartman }
639ab4382d2SGreg Kroah-Hartman 
uart_write_room(struct tty_struct * tty)64003b3b1a2SJiri Slaby static unsigned int uart_write_room(struct tty_struct *tty)
641ab4382d2SGreg Kroah-Hartman {
642ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6439ed19428SPeter Hurley 	struct uart_port *port;
644ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
64503b3b1a2SJiri Slaby 	unsigned int ret;
646ab4382d2SGreg Kroah-Hartman 
6479ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6481788cf6aSJiri Slaby (SUSE) 	ret = kfifo_avail(&state->port.xmit_fifo);
6499ed19428SPeter Hurley 	uart_port_unlock(port, flags);
650ab4382d2SGreg Kroah-Hartman 	return ret;
651ab4382d2SGreg Kroah-Hartman }
652ab4382d2SGreg Kroah-Hartman 
uart_chars_in_buffer(struct tty_struct * tty)653fff4ef17SJiri Slaby static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
654ab4382d2SGreg Kroah-Hartman {
655ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
6569ed19428SPeter Hurley 	struct uart_port *port;
657ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
658fff4ef17SJiri Slaby 	unsigned int ret;
659ab4382d2SGreg Kroah-Hartman 
6609ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6611788cf6aSJiri Slaby (SUSE) 	ret = kfifo_len(&state->port.xmit_fifo);
6629ed19428SPeter Hurley 	uart_port_unlock(port, flags);
663ab4382d2SGreg Kroah-Hartman 	return ret;
664ab4382d2SGreg Kroah-Hartman }
665ab4382d2SGreg Kroah-Hartman 
uart_flush_buffer(struct tty_struct * tty)666ab4382d2SGreg Kroah-Hartman static void uart_flush_buffer(struct tty_struct *tty)
667ab4382d2SGreg Kroah-Hartman {
668ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
669ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
670ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
671ab4382d2SGreg Kroah-Hartman 
672ab4382d2SGreg Kroah-Hartman 	/*
673ab4382d2SGreg Kroah-Hartman 	 * This means you called this function _after_ the port was
674ab4382d2SGreg Kroah-Hartman 	 * closed.  No cookie for you.
675ab4382d2SGreg Kroah-Hartman 	 */
676602c8021SJiri Slaby 	if (WARN_ON(!state))
677ab4382d2SGreg Kroah-Hartman 		return;
678ab4382d2SGreg Kroah-Hartman 
679ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_flush_buffer(%d) called\n", tty->index);
680ab4382d2SGreg Kroah-Hartman 
6819ed19428SPeter Hurley 	port = uart_port_lock(state, flags);
6829ed19428SPeter Hurley 	if (!port)
6839ed19428SPeter Hurley 		return;
6841788cf6aSJiri Slaby (SUSE) 	kfifo_reset(&state->port.xmit_fifo);
685ab4382d2SGreg Kroah-Hartman 	if (port->ops->flush_buffer)
686ab4382d2SGreg Kroah-Hartman 		port->ops->flush_buffer(port);
6879ed19428SPeter Hurley 	uart_port_unlock(port, flags);
688d0f4bce2SRob Herring 	tty_port_tty_wakeup(&state->port);
689ab4382d2SGreg Kroah-Hartman }
690ab4382d2SGreg Kroah-Hartman 
691ab4382d2SGreg Kroah-Hartman /*
692f58c252eSIlpo Järvinen  * This function performs low-level write of high-priority XON/XOFF
693f58c252eSIlpo Järvinen  * character and accounting for it.
694f58c252eSIlpo Järvinen  *
695f58c252eSIlpo Järvinen  * Requires uart_port to implement .serial_out().
696f58c252eSIlpo Järvinen  */
uart_xchar_out(struct uart_port * uport,int offset)697f58c252eSIlpo Järvinen void uart_xchar_out(struct uart_port *uport, int offset)
698f58c252eSIlpo Järvinen {
699f58c252eSIlpo Järvinen 	serial_port_out(uport, offset, uport->x_char);
700f58c252eSIlpo Järvinen 	uport->icount.tx++;
701f58c252eSIlpo Järvinen 	uport->x_char = 0;
702f58c252eSIlpo Järvinen }
703f58c252eSIlpo Järvinen EXPORT_SYMBOL_GPL(uart_xchar_out);
704f58c252eSIlpo Järvinen 
705f58c252eSIlpo Järvinen /*
706ab4382d2SGreg Kroah-Hartman  * This function is used to send a high-priority XON/XOFF character to
707ab4382d2SGreg Kroah-Hartman  * the device
708ab4382d2SGreg Kroah-Hartman  */
uart_send_xchar(struct tty_struct * tty,u8 ch)7093a00da02SJiri Slaby (SUSE) static void uart_send_xchar(struct tty_struct *tty, u8 ch)
710ab4382d2SGreg Kroah-Hartman {
711ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
7129ed19428SPeter Hurley 	struct uart_port *port;
713ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
714ab4382d2SGreg Kroah-Hartman 
7159ed19428SPeter Hurley 	port = uart_port_ref(state);
7169ed19428SPeter Hurley 	if (!port)
7179ed19428SPeter Hurley 		return;
7189ed19428SPeter Hurley 
719ab4382d2SGreg Kroah-Hartman 	if (port->ops->send_xchar)
720ab4382d2SGreg Kroah-Hartman 		port->ops->send_xchar(port, ch);
721ab4382d2SGreg Kroah-Hartman 	else {
722559c7ff4SThomas Gleixner 		uart_port_lock_irqsave(port, &flags);
723c235ccc1SPeter Hurley 		port->x_char = ch;
724c235ccc1SPeter Hurley 		if (ch)
725ab4382d2SGreg Kroah-Hartman 			port->ops->start_tx(port);
726559c7ff4SThomas Gleixner 		uart_port_unlock_irqrestore(port, flags);
727ab4382d2SGreg Kroah-Hartman 	}
7289ed19428SPeter Hurley 	uart_port_deref(port);
729ab4382d2SGreg Kroah-Hartman }
730ab4382d2SGreg Kroah-Hartman 
uart_throttle(struct tty_struct * tty)731ab4382d2SGreg Kroah-Hartman static void uart_throttle(struct tty_struct *tty)
732ab4382d2SGreg Kroah-Hartman {
733ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
734c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7359ed19428SPeter Hurley 	struct uart_port *port;
736ab4382d2SGreg Kroah-Hartman 
7379ed19428SPeter Hurley 	port = uart_port_ref(state);
7389ed19428SPeter Hurley 	if (!port)
7399ed19428SPeter Hurley 		return;
7409ed19428SPeter Hurley 
741ab4382d2SGreg Kroah-Hartman 	if (I_IXOFF(tty))
742391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7439db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
744391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7459aba8d5bSRussell King 
746391f93f2SPeter Hurley 	if (port->status & mask) {
7479aba8d5bSRussell King 		port->ops->throttle(port);
748391f93f2SPeter Hurley 		mask &= ~port->status;
7499aba8d5bSRussell King 	}
7509aba8d5bSRussell King 
751391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
7529aba8d5bSRussell King 		uart_clear_mctrl(port, TIOCM_RTS);
753b4749b97SPeter Hurley 
754b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
755b4749b97SPeter Hurley 		uart_send_xchar(tty, STOP_CHAR(tty));
7569ed19428SPeter Hurley 
7579ed19428SPeter Hurley 	uart_port_deref(port);
758ab4382d2SGreg Kroah-Hartman }
759ab4382d2SGreg Kroah-Hartman 
uart_unthrottle(struct tty_struct * tty)760ab4382d2SGreg Kroah-Hartman static void uart_unthrottle(struct tty_struct *tty)
761ab4382d2SGreg Kroah-Hartman {
762ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
763c5f78b1fSJeremy Kerr 	upstat_t mask = UPSTAT_SYNC_FIFO;
7649ed19428SPeter Hurley 	struct uart_port *port;
765ab4382d2SGreg Kroah-Hartman 
7669ed19428SPeter Hurley 	port = uart_port_ref(state);
7679ed19428SPeter Hurley 	if (!port)
7689ed19428SPeter Hurley 		return;
7699ed19428SPeter Hurley 
7709aba8d5bSRussell King 	if (I_IXOFF(tty))
771391f93f2SPeter Hurley 		mask |= UPSTAT_AUTOXOFF;
7729db276f8SPeter Hurley 	if (C_CRTSCTS(tty))
773391f93f2SPeter Hurley 		mask |= UPSTAT_AUTORTS;
7749aba8d5bSRussell King 
775391f93f2SPeter Hurley 	if (port->status & mask) {
7769aba8d5bSRussell King 		port->ops->unthrottle(port);
777391f93f2SPeter Hurley 		mask &= ~port->status;
7789aba8d5bSRussell King 	}
7799aba8d5bSRussell King 
780391f93f2SPeter Hurley 	if (mask & UPSTAT_AUTORTS)
781ab4382d2SGreg Kroah-Hartman 		uart_set_mctrl(port, TIOCM_RTS);
782b4749b97SPeter Hurley 
783b4749b97SPeter Hurley 	if (mask & UPSTAT_AUTOXOFF)
784b4749b97SPeter Hurley 		uart_send_xchar(tty, START_CHAR(tty));
7859ed19428SPeter Hurley 
7869ed19428SPeter Hurley 	uart_port_deref(port);
787ab4382d2SGreg Kroah-Hartman }
788ab4382d2SGreg Kroah-Hartman 
uart_get_info(struct tty_port * port,struct serial_struct * retinfo)7894047b371SPeter Hurley static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
790ab4382d2SGreg Kroah-Hartman {
7919f109694SAlan Cox 	struct uart_state *state = container_of(port, struct uart_state, port);
7924047b371SPeter Hurley 	struct uart_port *uport;
7934047b371SPeter Hurley 	int ret = -ENODEV;
7947ba2e769SAlan Cox 
795cfb5e0ceSAlexey Dobriyan 	/* Initialize structure in case we error out later to prevent any stack info leakage. */
796cfb5e0ceSAlexey Dobriyan 	*retinfo = (struct serial_struct){};
797cfb5e0ceSAlexey Dobriyan 
7983abe8c76SPeter Hurley 	/*
7993abe8c76SPeter Hurley 	 * Ensure the state we copy is consistent and no hardware changes
8003abe8c76SPeter Hurley 	 * occur as we go
8013abe8c76SPeter Hurley 	 */
8023abe8c76SPeter Hurley 	mutex_lock(&port->mutex);
8034047b371SPeter Hurley 	uport = uart_port_check(state);
8044047b371SPeter Hurley 	if (!uport)
8054047b371SPeter Hurley 		goto out;
8064047b371SPeter Hurley 
8077ba2e769SAlan Cox 	retinfo->type	    = uport->type;
8087ba2e769SAlan Cox 	retinfo->line	    = uport->line;
8097ba2e769SAlan Cox 	retinfo->port	    = uport->iobase;
8107ba2e769SAlan Cox 	if (HIGH_BITS_OFFSET)
8117ba2e769SAlan Cox 		retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
8127ba2e769SAlan Cox 	retinfo->irq		    = uport->irq;
813a17e74c5SAndy Shevchenko 	retinfo->flags	    = (__force int)uport->flags;
8147ba2e769SAlan Cox 	retinfo->xmit_fifo_size  = uport->fifosize;
8157ba2e769SAlan Cox 	retinfo->baud_base	    = uport->uartclk / 16;
8167ba2e769SAlan Cox 	retinfo->close_delay	    = jiffies_to_msecs(port->close_delay) / 10;
8177ba2e769SAlan Cox 	retinfo->closing_wait    = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
8187ba2e769SAlan Cox 				ASYNC_CLOSING_WAIT_NONE :
8197ba2e769SAlan Cox 				jiffies_to_msecs(port->closing_wait) / 10;
8207ba2e769SAlan Cox 	retinfo->custom_divisor  = uport->custom_divisor;
8217ba2e769SAlan Cox 	retinfo->hub6	    = uport->hub6;
8227ba2e769SAlan Cox 	retinfo->io_type         = uport->iotype;
8237ba2e769SAlan Cox 	retinfo->iomem_reg_shift = uport->regshift;
8247ba2e769SAlan Cox 	retinfo->iomem_base      = (void *)(unsigned long)uport->mapbase;
8254047b371SPeter Hurley 
8264047b371SPeter Hurley 	ret = 0;
8274047b371SPeter Hurley out:
828ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
8294047b371SPeter Hurley 	return ret;
8309f109694SAlan Cox }
8319f109694SAlan Cox 
uart_get_info_user(struct tty_struct * tty,struct serial_struct * ss)8325099d234SAl Viro static int uart_get_info_user(struct tty_struct *tty,
8335099d234SAl Viro 			 struct serial_struct *ss)
8349f109694SAlan Cox {
8355099d234SAl Viro 	struct uart_state *state = tty->driver_data;
8365099d234SAl Viro 	struct tty_port *port = &state->port;
8373abe8c76SPeter Hurley 
8385099d234SAl Viro 	return uart_get_info(port, ss) < 0 ? -EIO : 0;
839ab4382d2SGreg Kroah-Hartman }
840ab4382d2SGreg Kroah-Hartman 
uart_set_info(struct tty_struct * tty,struct tty_port * port,struct uart_state * state,struct serial_struct * new_info)8417ba2e769SAlan Cox static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
8427ba2e769SAlan Cox 			 struct uart_state *state,
8437ba2e769SAlan Cox 			 struct serial_struct *new_info)
844ab4382d2SGreg Kroah-Hartman {
8454047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
846ab4382d2SGreg Kroah-Hartman 	unsigned long new_port;
847ab4382d2SGreg Kroah-Hartman 	unsigned int change_irq, change_port, closing_wait;
848ab4382d2SGreg Kroah-Hartman 	unsigned int old_custom_divisor, close_delay;
849ab4382d2SGreg Kroah-Hartman 	upf_t old_flags, new_flags;
850ab4382d2SGreg Kroah-Hartman 	int retval = 0;
851ab4382d2SGreg Kroah-Hartman 
8524047b371SPeter Hurley 	if (!uport)
8534047b371SPeter Hurley 		return -EIO;
8544047b371SPeter Hurley 
8557ba2e769SAlan Cox 	new_port = new_info->port;
856ab4382d2SGreg Kroah-Hartman 	if (HIGH_BITS_OFFSET)
8577ba2e769SAlan Cox 		new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
858ab4382d2SGreg Kroah-Hartman 
8597ba2e769SAlan Cox 	new_info->irq = irq_canonicalize(new_info->irq);
8607ba2e769SAlan Cox 	close_delay = msecs_to_jiffies(new_info->close_delay * 10);
8617ba2e769SAlan Cox 	closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
8624cb0fbfdSJiri Slaby 			ASYNC_CLOSING_WAIT_NONE :
8637ba2e769SAlan Cox 			msecs_to_jiffies(new_info->closing_wait * 10);
864ab4382d2SGreg Kroah-Hartman 
865ab4382d2SGreg Kroah-Hartman 
866ab4382d2SGreg Kroah-Hartman 	change_irq  = !(uport->flags & UPF_FIXED_PORT)
8677ba2e769SAlan Cox 		&& new_info->irq != uport->irq;
868ab4382d2SGreg Kroah-Hartman 
869ab4382d2SGreg Kroah-Hartman 	/*
870ab4382d2SGreg Kroah-Hartman 	 * Since changing the 'type' of the port changes its resource
871ab4382d2SGreg Kroah-Hartman 	 * allocations, we should treat type changes the same as
872ab4382d2SGreg Kroah-Hartman 	 * IO port changes.
873ab4382d2SGreg Kroah-Hartman 	 */
874ab4382d2SGreg Kroah-Hartman 	change_port = !(uport->flags & UPF_FIXED_PORT)
875ab4382d2SGreg Kroah-Hartman 		&& (new_port != uport->iobase ||
8767ba2e769SAlan Cox 		    (unsigned long)new_info->iomem_base != uport->mapbase ||
8777ba2e769SAlan Cox 		    new_info->hub6 != uport->hub6 ||
8787ba2e769SAlan Cox 		    new_info->io_type != uport->iotype ||
8797ba2e769SAlan Cox 		    new_info->iomem_reg_shift != uport->regshift ||
8807ba2e769SAlan Cox 		    new_info->type != uport->type);
881ab4382d2SGreg Kroah-Hartman 
882ab4382d2SGreg Kroah-Hartman 	old_flags = uport->flags;
883a17e74c5SAndy Shevchenko 	new_flags = (__force upf_t)new_info->flags;
884ab4382d2SGreg Kroah-Hartman 	old_custom_divisor = uport->custom_divisor;
885ab4382d2SGreg Kroah-Hartman 
8866eabce66SGeorge Kennedy 	if (!(uport->flags & UPF_FIXED_PORT)) {
8876eabce66SGeorge Kennedy 		unsigned int uartclk = new_info->baud_base * 16;
8886eabce66SGeorge Kennedy 		/* check needs to be done here before other settings made */
8896eabce66SGeorge Kennedy 		if (uartclk == 0) {
8906eabce66SGeorge Kennedy 			retval = -EINVAL;
8916eabce66SGeorge Kennedy 			goto exit;
8926eabce66SGeorge Kennedy 		}
8936eabce66SGeorge Kennedy 	}
894ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN)) {
895ab4382d2SGreg Kroah-Hartman 		retval = -EPERM;
896ab4382d2SGreg Kroah-Hartman 		if (change_irq || change_port ||
8977ba2e769SAlan Cox 		    (new_info->baud_base != uport->uartclk / 16) ||
898ab4382d2SGreg Kroah-Hartman 		    (close_delay != port->close_delay) ||
899ab4382d2SGreg Kroah-Hartman 		    (closing_wait != port->closing_wait) ||
9007ba2e769SAlan Cox 		    (new_info->xmit_fifo_size &&
9017ba2e769SAlan Cox 		     new_info->xmit_fifo_size != uport->fifosize) ||
902ab4382d2SGreg Kroah-Hartman 		    (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
903ab4382d2SGreg Kroah-Hartman 			goto exit;
904ab4382d2SGreg Kroah-Hartman 		uport->flags = ((uport->flags & ~UPF_USR_MASK) |
905ab4382d2SGreg Kroah-Hartman 			       (new_flags & UPF_USR_MASK));
9067ba2e769SAlan Cox 		uport->custom_divisor = new_info->custom_divisor;
907ab4382d2SGreg Kroah-Hartman 		goto check_and_exit;
908ab4382d2SGreg Kroah-Hartman 	}
909ab4382d2SGreg Kroah-Hartman 
9105e722b21SOndrej Mosnacek 	if (change_irq || change_port) {
911794edf30SDavid Howells 		retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
9125e722b21SOndrej Mosnacek 		if (retval)
913794edf30SDavid Howells 			goto exit;
9145e722b21SOndrej Mosnacek 	}
915794edf30SDavid Howells 
916ab4382d2SGreg Kroah-Hartman 	/*
917ab4382d2SGreg Kroah-Hartman 	 * Ask the low level driver to verify the settings.
918ab4382d2SGreg Kroah-Hartman 	 */
919ab4382d2SGreg Kroah-Hartman 	if (uport->ops->verify_port)
9207ba2e769SAlan Cox 		retval = uport->ops->verify_port(uport, new_info);
921ab4382d2SGreg Kroah-Hartman 
9227ba2e769SAlan Cox 	if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
9237ba2e769SAlan Cox 	    (new_info->baud_base < 9600))
924ab4382d2SGreg Kroah-Hartman 		retval = -EINVAL;
925ab4382d2SGreg Kroah-Hartman 
926ab4382d2SGreg Kroah-Hartman 	if (retval)
927ab4382d2SGreg Kroah-Hartman 		goto exit;
928ab4382d2SGreg Kroah-Hartman 
929ab4382d2SGreg Kroah-Hartman 	if (change_port || change_irq) {
930ab4382d2SGreg Kroah-Hartman 		retval = -EBUSY;
931ab4382d2SGreg Kroah-Hartman 
932ab4382d2SGreg Kroah-Hartman 		/*
933ab4382d2SGreg Kroah-Hartman 		 * Make sure that we are the sole user of this port.
934ab4382d2SGreg Kroah-Hartman 		 */
935ab4382d2SGreg Kroah-Hartman 		if (tty_port_users(port) > 1)
936ab4382d2SGreg Kroah-Hartman 			goto exit;
937ab4382d2SGreg Kroah-Hartman 
938ab4382d2SGreg Kroah-Hartman 		/*
939ab4382d2SGreg Kroah-Hartman 		 * We need to shutdown the serial port at the old
940ab4382d2SGreg Kroah-Hartman 		 * port/type/irq combination.
941ab4382d2SGreg Kroah-Hartman 		 */
942ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
943ab4382d2SGreg Kroah-Hartman 	}
944ab4382d2SGreg Kroah-Hartman 
945ab4382d2SGreg Kroah-Hartman 	if (change_port) {
946ab4382d2SGreg Kroah-Hartman 		unsigned long old_iobase, old_mapbase;
947ab4382d2SGreg Kroah-Hartman 		unsigned int old_type, old_iotype, old_hub6, old_shift;
948ab4382d2SGreg Kroah-Hartman 
949ab4382d2SGreg Kroah-Hartman 		old_iobase = uport->iobase;
950ab4382d2SGreg Kroah-Hartman 		old_mapbase = uport->mapbase;
951ab4382d2SGreg Kroah-Hartman 		old_type = uport->type;
952ab4382d2SGreg Kroah-Hartman 		old_hub6 = uport->hub6;
953ab4382d2SGreg Kroah-Hartman 		old_iotype = uport->iotype;
954ab4382d2SGreg Kroah-Hartman 		old_shift = uport->regshift;
955ab4382d2SGreg Kroah-Hartman 
956ab4382d2SGreg Kroah-Hartman 		/*
957ab4382d2SGreg Kroah-Hartman 		 * Free and release old regions
958ab4382d2SGreg Kroah-Hartman 		 */
959a7cfaf16SFabio Estevam 		if (old_type != PORT_UNKNOWN && uport->ops->release_port)
960ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
961ab4382d2SGreg Kroah-Hartman 
962ab4382d2SGreg Kroah-Hartman 		uport->iobase = new_port;
9637ba2e769SAlan Cox 		uport->type = new_info->type;
9647ba2e769SAlan Cox 		uport->hub6 = new_info->hub6;
9657ba2e769SAlan Cox 		uport->iotype = new_info->io_type;
9667ba2e769SAlan Cox 		uport->regshift = new_info->iomem_reg_shift;
9677ba2e769SAlan Cox 		uport->mapbase = (unsigned long)new_info->iomem_base;
968ab4382d2SGreg Kroah-Hartman 
969ab4382d2SGreg Kroah-Hartman 		/*
970ab4382d2SGreg Kroah-Hartman 		 * Claim and map the new regions
971ab4382d2SGreg Kroah-Hartman 		 */
972a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
973ab4382d2SGreg Kroah-Hartman 			retval = uport->ops->request_port(uport);
974ab4382d2SGreg Kroah-Hartman 		} else {
975ab4382d2SGreg Kroah-Hartman 			/* Always success - Jean II */
976ab4382d2SGreg Kroah-Hartman 			retval = 0;
977ab4382d2SGreg Kroah-Hartman 		}
978ab4382d2SGreg Kroah-Hartman 
979ab4382d2SGreg Kroah-Hartman 		/*
980ab4382d2SGreg Kroah-Hartman 		 * If we fail to request resources for the
981ab4382d2SGreg Kroah-Hartman 		 * new port, try to restore the old settings.
982ab4382d2SGreg Kroah-Hartman 		 */
9837deb39edSThomas Pfaff 		if (retval) {
984ab4382d2SGreg Kroah-Hartman 			uport->iobase = old_iobase;
985ab4382d2SGreg Kroah-Hartman 			uport->type = old_type;
986ab4382d2SGreg Kroah-Hartman 			uport->hub6 = old_hub6;
987ab4382d2SGreg Kroah-Hartman 			uport->iotype = old_iotype;
988ab4382d2SGreg Kroah-Hartman 			uport->regshift = old_shift;
989ab4382d2SGreg Kroah-Hartman 			uport->mapbase = old_mapbase;
9907deb39edSThomas Pfaff 
9917deb39edSThomas Pfaff 			if (old_type != PORT_UNKNOWN) {
992ab4382d2SGreg Kroah-Hartman 				retval = uport->ops->request_port(uport);
993ab4382d2SGreg Kroah-Hartman 				/*
994ab4382d2SGreg Kroah-Hartman 				 * If we failed to restore the old settings,
995ab4382d2SGreg Kroah-Hartman 				 * we fail like this.
996ab4382d2SGreg Kroah-Hartman 				 */
997ab4382d2SGreg Kroah-Hartman 				if (retval)
998ab4382d2SGreg Kroah-Hartman 					uport->type = PORT_UNKNOWN;
999ab4382d2SGreg Kroah-Hartman 
1000ab4382d2SGreg Kroah-Hartman 				/*
1001ab4382d2SGreg Kroah-Hartman 				 * We failed anyway.
1002ab4382d2SGreg Kroah-Hartman 				 */
1003ab4382d2SGreg Kroah-Hartman 				retval = -EBUSY;
10047deb39edSThomas Pfaff 			}
10057deb39edSThomas Pfaff 
1006ab4382d2SGreg Kroah-Hartman 			/* Added to return the correct error -Ram Gupta */
1007ab4382d2SGreg Kroah-Hartman 			goto exit;
1008ab4382d2SGreg Kroah-Hartman 		}
1009ab4382d2SGreg Kroah-Hartman 	}
1010ab4382d2SGreg Kroah-Hartman 
1011ab4382d2SGreg Kroah-Hartman 	if (change_irq)
10127ba2e769SAlan Cox 		uport->irq      = new_info->irq;
1013ab4382d2SGreg Kroah-Hartman 	if (!(uport->flags & UPF_FIXED_PORT))
10147ba2e769SAlan Cox 		uport->uartclk  = new_info->baud_base * 16;
1015ab4382d2SGreg Kroah-Hartman 	uport->flags            = (uport->flags & ~UPF_CHANGE_MASK) |
1016ab4382d2SGreg Kroah-Hartman 				 (new_flags & UPF_CHANGE_MASK);
10177ba2e769SAlan Cox 	uport->custom_divisor   = new_info->custom_divisor;
1018ab4382d2SGreg Kroah-Hartman 	port->close_delay     = close_delay;
1019ab4382d2SGreg Kroah-Hartman 	port->closing_wait    = closing_wait;
10207ba2e769SAlan Cox 	if (new_info->xmit_fifo_size)
10217ba2e769SAlan Cox 		uport->fifosize = new_info->xmit_fifo_size;
1022ab4382d2SGreg Kroah-Hartman 
1023ab4382d2SGreg Kroah-Hartman  check_and_exit:
1024ab4382d2SGreg Kroah-Hartman 	retval = 0;
1025ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN)
1026ab4382d2SGreg Kroah-Hartman 		goto exit;
1027d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
1028ab4382d2SGreg Kroah-Hartman 		if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
1029ab4382d2SGreg Kroah-Hartman 		    old_custom_divisor != uport->custom_divisor) {
1030ab4382d2SGreg Kroah-Hartman 			/*
1031ab4382d2SGreg Kroah-Hartman 			 * If they're setting up a custom divisor or speed,
1032dc2f7271SJohan Hovold 			 * instead of clearing it, then bitch about it.
1033ab4382d2SGreg Kroah-Hartman 			 */
1034ab4382d2SGreg Kroah-Hartman 			if (uport->flags & UPF_SPD_MASK) {
1035dc2f7271SJohan Hovold 				dev_notice_ratelimited(uport->dev,
10362f2dafe7SSudip Mukherjee 				       "%s sets custom speed on %s. This is deprecated.\n",
10372f2dafe7SSudip Mukherjee 				      current->comm,
1038429b4749SRasmus Villemoes 				      tty_name(port->tty));
1039ab4382d2SGreg Kroah-Hartman 			}
1040826736a6SIlpo Järvinen 			uart_change_line_settings(tty, state, NULL);
1041ab4382d2SGreg Kroah-Hartman 		}
1042b3b57646SRob Herring 	} else {
1043dcd794c6SIlpo Järvinen 		retval = uart_startup(tty, state, true);
104444117a1dSSebastian Andrzej Siewior 		if (retval == 0)
104544117a1dSSebastian Andrzej Siewior 			tty_port_set_initialized(port, true);
1046b3b57646SRob Herring 		if (retval > 0)
1047b3b57646SRob Herring 			retval = 0;
1048b3b57646SRob Herring 	}
1049ab4382d2SGreg Kroah-Hartman  exit:
10507ba2e769SAlan Cox 	return retval;
10517ba2e769SAlan Cox }
10527ba2e769SAlan Cox 
uart_set_info_user(struct tty_struct * tty,struct serial_struct * ss)10535099d234SAl Viro static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
10547ba2e769SAlan Cox {
10555099d234SAl Viro 	struct uart_state *state = tty->driver_data;
10567ba2e769SAlan Cox 	struct tty_port *port = &state->port;
10577ba2e769SAlan Cox 	int retval;
10587ba2e769SAlan Cox 
10595099d234SAl Viro 	down_write(&tty->termios_rwsem);
10607ba2e769SAlan Cox 	/*
10617ba2e769SAlan Cox 	 * This semaphore protects port->count.  It is also
10627ba2e769SAlan Cox 	 * very useful to prevent opens.  Also, take the
10637ba2e769SAlan Cox 	 * port configuration semaphore to make sure that a
10647ba2e769SAlan Cox 	 * module insertion/removal doesn't change anything
10657ba2e769SAlan Cox 	 * under us.
10667ba2e769SAlan Cox 	 */
10677ba2e769SAlan Cox 	mutex_lock(&port->mutex);
10685099d234SAl Viro 	retval = uart_set_info(tty, port, state, ss);
1069ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
10705099d234SAl Viro 	up_write(&tty->termios_rwsem);
1071ab4382d2SGreg Kroah-Hartman 	return retval;
1072ab4382d2SGreg Kroah-Hartman }
1073ab4382d2SGreg Kroah-Hartman 
1074ab4382d2SGreg Kroah-Hartman /**
1075ab4382d2SGreg Kroah-Hartman  * uart_get_lsr_info - get line status register info
1076ab4382d2SGreg Kroah-Hartman  * @tty: tty associated with the UART
1077ab4382d2SGreg Kroah-Hartman  * @state: UART being queried
1078ab4382d2SGreg Kroah-Hartman  * @value: returned modem value
1079ab4382d2SGreg Kroah-Hartman  */
uart_get_lsr_info(struct tty_struct * tty,struct uart_state * state,unsigned int __user * value)1080ab4382d2SGreg Kroah-Hartman static int uart_get_lsr_info(struct tty_struct *tty,
1081ab4382d2SGreg Kroah-Hartman 			struct uart_state *state, unsigned int __user *value)
1082ab4382d2SGreg Kroah-Hartman {
10834047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1084ab4382d2SGreg Kroah-Hartman 	unsigned int result;
1085ab4382d2SGreg Kroah-Hartman 
1086ab4382d2SGreg Kroah-Hartman 	result = uport->ops->tx_empty(uport);
1087ab4382d2SGreg Kroah-Hartman 
1088ab4382d2SGreg Kroah-Hartman 	/*
1089ab4382d2SGreg Kroah-Hartman 	 * If we're about to load something into the transmit
1090ab4382d2SGreg Kroah-Hartman 	 * register, we'll pretend the transmitter isn't empty to
1091ab4382d2SGreg Kroah-Hartman 	 * avoid a race condition (depending on when the transmit
1092ab4382d2SGreg Kroah-Hartman 	 * interrupt happens).
1093ab4382d2SGreg Kroah-Hartman 	 */
1094ab4382d2SGreg Kroah-Hartman 	if (uport->x_char ||
10951788cf6aSJiri Slaby (SUSE) 	    (!kfifo_is_empty(&state->port.xmit_fifo) &&
1096d01f4d18SPeter Hurley 	     !uart_tx_stopped(uport)))
1097ab4382d2SGreg Kroah-Hartman 		result &= ~TIOCSER_TEMT;
1098ab4382d2SGreg Kroah-Hartman 
1099ab4382d2SGreg Kroah-Hartman 	return put_user(result, value);
1100ab4382d2SGreg Kroah-Hartman }
1101ab4382d2SGreg Kroah-Hartman 
uart_tiocmget(struct tty_struct * tty)110260b33c13SAlan Cox static int uart_tiocmget(struct tty_struct *tty)
1103ab4382d2SGreg Kroah-Hartman {
1104ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1105ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11064047b371SPeter Hurley 	struct uart_port *uport;
1107*5879adbfSJiri Slaby (SUSE) 	int result;
1108ab4382d2SGreg Kroah-Hartman 
1109*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
1110*5879adbfSJiri Slaby (SUSE) 
11114047b371SPeter Hurley 	uport = uart_port_check(state);
1112*5879adbfSJiri Slaby (SUSE) 	if (!uport || tty_io_error(tty))
1113*5879adbfSJiri Slaby (SUSE) 		return -EIO;
11144047b371SPeter Hurley 
1115559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
111630926783SGui-Dong Han 	result = uport->mctrl;
1117ab4382d2SGreg Kroah-Hartman 	result |= uport->ops->get_mctrl(uport);
1118559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
1119*5879adbfSJiri Slaby (SUSE) 
1120ab4382d2SGreg Kroah-Hartman 	return result;
1121ab4382d2SGreg Kroah-Hartman }
1122ab4382d2SGreg Kroah-Hartman 
1123ab4382d2SGreg Kroah-Hartman static int
uart_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)112420b9d177SAlan Cox uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1125ab4382d2SGreg Kroah-Hartman {
1126ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1127ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11284047b371SPeter Hurley 	struct uart_port *uport;
1129ab4382d2SGreg Kroah-Hartman 
1130*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
1131*5879adbfSJiri Slaby (SUSE) 
11324047b371SPeter Hurley 	uport = uart_port_check(state);
1133*5879adbfSJiri Slaby (SUSE) 	if (!uport || tty_io_error(tty))
1134*5879adbfSJiri Slaby (SUSE) 		return -EIO;
11354047b371SPeter Hurley 
1136ab4382d2SGreg Kroah-Hartman 	uart_update_mctrl(uport, set, clear);
1137*5879adbfSJiri Slaby (SUSE) 
1138*5879adbfSJiri Slaby (SUSE) 	return 0;
1139ab4382d2SGreg Kroah-Hartman }
1140ab4382d2SGreg Kroah-Hartman 
uart_break_ctl(struct tty_struct * tty,int break_state)1141ab4382d2SGreg Kroah-Hartman static int uart_break_ctl(struct tty_struct *tty, int break_state)
1142ab4382d2SGreg Kroah-Hartman {
1143ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1144ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11454047b371SPeter Hurley 	struct uart_port *uport;
1146ab4382d2SGreg Kroah-Hartman 
1147*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
1148*5879adbfSJiri Slaby (SUSE) 
11494047b371SPeter Hurley 	uport = uart_port_check(state);
11504047b371SPeter Hurley 	if (!uport)
1151*5879adbfSJiri Slaby (SUSE) 		return -EIO;
1152ab4382d2SGreg Kroah-Hartman 
11537d73170eSJiangfeng Xiao 	if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1154ab4382d2SGreg Kroah-Hartman 		uport->ops->break_ctl(uport, break_state);
1155*5879adbfSJiri Slaby (SUSE) 
1156*5879adbfSJiri Slaby (SUSE) 	return 0;
1157ab4382d2SGreg Kroah-Hartman }
1158ab4382d2SGreg Kroah-Hartman 
uart_do_autoconfig(struct tty_struct * tty,struct uart_state * state)1159ab4382d2SGreg Kroah-Hartman static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1160ab4382d2SGreg Kroah-Hartman {
1161ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
11624047b371SPeter Hurley 	struct uart_port *uport;
1163ab4382d2SGreg Kroah-Hartman 	int flags, ret;
1164ab4382d2SGreg Kroah-Hartman 
1165ab4382d2SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
1166ab4382d2SGreg Kroah-Hartman 		return -EPERM;
1167ab4382d2SGreg Kroah-Hartman 
1168ab4382d2SGreg Kroah-Hartman 	/*
1169ab4382d2SGreg Kroah-Hartman 	 * Take the per-port semaphore.  This prevents count from
1170ab4382d2SGreg Kroah-Hartman 	 * changing, and hence any extra opens of the port while
1171ab4382d2SGreg Kroah-Hartman 	 * we're auto-configuring.
1172ab4382d2SGreg Kroah-Hartman 	 */
1173*5879adbfSJiri Slaby (SUSE) 	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) {
11744047b371SPeter Hurley 		uport = uart_port_check(state);
1175*5879adbfSJiri Slaby (SUSE) 		if (!uport)
1176*5879adbfSJiri Slaby (SUSE) 			return -EIO;
11774047b371SPeter Hurley 
1178*5879adbfSJiri Slaby (SUSE) 		if (tty_port_users(port) != 1)
1179*5879adbfSJiri Slaby (SUSE) 			return -EBUSY;
1180*5879adbfSJiri Slaby (SUSE) 
1181ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1182ab4382d2SGreg Kroah-Hartman 
1183ab4382d2SGreg Kroah-Hartman 		/*
1184ab4382d2SGreg Kroah-Hartman 		 * If we already have a port type configured,
1185ab4382d2SGreg Kroah-Hartman 		 * we must release its resources.
1186ab4382d2SGreg Kroah-Hartman 		 */
1187a7cfaf16SFabio Estevam 		if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1188ab4382d2SGreg Kroah-Hartman 			uport->ops->release_port(uport);
1189ab4382d2SGreg Kroah-Hartman 
1190ab4382d2SGreg Kroah-Hartman 		flags = UART_CONFIG_TYPE;
1191ab4382d2SGreg Kroah-Hartman 		if (uport->flags & UPF_AUTO_IRQ)
1192ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_IRQ;
1193ab4382d2SGreg Kroah-Hartman 
1194ab4382d2SGreg Kroah-Hartman 		/*
1195ab4382d2SGreg Kroah-Hartman 		 * This will claim the ports resources if
1196ab4382d2SGreg Kroah-Hartman 		 * a port is found.
1197ab4382d2SGreg Kroah-Hartman 		 */
1198ab4382d2SGreg Kroah-Hartman 		uport->ops->config_port(uport, flags);
1199ab4382d2SGreg Kroah-Hartman 
1200dcd794c6SIlpo Järvinen 		ret = uart_startup(tty, state, true);
1201*5879adbfSJiri Slaby (SUSE) 		if (ret < 0)
1202ab4382d2SGreg Kroah-Hartman 			return ret;
1203*5879adbfSJiri Slaby (SUSE) 		if (ret > 0)
1204*5879adbfSJiri Slaby (SUSE) 			return 0;
1205*5879adbfSJiri Slaby (SUSE) 
1206*5879adbfSJiri Slaby (SUSE) 		tty_port_set_initialized(port, true);
1207*5879adbfSJiri Slaby (SUSE) 	}
1208*5879adbfSJiri Slaby (SUSE) 
1209*5879adbfSJiri Slaby (SUSE) 	return 0;
1210ab4382d2SGreg Kroah-Hartman }
1211ab4382d2SGreg Kroah-Hartman 
uart_enable_ms(struct uart_port * uport)12121fdc3106SAlexander Shiyan static void uart_enable_ms(struct uart_port *uport)
12131fdc3106SAlexander Shiyan {
12141fdc3106SAlexander Shiyan 	/*
12151fdc3106SAlexander Shiyan 	 * Force modem status interrupts on
12161fdc3106SAlexander Shiyan 	 */
12171fdc3106SAlexander Shiyan 	if (uport->ops->enable_ms)
12181fdc3106SAlexander Shiyan 		uport->ops->enable_ms(uport);
12191fdc3106SAlexander Shiyan }
12201fdc3106SAlexander Shiyan 
1221ab4382d2SGreg Kroah-Hartman /*
1222ab4382d2SGreg Kroah-Hartman  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1223ab4382d2SGreg Kroah-Hartman  * - mask passed in arg for lines of interest
1224ab4382d2SGreg Kroah-Hartman  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1225ab4382d2SGreg Kroah-Hartman  * Caller should use TIOCGICOUNT to see which one it was
1226ab4382d2SGreg Kroah-Hartman  *
1227ab4382d2SGreg Kroah-Hartman  * FIXME: This wants extracting into a common all driver implementation
1228ab4382d2SGreg Kroah-Hartman  * of TIOCMWAIT using tty_port.
1229ab4382d2SGreg Kroah-Hartman  */
uart_wait_modem_status(struct uart_state * state,unsigned long arg)12309ed19428SPeter Hurley static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1231ab4382d2SGreg Kroah-Hartman {
12329ed19428SPeter Hurley 	struct uart_port *uport;
1233ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1234ab4382d2SGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
1235ab4382d2SGreg Kroah-Hartman 	struct uart_icount cprev, cnow;
1236ab4382d2SGreg Kroah-Hartman 	int ret;
1237ab4382d2SGreg Kroah-Hartman 
1238ab4382d2SGreg Kroah-Hartman 	/*
1239ab4382d2SGreg Kroah-Hartman 	 * note the counters on entry
1240ab4382d2SGreg Kroah-Hartman 	 */
12419ed19428SPeter Hurley 	uport = uart_port_ref(state);
12429ed19428SPeter Hurley 	if (!uport)
12439ed19428SPeter Hurley 		return -EIO;
1244559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
1245ab4382d2SGreg Kroah-Hartman 	memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
12461fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1247559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
1248ab4382d2SGreg Kroah-Hartman 
1249ab4382d2SGreg Kroah-Hartman 	add_wait_queue(&port->delta_msr_wait, &wait);
1250ab4382d2SGreg Kroah-Hartman 	for (;;) {
1251559c7ff4SThomas Gleixner 		uart_port_lock_irq(uport);
1252ab4382d2SGreg Kroah-Hartman 		memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1253559c7ff4SThomas Gleixner 		uart_port_unlock_irq(uport);
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
1256ab4382d2SGreg Kroah-Hartman 
1257ab4382d2SGreg Kroah-Hartman 		if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1258ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1259ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1260ab4382d2SGreg Kroah-Hartman 		    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1261ab4382d2SGreg Kroah-Hartman 			ret = 0;
1262ab4382d2SGreg Kroah-Hartman 			break;
1263ab4382d2SGreg Kroah-Hartman 		}
1264ab4382d2SGreg Kroah-Hartman 
1265ab4382d2SGreg Kroah-Hartman 		schedule();
1266ab4382d2SGreg Kroah-Hartman 
1267ab4382d2SGreg Kroah-Hartman 		/* see if a signal did it */
1268ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current)) {
1269ab4382d2SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
1270ab4382d2SGreg Kroah-Hartman 			break;
1271ab4382d2SGreg Kroah-Hartman 		}
1272ab4382d2SGreg Kroah-Hartman 
1273ab4382d2SGreg Kroah-Hartman 		cprev = cnow;
1274ab4382d2SGreg Kroah-Hartman 	}
127597f9f707SFabian Frederick 	__set_current_state(TASK_RUNNING);
1276ab4382d2SGreg Kroah-Hartman 	remove_wait_queue(&port->delta_msr_wait, &wait);
12779ed19428SPeter Hurley 	uart_port_deref(uport);
1278ab4382d2SGreg Kroah-Hartman 
1279ab4382d2SGreg Kroah-Hartman 	return ret;
1280ab4382d2SGreg Kroah-Hartman }
1281ab4382d2SGreg Kroah-Hartman 
1282ab4382d2SGreg Kroah-Hartman /*
1283ab4382d2SGreg Kroah-Hartman  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1284ab4382d2SGreg Kroah-Hartman  * Return: write counters to the user passed counter struct
1285ab4382d2SGreg Kroah-Hartman  * NB: both 1->0 and 0->1 transitions are counted except for
1286ab4382d2SGreg Kroah-Hartman  *     RI where only 0->1 is counted.
1287ab4382d2SGreg Kroah-Hartman  */
uart_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)1288ab4382d2SGreg Kroah-Hartman static int uart_get_icount(struct tty_struct *tty,
1289ab4382d2SGreg Kroah-Hartman 			  struct serial_icounter_struct *icount)
1290ab4382d2SGreg Kroah-Hartman {
1291ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1292ab4382d2SGreg Kroah-Hartman 	struct uart_icount cnow;
12939ed19428SPeter Hurley 	struct uart_port *uport;
1294ab4382d2SGreg Kroah-Hartman 
12959ed19428SPeter Hurley 	uport = uart_port_ref(state);
12969ed19428SPeter Hurley 	if (!uport)
12979ed19428SPeter Hurley 		return -EIO;
1298559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
1299ab4382d2SGreg Kroah-Hartman 	memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1300559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
13019ed19428SPeter Hurley 	uart_port_deref(uport);
1302ab4382d2SGreg Kroah-Hartman 
1303ab4382d2SGreg Kroah-Hartman 	icount->cts         = cnow.cts;
1304ab4382d2SGreg Kroah-Hartman 	icount->dsr         = cnow.dsr;
1305ab4382d2SGreg Kroah-Hartman 	icount->rng         = cnow.rng;
1306ab4382d2SGreg Kroah-Hartman 	icount->dcd         = cnow.dcd;
1307ab4382d2SGreg Kroah-Hartman 	icount->rx          = cnow.rx;
1308ab4382d2SGreg Kroah-Hartman 	icount->tx          = cnow.tx;
1309ab4382d2SGreg Kroah-Hartman 	icount->frame       = cnow.frame;
1310ab4382d2SGreg Kroah-Hartman 	icount->overrun     = cnow.overrun;
1311ab4382d2SGreg Kroah-Hartman 	icount->parity      = cnow.parity;
1312ab4382d2SGreg Kroah-Hartman 	icount->brk         = cnow.brk;
1313ab4382d2SGreg Kroah-Hartman 	icount->buf_overrun = cnow.buf_overrun;
1314ab4382d2SGreg Kroah-Hartman 
1315ab4382d2SGreg Kroah-Hartman 	return 0;
1316ab4382d2SGreg Kroah-Hartman }
1317ab4382d2SGreg Kroah-Hartman 
131851ad36baSIlpo Järvinen #define SER_RS485_LEGACY_FLAGS	(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
131951ad36baSIlpo Järvinen 				 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
132051ad36baSIlpo Järvinen 				 SER_RS485_TERMINATE_BUS)
132151ad36baSIlpo Järvinen 
uart_check_rs485_flags(struct uart_port * port,struct serial_rs485 * rs485)132251ad36baSIlpo Järvinen static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
132351ad36baSIlpo Järvinen {
132451ad36baSIlpo Järvinen 	u32 flags = rs485->flags;
132551ad36baSIlpo Järvinen 
132651ad36baSIlpo Järvinen 	/* Don't return -EINVAL for unsupported legacy flags */
132751ad36baSIlpo Järvinen 	flags &= ~SER_RS485_LEGACY_FLAGS;
132851ad36baSIlpo Järvinen 
132951ad36baSIlpo Järvinen 	/*
133051ad36baSIlpo Järvinen 	 * For any bit outside of the legacy ones that is not supported by
133151ad36baSIlpo Järvinen 	 * the driver, return -EINVAL.
133251ad36baSIlpo Järvinen 	 */
13330139da50SIlpo Järvinen 	if (flags & ~port->rs485_supported.flags)
133451ad36baSIlpo Järvinen 		return -EINVAL;
133551ad36baSIlpo Järvinen 
13364f768e94SIlpo Järvinen 	/* Asking for address w/o addressing mode? */
13374f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDRB) &&
13384f768e94SIlpo Järvinen 	    (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
13394f768e94SIlpo Järvinen 		return -EINVAL;
13404f768e94SIlpo Järvinen 
13414f768e94SIlpo Järvinen 	/* Address given but not enabled? */
13424f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
13434f768e94SIlpo Järvinen 		return -EINVAL;
13444f768e94SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
13454f768e94SIlpo Järvinen 		return -EINVAL;
13464f768e94SIlpo Järvinen 
134751ad36baSIlpo Järvinen 	return 0;
134851ad36baSIlpo Järvinen }
134951ad36baSIlpo Järvinen 
uart_sanitize_serial_rs485_delays(struct uart_port * port,struct serial_rs485 * rs485)1350d8fcd9cfSLino Sanfilippo static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
1351d8fcd9cfSLino Sanfilippo 					      struct serial_rs485 *rs485)
13522dbd0c14SIlpo Järvinen {
13530139da50SIlpo Järvinen 	if (!port->rs485_supported.delay_rts_before_send) {
1354be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_before_send) {
1355be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1356be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay before sending not supported\n",
1357be2e2cb1SIlpo Järvinen 				port->name, port->line);
1358be2e2cb1SIlpo Järvinen 		}
1359be2e2cb1SIlpo Järvinen 		rs485->delay_rts_before_send = 0;
1360be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
13612dbd0c14SIlpo Järvinen 		rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
13622dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13632dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay before sending clamped to %u ms\n",
13642dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_before_send);
13652dbd0c14SIlpo Järvinen 	}
13662dbd0c14SIlpo Järvinen 
13670139da50SIlpo Järvinen 	if (!port->rs485_supported.delay_rts_after_send) {
1368be2e2cb1SIlpo Järvinen 		if (rs485->delay_rts_after_send) {
1369be2e2cb1SIlpo Järvinen 			dev_warn_ratelimited(port->dev,
1370be2e2cb1SIlpo Järvinen 				"%s (%d): RTS delay after sending not supported\n",
1371be2e2cb1SIlpo Järvinen 				port->name, port->line);
1372be2e2cb1SIlpo Järvinen 		}
1373be2e2cb1SIlpo Järvinen 		rs485->delay_rts_after_send = 0;
1374be2e2cb1SIlpo Järvinen 	} else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
13752dbd0c14SIlpo Järvinen 		rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
13762dbd0c14SIlpo Järvinen 		dev_warn_ratelimited(port->dev,
13772dbd0c14SIlpo Järvinen 			"%s (%d): RTS delay after sending clamped to %u ms\n",
13782dbd0c14SIlpo Järvinen 			port->name, port->line, rs485->delay_rts_after_send);
13792dbd0c14SIlpo Järvinen 	}
1380d8fcd9cfSLino Sanfilippo }
1381d8fcd9cfSLino Sanfilippo 
uart_sanitize_serial_rs485(struct uart_port * port,struct serial_rs485 * rs485)1382d8fcd9cfSLino Sanfilippo static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1383d8fcd9cfSLino Sanfilippo {
1384d8fcd9cfSLino Sanfilippo 	u32 supported_flags = port->rs485_supported.flags;
1385d8fcd9cfSLino Sanfilippo 
1386d8fcd9cfSLino Sanfilippo 	if (!(rs485->flags & SER_RS485_ENABLED)) {
1387d8fcd9cfSLino Sanfilippo 		memset(rs485, 0, sizeof(*rs485));
1388d8fcd9cfSLino Sanfilippo 		return;
1389d8fcd9cfSLino Sanfilippo 	}
1390d8fcd9cfSLino Sanfilippo 
13916056f20fSCrescent CY Hsieh 	/* Clear other RS485 flags but SER_RS485_TERMINATE_BUS and return if enabling RS422 */
13926056f20fSCrescent CY Hsieh 	if (rs485->flags & SER_RS485_MODE_RS422) {
13936056f20fSCrescent CY Hsieh 		rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422 | SER_RS485_TERMINATE_BUS);
13946056f20fSCrescent CY Hsieh 		return;
13956056f20fSCrescent CY Hsieh 	}
13966056f20fSCrescent CY Hsieh 
13974afeced5SLino Sanfilippo 	rs485->flags &= supported_flags;
13984afeced5SLino Sanfilippo 
1399d8fcd9cfSLino Sanfilippo 	/* Pick sane settings if the user hasn't */
14004afeced5SLino Sanfilippo 	if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1401d8fcd9cfSLino Sanfilippo 	    !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
14024afeced5SLino Sanfilippo 		if (supported_flags & SER_RS485_RTS_ON_SEND) {
14034afeced5SLino Sanfilippo 			rs485->flags |= SER_RS485_RTS_ON_SEND;
14044afeced5SLino Sanfilippo 			rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
14054afeced5SLino Sanfilippo 
1406d8fcd9cfSLino Sanfilippo 			dev_warn_ratelimited(port->dev,
1407d8fcd9cfSLino Sanfilippo 				"%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1408d8fcd9cfSLino Sanfilippo 				port->name, port->line);
14094afeced5SLino Sanfilippo 		} else {
14104afeced5SLino Sanfilippo 			rs485->flags |= SER_RS485_RTS_AFTER_SEND;
14114afeced5SLino Sanfilippo 			rs485->flags &= ~SER_RS485_RTS_ON_SEND;
1412be2e2cb1SIlpo Järvinen 
14134afeced5SLino Sanfilippo 			dev_warn_ratelimited(port->dev,
14144afeced5SLino Sanfilippo 				"%s (%d): invalid RTS setting, using RTS_AFTER_SEND instead\n",
14154afeced5SLino Sanfilippo 				port->name, port->line);
14164afeced5SLino Sanfilippo 		}
14174afeced5SLino Sanfilippo 	}
1418be2e2cb1SIlpo Järvinen 
1419d8fcd9cfSLino Sanfilippo 	uart_sanitize_serial_rs485_delays(port, rs485);
1420d8fcd9cfSLino Sanfilippo 
14212dbd0c14SIlpo Järvinen 	/* Return clean padding area to userspace */
14224f768e94SIlpo Järvinen 	memset(rs485->padding0, 0, sizeof(rs485->padding0));
14234f768e94SIlpo Järvinen 	memset(rs485->padding1, 0, sizeof(rs485->padding1));
14242dbd0c14SIlpo Järvinen }
14252dbd0c14SIlpo Järvinen 
uart_set_rs485_termination(struct uart_port * port,const struct serial_rs485 * rs485)142644b27aecSLino Sanfilippo static void uart_set_rs485_termination(struct uart_port *port,
142744b27aecSLino Sanfilippo 				       const struct serial_rs485 *rs485)
142844b27aecSLino Sanfilippo {
142944b27aecSLino Sanfilippo 	if (!(rs485->flags & SER_RS485_ENABLED))
143044b27aecSLino Sanfilippo 		return;
143144b27aecSLino Sanfilippo 
143244b27aecSLino Sanfilippo 	gpiod_set_value_cansleep(port->rs485_term_gpio,
143344b27aecSLino Sanfilippo 				 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
143444b27aecSLino Sanfilippo }
143544b27aecSLino Sanfilippo 
uart_set_rs485_rx_during_tx(struct uart_port * port,const struct serial_rs485 * rs485)143607c30ea5SLino Sanfilippo static void uart_set_rs485_rx_during_tx(struct uart_port *port,
143707c30ea5SLino Sanfilippo 					const struct serial_rs485 *rs485)
143807c30ea5SLino Sanfilippo {
143907c30ea5SLino Sanfilippo 	if (!(rs485->flags & SER_RS485_ENABLED))
144007c30ea5SLino Sanfilippo 		return;
144107c30ea5SLino Sanfilippo 
144207c30ea5SLino Sanfilippo 	gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
144307c30ea5SLino Sanfilippo 				 !!(rs485->flags & SER_RS485_RX_DURING_TX));
144407c30ea5SLino Sanfilippo }
144507c30ea5SLino Sanfilippo 
uart_rs485_config(struct uart_port * port)14467c7f9bc9SLukas Wunner static int uart_rs485_config(struct uart_port *port)
14478322b1f5SIlpo Järvinen {
1448be2e2cb1SIlpo Järvinen 	struct serial_rs485 *rs485 = &port->rs485;
14498679328eSLukas Wunner 	unsigned long flags;
1450596a9171SIlpo Järvinen 	int ret;
1451be2e2cb1SIlpo Järvinen 
14528679328eSLukas Wunner 	if (!(rs485->flags & SER_RS485_ENABLED))
14538679328eSLukas Wunner 		return 0;
14548679328eSLukas Wunner 
1455be2e2cb1SIlpo Järvinen 	uart_sanitize_serial_rs485(port, rs485);
145644b27aecSLino Sanfilippo 	uart_set_rs485_termination(port, rs485);
145707c30ea5SLino Sanfilippo 	uart_set_rs485_rx_during_tx(port, rs485);
1458be2e2cb1SIlpo Järvinen 
1459fe2017baSGreg Kroah-Hartman 	uart_port_lock_irqsave(port, &flags);
1460ae50bb27SIlpo Järvinen 	ret = port->rs485_config(port, NULL, rs485);
1461fe2017baSGreg Kroah-Hartman 	uart_port_unlock_irqrestore(port, flags);
146207c30ea5SLino Sanfilippo 	if (ret) {
1463596a9171SIlpo Järvinen 		memset(rs485, 0, sizeof(*rs485));
146407c30ea5SLino Sanfilippo 		/* unset GPIOs */
146507c30ea5SLino Sanfilippo 		gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
146607c30ea5SLino Sanfilippo 		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
146707c30ea5SLino Sanfilippo 	}
1468596a9171SIlpo Järvinen 
1469596a9171SIlpo Järvinen 	return ret;
14708322b1f5SIlpo Järvinen }
14718322b1f5SIlpo Järvinen 
uart_get_rs485_config(struct uart_port * port,struct serial_rs485 __user * rs485)1472a5f276f1SRicardo Ribalda Delgado static int uart_get_rs485_config(struct uart_port *port,
1473a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485)
1474a5f276f1SRicardo Ribalda Delgado {
1475bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1476bd737f87SRicardo Ribalda Delgado 	struct serial_rs485 aux;
1477bd737f87SRicardo Ribalda Delgado 
1478559c7ff4SThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
1479bd737f87SRicardo Ribalda Delgado 	aux = port->rs485;
1480559c7ff4SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
1481bd737f87SRicardo Ribalda Delgado 
1482bd737f87SRicardo Ribalda Delgado 	if (copy_to_user(rs485, &aux, sizeof(aux)))
1483a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1484bd737f87SRicardo Ribalda Delgado 
1485a5f276f1SRicardo Ribalda Delgado 	return 0;
1486a5f276f1SRicardo Ribalda Delgado }
1487a5f276f1SRicardo Ribalda Delgado 
uart_set_rs485_config(struct tty_struct * tty,struct uart_port * port,struct serial_rs485 __user * rs485_user)1488ae50bb27SIlpo Järvinen static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
1489a5f276f1SRicardo Ribalda Delgado 			 struct serial_rs485 __user *rs485_user)
1490a5f276f1SRicardo Ribalda Delgado {
1491a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485 rs485;
1492a5f276f1SRicardo Ribalda Delgado 	int ret;
1493bd737f87SRicardo Ribalda Delgado 	unsigned long flags;
1494a5f276f1SRicardo Ribalda Delgado 
1495c7398691SLino Sanfilippo 	if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
149679c5966cSJohan Hovold 		return -ENOTTY;
1497a5f276f1SRicardo Ribalda Delgado 
1498a5f276f1SRicardo Ribalda Delgado 	if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1499a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1500a5f276f1SRicardo Ribalda Delgado 
150151ad36baSIlpo Järvinen 	ret = uart_check_rs485_flags(port, &rs485);
150251ad36baSIlpo Järvinen 	if (ret)
150351ad36baSIlpo Järvinen 		return ret;
15042dbd0c14SIlpo Järvinen 	uart_sanitize_serial_rs485(port, &rs485);
150544b27aecSLino Sanfilippo 	uart_set_rs485_termination(port, &rs485);
150607c30ea5SLino Sanfilippo 	uart_set_rs485_rx_during_tx(port, &rs485);
15070ed12afaSLino Sanfilippo 
1508559c7ff4SThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
1509ae50bb27SIlpo Järvinen 	ret = port->rs485_config(port, &tty->termios, &rs485);
15107c7f9bc9SLukas Wunner 	if (!ret) {
15110ed12afaSLino Sanfilippo 		port->rs485 = rs485;
15127c7f9bc9SLukas Wunner 
15137c7f9bc9SLukas Wunner 		/* Reset RTS and other mctrl lines when disabling RS485 */
15147c7f9bc9SLukas Wunner 		if (!(rs485.flags & SER_RS485_ENABLED))
15157c7f9bc9SLukas Wunner 			port->ops->set_mctrl(port, port->mctrl);
15167c7f9bc9SLukas Wunner 	}
1517559c7ff4SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
151807c30ea5SLino Sanfilippo 	if (ret) {
151907c30ea5SLino Sanfilippo 		/* restore old GPIO settings */
152007c30ea5SLino Sanfilippo 		gpiod_set_value_cansleep(port->rs485_term_gpio,
152107c30ea5SLino Sanfilippo 			!!(port->rs485.flags & SER_RS485_TERMINATE_BUS));
152207c30ea5SLino Sanfilippo 		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
152307c30ea5SLino Sanfilippo 			!!(port->rs485.flags & SER_RS485_RX_DURING_TX));
1524a5f276f1SRicardo Ribalda Delgado 		return ret;
152507c30ea5SLino Sanfilippo 	}
1526a5f276f1SRicardo Ribalda Delgado 
1527a5f276f1SRicardo Ribalda Delgado 	if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1528a5f276f1SRicardo Ribalda Delgado 		return -EFAULT;
1529a5f276f1SRicardo Ribalda Delgado 
1530a5f276f1SRicardo Ribalda Delgado 	return 0;
1531a5f276f1SRicardo Ribalda Delgado }
1532a5f276f1SRicardo Ribalda Delgado 
uart_get_iso7816_config(struct uart_port * port,struct serial_iso7816 __user * iso7816)1533ad8c0eaaSNicolas Ferre static int uart_get_iso7816_config(struct uart_port *port,
1534ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816)
1535ad8c0eaaSNicolas Ferre {
1536ad8c0eaaSNicolas Ferre 	unsigned long flags;
1537ad8c0eaaSNicolas Ferre 	struct serial_iso7816 aux;
1538ad8c0eaaSNicolas Ferre 
1539ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
154079c5966cSJohan Hovold 		return -ENOTTY;
1541ad8c0eaaSNicolas Ferre 
1542559c7ff4SThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
1543ad8c0eaaSNicolas Ferre 	aux = port->iso7816;
1544559c7ff4SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
1545ad8c0eaaSNicolas Ferre 
1546ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816, &aux, sizeof(aux)))
1547ad8c0eaaSNicolas Ferre 		return -EFAULT;
1548ad8c0eaaSNicolas Ferre 
1549ad8c0eaaSNicolas Ferre 	return 0;
1550ad8c0eaaSNicolas Ferre }
1551ad8c0eaaSNicolas Ferre 
uart_set_iso7816_config(struct uart_port * port,struct serial_iso7816 __user * iso7816_user)1552ad8c0eaaSNicolas Ferre static int uart_set_iso7816_config(struct uart_port *port,
1553ad8c0eaaSNicolas Ferre 				   struct serial_iso7816 __user *iso7816_user)
1554ad8c0eaaSNicolas Ferre {
1555ad8c0eaaSNicolas Ferre 	struct serial_iso7816 iso7816;
1556ad8c0eaaSNicolas Ferre 	int i, ret;
1557ad8c0eaaSNicolas Ferre 	unsigned long flags;
1558ad8c0eaaSNicolas Ferre 
1559ad8c0eaaSNicolas Ferre 	if (!port->iso7816_config)
156079c5966cSJohan Hovold 		return -ENOTTY;
1561ad8c0eaaSNicolas Ferre 
1562ad8c0eaaSNicolas Ferre 	if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1563ad8c0eaaSNicolas Ferre 		return -EFAULT;
1564ad8c0eaaSNicolas Ferre 
1565ad8c0eaaSNicolas Ferre 	/*
1566ad8c0eaaSNicolas Ferre 	 * There are 5 words reserved for future use. Check that userspace
1567ad8c0eaaSNicolas Ferre 	 * doesn't put stuff in there to prevent breakages in the future.
1568ad8c0eaaSNicolas Ferre 	 */
1569eff37b5eSIlpo Järvinen 	for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++)
1570ad8c0eaaSNicolas Ferre 		if (iso7816.reserved[i])
1571ad8c0eaaSNicolas Ferre 			return -EINVAL;
1572ad8c0eaaSNicolas Ferre 
1573559c7ff4SThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
1574ad8c0eaaSNicolas Ferre 	ret = port->iso7816_config(port, &iso7816);
1575559c7ff4SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
1576ad8c0eaaSNicolas Ferre 	if (ret)
1577ad8c0eaaSNicolas Ferre 		return ret;
1578ad8c0eaaSNicolas Ferre 
1579ad8c0eaaSNicolas Ferre 	if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1580ad8c0eaaSNicolas Ferre 		return -EFAULT;
1581ad8c0eaaSNicolas Ferre 
1582ad8c0eaaSNicolas Ferre 	return 0;
1583ad8c0eaaSNicolas Ferre }
1584ad8c0eaaSNicolas Ferre 
1585ab4382d2SGreg Kroah-Hartman /*
1586ab4382d2SGreg Kroah-Hartman  * Called via sys_ioctl.  We can use spin_lock_irq() here.
1587ab4382d2SGreg Kroah-Hartman  */
1588ab4382d2SGreg Kroah-Hartman static int
uart_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)15894047b371SPeter Hurley uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1590ab4382d2SGreg Kroah-Hartman {
1591ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1592ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
15934047b371SPeter Hurley 	struct uart_port *uport;
1594ab4382d2SGreg Kroah-Hartman 	void __user *uarg = (void __user *)arg;
1595ab4382d2SGreg Kroah-Hartman 	int ret = -ENOIOCTLCMD;
1596ab4382d2SGreg Kroah-Hartman 
1597ab4382d2SGreg Kroah-Hartman 
1598ab4382d2SGreg Kroah-Hartman 	/*
1599ab4382d2SGreg Kroah-Hartman 	 * These ioctls don't rely on the hardware to be present.
1600ab4382d2SGreg Kroah-Hartman 	 */
1601ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1602ab4382d2SGreg Kroah-Hartman 	case TIOCSERCONFIG:
16037c8ab967SPeter Hurley 		down_write(&tty->termios_rwsem);
1604ab4382d2SGreg Kroah-Hartman 		ret = uart_do_autoconfig(tty, state);
16057c8ab967SPeter Hurley 		up_write(&tty->termios_rwsem);
1606ab4382d2SGreg Kroah-Hartman 		break;
1607ab4382d2SGreg Kroah-Hartman 	}
1608ab4382d2SGreg Kroah-Hartman 
1609ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1610ab4382d2SGreg Kroah-Hartman 		goto out;
1611ab4382d2SGreg Kroah-Hartman 
161218900ca6SPeter Hurley 	if (tty_io_error(tty)) {
1613ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1614ab4382d2SGreg Kroah-Hartman 		goto out;
1615ab4382d2SGreg Kroah-Hartman 	}
1616ab4382d2SGreg Kroah-Hartman 
1617ab4382d2SGreg Kroah-Hartman 	/*
1618ab4382d2SGreg Kroah-Hartman 	 * The following should only be used when hardware is present.
1619ab4382d2SGreg Kroah-Hartman 	 */
1620ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1621ab4382d2SGreg Kroah-Hartman 	case TIOCMIWAIT:
1622ab4382d2SGreg Kroah-Hartman 		ret = uart_wait_modem_status(state, arg);
1623ab4382d2SGreg Kroah-Hartman 		break;
1624ab4382d2SGreg Kroah-Hartman 	}
1625ab4382d2SGreg Kroah-Hartman 
1626ab4382d2SGreg Kroah-Hartman 	if (ret != -ENOIOCTLCMD)
1627ab4382d2SGreg Kroah-Hartman 		goto out;
1628ab4382d2SGreg Kroah-Hartman 
1629ae50bb27SIlpo Järvinen 	/* rs485_config requires more locking than others */
16309e4f2a80SJohan Hovold 	if (cmd == TIOCSRS485)
1631ae50bb27SIlpo Järvinen 		down_write(&tty->termios_rwsem);
1632ae50bb27SIlpo Järvinen 
1633ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
16344047b371SPeter Hurley 	uport = uart_port_check(state);
1635ab4382d2SGreg Kroah-Hartman 
16364047b371SPeter Hurley 	if (!uport || tty_io_error(tty)) {
1637ab4382d2SGreg Kroah-Hartman 		ret = -EIO;
1638ab4382d2SGreg Kroah-Hartman 		goto out_up;
1639ab4382d2SGreg Kroah-Hartman 	}
1640ab4382d2SGreg Kroah-Hartman 
1641ab4382d2SGreg Kroah-Hartman 	/*
1642ab4382d2SGreg Kroah-Hartman 	 * All these rely on hardware being present and need to be
1643ab4382d2SGreg Kroah-Hartman 	 * protected against the tty being hung up.
1644ab4382d2SGreg Kroah-Hartman 	 */
1645a9c20a9cSRicardo Ribalda Delgado 
1646ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1647a9c20a9cSRicardo Ribalda Delgado 	case TIOCSERGETLSR: /* Get line status register */
1648a9c20a9cSRicardo Ribalda Delgado 		ret = uart_get_lsr_info(tty, state, uarg);
1649a9c20a9cSRicardo Ribalda Delgado 		break;
1650a9c20a9cSRicardo Ribalda Delgado 
1651a5f276f1SRicardo Ribalda Delgado 	case TIOCGRS485:
16524047b371SPeter Hurley 		ret = uart_get_rs485_config(uport, uarg);
1653a5f276f1SRicardo Ribalda Delgado 		break;
1654a5f276f1SRicardo Ribalda Delgado 
1655a5f276f1SRicardo Ribalda Delgado 	case TIOCSRS485:
1656ae50bb27SIlpo Järvinen 		ret = uart_set_rs485_config(tty, uport, uarg);
1657a5f276f1SRicardo Ribalda Delgado 		break;
1658ad8c0eaaSNicolas Ferre 
1659ad8c0eaaSNicolas Ferre 	case TIOCSISO7816:
1660ad8c0eaaSNicolas Ferre 		ret = uart_set_iso7816_config(state->uart_port, uarg);
1661ad8c0eaaSNicolas Ferre 		break;
1662ad8c0eaaSNicolas Ferre 
1663ad8c0eaaSNicolas Ferre 	case TIOCGISO7816:
1664ad8c0eaaSNicolas Ferre 		ret = uart_get_iso7816_config(state->uart_port, uarg);
1665ad8c0eaaSNicolas Ferre 		break;
16664047b371SPeter Hurley 	default:
1667ab4382d2SGreg Kroah-Hartman 		if (uport->ops->ioctl)
1668ab4382d2SGreg Kroah-Hartman 			ret = uport->ops->ioctl(uport, cmd, arg);
1669ab4382d2SGreg Kroah-Hartman 		break;
1670ab4382d2SGreg Kroah-Hartman 	}
1671ab4382d2SGreg Kroah-Hartman out_up:
1672ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
16739e4f2a80SJohan Hovold 	if (cmd == TIOCSRS485)
1674ae50bb27SIlpo Järvinen 		up_write(&tty->termios_rwsem);
1675ab4382d2SGreg Kroah-Hartman out:
1676ab4382d2SGreg Kroah-Hartman 	return ret;
1677ab4382d2SGreg Kroah-Hartman }
1678ab4382d2SGreg Kroah-Hartman 
uart_set_ldisc(struct tty_struct * tty)1679ab4382d2SGreg Kroah-Hartman static void uart_set_ldisc(struct tty_struct *tty)
1680ab4382d2SGreg Kroah-Hartman {
1681ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
16824047b371SPeter Hurley 	struct uart_port *uport;
16832f70e49eSAlexey Kardashevskiy 	struct tty_port *port = &state->port;
16842f70e49eSAlexey Kardashevskiy 
16852f70e49eSAlexey Kardashevskiy 	if (!tty_port_initialized(port))
16862f70e49eSAlexey Kardashevskiy 		return;
1687ab4382d2SGreg Kroah-Hartman 
1688db1b9dfcSPeter Hurley 	mutex_lock(&state->port.mutex);
16894047b371SPeter Hurley 	uport = uart_port_check(state);
16904047b371SPeter Hurley 	if (uport && uport->ops->set_ldisc)
1691732a84a0SPeter Hurley 		uport->ops->set_ldisc(uport, &tty->termios);
1692db1b9dfcSPeter Hurley 	mutex_unlock(&state->port.mutex);
1693db1b9dfcSPeter Hurley }
1694ab4382d2SGreg Kroah-Hartman 
uart_set_termios(struct tty_struct * tty,const struct ktermios * old_termios)1695ab4382d2SGreg Kroah-Hartman static void uart_set_termios(struct tty_struct *tty,
1696a8c11c15SIlpo Järvinen 			     const struct ktermios *old_termios)
1697ab4382d2SGreg Kroah-Hartman {
1698ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
16994047b371SPeter Hurley 	struct uart_port *uport;
1700adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
17012cbacafdSRussell King 	unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
17022cbacafdSRussell King 	bool sw_changed = false;
1703ab4382d2SGreg Kroah-Hartman 
1704*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&state->port.mutex);
1705*5879adbfSJiri Slaby (SUSE) 
17064047b371SPeter Hurley 	uport = uart_port_check(state);
17074047b371SPeter Hurley 	if (!uport)
1708*5879adbfSJiri Slaby (SUSE) 		return;
17094047b371SPeter Hurley 
17102cbacafdSRussell King 	/*
17112cbacafdSRussell King 	 * Drivers doing software flow control also need to know
17122cbacafdSRussell King 	 * about changes to these input settings.
17132cbacafdSRussell King 	 */
17142cbacafdSRussell King 	if (uport->flags & UPF_SOFT_FLOW) {
17152cbacafdSRussell King 		iflag_mask |= IXANY|IXON|IXOFF;
17162cbacafdSRussell King 		sw_changed =
17172cbacafdSRussell King 		   tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
17182cbacafdSRussell King 		   tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
17192cbacafdSRussell King 	}
1720ab4382d2SGreg Kroah-Hartman 
1721ab4382d2SGreg Kroah-Hartman 	/*
1722ab4382d2SGreg Kroah-Hartman 	 * These are the bits that are used to setup various
1723ab4382d2SGreg Kroah-Hartman 	 * flags in the low level driver. We can ignore the Bfoo
1724ab4382d2SGreg Kroah-Hartman 	 * bits in c_cflag; c_[io]speed will always be set
1725ab4382d2SGreg Kroah-Hartman 	 * appropriately by set_termios() in tty_ioctl.c
1726ab4382d2SGreg Kroah-Hartman 	 */
1727ab4382d2SGreg Kroah-Hartman 	if ((cflag ^ old_termios->c_cflag) == 0 &&
1728adc8d746SAlan Cox 	    tty->termios.c_ospeed == old_termios->c_ospeed &&
1729adc8d746SAlan Cox 	    tty->termios.c_ispeed == old_termios->c_ispeed &&
17302cbacafdSRussell King 	    ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1731*5879adbfSJiri Slaby (SUSE) 	    !sw_changed)
1732*5879adbfSJiri Slaby (SUSE) 		return;
1733ab4382d2SGreg Kroah-Hartman 
1734826736a6SIlpo Järvinen 	uart_change_line_settings(tty, state, old_termios);
1735c7a6b9e4SHariprasad Kelam 	/* reload cflag from termios; port driver may have overridden flags */
1736c18b55fdSPeter Hurley 	cflag = tty->termios.c_cflag;
1737ab4382d2SGreg Kroah-Hartman 
1738ab4382d2SGreg Kroah-Hartman 	/* Handle transition to B0 status */
1739807fccf7SIlpo Järvinen 	if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0))
1740dec94e70SRussell King 		uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1741ab4382d2SGreg Kroah-Hartman 	/* Handle transition away from B0 status */
1742807fccf7SIlpo Järvinen 	else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) {
1743ab4382d2SGreg Kroah-Hartman 		unsigned int mask = TIOCM_DTR;
17444ed71addSTamseel Shams 
174597ef38b8SPeter Hurley 		if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1746ab4382d2SGreg Kroah-Hartman 			mask |= TIOCM_RTS;
1747dec94e70SRussell King 		uart_set_mctrl(uport, mask);
1748ab4382d2SGreg Kroah-Hartman 	}
1749ab4382d2SGreg Kroah-Hartman }
1750ab4382d2SGreg Kroah-Hartman 
1751ab4382d2SGreg Kroah-Hartman /*
1752ef4f527cSKevin Cernekee  * Calls to uart_close() are serialised via the tty_lock in
1753ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_release()
1754ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1755ab4382d2SGreg Kroah-Hartman  */
uart_close(struct tty_struct * tty,struct file * filp)1756ab4382d2SGreg Kroah-Hartman static void uart_close(struct tty_struct *tty, struct file *filp)
1757ab4382d2SGreg Kroah-Hartman {
1758ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1759ab4382d2SGreg Kroah-Hartman 
176091b32f54SPeter Hurley 	if (!state) {
176191b32f54SPeter Hurley 		struct uart_driver *drv = tty->driver->driver_state;
17629356335fSColin Ian King 		struct tty_port *port;
176391b32f54SPeter Hurley 
176491b32f54SPeter Hurley 		state = drv->state + tty->index;
176591b32f54SPeter Hurley 		port = &state->port;
176691b32f54SPeter Hurley 		spin_lock_irq(&port->lock);
176791b32f54SPeter Hurley 		--port->count;
176891b32f54SPeter Hurley 		spin_unlock_irq(&port->lock);
1769ab4382d2SGreg Kroah-Hartman 		return;
177091b32f54SPeter Hurley 	}
1771ab4382d2SGreg Kroah-Hartman 
177239b3d892SPeter Hurley 	pr_debug("uart_close(%d) called\n", tty->index);
1773ab4382d2SGreg Kroah-Hartman 
1774761ed4a9SRob Herring 	tty_port_close(tty->port, tty, filp);
1775761ed4a9SRob Herring }
1776ab4382d2SGreg Kroah-Hartman 
uart_tty_port_shutdown(struct tty_port * port)1777761ed4a9SRob Herring static void uart_tty_port_shutdown(struct tty_port *port)
1778761ed4a9SRob Herring {
1779761ed4a9SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
1780761ed4a9SRob Herring 	struct uart_port *uport = uart_port_check(state);
1781af224ca2SPeter Hurley 
1782ab4382d2SGreg Kroah-Hartman 	/*
1783ab4382d2SGreg Kroah-Hartman 	 * At this point, we stop accepting input.  To do this, we
1784ab4382d2SGreg Kroah-Hartman 	 * disable the receive line status interrupts.
1785ab4382d2SGreg Kroah-Hartman 	 */
1786a5a2b130SAndy Shevchenko 	if (WARN(!uport, "detached port still initialized!\n"))
1787a5a2b130SAndy Shevchenko 		return;
1788761ed4a9SRob Herring 
1789559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
1790ab4382d2SGreg Kroah-Hartman 	uport->ops->stop_rx(uport);
1791559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
1792761ed4a9SRob Herring 
17931aa4ad4eSTony Lindgren 	serial_base_port_shutdown(uport);
1794761ed4a9SRob Herring 	uart_port_shutdown(port);
1795761ed4a9SRob Herring 
1796ab4382d2SGreg Kroah-Hartman 	/*
1797761ed4a9SRob Herring 	 * It's possible for shutdown to be called after suspend if we get
1798761ed4a9SRob Herring 	 * a DCD drop (hangup) at just the right time.  Clear suspended bit so
1799761ed4a9SRob Herring 	 * we don't try to resume a port that has been shutdown.
1800ab4382d2SGreg Kroah-Hartman 	 */
180175b20a2aSIlpo Järvinen 	tty_port_set_suspended(port, false);
1802ab4382d2SGreg Kroah-Hartman 
1803abcd8632SAndy Shevchenko 	uart_free_xmit_buf(port);
180400de977fSJohan Hovold 
180500de977fSJohan Hovold 	uart_change_pm(state, UART_PM_STATE_OFF);
1806ab4382d2SGreg Kroah-Hartman }
1807ab4382d2SGreg Kroah-Hartman 
uart_wait_until_sent(struct tty_struct * tty,int timeout)18081f33a51dSJiri Slaby static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1809ab4382d2SGreg Kroah-Hartman {
18101f33a51dSJiri Slaby 	struct uart_state *state = tty->driver_data;
18119ed19428SPeter Hurley 	struct uart_port *port;
1812f9008285SIlpo Järvinen 	unsigned long char_time, expire, fifo_timeout;
1813ab4382d2SGreg Kroah-Hartman 
18149ed19428SPeter Hurley 	port = uart_port_ref(state);
1815ef510beaSAndy Shevchenko 	if (!port)
1816ef510beaSAndy Shevchenko 		return;
1817ef510beaSAndy Shevchenko 
1818ef510beaSAndy Shevchenko 	if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
18199ed19428SPeter Hurley 		uart_port_deref(port);
1820ab4382d2SGreg Kroah-Hartman 		return;
18219ed19428SPeter Hurley 	}
1822ab4382d2SGreg Kroah-Hartman 
1823ab4382d2SGreg Kroah-Hartman 	/*
1824ab4382d2SGreg Kroah-Hartman 	 * Set the check interval to be 1/5 of the estimated time to
1825ab4382d2SGreg Kroah-Hartman 	 * send a single character, and make it at least 1.  The check
1826ab4382d2SGreg Kroah-Hartman 	 * interval should also be less than the timeout.
1827ab4382d2SGreg Kroah-Hartman 	 *
1828ab4382d2SGreg Kroah-Hartman 	 * Note: we have to use pretty tight timings here to satisfy
1829ab4382d2SGreg Kroah-Hartman 	 * the NIST-PCTS.
1830ab4382d2SGreg Kroah-Hartman 	 */
183131f6bd7fSIlpo Järvinen 	char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
183231f6bd7fSIlpo Järvinen 
1833ab4382d2SGreg Kroah-Hartman 	if (timeout && timeout < char_time)
1834ab4382d2SGreg Kroah-Hartman 		char_time = timeout;
1835ab4382d2SGreg Kroah-Hartman 
1836b0e0bd9dSTomasz Moń 	if (!uart_cts_enabled(port)) {
1837ab4382d2SGreg Kroah-Hartman 		/*
1838ab4382d2SGreg Kroah-Hartman 		 * If the transmitter hasn't cleared in twice the approximate
1839ab4382d2SGreg Kroah-Hartman 		 * amount of time to send the entire FIFO, it probably won't
1840ab4382d2SGreg Kroah-Hartman 		 * ever clear.  This assumes the UART isn't doing flow
1841ab4382d2SGreg Kroah-Hartman 		 * control, which is currently the case.  Hence, if it ever
1842f9008285SIlpo Järvinen 		 * takes longer than FIFO timeout, this is probably due to a
1843ab4382d2SGreg Kroah-Hartman 		 * UART bug of some kind.  So, we clamp the timeout parameter at
1844f9008285SIlpo Järvinen 		 * 2 * FIFO timeout.
1845ab4382d2SGreg Kroah-Hartman 		 */
1846f9008285SIlpo Järvinen 		fifo_timeout = uart_fifo_timeout(port);
1847f9008285SIlpo Järvinen 		if (timeout == 0 || timeout > 2 * fifo_timeout)
1848f9008285SIlpo Järvinen 			timeout = 2 * fifo_timeout;
1849b0e0bd9dSTomasz Moń 	}
1850ab4382d2SGreg Kroah-Hartman 
1851ab4382d2SGreg Kroah-Hartman 	expire = jiffies + timeout;
1852ab4382d2SGreg Kroah-Hartman 
1853ab4382d2SGreg Kroah-Hartman 	pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1854ab4382d2SGreg Kroah-Hartman 		port->line, jiffies, expire);
1855ab4382d2SGreg Kroah-Hartman 
1856ab4382d2SGreg Kroah-Hartman 	/*
1857ab4382d2SGreg Kroah-Hartman 	 * Check whether the transmitter is empty every 'char_time'.
1858ab4382d2SGreg Kroah-Hartman 	 * 'timeout' / 'expire' give us the maximum amount of time
1859ab4382d2SGreg Kroah-Hartman 	 * we wait.
1860ab4382d2SGreg Kroah-Hartman 	 */
1861ab4382d2SGreg Kroah-Hartman 	while (!port->ops->tx_empty(port)) {
1862ab4382d2SGreg Kroah-Hartman 		msleep_interruptible(jiffies_to_msecs(char_time));
1863ab4382d2SGreg Kroah-Hartman 		if (signal_pending(current))
1864ab4382d2SGreg Kroah-Hartman 			break;
1865b0e0bd9dSTomasz Moń 		if (timeout && time_after(jiffies, expire))
1866ab4382d2SGreg Kroah-Hartman 			break;
1867ab4382d2SGreg Kroah-Hartman 	}
18689ed19428SPeter Hurley 	uart_port_deref(port);
1869ab4382d2SGreg Kroah-Hartman }
1870ab4382d2SGreg Kroah-Hartman 
1871ab4382d2SGreg Kroah-Hartman /*
1872ef4f527cSKevin Cernekee  * Calls to uart_hangup() are serialised by the tty_lock in
1873ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:do_tty_hangup()
1874ef4f527cSKevin Cernekee  * This runs from a workqueue and can sleep for a _short_ time only.
1875ab4382d2SGreg Kroah-Hartman  */
uart_hangup(struct tty_struct * tty)1876ab4382d2SGreg Kroah-Hartman static void uart_hangup(struct tty_struct *tty)
1877ab4382d2SGreg Kroah-Hartman {
1878ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = tty->driver_data;
1879ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
1880af224ca2SPeter Hurley 	struct uart_port *uport;
1881ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1882ab4382d2SGreg Kroah-Hartman 
188339b3d892SPeter Hurley 	pr_debug("uart_hangup(%d)\n", tty->index);
1884ab4382d2SGreg Kroah-Hartman 
1885ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
1886af224ca2SPeter Hurley 	uport = uart_port_check(state);
1887af224ca2SPeter Hurley 	WARN(!uport, "hangup of detached port!\n");
1888af224ca2SPeter Hurley 
1889807c8d81SPeter Hurley 	if (tty_port_active(port)) {
1890ab4382d2SGreg Kroah-Hartman 		uart_flush_buffer(tty);
1891ab4382d2SGreg Kroah-Hartman 		uart_shutdown(tty, state);
1892ab4382d2SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
1893ab4382d2SGreg Kroah-Hartman 		port->count = 0;
1894ab4382d2SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
18959b5aa549SIlpo Järvinen 		tty_port_set_active(port, false);
1896ab4382d2SGreg Kroah-Hartman 		tty_port_tty_set(port, NULL);
1897af224ca2SPeter Hurley 		if (uport && !uart_console(uport))
1898bf903c0cSGeert Uytterhoeven 			uart_change_pm(state, UART_PM_STATE_OFF);
1899ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
1900ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->delta_msr_wait);
1901ab4382d2SGreg Kroah-Hartman 	}
1902ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
1903ab4382d2SGreg Kroah-Hartman }
1904ab4382d2SGreg Kroah-Hartman 
1905af224ca2SPeter Hurley /* uport == NULL if uart_port has already been removed */
uart_port_shutdown(struct tty_port * port)19060b1db830SJiri Slaby static void uart_port_shutdown(struct tty_port *port)
19070b1db830SJiri Slaby {
1908b922e19dSJiri Slaby 	struct uart_state *state = container_of(port, struct uart_state, port);
19094047b371SPeter Hurley 	struct uart_port *uport = uart_port_check(state);
1910b922e19dSJiri Slaby 
1911b922e19dSJiri Slaby 	/*
1912b922e19dSJiri Slaby 	 * clear delta_msr_wait queue to avoid mem leaks: we may free
1913b922e19dSJiri Slaby 	 * the irq here so the queue might never be woken up.  Note
1914b922e19dSJiri Slaby 	 * that we won't end up waiting on delta_msr_wait again since
1915b922e19dSJiri Slaby 	 * any outstanding file descriptors should be pointing at
1916b922e19dSJiri Slaby 	 * hung_up_tty_fops now.
1917b922e19dSJiri Slaby 	 */
1918b922e19dSJiri Slaby 	wake_up_interruptible(&port->delta_msr_wait);
1919b922e19dSJiri Slaby 
19202765852eSJiri Slaby 	if (uport) {
19212765852eSJiri Slaby 		/* Free the IRQ and disable the port. */
1922b922e19dSJiri Slaby 		uport->ops->shutdown(uport);
1923b922e19dSJiri Slaby 
19242765852eSJiri Slaby 		/* Ensure that the IRQ handler isn't running on another CPU. */
1925b922e19dSJiri Slaby 		synchronize_irq(uport->irq);
19260b1db830SJiri Slaby 	}
19272765852eSJiri Slaby }
19280b1db830SJiri Slaby 
uart_carrier_raised(struct tty_port * port)1929b300fb26SIlpo Järvinen static bool uart_carrier_raised(struct tty_port *port)
1930ab4382d2SGreg Kroah-Hartman {
1931ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
19329ed19428SPeter Hurley 	struct uart_port *uport;
1933ab4382d2SGreg Kroah-Hartman 	int mctrl;
19349ed19428SPeter Hurley 
19359ed19428SPeter Hurley 	uport = uart_port_ref(state);
19369ed19428SPeter Hurley 	/*
19379ed19428SPeter Hurley 	 * Should never observe uport == NULL since checks for hangup should
19389ed19428SPeter Hurley 	 * abort the tty_port_block_til_ready() loop before checking for carrier
19399ed19428SPeter Hurley 	 * raised -- but report carrier raised if it does anyway so open will
19409ed19428SPeter Hurley 	 * continue and not sleep
19419ed19428SPeter Hurley 	 */
19429ed19428SPeter Hurley 	if (WARN_ON(!uport))
1943b300fb26SIlpo Järvinen 		return true;
1944559c7ff4SThomas Gleixner 	uart_port_lock_irq(uport);
19451fdc3106SAlexander Shiyan 	uart_enable_ms(uport);
1946ab4382d2SGreg Kroah-Hartman 	mctrl = uport->ops->get_mctrl(uport);
1947559c7ff4SThomas Gleixner 	uart_port_unlock_irq(uport);
19489ed19428SPeter Hurley 	uart_port_deref(uport);
1949b300fb26SIlpo Järvinen 
1950b300fb26SIlpo Järvinen 	return mctrl & TIOCM_CAR;
1951ab4382d2SGreg Kroah-Hartman }
1952ab4382d2SGreg Kroah-Hartman 
uart_dtr_rts(struct tty_port * port,bool active)19535701cb8bSIlpo Järvinen static void uart_dtr_rts(struct tty_port *port, bool active)
1954ab4382d2SGreg Kroah-Hartman {
1955ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = container_of(port, struct uart_state, port);
19569ed19428SPeter Hurley 	struct uart_port *uport;
19579ed19428SPeter Hurley 
19589ed19428SPeter Hurley 	uport = uart_port_ref(state);
19599ed19428SPeter Hurley 	if (!uport)
19609ed19428SPeter Hurley 		return;
19615701cb8bSIlpo Järvinen 	uart_port_dtr_rts(uport, active);
19629ed19428SPeter Hurley 	uart_port_deref(uport);
1963ab4382d2SGreg Kroah-Hartman }
1964ab4382d2SGreg Kroah-Hartman 
uart_install(struct tty_driver * driver,struct tty_struct * tty)19654cdd17baSJiri Slaby static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
19664cdd17baSJiri Slaby {
19674cdd17baSJiri Slaby 	struct uart_driver *drv = driver->driver_state;
19684cdd17baSJiri Slaby 	struct uart_state *state = drv->state + tty->index;
19694cdd17baSJiri Slaby 
19704cdd17baSJiri Slaby 	tty->driver_data = state;
19714cdd17baSJiri Slaby 
19724cdd17baSJiri Slaby 	return tty_standard_install(driver, tty);
19734cdd17baSJiri Slaby }
19744cdd17baSJiri Slaby 
1975ab4382d2SGreg Kroah-Hartman /*
1976ef4f527cSKevin Cernekee  * Calls to uart_open are serialised by the tty_lock in
1977ef4f527cSKevin Cernekee  *   drivers/tty/tty_io.c:tty_open()
1978ab4382d2SGreg Kroah-Hartman  * Note that if this fails, then uart_close() _will_ be called.
1979ab4382d2SGreg Kroah-Hartman  *
1980ab4382d2SGreg Kroah-Hartman  * In time, we want to scrap the "opening nonpresent ports"
1981ab4382d2SGreg Kroah-Hartman  * behaviour and implement an alternative way for setserial
1982ab4382d2SGreg Kroah-Hartman  * to set base addresses/ports/types.  This will allow us to
1983ab4382d2SGreg Kroah-Hartman  * get rid of a certain amount of extra tests.
1984ab4382d2SGreg Kroah-Hartman  */
uart_open(struct tty_struct * tty,struct file * filp)1985ab4382d2SGreg Kroah-Hartman static int uart_open(struct tty_struct *tty, struct file *filp)
1986ab4382d2SGreg Kroah-Hartman {
19874cdd17baSJiri Slaby 	struct uart_state *state = tty->driver_data;
19884cdd17baSJiri Slaby 	int retval;
1989b3b57646SRob Herring 
1990b3b57646SRob Herring 	retval = tty_port_open(&state->port, tty, filp);
1991b3b57646SRob Herring 	if (retval > 0)
1992b3b57646SRob Herring 		retval = 0;
1993b3b57646SRob Herring 
1994b3b57646SRob Herring 	return retval;
1995b3b57646SRob Herring }
1996b3b57646SRob Herring 
uart_port_activate(struct tty_port * port,struct tty_struct * tty)1997b3b57646SRob Herring static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1998b3b57646SRob Herring {
1999b3b57646SRob Herring 	struct uart_state *state = container_of(port, struct uart_state, port);
2000b3b57646SRob Herring 	struct uart_port *uport;
200113b18d35SSerge Semin 	int ret;
2002b3b57646SRob Herring 
2003b3b57646SRob Herring 	uport = uart_port_check(state);
2004b3b57646SRob Herring 	if (!uport || uport->flags & UPF_DEAD)
2005b3b57646SRob Herring 		return -ENXIO;
2006b3b57646SRob Herring 
2007ab4382d2SGreg Kroah-Hartman 	/*
2008ab4382d2SGreg Kroah-Hartman 	 * Start up the serial port.
2009ab4382d2SGreg Kroah-Hartman 	 */
2010dcd794c6SIlpo Järvinen 	ret = uart_startup(tty, state, false);
201113b18d35SSerge Semin 	if (ret > 0)
20129b5aa549SIlpo Järvinen 		tty_port_set_active(port, true);
201313b18d35SSerge Semin 
201413b18d35SSerge Semin 	return ret;
2015ab4382d2SGreg Kroah-Hartman }
2016ab4382d2SGreg Kroah-Hartman 
uart_type(struct uart_port * port)2017ab4382d2SGreg Kroah-Hartman static const char *uart_type(struct uart_port *port)
2018ab4382d2SGreg Kroah-Hartman {
2019ab4382d2SGreg Kroah-Hartman 	const char *str = NULL;
2020ab4382d2SGreg Kroah-Hartman 
2021ab4382d2SGreg Kroah-Hartman 	if (port->ops->type)
2022ab4382d2SGreg Kroah-Hartman 		str = port->ops->type(port);
2023ab4382d2SGreg Kroah-Hartman 
2024ab4382d2SGreg Kroah-Hartman 	if (!str)
2025ab4382d2SGreg Kroah-Hartman 		str = "unknown";
2026ab4382d2SGreg Kroah-Hartman 
2027ab4382d2SGreg Kroah-Hartman 	return str;
2028ab4382d2SGreg Kroah-Hartman }
2029ab4382d2SGreg Kroah-Hartman 
2030ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
2031ab4382d2SGreg Kroah-Hartman 
uart_line_info(struct seq_file * m,struct uart_driver * drv,int i)2032ab4382d2SGreg Kroah-Hartman static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
2033ab4382d2SGreg Kroah-Hartman {
2034ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + i;
2035ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
20366f538fe3SLinus Walleij 	enum uart_pm_state pm_state;
20374047b371SPeter Hurley 	struct uart_port *uport;
2038ab4382d2SGreg Kroah-Hartman 	char stat_buf[32];
2039ab4382d2SGreg Kroah-Hartman 	unsigned int status;
2040ab4382d2SGreg Kroah-Hartman 	int mmio;
2041ab4382d2SGreg Kroah-Hartman 
2042*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
2043*5879adbfSJiri Slaby (SUSE) 
20444047b371SPeter Hurley 	uport = uart_port_check(state);
2045ab4382d2SGreg Kroah-Hartman 	if (!uport)
2046*5879adbfSJiri Slaby (SUSE) 		return;
2047ab4382d2SGreg Kroah-Hartman 
2048ab4382d2SGreg Kroah-Hartman 	mmio = uport->iotype >= UPIO_MEM;
2049ab4382d2SGreg Kroah-Hartman 	seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
2050ab4382d2SGreg Kroah-Hartman 			uport->line, uart_type(uport),
2051ab4382d2SGreg Kroah-Hartman 			mmio ? "mmio:0x" : "port:",
2052ab4382d2SGreg Kroah-Hartman 			mmio ? (unsigned long long)uport->mapbase
2053ab4382d2SGreg Kroah-Hartman 			     : (unsigned long long)uport->iobase,
2054ab4382d2SGreg Kroah-Hartman 			uport->irq);
2055ab4382d2SGreg Kroah-Hartman 
2056ab4382d2SGreg Kroah-Hartman 	if (uport->type == PORT_UNKNOWN) {
2057ab4382d2SGreg Kroah-Hartman 		seq_putc(m, '\n');
2058*5879adbfSJiri Slaby (SUSE) 		return;
2059ab4382d2SGreg Kroah-Hartman 	}
2060ab4382d2SGreg Kroah-Hartman 
2061ab4382d2SGreg Kroah-Hartman 	if (capable(CAP_SYS_ADMIN)) {
2062ab4382d2SGreg Kroah-Hartman 		pm_state = state->pm_state;
20636f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
20646f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2065559c7ff4SThomas Gleixner 		uart_port_lock_irq(uport);
2066ab4382d2SGreg Kroah-Hartman 		status = uport->ops->get_mctrl(uport);
2067559c7ff4SThomas Gleixner 		uart_port_unlock_irq(uport);
20686f538fe3SLinus Walleij 		if (pm_state != UART_PM_STATE_ON)
2069ab4382d2SGreg Kroah-Hartman 			uart_change_pm(state, pm_state);
2070ab4382d2SGreg Kroah-Hartman 
2071ab4382d2SGreg Kroah-Hartman 		seq_printf(m, " tx:%d rx:%d",
2072ab4382d2SGreg Kroah-Hartman 				uport->icount.tx, uport->icount.rx);
2073ab4382d2SGreg Kroah-Hartman 		if (uport->icount.frame)
2074968af298SPeter Hurley 			seq_printf(m, " fe:%d",	uport->icount.frame);
2075ab4382d2SGreg Kroah-Hartman 		if (uport->icount.parity)
2076968af298SPeter Hurley 			seq_printf(m, " pe:%d",	uport->icount.parity);
2077ab4382d2SGreg Kroah-Hartman 		if (uport->icount.brk)
2078968af298SPeter Hurley 			seq_printf(m, " brk:%d", uport->icount.brk);
2079ab4382d2SGreg Kroah-Hartman 		if (uport->icount.overrun)
2080968af298SPeter Hurley 			seq_printf(m, " oe:%d", uport->icount.overrun);
20814f794097SJeremy Kerr 		if (uport->icount.buf_overrun)
20824f794097SJeremy Kerr 			seq_printf(m, " bo:%d", uport->icount.buf_overrun);
2083ab4382d2SGreg Kroah-Hartman 
2084ab4382d2SGreg Kroah-Hartman #define INFOBIT(bit, str) \
2085ab4382d2SGreg Kroah-Hartman 	if (uport->mctrl & (bit)) \
2086ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
2087ab4382d2SGreg Kroah-Hartman 			strlen(stat_buf) - 2)
2088ab4382d2SGreg Kroah-Hartman #define STATBIT(bit, str) \
2089ab4382d2SGreg Kroah-Hartman 	if (status & (bit)) \
2090ab4382d2SGreg Kroah-Hartman 		strncat(stat_buf, (str), sizeof(stat_buf) - \
2091ab4382d2SGreg Kroah-Hartman 		       strlen(stat_buf) - 2)
2092ab4382d2SGreg Kroah-Hartman 
2093ab4382d2SGreg Kroah-Hartman 		stat_buf[0] = '\0';
2094ab4382d2SGreg Kroah-Hartman 		stat_buf[1] = '\0';
2095ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_RTS, "|RTS");
2096ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CTS, "|CTS");
2097ab4382d2SGreg Kroah-Hartman 		INFOBIT(TIOCM_DTR, "|DTR");
2098ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_DSR, "|DSR");
2099ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_CAR, "|CD");
2100ab4382d2SGreg Kroah-Hartman 		STATBIT(TIOCM_RNG, "|RI");
2101ab4382d2SGreg Kroah-Hartman 		if (stat_buf[0])
2102ab4382d2SGreg Kroah-Hartman 			stat_buf[0] = ' ';
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 		seq_puts(m, stat_buf);
2105ab4382d2SGreg Kroah-Hartman 	}
2106ab4382d2SGreg Kroah-Hartman 	seq_putc(m, '\n');
2107ab4382d2SGreg Kroah-Hartman #undef STATBIT
2108ab4382d2SGreg Kroah-Hartman #undef INFOBIT
2109ab4382d2SGreg Kroah-Hartman }
2110ab4382d2SGreg Kroah-Hartman 
uart_proc_show(struct seq_file * m,void * v)2111ab4382d2SGreg Kroah-Hartman static int uart_proc_show(struct seq_file *m, void *v)
2112ab4382d2SGreg Kroah-Hartman {
2113ab4382d2SGreg Kroah-Hartman 	struct tty_driver *ttydrv = m->private;
2114ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = ttydrv->driver_state;
2115ab4382d2SGreg Kroah-Hartman 	int i;
2116ab4382d2SGreg Kroah-Hartman 
2117968af298SPeter Hurley 	seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2118ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++)
2119ab4382d2SGreg Kroah-Hartman 		uart_line_info(m, drv, i);
2120ab4382d2SGreg Kroah-Hartman 	return 0;
2121ab4382d2SGreg Kroah-Hartman }
2122ab4382d2SGreg Kroah-Hartman #endif
2123ab4382d2SGreg Kroah-Hartman 
uart_port_spin_lock_init(struct uart_port * port)2124e0830dbfSJohan Hovold static void uart_port_spin_lock_init(struct uart_port *port)
2125f743061aSAndy Shevchenko {
2126f743061aSAndy Shevchenko 	spin_lock_init(&port->lock);
2127f743061aSAndy Shevchenko 	lockdep_set_class(&port->lock, &port_lock_key);
2128f743061aSAndy Shevchenko }
2129f743061aSAndy Shevchenko 
2130ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
21311cfe42b7SPeter Hurley /**
2132ab4382d2SGreg Kroah-Hartman  * uart_console_write - write a console message to a serial port
2133ab4382d2SGreg Kroah-Hartman  * @port: the port to write the message
2134ab4382d2SGreg Kroah-Hartman  * @s: array of characters
2135ab4382d2SGreg Kroah-Hartman  * @count: number of characters in string to write
213610afbe34SPeter Hurley  * @putchar: function to write character to port
2137ab4382d2SGreg Kroah-Hartman  */
uart_console_write(struct uart_port * port,const char * s,unsigned int count,void (* putchar)(struct uart_port *,unsigned char))2138ab4382d2SGreg Kroah-Hartman void uart_console_write(struct uart_port *port, const char *s,
2139ab4382d2SGreg Kroah-Hartman 			unsigned int count,
21403f8bab17SJiri Slaby 			void (*putchar)(struct uart_port *, unsigned char))
2141ab4382d2SGreg Kroah-Hartman {
2142ab4382d2SGreg Kroah-Hartman 	unsigned int i;
2143ab4382d2SGreg Kroah-Hartman 
2144ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < count; i++, s++) {
2145ab4382d2SGreg Kroah-Hartman 		if (*s == '\n')
2146ab4382d2SGreg Kroah-Hartman 			putchar(port, '\r');
2147ab4382d2SGreg Kroah-Hartman 		putchar(port, *s);
2148ab4382d2SGreg Kroah-Hartman 	}
2149ab4382d2SGreg Kroah-Hartman }
2150ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_console_write);
2151ab4382d2SGreg Kroah-Hartman 
21529e5f399fSJiri Slaby /**
21539e5f399fSJiri Slaby  * uart_get_console - get uart port for console
21549e5f399fSJiri Slaby  * @ports: ports to search in
21559e5f399fSJiri Slaby  * @nr: number of @ports
21569e5f399fSJiri Slaby  * @co: console to search for
21579e5f399fSJiri Slaby  * Returns: uart_port for the console @co
21589e5f399fSJiri Slaby  *
21599e5f399fSJiri Slaby  * Check whether an invalid uart number has been specified (as @co->index), and
21609e5f399fSJiri Slaby  * if so, search for the first available port that does have console support.
2161ab4382d2SGreg Kroah-Hartman  */
2162ab4382d2SGreg Kroah-Hartman struct uart_port * __init
uart_get_console(struct uart_port * ports,int nr,struct console * co)2163ab4382d2SGreg Kroah-Hartman uart_get_console(struct uart_port *ports, int nr, struct console *co)
2164ab4382d2SGreg Kroah-Hartman {
2165ab4382d2SGreg Kroah-Hartman 	int idx = co->index;
2166ab4382d2SGreg Kroah-Hartman 
2167ab4382d2SGreg Kroah-Hartman 	if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2168ab4382d2SGreg Kroah-Hartman 				     ports[idx].membase == NULL))
2169ab4382d2SGreg Kroah-Hartman 		for (idx = 0; idx < nr; idx++)
2170ab4382d2SGreg Kroah-Hartman 			if (ports[idx].iobase != 0 ||
2171ab4382d2SGreg Kroah-Hartman 			    ports[idx].membase != NULL)
2172ab4382d2SGreg Kroah-Hartman 				break;
2173ab4382d2SGreg Kroah-Hartman 
2174ab4382d2SGreg Kroah-Hartman 	co->index = idx;
2175ab4382d2SGreg Kroah-Hartman 
2176ab4382d2SGreg Kroah-Hartman 	return ports + idx;
2177ab4382d2SGreg Kroah-Hartman }
2178ab4382d2SGreg Kroah-Hartman 
2179ab4382d2SGreg Kroah-Hartman /**
218073abaf87SPeter Hurley  * uart_parse_earlycon - Parse earlycon options
218173abaf87SPeter Hurley  * @p:	     ptr to 2nd field (ie., just beyond '<name>,')
218273abaf87SPeter Hurley  * @iotype:  ptr for decoded iotype (out)
218373abaf87SPeter Hurley  * @addr:    ptr for decoded mapbase/iobase (out)
2184987233b3SJiri Slaby  * @options: ptr for <options> field; %NULL if not present (out)
218573abaf87SPeter Hurley  *
2186987233b3SJiri Slaby  * Decodes earlycon kernel command line parameters of the form:
2187987233b3SJiri Slaby  *  * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2188987233b3SJiri Slaby  *  * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
218973abaf87SPeter Hurley  *
2190987233b3SJiri Slaby  * The optional form:
2191987233b3SJiri Slaby  *  * earlycon=<name>,0x<addr>,<options>
2192987233b3SJiri Slaby  *  * console=<name>,0x<addr>,<options>
2193ff30283aSRandy Dunlap  *
2194987233b3SJiri Slaby  * is also accepted; the returned @iotype will be %UPIO_MEM.
2195ff30283aSRandy Dunlap  *
2196987233b3SJiri Slaby  * Returns: 0 on success or -%EINVAL on failure
219773abaf87SPeter Hurley  */
uart_parse_earlycon(char * p,unsigned char * iotype,resource_size_t * addr,char ** options)219846e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
219973abaf87SPeter Hurley 			char **options)
220073abaf87SPeter Hurley {
220173abaf87SPeter Hurley 	if (strncmp(p, "mmio,", 5) == 0) {
220273abaf87SPeter Hurley 		*iotype = UPIO_MEM;
220373abaf87SPeter Hurley 		p += 5;
2204bd94c407SMasahiro Yamada 	} else if (strncmp(p, "mmio16,", 7) == 0) {
2205bd94c407SMasahiro Yamada 		*iotype = UPIO_MEM16;
2206bd94c407SMasahiro Yamada 		p += 7;
220773abaf87SPeter Hurley 	} else if (strncmp(p, "mmio32,", 7) == 0) {
220873abaf87SPeter Hurley 		*iotype = UPIO_MEM32;
220973abaf87SPeter Hurley 		p += 7;
22106e63be3fSNoam Camus 	} else if (strncmp(p, "mmio32be,", 9) == 0) {
22116e63be3fSNoam Camus 		*iotype = UPIO_MEM32BE;
22126e63be3fSNoam Camus 		p += 9;
2213d215d809SMax Filippov 	} else if (strncmp(p, "mmio32native,", 13) == 0) {
2214d215d809SMax Filippov 		*iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2215d215d809SMax Filippov 			UPIO_MEM32BE : UPIO_MEM32;
2216d215d809SMax Filippov 		p += 13;
221773abaf87SPeter Hurley 	} else if (strncmp(p, "io,", 3) == 0) {
221873abaf87SPeter Hurley 		*iotype = UPIO_PORT;
221973abaf87SPeter Hurley 		p += 3;
222073abaf87SPeter Hurley 	} else if (strncmp(p, "0x", 2) == 0) {
222173abaf87SPeter Hurley 		*iotype = UPIO_MEM;
222273abaf87SPeter Hurley 	} else {
222373abaf87SPeter Hurley 		return -EINVAL;
222473abaf87SPeter Hurley 	}
222573abaf87SPeter Hurley 
22268b2303deSAlexander Sverdlin 	/*
22278b2303deSAlexander Sverdlin 	 * Before you replace it with kstrtoull(), think about options separator
22288b2303deSAlexander Sverdlin 	 * (',') it will not tolerate
22298b2303deSAlexander Sverdlin 	 */
22308b2303deSAlexander Sverdlin 	*addr = simple_strtoull(p, NULL, 0);
223173abaf87SPeter Hurley 	p = strchr(p, ',');
223273abaf87SPeter Hurley 	if (p)
223373abaf87SPeter Hurley 		p++;
223473abaf87SPeter Hurley 
223573abaf87SPeter Hurley 	*options = p;
223673abaf87SPeter Hurley 	return 0;
223773abaf87SPeter Hurley }
223873abaf87SPeter Hurley EXPORT_SYMBOL_GPL(uart_parse_earlycon);
223973abaf87SPeter Hurley 
224073abaf87SPeter Hurley /**
224102088ca6SGeert Uytterhoeven  * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2242ab4382d2SGreg Kroah-Hartman  * @options: pointer to option string
2243ab4382d2SGreg Kroah-Hartman  * @baud: pointer to an 'int' variable for the baud rate.
2244ab4382d2SGreg Kroah-Hartman  * @parity: pointer to an 'int' variable for the parity.
2245ab4382d2SGreg Kroah-Hartman  * @bits: pointer to an 'int' variable for the number of data bits.
2246ab4382d2SGreg Kroah-Hartman  * @flow: pointer to an 'int' variable for the flow control character.
2247ab4382d2SGreg Kroah-Hartman  *
2248987233b3SJiri Slaby  * uart_parse_options() decodes a string containing the serial console
2249ab4382d2SGreg Kroah-Hartman  * options. The format of the string is <baud><parity><bits><flow>,
2250ab4382d2SGreg Kroah-Hartman  * eg: 115200n8r
2251ab4382d2SGreg Kroah-Hartman  */
2252ab4382d2SGreg Kroah-Hartman void
uart_parse_options(const char * options,int * baud,int * parity,int * bits,int * flow)22530f646b63SPaul Cercueil uart_parse_options(const char *options, int *baud, int *parity,
22540f646b63SPaul Cercueil 		   int *bits, int *flow)
2255ab4382d2SGreg Kroah-Hartman {
22560f646b63SPaul Cercueil 	const char *s = options;
2257ab4382d2SGreg Kroah-Hartman 
2258ab4382d2SGreg Kroah-Hartman 	*baud = simple_strtoul(s, NULL, 10);
2259ab4382d2SGreg Kroah-Hartman 	while (*s >= '0' && *s <= '9')
2260ab4382d2SGreg Kroah-Hartman 		s++;
2261ab4382d2SGreg Kroah-Hartman 	if (*s)
2262ab4382d2SGreg Kroah-Hartman 		*parity = *s++;
2263ab4382d2SGreg Kroah-Hartman 	if (*s)
2264ab4382d2SGreg Kroah-Hartman 		*bits = *s++ - '0';
2265ab4382d2SGreg Kroah-Hartman 	if (*s)
2266ab4382d2SGreg Kroah-Hartman 		*flow = *s;
2267ab4382d2SGreg Kroah-Hartman }
2268ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_parse_options);
2269ab4382d2SGreg Kroah-Hartman 
2270ab4382d2SGreg Kroah-Hartman /**
2271ab4382d2SGreg Kroah-Hartman  * uart_set_options - setup the serial console parameters
2272ab4382d2SGreg Kroah-Hartman  * @port: pointer to the serial ports uart_port structure
2273ab4382d2SGreg Kroah-Hartman  * @co: console pointer
2274ab4382d2SGreg Kroah-Hartman  * @baud: baud rate
2275ab4382d2SGreg Kroah-Hartman  * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2276ab4382d2SGreg Kroah-Hartman  * @bits: number of data bits
2277ab4382d2SGreg Kroah-Hartman  * @flow: flow control character - 'r' (rts)
22783ef5abd9SJohn Ogness  *
22793ef5abd9SJohn Ogness  * Locking: Caller must hold console_list_lock in order to serialize
22803ef5abd9SJohn Ogness  * early initialization of the serial-console lock.
2281ab4382d2SGreg Kroah-Hartman  */
2282ab4382d2SGreg Kroah-Hartman int
uart_set_options(struct uart_port * port,struct console * co,int baud,int parity,int bits,int flow)2283ab4382d2SGreg Kroah-Hartman uart_set_options(struct uart_port *port, struct console *co,
2284ab4382d2SGreg Kroah-Hartman 		 int baud, int parity, int bits, int flow)
2285ab4382d2SGreg Kroah-Hartman {
2286ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2287ab4382d2SGreg Kroah-Hartman 	static struct ktermios dummy;
2288ab4382d2SGreg Kroah-Hartman 
2289e0830dbfSJohan Hovold 	/*
2290e0830dbfSJohan Hovold 	 * Ensure that the serial-console lock is initialised early.
2291e0830dbfSJohan Hovold 	 *
2292452b9b24SJohn Ogness 	 * Note that the console-registered check is needed because
2293452b9b24SJohn Ogness 	 * kgdboc can call uart_set_options() for an already registered
2294e0830dbfSJohan Hovold 	 * console via tty_find_polling_driver() and uart_poll_init().
2295e0830dbfSJohan Hovold 	 */
2296452b9b24SJohn Ogness 	if (!uart_console_registered_locked(port) && !port->console_reinit)
2297d2403cadSAndy Shevchenko 		uart_port_spin_lock_init(port);
2298ab4382d2SGreg Kroah-Hartman 
2299ab4382d2SGreg Kroah-Hartman 	memset(&termios, 0, sizeof(struct ktermios));
2300ab4382d2SGreg Kroah-Hartman 
2301ba47f97aSJeffy Chen 	termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2302ba47f97aSJeffy Chen 	tty_termios_encode_baud_rate(&termios, baud, baud);
2303ab4382d2SGreg Kroah-Hartman 
2304ab4382d2SGreg Kroah-Hartman 	if (bits == 7)
2305ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS7;
2306ab4382d2SGreg Kroah-Hartman 	else
2307ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CS8;
2308ab4382d2SGreg Kroah-Hartman 
2309ab4382d2SGreg Kroah-Hartman 	switch (parity) {
2310ab4382d2SGreg Kroah-Hartman 	case 'o': case 'O':
2311ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARODD;
2312df561f66SGustavo A. R. Silva 		fallthrough;
2313ab4382d2SGreg Kroah-Hartman 	case 'e': case 'E':
2314ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= PARENB;
2315ab4382d2SGreg Kroah-Hartman 		break;
2316ab4382d2SGreg Kroah-Hartman 	}
2317ab4382d2SGreg Kroah-Hartman 
2318ab4382d2SGreg Kroah-Hartman 	if (flow == 'r')
2319ab4382d2SGreg Kroah-Hartman 		termios.c_cflag |= CRTSCTS;
2320ab4382d2SGreg Kroah-Hartman 
2321ab4382d2SGreg Kroah-Hartman 	/*
2322ab4382d2SGreg Kroah-Hartman 	 * some uarts on other side don't support no flow control.
2323ab4382d2SGreg Kroah-Hartman 	 * So we set * DTR in host uart to make them happy
2324ab4382d2SGreg Kroah-Hartman 	 */
2325ab4382d2SGreg Kroah-Hartman 	port->mctrl |= TIOCM_DTR;
2326ab4382d2SGreg Kroah-Hartman 
2327ab4382d2SGreg Kroah-Hartman 	port->ops->set_termios(port, &termios, &dummy);
2328ab4382d2SGreg Kroah-Hartman 	/*
2329ab4382d2SGreg Kroah-Hartman 	 * Allow the setting of the UART parameters with a NULL console
2330ab4382d2SGreg Kroah-Hartman 	 * too:
2331ab4382d2SGreg Kroah-Hartman 	 */
2332027b5717SPali Rohár 	if (co) {
2333ab4382d2SGreg Kroah-Hartman 		co->cflag = termios.c_cflag;
2334027b5717SPali Rohár 		co->ispeed = termios.c_ispeed;
2335027b5717SPali Rohár 		co->ospeed = termios.c_ospeed;
2336027b5717SPali Rohár 	}
2337ab4382d2SGreg Kroah-Hartman 
2338ab4382d2SGreg Kroah-Hartman 	return 0;
2339ab4382d2SGreg Kroah-Hartman }
2340ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(uart_set_options);
2341ab4382d2SGreg Kroah-Hartman #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2342ab4382d2SGreg Kroah-Hartman 
2343cf75525fSJiri Slaby /**
2344cf75525fSJiri Slaby  * uart_change_pm - set power state of the port
2345cf75525fSJiri Slaby  *
2346cf75525fSJiri Slaby  * @state: port descriptor
2347cf75525fSJiri Slaby  * @pm_state: new state
2348cf75525fSJiri Slaby  *
2349cf75525fSJiri Slaby  * Locking: port->mutex has to be held
2350cf75525fSJiri Slaby  */
uart_change_pm(struct uart_state * state,enum uart_pm_state pm_state)23516f538fe3SLinus Walleij static void uart_change_pm(struct uart_state *state,
23526f538fe3SLinus Walleij 			   enum uart_pm_state pm_state)
2353ab4382d2SGreg Kroah-Hartman {
23544047b371SPeter Hurley 	struct uart_port *port = uart_port_check(state);
2355ab4382d2SGreg Kroah-Hartman 
2356ab4382d2SGreg Kroah-Hartman 	if (state->pm_state != pm_state) {
23574047b371SPeter Hurley 		if (port && port->ops->pm)
2358ab4382d2SGreg Kroah-Hartman 			port->ops->pm(port, pm_state, state->pm_state);
2359ab4382d2SGreg Kroah-Hartman 		state->pm_state = pm_state;
2360ab4382d2SGreg Kroah-Hartman 	}
2361ab4382d2SGreg Kroah-Hartman }
2362ab4382d2SGreg Kroah-Hartman 
2363ab4382d2SGreg Kroah-Hartman struct uart_match {
2364ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2365ab4382d2SGreg Kroah-Hartman 	struct uart_driver *driver;
2366ab4382d2SGreg Kroah-Hartman };
2367ab4382d2SGreg Kroah-Hartman 
serial_match_port(struct device * dev,void * data)2368ab4382d2SGreg Kroah-Hartman static int serial_match_port(struct device *dev, void *data)
2369ab4382d2SGreg Kroah-Hartman {
2370ab4382d2SGreg Kroah-Hartman 	struct uart_match *match = data;
2371ab4382d2SGreg Kroah-Hartman 	struct tty_driver *tty_drv = match->driver->tty_driver;
2372ab4382d2SGreg Kroah-Hartman 	dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2373ab4382d2SGreg Kroah-Hartman 		match->port->line;
2374ab4382d2SGreg Kroah-Hartman 
2375ab4382d2SGreg Kroah-Hartman 	return dev->devt == devt; /* Actually, only one tty per port */
2376ab4382d2SGreg Kroah-Hartman }
2377ab4382d2SGreg Kroah-Hartman 
uart_suspend_port(struct uart_driver * drv,struct uart_port * uport)2378ab4382d2SGreg Kroah-Hartman int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2379ab4382d2SGreg Kroah-Hartman {
2380ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2381ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2382ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2383ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2384ab4382d2SGreg Kroah-Hartman 
2385*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
2386ab4382d2SGreg Kroah-Hartman 
2387b286f4e8STony Lindgren 	tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
238888e2582eSLucas Stach 	if (tty_dev && device_may_wakeup(tty_dev)) {
2389aef3ad10SAndy Shevchenko 		enable_irq_wake(uport->irq);
2390ab4382d2SGreg Kroah-Hartman 		put_device(tty_dev);
2391ab4382d2SGreg Kroah-Hartman 		return 0;
2392ab4382d2SGreg Kroah-Hartman 	}
23935a65dcc0SFederico Vaga 	put_device(tty_dev);
23945a65dcc0SFederico Vaga 
2395c9d2325cSVijaya Krishna Nivarthi 	/*
2396c9d2325cSVijaya Krishna Nivarthi 	 * Nothing to do if the console is not suspending
2397c9d2325cSVijaya Krishna Nivarthi 	 * except stop_rx to prevent any asynchronous data
2398cfab87c2SVijaya Krishna Nivarthi 	 * over RX line. However ensure that we will be
2399cfab87c2SVijaya Krishna Nivarthi 	 * able to Re-start_rx later.
2400c9d2325cSVijaya Krishna Nivarthi 	 */
2401c9d2325cSVijaya Krishna Nivarthi 	if (!console_suspend_enabled && uart_console(uport)) {
2402abcb0cf1SJohn Ogness 		if (uport->ops->start_rx) {
2403559c7ff4SThomas Gleixner 			uart_port_lock_irq(uport);
2404c9d2325cSVijaya Krishna Nivarthi 			uport->ops->stop_rx(uport);
2405559c7ff4SThomas Gleixner 			uart_port_unlock_irq(uport);
2406abcb0cf1SJohn Ogness 		}
2407a47cf07fSClaudiu Beznea 		device_set_awake_path(uport->dev);
2408*5879adbfSJiri Slaby (SUSE) 		return 0;
2409c9d2325cSVijaya Krishna Nivarthi 	}
2410b164c972SPeter Hurley 
2411ab4382d2SGreg Kroah-Hartman 	uport->suspended = 1;
2412ab4382d2SGreg Kroah-Hartman 
2413d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
2414ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2415ab4382d2SGreg Kroah-Hartman 		int tries;
241618c9d4a3SAl Cooper 		unsigned int mctrl;
2417ab4382d2SGreg Kroah-Hartman 
241875b20a2aSIlpo Järvinen 		tty_port_set_suspended(port, true);
2419515be7baSIlpo Järvinen 		tty_port_set_initialized(port, false);
2420ab4382d2SGreg Kroah-Hartman 
2421559c7ff4SThomas Gleixner 		uart_port_lock_irq(uport);
2422ab4382d2SGreg Kroah-Hartman 		ops->stop_tx(uport);
24237c7f9bc9SLukas Wunner 		if (!(uport->rs485.flags & SER_RS485_ENABLED))
2424ab4382d2SGreg Kroah-Hartman 			ops->set_mctrl(uport, 0);
242518c9d4a3SAl Cooper 		/* save mctrl so it can be restored on resume */
242618c9d4a3SAl Cooper 		mctrl = uport->mctrl;
242718c9d4a3SAl Cooper 		uport->mctrl = 0;
2428ab4382d2SGreg Kroah-Hartman 		ops->stop_rx(uport);
2429559c7ff4SThomas Gleixner 		uart_port_unlock_irq(uport);
2430ab4382d2SGreg Kroah-Hartman 
2431ab4382d2SGreg Kroah-Hartman 		/*
2432ab4382d2SGreg Kroah-Hartman 		 * Wait for the transmitter to empty.
2433ab4382d2SGreg Kroah-Hartman 		 */
2434ab4382d2SGreg Kroah-Hartman 		for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2435ab4382d2SGreg Kroah-Hartman 			msleep(10);
2436ab4382d2SGreg Kroah-Hartman 		if (!tries)
2437cade3580SAndy Shevchenko 			dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2438cade3580SAndy Shevchenko 				uport->name);
2439ab4382d2SGreg Kroah-Hartman 
2440ab4382d2SGreg Kroah-Hartman 		ops->shutdown(uport);
244118c9d4a3SAl Cooper 		uport->mctrl = mctrl;
2442ab4382d2SGreg Kroah-Hartman 	}
2443ab4382d2SGreg Kroah-Hartman 
2444ab4382d2SGreg Kroah-Hartman 	/*
2445ab4382d2SGreg Kroah-Hartman 	 * Disable the console device before suspending.
2446ab4382d2SGreg Kroah-Hartman 	 */
2447b164c972SPeter Hurley 	if (uart_console(uport))
2448ab4382d2SGreg Kroah-Hartman 		console_stop(uport->cons);
2449ab4382d2SGreg Kroah-Hartman 
24506f538fe3SLinus Walleij 	uart_change_pm(state, UART_PM_STATE_OFF);
2451ab4382d2SGreg Kroah-Hartman 
2452ab4382d2SGreg Kroah-Hartman 	return 0;
2453ab4382d2SGreg Kroah-Hartman }
245415dc475bSJiri Slaby EXPORT_SYMBOL(uart_suspend_port);
2455ab4382d2SGreg Kroah-Hartman 
uart_resume_port(struct uart_driver * drv,struct uart_port * uport)2456ab4382d2SGreg Kroah-Hartman int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2457ab4382d2SGreg Kroah-Hartman {
2458ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
2459ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
2460ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
2461ab4382d2SGreg Kroah-Hartman 	struct uart_match match = {uport, drv};
2462ab4382d2SGreg Kroah-Hartman 	struct ktermios termios;
2463ab4382d2SGreg Kroah-Hartman 
2464*5879adbfSJiri Slaby (SUSE) 	guard(mutex)(&port->mutex);
2465ab4382d2SGreg Kroah-Hartman 
2466b286f4e8STony Lindgren 	tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
2467ab4382d2SGreg Kroah-Hartman 	if (!uport->suspended && device_may_wakeup(tty_dev)) {
2468aef3ad10SAndy Shevchenko 		if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2469ab4382d2SGreg Kroah-Hartman 			disable_irq_wake(uport->irq);
24705a65dcc0SFederico Vaga 		put_device(tty_dev);
2471ab4382d2SGreg Kroah-Hartman 		return 0;
2472ab4382d2SGreg Kroah-Hartman 	}
24735a65dcc0SFederico Vaga 	put_device(tty_dev);
2474ab4382d2SGreg Kroah-Hartman 	uport->suspended = 0;
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman 	/*
2477ab4382d2SGreg Kroah-Hartman 	 * Re-enable the console device after suspending.
2478ab4382d2SGreg Kroah-Hartman 	 */
24795933a161SYin Kangkai 	if (uart_console(uport)) {
2480ab4382d2SGreg Kroah-Hartman 		/*
2481ab4382d2SGreg Kroah-Hartman 		 * First try to use the console cflag setting.
2482ab4382d2SGreg Kroah-Hartman 		 */
2483ab4382d2SGreg Kroah-Hartman 		memset(&termios, 0, sizeof(struct ktermios));
2484ab4382d2SGreg Kroah-Hartman 		termios.c_cflag = uport->cons->cflag;
2485027b5717SPali Rohár 		termios.c_ispeed = uport->cons->ispeed;
2486027b5717SPali Rohár 		termios.c_ospeed = uport->cons->ospeed;
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman 		/*
2489ab4382d2SGreg Kroah-Hartman 		 * If that's unset, use the tty termios setting.
2490ab4382d2SGreg Kroah-Hartman 		 */
2491adc8d746SAlan Cox 		if (port->tty && termios.c_cflag == 0)
2492adc8d746SAlan Cox 			termios = port->tty->termios;
2493ab4382d2SGreg Kroah-Hartman 
249494abc56fSNing Jiang 		if (console_suspend_enabled)
24956f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_ON);
2496ab4382d2SGreg Kroah-Hartman 		uport->ops->set_termios(uport, &termios, NULL);
249751e45fbaSJohn Ogness 		if (!console_suspend_enabled && uport->ops->start_rx) {
2498559c7ff4SThomas Gleixner 			uart_port_lock_irq(uport);
2499cfab87c2SVijaya Krishna Nivarthi 			uport->ops->start_rx(uport);
2500559c7ff4SThomas Gleixner 			uart_port_unlock_irq(uport);
250151e45fbaSJohn Ogness 		}
25025933a161SYin Kangkai 		if (console_suspend_enabled)
2503ab4382d2SGreg Kroah-Hartman 			console_start(uport->cons);
2504ab4382d2SGreg Kroah-Hartman 	}
2505ab4382d2SGreg Kroah-Hartman 
250680f02d54SPeter Hurley 	if (tty_port_suspended(port)) {
2507ab4382d2SGreg Kroah-Hartman 		const struct uart_ops *ops = uport->ops;
2508ab4382d2SGreg Kroah-Hartman 		int ret;
2509ab4382d2SGreg Kroah-Hartman 
25106f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2511559c7ff4SThomas Gleixner 		uart_port_lock_irq(uport);
25127c7f9bc9SLukas Wunner 		if (!(uport->rs485.flags & SER_RS485_ENABLED))
2513ab4382d2SGreg Kroah-Hartman 			ops->set_mctrl(uport, 0);
2514559c7ff4SThomas Gleixner 		uart_port_unlock_irq(uport);
2515ab4382d2SGreg Kroah-Hartman 		if (console_suspend_enabled || !uart_console(uport)) {
2516ab4382d2SGreg Kroah-Hartman 			/* Protected by port mutex for now */
2517ab4382d2SGreg Kroah-Hartman 			struct tty_struct *tty = port->tty;
25184ed71addSTamseel Shams 
2519ab4382d2SGreg Kroah-Hartman 			ret = ops->startup(uport);
2520ab4382d2SGreg Kroah-Hartman 			if (ret == 0) {
2521ab4382d2SGreg Kroah-Hartman 				if (tty)
2522826736a6SIlpo Järvinen 					uart_change_line_settings(tty, state, NULL);
25238679328eSLukas Wunner 				uart_rs485_config(uport);
2524559c7ff4SThomas Gleixner 				uart_port_lock_irq(uport);
25257c7f9bc9SLukas Wunner 				if (!(uport->rs485.flags & SER_RS485_ENABLED))
2526ab4382d2SGreg Kroah-Hartman 					ops->set_mctrl(uport, uport->mctrl);
2527ab4382d2SGreg Kroah-Hartman 				ops->start_tx(uport);
2528559c7ff4SThomas Gleixner 				uart_port_unlock_irq(uport);
2529515be7baSIlpo Järvinen 				tty_port_set_initialized(port, true);
2530ab4382d2SGreg Kroah-Hartman 			} else {
2531ab4382d2SGreg Kroah-Hartman 				/*
2532ab4382d2SGreg Kroah-Hartman 				 * Failed to resume - maybe hardware went away?
2533ab4382d2SGreg Kroah-Hartman 				 * Clear the "initialized" flag so we won't try
2534ab4382d2SGreg Kroah-Hartman 				 * to call the low level drivers shutdown method.
2535ab4382d2SGreg Kroah-Hartman 				 */
2536ab4382d2SGreg Kroah-Hartman 				uart_shutdown(tty, state);
2537ab4382d2SGreg Kroah-Hartman 			}
2538ab4382d2SGreg Kroah-Hartman 		}
2539ab4382d2SGreg Kroah-Hartman 
254075b20a2aSIlpo Järvinen 		tty_port_set_suspended(port, false);
2541ab4382d2SGreg Kroah-Hartman 	}
2542ab4382d2SGreg Kroah-Hartman 
2543ab4382d2SGreg Kroah-Hartman 	return 0;
2544ab4382d2SGreg Kroah-Hartman }
254515dc475bSJiri Slaby EXPORT_SYMBOL(uart_resume_port);
2546ab4382d2SGreg Kroah-Hartman 
2547ab4382d2SGreg Kroah-Hartman static inline void
uart_report_port(struct uart_driver * drv,struct uart_port * port)2548ab4382d2SGreg Kroah-Hartman uart_report_port(struct uart_driver *drv, struct uart_port *port)
2549ab4382d2SGreg Kroah-Hartman {
2550ab4382d2SGreg Kroah-Hartman 	char address[64];
2551ab4382d2SGreg Kroah-Hartman 
2552ab4382d2SGreg Kroah-Hartman 	switch (port->iotype) {
2553ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
2554ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2555ab4382d2SGreg Kroah-Hartman 		break;
2556ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
2557ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2558ab4382d2SGreg Kroah-Hartman 			 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2559ab4382d2SGreg Kroah-Hartman 		break;
2560ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
2561bd94c407SMasahiro Yamada 	case UPIO_MEM16:
2562ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
25633ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
2564ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
2565ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
2566ab4382d2SGreg Kroah-Hartman 		snprintf(address, sizeof(address),
2567ab4382d2SGreg Kroah-Hartman 			 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2568ab4382d2SGreg Kroah-Hartman 		break;
2569ab4382d2SGreg Kroah-Hartman 	default:
2570eb9e109dSWolfram Sang 		strscpy(address, "*unknown*", sizeof(address));
2571ab4382d2SGreg Kroah-Hartman 		break;
2572ab4382d2SGreg Kroah-Hartman 	}
2573ab4382d2SGreg Kroah-Hartman 
2574cade3580SAndy Shevchenko 	pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
257568ed7e1cSJames Bottomley 	       port->dev ? dev_name(port->dev) : "",
257668ed7e1cSJames Bottomley 	       port->dev ? ": " : "",
2577cade3580SAndy Shevchenko 	       port->name,
25787d12b976SKees Cook 	       address, port->irq, port->uartclk / 16, uart_type(port));
2579e7b91932SMaciej W. Rozycki 
2580e7b91932SMaciej W. Rozycki 	/* The magic multiplier feature is a bit obscure, so report it too.  */
2581e7b91932SMaciej W. Rozycki 	if (port->flags & UPF_MAGIC_MULTIPLIER)
2582e7b91932SMaciej W. Rozycki 		pr_info("%s%s%s extra baud rates supported: %d, %d",
2583e7b91932SMaciej W. Rozycki 			port->dev ? dev_name(port->dev) : "",
2584e7b91932SMaciej W. Rozycki 			port->dev ? ": " : "",
2585e7b91932SMaciej W. Rozycki 			port->name,
2586e7b91932SMaciej W. Rozycki 			port->uartclk / 8, port->uartclk / 4);
2587ab4382d2SGreg Kroah-Hartman }
2588ab4382d2SGreg Kroah-Hartman 
2589ab4382d2SGreg Kroah-Hartman static void
uart_configure_port(struct uart_driver * drv,struct uart_state * state,struct uart_port * port)2590ab4382d2SGreg Kroah-Hartman uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2591ab4382d2SGreg Kroah-Hartman 		    struct uart_port *port)
2592ab4382d2SGreg Kroah-Hartman {
2593ab4382d2SGreg Kroah-Hartman 	unsigned int flags;
2594ab4382d2SGreg Kroah-Hartman 
2595ab4382d2SGreg Kroah-Hartman 	/*
2596ab4382d2SGreg Kroah-Hartman 	 * If there isn't a port here, don't do anything further.
2597ab4382d2SGreg Kroah-Hartman 	 */
2598ab4382d2SGreg Kroah-Hartman 	if (!port->iobase && !port->mapbase && !port->membase)
2599ab4382d2SGreg Kroah-Hartman 		return;
2600ab4382d2SGreg Kroah-Hartman 
2601ab4382d2SGreg Kroah-Hartman 	/*
2602ab4382d2SGreg Kroah-Hartman 	 * Now do the auto configuration stuff.  Note that config_port
2603ab4382d2SGreg Kroah-Hartman 	 * is expected to claim the resources and map the port for us.
2604ab4382d2SGreg Kroah-Hartman 	 */
2605ab4382d2SGreg Kroah-Hartman 	flags = 0;
2606ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_AUTO_IRQ)
2607ab4382d2SGreg Kroah-Hartman 		flags |= UART_CONFIG_IRQ;
2608ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_BOOT_AUTOCONF) {
2609ab4382d2SGreg Kroah-Hartman 		if (!(port->flags & UPF_FIXED_TYPE)) {
2610ab4382d2SGreg Kroah-Hartman 			port->type = PORT_UNKNOWN;
2611ab4382d2SGreg Kroah-Hartman 			flags |= UART_CONFIG_TYPE;
2612ab4382d2SGreg Kroah-Hartman 		}
2613801410b2SPeter Collingbourne 		/* Synchronize with possible boot console. */
2614801410b2SPeter Collingbourne 		if (uart_console(port))
2615801410b2SPeter Collingbourne 			console_lock();
2616ab4382d2SGreg Kroah-Hartman 		port->ops->config_port(port, flags);
2617801410b2SPeter Collingbourne 		if (uart_console(port))
2618801410b2SPeter Collingbourne 			console_unlock();
2619ab4382d2SGreg Kroah-Hartman 	}
2620ab4382d2SGreg Kroah-Hartman 
2621ab4382d2SGreg Kroah-Hartman 	if (port->type != PORT_UNKNOWN) {
2622ab4382d2SGreg Kroah-Hartman 		unsigned long flags;
2623ab4382d2SGreg Kroah-Hartman 
2624ab4382d2SGreg Kroah-Hartman 		uart_report_port(drv, port);
2625ab4382d2SGreg Kroah-Hartman 
2626801410b2SPeter Collingbourne 		/* Synchronize with possible boot console. */
2627801410b2SPeter Collingbourne 		if (uart_console(port))
2628801410b2SPeter Collingbourne 			console_lock();
2629801410b2SPeter Collingbourne 
2630ab4382d2SGreg Kroah-Hartman 		/* Power up port for set_mctrl() */
26316f538fe3SLinus Walleij 		uart_change_pm(state, UART_PM_STATE_ON);
2632ab4382d2SGreg Kroah-Hartman 
2633ab4382d2SGreg Kroah-Hartman 		/*
2634ab4382d2SGreg Kroah-Hartman 		 * Ensure that the modem control lines are de-activated.
2635ab4382d2SGreg Kroah-Hartman 		 * keep the DTR setting that is set in uart_set_options()
2636ab4382d2SGreg Kroah-Hartman 		 * We probably don't need a spinlock around this, but
2637ab4382d2SGreg Kroah-Hartman 		 */
2638559c7ff4SThomas Gleixner 		uart_port_lock_irqsave(port, &flags);
263993a770b7SLukas Wunner 		port->mctrl &= TIOCM_DTR;
26407c7f9bc9SLukas Wunner 		if (!(port->rs485.flags & SER_RS485_ENABLED))
264193a770b7SLukas Wunner 			port->ops->set_mctrl(port, port->mctrl);
2642559c7ff4SThomas Gleixner 		uart_port_unlock_irqrestore(port, flags);
2643ab4382d2SGreg Kroah-Hartman 
26448679328eSLukas Wunner 		uart_rs485_config(port);
26458679328eSLukas Wunner 
2646801410b2SPeter Collingbourne 		if (uart_console(port))
2647801410b2SPeter Collingbourne 			console_unlock();
2648801410b2SPeter Collingbourne 
2649ab4382d2SGreg Kroah-Hartman 		/*
2650ab4382d2SGreg Kroah-Hartman 		 * If this driver supports console, and it hasn't been
2651ab4382d2SGreg Kroah-Hartman 		 * successfully registered yet, try to re-register it.
2652ab4382d2SGreg Kroah-Hartman 		 * It may be that the port was not available.
2653ab4382d2SGreg Kroah-Hartman 		 */
2654452b9b24SJohn Ogness 		if (port->cons && !console_is_registered(port->cons))
2655ab4382d2SGreg Kroah-Hartman 			register_console(port->cons);
2656ab4382d2SGreg Kroah-Hartman 
2657ab4382d2SGreg Kroah-Hartman 		/*
2658ab4382d2SGreg Kroah-Hartman 		 * Power down all ports by default, except the
2659ab4382d2SGreg Kroah-Hartman 		 * console if we have one.
2660ab4382d2SGreg Kroah-Hartman 		 */
2661ab4382d2SGreg Kroah-Hartman 		if (!uart_console(port))
26626f538fe3SLinus Walleij 			uart_change_pm(state, UART_PM_STATE_OFF);
2663ab4382d2SGreg Kroah-Hartman 	}
2664ab4382d2SGreg Kroah-Hartman }
2665ab4382d2SGreg Kroah-Hartman 
2666ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2667ab4382d2SGreg Kroah-Hartman 
uart_poll_init(struct tty_driver * driver,int line,char * options)2668ab4382d2SGreg Kroah-Hartman static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2669ab4382d2SGreg Kroah-Hartman {
2670ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2671ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
26725e227ef2SDouglas Anderson 	enum uart_pm_state pm_state;
267349c02304SPeter Hurley 	struct tty_port *tport;
2674ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2675ab4382d2SGreg Kroah-Hartman 	int baud = 9600;
2676ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2677ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2678ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
267949c02304SPeter Hurley 	int ret = 0;
2680ab4382d2SGreg Kroah-Hartman 
268149c02304SPeter Hurley 	tport = &state->port;
2682d0009a32SJiri Slaby (SUSE) 
2683d0009a32SJiri Slaby (SUSE) 	guard(mutex)(&tport->mutex);
268449c02304SPeter Hurley 
26854047b371SPeter Hurley 	port = uart_port_check(state);
2686788aeef3SMichael Trimarchi 	if (!port || port->type == PORT_UNKNOWN ||
2687d0009a32SJiri Slaby (SUSE) 	    !(port->ops->poll_get_char && port->ops->poll_put_char))
2688d0009a32SJiri Slaby (SUSE) 		return -1;
2689ab4382d2SGreg Kroah-Hartman 
26905e227ef2SDouglas Anderson 	pm_state = state->pm_state;
26915e227ef2SDouglas Anderson 	uart_change_pm(state, UART_PM_STATE_ON);
26925e227ef2SDouglas Anderson 
2693c7f3e708SAnton Vorontsov 	if (port->ops->poll_init) {
2694c7f3e708SAnton Vorontsov 		/*
2695d41861caSPeter Hurley 		 * We don't set initialized as we only initialized the hw,
2696d41861caSPeter Hurley 		 * e.g. state->xmit is still uninitialized.
2697c7f3e708SAnton Vorontsov 		 */
2698d41861caSPeter Hurley 		if (!tty_port_initialized(tport))
2699c7f3e708SAnton Vorontsov 			ret = port->ops->poll_init(port);
2700c7f3e708SAnton Vorontsov 	}
2701c7f3e708SAnton Vorontsov 
270249c02304SPeter Hurley 	if (!ret && options) {
2703ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
27043ef5abd9SJohn Ogness 		console_list_lock();
270549c02304SPeter Hurley 		ret = uart_set_options(port, NULL, baud, parity, bits, flow);
27063ef5abd9SJohn Ogness 		console_list_unlock();
2707ab4382d2SGreg Kroah-Hartman 	}
2708d0009a32SJiri Slaby (SUSE) 
27095e227ef2SDouglas Anderson 	if (ret)
27105e227ef2SDouglas Anderson 		uart_change_pm(state, pm_state);
2711d0009a32SJiri Slaby (SUSE) 
271249c02304SPeter Hurley 	return ret;
2713ab4382d2SGreg Kroah-Hartman }
2714ab4382d2SGreg Kroah-Hartman 
uart_poll_get_char(struct tty_driver * driver,int line)2715ab4382d2SGreg Kroah-Hartman static int uart_poll_get_char(struct tty_driver *driver, int line)
2716ab4382d2SGreg Kroah-Hartman {
2717ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2718ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2719ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
27209ed19428SPeter Hurley 	int ret = -1;
2721ab4382d2SGreg Kroah-Hartman 
27229ed19428SPeter Hurley 	port = uart_port_ref(state);
2723ef510beaSAndy Shevchenko 	if (port) {
27249ed19428SPeter Hurley 		ret = port->ops->poll_get_char(port);
27259ed19428SPeter Hurley 		uart_port_deref(port);
27269ed19428SPeter Hurley 	}
272722077b09SJiri Slaby 
27289ed19428SPeter Hurley 	return ret;
2729ab4382d2SGreg Kroah-Hartman }
2730ab4382d2SGreg Kroah-Hartman 
uart_poll_put_char(struct tty_driver * driver,int line,char ch)2731ab4382d2SGreg Kroah-Hartman static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2732ab4382d2SGreg Kroah-Hartman {
2733ab4382d2SGreg Kroah-Hartman 	struct uart_driver *drv = driver->driver_state;
2734ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + line;
2735ab4382d2SGreg Kroah-Hartman 	struct uart_port *port;
2736ab4382d2SGreg Kroah-Hartman 
27379ed19428SPeter Hurley 	port = uart_port_ref(state);
27389ed19428SPeter Hurley 	if (!port)
27399ed19428SPeter Hurley 		return;
2740c7d44a02SDoug Anderson 
2741c7d44a02SDoug Anderson 	if (ch == '\n')
2742c7d44a02SDoug Anderson 		port->ops->poll_put_char(port, '\r');
2743ab4382d2SGreg Kroah-Hartman 	port->ops->poll_put_char(port, ch);
27449ed19428SPeter Hurley 	uart_port_deref(port);
2745ab4382d2SGreg Kroah-Hartman }
2746ab4382d2SGreg Kroah-Hartman #endif
2747ab4382d2SGreg Kroah-Hartman 
2748ab4382d2SGreg Kroah-Hartman static const struct tty_operations uart_ops = {
27494cdd17baSJiri Slaby 	.install	= uart_install,
2750ab4382d2SGreg Kroah-Hartman 	.open		= uart_open,
2751ab4382d2SGreg Kroah-Hartman 	.close		= uart_close,
2752ab4382d2SGreg Kroah-Hartman 	.write		= uart_write,
2753ab4382d2SGreg Kroah-Hartman 	.put_char	= uart_put_char,
2754ab4382d2SGreg Kroah-Hartman 	.flush_chars	= uart_flush_chars,
2755ab4382d2SGreg Kroah-Hartman 	.write_room	= uart_write_room,
2756ab4382d2SGreg Kroah-Hartman 	.chars_in_buffer= uart_chars_in_buffer,
2757ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= uart_flush_buffer,
2758ab4382d2SGreg Kroah-Hartman 	.ioctl		= uart_ioctl,
2759ab4382d2SGreg Kroah-Hartman 	.throttle	= uart_throttle,
2760ab4382d2SGreg Kroah-Hartman 	.unthrottle	= uart_unthrottle,
2761ab4382d2SGreg Kroah-Hartman 	.send_xchar	= uart_send_xchar,
2762ab4382d2SGreg Kroah-Hartman 	.set_termios	= uart_set_termios,
2763ab4382d2SGreg Kroah-Hartman 	.set_ldisc	= uart_set_ldisc,
2764ab4382d2SGreg Kroah-Hartman 	.stop		= uart_stop,
2765ab4382d2SGreg Kroah-Hartman 	.start		= uart_start,
2766ab4382d2SGreg Kroah-Hartman 	.hangup		= uart_hangup,
2767ab4382d2SGreg Kroah-Hartman 	.break_ctl	= uart_break_ctl,
2768ab4382d2SGreg Kroah-Hartman 	.wait_until_sent= uart_wait_until_sent,
2769ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PROC_FS
27708a8dcabfSChristoph Hellwig 	.proc_show	= uart_proc_show,
2771ab4382d2SGreg Kroah-Hartman #endif
2772ab4382d2SGreg Kroah-Hartman 	.tiocmget	= uart_tiocmget,
2773ab4382d2SGreg Kroah-Hartman 	.tiocmset	= uart_tiocmset,
27745099d234SAl Viro 	.set_serial	= uart_set_info_user,
27755099d234SAl Viro 	.get_serial	= uart_get_info_user,
2776ab4382d2SGreg Kroah-Hartman 	.get_icount	= uart_get_icount,
2777ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2778ab4382d2SGreg Kroah-Hartman 	.poll_init	= uart_poll_init,
2779ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= uart_poll_get_char,
2780ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= uart_poll_put_char,
2781ab4382d2SGreg Kroah-Hartman #endif
2782ab4382d2SGreg Kroah-Hartman };
2783ab4382d2SGreg Kroah-Hartman 
2784ab4382d2SGreg Kroah-Hartman static const struct tty_port_operations uart_port_ops = {
2785ab4382d2SGreg Kroah-Hartman 	.carrier_raised = uart_carrier_raised,
2786ab4382d2SGreg Kroah-Hartman 	.dtr_rts	= uart_dtr_rts,
2787b3b57646SRob Herring 	.activate	= uart_port_activate,
2788761ed4a9SRob Herring 	.shutdown	= uart_tty_port_shutdown,
2789ab4382d2SGreg Kroah-Hartman };
2790ab4382d2SGreg Kroah-Hartman 
2791ab4382d2SGreg Kroah-Hartman /**
2792ab4382d2SGreg Kroah-Hartman  * uart_register_driver - register a driver with the uart core layer
2793ab4382d2SGreg Kroah-Hartman  * @drv: low level driver structure
2794ab4382d2SGreg Kroah-Hartman  *
2795c4bd17a6SJiri Slaby  * Register a uart driver with the core driver. We in turn register with the
2796c4bd17a6SJiri Slaby  * tty layer, and initialise the core driver per-port state.
2797ab4382d2SGreg Kroah-Hartman  *
2798c4bd17a6SJiri Slaby  * We have a proc file in /proc/tty/driver which is named after the normal
2799c4bd17a6SJiri Slaby  * driver.
2800ab4382d2SGreg Kroah-Hartman  *
2801c4bd17a6SJiri Slaby  * @drv->port should be %NULL, and the per-port structures should be registered
2802c4bd17a6SJiri Slaby  * using uart_add_one_port() after this call has succeeded.
2803c4bd17a6SJiri Slaby  *
2804c4bd17a6SJiri Slaby  * Locking: none, Interrupts: enabled
2805ab4382d2SGreg Kroah-Hartman  */
uart_register_driver(struct uart_driver * drv)2806ab4382d2SGreg Kroah-Hartman int uart_register_driver(struct uart_driver *drv)
2807ab4382d2SGreg Kroah-Hartman {
2808ab4382d2SGreg Kroah-Hartman 	struct tty_driver *normal;
2809050dfc09SSergey Organov 	int i, retval = -ENOMEM;
2810ab4382d2SGreg Kroah-Hartman 
2811ab4382d2SGreg Kroah-Hartman 	BUG_ON(drv->state);
2812ab4382d2SGreg Kroah-Hartman 
2813ab4382d2SGreg Kroah-Hartman 	/*
2814ab4382d2SGreg Kroah-Hartman 	 * Maybe we should be using a slab cache for this, especially if
2815ab4382d2SGreg Kroah-Hartman 	 * we have a large number of ports to handle.
2816ab4382d2SGreg Kroah-Hartman 	 */
28176396bb22SKees Cook 	drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2818ab4382d2SGreg Kroah-Hartman 	if (!drv->state)
2819ab4382d2SGreg Kroah-Hartman 		goto out;
2820ab4382d2SGreg Kroah-Hartman 
282139b7b42bSJiri Slaby 	normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
282239b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
282339b7b42bSJiri Slaby 	if (IS_ERR(normal)) {
282439b7b42bSJiri Slaby 		retval = PTR_ERR(normal);
2825ab4382d2SGreg Kroah-Hartman 		goto out_kfree;
282639b7b42bSJiri Slaby 	}
2827ab4382d2SGreg Kroah-Hartman 
2828ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = normal;
2829ab4382d2SGreg Kroah-Hartman 
2830ab4382d2SGreg Kroah-Hartman 	normal->driver_name	= drv->driver_name;
2831ab4382d2SGreg Kroah-Hartman 	normal->name		= drv->dev_name;
2832ab4382d2SGreg Kroah-Hartman 	normal->major		= drv->major;
2833ab4382d2SGreg Kroah-Hartman 	normal->minor_start	= drv->minor;
2834ab4382d2SGreg Kroah-Hartman 	normal->type		= TTY_DRIVER_TYPE_SERIAL;
2835ab4382d2SGreg Kroah-Hartman 	normal->subtype		= SERIAL_TYPE_NORMAL;
2836ab4382d2SGreg Kroah-Hartman 	normal->init_termios	= tty_std_termios;
2837ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2838ab4382d2SGreg Kroah-Hartman 	normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2839ab4382d2SGreg Kroah-Hartman 	normal->driver_state    = drv;
2840ab4382d2SGreg Kroah-Hartman 	tty_set_operations(normal, &uart_ops);
2841ab4382d2SGreg Kroah-Hartman 
2842ab4382d2SGreg Kroah-Hartman 	/*
2843ab4382d2SGreg Kroah-Hartman 	 * Initialise the UART state(s).
2844ab4382d2SGreg Kroah-Hartman 	 */
2845ab4382d2SGreg Kroah-Hartman 	for (i = 0; i < drv->nr; i++) {
2846ab4382d2SGreg Kroah-Hartman 		struct uart_state *state = drv->state + i;
2847ab4382d2SGreg Kroah-Hartman 		struct tty_port *port = &state->port;
2848ab4382d2SGreg Kroah-Hartman 
2849ab4382d2SGreg Kroah-Hartman 		tty_port_init(port);
2850ab4382d2SGreg Kroah-Hartman 		port->ops = &uart_port_ops;
2851ab4382d2SGreg Kroah-Hartman 	}
2852ab4382d2SGreg Kroah-Hartman 
2853ab4382d2SGreg Kroah-Hartman 	retval = tty_register_driver(normal);
2854ab4382d2SGreg Kroah-Hartman 	if (retval >= 0)
2855ab4382d2SGreg Kroah-Hartman 		return retval;
2856ab4382d2SGreg Kroah-Hartman 
2857191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2858191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
28599f90a4ddSJiri Slaby 	tty_driver_kref_put(normal);
2860ab4382d2SGreg Kroah-Hartman out_kfree:
2861ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
2862ab4382d2SGreg Kroah-Hartman out:
2863050dfc09SSergey Organov 	return retval;
2864ab4382d2SGreg Kroah-Hartman }
286515dc475bSJiri Slaby EXPORT_SYMBOL(uart_register_driver);
2866ab4382d2SGreg Kroah-Hartman 
2867ab4382d2SGreg Kroah-Hartman /**
2868ab4382d2SGreg Kroah-Hartman  * uart_unregister_driver - remove a driver from the uart core layer
2869ab4382d2SGreg Kroah-Hartman  * @drv: low level driver structure
2870ab4382d2SGreg Kroah-Hartman  *
2871c4bd17a6SJiri Slaby  * Remove all references to a driver from the core driver. The low level
2872c4bd17a6SJiri Slaby  * driver must have removed all its ports via the uart_remove_one_port() if it
2873c4bd17a6SJiri Slaby  * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
2874c4bd17a6SJiri Slaby  *
2875c4bd17a6SJiri Slaby  * Locking: none, Interrupts: enabled
2876ab4382d2SGreg Kroah-Hartman  */
uart_unregister_driver(struct uart_driver * drv)2877ab4382d2SGreg Kroah-Hartman void uart_unregister_driver(struct uart_driver *drv)
2878ab4382d2SGreg Kroah-Hartman {
2879ab4382d2SGreg Kroah-Hartman 	struct tty_driver *p = drv->tty_driver;
2880191c5f10SJiri Slaby 	unsigned int i;
2881191c5f10SJiri Slaby 
2882ab4382d2SGreg Kroah-Hartman 	tty_unregister_driver(p);
28839f90a4ddSJiri Slaby 	tty_driver_kref_put(p);
2884191c5f10SJiri Slaby 	for (i = 0; i < drv->nr; i++)
2885191c5f10SJiri Slaby 		tty_port_destroy(&drv->state[i].port);
2886ab4382d2SGreg Kroah-Hartman 	kfree(drv->state);
28871e66cdedSAlan Cox 	drv->state = NULL;
2888ab4382d2SGreg Kroah-Hartman 	drv->tty_driver = NULL;
2889ab4382d2SGreg Kroah-Hartman }
289015dc475bSJiri Slaby EXPORT_SYMBOL(uart_unregister_driver);
2891ab4382d2SGreg Kroah-Hartman 
uart_console_device(struct console * co,int * index)2892ab4382d2SGreg Kroah-Hartman struct tty_driver *uart_console_device(struct console *co, int *index)
2893ab4382d2SGreg Kroah-Hartman {
2894ab4382d2SGreg Kroah-Hartman 	struct uart_driver *p = co->data;
2895ab4382d2SGreg Kroah-Hartman 	*index = co->index;
2896ab4382d2SGreg Kroah-Hartman 	return p->tty_driver;
2897ab4382d2SGreg Kroah-Hartman }
2898488f49acSJohn Stultz EXPORT_SYMBOL_GPL(uart_console_device);
2899ab4382d2SGreg Kroah-Hartman 
uartclk_show(struct device * dev,struct device_attribute * attr,char * buf)2900143c02c8SAndy Shevchenko static ssize_t uartclk_show(struct device *dev,
29016915c0e4STomas Hlavacek 	struct device_attribute *attr, char *buf)
29026915c0e4STomas Hlavacek {
2903bebe73e3SAlan Cox 	struct serial_struct tmp;
29046915c0e4STomas Hlavacek 	struct tty_port *port = dev_get_drvdata(dev);
2905b1b79916STomas Hlavacek 
29069f109694SAlan Cox 	uart_get_info(port, &tmp);
29079cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.baud_base * 16);
29086915c0e4STomas Hlavacek }
29096915c0e4STomas Hlavacek 
type_show(struct device * dev,struct device_attribute * attr,char * buf)2910143c02c8SAndy Shevchenko static ssize_t type_show(struct device *dev,
2911373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2912373bac4cSAlan Cox {
2913373bac4cSAlan Cox 	struct serial_struct tmp;
2914373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2915373bac4cSAlan Cox 
2916373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29179cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.type);
2918373bac4cSAlan Cox }
2919143c02c8SAndy Shevchenko 
line_show(struct device * dev,struct device_attribute * attr,char * buf)2920143c02c8SAndy Shevchenko static ssize_t line_show(struct device *dev,
2921373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2922373bac4cSAlan Cox {
2923373bac4cSAlan Cox 	struct serial_struct tmp;
2924373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2925373bac4cSAlan Cox 
2926373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29279cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.line);
2928373bac4cSAlan Cox }
2929373bac4cSAlan Cox 
port_show(struct device * dev,struct device_attribute * attr,char * buf)2930143c02c8SAndy Shevchenko static ssize_t port_show(struct device *dev,
2931373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2932373bac4cSAlan Cox {
2933373bac4cSAlan Cox 	struct serial_struct tmp;
2934373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2935fd985e1dSAndrew Morton 	unsigned long ioaddr;
2936373bac4cSAlan Cox 
2937373bac4cSAlan Cox 	uart_get_info(port, &tmp);
2938fd985e1dSAndrew Morton 	ioaddr = tmp.port;
2939fd985e1dSAndrew Morton 	if (HIGH_BITS_OFFSET)
2940fd985e1dSAndrew Morton 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
29419cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", ioaddr);
2942373bac4cSAlan Cox }
2943373bac4cSAlan Cox 
irq_show(struct device * dev,struct device_attribute * attr,char * buf)2944143c02c8SAndy Shevchenko static ssize_t irq_show(struct device *dev,
2945373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2946373bac4cSAlan Cox {
2947373bac4cSAlan Cox 	struct serial_struct tmp;
2948373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2949373bac4cSAlan Cox 
2950373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29519cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.irq);
2952373bac4cSAlan Cox }
2953373bac4cSAlan Cox 
flags_show(struct device * dev,struct device_attribute * attr,char * buf)2954143c02c8SAndy Shevchenko static ssize_t flags_show(struct device *dev,
2955373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2956373bac4cSAlan Cox {
2957373bac4cSAlan Cox 	struct serial_struct tmp;
2958373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2959373bac4cSAlan Cox 
2960373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29619cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%X\n", tmp.flags);
2962373bac4cSAlan Cox }
2963373bac4cSAlan Cox 
xmit_fifo_size_show(struct device * dev,struct device_attribute * attr,char * buf)2964143c02c8SAndy Shevchenko static ssize_t xmit_fifo_size_show(struct device *dev,
2965373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2966373bac4cSAlan Cox {
2967373bac4cSAlan Cox 	struct serial_struct tmp;
2968373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2969373bac4cSAlan Cox 
2970373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29719cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2972373bac4cSAlan Cox }
2973373bac4cSAlan Cox 
close_delay_show(struct device * dev,struct device_attribute * attr,char * buf)2974143c02c8SAndy Shevchenko static ssize_t close_delay_show(struct device *dev,
2975373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2976373bac4cSAlan Cox {
2977373bac4cSAlan Cox 	struct serial_struct tmp;
2978373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2979373bac4cSAlan Cox 
2980373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29819cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.close_delay);
2982373bac4cSAlan Cox }
2983373bac4cSAlan Cox 
closing_wait_show(struct device * dev,struct device_attribute * attr,char * buf)2984143c02c8SAndy Shevchenko static ssize_t closing_wait_show(struct device *dev,
2985373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2986373bac4cSAlan Cox {
2987373bac4cSAlan Cox 	struct serial_struct tmp;
2988373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2989373bac4cSAlan Cox 
2990373bac4cSAlan Cox 	uart_get_info(port, &tmp);
29919cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.closing_wait);
2992373bac4cSAlan Cox }
2993373bac4cSAlan Cox 
custom_divisor_show(struct device * dev,struct device_attribute * attr,char * buf)2994143c02c8SAndy Shevchenko static ssize_t custom_divisor_show(struct device *dev,
2995373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
2996373bac4cSAlan Cox {
2997373bac4cSAlan Cox 	struct serial_struct tmp;
2998373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
2999373bac4cSAlan Cox 
3000373bac4cSAlan Cox 	uart_get_info(port, &tmp);
30019cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.custom_divisor);
3002373bac4cSAlan Cox }
3003373bac4cSAlan Cox 
io_type_show(struct device * dev,struct device_attribute * attr,char * buf)3004143c02c8SAndy Shevchenko static ssize_t io_type_show(struct device *dev,
3005373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
3006373bac4cSAlan Cox {
3007373bac4cSAlan Cox 	struct serial_struct tmp;
3008373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
3009373bac4cSAlan Cox 
3010373bac4cSAlan Cox 	uart_get_info(port, &tmp);
30119cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.io_type);
3012373bac4cSAlan Cox }
3013373bac4cSAlan Cox 
iomem_base_show(struct device * dev,struct device_attribute * attr,char * buf)3014143c02c8SAndy Shevchenko static ssize_t iomem_base_show(struct device *dev,
3015373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
3016373bac4cSAlan Cox {
3017373bac4cSAlan Cox 	struct serial_struct tmp;
3018373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
3019373bac4cSAlan Cox 
3020373bac4cSAlan Cox 	uart_get_info(port, &tmp);
30219cfbf7a6SAlex Dewar 	return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
3022373bac4cSAlan Cox }
3023373bac4cSAlan Cox 
iomem_reg_shift_show(struct device * dev,struct device_attribute * attr,char * buf)3024143c02c8SAndy Shevchenko static ssize_t iomem_reg_shift_show(struct device *dev,
3025373bac4cSAlan Cox 	struct device_attribute *attr, char *buf)
3026373bac4cSAlan Cox {
3027373bac4cSAlan Cox 	struct serial_struct tmp;
3028373bac4cSAlan Cox 	struct tty_port *port = dev_get_drvdata(dev);
3029373bac4cSAlan Cox 
3030373bac4cSAlan Cox 	uart_get_info(port, &tmp);
30319cfbf7a6SAlex Dewar 	return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
3032373bac4cSAlan Cox }
3033373bac4cSAlan Cox 
console_show(struct device * dev,struct device_attribute * attr,char * buf)3034a3cb39d2SAndy Shevchenko static ssize_t console_show(struct device *dev,
3035a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, char *buf)
3036a3cb39d2SAndy Shevchenko {
3037a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
3038a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
3039a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
3040a3cb39d2SAndy Shevchenko 	bool console = false;
3041a3cb39d2SAndy Shevchenko 
3042a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
3043a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
3044a3cb39d2SAndy Shevchenko 	if (uport)
3045452b9b24SJohn Ogness 		console = uart_console_registered(uport);
3046a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
3047a3cb39d2SAndy Shevchenko 
3048a3cb39d2SAndy Shevchenko 	return sprintf(buf, "%c\n", console ? 'Y' : 'N');
3049a3cb39d2SAndy Shevchenko }
3050a3cb39d2SAndy Shevchenko 
console_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3051a3cb39d2SAndy Shevchenko static ssize_t console_store(struct device *dev,
3052a3cb39d2SAndy Shevchenko 	struct device_attribute *attr, const char *buf, size_t count)
3053a3cb39d2SAndy Shevchenko {
3054a3cb39d2SAndy Shevchenko 	struct tty_port *port = dev_get_drvdata(dev);
3055a3cb39d2SAndy Shevchenko 	struct uart_state *state = container_of(port, struct uart_state, port);
3056a3cb39d2SAndy Shevchenko 	struct uart_port *uport;
3057a3cb39d2SAndy Shevchenko 	bool oldconsole, newconsole;
3058a3cb39d2SAndy Shevchenko 	int ret;
3059a3cb39d2SAndy Shevchenko 
3060a3cb39d2SAndy Shevchenko 	ret = kstrtobool(buf, &newconsole);
3061a3cb39d2SAndy Shevchenko 	if (ret)
3062a3cb39d2SAndy Shevchenko 		return ret;
3063a3cb39d2SAndy Shevchenko 
3064a3cb39d2SAndy Shevchenko 	mutex_lock(&port->mutex);
3065a3cb39d2SAndy Shevchenko 	uport = uart_port_check(state);
3066a3cb39d2SAndy Shevchenko 	if (uport) {
3067452b9b24SJohn Ogness 		oldconsole = uart_console_registered(uport);
3068a3cb39d2SAndy Shevchenko 		if (oldconsole && !newconsole) {
3069a3cb39d2SAndy Shevchenko 			ret = unregister_console(uport->cons);
3070a3cb39d2SAndy Shevchenko 		} else if (!oldconsole && newconsole) {
3071e0830dbfSJohan Hovold 			if (uart_console(uport)) {
3072e0830dbfSJohan Hovold 				uport->console_reinit = 1;
3073a3cb39d2SAndy Shevchenko 				register_console(uport->cons);
3074e0830dbfSJohan Hovold 			} else {
3075a3cb39d2SAndy Shevchenko 				ret = -ENOENT;
3076a3cb39d2SAndy Shevchenko 			}
3077e0830dbfSJohan Hovold 		}
3078a3cb39d2SAndy Shevchenko 	} else {
3079a3cb39d2SAndy Shevchenko 		ret = -ENXIO;
3080a3cb39d2SAndy Shevchenko 	}
3081a3cb39d2SAndy Shevchenko 	mutex_unlock(&port->mutex);
3082a3cb39d2SAndy Shevchenko 
3083a3cb39d2SAndy Shevchenko 	return ret < 0 ? ret : count;
3084a3cb39d2SAndy Shevchenko }
3085a3cb39d2SAndy Shevchenko 
3086143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(uartclk);
3087143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(type);
3088143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(line);
3089143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(port);
3090143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(irq);
3091143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(flags);
3092143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(xmit_fifo_size);
3093143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(close_delay);
3094143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(closing_wait);
3095143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(custom_divisor);
3096143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(io_type);
3097143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_base);
3098143c02c8SAndy Shevchenko static DEVICE_ATTR_RO(iomem_reg_shift);
3099a3cb39d2SAndy Shevchenko static DEVICE_ATTR_RW(console);
31006915c0e4STomas Hlavacek 
31016915c0e4STomas Hlavacek static struct attribute *tty_dev_attrs[] = {
3102143c02c8SAndy Shevchenko 	&dev_attr_uartclk.attr,
3103373bac4cSAlan Cox 	&dev_attr_type.attr,
3104373bac4cSAlan Cox 	&dev_attr_line.attr,
3105373bac4cSAlan Cox 	&dev_attr_port.attr,
3106373bac4cSAlan Cox 	&dev_attr_irq.attr,
3107373bac4cSAlan Cox 	&dev_attr_flags.attr,
3108373bac4cSAlan Cox 	&dev_attr_xmit_fifo_size.attr,
3109373bac4cSAlan Cox 	&dev_attr_close_delay.attr,
3110373bac4cSAlan Cox 	&dev_attr_closing_wait.attr,
3111373bac4cSAlan Cox 	&dev_attr_custom_divisor.attr,
3112373bac4cSAlan Cox 	&dev_attr_io_type.attr,
3113373bac4cSAlan Cox 	&dev_attr_iomem_base.attr,
3114373bac4cSAlan Cox 	&dev_attr_iomem_reg_shift.attr,
3115a3cb39d2SAndy Shevchenko 	&dev_attr_console.attr,
3116a3cb39d2SAndy Shevchenko 	NULL
31176915c0e4STomas Hlavacek };
31186915c0e4STomas Hlavacek 
3119b1b79916STomas Hlavacek static const struct attribute_group tty_dev_attr_group = {
31206915c0e4STomas Hlavacek 	.attrs = tty_dev_attrs,
31216915c0e4STomas Hlavacek };
31226915c0e4STomas Hlavacek 
3123ab4382d2SGreg Kroah-Hartman /**
312484a9582fSTony Lindgren  * serial_core_add_one_port - attach a driver-defined port structure
3125ab4382d2SGreg Kroah-Hartman  * @drv: pointer to the uart low level driver structure for this port
3126ab4382d2SGreg Kroah-Hartman  * @uport: uart port structure to use for this port.
3127ab4382d2SGreg Kroah-Hartman  *
3128a157270fSAhmed S. Darwish  * Context: task context, might sleep
3129a157270fSAhmed S. Darwish  *
3130c4bd17a6SJiri Slaby  * This allows the driver @drv to register its own uart_port structure with the
3131c4bd17a6SJiri Slaby  * core driver. The main purpose is to allow the low level uart drivers to
3132c4bd17a6SJiri Slaby  * expand uart_port, rather than having yet more levels of structures.
313384a9582fSTony Lindgren  * Caller must hold port_mutex.
3134ab4382d2SGreg Kroah-Hartman  */
serial_core_add_one_port(struct uart_driver * drv,struct uart_port * uport)313584a9582fSTony Lindgren static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport)
3136ab4382d2SGreg Kroah-Hartman {
3137ab4382d2SGreg Kroah-Hartman 	struct uart_state *state;
3138ab4382d2SGreg Kroah-Hartman 	struct tty_port *port;
3139ab4382d2SGreg Kroah-Hartman 	int ret = 0;
3140ab4382d2SGreg Kroah-Hartman 	struct device *tty_dev;
3141266dcff0SGreg Kroah-Hartman 	int num_groups;
3142ab4382d2SGreg Kroah-Hartman 
3143ab4382d2SGreg Kroah-Hartman 	if (uport->line >= drv->nr)
3144ab4382d2SGreg Kroah-Hartman 		return -EINVAL;
3145ab4382d2SGreg Kroah-Hartman 
3146ab4382d2SGreg Kroah-Hartman 	state = drv->state + uport->line;
3147ab4382d2SGreg Kroah-Hartman 	port = &state->port;
3148ab4382d2SGreg Kroah-Hartman 
3149ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
3150ab4382d2SGreg Kroah-Hartman 	if (state->uart_port) {
3151ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
3152ab4382d2SGreg Kroah-Hartman 		goto out;
3153ab4382d2SGreg Kroah-Hartman 	}
3154ab4382d2SGreg Kroah-Hartman 
31552b702b9bSPeter Hurley 	/* Link the port to the driver state table and vice versa */
31569ed19428SPeter Hurley 	atomic_set(&state->refcount, 1);
31579ed19428SPeter Hurley 	init_waitqueue_head(&state->remove_wait);
3158ab4382d2SGreg Kroah-Hartman 	state->uart_port = uport;
3159ab4382d2SGreg Kroah-Hartman 	uport->state = state;
3160ab4382d2SGreg Kroah-Hartman 
3161eabd4600SJohn Ogness 	/*
3162eabd4600SJohn Ogness 	 * If this port is in use as a console then the spinlock is already
3163eabd4600SJohn Ogness 	 * initialised.
3164eabd4600SJohn Ogness 	 */
3165eabd4600SJohn Ogness 	if (!uart_console_registered(uport))
3166eabd4600SJohn Ogness 		uart_port_spin_lock_init(uport);
3167eabd4600SJohn Ogness 
31682b702b9bSPeter Hurley 	state->pm_state = UART_PM_STATE_UNDEFINED;
3169eabd4600SJohn Ogness 	uart_port_set_cons(uport, drv->cons);
3170959801feSPeter Hurley 	uport->minor = drv->tty_driver->minor_start + uport->line;
3171f7048b15SVignesh R 	uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3172f7048b15SVignesh R 				drv->tty_driver->name_base + uport->line);
3173f7048b15SVignesh R 	if (!uport->name) {
3174f7048b15SVignesh R 		ret = -ENOMEM;
3175f7048b15SVignesh R 		goto out;
3176f7048b15SVignesh R 	}
31772b702b9bSPeter Hurley 
3178a208ffd2SGrant Likely 	if (uport->cons && uport->dev)
3179dd8b7a1dSMichal Simek 		of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3180ab4382d2SGreg Kroah-Hartman 
3181fb2b9001SSudip Mukherjee 	tty_port_link_device(port, drv->tty_driver, uport->line);
3182ab4382d2SGreg Kroah-Hartman 	uart_configure_port(drv, state, uport);
3183ab4382d2SGreg Kroah-Hartman 
31844dda864dSGeert Uytterhoeven 	port->console = uart_console(uport);
31854dda864dSGeert Uytterhoeven 
3186266dcff0SGreg Kroah-Hartman 	num_groups = 2;
3187266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3188266dcff0SGreg Kroah-Hartman 		num_groups++;
3189266dcff0SGreg Kroah-Hartman 
3190c2b703b8SYoshihiro YUNOMAE 	uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3191266dcff0SGreg Kroah-Hartman 				    GFP_KERNEL);
3192266dcff0SGreg Kroah-Hartman 	if (!uport->tty_groups) {
3193266dcff0SGreg Kroah-Hartman 		ret = -ENOMEM;
3194266dcff0SGreg Kroah-Hartman 		goto out;
3195266dcff0SGreg Kroah-Hartman 	}
3196266dcff0SGreg Kroah-Hartman 	uport->tty_groups[0] = &tty_dev_attr_group;
3197266dcff0SGreg Kroah-Hartman 	if (uport->attr_group)
3198266dcff0SGreg Kroah-Hartman 		uport->tty_groups[1] = uport->attr_group;
3199266dcff0SGreg Kroah-Hartman 
3200e21de145SHans de Goede 	/* Ensure serdev drivers can call serdev_device_open() right away */
3201e21de145SHans de Goede 	uport->flags &= ~UPF_DEAD;
3202e21de145SHans de Goede 
3203ab4382d2SGreg Kroah-Hartman 	/*
3204ab4382d2SGreg Kroah-Hartman 	 * Register the port whether it's detected or not.  This allows
3205015355b7SGeert Uytterhoeven 	 * setserial to be used to alter this port's parameters.
3206ab4382d2SGreg Kroah-Hartman 	 */
3207da4c2799SJohan Hovold 	tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3208b286f4e8STony Lindgren 			uport->line, uport->dev, &uport->port_dev->dev, port,
3209b286f4e8STony Lindgren 			uport->tty_groups);
3210b289c496SChengguang Xu 	if (!IS_ERR(tty_dev)) {
321177359835SSimon Glass 		device_set_wakeup_capable(tty_dev, 1);
321277359835SSimon Glass 	} else {
3213e21de145SHans de Goede 		uport->flags |= UPF_DEAD;
32142f2dafe7SSudip Mukherjee 		dev_err(uport->dev, "Cannot register tty device on line %d\n",
3215ab4382d2SGreg Kroah-Hartman 		       uport->line);
321677359835SSimon Glass 	}
3217ab4382d2SGreg Kroah-Hartman 
3218ab4382d2SGreg Kroah-Hartman  out:
3219ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3220ab4382d2SGreg Kroah-Hartman 
3221ab4382d2SGreg Kroah-Hartman 	return ret;
3222ab4382d2SGreg Kroah-Hartman }
3223ab4382d2SGreg Kroah-Hartman 
3224ab4382d2SGreg Kroah-Hartman /**
322584a9582fSTony Lindgren  * serial_core_remove_one_port - detach a driver defined port structure
3226ab4382d2SGreg Kroah-Hartman  * @drv: pointer to the uart low level driver structure for this port
3227ab4382d2SGreg Kroah-Hartman  * @uport: uart port structure for this port
3228ab4382d2SGreg Kroah-Hartman  *
3229a157270fSAhmed S. Darwish  * Context: task context, might sleep
3230a157270fSAhmed S. Darwish  *
3231c4bd17a6SJiri Slaby  * This unhooks (and hangs up) the specified port structure from the core
3232c4bd17a6SJiri Slaby  * driver. No further calls will be made to the low-level code for this port.
323384a9582fSTony Lindgren  * Caller must hold port_mutex.
3234ab4382d2SGreg Kroah-Hartman  */
serial_core_remove_one_port(struct uart_driver * drv,struct uart_port * uport)323584a9582fSTony Lindgren static void serial_core_remove_one_port(struct uart_driver *drv,
323684a9582fSTony Lindgren 					struct uart_port *uport)
3237ab4382d2SGreg Kroah-Hartman {
3238ab4382d2SGreg Kroah-Hartman 	struct uart_state *state = drv->state + uport->line;
3239ab4382d2SGreg Kroah-Hartman 	struct tty_port *port = &state->port;
32404047b371SPeter Hurley 	struct uart_port *uart_port;
32414c6d5b4dSGeert Uytterhoeven 	struct tty_struct *tty;
3242ab4382d2SGreg Kroah-Hartman 
3243ab4382d2SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
32444047b371SPeter Hurley 	uart_port = uart_port_check(state);
32454047b371SPeter Hurley 	if (uart_port != uport)
32464047b371SPeter Hurley 		dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
32474047b371SPeter Hurley 			  uart_port, uport);
32484047b371SPeter Hurley 
32494047b371SPeter Hurley 	if (!uart_port) {
3250b342dd51SChen Gang 		mutex_unlock(&port->mutex);
3251d0a39608SSteven Price 		return;
3252b342dd51SChen Gang 	}
3253ab4382d2SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
3254ab4382d2SGreg Kroah-Hartman 
3255ab4382d2SGreg Kroah-Hartman 	/*
3256ab4382d2SGreg Kroah-Hartman 	 * Remove the devices from the tty layer
3257ab4382d2SGreg Kroah-Hartman 	 */
3258da4c2799SJohan Hovold 	tty_port_unregister_device(port, drv->tty_driver, uport->line);
3259ab4382d2SGreg Kroah-Hartman 
32604c6d5b4dSGeert Uytterhoeven 	tty = tty_port_tty_get(port);
32614c6d5b4dSGeert Uytterhoeven 	if (tty) {
3262ab4382d2SGreg Kroah-Hartman 		tty_vhangup(port->tty);
32634c6d5b4dSGeert Uytterhoeven 		tty_kref_put(tty);
32644c6d5b4dSGeert Uytterhoeven 	}
3265ab4382d2SGreg Kroah-Hartman 
3266ab4382d2SGreg Kroah-Hartman 	/*
32675f5c9ae5SGeert Uytterhoeven 	 * If the port is used as a console, unregister it
32685f5c9ae5SGeert Uytterhoeven 	 */
32695f5c9ae5SGeert Uytterhoeven 	if (uart_console(uport))
32705f5c9ae5SGeert Uytterhoeven 		unregister_console(uport->cons);
32715f5c9ae5SGeert Uytterhoeven 
32725f5c9ae5SGeert Uytterhoeven 	/*
3273ab4382d2SGreg Kroah-Hartman 	 * Free the port IO and memory resources, if any.
3274ab4382d2SGreg Kroah-Hartman 	 */
3275a7cfaf16SFabio Estevam 	if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3276ab4382d2SGreg Kroah-Hartman 		uport->ops->release_port(uport);
3277266dcff0SGreg Kroah-Hartman 	kfree(uport->tty_groups);
3278f7048b15SVignesh R 	kfree(uport->name);
3279ab4382d2SGreg Kroah-Hartman 
3280ab4382d2SGreg Kroah-Hartman 	/*
3281ab4382d2SGreg Kroah-Hartman 	 * Indicate that there isn't a port here anymore.
3282ab4382d2SGreg Kroah-Hartman 	 */
3283ab4382d2SGreg Kroah-Hartman 	uport->type = PORT_UNKNOWN;
328484a9582fSTony Lindgren 	uport->port_dev = NULL;
3285ab4382d2SGreg Kroah-Hartman 
32864047b371SPeter Hurley 	mutex_lock(&port->mutex);
32879ed19428SPeter Hurley 	WARN_ON(atomic_dec_return(&state->refcount) < 0);
32889ed19428SPeter Hurley 	wait_event(state->remove_wait, !atomic_read(&state->refcount));
3289ab4382d2SGreg Kroah-Hartman 	state->uart_port = NULL;
32904047b371SPeter Hurley 	mutex_unlock(&port->mutex);
3291ab4382d2SGreg Kroah-Hartman }
3292ab4382d2SGreg Kroah-Hartman 
3293c4bd17a6SJiri Slaby /**
3294c4bd17a6SJiri Slaby  * uart_match_port - are the two ports equivalent?
3295c4bd17a6SJiri Slaby  * @port1: first port
3296c4bd17a6SJiri Slaby  * @port2: second port
3297c4bd17a6SJiri Slaby  *
3298c4bd17a6SJiri Slaby  * This utility function can be used to determine whether two uart_port
3299c4bd17a6SJiri Slaby  * structures describe the same port.
3300ab4382d2SGreg Kroah-Hartman  */
uart_match_port(const struct uart_port * port1,const struct uart_port * port2)3301b8be5db5SJiri Slaby bool uart_match_port(const struct uart_port *port1,
3302b8be5db5SJiri Slaby 		const struct uart_port *port2)
3303ab4382d2SGreg Kroah-Hartman {
3304ab4382d2SGreg Kroah-Hartman 	if (port1->iotype != port2->iotype)
3305b8be5db5SJiri Slaby 		return false;
3306ab4382d2SGreg Kroah-Hartman 
3307ab4382d2SGreg Kroah-Hartman 	switch (port1->iotype) {
3308ab4382d2SGreg Kroah-Hartman 	case UPIO_PORT:
3309b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase;
3310ab4382d2SGreg Kroah-Hartman 	case UPIO_HUB6:
3311b8be5db5SJiri Slaby 		return port1->iobase == port2->iobase &&
3312b8be5db5SJiri Slaby 		       port1->hub6   == port2->hub6;
3313ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM:
3314bd94c407SMasahiro Yamada 	case UPIO_MEM16:
3315ab4382d2SGreg Kroah-Hartman 	case UPIO_MEM32:
33163ffb1a81SKevin Cernekee 	case UPIO_MEM32BE:
3317ab4382d2SGreg Kroah-Hartman 	case UPIO_AU:
3318ab4382d2SGreg Kroah-Hartman 	case UPIO_TSI:
3319b8be5db5SJiri Slaby 		return port1->mapbase == port2->mapbase;
3320ab4382d2SGreg Kroah-Hartman 	}
3321b8be5db5SJiri Slaby 
3322b8be5db5SJiri Slaby 	return false;
3323ab4382d2SGreg Kroah-Hartman }
3324ab4382d2SGreg Kroah-Hartman EXPORT_SYMBOL(uart_match_port);
3325ab4382d2SGreg Kroah-Hartman 
332684a9582fSTony Lindgren static struct serial_ctrl_device *
serial_core_get_ctrl_dev(struct serial_port_device * port_dev)332784a9582fSTony Lindgren serial_core_get_ctrl_dev(struct serial_port_device *port_dev)
332884a9582fSTony Lindgren {
332984a9582fSTony Lindgren 	struct device *dev = &port_dev->dev;
333084a9582fSTony Lindgren 
333184a9582fSTony Lindgren 	return to_serial_base_ctrl_device(dev->parent);
333284a9582fSTony Lindgren }
333384a9582fSTony Lindgren 
333484a9582fSTony Lindgren /*
333584a9582fSTony Lindgren  * Find a registered serial core controller device if one exists. Returns
333684a9582fSTony Lindgren  * the first device matching the ctrl_id. Caller must hold port_mutex.
333784a9582fSTony Lindgren  */
serial_core_ctrl_find(struct uart_driver * drv,struct device * phys_dev,int ctrl_id)333884a9582fSTony Lindgren static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv,
333984a9582fSTony Lindgren 							struct device *phys_dev,
334084a9582fSTony Lindgren 							int ctrl_id)
334184a9582fSTony Lindgren {
334284a9582fSTony Lindgren 	struct uart_state *state;
334384a9582fSTony Lindgren 	int i;
334484a9582fSTony Lindgren 
334584a9582fSTony Lindgren 	lockdep_assert_held(&port_mutex);
334684a9582fSTony Lindgren 
334784a9582fSTony Lindgren 	for (i = 0; i < drv->nr; i++) {
334884a9582fSTony Lindgren 		state = drv->state + i;
334984a9582fSTony Lindgren 		if (!state->uart_port || !state->uart_port->port_dev)
335084a9582fSTony Lindgren 			continue;
335184a9582fSTony Lindgren 
335284a9582fSTony Lindgren 		if (state->uart_port->dev == phys_dev &&
335384a9582fSTony Lindgren 		    state->uart_port->ctrl_id == ctrl_id)
335484a9582fSTony Lindgren 			return serial_core_get_ctrl_dev(state->uart_port->port_dev);
335584a9582fSTony Lindgren 	}
335684a9582fSTony Lindgren 
335784a9582fSTony Lindgren 	return NULL;
335884a9582fSTony Lindgren }
335984a9582fSTony Lindgren 
serial_core_ctrl_device_add(struct uart_port * port)336084a9582fSTony Lindgren static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port)
336184a9582fSTony Lindgren {
336284a9582fSTony Lindgren 	return serial_base_ctrl_add(port, port->dev);
336384a9582fSTony Lindgren }
336484a9582fSTony Lindgren 
serial_core_port_device_add(struct serial_ctrl_device * ctrl_dev,struct uart_port * port)336584a9582fSTony Lindgren static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev,
336684a9582fSTony Lindgren 				       struct uart_port *port)
336784a9582fSTony Lindgren {
336884a9582fSTony Lindgren 	struct serial_port_device *port_dev;
336984a9582fSTony Lindgren 
337084a9582fSTony Lindgren 	port_dev = serial_base_port_add(port, ctrl_dev);
337184a9582fSTony Lindgren 	if (IS_ERR(port_dev))
337284a9582fSTony Lindgren 		return PTR_ERR(port_dev);
337384a9582fSTony Lindgren 
337484a9582fSTony Lindgren 	port->port_dev = port_dev;
337584a9582fSTony Lindgren 
337684a9582fSTony Lindgren 	return 0;
337784a9582fSTony Lindgren }
337884a9582fSTony Lindgren 
337984a9582fSTony Lindgren /*
338084a9582fSTony Lindgren  * Initialize a serial core port device, and a controller device if needed.
338184a9582fSTony Lindgren  */
serial_core_register_port(struct uart_driver * drv,struct uart_port * port)338284a9582fSTony Lindgren int serial_core_register_port(struct uart_driver *drv, struct uart_port *port)
338384a9582fSTony Lindgren {
338484a9582fSTony Lindgren 	struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL;
338584a9582fSTony Lindgren 	int ret;
338684a9582fSTony Lindgren 
338784a9582fSTony Lindgren 	mutex_lock(&port_mutex);
338884a9582fSTony Lindgren 
338984a9582fSTony Lindgren 	/*
339084a9582fSTony Lindgren 	 * Prevent serial_port_runtime_resume() from trying to use the port
339184a9582fSTony Lindgren 	 * until serial_core_add_one_port() has completed
339284a9582fSTony Lindgren 	 */
339384a9582fSTony Lindgren 	port->flags |= UPF_DEAD;
339484a9582fSTony Lindgren 
339584a9582fSTony Lindgren 	/* Inititalize a serial core controller device if needed */
339684a9582fSTony Lindgren 	ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id);
339784a9582fSTony Lindgren 	if (!ctrl_dev) {
339884a9582fSTony Lindgren 		new_ctrl_dev = serial_core_ctrl_device_add(port);
339949c80922STony Lindgren 		if (IS_ERR(new_ctrl_dev)) {
340049c80922STony Lindgren 			ret = PTR_ERR(new_ctrl_dev);
340184a9582fSTony Lindgren 			goto err_unlock;
340284a9582fSTony Lindgren 		}
340384a9582fSTony Lindgren 		ctrl_dev = new_ctrl_dev;
340484a9582fSTony Lindgren 	}
340584a9582fSTony Lindgren 
340684a9582fSTony Lindgren 	/*
340784a9582fSTony Lindgren 	 * Initialize a serial core port device. Tag the port dead to prevent
340884a9582fSTony Lindgren 	 * serial_port_runtime_resume() trying to do anything until port has
340984a9582fSTony Lindgren 	 * been registered. It gets cleared by serial_core_add_one_port().
341084a9582fSTony Lindgren 	 */
341184a9582fSTony Lindgren 	ret = serial_core_port_device_add(ctrl_dev, port);
341284a9582fSTony Lindgren 	if (ret)
341384a9582fSTony Lindgren 		goto err_unregister_ctrl_dev;
341484a9582fSTony Lindgren 
341512c91cecSTony Lindgren 	ret = serial_base_match_and_update_preferred_console(drv, port);
341612c91cecSTony Lindgren 	if (ret)
341712c91cecSTony Lindgren 		goto err_unregister_port_dev;
341812c91cecSTony Lindgren 
341984a9582fSTony Lindgren 	ret = serial_core_add_one_port(drv, port);
342084a9582fSTony Lindgren 	if (ret)
342184a9582fSTony Lindgren 		goto err_unregister_port_dev;
342284a9582fSTony Lindgren 
342384a9582fSTony Lindgren 	mutex_unlock(&port_mutex);
342484a9582fSTony Lindgren 
342584a9582fSTony Lindgren 	return 0;
342684a9582fSTony Lindgren 
342784a9582fSTony Lindgren err_unregister_port_dev:
342884a9582fSTony Lindgren 	serial_base_port_device_remove(port->port_dev);
342984a9582fSTony Lindgren 
343084a9582fSTony Lindgren err_unregister_ctrl_dev:
343184a9582fSTony Lindgren 	serial_base_ctrl_device_remove(new_ctrl_dev);
343284a9582fSTony Lindgren 
343384a9582fSTony Lindgren err_unlock:
343484a9582fSTony Lindgren 	mutex_unlock(&port_mutex);
343584a9582fSTony Lindgren 
343684a9582fSTony Lindgren 	return ret;
343784a9582fSTony Lindgren }
343884a9582fSTony Lindgren 
343984a9582fSTony Lindgren /*
344084a9582fSTony Lindgren  * Removes a serial core port device, and the related serial core controller
344184a9582fSTony Lindgren  * device if the last instance.
344284a9582fSTony Lindgren  */
serial_core_unregister_port(struct uart_driver * drv,struct uart_port * port)344384a9582fSTony Lindgren void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port)
344484a9582fSTony Lindgren {
344584a9582fSTony Lindgren 	struct device *phys_dev = port->dev;
344684a9582fSTony Lindgren 	struct serial_port_device *port_dev = port->port_dev;
344784a9582fSTony Lindgren 	struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev);
344884a9582fSTony Lindgren 	int ctrl_id = port->ctrl_id;
344984a9582fSTony Lindgren 
345084a9582fSTony Lindgren 	mutex_lock(&port_mutex);
345184a9582fSTony Lindgren 
345284a9582fSTony Lindgren 	port->flags |= UPF_DEAD;
345384a9582fSTony Lindgren 
345484a9582fSTony Lindgren 	serial_core_remove_one_port(drv, port);
345584a9582fSTony Lindgren 
345684a9582fSTony Lindgren 	/* Note that struct uart_port *port is no longer valid at this point */
345784a9582fSTony Lindgren 	serial_base_port_device_remove(port_dev);
345884a9582fSTony Lindgren 
345984a9582fSTony Lindgren 	/* Drop the serial core controller device if no ports are using it */
346084a9582fSTony Lindgren 	if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id))
346184a9582fSTony Lindgren 		serial_base_ctrl_device_remove(ctrl_dev);
346284a9582fSTony Lindgren 
346384a9582fSTony Lindgren 	mutex_unlock(&port_mutex);
346484a9582fSTony Lindgren }
346584a9582fSTony Lindgren 
3466027d7dacSJiri Slaby /**
3467027d7dacSJiri Slaby  * uart_handle_dcd_change - handle a change of carrier detect state
3468027d7dacSJiri Slaby  * @uport: uart_port structure for the open port
34690388a152SIlpo Järvinen  * @active: new carrier detect status
34704d90bb14SPeter Hurley  *
3471987233b3SJiri Slaby  * Caller must hold uport->lock.
3472027d7dacSJiri Slaby  */
uart_handle_dcd_change(struct uart_port * uport,bool active)34730388a152SIlpo Järvinen void uart_handle_dcd_change(struct uart_port *uport, bool active)
3474027d7dacSJiri Slaby {
347542381572SGeorge Spelvin 	struct tty_port *port = &uport->state->port;
347643eca0aeSAlan Cox 	struct tty_struct *tty = port->tty;
3477c993257bSPeter Hurley 	struct tty_ldisc *ld;
3478027d7dacSJiri Slaby 
34794d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
34804d90bb14SPeter Hurley 
3481c993257bSPeter Hurley 	if (tty) {
3482c993257bSPeter Hurley 		ld = tty_ldisc_ref(tty);
348342381572SGeorge Spelvin 		if (ld) {
348442381572SGeorge Spelvin 			if (ld->ops->dcd_change)
34850388a152SIlpo Järvinen 				ld->ops->dcd_change(tty, active);
348642381572SGeorge Spelvin 			tty_ldisc_deref(ld);
348742381572SGeorge Spelvin 		}
3488c993257bSPeter Hurley 	}
3489027d7dacSJiri Slaby 
3490027d7dacSJiri Slaby 	uport->icount.dcd++;
3491027d7dacSJiri Slaby 
3492299245a1SPeter Hurley 	if (uart_dcd_enabled(uport)) {
34930388a152SIlpo Järvinen 		if (active)
3494027d7dacSJiri Slaby 			wake_up_interruptible(&port->open_wait);
349543eca0aeSAlan Cox 		else if (tty)
349643eca0aeSAlan Cox 			tty_hangup(tty);
3497027d7dacSJiri Slaby 	}
3498027d7dacSJiri Slaby }
3499027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3500027d7dacSJiri Slaby 
3501027d7dacSJiri Slaby /**
3502027d7dacSJiri Slaby  * uart_handle_cts_change - handle a change of clear-to-send state
3503027d7dacSJiri Slaby  * @uport: uart_port structure for the open port
3504968d6457SIlpo Järvinen  * @active: new clear-to-send status
35054d90bb14SPeter Hurley  *
3506987233b3SJiri Slaby  * Caller must hold uport->lock.
3507027d7dacSJiri Slaby  */
uart_handle_cts_change(struct uart_port * uport,bool active)3508968d6457SIlpo Järvinen void uart_handle_cts_change(struct uart_port *uport, bool active)
3509027d7dacSJiri Slaby {
35104d90bb14SPeter Hurley 	lockdep_assert_held_once(&uport->lock);
35114d90bb14SPeter Hurley 
3512027d7dacSJiri Slaby 	uport->icount.cts++;
3513027d7dacSJiri Slaby 
3514391f93f2SPeter Hurley 	if (uart_softcts_mode(uport)) {
3515d01f4d18SPeter Hurley 		if (uport->hw_stopped) {
3516968d6457SIlpo Järvinen 			if (active) {
3517b5def43aSIlpo Järvinen 				uport->hw_stopped = false;
3518027d7dacSJiri Slaby 				uport->ops->start_tx(uport);
3519027d7dacSJiri Slaby 				uart_write_wakeup(uport);
3520027d7dacSJiri Slaby 			}
3521027d7dacSJiri Slaby 		} else {
3522968d6457SIlpo Järvinen 			if (!active) {
3523b5def43aSIlpo Järvinen 				uport->hw_stopped = true;
3524027d7dacSJiri Slaby 				uport->ops->stop_tx(uport);
3525027d7dacSJiri Slaby 			}
3526027d7dacSJiri Slaby 		}
3527391f93f2SPeter Hurley 
3528027d7dacSJiri Slaby 	}
3529027d7dacSJiri Slaby }
3530027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3531027d7dacSJiri Slaby 
3532cf75525fSJiri Slaby /**
3533cf75525fSJiri Slaby  * uart_insert_char - push a char to the uart layer
3534cf75525fSJiri Slaby  *
3535cf75525fSJiri Slaby  * User is responsible to call tty_flip_buffer_push when they are done with
3536cf75525fSJiri Slaby  * insertion.
3537cf75525fSJiri Slaby  *
3538cf75525fSJiri Slaby  * @port: corresponding port
3539cf75525fSJiri Slaby  * @status: state of the serial port RX buffer (LSR for 8250)
3540cf75525fSJiri Slaby  * @overrun: mask of overrun bits in @status
3541cf75525fSJiri Slaby  * @ch: character to push
3542cf75525fSJiri Slaby  * @flag: flag for the character (see TTY_NORMAL and friends)
3543cf75525fSJiri Slaby  */
uart_insert_char(struct uart_port * port,unsigned int status,unsigned int overrun,u8 ch,u8 flag)3544027d7dacSJiri Slaby void uart_insert_char(struct uart_port *port, unsigned int status,
3545df007fa0SJiri Slaby 		      unsigned int overrun, u8 ch, u8 flag)
3546027d7dacSJiri Slaby {
354792a19f9cSJiri Slaby 	struct tty_port *tport = &port->state->port;
3548027d7dacSJiri Slaby 
3549027d7dacSJiri Slaby 	if ((status & port->ignore_status_mask & ~overrun) == 0)
355092a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, ch, flag) == 0)
3551dabfb351SCorbin 			++port->icount.buf_overrun;
3552027d7dacSJiri Slaby 
3553027d7dacSJiri Slaby 	/*
3554027d7dacSJiri Slaby 	 * Overrun is special.  Since it's reported immediately,
3555027d7dacSJiri Slaby 	 * it doesn't affect the current character.
3556027d7dacSJiri Slaby 	 */
3557027d7dacSJiri Slaby 	if (status & ~port->ignore_status_mask & overrun)
355892a19f9cSJiri Slaby 		if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3559dabfb351SCorbin 			++port->icount.buf_overrun;
3560027d7dacSJiri Slaby }
3561027d7dacSJiri Slaby EXPORT_SYMBOL_GPL(uart_insert_char);
3562027d7dacSJiri Slaby 
356368af4317SDmitry Safonov #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
356412ae2359SJiri Slaby static const u8 sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
356568af4317SDmitry Safonov 
uart_sysrq_on(struct work_struct * w)356668af4317SDmitry Safonov static void uart_sysrq_on(struct work_struct *w)
356768af4317SDmitry Safonov {
3568b18896ffSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3569b18896ffSAndy Shevchenko 
357068af4317SDmitry Safonov 	sysrq_toggle_support(1);
3571b18896ffSAndy Shevchenko 	pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3572b18896ffSAndy Shevchenko 		sysrq_toggle_seq_len, sysrq_toggle_seq);
357368af4317SDmitry Safonov }
357468af4317SDmitry Safonov static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
357568af4317SDmitry Safonov 
357668af4317SDmitry Safonov /**
357768af4317SDmitry Safonov  * uart_try_toggle_sysrq - Enables SysRq from serial line
357868af4317SDmitry Safonov  * @port: uart_port structure where char(s) after BREAK met
357968af4317SDmitry Safonov  * @ch: new character in the sequence after received BREAK
358068af4317SDmitry Safonov  *
358168af4317SDmitry Safonov  * Enables magic SysRq when the required sequence is met on port
358268af4317SDmitry Safonov  * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
358368af4317SDmitry Safonov  *
3584987233b3SJiri Slaby  * Returns: %false if @ch is out of enabling sequence and should be
3585987233b3SJiri Slaby  * handled some other way, %true if @ch was consumed.
358668af4317SDmitry Safonov  */
uart_try_toggle_sysrq(struct uart_port * port,u8 ch)358712ae2359SJiri Slaby bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch)
358868af4317SDmitry Safonov {
35892ce5eaceSAndy Shevchenko 	int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
35902ce5eaceSAndy Shevchenko 
35912ce5eaceSAndy Shevchenko 	if (!sysrq_toggle_seq_len)
359268af4317SDmitry Safonov 		return false;
359368af4317SDmitry Safonov 
359468af4317SDmitry Safonov 	BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
359568af4317SDmitry Safonov 	if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
359668af4317SDmitry Safonov 		port->sysrq_seq = 0;
359768af4317SDmitry Safonov 		return false;
359868af4317SDmitry Safonov 	}
359968af4317SDmitry Safonov 
36002ce5eaceSAndy Shevchenko 	if (++port->sysrq_seq < sysrq_toggle_seq_len) {
360168af4317SDmitry Safonov 		port->sysrq = jiffies + SYSRQ_TIMEOUT;
360268af4317SDmitry Safonov 		return true;
360368af4317SDmitry Safonov 	}
360468af4317SDmitry Safonov 
360568af4317SDmitry Safonov 	schedule_work(&sysrq_enable_work);
360668af4317SDmitry Safonov 
360768af4317SDmitry Safonov 	port->sysrq = 0;
360868af4317SDmitry Safonov 	return true;
360968af4317SDmitry Safonov }
361008d54703SJohan Hovold EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
361168af4317SDmitry Safonov #endif
361268af4317SDmitry Safonov 
3613ef838a81SUwe Kleine-König /**
3614743f93f8SLukas Wunner  * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3615a7172561SRandy Dunlap  * @port: uart device's target port
3616ef838a81SUwe Kleine-König  *
3617ef838a81SUwe Kleine-König  * This function implements the device tree binding described in
3618ef838a81SUwe Kleine-König  * Documentation/devicetree/bindings/serial/rs485.txt.
3619ef838a81SUwe Kleine-König  */
uart_get_rs485_mode(struct uart_port * port)3620c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port)
3621ef838a81SUwe Kleine-König {
3622c150c0f3SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
3623c150c0f3SLukas Wunner 	struct device *dev = port->dev;
36247cda0b9eSAndy Shevchenko 	enum gpiod_flags dflags;
36257cda0b9eSAndy Shevchenko 	struct gpio_desc *desc;
3626ef838a81SUwe Kleine-König 	u32 rs485_delay[2];
3627ef838a81SUwe Kleine-König 	int ret;
3628ef838a81SUwe Kleine-König 
362974eab89bSLino Sanfilippo 	if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
363074eab89bSLino Sanfilippo 		return 0;
363174eab89bSLino Sanfilippo 
3632743f93f8SLukas Wunner 	ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3633743f93f8SLukas Wunner 					     rs485_delay, 2);
3634ef838a81SUwe Kleine-König 	if (!ret) {
3635ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = rs485_delay[0];
3636ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = rs485_delay[1];
3637ef838a81SUwe Kleine-König 	} else {
3638ef838a81SUwe Kleine-König 		rs485conf->delay_rts_before_send = 0;
3639ef838a81SUwe Kleine-König 		rs485conf->delay_rts_after_send = 0;
3640ef838a81SUwe Kleine-König 	}
3641ef838a81SUwe Kleine-König 
36424dfd1035SLino Sanfilippo 	uart_sanitize_serial_rs485_delays(port, rs485conf);
36434dfd1035SLino Sanfilippo 
3644ef838a81SUwe Kleine-König 	/*
3645f1e5b618SLukas Wunner 	 * Clear full-duplex and enabled flags, set RTS polarity to active high
3646f1e5b618SLukas Wunner 	 * to get to a defined state with the following properties:
3647ef838a81SUwe Kleine-König 	 */
3648f1e5b618SLukas Wunner 	rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3649d58a2df3SLukas Wunner 			      SER_RS485_TERMINATE_BUS |
3650f1e5b618SLukas Wunner 			      SER_RS485_RTS_AFTER_SEND);
3651f1e5b618SLukas Wunner 	rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3652ef838a81SUwe Kleine-König 
3653743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3654ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
3655ef838a81SUwe Kleine-König 
3656743f93f8SLukas Wunner 	if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3657ef838a81SUwe Kleine-König 		rs485conf->flags |= SER_RS485_ENABLED;
3658f1e5b618SLukas Wunner 
3659f1e5b618SLukas Wunner 	if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3660f1e5b618SLukas Wunner 		rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3661f1e5b618SLukas Wunner 		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3662f1e5b618SLukas Wunner 	}
3663c150c0f3SLukas Wunner 
3664d58a2df3SLukas Wunner 	/*
3665d58a2df3SLukas Wunner 	 * Disabling termination by default is the safe choice:  Else if many
3666d58a2df3SLukas Wunner 	 * bus participants enable it, no communication is possible at all.
3667d58a2df3SLukas Wunner 	 * Works fine for short cables and users may enable for longer cables.
3668d58a2df3SLukas Wunner 	 */
36697cda0b9eSAndy Shevchenko 	desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
36707cda0b9eSAndy Shevchenko 	if (IS_ERR(desc))
36717cda0b9eSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
36727cda0b9eSAndy Shevchenko 	port->rs485_term_gpio = desc;
36738bec874fSIlpo Järvinen 	if (port->rs485_term_gpio)
36748bec874fSIlpo Järvinen 		port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
3675d58a2df3SLukas Wunner 
36767cda0b9eSAndy Shevchenko 	dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
3677163f080eSChristoph Niedermaier 		 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
36787cda0b9eSAndy Shevchenko 	desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags);
36797cda0b9eSAndy Shevchenko 	if (IS_ERR(desc))
36807cda0b9eSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
36817cda0b9eSAndy Shevchenko 	port->rs485_rx_during_tx_gpio = desc;
36821a33e33cSLino Sanfilippo 	if (port->rs485_rx_during_tx_gpio)
36831a33e33cSLino Sanfilippo 		port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;
3684163f080eSChristoph Niedermaier 
3685c150c0f3SLukas Wunner 	return 0;
3686ef838a81SUwe Kleine-König }
3687743f93f8SLukas Wunner EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3688ef838a81SUwe Kleine-König 
36894f768e94SIlpo Järvinen /* Compile-time assertions for serial_rs485 layout */
36904f768e94SIlpo Järvinen static_assert(offsetof(struct serial_rs485, padding) ==
36914f768e94SIlpo Järvinen               (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
36924f768e94SIlpo Järvinen static_assert(offsetof(struct serial_rs485, padding1) ==
36934f768e94SIlpo Järvinen 	      offsetof(struct serial_rs485, padding[1]));
36944f768e94SIlpo Järvinen static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
36954f768e94SIlpo Järvinen 	      sizeof(struct serial_rs485));
36964f768e94SIlpo Järvinen 
3697ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Serial driver core");
3698ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
3699