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