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