blob: 1d21a9c1d33e6e3c5ef007a25e4b4550fb62a51c [file] [log] [blame]
Alan Cox9e485652008-10-13 10:37:07 +01001/*
2 * Tty port functions
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
Alan Cox3e616962009-01-02 13:45:26 +000010#include <linux/serial.h>
Alan Cox9e485652008-10-13 10:37:07 +010011#include <linux/timer.h>
12#include <linux/string.h>
13#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Alan Cox9e485652008-10-13 10:37:07 +010015#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
Rob Herring8ee3fde2017-02-02 13:48:09 -060019#include <linux/serdev.h>
Alan Cox9e485652008-10-13 10:37:07 +010020
Rob Herringc3485ee2017-02-02 13:48:05 -060021static int tty_port_default_receive_buf(struct tty_port *port,
22 const unsigned char *p,
23 const unsigned char *f, size_t count)
24{
25 int ret;
26 struct tty_struct *tty;
27 struct tty_ldisc *disc;
28
29 tty = READ_ONCE(port->itty);
30 if (!tty)
31 return 0;
32
33 disc = tty_ldisc_ref(tty);
34 if (!disc)
35 return 0;
36
37 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
38
39 tty_ldisc_deref(disc);
40
41 return ret;
42}
43
44static void tty_port_default_wakeup(struct tty_port *port)
45{
46 struct tty_struct *tty = tty_port_tty_get(port);
47
48 if (tty) {
49 tty_wakeup(tty);
50 tty_kref_put(tty);
51 }
52}
53
54static const struct tty_port_client_operations default_client_ops = {
55 .receive_buf = tty_port_default_receive_buf,
56 .write_wakeup = tty_port_default_wakeup,
57};
58
Alan Cox9e485652008-10-13 10:37:07 +010059void tty_port_init(struct tty_port *port)
60{
61 memset(port, 0, sizeof(*port));
Jiri Slabyecbbfd42012-10-18 22:26:47 +020062 tty_buffer_init(port);
Alan Cox9e485652008-10-13 10:37:07 +010063 init_waitqueue_head(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -070064 init_waitqueue_head(&port->delta_msr_wait);
Alan Cox9e485652008-10-13 10:37:07 +010065 mutex_init(&port->mutex);
Alan Cox44e49092009-11-30 13:16:41 +000066 mutex_init(&port->buf_mutex);
Alan Cox4a90f092008-10-13 10:39:46 +010067 spin_lock_init(&port->lock);
Alan Cox9e485652008-10-13 10:37:07 +010068 port->close_delay = (50 * HZ) / 100;
69 port->closing_wait = (3000 * HZ) / 100;
Rob Herringc3485ee2017-02-02 13:48:05 -060070 port->client_ops = &default_client_ops;
Alan Cox568aafc2009-11-30 13:17:14 +000071 kref_init(&port->kref);
Alan Cox9e485652008-10-13 10:37:07 +010072}
73EXPORT_SYMBOL(tty_port_init);
74
Jiri Slaby72a33bf2012-08-07 21:47:49 +020075/**
Jiri Slaby2cb4ca02012-08-07 21:47:50 +020076 * tty_port_link_device - link tty and tty_port
77 * @port: tty_port of the device
78 * @driver: tty_driver for this device
79 * @index: index of the tty
80 *
81 * Provide the tty layer wit ha link from a tty (specified by @index) to a
82 * tty_port (@port). Use this only if neither tty_port_register_device nor
83 * tty_port_install is used in the driver. If used, this has to be called before
84 * tty_register_driver.
85 */
86void tty_port_link_device(struct tty_port *port,
87 struct tty_driver *driver, unsigned index)
88{
89 if (WARN_ON(index >= driver->num))
90 return;
91 driver->ports[index] = port;
92}
93EXPORT_SYMBOL_GPL(tty_port_link_device);
94
95/**
Jiri Slaby72a33bf2012-08-07 21:47:49 +020096 * tty_port_register_device - register tty device
97 * @port: tty_port of the device
98 * @driver: tty_driver for this device
99 * @index: index of the tty
100 * @device: parent if exists, otherwise NULL
101 *
102 * It is the same as tty_register_device except the provided @port is linked to
103 * a concrete tty specified by @index. Use this or tty_port_install (or both).
104 * Call tty_port_link_device as a last resort.
105 */
Jiri Slaby057eb852012-06-04 13:35:37 +0200106struct device *tty_port_register_device(struct tty_port *port,
107 struct tty_driver *driver, unsigned index,
108 struct device *device)
109{
Rob Herring30863652017-01-16 16:54:30 -0600110 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
Jiri Slaby057eb852012-06-04 13:35:37 +0200111}
112EXPORT_SYMBOL_GPL(tty_port_register_device);
113
Tomas Hlavacekb1b79912012-09-06 23:17:47 +0200114/**
115 * tty_port_register_device_attr - register tty device
116 * @port: tty_port of the device
117 * @driver: tty_driver for this device
118 * @index: index of the tty
119 * @device: parent if exists, otherwise NULL
120 * @drvdata: Driver data to be set to device.
121 * @attr_grp: Attribute group to be set on device.
122 *
123 * It is the same as tty_register_device_attr except the provided @port is
124 * linked to a concrete tty specified by @index. Use this or tty_port_install
125 * (or both). Call tty_port_link_device as a last resort.
126 */
127struct device *tty_port_register_device_attr(struct tty_port *port,
128 struct tty_driver *driver, unsigned index,
129 struct device *device, void *drvdata,
130 const struct attribute_group **attr_grp)
131{
Rob Herring8ee3fde2017-02-02 13:48:09 -0600132 struct device *dev;
133
Tomas Hlavacekb1b79912012-09-06 23:17:47 +0200134 tty_port_link_device(port, driver, index);
Rob Herring8ee3fde2017-02-02 13:48:09 -0600135
136 dev = serdev_tty_port_register(port, device, driver, index);
137 if (PTR_ERR(dev) != -ENODEV)
138 /* Skip creating cdev if we registered a serdev device */
139 return dev;
140
Tomas Hlavacekb1b79912012-09-06 23:17:47 +0200141 return tty_register_device_attr(driver, index, device, drvdata,
142 attr_grp);
143}
144EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
145
Alan Cox9e485652008-10-13 10:37:07 +0100146int tty_port_alloc_xmit_buf(struct tty_port *port)
147{
148 /* We may sleep in get_zeroed_page() */
Alan Cox44e49092009-11-30 13:16:41 +0000149 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100150 if (port->xmit_buf == NULL)
151 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
Alan Cox44e49092009-11-30 13:16:41 +0000152 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100153 if (port->xmit_buf == NULL)
154 return -ENOMEM;
155 return 0;
156}
157EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
158
159void tty_port_free_xmit_buf(struct tty_port *port)
160{
Alan Cox44e49092009-11-30 13:16:41 +0000161 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100162 if (port->xmit_buf != NULL) {
163 free_page((unsigned long)port->xmit_buf);
164 port->xmit_buf = NULL;
165 }
Alan Cox44e49092009-11-30 13:16:41 +0000166 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100167}
168EXPORT_SYMBOL(tty_port_free_xmit_buf);
169
Jiri Slabyde274bf2012-11-15 09:49:54 +0100170/**
171 * tty_port_destroy -- destroy inited port
172 * @port: tty port to be doestroyed
173 *
174 * When a port was initialized using tty_port_init, one has to destroy the
175 * port by this function. Either indirectly by using tty_port refcounting
176 * (tty_port_put) or directly if refcounting is not used.
177 */
178void tty_port_destroy(struct tty_port *port)
179{
Peter Hurleye1760582015-10-17 16:36:23 -0400180 tty_buffer_cancel_work(port);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100181 tty_buffer_free_all(port);
182}
183EXPORT_SYMBOL(tty_port_destroy);
184
Alan Cox568aafc2009-11-30 13:17:14 +0000185static void tty_port_destructor(struct kref *kref)
186{
187 struct tty_port *port = container_of(kref, struct tty_port, kref);
Peter Hurleye3bfea22013-09-18 20:42:39 -0400188
189 /* check if last port ref was dropped before tty release */
190 if (WARN_ON(port->itty))
191 return;
Rob Herring8ee3fde2017-02-02 13:48:09 -0600192
193 serdev_tty_port_unregister(port);
194
Alan Cox568aafc2009-11-30 13:17:14 +0000195 if (port->xmit_buf)
196 free_page((unsigned long)port->xmit_buf);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100197 tty_port_destroy(port);
Jiri Slaby81c79832012-11-15 09:49:49 +0100198 if (port->ops && port->ops->destruct)
Alan Cox568aafc2009-11-30 13:17:14 +0000199 port->ops->destruct(port);
200 else
201 kfree(port);
202}
203
204void tty_port_put(struct tty_port *port)
205{
206 if (port)
207 kref_put(&port->kref, tty_port_destructor);
208}
209EXPORT_SYMBOL(tty_port_put);
Alan Cox9e485652008-10-13 10:37:07 +0100210
Alan Cox4a90f092008-10-13 10:39:46 +0100211/**
212 * tty_port_tty_get - get a tty reference
213 * @port: tty port
214 *
215 * Return a refcount protected tty instance or NULL if the port is not
216 * associated with a tty (eg due to close or hangup)
217 */
218
219struct tty_struct *tty_port_tty_get(struct tty_port *port)
220{
221 unsigned long flags;
222 struct tty_struct *tty;
223
224 spin_lock_irqsave(&port->lock, flags);
225 tty = tty_kref_get(port->tty);
226 spin_unlock_irqrestore(&port->lock, flags);
227 return tty;
228}
229EXPORT_SYMBOL(tty_port_tty_get);
230
231/**
232 * tty_port_tty_set - set the tty of a port
233 * @port: tty port
234 * @tty: the tty
235 *
236 * Associate the port and tty pair. Manages any internal refcounts.
237 * Pass NULL to deassociate a port
238 */
239
240void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(&port->lock, flags);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100245 tty_kref_put(port->tty);
Alan Coxcb4bca32008-10-21 13:47:44 +0100246 port->tty = tty_kref_get(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100247 spin_unlock_irqrestore(&port->lock, flags);
248}
249EXPORT_SYMBOL(tty_port_tty_set);
Alan Cox31f35932009-01-02 13:45:05 +0000250
Johan Hovold957daca2013-03-07 15:55:51 +0100251static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700252{
Alan Cox64bc3972009-10-06 16:06:11 +0100253 mutex_lock(&port->mutex);
Johan Hovold8bde9652013-03-07 15:55:48 +0100254 if (port->console)
255 goto out;
256
Peter Hurleyd41861c2016-04-09 17:53:25 -0700257 if (tty_port_initialized(port)) {
258 tty_port_set_initialized(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100259 /*
260 * Drop DTR/RTS if HUPCL is set. This causes any attached
261 * modem to hang up the line.
262 */
263 if (tty && C_HUPCL(tty))
264 tty_port_lower_dtr_rts(port);
265
Johan Hovold8bde9652013-03-07 15:55:48 +0100266 if (port->ops->shutdown)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700267 port->ops->shutdown(port);
Johan Hovold8bde9652013-03-07 15:55:48 +0100268 }
269out:
Alan Cox64bc3972009-10-06 16:06:11 +0100270 mutex_unlock(&port->mutex);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700271}
272
Alan Cox31f35932009-01-02 13:45:05 +0000273/**
Alan Cox3e616962009-01-02 13:45:26 +0000274 * tty_port_hangup - hangup helper
275 * @port: tty port
276 *
277 * Perform port level tty hangup flag and count changes. Drop the tty
278 * reference.
Peter Hurley9c9928b2014-06-16 09:17:02 -0400279 *
280 * Caller holds tty lock.
Alan Cox3e616962009-01-02 13:45:26 +0000281 */
282
283void tty_port_hangup(struct tty_port *port)
284{
Johan Hovold957daca2013-03-07 15:55:51 +0100285 struct tty_struct *tty;
Alan Cox3e616962009-01-02 13:45:26 +0000286 unsigned long flags;
287
288 spin_lock_irqsave(&port->lock, flags);
289 port->count = 0;
Johan Hovold957daca2013-03-07 15:55:51 +0100290 tty = port->tty;
291 if (tty)
292 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox3e616962009-01-02 13:45:26 +0000293 port->tty = NULL;
294 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700295 tty_port_set_active(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100296 tty_port_shutdown(port, tty);
297 tty_kref_put(tty);
Alan Cox3e616962009-01-02 13:45:26 +0000298 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -0700299 wake_up_interruptible(&port->delta_msr_wait);
Alan Cox3e616962009-01-02 13:45:26 +0000300}
301EXPORT_SYMBOL(tty_port_hangup);
302
303/**
Jiri Slabyaa27a092013-03-07 13:12:30 +0100304 * tty_port_tty_hangup - helper to hang up a tty
305 *
306 * @port: tty port
307 * @check_clocal: hang only ttys with CLOCAL unset?
308 */
309void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
310{
311 struct tty_struct *tty = tty_port_tty_get(port);
312
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200313 if (tty && (!check_clocal || !C_CLOCAL(tty)))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100314 tty_hangup(tty);
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200315 tty_kref_put(tty);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100316}
317EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
318
319/**
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100320 * tty_port_tty_wakeup - helper to wake up a tty
321 *
322 * @port: tty port
323 */
324void tty_port_tty_wakeup(struct tty_port *port)
325{
Rob Herringc3485ee2017-02-02 13:48:05 -0600326 port->client_ops->write_wakeup(port);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100327}
328EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
329
330/**
Alan Cox31f35932009-01-02 13:45:05 +0000331 * tty_port_carrier_raised - carrier raised check
332 * @port: tty port
333 *
334 * Wrapper for the carrier detect logic. For the moment this is used
335 * to hide some internal details. This will eventually become entirely
336 * internal to the tty port.
337 */
338
339int tty_port_carrier_raised(struct tty_port *port)
340{
341 if (port->ops->carrier_raised == NULL)
342 return 1;
343 return port->ops->carrier_raised(port);
344}
345EXPORT_SYMBOL(tty_port_carrier_raised);
Alan Cox5d951fb2009-01-02 13:45:19 +0000346
347/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100348 * tty_port_raise_dtr_rts - Raise DTR/RTS
Alan Cox5d951fb2009-01-02 13:45:19 +0000349 * @port: tty port
350 *
351 * Wrapper for the DTR/RTS raise logic. For the moment this is used
352 * to hide some internal details. This will eventually become entirely
353 * internal to the tty port.
354 */
355
356void tty_port_raise_dtr_rts(struct tty_port *port)
357{
Alan Coxfcc8ac12009-06-11 12:24:17 +0100358 if (port->ops->dtr_rts)
359 port->ops->dtr_rts(port, 1);
Alan Cox5d951fb2009-01-02 13:45:19 +0000360}
361EXPORT_SYMBOL(tty_port_raise_dtr_rts);
Alan Cox36c621d2009-01-02 13:46:10 +0000362
363/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100364 * tty_port_lower_dtr_rts - Lower DTR/RTS
365 * @port: tty port
366 *
367 * Wrapper for the DTR/RTS raise logic. For the moment this is used
368 * to hide some internal details. This will eventually become entirely
369 * internal to the tty port.
370 */
371
372void tty_port_lower_dtr_rts(struct tty_port *port)
373{
374 if (port->ops->dtr_rts)
375 port->ops->dtr_rts(port, 0);
376}
377EXPORT_SYMBOL(tty_port_lower_dtr_rts);
378
379/**
Alan Cox36c621d2009-01-02 13:46:10 +0000380 * tty_port_block_til_ready - Waiting logic for tty open
381 * @port: the tty port being opened
382 * @tty: the tty device being bound
Alan Coxed3f0af2017-01-16 16:54:29 -0600383 * @filp: the file pointer of the opener or NULL
Alan Cox36c621d2009-01-02 13:46:10 +0000384 *
385 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
386 * Handles:
387 * - hangup (both before and during)
388 * - non blocking open
389 * - rts/dtr/dcd
390 * - signals
391 * - port flags and counts
392 *
393 * The passed tty_port must implement the carrier_raised method if it can
Alan Coxfcc8ac12009-06-11 12:24:17 +0100394 * do carrier detect and the dtr_rts method if it supports software
Alan Cox36c621d2009-01-02 13:46:10 +0000395 * management of these lines. Note that the dtr/rts raise is done each
396 * iteration as a hangup may have previously dropped them while we wait.
Peter Hurleyc590f6b2014-06-16 09:17:01 -0400397 *
398 * Caller holds tty lock.
399 *
400 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
401 * may have changed state (eg., may have been hung up).
Alan Cox36c621d2009-01-02 13:46:10 +0000402 */
Alan Coxd774a562009-10-06 16:06:21 +0100403
Alan Cox36c621d2009-01-02 13:46:10 +0000404int tty_port_block_til_ready(struct tty_port *port,
405 struct tty_struct *tty, struct file *filp)
406{
407 int do_clocal = 0, retval;
408 unsigned long flags;
Jiri Slaby6af9a432009-06-24 18:35:05 +0100409 DEFINE_WAIT(wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000410
Alan Cox36c621d2009-01-02 13:46:10 +0000411 /* if non-blocking mode is set we can pass directly to open unless
412 the port has just hung up or is in another error state */
Peter Hurley18900ca2016-04-09 17:06:48 -0700413 if (tty_io_error(tty)) {
Peter Hurley807c8d812016-04-09 17:53:22 -0700414 tty_port_set_active(port, 1);
Alan Cox8627b962009-11-18 14:12:58 +0000415 return 0;
416 }
Alan Coxed3f0af2017-01-16 16:54:29 -0600417 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
Alan Cox4175f3e2009-10-28 21:12:32 +0100418 /* Indicate we are open */
Peter Hurley9db276f2016-01-10 20:36:15 -0800419 if (C_BAUD(tty))
Alan Cox4175f3e2009-10-28 21:12:32 +0100420 tty_port_raise_dtr_rts(port);
Peter Hurley807c8d812016-04-09 17:53:22 -0700421 tty_port_set_active(port, 1);
Alan Cox36c621d2009-01-02 13:46:10 +0000422 return 0;
423 }
424
425 if (C_CLOCAL(tty))
426 do_clocal = 1;
427
428 /* Block waiting until we can proceed. We may need to wait for the
429 carrier, but we must also wait for any close that is in progress
430 before the next open may complete */
431
432 retval = 0;
Alan Cox36c621d2009-01-02 13:46:10 +0000433
434 /* The port lock protects the port counts */
435 spin_lock_irqsave(&port->lock, flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400436 port->count--;
Alan Cox36c621d2009-01-02 13:46:10 +0000437 port->blocked_open++;
438 spin_unlock_irqrestore(&port->lock, flags);
439
440 while (1) {
441 /* Indicate we are open */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700442 if (C_BAUD(tty) && tty_port_initialized(port))
Alan Cox78349092009-01-02 13:46:43 +0000443 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000444
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100445 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Alan Coxd774a562009-10-06 16:06:21 +0100446 /* Check for a hangup or uninitialised port.
447 Return accordingly */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700448 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
Alan Cox36c621d2009-01-02 13:46:10 +0000449 if (port->flags & ASYNC_HUP_NOTIFY)
450 retval = -EAGAIN;
451 else
452 retval = -ERESTARTSYS;
453 break;
454 }
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100455 /*
456 * Probe the carrier. For devices with no carrier detect
457 * tty_port_carrier_raised will always return true.
458 * Never ask drivers if CLOCAL is set, this causes troubles
459 * on some hardware.
460 */
Peter Hurleyfef062c2015-10-10 16:00:52 -0400461 if (do_clocal || tty_port_carrier_raised(port))
Alan Cox36c621d2009-01-02 13:46:10 +0000462 break;
463 if (signal_pending(current)) {
464 retval = -ERESTARTSYS;
465 break;
466 }
Alan Cox89c8d912012-08-08 16:30:13 +0100467 tty_unlock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000468 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100469 tty_lock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000470 }
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100471 finish_wait(&port->open_wait, &wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000472
473 /* Update counts. A parallel hangup will have set count to zero and
474 we must not mess that up further */
475 spin_lock_irqsave(&port->lock, flags);
476 if (!tty_hung_up_p(filp))
477 port->count++;
478 port->blocked_open--;
Alan Cox36c621d2009-01-02 13:46:10 +0000479 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700480 if (retval == 0)
481 tty_port_set_active(port, 1);
Alan Coxecc2e052009-07-17 16:17:26 +0100482 return retval;
Alan Cox36c621d2009-01-02 13:46:10 +0000483}
484EXPORT_SYMBOL(tty_port_block_til_ready);
485
Johan Hovoldb74414f2013-03-07 15:55:52 +0100486static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
487{
488 unsigned int bps = tty_get_baud_rate(tty);
489 long timeout;
490
491 if (bps > 1200) {
492 timeout = (HZ * 10 * port->drain_delay) / bps;
493 timeout = max_t(long, timeout, HZ / 10);
494 } else {
495 timeout = 2 * HZ;
496 }
497 schedule_timeout_interruptible(timeout);
498}
499
Peter Hurley79c1faa2015-10-10 16:00:51 -0400500/* Caller holds tty lock. */
Alan Coxd774a562009-10-06 16:06:21 +0100501int tty_port_close_start(struct tty_port *port,
502 struct tty_struct *tty, struct file *filp)
Alan Coxa6614992009-01-02 13:46:50 +0000503{
504 unsigned long flags;
505
Peter Hurley633caba2014-11-05 12:40:03 -0500506 if (tty_hung_up_p(filp))
Alan Coxa6614992009-01-02 13:46:50 +0000507 return 0;
Alan Coxa6614992009-01-02 13:46:50 +0000508
Peter Hurley633caba2014-11-05 12:40:03 -0500509 spin_lock_irqsave(&port->lock, flags);
Alan Coxd774a562009-10-06 16:06:21 +0100510 if (tty->count == 1 && port->count != 1) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500511 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
512 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000513 port->count = 1;
514 }
515 if (--port->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500516 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
517 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000518 port->count = 0;
519 }
520
521 if (port->count) {
522 spin_unlock_irqrestore(&port->lock, flags);
523 return 0;
524 }
Alan Coxa6614992009-01-02 13:46:50 +0000525 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100526
Peter Hurleyddc7b752014-06-16 09:17:03 -0400527 tty->closing = 1;
528
Peter Hurleyd41861c2016-04-09 17:53:25 -0700529 if (tty_port_initialized(port)) {
Johan Hovold0b2588c2013-03-07 15:55:53 +0100530 /* Don't block on a stalled port, just pull the chain */
531 if (tty->flow_stopped)
532 tty_driver_flush_buffer(tty);
533 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Peter Hurley79c1faa2015-10-10 16:00:51 -0400534 tty_wait_until_sent(tty, port->closing_wait);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100535 if (port->drain_delay)
536 tty_port_drain_delay(port, tty);
537 }
Alan Coxe707c352009-11-05 13:27:57 +0000538 /* Flush the ldisc buffering */
539 tty_ldisc_flush(tty);
540
Peter Hurley469d6d02013-09-18 20:47:06 -0400541 /* Report to caller this is the last port reference */
Alan Coxa6614992009-01-02 13:46:50 +0000542 return 1;
543}
544EXPORT_SYMBOL(tty_port_close_start);
545
Peter Hurley0733db92014-06-16 09:16:59 -0400546/* Caller holds tty lock */
Alan Coxa6614992009-01-02 13:46:50 +0000547void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
548{
549 unsigned long flags;
550
Peter Hurley3f40f5b2014-11-05 12:40:05 -0500551 tty_ldisc_flush(tty);
Alan Coxa6614992009-01-02 13:46:50 +0000552 tty->closing = 0;
553
Peter Hurleyddc7b752014-06-16 09:17:03 -0400554 spin_lock_irqsave(&port->lock, flags);
555
Alan Coxa6614992009-01-02 13:46:50 +0000556 if (port->blocked_open) {
557 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley5823323e2016-01-10 20:36:14 -0800558 if (port->close_delay)
559 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Alan Coxa6614992009-01-02 13:46:50 +0000560 spin_lock_irqsave(&port->lock, flags);
561 wake_up_interruptible(&port->open_wait);
562 }
Alan Coxa6614992009-01-02 13:46:50 +0000563 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700564 tty_port_set_active(port, 0);
Alan Coxa6614992009-01-02 13:46:50 +0000565}
566EXPORT_SYMBOL(tty_port_close_end);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700567
Peter Hurley0733db92014-06-16 09:16:59 -0400568/**
569 * tty_port_close
570 *
571 * Caller holds tty lock
Peter Hurley0733db92014-06-16 09:16:59 -0400572 */
Alan Cox7ca0ff92009-09-19 13:13:20 -0700573void tty_port_close(struct tty_port *port, struct tty_struct *tty,
574 struct file *filp)
575{
576 if (tty_port_close_start(port, tty, filp) == 0)
577 return;
Johan Hovold957daca2013-03-07 15:55:51 +0100578 tty_port_shutdown(port, tty);
Alan Coxd74e8282009-11-30 13:16:52 +0000579 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700580 tty_port_close_end(port, tty);
581 tty_port_tty_set(port, NULL);
582}
583EXPORT_SYMBOL(tty_port_close);
Alan Cox64bc3972009-10-06 16:06:11 +0100584
Jiri Slaby72a33bf2012-08-07 21:47:49 +0200585/**
586 * tty_port_install - generic tty->ops->install handler
587 * @port: tty_port of the device
588 * @driver: tty_driver for this device
589 * @tty: tty to be installed
590 *
591 * It is the same as tty_standard_install except the provided @port is linked
592 * to a concrete tty specified by @tty. Use this or tty_port_register_device
593 * (or both). Call tty_port_link_device as a last resort.
594 */
Jiri Slaby695586c2012-06-04 13:35:32 +0200595int tty_port_install(struct tty_port *port, struct tty_driver *driver,
596 struct tty_struct *tty)
597{
598 tty->port = port;
599 return tty_standard_install(driver, tty);
600}
601EXPORT_SYMBOL_GPL(tty_port_install);
602
Peter Hurleyaddd4672014-06-16 09:17:00 -0400603/**
604 * tty_port_open
605 *
606 * Caller holds tty lock.
607 *
608 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
609 * tty and tty_port may have changed state (eg., may be hung up now)
610 */
Alan Cox64bc3972009-10-06 16:06:11 +0100611int tty_port_open(struct tty_port *port, struct tty_struct *tty,
Alan Coxd774a562009-10-06 16:06:21 +0100612 struct file *filp)
Alan Cox64bc3972009-10-06 16:06:11 +0100613{
614 spin_lock_irq(&port->lock);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400615 ++port->count;
Alan Cox64bc3972009-10-06 16:06:11 +0100616 spin_unlock_irq(&port->lock);
617 tty_port_tty_set(port, tty);
618
619 /*
620 * Do the device-specific open only if the hardware isn't
621 * already initialized. Serialize open and shutdown using the
622 * port mutex.
623 */
624
625 mutex_lock(&port->mutex);
626
Peter Hurleyd41861c2016-04-09 17:53:25 -0700627 if (!tty_port_initialized(port)) {
Alan Coxa9a37ec2009-11-30 13:16:57 +0000628 clear_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox64bc3972009-10-06 16:06:11 +0100629 if (port->ops->activate) {
630 int retval = port->ops->activate(port, tty);
631 if (retval) {
Alan Coxd774a562009-10-06 16:06:21 +0100632 mutex_unlock(&port->mutex);
633 return retval;
634 }
635 }
Peter Hurleyd41861c2016-04-09 17:53:25 -0700636 tty_port_set_initialized(port, 1);
Alan Cox64bc3972009-10-06 16:06:11 +0100637 }
638 mutex_unlock(&port->mutex);
639 return tty_port_block_til_ready(port, tty, filp);
640}
641
642EXPORT_SYMBOL(tty_port_open);