blob: 6cbd46c565f8504b43dc4e325e97308ca8199b41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver core for serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/tty.h>
Jiri Slaby027d7da2011-11-09 21:33:43 +010025#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/console.h>
Grant Likelya208ffd2014-03-27 18:29:46 -070029#include <linux/of.h>
Alexey Dobriyand196a942009-03-31 15:19:21 -070030#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/device.h>
33#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
Alan Coxccce6de2009-09-19 13:13:30 -070034#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * This is used to lock changes in serial line configuration.
43 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +000044static DEFINE_MUTEX(port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ingo Molnar13e83592006-07-03 00:25:03 -070046/*
47 * lockdep: port->lock is initialized in two places, but we
48 * want only one lock-class:
49 */
50static struct lock_class_key port_lock_key;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53
Alan Cox19225132010-06-01 22:52:51 +020054static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
Alan Coxa46c9992008-02-08 04:18:53 -080055 struct ktermios *old_termios);
Jiri Slaby1f33a512011-07-14 14:35:10 +020056static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
Linus Walleij6f538fe2012-12-07 11:36:08 +010057static void uart_change_pm(struct uart_state *state,
58 enum uart_pm_state pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slabyb922e192011-11-09 21:33:51 +010060static void uart_port_shutdown(struct tty_port *port);
61
Peter Hurley299245a2014-09-10 15:06:24 -040062static int uart_dcd_enabled(struct uart_port *uport)
63{
Peter Hurleyd4260b52014-10-16 14:19:47 -040064 return !!(uport->status & UPSTAT_DCD_ENABLE);
Peter Hurley299245a2014-09-10 15:06:24 -040065}
66
Peter Hurley9ed19422016-04-09 18:56:34 -070067static inline struct uart_port *uart_port_ref(struct uart_state *state)
68{
69 if (atomic_add_unless(&state->refcount, 1, 0))
70 return state->uart_port;
71 return NULL;
72}
73
74static inline void uart_port_deref(struct uart_port *uport)
75{
76 if (uport && atomic_dec_and_test(&uport->state->refcount))
77 wake_up(&uport->state->remove_wait);
78}
79
80#define uart_port_lock(state, flags) \
81 ({ \
82 struct uart_port *__uport = uart_port_ref(state); \
83 if (__uport) \
84 spin_lock_irqsave(&__uport->lock, flags); \
85 __uport; \
86 })
87
88#define uart_port_unlock(uport, flags) \
89 ({ \
90 struct uart_port *__uport = uport; \
91 if (__uport) \
92 spin_unlock_irqrestore(&__uport->lock, flags); \
93 uart_port_deref(__uport); \
94 })
95
Peter Hurley4047b372016-04-09 18:56:33 -070096static inline struct uart_port *uart_port_check(struct uart_state *state)
97{
Peter Hurley7da4b8b2016-05-03 14:01:51 -070098 lockdep_assert_held(&state->port.mutex);
Peter Hurley4047b372016-04-09 18:56:33 -070099 return state->uart_port;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * This routine is used by the interrupt handler to schedule processing in
104 * the software interrupt portion of the driver.
105 */
106void uart_write_wakeup(struct uart_port *port)
107{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700108 struct uart_state *state = port->state;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800109 /*
110 * This means you called this function _after_ the port was
111 * closed. No cookie for you.
112 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700113 BUG_ON(!state);
Rob Herringd0f4bce2016-10-28 07:07:48 -0500114 tty_port_tty_wakeup(&state->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void uart_stop(struct tty_struct *tty)
118{
119 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700120 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long flags;
122
Peter Hurley9ed19422016-04-09 18:56:34 -0700123 port = uart_port_lock(state, flags);
124 if (port)
125 port->ops->stop_tx(port);
126 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static void __uart_start(struct tty_struct *tty)
130{
131 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700132 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Peter Hurley9ed19422016-04-09 18:56:34 -0700134 if (port && !uart_tx_stopped(port))
Russell Kingb129a8c2005-08-31 10:12:14 +0100135 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138static void uart_start(struct tty_struct *tty)
139{
140 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700141 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long flags;
143
Peter Hurley9ed19422016-04-09 18:56:34 -0700144 port = uart_port_lock(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 __uart_start(tty);
Peter Hurley9ed19422016-04-09 18:56:34 -0700146 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Denys Vlasenkof4581ca2015-10-27 17:40:01 +0100149static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
151{
152 unsigned long flags;
153 unsigned int old;
154
155 spin_lock_irqsave(&port->lock, flags);
156 old = port->mctrl;
157 port->mctrl = (old & ~clear) | set;
158 if (old != port->mctrl)
159 port->ops->set_mctrl(port, port->mctrl);
160 spin_unlock_irqrestore(&port->lock, flags);
161}
162
Alan Coxa46c9992008-02-08 04:18:53 -0800163#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
164#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100168 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100170static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
171 int init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Peter Hurley4047b372016-04-09 18:56:33 -0700173 struct uart_port *uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned long page;
175 int retval = 0;
176
Alan Cox46d57a42009-09-19 13:13:29 -0700177 if (uport->type == PORT_UNKNOWN)
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100178 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200181 * Make sure the device is in D0 state.
182 */
183 uart_change_pm(state, UART_PM_STATE_ON);
184
185 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * Initialise and allocate the transmit and temporary
187 * buffer.
188 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700189 if (!state->xmit.buf) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100190 /* This is protected by the per port mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 page = get_zeroed_page(GFP_KERNEL);
192 if (!page)
193 return -ENOMEM;
194
Alan Coxebd2c8f2009-09-19 13:13:28 -0700195 state->xmit.buf = (unsigned char *) page;
196 uart_circ_clear(&state->xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Alan Cox46d57a42009-09-19 13:13:29 -0700199 retval = uport->ops->startup(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (retval == 0) {
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200201 if (uart_console(uport) && uport->cons->cflag) {
Alan Coxadc8d742012-07-14 15:31:47 +0100202 tty->termios.c_cflag = uport->cons->cflag;
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200203 uport->cons->cflag = 0;
204 }
205 /*
206 * Initialise the hardware port settings.
207 */
208 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Peter Hurley9db276f2016-01-10 20:36:15 -0800210 /*
211 * Setup the RTS and DTR signals once the
212 * port is open and ready to respond.
213 */
214 if (init_hw && C_BAUD(tty))
215 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Jiri Slaby00551972011-08-17 13:48:15 +0200218 /*
219 * This is to allow setserial on this port. People may want to set
220 * port/irq/type and then reconfigure the port properly if it failed
221 * now.
222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (retval && capable(CAP_SYS_ADMIN))
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100224 return 1;
225
226 return retval;
227}
228
229static int uart_startup(struct tty_struct *tty, struct uart_state *state,
230 int init_hw)
231{
232 struct tty_port *port = &state->port;
233 int retval;
234
Peter Hurleyd41861c2016-04-09 17:53:25 -0700235 if (tty_port_initialized(port))
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100236 return 0;
237
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100238 retval = uart_port_startup(tty, state, init_hw);
Rob Herringb3b57642016-08-22 17:39:09 -0500239 if (retval)
240 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 return retval;
243}
244
245/*
246 * This routine will shutdown a serial port; interrupts are disabled, and
247 * DTR is dropped if the hangup on close termio flag is on. Calls to
248 * uart_shutdown are serialised by the per-port semaphore.
Peter Hurleyaf224ca2016-04-09 18:56:35 -0700249 *
250 * uport == NULL if uart_port has already been removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Alan Cox19225132010-06-01 22:52:51 +0200252static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Peter Hurley4047b372016-04-09 18:56:33 -0700254 struct uart_port *uport = uart_port_check(state);
Alan Coxbdc04e32009-09-19 13:13:31 -0700255 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Russell Kingee31b332005-11-13 15:28:51 +0000257 /*
258 * Set the TTY IO error marker
259 */
Alan Coxf7519282009-01-02 13:49:21 +0000260 if (tty)
261 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000262
Peter Hurleyd41861c2016-04-09 17:53:25 -0700263 if (tty_port_initialized(port)) {
264 tty_port_set_initialized(port, 0);
265
Russell Kingee31b332005-11-13 15:28:51 +0000266 /*
267 * Turn off DTR and RTS early.
268 */
Peter Hurleyaf224ca2016-04-09 18:56:35 -0700269 if (uport && uart_console(uport) && tty)
Peter Hurleyae84db92014-07-09 09:21:14 -0400270 uport->cons->cflag = tty->termios.c_cflag;
271
Peter Hurley9db276f2016-01-10 20:36:15 -0800272 if (!tty || C_HUPCL(tty))
Alan Coxccce6de2009-09-19 13:13:30 -0700273 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Russell Kingee31b332005-11-13 15:28:51 +0000274
Jiri Slabyb922e192011-11-09 21:33:51 +0100275 uart_port_shutdown(port);
Russell Kingee31b332005-11-13 15:28:51 +0000276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
Doug Andersond208a3b2011-10-19 11:52:01 -0700279 * It's possible for shutdown to be called after suspend if we get
280 * a DCD drop (hangup) at just the right time. Clear suspended bit so
281 * we don't try to resume a port that has been shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Peter Hurley80f02d52016-04-09 17:53:24 -0700283 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * Free the transmit buffer page.
287 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700288 if (state->xmit.buf) {
289 free_page((unsigned long)state->xmit.buf);
290 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/**
295 * uart_update_timeout - update per-port FIFO timeout.
296 * @port: uart_port structure describing the port
297 * @cflag: termios cflag value
298 * @baud: speed of the port
299 *
300 * Set the port FIFO timeout value. The @cflag value should
301 * reflect the actual hardware settings.
302 */
303void
304uart_update_timeout(struct uart_port *port, unsigned int cflag,
305 unsigned int baud)
306{
307 unsigned int bits;
308
309 /* byte size and parity */
310 switch (cflag & CSIZE) {
311 case CS5:
312 bits = 7;
313 break;
314 case CS6:
315 bits = 8;
316 break;
317 case CS7:
318 bits = 9;
319 break;
320 default:
321 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800322 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
325 if (cflag & CSTOPB)
326 bits++;
327 if (cflag & PARENB)
328 bits++;
329
330 /*
331 * The total number of bits to be transmitted in the fifo.
332 */
333 bits = bits * port->fifosize;
334
335 /*
336 * Figure the timeout to send the above number of bits.
337 * Add .02 seconds of slop
338 */
339 port->timeout = (HZ * bits) / baud + HZ/50;
340}
341
342EXPORT_SYMBOL(uart_update_timeout);
343
344/**
345 * uart_get_baud_rate - return baud rate for a particular port
346 * @port: uart_port structure describing the port in question.
347 * @termios: desired termios settings.
348 * @old: old termios (or NULL)
349 * @min: minimum acceptable baud rate
350 * @max: maximum acceptable baud rate
351 *
352 * Decode the termios structure into a numeric baud rate,
353 * taking account of the magic 38400 baud rate (with spd_*
354 * flags), and mapping the %B0 rate to 9600 baud.
355 *
356 * If the new baud rate is invalid, try the old termios setting.
357 * If it's still invalid, we try 9600 baud.
358 *
359 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700360 * we're actually going to be using. Don't do this for the case
361 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800364uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
365 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Joakim Nordellf10a2232015-06-08 14:56:51 +0200367 unsigned int try;
368 unsigned int baud;
369 unsigned int altbaud;
Alan Coxeb424fd2008-04-28 02:14:07 -0700370 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000371 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Joakim Nordellf10a2232015-06-08 14:56:51 +0200373 switch (flags) {
374 case UPF_SPD_HI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 altbaud = 57600;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200376 break;
377 case UPF_SPD_VHI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 altbaud = 115200;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200379 break;
380 case UPF_SPD_SHI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 altbaud = 230400;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200382 break;
383 case UPF_SPD_WARP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 altbaud = 460800;
Joakim Nordellf10a2232015-06-08 14:56:51 +0200385 break;
386 default:
387 altbaud = 38400;
388 break;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 for (try = 0; try < 2; try++) {
392 baud = tty_termios_baud_rate(termios);
393
394 /*
395 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
396 * Die! Die! Die!
397 */
Peter Hurley547039e2014-10-16 13:46:38 -0400398 if (try == 0 && baud == 38400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 baud = altbaud;
400
401 /*
402 * Special case: B0 rate.
403 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700404 if (baud == 0) {
405 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (baud >= min && baud <= max)
410 return baud;
411
412 /*
413 * Oops, the quotient was zero. Try again with
414 * the old baud rate if possible.
415 */
416 termios->c_cflag &= ~CBAUD;
417 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800418 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700419 if (!hung_up)
420 tty_termios_encode_baud_rate(termios,
421 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 old = NULL;
423 continue;
424 }
425
426 /*
Alan Cox16ae2a82010-01-04 16:26:21 +0000427 * As a last resort, if the range cannot be met then clip to
428 * the nearest chip supported rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Alan Cox16ae2a82010-01-04 16:26:21 +0000430 if (!hung_up) {
431 if (baud <= min)
432 tty_termios_encode_baud_rate(termios,
433 min + 1, min + 1);
434 else
435 tty_termios_encode_baud_rate(termios,
436 max - 1, max - 1);
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Alan Cox16ae2a82010-01-04 16:26:21 +0000439 /* Should never happen */
440 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
444EXPORT_SYMBOL(uart_get_baud_rate);
445
446/**
447 * uart_get_divisor - return uart clock divisor
448 * @port: uart_port structure describing the port.
449 * @baud: desired baud rate
450 *
451 * Calculate the uart clock divisor for the port.
452 */
453unsigned int
454uart_get_divisor(struct uart_port *port, unsigned int baud)
455{
456 unsigned int quot;
457
458 /*
459 * Old custom speed handling.
460 */
461 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
462 quot = port->custom_divisor;
463 else
Uwe Kleine-König97d24632011-12-20 11:47:44 +0100464 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return quot;
467}
468
469EXPORT_SYMBOL(uart_get_divisor);
470
Peter Hurley7c8ab962014-10-16 16:54:20 -0400471/* Caller holds port mutex */
Alan Cox19225132010-06-01 22:52:51 +0200472static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
473 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Peter Hurley4047b372016-04-09 18:56:33 -0700475 struct uart_port *uport = uart_port_check(state);
Alan Cox606d0992006-12-08 02:38:45 -0800476 struct ktermios *termios;
Peter Hurley391f93f2015-01-25 14:44:51 -0500477 int hw_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /*
480 * If we have no tty, termios, or the port does not exist,
481 * then we can't set the parameters for this port.
482 */
Alan Coxadc8d742012-07-14 15:31:47 +0100483 if (!tty || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
485
Alan Coxadc8d742012-07-14 15:31:47 +0100486 termios = &tty->termios;
Peter Hurleyc18b55f2014-06-16 09:17:09 -0400487 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /*
Peter Hurley299245a2014-09-10 15:06:24 -0400490 * Set modem status enables based on termios cflag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Peter Hurley299245a2014-09-10 15:06:24 -0400492 spin_lock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (termios->c_cflag & CRTSCTS)
Peter Hurley299245a2014-09-10 15:06:24 -0400494 uport->status |= UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 else
Peter Hurley299245a2014-09-10 15:06:24 -0400496 uport->status &= ~UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (termios->c_cflag & CLOCAL)
Peter Hurley299245a2014-09-10 15:06:24 -0400499 uport->status &= ~UPSTAT_DCD_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 else
Peter Hurley299245a2014-09-10 15:06:24 -0400501 uport->status |= UPSTAT_DCD_ENABLE;
Peter Hurley391f93f2015-01-25 14:44:51 -0500502
503 /* reset sw-assisted CTS flow control based on (possibly) new mode */
504 hw_stopped = uport->hw_stopped;
505 uport->hw_stopped = uart_softcts_mode(uport) &&
506 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
507 if (uport->hw_stopped) {
508 if (!hw_stopped)
509 uport->ops->stop_tx(uport);
510 } else {
511 if (hw_stopped)
512 __uart_start(tty);
513 }
Peter Hurley299245a2014-09-10 15:06:24 -0400514 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800517static int uart_put_char(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800519 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700520 struct uart_port *port;
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800521 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700523 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Peter Hurleyf5291ec2016-01-10 20:23:55 -0800525 circ = &state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Peter Hurley9ed19422016-04-09 18:56:34 -0700529 port = uart_port_lock(state, flags);
530 if (port && uart_circ_chars_free(circ) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 circ->buf[circ->head] = c;
532 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700533 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Peter Hurley9ed19422016-04-09 18:56:34 -0700535 uart_port_unlock(port, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static void uart_flush_chars(struct tty_struct *tty)
540{
541 uart_start(tty);
542}
543
Alan Cox19225132010-06-01 22:52:51 +0200544static int uart_write(struct tty_struct *tty,
545 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800548 struct uart_port *port;
549 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unsigned long flags;
551 int c, ret = 0;
552
Pavel Machekd5f735e2006-03-07 21:55:20 -0800553 /*
554 * This means you called this function _after_ the port was
555 * closed. No cookie for you.
556 */
Alan Coxf7519282009-01-02 13:49:21 +0000557 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800558 WARN_ON(1);
559 return -EL3HLT;
560 }
561
Alan Coxebd2c8f2009-09-19 13:13:28 -0700562 circ = &state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!circ->buf)
564 return 0;
565
Peter Hurley9ed19422016-04-09 18:56:34 -0700566 port = uart_port_lock(state, flags);
567 while (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
569 if (count < c)
570 c = count;
571 if (c <= 0)
572 break;
573 memcpy(circ->buf + circ->head, buf, c);
574 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
575 buf += c;
576 count -= c;
577 ret += c;
578 }
Peter Hurley64dbee32014-10-16 16:54:26 -0400579
580 __uart_start(tty);
Peter Hurley9ed19422016-04-09 18:56:34 -0700581 uart_port_unlock(port, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
585static int uart_write_room(struct tty_struct *tty)
586{
587 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700588 struct uart_port *port;
Alan Coxf34d7a52008-04-30 00:54:13 -0700589 unsigned long flags;
590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Peter Hurley9ed19422016-04-09 18:56:34 -0700592 port = uart_port_lock(state, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700593 ret = uart_circ_chars_free(&state->xmit);
Peter Hurley9ed19422016-04-09 18:56:34 -0700594 uart_port_unlock(port, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700595 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static int uart_chars_in_buffer(struct tty_struct *tty)
599{
600 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700601 struct uart_port *port;
Alan Coxf34d7a52008-04-30 00:54:13 -0700602 unsigned long flags;
603 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Peter Hurley9ed19422016-04-09 18:56:34 -0700605 port = uart_port_lock(state, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700606 ret = uart_circ_chars_pending(&state->xmit);
Peter Hurley9ed19422016-04-09 18:56:34 -0700607 uart_port_unlock(port, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700608 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611static void uart_flush_buffer(struct tty_struct *tty)
612{
613 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700614 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long flags;
616
Pavel Machekd5f735e2006-03-07 21:55:20 -0800617 /*
618 * This means you called this function _after_ the port was
619 * closed. No cookie for you.
620 */
Alan Coxf7519282009-01-02 13:49:21 +0000621 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800622 WARN_ON(1);
623 return;
624 }
625
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700626 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Peter Hurley9ed19422016-04-09 18:56:34 -0700628 port = uart_port_lock(state, flags);
629 if (!port)
630 return;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700631 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100632 if (port->ops->flush_buffer)
633 port->ops->flush_buffer(port);
Peter Hurley9ed19422016-04-09 18:56:34 -0700634 uart_port_unlock(port, flags);
Rob Herringd0f4bce2016-10-28 07:07:48 -0500635 tty_port_tty_wakeup(&state->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
639 * This function is used to send a high-priority XON/XOFF character to
640 * the device
641 */
642static void uart_send_xchar(struct tty_struct *tty, char ch)
643{
644 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700645 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned long flags;
647
Peter Hurley9ed19422016-04-09 18:56:34 -0700648 port = uart_port_ref(state);
649 if (!port)
650 return;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (port->ops->send_xchar)
653 port->ops->send_xchar(port, ch);
654 else {
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400655 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 port->x_char = ch;
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400657 if (ch)
Russell Kingb129a8c2005-08-31 10:12:14 +0100658 port->ops->start_tx(port);
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400659 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Peter Hurley9ed19422016-04-09 18:56:34 -0700661 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void uart_throttle(struct tty_struct *tty)
665{
666 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700667 struct uart_port *port;
Peter Hurley391f93f2015-01-25 14:44:51 -0500668 upstat_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Peter Hurley9ed19422016-04-09 18:56:34 -0700670 port = uart_port_ref(state);
671 if (!port)
672 return;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (I_IXOFF(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500675 mask |= UPSTAT_AUTOXOFF;
Peter Hurley9db276f2016-01-10 20:36:15 -0800676 if (C_CRTSCTS(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500677 mask |= UPSTAT_AUTORTS;
Russell King9aba8d5b2012-04-17 17:23:14 +0100678
Peter Hurley391f93f2015-01-25 14:44:51 -0500679 if (port->status & mask) {
Russell King9aba8d5b2012-04-17 17:23:14 +0100680 port->ops->throttle(port);
Peter Hurley391f93f2015-01-25 14:44:51 -0500681 mask &= ~port->status;
Russell King9aba8d5b2012-04-17 17:23:14 +0100682 }
683
Peter Hurley391f93f2015-01-25 14:44:51 -0500684 if (mask & UPSTAT_AUTORTS)
Russell King9aba8d5b2012-04-17 17:23:14 +0100685 uart_clear_mctrl(port, TIOCM_RTS);
Peter Hurleyb4749b92016-01-10 20:24:02 -0800686
687 if (mask & UPSTAT_AUTOXOFF)
688 uart_send_xchar(tty, STOP_CHAR(tty));
Peter Hurley9ed19422016-04-09 18:56:34 -0700689
690 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static void uart_unthrottle(struct tty_struct *tty)
694{
695 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -0700696 struct uart_port *port;
Peter Hurley391f93f2015-01-25 14:44:51 -0500697 upstat_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Peter Hurley9ed19422016-04-09 18:56:34 -0700699 port = uart_port_ref(state);
700 if (!port)
701 return;
702
Russell King9aba8d5b2012-04-17 17:23:14 +0100703 if (I_IXOFF(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500704 mask |= UPSTAT_AUTOXOFF;
Peter Hurley9db276f2016-01-10 20:36:15 -0800705 if (C_CRTSCTS(tty))
Peter Hurley391f93f2015-01-25 14:44:51 -0500706 mask |= UPSTAT_AUTORTS;
Russell King9aba8d5b2012-04-17 17:23:14 +0100707
Peter Hurley391f93f2015-01-25 14:44:51 -0500708 if (port->status & mask) {
Russell King9aba8d5b2012-04-17 17:23:14 +0100709 port->ops->unthrottle(port);
Peter Hurley391f93f2015-01-25 14:44:51 -0500710 mask &= ~port->status;
Russell King9aba8d5b2012-04-17 17:23:14 +0100711 }
712
Peter Hurley391f93f2015-01-25 14:44:51 -0500713 if (mask & UPSTAT_AUTORTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 uart_set_mctrl(port, TIOCM_RTS);
Peter Hurleyb4749b92016-01-10 20:24:02 -0800715
716 if (mask & UPSTAT_AUTOXOFF)
717 uart_send_xchar(tty, START_CHAR(tty));
Peter Hurley9ed19422016-04-09 18:56:34 -0700718
719 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Peter Hurley4047b372016-04-09 18:56:33 -0700722static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Alan Cox9f109692012-10-29 15:20:25 +0000724 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley4047b372016-04-09 18:56:33 -0700725 struct uart_port *uport;
726 int ret = -ENODEV;
Alan Cox7ba2e762012-09-04 16:34:45 +0100727
Fengguang Wu37cd0c92012-09-06 10:27:51 +0800728 memset(retinfo, 0, sizeof(*retinfo));
Alan Cox7ba2e762012-09-04 16:34:45 +0100729
Peter Hurley3abe8c72016-01-10 20:23:56 -0800730 /*
731 * Ensure the state we copy is consistent and no hardware changes
732 * occur as we go
733 */
734 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -0700735 uport = uart_port_check(state);
736 if (!uport)
737 goto out;
738
Alan Cox7ba2e762012-09-04 16:34:45 +0100739 retinfo->type = uport->type;
740 retinfo->line = uport->line;
741 retinfo->port = uport->iobase;
742 if (HIGH_BITS_OFFSET)
743 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
744 retinfo->irq = uport->irq;
745 retinfo->flags = uport->flags;
746 retinfo->xmit_fifo_size = uport->fifosize;
747 retinfo->baud_base = uport->uartclk / 16;
748 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
749 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
750 ASYNC_CLOSING_WAIT_NONE :
751 jiffies_to_msecs(port->closing_wait) / 10;
752 retinfo->custom_divisor = uport->custom_divisor;
753 retinfo->hub6 = uport->hub6;
754 retinfo->io_type = uport->iotype;
755 retinfo->iomem_reg_shift = uport->regshift;
756 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
Peter Hurley4047b372016-04-09 18:56:33 -0700757
758 ret = 0;
759out:
Alan Coxa2bceae2009-09-19 13:13:31 -0700760 mutex_unlock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -0700761 return ret;
Alan Cox9f109692012-10-29 15:20:25 +0000762}
763
764static int uart_get_info_user(struct tty_port *port,
765 struct serial_struct __user *retinfo)
766{
767 struct serial_struct tmp;
Peter Hurley3abe8c72016-01-10 20:23:56 -0800768
Peter Hurley4047b372016-04-09 18:56:33 -0700769 if (uart_get_info(port, &tmp) < 0)
770 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
773 return -EFAULT;
774 return 0;
775}
776
Alan Cox7ba2e762012-09-04 16:34:45 +0100777static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
778 struct uart_state *state,
779 struct serial_struct *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Peter Hurley4047b372016-04-09 18:56:33 -0700781 struct uart_port *uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000783 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000785 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int retval = 0;
787
Peter Hurley4047b372016-04-09 18:56:33 -0700788 if (!uport)
789 return -EIO;
790
Alan Cox7ba2e762012-09-04 16:34:45 +0100791 new_port = new_info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (HIGH_BITS_OFFSET)
Alan Cox7ba2e762012-09-04 16:34:45 +0100793 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Alan Cox7ba2e762012-09-04 16:34:45 +0100795 new_info->irq = irq_canonicalize(new_info->irq);
796 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
797 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Jiri Slaby4cb0fbf2011-11-09 21:33:45 +0100798 ASYNC_CLOSING_WAIT_NONE :
Alan Cox7ba2e762012-09-04 16:34:45 +0100799 msecs_to_jiffies(new_info->closing_wait * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Alan Cox46d57a42009-09-19 13:13:29 -0700802 change_irq = !(uport->flags & UPF_FIXED_PORT)
Alan Cox7ba2e762012-09-04 16:34:45 +0100803 && new_info->irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 * Since changing the 'type' of the port changes its resource
807 * allocations, we should treat type changes the same as
808 * IO port changes.
809 */
Alan Cox46d57a42009-09-19 13:13:29 -0700810 change_port = !(uport->flags & UPF_FIXED_PORT)
811 && (new_port != uport->iobase ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100812 (unsigned long)new_info->iomem_base != uport->mapbase ||
813 new_info->hub6 != uport->hub6 ||
814 new_info->io_type != uport->iotype ||
815 new_info->iomem_reg_shift != uport->regshift ||
816 new_info->type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Alan Cox46d57a42009-09-19 13:13:29 -0700818 old_flags = uport->flags;
Alan Cox7ba2e762012-09-04 16:34:45 +0100819 new_flags = new_info->flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700820 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!capable(CAP_SYS_ADMIN)) {
823 retval = -EPERM;
824 if (change_irq || change_port ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100825 (new_info->baud_base != uport->uartclk / 16) ||
Alan Cox46d57a42009-09-19 13:13:29 -0700826 (close_delay != port->close_delay) ||
827 (closing_wait != port->closing_wait) ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100828 (new_info->xmit_fifo_size &&
829 new_info->xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000830 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700832 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000833 (new_flags & UPF_USR_MASK));
Alan Cox7ba2e762012-09-04 16:34:45 +0100834 uport->custom_divisor = new_info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto check_and_exit;
836 }
837
838 /*
839 * Ask the low level driver to verify the settings.
840 */
Alan Cox46d57a42009-09-19 13:13:29 -0700841 if (uport->ops->verify_port)
Alan Cox7ba2e762012-09-04 16:34:45 +0100842 retval = uport->ops->verify_port(uport, new_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Alan Cox7ba2e762012-09-04 16:34:45 +0100844 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
845 (new_info->baud_base < 9600))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 retval = -EINVAL;
847
848 if (retval)
849 goto exit;
850
851 if (change_port || change_irq) {
852 retval = -EBUSY;
853
854 /*
855 * Make sure that we are the sole user of this port.
856 */
Alan Coxb58d13a2009-09-19 13:13:32 -0700857 if (tty_port_users(port) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto exit;
859
860 /*
861 * We need to shutdown the serial port at the old
862 * port/type/irq combination.
863 */
Alan Cox19225132010-06-01 22:52:51 +0200864 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 if (change_port) {
868 unsigned long old_iobase, old_mapbase;
869 unsigned int old_type, old_iotype, old_hub6, old_shift;
870
Alan Cox46d57a42009-09-19 13:13:29 -0700871 old_iobase = uport->iobase;
872 old_mapbase = uport->mapbase;
873 old_type = uport->type;
874 old_hub6 = uport->hub6;
875 old_iotype = uport->iotype;
876 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
879 * Free and release old regions
880 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -0300881 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
Alan Cox46d57a42009-09-19 13:13:29 -0700882 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alan Cox46d57a42009-09-19 13:13:29 -0700884 uport->iobase = new_port;
Alan Cox7ba2e762012-09-04 16:34:45 +0100885 uport->type = new_info->type;
886 uport->hub6 = new_info->hub6;
887 uport->iotype = new_info->io_type;
888 uport->regshift = new_info->iomem_reg_shift;
889 uport->mapbase = (unsigned long)new_info->iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Claim and map the new regions
893 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -0300894 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
Alan Cox46d57a42009-09-19 13:13:29 -0700895 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 /* Always success - Jean II */
898 retval = 0;
899 }
900
901 /*
902 * If we fail to request resources for the
903 * new port, try to restore the old settings.
904 */
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200905 if (retval) {
Alan Cox46d57a42009-09-19 13:13:29 -0700906 uport->iobase = old_iobase;
907 uport->type = old_type;
908 uport->hub6 = old_hub6;
909 uport->iotype = old_iotype;
910 uport->regshift = old_shift;
911 uport->mapbase = old_mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200913 if (old_type != PORT_UNKNOWN) {
914 retval = uport->ops->request_port(uport);
915 /*
916 * If we failed to restore the old settings,
917 * we fail like this.
918 */
919 if (retval)
920 uport->type = PORT_UNKNOWN;
921
922 /*
923 * We failed anyway.
924 */
925 retval = -EBUSY;
926 }
927
Alan Coxa46c9992008-02-08 04:18:53 -0800928 /* Added to return the correct error -Ram Gupta */
929 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 }
932
David Gibsonabb4a232007-05-06 14:48:49 -0700933 if (change_irq)
Alan Cox7ba2e762012-09-04 16:34:45 +0100934 uport->irq = new_info->irq;
Alan Cox46d57a42009-09-19 13:13:29 -0700935 if (!(uport->flags & UPF_FIXED_PORT))
Alan Cox7ba2e762012-09-04 16:34:45 +0100936 uport->uartclk = new_info->baud_base * 16;
Alan Cox46d57a42009-09-19 13:13:29 -0700937 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000938 (new_flags & UPF_CHANGE_MASK);
Alan Cox7ba2e762012-09-04 16:34:45 +0100939 uport->custom_divisor = new_info->custom_divisor;
Alan Cox46d57a42009-09-19 13:13:29 -0700940 port->close_delay = close_delay;
941 port->closing_wait = closing_wait;
Alan Cox7ba2e762012-09-04 16:34:45 +0100942 if (new_info->xmit_fifo_size)
943 uport->fifosize = new_info->xmit_fifo_size;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100944 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 check_and_exit:
947 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700948 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto exit;
Peter Hurleyd41861c2016-04-09 17:53:25 -0700950 if (tty_port_initialized(port)) {
Alan Cox46d57a42009-09-19 13:13:29 -0700951 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
952 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /*
954 * If they're setting up a custom divisor or speed,
955 * instead of clearing it, then bitch about it. No
956 * need to rate-limit; it's CAP_SYS_ADMIN only.
957 */
Alan Cox46d57a42009-09-19 13:13:29 -0700958 if (uport->flags & UPF_SPD_MASK) {
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +0530959 dev_notice(uport->dev,
960 "%s sets custom speed on %s. This is deprecated.\n",
961 current->comm,
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200962 tty_name(port->tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Alan Cox19225132010-06-01 22:52:51 +0200964 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Rob Herringb3b57642016-08-22 17:39:09 -0500966 } else {
Alan Cox19225132010-06-01 22:52:51 +0200967 retval = uart_startup(tty, state, 1);
Sebastian Andrzej Siewiorffcf1672018-01-11 18:57:26 +0100968 if (retval == 0)
969 tty_port_set_initialized(port, true);
Rob Herringb3b57642016-08-22 17:39:09 -0500970 if (retval > 0)
971 retval = 0;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 exit:
Alan Cox7ba2e762012-09-04 16:34:45 +0100974 return retval;
975}
976
977static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
978 struct serial_struct __user *newinfo)
979{
980 struct serial_struct new_serial;
981 struct tty_port *port = &state->port;
982 int retval;
983
984 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
985 return -EFAULT;
986
987 /*
988 * This semaphore protects port->count. It is also
989 * very useful to prevent opens. Also, take the
990 * port configuration semaphore to make sure that a
991 * module insertion/removal doesn't change anything
992 * under us.
993 */
994 mutex_lock(&port->mutex);
995 retval = uart_set_info(tty, port, state, &new_serial);
Alan Coxa2bceae2009-09-19 13:13:31 -0700996 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return retval;
998}
999
Alan Cox19225132010-06-01 22:52:51 +02001000/**
1001 * uart_get_lsr_info - get line status register info
1002 * @tty: tty associated with the UART
1003 * @state: UART being queried
1004 * @value: returned modem value
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Alan Cox19225132010-06-01 22:52:51 +02001006static int uart_get_lsr_info(struct tty_struct *tty,
1007 struct uart_state *state, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Peter Hurley4047b372016-04-09 18:56:33 -07001009 struct uart_port *uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned int result;
1011
Alan Cox46d57a42009-09-19 13:13:29 -07001012 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
1015 * If we're about to load something into the transmit
1016 * register, we'll pretend the transmitter isn't empty to
1017 * avoid a race condition (depending on when the transmit
1018 * interrupt happens).
1019 */
Alan Cox46d57a42009-09-19 13:13:29 -07001020 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -07001021 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Peter Hurleyd01f4d12014-09-10 15:06:26 -04001022 !uart_tx_stopped(uport)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -08001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return put_user(result, value);
1026}
1027
Alan Cox60b33c12011-02-14 16:26:14 +00001028static int uart_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001031 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001032 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int result = -EIO;
1034
Alan Coxa2bceae2009-09-19 13:13:31 -07001035 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001036 uport = uart_port_check(state);
1037 if (!uport)
1038 goto out;
1039
Peter Hurley18900ca2016-04-09 17:06:48 -07001040 if (!tty_io_error(tty)) {
Alan Cox46d57a42009-09-19 13:13:29 -07001041 result = uport->mctrl;
Alan Cox46d57a42009-09-19 13:13:29 -07001042 spin_lock_irq(&uport->lock);
1043 result |= uport->ops->get_mctrl(uport);
1044 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Peter Hurley4047b372016-04-09 18:56:33 -07001046out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001047 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return result;
1049}
1050
1051static int
Alan Cox20b9d172011-02-14 16:26:50 +00001052uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001055 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001056 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int ret = -EIO;
1058
Alan Coxa2bceae2009-09-19 13:13:31 -07001059 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001060 uport = uart_port_check(state);
1061 if (!uport)
1062 goto out;
1063
Peter Hurley18900ca2016-04-09 17:06:48 -07001064 if (!tty_io_error(tty)) {
Alan Cox46d57a42009-09-19 13:13:29 -07001065 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ret = 0;
1067 }
Peter Hurley4047b372016-04-09 18:56:33 -07001068out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001069 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return ret;
1071}
1072
Alan Cox9e989662008-07-22 11:18:03 +01001073static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001076 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001077 struct uart_port *uport;
1078 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Alan Coxa2bceae2009-09-19 13:13:31 -07001080 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001081 uport = uart_port_check(state);
1082 if (!uport)
1083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Alan Cox46d57a42009-09-19 13:13:29 -07001085 if (uport->type != PORT_UNKNOWN)
1086 uport->ops->break_ctl(uport, break_state);
Peter Hurley4047b372016-04-09 18:56:33 -07001087 ret = 0;
1088out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001089 mutex_unlock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001090 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Alan Cox19225132010-06-01 22:52:51 +02001093static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Alan Coxa2bceae2009-09-19 13:13:31 -07001095 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001096 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int flags, ret;
1098
1099 if (!capable(CAP_SYS_ADMIN))
1100 return -EPERM;
1101
1102 /*
1103 * Take the per-port semaphore. This prevents count from
1104 * changing, and hence any extra opens of the port while
1105 * we're auto-configuring.
1106 */
Alan Coxa2bceae2009-09-19 13:13:31 -07001107 if (mutex_lock_interruptible(&port->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -ERESTARTSYS;
1109
Peter Hurley4047b372016-04-09 18:56:33 -07001110 uport = uart_port_check(state);
1111 if (!uport) {
1112 ret = -EIO;
1113 goto out;
1114 }
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ret = -EBUSY;
Alan Coxb58d13a2009-09-19 13:13:32 -07001117 if (tty_port_users(port) == 1) {
Alan Cox19225132010-06-01 22:52:51 +02001118 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /*
1121 * If we already have a port type configured,
1122 * we must release its resources.
1123 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -03001124 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
Alan Cox46d57a42009-09-19 13:13:29 -07001125 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -07001128 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 flags |= UART_CONFIG_IRQ;
1130
1131 /*
1132 * This will claim the ports resources if
1133 * a port is found.
1134 */
Alan Cox46d57a42009-09-19 13:13:29 -07001135 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alan Cox19225132010-06-01 22:52:51 +02001137 ret = uart_startup(tty, state, 1);
Sebastian Andrzej Siewiorfbe1ad92018-02-03 12:27:23 +01001138 if (ret == 0)
1139 tty_port_set_initialized(port, true);
Rob Herringb3b57642016-08-22 17:39:09 -05001140 if (ret > 0)
1141 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Peter Hurley4047b372016-04-09 18:56:33 -07001143out:
Alan Coxa2bceae2009-09-19 13:13:31 -07001144 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return ret;
1146}
1147
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001148static void uart_enable_ms(struct uart_port *uport)
1149{
1150 /*
1151 * Force modem status interrupts on
1152 */
1153 if (uport->ops->enable_ms)
1154 uport->ops->enable_ms(uport);
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1159 * - mask passed in arg for lines of interest
1160 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1161 * Caller should use TIOCGICOUNT to see which one it was
Alan Coxbdc04e32009-09-19 13:13:31 -07001162 *
1163 * FIXME: This wants extracting into a common all driver implementation
1164 * of TIOCMWAIT using tty_port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Peter Hurley9ed19422016-04-09 18:56:34 -07001166static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Peter Hurley9ed19422016-04-09 18:56:34 -07001168 struct uart_port *uport;
Alan Coxbdc04e32009-09-19 13:13:31 -07001169 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 DECLARE_WAITQUEUE(wait, current);
1171 struct uart_icount cprev, cnow;
1172 int ret;
1173
1174 /*
1175 * note the counters on entry
1176 */
Peter Hurley9ed19422016-04-09 18:56:34 -07001177 uport = uart_port_ref(state);
1178 if (!uport)
1179 return -EIO;
Alan Cox46d57a42009-09-19 13:13:29 -07001180 spin_lock_irq(&uport->lock);
1181 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001182 uart_enable_ms(uport);
Alan Cox46d57a42009-09-19 13:13:29 -07001183 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Alan Coxbdc04e32009-09-19 13:13:31 -07001185 add_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001187 spin_lock_irq(&uport->lock);
1188 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1189 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 set_current_state(TASK_INTERRUPTIBLE);
1192
1193 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1194 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1195 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1196 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001197 ret = 0;
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
1201 schedule();
1202
1203 /* see if a signal did it */
1204 if (signal_pending(current)) {
1205 ret = -ERESTARTSYS;
1206 break;
1207 }
1208
1209 cprev = cnow;
1210 }
Fabian Frederick97f9f702015-02-20 19:12:57 +01001211 __set_current_state(TASK_RUNNING);
Alan Coxbdc04e32009-09-19 13:13:31 -07001212 remove_wait_queue(&port->delta_msr_wait, &wait);
Peter Hurley9ed19422016-04-09 18:56:34 -07001213 uart_port_deref(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 return ret;
1216}
1217
1218/*
1219 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1220 * Return: write counters to the user passed counter struct
1221 * NB: both 1->0 and 0->1 transitions are counted except for
1222 * RI where only 0->1 is counted.
1223 */
Alan Coxd281da72010-09-16 18:21:24 +01001224static int uart_get_icount(struct tty_struct *tty,
1225 struct serial_icounter_struct *icount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Alan Coxd281da72010-09-16 18:21:24 +01001227 struct uart_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct uart_icount cnow;
Peter Hurley9ed19422016-04-09 18:56:34 -07001229 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Peter Hurley9ed19422016-04-09 18:56:34 -07001231 uport = uart_port_ref(state);
1232 if (!uport)
1233 return -EIO;
Alan Cox46d57a42009-09-19 13:13:29 -07001234 spin_lock_irq(&uport->lock);
1235 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1236 spin_unlock_irq(&uport->lock);
Peter Hurley9ed19422016-04-09 18:56:34 -07001237 uart_port_deref(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alan Coxd281da72010-09-16 18:21:24 +01001239 icount->cts = cnow.cts;
1240 icount->dsr = cnow.dsr;
1241 icount->rng = cnow.rng;
1242 icount->dcd = cnow.dcd;
1243 icount->rx = cnow.rx;
1244 icount->tx = cnow.tx;
1245 icount->frame = cnow.frame;
1246 icount->overrun = cnow.overrun;
1247 icount->parity = cnow.parity;
1248 icount->brk = cnow.brk;
1249 icount->buf_overrun = cnow.buf_overrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Alan Coxd281da72010-09-16 18:21:24 +01001251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001254static int uart_get_rs485_config(struct uart_port *port,
1255 struct serial_rs485 __user *rs485)
1256{
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001257 unsigned long flags;
1258 struct serial_rs485 aux;
1259
1260 spin_lock_irqsave(&port->lock, flags);
1261 aux = port->rs485;
1262 spin_unlock_irqrestore(&port->lock, flags);
1263
1264 if (copy_to_user(rs485, &aux, sizeof(aux)))
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001265 return -EFAULT;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001266
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001267 return 0;
1268}
1269
1270static int uart_set_rs485_config(struct uart_port *port,
1271 struct serial_rs485 __user *rs485_user)
1272{
1273 struct serial_rs485 rs485;
1274 int ret;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001275 unsigned long flags;
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001276
1277 if (!port->rs485_config)
1278 return -ENOIOCTLCMD;
1279
1280 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1281 return -EFAULT;
1282
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001283 spin_lock_irqsave(&port->lock, flags);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001284 ret = port->rs485_config(port, &rs485);
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01001285 spin_unlock_irqrestore(&port->lock, flags);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001286 if (ret)
1287 return ret;
1288
1289 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1290 return -EFAULT;
1291
1292 return 0;
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
Alan Coxe5238442008-04-30 00:53:28 -07001296 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298static int
Peter Hurley4047b372016-04-09 18:56:33 -07001299uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001302 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07001303 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 void __user *uarg = (void __user *)arg;
1305 int ret = -ENOIOCTLCMD;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
1309 * These ioctls don't rely on the hardware to be present.
1310 */
1311 switch (cmd) {
1312 case TIOCGSERIAL:
Alan Cox9f109692012-10-29 15:20:25 +00001313 ret = uart_get_info_user(port, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
1315
1316 case TIOCSSERIAL:
Peter Hurley7c8ab962014-10-16 16:54:20 -04001317 down_write(&tty->termios_rwsem);
Alan Cox7ba2e762012-09-04 16:34:45 +01001318 ret = uart_set_info_user(tty, state, uarg);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001319 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321
1322 case TIOCSERCONFIG:
Peter Hurley7c8ab962014-10-16 16:54:20 -04001323 down_write(&tty->termios_rwsem);
Alan Cox19225132010-06-01 22:52:51 +02001324 ret = uart_do_autoconfig(tty, state);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001325 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
1327
1328 case TIOCSERGWILD: /* obsolete */
1329 case TIOCSERSWILD: /* obsolete */
1330 ret = 0;
1331 break;
1332 }
1333
1334 if (ret != -ENOIOCTLCMD)
1335 goto out;
1336
Peter Hurley18900ca2016-04-09 17:06:48 -07001337 if (tty_io_error(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ret = -EIO;
1339 goto out;
1340 }
1341
1342 /*
1343 * The following should only be used when hardware is present.
1344 */
1345 switch (cmd) {
1346 case TIOCMIWAIT:
1347 ret = uart_wait_modem_status(state, arg);
1348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 if (ret != -ENOIOCTLCMD)
1352 goto out;
1353
Alan Coxa2bceae2009-09-19 13:13:31 -07001354 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07001355 uport = uart_port_check(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Peter Hurley4047b372016-04-09 18:56:33 -07001357 if (!uport || tty_io_error(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 ret = -EIO;
1359 goto out_up;
1360 }
1361
1362 /*
1363 * All these rely on hardware being present and need to be
1364 * protected against the tty being hung up.
1365 */
Ricardo Ribalda Delgadoa9c20a92014-11-06 09:22:59 +01001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 switch (cmd) {
Ricardo Ribalda Delgadoa9c20a92014-11-06 09:22:59 +01001368 case TIOCSERGETLSR: /* Get line status register */
1369 ret = uart_get_lsr_info(tty, state, uarg);
1370 break;
1371
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001372 case TIOCGRS485:
Peter Hurley4047b372016-04-09 18:56:33 -07001373 ret = uart_get_rs485_config(uport, uarg);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001374 break;
1375
1376 case TIOCSRS485:
Peter Hurley4047b372016-04-09 18:56:33 -07001377 ret = uart_set_rs485_config(uport, uarg);
Ricardo Ribalda Delgadoa5f276f2014-11-06 22:46:13 +01001378 break;
Peter Hurley4047b372016-04-09 18:56:33 -07001379 default:
Alan Cox46d57a42009-09-19 13:13:29 -07001380 if (uport->ops->ioctl)
1381 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001384out_up:
Alan Coxa2bceae2009-09-19 13:13:31 -07001385 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001386out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return ret;
1388}
1389
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001390static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001391{
1392 struct uart_state *state = tty->driver_data;
Peter Hurley4047b372016-04-09 18:56:33 -07001393 struct uart_port *uport;
Alan Cox64e91592008-06-03 15:18:54 +01001394
Peter Hurley4047b372016-04-09 18:56:33 -07001395 mutex_lock(&state->port.mutex);
1396 uport = uart_port_check(state);
1397 if (uport && uport->ops->set_ldisc)
Peter Hurley732a84a2014-11-05 13:11:43 -05001398 uport->ops->set_ldisc(uport, &tty->termios);
Peter Hurley4047b372016-04-09 18:56:33 -07001399 mutex_unlock(&state->port.mutex);
Alan Cox64e91592008-06-03 15:18:54 +01001400}
1401
Alan Coxa46c9992008-02-08 04:18:53 -08001402static void uart_set_termios(struct tty_struct *tty,
1403 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct uart_state *state = tty->driver_data;
Peter Hurley4047b372016-04-09 18:56:33 -07001406 struct uart_port *uport;
Alan Coxadc8d742012-07-14 15:31:47 +01001407 unsigned int cflag = tty->termios.c_cflag;
Russell King2cbacaf2012-04-17 16:34:13 +01001408 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1409 bool sw_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Peter Hurley4047b372016-04-09 18:56:33 -07001411 mutex_lock(&state->port.mutex);
1412 uport = uart_port_check(state);
1413 if (!uport)
1414 goto out;
1415
Russell King2cbacaf2012-04-17 16:34:13 +01001416 /*
1417 * Drivers doing software flow control also need to know
1418 * about changes to these input settings.
1419 */
1420 if (uport->flags & UPF_SOFT_FLOW) {
1421 iflag_mask |= IXANY|IXON|IXOFF;
1422 sw_changed =
1423 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1424 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /*
1428 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001429 * flags in the low level driver. We can ignore the Bfoo
1430 * bits in c_cflag; c_[io]speed will always be set
1431 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if ((cflag ^ old_termios->c_cflag) == 0 &&
Alan Coxadc8d742012-07-14 15:31:47 +01001434 tty->termios.c_ospeed == old_termios->c_ospeed &&
1435 tty->termios.c_ispeed == old_termios->c_ispeed &&
Russell King2cbacaf2012-04-17 16:34:13 +01001436 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1437 !sw_changed) {
Peter Hurley4047b372016-04-09 18:56:33 -07001438 goto out;
Alan Coxe5238442008-04-30 00:53:28 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Alan Cox19225132010-06-01 22:52:51 +02001441 uart_change_speed(tty, state, old_termios);
Peter Hurleyc18b55f2014-06-16 09:17:09 -04001442 /* reload cflag from termios; port driver may have overriden flags */
1443 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /* Handle transition to B0 status */
1446 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Russell Kingdec94e72012-09-24 11:13:15 +01001447 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Handle transition away from B0 status */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001449 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned int mask = TIOCM_DTR;
Peter Hurley97ef38b2016-04-09 17:11:36 -07001451 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 mask |= TIOCM_RTS;
Russell Kingdec94e72012-09-24 11:13:15 +01001453 uart_set_mctrl(uport, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Peter Hurley4047b372016-04-09 18:56:33 -07001455out:
1456 mutex_unlock(&state->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001460 * Calls to uart_close() are serialised via the tty_lock in
1461 * drivers/tty/tty_io.c:tty_release()
1462 * drivers/tty/tty_io.c:do_tty_hangup()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464static void uart_close(struct tty_struct *tty, struct file *filp)
1465{
1466 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001467 struct tty_port *port;
Alan Coxa46c9992008-02-08 04:18:53 -08001468
Peter Hurley91b32f52014-10-16 16:54:27 -04001469 if (!state) {
1470 struct uart_driver *drv = tty->driver->driver_state;
1471
1472 state = drv->state + tty->index;
1473 port = &state->port;
1474 spin_lock_irq(&port->lock);
1475 --port->count;
1476 spin_unlock_irq(&port->lock);
Linus Torvaldseea7e172009-10-12 19:13:54 +02001477 return;
Peter Hurley91b32f52014-10-16 16:54:27 -04001478 }
Linus Torvaldseea7e172009-10-12 19:13:54 +02001479
Alan Cox46d57a42009-09-19 13:13:29 -07001480 port = &state->port;
Peter Hurley39b3d892016-01-10 20:23:57 -08001481 pr_debug("uart_close(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Rob Herring761ed4a2016-08-22 17:39:10 -05001483 tty_port_close(tty->port, tty, filp);
1484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Rob Herring761ed4a2016-08-22 17:39:10 -05001486static void uart_tty_port_shutdown(struct tty_port *port)
1487{
1488 struct uart_state *state = container_of(port, struct uart_state, port);
1489 struct uart_port *uport = uart_port_check(state);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /*
1492 * At this point, we stop accepting input. To do this, we
1493 * disable the receive line status interrupts.
1494 */
Andy Shevchenkoa5a2b132016-09-13 00:23:42 +03001495 if (WARN(!uport, "detached port still initialized!\n"))
1496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Andy Shevchenkoa5a2b132016-09-13 00:23:42 +03001498 spin_lock_irq(&uport->lock);
Rob Herring761ed4a2016-08-22 17:39:10 -05001499 uport->ops->stop_rx(uport);
Rob Herring761ed4a2016-08-22 17:39:10 -05001500 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Rob Herring761ed4a2016-08-22 17:39:10 -05001502 uart_port_shutdown(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /*
Rob Herring761ed4a2016-08-22 17:39:10 -05001505 * It's possible for shutdown to be called after suspend if we get
1506 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1507 * we don't try to resume a port that has been shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
Rob Herring761ed4a2016-08-22 17:39:10 -05001509 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Rob Herring761ed4a2016-08-22 17:39:10 -05001511 uart_change_pm(state, UART_PM_STATE_OFF);
Peter Hurley2e758912014-10-16 16:54:19 -04001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Jiri Slaby1f33a512011-07-14 14:35:10 +02001515static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jiri Slaby1f33a512011-07-14 14:35:10 +02001517 struct uart_state *state = tty->driver_data;
Peter Hurley9ed19422016-04-09 18:56:34 -07001518 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 unsigned long char_time, expire;
1520
Peter Hurley9ed19422016-04-09 18:56:34 -07001521 port = uart_port_ref(state);
1522 if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) {
1523 uart_port_deref(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
Peter Hurley9ed19422016-04-09 18:56:34 -07001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /*
1528 * Set the check interval to be 1/5 of the estimated time to
1529 * send a single character, and make it at least 1. The check
1530 * interval should also be less than the timeout.
1531 *
1532 * Note: we have to use pretty tight timings here to satisfy
1533 * the NIST-PCTS.
1534 */
1535 char_time = (port->timeout - HZ/50) / port->fifosize;
1536 char_time = char_time / 5;
1537 if (char_time == 0)
1538 char_time = 1;
1539 if (timeout && timeout < char_time)
1540 char_time = timeout;
1541
1542 /*
1543 * If the transmitter hasn't cleared in twice the approximate
1544 * amount of time to send the entire FIFO, it probably won't
1545 * ever clear. This assumes the UART isn't doing flow
1546 * control, which is currently the case. Hence, if it ever
1547 * takes longer than port->timeout, this is probably due to a
1548 * UART bug of some kind. So, we clamp the timeout parameter at
1549 * 2*port->timeout.
1550 */
1551 if (timeout == 0 || timeout > 2 * port->timeout)
1552 timeout = 2 * port->timeout;
1553
1554 expire = jiffies + timeout;
1555
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001556 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001557 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /*
1560 * Check whether the transmitter is empty every 'char_time'.
1561 * 'timeout' / 'expire' give us the maximum amount of time
1562 * we wait.
1563 */
1564 while (!port->ops->tx_empty(port)) {
1565 msleep_interruptible(jiffies_to_msecs(char_time));
1566 if (signal_pending(current))
1567 break;
1568 if (time_after(jiffies, expire))
1569 break;
1570 }
Peter Hurley9ed19422016-04-09 18:56:34 -07001571 uart_port_deref(port);
Arnd Bergmann20365212010-06-01 22:53:07 +02001572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001575 * Calls to uart_hangup() are serialised by the tty_lock in
1576 * drivers/tty/tty_io.c:do_tty_hangup()
1577 * This runs from a workqueue and can sleep for a _short_ time only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
1579static void uart_hangup(struct tty_struct *tty)
1580{
1581 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001582 struct tty_port *port = &state->port;
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001583 struct uart_port *uport;
Alan Cox61cd8a22010-06-01 22:52:57 +02001584 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Peter Hurley39b3d892016-01-10 20:23:57 -08001586 pr_debug("uart_hangup(%d)\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Alan Coxa2bceae2009-09-19 13:13:31 -07001588 mutex_lock(&port->mutex);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001589 uport = uart_port_check(state);
1590 WARN(!uport, "hangup of detached port!\n");
1591
Peter Hurley807c8d812016-04-09 17:53:22 -07001592 if (tty_port_active(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 uart_flush_buffer(tty);
Alan Cox19225132010-06-01 22:52:51 +02001594 uart_shutdown(tty, state);
Alan Cox61cd8a22010-06-01 22:52:57 +02001595 spin_lock_irqsave(&port->lock, flags);
Alan Cox91312cd2009-09-19 13:13:29 -07001596 port->count = 0;
Alan Cox61cd8a22010-06-01 22:52:57 +02001597 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -07001598 tty_port_set_active(port, 0);
Alan Cox7b014782009-09-19 13:13:33 -07001599 tty_port_tty_set(port, NULL);
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001600 if (uport && !uart_console(uport))
Geert Uytterhoevenbf903c02014-03-27 11:40:39 +01001601 uart_change_pm(state, UART_PM_STATE_OFF);
Alan Cox46d57a42009-09-19 13:13:29 -07001602 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -07001603 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001605 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001608/* uport == NULL if uart_port has already been removed */
Jiri Slaby0b1db832011-11-09 21:33:50 +01001609static void uart_port_shutdown(struct tty_port *port)
1610{
Jiri Slabyb922e192011-11-09 21:33:51 +01001611 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley4047b372016-04-09 18:56:33 -07001612 struct uart_port *uport = uart_port_check(state);
Jiri Slabyb922e192011-11-09 21:33:51 +01001613
1614 /*
1615 * clear delta_msr_wait queue to avoid mem leaks: we may free
1616 * the irq here so the queue might never be woken up. Note
1617 * that we won't end up waiting on delta_msr_wait again since
1618 * any outstanding file descriptors should be pointing at
1619 * hung_up_tty_fops now.
1620 */
1621 wake_up_interruptible(&port->delta_msr_wait);
1622
1623 /*
1624 * Free the IRQ and disable the port.
1625 */
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001626 if (uport)
1627 uport->ops->shutdown(uport);
Jiri Slabyb922e192011-11-09 21:33:51 +01001628
1629 /*
1630 * Ensure that the IRQ handler isn't running on another CPU.
1631 */
Peter Hurleyaf224ca2016-04-09 18:56:35 -07001632 if (uport)
1633 synchronize_irq(uport->irq);
Jiri Slaby0b1db832011-11-09 21:33:50 +01001634}
1635
Alan Coxde0c8cb2010-06-01 22:52:58 +02001636static int uart_carrier_raised(struct tty_port *port)
1637{
1638 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley9ed19422016-04-09 18:56:34 -07001639 struct uart_port *uport;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001640 int mctrl;
Peter Hurley9ed19422016-04-09 18:56:34 -07001641
1642 uport = uart_port_ref(state);
1643 /*
1644 * Should never observe uport == NULL since checks for hangup should
1645 * abort the tty_port_block_til_ready() loop before checking for carrier
1646 * raised -- but report carrier raised if it does anyway so open will
1647 * continue and not sleep
1648 */
1649 if (WARN_ON(!uport))
1650 return 1;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001651 spin_lock_irq(&uport->lock);
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001652 uart_enable_ms(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001653 mctrl = uport->ops->get_mctrl(uport);
1654 spin_unlock_irq(&uport->lock);
Peter Hurley9ed19422016-04-09 18:56:34 -07001655 uart_port_deref(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001656 if (mctrl & TIOCM_CAR)
1657 return 1;
1658 return 0;
1659}
1660
1661static void uart_dtr_rts(struct tty_port *port, int onoff)
1662{
1663 struct uart_state *state = container_of(port, struct uart_state, port);
Peter Hurley9ed19422016-04-09 18:56:34 -07001664 struct uart_port *uport;
1665
1666 uport = uart_port_ref(state);
1667 if (!uport)
1668 return;
Alan Cox24fcc7c2010-06-01 22:52:59 +02001669
Jiri Slaby6f5c24a2011-03-30 00:10:57 +02001670 if (onoff)
Alan Coxde0c8cb2010-06-01 22:52:58 +02001671 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1672 else
1673 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Peter Hurley9ed19422016-04-09 18:56:34 -07001674
1675 uart_port_deref(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001679 * Calls to uart_open are serialised by the tty_lock in
1680 * drivers/tty/tty_io.c:tty_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * Note that if this fails, then uart_close() _will_ be called.
1682 *
1683 * In time, we want to scrap the "opening nonpresent ports"
1684 * behaviour and implement an alternative way for setserial
1685 * to set base addresses/ports/types. This will allow us to
1686 * get rid of a certain amount of extra tests.
1687 */
1688static int uart_open(struct tty_struct *tty, struct file *filp)
1689{
Peter Hurley5e388022016-01-10 20:24:00 -08001690 struct uart_driver *drv = tty->driver->driver_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int retval, line = tty->index;
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001692 struct uart_state *state = drv->state + line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 tty->driver_data = state;
Rob Herringb3b57642016-08-22 17:39:09 -05001695
1696 retval = tty_port_open(&state->port, tty, filp);
1697 if (retval > 0)
1698 retval = 0;
1699
1700 return retval;
1701}
1702
1703static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1704{
1705 struct uart_state *state = container_of(port, struct uart_state, port);
1706 struct uart_port *uport;
1707
1708 uport = uart_port_check(state);
1709 if (!uport || uport->flags & UPF_DEAD)
1710 return -ENXIO;
1711
Peter Hurley4047b372016-04-09 18:56:33 -07001712 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * Start up the serial port.
1716 */
Rob Herringb3b57642016-08-22 17:39:09 -05001717 return uart_startup(tty, state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720static const char *uart_type(struct uart_port *port)
1721{
1722 const char *str = NULL;
1723
1724 if (port->ops->type)
1725 str = port->ops->type(port);
1726
1727 if (!str)
1728 str = "unknown";
1729
1730 return str;
1731}
1732
1733#ifdef CONFIG_PROC_FS
1734
Alexey Dobriyand196a942009-03-31 15:19:21 -07001735static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07001738 struct tty_port *port = &state->port;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001739 enum uart_pm_state pm_state;
Peter Hurley4047b372016-04-09 18:56:33 -07001740 struct uart_port *uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 char stat_buf[32];
1742 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001743 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Peter Hurley4047b372016-04-09 18:56:33 -07001745 mutex_lock(&port->mutex);
1746 uport = uart_port_check(state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001747 if (!uport)
Peter Hurley4047b372016-04-09 18:56:33 -07001748 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Alan Coxa2bceae2009-09-19 13:13:31 -07001750 mmio = uport->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001751 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001752 uport->line, uart_type(uport),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001753 mmio ? "mmio:0x" : "port:",
Alan Coxa2bceae2009-09-19 13:13:31 -07001754 mmio ? (unsigned long long)uport->mapbase
1755 : (unsigned long long)uport->iobase,
1756 uport->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Alan Coxa2bceae2009-09-19 13:13:31 -07001758 if (uport->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001759 seq_putc(m, '\n');
Peter Hurley4047b372016-04-09 18:56:33 -07001760 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
Alan Coxa46c9992008-02-08 04:18:53 -08001763 if (capable(CAP_SYS_ADMIN)) {
George G. Davis3689a0e2007-02-14 00:33:06 -08001764 pm_state = state->pm_state;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001765 if (pm_state != UART_PM_STATE_ON)
1766 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxa2bceae2009-09-19 13:13:31 -07001767 spin_lock_irq(&uport->lock);
1768 status = uport->ops->get_mctrl(uport);
1769 spin_unlock_irq(&uport->lock);
Linus Walleij6f538fe2012-12-07 11:36:08 +01001770 if (pm_state != UART_PM_STATE_ON)
George G. Davis3689a0e2007-02-14 00:33:06 -08001771 uart_change_pm(state, pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Alexey Dobriyand196a942009-03-31 15:19:21 -07001773 seq_printf(m, " tx:%d rx:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001774 uport->icount.tx, uport->icount.rx);
1775 if (uport->icount.frame)
Peter Hurley968af292016-01-10 20:24:01 -08001776 seq_printf(m, " fe:%d", uport->icount.frame);
Alan Coxa2bceae2009-09-19 13:13:31 -07001777 if (uport->icount.parity)
Peter Hurley968af292016-01-10 20:24:01 -08001778 seq_printf(m, " pe:%d", uport->icount.parity);
Alan Coxa2bceae2009-09-19 13:13:31 -07001779 if (uport->icount.brk)
Peter Hurley968af292016-01-10 20:24:01 -08001780 seq_printf(m, " brk:%d", uport->icount.brk);
Alan Coxa2bceae2009-09-19 13:13:31 -07001781 if (uport->icount.overrun)
Peter Hurley968af292016-01-10 20:24:01 -08001782 seq_printf(m, " oe:%d", uport->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001783
1784#define INFOBIT(bit, str) \
Alan Coxa2bceae2009-09-19 13:13:31 -07001785 if (uport->mctrl & (bit)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 strncat(stat_buf, (str), sizeof(stat_buf) - \
1787 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001788#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (status & (bit)) \
1790 strncat(stat_buf, (str), sizeof(stat_buf) - \
1791 strlen(stat_buf) - 2)
1792
1793 stat_buf[0] = '\0';
1794 stat_buf[1] = '\0';
1795 INFOBIT(TIOCM_RTS, "|RTS");
1796 STATBIT(TIOCM_CTS, "|CTS");
1797 INFOBIT(TIOCM_DTR, "|DTR");
1798 STATBIT(TIOCM_DSR, "|DSR");
1799 STATBIT(TIOCM_CAR, "|CD");
1800 STATBIT(TIOCM_RNG, "|RI");
1801 if (stat_buf[0])
1802 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001803
Alexey Dobriyand196a942009-03-31 15:19:21 -07001804 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001806 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#undef STATBIT
1808#undef INFOBIT
Peter Hurley4047b372016-04-09 18:56:33 -07001809out:
1810 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Alexey Dobriyand196a942009-03-31 15:19:21 -07001813static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001815 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Peter Hurley968af292016-01-10 20:24:01 -08001819 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001820 for (i = 0; i < drv->nr; i++)
1821 uart_line_info(m, drv, i);
1822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001824
1825static int uart_proc_open(struct inode *inode, struct file *file)
1826{
Al Virod9dda782013-03-31 18:16:14 -04001827 return single_open(file, uart_proc_show, PDE_DATA(inode));
Alexey Dobriyand196a942009-03-31 15:19:21 -07001828}
1829
1830static const struct file_operations uart_proc_fops = {
1831 .owner = THIS_MODULE,
1832 .open = uart_proc_open,
1833 .read = seq_read,
1834 .llseek = seq_lseek,
1835 .release = single_release,
1836};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837#endif
1838
Andrew Morton4a1b5502008-03-07 15:51:16 -08001839#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Peter Hurley1cfe42b2015-03-09 16:27:13 -04001840/**
Russell Kingd3587882006-03-20 20:00:09 +00001841 * uart_console_write - write a console message to a serial port
1842 * @port: the port to write the message
1843 * @s: array of characters
1844 * @count: number of characters in string to write
Peter Hurley10afbe32015-04-11 10:05:07 -04001845 * @putchar: function to write character to port
Russell Kingd3587882006-03-20 20:00:09 +00001846 */
1847void uart_console_write(struct uart_port *port, const char *s,
1848 unsigned int count,
1849 void (*putchar)(struct uart_port *, int))
1850{
1851 unsigned int i;
1852
1853 for (i = 0; i < count; i++, s++) {
1854 if (*s == '\n')
1855 putchar(port, '\r');
1856 putchar(port, *s);
1857 }
1858}
1859EXPORT_SYMBOL_GPL(uart_console_write);
1860
1861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * Check whether an invalid uart number has been specified, and
1863 * if so, search for the first available port that does have
1864 * console support.
1865 */
1866struct uart_port * __init
1867uart_get_console(struct uart_port *ports, int nr, struct console *co)
1868{
1869 int idx = co->index;
1870
1871 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1872 ports[idx].membase == NULL))
1873 for (idx = 0; idx < nr; idx++)
1874 if (ports[idx].iobase != 0 ||
1875 ports[idx].membase != NULL)
1876 break;
1877
1878 co->index = idx;
1879
1880 return ports + idx;
1881}
1882
1883/**
Peter Hurley73abaf82015-03-01 11:05:46 -05001884 * uart_parse_earlycon - Parse earlycon options
1885 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1886 * @iotype: ptr for decoded iotype (out)
1887 * @addr: ptr for decoded mapbase/iobase (out)
1888 * @options: ptr for <options> field; NULL if not present (out)
1889 *
1890 * Decodes earlycon kernel command line parameters of the form
Masahiro Yamadabd94c402015-10-28 12:46:05 +09001891 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1892 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
Peter Hurley73abaf82015-03-01 11:05:46 -05001893 *
1894 * The optional form
1895 * earlycon=<name>,0x<addr>,<options>
1896 * console=<name>,0x<addr>,<options>
1897 * is also accepted; the returned @iotype will be UPIO_MEM.
1898 *
Alexander Sverdlin8b2303d2016-09-12 13:29:29 +02001899 * Returns 0 on success or -EINVAL on failure
Peter Hurley73abaf82015-03-01 11:05:46 -05001900 */
Alexander Sverdlin46e36682016-09-02 13:20:21 +02001901int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
Peter Hurley73abaf82015-03-01 11:05:46 -05001902 char **options)
1903{
1904 if (strncmp(p, "mmio,", 5) == 0) {
1905 *iotype = UPIO_MEM;
1906 p += 5;
Masahiro Yamadabd94c402015-10-28 12:46:05 +09001907 } else if (strncmp(p, "mmio16,", 7) == 0) {
1908 *iotype = UPIO_MEM16;
1909 p += 7;
Peter Hurley73abaf82015-03-01 11:05:46 -05001910 } else if (strncmp(p, "mmio32,", 7) == 0) {
1911 *iotype = UPIO_MEM32;
1912 p += 7;
Noam Camus6e63be32015-05-25 06:54:28 +03001913 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1914 *iotype = UPIO_MEM32BE;
1915 p += 9;
Max Filippovd215d802015-09-22 15:20:32 +03001916 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1917 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1918 UPIO_MEM32BE : UPIO_MEM32;
1919 p += 13;
Peter Hurley73abaf82015-03-01 11:05:46 -05001920 } else if (strncmp(p, "io,", 3) == 0) {
1921 *iotype = UPIO_PORT;
1922 p += 3;
1923 } else if (strncmp(p, "0x", 2) == 0) {
1924 *iotype = UPIO_MEM;
1925 } else {
1926 return -EINVAL;
1927 }
1928
Alexander Sverdlin8b2303d2016-09-12 13:29:29 +02001929 /*
1930 * Before you replace it with kstrtoull(), think about options separator
1931 * (',') it will not tolerate
1932 */
1933 *addr = simple_strtoull(p, NULL, 0);
Peter Hurley73abaf82015-03-01 11:05:46 -05001934 p = strchr(p, ',');
1935 if (p)
1936 p++;
1937
1938 *options = p;
1939 return 0;
1940}
1941EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1942
1943/**
Geert Uytterhoeven02088ca2014-03-11 11:23:35 +01001944 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * @options: pointer to option string
1946 * @baud: pointer to an 'int' variable for the baud rate.
1947 * @parity: pointer to an 'int' variable for the parity.
1948 * @bits: pointer to an 'int' variable for the number of data bits.
1949 * @flow: pointer to an 'int' variable for the flow control character.
1950 *
1951 * uart_parse_options decodes a string containing the serial console
1952 * options. The format of the string is <baud><parity><bits><flow>,
1953 * eg: 115200n8r
1954 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001955void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1957{
1958 char *s = options;
1959
1960 *baud = simple_strtoul(s, NULL, 10);
1961 while (*s >= '0' && *s <= '9')
1962 s++;
1963 if (*s)
1964 *parity = *s++;
1965 if (*s)
1966 *bits = *s++ - '0';
1967 if (*s)
1968 *flow = *s;
1969}
Jason Wesself2d937f2008-04-17 20:05:37 +02001970EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/**
1973 * uart_set_options - setup the serial console parameters
1974 * @port: pointer to the serial ports uart_port structure
1975 * @co: console pointer
1976 * @baud: baud rate
1977 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1978 * @bits: number of data bits
1979 * @flow: flow control character - 'r' (rts)
1980 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001981int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982uart_set_options(struct uart_port *port, struct console *co,
1983 int baud, int parity, int bits, int flow)
1984{
Alan Cox606d0992006-12-08 02:38:45 -08001985 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001986 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Russell King976ecd12005-07-03 21:05:45 +01001988 /*
1989 * Ensure that the serial console lock is initialised
1990 * early.
Randy Witt42b6a1b2013-10-17 16:56:47 -04001991 * If this port is a console, then the spinlock is already
1992 * initialised.
Russell King976ecd12005-07-03 21:05:45 +01001993 */
Randy Witt42b6a1b2013-10-17 16:56:47 -04001994 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1995 spin_lock_init(&port->lock);
1996 lockdep_set_class(&port->lock, &port_lock_key);
1997 }
Russell King976ecd12005-07-03 21:05:45 +01001998
Alan Cox606d0992006-12-08 02:38:45 -08001999 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Jeffy Chenba47f972016-01-04 15:54:46 +08002001 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2002 tty_termios_encode_baud_rate(&termios, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 if (bits == 7)
2005 termios.c_cflag |= CS7;
2006 else
2007 termios.c_cflag |= CS8;
2008
2009 switch (parity) {
2010 case 'o': case 'O':
2011 termios.c_cflag |= PARODD;
2012 /*fall through*/
2013 case 'e': case 'E':
2014 termios.c_cflag |= PARENB;
2015 break;
2016 }
2017
2018 if (flow == 'r')
2019 termios.c_cflag |= CRTSCTS;
2020
Yinghai Lu79492682007-07-15 23:37:25 -07002021 /*
2022 * some uarts on other side don't support no flow control.
2023 * So we set * DTR in host uart to make them happy
2024 */
2025 port->mctrl |= TIOCM_DTR;
2026
Alan Cox149b36e2007-10-18 01:24:16 -07002027 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02002028 /*
2029 * Allow the setting of the UART parameters with a NULL console
2030 * too:
2031 */
2032 if (co)
2033 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 return 0;
2036}
Jason Wesself2d937f2008-04-17 20:05:37 +02002037EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038#endif /* CONFIG_SERIAL_CORE_CONSOLE */
2039
Jiri Slabycf755252011-11-09 21:33:47 +01002040/**
2041 * uart_change_pm - set power state of the port
2042 *
2043 * @state: port descriptor
2044 * @pm_state: new state
2045 *
2046 * Locking: port->mutex has to be held
2047 */
Linus Walleij6f538fe2012-12-07 11:36:08 +01002048static void uart_change_pm(struct uart_state *state,
2049 enum uart_pm_state pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Peter Hurley4047b372016-04-09 18:56:33 -07002051 struct uart_port *port = uart_port_check(state);
Andrew Victor1281e362006-05-16 11:28:49 +01002052
2053 if (state->pm_state != pm_state) {
Peter Hurley4047b372016-04-09 18:56:33 -07002054 if (port && port->ops->pm)
Andrew Victor1281e362006-05-16 11:28:49 +01002055 port->ops->pm(port, pm_state, state->pm_state);
2056 state->pm_state = pm_state;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002060struct uart_match {
2061 struct uart_port *port;
2062 struct uart_driver *driver;
2063};
2064
2065static int serial_match_port(struct device *dev, void *data)
2066{
2067 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07002068 struct tty_driver *tty_drv = match->driver->tty_driver;
2069 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2070 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002071
2072 return dev->devt == devt; /* Actually, only one tty per port */
2073}
2074
Alan Coxccce6de2009-09-19 13:13:30 -07002075int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Alan Coxccce6de2009-09-19 13:13:30 -07002077 struct uart_state *state = drv->state + uport->line;
2078 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002079 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002080 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Alan Coxa2bceae2009-09-19 13:13:31 -07002082 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Alan Coxccce6de2009-09-19 13:13:30 -07002084 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002085 if (device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05302086 if (!enable_irq_wake(uport->irq))
2087 uport->irq_wake = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002088 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002089 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002090 return 0;
2091 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02002092 put_device(tty_dev);
2093
Peter Hurleyb164c972015-01-22 12:24:25 -05002094 /* Nothing to do if the console is not suspending */
2095 if (!console_suspend_enabled && uart_console(uport))
2096 goto unlock;
2097
2098 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002099
Peter Hurleyd41861c2016-04-09 17:53:25 -07002100 if (tty_port_initialized(port)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002101 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002102 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Peter Hurley80f02d52016-04-09 17:53:24 -07002104 tty_port_set_suspended(port, 1);
Peter Hurleyd41861c2016-04-09 17:53:25 -07002105 tty_port_set_initialized(port, 0);
Russell Kinga6b93a92006-10-01 17:17:40 +01002106
Peter Hurleyb164c972015-01-22 12:24:25 -05002107 spin_lock_irq(&uport->lock);
2108 ops->stop_tx(uport);
2109 ops->set_mctrl(uport, 0);
2110 ops->stop_rx(uport);
2111 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 /*
2114 * Wait for the transmitter to empty.
2115 */
Alan Coxccce6de2009-09-19 13:13:30 -07002116 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08002118 if (!tries)
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302119 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2120 drv->dev_name,
2121 drv->tty_driver->name_base + uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Peter Hurleyb164c972015-01-22 12:24:25 -05002123 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
2126 /*
2127 * Disable the console device before suspending.
2128 */
Peter Hurleyb164c972015-01-22 12:24:25 -05002129 if (uart_console(uport))
Alan Coxccce6de2009-09-19 13:13:30 -07002130 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Peter Hurleyb164c972015-01-22 12:24:25 -05002132 uart_change_pm(state, UART_PM_STATE_OFF);
2133unlock:
Alan Coxa2bceae2009-09-19 13:13:31 -07002134 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 return 0;
2137}
2138
Alan Coxccce6de2009-09-19 13:13:30 -07002139int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Alan Coxccce6de2009-09-19 13:13:30 -07002141 struct uart_state *state = drv->state + uport->line;
2142 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002143 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002144 struct uart_match match = {uport, drv};
Deepak Saxenaba15ab02009-09-19 13:13:33 -07002145 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Alan Coxa2bceae2009-09-19 13:13:31 -07002147 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Alan Coxccce6de2009-09-19 13:13:30 -07002149 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2150 if (!uport->suspended && device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05302151 if (uport->irq_wake) {
2152 disable_irq_wake(uport->irq);
2153 uport->irq_wake = 0;
2154 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02002155 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002156 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002157 return 0;
2158 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02002159 put_device(tty_dev);
Alan Coxccce6de2009-09-19 13:13:30 -07002160 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /*
2163 * Re-enable the console device after suspending.
2164 */
Yin Kangkai5933a162011-01-30 11:15:30 +08002165 if (uart_console(uport)) {
Jason Wang891b9dd2010-08-21 15:14:42 +08002166 /*
2167 * First try to use the console cflag setting.
2168 */
2169 memset(&termios, 0, sizeof(struct ktermios));
2170 termios.c_cflag = uport->cons->cflag;
2171
2172 /*
2173 * If that's unset, use the tty termios setting.
2174 */
Alan Coxadc8d742012-07-14 15:31:47 +01002175 if (port->tty && termios.c_cflag == 0)
2176 termios = port->tty->termios;
Jason Wang891b9dd2010-08-21 15:14:42 +08002177
Ning Jiang94abc562011-09-05 16:28:18 +08002178 if (console_suspend_enabled)
Linus Walleij6f538fe2012-12-07 11:36:08 +01002179 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002180 uport->ops->set_termios(uport, &termios, NULL);
Yin Kangkai5933a162011-01-30 11:15:30 +08002181 if (console_suspend_enabled)
2182 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
Peter Hurley80f02d52016-04-09 17:53:24 -07002185 if (tty_port_suspended(port)) {
Alan Coxccce6de2009-09-19 13:13:30 -07002186 const struct uart_ops *ops = uport->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Linus Walleij6f538fe2012-12-07 11:36:08 +01002189 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002190 spin_lock_irq(&uport->lock);
2191 ops->set_mctrl(uport, 0);
2192 spin_unlock_irq(&uport->lock);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002193 if (console_suspend_enabled || !uart_console(uport)) {
Alan Cox19225132010-06-01 22:52:51 +02002194 /* Protected by port mutex for now */
2195 struct tty_struct *tty = port->tty;
Stanislav Brabec4547be72009-12-02 16:20:56 +01002196 ret = ops->startup(uport);
2197 if (ret == 0) {
Alan Cox19225132010-06-01 22:52:51 +02002198 if (tty)
2199 uart_change_speed(tty, state, NULL);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002200 spin_lock_irq(&uport->lock);
2201 ops->set_mctrl(uport, uport->mctrl);
2202 ops->start_tx(uport);
2203 spin_unlock_irq(&uport->lock);
Peter Hurleyd41861c2016-04-09 17:53:25 -07002204 tty_port_set_initialized(port, 1);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002205 } else {
2206 /*
2207 * Failed to resume - maybe hardware went away?
2208 * Clear the "initialized" flag so we won't try
2209 * to call the low level drivers shutdown method.
2210 */
Alan Cox19225132010-06-01 22:52:51 +02002211 uart_shutdown(tty, state);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002212 }
Russell Kingee31b332005-11-13 15:28:51 +00002213 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002214
Peter Hurley80f02d52016-04-09 17:53:24 -07002215 tty_port_set_suspended(port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217
Alan Coxa2bceae2009-09-19 13:13:31 -07002218 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 return 0;
2221}
2222
2223static inline void
2224uart_report_port(struct uart_driver *drv, struct uart_port *port)
2225{
Russell King30b7a3b2005-09-03 15:30:21 +01002226 char address[64];
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 switch (port->iotype) {
2229 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002230 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 break;
2232 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002233 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002234 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 break;
2236 case UPIO_MEM:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002237 case UPIO_MEM16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 case UPIO_MEM32:
Kevin Cernekee3ffb1a82014-11-12 12:53:59 -08002239 case UPIO_MEM32BE:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002240 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002241 case UPIO_TSI:
Russell King30b7a3b2005-09-03 15:30:21 +01002242 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002243 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002244 break;
2245 default:
2246 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
2248 }
Russell King30b7a3b2005-09-03 15:30:21 +01002249
James Bottomley68ed7e12015-01-02 10:05:13 -08002250 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2251 port->dev ? dev_name(port->dev) : "",
2252 port->dev ? ": " : "",
David S. Miller84408382008-10-13 10:45:26 +01002253 drv->dev_name,
2254 drv->tty_driver->name_base + port->line,
Kees Cook7d12b972013-07-12 13:07:39 -07002255 address, port->irq, port->uartclk / 16, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
2258static void
2259uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2260 struct uart_port *port)
2261{
2262 unsigned int flags;
2263
2264 /*
2265 * If there isn't a port here, don't do anything further.
2266 */
2267 if (!port->iobase && !port->mapbase && !port->membase)
2268 return;
2269
2270 /*
2271 * Now do the auto configuration stuff. Note that config_port
2272 * is expected to claim the resources and map the port for us.
2273 */
David Daney8e23fcc2009-01-02 13:49:54 +00002274 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (port->flags & UPF_AUTO_IRQ)
2276 flags |= UART_CONFIG_IRQ;
2277 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002278 if (!(port->flags & UPF_FIXED_TYPE)) {
2279 port->type = PORT_UNKNOWN;
2280 flags |= UART_CONFIG_TYPE;
2281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 port->ops->config_port(port, flags);
2283 }
2284
2285 if (port->type != PORT_UNKNOWN) {
2286 unsigned long flags;
2287
2288 uart_report_port(drv, port);
2289
George G. Davis3689a0e2007-02-14 00:33:06 -08002290 /* Power up port for set_mctrl() */
Linus Walleij6f538fe2012-12-07 11:36:08 +01002291 uart_change_pm(state, UART_PM_STATE_ON);
George G. Davis3689a0e2007-02-14 00:33:06 -08002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 /*
2294 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002295 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 * We probably don't need a spinlock around this, but
2297 */
2298 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002299 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 spin_unlock_irqrestore(&port->lock, flags);
2301
2302 /*
Russell King97d97222007-09-01 21:25:09 +01002303 * If this driver supports console, and it hasn't been
2304 * successfully registered yet, try to re-register it.
2305 * It may be that the port was not available.
2306 */
2307 if (port->cons && !(port->cons->flags & CON_ENABLED))
2308 register_console(port->cons);
2309
2310 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * Power down all ports by default, except the
2312 * console if we have one.
2313 */
2314 if (!uart_console(port))
Linus Walleij6f538fe2012-12-07 11:36:08 +01002315 uart_change_pm(state, UART_PM_STATE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317}
2318
Jason Wesself2d937f2008-04-17 20:05:37 +02002319#ifdef CONFIG_CONSOLE_POLL
2320
2321static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2322{
2323 struct uart_driver *drv = driver->driver_state;
2324 struct uart_state *state = drv->state + line;
Peter Hurley49c02302016-04-09 18:56:32 -07002325 struct tty_port *tport;
Jason Wesself2d937f2008-04-17 20:05:37 +02002326 struct uart_port *port;
2327 int baud = 9600;
2328 int bits = 8;
2329 int parity = 'n';
2330 int flow = 'n';
Peter Hurley49c02302016-04-09 18:56:32 -07002331 int ret = 0;
Jason Wesself2d937f2008-04-17 20:05:37 +02002332
Peter Hurley49c02302016-04-09 18:56:32 -07002333 if (!state)
Jason Wesself2d937f2008-04-17 20:05:37 +02002334 return -1;
2335
Peter Hurley49c02302016-04-09 18:56:32 -07002336 tport = &state->port;
2337 mutex_lock(&tport->mutex);
2338
Peter Hurley4047b372016-04-09 18:56:33 -07002339 port = uart_port_check(state);
2340 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
Peter Hurley49c02302016-04-09 18:56:32 -07002341 ret = -1;
2342 goto out;
2343 }
Jason Wesself2d937f2008-04-17 20:05:37 +02002344
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002345 if (port->ops->poll_init) {
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002346 /*
Peter Hurleyd41861c2016-04-09 17:53:25 -07002347 * We don't set initialized as we only initialized the hw,
2348 * e.g. state->xmit is still uninitialized.
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002349 */
Peter Hurleyd41861c2016-04-09 17:53:25 -07002350 if (!tty_port_initialized(tport))
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002351 ret = port->ops->poll_init(port);
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002352 }
2353
Peter Hurley49c02302016-04-09 18:56:32 -07002354 if (!ret && options) {
Jason Wesself2d937f2008-04-17 20:05:37 +02002355 uart_parse_options(options, &baud, &parity, &bits, &flow);
Peter Hurley49c02302016-04-09 18:56:32 -07002356 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
Jason Wesself2d937f2008-04-17 20:05:37 +02002357 }
Peter Hurley49c02302016-04-09 18:56:32 -07002358out:
2359 mutex_unlock(&tport->mutex);
2360 return ret;
Jason Wesself2d937f2008-04-17 20:05:37 +02002361}
2362
2363static int uart_poll_get_char(struct tty_driver *driver, int line)
2364{
2365 struct uart_driver *drv = driver->driver_state;
2366 struct uart_state *state = drv->state + line;
2367 struct uart_port *port;
Peter Hurley9ed19422016-04-09 18:56:34 -07002368 int ret = -1;
Jason Wesself2d937f2008-04-17 20:05:37 +02002369
Peter Hurley9ed19422016-04-09 18:56:34 -07002370 if (state) {
2371 port = uart_port_ref(state);
2372 if (port)
2373 ret = port->ops->poll_get_char(port);
2374 uart_port_deref(port);
2375 }
2376 return ret;
Jason Wesself2d937f2008-04-17 20:05:37 +02002377}
2378
2379static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2380{
2381 struct uart_driver *drv = driver->driver_state;
2382 struct uart_state *state = drv->state + line;
2383 struct uart_port *port;
2384
Peter Hurley9ed19422016-04-09 18:56:34 -07002385 if (!state)
Jason Wesself2d937f2008-04-17 20:05:37 +02002386 return;
2387
Peter Hurley9ed19422016-04-09 18:56:34 -07002388 port = uart_port_ref(state);
2389 if (!port)
2390 return;
Doug Andersonc7d44a02014-04-21 10:06:43 -07002391
2392 if (ch == '\n')
2393 port->ops->poll_put_char(port, '\r');
Jason Wesself2d937f2008-04-17 20:05:37 +02002394 port->ops->poll_put_char(port, ch);
Peter Hurley9ed19422016-04-09 18:56:34 -07002395 uart_port_deref(port);
Jason Wesself2d937f2008-04-17 20:05:37 +02002396}
2397#endif
2398
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002399static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .open = uart_open,
2401 .close = uart_close,
2402 .write = uart_write,
2403 .put_char = uart_put_char,
2404 .flush_chars = uart_flush_chars,
2405 .write_room = uart_write_room,
2406 .chars_in_buffer= uart_chars_in_buffer,
2407 .flush_buffer = uart_flush_buffer,
2408 .ioctl = uart_ioctl,
2409 .throttle = uart_throttle,
2410 .unthrottle = uart_unthrottle,
2411 .send_xchar = uart_send_xchar,
2412 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002413 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 .stop = uart_stop,
2415 .start = uart_start,
2416 .hangup = uart_hangup,
2417 .break_ctl = uart_break_ctl,
2418 .wait_until_sent= uart_wait_until_sent,
2419#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002420 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421#endif
2422 .tiocmget = uart_tiocmget,
2423 .tiocmset = uart_tiocmset,
Alan Coxd281da72010-09-16 18:21:24 +01002424 .get_icount = uart_get_icount,
Jason Wesself2d937f2008-04-17 20:05:37 +02002425#ifdef CONFIG_CONSOLE_POLL
2426 .poll_init = uart_poll_init,
2427 .poll_get_char = uart_poll_get_char,
2428 .poll_put_char = uart_poll_put_char,
2429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430};
2431
Alan Coxde0c8cb2010-06-01 22:52:58 +02002432static const struct tty_port_operations uart_port_ops = {
2433 .carrier_raised = uart_carrier_raised,
2434 .dtr_rts = uart_dtr_rts,
Rob Herringb3b57642016-08-22 17:39:09 -05002435 .activate = uart_port_activate,
Rob Herring761ed4a2016-08-22 17:39:10 -05002436 .shutdown = uart_tty_port_shutdown,
Alan Coxde0c8cb2010-06-01 22:52:58 +02002437};
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439/**
2440 * uart_register_driver - register a driver with the uart core layer
2441 * @drv: low level driver structure
2442 *
2443 * Register a uart driver with the core driver. We in turn register
2444 * with the tty layer, and initialise the core driver per-port state.
2445 *
2446 * We have a proc file in /proc/tty/driver which is named after the
2447 * normal driver.
2448 *
2449 * drv->port should be NULL, and the per-port structures should be
2450 * registered using uart_add_one_port after this call has succeeded.
2451 */
2452int uart_register_driver(struct uart_driver *drv)
2453{
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002454 struct tty_driver *normal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 int i, retval;
2456
2457 BUG_ON(drv->state);
2458
2459 /*
2460 * Maybe we should be using a slab cache for this, especially if
2461 * we have a large number of ports to handle.
2462 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002463 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (!drv->state)
2465 goto out;
2466
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002467 normal = alloc_tty_driver(drv->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (!normal)
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002469 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 drv->tty_driver = normal;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 normal->name = drv->dev_name;
2475 normal->major = drv->major;
2476 normal->minor_start = drv->minor;
2477 normal->type = TTY_DRIVER_TYPE_SERIAL;
2478 normal->subtype = SERIAL_TYPE_NORMAL;
2479 normal->init_termios = tty_std_termios;
2480 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002481 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002482 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 normal->driver_state = drv;
2484 tty_set_operations(normal, &uart_ops);
2485
2486 /*
2487 * Initialise the UART state(s).
2488 */
2489 for (i = 0; i < drv->nr; i++) {
2490 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07002491 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Alan Coxa2bceae2009-09-19 13:13:31 -07002493 tty_port_init(port);
Alan Coxde0c8cb2010-06-01 22:52:58 +02002494 port->ops = &uart_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496
2497 retval = tty_register_driver(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002498 if (retval >= 0)
2499 return retval;
2500
Jiri Slaby191c5f12012-11-15 09:49:56 +01002501 for (i = 0; i < drv->nr; i++)
2502 tty_port_destroy(&drv->state[i].port);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002503 put_tty_driver(normal);
2504out_kfree:
2505 kfree(drv->state);
2506out:
2507 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/**
2511 * uart_unregister_driver - remove a driver from the uart core layer
2512 * @drv: low level driver structure
2513 *
2514 * Remove all references to a driver from the core driver. The low
2515 * level driver must have removed all its ports via the
2516 * uart_remove_one_port() if it registered them with uart_add_one_port().
2517 * (ie, drv->port == NULL)
2518 */
2519void uart_unregister_driver(struct uart_driver *drv)
2520{
2521 struct tty_driver *p = drv->tty_driver;
Jiri Slaby191c5f12012-11-15 09:49:56 +01002522 unsigned int i;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 tty_unregister_driver(p);
2525 put_tty_driver(p);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002526 for (i = 0; i < drv->nr; i++)
2527 tty_port_destroy(&drv->state[i].port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 kfree(drv->state);
Alan Cox1e66cded2012-05-14 14:51:22 +01002529 drv->state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 drv->tty_driver = NULL;
2531}
2532
2533struct tty_driver *uart_console_device(struct console *co, int *index)
2534{
2535 struct uart_driver *p = co->data;
2536 *index = co->index;
2537 return p->tty_driver;
2538}
2539
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002540static ssize_t uart_get_attr_uartclk(struct device *dev,
2541 struct device_attribute *attr, char *buf)
2542{
Alan Coxbebe73e2012-10-29 15:19:57 +00002543 struct serial_struct tmp;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002544 struct tty_port *port = dev_get_drvdata(dev);
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002545
Alan Cox9f109692012-10-29 15:20:25 +00002546 uart_get_info(port, &tmp);
Alan Coxbebe73e2012-10-29 15:19:57 +00002547 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002548}
2549
Alan Cox373bac42012-10-29 15:20:40 +00002550static ssize_t uart_get_attr_type(struct device *dev,
2551 struct device_attribute *attr, char *buf)
2552{
2553 struct serial_struct tmp;
2554 struct tty_port *port = dev_get_drvdata(dev);
2555
2556 uart_get_info(port, &tmp);
2557 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2558}
2559static ssize_t uart_get_attr_line(struct device *dev,
2560 struct device_attribute *attr, char *buf)
2561{
2562 struct serial_struct tmp;
2563 struct tty_port *port = dev_get_drvdata(dev);
2564
2565 uart_get_info(port, &tmp);
2566 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2567}
2568
2569static ssize_t uart_get_attr_port(struct device *dev,
2570 struct device_attribute *attr, char *buf)
2571{
2572 struct serial_struct tmp;
2573 struct tty_port *port = dev_get_drvdata(dev);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002574 unsigned long ioaddr;
Alan Cox373bac42012-10-29 15:20:40 +00002575
2576 uart_get_info(port, &tmp);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002577 ioaddr = tmp.port;
2578 if (HIGH_BITS_OFFSET)
2579 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2580 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
Alan Cox373bac42012-10-29 15:20:40 +00002581}
2582
2583static ssize_t uart_get_attr_irq(struct device *dev,
2584 struct device_attribute *attr, char *buf)
2585{
2586 struct serial_struct tmp;
2587 struct tty_port *port = dev_get_drvdata(dev);
2588
2589 uart_get_info(port, &tmp);
2590 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2591}
2592
2593static ssize_t uart_get_attr_flags(struct device *dev,
2594 struct device_attribute *attr, char *buf)
2595{
2596 struct serial_struct tmp;
2597 struct tty_port *port = dev_get_drvdata(dev);
2598
2599 uart_get_info(port, &tmp);
2600 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2601}
2602
2603static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2604 struct device_attribute *attr, char *buf)
2605{
2606 struct serial_struct tmp;
2607 struct tty_port *port = dev_get_drvdata(dev);
2608
2609 uart_get_info(port, &tmp);
2610 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2611}
2612
2613
2614static ssize_t uart_get_attr_close_delay(struct device *dev,
2615 struct device_attribute *attr, char *buf)
2616{
2617 struct serial_struct tmp;
2618 struct tty_port *port = dev_get_drvdata(dev);
2619
2620 uart_get_info(port, &tmp);
2621 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2622}
2623
2624
2625static ssize_t uart_get_attr_closing_wait(struct device *dev,
2626 struct device_attribute *attr, char *buf)
2627{
2628 struct serial_struct tmp;
2629 struct tty_port *port = dev_get_drvdata(dev);
2630
2631 uart_get_info(port, &tmp);
2632 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2633}
2634
2635static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2636 struct device_attribute *attr, char *buf)
2637{
2638 struct serial_struct tmp;
2639 struct tty_port *port = dev_get_drvdata(dev);
2640
2641 uart_get_info(port, &tmp);
2642 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2643}
2644
2645static ssize_t uart_get_attr_io_type(struct device *dev,
2646 struct device_attribute *attr, char *buf)
2647{
2648 struct serial_struct tmp;
2649 struct tty_port *port = dev_get_drvdata(dev);
2650
2651 uart_get_info(port, &tmp);
2652 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2653}
2654
2655static ssize_t uart_get_attr_iomem_base(struct device *dev,
2656 struct device_attribute *attr, char *buf)
2657{
2658 struct serial_struct tmp;
2659 struct tty_port *port = dev_get_drvdata(dev);
2660
2661 uart_get_info(port, &tmp);
2662 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2663}
2664
2665static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2666 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);
2672 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2673}
2674
2675static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2676static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2677static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2678static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2679static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2680static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002681static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
Alan Cox373bac42012-10-29 15:20:40 +00002682static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2683static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2684static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2685static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2686static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2687static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002688
2689static struct attribute *tty_dev_attrs[] = {
Alan Cox373bac42012-10-29 15:20:40 +00002690 &dev_attr_type.attr,
2691 &dev_attr_line.attr,
2692 &dev_attr_port.attr,
2693 &dev_attr_irq.attr,
2694 &dev_attr_flags.attr,
2695 &dev_attr_xmit_fifo_size.attr,
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002696 &dev_attr_uartclk.attr,
Alan Cox373bac42012-10-29 15:20:40 +00002697 &dev_attr_close_delay.attr,
2698 &dev_attr_closing_wait.attr,
2699 &dev_attr_custom_divisor.attr,
2700 &dev_attr_io_type.attr,
2701 &dev_attr_iomem_base.attr,
2702 &dev_attr_iomem_reg_shift.attr,
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002703 NULL,
2704 };
2705
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002706static const struct attribute_group tty_dev_attr_group = {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002707 .attrs = tty_dev_attrs,
2708 };
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/**
2711 * uart_add_one_port - attach a driver-defined port structure
2712 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002713 * @uport: uart port structure to use for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 *
2715 * This allows the driver to register its own uart_port structure
2716 * with the core driver. The main purpose is to allow the low
2717 * level uart drivers to expand uart_port, rather than having yet
2718 * more levels of structures.
2719 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002720int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07002723 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002725 struct device *tty_dev;
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002726 int num_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 BUG_ON(in_interrupt());
2729
Alan Coxa2bceae2009-09-19 13:13:31 -07002730 if (uport->line >= drv->nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return -EINVAL;
2732
Alan Coxa2bceae2009-09-19 13:13:31 -07002733 state = drv->state + uport->line;
2734 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002736 mutex_lock(&port_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07002737 mutex_lock(&port->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002738 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ret = -EINVAL;
2740 goto out;
2741 }
2742
Peter Hurley2b702b92014-10-16 16:54:25 -04002743 /* Link the port to the driver state table and vice versa */
Peter Hurley9ed19422016-04-09 18:56:34 -07002744 atomic_set(&state->refcount, 1);
2745 init_waitqueue_head(&state->remove_wait);
Alan Coxa2bceae2009-09-19 13:13:31 -07002746 state->uart_port = uport;
Alan Coxa2bceae2009-09-19 13:13:31 -07002747 uport->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Peter Hurley2b702b92014-10-16 16:54:25 -04002749 state->pm_state = UART_PM_STATE_UNDEFINED;
2750 uport->cons = drv->cons;
Peter Hurley959801f2015-02-24 14:25:00 -05002751 uport->minor = drv->tty_driver->minor_start + uport->line;
Peter Hurley2b702b92014-10-16 16:54:25 -04002752
Russell King976ecd12005-07-03 21:05:45 +01002753 /*
2754 * If this port is a console, then the spinlock is already
2755 * initialised.
2756 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002757 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2758 spin_lock_init(&uport->lock);
2759 lockdep_set_class(&uport->lock, &port_lock_key);
Ingo Molnar13e83592006-07-03 00:25:03 -07002760 }
Grant Likelya208ffd2014-03-27 18:29:46 -07002761 if (uport->cons && uport->dev)
2762 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
Russell King976ecd12005-07-03 21:05:45 +01002763
Alan Coxa2bceae2009-09-19 13:13:31 -07002764 uart_configure_port(drv, state, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Geert Uytterhoeven4dda8642016-10-28 07:07:47 -05002766 port->console = uart_console(uport);
2767
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002768 num_groups = 2;
2769 if (uport->attr_group)
2770 num_groups++;
2771
Yoshihiro YUNOMAEc2b703b2014-07-23 06:06:22 +00002772 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002773 GFP_KERNEL);
2774 if (!uport->tty_groups) {
2775 ret = -ENOMEM;
2776 goto out;
2777 }
2778 uport->tty_groups[0] = &tty_dev_attr_group;
2779 if (uport->attr_group)
2780 uport->tty_groups[1] = uport->attr_group;
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /*
2783 * Register the port whether it's detected or not. This allows
Geert Uytterhoeven015355b2014-03-11 11:23:36 +01002784 * setserial to be used to alter this port's parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 */
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002786 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002787 uport->line, uport->dev, port, uport->tty_groups);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002788 if (likely(!IS_ERR(tty_dev))) {
Simon Glass77359832012-01-19 11:28:56 -08002789 device_set_wakeup_capable(tty_dev, 1);
2790 } else {
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302791 dev_err(uport->dev, "Cannot register tty device on line %d\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002792 uport->line);
Simon Glass77359832012-01-19 11:28:56 -08002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 /*
Russell King68ac64c2006-04-30 11:13:50 +01002796 * Ensure UPF_DEAD is not set.
2797 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002798 uport->flags &= ~UPF_DEAD;
Russell King68ac64c2006-04-30 11:13:50 +01002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 out:
Alan Coxa2bceae2009-09-19 13:13:31 -07002801 mutex_unlock(&port->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002802 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 return ret;
2805}
2806
2807/**
2808 * uart_remove_one_port - detach a driver defined port structure
2809 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002810 * @uport: uart port structure for this port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 *
2812 * This unhooks (and hangs up) the specified port structure from the
2813 * core driver. No further calls will be made to the low-level code
2814 * for this port.
2815 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002816int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Alan Coxa2bceae2009-09-19 13:13:31 -07002818 struct uart_state *state = drv->state + uport->line;
2819 struct tty_port *port = &state->port;
Peter Hurley4047b372016-04-09 18:56:33 -07002820 struct uart_port *uart_port;
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002821 struct tty_struct *tty;
Chen Gangb342dd52012-12-27 15:51:31 +08002822 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 BUG_ON(in_interrupt());
2825
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002826 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /*
Russell King68ac64c2006-04-30 11:13:50 +01002829 * Mark the port "dead" - this prevents any opens from
2830 * succeeding while we shut down the port.
2831 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002832 mutex_lock(&port->mutex);
Peter Hurley4047b372016-04-09 18:56:33 -07002833 uart_port = uart_port_check(state);
2834 if (uart_port != uport)
2835 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
2836 uart_port, uport);
2837
2838 if (!uart_port) {
Chen Gangb342dd52012-12-27 15:51:31 +08002839 mutex_unlock(&port->mutex);
2840 ret = -EINVAL;
2841 goto out;
2842 }
Alan Coxa2bceae2009-09-19 13:13:31 -07002843 uport->flags |= UPF_DEAD;
2844 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002845
2846 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002847 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002849 tty_unregister_device(drv->tty_driver, uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002851 tty = tty_port_tty_get(port);
2852 if (tty) {
Alan Coxa2bceae2009-09-19 13:13:31 -07002853 tty_vhangup(port->tty);
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002854 tty_kref_put(tty);
2855 }
Russell King68ac64c2006-04-30 11:13:50 +01002856
2857 /*
Geert Uytterhoeven5f5c9ae2014-02-28 14:21:32 +01002858 * If the port is used as a console, unregister it
2859 */
2860 if (uart_console(uport))
2861 unregister_console(uport->cons);
2862
2863 /*
Russell King68ac64c2006-04-30 11:13:50 +01002864 * Free the port IO and memory resources, if any.
2865 */
Fabio Estevama7cfaf12016-05-20 01:59:54 -03002866 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
Alan Coxa2bceae2009-09-19 13:13:31 -07002867 uport->ops->release_port(uport);
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002868 kfree(uport->tty_groups);
Russell King68ac64c2006-04-30 11:13:50 +01002869
2870 /*
2871 * Indicate that there isn't a port here anymore.
2872 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002873 uport->type = PORT_UNKNOWN;
Russell King68ac64c2006-04-30 11:13:50 +01002874
Peter Hurley4047b372016-04-09 18:56:33 -07002875 mutex_lock(&port->mutex);
Peter Hurley9ed19422016-04-09 18:56:34 -07002876 WARN_ON(atomic_dec_return(&state->refcount) < 0);
2877 wait_event(state->remove_wait, !atomic_read(&state->refcount));
Alan Coxebd2c8f2009-09-19 13:13:28 -07002878 state->uart_port = NULL;
Peter Hurley4047b372016-04-09 18:56:33 -07002879 mutex_unlock(&port->mutex);
Chen Gangb342dd52012-12-27 15:51:31 +08002880out:
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002881 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Chen Gangb342dd52012-12-27 15:51:31 +08002883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
2886/*
2887 * Are the two ports equivalent?
2888 */
2889int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2890{
2891 if (port1->iotype != port2->iotype)
2892 return 0;
2893
2894 switch (port1->iotype) {
2895 case UPIO_PORT:
2896 return (port1->iobase == port2->iobase);
2897 case UPIO_HUB6:
2898 return (port1->iobase == port2->iobase) &&
2899 (port1->hub6 == port2->hub6);
2900 case UPIO_MEM:
Masahiro Yamadabd94c402015-10-28 12:46:05 +09002901 case UPIO_MEM16:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002902 case UPIO_MEM32:
Kevin Cernekee3ffb1a82014-11-12 12:53:59 -08002903 case UPIO_MEM32BE:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002904 case UPIO_AU:
2905 case UPIO_TSI:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002906 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908 return 0;
2909}
2910EXPORT_SYMBOL(uart_match_port);
2911
Jiri Slaby027d7da2011-11-09 21:33:43 +01002912/**
2913 * uart_handle_dcd_change - handle a change of carrier detect state
2914 * @uport: uart_port structure for the open port
2915 * @status: new carrier detect status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04002916 *
2917 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01002918 */
2919void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2920{
George Spelvin42381572013-02-10 04:44:30 -05002921 struct tty_port *port = &uport->state->port;
Alan Cox43eca0a2012-09-19 15:35:46 +01002922 struct tty_struct *tty = port->tty;
Peter Hurleyc9932572014-09-02 17:39:21 -04002923 struct tty_ldisc *ld;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002924
Peter Hurley4d90bb12014-09-10 15:06:23 -04002925 lockdep_assert_held_once(&uport->lock);
2926
Peter Hurleyc9932572014-09-02 17:39:21 -04002927 if (tty) {
2928 ld = tty_ldisc_ref(tty);
2929 if (ld) {
2930 if (ld->ops->dcd_change)
2931 ld->ops->dcd_change(tty, status);
2932 tty_ldisc_deref(ld);
2933 }
George Spelvin42381572013-02-10 04:44:30 -05002934 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01002935
2936 uport->icount.dcd++;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002937
Peter Hurley299245a2014-09-10 15:06:24 -04002938 if (uart_dcd_enabled(uport)) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01002939 if (status)
2940 wake_up_interruptible(&port->open_wait);
Alan Cox43eca0a2012-09-19 15:35:46 +01002941 else if (tty)
2942 tty_hangup(tty);
Jiri Slaby027d7da2011-11-09 21:33:43 +01002943 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01002944}
2945EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2946
2947/**
2948 * uart_handle_cts_change - handle a change of clear-to-send state
2949 * @uport: uart_port structure for the open port
2950 * @status: new clear to send status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04002951 *
2952 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01002953 */
2954void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2955{
Peter Hurley4d90bb12014-09-10 15:06:23 -04002956 lockdep_assert_held_once(&uport->lock);
2957
Jiri Slaby027d7da2011-11-09 21:33:43 +01002958 uport->icount.cts++;
2959
Peter Hurley391f93f2015-01-25 14:44:51 -05002960 if (uart_softcts_mode(uport)) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002961 if (uport->hw_stopped) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01002962 if (status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002963 uport->hw_stopped = 0;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002964 uport->ops->start_tx(uport);
2965 uart_write_wakeup(uport);
2966 }
2967 } else {
2968 if (!status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002969 uport->hw_stopped = 1;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002970 uport->ops->stop_tx(uport);
2971 }
2972 }
Peter Hurley391f93f2015-01-25 14:44:51 -05002973
Jiri Slaby027d7da2011-11-09 21:33:43 +01002974 }
2975}
2976EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2977
Jiri Slabycf755252011-11-09 21:33:47 +01002978/**
2979 * uart_insert_char - push a char to the uart layer
2980 *
2981 * User is responsible to call tty_flip_buffer_push when they are done with
2982 * insertion.
2983 *
2984 * @port: corresponding port
2985 * @status: state of the serial port RX buffer (LSR for 8250)
2986 * @overrun: mask of overrun bits in @status
2987 * @ch: character to push
2988 * @flag: flag for the character (see TTY_NORMAL and friends)
2989 */
Jiri Slaby027d7da2011-11-09 21:33:43 +01002990void uart_insert_char(struct uart_port *port, unsigned int status,
2991 unsigned int overrun, unsigned int ch, unsigned int flag)
2992{
Jiri Slaby92a19f92013-01-03 15:53:03 +01002993 struct tty_port *tport = &port->state->port;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002994
2995 if ((status & port->ignore_status_mask & ~overrun) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +01002996 if (tty_insert_flip_char(tport, ch, flag) == 0)
Corbindabfb352012-05-23 09:37:31 -05002997 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002998
2999 /*
3000 * Overrun is special. Since it's reported immediately,
3001 * it doesn't affect the current character.
3002 */
3003 if (status & ~port->ignore_status_mask & overrun)
Jiri Slaby92a19f92013-01-03 15:53:03 +01003004 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
Corbindabfb352012-05-23 09:37:31 -05003005 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01003006}
3007EXPORT_SYMBOL_GPL(uart_insert_char);
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009EXPORT_SYMBOL(uart_write_wakeup);
3010EXPORT_SYMBOL(uart_register_driver);
3011EXPORT_SYMBOL(uart_unregister_driver);
3012EXPORT_SYMBOL(uart_suspend_port);
3013EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014EXPORT_SYMBOL(uart_add_one_port);
3015EXPORT_SYMBOL(uart_remove_one_port);
3016
3017MODULE_DESCRIPTION("Serial driver core");
3018MODULE_LICENSE("GPL");