blob: 1e738f265eeaa210626df63cb124145f34d90751 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Driver core for serial ports
4 *
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 *
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/tty.h>
Jiri Slaby027d7da2011-11-09 21:33:43 +010012#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/console.h>
Johan Hovold167cbce2020-06-10 17:51:21 +020017#include <linux/gpio/consumer.h>
Grant Likelya208ffd2014-03-27 18:29:46 -070018#include <linux/of.h>
Alexey Dobriyand196a942009-03-31 15:19:21 -070019#include <linux/proc_fs.h>
20#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
Alan Coxccce6de2009-09-19 13:13:30 -070023#include <linux/serial_core.h>
Dmitry Safonov68af4312020-03-02 17:51:35 +000024#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000026#include <linux/mutex.h>
David Howells794edf32019-08-19 17:17:54 -070027#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Andy Shevchenkoaef3ad12017-08-13 17:47:42 +030029#include <linux/irq.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * This is used to lock changes in serial line configuration.
34 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000035static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ingo Molnar13e83592006-07-03 00:25:03 -070037/*
38 * lockdep: port->lock is initialized in two places, but we
39 * want only one lock-class:
40 */
41static struct lock_class_key port_lock_key;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
44
Alan Cox19225132010-06-01 22:52:51 +020045static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
Alan Coxa46c9992008-02-08 04:18:53 -080046 struct ktermios *old_termios);
Jiri Slaby1f33a512011-07-14 14:35:10 +020047static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
Linus Walleij6f538fe2012-12-07 11:36:08 +010048static void uart_change_pm(struct uart_state *state,
49 enum uart_pm_state pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jiri Slabyb922e192011-11-09 21:33:51 +010051static void uart_port_shutdown(struct tty_port *port);
52
Peter Hurley299245a2014-09-10 15:06:24 -040053static int uart_dcd_enabled(struct uart_port *uport)
54{
Peter Hurleyd4260b52014-10-16 14:19:47 -040055 return !!(uport->status & UPSTAT_DCD_ENABLE);
Peter Hurley299245a2014-09-10 15:06:24 -040056}
57
Peter Hurley9ed19422016-04-09 18:56:34 -070058static inline struct uart_port *uart_port_ref(struct uart_state *state)
59{
60 if (atomic_add_unless(&state->refcount, 1, 0))
61 return state->uart_port;
62 return NULL;
63}
64
65static inline void uart_port_deref(struct uart_port *uport)
66{
Andy Shevchenkoef510be2016-11-24 14:18:55 +020067 if (atomic_dec_and_test(&uport->state->refcount))
Peter Hurley9ed19422016-04-09 18:56:34 -070068 wake_up(&uport->state->remove_wait);
69}
70
71#define uart_port_lock(state, flags) \
72 ({ \
73 struct uart_port *__uport = uart_port_ref(state); \
74 if (__uport) \
75 spin_lock_irqsave(&__uport->lock, flags); \
76 __uport; \
77 })
78
79#define uart_port_unlock(uport, flags) \
80 ({ \
81 struct uart_port *__uport = uport; \
Andy Shevchenkoef510be2016-11-24 14:18:55 +020082 if (__uport) { \
Peter Hurley9ed19422016-04-09 18:56:34 -070083 spin_unlock_irqrestore(&__uport->lock, flags); \
Andy Shevchenkoef510be2016-11-24 14:18:55 +020084 uart_port_deref(__uport); \
85 } \
Peter Hurley9ed19422016-04-09 18:56:34 -070086 })
87
Peter Hurley4047b372016-04-09 18:56:33 -070088static inline struct uart_port *uart_port_check(struct uart_state *state)
89{
Peter Hurley7da4b8b2016-05-03 14:01:51 -070090 lockdep_assert_held(&state->port.mutex);
Peter Hurley4047b372016-04-09 18:56:33 -070091 return state->uart_port;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * This routine is used by the interrupt handler to schedule processing in
96 * the software interrupt portion of the driver.
97 */
98void uart_write_wakeup(struct uart_port *port)
99{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700100 struct uart_state *state = port->state;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800101 /*
102 * This means you called this function _after_ the port was
103 * closed. No cookie for you.
104 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700105 BUG_ON(!state);
Rob Herringd0f4bce2016-10-28 07:07:48 -0500106 tty_port_tty_wakeup(&state->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static void uart_stop(struct tty_struct *tty)
110{
111 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700112 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned long flags;
114
Peter Hurley9ed19422016-04-09 18:56:34 -0700115 port = uart_port_lock(state, flags);
116 if (port)
117 port->ops->stop_tx(port);
118 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static void __uart_start(struct tty_struct *tty)
122{
123 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700124 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Peter Hurley9ed19422016-04-09 18:56:34 -0700126 if (port && !uart_tx_stopped(port))
Russell Kingb129a8c2005-08-31 10:12:14 +0100127 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static void uart_start(struct tty_struct *tty)
131{
132 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700133 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long flags;
135
Peter Hurley9ed19422016-04-09 18:56:34 -0700136 port = uart_port_lock(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 __uart_start(tty);
Peter Hurley9ed19422016-04-09 18:56:34 -0700138 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Denys Vlasenkof4581ca2015-10-27 17:40:01 +0100141static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
143{
144 unsigned long flags;
145 unsigned int old;
146
147 spin_lock_irqsave(&port->lock, flags);
148 old = port->mctrl;
149 port->mctrl = (old & ~clear) | set;
150 if (old != port->mctrl)
151 port->ops->set_mctrl(port, port->mctrl);
152 spin_unlock_irqrestore(&port->lock, flags);
153}
154
Alan Coxa46c9992008-02-08 04:18:53 -0800155#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
156#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Rafael Gagoa6845e12017-07-31 10:46:42 +0200158static void uart_port_dtr_rts(struct uart_port *uport, int raise)
159{
160 int rs485_on = uport->rs485_config &&
161 (uport->rs485.flags & SER_RS485_ENABLED);
162 int RTS_after_send = !!(uport->rs485.flags & SER_RS485_RTS_AFTER_SEND);
163
164 if (raise) {
165 if (rs485_on && !RTS_after_send) {
166 uart_set_mctrl(uport, TIOCM_DTR);
167 uart_clear_mctrl(uport, TIOCM_RTS);
168 } else {
169 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
170 }
171 } else {
172 unsigned int clear = TIOCM_DTR;
173
174 clear |= (!rs485_on || !RTS_after_send) ? TIOCM_RTS : 0;
175 uart_clear_mctrl(uport, clear);
176 }
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100181 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100183static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
184 int init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Peter Hurley4047b372016-04-09 18:56:33 -0700186 struct uart_port *uport = uart_port_check(state);
Johan Hovold18ee37e2021-05-19 11:25:41 +0200187 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned long page;
189 int retval = 0;
190
Alan Cox46d57a42009-09-19 13:13:29 -0700191 if (uport->type == PORT_UNKNOWN)
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100192 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200195 * Make sure the device is in D0 state.
196 */
197 uart_change_pm(state, UART_PM_STATE_ON);
198
199 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * Initialise and allocate the transmit and temporary
201 * buffer.
202 */
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600203 page = get_zeroed_page(GFP_KERNEL);
204 if (!page)
205 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600207 uart_port_lock(state, flags);
208 if (!state->xmit.buf) {
Alan Coxebd2c8f2009-09-19 13:13:28 -0700209 state->xmit.buf = (unsigned char *) page;
210 uart_circ_clear(&state->xmit);
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900211 uart_port_unlock(uport, flags);
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600212 } else {
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900213 uart_port_unlock(uport, flags);
214 /*
215 * Do not free() the page under the port lock, see
216 * uart_shutdown().
217 */
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600218 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Alan Cox46d57a42009-09-19 13:13:29 -0700221 retval = uport->ops->startup(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (retval == 0) {
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200223 if (uart_console(uport) && uport->cons->cflag) {
Alan Coxadc8d742012-07-14 15:31:47 +0100224 tty->termios.c_cflag = uport->cons->cflag;
Pali Rohár027b5712021-10-02 15:09:00 +0200225 tty->termios.c_ispeed = uport->cons->ispeed;
226 tty->termios.c_ospeed = uport->cons->ospeed;
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200227 uport->cons->cflag = 0;
Pali Rohár027b5712021-10-02 15:09:00 +0200228 uport->cons->ispeed = 0;
229 uport->cons->ospeed = 0;
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200230 }
231 /*
232 * Initialise the hardware port settings.
233 */
234 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Peter Hurley9db276f2016-01-10 20:36:15 -0800236 /*
237 * Setup the RTS and DTR signals once the
238 * port is open and ready to respond.
239 */
240 if (init_hw && C_BAUD(tty))
Rafael Gagoa6845e12017-07-31 10:46:42 +0200241 uart_port_dtr_rts(uport, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Jiri Slaby00551972011-08-17 13:48:15 +0200244 /*
245 * This is to allow setserial on this port. People may want to set
246 * port/irq/type and then reconfigure the port properly if it failed
247 * now.
248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (retval && capable(CAP_SYS_ADMIN))
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100250 return 1;
251
252 return retval;
253}
254
255static int uart_startup(struct tty_struct *tty, struct uart_state *state,
256 int init_hw)
257{
258 struct tty_port *port = &state->port;
259 int retval;
260
Peter Hurleyd41861c2016-04-09 17:53:25 -0700261 if (tty_port_initialized(port))
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100262 return 0;
263
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100264 retval = uart_port_startup(tty, state, init_hw);
Rob Herringb3b57642016-08-22 17:39:09 -0500265 if (retval)
266 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return retval;
269}
270
271/*
272 * This routine will shutdown a serial port; interrupts are disabled, and
273 * DTR is dropped if the hangup on close termio flag is on. Calls to
274 * uart_shutdown are serialised by the per-port semaphore.
Peter Hurleyaf224ca2016-04-09 18:56:35 -0700275 *
276 * uport == NULL if uart_port has already been removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Alan Cox19225132010-06-01 22:52:51 +0200278static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Peter Hurley4047b372016-04-09 18:56:33 -0700280 struct uart_port *uport = uart_port_check(state);
Alan Coxbdc04e32009-09-19 13:13:31 -0700281 struct tty_port *port = &state->port;
Johan Hovold18ee37e2021-05-19 11:25:41 +0200282 unsigned long flags;
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900283 char *xmit_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Russell Kingee31b3372005-11-13 15:28:51 +0000285 /*
286 * Set the TTY IO error marker
287 */
Alan Coxf7519282009-01-02 13:49:21 +0000288 if (tty)
289 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b3372005-11-13 15:28:51 +0000290
Peter Hurleyd41861c2016-04-09 17:53:25 -0700291 if (tty_port_initialized(port)) {
292 tty_port_set_initialized(port, 0);
293
Russell Kingee31b3372005-11-13 15:28:51 +0000294 /*
295 * Turn off DTR and RTS early.
296 */
Pali Rohár027b5712021-10-02 15:09:00 +0200297 if (uport && uart_console(uport) && tty) {
Peter Hurleyae84db92014-07-09 09:21:14 -0400298 uport->cons->cflag = tty->termios.c_cflag;
Pali Rohár027b5712021-10-02 15:09:00 +0200299 uport->cons->ispeed = tty->termios.c_ispeed;
300 uport->cons->ospeed = tty->termios.c_ospeed;
301 }
Peter Hurleyae84db92014-07-09 09:21:14 -0400302
Peter Hurley9db276f2016-01-10 20:36:15 -0800303 if (!tty || C_HUPCL(tty))
Rafael Gagoa6845e12017-07-31 10:46:42 +0200304 uart_port_dtr_rts(uport, 0);
Russell Kingee31b3372005-11-13 15:28:51 +0000305
Jiri Slabyb922e192011-11-09 21:33:51 +0100306 uart_port_shutdown(port);
Russell Kingee31b3372005-11-13 15:28:51 +0000307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /*
Doug Andersond208a3b2011-10-19 11:52:01 -0700310 * It's possible for shutdown to be called after suspend if we get
311 * a DCD drop (hangup) at just the right time. Clear suspended bit so
312 * we don't try to resume a port that has been shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Peter Hurley80f02d52016-04-09 17:53:24 -0700314 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900317 * Do not free() the transmit buffer page under the port lock since
318 * this can create various circular locking scenarios. For instance,
319 * console driver may need to allocate/free a debug object, which
320 * can endup in printk() recursion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600322 uart_port_lock(state, flags);
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900323 xmit_buf = state->xmit.buf;
324 state->xmit.buf = NULL;
Tycho Andersena5ba1d92018-07-06 10:24:57 -0600325 uart_port_unlock(uport, flags);
Sergey Senozhatskyd7240212018-12-13 13:58:39 +0900326
327 if (xmit_buf)
328 free_page((unsigned long)xmit_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
332 * uart_update_timeout - update per-port FIFO timeout.
333 * @port: uart_port structure describing the port
334 * @cflag: termios cflag value
335 * @baud: speed of the port
336 *
337 * Set the port FIFO timeout value. The @cflag value should
338 * reflect the actual hardware settings.
339 */
340void
341uart_update_timeout(struct uart_port *port, unsigned int cflag,
342 unsigned int baud)
343{
Jiri Slaby654ee492021-06-10 11:02:44 +0200344 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jiri Slaby654ee492021-06-10 11:02:44 +0200346 size = tty_get_frame_size(cflag) * port->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /*
349 * Figure the timeout to send the above number of bits.
350 * Add .02 seconds of slop
351 */
Jiri Slaby654ee492021-06-10 11:02:44 +0200352 port->timeout = (HZ * size) / baud + HZ/50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355EXPORT_SYMBOL(uart_update_timeout);
356
357/**
358 * uart_get_baud_rate - return baud rate for a particular port
359 * @port: uart_port structure describing the port in question.
360 * @termios: desired termios settings.
361 * @old: old termios (or NULL)
362 * @min: minimum acceptable baud rate
363 * @max: maximum acceptable baud rate
364 *
365 * Decode the termios structure into a numeric baud rate,
366 * taking account of the magic 38400 baud rate (with spd_*
367 * flags), and mapping the %B0 rate to 9600 baud.
368 *
369 * If the new baud rate is invalid, try the old termios setting.
370 * If it's still invalid, we try 9600 baud.
371 *
372 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700373 * we're actually going to be using. Don't do this for the case
374 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
376unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800377uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
378 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Joakim Nordellf10a2232015-06-08 14:56:51 +0200380 unsigned int try;
381 unsigned int baud;
382 unsigned int altbaud;
Alan Coxeb424fd2008-04-28 02:14:07 -0700383 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000384 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Joakim Nordellf10a2232015-06-08 14:56:51 +0200386 switch (flags) {
387 case UPF_SPD_HI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 altbaud = 57600;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200389 break;
390 case UPF_SPD_VHI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 altbaud = 115200;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200392 break;
393 case UPF_SPD_SHI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 altbaud = 230400;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200395 break;
396 case UPF_SPD_WARP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 altbaud = 460800;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200398 break;
399 default:
400 altbaud = 38400;
401 break;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 for (try = 0; try < 2; try++) {
405 baud = tty_termios_baud_rate(termios);
406
407 /*
408 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
409 * Die! Die! Die!
410 */
Peter Hurley547039e2014-10-16 13:46:38 -0400411 if (try == 0 && baud == 38400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 baud = altbaud;
413
414 /*
415 * Special case: B0 rate.
416 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700417 if (baud == 0) {
418 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (baud >= min && baud <= max)
423 return baud;
424
425 /*
426 * Oops, the quotient was zero. Try again with
427 * the old baud rate if possible.
428 */
429 termios->c_cflag &= ~CBAUD;
430 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800431 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700432 if (!hung_up)
433 tty_termios_encode_baud_rate(termios,
434 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 old = NULL;
436 continue;
437 }
438
439 /*
Alan Cox16ae2a82010-01-04 16:26:21 +0000440 * As a last resort, if the range cannot be met then clip to
441 * the nearest chip supported rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Alan Cox16ae2a82010-01-04 16:26:21 +0000443 if (!hung_up) {
444 if (baud <= min)
445 tty_termios_encode_baud_rate(termios,
446 min + 1, min + 1);
447 else
448 tty_termios_encode_baud_rate(termios,
449 max - 1, max - 1);
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Alan Cox16ae2a82010-01-04 16:26:21 +0000452 /* Should never happen */
453 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457EXPORT_SYMBOL(uart_get_baud_rate);
458
459/**
460 * uart_get_divisor - return uart clock divisor
461 * @port: uart_port structure describing the port.
462 * @baud: desired baud rate
463 *
464 * Calculate the uart clock divisor for the port.
465 */
466unsigned int
467uart_get_divisor(struct uart_port *port, unsigned int baud)
468{
469 unsigned int quot;
470
471 /*
472 * Old custom speed handling.
473 */
474 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
475 quot = port->custom_divisor;
476 else
Uwe Kleine-König97d24632011-12-20 11:47:44 +0100477 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 return quot;
480}
481
482EXPORT_SYMBOL(uart_get_divisor);
483
Peter Hurley7c8ab962014-10-16 16:54:20 -0400484/* Caller holds port mutex */
Alan Cox19225132010-06-01 22:52:51 +0200485static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
486 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Peter Hurley4047b372016-04-09 18:56:33 -0700488 struct uart_port *uport = uart_port_check(state);
Alan Cox606d0992006-12-08 02:38:45 -0800489 struct ktermios *termios;
Peter Hurley391f93f2015-01-25 14:44:51 -0500490 int hw_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /*
493 * If we have no tty, termios, or the port does not exist,
494 * then we can't set the parameters for this port.
495 */
Alan Coxadc8d742012-07-14 15:31:47 +0100496 if (!tty || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return;
498
Alan Coxadc8d742012-07-14 15:31:47 +0100499 termios = &tty->termios;
Peter Hurleyc18b55f2014-06-16 09:17:09 -0400500 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
Peter Hurley299245a2014-09-10 15:06:24 -0400503 * Set modem status enables based on termios cflag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Peter Hurley299245a2014-09-10 15:06:24 -0400505 spin_lock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (termios->c_cflag & CRTSCTS)
Peter Hurley299245a2014-09-10 15:06:24 -0400507 uport->status |= UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 else
Peter Hurley299245a2014-09-10 15:06:24 -0400509 uport->status &= ~UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (termios->c_cflag & CLOCAL)
Peter Hurley299245a2014-09-10 15:06:24 -0400512 uport->status &= ~UPSTAT_DCD_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 else
Peter Hurley299245a2014-09-10 15:06:24 -0400514 uport->status |= UPSTAT_DCD_ENABLE;
Peter Hurley391f93f2015-01-25 14:44:51 -0500515
516 /* reset sw-assisted CTS flow control based on (possibly) new mode */
517 hw_stopped = uport->hw_stopped;
518 uport->hw_stopped = uart_softcts_mode(uport) &&
519 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
520 if (uport->hw_stopped) {
521 if (!hw_stopped)
522 uport->ops->stop_tx(uport);
523 } else {
524 if (hw_stopped)
525 __uart_start(tty);
526 }
Peter Hurley299245a2014-09-10 15:06:24 -0400527 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800530static int uart_put_char(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800532 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700533 struct uart_port *port;
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800534 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700536 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800538 circ = &state->xmit;
Peter Hurley9ed19422016-04-09 18:56:34 -0700539 port = uart_port_lock(state, flags);
Samir Virmaniaff9cf52019-01-16 10:28:07 -0800540 if (!circ->buf) {
541 uart_port_unlock(port, flags);
542 return 0;
543 }
544
Peter Hurley9ed19422016-04-09 18:56:34 -0700545 if (port && uart_circ_chars_free(circ) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 circ->buf[circ->head] = c;
547 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700548 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Peter Hurley9ed19422016-04-09 18:56:34 -0700550 uart_port_unlock(port, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void uart_flush_chars(struct tty_struct *tty)
555{
556 uart_start(tty);
557}
558
Alan Cox19225132010-06-01 22:52:51 +0200559static int uart_write(struct tty_struct *tty,
560 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800563 struct uart_port *port;
564 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned long flags;
566 int c, ret = 0;
567
Pavel Machekd5f735e2006-03-07 21:55:20 -0800568 /*
569 * This means you called this function _after_ the port was
570 * closed. No cookie for you.
571 */
Alan Coxf7519282009-01-02 13:49:21 +0000572 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800573 WARN_ON(1);
574 return -EL3HLT;
575 }
576
Peter Hurley9ed19422016-04-09 18:56:34 -0700577 port = uart_port_lock(state, flags);
Samir Virmaniaff9cf52019-01-16 10:28:07 -0800578 circ = &state->xmit;
579 if (!circ->buf) {
580 uart_port_unlock(port, flags);
581 return 0;
582 }
583
Peter Hurley9ed19422016-04-09 18:56:34 -0700584 while (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
586 if (count < c)
587 c = count;
588 if (c <= 0)
589 break;
590 memcpy(circ->buf + circ->head, buf, c);
591 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
592 buf += c;
593 count -= c;
594 ret += c;
595 }
Peter Hurley64dbee32014-10-16 16:54:26 -0400596
597 __uart_start(tty);
Peter Hurley9ed19422016-04-09 18:56:34 -0700598 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return ret;
600}
601
Jiri Slaby03b3b1a2021-05-05 11:19:15 +0200602static unsigned int uart_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700605 struct uart_port *port;
Alan Coxf34d7a52008-04-30 00:54:13 -0700606 unsigned long flags;
Jiri Slaby03b3b1a2021-05-05 11:19:15 +0200607 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Peter Hurley9ed19422016-04-09 18:56:34 -0700609 port = uart_port_lock(state, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700610 ret = uart_circ_chars_free(&state->xmit);
Peter Hurley9ed19422016-04-09 18:56:34 -0700611 uart_port_unlock(port, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200615static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700618 struct uart_port *port;
Alan Coxf34d7a52008-04-30 00:54:13 -0700619 unsigned long flags;
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200620 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Peter Hurley9ed19422016-04-09 18:56:34 -0700622 port = uart_port_lock(state, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700623 ret = uart_circ_chars_pending(&state->xmit);
Peter Hurley9ed19422016-04-09 18:56:34 -0700624 uart_port_unlock(port, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static void uart_flush_buffer(struct tty_struct *tty)
629{
630 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700631 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned long flags;
633
Pavel Machekd5f735e2006-03-07 21:55:20 -0800634 /*
635 * This means you called this function _after_ the port was
636 * closed. No cookie for you.
637 */
Alan Coxf7519282009-01-02 13:49:21 +0000638 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800639 WARN_ON(1);
640 return;
641 }
642
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700643 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Peter Hurley9ed19422016-04-09 18:56:34 -0700645 port = uart_port_lock(state, flags);
646 if (!port)
647 return;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700648 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100649 if (port->ops->flush_buffer)
650 port->ops->flush_buffer(port);
Peter Hurley9ed19422016-04-09 18:56:34 -0700651 uart_port_unlock(port, flags);
Rob Herringd0f4bce2016-10-28 07:07:48 -0500652 tty_port_tty_wakeup(&state->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
656 * This function is used to send a high-priority XON/XOFF character to
657 * the device
658 */
659static void uart_send_xchar(struct tty_struct *tty, char ch)
660{
661 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700662 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned long flags;
664
Peter Hurley9ed19422016-04-09 18:56:34 -0700665 port = uart_port_ref(state);
666 if (!port)
667 return;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (port->ops->send_xchar)
670 port->ops->send_xchar(port, ch);
671 else {
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400672 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 port->x_char = ch;
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400674 if (ch)
Russell Kingb129a8c2005-08-31 10:12:14 +0100675 port->ops->start_tx(port);
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400676 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Peter Hurley9ed19422016-04-09 18:56:34 -0700678 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static void uart_throttle(struct tty_struct *tty)
682{
683 struct uart_state *state = tty->driver_data;
Jeremy Kerrc5f78b12018-03-27 11:48:24 +0800684 upstat_t mask = UPSTAT_SYNC_FIFO;
Peter Hurley9ed19422016-04-09 18:56:34 -0700685 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Peter Hurley9ed19422016-04-09 18:56:34 -0700687 port = uart_port_ref(state);
688 if (!port)
689 return;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (I_IXOFF(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500692 mask |= UPSTAT_AUTOXOFF;
Peter Hurley9db276f2016-01-10 20:36:15 -0800693 if (C_CRTSCTS(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500694 mask |= UPSTAT_AUTORTS;
Russell King9aba8d5b2012-04-17 17:23:14 +0100695
Peter Hurley391f93f2015-01-25 14:44:51 -0500696 if (port->status & mask) {
Russell King9aba8d5b2012-04-17 17:23:14 +0100697 port->ops->throttle(port);
Peter Hurley391f93f2015-01-25 14:44:51 -0500698 mask &= ~port->status;
Russell King9aba8d5b2012-04-17 17:23:14 +0100699 }
700
Peter Hurley391f93f2015-01-25 14:44:51 -0500701 if (mask & UPSTAT_AUTORTS)
Russell King9aba8d5b2012-04-17 17:23:14 +0100702 uart_clear_mctrl(port, TIOCM_RTS);
Peter Hurleyb4749b92016-01-10 20:24:02 -0800703
704 if (mask & UPSTAT_AUTOXOFF)
705 uart_send_xchar(tty, STOP_CHAR(tty));
Peter Hurley9ed19422016-04-09 18:56:34 -0700706
707 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void uart_unthrottle(struct tty_struct *tty)
711{
712 struct uart_state *state = tty->driver_data;
Jeremy Kerrc5f78b12018-03-27 11:48:24 +0800713 upstat_t mask = UPSTAT_SYNC_FIFO;
Peter Hurley9ed19422016-04-09 18:56:34 -0700714 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Peter Hurley9ed19422016-04-09 18:56:34 -0700716 port = uart_port_ref(state);
717 if (!port)
718 return;
719
Russell King9aba8d5b2012-04-17 17:23:14 +0100720 if (I_IXOFF(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500721 mask |= UPSTAT_AUTOXOFF;
Peter Hurley9db276f2016-01-10 20:36:15 -0800722 if (C_CRTSCTS(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500723 mask |= UPSTAT_AUTORTS;
Russell King9aba8d5b2012-04-17 17:23:14 +0100724
Peter Hurley391f93f2015-01-25 14:44:51 -0500725 if (port->status & mask) {
Russell King9aba8d5b2012-04-17 17:23:14 +0100726 port->ops->unthrottle(port);
Peter Hurley391f93f2015-01-25 14:44:51 -0500727 mask &= ~port->status;
Russell King9aba8d5b2012-04-17 17:23:14 +0100728 }
729
Peter Hurley391f93f2015-01-25 14:44:51 -0500730 if (mask & UPSTAT_AUTORTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 uart_set_mctrl(port, TIOCM_RTS);
Peter Hurleyb4749b92016-01-10 20:24:02 -0800732
733 if (mask & UPSTAT_AUTOXOFF)
734 uart_send_xchar(tty, START_CHAR(tty));
Peter Hurley9ed19422016-04-09 18:56:34 -0700735
736 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Peter Hurley4047b372016-04-09 18:56:33 -0700739static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Alan Cox9f109692012-10-29 15:20:25 +0000741 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley4047b372016-04-09 18:56:33 -0700742 struct uart_port *uport;
743 int ret = -ENODEV;
Alan Cox7ba2e762012-09-04 16:34:45 +0100744
Peter Hurley3abe8c72016-01-10 20:23:56 -0800745 /*
746 * Ensure the state we copy is consistent and no hardware changes
747 * occur as we go
748 */
749 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -0700750 uport = uart_port_check(state);
751 if (!uport)
752 goto out;
753
Alan Cox7ba2e762012-09-04 16:34:45 +0100754 retinfo->type = uport->type;
755 retinfo->line = uport->line;
756 retinfo->port = uport->iobase;
757 if (HIGH_BITS_OFFSET)
758 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
759 retinfo->irq = uport->irq;
Andy Shevchenkoa17e74c2017-07-25 20:39:57 +0300760 retinfo->flags = (__force int)uport->flags;
Alan Cox7ba2e762012-09-04 16:34:45 +0100761 retinfo->xmit_fifo_size = uport->fifosize;
762 retinfo->baud_base = uport->uartclk / 16;
763 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
764 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
765 ASYNC_CLOSING_WAIT_NONE :
766 jiffies_to_msecs(port->closing_wait) / 10;
767 retinfo->custom_divisor = uport->custom_divisor;
768 retinfo->hub6 = uport->hub6;
769 retinfo->io_type = uport->iotype;
770 retinfo->iomem_reg_shift = uport->regshift;
771 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
Peter Hurley4047b372016-04-09 18:56:33 -0700772
773 ret = 0;
774out:
Alan Coxa2bceae2009-09-19 13:13:31 -0700775 mutex_unlock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -0700776 return ret;
Alan Cox9f109692012-10-29 15:20:25 +0000777}
778
Al Viro5099d232018-09-11 22:29:55 -0400779static int uart_get_info_user(struct tty_struct *tty,
780 struct serial_struct *ss)
Alan Cox9f109692012-10-29 15:20:25 +0000781{
Al Viro5099d232018-09-11 22:29:55 -0400782 struct uart_state *state = tty->driver_data;
783 struct tty_port *port = &state->port;
Peter Hurley3abe8c72016-01-10 20:23:56 -0800784
Al Viro5099d232018-09-11 22:29:55 -0400785 return uart_get_info(port, ss) < 0 ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Alan Cox7ba2e762012-09-04 16:34:45 +0100788static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
789 struct uart_state *state,
790 struct serial_struct *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Peter Hurley4047b372016-04-09 18:56:33 -0700792 struct uart_port *uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000794 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000796 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int retval = 0;
798
Peter Hurley4047b372016-04-09 18:56:33 -0700799 if (!uport)
800 return -EIO;
801
Alan Cox7ba2e762012-09-04 16:34:45 +0100802 new_port = new_info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (HIGH_BITS_OFFSET)
Alan Cox7ba2e762012-09-04 16:34:45 +0100804 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Alan Cox7ba2e762012-09-04 16:34:45 +0100806 new_info->irq = irq_canonicalize(new_info->irq);
807 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
808 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Jiri Slaby4cb0fbf2011-11-09 21:33:45 +0100809 ASYNC_CLOSING_WAIT_NONE :
Alan Cox7ba2e762012-09-04 16:34:45 +0100810 msecs_to_jiffies(new_info->closing_wait * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Alan Cox46d57a42009-09-19 13:13:29 -0700813 change_irq = !(uport->flags & UPF_FIXED_PORT)
Alan Cox7ba2e762012-09-04 16:34:45 +0100814 && new_info->irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /*
817 * Since changing the 'type' of the port changes its resource
818 * allocations, we should treat type changes the same as
819 * IO port changes.
820 */
Alan Cox46d57a42009-09-19 13:13:29 -0700821 change_port = !(uport->flags & UPF_FIXED_PORT)
822 && (new_port != uport->iobase ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100823 (unsigned long)new_info->iomem_base != uport->mapbase ||
824 new_info->hub6 != uport->hub6 ||
825 new_info->io_type != uport->iotype ||
826 new_info->iomem_reg_shift != uport->regshift ||
827 new_info->type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Alan Cox46d57a42009-09-19 13:13:29 -0700829 old_flags = uport->flags;
Andy Shevchenkoa17e74c2017-07-25 20:39:57 +0300830 new_flags = (__force upf_t)new_info->flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700831 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (!capable(CAP_SYS_ADMIN)) {
834 retval = -EPERM;
835 if (change_irq || change_port ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100836 (new_info->baud_base != uport->uartclk / 16) ||
Alan Cox46d57a42009-09-19 13:13:29 -0700837 (close_delay != port->close_delay) ||
838 (closing_wait != port->closing_wait) ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100839 (new_info->xmit_fifo_size &&
840 new_info->xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000841 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700843 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000844 (new_flags & UPF_USR_MASK));
Alan Cox7ba2e762012-09-04 16:34:45 +0100845 uport->custom_divisor = new_info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto check_and_exit;
847 }
848
Ondrej Mosnacek5e722b22021-05-07 13:57:19 +0200849 if (change_irq || change_port) {
850 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
851 if (retval)
852 goto exit;
853 }
David Howells794edf32019-08-19 17:17:54 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Ask the low level driver to verify the settings.
857 */
Alan Cox46d57a42009-09-19 13:13:29 -0700858 if (uport->ops->verify_port)
Alan Cox7ba2e762012-09-04 16:34:45 +0100859 retval = uport->ops->verify_port(uport, new_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Alan Cox7ba2e762012-09-04 16:34:45 +0100861 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
862 (new_info->baud_base < 9600))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 retval = -EINVAL;
864
865 if (retval)
866 goto exit;
867
868 if (change_port || change_irq) {
869 retval = -EBUSY;
870
871 /*
872 * Make sure that we are the sole user of this port.
873 */
Alan Coxb58d13a2009-09-19 13:13:32 -0700874 if (tty_port_users(port) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto exit;
876
877 /*
878 * We need to shutdown the serial port at the old
879 * port/type/irq combination.
880 */
Alan Cox19225132010-06-01 22:52:51 +0200881 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 if (change_port) {
885 unsigned long old_iobase, old_mapbase;
886 unsigned int old_type, old_iotype, old_hub6, old_shift;
887
Alan Cox46d57a42009-09-19 13:13:29 -0700888 old_iobase = uport->iobase;
889 old_mapbase = uport->mapbase;
890 old_type = uport->type;
891 old_hub6 = uport->hub6;
892 old_iotype = uport->iotype;
893 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /*
896 * Free and release old regions
897 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -0300898 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
Alan Cox46d57a42009-09-19 13:13:29 -0700899 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alan Cox46d57a42009-09-19 13:13:29 -0700901 uport->iobase = new_port;
Alan Cox7ba2e762012-09-04 16:34:45 +0100902 uport->type = new_info->type;
903 uport->hub6 = new_info->hub6;
904 uport->iotype = new_info->io_type;
905 uport->regshift = new_info->iomem_reg_shift;
906 uport->mapbase = (unsigned long)new_info->iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /*
909 * Claim and map the new regions
910 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -0300911 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
Alan Cox46d57a42009-09-19 13:13:29 -0700912 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
914 /* Always success - Jean II */
915 retval = 0;
916 }
917
918 /*
919 * If we fail to request resources for the
920 * new port, try to restore the old settings.
921 */
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200922 if (retval) {
Alan Cox46d57a42009-09-19 13:13:29 -0700923 uport->iobase = old_iobase;
924 uport->type = old_type;
925 uport->hub6 = old_hub6;
926 uport->iotype = old_iotype;
927 uport->regshift = old_shift;
928 uport->mapbase = old_mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200930 if (old_type != PORT_UNKNOWN) {
931 retval = uport->ops->request_port(uport);
932 /*
933 * If we failed to restore the old settings,
934 * we fail like this.
935 */
936 if (retval)
937 uport->type = PORT_UNKNOWN;
938
939 /*
940 * We failed anyway.
941 */
942 retval = -EBUSY;
943 }
944
Alan Coxa46c9992008-02-08 04:18:53 -0800945 /* Added to return the correct error -Ram Gupta */
946 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 }
949
David Gibsonabb4a232007-05-06 14:48:49 -0700950 if (change_irq)
Alan Cox7ba2e762012-09-04 16:34:45 +0100951 uport->irq = new_info->irq;
Alan Cox46d57a42009-09-19 13:13:29 -0700952 if (!(uport->flags & UPF_FIXED_PORT))
Alan Cox7ba2e762012-09-04 16:34:45 +0100953 uport->uartclk = new_info->baud_base * 16;
Alan Cox46d57a42009-09-19 13:13:29 -0700954 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000955 (new_flags & UPF_CHANGE_MASK);
Alan Cox7ba2e762012-09-04 16:34:45 +0100956 uport->custom_divisor = new_info->custom_divisor;
Alan Cox46d57a42009-09-19 13:13:29 -0700957 port->close_delay = close_delay;
958 port->closing_wait = closing_wait;
Alan Cox7ba2e762012-09-04 16:34:45 +0100959 if (new_info->xmit_fifo_size)
960 uport->fifosize = new_info->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 check_and_exit:
963 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700964 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto exit;
Peter Hurleyd41861c2016-04-09 17:53:25 -0700966 if (tty_port_initialized(port)) {
Alan Cox46d57a42009-09-19 13:13:29 -0700967 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
968 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
970 * If they're setting up a custom divisor or speed,
Johan Hovolddc2f7272017-06-06 12:54:33 +0200971 * instead of clearing it, then bitch about it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Alan Cox46d57a42009-09-19 13:13:29 -0700973 if (uport->flags & UPF_SPD_MASK) {
Johan Hovolddc2f7272017-06-06 12:54:33 +0200974 dev_notice_ratelimited(uport->dev,
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +0530975 "%s sets custom speed on %s. This is deprecated.\n",
976 current->comm,
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200977 tty_name(port->tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Alan Cox19225132010-06-01 22:52:51 +0200979 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Rob Herringb3b57642016-08-22 17:39:09 -0500981 } else {
Alan Cox19225132010-06-01 22:52:51 +0200982 retval = uart_startup(tty, state, 1);
Sebastian Andrzej Siewior44117a12018-01-11 18:57:26 +0100983 if (retval == 0)
984 tty_port_set_initialized(port, true);
Rob Herringb3b57642016-08-22 17:39:09 -0500985 if (retval > 0)
986 retval = 0;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 exit:
Alan Cox7ba2e762012-09-04 16:34:45 +0100989 return retval;
990}
991
Al Viro5099d232018-09-11 22:29:55 -0400992static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
Alan Cox7ba2e762012-09-04 16:34:45 +0100993{
Al Viro5099d232018-09-11 22:29:55 -0400994 struct uart_state *state = tty->driver_data;
Alan Cox7ba2e762012-09-04 16:34:45 +0100995 struct tty_port *port = &state->port;
996 int retval;
997
Al Viro5099d232018-09-11 22:29:55 -0400998 down_write(&tty->termios_rwsem);
Alan Cox7ba2e762012-09-04 16:34:45 +0100999 /*
1000 * This semaphore protects port->count. It is also
1001 * very useful to prevent opens. Also, take the
1002 * port configuration semaphore to make sure that a
1003 * module insertion/removal doesn't change anything
1004 * under us.
1005 */
1006 mutex_lock(&port->mutex);
Al Viro5099d232018-09-11 22:29:55 -04001007 retval = uart_set_info(tty, port, state, ss);
Alan Coxa2bceae2009-09-19 13:13:31 -07001008 mutex_unlock(&port->mutex);
Al Viro5099d232018-09-11 22:29:55 -04001009 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return retval;
1011}
1012
Alan Cox19225132010-06-01 22:52:51 +02001013/**
1014 * uart_get_lsr_info - get line status register info
1015 * @tty: tty associated with the UART
1016 * @state: UART being queried
1017 * @value: returned modem value
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
Alan Cox19225132010-06-01 22:52:51 +02001019static int uart_get_lsr_info(struct tty_struct *tty,
1020 struct uart_state *state, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Peter Hurley4047b372016-04-09 18:56:33 -07001022 struct uart_port *uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 unsigned int result;
1024
Alan Cox46d57a42009-09-19 13:13:29 -07001025 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /*
1028 * If we're about to load something into the transmit
1029 * register, we'll pretend the transmitter isn't empty to
1030 * avoid a race condition (depending on when the transmit
1031 * interrupt happens).
1032 */
Alan Cox46d57a42009-09-19 13:13:29 -07001033 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -07001034 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Peter Hurleyd01f4d12014-09-10 15:06:26 -04001035 !uart_tx_stopped(uport)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -08001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return put_user(result, value);
1039}
1040
Alan Cox60b33c12011-02-14 16:26:14 +00001041static int uart_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001044 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001045 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int result = -EIO;
1047
Alan Coxa2bceae2009-09-19 13:13:31 -07001048 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001049 uport = uart_port_check(state);
1050 if (!uport)
1051 goto out;
1052
Peter Hurley18900ca2016-04-09 17:06:48 -07001053 if (!tty_io_error(tty)) {
Alan Cox46d57a42009-09-19 13:13:29 -07001054 result = uport->mctrl;
Alan Cox46d57a42009-09-19 13:13:29 -07001055 spin_lock_irq(&uport->lock);
1056 result |= uport->ops->get_mctrl(uport);
1057 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Peter Hurley4047b372016-04-09 18:56:33 -07001059out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001060 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return result;
1062}
1063
1064static int
Alan Cox20b9d172011-02-14 16:26:50 +00001065uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001068 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001069 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int ret = -EIO;
1071
Alan Coxa2bceae2009-09-19 13:13:31 -07001072 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001073 uport = uart_port_check(state);
1074 if (!uport)
1075 goto out;
1076
Peter Hurley18900ca2016-04-09 17:06:48 -07001077 if (!tty_io_error(tty)) {
Alan Cox46d57a42009-09-19 13:13:29 -07001078 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = 0;
1080 }
Peter Hurley4047b372016-04-09 18:56:33 -07001081out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001082 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return ret;
1084}
1085
Alan Cox9e98966c2008-07-22 11:18:03 +01001086static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001089 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001090 struct uart_port *uport;
1091 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alan Coxa2bceae2009-09-19 13:13:31 -07001093 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001094 uport = uart_port_check(state);
1095 if (!uport)
1096 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jiangfeng Xiao7d731702019-11-20 23:18:53 +08001098 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
Alan Cox46d57a42009-09-19 13:13:29 -07001099 uport->ops->break_ctl(uport, break_state);
Peter Hurley4047b372016-04-09 18:56:33 -07001100 ret = 0;
1101out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001102 mutex_unlock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Tamseel Shams4ed71ad2020-07-16 17:24:38 +05301106static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Alan Coxa2bceae2009-09-19 13:13:31 -07001108 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001109 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int flags, ret;
1111
1112 if (!capable(CAP_SYS_ADMIN))
1113 return -EPERM;
1114
1115 /*
1116 * Take the per-port semaphore. This prevents count from
1117 * changing, and hence any extra opens of the port while
1118 * we're auto-configuring.
1119 */
Alan Coxa2bceae2009-09-19 13:13:31 -07001120 if (mutex_lock_interruptible(&port->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -ERESTARTSYS;
1122
Peter Hurley4047b372016-04-09 18:56:33 -07001123 uport = uart_port_check(state);
1124 if (!uport) {
1125 ret = -EIO;
1126 goto out;
1127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ret = -EBUSY;
Alan Coxb58d13a2009-09-19 13:13:32 -07001130 if (tty_port_users(port) == 1) {
Alan Cox19225132010-06-01 22:52:51 +02001131 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /*
1134 * If we already have a port type configured,
1135 * we must release its resources.
1136 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -03001137 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
Alan Cox46d57a42009-09-19 13:13:29 -07001138 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -07001141 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 flags |= UART_CONFIG_IRQ;
1143
1144 /*
1145 * This will claim the ports resources if
1146 * a port is found.
1147 */
Alan Cox46d57a42009-09-19 13:13:29 -07001148 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Alan Cox19225132010-06-01 22:52:51 +02001150 ret = uart_startup(tty, state, 1);
Sebastian Andrzej Siewior71456902018-02-03 12:27:23 +01001151 if (ret == 0)
1152 tty_port_set_initialized(port, true);
Rob Herringb3b57642016-08-22 17:39:09 -05001153 if (ret > 0)
1154 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Peter Hurley4047b372016-04-09 18:56:33 -07001156out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001157 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return ret;
1159}
1160
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001161static void uart_enable_ms(struct uart_port *uport)
1162{
1163 /*
1164 * Force modem status interrupts on
1165 */
1166 if (uport->ops->enable_ms)
1167 uport->ops->enable_ms(uport);
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1172 * - mask passed in arg for lines of interest
1173 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1174 * Caller should use TIOCGICOUNT to see which one it was
Alan Coxbdc04e32009-09-19 13:13:31 -07001175 *
1176 * FIXME: This wants extracting into a common all driver implementation
1177 * of TIOCMWAIT using tty_port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Peter Hurley9ed19422016-04-09 18:56:34 -07001179static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Peter Hurley9ed19422016-04-09 18:56:34 -07001181 struct uart_port *uport;
Alan Coxbdc04e32009-09-19 13:13:31 -07001182 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 DECLARE_WAITQUEUE(wait, current);
1184 struct uart_icount cprev, cnow;
1185 int ret;
1186
1187 /*
1188 * note the counters on entry
1189 */
Peter Hurley9ed19422016-04-09 18:56:34 -07001190 uport = uart_port_ref(state);
1191 if (!uport)
1192 return -EIO;
Alan Cox46d57a42009-09-19 13:13:29 -07001193 spin_lock_irq(&uport->lock);
1194 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001195 uart_enable_ms(uport);
Alan Cox46d57a42009-09-19 13:13:29 -07001196 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Alan Coxbdc04e32009-09-19 13:13:31 -07001198 add_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001200 spin_lock_irq(&uport->lock);
1201 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1202 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 set_current_state(TASK_INTERRUPTIBLE);
1205
1206 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1207 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1208 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1209 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001210 ret = 0;
1211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 schedule();
1215
1216 /* see if a signal did it */
1217 if (signal_pending(current)) {
1218 ret = -ERESTARTSYS;
1219 break;
1220 }
1221
1222 cprev = cnow;
1223 }
Fabian Frederick97f9f702015-02-20 19:12:57 +01001224 __set_current_state(TASK_RUNNING);
Alan Coxbdc04e32009-09-19 13:13:31 -07001225 remove_wait_queue(&port->delta_msr_wait, &wait);
Peter Hurley9ed19422016-04-09 18:56:34 -07001226 uart_port_deref(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 return ret;
1229}
1230
1231/*
1232 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1233 * Return: write counters to the user passed counter struct
1234 * NB: both 1->0 and 0->1 transitions are counted except for
1235 * RI where only 0->1 is counted.
1236 */
Alan Coxd281da72010-09-16 18:21:24 +01001237static int uart_get_icount(struct tty_struct *tty,
1238 struct serial_icounter_struct *icount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Alan Coxd281da72010-09-16 18:21:24 +01001240 struct uart_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct uart_icount cnow;
Peter Hurley9ed19422016-04-09 18:56:34 -07001242 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Peter Hurley9ed19422016-04-09 18:56:34 -07001244 uport = uart_port_ref(state);
1245 if (!uport)
1246 return -EIO;
Alan Cox46d57a42009-09-19 13:13:29 -07001247 spin_lock_irq(&uport->lock);
1248 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1249 spin_unlock_irq(&uport->lock);
Peter Hurley9ed19422016-04-09 18:56:34 -07001250 uart_port_deref(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alan Coxd281da72010-09-16 18:21:24 +01001252 icount->cts = cnow.cts;
1253 icount->dsr = cnow.dsr;
1254 icount->rng = cnow.rng;
1255 icount->dcd = cnow.dcd;
1256 icount->rx = cnow.rx;
1257 icount->tx = cnow.tx;
1258 icount->frame = cnow.frame;
1259 icount->overrun = cnow.overrun;
1260 icount->parity = cnow.parity;
1261 icount->brk = cnow.brk;
1262 icount->buf_overrun = cnow.buf_overrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Alan Coxd281da72010-09-16 18:21:24 +01001264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001267static int uart_get_rs485_config(struct uart_port *port,
1268 struct serial_rs485 __user *rs485)
1269{
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001270 unsigned long flags;
1271 struct serial_rs485 aux;
1272
1273 spin_lock_irqsave(&port->lock, flags);
1274 aux = port->rs485;
1275 spin_unlock_irqrestore(&port->lock, flags);
1276
1277 if (copy_to_user(rs485, &aux, sizeof(aux)))
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001278 return -EFAULT;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001279
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001280 return 0;
1281}
1282
1283static int uart_set_rs485_config(struct uart_port *port,
1284 struct serial_rs485 __user *rs485_user)
1285{
1286 struct serial_rs485 rs485;
1287 int ret;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001288 unsigned long flags;
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001289
1290 if (!port->rs485_config)
Johan Hovold79c59662021-04-07 11:52:08 +02001291 return -ENOTTY;
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001292
1293 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1294 return -EFAULT;
1295
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001296 spin_lock_irqsave(&port->lock, flags);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001297 ret = port->rs485_config(port, &rs485);
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001298 spin_unlock_irqrestore(&port->lock, flags);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001299 if (ret)
1300 return ret;
1301
1302 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1303 return -EFAULT;
1304
1305 return 0;
1306}
1307
Nicolas Ferread8c0ea2018-09-26 14:58:47 +02001308static int uart_get_iso7816_config(struct uart_port *port,
1309 struct serial_iso7816 __user *iso7816)
1310{
1311 unsigned long flags;
1312 struct serial_iso7816 aux;
1313
1314 if (!port->iso7816_config)
Johan Hovold79c59662021-04-07 11:52:08 +02001315 return -ENOTTY;
Nicolas Ferread8c0ea2018-09-26 14:58:47 +02001316
1317 spin_lock_irqsave(&port->lock, flags);
1318 aux = port->iso7816;
1319 spin_unlock_irqrestore(&port->lock, flags);
1320
1321 if (copy_to_user(iso7816, &aux, sizeof(aux)))
1322 return -EFAULT;
1323
1324 return 0;
1325}
1326
1327static int uart_set_iso7816_config(struct uart_port *port,
1328 struct serial_iso7816 __user *iso7816_user)
1329{
1330 struct serial_iso7816 iso7816;
1331 int i, ret;
1332 unsigned long flags;
1333
1334 if (!port->iso7816_config)
Johan Hovold79c59662021-04-07 11:52:08 +02001335 return -ENOTTY;
Nicolas Ferread8c0ea2018-09-26 14:58:47 +02001336
1337 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1338 return -EFAULT;
1339
1340 /*
1341 * There are 5 words reserved for future use. Check that userspace
1342 * doesn't put stuff in there to prevent breakages in the future.
1343 */
1344 for (i = 0; i < 5; i++)
1345 if (iso7816.reserved[i])
1346 return -EINVAL;
1347
1348 spin_lock_irqsave(&port->lock, flags);
1349 ret = port->iso7816_config(port, &iso7816);
1350 spin_unlock_irqrestore(&port->lock, flags);
1351 if (ret)
1352 return ret;
1353
1354 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1355 return -EFAULT;
1356
1357 return 0;
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/*
Alan Coxe5238442008-04-30 00:53:28 -07001361 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363static int
Peter Hurley4047b372016-04-09 18:56:33 -07001364uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001367 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001368 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 void __user *uarg = (void __user *)arg;
1370 int ret = -ENOIOCTLCMD;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * These ioctls don't rely on the hardware to be present.
1375 */
1376 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 case TIOCSERCONFIG:
Peter Hurley7c8ab962014-10-16 16:54:20 -04001378 down_write(&tty->termios_rwsem);
Alan Cox19225132010-06-01 22:52:51 +02001379 ret = uart_do_autoconfig(tty, state);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001380 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
1384 if (ret != -ENOIOCTLCMD)
1385 goto out;
1386
Peter Hurley18900ca2016-04-09 17:06:48 -07001387 if (tty_io_error(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ret = -EIO;
1389 goto out;
1390 }
1391
1392 /*
1393 * The following should only be used when hardware is present.
1394 */
1395 switch (cmd) {
1396 case TIOCMIWAIT:
1397 ret = uart_wait_modem_status(state, arg);
1398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
1401 if (ret != -ENOIOCTLCMD)
1402 goto out;
1403
Alan Coxa2bceae2009-09-19 13:13:31 -07001404 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001405 uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Peter Hurley4047b372016-04-09 18:56:33 -07001407 if (!uport || tty_io_error(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ret = -EIO;
1409 goto out_up;
1410 }
1411
1412 /*
1413 * All these rely on hardware being present and need to be
1414 * protected against the tty being hung up.
1415 */
Ricardo Ribalda Delgadoa9c20a92014-11-06 09:22:59 +01001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 switch (cmd) {
Ricardo Ribalda Delgadoa9c20a92014-11-06 09:22:59 +01001418 case TIOCSERGETLSR: /* Get line status register */
1419 ret = uart_get_lsr_info(tty, state, uarg);
1420 break;
1421
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001422 case TIOCGRS485:
Peter Hurley4047b372016-04-09 18:56:33 -07001423 ret = uart_get_rs485_config(uport, uarg);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001424 break;
1425
1426 case TIOCSRS485:
Peter Hurley4047b372016-04-09 18:56:33 -07001427 ret = uart_set_rs485_config(uport, uarg);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001428 break;
Nicolas Ferread8c0ea2018-09-26 14:58:47 +02001429
1430 case TIOCSISO7816:
1431 ret = uart_set_iso7816_config(state->uart_port, uarg);
1432 break;
1433
1434 case TIOCGISO7816:
1435 ret = uart_get_iso7816_config(state->uart_port, uarg);
1436 break;
Peter Hurley4047b372016-04-09 18:56:33 -07001437 default:
Alan Cox46d57a42009-09-19 13:13:29 -07001438 if (uport->ops->ioctl)
1439 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break;
1441 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001442out_up:
Alan Coxa2bceae2009-09-19 13:13:31 -07001443 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001444out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return ret;
1446}
1447
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001448static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001449{
1450 struct uart_state *state = tty->driver_data;
Peter Hurley4047b372016-04-09 18:56:33 -07001451 struct uart_port *uport;
Alexey Kardashevskiy2f70e492020-12-03 16:58:34 +11001452 struct tty_port *port = &state->port;
1453
1454 if (!tty_port_initialized(port))
1455 return;
Alan Cox64e91592008-06-03 15:18:54 +01001456
Peter Hurley4047b372016-04-09 18:56:33 -07001457 mutex_lock(&state->port.mutex);
1458 uport = uart_port_check(state);
1459 if (uport && uport->ops->set_ldisc)
Peter Hurley732a84a2014-11-05 13:11:43 -05001460 uport->ops->set_ldisc(uport, &tty->termios);
Peter Hurley4047b372016-04-09 18:56:33 -07001461 mutex_unlock(&state->port.mutex);
Alan Cox64e91592008-06-03 15:18:54 +01001462}
1463
Alan Coxa46c9992008-02-08 04:18:53 -08001464static void uart_set_termios(struct tty_struct *tty,
1465 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct uart_state *state = tty->driver_data;
Peter Hurley4047b372016-04-09 18:56:33 -07001468 struct uart_port *uport;
Alan Coxadc8d742012-07-14 15:31:47 +01001469 unsigned int cflag = tty->termios.c_cflag;
Russell King2cbacaf2012-04-17 16:34:13 +01001470 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1471 bool sw_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Peter Hurley4047b372016-04-09 18:56:33 -07001473 mutex_lock(&state->port.mutex);
1474 uport = uart_port_check(state);
1475 if (!uport)
1476 goto out;
1477
Russell King2cbacaf2012-04-17 16:34:13 +01001478 /*
1479 * Drivers doing software flow control also need to know
1480 * about changes to these input settings.
1481 */
1482 if (uport->flags & UPF_SOFT_FLOW) {
1483 iflag_mask |= IXANY|IXON|IXOFF;
1484 sw_changed =
1485 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1486 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 /*
1490 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001491 * flags in the low level driver. We can ignore the Bfoo
1492 * bits in c_cflag; c_[io]speed will always be set
1493 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if ((cflag ^ old_termios->c_cflag) == 0 &&
Alan Coxadc8d742012-07-14 15:31:47 +01001496 tty->termios.c_ospeed == old_termios->c_ospeed &&
1497 tty->termios.c_ispeed == old_termios->c_ispeed &&
Russell King2cbacaf2012-04-17 16:34:13 +01001498 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1499 !sw_changed) {
Peter Hurley4047b372016-04-09 18:56:33 -07001500 goto out;
Alan Coxe5238442008-04-30 00:53:28 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Alan Cox19225132010-06-01 22:52:51 +02001503 uart_change_speed(tty, state, old_termios);
Hariprasad Kelamc7a6b9e2019-03-28 08:47:33 +05301504 /* reload cflag from termios; port driver may have overridden flags */
Peter Hurleyc18b55f2014-06-16 09:17:09 -04001505 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* Handle transition to B0 status */
1508 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Russell Kingdec94e72012-09-24 11:13:15 +01001509 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Handle transition away from B0 status */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001511 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned int mask = TIOCM_DTR;
Tamseel Shams4ed71ad2020-07-16 17:24:38 +05301513
Peter Hurley97ef38b2016-04-09 17:11:36 -07001514 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 mask |= TIOCM_RTS;
Russell Kingdec94e72012-09-24 11:13:15 +01001516 uart_set_mctrl(uport, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Peter Hurley4047b372016-04-09 18:56:33 -07001518out:
1519 mutex_unlock(&state->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001523 * Calls to uart_close() are serialised via the tty_lock in
1524 * drivers/tty/tty_io.c:tty_release()
1525 * drivers/tty/tty_io.c:do_tty_hangup()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
1527static void uart_close(struct tty_struct *tty, struct file *filp)
1528{
1529 struct uart_state *state = tty->driver_data;
Alan Coxa46c9992008-02-08 04:18:53 -08001530
Peter Hurley91b32f52014-10-16 16:54:27 -04001531 if (!state) {
1532 struct uart_driver *drv = tty->driver->driver_state;
Colin Ian King93563352017-11-01 10:49:23 +00001533 struct tty_port *port;
Peter Hurley91b32f52014-10-16 16:54:27 -04001534
1535 state = drv->state + tty->index;
1536 port = &state->port;
1537 spin_lock_irq(&port->lock);
1538 --port->count;
1539 spin_unlock_irq(&port->lock);
Linus Torvaldseea7e172009-10-12 19:13:54 +02001540 return;
Peter Hurley91b32f52014-10-16 16:54:27 -04001541 }
Linus Torvaldseea7e172009-10-12 19:13:54 +02001542
Peter Hurley39b3d892016-01-10 20:23:57 -08001543 pr_debug("uart_close(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Rob Herring761ed4a2016-08-22 17:39:10 -05001545 tty_port_close(tty->port, tty, filp);
1546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Rob Herring761ed4a2016-08-22 17:39:10 -05001548static void uart_tty_port_shutdown(struct tty_port *port)
1549{
1550 struct uart_state *state = container_of(port, struct uart_state, port);
1551 struct uart_port *uport = uart_port_check(state);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /*
1554 * At this point, we stop accepting input. To do this, we
1555 * disable the receive line status interrupts.
1556 */
Andy Shevchenkoa5a2b132016-09-13 00:23:42 +03001557 if (WARN(!uport, "detached port still initialized!\n"))
1558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Andy Shevchenkoa5a2b132016-09-13 00:23:42 +03001560 spin_lock_irq(&uport->lock);
Rob Herring761ed4a2016-08-22 17:39:10 -05001561 uport->ops->stop_rx(uport);
Rob Herring761ed4a2016-08-22 17:39:10 -05001562 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Rob Herring761ed4a2016-08-22 17:39:10 -05001564 uart_port_shutdown(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /*
Rob Herring761ed4a2016-08-22 17:39:10 -05001567 * It's possible for shutdown to be called after suspend if we get
1568 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1569 * we don't try to resume a port that has been shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
Rob Herring761ed4a2016-08-22 17:39:10 -05001571 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Rob Herring761ed4a2016-08-22 17:39:10 -05001573 uart_change_pm(state, UART_PM_STATE_OFF);
Peter Hurley2e758912014-10-16 16:54:19 -04001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Jiri Slaby1f33a512011-07-14 14:35:10 +02001577static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Jiri Slaby1f33a512011-07-14 14:35:10 +02001579 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -07001580 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 unsigned long char_time, expire;
1582
Peter Hurley9ed19422016-04-09 18:56:34 -07001583 port = uart_port_ref(state);
Andy Shevchenkoef510be2016-11-24 14:18:55 +02001584 if (!port)
1585 return;
1586
1587 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
Peter Hurley9ed19422016-04-09 18:56:34 -07001588 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return;
Peter Hurley9ed19422016-04-09 18:56:34 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * Set the check interval to be 1/5 of the estimated time to
1594 * send a single character, and make it at least 1. The check
1595 * interval should also be less than the timeout.
1596 *
1597 * Note: we have to use pretty tight timings here to satisfy
1598 * the NIST-PCTS.
1599 */
1600 char_time = (port->timeout - HZ/50) / port->fifosize;
1601 char_time = char_time / 5;
1602 if (char_time == 0)
1603 char_time = 1;
1604 if (timeout && timeout < char_time)
1605 char_time = timeout;
1606
1607 /*
1608 * If the transmitter hasn't cleared in twice the approximate
1609 * amount of time to send the entire FIFO, it probably won't
1610 * ever clear. This assumes the UART isn't doing flow
1611 * control, which is currently the case. Hence, if it ever
1612 * takes longer than port->timeout, this is probably due to a
1613 * UART bug of some kind. So, we clamp the timeout parameter at
1614 * 2*port->timeout.
1615 */
1616 if (timeout == 0 || timeout > 2 * port->timeout)
1617 timeout = 2 * port->timeout;
1618
1619 expire = jiffies + timeout;
1620
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001621 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001622 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /*
1625 * Check whether the transmitter is empty every 'char_time'.
1626 * 'timeout' / 'expire' give us the maximum amount of time
1627 * we wait.
1628 */
1629 while (!port->ops->tx_empty(port)) {
1630 msleep_interruptible(jiffies_to_msecs(char_time));
1631 if (signal_pending(current))
1632 break;
1633 if (time_after(jiffies, expire))
1634 break;
1635 }
Peter Hurley9ed19422016-04-09 18:56:34 -07001636 uart_port_deref(port);
Arnd Bergmann20365212010-06-01 22:53:07 +02001637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001640 * Calls to uart_hangup() are serialised by the tty_lock in
1641 * drivers/tty/tty_io.c:do_tty_hangup()
1642 * This runs from a workqueue and can sleep for a _short_ time only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
1644static void uart_hangup(struct tty_struct *tty)
1645{
1646 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001647 struct tty_port *port = &state->port;
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001648 struct uart_port *uport;
Alan Cox61cd8a22010-06-01 22:52:57 +02001649 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Peter Hurley39b3d892016-01-10 20:23:57 -08001651 pr_debug("uart_hangup(%d)\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Alan Coxa2bceae2009-09-19 13:13:31 -07001653 mutex_lock(&port->mutex);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001654 uport = uart_port_check(state);
1655 WARN(!uport, "hangup of detached port!\n");
1656
Peter Hurley807c8d812016-04-09 17:53:22 -07001657 if (tty_port_active(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 uart_flush_buffer(tty);
Alan Cox19225132010-06-01 22:52:51 +02001659 uart_shutdown(tty, state);
Alan Cox61cd8a22010-06-01 22:52:57 +02001660 spin_lock_irqsave(&port->lock, flags);
Alan Cox91312cd2009-09-19 13:13:29 -07001661 port->count = 0;
Alan Cox61cd8a22010-06-01 22:52:57 +02001662 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -07001663 tty_port_set_active(port, 0);
Alan Cox7b014782009-09-19 13:13:33 -07001664 tty_port_tty_set(port, NULL);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001665 if (uport && !uart_console(uport))
Geert Uytterhoevenbf903c02014-03-27 11:40:39 +01001666 uart_change_pm(state, UART_PM_STATE_OFF);
Alan Cox46d57a42009-09-19 13:13:29 -07001667 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -07001668 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001670 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001673/* uport == NULL if uart_port has already been removed */
Jiri Slaby0b1db832011-11-09 21:33:50 +01001674static void uart_port_shutdown(struct tty_port *port)
1675{
Jiri Slabyb922e192011-11-09 21:33:51 +01001676 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley4047b372016-04-09 18:56:33 -07001677 struct uart_port *uport = uart_port_check(state);
Jiri Slabyb922e192011-11-09 21:33:51 +01001678
1679 /*
1680 * clear delta_msr_wait queue to avoid mem leaks: we may free
1681 * the irq here so the queue might never be woken up. Note
1682 * that we won't end up waiting on delta_msr_wait again since
1683 * any outstanding file descriptors should be pointing at
1684 * hung_up_tty_fops now.
1685 */
1686 wake_up_interruptible(&port->delta_msr_wait);
1687
1688 /*
1689 * Free the IRQ and disable the port.
1690 */
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001691 if (uport)
1692 uport->ops->shutdown(uport);
Jiri Slabyb922e192011-11-09 21:33:51 +01001693
1694 /*
1695 * Ensure that the IRQ handler isn't running on another CPU.
1696 */
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001697 if (uport)
1698 synchronize_irq(uport->irq);
Jiri Slaby0b1db832011-11-09 21:33:50 +01001699}
1700
Alan Coxde0c8cb2010-06-01 22:52:58 +02001701static int uart_carrier_raised(struct tty_port *port)
1702{
1703 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley9ed19422016-04-09 18:56:34 -07001704 struct uart_port *uport;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001705 int mctrl;
Peter Hurley9ed19422016-04-09 18:56:34 -07001706
1707 uport = uart_port_ref(state);
1708 /*
1709 * Should never observe uport == NULL since checks for hangup should
1710 * abort the tty_port_block_til_ready() loop before checking for carrier
1711 * raised -- but report carrier raised if it does anyway so open will
1712 * continue and not sleep
1713 */
1714 if (WARN_ON(!uport))
1715 return 1;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001716 spin_lock_irq(&uport->lock);
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001717 uart_enable_ms(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001718 mctrl = uport->ops->get_mctrl(uport);
1719 spin_unlock_irq(&uport->lock);
Peter Hurley9ed19422016-04-09 18:56:34 -07001720 uart_port_deref(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001721 if (mctrl & TIOCM_CAR)
1722 return 1;
1723 return 0;
1724}
1725
Rafael Gagoa6845e12017-07-31 10:46:42 +02001726static void uart_dtr_rts(struct tty_port *port, int raise)
Alan Coxde0c8cb2010-06-01 22:52:58 +02001727{
1728 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley9ed19422016-04-09 18:56:34 -07001729 struct uart_port *uport;
1730
1731 uport = uart_port_ref(state);
1732 if (!uport)
1733 return;
Rafael Gagoa6845e12017-07-31 10:46:42 +02001734 uart_port_dtr_rts(uport, raise);
Peter Hurley9ed19422016-04-09 18:56:34 -07001735 uart_port_deref(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001736}
1737
Jiri Slaby4cdd17b2019-04-17 10:58:53 +02001738static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1739{
1740 struct uart_driver *drv = driver->driver_state;
1741 struct uart_state *state = drv->state + tty->index;
1742
1743 tty->driver_data = state;
1744
1745 return tty_standard_install(driver, tty);
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001749 * Calls to uart_open are serialised by the tty_lock in
1750 * drivers/tty/tty_io.c:tty_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 * Note that if this fails, then uart_close() _will_ be called.
1752 *
1753 * In time, we want to scrap the "opening nonpresent ports"
1754 * behaviour and implement an alternative way for setserial
1755 * to set base addresses/ports/types. This will allow us to
1756 * get rid of a certain amount of extra tests.
1757 */
1758static int uart_open(struct tty_struct *tty, struct file *filp)
1759{
Jiri Slaby4cdd17b2019-04-17 10:58:53 +02001760 struct uart_state *state = tty->driver_data;
1761 int retval;
Rob Herringb3b57642016-08-22 17:39:09 -05001762
1763 retval = tty_port_open(&state->port, tty, filp);
1764 if (retval > 0)
1765 retval = 0;
1766
1767 return retval;
1768}
1769
1770static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1771{
1772 struct uart_state *state = container_of(port, struct uart_state, port);
1773 struct uart_port *uport;
Serge Semin13b18d32019-05-08 13:44:41 +03001774 int ret;
Rob Herringb3b57642016-08-22 17:39:09 -05001775
1776 uport = uart_port_check(state);
1777 if (!uport || uport->flags & UPF_DEAD)
1778 return -ENXIO;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * Start up the serial port.
1782 */
Serge Semin13b18d32019-05-08 13:44:41 +03001783 ret = uart_startup(tty, state, 0);
1784 if (ret > 0)
1785 tty_port_set_active(port, 1);
1786
1787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
1790static const char *uart_type(struct uart_port *port)
1791{
1792 const char *str = NULL;
1793
1794 if (port->ops->type)
1795 str = port->ops->type(port);
1796
1797 if (!str)
1798 str = "unknown";
1799
1800 return str;
1801}
1802
1803#ifdef CONFIG_PROC_FS
1804
Alexey Dobriyand196a942009-03-31 15:19:21 -07001805static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07001808 struct tty_port *port = &state->port;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001809 enum uart_pm_state pm_state;
Peter Hurley4047b372016-04-09 18:56:33 -07001810 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 char stat_buf[32];
1812 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001813 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Peter Hurley4047b372016-04-09 18:56:33 -07001815 mutex_lock(&port->mutex);
1816 uport = uart_port_check(state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001817 if (!uport)
Peter Hurley4047b372016-04-09 18:56:33 -07001818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Alan Coxa2bceae2009-09-19 13:13:31 -07001820 mmio = uport->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001821 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001822 uport->line, uart_type(uport),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001823 mmio ? "mmio:0x" : "port:",
Alan Coxa2bceae2009-09-19 13:13:31 -07001824 mmio ? (unsigned long long)uport->mapbase
1825 : (unsigned long long)uport->iobase,
1826 uport->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Alan Coxa2bceae2009-09-19 13:13:31 -07001828 if (uport->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001829 seq_putc(m, '\n');
Peter Hurley4047b372016-04-09 18:56:33 -07001830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
Alan Coxa46c9992008-02-08 04:18:53 -08001833 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001834 pm_state = state->pm_state;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001835 if (pm_state != UART_PM_STATE_ON)
1836 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxa2bceae2009-09-19 13:13:31 -07001837 spin_lock_irq(&uport->lock);
1838 status = uport->ops->get_mctrl(uport);
1839 spin_unlock_irq(&uport->lock);
Linus Walleij6f538fe2012-12-07 11:36:08 +01001840 if (pm_state != UART_PM_STATE_ON)
George G. Davis3689a0e2007-02-14 00:33:06 -08001841 uart_change_pm(state, pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Alexey Dobriyand196a942009-03-31 15:19:21 -07001843 seq_printf(m, " tx:%d rx:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001844 uport->icount.tx, uport->icount.rx);
1845 if (uport->icount.frame)
Peter Hurley968af292016-01-10 20:24:01 -08001846 seq_printf(m, " fe:%d", uport->icount.frame);
Alan Coxa2bceae2009-09-19 13:13:31 -07001847 if (uport->icount.parity)
Peter Hurley968af292016-01-10 20:24:01 -08001848 seq_printf(m, " pe:%d", uport->icount.parity);
Alan Coxa2bceae2009-09-19 13:13:31 -07001849 if (uport->icount.brk)
Peter Hurley968af292016-01-10 20:24:01 -08001850 seq_printf(m, " brk:%d", uport->icount.brk);
Alan Coxa2bceae2009-09-19 13:13:31 -07001851 if (uport->icount.overrun)
Peter Hurley968af292016-01-10 20:24:01 -08001852 seq_printf(m, " oe:%d", uport->icount.overrun);
Jeremy Kerr4f794092018-03-21 10:52:38 +08001853 if (uport->icount.buf_overrun)
1854 seq_printf(m, " bo:%d", uport->icount.buf_overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001855
1856#define INFOBIT(bit, str) \
Alan Coxa2bceae2009-09-19 13:13:31 -07001857 if (uport->mctrl & (bit)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 strncat(stat_buf, (str), sizeof(stat_buf) - \
1859 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001860#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (status & (bit)) \
1862 strncat(stat_buf, (str), sizeof(stat_buf) - \
1863 strlen(stat_buf) - 2)
1864
1865 stat_buf[0] = '\0';
1866 stat_buf[1] = '\0';
1867 INFOBIT(TIOCM_RTS, "|RTS");
1868 STATBIT(TIOCM_CTS, "|CTS");
1869 INFOBIT(TIOCM_DTR, "|DTR");
1870 STATBIT(TIOCM_DSR, "|DSR");
1871 STATBIT(TIOCM_CAR, "|CD");
1872 STATBIT(TIOCM_RNG, "|RI");
1873 if (stat_buf[0])
1874 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001875
Alexey Dobriyand196a942009-03-31 15:19:21 -07001876 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001878 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879#undef STATBIT
1880#undef INFOBIT
Peter Hurley4047b372016-04-09 18:56:33 -07001881out:
1882 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Alexey Dobriyand196a942009-03-31 15:19:21 -07001885static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001887 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001889 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Peter Hurley968af292016-01-10 20:24:01 -08001891 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001892 for (i = 0; i < drv->nr; i++)
1893 uart_line_info(m, drv, i);
1894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896#endif
1897
Andy Shevchenko7a499552020-02-14 13:43:32 +02001898static inline bool uart_console_enabled(struct uart_port *port)
1899{
1900 return uart_console(port) && (port->cons->flags & CON_ENABLED);
1901}
1902
Johan Hovolde0830dbf2020-09-09 16:31:01 +02001903static void uart_port_spin_lock_init(struct uart_port *port)
Andy Shevchenkof7430612020-07-07 00:49:03 +03001904{
1905 spin_lock_init(&port->lock);
1906 lockdep_set_class(&port->lock, &port_lock_key);
1907}
1908
Andrew Morton4a1b5502008-03-07 15:51:16 -08001909#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Peter Hurley1cfe42b2015-03-09 16:27:13 -04001910/**
Russell Kingd3587882006-03-20 20:00:09 +00001911 * uart_console_write - write a console message to a serial port
1912 * @port: the port to write the message
1913 * @s: array of characters
1914 * @count: number of characters in string to write
Peter Hurley10afbe32015-04-11 10:05:07 -04001915 * @putchar: function to write character to port
Russell Kingd3587882006-03-20 20:00:09 +00001916 */
1917void uart_console_write(struct uart_port *port, const char *s,
1918 unsigned int count,
1919 void (*putchar)(struct uart_port *, int))
1920{
1921 unsigned int i;
1922
1923 for (i = 0; i < count; i++, s++) {
1924 if (*s == '\n')
1925 putchar(port, '\r');
1926 putchar(port, *s);
1927 }
1928}
1929EXPORT_SYMBOL_GPL(uart_console_write);
1930
1931/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * Check whether an invalid uart number has been specified, and
1933 * if so, search for the first available port that does have
1934 * console support.
1935 */
1936struct uart_port * __init
1937uart_get_console(struct uart_port *ports, int nr, struct console *co)
1938{
1939 int idx = co->index;
1940
1941 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1942 ports[idx].membase == NULL))
1943 for (idx = 0; idx < nr; idx++)
1944 if (ports[idx].iobase != 0 ||
1945 ports[idx].membase != NULL)
1946 break;
1947
1948 co->index = idx;
1949
1950 return ports + idx;
1951}
1952
1953/**
Peter Hurley73abaf82015-03-01 11:05:46 -05001954 * uart_parse_earlycon - Parse earlycon options
1955 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1956 * @iotype: ptr for decoded iotype (out)
1957 * @addr: ptr for decoded mapbase/iobase (out)
1958 * @options: ptr for <options> field; NULL if not present (out)
1959 *
1960 * Decodes earlycon kernel command line parameters of the form
Masahiro Yamadabd94c402015-10-28 12:46:05 +09001961 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1962 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
Peter Hurley73abaf82015-03-01 11:05:46 -05001963 *
1964 * The optional form
Randy Dunlapff302832019-10-08 13:46:53 -07001965 *
Peter Hurley73abaf82015-03-01 11:05:46 -05001966 * earlycon=<name>,0x<addr>,<options>
1967 * console=<name>,0x<addr>,<options>
Randy Dunlapff302832019-10-08 13:46:53 -07001968 *
Peter Hurley73abaf82015-03-01 11:05:46 -05001969 * is also accepted; the returned @iotype will be UPIO_MEM.
1970 *
Alexander Sverdlin8b2303d2016-09-12 13:29:29 +02001971 * Returns 0 on success or -EINVAL on failure
Peter Hurley73abaf82015-03-01 11:05:46 -05001972 */
Alexander Sverdlin46e36682016-09-02 13:20:21 +02001973int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
Peter Hurley73abaf82015-03-01 11:05:46 -05001974 char **options)
1975{
1976 if (strncmp(p, "mmio,", 5) == 0) {
1977 *iotype = UPIO_MEM;
1978 p += 5;
Masahiro Yamadabd94c402015-10-28 12:46:05 +09001979 } else if (strncmp(p, "mmio16,", 7) == 0) {
1980 *iotype = UPIO_MEM16;
1981 p += 7;
Peter Hurley73abaf82015-03-01 11:05:46 -05001982 } else if (strncmp(p, "mmio32,", 7) == 0) {
1983 *iotype = UPIO_MEM32;
1984 p += 7;
Noam Camus6e63be32015-05-25 06:54:28 +03001985 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1986 *iotype = UPIO_MEM32BE;
1987 p += 9;
Max Filippovd215d802015-09-22 15:20:32 +03001988 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1989 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1990 UPIO_MEM32BE : UPIO_MEM32;
1991 p += 13;
Peter Hurley73abaf82015-03-01 11:05:46 -05001992 } else if (strncmp(p, "io,", 3) == 0) {
1993 *iotype = UPIO_PORT;
1994 p += 3;
1995 } else if (strncmp(p, "0x", 2) == 0) {
1996 *iotype = UPIO_MEM;
1997 } else {
1998 return -EINVAL;
1999 }
2000
Alexander Sverdlin8b2303d2016-09-12 13:29:29 +02002001 /*
2002 * Before you replace it with kstrtoull(), think about options separator
2003 * (',') it will not tolerate
2004 */
2005 *addr = simple_strtoull(p, NULL, 0);
Peter Hurley73abaf82015-03-01 11:05:46 -05002006 p = strchr(p, ',');
2007 if (p)
2008 p++;
2009
2010 *options = p;
2011 return 0;
2012}
2013EXPORT_SYMBOL_GPL(uart_parse_earlycon);
2014
2015/**
Geert Uytterhoeven02088ca2014-03-11 11:23:35 +01002016 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 * @options: pointer to option string
2018 * @baud: pointer to an 'int' variable for the baud rate.
2019 * @parity: pointer to an 'int' variable for the parity.
2020 * @bits: pointer to an 'int' variable for the number of data bits.
2021 * @flow: pointer to an 'int' variable for the flow control character.
2022 *
2023 * uart_parse_options decodes a string containing the serial console
2024 * options. The format of the string is <baud><parity><bits><flow>,
2025 * eg: 115200n8r
2026 */
Jason Wesself2d937f2008-04-17 20:05:37 +02002027void
Paul Cercueil0f646b62017-12-28 14:07:07 +01002028uart_parse_options(const char *options, int *baud, int *parity,
2029 int *bits, int *flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Paul Cercueil0f646b62017-12-28 14:07:07 +01002031 const char *s = options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 *baud = simple_strtoul(s, NULL, 10);
2034 while (*s >= '0' && *s <= '9')
2035 s++;
2036 if (*s)
2037 *parity = *s++;
2038 if (*s)
2039 *bits = *s++ - '0';
2040 if (*s)
2041 *flow = *s;
2042}
Jason Wesself2d937f2008-04-17 20:05:37 +02002043EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/**
2046 * uart_set_options - setup the serial console parameters
2047 * @port: pointer to the serial ports uart_port structure
2048 * @co: console pointer
2049 * @baud: baud rate
2050 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2051 * @bits: number of data bits
2052 * @flow: flow control character - 'r' (rts)
2053 */
Jason Wesself2d937f2008-04-17 20:05:37 +02002054int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055uart_set_options(struct uart_port *port, struct console *co,
2056 int baud, int parity, int bits, int flow)
2057{
Alan Cox606d0992006-12-08 02:38:45 -08002058 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07002059 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Johan Hovolde0830dbf2020-09-09 16:31:01 +02002061 /*
2062 * Ensure that the serial-console lock is initialised early.
2063 *
2064 * Note that the console-enabled check is needed because of kgdboc,
2065 * which can end up calling uart_set_options() for an already enabled
2066 * console via tty_find_polling_driver() and uart_poll_init().
2067 */
2068 if (!uart_console_enabled(port) && !port->console_reinit)
2069 uart_port_spin_lock_init(port);
Russell King976ecd12005-07-03 21:05:45 +01002070
Alan Cox606d0992006-12-08 02:38:45 -08002071 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jeffy Chenba47f972016-01-04 15:54:46 +08002073 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2074 tty_termios_encode_baud_rate(&termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 if (bits == 7)
2077 termios.c_cflag |= CS7;
2078 else
2079 termios.c_cflag |= CS8;
2080
2081 switch (parity) {
2082 case 'o': case 'O':
2083 termios.c_cflag |= PARODD;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002084 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 case 'e': case 'E':
2086 termios.c_cflag |= PARENB;
2087 break;
2088 }
2089
2090 if (flow == 'r')
2091 termios.c_cflag |= CRTSCTS;
2092
Yinghai Lu79492682007-07-15 23:37:25 -07002093 /*
2094 * some uarts on other side don't support no flow control.
2095 * So we set * DTR in host uart to make them happy
2096 */
2097 port->mctrl |= TIOCM_DTR;
2098
Alan Cox149b36e2007-10-18 01:24:16 -07002099 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02002100 /*
2101 * Allow the setting of the UART parameters with a NULL console
2102 * too:
2103 */
Pali Rohár027b5712021-10-02 15:09:00 +02002104 if (co) {
Jason Wesself2d937f2008-04-17 20:05:37 +02002105 co->cflag = termios.c_cflag;
Pali Rohár027b5712021-10-02 15:09:00 +02002106 co->ispeed = termios.c_ispeed;
2107 co->ospeed = termios.c_ospeed;
2108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 return 0;
2111}
Jason Wesself2d937f2008-04-17 20:05:37 +02002112EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#endif /* CONFIG_SERIAL_CORE_CONSOLE */
2114
Jiri Slabycf755252011-11-09 21:33:47 +01002115/**
2116 * uart_change_pm - set power state of the port
2117 *
2118 * @state: port descriptor
2119 * @pm_state: new state
2120 *
2121 * Locking: port->mutex has to be held
2122 */
Linus Walleij6f538fe2012-12-07 11:36:08 +01002123static void uart_change_pm(struct uart_state *state,
2124 enum uart_pm_state pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Peter Hurley4047b372016-04-09 18:56:33 -07002126 struct uart_port *port = uart_port_check(state);
Andrew Victor1281e362006-05-16 11:28:49 +01002127
2128 if (state->pm_state != pm_state) {
Peter Hurley4047b372016-04-09 18:56:33 -07002129 if (port && port->ops->pm)
Andrew Victor1281e362006-05-16 11:28:49 +01002130 port->ops->pm(port, pm_state, state->pm_state);
2131 state->pm_state = pm_state;
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002135struct uart_match {
2136 struct uart_port *port;
2137 struct uart_driver *driver;
2138};
2139
2140static int serial_match_port(struct device *dev, void *data)
2141{
2142 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07002143 struct tty_driver *tty_drv = match->driver->tty_driver;
2144 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2145 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002146
2147 return dev->devt == devt; /* Actually, only one tty per port */
2148}
2149
Alan Coxccce6de2009-09-19 13:13:30 -07002150int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Alan Coxccce6de2009-09-19 13:13:30 -07002152 struct uart_state *state = drv->state + uport->line;
2153 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002154 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002155 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Alan Coxa2bceae2009-09-19 13:13:31 -07002157 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Alan Coxccce6de2009-09-19 13:13:30 -07002159 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Lucas Stach88e25822017-05-11 12:56:14 +02002160 if (tty_dev && device_may_wakeup(tty_dev)) {
Andy Shevchenkoaef3ad12017-08-13 17:47:42 +03002161 enable_irq_wake(uport->irq);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002162 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002163 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002164 return 0;
2165 }
Federico Vaga5a65dcc02013-04-15 16:01:07 +02002166 put_device(tty_dev);
2167
Peter Hurleyb164c972015-01-22 12:24:25 -05002168 /* Nothing to do if the console is not suspending */
2169 if (!console_suspend_enabled && uart_console(uport))
2170 goto unlock;
2171
2172 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002173
Peter Hurleyd41861c2016-04-09 17:53:25 -07002174 if (tty_port_initialized(port)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002175 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002176 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Peter Hurley80f02d52016-04-09 17:53:24 -07002178 tty_port_set_suspended(port, 1);
Peter Hurleyd41861c2016-04-09 17:53:25 -07002179 tty_port_set_initialized(port, 0);
Russell Kinga6b93a92006-10-01 17:17:40 +01002180
Peter Hurleyb164c972015-01-22 12:24:25 -05002181 spin_lock_irq(&uport->lock);
2182 ops->stop_tx(uport);
2183 ops->set_mctrl(uport, 0);
2184 ops->stop_rx(uport);
2185 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /*
2188 * Wait for the transmitter to empty.
2189 */
Alan Coxccce6de2009-09-19 13:13:30 -07002190 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002192 if (!tries)
Andy Shevchenkocade3582017-03-31 21:35:18 +03002193 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2194 uport->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Peter Hurleyb164c972015-01-22 12:24:25 -05002196 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
2199 /*
2200 * Disable the console device before suspending.
2201 */
Peter Hurleyb164c972015-01-22 12:24:25 -05002202 if (uart_console(uport))
Alan Coxccce6de2009-09-19 13:13:30 -07002203 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Peter Hurleyb164c972015-01-22 12:24:25 -05002205 uart_change_pm(state, UART_PM_STATE_OFF);
2206unlock:
Alan Coxa2bceae2009-09-19 13:13:31 -07002207 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 return 0;
2210}
2211
Alan Coxccce6de2009-09-19 13:13:30 -07002212int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Alan Coxccce6de2009-09-19 13:13:30 -07002214 struct uart_state *state = drv->state + uport->line;
2215 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002216 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002217 struct uart_match match = {uport, drv};
Deepak Saxenaba15ab02009-09-19 13:13:33 -07002218 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Alan Coxa2bceae2009-09-19 13:13:31 -07002220 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Alan Coxccce6de2009-09-19 13:13:30 -07002222 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2223 if (!uport->suspended && device_may_wakeup(tty_dev)) {
Andy Shevchenkoaef3ad12017-08-13 17:47:42 +03002224 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
Govindraj.R3f960db2010-12-16 18:12:47 +05302225 disable_irq_wake(uport->irq);
Federico Vaga5a65dcc02013-04-15 16:01:07 +02002226 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002227 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002228 return 0;
2229 }
Federico Vaga5a65dcc02013-04-15 16:01:07 +02002230 put_device(tty_dev);
Alan Coxccce6de2009-09-19 13:13:30 -07002231 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /*
2234 * Re-enable the console device after suspending.
2235 */
Yin Kangkai5933a162011-01-30 11:15:30 +08002236 if (uart_console(uport)) {
Jason Wang891b9dd2010-08-21 15:14:42 +08002237 /*
2238 * First try to use the console cflag setting.
2239 */
2240 memset(&termios, 0, sizeof(struct ktermios));
2241 termios.c_cflag = uport->cons->cflag;
Pali Rohár027b5712021-10-02 15:09:00 +02002242 termios.c_ispeed = uport->cons->ispeed;
2243 termios.c_ospeed = uport->cons->ospeed;
Jason Wang891b9dd2010-08-21 15:14:42 +08002244
2245 /*
2246 * If that's unset, use the tty termios setting.
2247 */
Alan Coxadc8d742012-07-14 15:31:47 +01002248 if (port->tty && termios.c_cflag == 0)
2249 termios = port->tty->termios;
Jason Wang891b9dd2010-08-21 15:14:42 +08002250
Ning Jiang94abc562011-09-05 16:28:18 +08002251 if (console_suspend_enabled)
Linus Walleij6f538fe2012-12-07 11:36:08 +01002252 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002253 uport->ops->set_termios(uport, &termios, NULL);
Yin Kangkai5933a162011-01-30 11:15:30 +08002254 if (console_suspend_enabled)
2255 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257
Peter Hurley80f02d52016-04-09 17:53:24 -07002258 if (tty_port_suspended(port)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002259 const struct uart_ops *ops = uport->ops;
Russell Kingee31b3372005-11-13 15:28:51 +00002260 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Linus Walleij6f538fe2012-12-07 11:36:08 +01002262 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002263 spin_lock_irq(&uport->lock);
2264 ops->set_mctrl(uport, 0);
2265 spin_unlock_irq(&uport->lock);
Stanislav Brabec4547be782009-12-02 16:20:56 +01002266 if (console_suspend_enabled || !uart_console(uport)) {
Alan Cox19225132010-06-01 22:52:51 +02002267 /* Protected by port mutex for now */
2268 struct tty_struct *tty = port->tty;
Tamseel Shams4ed71ad2020-07-16 17:24:38 +05302269
Stanislav Brabec4547be782009-12-02 16:20:56 +01002270 ret = ops->startup(uport);
2271 if (ret == 0) {
Alan Cox19225132010-06-01 22:52:51 +02002272 if (tty)
2273 uart_change_speed(tty, state, NULL);
Stanislav Brabec4547be782009-12-02 16:20:56 +01002274 spin_lock_irq(&uport->lock);
2275 ops->set_mctrl(uport, uport->mctrl);
2276 ops->start_tx(uport);
2277 spin_unlock_irq(&uport->lock);
Peter Hurleyd41861c2016-04-09 17:53:25 -07002278 tty_port_set_initialized(port, 1);
Stanislav Brabec4547be782009-12-02 16:20:56 +01002279 } else {
2280 /*
2281 * Failed to resume - maybe hardware went away?
2282 * Clear the "initialized" flag so we won't try
2283 * to call the low level drivers shutdown method.
2284 */
Alan Cox19225132010-06-01 22:52:51 +02002285 uart_shutdown(tty, state);
Stanislav Brabec4547be782009-12-02 16:20:56 +01002286 }
Russell Kingee31b3372005-11-13 15:28:51 +00002287 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002288
Peter Hurley80f02d52016-04-09 17:53:24 -07002289 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291
Alan Coxa2bceae2009-09-19 13:13:31 -07002292 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 return 0;
2295}
2296
2297static inline void
2298uart_report_port(struct uart_driver *drv, struct uart_port *port)
2299{
Russell King30b7a3b2005-09-03 15:30:21 +01002300 char address[64];
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 switch (port->iotype) {
2303 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002304 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 break;
2306 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002307 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002308 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 break;
2310 case UPIO_MEM:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002311 case UPIO_MEM16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 case UPIO_MEM32:
Kevin Cernekee3ffb1a82014-11-12 12:53:59 -08002313 case UPIO_MEM32BE:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002314 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002315 case UPIO_TSI:
Russell King30b7a3b2005-09-03 15:30:21 +01002316 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002317 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002318 break;
2319 default:
2320 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 break;
2322 }
Russell King30b7a3b2005-09-03 15:30:21 +01002323
Andy Shevchenkocade3582017-03-31 21:35:18 +03002324 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
James Bottomley68ed7e12015-01-02 10:05:13 -08002325 port->dev ? dev_name(port->dev) : "",
2326 port->dev ? ": " : "",
Andy Shevchenkocade3582017-03-31 21:35:18 +03002327 port->name,
Kees Cook7d12b972013-07-12 13:07:39 -07002328 address, port->irq, port->uartclk / 16, uart_type(port));
Maciej W. Rozyckie7b91932021-06-26 06:11:21 +02002329
2330 /* The magic multiplier feature is a bit obscure, so report it too. */
2331 if (port->flags & UPF_MAGIC_MULTIPLIER)
2332 pr_info("%s%s%s extra baud rates supported: %d, %d",
2333 port->dev ? dev_name(port->dev) : "",
2334 port->dev ? ": " : "",
2335 port->name,
2336 port->uartclk / 8, port->uartclk / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339static void
2340uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2341 struct uart_port *port)
2342{
2343 unsigned int flags;
2344
2345 /*
2346 * If there isn't a port here, don't do anything further.
2347 */
2348 if (!port->iobase && !port->mapbase && !port->membase)
2349 return;
2350
2351 /*
2352 * Now do the auto configuration stuff. Note that config_port
2353 * is expected to claim the resources and map the port for us.
2354 */
David Daney8e23fcc2009-01-02 13:49:54 +00002355 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (port->flags & UPF_AUTO_IRQ)
2357 flags |= UART_CONFIG_IRQ;
2358 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002359 if (!(port->flags & UPF_FIXED_TYPE)) {
2360 port->type = PORT_UNKNOWN;
2361 flags |= UART_CONFIG_TYPE;
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 port->ops->config_port(port, flags);
2364 }
2365
2366 if (port->type != PORT_UNKNOWN) {
2367 unsigned long flags;
2368
2369 uart_report_port(drv, port);
2370
George G. Davis3689a0e2007-02-14 00:33:06 -08002371 /* Power up port for set_mctrl() */
Linus Walleij6f538fe2012-12-07 11:36:08 +01002372 uart_change_pm(state, UART_PM_STATE_ON);
George G. Davis3689a0e2007-02-14 00:33:06 -08002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002376 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 * We probably don't need a spinlock around this, but
2378 */
2379 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002380 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 spin_unlock_irqrestore(&port->lock, flags);
2382
2383 /*
Russell King97d97222007-09-01 21:25:09 +01002384 * If this driver supports console, and it hasn't been
2385 * successfully registered yet, try to re-register it.
2386 * It may be that the port was not available.
2387 */
2388 if (port->cons && !(port->cons->flags & CON_ENABLED))
2389 register_console(port->cons);
2390
2391 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 * Power down all ports by default, except the
2393 * console if we have one.
2394 */
2395 if (!uart_console(port))
Linus Walleij6f538fe2012-12-07 11:36:08 +01002396 uart_change_pm(state, UART_PM_STATE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398}
2399
Jason Wesself2d937f2008-04-17 20:05:37 +02002400#ifdef CONFIG_CONSOLE_POLL
2401
2402static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2403{
2404 struct uart_driver *drv = driver->driver_state;
2405 struct uart_state *state = drv->state + line;
Peter Hurley49c02302016-04-09 18:56:32 -07002406 struct tty_port *tport;
Jason Wesself2d937f2008-04-17 20:05:37 +02002407 struct uart_port *port;
2408 int baud = 9600;
2409 int bits = 8;
2410 int parity = 'n';
2411 int flow = 'n';
Peter Hurley49c02302016-04-09 18:56:32 -07002412 int ret = 0;
Jason Wesself2d937f2008-04-17 20:05:37 +02002413
Peter Hurley49c02302016-04-09 18:56:32 -07002414 tport = &state->port;
2415 mutex_lock(&tport->mutex);
2416
Peter Hurley4047b372016-04-09 18:56:33 -07002417 port = uart_port_check(state);
2418 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
Peter Hurley49c02302016-04-09 18:56:32 -07002419 ret = -1;
2420 goto out;
2421 }
Jason Wesself2d937f2008-04-17 20:05:37 +02002422
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002423 if (port->ops->poll_init) {
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002424 /*
Peter Hurleyd41861c2016-04-09 17:53:25 -07002425 * We don't set initialized as we only initialized the hw,
2426 * e.g. state->xmit is still uninitialized.
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002427 */
Peter Hurleyd41861c2016-04-09 17:53:25 -07002428 if (!tty_port_initialized(tport))
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002429 ret = port->ops->poll_init(port);
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002430 }
2431
Peter Hurley49c02302016-04-09 18:56:32 -07002432 if (!ret && options) {
Jason Wesself2d937f2008-04-17 20:05:37 +02002433 uart_parse_options(options, &baud, &parity, &bits, &flow);
Peter Hurley49c02302016-04-09 18:56:32 -07002434 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
Jason Wesself2d937f2008-04-17 20:05:37 +02002435 }
Peter Hurley49c02302016-04-09 18:56:32 -07002436out:
2437 mutex_unlock(&tport->mutex);
2438 return ret;
Jason Wesself2d937f2008-04-17 20:05:37 +02002439}
2440
2441static int uart_poll_get_char(struct tty_driver *driver, int line)
2442{
2443 struct uart_driver *drv = driver->driver_state;
2444 struct uart_state *state = drv->state + line;
2445 struct uart_port *port;
Peter Hurley9ed19422016-04-09 18:56:34 -07002446 int ret = -1;
Jason Wesself2d937f2008-04-17 20:05:37 +02002447
Jiri Slaby22077b02017-03-15 12:00:23 +01002448 port = uart_port_ref(state);
2449 if (port) {
2450 ret = port->ops->poll_get_char(port);
2451 uart_port_deref(port);
Peter Hurley9ed19422016-04-09 18:56:34 -07002452 }
Jiri Slaby22077b02017-03-15 12:00:23 +01002453
Peter Hurley9ed19422016-04-09 18:56:34 -07002454 return ret;
Jason Wesself2d937f2008-04-17 20:05:37 +02002455}
2456
2457static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2458{
2459 struct uart_driver *drv = driver->driver_state;
2460 struct uart_state *state = drv->state + line;
2461 struct uart_port *port;
2462
Peter Hurley9ed19422016-04-09 18:56:34 -07002463 port = uart_port_ref(state);
2464 if (!port)
2465 return;
Doug Andersonc7d44a02014-04-21 10:06:43 -07002466
2467 if (ch == '\n')
2468 port->ops->poll_put_char(port, '\r');
Jason Wesself2d937f2008-04-17 20:05:37 +02002469 port->ops->poll_put_char(port, ch);
Peter Hurley9ed19422016-04-09 18:56:34 -07002470 uart_port_deref(port);
Jason Wesself2d937f2008-04-17 20:05:37 +02002471}
2472#endif
2473
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002474static const struct tty_operations uart_ops = {
Jiri Slaby4cdd17b2019-04-17 10:58:53 +02002475 .install = uart_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .open = uart_open,
2477 .close = uart_close,
2478 .write = uart_write,
2479 .put_char = uart_put_char,
2480 .flush_chars = uart_flush_chars,
2481 .write_room = uart_write_room,
2482 .chars_in_buffer= uart_chars_in_buffer,
2483 .flush_buffer = uart_flush_buffer,
2484 .ioctl = uart_ioctl,
2485 .throttle = uart_throttle,
2486 .unthrottle = uart_unthrottle,
2487 .send_xchar = uart_send_xchar,
2488 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002489 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .stop = uart_stop,
2491 .start = uart_start,
2492 .hangup = uart_hangup,
2493 .break_ctl = uart_break_ctl,
2494 .wait_until_sent= uart_wait_until_sent,
2495#ifdef CONFIG_PROC_FS
Christoph Hellwig8a8dcab2018-04-13 21:04:45 +02002496 .proc_show = uart_proc_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497#endif
2498 .tiocmget = uart_tiocmget,
2499 .tiocmset = uart_tiocmset,
Al Viro5099d232018-09-11 22:29:55 -04002500 .set_serial = uart_set_info_user,
2501 .get_serial = uart_get_info_user,
Alan Coxd281da72010-09-16 18:21:24 +01002502 .get_icount = uart_get_icount,
Jason Wesself2d937f2008-04-17 20:05:37 +02002503#ifdef CONFIG_CONSOLE_POLL
2504 .poll_init = uart_poll_init,
2505 .poll_get_char = uart_poll_get_char,
2506 .poll_put_char = uart_poll_put_char,
2507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508};
2509
Alan Coxde0c8cb2010-06-01 22:52:58 +02002510static const struct tty_port_operations uart_port_ops = {
2511 .carrier_raised = uart_carrier_raised,
2512 .dtr_rts = uart_dtr_rts,
Rob Herringb3b57642016-08-22 17:39:09 -05002513 .activate = uart_port_activate,
Rob Herring761ed4a2016-08-22 17:39:10 -05002514 .shutdown = uart_tty_port_shutdown,
Alan Coxde0c8cb2010-06-01 22:52:58 +02002515};
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/**
2518 * uart_register_driver - register a driver with the uart core layer
2519 * @drv: low level driver structure
2520 *
2521 * Register a uart driver with the core driver. We in turn register
2522 * with the tty layer, and initialise the core driver per-port state.
2523 *
2524 * We have a proc file in /proc/tty/driver which is named after the
2525 * normal driver.
2526 *
2527 * drv->port should be NULL, and the per-port structures should be
2528 * registered using uart_add_one_port after this call has succeeded.
2529 */
2530int uart_register_driver(struct uart_driver *drv)
2531{
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002532 struct tty_driver *normal;
Sergey Organov050dfc02019-04-23 08:06:58 +03002533 int i, retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 BUG_ON(drv->state);
2536
2537 /*
2538 * Maybe we should be using a slab cache for this, especially if
2539 * we have a large number of ports to handle.
2540 */
Kees Cook6396bb22018-06-12 14:03:40 -07002541 drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (!drv->state)
2543 goto out;
2544
Jiri Slaby39b7b422021-07-23 09:43:13 +02002545 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
2546 TTY_DRIVER_DYNAMIC_DEV);
2547 if (IS_ERR(normal)) {
2548 retval = PTR_ERR(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002549 goto out_kfree;
Jiri Slaby39b7b422021-07-23 09:43:13 +02002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 drv->tty_driver = normal;
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 normal->name = drv->dev_name;
2556 normal->major = drv->major;
2557 normal->minor_start = drv->minor;
2558 normal->type = TTY_DRIVER_TYPE_SERIAL;
2559 normal->subtype = SERIAL_TYPE_NORMAL;
2560 normal->init_termios = tty_std_termios;
2561 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002562 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 normal->driver_state = drv;
2564 tty_set_operations(normal, &uart_ops);
2565
2566 /*
2567 * Initialise the UART state(s).
2568 */
2569 for (i = 0; i < drv->nr; i++) {
2570 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07002571 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Alan Coxa2bceae2009-09-19 13:13:31 -07002573 tty_port_init(port);
Alan Coxde0c8cb2010-06-01 22:52:58 +02002574 port->ops = &uart_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576
2577 retval = tty_register_driver(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002578 if (retval >= 0)
2579 return retval;
2580
Jiri Slaby191c5f12012-11-15 09:49:56 +01002581 for (i = 0; i < drv->nr; i++)
2582 tty_port_destroy(&drv->state[i].port);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02002583 tty_driver_kref_put(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002584out_kfree:
2585 kfree(drv->state);
2586out:
Sergey Organov050dfc02019-04-23 08:06:58 +03002587 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/**
2591 * uart_unregister_driver - remove a driver from the uart core layer
2592 * @drv: low level driver structure
2593 *
2594 * Remove all references to a driver from the core driver. The low
2595 * level driver must have removed all its ports via the
2596 * uart_remove_one_port() if it registered them with uart_add_one_port().
2597 * (ie, drv->port == NULL)
2598 */
2599void uart_unregister_driver(struct uart_driver *drv)
2600{
2601 struct tty_driver *p = drv->tty_driver;
Jiri Slaby191c5f12012-11-15 09:49:56 +01002602 unsigned int i;
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 tty_unregister_driver(p);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +02002605 tty_driver_kref_put(p);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002606 for (i = 0; i < drv->nr; i++)
2607 tty_port_destroy(&drv->state[i].port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 kfree(drv->state);
Alan Cox1e66cde2012-05-14 14:51:22 +01002609 drv->state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 drv->tty_driver = NULL;
2611}
2612
2613struct tty_driver *uart_console_device(struct console *co, int *index)
2614{
2615 struct uart_driver *p = co->data;
2616 *index = co->index;
2617 return p->tty_driver;
2618}
John Stultz488f49a2020-01-07 01:03:10 +00002619EXPORT_SYMBOL_GPL(uart_console_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002621static ssize_t uartclk_show(struct device *dev,
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002622 struct device_attribute *attr, char *buf)
2623{
Alan Coxbebe73e2012-10-29 15:19:57 +00002624 struct serial_struct tmp;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002625 struct tty_port *port = dev_get_drvdata(dev);
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002626
Alan Cox9f109692012-10-29 15:20:25 +00002627 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002628 return sprintf(buf, "%d\n", tmp.baud_base * 16);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002629}
2630
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002631static ssize_t type_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002632 struct device_attribute *attr, char *buf)
2633{
2634 struct serial_struct tmp;
2635 struct tty_port *port = dev_get_drvdata(dev);
2636
2637 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002638 return sprintf(buf, "%d\n", tmp.type);
Alan Cox373bac42012-10-29 15:20:40 +00002639}
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002640
2641static ssize_t line_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002642 struct device_attribute *attr, char *buf)
2643{
2644 struct serial_struct tmp;
2645 struct tty_port *port = dev_get_drvdata(dev);
2646
2647 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002648 return sprintf(buf, "%d\n", tmp.line);
Alan Cox373bac42012-10-29 15:20:40 +00002649}
2650
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002651static ssize_t port_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002652 struct device_attribute *attr, char *buf)
2653{
2654 struct serial_struct tmp;
2655 struct tty_port *port = dev_get_drvdata(dev);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002656 unsigned long ioaddr;
Alan Cox373bac42012-10-29 15:20:40 +00002657
2658 uart_get_info(port, &tmp);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002659 ioaddr = tmp.port;
2660 if (HIGH_BITS_OFFSET)
2661 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002662 return sprintf(buf, "0x%lX\n", ioaddr);
Alan Cox373bac42012-10-29 15:20:40 +00002663}
2664
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002665static ssize_t irq_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002666 struct device_attribute *attr, char *buf)
2667{
2668 struct serial_struct tmp;
2669 struct tty_port *port = dev_get_drvdata(dev);
2670
2671 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002672 return sprintf(buf, "%d\n", tmp.irq);
Alan Cox373bac42012-10-29 15:20:40 +00002673}
2674
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002675static ssize_t flags_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002676 struct device_attribute *attr, char *buf)
2677{
2678 struct serial_struct tmp;
2679 struct tty_port *port = dev_get_drvdata(dev);
2680
2681 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002682 return sprintf(buf, "0x%X\n", tmp.flags);
Alan Cox373bac42012-10-29 15:20:40 +00002683}
2684
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002685static ssize_t xmit_fifo_size_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002686 struct device_attribute *attr, char *buf)
2687{
2688 struct serial_struct tmp;
2689 struct tty_port *port = dev_get_drvdata(dev);
2690
2691 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002692 return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
Alan Cox373bac42012-10-29 15:20:40 +00002693}
2694
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002695static ssize_t close_delay_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002696 struct device_attribute *attr, char *buf)
2697{
2698 struct serial_struct tmp;
2699 struct tty_port *port = dev_get_drvdata(dev);
2700
2701 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002702 return sprintf(buf, "%d\n", tmp.close_delay);
Alan Cox373bac42012-10-29 15:20:40 +00002703}
2704
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002705static ssize_t closing_wait_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002706 struct device_attribute *attr, char *buf)
2707{
2708 struct serial_struct tmp;
2709 struct tty_port *port = dev_get_drvdata(dev);
2710
2711 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002712 return sprintf(buf, "%d\n", tmp.closing_wait);
Alan Cox373bac42012-10-29 15:20:40 +00002713}
2714
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002715static ssize_t custom_divisor_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002716 struct device_attribute *attr, char *buf)
2717{
2718 struct serial_struct tmp;
2719 struct tty_port *port = dev_get_drvdata(dev);
2720
2721 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002722 return sprintf(buf, "%d\n", tmp.custom_divisor);
Alan Cox373bac42012-10-29 15:20:40 +00002723}
2724
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002725static ssize_t io_type_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002726 struct device_attribute *attr, char *buf)
2727{
2728 struct serial_struct tmp;
2729 struct tty_port *port = dev_get_drvdata(dev);
2730
2731 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002732 return sprintf(buf, "%d\n", tmp.io_type);
Alan Cox373bac42012-10-29 15:20:40 +00002733}
2734
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002735static ssize_t iomem_base_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002736 struct device_attribute *attr, char *buf)
2737{
2738 struct serial_struct tmp;
2739 struct tty_port *port = dev_get_drvdata(dev);
2740
2741 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002742 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
Alan Cox373bac42012-10-29 15:20:40 +00002743}
2744
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002745static ssize_t iomem_reg_shift_show(struct device *dev,
Alan Cox373bac42012-10-29 15:20:40 +00002746 struct device_attribute *attr, char *buf)
2747{
2748 struct serial_struct tmp;
2749 struct tty_port *port = dev_get_drvdata(dev);
2750
2751 uart_get_info(port, &tmp);
Alex Dewar9cfbf7a2020-08-24 23:39:33 +01002752 return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
Alan Cox373bac42012-10-29 15:20:40 +00002753}
2754
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002755static ssize_t console_show(struct device *dev,
2756 struct device_attribute *attr, char *buf)
2757{
2758 struct tty_port *port = dev_get_drvdata(dev);
2759 struct uart_state *state = container_of(port, struct uart_state, port);
2760 struct uart_port *uport;
2761 bool console = false;
2762
2763 mutex_lock(&port->mutex);
2764 uport = uart_port_check(state);
2765 if (uport)
2766 console = uart_console_enabled(uport);
2767 mutex_unlock(&port->mutex);
2768
2769 return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2770}
2771
2772static ssize_t console_store(struct device *dev,
2773 struct device_attribute *attr, const char *buf, size_t count)
2774{
2775 struct tty_port *port = dev_get_drvdata(dev);
2776 struct uart_state *state = container_of(port, struct uart_state, port);
2777 struct uart_port *uport;
2778 bool oldconsole, newconsole;
2779 int ret;
2780
2781 ret = kstrtobool(buf, &newconsole);
2782 if (ret)
2783 return ret;
2784
2785 mutex_lock(&port->mutex);
2786 uport = uart_port_check(state);
2787 if (uport) {
2788 oldconsole = uart_console_enabled(uport);
2789 if (oldconsole && !newconsole) {
2790 ret = unregister_console(uport->cons);
2791 } else if (!oldconsole && newconsole) {
Johan Hovolde0830dbf2020-09-09 16:31:01 +02002792 if (uart_console(uport)) {
2793 uport->console_reinit = 1;
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002794 register_console(uport->cons);
Johan Hovolde0830dbf2020-09-09 16:31:01 +02002795 } else {
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002796 ret = -ENOENT;
Johan Hovolde0830dbf2020-09-09 16:31:01 +02002797 }
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002798 }
2799 } else {
2800 ret = -ENXIO;
2801 }
2802 mutex_unlock(&port->mutex);
2803
2804 return ret < 0 ? ret : count;
2805}
2806
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002807static DEVICE_ATTR_RO(uartclk);
2808static DEVICE_ATTR_RO(type);
2809static DEVICE_ATTR_RO(line);
2810static DEVICE_ATTR_RO(port);
2811static DEVICE_ATTR_RO(irq);
2812static DEVICE_ATTR_RO(flags);
2813static DEVICE_ATTR_RO(xmit_fifo_size);
2814static DEVICE_ATTR_RO(close_delay);
2815static DEVICE_ATTR_RO(closing_wait);
2816static DEVICE_ATTR_RO(custom_divisor);
2817static DEVICE_ATTR_RO(io_type);
2818static DEVICE_ATTR_RO(iomem_base);
2819static DEVICE_ATTR_RO(iomem_reg_shift);
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002820static DEVICE_ATTR_RW(console);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002821
2822static struct attribute *tty_dev_attrs[] = {
Andy Shevchenko143c02c2020-02-17 13:40:11 +02002823 &dev_attr_uartclk.attr,
Alan Cox373bac42012-10-29 15:20:40 +00002824 &dev_attr_type.attr,
2825 &dev_attr_line.attr,
2826 &dev_attr_port.attr,
2827 &dev_attr_irq.attr,
2828 &dev_attr_flags.attr,
2829 &dev_attr_xmit_fifo_size.attr,
Alan Cox373bac42012-10-29 15:20:40 +00002830 &dev_attr_close_delay.attr,
2831 &dev_attr_closing_wait.attr,
2832 &dev_attr_custom_divisor.attr,
2833 &dev_attr_io_type.attr,
2834 &dev_attr_iomem_base.attr,
2835 &dev_attr_iomem_reg_shift.attr,
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002836 &dev_attr_console.attr,
2837 NULL
2838};
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002839
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002840static const struct attribute_group tty_dev_attr_group = {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002841 .attrs = tty_dev_attrs,
Andy Shevchenkoa3cb39d2020-02-17 13:40:12 +02002842};
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844/**
2845 * uart_add_one_port - attach a driver-defined port structure
2846 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002847 * @uport: uart port structure to use for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 *
Ahmed S. Darwisha1572702021-02-08 19:16:15 +01002849 * Context: task context, might sleep
2850 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 * This allows the driver to register its own uart_port structure
2852 * with the core driver. The main purpose is to allow the low
2853 * level uart drivers to expand uart_port, rather than having yet
2854 * more levels of structures.
2855 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002856int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07002859 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002861 struct device *tty_dev;
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002862 int num_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Alan Coxa2bceae2009-09-19 13:13:31 -07002864 if (uport->line >= drv->nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return -EINVAL;
2866
Alan Coxa2bceae2009-09-19 13:13:31 -07002867 state = drv->state + uport->line;
2868 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002870 mutex_lock(&port_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07002871 mutex_lock(&port->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002872 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 ret = -EINVAL;
2874 goto out;
2875 }
2876
Peter Hurley2b702b92014-10-16 16:54:25 -04002877 /* Link the port to the driver state table and vice versa */
Peter Hurley9ed19422016-04-09 18:56:34 -07002878 atomic_set(&state->refcount, 1);
2879 init_waitqueue_head(&state->remove_wait);
Alan Coxa2bceae2009-09-19 13:13:31 -07002880 state->uart_port = uport;
Alan Coxa2bceae2009-09-19 13:13:31 -07002881 uport->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Peter Hurley2b702b92014-10-16 16:54:25 -04002883 state->pm_state = UART_PM_STATE_UNDEFINED;
2884 uport->cons = drv->cons;
Peter Hurley959801f2015-02-24 14:25:00 -05002885 uport->minor = drv->tty_driver->minor_start + uport->line;
Vignesh Rf7048b12017-03-24 10:57:59 +05302886 uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
2887 drv->tty_driver->name_base + uport->line);
2888 if (!uport->name) {
2889 ret = -ENOMEM;
2890 goto out;
2891 }
Peter Hurley2b702b92014-10-16 16:54:25 -04002892
Johan Hovoldfe88c642020-09-09 16:31:00 +02002893 /*
2894 * If this port is in use as a console then the spinlock is already
2895 * initialised.
2896 */
2897 if (!uart_console_enabled(uport))
Johan Hovolde0830dbf2020-09-09 16:31:01 +02002898 uart_port_spin_lock_init(uport);
Andy Shevchenkod2403ca2020-02-14 13:43:33 +02002899
Grant Likelya208ffd2014-03-27 18:29:46 -07002900 if (uport->cons && uport->dev)
Michal Simekdd8b7a12019-10-16 12:36:41 +02002901 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
Russell King976ecd12005-07-03 21:05:45 +01002902
Sudip Mukherjeefb2b9002019-12-12 13:16:02 +00002903 tty_port_link_device(port, drv->tty_driver, uport->line);
Alan Coxa2bceae2009-09-19 13:13:31 -07002904 uart_configure_port(drv, state, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Geert Uytterhoeven4dda8642016-10-28 07:07:47 -05002906 port->console = uart_console(uport);
2907
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002908 num_groups = 2;
2909 if (uport->attr_group)
2910 num_groups++;
2911
Yoshihiro YUNOMAEc2b703b2014-07-23 06:06:22 +00002912 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002913 GFP_KERNEL);
2914 if (!uport->tty_groups) {
2915 ret = -ENOMEM;
2916 goto out;
2917 }
2918 uport->tty_groups[0] = &tty_dev_attr_group;
2919 if (uport->attr_group)
2920 uport->tty_groups[1] = uport->attr_group;
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
2923 * Register the port whether it's detected or not. This allows
Geert Uytterhoeven015355b2014-03-11 11:23:36 +01002924 * setserial to be used to alter this port's parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 */
Johan Hovoldda4c2792017-05-18 17:33:01 +02002926 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002927 uport->line, uport->dev, port, uport->tty_groups);
Chengguang Xub289c492019-02-18 16:31:04 +08002928 if (!IS_ERR(tty_dev)) {
Simon Glass77359832012-01-19 11:28:56 -08002929 device_set_wakeup_capable(tty_dev, 1);
2930 } else {
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302931 dev_err(uport->dev, "Cannot register tty device on line %d\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002932 uport->line);
Simon Glass77359832012-01-19 11:28:56 -08002933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 /*
Russell King68ac64c2006-04-30 11:13:50 +01002936 * Ensure UPF_DEAD is not set.
2937 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002938 uport->flags &= ~UPF_DEAD;
Russell King68ac64c2006-04-30 11:13:50 +01002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 out:
Alan Coxa2bceae2009-09-19 13:13:31 -07002941 mutex_unlock(&port->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002942 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 return ret;
2945}
2946
2947/**
2948 * uart_remove_one_port - detach a driver defined port structure
2949 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002950 * @uport: uart port structure for this port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 *
Ahmed S. Darwisha1572702021-02-08 19:16:15 +01002952 * Context: task context, might sleep
2953 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 * This unhooks (and hangs up) the specified port structure from the
2955 * core driver. No further calls will be made to the low-level code
2956 * for this port.
2957 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002958int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Alan Coxa2bceae2009-09-19 13:13:31 -07002960 struct uart_state *state = drv->state + uport->line;
2961 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07002962 struct uart_port *uart_port;
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002963 struct tty_struct *tty;
Chen Gangb342dd52012-12-27 15:51:31 +08002964 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002966 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 /*
Russell King68ac64c2006-04-30 11:13:50 +01002969 * Mark the port "dead" - this prevents any opens from
2970 * succeeding while we shut down the port.
2971 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002972 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07002973 uart_port = uart_port_check(state);
2974 if (uart_port != uport)
2975 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2976 uart_port, uport);
2977
2978 if (!uart_port) {
Chen Gangb342dd52012-12-27 15:51:31 +08002979 mutex_unlock(&port->mutex);
2980 ret = -EINVAL;
2981 goto out;
2982 }
Alan Coxa2bceae2009-09-19 13:13:31 -07002983 uport->flags |= UPF_DEAD;
2984 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002985
2986 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002987 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
Johan Hovoldda4c2792017-05-18 17:33:01 +02002989 tty_port_unregister_device(port, drv->tty_driver, uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002991 tty = tty_port_tty_get(port);
2992 if (tty) {
Alan Coxa2bceae2009-09-19 13:13:31 -07002993 tty_vhangup(port->tty);
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002994 tty_kref_put(tty);
2995 }
Russell King68ac64c2006-04-30 11:13:50 +01002996
2997 /*
Geert Uytterhoeven5f5c9ae2014-02-28 14:21:32 +01002998 * If the port is used as a console, unregister it
2999 */
3000 if (uart_console(uport))
3001 unregister_console(uport->cons);
3002
3003 /*
Russell King68ac64c2006-04-30 11:13:50 +01003004 * Free the port IO and memory resources, if any.
3005 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -03003006 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
Alan Coxa2bceae2009-09-19 13:13:31 -07003007 uport->ops->release_port(uport);
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00003008 kfree(uport->tty_groups);
Vignesh Rf7048b12017-03-24 10:57:59 +05303009 kfree(uport->name);
Russell King68ac64c2006-04-30 11:13:50 +01003010
3011 /*
3012 * Indicate that there isn't a port here anymore.
3013 */
Alan Coxa2bceae2009-09-19 13:13:31 -07003014 uport->type = PORT_UNKNOWN;
Russell King68ac64c2006-04-30 11:13:50 +01003015
Peter Hurley4047b372016-04-09 18:56:33 -07003016 mutex_lock(&port->mutex);
Peter Hurley9ed19422016-04-09 18:56:34 -07003017 WARN_ON(atomic_dec_return(&state->refcount) < 0);
3018 wait_event(state->remove_wait, !atomic_read(&state->refcount));
Alan Coxebd2c8f2009-09-19 13:13:28 -07003019 state->uart_port = NULL;
Peter Hurley4047b372016-04-09 18:56:33 -07003020 mutex_unlock(&port->mutex);
Chen Gangb342dd52012-12-27 15:51:31 +08003021out:
Arjan van de Venf392ecf2006-01-12 18:44:32 +00003022 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Chen Gangb342dd52012-12-27 15:51:31 +08003024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027/*
3028 * Are the two ports equivalent?
3029 */
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003030bool uart_match_port(const struct uart_port *port1,
3031 const struct uart_port *port2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 if (port1->iotype != port2->iotype)
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003034 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 switch (port1->iotype) {
3037 case UPIO_PORT:
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003038 return port1->iobase == port2->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 case UPIO_HUB6:
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003040 return port1->iobase == port2->iobase &&
3041 port1->hub6 == port2->hub6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 case UPIO_MEM:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09003043 case UPIO_MEM16:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04003044 case UPIO_MEM32:
Kevin Cernekee3ffb1a82014-11-12 12:53:59 -08003045 case UPIO_MEM32BE:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04003046 case UPIO_AU:
3047 case UPIO_TSI:
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003048 return port1->mapbase == port2->mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Jiri Slabyb8be5db2021-05-19 09:21:50 +02003050
3051 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053EXPORT_SYMBOL(uart_match_port);
3054
Jiri Slaby027d7da2011-11-09 21:33:43 +01003055/**
3056 * uart_handle_dcd_change - handle a change of carrier detect state
3057 * @uport: uart_port structure for the open port
3058 * @status: new carrier detect status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04003059 *
3060 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01003061 */
3062void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
3063{
George Spelvin42381572013-02-10 04:44:30 -05003064 struct tty_port *port = &uport->state->port;
Alan Cox43eca0a2012-09-19 15:35:46 +01003065 struct tty_struct *tty = port->tty;
Peter Hurleyc9932572014-09-02 17:39:21 -04003066 struct tty_ldisc *ld;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003067
Peter Hurley4d90bb12014-09-10 15:06:23 -04003068 lockdep_assert_held_once(&uport->lock);
3069
Peter Hurleyc9932572014-09-02 17:39:21 -04003070 if (tty) {
3071 ld = tty_ldisc_ref(tty);
3072 if (ld) {
3073 if (ld->ops->dcd_change)
3074 ld->ops->dcd_change(tty, status);
3075 tty_ldisc_deref(ld);
3076 }
George Spelvin42381572013-02-10 04:44:30 -05003077 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01003078
3079 uport->icount.dcd++;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003080
Peter Hurley299245a2014-09-10 15:06:24 -04003081 if (uart_dcd_enabled(uport)) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01003082 if (status)
3083 wake_up_interruptible(&port->open_wait);
Alan Cox43eca0a2012-09-19 15:35:46 +01003084 else if (tty)
3085 tty_hangup(tty);
Jiri Slaby027d7da2011-11-09 21:33:43 +01003086 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01003087}
3088EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3089
3090/**
3091 * uart_handle_cts_change - handle a change of clear-to-send state
3092 * @uport: uart_port structure for the open port
3093 * @status: new clear to send status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04003094 *
3095 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01003096 */
3097void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3098{
Peter Hurley4d90bb12014-09-10 15:06:23 -04003099 lockdep_assert_held_once(&uport->lock);
3100
Jiri Slaby027d7da2011-11-09 21:33:43 +01003101 uport->icount.cts++;
3102
Peter Hurley391f93f2015-01-25 14:44:51 -05003103 if (uart_softcts_mode(uport)) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04003104 if (uport->hw_stopped) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01003105 if (status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04003106 uport->hw_stopped = 0;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003107 uport->ops->start_tx(uport);
3108 uart_write_wakeup(uport);
3109 }
3110 } else {
3111 if (!status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04003112 uport->hw_stopped = 1;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003113 uport->ops->stop_tx(uport);
3114 }
3115 }
Peter Hurley391f93f2015-01-25 14:44:51 -05003116
Jiri Slaby027d7da2011-11-09 21:33:43 +01003117 }
3118}
3119EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3120
Jiri Slabycf755252011-11-09 21:33:47 +01003121/**
3122 * uart_insert_char - push a char to the uart layer
3123 *
3124 * User is responsible to call tty_flip_buffer_push when they are done with
3125 * insertion.
3126 *
3127 * @port: corresponding port
3128 * @status: state of the serial port RX buffer (LSR for 8250)
3129 * @overrun: mask of overrun bits in @status
3130 * @ch: character to push
3131 * @flag: flag for the character (see TTY_NORMAL and friends)
3132 */
Jiri Slaby027d7da2011-11-09 21:33:43 +01003133void uart_insert_char(struct uart_port *port, unsigned int status,
3134 unsigned int overrun, unsigned int ch, unsigned int flag)
3135{
Jiri Slaby92a19f92013-01-03 15:53:03 +01003136 struct tty_port *tport = &port->state->port;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003137
3138 if ((status & port->ignore_status_mask & ~overrun) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +01003139 if (tty_insert_flip_char(tport, ch, flag) == 0)
Corbindabfb352012-05-23 09:37:31 -05003140 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003141
3142 /*
3143 * Overrun is special. Since it's reported immediately,
3144 * it doesn't affect the current character.
3145 */
3146 if (status & ~port->ignore_status_mask & overrun)
Jiri Slaby92a19f92013-01-03 15:53:03 +01003147 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
Corbindabfb352012-05-23 09:37:31 -05003148 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003149}
3150EXPORT_SYMBOL_GPL(uart_insert_char);
3151
Dmitry Safonov68af4312020-03-02 17:51:35 +00003152#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3153static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3154
3155static void uart_sysrq_on(struct work_struct *w)
3156{
Andy Shevchenkob18896f2020-03-10 19:43:35 +02003157 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3158
Dmitry Safonov68af4312020-03-02 17:51:35 +00003159 sysrq_toggle_support(1);
Andy Shevchenkob18896f2020-03-10 19:43:35 +02003160 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3161 sysrq_toggle_seq_len, sysrq_toggle_seq);
Dmitry Safonov68af4312020-03-02 17:51:35 +00003162}
3163static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3164
3165/**
3166 * uart_try_toggle_sysrq - Enables SysRq from serial line
3167 * @port: uart_port structure where char(s) after BREAK met
3168 * @ch: new character in the sequence after received BREAK
3169 *
3170 * Enables magic SysRq when the required sequence is met on port
3171 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3172 *
3173 * Returns false if @ch is out of enabling sequence and should be
3174 * handled some other way, true if @ch was consumed.
3175 */
Johan Hovold08d54702020-06-10 17:22:31 +02003176bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
Dmitry Safonov68af4312020-03-02 17:51:35 +00003177{
Andy Shevchenko2ce5eac2020-03-10 19:43:34 +02003178 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3179
3180 if (!sysrq_toggle_seq_len)
Dmitry Safonov68af4312020-03-02 17:51:35 +00003181 return false;
3182
3183 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3184 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3185 port->sysrq_seq = 0;
3186 return false;
3187 }
3188
Andy Shevchenko2ce5eac2020-03-10 19:43:34 +02003189 if (++port->sysrq_seq < sysrq_toggle_seq_len) {
Dmitry Safonov68af4312020-03-02 17:51:35 +00003190 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3191 return true;
3192 }
3193
3194 schedule_work(&sysrq_enable_work);
3195
3196 port->sysrq = 0;
3197 return true;
3198}
Johan Hovold08d54702020-06-10 17:22:31 +02003199EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
Dmitry Safonov68af4312020-03-02 17:51:35 +00003200#endif
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202EXPORT_SYMBOL(uart_write_wakeup);
3203EXPORT_SYMBOL(uart_register_driver);
3204EXPORT_SYMBOL(uart_unregister_driver);
3205EXPORT_SYMBOL(uart_suspend_port);
3206EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207EXPORT_SYMBOL(uart_add_one_port);
3208EXPORT_SYMBOL(uart_remove_one_port);
3209
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003210/**
Lukas Wunner743f93f2017-11-24 23:26:40 +01003211 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
Randy Dunlapa7172562020-06-15 12:24:36 -07003212 * @port: uart device's target port
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003213 *
3214 * This function implements the device tree binding described in
3215 * Documentation/devicetree/bindings/serial/rs485.txt.
3216 */
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02003217int uart_get_rs485_mode(struct uart_port *port)
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003218{
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02003219 struct serial_rs485 *rs485conf = &port->rs485;
3220 struct device *dev = port->dev;
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003221 u32 rs485_delay[2];
3222 int ret;
3223
Lukas Wunner743f93f2017-11-24 23:26:40 +01003224 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3225 rs485_delay, 2);
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003226 if (!ret) {
3227 rs485conf->delay_rts_before_send = rs485_delay[0];
3228 rs485conf->delay_rts_after_send = rs485_delay[1];
3229 } else {
3230 rs485conf->delay_rts_before_send = 0;
3231 rs485conf->delay_rts_after_send = 0;
3232 }
3233
3234 /*
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01003235 * Clear full-duplex and enabled flags, set RTS polarity to active high
3236 * to get to a defined state with the following properties:
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003237 */
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01003238 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
Lukas Wunnerd58a2df2020-05-18 16:45:02 +02003239 SER_RS485_TERMINATE_BUS |
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01003240 SER_RS485_RTS_AFTER_SEND);
3241 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003242
Lukas Wunner743f93f2017-11-24 23:26:40 +01003243 if (device_property_read_bool(dev, "rs485-rx-during-tx"))
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003244 rs485conf->flags |= SER_RS485_RX_DURING_TX;
3245
Lukas Wunner743f93f2017-11-24 23:26:40 +01003246 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003247 rs485conf->flags |= SER_RS485_ENABLED;
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01003248
3249 if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3250 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3251 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3252 }
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02003253
Lukas Wunnerd58a2df2020-05-18 16:45:02 +02003254 /*
3255 * Disabling termination by default is the safe choice: Else if many
3256 * bus participants enable it, no communication is possible at all.
3257 * Works fine for short cables and users may enable for longer cables.
3258 */
3259 port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3260 GPIOD_OUT_LOW);
3261 if (IS_ERR(port->rs485_term_gpio)) {
3262 ret = PTR_ERR(port->rs485_term_gpio);
3263 port->rs485_term_gpio = NULL;
Krzysztof Kozlowski89c65d62020-09-01 17:31:00 +02003264 return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
Lukas Wunnerd58a2df2020-05-18 16:45:02 +02003265 }
3266
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02003267 return 0;
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003268}
Lukas Wunner743f93f2017-11-24 23:26:40 +01003269EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
Uwe Kleine-Königef838a82017-09-13 10:18:27 +02003270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271MODULE_DESCRIPTION("Serial driver core");
3272MODULE_LICENSE("GPL");