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