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