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