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