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