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