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