xref: /openbmc/linux/drivers/tty/tty_port.c (revision de274bfe0fc81def6ddb8a17020a9a4b56477cc4)
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));
24ecbbfd44SJiri Slaby 	tty_buffer_init(port);
2596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->open_wait);
2696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->close_wait);
2796fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->delta_msr_wait);
2896fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->mutex);
2996fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->buf_mutex);
3096fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&port->lock);
3196fd7ce5SGreg Kroah-Hartman 	port->close_delay = (50 * HZ) / 100;
3296fd7ce5SGreg Kroah-Hartman 	port->closing_wait = (3000 * HZ) / 100;
3396fd7ce5SGreg Kroah-Hartman 	kref_init(&port->kref);
3496fd7ce5SGreg Kroah-Hartman }
3596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init);
3696fd7ce5SGreg Kroah-Hartman 
3772a33bf5SJiri Slaby /**
382cb4ca02SJiri Slaby  * tty_port_link_device - link tty and tty_port
392cb4ca02SJiri Slaby  * @port: tty_port of the device
402cb4ca02SJiri Slaby  * @driver: tty_driver for this device
412cb4ca02SJiri Slaby  * @index: index of the tty
422cb4ca02SJiri Slaby  *
432cb4ca02SJiri Slaby  * Provide the tty layer wit ha link from a tty (specified by @index) to a
442cb4ca02SJiri Slaby  * tty_port (@port). Use this only if neither tty_port_register_device nor
452cb4ca02SJiri Slaby  * tty_port_install is used in the driver. If used, this has to be called before
462cb4ca02SJiri Slaby  * tty_register_driver.
472cb4ca02SJiri Slaby  */
482cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port,
492cb4ca02SJiri Slaby 		struct tty_driver *driver, unsigned index)
502cb4ca02SJiri Slaby {
512cb4ca02SJiri Slaby 	if (WARN_ON(index >= driver->num))
522cb4ca02SJiri Slaby 		return;
532cb4ca02SJiri Slaby 	driver->ports[index] = port;
542cb4ca02SJiri Slaby }
552cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device);
562cb4ca02SJiri Slaby 
572cb4ca02SJiri Slaby /**
5872a33bf5SJiri Slaby  * tty_port_register_device - register tty device
5972a33bf5SJiri Slaby  * @port: tty_port of the device
6072a33bf5SJiri Slaby  * @driver: tty_driver for this device
6172a33bf5SJiri Slaby  * @index: index of the tty
6272a33bf5SJiri Slaby  * @device: parent if exists, otherwise NULL
6372a33bf5SJiri Slaby  *
6472a33bf5SJiri Slaby  * It is the same as tty_register_device except the provided @port is linked to
6572a33bf5SJiri Slaby  * a concrete tty specified by @index. Use this or tty_port_install (or both).
6672a33bf5SJiri Slaby  * Call tty_port_link_device as a last resort.
6772a33bf5SJiri Slaby  */
68057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port,
69057eb856SJiri Slaby 		struct tty_driver *driver, unsigned index,
70057eb856SJiri Slaby 		struct device *device)
71057eb856SJiri Slaby {
722cb4ca02SJiri Slaby 	tty_port_link_device(port, driver, index);
73057eb856SJiri Slaby 	return tty_register_device(driver, index, device);
74057eb856SJiri Slaby }
75057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device);
76057eb856SJiri Slaby 
77b1b79916STomas Hlavacek /**
78b1b79916STomas Hlavacek  * tty_port_register_device_attr - register tty device
79b1b79916STomas Hlavacek  * @port: tty_port of the device
80b1b79916STomas Hlavacek  * @driver: tty_driver for this device
81b1b79916STomas Hlavacek  * @index: index of the tty
82b1b79916STomas Hlavacek  * @device: parent if exists, otherwise NULL
83b1b79916STomas Hlavacek  * @drvdata: Driver data to be set to device.
84b1b79916STomas Hlavacek  * @attr_grp: Attribute group to be set on device.
85b1b79916STomas Hlavacek  *
86b1b79916STomas Hlavacek  * It is the same as tty_register_device_attr except the provided @port is
87b1b79916STomas Hlavacek  * linked to a concrete tty specified by @index. Use this or tty_port_install
88b1b79916STomas Hlavacek  * (or both). Call tty_port_link_device as a last resort.
89b1b79916STomas Hlavacek  */
90b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port,
91b1b79916STomas Hlavacek 		struct tty_driver *driver, unsigned index,
92b1b79916STomas Hlavacek 		struct device *device, void *drvdata,
93b1b79916STomas Hlavacek 		const struct attribute_group **attr_grp)
94b1b79916STomas Hlavacek {
95b1b79916STomas Hlavacek 	tty_port_link_device(port, driver, index);
96b1b79916STomas Hlavacek 	return tty_register_device_attr(driver, index, device, drvdata,
97b1b79916STomas Hlavacek 			attr_grp);
98b1b79916STomas Hlavacek }
99b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
100b1b79916STomas Hlavacek 
10196fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
10296fd7ce5SGreg Kroah-Hartman {
10396fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
10496fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
10596fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
10696fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
10796fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
10896fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
10996fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
11096fd7ce5SGreg Kroah-Hartman 	return 0;
11196fd7ce5SGreg Kroah-Hartman }
11296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
11396fd7ce5SGreg Kroah-Hartman 
11496fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
11596fd7ce5SGreg Kroah-Hartman {
11696fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
11796fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
11896fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
11996fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
12096fd7ce5SGreg Kroah-Hartman 	}
12196fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
12296fd7ce5SGreg Kroah-Hartman }
12396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
12496fd7ce5SGreg Kroah-Hartman 
125*de274bfeSJiri Slaby /**
126*de274bfeSJiri Slaby  * tty_port_destroy -- destroy inited port
127*de274bfeSJiri Slaby  * @port: tty port to be doestroyed
128*de274bfeSJiri Slaby  *
129*de274bfeSJiri Slaby  * When a port was initialized using tty_port_init, one has to destroy the
130*de274bfeSJiri Slaby  * port by this function. Either indirectly by using tty_port refcounting
131*de274bfeSJiri Slaby  * (tty_port_put) or directly if refcounting is not used.
132*de274bfeSJiri Slaby  */
133*de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port)
134*de274bfeSJiri Slaby {
135*de274bfeSJiri Slaby 	tty_buffer_free_all(port);
136*de274bfeSJiri Slaby }
137*de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy);
138*de274bfeSJiri Slaby 
13996fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
14096fd7ce5SGreg Kroah-Hartman {
14196fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
14296fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
14396fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
144*de274bfeSJiri Slaby 	tty_port_destroy(port);
14581c79838SJiri Slaby 	if (port->ops && port->ops->destruct)
14696fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
14796fd7ce5SGreg Kroah-Hartman 	else
14896fd7ce5SGreg Kroah-Hartman 		kfree(port);
14996fd7ce5SGreg Kroah-Hartman }
15096fd7ce5SGreg Kroah-Hartman 
15196fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
15296fd7ce5SGreg Kroah-Hartman {
15396fd7ce5SGreg Kroah-Hartman 	if (port)
15496fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
15596fd7ce5SGreg Kroah-Hartman }
15696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
15796fd7ce5SGreg Kroah-Hartman 
15896fd7ce5SGreg Kroah-Hartman /**
15996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_get	-	get a tty reference
16096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
16196fd7ce5SGreg Kroah-Hartman  *
16296fd7ce5SGreg Kroah-Hartman  *	Return a refcount protected tty instance or NULL if the port is not
16396fd7ce5SGreg Kroah-Hartman  *	associated with a tty (eg due to close or hangup)
16496fd7ce5SGreg Kroah-Hartman  */
16596fd7ce5SGreg Kroah-Hartman 
16696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
16796fd7ce5SGreg Kroah-Hartman {
16896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
16996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
17096fd7ce5SGreg Kroah-Hartman 
17196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
17296fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
17396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
17496fd7ce5SGreg Kroah-Hartman 	return tty;
17596fd7ce5SGreg Kroah-Hartman }
17696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
17796fd7ce5SGreg Kroah-Hartman 
17896fd7ce5SGreg Kroah-Hartman /**
17996fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_set	-	set the tty of a port
18096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
18196fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
18296fd7ce5SGreg Kroah-Hartman  *
18396fd7ce5SGreg Kroah-Hartman  *	Associate the port and tty pair. Manages any internal refcounts.
18496fd7ce5SGreg Kroah-Hartman  *	Pass NULL to deassociate a port
18596fd7ce5SGreg Kroah-Hartman  */
18696fd7ce5SGreg Kroah-Hartman 
18796fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
18896fd7ce5SGreg Kroah-Hartman {
18996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
19096fd7ce5SGreg Kroah-Hartman 
19196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
19296fd7ce5SGreg Kroah-Hartman 	if (port->tty)
19396fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
19496fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
19596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
19696fd7ce5SGreg Kroah-Hartman }
19796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
19896fd7ce5SGreg Kroah-Hartman 
19996fd7ce5SGreg Kroah-Hartman static void tty_port_shutdown(struct tty_port *port)
20096fd7ce5SGreg Kroah-Hartman {
20196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
20296fd7ce5SGreg Kroah-Hartman 	if (port->ops->shutdown && !port->console &&
20396fd7ce5SGreg Kroah-Hartman 		test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
20496fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
20596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
20696fd7ce5SGreg Kroah-Hartman }
20796fd7ce5SGreg Kroah-Hartman 
20896fd7ce5SGreg Kroah-Hartman /**
20996fd7ce5SGreg Kroah-Hartman  *	tty_port_hangup		-	hangup helper
21096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
21196fd7ce5SGreg Kroah-Hartman  *
21296fd7ce5SGreg Kroah-Hartman  *	Perform port level tty hangup flag and count changes. Drop the tty
21396fd7ce5SGreg Kroah-Hartman  *	reference.
21496fd7ce5SGreg Kroah-Hartman  */
21596fd7ce5SGreg Kroah-Hartman 
21696fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
21796fd7ce5SGreg Kroah-Hartman {
21896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
21996fd7ce5SGreg Kroah-Hartman 
22096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
22196fd7ce5SGreg Kroah-Hartman 	port->count = 0;
22296fd7ce5SGreg Kroah-Hartman 	port->flags &= ~ASYNC_NORMAL_ACTIVE;
22396fd7ce5SGreg Kroah-Hartman 	if (port->tty) {
22496fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &port->tty->flags);
22596fd7ce5SGreg Kroah-Hartman 		tty_kref_put(port->tty);
22696fd7ce5SGreg Kroah-Hartman 	}
22796fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
22896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
22996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
23096fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
23196fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
23296fd7ce5SGreg Kroah-Hartman }
23396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
23496fd7ce5SGreg Kroah-Hartman 
23596fd7ce5SGreg Kroah-Hartman /**
23696fd7ce5SGreg Kroah-Hartman  *	tty_port_carrier_raised	-	carrier raised check
23796fd7ce5SGreg Kroah-Hartman  *	@port: tty port
23896fd7ce5SGreg Kroah-Hartman  *
23996fd7ce5SGreg Kroah-Hartman  *	Wrapper for the carrier detect logic. For the moment this is used
24096fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
24196fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
24296fd7ce5SGreg Kroah-Hartman  */
24396fd7ce5SGreg Kroah-Hartman 
24496fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
24596fd7ce5SGreg Kroah-Hartman {
24696fd7ce5SGreg Kroah-Hartman 	if (port->ops->carrier_raised == NULL)
24796fd7ce5SGreg Kroah-Hartman 		return 1;
24896fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
24996fd7ce5SGreg Kroah-Hartman }
25096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
25196fd7ce5SGreg Kroah-Hartman 
25296fd7ce5SGreg Kroah-Hartman /**
25396fd7ce5SGreg Kroah-Hartman  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
25496fd7ce5SGreg Kroah-Hartman  *	@port: tty port
25596fd7ce5SGreg Kroah-Hartman  *
25696fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
25796fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
25896fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
25996fd7ce5SGreg Kroah-Hartman  */
26096fd7ce5SGreg Kroah-Hartman 
26196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
26296fd7ce5SGreg Kroah-Hartman {
26396fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
26496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
26596fd7ce5SGreg Kroah-Hartman }
26696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
26796fd7ce5SGreg Kroah-Hartman 
26896fd7ce5SGreg Kroah-Hartman /**
26996fd7ce5SGreg Kroah-Hartman  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
27096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
27196fd7ce5SGreg Kroah-Hartman  *
27296fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
27396fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
27496fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
27596fd7ce5SGreg Kroah-Hartman  */
27696fd7ce5SGreg Kroah-Hartman 
27796fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
27896fd7ce5SGreg Kroah-Hartman {
27996fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
28096fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
28196fd7ce5SGreg Kroah-Hartman }
28296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
28396fd7ce5SGreg Kroah-Hartman 
28496fd7ce5SGreg Kroah-Hartman /**
28596fd7ce5SGreg Kroah-Hartman  *	tty_port_block_til_ready	-	Waiting logic for tty open
28696fd7ce5SGreg Kroah-Hartman  *	@port: the tty port being opened
28796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty device being bound
28896fd7ce5SGreg Kroah-Hartman  *	@filp: the file pointer of the opener
28996fd7ce5SGreg Kroah-Hartman  *
29096fd7ce5SGreg Kroah-Hartman  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
29196fd7ce5SGreg Kroah-Hartman  *	Handles:
29296fd7ce5SGreg Kroah-Hartman  *		- hangup (both before and during)
29396fd7ce5SGreg Kroah-Hartman  *		- non blocking open
29496fd7ce5SGreg Kroah-Hartman  *		- rts/dtr/dcd
29596fd7ce5SGreg Kroah-Hartman  *		- signals
29696fd7ce5SGreg Kroah-Hartman  *		- port flags and counts
29796fd7ce5SGreg Kroah-Hartman  *
29896fd7ce5SGreg Kroah-Hartman  *	The passed tty_port must implement the carrier_raised method if it can
29996fd7ce5SGreg Kroah-Hartman  *	do carrier detect and the dtr_rts method if it supports software
30096fd7ce5SGreg Kroah-Hartman  *	management of these lines. Note that the dtr/rts raise is done each
30196fd7ce5SGreg Kroah-Hartman  *	iteration as a hangup may have previously dropped them while we wait.
30296fd7ce5SGreg Kroah-Hartman  */
30396fd7ce5SGreg Kroah-Hartman 
30496fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
30596fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
30696fd7ce5SGreg Kroah-Hartman {
30796fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
30896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
30996fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
31096fd7ce5SGreg Kroah-Hartman 
31196fd7ce5SGreg Kroah-Hartman 	/* block if port is in the process of being closed */
31296fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
31389c8d91eSAlan Cox 		wait_event_interruptible_tty(tty, port->close_wait,
31496fd7ce5SGreg Kroah-Hartman 				!(port->flags & ASYNC_CLOSING));
31596fd7ce5SGreg Kroah-Hartman 		if (port->flags & ASYNC_HUP_NOTIFY)
31696fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
31796fd7ce5SGreg Kroah-Hartman 		else
31896fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
31996fd7ce5SGreg Kroah-Hartman 	}
32096fd7ce5SGreg Kroah-Hartman 
32196fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
32296fd7ce5SGreg Kroah-Hartman 	   the port has just hung up or is in another error state */
32396fd7ce5SGreg Kroah-Hartman 	if (tty->flags & (1 << TTY_IO_ERROR)) {
32496fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
32596fd7ce5SGreg Kroah-Hartman 		return 0;
32696fd7ce5SGreg Kroah-Hartman 	}
32796fd7ce5SGreg Kroah-Hartman 	if (filp->f_flags & O_NONBLOCK) {
32896fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
329adc8d746SAlan Cox 		if (tty->termios.c_cflag & CBAUD)
33096fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
33196fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
33296fd7ce5SGreg Kroah-Hartman 		return 0;
33396fd7ce5SGreg Kroah-Hartman 	}
33496fd7ce5SGreg Kroah-Hartman 
33596fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
33696fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
33796fd7ce5SGreg Kroah-Hartman 
33896fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
33996fd7ce5SGreg Kroah-Hartman 	   carrier, but we must also wait for any close that is in progress
34096fd7ce5SGreg Kroah-Hartman 	   before the next open may complete */
34196fd7ce5SGreg Kroah-Hartman 
34296fd7ce5SGreg Kroah-Hartman 	retval = 0;
34396fd7ce5SGreg Kroah-Hartman 
34496fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
34596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
34696fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
34796fd7ce5SGreg Kroah-Hartman 		port->count--;
34896fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
34996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
35096fd7ce5SGreg Kroah-Hartman 
35196fd7ce5SGreg Kroah-Hartman 	while (1) {
35296fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
353adc8d746SAlan Cox 		if (tty->termios.c_cflag & CBAUD)
35496fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
35596fd7ce5SGreg Kroah-Hartman 
35696fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
35796fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
35896fd7ce5SGreg Kroah-Hartman 							Return accordingly */
35996fd7ce5SGreg Kroah-Hartman 		if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
36096fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
36196fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
36296fd7ce5SGreg Kroah-Hartman 			else
36396fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
36496fd7ce5SGreg Kroah-Hartman 			break;
36596fd7ce5SGreg Kroah-Hartman 		}
3660eee50afSJiri Slaby 		/*
3670eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
3680eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
3690eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
3700eee50afSJiri Slaby 		 * on some hardware.
3710eee50afSJiri Slaby 		 */
37296fd7ce5SGreg Kroah-Hartman 		if (!(port->flags & ASYNC_CLOSING) &&
3730eee50afSJiri Slaby 				(do_clocal || tty_port_carrier_raised(port)))
37496fd7ce5SGreg Kroah-Hartman 			break;
37596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
37696fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
37796fd7ce5SGreg Kroah-Hartman 			break;
37896fd7ce5SGreg Kroah-Hartman 		}
37989c8d91eSAlan Cox 		tty_unlock(tty);
38096fd7ce5SGreg Kroah-Hartman 		schedule();
38189c8d91eSAlan Cox 		tty_lock(tty);
38296fd7ce5SGreg Kroah-Hartman 	}
38396fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
38496fd7ce5SGreg Kroah-Hartman 
38596fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
38696fd7ce5SGreg Kroah-Hartman 	   we must not mess that up further */
38796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
38896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
38996fd7ce5SGreg Kroah-Hartman 		port->count++;
39096fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
39196fd7ce5SGreg Kroah-Hartman 	if (retval == 0)
39296fd7ce5SGreg Kroah-Hartman 		port->flags |= ASYNC_NORMAL_ACTIVE;
39396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
39496fd7ce5SGreg Kroah-Hartman 	return retval;
39596fd7ce5SGreg Kroah-Hartman }
39696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
39796fd7ce5SGreg Kroah-Hartman 
39896fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
39996fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
40096fd7ce5SGreg Kroah-Hartman {
40196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
40296fd7ce5SGreg Kroah-Hartman 
40396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
40496fd7ce5SGreg Kroah-Hartman 	if (tty_hung_up_p(filp)) {
40596fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
40696fd7ce5SGreg Kroah-Hartman 		return 0;
40796fd7ce5SGreg Kroah-Hartman 	}
40896fd7ce5SGreg Kroah-Hartman 
40996fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
41096fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING
41196fd7ce5SGreg Kroah-Hartman 		    "tty_port_close_start: tty->count = 1 port count = %d.\n",
41296fd7ce5SGreg Kroah-Hartman 								port->count);
41396fd7ce5SGreg Kroah-Hartman 		port->count = 1;
41496fd7ce5SGreg Kroah-Hartman 	}
41596fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
41696fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING "tty_port_close_start: count = %d\n",
41796fd7ce5SGreg Kroah-Hartman 								port->count);
41896fd7ce5SGreg Kroah-Hartman 		port->count = 0;
41996fd7ce5SGreg Kroah-Hartman 	}
42096fd7ce5SGreg Kroah-Hartman 
42196fd7ce5SGreg Kroah-Hartman 	if (port->count) {
42296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
42396fd7ce5SGreg Kroah-Hartman 		if (port->ops->drop)
42496fd7ce5SGreg Kroah-Hartman 			port->ops->drop(port);
42596fd7ce5SGreg Kroah-Hartman 		return 0;
42696fd7ce5SGreg Kroah-Hartman 	}
42796fd7ce5SGreg Kroah-Hartman 	set_bit(ASYNCB_CLOSING, &port->flags);
42896fd7ce5SGreg Kroah-Hartman 	tty->closing = 1;
42996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
43096fd7ce5SGreg Kroah-Hartman 	/* Don't block on a stalled port, just pull the chain */
43196fd7ce5SGreg Kroah-Hartman 	if (tty->flow_stopped)
43296fd7ce5SGreg Kroah-Hartman 		tty_driver_flush_buffer(tty);
43396fd7ce5SGreg Kroah-Hartman 	if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
43496fd7ce5SGreg Kroah-Hartman 			port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
435424cc039SJiri Slaby 		tty_wait_until_sent_from_close(tty, port->closing_wait);
43696fd7ce5SGreg Kroah-Hartman 	if (port->drain_delay) {
43796fd7ce5SGreg Kroah-Hartman 		unsigned int bps = tty_get_baud_rate(tty);
43896fd7ce5SGreg Kroah-Hartman 		long timeout;
43996fd7ce5SGreg Kroah-Hartman 
44096fd7ce5SGreg Kroah-Hartman 		if (bps > 1200)
44196fd7ce5SGreg Kroah-Hartman 			timeout = max_t(long,
44296fd7ce5SGreg Kroah-Hartman 				(HZ * 10 * port->drain_delay) / bps, HZ / 10);
44396fd7ce5SGreg Kroah-Hartman 		else
44496fd7ce5SGreg Kroah-Hartman 			timeout = 2 * HZ;
44596fd7ce5SGreg Kroah-Hartman 		schedule_timeout_interruptible(timeout);
44696fd7ce5SGreg Kroah-Hartman 	}
44796fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
44896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
44996fd7ce5SGreg Kroah-Hartman 
45096fd7ce5SGreg Kroah-Hartman 	/* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
45196fd7ce5SGreg Kroah-Hartman 	   hang up the line */
452adc8d746SAlan Cox 	if (tty->termios.c_cflag & HUPCL)
45396fd7ce5SGreg Kroah-Hartman 		tty_port_lower_dtr_rts(port);
45496fd7ce5SGreg Kroah-Hartman 
45596fd7ce5SGreg Kroah-Hartman 	/* Don't call port->drop for the last reference. Callers will want
45696fd7ce5SGreg Kroah-Hartman 	   to drop the last active reference in ->shutdown() or the tty
45796fd7ce5SGreg Kroah-Hartman 	   shutdown path */
45896fd7ce5SGreg Kroah-Hartman 	return 1;
45996fd7ce5SGreg Kroah-Hartman }
46096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
46196fd7ce5SGreg Kroah-Hartman 
46296fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
46396fd7ce5SGreg Kroah-Hartman {
46496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
46596fd7ce5SGreg Kroah-Hartman 
46696fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
46796fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
46896fd7ce5SGreg Kroah-Hartman 
46996fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
47096fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
47196fd7ce5SGreg Kroah-Hartman 		if (port->close_delay) {
47296fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(
47396fd7ce5SGreg Kroah-Hartman 				jiffies_to_msecs(port->close_delay));
47496fd7ce5SGreg Kroah-Hartman 		}
47596fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
47696fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
47796fd7ce5SGreg Kroah-Hartman 	}
47896fd7ce5SGreg Kroah-Hartman 	port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
47996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->close_wait);
48096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
48196fd7ce5SGreg Kroah-Hartman }
48296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
48396fd7ce5SGreg Kroah-Hartman 
48496fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
48596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
48696fd7ce5SGreg Kroah-Hartman {
48796fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
48896fd7ce5SGreg Kroah-Hartman 		return;
48996fd7ce5SGreg Kroah-Hartman 	tty_port_shutdown(port);
49096fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_IO_ERROR, &tty->flags);
49196fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
49296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
49396fd7ce5SGreg Kroah-Hartman }
49496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
49596fd7ce5SGreg Kroah-Hartman 
49672a33bf5SJiri Slaby /**
49772a33bf5SJiri Slaby  * tty_port_install - generic tty->ops->install handler
49872a33bf5SJiri Slaby  * @port: tty_port of the device
49972a33bf5SJiri Slaby  * @driver: tty_driver for this device
50072a33bf5SJiri Slaby  * @tty: tty to be installed
50172a33bf5SJiri Slaby  *
50272a33bf5SJiri Slaby  * It is the same as tty_standard_install except the provided @port is linked
50372a33bf5SJiri Slaby  * to a concrete tty specified by @tty. Use this or tty_port_register_device
50472a33bf5SJiri Slaby  * (or both). Call tty_port_link_device as a last resort.
50572a33bf5SJiri Slaby  */
506695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
507695586caSJiri Slaby 		struct tty_struct *tty)
508695586caSJiri Slaby {
509695586caSJiri Slaby 	tty->port = port;
510695586caSJiri Slaby 	return tty_standard_install(driver, tty);
511695586caSJiri Slaby }
512695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
513695586caSJiri Slaby 
51496fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
51596fd7ce5SGreg Kroah-Hartman 							struct file *filp)
51696fd7ce5SGreg Kroah-Hartman {
51796fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
51896fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
51996fd7ce5SGreg Kroah-Hartman 		++port->count;
52096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
52196fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
52296fd7ce5SGreg Kroah-Hartman 
52396fd7ce5SGreg Kroah-Hartman 	/*
52496fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
52596fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
52696fd7ce5SGreg Kroah-Hartman 	 * port mutex.
52796fd7ce5SGreg Kroah-Hartman 	 */
52896fd7ce5SGreg Kroah-Hartman 
52996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
53096fd7ce5SGreg Kroah-Hartman 
53196fd7ce5SGreg Kroah-Hartman 	if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
53296fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
53396fd7ce5SGreg Kroah-Hartman 		if (port->ops->activate) {
53496fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
53596fd7ce5SGreg Kroah-Hartman 			if (retval) {
53696fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
53796fd7ce5SGreg Kroah-Hartman 				return retval;
53896fd7ce5SGreg Kroah-Hartman 			}
53996fd7ce5SGreg Kroah-Hartman 		}
54096fd7ce5SGreg Kroah-Hartman 		set_bit(ASYNCB_INITIALIZED, &port->flags);
54196fd7ce5SGreg Kroah-Hartman 	}
54296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
54396fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
54496fd7ce5SGreg Kroah-Hartman }
54596fd7ce5SGreg Kroah-Hartman 
54696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
547