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