xref: /openbmc/linux/drivers/tty/tty_port.c (revision 72a33bf58c50892bce7ee4f58d487e818dec1c7e)
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 
36*72a33bf5SJiri Slaby /**
37*72a33bf5SJiri Slaby  * tty_port_register_device - register tty device
38*72a33bf5SJiri Slaby  * @port: tty_port of the device
39*72a33bf5SJiri Slaby  * @driver: tty_driver for this device
40*72a33bf5SJiri Slaby  * @index: index of the tty
41*72a33bf5SJiri Slaby  * @device: parent if exists, otherwise NULL
42*72a33bf5SJiri Slaby  *
43*72a33bf5SJiri Slaby  * It is the same as tty_register_device except the provided @port is linked to
44*72a33bf5SJiri Slaby  * a concrete tty specified by @index. Use this or tty_port_install (or both).
45*72a33bf5SJiri Slaby  * Call tty_port_link_device as a last resort.
46*72a33bf5SJiri Slaby  */
47057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port,
48057eb856SJiri Slaby 		struct tty_driver *driver, unsigned index,
49057eb856SJiri Slaby 		struct device *device)
50057eb856SJiri Slaby {
51057eb856SJiri Slaby 	driver->ports[index] = port;
52057eb856SJiri Slaby 	return tty_register_device(driver, index, device);
53057eb856SJiri Slaby }
54057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device);
55057eb856SJiri Slaby 
5696fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
5796fd7ce5SGreg Kroah-Hartman {
5896fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
5996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
6096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
6196fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
6296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
6396fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
6496fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
6596fd7ce5SGreg Kroah-Hartman 	return 0;
6696fd7ce5SGreg Kroah-Hartman }
6796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
6896fd7ce5SGreg Kroah-Hartman 
6996fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
7096fd7ce5SGreg Kroah-Hartman {
7196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
7296fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
7396fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
7496fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
7596fd7ce5SGreg Kroah-Hartman 	}
7696fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
7796fd7ce5SGreg Kroah-Hartman }
7896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
7996fd7ce5SGreg Kroah-Hartman 
8096fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
8196fd7ce5SGreg Kroah-Hartman {
8296fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
8396fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
8496fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
8596fd7ce5SGreg Kroah-Hartman 	if (port->ops->destruct)
8696fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
8796fd7ce5SGreg Kroah-Hartman 	else
8896fd7ce5SGreg Kroah-Hartman 		kfree(port);
8996fd7ce5SGreg Kroah-Hartman }
9096fd7ce5SGreg Kroah-Hartman 
9196fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
9296fd7ce5SGreg Kroah-Hartman {
9396fd7ce5SGreg Kroah-Hartman 	if (port)
9496fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
9596fd7ce5SGreg Kroah-Hartman }
9696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
9796fd7ce5SGreg Kroah-Hartman 
9896fd7ce5SGreg Kroah-Hartman /**
9996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_get	-	get a tty reference
10096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
10196fd7ce5SGreg Kroah-Hartman  *
10296fd7ce5SGreg Kroah-Hartman  *	Return a refcount protected tty instance or NULL if the port is not
10396fd7ce5SGreg Kroah-Hartman  *	associated with a tty (eg due to close or hangup)
10496fd7ce5SGreg Kroah-Hartman  */
10596fd7ce5SGreg Kroah-Hartman 
10696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
10796fd7ce5SGreg Kroah-Hartman {
10896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
10996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
11096fd7ce5SGreg Kroah-Hartman 
11196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
11296fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
11396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
11496fd7ce5SGreg Kroah-Hartman 	return tty;
11596fd7ce5SGreg Kroah-Hartman }
11696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
11796fd7ce5SGreg Kroah-Hartman 
11896fd7ce5SGreg Kroah-Hartman /**
11996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_set	-	set the tty of a port
12096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
12196fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
12296fd7ce5SGreg Kroah-Hartman  *
12396fd7ce5SGreg Kroah-Hartman  *	Associate the port and tty pair. Manages any internal refcounts.
12496fd7ce5SGreg Kroah-Hartman  *	Pass NULL to deassociate a port
12596fd7ce5SGreg Kroah-Hartman  */
12696fd7ce5SGreg Kroah-Hartman 
12796fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
12896fd7ce5SGreg Kroah-Hartman {
12996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
13096fd7ce5SGreg Kroah-Hartman 
13196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
13296fd7ce5SGreg Kroah-Hartman 	if (port->tty)
13396fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
13496fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
13596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
13696fd7ce5SGreg Kroah-Hartman }
13796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
13896fd7ce5SGreg Kroah-Hartman 
13996fd7ce5SGreg Kroah-Hartman static void tty_port_shutdown(struct tty_port *port)
14096fd7ce5SGreg Kroah-Hartman {
14196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
14296fd7ce5SGreg Kroah-Hartman 	if (port->ops->shutdown && !port->console &&
14396fd7ce5SGreg Kroah-Hartman 		test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
14496fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
14596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
14696fd7ce5SGreg Kroah-Hartman }
14796fd7ce5SGreg Kroah-Hartman 
14896fd7ce5SGreg Kroah-Hartman /**
14996fd7ce5SGreg Kroah-Hartman  *	tty_port_hangup		-	hangup helper
15096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
15196fd7ce5SGreg Kroah-Hartman  *
15296fd7ce5SGreg Kroah-Hartman  *	Perform port level tty hangup flag and count changes. Drop the tty
15396fd7ce5SGreg Kroah-Hartman  *	reference.
15496fd7ce5SGreg Kroah-Hartman  */
15596fd7ce5SGreg Kroah-Hartman 
15696fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
15796fd7ce5SGreg Kroah-Hartman {
15896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
15996fd7ce5SGreg Kroah-Hartman 
16096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
16196fd7ce5SGreg Kroah-Hartman 	port->count = 0;
16296fd7ce5SGreg Kroah-Hartman 	port->flags &= ~ASYNC_NORMAL_ACTIVE;
16396fd7ce5SGreg Kroah-Hartman 	if (port->tty) {
16496fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &port->tty->flags);
16596fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
16696fd7ce5SGreg Kroah-Hartman 	}
16796fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
16896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
16996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
17096fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
17196fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
17296fd7ce5SGreg Kroah-Hartman }
17396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
17496fd7ce5SGreg Kroah-Hartman 
17596fd7ce5SGreg Kroah-Hartman /**
17696fd7ce5SGreg Kroah-Hartman  *	tty_port_carrier_raised	-	carrier raised check
17796fd7ce5SGreg Kroah-Hartman  *	@port: tty port
17896fd7ce5SGreg Kroah-Hartman  *
17996fd7ce5SGreg Kroah-Hartman  *	Wrapper for the carrier detect logic. For the moment this is used
18096fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
18196fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
18296fd7ce5SGreg Kroah-Hartman  */
18396fd7ce5SGreg Kroah-Hartman 
18496fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
18596fd7ce5SGreg Kroah-Hartman {
18696fd7ce5SGreg Kroah-Hartman 	if (port->ops->carrier_raised == NULL)
18796fd7ce5SGreg Kroah-Hartman 		return 1;
18896fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
18996fd7ce5SGreg Kroah-Hartman }
19096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
19196fd7ce5SGreg Kroah-Hartman 
19296fd7ce5SGreg Kroah-Hartman /**
19396fd7ce5SGreg Kroah-Hartman  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
19496fd7ce5SGreg Kroah-Hartman  *	@port: tty port
19596fd7ce5SGreg Kroah-Hartman  *
19696fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
19796fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
19896fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
19996fd7ce5SGreg Kroah-Hartman  */
20096fd7ce5SGreg Kroah-Hartman 
20196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
20296fd7ce5SGreg Kroah-Hartman {
20396fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
20496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
20596fd7ce5SGreg Kroah-Hartman }
20696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
20796fd7ce5SGreg Kroah-Hartman 
20896fd7ce5SGreg Kroah-Hartman /**
20996fd7ce5SGreg Kroah-Hartman  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
21096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
21196fd7ce5SGreg Kroah-Hartman  *
21296fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
21396fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
21496fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
21596fd7ce5SGreg Kroah-Hartman  */
21696fd7ce5SGreg Kroah-Hartman 
21796fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
21896fd7ce5SGreg Kroah-Hartman {
21996fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
22096fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
22196fd7ce5SGreg Kroah-Hartman }
22296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
22396fd7ce5SGreg Kroah-Hartman 
22496fd7ce5SGreg Kroah-Hartman /**
22596fd7ce5SGreg Kroah-Hartman  *	tty_port_block_til_ready	-	Waiting logic for tty open
22696fd7ce5SGreg Kroah-Hartman  *	@port: the tty port being opened
22796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty device being bound
22896fd7ce5SGreg Kroah-Hartman  *	@filp: the file pointer of the opener
22996fd7ce5SGreg Kroah-Hartman  *
23096fd7ce5SGreg Kroah-Hartman  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
23196fd7ce5SGreg Kroah-Hartman  *	Handles:
23296fd7ce5SGreg Kroah-Hartman  *		- hangup (both before and during)
23396fd7ce5SGreg Kroah-Hartman  *		- non blocking open
23496fd7ce5SGreg Kroah-Hartman  *		- rts/dtr/dcd
23596fd7ce5SGreg Kroah-Hartman  *		- signals
23696fd7ce5SGreg Kroah-Hartman  *		- port flags and counts
23796fd7ce5SGreg Kroah-Hartman  *
23896fd7ce5SGreg Kroah-Hartman  *	The passed tty_port must implement the carrier_raised method if it can
23996fd7ce5SGreg Kroah-Hartman  *	do carrier detect and the dtr_rts method if it supports software
24096fd7ce5SGreg Kroah-Hartman  *	management of these lines. Note that the dtr/rts raise is done each
24196fd7ce5SGreg Kroah-Hartman  *	iteration as a hangup may have previously dropped them while we wait.
24296fd7ce5SGreg Kroah-Hartman  */
24396fd7ce5SGreg Kroah-Hartman 
24496fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
24596fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
24696fd7ce5SGreg Kroah-Hartman {
24796fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
24896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
24996fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
25096fd7ce5SGreg Kroah-Hartman 
25196fd7ce5SGreg Kroah-Hartman 	/* block if port is in the process of being closed */
25296fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
25389c8d91eSAlan Cox 		wait_event_interruptible_tty(tty, port->close_wait,
25496fd7ce5SGreg Kroah-Hartman 				!(port->flags & ASYNC_CLOSING));
25596fd7ce5SGreg Kroah-Hartman 		if (port->flags & ASYNC_HUP_NOTIFY)
25696fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
25796fd7ce5SGreg Kroah-Hartman 		else
25896fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
25996fd7ce5SGreg Kroah-Hartman 	}
26096fd7ce5SGreg Kroah-Hartman 
26196fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
26296fd7ce5SGreg Kroah-Hartman 	   the port has just hung up or is in another error state */
26396fd7ce5SGreg Kroah-Hartman 	if (tty->flags & (1 << TTY_IO_ERROR)) {
26496fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
26596fd7ce5SGreg Kroah-Hartman 		return 0;
26696fd7ce5SGreg Kroah-Hartman 	}
26796fd7ce5SGreg Kroah-Hartman 	if (filp->f_flags & O_NONBLOCK) {
26896fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
269adc8d746SAlan Cox 		if (tty->termios.c_cflag & CBAUD)
27096fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
27196fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
27296fd7ce5SGreg Kroah-Hartman 		return 0;
27396fd7ce5SGreg Kroah-Hartman 	}
27496fd7ce5SGreg Kroah-Hartman 
27596fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
27696fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
27796fd7ce5SGreg Kroah-Hartman 
27896fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
27996fd7ce5SGreg Kroah-Hartman 	   carrier, but we must also wait for any close that is in progress
28096fd7ce5SGreg Kroah-Hartman 	   before the next open may complete */
28196fd7ce5SGreg Kroah-Hartman 
28296fd7ce5SGreg Kroah-Hartman 	retval = 0;
28396fd7ce5SGreg Kroah-Hartman 
28496fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
28596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
28696fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
28796fd7ce5SGreg Kroah-Hartman 		port->count--;
28896fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
28996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
29096fd7ce5SGreg Kroah-Hartman 
29196fd7ce5SGreg Kroah-Hartman 	while (1) {
29296fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
293adc8d746SAlan Cox 		if (tty->termios.c_cflag & CBAUD)
29496fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
29596fd7ce5SGreg Kroah-Hartman 
29696fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
29796fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
29896fd7ce5SGreg Kroah-Hartman 							Return accordingly */
29996fd7ce5SGreg Kroah-Hartman 		if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
30096fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
30196fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
30296fd7ce5SGreg Kroah-Hartman 			else
30396fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
30496fd7ce5SGreg Kroah-Hartman 			break;
30596fd7ce5SGreg Kroah-Hartman 		}
3060eee50afSJiri Slaby 		/*
3070eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
3080eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
3090eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
3100eee50afSJiri Slaby 		 * on some hardware.
3110eee50afSJiri Slaby 		 */
31296fd7ce5SGreg Kroah-Hartman 		if (!(port->flags & ASYNC_CLOSING) &&
3130eee50afSJiri Slaby 				(do_clocal || tty_port_carrier_raised(port)))
31496fd7ce5SGreg Kroah-Hartman 			break;
31596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
31696fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
31796fd7ce5SGreg Kroah-Hartman 			break;
31896fd7ce5SGreg Kroah-Hartman 		}
31989c8d91eSAlan Cox 		tty_unlock(tty);
32096fd7ce5SGreg Kroah-Hartman 		schedule();
32189c8d91eSAlan Cox 		tty_lock(tty);
32296fd7ce5SGreg Kroah-Hartman 	}
32396fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
32496fd7ce5SGreg Kroah-Hartman 
32596fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
32696fd7ce5SGreg Kroah-Hartman 	   we must not mess that up further */
32796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
32896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
32996fd7ce5SGreg Kroah-Hartman 		port->count++;
33096fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
33196fd7ce5SGreg Kroah-Hartman 	if (retval == 0)
33296fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
33396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
33496fd7ce5SGreg Kroah-Hartman 	return retval;
33596fd7ce5SGreg Kroah-Hartman }
33696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
33796fd7ce5SGreg Kroah-Hartman 
33896fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
33996fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
34096fd7ce5SGreg Kroah-Hartman {
34196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
34296fd7ce5SGreg Kroah-Hartman 
34396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
34496fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp)) {
34596fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
34696fd7ce5SGreg Kroah-Hartman 		return 0;
34796fd7ce5SGreg Kroah-Hartman 	}
34896fd7ce5SGreg Kroah-Hartman 
34996fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
35096fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING
35196fd7ce5SGreg Kroah-Hartman 		    "tty_port_close_start: tty->count = 1 port count = %d.\n",
35296fd7ce5SGreg Kroah-Hartman 								port->count);
35396fd7ce5SGreg Kroah-Hartman 		port->count = 1;
35496fd7ce5SGreg Kroah-Hartman 	}
35596fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
35696fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING "tty_port_close_start: count = %d\n",
35796fd7ce5SGreg Kroah-Hartman 								port->count);
35896fd7ce5SGreg Kroah-Hartman 		port->count = 0;
35996fd7ce5SGreg Kroah-Hartman 	}
36096fd7ce5SGreg Kroah-Hartman 
36196fd7ce5SGreg Kroah-Hartman 	if (port->count) {
36296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
36396fd7ce5SGreg Kroah-Hartman 		if (port->ops->drop)
36496fd7ce5SGreg Kroah-Hartman 			port->ops->drop(port);
36596fd7ce5SGreg Kroah-Hartman 		return 0;
36696fd7ce5SGreg Kroah-Hartman 	}
36796fd7ce5SGreg Kroah-Hartman 	set_bit(ASYNCB_CLOSING, &port->flags);
36896fd7ce5SGreg Kroah-Hartman 	tty->closing = 1;
36996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
37096fd7ce5SGreg Kroah-Hartman 	/* Don't block on a stalled port, just pull the chain */
37196fd7ce5SGreg Kroah-Hartman 	if (tty->flow_stopped)
37296fd7ce5SGreg Kroah-Hartman 		tty_driver_flush_buffer(tty);
37396fd7ce5SGreg Kroah-Hartman 	if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
37496fd7ce5SGreg Kroah-Hartman 			port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
375424cc039SJiri Slaby 		tty_wait_until_sent_from_close(tty, port->closing_wait);
37696fd7ce5SGreg Kroah-Hartman 	if (port->drain_delay) {
37796fd7ce5SGreg Kroah-Hartman 		unsigned int bps = tty_get_baud_rate(tty);
37896fd7ce5SGreg Kroah-Hartman 		long timeout;
37996fd7ce5SGreg Kroah-Hartman 
38096fd7ce5SGreg Kroah-Hartman 		if (bps > 1200)
38196fd7ce5SGreg Kroah-Hartman 			timeout = max_t(long,
38296fd7ce5SGreg Kroah-Hartman 				(HZ * 10 * port->drain_delay) / bps, HZ / 10);
38396fd7ce5SGreg Kroah-Hartman 		else
38496fd7ce5SGreg Kroah-Hartman 			timeout = 2 * HZ;
38596fd7ce5SGreg Kroah-Hartman 		schedule_timeout_interruptible(timeout);
38696fd7ce5SGreg Kroah-Hartman 	}
38796fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
38896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
38996fd7ce5SGreg Kroah-Hartman 
39096fd7ce5SGreg Kroah-Hartman 	/* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
39196fd7ce5SGreg Kroah-Hartman 	   hang up the line */
392adc8d746SAlan Cox 	if (tty->termios.c_cflag & HUPCL)
39396fd7ce5SGreg Kroah-Hartman 		tty_port_lower_dtr_rts(port);
39496fd7ce5SGreg Kroah-Hartman 
39596fd7ce5SGreg Kroah-Hartman 	/* Don't call port->drop for the last reference. Callers will want
39696fd7ce5SGreg Kroah-Hartman 	   to drop the last active reference in ->shutdown() or the tty
39796fd7ce5SGreg Kroah-Hartman 	   shutdown path */
39896fd7ce5SGreg Kroah-Hartman 	return 1;
39996fd7ce5SGreg Kroah-Hartman }
40096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
40196fd7ce5SGreg Kroah-Hartman 
40296fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
40396fd7ce5SGreg Kroah-Hartman {
40496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
40596fd7ce5SGreg Kroah-Hartman 
40696fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
40796fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
40896fd7ce5SGreg Kroah-Hartman 
40996fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
41096fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
41196fd7ce5SGreg Kroah-Hartman 		if (port->close_delay) {
41296fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(
41396fd7ce5SGreg Kroah-Hartman 				jiffies_to_msecs(port->close_delay));
41496fd7ce5SGreg Kroah-Hartman 		}
41596fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
41696fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
41796fd7ce5SGreg Kroah-Hartman 	}
41896fd7ce5SGreg Kroah-Hartman 	port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
41996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->close_wait);
42096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
42196fd7ce5SGreg Kroah-Hartman }
42296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
42396fd7ce5SGreg Kroah-Hartman 
42496fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
42596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
42696fd7ce5SGreg Kroah-Hartman {
42796fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
42896fd7ce5SGreg Kroah-Hartman 		return;
42996fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
43096fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_IO_ERROR, &tty->flags);
43196fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
43296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
43396fd7ce5SGreg Kroah-Hartman }
43496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
43596fd7ce5SGreg Kroah-Hartman 
436*72a33bf5SJiri Slaby /**
437*72a33bf5SJiri Slaby  * tty_port_install - generic tty->ops->install handler
438*72a33bf5SJiri Slaby  * @port: tty_port of the device
439*72a33bf5SJiri Slaby  * @driver: tty_driver for this device
440*72a33bf5SJiri Slaby  * @tty: tty to be installed
441*72a33bf5SJiri Slaby  *
442*72a33bf5SJiri Slaby  * It is the same as tty_standard_install except the provided @port is linked
443*72a33bf5SJiri Slaby  * to a concrete tty specified by @tty. Use this or tty_port_register_device
444*72a33bf5SJiri Slaby  * (or both). Call tty_port_link_device as a last resort.
445*72a33bf5SJiri Slaby  */
446695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
447695586caSJiri Slaby 		struct tty_struct *tty)
448695586caSJiri Slaby {
449695586caSJiri Slaby 	tty->port = port;
450695586caSJiri Slaby 	return tty_standard_install(driver, tty);
451695586caSJiri Slaby }
452695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
453695586caSJiri Slaby 
45496fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
45596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
45696fd7ce5SGreg Kroah-Hartman {
45796fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
45896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
45996fd7ce5SGreg Kroah-Hartman 		++port->count;
46096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
46196fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
46296fd7ce5SGreg Kroah-Hartman 
46396fd7ce5SGreg Kroah-Hartman 	/*
46496fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
46596fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
46696fd7ce5SGreg Kroah-Hartman 	 * port mutex.
46796fd7ce5SGreg Kroah-Hartman 	 */
46896fd7ce5SGreg Kroah-Hartman 
46996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
47096fd7ce5SGreg Kroah-Hartman 
47196fd7ce5SGreg Kroah-Hartman 	if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
47296fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
47396fd7ce5SGreg Kroah-Hartman 		if (port->ops->activate) {
47496fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
47596fd7ce5SGreg Kroah-Hartman 			if (retval) {
47696fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
47796fd7ce5SGreg Kroah-Hartman 				return retval;
47896fd7ce5SGreg Kroah-Hartman 			}
47996fd7ce5SGreg Kroah-Hartman 		}
48096fd7ce5SGreg Kroah-Hartman 		set_bit(ASYNCB_INITIALIZED, &port->flags);
48196fd7ce5SGreg Kroah-Hartman 	}
48296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
48396fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
48496fd7ce5SGreg Kroah-Hartman }
48596fd7ce5SGreg Kroah-Hartman 
48696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
487