xref: /openbmc/linux/drivers/tty/tty_port.c (revision 3be491d74a95b15cd1a725bcb10208dca346ec7a)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
296fd7ce5SGreg Kroah-Hartman /*
396fd7ce5SGreg Kroah-Hartman  * Tty port functions
496fd7ce5SGreg Kroah-Hartman  */
596fd7ce5SGreg Kroah-Hartman 
696fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
796fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
896fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
996fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
1096fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h>
1196fd7ce5SGreg Kroah-Hartman #include <linux/serial.h>
1296fd7ce5SGreg Kroah-Hartman #include <linux/timer.h>
1396fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
1496fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
15174cd4b1SIngo Molnar #include <linux/sched/signal.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>
208cde11b2SJohan Hovold #include <linux/serdev.h>
2198602c01SGreg Kroah-Hartman #include "tty.h"
2296fd7ce5SGreg Kroah-Hartman 
23c3485ee0SRob Herring static int tty_port_default_receive_buf(struct tty_port *port,
24c3485ee0SRob Herring 					const unsigned char *p,
25c3485ee0SRob Herring 					const unsigned char *f, size_t count)
26c3485ee0SRob Herring {
27c3485ee0SRob Herring 	int ret;
28c3485ee0SRob Herring 	struct tty_struct *tty;
29c3485ee0SRob Herring 	struct tty_ldisc *disc;
30c3485ee0SRob Herring 
31c3485ee0SRob Herring 	tty = READ_ONCE(port->itty);
32c3485ee0SRob Herring 	if (!tty)
33c3485ee0SRob Herring 		return 0;
34c3485ee0SRob Herring 
35c3485ee0SRob Herring 	disc = tty_ldisc_ref(tty);
36c3485ee0SRob Herring 	if (!disc)
37c3485ee0SRob Herring 		return 0;
38c3485ee0SRob Herring 
39c3485ee0SRob Herring 	ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
40c3485ee0SRob Herring 
41c3485ee0SRob Herring 	tty_ldisc_deref(disc);
42c3485ee0SRob Herring 
43c3485ee0SRob Herring 	return ret;
44c3485ee0SRob Herring }
45c3485ee0SRob Herring 
46c3485ee0SRob Herring static void tty_port_default_wakeup(struct tty_port *port)
47c3485ee0SRob Herring {
48c3485ee0SRob Herring 	struct tty_struct *tty = tty_port_tty_get(port);
49c3485ee0SRob Herring 
50c3485ee0SRob Herring 	if (tty) {
51c3485ee0SRob Herring 		tty_wakeup(tty);
52c3485ee0SRob Herring 		tty_kref_put(tty);
53c3485ee0SRob Herring 	}
54c3485ee0SRob Herring }
55c3485ee0SRob Herring 
560c5aae59SJohan Hovold const struct tty_port_client_operations tty_port_default_client_ops = {
57c3485ee0SRob Herring 	.receive_buf = tty_port_default_receive_buf,
58c3485ee0SRob Herring 	.write_wakeup = tty_port_default_wakeup,
59c3485ee0SRob Herring };
600c5aae59SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
61c3485ee0SRob Herring 
62*3be491d7SJiri Slaby /**
63*3be491d7SJiri Slaby  * tty_port_init -- initialize tty_port
64*3be491d7SJiri Slaby  * @port: tty_port to initialize
65*3be491d7SJiri Slaby  *
66*3be491d7SJiri Slaby  * Initializes the state of struct tty_port. When a port was initialized using
67*3be491d7SJiri Slaby  * this function, one has to destroy the port by tty_port_destroy(). Either
68*3be491d7SJiri Slaby  * indirectly by using &tty_port refcounting (tty_port_put()) or directly if
69*3be491d7SJiri Slaby  * refcounting is not used.
70*3be491d7SJiri Slaby  */
7196fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port)
7296fd7ce5SGreg Kroah-Hartman {
7396fd7ce5SGreg Kroah-Hartman 	memset(port, 0, sizeof(*port));
74ecbbfd44SJiri Slaby 	tty_buffer_init(port);
7596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->open_wait);
7696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->delta_msr_wait);
7796fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->mutex);
7896fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->buf_mutex);
7996fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&port->lock);
8096fd7ce5SGreg Kroah-Hartman 	port->close_delay = (50 * HZ) / 100;
8196fd7ce5SGreg Kroah-Hartman 	port->closing_wait = (3000 * HZ) / 100;
820c5aae59SJohan Hovold 	port->client_ops = &tty_port_default_client_ops;
8396fd7ce5SGreg Kroah-Hartman 	kref_init(&port->kref);
8496fd7ce5SGreg Kroah-Hartman }
8596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init);
8696fd7ce5SGreg Kroah-Hartman 
8772a33bf5SJiri Slaby /**
882cb4ca02SJiri Slaby  * tty_port_link_device - link tty and tty_port
892cb4ca02SJiri Slaby  * @port: tty_port of the device
902cb4ca02SJiri Slaby  * @driver: tty_driver for this device
912cb4ca02SJiri Slaby  * @index: index of the tty
922cb4ca02SJiri Slaby  *
932cb4ca02SJiri Slaby  * Provide the tty layer with a link from a tty (specified by @index) to a
94cb6f6f98SJiri Slaby  * tty_port (@port). Use this only if neither tty_port_register_device() nor
95cb6f6f98SJiri Slaby  * tty_port_install() is used in the driver. If used, this has to be called
96cb6f6f98SJiri Slaby  * before tty_register_driver().
972cb4ca02SJiri Slaby  */
982cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port,
992cb4ca02SJiri Slaby 		struct tty_driver *driver, unsigned index)
1002cb4ca02SJiri Slaby {
1012cb4ca02SJiri Slaby 	if (WARN_ON(index >= driver->num))
1022cb4ca02SJiri Slaby 		return;
1032cb4ca02SJiri Slaby 	driver->ports[index] = port;
1042cb4ca02SJiri Slaby }
1052cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device);
1062cb4ca02SJiri Slaby 
1072cb4ca02SJiri Slaby /**
10872a33bf5SJiri Slaby  * tty_port_register_device - register tty device
10972a33bf5SJiri Slaby  * @port: tty_port of the device
11072a33bf5SJiri Slaby  * @driver: tty_driver for this device
11172a33bf5SJiri Slaby  * @index: index of the tty
11272a33bf5SJiri Slaby  * @device: parent if exists, otherwise NULL
11372a33bf5SJiri Slaby  *
114cb6f6f98SJiri Slaby  * It is the same as tty_register_device() except the provided @port is linked
115cb6f6f98SJiri Slaby  * to a concrete tty specified by @index. Use this or tty_port_install() (or
116cb6f6f98SJiri Slaby  * both). Call tty_port_link_device() as a last resort.
11772a33bf5SJiri Slaby  */
118057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port,
119057eb856SJiri Slaby 		struct tty_driver *driver, unsigned index,
120057eb856SJiri Slaby 		struct device *device)
121057eb856SJiri Slaby {
1223086365dSRob Herring 	return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
123057eb856SJiri Slaby }
124057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device);
125057eb856SJiri Slaby 
126b1b79916STomas Hlavacek /**
127b1b79916STomas Hlavacek  * tty_port_register_device_attr - register tty device
128b1b79916STomas Hlavacek  * @port: tty_port of the device
129b1b79916STomas Hlavacek  * @driver: tty_driver for this device
130b1b79916STomas Hlavacek  * @index: index of the tty
131b1b79916STomas Hlavacek  * @device: parent if exists, otherwise NULL
132b1b79916STomas Hlavacek  * @drvdata: Driver data to be set to device.
133b1b79916STomas Hlavacek  * @attr_grp: Attribute group to be set on device.
134b1b79916STomas Hlavacek  *
135cb6f6f98SJiri Slaby  * It is the same as tty_register_device_attr() except the provided @port is
136cb6f6f98SJiri Slaby  * linked to a concrete tty specified by @index. Use this or tty_port_install()
137cb6f6f98SJiri Slaby  * (or both). Call tty_port_link_device() as a last resort.
138b1b79916STomas Hlavacek  */
139b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port,
140b1b79916STomas Hlavacek 		struct tty_driver *driver, unsigned index,
141b1b79916STomas Hlavacek 		struct device *device, void *drvdata,
142b1b79916STomas Hlavacek 		const struct attribute_group **attr_grp)
143b1b79916STomas Hlavacek {
144b1b79916STomas Hlavacek 	tty_port_link_device(port, driver, index);
145b1b79916STomas Hlavacek 	return tty_register_device_attr(driver, index, device, drvdata,
146b1b79916STomas Hlavacek 			attr_grp);
147b1b79916STomas Hlavacek }
148b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
149b1b79916STomas Hlavacek 
1508cde11b2SJohan Hovold /**
1518cde11b2SJohan Hovold  * tty_port_register_device_attr_serdev - register tty or serdev device
1528cde11b2SJohan Hovold  * @port: tty_port of the device
1538cde11b2SJohan Hovold  * @driver: tty_driver for this device
1548cde11b2SJohan Hovold  * @index: index of the tty
1558cde11b2SJohan Hovold  * @device: parent if exists, otherwise NULL
1568cde11b2SJohan Hovold  * @drvdata: driver data for the device
1578cde11b2SJohan Hovold  * @attr_grp: attribute group for the device
1588cde11b2SJohan Hovold  *
1598cde11b2SJohan Hovold  * Register a serdev or tty device depending on if the parent device has any
1608cde11b2SJohan Hovold  * defined serdev clients or not.
1618cde11b2SJohan Hovold  */
1628cde11b2SJohan Hovold struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
1638cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index,
1648cde11b2SJohan Hovold 		struct device *device, void *drvdata,
1658cde11b2SJohan Hovold 		const struct attribute_group **attr_grp)
1668cde11b2SJohan Hovold {
1678cde11b2SJohan Hovold 	struct device *dev;
1688cde11b2SJohan Hovold 
1698cde11b2SJohan Hovold 	tty_port_link_device(port, driver, index);
1708cde11b2SJohan Hovold 
1718cde11b2SJohan Hovold 	dev = serdev_tty_port_register(port, device, driver, index);
1728cde11b2SJohan Hovold 	if (PTR_ERR(dev) != -ENODEV) {
1738cde11b2SJohan Hovold 		/* Skip creating cdev if we registered a serdev device */
1748cde11b2SJohan Hovold 		return dev;
1758cde11b2SJohan Hovold 	}
1768cde11b2SJohan Hovold 
1778cde11b2SJohan Hovold 	return tty_register_device_attr(driver, index, device, drvdata,
1788cde11b2SJohan Hovold 			attr_grp);
1798cde11b2SJohan Hovold }
1808cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
1818cde11b2SJohan Hovold 
1828cde11b2SJohan Hovold /**
1838cde11b2SJohan Hovold  * tty_port_register_device_serdev - register tty or serdev device
1848cde11b2SJohan Hovold  * @port: tty_port of the device
1858cde11b2SJohan Hovold  * @driver: tty_driver for this device
1868cde11b2SJohan Hovold  * @index: index of the tty
1878cde11b2SJohan Hovold  * @device: parent if exists, otherwise NULL
1888cde11b2SJohan Hovold  *
1898cde11b2SJohan Hovold  * Register a serdev or tty device depending on if the parent device has any
1908cde11b2SJohan Hovold  * defined serdev clients or not.
1918cde11b2SJohan Hovold  */
1928cde11b2SJohan Hovold struct device *tty_port_register_device_serdev(struct tty_port *port,
1938cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index,
1948cde11b2SJohan Hovold 		struct device *device)
1958cde11b2SJohan Hovold {
1968cde11b2SJohan Hovold 	return tty_port_register_device_attr_serdev(port, driver, index,
1978cde11b2SJohan Hovold 			device, NULL, NULL);
1988cde11b2SJohan Hovold }
1998cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
2008cde11b2SJohan Hovold 
2018cde11b2SJohan Hovold /**
2028cde11b2SJohan Hovold  * tty_port_unregister_device - deregister a tty or serdev device
2038cde11b2SJohan Hovold  * @port: tty_port of the device
2048cde11b2SJohan Hovold  * @driver: tty_driver for this device
2058cde11b2SJohan Hovold  * @index: index of the tty
2068cde11b2SJohan Hovold  *
2078cde11b2SJohan Hovold  * If a tty or serdev device is registered with a call to
2088cde11b2SJohan Hovold  * tty_port_register_device_serdev() then this function must be called when
2098cde11b2SJohan Hovold  * the device is gone.
2108cde11b2SJohan Hovold  */
2118cde11b2SJohan Hovold void tty_port_unregister_device(struct tty_port *port,
2128cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index)
2138cde11b2SJohan Hovold {
2148cde11b2SJohan Hovold 	int ret;
2158cde11b2SJohan Hovold 
2168cde11b2SJohan Hovold 	ret = serdev_tty_port_unregister(port);
2178cde11b2SJohan Hovold 	if (ret == 0)
2188cde11b2SJohan Hovold 		return;
2198cde11b2SJohan Hovold 
2208cde11b2SJohan Hovold 	tty_unregister_device(driver, index);
2218cde11b2SJohan Hovold }
2228cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_unregister_device);
2238cde11b2SJohan Hovold 
22496fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
22596fd7ce5SGreg Kroah-Hartman {
22696fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
22796fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
22896fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
22996fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
23096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
23196fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
23296fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
23396fd7ce5SGreg Kroah-Hartman 	return 0;
23496fd7ce5SGreg Kroah-Hartman }
23596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
23696fd7ce5SGreg Kroah-Hartman 
23796fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
23896fd7ce5SGreg Kroah-Hartman {
23996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
24096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
24196fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
24296fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
24396fd7ce5SGreg Kroah-Hartman 	}
24496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
24596fd7ce5SGreg Kroah-Hartman }
24696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
24796fd7ce5SGreg Kroah-Hartman 
248de274bfeSJiri Slaby /**
249de274bfeSJiri Slaby  * tty_port_destroy -- destroy inited port
2501926e5d3SAntonio Borneo  * @port: tty port to be destroyed
251de274bfeSJiri Slaby  *
252cb6f6f98SJiri Slaby  * When a port was initialized using tty_port_init(), one has to destroy the
253cb6f6f98SJiri Slaby  * port by this function. Either indirectly by using &tty_port refcounting
254cb6f6f98SJiri Slaby  * (tty_port_put()) or directly if refcounting is not used.
255de274bfeSJiri Slaby  */
256de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port)
257de274bfeSJiri Slaby {
258e176058fSPeter Hurley 	tty_buffer_cancel_work(port);
259de274bfeSJiri Slaby 	tty_buffer_free_all(port);
260de274bfeSJiri Slaby }
261de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy);
262de274bfeSJiri Slaby 
26396fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
26496fd7ce5SGreg Kroah-Hartman {
26596fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
266e3bfea23SPeter Hurley 
267e3bfea23SPeter Hurley 	/* check if last port ref was dropped before tty release */
268e3bfea23SPeter Hurley 	if (WARN_ON(port->itty))
269e3bfea23SPeter Hurley 		return;
27096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
27196fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
272de274bfeSJiri Slaby 	tty_port_destroy(port);
27381c79838SJiri Slaby 	if (port->ops && port->ops->destruct)
27496fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
27596fd7ce5SGreg Kroah-Hartman 	else
27696fd7ce5SGreg Kroah-Hartman 		kfree(port);
27796fd7ce5SGreg Kroah-Hartman }
27896fd7ce5SGreg Kroah-Hartman 
279*3be491d7SJiri Slaby /**
280*3be491d7SJiri Slaby  * tty_port_put -- drop a reference to tty_port
281*3be491d7SJiri Slaby  * @port: port to drop a reference of (can be NULL)
282*3be491d7SJiri Slaby  *
283*3be491d7SJiri Slaby  * The final put will destroy and free up the @port using
284*3be491d7SJiri Slaby  * @port->ops->destruct() hook, or using kfree() if not provided.
285*3be491d7SJiri Slaby  */
28696fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
28796fd7ce5SGreg Kroah-Hartman {
28896fd7ce5SGreg Kroah-Hartman 	if (port)
28996fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
29096fd7ce5SGreg Kroah-Hartman }
29196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
29296fd7ce5SGreg Kroah-Hartman 
29396fd7ce5SGreg Kroah-Hartman /**
29496fd7ce5SGreg Kroah-Hartman  * tty_port_tty_get	-	get a tty reference
29596fd7ce5SGreg Kroah-Hartman  * @port: tty port
29696fd7ce5SGreg Kroah-Hartman  *
297cb6f6f98SJiri Slaby  * Return a refcount protected tty instance or %NULL if the port is not
298cb6f6f98SJiri Slaby  * associated with a tty (eg due to close or hangup).
29996fd7ce5SGreg Kroah-Hartman  */
30096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
30196fd7ce5SGreg Kroah-Hartman {
30296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
30396fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
30496fd7ce5SGreg Kroah-Hartman 
30596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
30696fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
30796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
30896fd7ce5SGreg Kroah-Hartman 	return tty;
30996fd7ce5SGreg Kroah-Hartman }
31096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
31196fd7ce5SGreg Kroah-Hartman 
31296fd7ce5SGreg Kroah-Hartman /**
31396fd7ce5SGreg Kroah-Hartman  * tty_port_tty_set	-	set the tty of a port
31496fd7ce5SGreg Kroah-Hartman  * @port: tty port
31596fd7ce5SGreg Kroah-Hartman  * @tty: the tty
31696fd7ce5SGreg Kroah-Hartman  *
317cb6f6f98SJiri Slaby  * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
318cb6f6f98SJiri Slaby  * to deassociate a port.
31996fd7ce5SGreg Kroah-Hartman  */
32096fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
32196fd7ce5SGreg Kroah-Hartman {
32296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
32396fd7ce5SGreg Kroah-Hartman 
32496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
32596fd7ce5SGreg Kroah-Hartman 	tty_kref_put(port->tty);
32696fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
32796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
32896fd7ce5SGreg Kroah-Hartman }
32996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
33096fd7ce5SGreg Kroah-Hartman 
331*3be491d7SJiri Slaby /**
332*3be491d7SJiri Slaby  * tty_port_shutdown - internal helper to shutdown the device
333*3be491d7SJiri Slaby  * @port: tty port to be shut down
334*3be491d7SJiri Slaby  * @tty: the associated tty
335*3be491d7SJiri Slaby  *
336*3be491d7SJiri Slaby  * It is used by tty_port_hangup() and tty_port_close(). Its task is to
337*3be491d7SJiri Slaby  * shutdown the device if it was initialized (note consoles remain
338*3be491d7SJiri Slaby  * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
339*3be491d7SJiri Slaby  * @port->ops->shutdown().
340*3be491d7SJiri Slaby  */
341957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
34296fd7ce5SGreg Kroah-Hartman {
34396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
3448bde9658SJohan Hovold 	if (port->console)
3458bde9658SJohan Hovold 		goto out;
3468bde9658SJohan Hovold 
347d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
348d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
349957dacaeSJohan Hovold 		/*
350957dacaeSJohan Hovold 		 * Drop DTR/RTS if HUPCL is set. This causes any attached
351957dacaeSJohan Hovold 		 * modem to hang up the line.
352957dacaeSJohan Hovold 		 */
353957dacaeSJohan Hovold 		if (tty && C_HUPCL(tty))
354957dacaeSJohan Hovold 			tty_port_lower_dtr_rts(port);
355957dacaeSJohan Hovold 
3560d3cb6f6SJohan Hovold 		if (port->ops->shutdown)
35796fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
3588bde9658SJohan Hovold 	}
3598bde9658SJohan Hovold out:
36096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
36196fd7ce5SGreg Kroah-Hartman }
36296fd7ce5SGreg Kroah-Hartman 
36396fd7ce5SGreg Kroah-Hartman /**
36496fd7ce5SGreg Kroah-Hartman  * tty_port_hangup		-	hangup helper
36596fd7ce5SGreg Kroah-Hartman  * @port: tty port
36696fd7ce5SGreg Kroah-Hartman  *
36796fd7ce5SGreg Kroah-Hartman  * Perform port level tty hangup flag and count changes. Drop the tty
36896fd7ce5SGreg Kroah-Hartman  * reference.
3699c9928bdSPeter Hurley  *
3709c9928bdSPeter Hurley  * Caller holds tty lock.
37196fd7ce5SGreg Kroah-Hartman  */
37296fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
37396fd7ce5SGreg Kroah-Hartman {
374957dacaeSJohan Hovold 	struct tty_struct *tty;
37596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
37696fd7ce5SGreg Kroah-Hartman 
37796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
37896fd7ce5SGreg Kroah-Hartman 	port->count = 0;
379957dacaeSJohan Hovold 	tty = port->tty;
380957dacaeSJohan Hovold 	if (tty)
381957dacaeSJohan Hovold 		set_bit(TTY_IO_ERROR, &tty->flags);
38296fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
38396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
384807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
385957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
386957dacaeSJohan Hovold 	tty_kref_put(tty);
38796fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
38896fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
38996fd7ce5SGreg Kroah-Hartman }
39096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
39196fd7ce5SGreg Kroah-Hartman 
39296fd7ce5SGreg Kroah-Hartman /**
393aa27a094SJiri Slaby  * tty_port_tty_hangup - helper to hang up a tty
394aa27a094SJiri Slaby  * @port: tty port
395cb6f6f98SJiri Slaby  * @check_clocal: hang only ttys with %CLOCAL unset?
396aa27a094SJiri Slaby  */
397aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
398aa27a094SJiri Slaby {
399aa27a094SJiri Slaby 	struct tty_struct *tty = tty_port_tty_get(port);
400aa27a094SJiri Slaby 
4011d9e689cSGianluca Anzolin 	if (tty && (!check_clocal || !C_CLOCAL(tty)))
402aa27a094SJiri Slaby 		tty_hangup(tty);
403aa27a094SJiri Slaby 	tty_kref_put(tty);
404aa27a094SJiri Slaby }
405aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
406aa27a094SJiri Slaby 
407aa27a094SJiri Slaby /**
4086aad04f2SJiri Slaby  * tty_port_tty_wakeup - helper to wake up a tty
4096aad04f2SJiri Slaby  * @port: tty port
4106aad04f2SJiri Slaby  */
4116aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port)
4126aad04f2SJiri Slaby {
413c3485ee0SRob Herring 	port->client_ops->write_wakeup(port);
4146aad04f2SJiri Slaby }
4156aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
4166aad04f2SJiri Slaby 
4176aad04f2SJiri Slaby /**
41896fd7ce5SGreg Kroah-Hartman  * tty_port_carrier_raised	-	carrier raised check
41996fd7ce5SGreg Kroah-Hartman  * @port: tty port
42096fd7ce5SGreg Kroah-Hartman  *
42196fd7ce5SGreg Kroah-Hartman  * Wrapper for the carrier detect logic. For the moment this is used
42296fd7ce5SGreg Kroah-Hartman  * to hide some internal details. This will eventually become entirely
42396fd7ce5SGreg Kroah-Hartman  * internal to the tty port.
42496fd7ce5SGreg Kroah-Hartman  */
42596fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
42696fd7ce5SGreg Kroah-Hartman {
4270d3cb6f6SJohan Hovold 	if (port->ops->carrier_raised == NULL)
42896fd7ce5SGreg Kroah-Hartman 		return 1;
42996fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
43096fd7ce5SGreg Kroah-Hartman }
43196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
43296fd7ce5SGreg Kroah-Hartman 
43396fd7ce5SGreg Kroah-Hartman /**
43496fd7ce5SGreg Kroah-Hartman  * tty_port_raise_dtr_rts	-	Raise DTR/RTS
43596fd7ce5SGreg Kroah-Hartman  * @port: tty port
43696fd7ce5SGreg Kroah-Hartman  *
437cb6f6f98SJiri Slaby  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
438cb6f6f98SJiri Slaby  * some internal details. This will eventually become entirely internal to the
439cb6f6f98SJiri Slaby  * tty port.
44096fd7ce5SGreg Kroah-Hartman  */
44196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
44296fd7ce5SGreg Kroah-Hartman {
4430d3cb6f6SJohan Hovold 	if (port->ops->dtr_rts)
44496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
44596fd7ce5SGreg Kroah-Hartman }
44696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
44796fd7ce5SGreg Kroah-Hartman 
44896fd7ce5SGreg Kroah-Hartman /**
44996fd7ce5SGreg Kroah-Hartman  * tty_port_lower_dtr_rts	-	Lower DTR/RTS
45096fd7ce5SGreg Kroah-Hartman  * @port: tty port
45196fd7ce5SGreg Kroah-Hartman  *
452cb6f6f98SJiri Slaby  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
453cb6f6f98SJiri Slaby  * some internal details. This will eventually become entirely internal to the
454cb6f6f98SJiri Slaby  * tty port.
45596fd7ce5SGreg Kroah-Hartman  */
45696fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
45796fd7ce5SGreg Kroah-Hartman {
4580d3cb6f6SJohan Hovold 	if (port->ops->dtr_rts)
45996fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
46096fd7ce5SGreg Kroah-Hartman }
46196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
46296fd7ce5SGreg Kroah-Hartman 
46396fd7ce5SGreg Kroah-Hartman /**
46496fd7ce5SGreg Kroah-Hartman  * tty_port_block_til_ready	-	Waiting logic for tty open
46596fd7ce5SGreg Kroah-Hartman  * @port: the tty port being opened
46696fd7ce5SGreg Kroah-Hartman  * @tty: the tty device being bound
467cb6f6f98SJiri Slaby  * @filp: the file pointer of the opener or %NULL
46896fd7ce5SGreg Kroah-Hartman  *
46996fd7ce5SGreg Kroah-Hartman  * Implement the core POSIX/SuS tty behaviour when opening a tty device.
47096fd7ce5SGreg Kroah-Hartman  * Handles:
471cb6f6f98SJiri Slaby  *
47296fd7ce5SGreg Kroah-Hartman  *	- hangup (both before and during)
47396fd7ce5SGreg Kroah-Hartman  *	- non blocking open
47496fd7ce5SGreg Kroah-Hartman  *	- rts/dtr/dcd
47596fd7ce5SGreg Kroah-Hartman  *	- signals
47696fd7ce5SGreg Kroah-Hartman  *	- port flags and counts
47796fd7ce5SGreg Kroah-Hartman  *
478cb6f6f98SJiri Slaby  * The passed @port must implement the @port->ops->carrier_raised method if it
479cb6f6f98SJiri Slaby  * can do carrier detect and the @port->ops->dtr_rts method if it supports
480cb6f6f98SJiri Slaby  * software management of these lines. Note that the dtr/rts raise is done each
48196fd7ce5SGreg Kroah-Hartman  * iteration as a hangup may have previously dropped them while we wait.
482c590f6b6SPeter Hurley  *
483c590f6b6SPeter Hurley  * Caller holds tty lock.
484c590f6b6SPeter Hurley  *
485cb6f6f98SJiri Slaby  * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
486cb6f6f98SJiri Slaby  * have changed state (eg., may have been hung up).
48796fd7ce5SGreg Kroah-Hartman  */
48896fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
48996fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
49096fd7ce5SGreg Kroah-Hartman {
49196fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
49296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
49396fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
49496fd7ce5SGreg Kroah-Hartman 
49596fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
4961df92640SXiaofei Tan 	 * the port has just hung up or is in another error state.
4971df92640SXiaofei Tan 	 */
49818900ca6SPeter Hurley 	if (tty_io_error(tty)) {
499807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
50096fd7ce5SGreg Kroah-Hartman 		return 0;
50196fd7ce5SGreg Kroah-Hartman 	}
502ed3f0af8SAlan Cox 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
50396fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
5049db276f8SPeter Hurley 		if (C_BAUD(tty))
50596fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
506807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
50796fd7ce5SGreg Kroah-Hartman 		return 0;
50896fd7ce5SGreg Kroah-Hartman 	}
50996fd7ce5SGreg Kroah-Hartman 
51096fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
51196fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
51296fd7ce5SGreg Kroah-Hartman 
51396fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
5141df92640SXiaofei Tan 	 * carrier, but we must also wait for any close that is in progress
5151df92640SXiaofei Tan 	 * before the next open may complete.
5161df92640SXiaofei Tan 	 */
51796fd7ce5SGreg Kroah-Hartman 
51896fd7ce5SGreg Kroah-Hartman 	retval = 0;
51996fd7ce5SGreg Kroah-Hartman 
52096fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
52196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
52296fd7ce5SGreg Kroah-Hartman 	port->count--;
52396fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
52496fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
52596fd7ce5SGreg Kroah-Hartman 
52696fd7ce5SGreg Kroah-Hartman 	while (1) {
52796fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
528d41861caSPeter Hurley 		if (C_BAUD(tty) && tty_port_initialized(port))
52996fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
53096fd7ce5SGreg Kroah-Hartman 
53196fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
53296fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
5331df92640SXiaofei Tan 		 * Return accordingly.
5341df92640SXiaofei Tan 		 */
535d41861caSPeter Hurley 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
53696fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
53796fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
53896fd7ce5SGreg Kroah-Hartman 			else
53996fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
54096fd7ce5SGreg Kroah-Hartman 			break;
54196fd7ce5SGreg Kroah-Hartman 		}
5420eee50afSJiri Slaby 		/*
5430eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
5440eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
5450eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
5460eee50afSJiri Slaby 		 * on some hardware.
5470eee50afSJiri Slaby 		 */
548fef062cbSPeter Hurley 		if (do_clocal || tty_port_carrier_raised(port))
54996fd7ce5SGreg Kroah-Hartman 			break;
55096fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
55196fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
55296fd7ce5SGreg Kroah-Hartman 			break;
55396fd7ce5SGreg Kroah-Hartman 		}
55489c8d91eSAlan Cox 		tty_unlock(tty);
55596fd7ce5SGreg Kroah-Hartman 		schedule();
55689c8d91eSAlan Cox 		tty_lock(tty);
55796fd7ce5SGreg Kroah-Hartman 	}
55896fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
55996fd7ce5SGreg Kroah-Hartman 
56096fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
5611df92640SXiaofei Tan 	 * we must not mess that up further.
5621df92640SXiaofei Tan 	 */
56396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
56496fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
56596fd7ce5SGreg Kroah-Hartman 		port->count++;
56696fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
56796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
568807c8d81SPeter Hurley 	if (retval == 0)
569807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
57096fd7ce5SGreg Kroah-Hartman 	return retval;
57196fd7ce5SGreg Kroah-Hartman }
57296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
57396fd7ce5SGreg Kroah-Hartman 
574b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
575b74414f5SJohan Hovold {
576b74414f5SJohan Hovold 	unsigned int bps = tty_get_baud_rate(tty);
577b74414f5SJohan Hovold 	long timeout;
578b74414f5SJohan Hovold 
579b74414f5SJohan Hovold 	if (bps > 1200) {
580b74414f5SJohan Hovold 		timeout = (HZ * 10 * port->drain_delay) / bps;
581b74414f5SJohan Hovold 		timeout = max_t(long, timeout, HZ / 10);
582b74414f5SJohan Hovold 	} else {
583b74414f5SJohan Hovold 		timeout = 2 * HZ;
584b74414f5SJohan Hovold 	}
585b74414f5SJohan Hovold 	schedule_timeout_interruptible(timeout);
586b74414f5SJohan Hovold }
587b74414f5SJohan Hovold 
588*3be491d7SJiri Slaby /**
589*3be491d7SJiri Slaby  * tty_port_close_start - helper for tty->ops->close, part 1/2
590*3be491d7SJiri Slaby  * @port: tty_port of the device
591*3be491d7SJiri Slaby  * @tty: tty being closed
592*3be491d7SJiri Slaby  * @filp: passed file pointer
593*3be491d7SJiri Slaby  *
594*3be491d7SJiri Slaby  * Decrements and checks open count. Flushes the port if this is the last
595*3be491d7SJiri Slaby  * close. That means, dropping the data from the outpu buffer on the device and
596*3be491d7SJiri Slaby  * waiting for sending logic to finish. The rest of close handling is performed
597*3be491d7SJiri Slaby  * in tty_port_close_end().
598*3be491d7SJiri Slaby  *
599*3be491d7SJiri Slaby  * Locking: Caller holds tty lock.
600*3be491d7SJiri Slaby  *
601*3be491d7SJiri Slaby  * Return: 1 if this is the last close, otherwise 0
602*3be491d7SJiri Slaby  */
60396fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
60496fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
60596fd7ce5SGreg Kroah-Hartman {
60696fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
60796fd7ce5SGreg Kroah-Hartman 
608633caba8SPeter Hurley 	if (tty_hung_up_p(filp))
60996fd7ce5SGreg Kroah-Hartman 		return 0;
61096fd7ce5SGreg Kroah-Hartman 
611633caba8SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
61296fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
613339f36baSPeter Hurley 		tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
61496fd7ce5SGreg Kroah-Hartman 			 port->count);
61596fd7ce5SGreg Kroah-Hartman 		port->count = 1;
61696fd7ce5SGreg Kroah-Hartman 	}
61796fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
618339f36baSPeter Hurley 		tty_warn(tty, "%s: bad port count (%d)\n", __func__,
61996fd7ce5SGreg Kroah-Hartman 			 port->count);
62096fd7ce5SGreg Kroah-Hartman 		port->count = 0;
62196fd7ce5SGreg Kroah-Hartman 	}
62296fd7ce5SGreg Kroah-Hartman 
62396fd7ce5SGreg Kroah-Hartman 	if (port->count) {
62496fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
62596fd7ce5SGreg Kroah-Hartman 		return 0;
62696fd7ce5SGreg Kroah-Hartman 	}
62796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
6280b2588caSJohan Hovold 
629ddc7b758SPeter Hurley 	tty->closing = 1;
630ddc7b758SPeter Hurley 
631d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
63296fd7ce5SGreg Kroah-Hartman 		/* Don't block on a stalled port, just pull the chain */
6336e94dbc7SJiri Slaby 		if (tty->flow.tco_stopped)
63496fd7ce5SGreg Kroah-Hartman 			tty_driver_flush_buffer(tty);
6350b2588caSJohan Hovold 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
63679c1faa4SPeter Hurley 			tty_wait_until_sent(tty, port->closing_wait);
637b74414f5SJohan Hovold 		if (port->drain_delay)
638b74414f5SJohan Hovold 			tty_port_drain_delay(port, tty);
6390b2588caSJohan Hovold 	}
64096fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
64196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
64296fd7ce5SGreg Kroah-Hartman 
643469d6d06SPeter Hurley 	/* Report to caller this is the last port reference */
64496fd7ce5SGreg Kroah-Hartman 	return 1;
64596fd7ce5SGreg Kroah-Hartman }
64696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
64796fd7ce5SGreg Kroah-Hartman 
648*3be491d7SJiri Slaby /**
649*3be491d7SJiri Slaby  * tty_port_close_end - helper for tty->ops->close, part 2/2
650*3be491d7SJiri Slaby  * @port: tty_port of the device
651*3be491d7SJiri Slaby  * @tty: tty being closed
652*3be491d7SJiri Slaby  *
653*3be491d7SJiri Slaby  * This is a continuation of the first part: tty_port_close_start(). This
654*3be491d7SJiri Slaby  * should be called after turning off the device. It flushes the data from the
655*3be491d7SJiri Slaby  * line discipline and delays the close by @port->close_delay.
656*3be491d7SJiri Slaby  *
657*3be491d7SJiri Slaby  * Locking: Caller holds tty lock.
658*3be491d7SJiri Slaby  */
65996fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
66096fd7ce5SGreg Kroah-Hartman {
66196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
66296fd7ce5SGreg Kroah-Hartman 
6633f40f5b2SPeter Hurley 	tty_ldisc_flush(tty);
66496fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
66596fd7ce5SGreg Kroah-Hartman 
666ddc7b758SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
667ddc7b758SPeter Hurley 
66896fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
66996fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
6705823323eSPeter Hurley 		if (port->close_delay)
6715823323eSPeter Hurley 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
67296fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
67396fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
67496fd7ce5SGreg Kroah-Hartman 	}
67596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
676807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
67796fd7ce5SGreg Kroah-Hartman }
67896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
67996fd7ce5SGreg Kroah-Hartman 
680cb6f6f98SJiri Slaby /**
681cb6f6f98SJiri Slaby  * tty_port_close - generic tty->ops->close handler
682cb6f6f98SJiri Slaby  * @port: tty_port of the device
683cb6f6f98SJiri Slaby  * @tty: tty being closed
684cb6f6f98SJiri Slaby  * @filp: passed file pointer
6850733db91SPeter Hurley  *
686cb6f6f98SJiri Slaby  * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
687cb6f6f98SJiri Slaby  * sequence of tty_port_close_start(), tty_port_shutdown(), and
688cb6f6f98SJiri Slaby  * tty_port_close_end(). The latter two are called only if this is the last
689cb6f6f98SJiri Slaby  * close. See the respective functions for the details.
690cb6f6f98SJiri Slaby  *
691cb6f6f98SJiri Slaby  * Locking: Caller holds tty lock
6920733db91SPeter Hurley  */
69396fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
69496fd7ce5SGreg Kroah-Hartman 							struct file *filp)
69596fd7ce5SGreg Kroah-Hartman {
69696fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
69796fd7ce5SGreg Kroah-Hartman 		return;
698957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
6992a486026SChanho Park 	if (!port->console)
70096fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_IO_ERROR, &tty->flags);
70196fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
70296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
70396fd7ce5SGreg Kroah-Hartman }
70496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
70596fd7ce5SGreg Kroah-Hartman 
70672a33bf5SJiri Slaby /**
70772a33bf5SJiri Slaby  * tty_port_install - generic tty->ops->install handler
70872a33bf5SJiri Slaby  * @port: tty_port of the device
70972a33bf5SJiri Slaby  * @driver: tty_driver for this device
71072a33bf5SJiri Slaby  * @tty: tty to be installed
71172a33bf5SJiri Slaby  *
712cb6f6f98SJiri Slaby  * It is the same as tty_standard_install() except the provided @port is linked
713cb6f6f98SJiri Slaby  * to a concrete tty specified by @tty. Use this or tty_port_register_device()
714cb6f6f98SJiri Slaby  * (or both). Call tty_port_link_device() as a last resort.
71572a33bf5SJiri Slaby  */
716695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
717695586caSJiri Slaby 		struct tty_struct *tty)
718695586caSJiri Slaby {
719695586caSJiri Slaby 	tty->port = port;
720695586caSJiri Slaby 	return tty_standard_install(driver, tty);
721695586caSJiri Slaby }
722695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
723695586caSJiri Slaby 
724cb6f6f98SJiri Slaby /**
725cb6f6f98SJiri Slaby  * tty_port_open - generic tty->ops->open handler
726cb6f6f98SJiri Slaby  * @port: tty_port of the device
727cb6f6f98SJiri Slaby  * @tty: tty to be opened
728cb6f6f98SJiri Slaby  * @filp: passed file pointer
729addd4672SPeter Hurley  *
730cb6f6f98SJiri Slaby  * It is a generic helper to be used in driver's @tty->ops->open. It activates
731cb6f6f98SJiri Slaby  * the devices using @port->ops->activate if not active already. And waits for
732cb6f6f98SJiri Slaby  * the device to be ready using tty_port_block_til_ready() (e.g.  raises
733cb6f6f98SJiri Slaby  * DTR/CTS and waits for carrier).
734addd4672SPeter Hurley  *
735cb6f6f98SJiri Slaby  * Locking: Caller holds tty lock.
736cb6f6f98SJiri Slaby  *
737cb6f6f98SJiri Slaby  * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
738cb6f6f98SJiri Slaby  * @tty and @port may have changed state (eg., may be hung up now).
739addd4672SPeter Hurley  */
74096fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
74196fd7ce5SGreg Kroah-Hartman 							struct file *filp)
74296fd7ce5SGreg Kroah-Hartman {
74396fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
74496fd7ce5SGreg Kroah-Hartman 	++port->count;
74596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
74696fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
74796fd7ce5SGreg Kroah-Hartman 
74896fd7ce5SGreg Kroah-Hartman 	/*
74996fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
75096fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
75196fd7ce5SGreg Kroah-Hartman 	 * port mutex.
75296fd7ce5SGreg Kroah-Hartman 	 */
75396fd7ce5SGreg Kroah-Hartman 
75496fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
75596fd7ce5SGreg Kroah-Hartman 
756d41861caSPeter Hurley 	if (!tty_port_initialized(port)) {
75796fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
7580d3cb6f6SJohan Hovold 		if (port->ops->activate) {
75996fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
76054ad59a2SXiaofei Tan 
76196fd7ce5SGreg Kroah-Hartman 			if (retval) {
76296fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
76396fd7ce5SGreg Kroah-Hartman 				return retval;
76496fd7ce5SGreg Kroah-Hartman 			}
76596fd7ce5SGreg Kroah-Hartman 		}
766d41861caSPeter Hurley 		tty_port_set_initialized(port, 1);
76796fd7ce5SGreg Kroah-Hartman 	}
76896fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
76996fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
77096fd7ce5SGreg Kroah-Hartman }
77196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
772