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