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