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