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