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