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