xref: /openbmc/linux/drivers/tty/tty_port.c (revision 8cde11b2baa1d02eb2eb955dfd47d9f2a12f12cf)
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>
14174cd4b1SIngo Molnar #include <linux/sched/signal.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>
19*8cde11b2SJohan Hovold #include <linux/serdev.h>
2096fd7ce5SGreg Kroah-Hartman 
21c3485ee0SRob Herring static int tty_port_default_receive_buf(struct tty_port *port,
22c3485ee0SRob Herring 					const unsigned char *p,
23c3485ee0SRob Herring 					const unsigned char *f, size_t count)
24c3485ee0SRob Herring {
25c3485ee0SRob Herring 	int ret;
26c3485ee0SRob Herring 	struct tty_struct *tty;
27c3485ee0SRob Herring 	struct tty_ldisc *disc;
28c3485ee0SRob Herring 
29c3485ee0SRob Herring 	tty = READ_ONCE(port->itty);
30c3485ee0SRob Herring 	if (!tty)
31c3485ee0SRob Herring 		return 0;
32c3485ee0SRob Herring 
33c3485ee0SRob Herring 	disc = tty_ldisc_ref(tty);
34c3485ee0SRob Herring 	if (!disc)
35c3485ee0SRob Herring 		return 0;
36c3485ee0SRob Herring 
37925bb1ceSVegard Nossum 	mutex_lock(&tty->atomic_write_lock);
38c3485ee0SRob Herring 	ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
39925bb1ceSVegard Nossum 	mutex_unlock(&tty->atomic_write_lock);
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 
56c3485ee0SRob Herring static const struct tty_port_client_operations default_client_ops = {
57c3485ee0SRob Herring 	.receive_buf = tty_port_default_receive_buf,
58c3485ee0SRob Herring 	.write_wakeup = tty_port_default_wakeup,
59c3485ee0SRob Herring };
60c3485ee0SRob Herring 
6196fd7ce5SGreg Kroah-Hartman void tty_port_init(struct tty_port *port)
6296fd7ce5SGreg Kroah-Hartman {
6396fd7ce5SGreg Kroah-Hartman 	memset(port, 0, sizeof(*port));
64ecbbfd44SJiri Slaby 	tty_buffer_init(port);
6596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->open_wait);
6696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&port->delta_msr_wait);
6796fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->mutex);
6896fd7ce5SGreg Kroah-Hartman 	mutex_init(&port->buf_mutex);
6996fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&port->lock);
7096fd7ce5SGreg Kroah-Hartman 	port->close_delay = (50 * HZ) / 100;
7196fd7ce5SGreg Kroah-Hartman 	port->closing_wait = (3000 * HZ) / 100;
72c3485ee0SRob Herring 	port->client_ops = &default_client_ops;
7396fd7ce5SGreg Kroah-Hartman 	kref_init(&port->kref);
7496fd7ce5SGreg Kroah-Hartman }
7596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_init);
7696fd7ce5SGreg Kroah-Hartman 
7772a33bf5SJiri Slaby /**
782cb4ca02SJiri Slaby  * tty_port_link_device - link tty and tty_port
792cb4ca02SJiri Slaby  * @port: tty_port of the device
802cb4ca02SJiri Slaby  * @driver: tty_driver for this device
812cb4ca02SJiri Slaby  * @index: index of the tty
822cb4ca02SJiri Slaby  *
832cb4ca02SJiri Slaby  * Provide the tty layer wit ha link from a tty (specified by @index) to a
842cb4ca02SJiri Slaby  * tty_port (@port). Use this only if neither tty_port_register_device nor
852cb4ca02SJiri Slaby  * tty_port_install is used in the driver. If used, this has to be called before
862cb4ca02SJiri Slaby  * tty_register_driver.
872cb4ca02SJiri Slaby  */
882cb4ca02SJiri Slaby void tty_port_link_device(struct tty_port *port,
892cb4ca02SJiri Slaby 		struct tty_driver *driver, unsigned index)
902cb4ca02SJiri Slaby {
912cb4ca02SJiri Slaby 	if (WARN_ON(index >= driver->num))
922cb4ca02SJiri Slaby 		return;
932cb4ca02SJiri Slaby 	driver->ports[index] = port;
942cb4ca02SJiri Slaby }
952cb4ca02SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_link_device);
962cb4ca02SJiri Slaby 
972cb4ca02SJiri Slaby /**
9872a33bf5SJiri Slaby  * tty_port_register_device - register tty device
9972a33bf5SJiri Slaby  * @port: tty_port of the device
10072a33bf5SJiri Slaby  * @driver: tty_driver for this device
10172a33bf5SJiri Slaby  * @index: index of the tty
10272a33bf5SJiri Slaby  * @device: parent if exists, otherwise NULL
10372a33bf5SJiri Slaby  *
10472a33bf5SJiri Slaby  * It is the same as tty_register_device except the provided @port is linked to
10572a33bf5SJiri Slaby  * a concrete tty specified by @index. Use this or tty_port_install (or both).
10672a33bf5SJiri Slaby  * Call tty_port_link_device as a last resort.
10772a33bf5SJiri Slaby  */
108057eb856SJiri Slaby struct device *tty_port_register_device(struct tty_port *port,
109057eb856SJiri Slaby 		struct tty_driver *driver, unsigned index,
110057eb856SJiri Slaby 		struct device *device)
111057eb856SJiri Slaby {
1123086365dSRob Herring 	return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
113057eb856SJiri Slaby }
114057eb856SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_register_device);
115057eb856SJiri Slaby 
116b1b79916STomas Hlavacek /**
117b1b79916STomas Hlavacek  * tty_port_register_device_attr - register tty device
118b1b79916STomas Hlavacek  * @port: tty_port of the device
119b1b79916STomas Hlavacek  * @driver: tty_driver for this device
120b1b79916STomas Hlavacek  * @index: index of the tty
121b1b79916STomas Hlavacek  * @device: parent if exists, otherwise NULL
122b1b79916STomas Hlavacek  * @drvdata: Driver data to be set to device.
123b1b79916STomas Hlavacek  * @attr_grp: Attribute group to be set on device.
124b1b79916STomas Hlavacek  *
125b1b79916STomas Hlavacek  * It is the same as tty_register_device_attr except the provided @port is
126b1b79916STomas Hlavacek  * linked to a concrete tty specified by @index. Use this or tty_port_install
127b1b79916STomas Hlavacek  * (or both). Call tty_port_link_device as a last resort.
128b1b79916STomas Hlavacek  */
129b1b79916STomas Hlavacek struct device *tty_port_register_device_attr(struct tty_port *port,
130b1b79916STomas Hlavacek 		struct tty_driver *driver, unsigned index,
131b1b79916STomas Hlavacek 		struct device *device, void *drvdata,
132b1b79916STomas Hlavacek 		const struct attribute_group **attr_grp)
133b1b79916STomas Hlavacek {
134b1b79916STomas Hlavacek 	tty_port_link_device(port, driver, index);
135b1b79916STomas Hlavacek 	return tty_register_device_attr(driver, index, device, drvdata,
136b1b79916STomas Hlavacek 			attr_grp);
137b1b79916STomas Hlavacek }
138b1b79916STomas Hlavacek EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
139b1b79916STomas Hlavacek 
140*8cde11b2SJohan Hovold /**
141*8cde11b2SJohan Hovold  * tty_port_register_device_attr_serdev - register tty or serdev device
142*8cde11b2SJohan Hovold  * @port: tty_port of the device
143*8cde11b2SJohan Hovold  * @driver: tty_driver for this device
144*8cde11b2SJohan Hovold  * @index: index of the tty
145*8cde11b2SJohan Hovold  * @device: parent if exists, otherwise NULL
146*8cde11b2SJohan Hovold  * @drvdata: driver data for the device
147*8cde11b2SJohan Hovold  * @attr_grp: attribute group for the device
148*8cde11b2SJohan Hovold  *
149*8cde11b2SJohan Hovold  * Register a serdev or tty device depending on if the parent device has any
150*8cde11b2SJohan Hovold  * defined serdev clients or not.
151*8cde11b2SJohan Hovold  */
152*8cde11b2SJohan Hovold struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
153*8cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index,
154*8cde11b2SJohan Hovold 		struct device *device, void *drvdata,
155*8cde11b2SJohan Hovold 		const struct attribute_group **attr_grp)
156*8cde11b2SJohan Hovold {
157*8cde11b2SJohan Hovold 	struct device *dev;
158*8cde11b2SJohan Hovold 
159*8cde11b2SJohan Hovold 	tty_port_link_device(port, driver, index);
160*8cde11b2SJohan Hovold 
161*8cde11b2SJohan Hovold 	dev = serdev_tty_port_register(port, device, driver, index);
162*8cde11b2SJohan Hovold 	if (PTR_ERR(dev) != -ENODEV) {
163*8cde11b2SJohan Hovold 		/* Skip creating cdev if we registered a serdev device */
164*8cde11b2SJohan Hovold 		return dev;
165*8cde11b2SJohan Hovold 	}
166*8cde11b2SJohan Hovold 
167*8cde11b2SJohan Hovold 	return tty_register_device_attr(driver, index, device, drvdata,
168*8cde11b2SJohan Hovold 			attr_grp);
169*8cde11b2SJohan Hovold }
170*8cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
171*8cde11b2SJohan Hovold 
172*8cde11b2SJohan Hovold /**
173*8cde11b2SJohan Hovold  * tty_port_register_device_serdev - register tty or serdev device
174*8cde11b2SJohan Hovold  * @port: tty_port of the device
175*8cde11b2SJohan Hovold  * @driver: tty_driver for this device
176*8cde11b2SJohan Hovold  * @index: index of the tty
177*8cde11b2SJohan Hovold  * @device: parent if exists, otherwise NULL
178*8cde11b2SJohan Hovold  *
179*8cde11b2SJohan Hovold  * Register a serdev or tty device depending on if the parent device has any
180*8cde11b2SJohan Hovold  * defined serdev clients or not.
181*8cde11b2SJohan Hovold  */
182*8cde11b2SJohan Hovold struct device *tty_port_register_device_serdev(struct tty_port *port,
183*8cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index,
184*8cde11b2SJohan Hovold 		struct device *device)
185*8cde11b2SJohan Hovold {
186*8cde11b2SJohan Hovold 	return tty_port_register_device_attr_serdev(port, driver, index,
187*8cde11b2SJohan Hovold 			device, NULL, NULL);
188*8cde11b2SJohan Hovold }
189*8cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
190*8cde11b2SJohan Hovold 
191*8cde11b2SJohan Hovold /**
192*8cde11b2SJohan Hovold  * tty_port_unregister_device - deregister a tty or serdev device
193*8cde11b2SJohan Hovold  * @port: tty_port of the device
194*8cde11b2SJohan Hovold  * @driver: tty_driver for this device
195*8cde11b2SJohan Hovold  * @index: index of the tty
196*8cde11b2SJohan Hovold  *
197*8cde11b2SJohan Hovold  * If a tty or serdev device is registered with a call to
198*8cde11b2SJohan Hovold  * tty_port_register_device_serdev() then this function must be called when
199*8cde11b2SJohan Hovold  * the device is gone.
200*8cde11b2SJohan Hovold  */
201*8cde11b2SJohan Hovold void tty_port_unregister_device(struct tty_port *port,
202*8cde11b2SJohan Hovold 		struct tty_driver *driver, unsigned index)
203*8cde11b2SJohan Hovold {
204*8cde11b2SJohan Hovold 	int ret;
205*8cde11b2SJohan Hovold 
206*8cde11b2SJohan Hovold 	ret = serdev_tty_port_unregister(port);
207*8cde11b2SJohan Hovold 	if (ret == 0)
208*8cde11b2SJohan Hovold 		return;
209*8cde11b2SJohan Hovold 
210*8cde11b2SJohan Hovold 	tty_unregister_device(driver, index);
211*8cde11b2SJohan Hovold }
212*8cde11b2SJohan Hovold EXPORT_SYMBOL_GPL(tty_port_unregister_device);
213*8cde11b2SJohan Hovold 
21496fd7ce5SGreg Kroah-Hartman int tty_port_alloc_xmit_buf(struct tty_port *port)
21596fd7ce5SGreg Kroah-Hartman {
21696fd7ce5SGreg Kroah-Hartman 	/* We may sleep in get_zeroed_page() */
21796fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
21896fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
21996fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
22096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
22196fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf == NULL)
22296fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
22396fd7ce5SGreg Kroah-Hartman 	return 0;
22496fd7ce5SGreg Kroah-Hartman }
22596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
22696fd7ce5SGreg Kroah-Hartman 
22796fd7ce5SGreg Kroah-Hartman void tty_port_free_xmit_buf(struct tty_port *port)
22896fd7ce5SGreg Kroah-Hartman {
22996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->buf_mutex);
23096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf != NULL) {
23196fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
23296fd7ce5SGreg Kroah-Hartman 		port->xmit_buf = NULL;
23396fd7ce5SGreg Kroah-Hartman 	}
23496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->buf_mutex);
23596fd7ce5SGreg Kroah-Hartman }
23696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_free_xmit_buf);
23796fd7ce5SGreg Kroah-Hartman 
238de274bfeSJiri Slaby /**
239de274bfeSJiri Slaby  * tty_port_destroy -- destroy inited port
240de274bfeSJiri Slaby  * @port: tty port to be doestroyed
241de274bfeSJiri Slaby  *
242de274bfeSJiri Slaby  * When a port was initialized using tty_port_init, one has to destroy the
243de274bfeSJiri Slaby  * port by this function. Either indirectly by using tty_port refcounting
244de274bfeSJiri Slaby  * (tty_port_put) or directly if refcounting is not used.
245de274bfeSJiri Slaby  */
246de274bfeSJiri Slaby void tty_port_destroy(struct tty_port *port)
247de274bfeSJiri Slaby {
248e176058fSPeter Hurley 	tty_buffer_cancel_work(port);
249de274bfeSJiri Slaby 	tty_buffer_free_all(port);
250de274bfeSJiri Slaby }
251de274bfeSJiri Slaby EXPORT_SYMBOL(tty_port_destroy);
252de274bfeSJiri Slaby 
25396fd7ce5SGreg Kroah-Hartman static void tty_port_destructor(struct kref *kref)
25496fd7ce5SGreg Kroah-Hartman {
25596fd7ce5SGreg Kroah-Hartman 	struct tty_port *port = container_of(kref, struct tty_port, kref);
256e3bfea23SPeter Hurley 
257e3bfea23SPeter Hurley 	/* check if last port ref was dropped before tty release */
258e3bfea23SPeter Hurley 	if (WARN_ON(port->itty))
259e3bfea23SPeter Hurley 		return;
26096fd7ce5SGreg Kroah-Hartman 	if (port->xmit_buf)
26196fd7ce5SGreg Kroah-Hartman 		free_page((unsigned long)port->xmit_buf);
262de274bfeSJiri Slaby 	tty_port_destroy(port);
26381c79838SJiri Slaby 	if (port->ops && port->ops->destruct)
26496fd7ce5SGreg Kroah-Hartman 		port->ops->destruct(port);
26596fd7ce5SGreg Kroah-Hartman 	else
26696fd7ce5SGreg Kroah-Hartman 		kfree(port);
26796fd7ce5SGreg Kroah-Hartman }
26896fd7ce5SGreg Kroah-Hartman 
26996fd7ce5SGreg Kroah-Hartman void tty_port_put(struct tty_port *port)
27096fd7ce5SGreg Kroah-Hartman {
27196fd7ce5SGreg Kroah-Hartman 	if (port)
27296fd7ce5SGreg Kroah-Hartman 		kref_put(&port->kref, tty_port_destructor);
27396fd7ce5SGreg Kroah-Hartman }
27496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_put);
27596fd7ce5SGreg Kroah-Hartman 
27696fd7ce5SGreg Kroah-Hartman /**
27796fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_get	-	get a tty reference
27896fd7ce5SGreg Kroah-Hartman  *	@port: tty port
27996fd7ce5SGreg Kroah-Hartman  *
28096fd7ce5SGreg Kroah-Hartman  *	Return a refcount protected tty instance or NULL if the port is not
28196fd7ce5SGreg Kroah-Hartman  *	associated with a tty (eg due to close or hangup)
28296fd7ce5SGreg Kroah-Hartman  */
28396fd7ce5SGreg Kroah-Hartman 
28496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_port_tty_get(struct tty_port *port)
28596fd7ce5SGreg Kroah-Hartman {
28696fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
28796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
28896fd7ce5SGreg Kroah-Hartman 
28996fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
29096fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(port->tty);
29196fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
29296fd7ce5SGreg Kroah-Hartman 	return tty;
29396fd7ce5SGreg Kroah-Hartman }
29496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_get);
29596fd7ce5SGreg Kroah-Hartman 
29696fd7ce5SGreg Kroah-Hartman /**
29796fd7ce5SGreg Kroah-Hartman  *	tty_port_tty_set	-	set the tty of a port
29896fd7ce5SGreg Kroah-Hartman  *	@port: tty port
29996fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
30096fd7ce5SGreg Kroah-Hartman  *
30196fd7ce5SGreg Kroah-Hartman  *	Associate the port and tty pair. Manages any internal refcounts.
30296fd7ce5SGreg Kroah-Hartman  *	Pass NULL to deassociate a port
30396fd7ce5SGreg Kroah-Hartman  */
30496fd7ce5SGreg Kroah-Hartman 
30596fd7ce5SGreg Kroah-Hartman void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
30696fd7ce5SGreg Kroah-Hartman {
30796fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
30896fd7ce5SGreg Kroah-Hartman 
30996fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
31096fd7ce5SGreg Kroah-Hartman 	tty_kref_put(port->tty);
31196fd7ce5SGreg Kroah-Hartman 	port->tty = tty_kref_get(tty);
31296fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
31396fd7ce5SGreg Kroah-Hartman }
31496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_tty_set);
31596fd7ce5SGreg Kroah-Hartman 
316957dacaeSJohan Hovold static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
31796fd7ce5SGreg Kroah-Hartman {
31896fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
3198bde9658SJohan Hovold 	if (port->console)
3208bde9658SJohan Hovold 		goto out;
3218bde9658SJohan Hovold 
322d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
323d41861caSPeter Hurley 		tty_port_set_initialized(port, 0);
324957dacaeSJohan Hovold 		/*
325957dacaeSJohan Hovold 		 * Drop DTR/RTS if HUPCL is set. This causes any attached
326957dacaeSJohan Hovold 		 * modem to hang up the line.
327957dacaeSJohan Hovold 		 */
328957dacaeSJohan Hovold 		if (tty && C_HUPCL(tty))
329957dacaeSJohan Hovold 			tty_port_lower_dtr_rts(port);
330957dacaeSJohan Hovold 
3318bde9658SJohan Hovold 		if (port->ops->shutdown)
33296fd7ce5SGreg Kroah-Hartman 			port->ops->shutdown(port);
3338bde9658SJohan Hovold 	}
3348bde9658SJohan Hovold out:
33596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
33696fd7ce5SGreg Kroah-Hartman }
33796fd7ce5SGreg Kroah-Hartman 
33896fd7ce5SGreg Kroah-Hartman /**
33996fd7ce5SGreg Kroah-Hartman  *	tty_port_hangup		-	hangup helper
34096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
34196fd7ce5SGreg Kroah-Hartman  *
34296fd7ce5SGreg Kroah-Hartman  *	Perform port level tty hangup flag and count changes. Drop the tty
34396fd7ce5SGreg Kroah-Hartman  *	reference.
3449c9928bdSPeter Hurley  *
3459c9928bdSPeter Hurley  *	Caller holds tty lock.
34696fd7ce5SGreg Kroah-Hartman  */
34796fd7ce5SGreg Kroah-Hartman 
34896fd7ce5SGreg Kroah-Hartman void tty_port_hangup(struct tty_port *port)
34996fd7ce5SGreg Kroah-Hartman {
350957dacaeSJohan Hovold 	struct tty_struct *tty;
35196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
35296fd7ce5SGreg Kroah-Hartman 
35396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
35496fd7ce5SGreg Kroah-Hartman 	port->count = 0;
355957dacaeSJohan Hovold 	tty = port->tty;
356957dacaeSJohan Hovold 	if (tty)
357957dacaeSJohan Hovold 		set_bit(TTY_IO_ERROR, &tty->flags);
35896fd7ce5SGreg Kroah-Hartman 	port->tty = NULL;
35996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
360807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
361957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
362957dacaeSJohan Hovold 	tty_kref_put(tty);
36396fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->open_wait);
36496fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible(&port->delta_msr_wait);
36596fd7ce5SGreg Kroah-Hartman }
36696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_hangup);
36796fd7ce5SGreg Kroah-Hartman 
36896fd7ce5SGreg Kroah-Hartman /**
369aa27a094SJiri Slaby  * tty_port_tty_hangup - helper to hang up a tty
370aa27a094SJiri Slaby  *
371aa27a094SJiri Slaby  * @port: tty port
372aa27a094SJiri Slaby  * @check_clocal: hang only ttys with CLOCAL unset?
373aa27a094SJiri Slaby  */
374aa27a094SJiri Slaby void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
375aa27a094SJiri Slaby {
376aa27a094SJiri Slaby 	struct tty_struct *tty = tty_port_tty_get(port);
377aa27a094SJiri Slaby 
3781d9e689cSGianluca Anzolin 	if (tty && (!check_clocal || !C_CLOCAL(tty)))
379aa27a094SJiri Slaby 		tty_hangup(tty);
380aa27a094SJiri Slaby 	tty_kref_put(tty);
381aa27a094SJiri Slaby }
382aa27a094SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
383aa27a094SJiri Slaby 
384aa27a094SJiri Slaby /**
3856aad04f2SJiri Slaby  * tty_port_tty_wakeup - helper to wake up a tty
3866aad04f2SJiri Slaby  *
3876aad04f2SJiri Slaby  * @port: tty port
3886aad04f2SJiri Slaby  */
3896aad04f2SJiri Slaby void tty_port_tty_wakeup(struct tty_port *port)
3906aad04f2SJiri Slaby {
391c3485ee0SRob Herring 	port->client_ops->write_wakeup(port);
3926aad04f2SJiri Slaby }
3936aad04f2SJiri Slaby EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
3946aad04f2SJiri Slaby 
3956aad04f2SJiri Slaby /**
39696fd7ce5SGreg Kroah-Hartman  *	tty_port_carrier_raised	-	carrier raised check
39796fd7ce5SGreg Kroah-Hartman  *	@port: tty port
39896fd7ce5SGreg Kroah-Hartman  *
39996fd7ce5SGreg Kroah-Hartman  *	Wrapper for the carrier detect logic. For the moment this is used
40096fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
40196fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
40296fd7ce5SGreg Kroah-Hartman  */
40396fd7ce5SGreg Kroah-Hartman 
40496fd7ce5SGreg Kroah-Hartman int tty_port_carrier_raised(struct tty_port *port)
40596fd7ce5SGreg Kroah-Hartman {
40696fd7ce5SGreg Kroah-Hartman 	if (port->ops->carrier_raised == NULL)
40796fd7ce5SGreg Kroah-Hartman 		return 1;
40896fd7ce5SGreg Kroah-Hartman 	return port->ops->carrier_raised(port);
40996fd7ce5SGreg Kroah-Hartman }
41096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_carrier_raised);
41196fd7ce5SGreg Kroah-Hartman 
41296fd7ce5SGreg Kroah-Hartman /**
41396fd7ce5SGreg Kroah-Hartman  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
41496fd7ce5SGreg Kroah-Hartman  *	@port: tty port
41596fd7ce5SGreg Kroah-Hartman  *
41696fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
41796fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
41896fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
41996fd7ce5SGreg Kroah-Hartman  */
42096fd7ce5SGreg Kroah-Hartman 
42196fd7ce5SGreg Kroah-Hartman void tty_port_raise_dtr_rts(struct tty_port *port)
42296fd7ce5SGreg Kroah-Hartman {
42396fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
42496fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 1);
42596fd7ce5SGreg Kroah-Hartman }
42696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_raise_dtr_rts);
42796fd7ce5SGreg Kroah-Hartman 
42896fd7ce5SGreg Kroah-Hartman /**
42996fd7ce5SGreg Kroah-Hartman  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
43096fd7ce5SGreg Kroah-Hartman  *	@port: tty port
43196fd7ce5SGreg Kroah-Hartman  *
43296fd7ce5SGreg Kroah-Hartman  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
43396fd7ce5SGreg Kroah-Hartman  *	to hide some internal details. This will eventually become entirely
43496fd7ce5SGreg Kroah-Hartman  *	internal to the tty port.
43596fd7ce5SGreg Kroah-Hartman  */
43696fd7ce5SGreg Kroah-Hartman 
43796fd7ce5SGreg Kroah-Hartman void tty_port_lower_dtr_rts(struct tty_port *port)
43896fd7ce5SGreg Kroah-Hartman {
43996fd7ce5SGreg Kroah-Hartman 	if (port->ops->dtr_rts)
44096fd7ce5SGreg Kroah-Hartman 		port->ops->dtr_rts(port, 0);
44196fd7ce5SGreg Kroah-Hartman }
44296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_lower_dtr_rts);
44396fd7ce5SGreg Kroah-Hartman 
44496fd7ce5SGreg Kroah-Hartman /**
44596fd7ce5SGreg Kroah-Hartman  *	tty_port_block_til_ready	-	Waiting logic for tty open
44696fd7ce5SGreg Kroah-Hartman  *	@port: the tty port being opened
44796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty device being bound
448ed3f0af8SAlan Cox  *	@filp: the file pointer of the opener or NULL
44996fd7ce5SGreg Kroah-Hartman  *
45096fd7ce5SGreg Kroah-Hartman  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
45196fd7ce5SGreg Kroah-Hartman  *	Handles:
45296fd7ce5SGreg Kroah-Hartman  *		- hangup (both before and during)
45396fd7ce5SGreg Kroah-Hartman  *		- non blocking open
45496fd7ce5SGreg Kroah-Hartman  *		- rts/dtr/dcd
45596fd7ce5SGreg Kroah-Hartman  *		- signals
45696fd7ce5SGreg Kroah-Hartman  *		- port flags and counts
45796fd7ce5SGreg Kroah-Hartman  *
45896fd7ce5SGreg Kroah-Hartman  *	The passed tty_port must implement the carrier_raised method if it can
45996fd7ce5SGreg Kroah-Hartman  *	do carrier detect and the dtr_rts method if it supports software
46096fd7ce5SGreg Kroah-Hartman  *	management of these lines. Note that the dtr/rts raise is done each
46196fd7ce5SGreg Kroah-Hartman  *	iteration as a hangup may have previously dropped them while we wait.
462c590f6b6SPeter Hurley  *
463c590f6b6SPeter Hurley  *	Caller holds tty lock.
464c590f6b6SPeter Hurley  *
465c590f6b6SPeter Hurley  *      NB: May drop and reacquire tty lock when blocking, so tty and tty_port
466c590f6b6SPeter Hurley  *      may have changed state (eg., may have been hung up).
46796fd7ce5SGreg Kroah-Hartman  */
46896fd7ce5SGreg Kroah-Hartman 
46996fd7ce5SGreg Kroah-Hartman int tty_port_block_til_ready(struct tty_port *port,
47096fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
47196fd7ce5SGreg Kroah-Hartman {
47296fd7ce5SGreg Kroah-Hartman 	int do_clocal = 0, retval;
47396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
47496fd7ce5SGreg Kroah-Hartman 	DEFINE_WAIT(wait);
47596fd7ce5SGreg Kroah-Hartman 
47696fd7ce5SGreg Kroah-Hartman 	/* if non-blocking mode is set we can pass directly to open unless
47796fd7ce5SGreg Kroah-Hartman 	   the port has just hung up or is in another error state */
47818900ca6SPeter Hurley 	if (tty_io_error(tty)) {
479807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
48096fd7ce5SGreg Kroah-Hartman 		return 0;
48196fd7ce5SGreg Kroah-Hartman 	}
482ed3f0af8SAlan Cox 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
48396fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
4849db276f8SPeter Hurley 		if (C_BAUD(tty))
48596fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
486807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
48796fd7ce5SGreg Kroah-Hartman 		return 0;
48896fd7ce5SGreg Kroah-Hartman 	}
48996fd7ce5SGreg Kroah-Hartman 
49096fd7ce5SGreg Kroah-Hartman 	if (C_CLOCAL(tty))
49196fd7ce5SGreg Kroah-Hartman 		do_clocal = 1;
49296fd7ce5SGreg Kroah-Hartman 
49396fd7ce5SGreg Kroah-Hartman 	/* Block waiting until we can proceed. We may need to wait for the
49496fd7ce5SGreg Kroah-Hartman 	   carrier, but we must also wait for any close that is in progress
49596fd7ce5SGreg Kroah-Hartman 	   before the next open may complete */
49696fd7ce5SGreg Kroah-Hartman 
49796fd7ce5SGreg Kroah-Hartman 	retval = 0;
49896fd7ce5SGreg Kroah-Hartman 
49996fd7ce5SGreg Kroah-Hartman 	/* The port lock protects the port counts */
50096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
50196fd7ce5SGreg Kroah-Hartman 	port->count--;
50296fd7ce5SGreg Kroah-Hartman 	port->blocked_open++;
50396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
50496fd7ce5SGreg Kroah-Hartman 
50596fd7ce5SGreg Kroah-Hartman 	while (1) {
50696fd7ce5SGreg Kroah-Hartman 		/* Indicate we are open */
507d41861caSPeter Hurley 		if (C_BAUD(tty) && tty_port_initialized(port))
50896fd7ce5SGreg Kroah-Hartman 			tty_port_raise_dtr_rts(port);
50996fd7ce5SGreg Kroah-Hartman 
51096fd7ce5SGreg Kroah-Hartman 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
51196fd7ce5SGreg Kroah-Hartman 		/* Check for a hangup or uninitialised port.
51296fd7ce5SGreg Kroah-Hartman 							Return accordingly */
513d41861caSPeter Hurley 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
51496fd7ce5SGreg Kroah-Hartman 			if (port->flags & ASYNC_HUP_NOTIFY)
51596fd7ce5SGreg Kroah-Hartman 				retval = -EAGAIN;
51696fd7ce5SGreg Kroah-Hartman 			else
51796fd7ce5SGreg Kroah-Hartman 				retval = -ERESTARTSYS;
51896fd7ce5SGreg Kroah-Hartman 			break;
51996fd7ce5SGreg Kroah-Hartman 		}
5200eee50afSJiri Slaby 		/*
5210eee50afSJiri Slaby 		 * Probe the carrier. For devices with no carrier detect
5220eee50afSJiri Slaby 		 * tty_port_carrier_raised will always return true.
5230eee50afSJiri Slaby 		 * Never ask drivers if CLOCAL is set, this causes troubles
5240eee50afSJiri Slaby 		 * on some hardware.
5250eee50afSJiri Slaby 		 */
526fef062cbSPeter Hurley 		if (do_clocal || tty_port_carrier_raised(port))
52796fd7ce5SGreg Kroah-Hartman 			break;
52896fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current)) {
52996fd7ce5SGreg Kroah-Hartman 			retval = -ERESTARTSYS;
53096fd7ce5SGreg Kroah-Hartman 			break;
53196fd7ce5SGreg Kroah-Hartman 		}
53289c8d91eSAlan Cox 		tty_unlock(tty);
53396fd7ce5SGreg Kroah-Hartman 		schedule();
53489c8d91eSAlan Cox 		tty_lock(tty);
53596fd7ce5SGreg Kroah-Hartman 	}
53696fd7ce5SGreg Kroah-Hartman 	finish_wait(&port->open_wait, &wait);
53796fd7ce5SGreg Kroah-Hartman 
53896fd7ce5SGreg Kroah-Hartman 	/* Update counts. A parallel hangup will have set count to zero and
53996fd7ce5SGreg Kroah-Hartman 	   we must not mess that up further */
54096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
54196fd7ce5SGreg Kroah-Hartman 	if (!tty_hung_up_p(filp))
54296fd7ce5SGreg Kroah-Hartman 		port->count++;
54396fd7ce5SGreg Kroah-Hartman 	port->blocked_open--;
54496fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
545807c8d81SPeter Hurley 	if (retval == 0)
546807c8d81SPeter Hurley 		tty_port_set_active(port, 1);
54796fd7ce5SGreg Kroah-Hartman 	return retval;
54896fd7ce5SGreg Kroah-Hartman }
54996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_block_til_ready);
55096fd7ce5SGreg Kroah-Hartman 
551b74414f5SJohan Hovold static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
552b74414f5SJohan Hovold {
553b74414f5SJohan Hovold 	unsigned int bps = tty_get_baud_rate(tty);
554b74414f5SJohan Hovold 	long timeout;
555b74414f5SJohan Hovold 
556b74414f5SJohan Hovold 	if (bps > 1200) {
557b74414f5SJohan Hovold 		timeout = (HZ * 10 * port->drain_delay) / bps;
558b74414f5SJohan Hovold 		timeout = max_t(long, timeout, HZ / 10);
559b74414f5SJohan Hovold 	} else {
560b74414f5SJohan Hovold 		timeout = 2 * HZ;
561b74414f5SJohan Hovold 	}
562b74414f5SJohan Hovold 	schedule_timeout_interruptible(timeout);
563b74414f5SJohan Hovold }
564b74414f5SJohan Hovold 
56579c1faa4SPeter Hurley /* Caller holds tty lock. */
56696fd7ce5SGreg Kroah-Hartman int tty_port_close_start(struct tty_port *port,
56796fd7ce5SGreg Kroah-Hartman 				struct tty_struct *tty, struct file *filp)
56896fd7ce5SGreg Kroah-Hartman {
56996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
57096fd7ce5SGreg Kroah-Hartman 
571633caba8SPeter Hurley 	if (tty_hung_up_p(filp))
57296fd7ce5SGreg Kroah-Hartman 		return 0;
57396fd7ce5SGreg Kroah-Hartman 
574633caba8SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
57596fd7ce5SGreg Kroah-Hartman 	if (tty->count == 1 && port->count != 1) {
576339f36baSPeter Hurley 		tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
57796fd7ce5SGreg Kroah-Hartman 			 port->count);
57896fd7ce5SGreg Kroah-Hartman 		port->count = 1;
57996fd7ce5SGreg Kroah-Hartman 	}
58096fd7ce5SGreg Kroah-Hartman 	if (--port->count < 0) {
581339f36baSPeter Hurley 		tty_warn(tty, "%s: bad port count (%d)\n", __func__,
58296fd7ce5SGreg Kroah-Hartman 			 port->count);
58396fd7ce5SGreg Kroah-Hartman 		port->count = 0;
58496fd7ce5SGreg Kroah-Hartman 	}
58596fd7ce5SGreg Kroah-Hartman 
58696fd7ce5SGreg Kroah-Hartman 	if (port->count) {
58796fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
58896fd7ce5SGreg Kroah-Hartman 		return 0;
58996fd7ce5SGreg Kroah-Hartman 	}
59096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
5910b2588caSJohan Hovold 
592ddc7b758SPeter Hurley 	tty->closing = 1;
593ddc7b758SPeter Hurley 
594d41861caSPeter Hurley 	if (tty_port_initialized(port)) {
59596fd7ce5SGreg Kroah-Hartman 		/* Don't block on a stalled port, just pull the chain */
59696fd7ce5SGreg Kroah-Hartman 		if (tty->flow_stopped)
59796fd7ce5SGreg Kroah-Hartman 			tty_driver_flush_buffer(tty);
5980b2588caSJohan Hovold 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
59979c1faa4SPeter Hurley 			tty_wait_until_sent(tty, port->closing_wait);
600b74414f5SJohan Hovold 		if (port->drain_delay)
601b74414f5SJohan Hovold 			tty_port_drain_delay(port, tty);
6020b2588caSJohan Hovold 	}
60396fd7ce5SGreg Kroah-Hartman 	/* Flush the ldisc buffering */
60496fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
60596fd7ce5SGreg Kroah-Hartman 
606469d6d06SPeter Hurley 	/* Report to caller this is the last port reference */
60796fd7ce5SGreg Kroah-Hartman 	return 1;
60896fd7ce5SGreg Kroah-Hartman }
60996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_start);
61096fd7ce5SGreg Kroah-Hartman 
6110733db91SPeter Hurley /* Caller holds tty lock */
61296fd7ce5SGreg Kroah-Hartman void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
61396fd7ce5SGreg Kroah-Hartman {
61496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
61596fd7ce5SGreg Kroah-Hartman 
6163f40f5b2SPeter Hurley 	tty_ldisc_flush(tty);
61796fd7ce5SGreg Kroah-Hartman 	tty->closing = 0;
61896fd7ce5SGreg Kroah-Hartman 
619ddc7b758SPeter Hurley 	spin_lock_irqsave(&port->lock, flags);
620ddc7b758SPeter Hurley 
62196fd7ce5SGreg Kroah-Hartman 	if (port->blocked_open) {
62296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&port->lock, flags);
6235823323eSPeter Hurley 		if (port->close_delay)
6245823323eSPeter Hurley 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
62596fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&port->lock, flags);
62696fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible(&port->open_wait);
62796fd7ce5SGreg Kroah-Hartman 	}
62896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
629807c8d81SPeter Hurley 	tty_port_set_active(port, 0);
63096fd7ce5SGreg Kroah-Hartman }
63196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close_end);
63296fd7ce5SGreg Kroah-Hartman 
6330733db91SPeter Hurley /**
6340733db91SPeter Hurley  * tty_port_close
6350733db91SPeter Hurley  *
6360733db91SPeter Hurley  * Caller holds tty lock
6370733db91SPeter Hurley  */
63896fd7ce5SGreg Kroah-Hartman void tty_port_close(struct tty_port *port, struct tty_struct *tty,
63996fd7ce5SGreg Kroah-Hartman 							struct file *filp)
64096fd7ce5SGreg Kroah-Hartman {
64196fd7ce5SGreg Kroah-Hartman 	if (tty_port_close_start(port, tty, filp) == 0)
64296fd7ce5SGreg Kroah-Hartman 		return;
643957dacaeSJohan Hovold 	tty_port_shutdown(port, tty);
64496fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_IO_ERROR, &tty->flags);
64596fd7ce5SGreg Kroah-Hartman 	tty_port_close_end(port, tty);
64696fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, NULL);
64796fd7ce5SGreg Kroah-Hartman }
64896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_close);
64996fd7ce5SGreg Kroah-Hartman 
65072a33bf5SJiri Slaby /**
65172a33bf5SJiri Slaby  * tty_port_install - generic tty->ops->install handler
65272a33bf5SJiri Slaby  * @port: tty_port of the device
65372a33bf5SJiri Slaby  * @driver: tty_driver for this device
65472a33bf5SJiri Slaby  * @tty: tty to be installed
65572a33bf5SJiri Slaby  *
65672a33bf5SJiri Slaby  * It is the same as tty_standard_install except the provided @port is linked
65772a33bf5SJiri Slaby  * to a concrete tty specified by @tty. Use this or tty_port_register_device
65872a33bf5SJiri Slaby  * (or both). Call tty_port_link_device as a last resort.
65972a33bf5SJiri Slaby  */
660695586caSJiri Slaby int tty_port_install(struct tty_port *port, struct tty_driver *driver,
661695586caSJiri Slaby 		struct tty_struct *tty)
662695586caSJiri Slaby {
663695586caSJiri Slaby 	tty->port = port;
664695586caSJiri Slaby 	return tty_standard_install(driver, tty);
665695586caSJiri Slaby }
666695586caSJiri Slaby EXPORT_SYMBOL_GPL(tty_port_install);
667695586caSJiri Slaby 
668addd4672SPeter Hurley /**
669addd4672SPeter Hurley  * tty_port_open
670addd4672SPeter Hurley  *
671addd4672SPeter Hurley  * Caller holds tty lock.
672addd4672SPeter Hurley  *
673addd4672SPeter Hurley  * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
674addd4672SPeter Hurley  * tty and tty_port may have changed state (eg., may be hung up now)
675addd4672SPeter Hurley  */
67696fd7ce5SGreg Kroah-Hartman int tty_port_open(struct tty_port *port, struct tty_struct *tty,
67796fd7ce5SGreg Kroah-Hartman 							struct file *filp)
67896fd7ce5SGreg Kroah-Hartman {
67996fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&port->lock);
68096fd7ce5SGreg Kroah-Hartman 	++port->count;
68196fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&port->lock);
68296fd7ce5SGreg Kroah-Hartman 	tty_port_tty_set(port, tty);
68396fd7ce5SGreg Kroah-Hartman 
68496fd7ce5SGreg Kroah-Hartman 	/*
68596fd7ce5SGreg Kroah-Hartman 	 * Do the device-specific open only if the hardware isn't
68696fd7ce5SGreg Kroah-Hartman 	 * already initialized. Serialize open and shutdown using the
68796fd7ce5SGreg Kroah-Hartman 	 * port mutex.
68896fd7ce5SGreg Kroah-Hartman 	 */
68996fd7ce5SGreg Kroah-Hartman 
69096fd7ce5SGreg Kroah-Hartman 	mutex_lock(&port->mutex);
69196fd7ce5SGreg Kroah-Hartman 
692d41861caSPeter Hurley 	if (!tty_port_initialized(port)) {
69396fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_IO_ERROR, &tty->flags);
69496fd7ce5SGreg Kroah-Hartman 		if (port->ops->activate) {
69596fd7ce5SGreg Kroah-Hartman 			int retval = port->ops->activate(port, tty);
69696fd7ce5SGreg Kroah-Hartman 			if (retval) {
69796fd7ce5SGreg Kroah-Hartman 				mutex_unlock(&port->mutex);
69896fd7ce5SGreg Kroah-Hartman 				return retval;
69996fd7ce5SGreg Kroah-Hartman 			}
70096fd7ce5SGreg Kroah-Hartman 		}
701d41861caSPeter Hurley 		tty_port_set_initialized(port, 1);
70296fd7ce5SGreg Kroah-Hartman 	}
70396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&port->mutex);
70496fd7ce5SGreg Kroah-Hartman 	return tty_port_block_til_ready(port, tty, filp);
70596fd7ce5SGreg Kroah-Hartman }
70696fd7ce5SGreg Kroah-Hartman 
70796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_port_open);
708