]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/tty/serial/serial_core.c
serial: core: Use tty->index for port # in debug messages
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / serial / serial_core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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 */
1da177e4
LT
23#include <linux/module.h>
24#include <linux/tty.h>
027d7dac 25#include <linux/tty_flip.h>
1da177e4
LT
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/console.h>
a208ffd2 29#include <linux/of.h>
d196a949
AD
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
1da177e4
LT
32#include <linux/device.h>
33#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
ccce6deb 34#include <linux/serial_core.h>
1da177e4 35#include <linux/delay.h>
f392ecfa 36#include <linux/mutex.h>
1da177e4
LT
37
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40
1da177e4
LT
41/*
42 * This is used to lock changes in serial line configuration.
43 */
f392ecfa 44static DEFINE_MUTEX(port_mutex);
1da177e4 45
13e83599
IM
46/*
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
1da177e4
LT
52#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
53
19225135 54static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
a46c9994 55 struct ktermios *old_termios);
1f33a51d 56static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
6f538fe3
LW
57static void uart_change_pm(struct uart_state *state,
58 enum uart_pm_state pm_state);
1da177e4 59
b922e19d
JS
60static void uart_port_shutdown(struct tty_port *port);
61
299245a1
PH
62static int uart_dcd_enabled(struct uart_port *uport)
63{
d4260b51 64 return !!(uport->status & UPSTAT_DCD_ENABLE);
299245a1
PH
65}
66
1da177e4
LT
67/*
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{
ebd2c8f6 73 struct uart_state *state = port->state;
d5f735e5
PM
74 /*
75 * This means you called this function _after_ the port was
76 * closed. No cookie for you.
77 */
ebd2c8f6 78 BUG_ON(!state);
6a3e492b 79 tty_wakeup(state->port.tty);
1da177e4
LT
80}
81
82static void uart_stop(struct tty_struct *tty)
83{
84 struct uart_state *state = tty->driver_data;
ebd2c8f6 85 struct uart_port *port = state->uart_port;
1da177e4
LT
86 unsigned long flags;
87
88 spin_lock_irqsave(&port->lock, flags);
b129a8cc 89 port->ops->stop_tx(port);
1da177e4
LT
90 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;
ebd2c8f6 96 struct uart_port *port = state->uart_port;
1da177e4 97
d01f4d18 98 if (!uart_tx_stopped(port))
b129a8cc 99 port->ops->start_tx(port);
1da177e4
LT
100}
101
102static void uart_start(struct tty_struct *tty)
103{
104 struct uart_state *state = tty->driver_data;
ebd2c8f6 105 struct uart_port *port = state->uart_port;
1da177e4
LT
106 unsigned long flags;
107
108 spin_lock_irqsave(&port->lock, flags);
109 __uart_start(tty);
110 spin_unlock_irqrestore(&port->lock, flags);
111}
112
f4581cab 113static void
1da177e4
LT
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
a46c9994
AC
127#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)
1da177e4
LT
129
130/*
131 * Startup the port. This will be called once per open. All calls
df4f4dd4 132 * will be serialised by the per-port mutex.
1da177e4 133 */
c0d92be6
JS
134static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
135 int init_hw)
1da177e4 136{
46d57a44 137 struct uart_port *uport = state->uart_port;
1da177e4
LT
138 unsigned long page;
139 int retval = 0;
140
46d57a44 141 if (uport->type == PORT_UNKNOWN)
c0d92be6 142 return 1;
1da177e4 143
7deb39ed
TP
144 /*
145 * Make sure the device is in D0 state.
146 */
147 uart_change_pm(state, UART_PM_STATE_ON);
148
1da177e4
LT
149 /*
150 * Initialise and allocate the transmit and temporary
151 * buffer.
152 */
ebd2c8f6 153 if (!state->xmit.buf) {
df4f4dd4 154 /* This is protected by the per port mutex */
1da177e4
LT
155 page = get_zeroed_page(GFP_KERNEL);
156 if (!page)
157 return -ENOMEM;
158
ebd2c8f6
AC
159 state->xmit.buf = (unsigned char *) page;
160 uart_circ_clear(&state->xmit);
1da177e4
LT
161 }
162
46d57a44 163 retval = uport->ops->startup(uport);
1da177e4 164 if (retval == 0) {
c7d7abff 165 if (uart_console(uport) && uport->cons->cflag) {
adc8d746 166 tty->termios.c_cflag = uport->cons->cflag;
c7d7abff
JS
167 uport->cons->cflag = 0;
168 }
169 /*
170 * Initialise the hardware port settings.
171 */
172 uart_change_speed(tty, state, NULL);
1da177e4 173
9db276f8
PH
174 /*
175 * Setup the RTS and DTR signals once the
176 * port is open and ready to respond.
177 */
178 if (init_hw && C_BAUD(tty))
179 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1da177e4
LT
180 }
181
0055197e
JS
182 /*
183 * This is to allow setserial on this port. People may want to set
184 * port/irq/type and then reconfigure the port properly if it failed
185 * now.
186 */
1da177e4 187 if (retval && capable(CAP_SYS_ADMIN))
c0d92be6
JS
188 return 1;
189
190 return retval;
191}
192
193static int uart_startup(struct tty_struct *tty, struct uart_state *state,
194 int init_hw)
195{
196 struct tty_port *port = &state->port;
197 int retval;
198
199 if (port->flags & ASYNC_INITIALIZED)
200 return 0;
201
202 /*
203 * Set the TTY IO error marker - we will only clear this
204 * once we have successfully opened the port.
205 */
206 set_bit(TTY_IO_ERROR, &tty->flags);
207
208 retval = uart_port_startup(tty, state, init_hw);
209 if (!retval) {
210 set_bit(ASYNCB_INITIALIZED, &port->flags);
211 clear_bit(TTY_IO_ERROR, &tty->flags);
212 } else if (retval > 0)
1da177e4
LT
213 retval = 0;
214
215 return retval;
216}
217
218/*
219 * This routine will shutdown a serial port; interrupts are disabled, and
220 * DTR is dropped if the hangup on close termio flag is on. Calls to
221 * uart_shutdown are serialised by the per-port semaphore.
222 */
19225135 223static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
1da177e4 224{
ccce6deb 225 struct uart_port *uport = state->uart_port;
bdc04e31 226 struct tty_port *port = &state->port;
1da177e4 227
1da177e4 228 /*
ee31b337 229 * Set the TTY IO error marker
1da177e4 230 */
f751928e
AC
231 if (tty)
232 set_bit(TTY_IO_ERROR, &tty->flags);
1da177e4 233
bdc04e31 234 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
ee31b337
RK
235 /*
236 * Turn off DTR and RTS early.
237 */
ae84db96
PH
238 if (uart_console(uport) && tty)
239 uport->cons->cflag = tty->termios.c_cflag;
240
9db276f8 241 if (!tty || C_HUPCL(tty))
ccce6deb 242 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
ee31b337 243
b922e19d 244 uart_port_shutdown(port);
ee31b337 245 }
1da177e4
LT
246
247 /*
d208a3bf
DA
248 * It's possible for shutdown to be called after suspend if we get
249 * a DCD drop (hangup) at just the right time. Clear suspended bit so
250 * we don't try to resume a port that has been shutdown.
1da177e4 251 */
d208a3bf 252 clear_bit(ASYNCB_SUSPENDED, &port->flags);
1da177e4
LT
253
254 /*
255 * Free the transmit buffer page.
256 */
ebd2c8f6
AC
257 if (state->xmit.buf) {
258 free_page((unsigned long)state->xmit.buf);
259 state->xmit.buf = NULL;
1da177e4 260 }
1da177e4
LT
261}
262
263/**
264 * uart_update_timeout - update per-port FIFO timeout.
265 * @port: uart_port structure describing the port
266 * @cflag: termios cflag value
267 * @baud: speed of the port
268 *
269 * Set the port FIFO timeout value. The @cflag value should
270 * reflect the actual hardware settings.
271 */
272void
273uart_update_timeout(struct uart_port *port, unsigned int cflag,
274 unsigned int baud)
275{
276 unsigned int bits;
277
278 /* byte size and parity */
279 switch (cflag & CSIZE) {
280 case CS5:
281 bits = 7;
282 break;
283 case CS6:
284 bits = 8;
285 break;
286 case CS7:
287 bits = 9;
288 break;
289 default:
290 bits = 10;
a46c9994 291 break; /* CS8 */
1da177e4
LT
292 }
293
294 if (cflag & CSTOPB)
295 bits++;
296 if (cflag & PARENB)
297 bits++;
298
299 /*
300 * The total number of bits to be transmitted in the fifo.
301 */
302 bits = bits * port->fifosize;
303
304 /*
305 * Figure the timeout to send the above number of bits.
306 * Add .02 seconds of slop
307 */
308 port->timeout = (HZ * bits) / baud + HZ/50;
309}
310
311EXPORT_SYMBOL(uart_update_timeout);
312
313/**
314 * uart_get_baud_rate - return baud rate for a particular port
315 * @port: uart_port structure describing the port in question.
316 * @termios: desired termios settings.
317 * @old: old termios (or NULL)
318 * @min: minimum acceptable baud rate
319 * @max: maximum acceptable baud rate
320 *
321 * Decode the termios structure into a numeric baud rate,
322 * taking account of the magic 38400 baud rate (with spd_*
323 * flags), and mapping the %B0 rate to 9600 baud.
324 *
325 * If the new baud rate is invalid, try the old termios setting.
326 * If it's still invalid, we try 9600 baud.
327 *
328 * Update the @termios structure to reflect the baud rate
eb424fd2
AC
329 * we're actually going to be using. Don't do this for the case
330 * where B0 is requested ("hang up").
1da177e4
LT
331 */
332unsigned int
606d099c
AC
333uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
334 struct ktermios *old, unsigned int min, unsigned int max)
1da177e4 335{
f10a2233
JN
336 unsigned int try;
337 unsigned int baud;
338 unsigned int altbaud;
eb424fd2 339 int hung_up = 0;
0077d45e 340 upf_t flags = port->flags & UPF_SPD_MASK;
1da177e4 341
f10a2233
JN
342 switch (flags) {
343 case UPF_SPD_HI:
1da177e4 344 altbaud = 57600;
f10a2233
JN
345 break;
346 case UPF_SPD_VHI:
1da177e4 347 altbaud = 115200;
f10a2233
JN
348 break;
349 case UPF_SPD_SHI:
1da177e4 350 altbaud = 230400;
f10a2233
JN
351 break;
352 case UPF_SPD_WARP:
1da177e4 353 altbaud = 460800;
f10a2233
JN
354 break;
355 default:
356 altbaud = 38400;
357 break;
358 }
1da177e4
LT
359
360 for (try = 0; try < 2; try++) {
361 baud = tty_termios_baud_rate(termios);
362
363 /*
364 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
365 * Die! Die! Die!
366 */
547039ec 367 if (try == 0 && baud == 38400)
1da177e4
LT
368 baud = altbaud;
369
370 /*
371 * Special case: B0 rate.
372 */
eb424fd2
AC
373 if (baud == 0) {
374 hung_up = 1;
1da177e4 375 baud = 9600;
eb424fd2 376 }
1da177e4
LT
377
378 if (baud >= min && baud <= max)
379 return baud;
380
381 /*
382 * Oops, the quotient was zero. Try again with
383 * the old baud rate if possible.
384 */
385 termios->c_cflag &= ~CBAUD;
386 if (old) {
6d4d67be 387 baud = tty_termios_baud_rate(old);
eb424fd2
AC
388 if (!hung_up)
389 tty_termios_encode_baud_rate(termios,
390 baud, baud);
1da177e4
LT
391 old = NULL;
392 continue;
393 }
394
395 /*
16ae2a87
AC
396 * As a last resort, if the range cannot be met then clip to
397 * the nearest chip supported rate.
1da177e4 398 */
16ae2a87
AC
399 if (!hung_up) {
400 if (baud <= min)
401 tty_termios_encode_baud_rate(termios,
402 min + 1, min + 1);
403 else
404 tty_termios_encode_baud_rate(termios,
405 max - 1, max - 1);
406 }
1da177e4 407 }
16ae2a87
AC
408 /* Should never happen */
409 WARN_ON(1);
1da177e4
LT
410 return 0;
411}
412
413EXPORT_SYMBOL(uart_get_baud_rate);
414
415/**
416 * uart_get_divisor - return uart clock divisor
417 * @port: uart_port structure describing the port.
418 * @baud: desired baud rate
419 *
420 * Calculate the uart clock divisor for the port.
421 */
422unsigned int
423uart_get_divisor(struct uart_port *port, unsigned int baud)
424{
425 unsigned int quot;
426
427 /*
428 * Old custom speed handling.
429 */
430 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
431 quot = port->custom_divisor;
432 else
97d24634 433 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
1da177e4
LT
434
435 return quot;
436}
437
438EXPORT_SYMBOL(uart_get_divisor);
439
7c8ab967 440/* Caller holds port mutex */
19225135
AC
441static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
442 struct ktermios *old_termios)
1da177e4 443{
ccce6deb 444 struct uart_port *uport = state->uart_port;
606d099c 445 struct ktermios *termios;
391f93f2 446 int hw_stopped;
1da177e4
LT
447
448 /*
449 * If we have no tty, termios, or the port does not exist,
450 * then we can't set the parameters for this port.
451 */
adc8d746 452 if (!tty || uport->type == PORT_UNKNOWN)
1da177e4
LT
453 return;
454
adc8d746 455 termios = &tty->termios;
c18b55fd 456 uport->ops->set_termios(uport, termios, old_termios);
1da177e4
LT
457
458 /*
299245a1 459 * Set modem status enables based on termios cflag
1da177e4 460 */
299245a1 461 spin_lock_irq(&uport->lock);
1da177e4 462 if (termios->c_cflag & CRTSCTS)
299245a1 463 uport->status |= UPSTAT_CTS_ENABLE;
1da177e4 464 else
299245a1 465 uport->status &= ~UPSTAT_CTS_ENABLE;
1da177e4
LT
466
467 if (termios->c_cflag & CLOCAL)
299245a1 468 uport->status &= ~UPSTAT_DCD_ENABLE;
1da177e4 469 else
299245a1 470 uport->status |= UPSTAT_DCD_ENABLE;
391f93f2
PH
471
472 /* reset sw-assisted CTS flow control based on (possibly) new mode */
473 hw_stopped = uport->hw_stopped;
474 uport->hw_stopped = uart_softcts_mode(uport) &&
475 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
476 if (uport->hw_stopped) {
477 if (!hw_stopped)
478 uport->ops->stop_tx(uport);
479 } else {
480 if (hw_stopped)
481 __uart_start(tty);
482 }
299245a1 483 spin_unlock_irq(&uport->lock);
1da177e4
LT
484}
485
f5291ecc 486static int uart_put_char(struct tty_struct *tty, unsigned char c)
1da177e4 487{
f5291ecc
PH
488 struct uart_state *state = tty->driver_data;
489 struct uart_port *port = state->uart_port;
490 struct circ_buf *circ;
1da177e4 491 unsigned long flags;
23d22cea 492 int ret = 0;
1da177e4 493
f5291ecc 494 circ = &state->xmit;
1da177e4 495 if (!circ->buf)
23d22cea 496 return 0;
1da177e4
LT
497
498 spin_lock_irqsave(&port->lock, flags);
499 if (uart_circ_chars_free(circ) != 0) {
500 circ->buf[circ->head] = c;
501 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
23d22cea 502 ret = 1;
1da177e4
LT
503 }
504 spin_unlock_irqrestore(&port->lock, flags);
23d22cea 505 return ret;
1da177e4
LT
506}
507
1da177e4
LT
508static void uart_flush_chars(struct tty_struct *tty)
509{
510 uart_start(tty);
511}
512
19225135
AC
513static int uart_write(struct tty_struct *tty,
514 const unsigned char *buf, int count)
1da177e4
LT
515{
516 struct uart_state *state = tty->driver_data;
d5f735e5
PM
517 struct uart_port *port;
518 struct circ_buf *circ;
1da177e4
LT
519 unsigned long flags;
520 int c, ret = 0;
521
d5f735e5
PM
522 /*
523 * This means you called this function _after_ the port was
524 * closed. No cookie for you.
525 */
f751928e 526 if (!state) {
d5f735e5
PM
527 WARN_ON(1);
528 return -EL3HLT;
529 }
530
ebd2c8f6
AC
531 port = state->uart_port;
532 circ = &state->xmit;
d5f735e5 533
1da177e4
LT
534 if (!circ->buf)
535 return 0;
536
537 spin_lock_irqsave(&port->lock, flags);
538 while (1) {
539 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
540 if (count < c)
541 c = count;
542 if (c <= 0)
543 break;
544 memcpy(circ->buf + circ->head, buf, c);
545 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
546 buf += c;
547 count -= c;
548 ret += c;
549 }
64dbee31
PH
550
551 __uart_start(tty);
1da177e4
LT
552 spin_unlock_irqrestore(&port->lock, flags);
553
1da177e4
LT
554 return ret;
555}
556
557static int uart_write_room(struct tty_struct *tty)
558{
559 struct uart_state *state = tty->driver_data;
f34d7a5b
AC
560 unsigned long flags;
561 int ret;
1da177e4 562
ebd2c8f6
AC
563 spin_lock_irqsave(&state->uart_port->lock, flags);
564 ret = uart_circ_chars_free(&state->xmit);
565 spin_unlock_irqrestore(&state->uart_port->lock, flags);
f34d7a5b 566 return ret;
1da177e4
LT
567}
568
569static int uart_chars_in_buffer(struct tty_struct *tty)
570{
571 struct uart_state *state = tty->driver_data;
f34d7a5b
AC
572 unsigned long flags;
573 int ret;
1da177e4 574
ebd2c8f6
AC
575 spin_lock_irqsave(&state->uart_port->lock, flags);
576 ret = uart_circ_chars_pending(&state->xmit);
577 spin_unlock_irqrestore(&state->uart_port->lock, flags);
f34d7a5b 578 return ret;
1da177e4
LT
579}
580
581static void uart_flush_buffer(struct tty_struct *tty)
582{
583 struct uart_state *state = tty->driver_data;
55d7b689 584 struct uart_port *port;
1da177e4
LT
585 unsigned long flags;
586
d5f735e5
PM
587 /*
588 * This means you called this function _after_ the port was
589 * closed. No cookie for you.
590 */
f751928e 591 if (!state) {
d5f735e5
PM
592 WARN_ON(1);
593 return;
594 }
595
ebd2c8f6 596 port = state->uart_port;
eb3a1e11 597 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
1da177e4
LT
598
599 spin_lock_irqsave(&port->lock, flags);
ebd2c8f6 600 uart_circ_clear(&state->xmit);
6bb0e3a5
HS
601 if (port->ops->flush_buffer)
602 port->ops->flush_buffer(port);
1da177e4
LT
603 spin_unlock_irqrestore(&port->lock, flags);
604 tty_wakeup(tty);
605}
606
607/*
608 * This function is used to send a high-priority XON/XOFF character to
609 * the device
610 */
611static void uart_send_xchar(struct tty_struct *tty, char ch)
612{
613 struct uart_state *state = tty->driver_data;
ebd2c8f6 614 struct uart_port *port = state->uart_port;
1da177e4
LT
615 unsigned long flags;
616
617 if (port->ops->send_xchar)
618 port->ops->send_xchar(port, ch);
619 else {
c235ccc1 620 spin_lock_irqsave(&port->lock, flags);
1da177e4 621 port->x_char = ch;
c235ccc1 622 if (ch)
b129a8cc 623 port->ops->start_tx(port);
c235ccc1 624 spin_unlock_irqrestore(&port->lock, flags);
1da177e4
LT
625 }
626}
627
628static void uart_throttle(struct tty_struct *tty)
629{
630 struct uart_state *state = tty->driver_data;
9aba8d5b 631 struct uart_port *port = state->uart_port;
391f93f2 632 upstat_t mask = 0;
1da177e4
LT
633
634 if (I_IXOFF(tty))
391f93f2 635 mask |= UPSTAT_AUTOXOFF;
9db276f8 636 if (C_CRTSCTS(tty))
391f93f2 637 mask |= UPSTAT_AUTORTS;
9aba8d5b 638
391f93f2 639 if (port->status & mask) {
9aba8d5b 640 port->ops->throttle(port);
391f93f2 641 mask &= ~port->status;
9aba8d5b
RK
642 }
643
391f93f2 644 if (mask & UPSTAT_AUTOXOFF)
1da177e4
LT
645 uart_send_xchar(tty, STOP_CHAR(tty));
646
391f93f2 647 if (mask & UPSTAT_AUTORTS)
9aba8d5b 648 uart_clear_mctrl(port, TIOCM_RTS);
1da177e4
LT
649}
650
651static void uart_unthrottle(struct tty_struct *tty)
652{
653 struct uart_state *state = tty->driver_data;
ebd2c8f6 654 struct uart_port *port = state->uart_port;
391f93f2 655 upstat_t mask = 0;
1da177e4 656
9aba8d5b 657 if (I_IXOFF(tty))
391f93f2 658 mask |= UPSTAT_AUTOXOFF;
9db276f8 659 if (C_CRTSCTS(tty))
391f93f2 660 mask |= UPSTAT_AUTORTS;
9aba8d5b 661
391f93f2 662 if (port->status & mask) {
9aba8d5b 663 port->ops->unthrottle(port);
391f93f2 664 mask &= ~port->status;
9aba8d5b 665 }
1da177e4 666
391f93f2 667 if (mask & UPSTAT_AUTOXOFF)
fba594a8 668 uart_send_xchar(tty, START_CHAR(tty));
1da177e4 669
391f93f2 670 if (mask & UPSTAT_AUTORTS)
1da177e4
LT
671 uart_set_mctrl(port, TIOCM_RTS);
672}
673
3abe8c76 674static void uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
1da177e4 675{
9f109694 676 struct uart_state *state = container_of(port, struct uart_state, port);
a2bceae0 677 struct uart_port *uport = state->uart_port;
f34d7a5b 678
37cd0c99 679 memset(retinfo, 0, sizeof(*retinfo));
f34d7a5b 680
3abe8c76
PH
681 /*
682 * Ensure the state we copy is consistent and no hardware changes
683 * occur as we go
684 */
685 mutex_lock(&port->mutex);
7ba2e769
AC
686 retinfo->type = uport->type;
687 retinfo->line = uport->line;
688 retinfo->port = uport->iobase;
1da177e4 689 if (HIGH_BITS_OFFSET)
7ba2e769
AC
690 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
691 retinfo->irq = uport->irq;
692 retinfo->flags = uport->flags;
693 retinfo->xmit_fifo_size = uport->fifosize;
694 retinfo->baud_base = uport->uartclk / 16;
695 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
696 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
1da177e4 697 ASYNC_CLOSING_WAIT_NONE :
4cb0fbfd 698 jiffies_to_msecs(port->closing_wait) / 10;
7ba2e769
AC
699 retinfo->custom_divisor = uport->custom_divisor;
700 retinfo->hub6 = uport->hub6;
701 retinfo->io_type = uport->iotype;
702 retinfo->iomem_reg_shift = uport->regshift;
703 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
a2bceae0 704 mutex_unlock(&port->mutex);
9f109694
AC
705}
706
707static int uart_get_info_user(struct tty_port *port,
708 struct serial_struct __user *retinfo)
709{
710 struct serial_struct tmp;
3abe8c76 711
9f109694 712 uart_get_info(port, &tmp);
f34d7a5b 713
1da177e4
LT
714 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
715 return -EFAULT;
716 return 0;
717}
718
7ba2e769
AC
719static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
720 struct uart_state *state,
721 struct serial_struct *new_info)
1da177e4 722{
46d57a44 723 struct uart_port *uport = state->uart_port;
1da177e4 724 unsigned long new_port;
0077d45e 725 unsigned int change_irq, change_port, closing_wait;
1da177e4 726 unsigned int old_custom_divisor, close_delay;
0077d45e 727 upf_t old_flags, new_flags;
1da177e4
LT
728 int retval = 0;
729
7ba2e769 730 new_port = new_info->port;
1da177e4 731 if (HIGH_BITS_OFFSET)
7ba2e769 732 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
1da177e4 733
7ba2e769
AC
734 new_info->irq = irq_canonicalize(new_info->irq);
735 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
736 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
4cb0fbfd 737 ASYNC_CLOSING_WAIT_NONE :
7ba2e769 738 msecs_to_jiffies(new_info->closing_wait * 10);
1da177e4 739
1da177e4 740
46d57a44 741 change_irq = !(uport->flags & UPF_FIXED_PORT)
7ba2e769 742 && new_info->irq != uport->irq;
1da177e4
LT
743
744 /*
745 * Since changing the 'type' of the port changes its resource
746 * allocations, we should treat type changes the same as
747 * IO port changes.
748 */
46d57a44
AC
749 change_port = !(uport->flags & UPF_FIXED_PORT)
750 && (new_port != uport->iobase ||
7ba2e769
AC
751 (unsigned long)new_info->iomem_base != uport->mapbase ||
752 new_info->hub6 != uport->hub6 ||
753 new_info->io_type != uport->iotype ||
754 new_info->iomem_reg_shift != uport->regshift ||
755 new_info->type != uport->type);
46d57a44
AC
756
757 old_flags = uport->flags;
7ba2e769 758 new_flags = new_info->flags;
46d57a44 759 old_custom_divisor = uport->custom_divisor;
1da177e4
LT
760
761 if (!capable(CAP_SYS_ADMIN)) {
762 retval = -EPERM;
763 if (change_irq || change_port ||
7ba2e769 764 (new_info->baud_base != uport->uartclk / 16) ||
46d57a44
AC
765 (close_delay != port->close_delay) ||
766 (closing_wait != port->closing_wait) ||
7ba2e769
AC
767 (new_info->xmit_fifo_size &&
768 new_info->xmit_fifo_size != uport->fifosize) ||
0077d45e 769 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
1da177e4 770 goto exit;
46d57a44 771 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
0077d45e 772 (new_flags & UPF_USR_MASK));
7ba2e769 773 uport->custom_divisor = new_info->custom_divisor;
1da177e4
LT
774 goto check_and_exit;
775 }
776
777 /*
778 * Ask the low level driver to verify the settings.
779 */
46d57a44 780 if (uport->ops->verify_port)
7ba2e769 781 retval = uport->ops->verify_port(uport, new_info);
1da177e4 782
7ba2e769
AC
783 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
784 (new_info->baud_base < 9600))
1da177e4
LT
785 retval = -EINVAL;
786
787 if (retval)
788 goto exit;
789
790 if (change_port || change_irq) {
791 retval = -EBUSY;
792
793 /*
794 * Make sure that we are the sole user of this port.
795 */
b58d13a0 796 if (tty_port_users(port) > 1)
1da177e4
LT
797 goto exit;
798
799 /*
800 * We need to shutdown the serial port at the old
801 * port/type/irq combination.
802 */
19225135 803 uart_shutdown(tty, state);
1da177e4
LT
804 }
805
806 if (change_port) {
807 unsigned long old_iobase, old_mapbase;
808 unsigned int old_type, old_iotype, old_hub6, old_shift;
809
46d57a44
AC
810 old_iobase = uport->iobase;
811 old_mapbase = uport->mapbase;
812 old_type = uport->type;
813 old_hub6 = uport->hub6;
814 old_iotype = uport->iotype;
815 old_shift = uport->regshift;
1da177e4
LT
816
817 /*
818 * Free and release old regions
819 */
820 if (old_type != PORT_UNKNOWN)
46d57a44 821 uport->ops->release_port(uport);
1da177e4 822
46d57a44 823 uport->iobase = new_port;
7ba2e769
AC
824 uport->type = new_info->type;
825 uport->hub6 = new_info->hub6;
826 uport->iotype = new_info->io_type;
827 uport->regshift = new_info->iomem_reg_shift;
828 uport->mapbase = (unsigned long)new_info->iomem_base;
1da177e4
LT
829
830 /*
831 * Claim and map the new regions
832 */
46d57a44
AC
833 if (uport->type != PORT_UNKNOWN) {
834 retval = uport->ops->request_port(uport);
1da177e4
LT
835 } else {
836 /* Always success - Jean II */
837 retval = 0;
838 }
839
840 /*
841 * If we fail to request resources for the
842 * new port, try to restore the old settings.
843 */
7deb39ed 844 if (retval) {
46d57a44
AC
845 uport->iobase = old_iobase;
846 uport->type = old_type;
847 uport->hub6 = old_hub6;
848 uport->iotype = old_iotype;
849 uport->regshift = old_shift;
850 uport->mapbase = old_mapbase;
1da177e4 851
7deb39ed
TP
852 if (old_type != PORT_UNKNOWN) {
853 retval = uport->ops->request_port(uport);
854 /*
855 * If we failed to restore the old settings,
856 * we fail like this.
857 */
858 if (retval)
859 uport->type = PORT_UNKNOWN;
860
861 /*
862 * We failed anyway.
863 */
864 retval = -EBUSY;
865 }
866
a46c9994
AC
867 /* Added to return the correct error -Ram Gupta */
868 goto exit;
1da177e4
LT
869 }
870 }
871
abb4a239 872 if (change_irq)
7ba2e769 873 uport->irq = new_info->irq;
46d57a44 874 if (!(uport->flags & UPF_FIXED_PORT))
7ba2e769 875 uport->uartclk = new_info->baud_base * 16;
46d57a44 876 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
0077d45e 877 (new_flags & UPF_CHANGE_MASK);
7ba2e769 878 uport->custom_divisor = new_info->custom_divisor;
46d57a44
AC
879 port->close_delay = close_delay;
880 port->closing_wait = closing_wait;
7ba2e769
AC
881 if (new_info->xmit_fifo_size)
882 uport->fifosize = new_info->xmit_fifo_size;
d6c53c0e 883 port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
1da177e4
LT
884
885 check_and_exit:
886 retval = 0;
46d57a44 887 if (uport->type == PORT_UNKNOWN)
1da177e4 888 goto exit;
ccce6deb 889 if (port->flags & ASYNC_INITIALIZED) {
46d57a44
AC
890 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
891 old_custom_divisor != uport->custom_divisor) {
1da177e4
LT
892 /*
893 * If they're setting up a custom divisor or speed,
894 * instead of clearing it, then bitch about it. No
895 * need to rate-limit; it's CAP_SYS_ADMIN only.
896 */
46d57a44 897 if (uport->flags & UPF_SPD_MASK) {
2f2dafe7
SM
898 dev_notice(uport->dev,
899 "%s sets custom speed on %s. This is deprecated.\n",
900 current->comm,
429b4749 901 tty_name(port->tty));
1da177e4 902 }
19225135 903 uart_change_speed(tty, state, NULL);
1da177e4
LT
904 }
905 } else
19225135 906 retval = uart_startup(tty, state, 1);
1da177e4 907 exit:
7ba2e769
AC
908 return retval;
909}
910
911static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
912 struct serial_struct __user *newinfo)
913{
914 struct serial_struct new_serial;
915 struct tty_port *port = &state->port;
916 int retval;
917
918 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
919 return -EFAULT;
920
921 /*
922 * This semaphore protects port->count. It is also
923 * very useful to prevent opens. Also, take the
924 * port configuration semaphore to make sure that a
925 * module insertion/removal doesn't change anything
926 * under us.
927 */
928 mutex_lock(&port->mutex);
929 retval = uart_set_info(tty, port, state, &new_serial);
a2bceae0 930 mutex_unlock(&port->mutex);
1da177e4
LT
931 return retval;
932}
933
19225135
AC
934/**
935 * uart_get_lsr_info - get line status register info
936 * @tty: tty associated with the UART
937 * @state: UART being queried
938 * @value: returned modem value
939 *
940 * Note: uart_ioctl protects us against hangups.
1da177e4 941 */
19225135
AC
942static int uart_get_lsr_info(struct tty_struct *tty,
943 struct uart_state *state, unsigned int __user *value)
1da177e4 944{
46d57a44 945 struct uart_port *uport = state->uart_port;
1da177e4
LT
946 unsigned int result;
947
46d57a44 948 result = uport->ops->tx_empty(uport);
1da177e4
LT
949
950 /*
951 * If we're about to load something into the transmit
952 * register, we'll pretend the transmitter isn't empty to
953 * avoid a race condition (depending on when the transmit
954 * interrupt happens).
955 */
46d57a44 956 if (uport->x_char ||
ebd2c8f6 957 ((uart_circ_chars_pending(&state->xmit) > 0) &&
d01f4d18 958 !uart_tx_stopped(uport)))
1da177e4 959 result &= ~TIOCSER_TEMT;
a46c9994 960
1da177e4
LT
961 return put_user(result, value);
962}
963
60b33c13 964static int uart_tiocmget(struct tty_struct *tty)
1da177e4
LT
965{
966 struct uart_state *state = tty->driver_data;
a2bceae0 967 struct tty_port *port = &state->port;
46d57a44 968 struct uart_port *uport = state->uart_port;
1da177e4
LT
969 int result = -EIO;
970
a2bceae0 971 mutex_lock(&port->mutex);
60b33c13 972 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
46d57a44 973 result = uport->mctrl;
46d57a44
AC
974 spin_lock_irq(&uport->lock);
975 result |= uport->ops->get_mctrl(uport);
976 spin_unlock_irq(&uport->lock);
1da177e4 977 }
a2bceae0 978 mutex_unlock(&port->mutex);
1da177e4
LT
979
980 return result;
981}
982
983static int
20b9d177 984uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1da177e4
LT
985{
986 struct uart_state *state = tty->driver_data;
46d57a44 987 struct uart_port *uport = state->uart_port;
a2bceae0 988 struct tty_port *port = &state->port;
1da177e4
LT
989 int ret = -EIO;
990
a2bceae0 991 mutex_lock(&port->mutex);
20b9d177 992 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
46d57a44 993 uart_update_mctrl(uport, set, clear);
1da177e4
LT
994 ret = 0;
995 }
a2bceae0 996 mutex_unlock(&port->mutex);
1da177e4
LT
997 return ret;
998}
999
9e98966c 1000static int uart_break_ctl(struct tty_struct *tty, int break_state)
1da177e4
LT
1001{
1002 struct uart_state *state = tty->driver_data;
a2bceae0 1003 struct tty_port *port = &state->port;
46d57a44 1004 struct uart_port *uport = state->uart_port;
1da177e4 1005
a2bceae0 1006 mutex_lock(&port->mutex);
1da177e4 1007
46d57a44
AC
1008 if (uport->type != PORT_UNKNOWN)
1009 uport->ops->break_ctl(uport, break_state);
1da177e4 1010
a2bceae0 1011 mutex_unlock(&port->mutex);
9e98966c 1012 return 0;
1da177e4
LT
1013}
1014
19225135 1015static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1da177e4 1016{
46d57a44 1017 struct uart_port *uport = state->uart_port;
a2bceae0 1018 struct tty_port *port = &state->port;
1da177e4
LT
1019 int flags, ret;
1020
1021 if (!capable(CAP_SYS_ADMIN))
1022 return -EPERM;
1023
1024 /*
1025 * Take the per-port semaphore. This prevents count from
1026 * changing, and hence any extra opens of the port while
1027 * we're auto-configuring.
1028 */
a2bceae0 1029 if (mutex_lock_interruptible(&port->mutex))
1da177e4
LT
1030 return -ERESTARTSYS;
1031
1032 ret = -EBUSY;
b58d13a0 1033 if (tty_port_users(port) == 1) {
19225135 1034 uart_shutdown(tty, state);
1da177e4
LT
1035
1036 /*
1037 * If we already have a port type configured,
1038 * we must release its resources.
1039 */
46d57a44
AC
1040 if (uport->type != PORT_UNKNOWN)
1041 uport->ops->release_port(uport);
1da177e4
LT
1042
1043 flags = UART_CONFIG_TYPE;
46d57a44 1044 if (uport->flags & UPF_AUTO_IRQ)
1da177e4
LT
1045 flags |= UART_CONFIG_IRQ;
1046
1047 /*
1048 * This will claim the ports resources if
1049 * a port is found.
1050 */
46d57a44 1051 uport->ops->config_port(uport, flags);
1da177e4 1052
19225135 1053 ret = uart_startup(tty, state, 1);
1da177e4 1054 }
a2bceae0 1055 mutex_unlock(&port->mutex);
1da177e4
LT
1056 return ret;
1057}
1058
1fdc3106
AS
1059static void uart_enable_ms(struct uart_port *uport)
1060{
1061 /*
1062 * Force modem status interrupts on
1063 */
1064 if (uport->ops->enable_ms)
1065 uport->ops->enable_ms(uport);
1066}
1067
1da177e4
LT
1068/*
1069 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1070 * - mask passed in arg for lines of interest
1071 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1072 * Caller should use TIOCGICOUNT to see which one it was
bdc04e31
AC
1073 *
1074 * FIXME: This wants extracting into a common all driver implementation
1075 * of TIOCMWAIT using tty_port.
1da177e4
LT
1076 */
1077static int
1078uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1079{
46d57a44 1080 struct uart_port *uport = state->uart_port;
bdc04e31 1081 struct tty_port *port = &state->port;
1da177e4
LT
1082 DECLARE_WAITQUEUE(wait, current);
1083 struct uart_icount cprev, cnow;
1084 int ret;
1085
1086 /*
1087 * note the counters on entry
1088 */
46d57a44
AC
1089 spin_lock_irq(&uport->lock);
1090 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1fdc3106 1091 uart_enable_ms(uport);
46d57a44 1092 spin_unlock_irq(&uport->lock);
1da177e4 1093
bdc04e31 1094 add_wait_queue(&port->delta_msr_wait, &wait);
1da177e4 1095 for (;;) {
46d57a44
AC
1096 spin_lock_irq(&uport->lock);
1097 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1098 spin_unlock_irq(&uport->lock);
1da177e4
LT
1099
1100 set_current_state(TASK_INTERRUPTIBLE);
1101
1102 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1103 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1104 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1105 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
a46c9994
AC
1106 ret = 0;
1107 break;
1da177e4
LT
1108 }
1109
1110 schedule();
1111
1112 /* see if a signal did it */
1113 if (signal_pending(current)) {
1114 ret = -ERESTARTSYS;
1115 break;
1116 }
1117
1118 cprev = cnow;
1119 }
97f9f707 1120 __set_current_state(TASK_RUNNING);
bdc04e31 1121 remove_wait_queue(&port->delta_msr_wait, &wait);
1da177e4
LT
1122
1123 return ret;
1124}
1125
1126/*
1127 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1128 * Return: write counters to the user passed counter struct
1129 * NB: both 1->0 and 0->1 transitions are counted except for
1130 * RI where only 0->1 is counted.
1131 */
d281da7f
AC
1132static int uart_get_icount(struct tty_struct *tty,
1133 struct serial_icounter_struct *icount)
1da177e4 1134{
d281da7f 1135 struct uart_state *state = tty->driver_data;
1da177e4 1136 struct uart_icount cnow;
46d57a44 1137 struct uart_port *uport = state->uart_port;
1da177e4 1138
46d57a44
AC
1139 spin_lock_irq(&uport->lock);
1140 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1141 spin_unlock_irq(&uport->lock);
1da177e4 1142
d281da7f
AC
1143 icount->cts = cnow.cts;
1144 icount->dsr = cnow.dsr;
1145 icount->rng = cnow.rng;
1146 icount->dcd = cnow.dcd;
1147 icount->rx = cnow.rx;
1148 icount->tx = cnow.tx;
1149 icount->frame = cnow.frame;
1150 icount->overrun = cnow.overrun;
1151 icount->parity = cnow.parity;
1152 icount->brk = cnow.brk;
1153 icount->buf_overrun = cnow.buf_overrun;
1154
1155 return 0;
1da177e4
LT
1156}
1157
a5f276f1
RRD
1158static int uart_get_rs485_config(struct uart_port *port,
1159 struct serial_rs485 __user *rs485)
1160{
bd737f87
RRD
1161 unsigned long flags;
1162 struct serial_rs485 aux;
1163
1164 spin_lock_irqsave(&port->lock, flags);
1165 aux = port->rs485;
1166 spin_unlock_irqrestore(&port->lock, flags);
1167
1168 if (copy_to_user(rs485, &aux, sizeof(aux)))
a5f276f1 1169 return -EFAULT;
bd737f87 1170
a5f276f1
RRD
1171 return 0;
1172}
1173
1174static int uart_set_rs485_config(struct uart_port *port,
1175 struct serial_rs485 __user *rs485_user)
1176{
1177 struct serial_rs485 rs485;
1178 int ret;
bd737f87 1179 unsigned long flags;
a5f276f1
RRD
1180
1181 if (!port->rs485_config)
1182 return -ENOIOCTLCMD;
1183
1184 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1185 return -EFAULT;
1186
bd737f87 1187 spin_lock_irqsave(&port->lock, flags);
a5f276f1 1188 ret = port->rs485_config(port, &rs485);
bd737f87 1189 spin_unlock_irqrestore(&port->lock, flags);
a5f276f1
RRD
1190 if (ret)
1191 return ret;
1192
1193 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1194 return -EFAULT;
1195
1196 return 0;
1197}
1198
1da177e4 1199/*
e5238442 1200 * Called via sys_ioctl. We can use spin_lock_irq() here.
1da177e4
LT
1201 */
1202static int
6caa76b7 1203uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1da177e4
LT
1204 unsigned long arg)
1205{
1206 struct uart_state *state = tty->driver_data;
a2bceae0 1207 struct tty_port *port = &state->port;
1da177e4
LT
1208 void __user *uarg = (void __user *)arg;
1209 int ret = -ENOIOCTLCMD;
1210
1da177e4
LT
1211
1212 /*
1213 * These ioctls don't rely on the hardware to be present.
1214 */
1215 switch (cmd) {
1216 case TIOCGSERIAL:
9f109694 1217 ret = uart_get_info_user(port, uarg);
1da177e4
LT
1218 break;
1219
1220 case TIOCSSERIAL:
7c8ab967 1221 down_write(&tty->termios_rwsem);
7ba2e769 1222 ret = uart_set_info_user(tty, state, uarg);
7c8ab967 1223 up_write(&tty->termios_rwsem);
1da177e4
LT
1224 break;
1225
1226 case TIOCSERCONFIG:
7c8ab967 1227 down_write(&tty->termios_rwsem);
19225135 1228 ret = uart_do_autoconfig(tty, state);
7c8ab967 1229 up_write(&tty->termios_rwsem);
1da177e4
LT
1230 break;
1231
1232 case TIOCSERGWILD: /* obsolete */
1233 case TIOCSERSWILD: /* obsolete */
1234 ret = 0;
1235 break;
1236 }
1237
1238 if (ret != -ENOIOCTLCMD)
1239 goto out;
1240
1241 if (tty->flags & (1 << TTY_IO_ERROR)) {
1242 ret = -EIO;
1243 goto out;
1244 }
1245
1246 /*
1247 * The following should only be used when hardware is present.
1248 */
1249 switch (cmd) {
1250 case TIOCMIWAIT:
1251 ret = uart_wait_modem_status(state, arg);
1252 break;
1da177e4
LT
1253 }
1254
1255 if (ret != -ENOIOCTLCMD)
1256 goto out;
1257
a2bceae0 1258 mutex_lock(&port->mutex);
1da177e4 1259
6caa76b7 1260 if (tty->flags & (1 << TTY_IO_ERROR)) {
1da177e4
LT
1261 ret = -EIO;
1262 goto out_up;
1263 }
1264
1265 /*
1266 * All these rely on hardware being present and need to be
1267 * protected against the tty being hung up.
1268 */
a9c20a9c 1269
1da177e4 1270 switch (cmd) {
a9c20a9c
RRD
1271 case TIOCSERGETLSR: /* Get line status register */
1272 ret = uart_get_lsr_info(tty, state, uarg);
1273 break;
1274
a5f276f1
RRD
1275 case TIOCGRS485:
1276 ret = uart_get_rs485_config(state->uart_port, uarg);
1277 break;
1278
1279 case TIOCSRS485:
1280 ret = uart_set_rs485_config(state->uart_port, uarg);
1281 break;
1da177e4 1282 default: {
46d57a44
AC
1283 struct uart_port *uport = state->uart_port;
1284 if (uport->ops->ioctl)
1285 ret = uport->ops->ioctl(uport, cmd, arg);
1da177e4
LT
1286 break;
1287 }
1288 }
f34d7a5b 1289out_up:
a2bceae0 1290 mutex_unlock(&port->mutex);
f34d7a5b 1291out:
1da177e4
LT
1292 return ret;
1293}
1294
edeb280e 1295static void uart_set_ldisc(struct tty_struct *tty)
64e9159f
AC
1296{
1297 struct uart_state *state = tty->driver_data;
46d57a44 1298 struct uart_port *uport = state->uart_port;
64e9159f 1299
db1b9dfc
PH
1300 if (uport->ops->set_ldisc) {
1301 mutex_lock(&state->port.mutex);
732a84a0 1302 uport->ops->set_ldisc(uport, &tty->termios);
db1b9dfc
PH
1303 mutex_unlock(&state->port.mutex);
1304 }
64e9159f
AC
1305}
1306
a46c9994
AC
1307static void uart_set_termios(struct tty_struct *tty,
1308 struct ktermios *old_termios)
1da177e4
LT
1309{
1310 struct uart_state *state = tty->driver_data;
dec94e70 1311 struct uart_port *uport = state->uart_port;
adc8d746 1312 unsigned int cflag = tty->termios.c_cflag;
2cbacafd
RK
1313 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1314 bool sw_changed = false;
1da177e4 1315
2cbacafd
RK
1316 /*
1317 * Drivers doing software flow control also need to know
1318 * about changes to these input settings.
1319 */
1320 if (uport->flags & UPF_SOFT_FLOW) {
1321 iflag_mask |= IXANY|IXON|IXOFF;
1322 sw_changed =
1323 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1324 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1325 }
1da177e4
LT
1326
1327 /*
1328 * These are the bits that are used to setup various
20620d68
DW
1329 * flags in the low level driver. We can ignore the Bfoo
1330 * bits in c_cflag; c_[io]speed will always be set
1331 * appropriately by set_termios() in tty_ioctl.c
1da177e4 1332 */
1da177e4 1333 if ((cflag ^ old_termios->c_cflag) == 0 &&
adc8d746
AC
1334 tty->termios.c_ospeed == old_termios->c_ospeed &&
1335 tty->termios.c_ispeed == old_termios->c_ispeed &&
2cbacafd
RK
1336 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1337 !sw_changed) {
1da177e4 1338 return;
e5238442 1339 }
1da177e4 1340
7c8ab967 1341 mutex_lock(&state->port.mutex);
19225135 1342 uart_change_speed(tty, state, old_termios);
7c8ab967 1343 mutex_unlock(&state->port.mutex);
c18b55fd
PH
1344 /* reload cflag from termios; port driver may have overriden flags */
1345 cflag = tty->termios.c_cflag;
1da177e4
LT
1346
1347 /* Handle transition to B0 status */
1348 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
dec94e70 1349 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1da177e4 1350 /* Handle transition away from B0 status */
82cb7ba1 1351 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1da177e4 1352 unsigned int mask = TIOCM_DTR;
99abf3b9 1353 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
1da177e4 1354 mask |= TIOCM_RTS;
dec94e70 1355 uart_set_mctrl(uport, mask);
1da177e4 1356 }
1da177e4
LT
1357}
1358
1359/*
ef4f527c
KC
1360 * Calls to uart_close() are serialised via the tty_lock in
1361 * drivers/tty/tty_io.c:tty_release()
1362 * drivers/tty/tty_io.c:do_tty_hangup()
1363 * This runs from a workqueue and can sleep for a _short_ time only.
1da177e4
LT
1364 */
1365static void uart_close(struct tty_struct *tty, struct file *filp)
1366{
1367 struct uart_state *state = tty->driver_data;
46d57a44
AC
1368 struct tty_port *port;
1369 struct uart_port *uport;
a46c9994 1370
91b32f54
PH
1371 if (!state) {
1372 struct uart_driver *drv = tty->driver->driver_state;
1373
1374 state = drv->state + tty->index;
1375 port = &state->port;
1376 spin_lock_irq(&port->lock);
1377 --port->count;
1378 spin_unlock_irq(&port->lock);
eea7e17e 1379 return;
91b32f54 1380 }
eea7e17e 1381
46d57a44
AC
1382 uport = state->uart_port;
1383 port = &state->port;
39b3d892 1384 pr_debug("uart_close(%d) called\n", tty->index);
1da177e4 1385
4ed94cd4 1386 if (!port->count || tty_port_close_start(port, tty, filp) == 0)
55956216 1387 return;
1da177e4
LT
1388
1389 /*
1390 * At this point, we stop accepting input. To do this, we
1391 * disable the receive line status interrupts.
1392 */
ccce6deb 1393 if (port->flags & ASYNC_INITIALIZED) {
6fb98fb3 1394 spin_lock_irq(&uport->lock);
46d57a44 1395 uport->ops->stop_rx(uport);
6fb98fb3 1396 spin_unlock_irq(&uport->lock);
1da177e4
LT
1397 /*
1398 * Before we drop DTR, make sure the UART transmitter
1399 * has completely drained; this is especially
1400 * important if there is a transmit FIFO!
1401 */
1f33a51d 1402 uart_wait_until_sent(tty, uport->timeout);
1da177e4
LT
1403 }
1404
bafb0bd2 1405 mutex_lock(&port->mutex);
19225135 1406 uart_shutdown(tty, state);
7b01478f 1407 tty_port_tty_set(port, NULL);
e144c58c 1408
6fb98fb3 1409 spin_lock_irq(&port->lock);
1da177e4 1410
46d57a44 1411 if (port->blocked_open) {
6fb98fb3 1412 spin_unlock_irq(&port->lock);
46d57a44 1413 if (port->close_delay)
74866e75 1414 msleep_interruptible(jiffies_to_msecs(port->close_delay));
6fb98fb3 1415 spin_lock_irq(&port->lock);
46d57a44 1416 } else if (!uart_console(uport)) {
6fb98fb3 1417 spin_unlock_irq(&port->lock);
6f538fe3 1418 uart_change_pm(state, UART_PM_STATE_OFF);
6fb98fb3 1419 spin_lock_irq(&port->lock);
1da177e4
LT
1420 }
1421
1422 /*
1423 * Wake up anyone trying to open this port.
1424 */
ccce6deb 1425 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
426929f8 1426 clear_bit(ASYNCB_CLOSING, &port->flags);
6fb98fb3 1427 spin_unlock_irq(&port->lock);
46d57a44 1428 wake_up_interruptible(&port->open_wait);
1da177e4 1429
a2bceae0 1430 mutex_unlock(&port->mutex);
2e758910
PH
1431
1432 tty_ldisc_flush(tty);
e144c58c 1433 tty->closing = 0;
1da177e4
LT
1434}
1435
1f33a51d 1436static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1da177e4 1437{
1f33a51d
JS
1438 struct uart_state *state = tty->driver_data;
1439 struct uart_port *port = state->uart_port;
1da177e4
LT
1440 unsigned long char_time, expire;
1441
1da177e4
LT
1442 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1443 return;
1444
1445 /*
1446 * Set the check interval to be 1/5 of the estimated time to
1447 * send a single character, and make it at least 1. The check
1448 * interval should also be less than the timeout.
1449 *
1450 * Note: we have to use pretty tight timings here to satisfy
1451 * the NIST-PCTS.
1452 */
1453 char_time = (port->timeout - HZ/50) / port->fifosize;
1454 char_time = char_time / 5;
1455 if (char_time == 0)
1456 char_time = 1;
1457 if (timeout && timeout < char_time)
1458 char_time = timeout;
1459
1460 /*
1461 * If the transmitter hasn't cleared in twice the approximate
1462 * amount of time to send the entire FIFO, it probably won't
1463 * ever clear. This assumes the UART isn't doing flow
1464 * control, which is currently the case. Hence, if it ever
1465 * takes longer than port->timeout, this is probably due to a
1466 * UART bug of some kind. So, we clamp the timeout parameter at
1467 * 2*port->timeout.
1468 */
1469 if (timeout == 0 || timeout > 2 * port->timeout)
1470 timeout = 2 * port->timeout;
1471
1472 expire = jiffies + timeout;
1473
eb3a1e11 1474 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
a46c9994 1475 port->line, jiffies, expire);
1da177e4
LT
1476
1477 /*
1478 * Check whether the transmitter is empty every 'char_time'.
1479 * 'timeout' / 'expire' give us the maximum amount of time
1480 * we wait.
1481 */
1482 while (!port->ops->tx_empty(port)) {
1483 msleep_interruptible(jiffies_to_msecs(char_time));
1484 if (signal_pending(current))
1485 break;
1486 if (time_after(jiffies, expire))
1487 break;
1488 }
20365219
AB
1489}
1490
1da177e4 1491/*
ef4f527c
KC
1492 * Calls to uart_hangup() are serialised by the tty_lock in
1493 * drivers/tty/tty_io.c:do_tty_hangup()
1494 * This runs from a workqueue and can sleep for a _short_ time only.
1da177e4
LT
1495 */
1496static void uart_hangup(struct tty_struct *tty)
1497{
1498 struct uart_state *state = tty->driver_data;
46d57a44 1499 struct tty_port *port = &state->port;
61cd8a21 1500 unsigned long flags;
1da177e4 1501
39b3d892 1502 pr_debug("uart_hangup(%d)\n", tty->index);
1da177e4 1503
a2bceae0 1504 mutex_lock(&port->mutex);
ccce6deb 1505 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1da177e4 1506 uart_flush_buffer(tty);
19225135 1507 uart_shutdown(tty, state);
61cd8a21 1508 spin_lock_irqsave(&port->lock, flags);
91312cdb 1509 port->count = 0;
ccce6deb 1510 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
61cd8a21 1511 spin_unlock_irqrestore(&port->lock, flags);
7b01478f 1512 tty_port_tty_set(port, NULL);
bf903c0c
GU
1513 if (!uart_console(state->uart_port))
1514 uart_change_pm(state, UART_PM_STATE_OFF);
46d57a44 1515 wake_up_interruptible(&port->open_wait);
bdc04e31 1516 wake_up_interruptible(&port->delta_msr_wait);
1da177e4 1517 }
a2bceae0 1518 mutex_unlock(&port->mutex);
1da177e4
LT
1519}
1520
0b1db830
JS
1521static void uart_port_shutdown(struct tty_port *port)
1522{
b922e19d
JS
1523 struct uart_state *state = container_of(port, struct uart_state, port);
1524 struct uart_port *uport = state->uart_port;
1525
1526 /*
1527 * clear delta_msr_wait queue to avoid mem leaks: we may free
1528 * the irq here so the queue might never be woken up. Note
1529 * that we won't end up waiting on delta_msr_wait again since
1530 * any outstanding file descriptors should be pointing at
1531 * hung_up_tty_fops now.
1532 */
1533 wake_up_interruptible(&port->delta_msr_wait);
1534
1535 /*
1536 * Free the IRQ and disable the port.
1537 */
1538 uport->ops->shutdown(uport);
1539
1540 /*
1541 * Ensure that the IRQ handler isn't running on another CPU.
1542 */
1543 synchronize_irq(uport->irq);
0b1db830
JS
1544}
1545
de0c8cb3
AC
1546static int uart_carrier_raised(struct tty_port *port)
1547{
1548 struct uart_state *state = container_of(port, struct uart_state, port);
1549 struct uart_port *uport = state->uart_port;
1550 int mctrl;
de0c8cb3 1551 spin_lock_irq(&uport->lock);
1fdc3106 1552 uart_enable_ms(uport);
de0c8cb3
AC
1553 mctrl = uport->ops->get_mctrl(uport);
1554 spin_unlock_irq(&uport->lock);
de0c8cb3
AC
1555 if (mctrl & TIOCM_CAR)
1556 return 1;
1557 return 0;
1558}
1559
1560static void uart_dtr_rts(struct tty_port *port, int onoff)
1561{
1562 struct uart_state *state = container_of(port, struct uart_state, port);
1563 struct uart_port *uport = state->uart_port;
24fcc7c8 1564
6f5c24ad 1565 if (onoff)
de0c8cb3
AC
1566 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1567 else
1568 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
de0c8cb3
AC
1569}
1570
1da177e4 1571/*
ef4f527c
KC
1572 * Calls to uart_open are serialised by the tty_lock in
1573 * drivers/tty/tty_io.c:tty_open()
1da177e4
LT
1574 * Note that if this fails, then uart_close() _will_ be called.
1575 *
1576 * In time, we want to scrap the "opening nonpresent ports"
1577 * behaviour and implement an alternative way for setserial
1578 * to set base addresses/ports/types. This will allow us to
1579 * get rid of a certain amount of extra tests.
1580 */
1581static int uart_open(struct tty_struct *tty, struct file *filp)
1582{
1583 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1da177e4 1584 int retval, line = tty->index;
1c7b13c4
JS
1585 struct uart_state *state = drv->state + line;
1586 struct tty_port *port = &state->port;
1da177e4 1587
eb3a1e11 1588 pr_debug("uart_open(%d) called\n", line);
1da177e4 1589
91b32f54
PH
1590 spin_lock_irq(&port->lock);
1591 ++port->count;
1592 spin_unlock_irq(&port->lock);
1593
1da177e4 1594 /*
1c7b13c4
JS
1595 * We take the semaphore here to guarantee that we won't be re-entered
1596 * while allocating the state structure, or while we request any IRQs
1597 * that the driver may need. This also has the nice side-effect that
1598 * it delays the action of uart_hangup, so we can guarantee that
1599 * state->port.tty will always contain something reasonable.
1da177e4 1600 */
1c7b13c4
JS
1601 if (mutex_lock_interruptible(&port->mutex)) {
1602 retval = -ERESTARTSYS;
1603 goto end;
1604 }
1605
1c7b13c4
JS
1606 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1607 retval = -ENXIO;
91b32f54 1608 goto err_unlock;
1da177e4
LT
1609 }
1610
1da177e4 1611 tty->driver_data = state;
ebd2c8f6 1612 state->uart_port->state = state;
d6c53c0e
JS
1613 state->port.low_latency =
1614 (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
7b01478f 1615 tty_port_tty_set(port, tty);
1da177e4 1616
1da177e4
LT
1617 /*
1618 * Start up the serial port.
1619 */
19225135 1620 retval = uart_startup(tty, state, 0);
1da177e4
LT
1621
1622 /*
1623 * If we succeeded, wait until the port is ready.
1624 */
61cd8a21 1625 mutex_unlock(&port->mutex);
1da177e4 1626 if (retval == 0)
74c21077 1627 retval = tty_port_block_til_ready(port, tty, filp);
1da177e4 1628
1c7b13c4 1629end:
1da177e4 1630 return retval;
91b32f54 1631err_unlock:
1c7b13c4
JS
1632 mutex_unlock(&port->mutex);
1633 goto end;
1da177e4
LT
1634}
1635
1636static const char *uart_type(struct uart_port *port)
1637{
1638 const char *str = NULL;
1639
1640 if (port->ops->type)
1641 str = port->ops->type(port);
1642
1643 if (!str)
1644 str = "unknown";
1645
1646 return str;
1647}
1648
1649#ifdef CONFIG_PROC_FS
1650
d196a949 1651static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1da177e4
LT
1652{
1653 struct uart_state *state = drv->state + i;
a2bceae0 1654 struct tty_port *port = &state->port;
6f538fe3 1655 enum uart_pm_state pm_state;
a2bceae0 1656 struct uart_port *uport = state->uart_port;
1da177e4
LT
1657 char stat_buf[32];
1658 unsigned int status;
d196a949 1659 int mmio;
1da177e4 1660
a2bceae0 1661 if (!uport)
d196a949 1662 return;
1da177e4 1663
a2bceae0 1664 mmio = uport->iotype >= UPIO_MEM;
d196a949 1665 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
a2bceae0 1666 uport->line, uart_type(uport),
6c6a2334 1667 mmio ? "mmio:0x" : "port:",
a2bceae0
AC
1668 mmio ? (unsigned long long)uport->mapbase
1669 : (unsigned long long)uport->iobase,
1670 uport->irq);
1da177e4 1671
a2bceae0 1672 if (uport->type == PORT_UNKNOWN) {
d196a949
AD
1673 seq_putc(m, '\n');
1674 return;
1da177e4
LT
1675 }
1676
a46c9994 1677 if (capable(CAP_SYS_ADMIN)) {
a2bceae0 1678 mutex_lock(&port->mutex);
3689a0ec 1679 pm_state = state->pm_state;
6f538fe3
LW
1680 if (pm_state != UART_PM_STATE_ON)
1681 uart_change_pm(state, UART_PM_STATE_ON);
a2bceae0
AC
1682 spin_lock_irq(&uport->lock);
1683 status = uport->ops->get_mctrl(uport);
1684 spin_unlock_irq(&uport->lock);
6f538fe3 1685 if (pm_state != UART_PM_STATE_ON)
3689a0ec 1686 uart_change_pm(state, pm_state);
a2bceae0 1687 mutex_unlock(&port->mutex);
1da177e4 1688
d196a949 1689 seq_printf(m, " tx:%d rx:%d",
a2bceae0
AC
1690 uport->icount.tx, uport->icount.rx);
1691 if (uport->icount.frame)
d196a949 1692 seq_printf(m, " fe:%d",
a2bceae0
AC
1693 uport->icount.frame);
1694 if (uport->icount.parity)
d196a949 1695 seq_printf(m, " pe:%d",
a2bceae0
AC
1696 uport->icount.parity);
1697 if (uport->icount.brk)
d196a949 1698 seq_printf(m, " brk:%d",
a2bceae0
AC
1699 uport->icount.brk);
1700 if (uport->icount.overrun)
d196a949 1701 seq_printf(m, " oe:%d",
a2bceae0 1702 uport->icount.overrun);
a46c9994
AC
1703
1704#define INFOBIT(bit, str) \
a2bceae0 1705 if (uport->mctrl & (bit)) \
1da177e4
LT
1706 strncat(stat_buf, (str), sizeof(stat_buf) - \
1707 strlen(stat_buf) - 2)
a46c9994 1708#define STATBIT(bit, str) \
1da177e4
LT
1709 if (status & (bit)) \
1710 strncat(stat_buf, (str), sizeof(stat_buf) - \
1711 strlen(stat_buf) - 2)
1712
1713 stat_buf[0] = '\0';
1714 stat_buf[1] = '\0';
1715 INFOBIT(TIOCM_RTS, "|RTS");
1716 STATBIT(TIOCM_CTS, "|CTS");
1717 INFOBIT(TIOCM_DTR, "|DTR");
1718 STATBIT(TIOCM_DSR, "|DSR");
1719 STATBIT(TIOCM_CAR, "|CD");
1720 STATBIT(TIOCM_RNG, "|RI");
1721 if (stat_buf[0])
1722 stat_buf[0] = ' ';
a46c9994 1723
d196a949 1724 seq_puts(m, stat_buf);
1da177e4 1725 }
d196a949 1726 seq_putc(m, '\n');
1da177e4
LT
1727#undef STATBIT
1728#undef INFOBIT
1da177e4
LT
1729}
1730
d196a949 1731static int uart_proc_show(struct seq_file *m, void *v)
1da177e4 1732{
833bb304 1733 struct tty_driver *ttydrv = m->private;
1da177e4 1734 struct uart_driver *drv = ttydrv->driver_state;
d196a949 1735 int i;
1da177e4 1736
d196a949 1737 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1da177e4 1738 "", "", "");
d196a949
AD
1739 for (i = 0; i < drv->nr; i++)
1740 uart_line_info(m, drv, i);
1741 return 0;
1da177e4 1742}
d196a949
AD
1743
1744static int uart_proc_open(struct inode *inode, struct file *file)
1745{
d9dda78b 1746 return single_open(file, uart_proc_show, PDE_DATA(inode));
d196a949
AD
1747}
1748
1749static const struct file_operations uart_proc_fops = {
1750 .owner = THIS_MODULE,
1751 .open = uart_proc_open,
1752 .read = seq_read,
1753 .llseek = seq_lseek,
1754 .release = single_release,
1755};
1da177e4
LT
1756#endif
1757
4a1b5502 1758#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1cfe42b7 1759/**
d358788f
RK
1760 * uart_console_write - write a console message to a serial port
1761 * @port: the port to write the message
1762 * @s: array of characters
1763 * @count: number of characters in string to write
10afbe34 1764 * @putchar: function to write character to port
d358788f
RK
1765 */
1766void uart_console_write(struct uart_port *port, const char *s,
1767 unsigned int count,
1768 void (*putchar)(struct uart_port *, int))
1769{
1770 unsigned int i;
1771
1772 for (i = 0; i < count; i++, s++) {
1773 if (*s == '\n')
1774 putchar(port, '\r');
1775 putchar(port, *s);
1776 }
1777}
1778EXPORT_SYMBOL_GPL(uart_console_write);
1779
1da177e4
LT
1780/*
1781 * Check whether an invalid uart number has been specified, and
1782 * if so, search for the first available port that does have
1783 * console support.
1784 */
1785struct uart_port * __init
1786uart_get_console(struct uart_port *ports, int nr, struct console *co)
1787{
1788 int idx = co->index;
1789
1790 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1791 ports[idx].membase == NULL))
1792 for (idx = 0; idx < nr; idx++)
1793 if (ports[idx].iobase != 0 ||
1794 ports[idx].membase != NULL)
1795 break;
1796
1797 co->index = idx;
1798
1799 return ports + idx;
1800}
1801
73abaf87
PH
1802/**
1803 * uart_parse_earlycon - Parse earlycon options
1804 * @p: ptr to 2nd field (ie., just beyond '<name>,')
1805 * @iotype: ptr for decoded iotype (out)
1806 * @addr: ptr for decoded mapbase/iobase (out)
1807 * @options: ptr for <options> field; NULL if not present (out)
1808 *
1809 * Decodes earlycon kernel command line parameters of the form
bd94c407
MY
1810 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
1811 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
73abaf87
PH
1812 *
1813 * The optional form
1814 * earlycon=<name>,0x<addr>,<options>
1815 * console=<name>,0x<addr>,<options>
1816 * is also accepted; the returned @iotype will be UPIO_MEM.
1817 *
1818 * Returns 0 on success or -EINVAL on failure
1819 */
1820int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
1821 char **options)
1822{
1823 if (strncmp(p, "mmio,", 5) == 0) {
1824 *iotype = UPIO_MEM;
1825 p += 5;
bd94c407
MY
1826 } else if (strncmp(p, "mmio16,", 7) == 0) {
1827 *iotype = UPIO_MEM16;
1828 p += 7;
73abaf87
PH
1829 } else if (strncmp(p, "mmio32,", 7) == 0) {
1830 *iotype = UPIO_MEM32;
1831 p += 7;
6e63be3f
NC
1832 } else if (strncmp(p, "mmio32be,", 9) == 0) {
1833 *iotype = UPIO_MEM32BE;
1834 p += 9;
d215d809
MF
1835 } else if (strncmp(p, "mmio32native,", 13) == 0) {
1836 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
1837 UPIO_MEM32BE : UPIO_MEM32;
1838 p += 13;
73abaf87
PH
1839 } else if (strncmp(p, "io,", 3) == 0) {
1840 *iotype = UPIO_PORT;
1841 p += 3;
1842 } else if (strncmp(p, "0x", 2) == 0) {
1843 *iotype = UPIO_MEM;
1844 } else {
1845 return -EINVAL;
1846 }
1847
1848 *addr = simple_strtoul(p, NULL, 0);
1849 p = strchr(p, ',');
1850 if (p)
1851 p++;
1852
1853 *options = p;
1854 return 0;
1855}
1856EXPORT_SYMBOL_GPL(uart_parse_earlycon);
1857
1da177e4 1858/**
02088ca6 1859 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
1da177e4
LT
1860 * @options: pointer to option string
1861 * @baud: pointer to an 'int' variable for the baud rate.
1862 * @parity: pointer to an 'int' variable for the parity.
1863 * @bits: pointer to an 'int' variable for the number of data bits.
1864 * @flow: pointer to an 'int' variable for the flow control character.
1865 *
1866 * uart_parse_options decodes a string containing the serial console
1867 * options. The format of the string is <baud><parity><bits><flow>,
1868 * eg: 115200n8r
1869 */
f2d937f3 1870void
1da177e4
LT
1871uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1872{
1873 char *s = options;
1874
1875 *baud = simple_strtoul(s, NULL, 10);
1876 while (*s >= '0' && *s <= '9')
1877 s++;
1878 if (*s)
1879 *parity = *s++;
1880 if (*s)
1881 *bits = *s++ - '0';
1882 if (*s)
1883 *flow = *s;
1884}
f2d937f3 1885EXPORT_SYMBOL_GPL(uart_parse_options);
1da177e4
LT
1886
1887struct baud_rates {
1888 unsigned int rate;
1889 unsigned int cflag;
1890};
1891
cb3592be 1892static const struct baud_rates baud_rates[] = {
1da177e4
LT
1893 { 921600, B921600 },
1894 { 460800, B460800 },
1895 { 230400, B230400 },
1896 { 115200, B115200 },
1897 { 57600, B57600 },
1898 { 38400, B38400 },
1899 { 19200, B19200 },
1900 { 9600, B9600 },
1901 { 4800, B4800 },
1902 { 2400, B2400 },
1903 { 1200, B1200 },
1904 { 0, B38400 }
1905};
1906
1907/**
1908 * uart_set_options - setup the serial console parameters
1909 * @port: pointer to the serial ports uart_port structure
1910 * @co: console pointer
1911 * @baud: baud rate
1912 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1913 * @bits: number of data bits
1914 * @flow: flow control character - 'r' (rts)
1915 */
f2d937f3 1916int
1da177e4
LT
1917uart_set_options(struct uart_port *port, struct console *co,
1918 int baud, int parity, int bits, int flow)
1919{
606d099c 1920 struct ktermios termios;
149b36ea 1921 static struct ktermios dummy;
1da177e4
LT
1922 int i;
1923
976ecd12
RK
1924 /*
1925 * Ensure that the serial console lock is initialised
1926 * early.
42b6a1ba
RW
1927 * If this port is a console, then the spinlock is already
1928 * initialised.
976ecd12 1929 */
42b6a1ba
RW
1930 if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
1931 spin_lock_init(&port->lock);
1932 lockdep_set_class(&port->lock, &port_lock_key);
1933 }
976ecd12 1934
606d099c 1935 memset(&termios, 0, sizeof(struct ktermios));
1da177e4
LT
1936
1937 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1938
1939 /*
1940 * Construct a cflag setting.
1941 */
1942 for (i = 0; baud_rates[i].rate; i++)
1943 if (baud_rates[i].rate <= baud)
1944 break;
1945
1946 termios.c_cflag |= baud_rates[i].cflag;
1947
1948 if (bits == 7)
1949 termios.c_cflag |= CS7;
1950 else
1951 termios.c_cflag |= CS8;
1952
1953 switch (parity) {
1954 case 'o': case 'O':
1955 termios.c_cflag |= PARODD;
1956 /*fall through*/
1957 case 'e': case 'E':
1958 termios.c_cflag |= PARENB;
1959 break;
1960 }
1961
1962 if (flow == 'r')
1963 termios.c_cflag |= CRTSCTS;
1964
79492689
YL
1965 /*
1966 * some uarts on other side don't support no flow control.
1967 * So we set * DTR in host uart to make them happy
1968 */
1969 port->mctrl |= TIOCM_DTR;
1970
149b36ea 1971 port->ops->set_termios(port, &termios, &dummy);
f2d937f3
JW
1972 /*
1973 * Allow the setting of the UART parameters with a NULL console
1974 * too:
1975 */
1976 if (co)
1977 co->cflag = termios.c_cflag;
1da177e4
LT
1978
1979 return 0;
1980}
f2d937f3 1981EXPORT_SYMBOL_GPL(uart_set_options);
1da177e4
LT
1982#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1983
cf75525f
JS
1984/**
1985 * uart_change_pm - set power state of the port
1986 *
1987 * @state: port descriptor
1988 * @pm_state: new state
1989 *
1990 * Locking: port->mutex has to be held
1991 */
6f538fe3
LW
1992static void uart_change_pm(struct uart_state *state,
1993 enum uart_pm_state pm_state)
1da177e4 1994{
ebd2c8f6 1995 struct uart_port *port = state->uart_port;
1281e360
AV
1996
1997 if (state->pm_state != pm_state) {
1998 if (port->ops->pm)
1999 port->ops->pm(port, pm_state, state->pm_state);
2000 state->pm_state = pm_state;
2001 }
1da177e4
LT
2002}
2003
b3b708fa
GL
2004struct uart_match {
2005 struct uart_port *port;
2006 struct uart_driver *driver;
2007};
2008
2009static int serial_match_port(struct device *dev, void *data)
2010{
2011 struct uart_match *match = data;
7ca796f4
GL
2012 struct tty_driver *tty_drv = match->driver->tty_driver;
2013 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2014 match->port->line;
b3b708fa
GL
2015
2016 return dev->devt == devt; /* Actually, only one tty per port */
2017}
2018
ccce6deb 2019int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1da177e4 2020{
ccce6deb
AC
2021 struct uart_state *state = drv->state + uport->line;
2022 struct tty_port *port = &state->port;
b3b708fa 2023 struct device *tty_dev;
ccce6deb 2024 struct uart_match match = {uport, drv};
1da177e4 2025
a2bceae0 2026 mutex_lock(&port->mutex);
1da177e4 2027
ccce6deb 2028 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
b3b708fa 2029 if (device_may_wakeup(tty_dev)) {
3f960dbb
G
2030 if (!enable_irq_wake(uport->irq))
2031 uport->irq_wake = 1;
b3b708fa 2032 put_device(tty_dev);
a2bceae0 2033 mutex_unlock(&port->mutex);
b3b708fa
GL
2034 return 0;
2035 }
5a65dcc0
FV
2036 put_device(tty_dev);
2037
b164c972
PH
2038 /* Nothing to do if the console is not suspending */
2039 if (!console_suspend_enabled && uart_console(uport))
2040 goto unlock;
2041
2042 uport->suspended = 1;
b3b708fa 2043
ccce6deb
AC
2044 if (port->flags & ASYNC_INITIALIZED) {
2045 const struct uart_ops *ops = uport->ops;
c8c6bfa3 2046 int tries;
1da177e4 2047
b164c972
PH
2048 set_bit(ASYNCB_SUSPENDED, &port->flags);
2049 clear_bit(ASYNCB_INITIALIZED, &port->flags);
a6b93a90 2050
b164c972
PH
2051 spin_lock_irq(&uport->lock);
2052 ops->stop_tx(uport);
2053 ops->set_mctrl(uport, 0);
2054 ops->stop_rx(uport);
2055 spin_unlock_irq(&uport->lock);
1da177e4
LT
2056
2057 /*
2058 * Wait for the transmitter to empty.
2059 */
ccce6deb 2060 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1da177e4 2061 msleep(10);
c8c6bfa3 2062 if (!tries)
2f2dafe7
SM
2063 dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
2064 drv->dev_name,
2065 drv->tty_driver->name_base + uport->line);
1da177e4 2066
b164c972 2067 ops->shutdown(uport);
1da177e4
LT
2068 }
2069
2070 /*
2071 * Disable the console device before suspending.
2072 */
b164c972 2073 if (uart_console(uport))
ccce6deb 2074 console_stop(uport->cons);
1da177e4 2075
b164c972
PH
2076 uart_change_pm(state, UART_PM_STATE_OFF);
2077unlock:
a2bceae0 2078 mutex_unlock(&port->mutex);
1da177e4
LT
2079
2080 return 0;
2081}
2082
ccce6deb 2083int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1da177e4 2084{
ccce6deb
AC
2085 struct uart_state *state = drv->state + uport->line;
2086 struct tty_port *port = &state->port;
03a74dcc 2087 struct device *tty_dev;
ccce6deb 2088 struct uart_match match = {uport, drv};
ba15ab0e 2089 struct ktermios termios;
1da177e4 2090
a2bceae0 2091 mutex_lock(&port->mutex);
1da177e4 2092
ccce6deb
AC
2093 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2094 if (!uport->suspended && device_may_wakeup(tty_dev)) {
3f960dbb
G
2095 if (uport->irq_wake) {
2096 disable_irq_wake(uport->irq);
2097 uport->irq_wake = 0;
2098 }
5a65dcc0 2099 put_device(tty_dev);
a2bceae0 2100 mutex_unlock(&port->mutex);
b3b708fa
GL
2101 return 0;
2102 }
5a65dcc0 2103 put_device(tty_dev);
ccce6deb 2104 uport->suspended = 0;
b3b708fa 2105
1da177e4
LT
2106 /*
2107 * Re-enable the console device after suspending.
2108 */
5933a161 2109 if (uart_console(uport)) {
891b9dd1
JW
2110 /*
2111 * First try to use the console cflag setting.
2112 */
2113 memset(&termios, 0, sizeof(struct ktermios));
2114 termios.c_cflag = uport->cons->cflag;
2115
2116 /*
2117 * If that's unset, use the tty termios setting.
2118 */
adc8d746
AC
2119 if (port->tty && termios.c_cflag == 0)
2120 termios = port->tty->termios;
891b9dd1 2121
94abc56f 2122 if (console_suspend_enabled)
6f538fe3 2123 uart_change_pm(state, UART_PM_STATE_ON);
ccce6deb 2124 uport->ops->set_termios(uport, &termios, NULL);
5933a161
YK
2125 if (console_suspend_enabled)
2126 console_start(uport->cons);
1da177e4
LT
2127 }
2128
ccce6deb
AC
2129 if (port->flags & ASYNC_SUSPENDED) {
2130 const struct uart_ops *ops = uport->ops;
ee31b337 2131 int ret;
1da177e4 2132
6f538fe3 2133 uart_change_pm(state, UART_PM_STATE_ON);
ccce6deb
AC
2134 spin_lock_irq(&uport->lock);
2135 ops->set_mctrl(uport, 0);
2136 spin_unlock_irq(&uport->lock);
4547be78 2137 if (console_suspend_enabled || !uart_console(uport)) {
19225135
AC
2138 /* Protected by port mutex for now */
2139 struct tty_struct *tty = port->tty;
4547be78
SB
2140 ret = ops->startup(uport);
2141 if (ret == 0) {
19225135
AC
2142 if (tty)
2143 uart_change_speed(tty, state, NULL);
4547be78
SB
2144 spin_lock_irq(&uport->lock);
2145 ops->set_mctrl(uport, uport->mctrl);
2146 ops->start_tx(uport);
2147 spin_unlock_irq(&uport->lock);
2148 set_bit(ASYNCB_INITIALIZED, &port->flags);
2149 } else {
2150 /*
2151 * Failed to resume - maybe hardware went away?
2152 * Clear the "initialized" flag so we won't try
2153 * to call the low level drivers shutdown method.
2154 */
19225135 2155 uart_shutdown(tty, state);
4547be78 2156 }
ee31b337 2157 }
a6b93a90 2158
ccce6deb 2159 clear_bit(ASYNCB_SUSPENDED, &port->flags);
1da177e4
LT
2160 }
2161
a2bceae0 2162 mutex_unlock(&port->mutex);
1da177e4
LT
2163
2164 return 0;
2165}
2166
2167static inline void
2168uart_report_port(struct uart_driver *drv, struct uart_port *port)
2169{
30b7a3bc
RK
2170 char address[64];
2171
1da177e4
LT
2172 switch (port->iotype) {
2173 case UPIO_PORT:
9bde10a4 2174 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
1da177e4
LT
2175 break;
2176 case UPIO_HUB6:
30b7a3bc 2177 snprintf(address, sizeof(address),
9bde10a4 2178 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
1da177e4
LT
2179 break;
2180 case UPIO_MEM:
bd94c407 2181 case UPIO_MEM16:
1da177e4 2182 case UPIO_MEM32:
3ffb1a81 2183 case UPIO_MEM32BE:
21c614a7 2184 case UPIO_AU:
3be91ec7 2185 case UPIO_TSI:
30b7a3bc 2186 snprintf(address, sizeof(address),
4f640efb 2187 "MMIO 0x%llx", (unsigned long long)port->mapbase);
30b7a3bc
RK
2188 break;
2189 default:
2190 strlcpy(address, "*unknown*", sizeof(address));
1da177e4
LT
2191 break;
2192 }
30b7a3bc 2193
68ed7e1c
JB
2194 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2195 port->dev ? dev_name(port->dev) : "",
2196 port->dev ? ": " : "",
8440838b
DM
2197 drv->dev_name,
2198 drv->tty_driver->name_base + port->line,
7d12b976 2199 address, port->irq, port->uartclk / 16, uart_type(port));
1da177e4
LT
2200}
2201
2202static void
2203uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2204 struct uart_port *port)
2205{
2206 unsigned int flags;
2207
2208 /*
2209 * If there isn't a port here, don't do anything further.
2210 */
2211 if (!port->iobase && !port->mapbase && !port->membase)
2212 return;
2213
2214 /*
2215 * Now do the auto configuration stuff. Note that config_port
2216 * is expected to claim the resources and map the port for us.
2217 */
8e23fcc8 2218 flags = 0;
1da177e4
LT
2219 if (port->flags & UPF_AUTO_IRQ)
2220 flags |= UART_CONFIG_IRQ;
2221 if (port->flags & UPF_BOOT_AUTOCONF) {
8e23fcc8
DD
2222 if (!(port->flags & UPF_FIXED_TYPE)) {
2223 port->type = PORT_UNKNOWN;
2224 flags |= UART_CONFIG_TYPE;
2225 }
1da177e4
LT
2226 port->ops->config_port(port, flags);
2227 }
2228
2229 if (port->type != PORT_UNKNOWN) {
2230 unsigned long flags;
2231
2232 uart_report_port(drv, port);
2233
3689a0ec 2234 /* Power up port for set_mctrl() */
6f538fe3 2235 uart_change_pm(state, UART_PM_STATE_ON);
3689a0ec 2236
1da177e4
LT
2237 /*
2238 * Ensure that the modem control lines are de-activated.
c3e4642b 2239 * keep the DTR setting that is set in uart_set_options()
1da177e4
LT
2240 * We probably don't need a spinlock around this, but
2241 */
2242 spin_lock_irqsave(&port->lock, flags);
c3e4642b 2243 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
1da177e4
LT
2244 spin_unlock_irqrestore(&port->lock, flags);
2245
97d97224
RK
2246 /*
2247 * If this driver supports console, and it hasn't been
2248 * successfully registered yet, try to re-register it.
2249 * It may be that the port was not available.
2250 */
2251 if (port->cons && !(port->cons->flags & CON_ENABLED))
2252 register_console(port->cons);
2253
1da177e4
LT
2254 /*
2255 * Power down all ports by default, except the
2256 * console if we have one.
2257 */
2258 if (!uart_console(port))
6f538fe3 2259 uart_change_pm(state, UART_PM_STATE_OFF);
1da177e4
LT
2260 }
2261}
2262
f2d937f3
JW
2263#ifdef CONFIG_CONSOLE_POLL
2264
2265static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2266{
2267 struct uart_driver *drv = driver->driver_state;
2268 struct uart_state *state = drv->state + line;
2269 struct uart_port *port;
2270 int baud = 9600;
2271 int bits = 8;
2272 int parity = 'n';
2273 int flow = 'n';
c7f3e708 2274 int ret;
f2d937f3 2275
ebd2c8f6 2276 if (!state || !state->uart_port)
f2d937f3
JW
2277 return -1;
2278
ebd2c8f6 2279 port = state->uart_port;
f2d937f3
JW
2280 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2281 return -1;
2282
c7f3e708
AV
2283 if (port->ops->poll_init) {
2284 struct tty_port *tport = &state->port;
2285
2286 ret = 0;
2287 mutex_lock(&tport->mutex);
2288 /*
2289 * We don't set ASYNCB_INITIALIZED as we only initialized the
2290 * hw, e.g. state->xmit is still uninitialized.
2291 */
2292 if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
2293 ret = port->ops->poll_init(port);
2294 mutex_unlock(&tport->mutex);
2295 if (ret)
2296 return ret;
2297 }
2298
f2d937f3
JW
2299 if (options) {
2300 uart_parse_options(options, &baud, &parity, &bits, &flow);
2301 return uart_set_options(port, NULL, baud, parity, bits, flow);
2302 }
2303
2304 return 0;
2305}
2306
2307static int uart_poll_get_char(struct tty_driver *driver, int line)
2308{
2309 struct uart_driver *drv = driver->driver_state;
2310 struct uart_state *state = drv->state + line;
2311 struct uart_port *port;
2312
ebd2c8f6 2313 if (!state || !state->uart_port)
f2d937f3
JW
2314 return -1;
2315
ebd2c8f6 2316 port = state->uart_port;
f2d937f3
JW
2317 return port->ops->poll_get_char(port);
2318}
2319
2320static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2321{
2322 struct uart_driver *drv = driver->driver_state;
2323 struct uart_state *state = drv->state + line;
2324 struct uart_port *port;
2325
ebd2c8f6 2326 if (!state || !state->uart_port)
f2d937f3
JW
2327 return;
2328
ebd2c8f6 2329 port = state->uart_port;
c7d44a02
DA
2330
2331 if (ch == '\n')
2332 port->ops->poll_put_char(port, '\r');
f2d937f3
JW
2333 port->ops->poll_put_char(port, ch);
2334}
2335#endif
2336
b68e31d0 2337static const struct tty_operations uart_ops = {
1da177e4
LT
2338 .open = uart_open,
2339 .close = uart_close,
2340 .write = uart_write,
2341 .put_char = uart_put_char,
2342 .flush_chars = uart_flush_chars,
2343 .write_room = uart_write_room,
2344 .chars_in_buffer= uart_chars_in_buffer,
2345 .flush_buffer = uart_flush_buffer,
2346 .ioctl = uart_ioctl,
2347 .throttle = uart_throttle,
2348 .unthrottle = uart_unthrottle,
2349 .send_xchar = uart_send_xchar,
2350 .set_termios = uart_set_termios,
64e9159f 2351 .set_ldisc = uart_set_ldisc,
1da177e4
LT
2352 .stop = uart_stop,
2353 .start = uart_start,
2354 .hangup = uart_hangup,
2355 .break_ctl = uart_break_ctl,
2356 .wait_until_sent= uart_wait_until_sent,
2357#ifdef CONFIG_PROC_FS
d196a949 2358 .proc_fops = &uart_proc_fops,
1da177e4
LT
2359#endif
2360 .tiocmget = uart_tiocmget,
2361 .tiocmset = uart_tiocmset,
d281da7f 2362 .get_icount = uart_get_icount,
f2d937f3
JW
2363#ifdef CONFIG_CONSOLE_POLL
2364 .poll_init = uart_poll_init,
2365 .poll_get_char = uart_poll_get_char,
2366 .poll_put_char = uart_poll_put_char,
2367#endif
1da177e4
LT
2368};
2369
de0c8cb3
AC
2370static const struct tty_port_operations uart_port_ops = {
2371 .carrier_raised = uart_carrier_raised,
2372 .dtr_rts = uart_dtr_rts,
2373};
2374
1da177e4
LT
2375/**
2376 * uart_register_driver - register a driver with the uart core layer
2377 * @drv: low level driver structure
2378 *
2379 * Register a uart driver with the core driver. We in turn register
2380 * with the tty layer, and initialise the core driver per-port state.
2381 *
2382 * We have a proc file in /proc/tty/driver which is named after the
2383 * normal driver.
2384 *
2385 * drv->port should be NULL, and the per-port structures should be
2386 * registered using uart_add_one_port after this call has succeeded.
2387 */
2388int uart_register_driver(struct uart_driver *drv)
2389{
9e845abf 2390 struct tty_driver *normal;
1da177e4
LT
2391 int i, retval;
2392
2393 BUG_ON(drv->state);
2394
2395 /*
2396 * Maybe we should be using a slab cache for this, especially if
2397 * we have a large number of ports to handle.
2398 */
8f31bb39 2399 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
1da177e4
LT
2400 if (!drv->state)
2401 goto out;
2402
9e845abf 2403 normal = alloc_tty_driver(drv->nr);
1da177e4 2404 if (!normal)
9e845abf 2405 goto out_kfree;
1da177e4
LT
2406
2407 drv->tty_driver = normal;
2408
1da177e4 2409 normal->driver_name = drv->driver_name;
1da177e4
LT
2410 normal->name = drv->dev_name;
2411 normal->major = drv->major;
2412 normal->minor_start = drv->minor;
2413 normal->type = TTY_DRIVER_TYPE_SERIAL;
2414 normal->subtype = SERIAL_TYPE_NORMAL;
2415 normal->init_termios = tty_std_termios;
2416 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
606d099c 2417 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
331b8319 2418 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1da177e4
LT
2419 normal->driver_state = drv;
2420 tty_set_operations(normal, &uart_ops);
2421
2422 /*
2423 * Initialise the UART state(s).
2424 */
2425 for (i = 0; i < drv->nr; i++) {
2426 struct uart_state *state = drv->state + i;
a2bceae0 2427 struct tty_port *port = &state->port;
1da177e4 2428
a2bceae0 2429 tty_port_init(port);
de0c8cb3 2430 port->ops = &uart_port_ops;
1da177e4
LT
2431 }
2432
2433 retval = tty_register_driver(normal);
9e845abf
AGR
2434 if (retval >= 0)
2435 return retval;
2436
191c5f10
JS
2437 for (i = 0; i < drv->nr; i++)
2438 tty_port_destroy(&drv->state[i].port);
9e845abf
AGR
2439 put_tty_driver(normal);
2440out_kfree:
2441 kfree(drv->state);
2442out:
2443 return -ENOMEM;
1da177e4
LT
2444}
2445
2446/**
2447 * uart_unregister_driver - remove a driver from the uart core layer
2448 * @drv: low level driver structure
2449 *
2450 * Remove all references to a driver from the core driver. The low
2451 * level driver must have removed all its ports via the
2452 * uart_remove_one_port() if it registered them with uart_add_one_port().
2453 * (ie, drv->port == NULL)
2454 */
2455void uart_unregister_driver(struct uart_driver *drv)
2456{
2457 struct tty_driver *p = drv->tty_driver;
191c5f10
JS
2458 unsigned int i;
2459
1da177e4
LT
2460 tty_unregister_driver(p);
2461 put_tty_driver(p);
191c5f10
JS
2462 for (i = 0; i < drv->nr; i++)
2463 tty_port_destroy(&drv->state[i].port);
1da177e4 2464 kfree(drv->state);
1e66cded 2465 drv->state = NULL;
1da177e4
LT
2466 drv->tty_driver = NULL;
2467}
2468
2469struct tty_driver *uart_console_device(struct console *co, int *index)
2470{
2471 struct uart_driver *p = co->data;
2472 *index = co->index;
2473 return p->tty_driver;
2474}
2475
6915c0e4
TH
2476static ssize_t uart_get_attr_uartclk(struct device *dev,
2477 struct device_attribute *attr, char *buf)
2478{
bebe73e3 2479 struct serial_struct tmp;
6915c0e4 2480 struct tty_port *port = dev_get_drvdata(dev);
6915c0e4 2481
9f109694 2482 uart_get_info(port, &tmp);
bebe73e3 2483 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
6915c0e4
TH
2484}
2485
373bac4c
AC
2486static ssize_t uart_get_attr_type(struct device *dev,
2487 struct device_attribute *attr, char *buf)
2488{
2489 struct serial_struct tmp;
2490 struct tty_port *port = dev_get_drvdata(dev);
2491
2492 uart_get_info(port, &tmp);
2493 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2494}
2495static ssize_t uart_get_attr_line(struct device *dev,
2496 struct device_attribute *attr, char *buf)
2497{
2498 struct serial_struct tmp;
2499 struct tty_port *port = dev_get_drvdata(dev);
2500
2501 uart_get_info(port, &tmp);
2502 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2503}
2504
2505static ssize_t uart_get_attr_port(struct device *dev,
2506 struct device_attribute *attr, char *buf)
2507{
2508 struct serial_struct tmp;
2509 struct tty_port *port = dev_get_drvdata(dev);
fd985e1d 2510 unsigned long ioaddr;
373bac4c
AC
2511
2512 uart_get_info(port, &tmp);
fd985e1d
AM
2513 ioaddr = tmp.port;
2514 if (HIGH_BITS_OFFSET)
2515 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2516 return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
373bac4c
AC
2517}
2518
2519static ssize_t uart_get_attr_irq(struct device *dev,
2520 struct device_attribute *attr, char *buf)
2521{
2522 struct serial_struct tmp;
2523 struct tty_port *port = dev_get_drvdata(dev);
2524
2525 uart_get_info(port, &tmp);
2526 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2527}
2528
2529static ssize_t uart_get_attr_flags(struct device *dev,
2530 struct device_attribute *attr, char *buf)
2531{
2532 struct serial_struct tmp;
2533 struct tty_port *port = dev_get_drvdata(dev);
2534
2535 uart_get_info(port, &tmp);
2536 return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2537}
2538
2539static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2540 struct device_attribute *attr, char *buf)
2541{
2542 struct serial_struct tmp;
2543 struct tty_port *port = dev_get_drvdata(dev);
2544
2545 uart_get_info(port, &tmp);
2546 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2547}
2548
2549
2550static ssize_t uart_get_attr_close_delay(struct device *dev,
2551 struct device_attribute *attr, char *buf)
2552{
2553 struct serial_struct tmp;
2554 struct tty_port *port = dev_get_drvdata(dev);
2555
2556 uart_get_info(port, &tmp);
2557 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2558}
2559
2560
2561static ssize_t uart_get_attr_closing_wait(struct device *dev,
2562 struct device_attribute *attr, char *buf)
2563{
2564 struct serial_struct tmp;
2565 struct tty_port *port = dev_get_drvdata(dev);
2566
2567 uart_get_info(port, &tmp);
2568 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2569}
2570
2571static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2572 struct device_attribute *attr, char *buf)
2573{
2574 struct serial_struct tmp;
2575 struct tty_port *port = dev_get_drvdata(dev);
2576
2577 uart_get_info(port, &tmp);
2578 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2579}
2580
2581static ssize_t uart_get_attr_io_type(struct device *dev,
2582 struct device_attribute *attr, char *buf)
2583{
2584 struct serial_struct tmp;
2585 struct tty_port *port = dev_get_drvdata(dev);
2586
2587 uart_get_info(port, &tmp);
2588 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2589}
2590
2591static ssize_t uart_get_attr_iomem_base(struct device *dev,
2592 struct device_attribute *attr, char *buf)
2593{
2594 struct serial_struct tmp;
2595 struct tty_port *port = dev_get_drvdata(dev);
2596
2597 uart_get_info(port, &tmp);
2598 return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2599}
2600
2601static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2602 struct device_attribute *attr, char *buf)
2603{
2604 struct serial_struct tmp;
2605 struct tty_port *port = dev_get_drvdata(dev);
2606
2607 uart_get_info(port, &tmp);
2608 return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2609}
2610
2611static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2612static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2613static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2614static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2615static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2616static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
6915c0e4 2617static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
373bac4c
AC
2618static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2619static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2620static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2621static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2622static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2623static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
6915c0e4
TH
2624
2625static struct attribute *tty_dev_attrs[] = {
373bac4c
AC
2626 &dev_attr_type.attr,
2627 &dev_attr_line.attr,
2628 &dev_attr_port.attr,
2629 &dev_attr_irq.attr,
2630 &dev_attr_flags.attr,
2631 &dev_attr_xmit_fifo_size.attr,
6915c0e4 2632 &dev_attr_uartclk.attr,
373bac4c
AC
2633 &dev_attr_close_delay.attr,
2634 &dev_attr_closing_wait.attr,
2635 &dev_attr_custom_divisor.attr,
2636 &dev_attr_io_type.attr,
2637 &dev_attr_iomem_base.attr,
2638 &dev_attr_iomem_reg_shift.attr,
6915c0e4
TH
2639 NULL,
2640 };
2641
b1b79916 2642static const struct attribute_group tty_dev_attr_group = {
6915c0e4
TH
2643 .attrs = tty_dev_attrs,
2644 };
2645
1da177e4
LT
2646/**
2647 * uart_add_one_port - attach a driver-defined port structure
2648 * @drv: pointer to the uart low level driver structure for this port
1b9894f3 2649 * @uport: uart port structure to use for this port.
1da177e4
LT
2650 *
2651 * This allows the driver to register its own uart_port structure
2652 * with the core driver. The main purpose is to allow the low
2653 * level uart drivers to expand uart_port, rather than having yet
2654 * more levels of structures.
2655 */
a2bceae0 2656int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
1da177e4
LT
2657{
2658 struct uart_state *state;
a2bceae0 2659 struct tty_port *port;
1da177e4 2660 int ret = 0;
b3b708fa 2661 struct device *tty_dev;
266dcff0 2662 int num_groups;
1da177e4
LT
2663
2664 BUG_ON(in_interrupt());
2665
a2bceae0 2666 if (uport->line >= drv->nr)
1da177e4
LT
2667 return -EINVAL;
2668
a2bceae0
AC
2669 state = drv->state + uport->line;
2670 port = &state->port;
1da177e4 2671
f392ecfa 2672 mutex_lock(&port_mutex);
a2bceae0 2673 mutex_lock(&port->mutex);
ebd2c8f6 2674 if (state->uart_port) {
1da177e4
LT
2675 ret = -EINVAL;
2676 goto out;
2677 }
2678
2b702b9b 2679 /* Link the port to the driver state table and vice versa */
a2bceae0 2680 state->uart_port = uport;
2b702b9b 2681 uport->state = state;
1da177e4 2682
2b702b9b 2683 state->pm_state = UART_PM_STATE_UNDEFINED;
a2bceae0 2684 uport->cons = drv->cons;
959801fe 2685 uport->minor = drv->tty_driver->minor_start + uport->line;
1da177e4 2686
976ecd12
RK
2687 /*
2688 * If this port is a console, then the spinlock is already
2689 * initialised.
2690 */
a2bceae0
AC
2691 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2692 spin_lock_init(&uport->lock);
2693 lockdep_set_class(&uport->lock, &port_lock_key);
13e83599 2694 }
a208ffd2
GL
2695 if (uport->cons && uport->dev)
2696 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
976ecd12 2697
a2bceae0 2698 uart_configure_port(drv, state, uport);
1da177e4 2699
266dcff0
GKH
2700 num_groups = 2;
2701 if (uport->attr_group)
2702 num_groups++;
2703
c2b703b8 2704 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
266dcff0
GKH
2705 GFP_KERNEL);
2706 if (!uport->tty_groups) {
2707 ret = -ENOMEM;
2708 goto out;
2709 }
2710 uport->tty_groups[0] = &tty_dev_attr_group;
2711 if (uport->attr_group)
2712 uport->tty_groups[1] = uport->attr_group;
2713
1da177e4
LT
2714 /*
2715 * Register the port whether it's detected or not. This allows
015355b7 2716 * setserial to be used to alter this port's parameters.
1da177e4 2717 */
b1b79916 2718 tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
266dcff0 2719 uport->line, uport->dev, port, uport->tty_groups);
b3b708fa 2720 if (likely(!IS_ERR(tty_dev))) {
77359835
SG
2721 device_set_wakeup_capable(tty_dev, 1);
2722 } else {
2f2dafe7 2723 dev_err(uport->dev, "Cannot register tty device on line %d\n",
a2bceae0 2724 uport->line);
77359835 2725 }
1da177e4 2726
68ac64cd
RK
2727 /*
2728 * Ensure UPF_DEAD is not set.
2729 */
a2bceae0 2730 uport->flags &= ~UPF_DEAD;
68ac64cd 2731
1da177e4 2732 out:
a2bceae0 2733 mutex_unlock(&port->mutex);
f392ecfa 2734 mutex_unlock(&port_mutex);
1da177e4
LT
2735
2736 return ret;
2737}
2738
2739/**
2740 * uart_remove_one_port - detach a driver defined port structure
2741 * @drv: pointer to the uart low level driver structure for this port
1b9894f3 2742 * @uport: uart port structure for this port
1da177e4
LT
2743 *
2744 * This unhooks (and hangs up) the specified port structure from the
2745 * core driver. No further calls will be made to the low-level code
2746 * for this port.
2747 */
a2bceae0 2748int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
1da177e4 2749{
a2bceae0
AC
2750 struct uart_state *state = drv->state + uport->line;
2751 struct tty_port *port = &state->port;
4c6d5b4d 2752 struct tty_struct *tty;
b342dd51 2753 int ret = 0;
1da177e4
LT
2754
2755 BUG_ON(in_interrupt());
2756
a2bceae0 2757 if (state->uart_port != uport)
2f2dafe7 2758 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
a2bceae0 2759 state->uart_port, uport);
1da177e4 2760
f392ecfa 2761 mutex_lock(&port_mutex);
1da177e4 2762
68ac64cd
RK
2763 /*
2764 * Mark the port "dead" - this prevents any opens from
2765 * succeeding while we shut down the port.
2766 */
a2bceae0 2767 mutex_lock(&port->mutex);
b342dd51
CG
2768 if (!state->uart_port) {
2769 mutex_unlock(&port->mutex);
2770 ret = -EINVAL;
2771 goto out;
2772 }
a2bceae0
AC
2773 uport->flags |= UPF_DEAD;
2774 mutex_unlock(&port->mutex);
68ac64cd 2775
1da177e4 2776 /*
aa4148cf 2777 * Remove the devices from the tty layer
1da177e4 2778 */
a2bceae0 2779 tty_unregister_device(drv->tty_driver, uport->line);
1da177e4 2780
4c6d5b4d
GU
2781 tty = tty_port_tty_get(port);
2782 if (tty) {
a2bceae0 2783 tty_vhangup(port->tty);
4c6d5b4d
GU
2784 tty_kref_put(tty);
2785 }
68ac64cd 2786
5f5c9ae5
GU
2787 /*
2788 * If the port is used as a console, unregister it
2789 */
2790 if (uart_console(uport))
2791 unregister_console(uport->cons);
2792
68ac64cd
RK
2793 /*
2794 * Free the port IO and memory resources, if any.
2795 */
a2bceae0
AC
2796 if (uport->type != PORT_UNKNOWN)
2797 uport->ops->release_port(uport);
266dcff0 2798 kfree(uport->tty_groups);
68ac64cd
RK
2799
2800 /*
2801 * Indicate that there isn't a port here anymore.
2802 */
a2bceae0 2803 uport->type = PORT_UNKNOWN;
68ac64cd 2804
ebd2c8f6 2805 state->uart_port = NULL;
b342dd51 2806out:
f392ecfa 2807 mutex_unlock(&port_mutex);
1da177e4 2808
b342dd51 2809 return ret;
1da177e4
LT
2810}
2811
2812/*
2813 * Are the two ports equivalent?
2814 */
2815int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2816{
2817 if (port1->iotype != port2->iotype)
2818 return 0;
2819
2820 switch (port1->iotype) {
2821 case UPIO_PORT:
2822 return (port1->iobase == port2->iobase);
2823 case UPIO_HUB6:
2824 return (port1->iobase == port2->iobase) &&
2825 (port1->hub6 == port2->hub6);
2826 case UPIO_MEM:
bd94c407 2827 case UPIO_MEM16:
d21b55d3 2828 case UPIO_MEM32:
3ffb1a81 2829 case UPIO_MEM32BE:
d21b55d3
SS
2830 case UPIO_AU:
2831 case UPIO_TSI:
1624f003 2832 return (port1->mapbase == port2->mapbase);
1da177e4
LT
2833 }
2834 return 0;
2835}
2836EXPORT_SYMBOL(uart_match_port);
2837
027d7dac
JS
2838/**
2839 * uart_handle_dcd_change - handle a change of carrier detect state
2840 * @uport: uart_port structure for the open port
2841 * @status: new carrier detect status, nonzero if active
4d90bb14
PH
2842 *
2843 * Caller must hold uport->lock
027d7dac
JS
2844 */
2845void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
2846{
42381572 2847 struct tty_port *port = &uport->state->port;
43eca0ae 2848 struct tty_struct *tty = port->tty;
c993257b 2849 struct tty_ldisc *ld;
027d7dac 2850
4d90bb14
PH
2851 lockdep_assert_held_once(&uport->lock);
2852
c993257b
PH
2853 if (tty) {
2854 ld = tty_ldisc_ref(tty);
2855 if (ld) {
2856 if (ld->ops->dcd_change)
2857 ld->ops->dcd_change(tty, status);
2858 tty_ldisc_deref(ld);
2859 }
42381572 2860 }
027d7dac
JS
2861
2862 uport->icount.dcd++;
027d7dac 2863
299245a1 2864 if (uart_dcd_enabled(uport)) {
027d7dac
JS
2865 if (status)
2866 wake_up_interruptible(&port->open_wait);
43eca0ae
AC
2867 else if (tty)
2868 tty_hangup(tty);
027d7dac 2869 }
027d7dac
JS
2870}
2871EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
2872
2873/**
2874 * uart_handle_cts_change - handle a change of clear-to-send state
2875 * @uport: uart_port structure for the open port
2876 * @status: new clear to send status, nonzero if active
4d90bb14
PH
2877 *
2878 * Caller must hold uport->lock
027d7dac
JS
2879 */
2880void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
2881{
4d90bb14
PH
2882 lockdep_assert_held_once(&uport->lock);
2883
027d7dac
JS
2884 uport->icount.cts++;
2885
391f93f2 2886 if (uart_softcts_mode(uport)) {
d01f4d18 2887 if (uport->hw_stopped) {
027d7dac 2888 if (status) {
d01f4d18 2889 uport->hw_stopped = 0;
027d7dac
JS
2890 uport->ops->start_tx(uport);
2891 uart_write_wakeup(uport);
2892 }
2893 } else {
2894 if (!status) {
d01f4d18 2895 uport->hw_stopped = 1;
027d7dac
JS
2896 uport->ops->stop_tx(uport);
2897 }
2898 }
391f93f2 2899
027d7dac
JS
2900 }
2901}
2902EXPORT_SYMBOL_GPL(uart_handle_cts_change);
2903
cf75525f
JS
2904/**
2905 * uart_insert_char - push a char to the uart layer
2906 *
2907 * User is responsible to call tty_flip_buffer_push when they are done with
2908 * insertion.
2909 *
2910 * @port: corresponding port
2911 * @status: state of the serial port RX buffer (LSR for 8250)
2912 * @overrun: mask of overrun bits in @status
2913 * @ch: character to push
2914 * @flag: flag for the character (see TTY_NORMAL and friends)
2915 */
027d7dac
JS
2916void uart_insert_char(struct uart_port *port, unsigned int status,
2917 unsigned int overrun, unsigned int ch, unsigned int flag)
2918{
92a19f9c 2919 struct tty_port *tport = &port->state->port;
027d7dac
JS
2920
2921 if ((status & port->ignore_status_mask & ~overrun) == 0)
92a19f9c 2922 if (tty_insert_flip_char(tport, ch, flag) == 0)
dabfb351 2923 ++port->icount.buf_overrun;
027d7dac
JS
2924
2925 /*
2926 * Overrun is special. Since it's reported immediately,
2927 * it doesn't affect the current character.
2928 */
2929 if (status & ~port->ignore_status_mask & overrun)
92a19f9c 2930 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
dabfb351 2931 ++port->icount.buf_overrun;
027d7dac
JS
2932}
2933EXPORT_SYMBOL_GPL(uart_insert_char);
2934
1da177e4
LT
2935EXPORT_SYMBOL(uart_write_wakeup);
2936EXPORT_SYMBOL(uart_register_driver);
2937EXPORT_SYMBOL(uart_unregister_driver);
2938EXPORT_SYMBOL(uart_suspend_port);
2939EXPORT_SYMBOL(uart_resume_port);
1da177e4
LT
2940EXPORT_SYMBOL(uart_add_one_port);
2941EXPORT_SYMBOL(uart_remove_one_port);
2942
2943MODULE_DESCRIPTION("Serial driver core");
2944MODULE_LICENSE("GPL");