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