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