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