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