xref: /openbmc/linux/drivers/tty/tty_port.c (revision 695586ca20c56cf8cfa87160383307a288d32496)
196fd7ce5SGreg Kroah-Hartman /*
296fd7ce5SGreg Kroah-Hartman  * Tty port functions
396fd7ce5SGreg Kroah-Hartman  */
496fd7ce5SGreg Kroah-Hartman 
596fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
696fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
796fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
896fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
996fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h>
1096fd7ce5SGreg Kroah-Hartman #include <linux/serial.h>
1196fd7ce5SGreg Kroah-Hartman #include <linux/timer.h>
1296fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
1396fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
1496fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
1596fd7ce5SGreg Kroah-Hartman #include <linux/init.h>
1696fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
1796fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
1896fd7ce5SGreg Kroah-Hartman #include <linux/delay.h>
1996fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
2096fd7ce5SGreg Kroah-Hartman 
2196fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port)
2296fd7ce5SGreg Kroah-Hartman {
2396fd7ce5SGreg Kroah-Hartman 	memset(port, 0, sizeof(*port));
2496fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->open_wait);
2596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->close_wait);
2696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->delta_msr_wait);
2796fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->mutex);
2896fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->buf_mutex);
2996fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&port->lock);
3096fd7ce5SGreg Kroah-Hartman 	port->close_delay = (50 * HZ) / 100;
3196fd7ce5SGreg Kroah-Hartman 	port->closing_wait = (3000 * HZ) / 100;
3296fd7ce5SGreg Kroah-Hartman 	kref_init(&port->kref);
3396fd7ce5SGreg Kroah-Hartman }
3496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init);
3596fd7ce5SGreg Kroah-Hartman 
3696fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
3796fd7ce5SGreg Kroah-Hartman {
3896fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
3996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
4096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
4196fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
4296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
4396fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
4496fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
4596fd7ce5SGreg Kroah-Hartman 	return 0;
4696fd7ce5SGreg Kroah-Hartman }
4796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
4896fd7ce5SGreg Kroah-Hartman 
4996fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
5096fd7ce5SGreg Kroah-Hartman {
5196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
5296fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
5396fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
5496fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
5596fd7ce5SGreg Kroah-Hartman 	}
5696fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
5796fd7ce5SGreg Kroah-Hartman }
5896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
5996fd7ce5SGreg Kroah-Hartman 
6096fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
6196fd7ce5SGreg Kroah-Hartman {
6296fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
6396fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
6496fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
6596fd7ce5SGreg Kroah-Hartman 	if (port->ops->destruct)
6696fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
6796fd7ce5SGreg Kroah-Hartman 	else
6896fd7ce5SGreg Kroah-Hartman 		kfree(port);
6996fd7ce5SGreg Kroah-Hartman }
7096fd7ce5SGreg Kroah-Hartman 
7196fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
7296fd7ce5SGreg Kroah-Hartman {
7396fd7ce5SGreg Kroah-Hartman 	if (port)
7496fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
7596fd7ce5SGreg Kroah-Hartman }
7696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
7796fd7ce5SGreg Kroah-Hartman 
7896fd7ce5SGreg Kroah-Hartman /**
7996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_get	-	get a tty reference
8096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
8196fd7ce5SGreg Kroah-Hartman  *
8296fd7ce5SGreg Kroah-Hartman  *	Return a refcount protected tty instance or NULL if the port is not
8396fd7ce5SGreg Kroah-Hartman  *	associated with a tty (eg due to close or hangup)
8496fd7ce5SGreg Kroah-Hartman  */
8596fd7ce5SGreg Kroah-Hartman 
8696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
8796fd7ce5SGreg Kroah-Hartman {
8896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
8996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
9096fd7ce5SGreg Kroah-Hartman 
9196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
9296fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
9396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
9496fd7ce5SGreg Kroah-Hartman 	return tty;
9596fd7ce5SGreg Kroah-Hartman }
9696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
9796fd7ce5SGreg Kroah-Hartman 
9896fd7ce5SGreg Kroah-Hartman /**
9996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_set	-	set the tty of a port
10096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
10196fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
10296fd7ce5SGreg Kroah-Hartman  *
10396fd7ce5SGreg Kroah-Hartman  *	Associate the port and tty pair. Manages any internal refcounts.
10496fd7ce5SGreg Kroah-Hartman  *	Pass NULL to deassociate a port
10596fd7ce5SGreg Kroah-Hartman  */
10696fd7ce5SGreg Kroah-Hartman 
10796fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
10896fd7ce5SGreg Kroah-Hartman {
10996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
11096fd7ce5SGreg Kroah-Hartman 
11196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
11296fd7ce5SGreg Kroah-Hartman 	if (port->tty)
11396fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
11496fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
11596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
11696fd7ce5SGreg Kroah-Hartman }
11796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
11896fd7ce5SGreg Kroah-Hartman 
11996fd7ce5SGreg Kroah-Hartman static void tty_port_shutdown(struct tty_port *port)
12096fd7ce5SGreg Kroah-Hartman {
12196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
12296fd7ce5SGreg Kroah-Hartman 	if (port->ops->shutdown && !port->console &&
12396fd7ce5SGreg Kroah-Hartman 		test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
12496fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
12596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
12696fd7ce5SGreg Kroah-Hartman }
12796fd7ce5SGreg Kroah-Hartman 
12896fd7ce5SGreg Kroah-Hartman /**
12996fd7ce5SGreg Kroah-Hartman  *	tty_port_hangup		-	hangup helper
13096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
13196fd7ce5SGreg Kroah-Hartman  *
13296fd7ce5SGreg Kroah-Hartman  *	Perform port level tty hangup flag and count changes. Drop the tty
13396fd7ce5SGreg Kroah-Hartman  *	reference.
13496fd7ce5SGreg Kroah-Hartman  */
13596fd7ce5SGreg Kroah-Hartman 
13696fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
13796fd7ce5SGreg Kroah-Hartman {
13896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
13996fd7ce5SGreg Kroah-Hartman 
14096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
14196fd7ce5SGreg Kroah-Hartman 	port->count = 0;
14296fd7ce5SGreg Kroah-Hartman 	port->flags &= ~ASYNC_NORMAL_ACTIVE;
14396fd7ce5SGreg Kroah-Hartman 	if (port->tty) {
14496fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &port->tty->flags);
14596fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
14696fd7ce5SGreg Kroah-Hartman 	}
14796fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
14896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
14996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
15096fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
15196fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
15296fd7ce5SGreg Kroah-Hartman }
15396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
15496fd7ce5SGreg Kroah-Hartman 
15596fd7ce5SGreg Kroah-Hartman /**
15696fd7ce5SGreg Kroah-Hartman  *	tty_port_carrier_raised	-	carrier raised check
15796fd7ce5SGreg Kroah-Hartman  *	@port: tty port
15896fd7ce5SGreg Kroah-Hartman  *
15996fd7ce5SGreg Kroah-Hartman  *	Wrapper for the carrier detect logic. For the moment this is used
16096fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
16196fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
16296fd7ce5SGreg Kroah-Hartman  */
16396fd7ce5SGreg Kroah-Hartman 
16496fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
16596fd7ce5SGreg Kroah-Hartman {
16696fd7ce5SGreg Kroah-Hartman 	if (port->ops->carrier_raised == NULL)
16796fd7ce5SGreg Kroah-Hartman 		return 1;
16896fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
16996fd7ce5SGreg Kroah-Hartman }
17096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
17196fd7ce5SGreg Kroah-Hartman 
17296fd7ce5SGreg Kroah-Hartman /**
17396fd7ce5SGreg Kroah-Hartman  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
17496fd7ce5SGreg Kroah-Hartman  *	@port: tty port
17596fd7ce5SGreg Kroah-Hartman  *
17696fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
17796fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
17896fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
17996fd7ce5SGreg Kroah-Hartman  */
18096fd7ce5SGreg Kroah-Hartman 
18196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
18296fd7ce5SGreg Kroah-Hartman {
18396fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
18496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
18596fd7ce5SGreg Kroah-Hartman }
18696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
18796fd7ce5SGreg Kroah-Hartman 
18896fd7ce5SGreg Kroah-Hartman /**
18996fd7ce5SGreg Kroah-Hartman  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
19096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
19196fd7ce5SGreg Kroah-Hartman  *
19296fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
19396fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
19496fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
19596fd7ce5SGreg Kroah-Hartman  */
19696fd7ce5SGreg Kroah-Hartman 
19796fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
19896fd7ce5SGreg Kroah-Hartman {
19996fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
20096fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
20196fd7ce5SGreg Kroah-Hartman }
20296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
20396fd7ce5SGreg Kroah-Hartman 
20496fd7ce5SGreg Kroah-Hartman /**
20596fd7ce5SGreg Kroah-Hartman  *	tty_port_block_til_ready	-	Waiting logic for tty open
20696fd7ce5SGreg Kroah-Hartman  *	@port: the tty port being opened
20796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty device being bound
20896fd7ce5SGreg Kroah-Hartman  *	@filp: the file pointer of the opener
20996fd7ce5SGreg Kroah-Hartman  *
21096fd7ce5SGreg Kroah-Hartman  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
21196fd7ce5SGreg Kroah-Hartman  *	Handles:
21296fd7ce5SGreg Kroah-Hartman  *		- hangup (both before and during)
21396fd7ce5SGreg Kroah-Hartman  *		- non blocking open
21496fd7ce5SGreg Kroah-Hartman  *		- rts/dtr/dcd
21596fd7ce5SGreg Kroah-Hartman  *		- signals
21696fd7ce5SGreg Kroah-Hartman  *		- port flags and counts
21796fd7ce5SGreg Kroah-Hartman  *
21896fd7ce5SGreg Kroah-Hartman  *	The passed tty_port must implement the carrier_raised method if it can
21996fd7ce5SGreg Kroah-Hartman  *	do carrier detect and the dtr_rts method if it supports software
22096fd7ce5SGreg Kroah-Hartman  *	management of these lines. Note that the dtr/rts raise is done each
22196fd7ce5SGreg Kroah-Hartman  *	iteration as a hangup may have previously dropped them while we wait.
22296fd7ce5SGreg Kroah-Hartman  */
22396fd7ce5SGreg Kroah-Hartman 
22496fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
22596fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
22696fd7ce5SGreg Kroah-Hartman {
22796fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
22896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
22996fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
23096fd7ce5SGreg Kroah-Hartman 
23196fd7ce5SGreg Kroah-Hartman 	/* block if port is in the process of being closed */
23296fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
233f309532bSLinus Torvalds 		wait_event_interruptible_tty(port->close_wait,
23496fd7ce5SGreg Kroah-Hartman 				!(port->flags & ASYNC_CLOSING));
23596fd7ce5SGreg Kroah-Hartman 		if (port->flags & ASYNC_HUP_NOTIFY)
23696fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
23796fd7ce5SGreg Kroah-Hartman 		else
23896fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
23996fd7ce5SGreg Kroah-Hartman 	}
24096fd7ce5SGreg Kroah-Hartman 
24196fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
24296fd7ce5SGreg Kroah-Hartman 	   the port has just hung up or is in another error state */
24396fd7ce5SGreg Kroah-Hartman 	if (tty->flags & (1 << TTY_IO_ERROR)) {
24496fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
24596fd7ce5SGreg Kroah-Hartman 		return 0;
24696fd7ce5SGreg Kroah-Hartman 	}
24796fd7ce5SGreg Kroah-Hartman 	if (filp->f_flags & O_NONBLOCK) {
24896fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
24996fd7ce5SGreg Kroah-Hartman 		if (tty->termios->c_cflag & CBAUD)
25096fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
25196fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
25296fd7ce5SGreg Kroah-Hartman 		return 0;
25396fd7ce5SGreg Kroah-Hartman 	}
25496fd7ce5SGreg Kroah-Hartman 
25596fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
25696fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
25796fd7ce5SGreg Kroah-Hartman 
25896fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
25996fd7ce5SGreg Kroah-Hartman 	   carrier, but we must also wait for any close that is in progress
26096fd7ce5SGreg Kroah-Hartman 	   before the next open may complete */
26196fd7ce5SGreg Kroah-Hartman 
26296fd7ce5SGreg Kroah-Hartman 	retval = 0;
26396fd7ce5SGreg Kroah-Hartman 
26496fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
26596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
26696fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
26796fd7ce5SGreg Kroah-Hartman 		port->count--;
26896fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
26996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
27096fd7ce5SGreg Kroah-Hartman 
27196fd7ce5SGreg Kroah-Hartman 	while (1) {
27296fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
27396fd7ce5SGreg Kroah-Hartman 		if (tty->termios->c_cflag & CBAUD)
27496fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
27596fd7ce5SGreg Kroah-Hartman 
27696fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
27796fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
27896fd7ce5SGreg Kroah-Hartman 							Return accordingly */
27996fd7ce5SGreg Kroah-Hartman 		if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
28096fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
28196fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
28296fd7ce5SGreg Kroah-Hartman 			else
28396fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
28496fd7ce5SGreg Kroah-Hartman 			break;
28596fd7ce5SGreg Kroah-Hartman 		}
2860eee50afSJiri Slaby 		/*
2870eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
2880eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
2890eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
2900eee50afSJiri Slaby 		 * on some hardware.
2910eee50afSJiri Slaby 		 */
29296fd7ce5SGreg Kroah-Hartman 		if (!(port->flags & ASYNC_CLOSING) &&
2930eee50afSJiri Slaby 				(do_clocal || tty_port_carrier_raised(port)))
29496fd7ce5SGreg Kroah-Hartman 			break;
29596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
29696fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
29796fd7ce5SGreg Kroah-Hartman 			break;
29896fd7ce5SGreg Kroah-Hartman 		}
299f309532bSLinus Torvalds 		tty_unlock();
30096fd7ce5SGreg Kroah-Hartman 		schedule();
301f309532bSLinus Torvalds 		tty_lock();
30296fd7ce5SGreg Kroah-Hartman 	}
30396fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
30496fd7ce5SGreg Kroah-Hartman 
30596fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
30696fd7ce5SGreg Kroah-Hartman 	   we must not mess that up further */
30796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
30896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
30996fd7ce5SGreg Kroah-Hartman 		port->count++;
31096fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
31196fd7ce5SGreg Kroah-Hartman 	if (retval == 0)
31296fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
31396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
31496fd7ce5SGreg Kroah-Hartman 	return retval;
31596fd7ce5SGreg Kroah-Hartman }
31696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
31796fd7ce5SGreg Kroah-Hartman 
31896fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
31996fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
32096fd7ce5SGreg Kroah-Hartman {
32196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
32296fd7ce5SGreg Kroah-Hartman 
32396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
32496fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp)) {
32596fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
32696fd7ce5SGreg Kroah-Hartman 		return 0;
32796fd7ce5SGreg Kroah-Hartman 	}
32896fd7ce5SGreg Kroah-Hartman 
32996fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
33096fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING
33196fd7ce5SGreg Kroah-Hartman 		    "tty_port_close_start: tty->count = 1 port count = %d.\n",
33296fd7ce5SGreg Kroah-Hartman 								port->count);
33396fd7ce5SGreg Kroah-Hartman 		port->count = 1;
33496fd7ce5SGreg Kroah-Hartman 	}
33596fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
33696fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING "tty_port_close_start: count = %d\n",
33796fd7ce5SGreg Kroah-Hartman 								port->count);
33896fd7ce5SGreg Kroah-Hartman 		port->count = 0;
33996fd7ce5SGreg Kroah-Hartman 	}
34096fd7ce5SGreg Kroah-Hartman 
34196fd7ce5SGreg Kroah-Hartman 	if (port->count) {
34296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
34396fd7ce5SGreg Kroah-Hartman 		if (port->ops->drop)
34496fd7ce5SGreg Kroah-Hartman 			port->ops->drop(port);
34596fd7ce5SGreg Kroah-Hartman 		return 0;
34696fd7ce5SGreg Kroah-Hartman 	}
34796fd7ce5SGreg Kroah-Hartman 	set_bit(ASYNCB_CLOSING, &port->flags);
34896fd7ce5SGreg Kroah-Hartman 	tty->closing = 1;
34996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
35096fd7ce5SGreg Kroah-Hartman 	/* Don't block on a stalled port, just pull the chain */
35196fd7ce5SGreg Kroah-Hartman 	if (tty->flow_stopped)
35296fd7ce5SGreg Kroah-Hartman 		tty_driver_flush_buffer(tty);
35396fd7ce5SGreg Kroah-Hartman 	if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
35496fd7ce5SGreg Kroah-Hartman 			port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
355424cc039SJiri Slaby 		tty_wait_until_sent_from_close(tty, port->closing_wait);
35696fd7ce5SGreg Kroah-Hartman 	if (port->drain_delay) {
35796fd7ce5SGreg Kroah-Hartman 		unsigned int bps = tty_get_baud_rate(tty);
35896fd7ce5SGreg Kroah-Hartman 		long timeout;
35996fd7ce5SGreg Kroah-Hartman 
36096fd7ce5SGreg Kroah-Hartman 		if (bps > 1200)
36196fd7ce5SGreg Kroah-Hartman 			timeout = max_t(long,
36296fd7ce5SGreg Kroah-Hartman 				(HZ * 10 * port->drain_delay) / bps, HZ / 10);
36396fd7ce5SGreg Kroah-Hartman 		else
36496fd7ce5SGreg Kroah-Hartman 			timeout = 2 * HZ;
36596fd7ce5SGreg Kroah-Hartman 		schedule_timeout_interruptible(timeout);
36696fd7ce5SGreg Kroah-Hartman 	}
36796fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
36896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
36996fd7ce5SGreg Kroah-Hartman 
37096fd7ce5SGreg Kroah-Hartman 	/* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
37196fd7ce5SGreg Kroah-Hartman 	   hang up the line */
37296fd7ce5SGreg Kroah-Hartman 	if (tty->termios->c_cflag & HUPCL)
37396fd7ce5SGreg Kroah-Hartman 		tty_port_lower_dtr_rts(port);
37496fd7ce5SGreg Kroah-Hartman 
37596fd7ce5SGreg Kroah-Hartman 	/* Don't call port->drop for the last reference. Callers will want
37696fd7ce5SGreg Kroah-Hartman 	   to drop the last active reference in ->shutdown() or the tty
37796fd7ce5SGreg Kroah-Hartman 	   shutdown path */
37896fd7ce5SGreg Kroah-Hartman 	return 1;
37996fd7ce5SGreg Kroah-Hartman }
38096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
38196fd7ce5SGreg Kroah-Hartman 
38296fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
38396fd7ce5SGreg Kroah-Hartman {
38496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
38596fd7ce5SGreg Kroah-Hartman 
38696fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
38796fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
38896fd7ce5SGreg Kroah-Hartman 
38996fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
39096fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
39196fd7ce5SGreg Kroah-Hartman 		if (port->close_delay) {
39296fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(
39396fd7ce5SGreg Kroah-Hartman 				jiffies_to_msecs(port->close_delay));
39496fd7ce5SGreg Kroah-Hartman 		}
39596fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
39696fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
39796fd7ce5SGreg Kroah-Hartman 	}
39896fd7ce5SGreg Kroah-Hartman 	port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
39996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->close_wait);
40096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
40196fd7ce5SGreg Kroah-Hartman }
40296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
40396fd7ce5SGreg Kroah-Hartman 
40496fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
40596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
40696fd7ce5SGreg Kroah-Hartman {
40796fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
40896fd7ce5SGreg Kroah-Hartman 		return;
40996fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
41096fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_IO_ERROR, &tty->flags);
41196fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
41296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
41396fd7ce5SGreg Kroah-Hartman }
41496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
41596fd7ce5SGreg Kroah-Hartman 
416*695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
417*695586caSJiri Slaby 		struct tty_struct *tty)
418*695586caSJiri Slaby {
419*695586caSJiri Slaby 	tty->port = port;
420*695586caSJiri Slaby 	return tty_standard_install(driver, tty);
421*695586caSJiri Slaby }
422*695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
423*695586caSJiri Slaby 
42496fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
42596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
42696fd7ce5SGreg Kroah-Hartman {
42796fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
42896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
42996fd7ce5SGreg Kroah-Hartman 		++port->count;
43096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
43196fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
43296fd7ce5SGreg Kroah-Hartman 
43396fd7ce5SGreg Kroah-Hartman 	/*
43496fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
43596fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
43696fd7ce5SGreg Kroah-Hartman 	 * port mutex.
43796fd7ce5SGreg Kroah-Hartman 	 */
43896fd7ce5SGreg Kroah-Hartman 
43996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
44096fd7ce5SGreg Kroah-Hartman 
44196fd7ce5SGreg Kroah-Hartman 	if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
44296fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
44396fd7ce5SGreg Kroah-Hartman 		if (port->ops->activate) {
44496fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
44596fd7ce5SGreg Kroah-Hartman 			if (retval) {
44696fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
44796fd7ce5SGreg Kroah-Hartman 				return retval;
44896fd7ce5SGreg Kroah-Hartman 			}
44996fd7ce5SGreg Kroah-Hartman 		}
45096fd7ce5SGreg Kroah-Hartman 		set_bit(ASYNCB_INITIALIZED, &port->flags);
45196fd7ce5SGreg Kroah-Hartman 	}
45296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
45396fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
45496fd7ce5SGreg Kroah-Hartman }
45596fd7ce5SGreg Kroah-Hartman 
45696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
457