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