xref: /openbmc/linux/drivers/tty/tty_port.c (revision ed3f0af8)
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/wait.h>
1696fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
1796fd7ce5SGreg Kroah-Hartman #include <linux/delay.h>
1896fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
1996fd7ce5SGreg Kroah-Hartman 
2096fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port)
2196fd7ce5SGreg Kroah-Hartman {
2296fd7ce5SGreg Kroah-Hartman 	memset(port, 0, sizeof(*port));
23ecbbfd44SJiri Slaby 	tty_buffer_init(port);
2496fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->open_wait);
2596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->delta_msr_wait);
2696fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->mutex);
2796fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->buf_mutex);
2896fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&port->lock);
2996fd7ce5SGreg Kroah-Hartman 	port->close_delay = (50 * HZ) / 100;
3096fd7ce5SGreg Kroah-Hartman 	port->closing_wait = (3000 * HZ) / 100;
3196fd7ce5SGreg Kroah-Hartman 	kref_init(&port->kref);
3296fd7ce5SGreg Kroah-Hartman }
3396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init);
3496fd7ce5SGreg Kroah-Hartman 
3572a33bf5SJiri Slaby /**
362cb4ca02SJiri Slaby  * tty_port_link_device - link tty and tty_port
372cb4ca02SJiri Slaby  * @port: tty_port of the device
382cb4ca02SJiri Slaby  * @driver: tty_driver for this device
392cb4ca02SJiri Slaby  * @index: index of the tty
402cb4ca02SJiri Slaby  *
412cb4ca02SJiri Slaby  * Provide the tty layer wit ha link from a tty (specified by @index) to a
422cb4ca02SJiri Slaby  * tty_port (@port). Use this only if neither tty_port_register_device nor
432cb4ca02SJiri Slaby  * tty_port_install is used in the driver. If used, this has to be called before
442cb4ca02SJiri Slaby  * tty_register_driver.
452cb4ca02SJiri Slaby  */
462cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port,
472cb4ca02SJiri Slaby 		struct tty_driver *driver, unsigned index)
482cb4ca02SJiri Slaby {
492cb4ca02SJiri Slaby 	if (WARN_ON(index >= driver->num))
502cb4ca02SJiri Slaby 		return;
512cb4ca02SJiri Slaby 	driver->ports[index] = port;
522cb4ca02SJiri Slaby }
532cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device);
542cb4ca02SJiri Slaby 
552cb4ca02SJiri Slaby /**
5672a33bf5SJiri Slaby  * tty_port_register_device - register tty device
5772a33bf5SJiri Slaby  * @port: tty_port of the device
5872a33bf5SJiri Slaby  * @driver: tty_driver for this device
5972a33bf5SJiri Slaby  * @index: index of the tty
6072a33bf5SJiri Slaby  * @device: parent if exists, otherwise NULL
6172a33bf5SJiri Slaby  *
6272a33bf5SJiri Slaby  * It is the same as tty_register_device except the provided @port is linked to
6372a33bf5SJiri Slaby  * a concrete tty specified by @index. Use this or tty_port_install (or both).
6472a33bf5SJiri Slaby  * Call tty_port_link_device as a last resort.
6572a33bf5SJiri Slaby  */
66057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port,
67057eb856SJiri Slaby 		struct tty_driver *driver, unsigned index,
68057eb856SJiri Slaby 		struct device *device)
69057eb856SJiri Slaby {
702cb4ca02SJiri Slaby 	tty_port_link_device(port, driver, index);
71057eb856SJiri Slaby 	return tty_register_device(driver, index, device);
72057eb856SJiri Slaby }
73057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device);
74057eb856SJiri Slaby 
75b1b79916STomas Hlavacek /**
76b1b79916STomas Hlavacek  * tty_port_register_device_attr - register tty device
77b1b79916STomas Hlavacek  * @port: tty_port of the device
78b1b79916STomas Hlavacek  * @driver: tty_driver for this device
79b1b79916STomas Hlavacek  * @index: index of the tty
80b1b79916STomas Hlavacek  * @device: parent if exists, otherwise NULL
81b1b79916STomas Hlavacek  * @drvdata: Driver data to be set to device.
82b1b79916STomas Hlavacek  * @attr_grp: Attribute group to be set on device.
83b1b79916STomas Hlavacek  *
84b1b79916STomas Hlavacek  * It is the same as tty_register_device_attr except the provided @port is
85b1b79916STomas Hlavacek  * linked to a concrete tty specified by @index. Use this or tty_port_install
86b1b79916STomas Hlavacek  * (or both). Call tty_port_link_device as a last resort.
87b1b79916STomas Hlavacek  */
88b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port,
89b1b79916STomas Hlavacek 		struct tty_driver *driver, unsigned index,
90b1b79916STomas Hlavacek 		struct device *device, void *drvdata,
91b1b79916STomas Hlavacek 		const struct attribute_group **attr_grp)
92b1b79916STomas Hlavacek {
93b1b79916STomas Hlavacek 	tty_port_link_device(port, driver, index);
94b1b79916STomas Hlavacek 	return tty_register_device_attr(driver, index, device, drvdata,
95b1b79916STomas Hlavacek 			attr_grp);
96b1b79916STomas Hlavacek }
97b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
98b1b79916STomas Hlavacek 
9996fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
10096fd7ce5SGreg Kroah-Hartman {
10196fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
10296fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
10396fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
10496fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
10596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
10696fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
10796fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
10896fd7ce5SGreg Kroah-Hartman 	return 0;
10996fd7ce5SGreg Kroah-Hartman }
11096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
11196fd7ce5SGreg Kroah-Hartman 
11296fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
11396fd7ce5SGreg Kroah-Hartman {
11496fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
11596fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
11696fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
11796fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
11896fd7ce5SGreg Kroah-Hartman 	}
11996fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
12096fd7ce5SGreg Kroah-Hartman }
12196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
12296fd7ce5SGreg Kroah-Hartman 
123de274bfeSJiri Slaby /**
124de274bfeSJiri Slaby  * tty_port_destroy -- destroy inited port
125de274bfeSJiri Slaby  * @port: tty port to be doestroyed
126de274bfeSJiri Slaby  *
127de274bfeSJiri Slaby  * When a port was initialized using tty_port_init, one has to destroy the
128de274bfeSJiri Slaby  * port by this function. Either indirectly by using tty_port refcounting
129de274bfeSJiri Slaby  * (tty_port_put) or directly if refcounting is not used.
130de274bfeSJiri Slaby  */
131de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port)
132de274bfeSJiri Slaby {
133e176058fSPeter Hurley 	tty_buffer_cancel_work(port);
134de274bfeSJiri Slaby 	tty_buffer_free_all(port);
135de274bfeSJiri Slaby }
136de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy);
137de274bfeSJiri Slaby 
13896fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
13996fd7ce5SGreg Kroah-Hartman {
14096fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
141e3bfea23SPeter Hurley 
142e3bfea23SPeter Hurley 	/* check if last port ref was dropped before tty release */
143e3bfea23SPeter Hurley 	if (WARN_ON(port->itty))
144e3bfea23SPeter Hurley 		return;
14596fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
14696fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
147de274bfeSJiri Slaby 	tty_port_destroy(port);
14881c79838SJiri Slaby 	if (port->ops && port->ops->destruct)
14996fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
15096fd7ce5SGreg Kroah-Hartman 	else
15196fd7ce5SGreg Kroah-Hartman 		kfree(port);
15296fd7ce5SGreg Kroah-Hartman }
15396fd7ce5SGreg Kroah-Hartman 
15496fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
15596fd7ce5SGreg Kroah-Hartman {
15696fd7ce5SGreg Kroah-Hartman 	if (port)
15796fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
15896fd7ce5SGreg Kroah-Hartman }
15996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
16096fd7ce5SGreg Kroah-Hartman 
16196fd7ce5SGreg Kroah-Hartman /**
16296fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_get	-	get a tty reference
16396fd7ce5SGreg Kroah-Hartman  *	@port: tty port
16496fd7ce5SGreg Kroah-Hartman  *
16596fd7ce5SGreg Kroah-Hartman  *	Return a refcount protected tty instance or NULL if the port is not
16696fd7ce5SGreg Kroah-Hartman  *	associated with a tty (eg due to close or hangup)
16796fd7ce5SGreg Kroah-Hartman  */
16896fd7ce5SGreg Kroah-Hartman 
16996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
17096fd7ce5SGreg Kroah-Hartman {
17196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
17296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
17396fd7ce5SGreg Kroah-Hartman 
17496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
17596fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
17696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
17796fd7ce5SGreg Kroah-Hartman 	return tty;
17896fd7ce5SGreg Kroah-Hartman }
17996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
18096fd7ce5SGreg Kroah-Hartman 
18196fd7ce5SGreg Kroah-Hartman /**
18296fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_set	-	set the tty of a port
18396fd7ce5SGreg Kroah-Hartman  *	@port: tty port
18496fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
18596fd7ce5SGreg Kroah-Hartman  *
18696fd7ce5SGreg Kroah-Hartman  *	Associate the port and tty pair. Manages any internal refcounts.
18796fd7ce5SGreg Kroah-Hartman  *	Pass NULL to deassociate a port
18896fd7ce5SGreg Kroah-Hartman  */
18996fd7ce5SGreg Kroah-Hartman 
19096fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
19196fd7ce5SGreg Kroah-Hartman {
19296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
19396fd7ce5SGreg Kroah-Hartman 
19496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
19596fd7ce5SGreg Kroah-Hartman 	tty_kref_put(port->tty);
19696fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
19796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
19896fd7ce5SGreg Kroah-Hartman }
19996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
20096fd7ce5SGreg Kroah-Hartman 
201957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
20296fd7ce5SGreg Kroah-Hartman {
20396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
2048bde9658SJohan Hovold 	if (port->console)
2058bde9658SJohan Hovold 		goto out;
2068bde9658SJohan Hovold 
207d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
208d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
209957dacaeSJohan Hovold 		/*
210957dacaeSJohan Hovold 		 * Drop DTR/RTS if HUPCL is set. This causes any attached
211957dacaeSJohan Hovold 		 * modem to hang up the line.
212957dacaeSJohan Hovold 		 */
213957dacaeSJohan Hovold 		if (tty && C_HUPCL(tty))
214957dacaeSJohan Hovold 			tty_port_lower_dtr_rts(port);
215957dacaeSJohan Hovold 
2168bde9658SJohan Hovold 		if (port->ops->shutdown)
21796fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
2188bde9658SJohan Hovold 	}
2198bde9658SJohan Hovold out:
22096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
22196fd7ce5SGreg Kroah-Hartman }
22296fd7ce5SGreg Kroah-Hartman 
22396fd7ce5SGreg Kroah-Hartman /**
22496fd7ce5SGreg Kroah-Hartman  *	tty_port_hangup		-	hangup helper
22596fd7ce5SGreg Kroah-Hartman  *	@port: tty port
22696fd7ce5SGreg Kroah-Hartman  *
22796fd7ce5SGreg Kroah-Hartman  *	Perform port level tty hangup flag and count changes. Drop the tty
22896fd7ce5SGreg Kroah-Hartman  *	reference.
2299c9928bdSPeter Hurley  *
2309c9928bdSPeter Hurley  *	Caller holds tty lock.
23196fd7ce5SGreg Kroah-Hartman  */
23296fd7ce5SGreg Kroah-Hartman 
23396fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
23496fd7ce5SGreg Kroah-Hartman {
235957dacaeSJohan Hovold 	struct tty_struct *tty;
23696fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
23796fd7ce5SGreg Kroah-Hartman 
23896fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
23996fd7ce5SGreg Kroah-Hartman 	port->count = 0;
240957dacaeSJohan Hovold 	tty = port->tty;
241957dacaeSJohan Hovold 	if (tty)
242957dacaeSJohan Hovold 		set_bit(TTY_IO_ERROR, &tty->flags);
24396fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
24496fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
245807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
246957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
247957dacaeSJohan Hovold 	tty_kref_put(tty);
24896fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
24996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
25096fd7ce5SGreg Kroah-Hartman }
25196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
25296fd7ce5SGreg Kroah-Hartman 
25396fd7ce5SGreg Kroah-Hartman /**
254aa27a094SJiri Slaby  * tty_port_tty_hangup - helper to hang up a tty
255aa27a094SJiri Slaby  *
256aa27a094SJiri Slaby  * @port: tty port
257aa27a094SJiri Slaby  * @check_clocal: hang only ttys with CLOCAL unset?
258aa27a094SJiri Slaby  */
259aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
260aa27a094SJiri Slaby {
261aa27a094SJiri Slaby 	struct tty_struct *tty = tty_port_tty_get(port);
262aa27a094SJiri Slaby 
2631d9e689cSGianluca Anzolin 	if (tty && (!check_clocal || !C_CLOCAL(tty)))
264aa27a094SJiri Slaby 		tty_hangup(tty);
265aa27a094SJiri Slaby 	tty_kref_put(tty);
266aa27a094SJiri Slaby }
267aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
268aa27a094SJiri Slaby 
269aa27a094SJiri Slaby /**
2706aad04f2SJiri Slaby  * tty_port_tty_wakeup - helper to wake up a tty
2716aad04f2SJiri Slaby  *
2726aad04f2SJiri Slaby  * @port: tty port
2736aad04f2SJiri Slaby  */
2746aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port)
2756aad04f2SJiri Slaby {
2766aad04f2SJiri Slaby 	struct tty_struct *tty = tty_port_tty_get(port);
2776aad04f2SJiri Slaby 
2786aad04f2SJiri Slaby 	if (tty) {
2796aad04f2SJiri Slaby 		tty_wakeup(tty);
2806aad04f2SJiri Slaby 		tty_kref_put(tty);
2816aad04f2SJiri Slaby 	}
2826aad04f2SJiri Slaby }
2836aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
2846aad04f2SJiri Slaby 
2856aad04f2SJiri Slaby /**
28696fd7ce5SGreg Kroah-Hartman  *	tty_port_carrier_raised	-	carrier raised check
28796fd7ce5SGreg Kroah-Hartman  *	@port: tty port
28896fd7ce5SGreg Kroah-Hartman  *
28996fd7ce5SGreg Kroah-Hartman  *	Wrapper for the carrier detect logic. For the moment this is used
29096fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
29196fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
29296fd7ce5SGreg Kroah-Hartman  */
29396fd7ce5SGreg Kroah-Hartman 
29496fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
29596fd7ce5SGreg Kroah-Hartman {
29696fd7ce5SGreg Kroah-Hartman 	if (port->ops->carrier_raised == NULL)
29796fd7ce5SGreg Kroah-Hartman 		return 1;
29896fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
29996fd7ce5SGreg Kroah-Hartman }
30096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
30196fd7ce5SGreg Kroah-Hartman 
30296fd7ce5SGreg Kroah-Hartman /**
30396fd7ce5SGreg Kroah-Hartman  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
30496fd7ce5SGreg Kroah-Hartman  *	@port: tty port
30596fd7ce5SGreg Kroah-Hartman  *
30696fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
30796fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
30896fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
30996fd7ce5SGreg Kroah-Hartman  */
31096fd7ce5SGreg Kroah-Hartman 
31196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
31296fd7ce5SGreg Kroah-Hartman {
31396fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
31496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
31596fd7ce5SGreg Kroah-Hartman }
31696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
31796fd7ce5SGreg Kroah-Hartman 
31896fd7ce5SGreg Kroah-Hartman /**
31996fd7ce5SGreg Kroah-Hartman  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
32096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
32196fd7ce5SGreg Kroah-Hartman  *
32296fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
32396fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
32496fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
32596fd7ce5SGreg Kroah-Hartman  */
32696fd7ce5SGreg Kroah-Hartman 
32796fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
32896fd7ce5SGreg Kroah-Hartman {
32996fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
33096fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
33196fd7ce5SGreg Kroah-Hartman }
33296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
33396fd7ce5SGreg Kroah-Hartman 
33496fd7ce5SGreg Kroah-Hartman /**
33596fd7ce5SGreg Kroah-Hartman  *	tty_port_block_til_ready	-	Waiting logic for tty open
33696fd7ce5SGreg Kroah-Hartman  *	@port: the tty port being opened
33796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty device being bound
338ed3f0af8SAlan Cox  *	@filp: the file pointer of the opener or NULL
33996fd7ce5SGreg Kroah-Hartman  *
34096fd7ce5SGreg Kroah-Hartman  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
34196fd7ce5SGreg Kroah-Hartman  *	Handles:
34296fd7ce5SGreg Kroah-Hartman  *		- hangup (both before and during)
34396fd7ce5SGreg Kroah-Hartman  *		- non blocking open
34496fd7ce5SGreg Kroah-Hartman  *		- rts/dtr/dcd
34596fd7ce5SGreg Kroah-Hartman  *		- signals
34696fd7ce5SGreg Kroah-Hartman  *		- port flags and counts
34796fd7ce5SGreg Kroah-Hartman  *
34896fd7ce5SGreg Kroah-Hartman  *	The passed tty_port must implement the carrier_raised method if it can
34996fd7ce5SGreg Kroah-Hartman  *	do carrier detect and the dtr_rts method if it supports software
35096fd7ce5SGreg Kroah-Hartman  *	management of these lines. Note that the dtr/rts raise is done each
35196fd7ce5SGreg Kroah-Hartman  *	iteration as a hangup may have previously dropped them while we wait.
352c590f6b6SPeter Hurley  *
353c590f6b6SPeter Hurley  *	Caller holds tty lock.
354c590f6b6SPeter Hurley  *
355c590f6b6SPeter Hurley  *      NB: May drop and reacquire tty lock when blocking, so tty and tty_port
356c590f6b6SPeter Hurley  *      may have changed state (eg., may have been hung up).
35796fd7ce5SGreg Kroah-Hartman  */
35896fd7ce5SGreg Kroah-Hartman 
35996fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
36096fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
36196fd7ce5SGreg Kroah-Hartman {
36296fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
36396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
36496fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
36596fd7ce5SGreg Kroah-Hartman 
36696fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
36796fd7ce5SGreg Kroah-Hartman 	   the port has just hung up or is in another error state */
36818900ca6SPeter Hurley 	if (tty_io_error(tty)) {
369807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
37096fd7ce5SGreg Kroah-Hartman 		return 0;
37196fd7ce5SGreg Kroah-Hartman 	}
372ed3f0af8SAlan Cox 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
37396fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
3749db276f8SPeter Hurley 		if (C_BAUD(tty))
37596fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
376807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
37796fd7ce5SGreg Kroah-Hartman 		return 0;
37896fd7ce5SGreg Kroah-Hartman 	}
37996fd7ce5SGreg Kroah-Hartman 
38096fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
38196fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
38296fd7ce5SGreg Kroah-Hartman 
38396fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
38496fd7ce5SGreg Kroah-Hartman 	   carrier, but we must also wait for any close that is in progress
38596fd7ce5SGreg Kroah-Hartman 	   before the next open may complete */
38696fd7ce5SGreg Kroah-Hartman 
38796fd7ce5SGreg Kroah-Hartman 	retval = 0;
38896fd7ce5SGreg Kroah-Hartman 
38996fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
39096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
39196fd7ce5SGreg Kroah-Hartman 	port->count--;
39296fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
39396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
39496fd7ce5SGreg Kroah-Hartman 
39596fd7ce5SGreg Kroah-Hartman 	while (1) {
39696fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
397d41861caSPeter Hurley 		if (C_BAUD(tty) && tty_port_initialized(port))
39896fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
39996fd7ce5SGreg Kroah-Hartman 
40096fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
40196fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
40296fd7ce5SGreg Kroah-Hartman 							Return accordingly */
403d41861caSPeter Hurley 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
40496fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
40596fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
40696fd7ce5SGreg Kroah-Hartman 			else
40796fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
40896fd7ce5SGreg Kroah-Hartman 			break;
40996fd7ce5SGreg Kroah-Hartman 		}
4100eee50afSJiri Slaby 		/*
4110eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
4120eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
4130eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
4140eee50afSJiri Slaby 		 * on some hardware.
4150eee50afSJiri Slaby 		 */
416fef062cbSPeter Hurley 		if (do_clocal || tty_port_carrier_raised(port))
41796fd7ce5SGreg Kroah-Hartman 			break;
41896fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
41996fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
42096fd7ce5SGreg Kroah-Hartman 			break;
42196fd7ce5SGreg Kroah-Hartman 		}
42289c8d91eSAlan Cox 		tty_unlock(tty);
42396fd7ce5SGreg Kroah-Hartman 		schedule();
42489c8d91eSAlan Cox 		tty_lock(tty);
42596fd7ce5SGreg Kroah-Hartman 	}
42696fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
42796fd7ce5SGreg Kroah-Hartman 
42896fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
42996fd7ce5SGreg Kroah-Hartman 	   we must not mess that up further */
43096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
43196fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
43296fd7ce5SGreg Kroah-Hartman 		port->count++;
43396fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
43496fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
435807c8d81SPeter Hurley 	if (retval == 0)
436807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
43796fd7ce5SGreg Kroah-Hartman 	return retval;
43896fd7ce5SGreg Kroah-Hartman }
43996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
44096fd7ce5SGreg Kroah-Hartman 
441b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
442b74414f5SJohan Hovold {
443b74414f5SJohan Hovold 	unsigned int bps = tty_get_baud_rate(tty);
444b74414f5SJohan Hovold 	long timeout;
445b74414f5SJohan Hovold 
446b74414f5SJohan Hovold 	if (bps > 1200) {
447b74414f5SJohan Hovold 		timeout = (HZ * 10 * port->drain_delay) / bps;
448b74414f5SJohan Hovold 		timeout = max_t(long, timeout, HZ / 10);
449b74414f5SJohan Hovold 	} else {
450b74414f5SJohan Hovold 		timeout = 2 * HZ;
451b74414f5SJohan Hovold 	}
452b74414f5SJohan Hovold 	schedule_timeout_interruptible(timeout);
453b74414f5SJohan Hovold }
454b74414f5SJohan Hovold 
45579c1faa4SPeter Hurley /* Caller holds tty lock. */
45696fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
45796fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
45896fd7ce5SGreg Kroah-Hartman {
45996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
46096fd7ce5SGreg Kroah-Hartman 
461633caba8SPeter Hurley 	if (tty_hung_up_p(filp))
46296fd7ce5SGreg Kroah-Hartman 		return 0;
46396fd7ce5SGreg Kroah-Hartman 
464633caba8SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
46596fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
466339f36baSPeter Hurley 		tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
46796fd7ce5SGreg Kroah-Hartman 			 port->count);
46896fd7ce5SGreg Kroah-Hartman 		port->count = 1;
46996fd7ce5SGreg Kroah-Hartman 	}
47096fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
471339f36baSPeter Hurley 		tty_warn(tty, "%s: bad port count (%d)\n", __func__,
47296fd7ce5SGreg Kroah-Hartman 			 port->count);
47396fd7ce5SGreg Kroah-Hartman 		port->count = 0;
47496fd7ce5SGreg Kroah-Hartman 	}
47596fd7ce5SGreg Kroah-Hartman 
47696fd7ce5SGreg Kroah-Hartman 	if (port->count) {
47796fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
47896fd7ce5SGreg Kroah-Hartman 		return 0;
47996fd7ce5SGreg Kroah-Hartman 	}
48096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
4810b2588caSJohan Hovold 
482ddc7b758SPeter Hurley 	tty->closing = 1;
483ddc7b758SPeter Hurley 
484d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
48596fd7ce5SGreg Kroah-Hartman 		/* Don't block on a stalled port, just pull the chain */
48696fd7ce5SGreg Kroah-Hartman 		if (tty->flow_stopped)
48796fd7ce5SGreg Kroah-Hartman 			tty_driver_flush_buffer(tty);
4880b2588caSJohan Hovold 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
48979c1faa4SPeter Hurley 			tty_wait_until_sent(tty, port->closing_wait);
490b74414f5SJohan Hovold 		if (port->drain_delay)
491b74414f5SJohan Hovold 			tty_port_drain_delay(port, tty);
4920b2588caSJohan Hovold 	}
49396fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
49496fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
49596fd7ce5SGreg Kroah-Hartman 
496469d6d06SPeter Hurley 	/* Report to caller this is the last port reference */
49796fd7ce5SGreg Kroah-Hartman 	return 1;
49896fd7ce5SGreg Kroah-Hartman }
49996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
50096fd7ce5SGreg Kroah-Hartman 
5010733db91SPeter Hurley /* Caller holds tty lock */
50296fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
50396fd7ce5SGreg Kroah-Hartman {
50496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
50596fd7ce5SGreg Kroah-Hartman 
5063f40f5b2SPeter Hurley 	tty_ldisc_flush(tty);
50796fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
50896fd7ce5SGreg Kroah-Hartman 
509ddc7b758SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
510ddc7b758SPeter Hurley 
51196fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
51296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
5135823323eSPeter Hurley 		if (port->close_delay)
5145823323eSPeter Hurley 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
51596fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
51696fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
51796fd7ce5SGreg Kroah-Hartman 	}
51896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
519807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
52096fd7ce5SGreg Kroah-Hartman }
52196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
52296fd7ce5SGreg Kroah-Hartman 
5230733db91SPeter Hurley /**
5240733db91SPeter Hurley  * tty_port_close
5250733db91SPeter Hurley  *
5260733db91SPeter Hurley  * Caller holds tty lock
5270733db91SPeter Hurley  */
52896fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
52996fd7ce5SGreg Kroah-Hartman 							struct file *filp)
53096fd7ce5SGreg Kroah-Hartman {
53196fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
53296fd7ce5SGreg Kroah-Hartman 		return;
533957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
53496fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_IO_ERROR, &tty->flags);
53596fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
53696fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
53796fd7ce5SGreg Kroah-Hartman }
53896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
53996fd7ce5SGreg Kroah-Hartman 
54072a33bf5SJiri Slaby /**
54172a33bf5SJiri Slaby  * tty_port_install - generic tty->ops->install handler
54272a33bf5SJiri Slaby  * @port: tty_port of the device
54372a33bf5SJiri Slaby  * @driver: tty_driver for this device
54472a33bf5SJiri Slaby  * @tty: tty to be installed
54572a33bf5SJiri Slaby  *
54672a33bf5SJiri Slaby  * It is the same as tty_standard_install except the provided @port is linked
54772a33bf5SJiri Slaby  * to a concrete tty specified by @tty. Use this or tty_port_register_device
54872a33bf5SJiri Slaby  * (or both). Call tty_port_link_device as a last resort.
54972a33bf5SJiri Slaby  */
550695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
551695586caSJiri Slaby 		struct tty_struct *tty)
552695586caSJiri Slaby {
553695586caSJiri Slaby 	tty->port = port;
554695586caSJiri Slaby 	return tty_standard_install(driver, tty);
555695586caSJiri Slaby }
556695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
557695586caSJiri Slaby 
558addd4672SPeter Hurley /**
559addd4672SPeter Hurley  * tty_port_open
560addd4672SPeter Hurley  *
561addd4672SPeter Hurley  * Caller holds tty lock.
562addd4672SPeter Hurley  *
563addd4672SPeter Hurley  * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
564addd4672SPeter Hurley  * tty and tty_port may have changed state (eg., may be hung up now)
565addd4672SPeter Hurley  */
56696fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
56796fd7ce5SGreg Kroah-Hartman 							struct file *filp)
56896fd7ce5SGreg Kroah-Hartman {
56996fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
57096fd7ce5SGreg Kroah-Hartman 	++port->count;
57196fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
57296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
57396fd7ce5SGreg Kroah-Hartman 
57496fd7ce5SGreg Kroah-Hartman 	/*
57596fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
57696fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
57796fd7ce5SGreg Kroah-Hartman 	 * port mutex.
57896fd7ce5SGreg Kroah-Hartman 	 */
57996fd7ce5SGreg Kroah-Hartman 
58096fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
58196fd7ce5SGreg Kroah-Hartman 
582d41861caSPeter Hurley 	if (!tty_port_initialized(port)) {
58396fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
58496fd7ce5SGreg Kroah-Hartman 		if (port->ops->activate) {
58596fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
58696fd7ce5SGreg Kroah-Hartman 			if (retval) {
58796fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
58896fd7ce5SGreg Kroah-Hartman 				return retval;
58996fd7ce5SGreg Kroah-Hartman 			}
59096fd7ce5SGreg Kroah-Hartman 		}
591d41861caSPeter Hurley 		tty_port_set_initialized(port, 1);
59296fd7ce5SGreg Kroah-Hartman 	}
59396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
59496fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
59596fd7ce5SGreg Kroah-Hartman }
59696fd7ce5SGreg Kroah-Hartman 
59796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
598