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