blob: 346d20f4a4862e62a8fe27e45d147852b5a70b99 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Alan Cox9e485652008-10-13 10:37:07 +01002/*
3 * Tty port functions
4 */
5
6#include <linux/types.h>
7#include <linux/errno.h>
8#include <linux/tty.h>
9#include <linux/tty_driver.h>
10#include <linux/tty_flip.h>
Alan Cox3e616962009-01-02 13:45:26 +000011#include <linux/serial.h>
Alan Cox9e485652008-10-13 10:37:07 +010012#include <linux/timer.h>
13#include <linux/string.h>
14#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Alan Cox9e485652008-10-13 10:37:07 +010016#include <linux/wait.h>
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/module.h>
Johan Hovold8cde11b2017-05-18 17:33:00 +020020#include <linux/serdev.h>
Alan Cox9e485652008-10-13 10:37:07 +010021
Rob Herringc3485ee2017-02-02 13:48:05 -060022static int tty_port_default_receive_buf(struct tty_port *port,
23 const unsigned char *p,
24 const unsigned char *f, size_t count)
25{
26 int ret;
27 struct tty_struct *tty;
28 struct tty_ldisc *disc;
29
30 tty = READ_ONCE(port->itty);
31 if (!tty)
32 return 0;
33
34 disc = tty_ldisc_ref(tty);
35 if (!disc)
36 return 0;
37
38 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
39
40 tty_ldisc_deref(disc);
41
42 return ret;
43}
44
45static void tty_port_default_wakeup(struct tty_port *port)
46{
47 struct tty_struct *tty = tty_port_tty_get(port);
48
49 if (tty) {
50 tty_wakeup(tty);
51 tty_kref_put(tty);
52 }
53}
54
Johan Hovold0c5aae52020-02-10 15:57:30 +010055const struct tty_port_client_operations tty_port_default_client_ops = {
Rob Herringc3485ee2017-02-02 13:48:05 -060056 .receive_buf = tty_port_default_receive_buf,
57 .write_wakeup = tty_port_default_wakeup,
58};
Johan Hovold0c5aae52020-02-10 15:57:30 +010059EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
Rob Herringc3485ee2017-02-02 13:48:05 -060060
Alan Cox9e485652008-10-13 10:37:07 +010061void tty_port_init(struct tty_port *port)
62{
63 memset(port, 0, sizeof(*port));
Jiri Slabyecbbfd42012-10-18 22:26:47 +020064 tty_buffer_init(port);
Alan Cox9e485652008-10-13 10:37:07 +010065 init_waitqueue_head(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -070066 init_waitqueue_head(&port->delta_msr_wait);
Alan Cox9e485652008-10-13 10:37:07 +010067 mutex_init(&port->mutex);
Alan Cox44e49092009-11-30 13:16:41 +000068 mutex_init(&port->buf_mutex);
Alan Cox4a90f092008-10-13 10:39:46 +010069 spin_lock_init(&port->lock);
Alan Cox9e485652008-10-13 10:37:07 +010070 port->close_delay = (50 * HZ) / 100;
71 port->closing_wait = (3000 * HZ) / 100;
Johan Hovold0c5aae52020-02-10 15:57:30 +010072 port->client_ops = &tty_port_default_client_ops;
Alan Cox568aafc2009-11-30 13:17:14 +000073 kref_init(&port->kref);
Alan Cox9e485652008-10-13 10:37:07 +010074}
75EXPORT_SYMBOL(tty_port_init);
76
Jiri Slaby72a33bf2012-08-07 21:47:49 +020077/**
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020078 * tty_port_link_device - link tty and tty_port
79 * @port: tty_port of the device
80 * @driver: tty_driver for this device
81 * @index: index of the tty
82 *
Antonio Borneo1926e5d2017-09-08 08:59:42 +020083 * Provide the tty layer with a link from a tty (specified by @index) to a
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020084 * tty_port (@port). Use this only if neither tty_port_register_device nor
85 * tty_port_install is used in the driver. If used, this has to be called before
86 * tty_register_driver.
87 */
88void tty_port_link_device(struct tty_port *port,
89 struct tty_driver *driver, unsigned index)
90{
91 if (WARN_ON(index >= driver->num))
92 return;
Sudip Mukherjee273f63292019-12-27 17:44:34 +000093 driver->ports[index] = port;
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020094}
95EXPORT_SYMBOL_GPL(tty_port_link_device);
96
97/**
Jiri Slaby72a33bf2012-08-07 21:47:49 +020098 * tty_port_register_device - register tty device
99 * @port: tty_port of the device
100 * @driver: tty_driver for this device
101 * @index: index of the tty
102 * @device: parent if exists, otherwise NULL
103 *
104 * It is the same as tty_register_device except the provided @port is linked to
105 * a concrete tty specified by @index. Use this or tty_port_install (or both).
106 * Call tty_port_link_device as a last resort.
107 */
Jiri Slaby057eb852012-06-04 13:35:37 +0200108struct device *tty_port_register_device(struct tty_port *port,
109 struct tty_driver *driver, unsigned index,
110 struct device *device)
111{
Rob Herring30863652017-01-16 16:54:30 -0600112 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
Jiri Slaby057eb852012-06-04 13:35:37 +0200113}
114EXPORT_SYMBOL_GPL(tty_port_register_device);
115
Tomas Hlavacekb1b79912012-09-06 23:17:47 +0200116/**
117 * tty_port_register_device_attr - register tty device
118 * @port: tty_port of the device
119 * @driver: tty_driver for this device
120 * @index: index of the tty
121 * @device: parent if exists, otherwise NULL
122 * @drvdata: Driver data to be set to device.
123 * @attr_grp: Attribute group to be set on device.
124 *
125 * It is the same as tty_register_device_attr except the provided @port is
126 * linked to a concrete tty specified by @index. Use this or tty_port_install
127 * (or both). Call tty_port_link_device as a last resort.
128 */
129struct device *tty_port_register_device_attr(struct tty_port *port,
130 struct tty_driver *driver, unsigned index,
131 struct device *device, void *drvdata,
132 const struct attribute_group **attr_grp)
133{
134 tty_port_link_device(port, driver, index);
135 return tty_register_device_attr(driver, index, device, drvdata,
136 attr_grp);
137}
138EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
139
Johan Hovold8cde11b2017-05-18 17:33:00 +0200140/**
141 * tty_port_register_device_attr_serdev - register tty or serdev device
142 * @port: tty_port of the device
143 * @driver: tty_driver for this device
144 * @index: index of the tty
145 * @device: parent if exists, otherwise NULL
146 * @drvdata: driver data for the device
147 * @attr_grp: attribute group for the device
148 *
149 * Register a serdev or tty device depending on if the parent device has any
150 * defined serdev clients or not.
151 */
152struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
153 struct tty_driver *driver, unsigned index,
154 struct device *device, void *drvdata,
155 const struct attribute_group **attr_grp)
156{
157 struct device *dev;
158
159 tty_port_link_device(port, driver, index);
160
161 dev = serdev_tty_port_register(port, device, driver, index);
162 if (PTR_ERR(dev) != -ENODEV) {
163 /* Skip creating cdev if we registered a serdev device */
164 return dev;
165 }
166
167 return tty_register_device_attr(driver, index, device, drvdata,
168 attr_grp);
169}
170EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
171
172/**
173 * tty_port_register_device_serdev - register tty or serdev device
174 * @port: tty_port of the device
175 * @driver: tty_driver for this device
176 * @index: index of the tty
177 * @device: parent if exists, otherwise NULL
178 *
179 * Register a serdev or tty device depending on if the parent device has any
180 * defined serdev clients or not.
181 */
182struct device *tty_port_register_device_serdev(struct tty_port *port,
183 struct tty_driver *driver, unsigned index,
184 struct device *device)
185{
186 return tty_port_register_device_attr_serdev(port, driver, index,
187 device, NULL, NULL);
188}
189EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
190
191/**
192 * tty_port_unregister_device - deregister a tty or serdev device
193 * @port: tty_port of the device
194 * @driver: tty_driver for this device
195 * @index: index of the tty
196 *
197 * If a tty or serdev device is registered with a call to
198 * tty_port_register_device_serdev() then this function must be called when
199 * the device is gone.
200 */
201void tty_port_unregister_device(struct tty_port *port,
202 struct tty_driver *driver, unsigned index)
203{
204 int ret;
205
206 ret = serdev_tty_port_unregister(port);
207 if (ret == 0)
208 return;
209
210 tty_unregister_device(driver, index);
211}
212EXPORT_SYMBOL_GPL(tty_port_unregister_device);
213
Alan Cox9e485652008-10-13 10:37:07 +0100214int tty_port_alloc_xmit_buf(struct tty_port *port)
215{
216 /* We may sleep in get_zeroed_page() */
Alan Cox44e49092009-11-30 13:16:41 +0000217 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100218 if (port->xmit_buf == NULL)
219 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
Alan Cox44e49092009-11-30 13:16:41 +0000220 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100221 if (port->xmit_buf == NULL)
222 return -ENOMEM;
223 return 0;
224}
225EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
226
227void tty_port_free_xmit_buf(struct tty_port *port)
228{
Alan Cox44e49092009-11-30 13:16:41 +0000229 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100230 if (port->xmit_buf != NULL) {
231 free_page((unsigned long)port->xmit_buf);
232 port->xmit_buf = NULL;
233 }
Alan Cox44e49092009-11-30 13:16:41 +0000234 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100235}
236EXPORT_SYMBOL(tty_port_free_xmit_buf);
237
Jiri Slabyde274bf2012-11-15 09:49:54 +0100238/**
239 * tty_port_destroy -- destroy inited port
Antonio Borneo1926e5d2017-09-08 08:59:42 +0200240 * @port: tty port to be destroyed
Jiri Slabyde274bf2012-11-15 09:49:54 +0100241 *
242 * When a port was initialized using tty_port_init, one has to destroy the
243 * port by this function. Either indirectly by using tty_port refcounting
244 * (tty_port_put) or directly if refcounting is not used.
245 */
246void tty_port_destroy(struct tty_port *port)
247{
Peter Hurleye1760582015-10-17 16:36:23 -0400248 tty_buffer_cancel_work(port);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100249 tty_buffer_free_all(port);
250}
251EXPORT_SYMBOL(tty_port_destroy);
252
Alan Cox568aafc2009-11-30 13:17:14 +0000253static void tty_port_destructor(struct kref *kref)
254{
255 struct tty_port *port = container_of(kref, struct tty_port, kref);
Peter Hurleye3bfea22013-09-18 20:42:39 -0400256
257 /* check if last port ref was dropped before tty release */
258 if (WARN_ON(port->itty))
259 return;
Alan Cox568aafc2009-11-30 13:17:14 +0000260 if (port->xmit_buf)
261 free_page((unsigned long)port->xmit_buf);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100262 tty_port_destroy(port);
Jiri Slaby81c79832012-11-15 09:49:49 +0100263 if (port->ops && port->ops->destruct)
Alan Cox568aafc2009-11-30 13:17:14 +0000264 port->ops->destruct(port);
265 else
266 kfree(port);
267}
268
269void tty_port_put(struct tty_port *port)
270{
271 if (port)
272 kref_put(&port->kref, tty_port_destructor);
273}
274EXPORT_SYMBOL(tty_port_put);
Alan Cox9e485652008-10-13 10:37:07 +0100275
Alan Cox4a90f092008-10-13 10:39:46 +0100276/**
277 * tty_port_tty_get - get a tty reference
278 * @port: tty port
279 *
280 * Return a refcount protected tty instance or NULL if the port is not
281 * associated with a tty (eg due to close or hangup)
282 */
Alan Cox4a90f092008-10-13 10:39:46 +0100283struct tty_struct *tty_port_tty_get(struct tty_port *port)
284{
285 unsigned long flags;
286 struct tty_struct *tty;
287
288 spin_lock_irqsave(&port->lock, flags);
289 tty = tty_kref_get(port->tty);
290 spin_unlock_irqrestore(&port->lock, flags);
291 return tty;
292}
293EXPORT_SYMBOL(tty_port_tty_get);
294
295/**
296 * tty_port_tty_set - set the tty of a port
297 * @port: tty port
298 * @tty: the tty
299 *
300 * Associate the port and tty pair. Manages any internal refcounts.
301 * Pass NULL to deassociate a port
302 */
Alan Cox4a90f092008-10-13 10:39:46 +0100303void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
304{
305 unsigned long flags;
306
307 spin_lock_irqsave(&port->lock, flags);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100308 tty_kref_put(port->tty);
Alan Coxcb4bca32008-10-21 13:47:44 +0100309 port->tty = tty_kref_get(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100310 spin_unlock_irqrestore(&port->lock, flags);
311}
312EXPORT_SYMBOL(tty_port_tty_set);
Alan Cox31f35932009-01-02 13:45:05 +0000313
Johan Hovold957daca2013-03-07 15:55:51 +0100314static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700315{
Alan Cox64bc3972009-10-06 16:06:11 +0100316 mutex_lock(&port->mutex);
Johan Hovold8bde9652013-03-07 15:55:48 +0100317 if (port->console)
318 goto out;
319
Peter Hurleyd41861c2016-04-09 17:53:25 -0700320 if (tty_port_initialized(port)) {
321 tty_port_set_initialized(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100322 /*
323 * Drop DTR/RTS if HUPCL is set. This causes any attached
324 * modem to hang up the line.
325 */
326 if (tty && C_HUPCL(tty))
327 tty_port_lower_dtr_rts(port);
328
Johan Hovold0d3cb6f2019-04-03 09:40:53 +0200329 if (port->ops->shutdown)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700330 port->ops->shutdown(port);
Johan Hovold8bde9652013-03-07 15:55:48 +0100331 }
332out:
Alan Cox64bc3972009-10-06 16:06:11 +0100333 mutex_unlock(&port->mutex);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700334}
335
Alan Cox31f35932009-01-02 13:45:05 +0000336/**
Alan Cox3e616962009-01-02 13:45:26 +0000337 * tty_port_hangup - hangup helper
338 * @port: tty port
339 *
340 * Perform port level tty hangup flag and count changes. Drop the tty
341 * reference.
Peter Hurley9c9928b2014-06-16 09:17:02 -0400342 *
343 * Caller holds tty lock.
Alan Cox3e616962009-01-02 13:45:26 +0000344 */
Alan Cox3e616962009-01-02 13:45:26 +0000345void tty_port_hangup(struct tty_port *port)
346{
Johan Hovold957daca2013-03-07 15:55:51 +0100347 struct tty_struct *tty;
Alan Cox3e616962009-01-02 13:45:26 +0000348 unsigned long flags;
349
350 spin_lock_irqsave(&port->lock, flags);
351 port->count = 0;
Johan Hovold957daca2013-03-07 15:55:51 +0100352 tty = port->tty;
353 if (tty)
354 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox3e616962009-01-02 13:45:26 +0000355 port->tty = NULL;
356 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700357 tty_port_set_active(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100358 tty_port_shutdown(port, tty);
359 tty_kref_put(tty);
Alan Cox3e616962009-01-02 13:45:26 +0000360 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -0700361 wake_up_interruptible(&port->delta_msr_wait);
Alan Cox3e616962009-01-02 13:45:26 +0000362}
363EXPORT_SYMBOL(tty_port_hangup);
364
365/**
Jiri Slabyaa27a092013-03-07 13:12:30 +0100366 * tty_port_tty_hangup - helper to hang up a tty
367 *
368 * @port: tty port
369 * @check_clocal: hang only ttys with CLOCAL unset?
370 */
371void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
372{
373 struct tty_struct *tty = tty_port_tty_get(port);
374
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200375 if (tty && (!check_clocal || !C_CLOCAL(tty)))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100376 tty_hangup(tty);
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200377 tty_kref_put(tty);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100378}
379EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
380
381/**
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100382 * tty_port_tty_wakeup - helper to wake up a tty
383 *
384 * @port: tty port
385 */
386void tty_port_tty_wakeup(struct tty_port *port)
387{
Rob Herringc3485ee2017-02-02 13:48:05 -0600388 port->client_ops->write_wakeup(port);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100389}
390EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
391
392/**
Alan Cox31f35932009-01-02 13:45:05 +0000393 * tty_port_carrier_raised - carrier raised check
394 * @port: tty port
395 *
396 * Wrapper for the carrier detect logic. For the moment this is used
397 * to hide some internal details. This will eventually become entirely
398 * internal to the tty port.
399 */
Alan Cox31f35932009-01-02 13:45:05 +0000400int tty_port_carrier_raised(struct tty_port *port)
401{
Johan Hovold0d3cb6f2019-04-03 09:40:53 +0200402 if (port->ops->carrier_raised == NULL)
Alan Cox31f35932009-01-02 13:45:05 +0000403 return 1;
404 return port->ops->carrier_raised(port);
405}
406EXPORT_SYMBOL(tty_port_carrier_raised);
Alan Cox5d951fb2009-01-02 13:45:19 +0000407
408/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100409 * tty_port_raise_dtr_rts - Raise DTR/RTS
Alan Cox5d951fb2009-01-02 13:45:19 +0000410 * @port: tty port
411 *
412 * Wrapper for the DTR/RTS raise logic. For the moment this is used
413 * to hide some internal details. This will eventually become entirely
414 * internal to the tty port.
415 */
Alan Cox5d951fb2009-01-02 13:45:19 +0000416void tty_port_raise_dtr_rts(struct tty_port *port)
417{
Johan Hovold0d3cb6f2019-04-03 09:40:53 +0200418 if (port->ops->dtr_rts)
Alan Coxfcc8ac12009-06-11 12:24:17 +0100419 port->ops->dtr_rts(port, 1);
Alan Cox5d951fb2009-01-02 13:45:19 +0000420}
421EXPORT_SYMBOL(tty_port_raise_dtr_rts);
Alan Cox36c621d2009-01-02 13:46:10 +0000422
423/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100424 * tty_port_lower_dtr_rts - Lower DTR/RTS
425 * @port: tty port
426 *
427 * Wrapper for the DTR/RTS raise logic. For the moment this is used
428 * to hide some internal details. This will eventually become entirely
429 * internal to the tty port.
430 */
Alan Coxfcc8ac12009-06-11 12:24:17 +0100431void tty_port_lower_dtr_rts(struct tty_port *port)
432{
Johan Hovold0d3cb6f2019-04-03 09:40:53 +0200433 if (port->ops->dtr_rts)
Alan Coxfcc8ac12009-06-11 12:24:17 +0100434 port->ops->dtr_rts(port, 0);
435}
436EXPORT_SYMBOL(tty_port_lower_dtr_rts);
437
438/**
Alan Cox36c621d2009-01-02 13:46:10 +0000439 * tty_port_block_til_ready - Waiting logic for tty open
440 * @port: the tty port being opened
441 * @tty: the tty device being bound
Alan Coxed3f0af2017-01-16 16:54:29 -0600442 * @filp: the file pointer of the opener or NULL
Alan Cox36c621d2009-01-02 13:46:10 +0000443 *
444 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
445 * Handles:
446 * - hangup (both before and during)
447 * - non blocking open
448 * - rts/dtr/dcd
449 * - signals
450 * - port flags and counts
451 *
452 * The passed tty_port must implement the carrier_raised method if it can
Alan Coxfcc8ac12009-06-11 12:24:17 +0100453 * do carrier detect and the dtr_rts method if it supports software
Alan Cox36c621d2009-01-02 13:46:10 +0000454 * management of these lines. Note that the dtr/rts raise is done each
455 * iteration as a hangup may have previously dropped them while we wait.
Peter Hurleyc590f6b2014-06-16 09:17:01 -0400456 *
457 * Caller holds tty lock.
458 *
459 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
460 * may have changed state (eg., may have been hung up).
Alan Cox36c621d2009-01-02 13:46:10 +0000461 */
Alan Cox36c621d2009-01-02 13:46:10 +0000462int tty_port_block_til_ready(struct tty_port *port,
463 struct tty_struct *tty, struct file *filp)
464{
465 int do_clocal = 0, retval;
466 unsigned long flags;
Jiri Slaby6af9a432009-06-24 18:35:05 +0100467 DEFINE_WAIT(wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000468
Alan Cox36c621d2009-01-02 13:46:10 +0000469 /* if non-blocking mode is set we can pass directly to open unless
470 the port has just hung up or is in another error state */
Peter Hurley18900ca2016-04-09 17:06:48 -0700471 if (tty_io_error(tty)) {
Peter Hurley807c8d812016-04-09 17:53:22 -0700472 tty_port_set_active(port, 1);
Alan Cox8627b962009-11-18 14:12:58 +0000473 return 0;
474 }
Alan Coxed3f0af2017-01-16 16:54:29 -0600475 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
Alan Cox4175f3e2009-10-28 21:12:32 +0100476 /* Indicate we are open */
Peter Hurley9db276f2016-01-10 20:36:15 -0800477 if (C_BAUD(tty))
Alan Cox4175f3e2009-10-28 21:12:32 +0100478 tty_port_raise_dtr_rts(port);
Peter Hurley807c8d812016-04-09 17:53:22 -0700479 tty_port_set_active(port, 1);
Alan Cox36c621d2009-01-02 13:46:10 +0000480 return 0;
481 }
482
483 if (C_CLOCAL(tty))
484 do_clocal = 1;
485
486 /* Block waiting until we can proceed. We may need to wait for the
487 carrier, but we must also wait for any close that is in progress
488 before the next open may complete */
489
490 retval = 0;
Alan Cox36c621d2009-01-02 13:46:10 +0000491
492 /* The port lock protects the port counts */
493 spin_lock_irqsave(&port->lock, flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400494 port->count--;
Alan Cox36c621d2009-01-02 13:46:10 +0000495 port->blocked_open++;
496 spin_unlock_irqrestore(&port->lock, flags);
497
498 while (1) {
499 /* Indicate we are open */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700500 if (C_BAUD(tty) && tty_port_initialized(port))
Alan Cox78349092009-01-02 13:46:43 +0000501 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000502
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100503 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Alan Coxd774a562009-10-06 16:06:21 +0100504 /* Check for a hangup or uninitialised port.
505 Return accordingly */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700506 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
Alan Cox36c621d2009-01-02 13:46:10 +0000507 if (port->flags & ASYNC_HUP_NOTIFY)
508 retval = -EAGAIN;
509 else
510 retval = -ERESTARTSYS;
511 break;
512 }
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100513 /*
514 * Probe the carrier. For devices with no carrier detect
515 * tty_port_carrier_raised will always return true.
516 * Never ask drivers if CLOCAL is set, this causes troubles
517 * on some hardware.
518 */
Peter Hurleyfef062c2015-10-10 16:00:52 -0400519 if (do_clocal || tty_port_carrier_raised(port))
Alan Cox36c621d2009-01-02 13:46:10 +0000520 break;
521 if (signal_pending(current)) {
522 retval = -ERESTARTSYS;
523 break;
524 }
Alan Cox89c8d912012-08-08 16:30:13 +0100525 tty_unlock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000526 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100527 tty_lock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000528 }
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100529 finish_wait(&port->open_wait, &wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000530
531 /* Update counts. A parallel hangup will have set count to zero and
532 we must not mess that up further */
533 spin_lock_irqsave(&port->lock, flags);
534 if (!tty_hung_up_p(filp))
535 port->count++;
536 port->blocked_open--;
Alan Cox36c621d2009-01-02 13:46:10 +0000537 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700538 if (retval == 0)
539 tty_port_set_active(port, 1);
Alan Coxecc2e052009-07-17 16:17:26 +0100540 return retval;
Alan Cox36c621d2009-01-02 13:46:10 +0000541}
542EXPORT_SYMBOL(tty_port_block_til_ready);
543
Johan Hovoldb74414f2013-03-07 15:55:52 +0100544static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
545{
546 unsigned int bps = tty_get_baud_rate(tty);
547 long timeout;
548
549 if (bps > 1200) {
550 timeout = (HZ * 10 * port->drain_delay) / bps;
551 timeout = max_t(long, timeout, HZ / 10);
552 } else {
553 timeout = 2 * HZ;
554 }
555 schedule_timeout_interruptible(timeout);
556}
557
Peter Hurley79c1faa2015-10-10 16:00:51 -0400558/* Caller holds tty lock. */
Alan Coxd774a562009-10-06 16:06:21 +0100559int tty_port_close_start(struct tty_port *port,
560 struct tty_struct *tty, struct file *filp)
Alan Coxa6614992009-01-02 13:46:50 +0000561{
562 unsigned long flags;
563
Peter Hurley633caba2014-11-05 12:40:03 -0500564 if (tty_hung_up_p(filp))
Alan Coxa6614992009-01-02 13:46:50 +0000565 return 0;
Alan Coxa6614992009-01-02 13:46:50 +0000566
Peter Hurley633caba2014-11-05 12:40:03 -0500567 spin_lock_irqsave(&port->lock, flags);
Alan Coxd774a562009-10-06 16:06:21 +0100568 if (tty->count == 1 && port->count != 1) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500569 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
570 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000571 port->count = 1;
572 }
573 if (--port->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500574 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
575 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000576 port->count = 0;
577 }
578
579 if (port->count) {
580 spin_unlock_irqrestore(&port->lock, flags);
581 return 0;
582 }
Alan Coxa6614992009-01-02 13:46:50 +0000583 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100584
Peter Hurleyddc7b752014-06-16 09:17:03 -0400585 tty->closing = 1;
586
Peter Hurleyd41861c2016-04-09 17:53:25 -0700587 if (tty_port_initialized(port)) {
Johan Hovold0b2588c2013-03-07 15:55:53 +0100588 /* Don't block on a stalled port, just pull the chain */
589 if (tty->flow_stopped)
590 tty_driver_flush_buffer(tty);
591 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Peter Hurley79c1faa2015-10-10 16:00:51 -0400592 tty_wait_until_sent(tty, port->closing_wait);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100593 if (port->drain_delay)
594 tty_port_drain_delay(port, tty);
595 }
Alan Coxe707c352009-11-05 13:27:57 +0000596 /* Flush the ldisc buffering */
597 tty_ldisc_flush(tty);
598
Peter Hurley469d6d02013-09-18 20:47:06 -0400599 /* Report to caller this is the last port reference */
Alan Coxa6614992009-01-02 13:46:50 +0000600 return 1;
601}
602EXPORT_SYMBOL(tty_port_close_start);
603
Peter Hurley0733db912014-06-16 09:16:59 -0400604/* Caller holds tty lock */
Alan Coxa6614992009-01-02 13:46:50 +0000605void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
606{
607 unsigned long flags;
608
Peter Hurley3f40f5b2014-11-05 12:40:05 -0500609 tty_ldisc_flush(tty);
Alan Coxa6614992009-01-02 13:46:50 +0000610 tty->closing = 0;
611
Peter Hurleyddc7b752014-06-16 09:17:03 -0400612 spin_lock_irqsave(&port->lock, flags);
613
Alan Coxa6614992009-01-02 13:46:50 +0000614 if (port->blocked_open) {
615 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley5823323e2016-01-10 20:36:14 -0800616 if (port->close_delay)
617 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Alan Coxa6614992009-01-02 13:46:50 +0000618 spin_lock_irqsave(&port->lock, flags);
619 wake_up_interruptible(&port->open_wait);
620 }
Alan Coxa6614992009-01-02 13:46:50 +0000621 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700622 tty_port_set_active(port, 0);
Alan Coxa6614992009-01-02 13:46:50 +0000623}
624EXPORT_SYMBOL(tty_port_close_end);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700625
Lee Jones1b1deb42020-11-04 19:35:19 +0000626/*
Peter Hurley0733db912014-06-16 09:16:59 -0400627 * tty_port_close
628 *
629 * Caller holds tty lock
Peter Hurley0733db912014-06-16 09:16:59 -0400630 */
Alan Cox7ca0ff92009-09-19 13:13:20 -0700631void tty_port_close(struct tty_port *port, struct tty_struct *tty,
632 struct file *filp)
633{
634 if (tty_port_close_start(port, tty, filp) == 0)
635 return;
Johan Hovold957daca2013-03-07 15:55:51 +0100636 tty_port_shutdown(port, tty);
Chanho Park2a486022018-11-22 18:23:47 +0900637 if (!port->console)
638 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700639 tty_port_close_end(port, tty);
640 tty_port_tty_set(port, NULL);
641}
642EXPORT_SYMBOL(tty_port_close);
Alan Cox64bc3972009-10-06 16:06:11 +0100643
Jiri Slaby72a33bf2012-08-07 21:47:49 +0200644/**
645 * tty_port_install - generic tty->ops->install handler
646 * @port: tty_port of the device
647 * @driver: tty_driver for this device
648 * @tty: tty to be installed
649 *
650 * It is the same as tty_standard_install except the provided @port is linked
651 * to a concrete tty specified by @tty. Use this or tty_port_register_device
652 * (or both). Call tty_port_link_device as a last resort.
653 */
Jiri Slaby695586c2012-06-04 13:35:32 +0200654int tty_port_install(struct tty_port *port, struct tty_driver *driver,
655 struct tty_struct *tty)
656{
657 tty->port = port;
658 return tty_standard_install(driver, tty);
659}
660EXPORT_SYMBOL_GPL(tty_port_install);
661
Lee Jones1b1deb42020-11-04 19:35:19 +0000662/*
Peter Hurleyaddd4672014-06-16 09:17:00 -0400663 * tty_port_open
664 *
665 * Caller holds tty lock.
666 *
667 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
668 * tty and tty_port may have changed state (eg., may be hung up now)
669 */
Alan Cox64bc3972009-10-06 16:06:11 +0100670int tty_port_open(struct tty_port *port, struct tty_struct *tty,
Alan Coxd774a562009-10-06 16:06:21 +0100671 struct file *filp)
Alan Cox64bc3972009-10-06 16:06:11 +0100672{
673 spin_lock_irq(&port->lock);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400674 ++port->count;
Alan Cox64bc3972009-10-06 16:06:11 +0100675 spin_unlock_irq(&port->lock);
676 tty_port_tty_set(port, tty);
677
678 /*
679 * Do the device-specific open only if the hardware isn't
680 * already initialized. Serialize open and shutdown using the
681 * port mutex.
682 */
683
684 mutex_lock(&port->mutex);
685
Peter Hurleyd41861c2016-04-09 17:53:25 -0700686 if (!tty_port_initialized(port)) {
Alan Coxa9a37ec2009-11-30 13:16:57 +0000687 clear_bit(TTY_IO_ERROR, &tty->flags);
Johan Hovold0d3cb6f2019-04-03 09:40:53 +0200688 if (port->ops->activate) {
Alan Cox64bc3972009-10-06 16:06:11 +0100689 int retval = port->ops->activate(port, tty);
690 if (retval) {
Alan Coxd774a562009-10-06 16:06:21 +0100691 mutex_unlock(&port->mutex);
692 return retval;
693 }
694 }
Peter Hurleyd41861c2016-04-09 17:53:25 -0700695 tty_port_set_initialized(port, 1);
Alan Cox64bc3972009-10-06 16:06:11 +0100696 }
697 mutex_unlock(&port->mutex);
698 return tty_port_block_til_ready(port, tty, filp);
699}
700
701EXPORT_SYMBOL(tty_port_open);