blob: e31d56159aee9441e5b68a259dad939a01f66aeb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * This routine is used by the interrupt handler to schedule processing in
69 * the software interrupt portion of the driver.
70 */
71void uart_write_wakeup(struct uart_port *port)
72{
Alan Coxebd2c8f2009-09-19 13:13:28 -070073 struct uart_state *state = port->state;
Pavel Machekd5f735e2006-03-07 21:55:20 -080074 /*
75 * This means you called this function _after_ the port was
76 * closed. No cookie for you.
77 */
Alan Coxebd2c8f2009-09-19 13:13:28 -070078 BUG_ON(!state);
Jiri Slaby6a3e492b2011-07-14 14:35:12 +020079 tty_wakeup(state->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static void uart_stop(struct tty_struct *tty)
83{
84 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -070085 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long flags;
87
88 spin_lock_irqsave(&port->lock, flags);
Russell Kingb129a8c2005-08-31 10:12:14 +010089 port->ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_irqrestore(&port->lock, flags);
91}
92
93static void __uart_start(struct tty_struct *tty)
94{
95 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -070096 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Peter Hurleyd01f4d12014-09-10 15:06:26 -040098 if (!uart_tx_stopped(port))
Russell Kingb129a8c2005-08-31 10:12:14 +010099 port->ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void uart_start(struct tty_struct *tty)
103{
104 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700105 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long flags;
107
108 spin_lock_irqsave(&port->lock, flags);
109 __uart_start(tty);
110 spin_unlock_irqrestore(&port->lock, flags);
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static inline void
114uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
115{
116 unsigned long flags;
117 unsigned int old;
118
119 spin_lock_irqsave(&port->lock, flags);
120 old = port->mctrl;
121 port->mctrl = (old & ~clear) | set;
122 if (old != port->mctrl)
123 port->ops->set_mctrl(port, port->mctrl);
124 spin_unlock_irqrestore(&port->lock, flags);
125}
126
Alan Coxa46c9992008-02-08 04:18:53 -0800127#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
128#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * Startup the port. This will be called once per open. All calls
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100132 * will be serialised by the per-port mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100134static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
135 int init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Alan Cox46d57a42009-09-19 13:13:29 -0700137 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned long page;
139 int retval = 0;
140
Alan Cox46d57a42009-09-19 13:13:29 -0700141 if (uport->type == PORT_UNKNOWN)
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /*
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200145 * Make sure the device is in D0 state.
146 */
147 uart_change_pm(state, UART_PM_STATE_ON);
148
149 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * Initialise and allocate the transmit and temporary
151 * buffer.
152 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700153 if (!state->xmit.buf) {
Alan Coxdf4f4dd2008-07-16 21:53:50 +0100154 /* This is protected by the per port mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 page = get_zeroed_page(GFP_KERNEL);
156 if (!page)
157 return -ENOMEM;
158
Alan Coxebd2c8f2009-09-19 13:13:28 -0700159 state->xmit.buf = (unsigned char *) page;
160 uart_circ_clear(&state->xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Alan Cox46d57a42009-09-19 13:13:29 -0700163 retval = uport->ops->startup(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (retval == 0) {
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200165 if (uart_console(uport) && uport->cons->cflag) {
Alan Coxadc8d742012-07-14 15:31:47 +0100166 tty->termios.c_cflag = uport->cons->cflag;
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200167 uport->cons->cflag = 0;
168 }
169 /*
170 * Initialise the hardware port settings.
171 */
172 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jiri Slabyc7d7abf2011-03-30 00:10:55 +0200174 if (init_hw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /*
176 * Setup the RTS and DTR signals once the
177 * port is open and ready to respond.
178 */
Alan Coxadc8d742012-07-14 15:31:47 +0100179 if (tty->termios.c_cflag & CBAUD)
Alan Cox46d57a42009-09-19 13:13:29 -0700180 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Peter Hurleya6eec922014-09-02 17:39:10 -0400182
Peter Hurley299245a2014-09-10 15:06:24 -0400183 spin_lock_irq(&uport->lock);
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400184 if (uart_cts_enabled(uport) &&
185 !(uport->ops->get_mctrl(uport) & TIOCM_CTS))
186 uport->hw_stopped = 1;
187 else
188 uport->hw_stopped = 0;
Peter Hurley299245a2014-09-10 15:06:24 -0400189 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Jiri Slaby00551972011-08-17 13:48:15 +0200192 /*
193 * This is to allow setserial on this port. People may want to set
194 * port/irq/type and then reconfigure the port properly if it failed
195 * now.
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (retval && capable(CAP_SYS_ADMIN))
Jiri Slabyc0d92be2011-11-09 21:34:14 +0100198 return 1;
199
200 return retval;
201}
202
203static int uart_startup(struct tty_struct *tty, struct uart_state *state,
204 int init_hw)
205{
206 struct tty_port *port = &state->port;
207 int retval;
208
209 if (port->flags & ASYNC_INITIALIZED)
210 return 0;
211
212 /*
213 * Set the TTY IO error marker - we will only clear this
214 * once we have successfully opened the port.
215 */
216 set_bit(TTY_IO_ERROR, &tty->flags);
217
218 retval = uart_port_startup(tty, state, init_hw);
219 if (!retval) {
220 set_bit(ASYNCB_INITIALIZED, &port->flags);
221 clear_bit(TTY_IO_ERROR, &tty->flags);
222 } else if (retval > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 retval = 0;
224
225 return retval;
226}
227
228/*
229 * This routine will shutdown a serial port; interrupts are disabled, and
230 * DTR is dropped if the hangup on close termio flag is on. Calls to
231 * uart_shutdown are serialised by the per-port semaphore.
232 */
Alan Cox19225132010-06-01 22:52:51 +0200233static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Alan Coxccce6de2009-09-19 13:13:30 -0700235 struct uart_port *uport = state->uart_port;
Alan Coxbdc04e32009-09-19 13:13:31 -0700236 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Russell Kingee31b332005-11-13 15:28:51 +0000238 /*
239 * Set the TTY IO error marker
240 */
Alan Coxf7519282009-01-02 13:49:21 +0000241 if (tty)
242 set_bit(TTY_IO_ERROR, &tty->flags);
Russell Kingee31b332005-11-13 15:28:51 +0000243
Alan Coxbdc04e32009-09-19 13:13:31 -0700244 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
Russell Kingee31b332005-11-13 15:28:51 +0000245 /*
246 * Turn off DTR and RTS early.
247 */
Peter Hurleyae84db92014-07-09 09:21:14 -0400248 if (uart_console(uport) && tty)
249 uport->cons->cflag = tty->termios.c_cflag;
250
Alan Coxadc8d742012-07-14 15:31:47 +0100251 if (!tty || (tty->termios.c_cflag & HUPCL))
Alan Coxccce6de2009-09-19 13:13:30 -0700252 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Russell Kingee31b332005-11-13 15:28:51 +0000253
Jiri Slabyb922e192011-11-09 21:33:51 +0100254 uart_port_shutdown(port);
Russell Kingee31b332005-11-13 15:28:51 +0000255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /*
Doug Andersond208a3b2011-10-19 11:52:01 -0700258 * It's possible for shutdown to be called after suspend if we get
259 * a DCD drop (hangup) at just the right time. Clear suspended bit so
260 * we don't try to resume a port that has been shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Doug Andersond208a3b2011-10-19 11:52:01 -0700262 clear_bit(ASYNCB_SUSPENDED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Free the transmit buffer page.
266 */
Alan Coxebd2c8f2009-09-19 13:13:28 -0700267 if (state->xmit.buf) {
268 free_page((unsigned long)state->xmit.buf);
269 state->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/**
274 * uart_update_timeout - update per-port FIFO timeout.
275 * @port: uart_port structure describing the port
276 * @cflag: termios cflag value
277 * @baud: speed of the port
278 *
279 * Set the port FIFO timeout value. The @cflag value should
280 * reflect the actual hardware settings.
281 */
282void
283uart_update_timeout(struct uart_port *port, unsigned int cflag,
284 unsigned int baud)
285{
286 unsigned int bits;
287
288 /* byte size and parity */
289 switch (cflag & CSIZE) {
290 case CS5:
291 bits = 7;
292 break;
293 case CS6:
294 bits = 8;
295 break;
296 case CS7:
297 bits = 9;
298 break;
299 default:
300 bits = 10;
Alan Coxa46c9992008-02-08 04:18:53 -0800301 break; /* CS8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 if (cflag & CSTOPB)
305 bits++;
306 if (cflag & PARENB)
307 bits++;
308
309 /*
310 * The total number of bits to be transmitted in the fifo.
311 */
312 bits = bits * port->fifosize;
313
314 /*
315 * Figure the timeout to send the above number of bits.
316 * Add .02 seconds of slop
317 */
318 port->timeout = (HZ * bits) / baud + HZ/50;
319}
320
321EXPORT_SYMBOL(uart_update_timeout);
322
323/**
324 * uart_get_baud_rate - return baud rate for a particular port
325 * @port: uart_port structure describing the port in question.
326 * @termios: desired termios settings.
327 * @old: old termios (or NULL)
328 * @min: minimum acceptable baud rate
329 * @max: maximum acceptable baud rate
330 *
331 * Decode the termios structure into a numeric baud rate,
332 * taking account of the magic 38400 baud rate (with spd_*
333 * flags), and mapping the %B0 rate to 9600 baud.
334 *
335 * If the new baud rate is invalid, try the old termios setting.
336 * If it's still invalid, we try 9600 baud.
337 *
338 * Update the @termios structure to reflect the baud rate
Alan Coxeb424fd2008-04-28 02:14:07 -0700339 * we're actually going to be using. Don't do this for the case
340 * where B0 is requested ("hang up").
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
342unsigned int
Alan Cox606d0992006-12-08 02:38:45 -0800343uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
344 struct ktermios *old, unsigned int min, unsigned int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned int try, baud, altbaud = 38400;
Alan Coxeb424fd2008-04-28 02:14:07 -0700347 int hung_up = 0;
Russell King0077d452006-01-21 23:03:28 +0000348 upf_t flags = port->flags & UPF_SPD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (flags == UPF_SPD_HI)
351 altbaud = 57600;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200352 else if (flags == UPF_SPD_VHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 altbaud = 115200;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200354 else if (flags == UPF_SPD_SHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 altbaud = 230400;
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -0200356 else if (flags == UPF_SPD_WARP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 altbaud = 460800;
358
359 for (try = 0; try < 2; try++) {
360 baud = tty_termios_baud_rate(termios);
361
362 /*
363 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
364 * Die! Die! Die!
365 */
366 if (baud == 38400)
367 baud = altbaud;
368
369 /*
370 * Special case: B0 rate.
371 */
Alan Coxeb424fd2008-04-28 02:14:07 -0700372 if (baud == 0) {
373 hung_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 baud = 9600;
Alan Coxeb424fd2008-04-28 02:14:07 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (baud >= min && baud <= max)
378 return baud;
379
380 /*
381 * Oops, the quotient was zero. Try again with
382 * the old baud rate if possible.
383 */
384 termios->c_cflag &= ~CBAUD;
385 if (old) {
Alan Cox6d4d67b2008-02-04 22:27:53 -0800386 baud = tty_termios_baud_rate(old);
Alan Coxeb424fd2008-04-28 02:14:07 -0700387 if (!hung_up)
388 tty_termios_encode_baud_rate(termios,
389 baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 old = NULL;
391 continue;
392 }
393
394 /*
Alan Cox16ae2a82010-01-04 16:26:21 +0000395 * As a last resort, if the range cannot be met then clip to
396 * the nearest chip supported rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Alan Cox16ae2a82010-01-04 16:26:21 +0000398 if (!hung_up) {
399 if (baud <= min)
400 tty_termios_encode_baud_rate(termios,
401 min + 1, min + 1);
402 else
403 tty_termios_encode_baud_rate(termios,
404 max - 1, max - 1);
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Alan Cox16ae2a82010-01-04 16:26:21 +0000407 /* Should never happen */
408 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410}
411
412EXPORT_SYMBOL(uart_get_baud_rate);
413
414/**
415 * uart_get_divisor - return uart clock divisor
416 * @port: uart_port structure describing the port.
417 * @baud: desired baud rate
418 *
419 * Calculate the uart clock divisor for the port.
420 */
421unsigned int
422uart_get_divisor(struct uart_port *port, unsigned int baud)
423{
424 unsigned int quot;
425
426 /*
427 * Old custom speed handling.
428 */
429 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
430 quot = port->custom_divisor;
431 else
Uwe Kleine-König97d24632011-12-20 11:47:44 +0100432 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return quot;
435}
436
437EXPORT_SYMBOL(uart_get_divisor);
438
Peter Hurley7c8ab962014-10-16 16:54:20 -0400439/* Caller holds port mutex */
Alan Cox19225132010-06-01 22:52:51 +0200440static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
441 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Alan Coxccce6de2009-09-19 13:13:30 -0700443 struct uart_port *uport = state->uart_port;
Alan Cox606d0992006-12-08 02:38:45 -0800444 struct ktermios *termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * If we have no tty, termios, or the port does not exist,
448 * then we can't set the parameters for this port.
449 */
Alan Coxadc8d742012-07-14 15:31:47 +0100450 if (!tty || uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return;
452
Alan Coxadc8d742012-07-14 15:31:47 +0100453 termios = &tty->termios;
Peter Hurleyc18b55f2014-06-16 09:17:09 -0400454 uport->ops->set_termios(uport, termios, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /*
Peter Hurley299245a2014-09-10 15:06:24 -0400457 * Set modem status enables based on termios cflag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
Peter Hurley299245a2014-09-10 15:06:24 -0400459 spin_lock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (termios->c_cflag & CRTSCTS)
Peter Hurley299245a2014-09-10 15:06:24 -0400461 uport->status |= UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 else
Peter Hurley299245a2014-09-10 15:06:24 -0400463 uport->status &= ~UPSTAT_CTS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (termios->c_cflag & CLOCAL)
Peter Hurley299245a2014-09-10 15:06:24 -0400466 uport->status &= ~UPSTAT_DCD_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 else
Peter Hurley299245a2014-09-10 15:06:24 -0400468 uport->status |= UPSTAT_DCD_ENABLE;
469 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Alan Cox19225132010-06-01 22:52:51 +0200472static inline int __uart_put_char(struct uart_port *port,
473 struct circ_buf *circ, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 unsigned long flags;
Alan Cox23d22ce2008-04-30 00:54:11 -0700476 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (!circ->buf)
Alan Cox23d22ce2008-04-30 00:54:11 -0700479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 spin_lock_irqsave(&port->lock, flags);
482 if (uart_circ_chars_free(circ) != 0) {
483 circ->buf[circ->head] = c;
484 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
Alan Cox23d22ce2008-04-30 00:54:11 -0700485 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox23d22ce2008-04-30 00:54:11 -0700488 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Alan Cox23d22ce2008-04-30 00:54:11 -0700491static int uart_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct uart_state *state = tty->driver_data;
494
Alan Coxebd2c8f2009-09-19 13:13:28 -0700495 return __uart_put_char(state->uart_port, &state->xmit, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static void uart_flush_chars(struct tty_struct *tty)
499{
500 uart_start(tty);
501}
502
Alan Cox19225132010-06-01 22:52:51 +0200503static int uart_write(struct tty_struct *tty,
504 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct uart_state *state = tty->driver_data;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800507 struct uart_port *port;
508 struct circ_buf *circ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 unsigned long flags;
510 int c, ret = 0;
511
Pavel Machekd5f735e2006-03-07 21:55:20 -0800512 /*
513 * This means you called this function _after_ the port was
514 * closed. No cookie for you.
515 */
Alan Coxf7519282009-01-02 13:49:21 +0000516 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800517 WARN_ON(1);
518 return -EL3HLT;
519 }
520
Alan Coxebd2c8f2009-09-19 13:13:28 -0700521 port = state->uart_port;
522 circ = &state->xmit;
Pavel Machekd5f735e2006-03-07 21:55:20 -0800523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!circ->buf)
525 return 0;
526
527 spin_lock_irqsave(&port->lock, flags);
528 while (1) {
529 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
530 if (count < c)
531 c = count;
532 if (c <= 0)
533 break;
534 memcpy(circ->buf + circ->head, buf, c);
535 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
536 buf += c;
537 count -= c;
538 ret += c;
539 }
540 spin_unlock_irqrestore(&port->lock, flags);
541
542 uart_start(tty);
543 return ret;
544}
545
546static int uart_write_room(struct tty_struct *tty)
547{
548 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700549 unsigned long flags;
550 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alan Coxebd2c8f2009-09-19 13:13:28 -0700552 spin_lock_irqsave(&state->uart_port->lock, flags);
553 ret = uart_circ_chars_free(&state->xmit);
554 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static int uart_chars_in_buffer(struct tty_struct *tty)
559{
560 struct uart_state *state = tty->driver_data;
Alan Coxf34d7a52008-04-30 00:54:13 -0700561 unsigned long flags;
562 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Alan Coxebd2c8f2009-09-19 13:13:28 -0700564 spin_lock_irqsave(&state->uart_port->lock, flags);
565 ret = uart_circ_chars_pending(&state->xmit);
566 spin_unlock_irqrestore(&state->uart_port->lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static void uart_flush_buffer(struct tty_struct *tty)
571{
572 struct uart_state *state = tty->driver_data;
Tetsuo Handa55d7b682008-05-06 20:42:27 -0700573 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unsigned long flags;
575
Pavel Machekd5f735e2006-03-07 21:55:20 -0800576 /*
577 * This means you called this function _after_ the port was
578 * closed. No cookie for you.
579 */
Alan Coxf7519282009-01-02 13:49:21 +0000580 if (!state) {
Pavel Machekd5f735e2006-03-07 21:55:20 -0800581 WARN_ON(1);
582 return;
583 }
584
Alan Coxebd2c8f2009-09-19 13:13:28 -0700585 port = state->uart_port;
Jiri Slabyeb3a1e12007-05-06 14:48:52 -0700586 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 spin_lock_irqsave(&port->lock, flags);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700589 uart_circ_clear(&state->xmit);
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100590 if (port->ops->flush_buffer)
591 port->ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_unlock_irqrestore(&port->lock, flags);
593 tty_wakeup(tty);
594}
595
596/*
597 * This function is used to send a high-priority XON/XOFF character to
598 * the device
599 */
600static void uart_send_xchar(struct tty_struct *tty, char ch)
601{
602 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700603 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned long flags;
605
606 if (port->ops->send_xchar)
607 port->ops->send_xchar(port, ch);
608 else {
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400609 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 port->x_char = ch;
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400611 if (ch)
Russell Kingb129a8c2005-08-31 10:12:14 +0100612 port->ops->start_tx(port);
Peter Hurleyc235ccc2014-09-02 17:39:13 -0400613 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615}
616
617static void uart_throttle(struct tty_struct *tty)
618{
619 struct uart_state *state = tty->driver_data;
Russell King9aba8d5b2012-04-17 17:23:14 +0100620 struct uart_port *port = state->uart_port;
Peter Hurley91f189de2014-10-16 14:19:48 -0400621 upf_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (I_IXOFF(tty))
Russell King9aba8d5b2012-04-17 17:23:14 +0100624 mask |= UPF_SOFT_FLOW;
625 if (tty->termios.c_cflag & CRTSCTS)
626 mask |= UPF_HARD_FLOW;
627
628 if (port->flags & mask) {
629 port->ops->throttle(port);
630 mask &= ~port->flags;
631 }
632
633 if (mask & UPF_SOFT_FLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 uart_send_xchar(tty, STOP_CHAR(tty));
635
Russell King9aba8d5b2012-04-17 17:23:14 +0100636 if (mask & UPF_HARD_FLOW)
637 uart_clear_mctrl(port, TIOCM_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640static void uart_unthrottle(struct tty_struct *tty)
641{
642 struct uart_state *state = tty->driver_data;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700643 struct uart_port *port = state->uart_port;
Peter Hurley91f189de2014-10-16 14:19:48 -0400644 upf_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Russell King9aba8d5b2012-04-17 17:23:14 +0100646 if (I_IXOFF(tty))
647 mask |= UPF_SOFT_FLOW;
648 if (tty->termios.c_cflag & CRTSCTS)
649 mask |= UPF_HARD_FLOW;
650
651 if (port->flags & mask) {
652 port->ops->unthrottle(port);
653 mask &= ~port->flags;
654 }
655
Peter Hurleyfba594a2014-09-02 17:39:14 -0400656 if (mask & UPF_SOFT_FLOW)
657 uart_send_xchar(tty, START_CHAR(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Russell King9aba8d5b2012-04-17 17:23:14 +0100659 if (mask & UPF_HARD_FLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 uart_set_mctrl(port, TIOCM_RTS);
661}
662
Alan Cox9f109692012-10-29 15:20:25 +0000663static void do_uart_get_info(struct tty_port *port,
Alan Cox7ba2e762012-09-04 16:34:45 +0100664 struct serial_struct *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Alan Cox9f109692012-10-29 15:20:25 +0000666 struct uart_state *state = container_of(port, struct uart_state, port);
Alan Coxa2bceae2009-09-19 13:13:31 -0700667 struct uart_port *uport = state->uart_port;
Alan Cox7ba2e762012-09-04 16:34:45 +0100668
Fengguang Wu37cd0c92012-09-06 10:27:51 +0800669 memset(retinfo, 0, sizeof(*retinfo));
Alan Cox7ba2e762012-09-04 16:34:45 +0100670
671 retinfo->type = uport->type;
672 retinfo->line = uport->line;
673 retinfo->port = uport->iobase;
674 if (HIGH_BITS_OFFSET)
675 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
676 retinfo->irq = uport->irq;
677 retinfo->flags = uport->flags;
678 retinfo->xmit_fifo_size = uport->fifosize;
679 retinfo->baud_base = uport->uartclk / 16;
680 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
681 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
682 ASYNC_CLOSING_WAIT_NONE :
683 jiffies_to_msecs(port->closing_wait) / 10;
684 retinfo->custom_divisor = uport->custom_divisor;
685 retinfo->hub6 = uport->hub6;
686 retinfo->io_type = uport->iotype;
687 retinfo->iomem_reg_shift = uport->regshift;
688 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
689}
690
Alan Cox9f109692012-10-29 15:20:25 +0000691static void uart_get_info(struct tty_port *port,
692 struct serial_struct *retinfo)
Alan Cox7ba2e762012-09-04 16:34:45 +0100693{
Alan Coxf34d7a52008-04-30 00:54:13 -0700694 /* Ensure the state we copy is consistent and no hardware changes
695 occur as we go */
Alan Coxa2bceae2009-09-19 13:13:31 -0700696 mutex_lock(&port->mutex);
Alan Cox9f109692012-10-29 15:20:25 +0000697 do_uart_get_info(port, retinfo);
Alan Coxa2bceae2009-09-19 13:13:31 -0700698 mutex_unlock(&port->mutex);
Alan Cox9f109692012-10-29 15:20:25 +0000699}
700
701static int uart_get_info_user(struct tty_port *port,
702 struct serial_struct __user *retinfo)
703{
704 struct serial_struct tmp;
705 uart_get_info(port, &tmp);
Alan Coxf34d7a52008-04-30 00:54:13 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
708 return -EFAULT;
709 return 0;
710}
711
Alan Cox7ba2e762012-09-04 16:34:45 +0100712static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
713 struct uart_state *state,
714 struct serial_struct *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Alan Cox46d57a42009-09-19 13:13:29 -0700716 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned long new_port;
Russell King0077d452006-01-21 23:03:28 +0000718 unsigned int change_irq, change_port, closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned int old_custom_divisor, close_delay;
Russell King0077d452006-01-21 23:03:28 +0000720 upf_t old_flags, new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int retval = 0;
722
Alan Cox7ba2e762012-09-04 16:34:45 +0100723 new_port = new_info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (HIGH_BITS_OFFSET)
Alan Cox7ba2e762012-09-04 16:34:45 +0100725 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alan Cox7ba2e762012-09-04 16:34:45 +0100727 new_info->irq = irq_canonicalize(new_info->irq);
728 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
729 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Jiri Slaby4cb0fbf2011-11-09 21:33:45 +0100730 ASYNC_CLOSING_WAIT_NONE :
Alan Cox7ba2e762012-09-04 16:34:45 +0100731 msecs_to_jiffies(new_info->closing_wait * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Alan Cox46d57a42009-09-19 13:13:29 -0700734 change_irq = !(uport->flags & UPF_FIXED_PORT)
Alan Cox7ba2e762012-09-04 16:34:45 +0100735 && new_info->irq != uport->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Since changing the 'type' of the port changes its resource
739 * allocations, we should treat type changes the same as
740 * IO port changes.
741 */
Alan Cox46d57a42009-09-19 13:13:29 -0700742 change_port = !(uport->flags & UPF_FIXED_PORT)
743 && (new_port != uport->iobase ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100744 (unsigned long)new_info->iomem_base != uport->mapbase ||
745 new_info->hub6 != uport->hub6 ||
746 new_info->io_type != uport->iotype ||
747 new_info->iomem_reg_shift != uport->regshift ||
748 new_info->type != uport->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Alan Cox46d57a42009-09-19 13:13:29 -0700750 old_flags = uport->flags;
Alan Cox7ba2e762012-09-04 16:34:45 +0100751 new_flags = new_info->flags;
Alan Cox46d57a42009-09-19 13:13:29 -0700752 old_custom_divisor = uport->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!capable(CAP_SYS_ADMIN)) {
755 retval = -EPERM;
756 if (change_irq || change_port ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100757 (new_info->baud_base != uport->uartclk / 16) ||
Alan Cox46d57a42009-09-19 13:13:29 -0700758 (close_delay != port->close_delay) ||
759 (closing_wait != port->closing_wait) ||
Alan Cox7ba2e762012-09-04 16:34:45 +0100760 (new_info->xmit_fifo_size &&
761 new_info->xmit_fifo_size != uport->fifosize) ||
Russell King0077d452006-01-21 23:03:28 +0000762 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto exit;
Alan Cox46d57a42009-09-19 13:13:29 -0700764 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000765 (new_flags & UPF_USR_MASK));
Alan Cox7ba2e762012-09-04 16:34:45 +0100766 uport->custom_divisor = new_info->custom_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto check_and_exit;
768 }
769
770 /*
771 * Ask the low level driver to verify the settings.
772 */
Alan Cox46d57a42009-09-19 13:13:29 -0700773 if (uport->ops->verify_port)
Alan Cox7ba2e762012-09-04 16:34:45 +0100774 retval = uport->ops->verify_port(uport, new_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Alan Cox7ba2e762012-09-04 16:34:45 +0100776 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
777 (new_info->baud_base < 9600))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 retval = -EINVAL;
779
780 if (retval)
781 goto exit;
782
783 if (change_port || change_irq) {
784 retval = -EBUSY;
785
786 /*
787 * Make sure that we are the sole user of this port.
788 */
Alan Coxb58d13a2009-09-19 13:13:32 -0700789 if (tty_port_users(port) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto exit;
791
792 /*
793 * We need to shutdown the serial port at the old
794 * port/type/irq combination.
795 */
Alan Cox19225132010-06-01 22:52:51 +0200796 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 if (change_port) {
800 unsigned long old_iobase, old_mapbase;
801 unsigned int old_type, old_iotype, old_hub6, old_shift;
802
Alan Cox46d57a42009-09-19 13:13:29 -0700803 old_iobase = uport->iobase;
804 old_mapbase = uport->mapbase;
805 old_type = uport->type;
806 old_hub6 = uport->hub6;
807 old_iotype = uport->iotype;
808 old_shift = uport->regshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * Free and release old regions
812 */
813 if (old_type != PORT_UNKNOWN)
Alan Cox46d57a42009-09-19 13:13:29 -0700814 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alan Cox46d57a42009-09-19 13:13:29 -0700816 uport->iobase = new_port;
Alan Cox7ba2e762012-09-04 16:34:45 +0100817 uport->type = new_info->type;
818 uport->hub6 = new_info->hub6;
819 uport->iotype = new_info->io_type;
820 uport->regshift = new_info->iomem_reg_shift;
821 uport->mapbase = (unsigned long)new_info->iomem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * Claim and map the new regions
825 */
Alan Cox46d57a42009-09-19 13:13:29 -0700826 if (uport->type != PORT_UNKNOWN) {
827 retval = uport->ops->request_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 } else {
829 /* Always success - Jean II */
830 retval = 0;
831 }
832
833 /*
834 * If we fail to request resources for the
835 * new port, try to restore the old settings.
836 */
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200837 if (retval) {
Alan Cox46d57a42009-09-19 13:13:29 -0700838 uport->iobase = old_iobase;
839 uport->type = old_type;
840 uport->hub6 = old_hub6;
841 uport->iotype = old_iotype;
842 uport->regshift = old_shift;
843 uport->mapbase = old_mapbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Thomas Pfaff7deb39e2014-04-23 12:33:22 +0200845 if (old_type != PORT_UNKNOWN) {
846 retval = uport->ops->request_port(uport);
847 /*
848 * If we failed to restore the old settings,
849 * we fail like this.
850 */
851 if (retval)
852 uport->type = PORT_UNKNOWN;
853
854 /*
855 * We failed anyway.
856 */
857 retval = -EBUSY;
858 }
859
Alan Coxa46c9992008-02-08 04:18:53 -0800860 /* Added to return the correct error -Ram Gupta */
861 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 }
864
David Gibsonabb4a232007-05-06 14:48:49 -0700865 if (change_irq)
Alan Cox7ba2e762012-09-04 16:34:45 +0100866 uport->irq = new_info->irq;
Alan Cox46d57a42009-09-19 13:13:29 -0700867 if (!(uport->flags & UPF_FIXED_PORT))
Alan Cox7ba2e762012-09-04 16:34:45 +0100868 uport->uartclk = new_info->baud_base * 16;
Alan Cox46d57a42009-09-19 13:13:29 -0700869 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
Russell King0077d452006-01-21 23:03:28 +0000870 (new_flags & UPF_CHANGE_MASK);
Alan Cox7ba2e762012-09-04 16:34:45 +0100871 uport->custom_divisor = new_info->custom_divisor;
Alan Cox46d57a42009-09-19 13:13:29 -0700872 port->close_delay = close_delay;
873 port->closing_wait = closing_wait;
Alan Cox7ba2e762012-09-04 16:34:45 +0100874 if (new_info->xmit_fifo_size)
875 uport->fifosize = new_info->xmit_fifo_size;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100876 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 check_and_exit:
879 retval = 0;
Alan Cox46d57a42009-09-19 13:13:29 -0700880 if (uport->type == PORT_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto exit;
Alan Coxccce6de2009-09-19 13:13:30 -0700882 if (port->flags & ASYNC_INITIALIZED) {
Alan Cox46d57a42009-09-19 13:13:29 -0700883 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
884 old_custom_divisor != uport->custom_divisor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * If they're setting up a custom divisor or speed,
887 * instead of clearing it, then bitch about it. No
888 * need to rate-limit; it's CAP_SYS_ADMIN only.
889 */
Alan Cox46d57a42009-09-19 13:13:29 -0700890 if (uport->flags & UPF_SPD_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 char buf[64];
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +0530892
893 dev_notice(uport->dev,
894 "%s sets custom speed on %s. This is deprecated.\n",
895 current->comm,
896 tty_name(port->tty, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Alan Cox19225132010-06-01 22:52:51 +0200898 uart_change_speed(tty, state, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 } else
Alan Cox19225132010-06-01 22:52:51 +0200901 retval = uart_startup(tty, state, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 exit:
Alan Cox7ba2e762012-09-04 16:34:45 +0100903 return retval;
904}
905
906static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
907 struct serial_struct __user *newinfo)
908{
909 struct serial_struct new_serial;
910 struct tty_port *port = &state->port;
911 int retval;
912
913 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
914 return -EFAULT;
915
916 /*
917 * This semaphore protects port->count. It is also
918 * very useful to prevent opens. Also, take the
919 * port configuration semaphore to make sure that a
920 * module insertion/removal doesn't change anything
921 * under us.
922 */
923 mutex_lock(&port->mutex);
924 retval = uart_set_info(tty, port, state, &new_serial);
Alan Coxa2bceae2009-09-19 13:13:31 -0700925 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return retval;
927}
928
Alan Cox19225132010-06-01 22:52:51 +0200929/**
930 * uart_get_lsr_info - get line status register info
931 * @tty: tty associated with the UART
932 * @state: UART being queried
933 * @value: returned modem value
934 *
935 * Note: uart_ioctl protects us against hangups.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Alan Cox19225132010-06-01 22:52:51 +0200937static int uart_get_lsr_info(struct tty_struct *tty,
938 struct uart_state *state, unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Alan Cox46d57a42009-09-19 13:13:29 -0700940 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned int result;
942
Alan Cox46d57a42009-09-19 13:13:29 -0700943 result = uport->ops->tx_empty(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
946 * If we're about to load something into the transmit
947 * register, we'll pretend the transmitter isn't empty to
948 * avoid a race condition (depending on when the transmit
949 * interrupt happens).
950 */
Alan Cox46d57a42009-09-19 13:13:29 -0700951 if (uport->x_char ||
Alan Coxebd2c8f2009-09-19 13:13:28 -0700952 ((uart_circ_chars_pending(&state->xmit) > 0) &&
Peter Hurleyd01f4d12014-09-10 15:06:26 -0400953 !uart_tx_stopped(uport)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 result &= ~TIOCSER_TEMT;
Alan Coxa46c9992008-02-08 04:18:53 -0800955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return put_user(result, value);
957}
958
Alan Cox60b33c12011-02-14 16:26:14 +0000959static int uart_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700962 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700963 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int result = -EIO;
965
Alan Coxa2bceae2009-09-19 13:13:31 -0700966 mutex_lock(&port->mutex);
Alan Cox60b33c12011-02-14 16:26:14 +0000967 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700968 result = uport->mctrl;
Alan Cox46d57a42009-09-19 13:13:29 -0700969 spin_lock_irq(&uport->lock);
970 result |= uport->ops->get_mctrl(uport);
971 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700973 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 return result;
976}
977
978static int
Alan Cox20b9d172011-02-14 16:26:50 +0000979uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -0700982 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -0700983 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int ret = -EIO;
985
Alan Coxa2bceae2009-09-19 13:13:31 -0700986 mutex_lock(&port->mutex);
Alan Cox20b9d172011-02-14 16:26:50 +0000987 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
Alan Cox46d57a42009-09-19 13:13:29 -0700988 uart_update_mctrl(uport, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ret = 0;
990 }
Alan Coxa2bceae2009-09-19 13:13:31 -0700991 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return ret;
993}
994
Alan Cox9e989662008-07-22 11:18:03 +0100995static int uart_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -0700998 struct tty_port *port = &state->port;
Alan Cox46d57a42009-09-19 13:13:29 -0700999 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Alan Coxa2bceae2009-09-19 13:13:31 -07001001 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Alan Cox46d57a42009-09-19 13:13:29 -07001003 if (uport->type != PORT_UNKNOWN)
1004 uport->ops->break_ctl(uport, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Alan Coxa2bceae2009-09-19 13:13:31 -07001006 mutex_unlock(&port->mutex);
Alan Cox9e989662008-07-22 11:18:03 +01001007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Alan Cox19225132010-06-01 22:52:51 +02001010static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Alan Cox46d57a42009-09-19 13:13:29 -07001012 struct uart_port *uport = state->uart_port;
Alan Coxa2bceae2009-09-19 13:13:31 -07001013 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int flags, ret;
1015
1016 if (!capable(CAP_SYS_ADMIN))
1017 return -EPERM;
1018
1019 /*
1020 * Take the per-port semaphore. This prevents count from
1021 * changing, and hence any extra opens of the port while
1022 * we're auto-configuring.
1023 */
Alan Coxa2bceae2009-09-19 13:13:31 -07001024 if (mutex_lock_interruptible(&port->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -ERESTARTSYS;
1026
1027 ret = -EBUSY;
Alan Coxb58d13a2009-09-19 13:13:32 -07001028 if (tty_port_users(port) == 1) {
Alan Cox19225132010-06-01 22:52:51 +02001029 uart_shutdown(tty, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /*
1032 * If we already have a port type configured,
1033 * we must release its resources.
1034 */
Alan Cox46d57a42009-09-19 13:13:29 -07001035 if (uport->type != PORT_UNKNOWN)
1036 uport->ops->release_port(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 flags = UART_CONFIG_TYPE;
Alan Cox46d57a42009-09-19 13:13:29 -07001039 if (uport->flags & UPF_AUTO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 flags |= UART_CONFIG_IRQ;
1041
1042 /*
1043 * This will claim the ports resources if
1044 * a port is found.
1045 */
Alan Cox46d57a42009-09-19 13:13:29 -07001046 uport->ops->config_port(uport, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Alan Cox19225132010-06-01 22:52:51 +02001048 ret = uart_startup(tty, state, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001050 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return ret;
1052}
1053
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001054static void uart_enable_ms(struct uart_port *uport)
1055{
1056 /*
1057 * Force modem status interrupts on
1058 */
1059 if (uport->ops->enable_ms)
1060 uport->ops->enable_ms(uport);
1061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/*
1064 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1065 * - mask passed in arg for lines of interest
1066 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1067 * Caller should use TIOCGICOUNT to see which one it was
Alan Coxbdc04e32009-09-19 13:13:31 -07001068 *
1069 * FIXME: This wants extracting into a common all driver implementation
1070 * of TIOCMWAIT using tty_port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072static int
1073uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1074{
Alan Cox46d57a42009-09-19 13:13:29 -07001075 struct uart_port *uport = state->uart_port;
Alan Coxbdc04e32009-09-19 13:13:31 -07001076 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 DECLARE_WAITQUEUE(wait, current);
1078 struct uart_icount cprev, cnow;
1079 int ret;
1080
1081 /*
1082 * note the counters on entry
1083 */
Alan Cox46d57a42009-09-19 13:13:29 -07001084 spin_lock_irq(&uport->lock);
1085 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001086 uart_enable_ms(uport);
Alan Cox46d57a42009-09-19 13:13:29 -07001087 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alan Coxbdc04e32009-09-19 13:13:31 -07001089 add_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 for (;;) {
Alan Cox46d57a42009-09-19 13:13:29 -07001091 spin_lock_irq(&uport->lock);
1092 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1093 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 set_current_state(TASK_INTERRUPTIBLE);
1096
1097 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1098 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1099 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1100 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Alan Coxa46c9992008-02-08 04:18:53 -08001101 ret = 0;
1102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
1105 schedule();
1106
1107 /* see if a signal did it */
1108 if (signal_pending(current)) {
1109 ret = -ERESTARTSYS;
1110 break;
1111 }
1112
1113 cprev = cnow;
1114 }
1115
1116 current->state = TASK_RUNNING;
Alan Coxbdc04e32009-09-19 13:13:31 -07001117 remove_wait_queue(&port->delta_msr_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return ret;
1120}
1121
1122/*
1123 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1124 * Return: write counters to the user passed counter struct
1125 * NB: both 1->0 and 0->1 transitions are counted except for
1126 * RI where only 0->1 is counted.
1127 */
Alan Coxd281da72010-09-16 18:21:24 +01001128static int uart_get_icount(struct tty_struct *tty,
1129 struct serial_icounter_struct *icount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Alan Coxd281da72010-09-16 18:21:24 +01001131 struct uart_state *state = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct uart_icount cnow;
Alan Cox46d57a42009-09-19 13:13:29 -07001133 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Alan Cox46d57a42009-09-19 13:13:29 -07001135 spin_lock_irq(&uport->lock);
1136 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1137 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Alan Coxd281da72010-09-16 18:21:24 +01001139 icount->cts = cnow.cts;
1140 icount->dsr = cnow.dsr;
1141 icount->rng = cnow.rng;
1142 icount->dcd = cnow.dcd;
1143 icount->rx = cnow.rx;
1144 icount->tx = cnow.tx;
1145 icount->frame = cnow.frame;
1146 icount->overrun = cnow.overrun;
1147 icount->parity = cnow.parity;
1148 icount->brk = cnow.brk;
1149 icount->buf_overrun = cnow.buf_overrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Alan Coxd281da72010-09-16 18:21:24 +01001151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154/*
Alan Coxe5238442008-04-30 00:53:28 -07001155 * Called via sys_ioctl. We can use spin_lock_irq() here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
1157static int
Alan Cox6caa76b2011-02-14 16:27:22 +00001158uart_ioctl(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 unsigned long arg)
1160{
1161 struct uart_state *state = tty->driver_data;
Alan Coxa2bceae2009-09-19 13:13:31 -07001162 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 void __user *uarg = (void __user *)arg;
1164 int ret = -ENOIOCTLCMD;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * These ioctls don't rely on the hardware to be present.
1169 */
1170 switch (cmd) {
1171 case TIOCGSERIAL:
Alan Cox9f109692012-10-29 15:20:25 +00001172 ret = uart_get_info_user(port, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174
1175 case TIOCSSERIAL:
Peter Hurley7c8ab962014-10-16 16:54:20 -04001176 down_write(&tty->termios_rwsem);
Alan Cox7ba2e762012-09-04 16:34:45 +01001177 ret = uart_set_info_user(tty, state, uarg);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001178 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180
1181 case TIOCSERCONFIG:
Peter Hurley7c8ab962014-10-16 16:54:20 -04001182 down_write(&tty->termios_rwsem);
Alan Cox19225132010-06-01 22:52:51 +02001183 ret = uart_do_autoconfig(tty, state);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001184 up_write(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186
1187 case TIOCSERGWILD: /* obsolete */
1188 case TIOCSERSWILD: /* obsolete */
1189 ret = 0;
1190 break;
1191 }
1192
1193 if (ret != -ENOIOCTLCMD)
1194 goto out;
1195
1196 if (tty->flags & (1 << TTY_IO_ERROR)) {
1197 ret = -EIO;
1198 goto out;
1199 }
1200
1201 /*
1202 * The following should only be used when hardware is present.
1203 */
1204 switch (cmd) {
1205 case TIOCMIWAIT:
1206 ret = uart_wait_modem_status(state, arg);
1207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 if (ret != -ENOIOCTLCMD)
1211 goto out;
1212
Alan Coxa2bceae2009-09-19 13:13:31 -07001213 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Alan Cox6caa76b2011-02-14 16:27:22 +00001215 if (tty->flags & (1 << TTY_IO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ret = -EIO;
1217 goto out_up;
1218 }
1219
1220 /*
1221 * All these rely on hardware being present and need to be
1222 * protected against the tty being hung up.
1223 */
1224 switch (cmd) {
1225 case TIOCSERGETLSR: /* Get line status register */
Alan Cox19225132010-06-01 22:52:51 +02001226 ret = uart_get_lsr_info(tty, state, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228
1229 default: {
Alan Cox46d57a42009-09-19 13:13:29 -07001230 struct uart_port *uport = state->uart_port;
1231 if (uport->ops->ioctl)
1232 ret = uport->ops->ioctl(uport, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 }
1235 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001236out_up:
Alan Coxa2bceae2009-09-19 13:13:31 -07001237 mutex_unlock(&port->mutex);
Alan Coxf34d7a52008-04-30 00:54:13 -07001238out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return ret;
1240}
1241
Linus Torvaldsedeb2802008-06-04 10:35:03 -07001242static void uart_set_ldisc(struct tty_struct *tty)
Alan Cox64e91592008-06-03 15:18:54 +01001243{
1244 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001245 struct uart_port *uport = state->uart_port;
Alan Cox64e91592008-06-03 15:18:54 +01001246
Alan Cox46d57a42009-09-19 13:13:29 -07001247 if (uport->ops->set_ldisc)
Alan Coxadc8d742012-07-14 15:31:47 +01001248 uport->ops->set_ldisc(uport, tty->termios.c_line);
Alan Cox64e91592008-06-03 15:18:54 +01001249}
1250
Alan Coxa46c9992008-02-08 04:18:53 -08001251static void uart_set_termios(struct tty_struct *tty,
1252 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct uart_state *state = tty->driver_data;
Russell Kingdec94e72012-09-24 11:13:15 +01001255 struct uart_port *uport = state->uart_port;
Alan Coxadc8d742012-07-14 15:31:47 +01001256 unsigned int cflag = tty->termios.c_cflag;
Russell King2cbacaf2012-04-17 16:34:13 +01001257 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1258 bool sw_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Russell King2cbacaf2012-04-17 16:34:13 +01001260 /*
1261 * Drivers doing software flow control also need to know
1262 * about changes to these input settings.
1263 */
1264 if (uport->flags & UPF_SOFT_FLOW) {
1265 iflag_mask |= IXANY|IXON|IXOFF;
1266 sw_changed =
1267 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1268 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /*
1272 * These are the bits that are used to setup various
David Woodhouse20620d62007-08-22 14:01:11 -07001273 * flags in the low level driver. We can ignore the Bfoo
1274 * bits in c_cflag; c_[io]speed will always be set
1275 * appropriately by set_termios() in tty_ioctl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if ((cflag ^ old_termios->c_cflag) == 0 &&
Alan Coxadc8d742012-07-14 15:31:47 +01001278 tty->termios.c_ospeed == old_termios->c_ospeed &&
1279 tty->termios.c_ispeed == old_termios->c_ispeed &&
Russell King2cbacaf2012-04-17 16:34:13 +01001280 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1281 !sw_changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return;
Alan Coxe5238442008-04-30 00:53:28 -07001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Peter Hurley7c8ab962014-10-16 16:54:20 -04001285 mutex_lock(&state->port.mutex);
Alan Cox19225132010-06-01 22:52:51 +02001286 uart_change_speed(tty, state, old_termios);
Peter Hurley7c8ab962014-10-16 16:54:20 -04001287 mutex_unlock(&state->port.mutex);
Peter Hurleyc18b55f2014-06-16 09:17:09 -04001288 /* reload cflag from termios; port driver may have overriden flags */
1289 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* Handle transition to B0 status */
1292 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
Russell Kingdec94e72012-09-24 11:13:15 +01001293 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* Handle transition away from B0 status */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001295 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned int mask = TIOCM_DTR;
Peter Hurley99abf3b2014-09-02 17:39:11 -04001297 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 mask |= TIOCM_RTS;
Russell Kingdec94e72012-09-24 11:13:15 +01001299 uart_set_mctrl(uport, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Russell Kingdba05832012-04-17 16:41:10 +01001302 /*
1303 * If the port is doing h/w assisted flow control, do nothing.
Peter Hurleyd01f4d12014-09-10 15:06:26 -04001304 * We assume that port->hw_stopped has never been set.
Russell Kingdba05832012-04-17 16:41:10 +01001305 */
1306 if (uport->flags & UPF_HARD_FLOW)
1307 return;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* Handle turning off CRTSCTS */
1310 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
Peter Hurley938f7e12014-09-10 15:06:29 -04001311 spin_lock_irq(&uport->lock);
Peter Hurleyd01f4d12014-09-10 15:06:26 -04001312 uport->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 __uart_start(tty);
Peter Hurley938f7e12014-09-10 15:06:29 -04001314 spin_unlock_irq(&uport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Russell King0dd7a1a2005-06-29 18:40:53 +01001316 /* Handle turning on CRTSCTS */
André Goddard Rosa82cb7ba2009-10-25 11:18:26 -02001317 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
Peter Hurley938f7e12014-09-10 15:06:29 -04001318 spin_lock_irq(&uport->lock);
Russell Kingdec94e72012-09-24 11:13:15 +01001319 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04001320 uport->hw_stopped = 1;
Russell Kingdec94e72012-09-24 11:13:15 +01001321 uport->ops->stop_tx(uport);
Russell King0dd7a1a2005-06-29 18:40:53 +01001322 }
Peter Hurley938f7e12014-09-10 15:06:29 -04001323 spin_unlock_irq(&uport->lock);
Russell King0dd7a1a2005-06-29 18:40:53 +01001324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001328 * Calls to uart_close() are serialised via the tty_lock in
1329 * drivers/tty/tty_io.c:tty_release()
1330 * drivers/tty/tty_io.c:do_tty_hangup()
1331 * This runs from a workqueue and can sleep for a _short_ time only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
1333static void uart_close(struct tty_struct *tty, struct file *filp)
1334{
1335 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001336 struct tty_port *port;
1337 struct uart_port *uport;
Alan Cox61cd8a22010-06-01 22:52:57 +02001338 unsigned long flags;
Alan Coxa46c9992008-02-08 04:18:53 -08001339
Linus Torvaldseea7e172009-10-12 19:13:54 +02001340 if (!state)
1341 return;
1342
Alan Cox46d57a42009-09-19 13:13:29 -07001343 uport = state->uart_port;
1344 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Geert Uytterhoeven4ed94cd2014-03-17 14:10:59 +01001346 pr_debug("uart_close(%d) called\n", uport ? uport->line : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Geert Uytterhoeven4ed94cd2014-03-17 14:10:59 +01001348 if (!port->count || tty_port_close_start(port, tty, filp) == 0)
Nobuhiro Iwamatsu55956212011-08-29 15:43:36 +09001349 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /*
1352 * At this point, we stop accepting input. To do this, we
1353 * disable the receive line status interrupts.
1354 */
Alan Coxccce6de2009-09-19 13:13:30 -07001355 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned long flags;
Linus Torvaldseea7e172009-10-12 19:13:54 +02001357 spin_lock_irqsave(&uport->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001358 uport->ops->stop_rx(uport);
Linus Torvaldseea7e172009-10-12 19:13:54 +02001359 spin_unlock_irqrestore(&uport->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * Before we drop DTR, make sure the UART transmitter
1362 * has completely drained; this is especially
1363 * important if there is a transmit FIFO!
1364 */
Jiri Slaby1f33a512011-07-14 14:35:10 +02001365 uart_wait_until_sent(tty, uport->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Jiri Slabybafb0bd2011-08-25 15:12:05 +02001368 mutex_lock(&port->mutex);
Alan Cox19225132010-06-01 22:52:51 +02001369 uart_shutdown(tty, state);
Alan Cox7b014782009-09-19 13:13:33 -07001370 tty_port_tty_set(port, NULL);
Alan Cox61cd8a22010-06-01 22:52:57 +02001371 tty->closing = 0;
Peter Hurleyddc7b752014-06-16 09:17:03 -04001372 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Alan Cox46d57a42009-09-19 13:13:29 -07001374 if (port->blocked_open) {
Alan Cox61cd8a22010-06-01 22:52:57 +02001375 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001376 if (port->close_delay)
Jiri Slaby4cb0fbf2011-11-09 21:33:45 +01001377 msleep_interruptible(
1378 jiffies_to_msecs(port->close_delay));
Alan Cox61cd8a22010-06-01 22:52:57 +02001379 spin_lock_irqsave(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001380 } else if (!uart_console(uport)) {
Alan Cox61cd8a22010-06-01 22:52:57 +02001381 spin_unlock_irqrestore(&port->lock, flags);
Linus Walleij6f538fe2012-12-07 11:36:08 +01001382 uart_change_pm(state, UART_PM_STATE_OFF);
Alan Cox61cd8a22010-06-01 22:52:57 +02001383 spin_lock_irqsave(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386 /*
1387 * Wake up anyone trying to open this port.
1388 */
Alan Coxccce6de2009-09-19 13:13:30 -07001389 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slaby426929f2011-08-25 15:12:04 +02001390 clear_bit(ASYNCB_CLOSING, &port->flags);
Alan Cox61cd8a22010-06-01 22:52:57 +02001391 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox46d57a42009-09-19 13:13:29 -07001392 wake_up_interruptible(&port->open_wait);
Jiri Slaby426929f2011-08-25 15:12:04 +02001393 wake_up_interruptible(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Alan Coxa2bceae2009-09-19 13:13:31 -07001395 mutex_unlock(&port->mutex);
Peter Hurley2e758912014-10-16 16:54:19 -04001396
1397 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Jiri Slaby1f33a512011-07-14 14:35:10 +02001400static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Jiri Slaby1f33a512011-07-14 14:35:10 +02001402 struct uart_state *state = tty->driver_data;
1403 struct uart_port *port = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned long char_time, expire;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1407 return;
1408
1409 /*
1410 * Set the check interval to be 1/5 of the estimated time to
1411 * send a single character, and make it at least 1. The check
1412 * interval should also be less than the timeout.
1413 *
1414 * Note: we have to use pretty tight timings here to satisfy
1415 * the NIST-PCTS.
1416 */
1417 char_time = (port->timeout - HZ/50) / port->fifosize;
1418 char_time = char_time / 5;
1419 if (char_time == 0)
1420 char_time = 1;
1421 if (timeout && timeout < char_time)
1422 char_time = timeout;
1423
1424 /*
1425 * If the transmitter hasn't cleared in twice the approximate
1426 * amount of time to send the entire FIFO, it probably won't
1427 * ever clear. This assumes the UART isn't doing flow
1428 * control, which is currently the case. Hence, if it ever
1429 * takes longer than port->timeout, this is probably due to a
1430 * UART bug of some kind. So, we clamp the timeout parameter at
1431 * 2*port->timeout.
1432 */
1433 if (timeout == 0 || timeout > 2 * port->timeout)
1434 timeout = 2 * port->timeout;
1435
1436 expire = jiffies + timeout;
1437
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001438 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
Alan Coxa46c9992008-02-08 04:18:53 -08001439 port->line, jiffies, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
1442 * Check whether the transmitter is empty every 'char_time'.
1443 * 'timeout' / 'expire' give us the maximum amount of time
1444 * we wait.
1445 */
1446 while (!port->ops->tx_empty(port)) {
1447 msleep_interruptible(jiffies_to_msecs(char_time));
1448 if (signal_pending(current))
1449 break;
1450 if (time_after(jiffies, expire))
1451 break;
1452 }
Arnd Bergmann20365212010-06-01 22:53:07 +02001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001456 * Calls to uart_hangup() are serialised by the tty_lock in
1457 * drivers/tty/tty_io.c:do_tty_hangup()
1458 * This runs from a workqueue and can sleep for a _short_ time only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
1460static void uart_hangup(struct tty_struct *tty)
1461{
1462 struct uart_state *state = tty->driver_data;
Alan Cox46d57a42009-09-19 13:13:29 -07001463 struct tty_port *port = &state->port;
Alan Cox61cd8a22010-06-01 22:52:57 +02001464 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Alan Coxebd2c8f2009-09-19 13:13:28 -07001466 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Alan Coxa2bceae2009-09-19 13:13:31 -07001468 mutex_lock(&port->mutex);
Alan Coxccce6de2009-09-19 13:13:30 -07001469 if (port->flags & ASYNC_NORMAL_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 uart_flush_buffer(tty);
Alan Cox19225132010-06-01 22:52:51 +02001471 uart_shutdown(tty, state);
Alan Cox61cd8a22010-06-01 22:52:57 +02001472 spin_lock_irqsave(&port->lock, flags);
Alan Cox91312cd2009-09-19 13:13:29 -07001473 port->count = 0;
Alan Coxccce6de2009-09-19 13:13:30 -07001474 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Alan Cox61cd8a22010-06-01 22:52:57 +02001475 spin_unlock_irqrestore(&port->lock, flags);
Alan Cox7b014782009-09-19 13:13:33 -07001476 tty_port_tty_set(port, NULL);
Geert Uytterhoevenbf903c02014-03-27 11:40:39 +01001477 if (!uart_console(state->uart_port))
1478 uart_change_pm(state, UART_PM_STATE_OFF);
Alan Cox46d57a42009-09-19 13:13:29 -07001479 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -07001480 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Alan Coxa2bceae2009-09-19 13:13:31 -07001482 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Jiri Slaby0b1db832011-11-09 21:33:50 +01001485static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1486{
1487 return 0;
1488}
1489
1490static void uart_port_shutdown(struct tty_port *port)
1491{
Jiri Slabyb922e192011-11-09 21:33:51 +01001492 struct uart_state *state = container_of(port, struct uart_state, port);
1493 struct uart_port *uport = state->uart_port;
1494
1495 /*
1496 * clear delta_msr_wait queue to avoid mem leaks: we may free
1497 * the irq here so the queue might never be woken up. Note
1498 * that we won't end up waiting on delta_msr_wait again since
1499 * any outstanding file descriptors should be pointing at
1500 * hung_up_tty_fops now.
1501 */
1502 wake_up_interruptible(&port->delta_msr_wait);
1503
1504 /*
1505 * Free the IRQ and disable the port.
1506 */
1507 uport->ops->shutdown(uport);
1508
1509 /*
1510 * Ensure that the IRQ handler isn't running on another CPU.
1511 */
1512 synchronize_irq(uport->irq);
Jiri Slaby0b1db832011-11-09 21:33:50 +01001513}
1514
Alan Coxde0c8cb2010-06-01 22:52:58 +02001515static int uart_carrier_raised(struct tty_port *port)
1516{
1517 struct uart_state *state = container_of(port, struct uart_state, port);
1518 struct uart_port *uport = state->uart_port;
1519 int mctrl;
Alan Coxde0c8cb2010-06-01 22:52:58 +02001520 spin_lock_irq(&uport->lock);
Alexander Shiyan1fdc3102014-06-03 18:54:43 +04001521 uart_enable_ms(uport);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001522 mctrl = uport->ops->get_mctrl(uport);
1523 spin_unlock_irq(&uport->lock);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001524 if (mctrl & TIOCM_CAR)
1525 return 1;
1526 return 0;
1527}
1528
1529static void uart_dtr_rts(struct tty_port *port, int onoff)
1530{
1531 struct uart_state *state = container_of(port, struct uart_state, port);
1532 struct uart_port *uport = state->uart_port;
Alan Cox24fcc7c2010-06-01 22:52:59 +02001533
Jiri Slaby6f5c24a2011-03-30 00:10:57 +02001534 if (onoff)
Alan Coxde0c8cb2010-06-01 22:52:58 +02001535 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1536 else
1537 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
Alan Coxde0c8cb2010-06-01 22:52:58 +02001538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/*
Kevin Cernekeeef4f5272012-12-26 20:43:41 -08001541 * Calls to uart_open are serialised by the tty_lock in
1542 * drivers/tty/tty_io.c:tty_open()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * Note that if this fails, then uart_close() _will_ be called.
1544 *
1545 * In time, we want to scrap the "opening nonpresent ports"
1546 * behaviour and implement an alternative way for setserial
1547 * to set base addresses/ports/types. This will allow us to
1548 * get rid of a certain amount of extra tests.
1549 */
1550static int uart_open(struct tty_struct *tty, struct file *filp)
1551{
1552 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int retval, line = tty->index;
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001554 struct uart_state *state = drv->state + line;
1555 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jiri Slabyeb3a1e12007-05-06 14:48:52 -07001557 pr_debug("uart_open(%d) called\n", line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /*
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001560 * We take the semaphore here to guarantee that we won't be re-entered
1561 * while allocating the state structure, or while we request any IRQs
1562 * that the driver may need. This also has the nice side-effect that
1563 * it delays the action of uart_hangup, so we can guarantee that
1564 * state->port.tty will always contain something reasonable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001566 if (mutex_lock_interruptible(&port->mutex)) {
1567 retval = -ERESTARTSYS;
1568 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001570
1571 port->count++;
1572 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1573 retval = -ENXIO;
1574 goto err_dec_count;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
1578 * Once we set tty->driver_data here, we are guaranteed that
1579 * uart_close() will decrement the driver module use count.
1580 * Any failures from here onwards should not touch the count.
1581 */
1582 tty->driver_data = state;
Alan Coxebd2c8f2009-09-19 13:13:28 -07001583 state->uart_port->state = state;
Jiri Slabyd6c53c02013-01-03 15:53:05 +01001584 state->port.low_latency =
1585 (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
Alan Cox7b014782009-09-19 13:13:33 -07001586 tty_port_tty_set(port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 * Start up the serial port.
1590 */
Alan Cox19225132010-06-01 22:52:51 +02001591 retval = uart_startup(tty, state, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /*
1594 * If we succeeded, wait until the port is ready.
1595 */
Alan Cox61cd8a22010-06-01 22:52:57 +02001596 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (retval == 0)
Alan Cox74c21072010-06-01 22:53:00 +02001598 retval = tty_port_block_til_ready(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001600end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return retval;
Jiri Slaby1c7b13c2011-11-09 21:33:49 +01001602err_dec_count:
1603 port->count--;
1604 mutex_unlock(&port->mutex);
1605 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608static const char *uart_type(struct uart_port *port)
1609{
1610 const char *str = NULL;
1611
1612 if (port->ops->type)
1613 str = port->ops->type(port);
1614
1615 if (!str)
1616 str = "unknown";
1617
1618 return str;
1619}
1620
1621#ifdef CONFIG_PROC_FS
1622
Alexey Dobriyand196a942009-03-31 15:19:21 -07001623static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07001626 struct tty_port *port = &state->port;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001627 enum uart_pm_state pm_state;
Alan Coxa2bceae2009-09-19 13:13:31 -07001628 struct uart_port *uport = state->uart_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 char stat_buf[32];
1630 unsigned int status;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001631 int mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Alan Coxa2bceae2009-09-19 13:13:31 -07001633 if (!uport)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Alan Coxa2bceae2009-09-19 13:13:31 -07001636 mmio = uport->iotype >= UPIO_MEM;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001637 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001638 uport->line, uart_type(uport),
Sergei Shtylyov6c6a2332006-09-04 00:04:20 +04001639 mmio ? "mmio:0x" : "port:",
Alan Coxa2bceae2009-09-19 13:13:31 -07001640 mmio ? (unsigned long long)uport->mapbase
1641 : (unsigned long long)uport->iobase,
1642 uport->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Alan Coxa2bceae2009-09-19 13:13:31 -07001644 if (uport->type == PORT_UNKNOWN) {
Alexey Dobriyand196a942009-03-31 15:19:21 -07001645 seq_putc(m, '\n');
1646 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Alan Coxa46c9992008-02-08 04:18:53 -08001649 if (capable(CAP_SYS_ADMIN)) {
Alan Coxa2bceae2009-09-19 13:13:31 -07001650 mutex_lock(&port->mutex);
George G. Davis3689a0e2007-02-14 00:33:06 -08001651 pm_state = state->pm_state;
Linus Walleij6f538fe2012-12-07 11:36:08 +01001652 if (pm_state != UART_PM_STATE_ON)
1653 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxa2bceae2009-09-19 13:13:31 -07001654 spin_lock_irq(&uport->lock);
1655 status = uport->ops->get_mctrl(uport);
1656 spin_unlock_irq(&uport->lock);
Linus Walleij6f538fe2012-12-07 11:36:08 +01001657 if (pm_state != UART_PM_STATE_ON)
George G. Davis3689a0e2007-02-14 00:33:06 -08001658 uart_change_pm(state, pm_state);
Alan Coxa2bceae2009-09-19 13:13:31 -07001659 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Alexey Dobriyand196a942009-03-31 15:19:21 -07001661 seq_printf(m, " tx:%d rx:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001662 uport->icount.tx, uport->icount.rx);
1663 if (uport->icount.frame)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001664 seq_printf(m, " fe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001665 uport->icount.frame);
1666 if (uport->icount.parity)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001667 seq_printf(m, " pe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001668 uport->icount.parity);
1669 if (uport->icount.brk)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001670 seq_printf(m, " brk:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001671 uport->icount.brk);
1672 if (uport->icount.overrun)
Alexey Dobriyand196a942009-03-31 15:19:21 -07001673 seq_printf(m, " oe:%d",
Alan Coxa2bceae2009-09-19 13:13:31 -07001674 uport->icount.overrun);
Alan Coxa46c9992008-02-08 04:18:53 -08001675
1676#define INFOBIT(bit, str) \
Alan Coxa2bceae2009-09-19 13:13:31 -07001677 if (uport->mctrl & (bit)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 strncat(stat_buf, (str), sizeof(stat_buf) - \
1679 strlen(stat_buf) - 2)
Alan Coxa46c9992008-02-08 04:18:53 -08001680#define STATBIT(bit, str) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (status & (bit)) \
1682 strncat(stat_buf, (str), sizeof(stat_buf) - \
1683 strlen(stat_buf) - 2)
1684
1685 stat_buf[0] = '\0';
1686 stat_buf[1] = '\0';
1687 INFOBIT(TIOCM_RTS, "|RTS");
1688 STATBIT(TIOCM_CTS, "|CTS");
1689 INFOBIT(TIOCM_DTR, "|DTR");
1690 STATBIT(TIOCM_DSR, "|DSR");
1691 STATBIT(TIOCM_CAR, "|CD");
1692 STATBIT(TIOCM_RNG, "|RI");
1693 if (stat_buf[0])
1694 stat_buf[0] = ' ';
Alan Coxa46c9992008-02-08 04:18:53 -08001695
Alexey Dobriyand196a942009-03-31 15:19:21 -07001696 seq_puts(m, stat_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Alexey Dobriyand196a942009-03-31 15:19:21 -07001698 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699#undef STATBIT
1700#undef INFOBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Alexey Dobriyand196a942009-03-31 15:19:21 -07001703static int uart_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Alexey Dobriyan833bb302009-04-02 01:30:04 +04001705 struct tty_driver *ttydrv = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 struct uart_driver *drv = ttydrv->driver_state;
Alexey Dobriyand196a942009-03-31 15:19:21 -07001707 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Alexey Dobriyand196a942009-03-31 15:19:21 -07001709 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 "", "", "");
Alexey Dobriyand196a942009-03-31 15:19:21 -07001711 for (i = 0; i < drv->nr; i++)
1712 uart_line_info(m, drv, i);
1713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
Alexey Dobriyand196a942009-03-31 15:19:21 -07001715
1716static int uart_proc_open(struct inode *inode, struct file *file)
1717{
Al Virod9dda782013-03-31 18:16:14 -04001718 return single_open(file, uart_proc_show, PDE_DATA(inode));
Alexey Dobriyand196a942009-03-31 15:19:21 -07001719}
1720
1721static const struct file_operations uart_proc_fops = {
1722 .owner = THIS_MODULE,
1723 .open = uart_proc_open,
1724 .read = seq_read,
1725 .llseek = seq_lseek,
1726 .release = single_release,
1727};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728#endif
1729
Andrew Morton4a1b5502008-03-07 15:51:16 -08001730#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/*
Russell Kingd3587882006-03-20 20:00:09 +00001732 * uart_console_write - write a console message to a serial port
1733 * @port: the port to write the message
1734 * @s: array of characters
1735 * @count: number of characters in string to write
1736 * @write: function to write character to port
1737 */
1738void uart_console_write(struct uart_port *port, const char *s,
1739 unsigned int count,
1740 void (*putchar)(struct uart_port *, int))
1741{
1742 unsigned int i;
1743
1744 for (i = 0; i < count; i++, s++) {
1745 if (*s == '\n')
1746 putchar(port, '\r');
1747 putchar(port, *s);
1748 }
1749}
1750EXPORT_SYMBOL_GPL(uart_console_write);
1751
1752/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * Check whether an invalid uart number has been specified, and
1754 * if so, search for the first available port that does have
1755 * console support.
1756 */
1757struct uart_port * __init
1758uart_get_console(struct uart_port *ports, int nr, struct console *co)
1759{
1760 int idx = co->index;
1761
1762 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1763 ports[idx].membase == NULL))
1764 for (idx = 0; idx < nr; idx++)
1765 if (ports[idx].iobase != 0 ||
1766 ports[idx].membase != NULL)
1767 break;
1768
1769 co->index = idx;
1770
1771 return ports + idx;
1772}
1773
1774/**
Geert Uytterhoeven02088ca2014-03-11 11:23:35 +01001775 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * @options: pointer to option string
1777 * @baud: pointer to an 'int' variable for the baud rate.
1778 * @parity: pointer to an 'int' variable for the parity.
1779 * @bits: pointer to an 'int' variable for the number of data bits.
1780 * @flow: pointer to an 'int' variable for the flow control character.
1781 *
1782 * uart_parse_options decodes a string containing the serial console
1783 * options. The format of the string is <baud><parity><bits><flow>,
1784 * eg: 115200n8r
1785 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001786void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1788{
1789 char *s = options;
1790
1791 *baud = simple_strtoul(s, NULL, 10);
1792 while (*s >= '0' && *s <= '9')
1793 s++;
1794 if (*s)
1795 *parity = *s++;
1796 if (*s)
1797 *bits = *s++ - '0';
1798 if (*s)
1799 *flow = *s;
1800}
Jason Wesself2d937f2008-04-17 20:05:37 +02001801EXPORT_SYMBOL_GPL(uart_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803struct baud_rates {
1804 unsigned int rate;
1805 unsigned int cflag;
1806};
1807
Arjan van de Vencb3592b2005-11-28 21:04:11 +00001808static const struct baud_rates baud_rates[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 { 921600, B921600 },
1810 { 460800, B460800 },
1811 { 230400, B230400 },
1812 { 115200, B115200 },
1813 { 57600, B57600 },
1814 { 38400, B38400 },
1815 { 19200, B19200 },
1816 { 9600, B9600 },
1817 { 4800, B4800 },
1818 { 2400, B2400 },
1819 { 1200, B1200 },
1820 { 0, B38400 }
1821};
1822
1823/**
1824 * uart_set_options - setup the serial console parameters
1825 * @port: pointer to the serial ports uart_port structure
1826 * @co: console pointer
1827 * @baud: baud rate
1828 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1829 * @bits: number of data bits
1830 * @flow: flow control character - 'r' (rts)
1831 */
Jason Wesself2d937f2008-04-17 20:05:37 +02001832int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833uart_set_options(struct uart_port *port, struct console *co,
1834 int baud, int parity, int bits, int flow)
1835{
Alan Cox606d0992006-12-08 02:38:45 -08001836 struct ktermios termios;
Alan Cox149b36e2007-10-18 01:24:16 -07001837 static struct ktermios dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int i;
1839
Russell King976ecd12005-07-03 21:05:45 +01001840 /*
1841 * Ensure that the serial console lock is initialised
1842 * early.
Randy Witt42b6a1b2013-10-17 16:56:47 -04001843 * If this port is a console, then the spinlock is already
1844 * initialised.
Russell King976ecd12005-07-03 21:05:45 +01001845 */
Randy Witt42b6a1b2013-10-17 16:56:47 -04001846 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1847 spin_lock_init(&port->lock);
1848 lockdep_set_class(&port->lock, &port_lock_key);
1849 }
Russell King976ecd12005-07-03 21:05:45 +01001850
Alan Cox606d0992006-12-08 02:38:45 -08001851 memset(&termios, 0, sizeof(struct ktermios));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1854
1855 /*
1856 * Construct a cflag setting.
1857 */
1858 for (i = 0; baud_rates[i].rate; i++)
1859 if (baud_rates[i].rate <= baud)
1860 break;
1861
1862 termios.c_cflag |= baud_rates[i].cflag;
1863
1864 if (bits == 7)
1865 termios.c_cflag |= CS7;
1866 else
1867 termios.c_cflag |= CS8;
1868
1869 switch (parity) {
1870 case 'o': case 'O':
1871 termios.c_cflag |= PARODD;
1872 /*fall through*/
1873 case 'e': case 'E':
1874 termios.c_cflag |= PARENB;
1875 break;
1876 }
1877
1878 if (flow == 'r')
1879 termios.c_cflag |= CRTSCTS;
1880
Yinghai Lu79492682007-07-15 23:37:25 -07001881 /*
1882 * some uarts on other side don't support no flow control.
1883 * So we set * DTR in host uart to make them happy
1884 */
1885 port->mctrl |= TIOCM_DTR;
1886
Alan Cox149b36e2007-10-18 01:24:16 -07001887 port->ops->set_termios(port, &termios, &dummy);
Jason Wesself2d937f2008-04-17 20:05:37 +02001888 /*
1889 * Allow the setting of the UART parameters with a NULL console
1890 * too:
1891 */
1892 if (co)
1893 co->cflag = termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 return 0;
1896}
Jason Wesself2d937f2008-04-17 20:05:37 +02001897EXPORT_SYMBOL_GPL(uart_set_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1899
Jiri Slabycf755252011-11-09 21:33:47 +01001900/**
1901 * uart_change_pm - set power state of the port
1902 *
1903 * @state: port descriptor
1904 * @pm_state: new state
1905 *
1906 * Locking: port->mutex has to be held
1907 */
Linus Walleij6f538fe2012-12-07 11:36:08 +01001908static void uart_change_pm(struct uart_state *state,
1909 enum uart_pm_state pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001911 struct uart_port *port = state->uart_port;
Andrew Victor1281e362006-05-16 11:28:49 +01001912
1913 if (state->pm_state != pm_state) {
1914 if (port->ops->pm)
1915 port->ops->pm(port, pm_state, state->pm_state);
1916 state->pm_state = pm_state;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001920struct uart_match {
1921 struct uart_port *port;
1922 struct uart_driver *driver;
1923};
1924
1925static int serial_match_port(struct device *dev, void *data)
1926{
1927 struct uart_match *match = data;
Guennadi Liakhovetski7ca796f2008-07-04 09:59:28 -07001928 struct tty_driver *tty_drv = match->driver->tty_driver;
1929 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1930 match->port->line;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001931
1932 return dev->devt == devt; /* Actually, only one tty per port */
1933}
1934
Alan Coxccce6de2009-09-19 13:13:30 -07001935int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Alan Coxccce6de2009-09-19 13:13:30 -07001937 struct uart_state *state = drv->state + uport->line;
1938 struct tty_port *port = &state->port;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001939 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07001940 struct uart_match match = {uport, drv};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Alan Coxa2bceae2009-09-19 13:13:31 -07001942 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Alan Coxccce6de2009-09-19 13:13:30 -07001944 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001945 if (device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05301946 if (!enable_irq_wake(uport->irq))
1947 uport->irq_wake = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001948 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07001949 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001950 return 0;
1951 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02001952 put_device(tty_dev);
1953
Stanislav Brabec4547be72009-12-02 16:20:56 +01001954 if (console_suspend_enabled || !uart_console(uport))
1955 uport->suspended = 1;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07001956
Alan Coxccce6de2009-09-19 13:13:30 -07001957 if (port->flags & ASYNC_INITIALIZED) {
1958 const struct uart_ops *ops = uport->ops;
Russell Kingc8c6bfa2008-02-04 22:27:52 -08001959 int tries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Stanislav Brabec4547be72009-12-02 16:20:56 +01001961 if (console_suspend_enabled || !uart_console(uport)) {
1962 set_bit(ASYNCB_SUSPENDED, &port->flags);
1963 clear_bit(ASYNCB_INITIALIZED, &port->flags);
Russell Kinga6b93a92006-10-01 17:17:40 +01001964
Stanislav Brabec4547be72009-12-02 16:20:56 +01001965 spin_lock_irq(&uport->lock);
1966 ops->stop_tx(uport);
1967 ops->set_mctrl(uport, 0);
1968 ops->stop_rx(uport);
1969 spin_unlock_irq(&uport->lock);
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /*
1973 * Wait for the transmitter to empty.
1974 */
Alan Coxccce6de2009-09-19 13:13:30 -07001975 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 msleep(10);
Russell Kingc8c6bfa2008-02-04 22:27:52 -08001977 if (!tries)
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05301978 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
1979 drv->dev_name,
1980 drv->tty_driver->name_base + uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Stanislav Brabec4547be72009-12-02 16:20:56 +01001982 if (console_suspend_enabled || !uart_console(uport))
1983 ops->shutdown(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
1986 /*
1987 * Disable the console device before suspending.
1988 */
Stanislav Brabec4547be72009-12-02 16:20:56 +01001989 if (console_suspend_enabled && uart_console(uport))
Alan Coxccce6de2009-09-19 13:13:30 -07001990 console_stop(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Stanislav Brabec4547be72009-12-02 16:20:56 +01001992 if (console_suspend_enabled || !uart_console(uport))
Linus Walleij6f538fe2012-12-07 11:36:08 +01001993 uart_change_pm(state, UART_PM_STATE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Alan Coxa2bceae2009-09-19 13:13:31 -07001995 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 return 0;
1998}
1999
Alan Coxccce6de2009-09-19 13:13:30 -07002000int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Alan Coxccce6de2009-09-19 13:13:30 -07002002 struct uart_state *state = drv->state + uport->line;
2003 struct tty_port *port = &state->port;
Arjan van de Ven03a74dc2008-05-23 13:04:49 -07002004 struct device *tty_dev;
Alan Coxccce6de2009-09-19 13:13:30 -07002005 struct uart_match match = {uport, drv};
Deepak Saxenaba15ab02009-09-19 13:13:33 -07002006 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Alan Coxa2bceae2009-09-19 13:13:31 -07002008 mutex_lock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alan Coxccce6de2009-09-19 13:13:30 -07002010 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2011 if (!uport->suspended && device_may_wakeup(tty_dev)) {
Govindraj.R3f960db2010-12-16 18:12:47 +05302012 if (uport->irq_wake) {
2013 disable_irq_wake(uport->irq);
2014 uport->irq_wake = 0;
2015 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02002016 put_device(tty_dev);
Alan Coxa2bceae2009-09-19 13:13:31 -07002017 mutex_unlock(&port->mutex);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002018 return 0;
2019 }
Federico Vaga5a65dcc2013-04-15 16:01:07 +02002020 put_device(tty_dev);
Alan Coxccce6de2009-09-19 13:13:30 -07002021 uport->suspended = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
2024 * Re-enable the console device after suspending.
2025 */
Yin Kangkai5933a162011-01-30 11:15:30 +08002026 if (uart_console(uport)) {
Jason Wang891b9dd2010-08-21 15:14:42 +08002027 /*
2028 * First try to use the console cflag setting.
2029 */
2030 memset(&termios, 0, sizeof(struct ktermios));
2031 termios.c_cflag = uport->cons->cflag;
2032
2033 /*
2034 * If that's unset, use the tty termios setting.
2035 */
Alan Coxadc8d742012-07-14 15:31:47 +01002036 if (port->tty && termios.c_cflag == 0)
2037 termios = port->tty->termios;
Jason Wang891b9dd2010-08-21 15:14:42 +08002038
Ning Jiang94abc562011-09-05 16:28:18 +08002039 if (console_suspend_enabled)
Linus Walleij6f538fe2012-12-07 11:36:08 +01002040 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002041 uport->ops->set_termios(uport, &termios, NULL);
Yin Kangkai5933a162011-01-30 11:15:30 +08002042 if (console_suspend_enabled)
2043 console_start(uport->cons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
Alan Coxccce6de2009-09-19 13:13:30 -07002046 if (port->flags & ASYNC_SUSPENDED) {
2047 const struct uart_ops *ops = uport->ops;
Russell Kingee31b332005-11-13 15:28:51 +00002048 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Linus Walleij6f538fe2012-12-07 11:36:08 +01002050 uart_change_pm(state, UART_PM_STATE_ON);
Alan Coxccce6de2009-09-19 13:13:30 -07002051 spin_lock_irq(&uport->lock);
2052 ops->set_mctrl(uport, 0);
2053 spin_unlock_irq(&uport->lock);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002054 if (console_suspend_enabled || !uart_console(uport)) {
Alan Cox19225132010-06-01 22:52:51 +02002055 /* Protected by port mutex for now */
2056 struct tty_struct *tty = port->tty;
Stanislav Brabec4547be72009-12-02 16:20:56 +01002057 ret = ops->startup(uport);
2058 if (ret == 0) {
Alan Cox19225132010-06-01 22:52:51 +02002059 if (tty)
2060 uart_change_speed(tty, state, NULL);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002061 spin_lock_irq(&uport->lock);
2062 ops->set_mctrl(uport, uport->mctrl);
2063 ops->start_tx(uport);
2064 spin_unlock_irq(&uport->lock);
2065 set_bit(ASYNCB_INITIALIZED, &port->flags);
2066 } else {
2067 /*
2068 * Failed to resume - maybe hardware went away?
2069 * Clear the "initialized" flag so we won't try
2070 * to call the low level drivers shutdown method.
2071 */
Alan Cox19225132010-06-01 22:52:51 +02002072 uart_shutdown(tty, state);
Stanislav Brabec4547be72009-12-02 16:20:56 +01002073 }
Russell Kingee31b332005-11-13 15:28:51 +00002074 }
Russell Kinga6b93a92006-10-01 17:17:40 +01002075
Alan Coxccce6de2009-09-19 13:13:30 -07002076 clear_bit(ASYNCB_SUSPENDED, &port->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078
Alan Coxa2bceae2009-09-19 13:13:31 -07002079 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 return 0;
2082}
2083
2084static inline void
2085uart_report_port(struct uart_driver *drv, struct uart_port *port)
2086{
Russell King30b7a3b2005-09-03 15:30:21 +01002087 char address[64];
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 switch (port->iotype) {
2090 case UPIO_PORT:
Andrew Morton9bde10a2008-10-13 10:35:42 +01002091 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 case UPIO_HUB6:
Russell King30b7a3b2005-09-03 15:30:21 +01002094 snprintf(address, sizeof(address),
Andrew Morton9bde10a2008-10-13 10:35:42 +01002095 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 break;
2097 case UPIO_MEM:
2098 case UPIO_MEM32:
Pantelis Antoniou21c614a2005-11-06 09:07:03 +00002099 case UPIO_AU:
Zang Roy-r619113be91ec2006-06-30 02:29:58 -07002100 case UPIO_TSI:
Russell King30b7a3b2005-09-03 15:30:21 +01002101 snprintf(address, sizeof(address),
Josh Boyer4f640ef2007-07-23 18:43:44 -07002102 "MMIO 0x%llx", (unsigned long long)port->mapbase);
Russell King30b7a3b2005-09-03 15:30:21 +01002103 break;
2104 default:
2105 strlcpy(address, "*unknown*", sizeof(address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 break;
2107 }
Russell King30b7a3b2005-09-03 15:30:21 +01002108
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302109 dev_info(port->dev, "%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
David S. Miller84408382008-10-13 10:45:26 +01002110 drv->dev_name,
2111 drv->tty_driver->name_base + port->line,
Kees Cook7d12b972013-07-12 13:07:39 -07002112 address, port->irq, port->uartclk / 16, uart_type(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115static void
2116uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2117 struct uart_port *port)
2118{
2119 unsigned int flags;
2120
2121 /*
2122 * If there isn't a port here, don't do anything further.
2123 */
2124 if (!port->iobase && !port->mapbase && !port->membase)
2125 return;
2126
2127 /*
2128 * Now do the auto configuration stuff. Note that config_port
2129 * is expected to claim the resources and map the port for us.
2130 */
David Daney8e23fcc2009-01-02 13:49:54 +00002131 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (port->flags & UPF_AUTO_IRQ)
2133 flags |= UART_CONFIG_IRQ;
2134 if (port->flags & UPF_BOOT_AUTOCONF) {
David Daney8e23fcc2009-01-02 13:49:54 +00002135 if (!(port->flags & UPF_FIXED_TYPE)) {
2136 port->type = PORT_UNKNOWN;
2137 flags |= UART_CONFIG_TYPE;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 port->ops->config_port(port, flags);
2140 }
2141
2142 if (port->type != PORT_UNKNOWN) {
2143 unsigned long flags;
2144
2145 uart_report_port(drv, port);
2146
George G. Davis3689a0e2007-02-14 00:33:06 -08002147 /* Power up port for set_mctrl() */
Linus Walleij6f538fe2012-12-07 11:36:08 +01002148 uart_change_pm(state, UART_PM_STATE_ON);
George G. Davis3689a0e2007-02-14 00:33:06 -08002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * Ensure that the modem control lines are de-activated.
Yinghai Luc3e46422008-02-04 22:27:46 -08002152 * keep the DTR setting that is set in uart_set_options()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 * We probably don't need a spinlock around this, but
2154 */
2155 spin_lock_irqsave(&port->lock, flags);
Yinghai Luc3e46422008-02-04 22:27:46 -08002156 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 spin_unlock_irqrestore(&port->lock, flags);
2158
2159 /*
Russell King97d97222007-09-01 21:25:09 +01002160 * If this driver supports console, and it hasn't been
2161 * successfully registered yet, try to re-register it.
2162 * It may be that the port was not available.
2163 */
2164 if (port->cons && !(port->cons->flags & CON_ENABLED))
2165 register_console(port->cons);
2166
2167 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 * Power down all ports by default, except the
2169 * console if we have one.
2170 */
2171 if (!uart_console(port))
Linus Walleij6f538fe2012-12-07 11:36:08 +01002172 uart_change_pm(state, UART_PM_STATE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174}
2175
Jason Wesself2d937f2008-04-17 20:05:37 +02002176#ifdef CONFIG_CONSOLE_POLL
2177
2178static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2179{
2180 struct uart_driver *drv = driver->driver_state;
2181 struct uart_state *state = drv->state + line;
2182 struct uart_port *port;
2183 int baud = 9600;
2184 int bits = 8;
2185 int parity = 'n';
2186 int flow = 'n';
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002187 int ret;
Jason Wesself2d937f2008-04-17 20:05:37 +02002188
Alan Coxebd2c8f2009-09-19 13:13:28 -07002189 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002190 return -1;
2191
Alan Coxebd2c8f2009-09-19 13:13:28 -07002192 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002193 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2194 return -1;
2195
Anton Vorontsovc7f3e702012-09-24 14:27:53 -07002196 if (port->ops->poll_init) {
2197 struct tty_port *tport = &state->port;
2198
2199 ret = 0;
2200 mutex_lock(&tport->mutex);
2201 /*
2202 * We don't set ASYNCB_INITIALIZED as we only initialized the
2203 * hw, e.g. state->xmit is still uninitialized.
2204 */
2205 if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
2206 ret = port->ops->poll_init(port);
2207 mutex_unlock(&tport->mutex);
2208 if (ret)
2209 return ret;
2210 }
2211
Jason Wesself2d937f2008-04-17 20:05:37 +02002212 if (options) {
2213 uart_parse_options(options, &baud, &parity, &bits, &flow);
2214 return uart_set_options(port, NULL, baud, parity, bits, flow);
2215 }
2216
2217 return 0;
2218}
2219
2220static int uart_poll_get_char(struct tty_driver *driver, int line)
2221{
2222 struct uart_driver *drv = driver->driver_state;
2223 struct uart_state *state = drv->state + line;
2224 struct uart_port *port;
2225
Alan Coxebd2c8f2009-09-19 13:13:28 -07002226 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002227 return -1;
2228
Alan Coxebd2c8f2009-09-19 13:13:28 -07002229 port = state->uart_port;
Jason Wesself2d937f2008-04-17 20:05:37 +02002230 return port->ops->poll_get_char(port);
2231}
2232
2233static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2234{
2235 struct uart_driver *drv = driver->driver_state;
2236 struct uart_state *state = drv->state + line;
2237 struct uart_port *port;
2238
Alan Coxebd2c8f2009-09-19 13:13:28 -07002239 if (!state || !state->uart_port)
Jason Wesself2d937f2008-04-17 20:05:37 +02002240 return;
2241
Alan Coxebd2c8f2009-09-19 13:13:28 -07002242 port = state->uart_port;
Doug Andersonc7d44a02014-04-21 10:06:43 -07002243
2244 if (ch == '\n')
2245 port->ops->poll_put_char(port, '\r');
Jason Wesself2d937f2008-04-17 20:05:37 +02002246 port->ops->poll_put_char(port, ch);
2247}
2248#endif
2249
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002250static const struct tty_operations uart_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 .open = uart_open,
2252 .close = uart_close,
2253 .write = uart_write,
2254 .put_char = uart_put_char,
2255 .flush_chars = uart_flush_chars,
2256 .write_room = uart_write_room,
2257 .chars_in_buffer= uart_chars_in_buffer,
2258 .flush_buffer = uart_flush_buffer,
2259 .ioctl = uart_ioctl,
2260 .throttle = uart_throttle,
2261 .unthrottle = uart_unthrottle,
2262 .send_xchar = uart_send_xchar,
2263 .set_termios = uart_set_termios,
Alan Cox64e91592008-06-03 15:18:54 +01002264 .set_ldisc = uart_set_ldisc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 .stop = uart_stop,
2266 .start = uart_start,
2267 .hangup = uart_hangup,
2268 .break_ctl = uart_break_ctl,
2269 .wait_until_sent= uart_wait_until_sent,
2270#ifdef CONFIG_PROC_FS
Alexey Dobriyand196a942009-03-31 15:19:21 -07002271 .proc_fops = &uart_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272#endif
2273 .tiocmget = uart_tiocmget,
2274 .tiocmset = uart_tiocmset,
Alan Coxd281da72010-09-16 18:21:24 +01002275 .get_icount = uart_get_icount,
Jason Wesself2d937f2008-04-17 20:05:37 +02002276#ifdef CONFIG_CONSOLE_POLL
2277 .poll_init = uart_poll_init,
2278 .poll_get_char = uart_poll_get_char,
2279 .poll_put_char = uart_poll_put_char,
2280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281};
2282
Alan Coxde0c8cb2010-06-01 22:52:58 +02002283static const struct tty_port_operations uart_port_ops = {
Jiri Slaby0b1db832011-11-09 21:33:50 +01002284 .activate = uart_port_activate,
2285 .shutdown = uart_port_shutdown,
Alan Coxde0c8cb2010-06-01 22:52:58 +02002286 .carrier_raised = uart_carrier_raised,
2287 .dtr_rts = uart_dtr_rts,
2288};
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290/**
2291 * uart_register_driver - register a driver with the uart core layer
2292 * @drv: low level driver structure
2293 *
2294 * Register a uart driver with the core driver. We in turn register
2295 * with the tty layer, and initialise the core driver per-port state.
2296 *
2297 * We have a proc file in /proc/tty/driver which is named after the
2298 * normal driver.
2299 *
2300 * drv->port should be NULL, and the per-port structures should be
2301 * registered using uart_add_one_port after this call has succeeded.
2302 */
2303int uart_register_driver(struct uart_driver *drv)
2304{
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002305 struct tty_driver *normal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int i, retval;
2307
2308 BUG_ON(drv->state);
2309
2310 /*
2311 * Maybe we should be using a slab cache for this, especially if
2312 * we have a large number of ports to handle.
2313 */
Burman Yan8f31bb32007-02-14 00:33:07 -08002314 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (!drv->state)
2316 goto out;
2317
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002318 normal = alloc_tty_driver(drv->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!normal)
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002320 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 drv->tty_driver = normal;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 normal->driver_name = drv->driver_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 normal->name = drv->dev_name;
2326 normal->major = drv->major;
2327 normal->minor_start = drv->minor;
2328 normal->type = TTY_DRIVER_TYPE_SERIAL;
2329 normal->subtype = SERIAL_TYPE_NORMAL;
2330 normal->init_termios = tty_std_termios;
2331 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08002332 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002333 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 normal->driver_state = drv;
2335 tty_set_operations(normal, &uart_ops);
2336
2337 /*
2338 * Initialise the UART state(s).
2339 */
2340 for (i = 0; i < drv->nr; i++) {
2341 struct uart_state *state = drv->state + i;
Alan Coxa2bceae2009-09-19 13:13:31 -07002342 struct tty_port *port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Alan Coxa2bceae2009-09-19 13:13:31 -07002344 tty_port_init(port);
Alan Coxde0c8cb2010-06-01 22:52:58 +02002345 port->ops = &uart_port_ops;
Jiri Slaby4cb0fbf2011-11-09 21:33:45 +01002346 port->close_delay = HZ / 2; /* .5 seconds */
2347 port->closing_wait = 30 * HZ;/* 30 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
2350 retval = tty_register_driver(normal);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002351 if (retval >= 0)
2352 return retval;
2353
Jiri Slaby191c5f12012-11-15 09:49:56 +01002354 for (i = 0; i < drv->nr; i++)
2355 tty_port_destroy(&drv->state[i].port);
André Goddard Rosa9e845ab2009-10-25 11:16:32 -02002356 put_tty_driver(normal);
2357out_kfree:
2358 kfree(drv->state);
2359out:
2360 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363/**
2364 * uart_unregister_driver - remove a driver from the uart core layer
2365 * @drv: low level driver structure
2366 *
2367 * Remove all references to a driver from the core driver. The low
2368 * level driver must have removed all its ports via the
2369 * uart_remove_one_port() if it registered them with uart_add_one_port().
2370 * (ie, drv->port == NULL)
2371 */
2372void uart_unregister_driver(struct uart_driver *drv)
2373{
2374 struct tty_driver *p = drv->tty_driver;
Jiri Slaby191c5f12012-11-15 09:49:56 +01002375 unsigned int i;
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 tty_unregister_driver(p);
2378 put_tty_driver(p);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002379 for (i = 0; i < drv->nr; i++)
2380 tty_port_destroy(&drv->state[i].port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 kfree(drv->state);
Alan Cox1e66cded2012-05-14 14:51:22 +01002382 drv->state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 drv->tty_driver = NULL;
2384}
2385
2386struct tty_driver *uart_console_device(struct console *co, int *index)
2387{
2388 struct uart_driver *p = co->data;
2389 *index = co->index;
2390 return p->tty_driver;
2391}
2392
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002393static ssize_t uart_get_attr_uartclk(struct device *dev,
2394 struct device_attribute *attr, char *buf)
2395{
Alan Coxbebe73e2012-10-29 15:19:57 +00002396 struct serial_struct tmp;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002397 struct tty_port *port = dev_get_drvdata(dev);
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002398
Alan Cox9f109692012-10-29 15:20:25 +00002399 uart_get_info(port, &tmp);
Alan Coxbebe73e2012-10-29 15:19:57 +00002400 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002401}
2402
Alan Cox373bac42012-10-29 15:20:40 +00002403static ssize_t uart_get_attr_type(struct device *dev,
2404 struct device_attribute *attr, char *buf)
2405{
2406 struct serial_struct tmp;
2407 struct tty_port *port = dev_get_drvdata(dev);
2408
2409 uart_get_info(port, &tmp);
2410 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2411}
2412static ssize_t uart_get_attr_line(struct device *dev,
2413 struct device_attribute *attr, char *buf)
2414{
2415 struct serial_struct tmp;
2416 struct tty_port *port = dev_get_drvdata(dev);
2417
2418 uart_get_info(port, &tmp);
2419 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2420}
2421
2422static ssize_t uart_get_attr_port(struct device *dev,
2423 struct device_attribute *attr, char *buf)
2424{
2425 struct serial_struct tmp;
2426 struct tty_port *port = dev_get_drvdata(dev);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002427 unsigned long ioaddr;
Alan Cox373bac42012-10-29 15:20:40 +00002428
2429 uart_get_info(port, &tmp);
Andrew Mortonfd985e12012-11-26 15:47:15 -08002430 ioaddr = tmp.port;
2431 if (HIGH_BITS_OFFSET)
2432 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2433 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
Alan Cox373bac42012-10-29 15:20:40 +00002434}
2435
2436static ssize_t uart_get_attr_irq(struct device *dev,
2437 struct device_attribute *attr, char *buf)
2438{
2439 struct serial_struct tmp;
2440 struct tty_port *port = dev_get_drvdata(dev);
2441
2442 uart_get_info(port, &tmp);
2443 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2444}
2445
2446static ssize_t uart_get_attr_flags(struct device *dev,
2447 struct device_attribute *attr, char *buf)
2448{
2449 struct serial_struct tmp;
2450 struct tty_port *port = dev_get_drvdata(dev);
2451
2452 uart_get_info(port, &tmp);
2453 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2454}
2455
2456static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2457 struct device_attribute *attr, char *buf)
2458{
2459 struct serial_struct tmp;
2460 struct tty_port *port = dev_get_drvdata(dev);
2461
2462 uart_get_info(port, &tmp);
2463 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2464}
2465
2466
2467static ssize_t uart_get_attr_close_delay(struct device *dev,
2468 struct device_attribute *attr, char *buf)
2469{
2470 struct serial_struct tmp;
2471 struct tty_port *port = dev_get_drvdata(dev);
2472
2473 uart_get_info(port, &tmp);
2474 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2475}
2476
2477
2478static ssize_t uart_get_attr_closing_wait(struct device *dev,
2479 struct device_attribute *attr, char *buf)
2480{
2481 struct serial_struct tmp;
2482 struct tty_port *port = dev_get_drvdata(dev);
2483
2484 uart_get_info(port, &tmp);
2485 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2486}
2487
2488static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2489 struct device_attribute *attr, char *buf)
2490{
2491 struct serial_struct tmp;
2492 struct tty_port *port = dev_get_drvdata(dev);
2493
2494 uart_get_info(port, &tmp);
2495 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2496}
2497
2498static ssize_t uart_get_attr_io_type(struct device *dev,
2499 struct device_attribute *attr, char *buf)
2500{
2501 struct serial_struct tmp;
2502 struct tty_port *port = dev_get_drvdata(dev);
2503
2504 uart_get_info(port, &tmp);
2505 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2506}
2507
2508static ssize_t uart_get_attr_iomem_base(struct device *dev,
2509 struct device_attribute *attr, char *buf)
2510{
2511 struct serial_struct tmp;
2512 struct tty_port *port = dev_get_drvdata(dev);
2513
2514 uart_get_info(port, &tmp);
2515 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2516}
2517
2518static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2519 struct device_attribute *attr, char *buf)
2520{
2521 struct serial_struct tmp;
2522 struct tty_port *port = dev_get_drvdata(dev);
2523
2524 uart_get_info(port, &tmp);
2525 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2526}
2527
2528static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2529static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2530static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2531static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2532static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2533static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002534static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
Alan Cox373bac42012-10-29 15:20:40 +00002535static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2536static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2537static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2538static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2539static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2540static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002541
2542static struct attribute *tty_dev_attrs[] = {
Alan Cox373bac42012-10-29 15:20:40 +00002543 &dev_attr_type.attr,
2544 &dev_attr_line.attr,
2545 &dev_attr_port.attr,
2546 &dev_attr_irq.attr,
2547 &dev_attr_flags.attr,
2548 &dev_attr_xmit_fifo_size.attr,
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002549 &dev_attr_uartclk.attr,
Alan Cox373bac42012-10-29 15:20:40 +00002550 &dev_attr_close_delay.attr,
2551 &dev_attr_closing_wait.attr,
2552 &dev_attr_custom_divisor.attr,
2553 &dev_attr_io_type.attr,
2554 &dev_attr_iomem_base.attr,
2555 &dev_attr_iomem_reg_shift.attr,
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002556 NULL,
2557 };
2558
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002559static const struct attribute_group tty_dev_attr_group = {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02002560 .attrs = tty_dev_attrs,
2561 };
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563/**
2564 * uart_add_one_port - attach a driver-defined port structure
2565 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002566 * @uport: uart port structure to use for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 *
2568 * This allows the driver to register its own uart_port structure
2569 * with the core driver. The main purpose is to allow the low
2570 * level uart drivers to expand uart_port, rather than having yet
2571 * more levels of structures.
2572 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002573int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 struct uart_state *state;
Alan Coxa2bceae2009-09-19 13:13:31 -07002576 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 int ret = 0;
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002578 struct device *tty_dev;
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002579 int num_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 BUG_ON(in_interrupt());
2582
Alan Coxa2bceae2009-09-19 13:13:31 -07002583 if (uport->line >= drv->nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return -EINVAL;
2585
Alan Coxa2bceae2009-09-19 13:13:31 -07002586 state = drv->state + uport->line;
2587 port = &state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002589 mutex_lock(&port_mutex);
Alan Coxa2bceae2009-09-19 13:13:31 -07002590 mutex_lock(&port->mutex);
Alan Coxebd2c8f2009-09-19 13:13:28 -07002591 if (state->uart_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 ret = -EINVAL;
2593 goto out;
2594 }
2595
Alan Coxa2bceae2009-09-19 13:13:31 -07002596 state->uart_port = uport;
Linus Walleij6f538fe2012-12-07 11:36:08 +01002597 state->pm_state = UART_PM_STATE_UNDEFINED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Alan Coxa2bceae2009-09-19 13:13:31 -07002599 uport->cons = drv->cons;
2600 uport->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Russell King976ecd12005-07-03 21:05:45 +01002602 /*
2603 * If this port is a console, then the spinlock is already
2604 * initialised.
2605 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002606 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2607 spin_lock_init(&uport->lock);
2608 lockdep_set_class(&uport->lock, &port_lock_key);
Ingo Molnar13e83592006-07-03 00:25:03 -07002609 }
Grant Likelya208ffd2014-03-27 18:29:46 -07002610 if (uport->cons && uport->dev)
2611 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
Russell King976ecd12005-07-03 21:05:45 +01002612
Alan Coxa2bceae2009-09-19 13:13:31 -07002613 uart_configure_port(drv, state, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002615 num_groups = 2;
2616 if (uport->attr_group)
2617 num_groups++;
2618
Yoshihiro YUNOMAEc2b703b2014-07-23 06:06:22 +00002619 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002620 GFP_KERNEL);
2621 if (!uport->tty_groups) {
2622 ret = -ENOMEM;
2623 goto out;
2624 }
2625 uport->tty_groups[0] = &tty_dev_attr_group;
2626 if (uport->attr_group)
2627 uport->tty_groups[1] = uport->attr_group;
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /*
2630 * Register the port whether it's detected or not. This allows
Geert Uytterhoeven015355b2014-03-11 11:23:36 +01002631 * setserial to be used to alter this port's parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02002633 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002634 uport->line, uport->dev, port, uport->tty_groups);
Guennadi Liakhovetskib3b708f2007-10-16 01:24:02 -07002635 if (likely(!IS_ERR(tty_dev))) {
Simon Glass77359832012-01-19 11:28:56 -08002636 device_set_wakeup_capable(tty_dev, 1);
2637 } else {
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302638 dev_err(uport->dev, "Cannot register tty device on line %d\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002639 uport->line);
Simon Glass77359832012-01-19 11:28:56 -08002640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 /*
Russell King68ac64c2006-04-30 11:13:50 +01002643 * Ensure UPF_DEAD is not set.
2644 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002645 uport->flags &= ~UPF_DEAD;
Russell King68ac64c2006-04-30 11:13:50 +01002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 out:
Alan Coxa2bceae2009-09-19 13:13:31 -07002648 mutex_unlock(&port->mutex);
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002649 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 return ret;
2652}
2653
2654/**
2655 * uart_remove_one_port - detach a driver defined port structure
2656 * @drv: pointer to the uart low level driver structure for this port
Randy Dunlap1b9894f2009-09-21 11:12:03 -07002657 * @uport: uart port structure for this port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 *
2659 * This unhooks (and hangs up) the specified port structure from the
2660 * core driver. No further calls will be made to the low-level code
2661 * for this port.
2662 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002663int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Alan Coxa2bceae2009-09-19 13:13:31 -07002665 struct uart_state *state = drv->state + uport->line;
2666 struct tty_port *port = &state->port;
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002667 struct tty_struct *tty;
Chen Gangb342dd52012-12-27 15:51:31 +08002668 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 BUG_ON(in_interrupt());
2671
Alan Coxa2bceae2009-09-19 13:13:31 -07002672 if (state->uart_port != uport)
Sudip Mukherjee2f2dafe2014-09-01 20:49:43 +05302673 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
Alan Coxa2bceae2009-09-19 13:13:31 -07002674 state->uart_port, uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002676 mutex_lock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 /*
Russell King68ac64c2006-04-30 11:13:50 +01002679 * Mark the port "dead" - this prevents any opens from
2680 * succeeding while we shut down the port.
2681 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002682 mutex_lock(&port->mutex);
Chen Gangb342dd52012-12-27 15:51:31 +08002683 if (!state->uart_port) {
2684 mutex_unlock(&port->mutex);
2685 ret = -EINVAL;
2686 goto out;
2687 }
Alan Coxa2bceae2009-09-19 13:13:31 -07002688 uport->flags |= UPF_DEAD;
2689 mutex_unlock(&port->mutex);
Russell King68ac64c2006-04-30 11:13:50 +01002690
2691 /*
Greg Kroah-Hartmanaa4148c2005-06-20 21:15:16 -07002692 * Remove the devices from the tty layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002694 tty_unregister_device(drv->tty_driver, uport->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002696 tty = tty_port_tty_get(port);
2697 if (tty) {
Alan Coxa2bceae2009-09-19 13:13:31 -07002698 tty_vhangup(port->tty);
Geert Uytterhoeven4c6d5b42014-03-17 14:10:58 +01002699 tty_kref_put(tty);
2700 }
Russell King68ac64c2006-04-30 11:13:50 +01002701
2702 /*
Geert Uytterhoeven5f5c9ae2014-02-28 14:21:32 +01002703 * If the port is used as a console, unregister it
2704 */
2705 if (uart_console(uport))
2706 unregister_console(uport->cons);
2707
2708 /*
Russell King68ac64c2006-04-30 11:13:50 +01002709 * Free the port IO and memory resources, if any.
2710 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002711 if (uport->type != PORT_UNKNOWN)
2712 uport->ops->release_port(uport);
Greg Kroah-Hartman266dcff2014-07-16 01:19:34 +00002713 kfree(uport->tty_groups);
Russell King68ac64c2006-04-30 11:13:50 +01002714
2715 /*
2716 * Indicate that there isn't a port here anymore.
2717 */
Alan Coxa2bceae2009-09-19 13:13:31 -07002718 uport->type = PORT_UNKNOWN;
Russell King68ac64c2006-04-30 11:13:50 +01002719
Alan Coxebd2c8f2009-09-19 13:13:28 -07002720 state->uart_port = NULL;
Chen Gangb342dd52012-12-27 15:51:31 +08002721out:
Arjan van de Venf392ecf2006-01-12 18:44:32 +00002722 mutex_unlock(&port_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Chen Gangb342dd52012-12-27 15:51:31 +08002724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727/*
2728 * Are the two ports equivalent?
2729 */
2730int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2731{
2732 if (port1->iotype != port2->iotype)
2733 return 0;
2734
2735 switch (port1->iotype) {
2736 case UPIO_PORT:
2737 return (port1->iobase == port2->iobase);
2738 case UPIO_HUB6:
2739 return (port1->iobase == port2->iobase) &&
2740 (port1->hub6 == port2->hub6);
2741 case UPIO_MEM:
Sergei Shtylyovd21b55d2006-08-28 19:49:03 +04002742 case UPIO_MEM32:
2743 case UPIO_AU:
2744 case UPIO_TSI:
Benjamin Herrenschmidt1624f002006-01-04 18:09:44 +00002745 return (port1->mapbase == port2->mapbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747 return 0;
2748}
2749EXPORT_SYMBOL(uart_match_port);
2750
Jiri Slaby027d7da2011-11-09 21:33:43 +01002751/**
2752 * uart_handle_dcd_change - handle a change of carrier detect state
2753 * @uport: uart_port structure for the open port
2754 * @status: new carrier detect status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04002755 *
2756 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01002757 */
2758void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2759{
George Spelvin42381572013-02-10 04:44:30 -05002760 struct tty_port *port = &uport->state->port;
Alan Cox43eca0a2012-09-19 15:35:46 +01002761 struct tty_struct *tty = port->tty;
Peter Hurleyc9932572014-09-02 17:39:21 -04002762 struct tty_ldisc *ld;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002763
Peter Hurley4d90bb12014-09-10 15:06:23 -04002764 lockdep_assert_held_once(&uport->lock);
2765
Peter Hurleyc9932572014-09-02 17:39:21 -04002766 if (tty) {
2767 ld = tty_ldisc_ref(tty);
2768 if (ld) {
2769 if (ld->ops->dcd_change)
2770 ld->ops->dcd_change(tty, status);
2771 tty_ldisc_deref(ld);
2772 }
George Spelvin42381572013-02-10 04:44:30 -05002773 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01002774
2775 uport->icount.dcd++;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002776
Peter Hurley299245a2014-09-10 15:06:24 -04002777 if (uart_dcd_enabled(uport)) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01002778 if (status)
2779 wake_up_interruptible(&port->open_wait);
Alan Cox43eca0a2012-09-19 15:35:46 +01002780 else if (tty)
2781 tty_hangup(tty);
Jiri Slaby027d7da2011-11-09 21:33:43 +01002782 }
Jiri Slaby027d7da2011-11-09 21:33:43 +01002783}
2784EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2785
2786/**
2787 * uart_handle_cts_change - handle a change of clear-to-send state
2788 * @uport: uart_port structure for the open port
2789 * @status: new clear to send status, nonzero if active
Peter Hurley4d90bb12014-09-10 15:06:23 -04002790 *
2791 * Caller must hold uport->lock
Jiri Slaby027d7da2011-11-09 21:33:43 +01002792 */
2793void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2794{
Peter Hurley4d90bb12014-09-10 15:06:23 -04002795 lockdep_assert_held_once(&uport->lock);
2796
Jiri Slaby027d7da2011-11-09 21:33:43 +01002797 uport->icount.cts++;
2798
Peter Hurley299245a2014-09-10 15:06:24 -04002799 if (uart_cts_enabled(uport)) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002800 if (uport->hw_stopped) {
Jiri Slaby027d7da2011-11-09 21:33:43 +01002801 if (status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002802 uport->hw_stopped = 0;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002803 uport->ops->start_tx(uport);
2804 uart_write_wakeup(uport);
2805 }
2806 } else {
2807 if (!status) {
Peter Hurleyd01f4d12014-09-10 15:06:26 -04002808 uport->hw_stopped = 1;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002809 uport->ops->stop_tx(uport);
2810 }
2811 }
2812 }
2813}
2814EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2815
Jiri Slabycf755252011-11-09 21:33:47 +01002816/**
2817 * uart_insert_char - push a char to the uart layer
2818 *
2819 * User is responsible to call tty_flip_buffer_push when they are done with
2820 * insertion.
2821 *
2822 * @port: corresponding port
2823 * @status: state of the serial port RX buffer (LSR for 8250)
2824 * @overrun: mask of overrun bits in @status
2825 * @ch: character to push
2826 * @flag: flag for the character (see TTY_NORMAL and friends)
2827 */
Jiri Slaby027d7da2011-11-09 21:33:43 +01002828void uart_insert_char(struct uart_port *port, unsigned int status,
2829 unsigned int overrun, unsigned int ch, unsigned int flag)
2830{
Jiri Slaby92a19f92013-01-03 15:53:03 +01002831 struct tty_port *tport = &port->state->port;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002832
2833 if ((status & port->ignore_status_mask & ~overrun) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +01002834 if (tty_insert_flip_char(tport, ch, flag) == 0)
Corbindabfb352012-05-23 09:37:31 -05002835 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002836
2837 /*
2838 * Overrun is special. Since it's reported immediately,
2839 * it doesn't affect the current character.
2840 */
2841 if (status & ~port->ignore_status_mask & overrun)
Jiri Slaby92a19f92013-01-03 15:53:03 +01002842 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
Corbindabfb352012-05-23 09:37:31 -05002843 ++port->icount.buf_overrun;
Jiri Slaby027d7da2011-11-09 21:33:43 +01002844}
2845EXPORT_SYMBOL_GPL(uart_insert_char);
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847EXPORT_SYMBOL(uart_write_wakeup);
2848EXPORT_SYMBOL(uart_register_driver);
2849EXPORT_SYMBOL(uart_unregister_driver);
2850EXPORT_SYMBOL(uart_suspend_port);
2851EXPORT_SYMBOL(uart_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852EXPORT_SYMBOL(uart_add_one_port);
2853EXPORT_SYMBOL(uart_remove_one_port);
2854
2855MODULE_DESCRIPTION("Serial driver core");
2856MODULE_LICENSE("GPL");