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