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