blob: 4fb3165384c4495bb83a785dc564f76be68e340a [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>
Johan Hovold8cde11b2017-05-18 17:33:00 +020019#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
Vegard Nossum925bb1c2017-05-11 12:18:52 +020037 mutex_lock(&tty->atomic_write_lock);
Rob Herringc3485ee2017-02-02 13:48:05 -060038 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
Vegard Nossum925bb1c2017-05-11 12:18:52 +020039 mutex_unlock(&tty->atomic_write_lock);
Rob Herringc3485ee2017-02-02 13:48:05 -060040
41 tty_ldisc_deref(disc);
42
43 return ret;
44}
45
46static void tty_port_default_wakeup(struct tty_port *port)
47{
48 struct tty_struct *tty = tty_port_tty_get(port);
49
50 if (tty) {
51 tty_wakeup(tty);
52 tty_kref_put(tty);
53 }
54}
55
56static const struct tty_port_client_operations default_client_ops = {
57 .receive_buf = tty_port_default_receive_buf,
58 .write_wakeup = tty_port_default_wakeup,
59};
60
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;
Rob Herringc3485ee2017-02-02 13:48:05 -060072 port->client_ops = &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 *
83 * Provide the tty layer wit ha link from a tty (specified by @index) to a
84 * 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;
93 driver->ports[index] = port;
94}
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
240 * @port: tty port to be doestroyed
241 *
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 */
283
284struct tty_struct *tty_port_tty_get(struct tty_port *port)
285{
286 unsigned long flags;
287 struct tty_struct *tty;
288
289 spin_lock_irqsave(&port->lock, flags);
290 tty = tty_kref_get(port->tty);
291 spin_unlock_irqrestore(&port->lock, flags);
292 return tty;
293}
294EXPORT_SYMBOL(tty_port_tty_get);
295
296/**
297 * tty_port_tty_set - set the tty of a port
298 * @port: tty port
299 * @tty: the tty
300 *
301 * Associate the port and tty pair. Manages any internal refcounts.
302 * Pass NULL to deassociate a port
303 */
304
305void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
306{
307 unsigned long flags;
308
309 spin_lock_irqsave(&port->lock, flags);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100310 tty_kref_put(port->tty);
Alan Coxcb4bca32008-10-21 13:47:44 +0100311 port->tty = tty_kref_get(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100312 spin_unlock_irqrestore(&port->lock, flags);
313}
314EXPORT_SYMBOL(tty_port_tty_set);
Alan Cox31f35932009-01-02 13:45:05 +0000315
Johan Hovold957daca2013-03-07 15:55:51 +0100316static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700317{
Alan Cox64bc3972009-10-06 16:06:11 +0100318 mutex_lock(&port->mutex);
Johan Hovold8bde9652013-03-07 15:55:48 +0100319 if (port->console)
320 goto out;
321
Peter Hurleyd41861c2016-04-09 17:53:25 -0700322 if (tty_port_initialized(port)) {
323 tty_port_set_initialized(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100324 /*
325 * Drop DTR/RTS if HUPCL is set. This causes any attached
326 * modem to hang up the line.
327 */
328 if (tty && C_HUPCL(tty))
329 tty_port_lower_dtr_rts(port);
330
Johan Hovold8bde9652013-03-07 15:55:48 +0100331 if (port->ops->shutdown)
Alan Cox7ca0ff92009-09-19 13:13:20 -0700332 port->ops->shutdown(port);
Johan Hovold8bde9652013-03-07 15:55:48 +0100333 }
334out:
Alan Cox64bc3972009-10-06 16:06:11 +0100335 mutex_unlock(&port->mutex);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700336}
337
Alan Cox31f35932009-01-02 13:45:05 +0000338/**
Alan Cox3e616962009-01-02 13:45:26 +0000339 * tty_port_hangup - hangup helper
340 * @port: tty port
341 *
342 * Perform port level tty hangup flag and count changes. Drop the tty
343 * reference.
Peter Hurley9c9928b2014-06-16 09:17:02 -0400344 *
345 * Caller holds tty lock.
Alan Cox3e616962009-01-02 13:45:26 +0000346 */
347
348void tty_port_hangup(struct tty_port *port)
349{
Johan Hovold957daca2013-03-07 15:55:51 +0100350 struct tty_struct *tty;
Alan Cox3e616962009-01-02 13:45:26 +0000351 unsigned long flags;
352
353 spin_lock_irqsave(&port->lock, flags);
354 port->count = 0;
Johan Hovold957daca2013-03-07 15:55:51 +0100355 tty = port->tty;
356 if (tty)
357 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox3e616962009-01-02 13:45:26 +0000358 port->tty = NULL;
359 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700360 tty_port_set_active(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100361 tty_port_shutdown(port, tty);
362 tty_kref_put(tty);
Alan Cox3e616962009-01-02 13:45:26 +0000363 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -0700364 wake_up_interruptible(&port->delta_msr_wait);
Alan Cox3e616962009-01-02 13:45:26 +0000365}
366EXPORT_SYMBOL(tty_port_hangup);
367
368/**
Jiri Slabyaa27a092013-03-07 13:12:30 +0100369 * tty_port_tty_hangup - helper to hang up a tty
370 *
371 * @port: tty port
372 * @check_clocal: hang only ttys with CLOCAL unset?
373 */
374void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
375{
376 struct tty_struct *tty = tty_port_tty_get(port);
377
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200378 if (tty && (!check_clocal || !C_CLOCAL(tty)))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100379 tty_hangup(tty);
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200380 tty_kref_put(tty);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100381}
382EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
383
384/**
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100385 * tty_port_tty_wakeup - helper to wake up a tty
386 *
387 * @port: tty port
388 */
389void tty_port_tty_wakeup(struct tty_port *port)
390{
Rob Herringc3485ee2017-02-02 13:48:05 -0600391 port->client_ops->write_wakeup(port);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100392}
393EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
394
395/**
Alan Cox31f35932009-01-02 13:45:05 +0000396 * tty_port_carrier_raised - carrier raised check
397 * @port: tty port
398 *
399 * Wrapper for the carrier detect logic. For the moment this is used
400 * to hide some internal details. This will eventually become entirely
401 * internal to the tty port.
402 */
403
404int tty_port_carrier_raised(struct tty_port *port)
405{
406 if (port->ops->carrier_raised == NULL)
407 return 1;
408 return port->ops->carrier_raised(port);
409}
410EXPORT_SYMBOL(tty_port_carrier_raised);
Alan Cox5d951fb2009-01-02 13:45:19 +0000411
412/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100413 * tty_port_raise_dtr_rts - Raise DTR/RTS
Alan Cox5d951fb2009-01-02 13:45:19 +0000414 * @port: tty port
415 *
416 * Wrapper for the DTR/RTS raise logic. For the moment this is used
417 * to hide some internal details. This will eventually become entirely
418 * internal to the tty port.
419 */
420
421void tty_port_raise_dtr_rts(struct tty_port *port)
422{
Alan Coxfcc8ac12009-06-11 12:24:17 +0100423 if (port->ops->dtr_rts)
424 port->ops->dtr_rts(port, 1);
Alan Cox5d951fb2009-01-02 13:45:19 +0000425}
426EXPORT_SYMBOL(tty_port_raise_dtr_rts);
Alan Cox36c621d2009-01-02 13:46:10 +0000427
428/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100429 * tty_port_lower_dtr_rts - Lower DTR/RTS
430 * @port: tty port
431 *
432 * Wrapper for the DTR/RTS raise logic. For the moment this is used
433 * to hide some internal details. This will eventually become entirely
434 * internal to the tty port.
435 */
436
437void tty_port_lower_dtr_rts(struct tty_port *port)
438{
439 if (port->ops->dtr_rts)
440 port->ops->dtr_rts(port, 0);
441}
442EXPORT_SYMBOL(tty_port_lower_dtr_rts);
443
444/**
Alan Cox36c621d2009-01-02 13:46:10 +0000445 * tty_port_block_til_ready - Waiting logic for tty open
446 * @port: the tty port being opened
447 * @tty: the tty device being bound
Alan Coxed3f0af2017-01-16 16:54:29 -0600448 * @filp: the file pointer of the opener or NULL
Alan Cox36c621d2009-01-02 13:46:10 +0000449 *
450 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
451 * Handles:
452 * - hangup (both before and during)
453 * - non blocking open
454 * - rts/dtr/dcd
455 * - signals
456 * - port flags and counts
457 *
458 * The passed tty_port must implement the carrier_raised method if it can
Alan Coxfcc8ac12009-06-11 12:24:17 +0100459 * do carrier detect and the dtr_rts method if it supports software
Alan Cox36c621d2009-01-02 13:46:10 +0000460 * management of these lines. Note that the dtr/rts raise is done each
461 * iteration as a hangup may have previously dropped them while we wait.
Peter Hurleyc590f6b2014-06-16 09:17:01 -0400462 *
463 * Caller holds tty lock.
464 *
465 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
466 * may have changed state (eg., may have been hung up).
Alan Cox36c621d2009-01-02 13:46:10 +0000467 */
Alan Coxd774a562009-10-06 16:06:21 +0100468
Alan Cox36c621d2009-01-02 13:46:10 +0000469int tty_port_block_til_ready(struct tty_port *port,
470 struct tty_struct *tty, struct file *filp)
471{
472 int do_clocal = 0, retval;
473 unsigned long flags;
Jiri Slaby6af9a432009-06-24 18:35:05 +0100474 DEFINE_WAIT(wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000475
Alan Cox36c621d2009-01-02 13:46:10 +0000476 /* if non-blocking mode is set we can pass directly to open unless
477 the port has just hung up or is in another error state */
Peter Hurley18900ca2016-04-09 17:06:48 -0700478 if (tty_io_error(tty)) {
Peter Hurley807c8d812016-04-09 17:53:22 -0700479 tty_port_set_active(port, 1);
Alan Cox8627b962009-11-18 14:12:58 +0000480 return 0;
481 }
Alan Coxed3f0af2017-01-16 16:54:29 -0600482 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
Alan Cox4175f3e2009-10-28 21:12:32 +0100483 /* Indicate we are open */
Peter Hurley9db276f2016-01-10 20:36:15 -0800484 if (C_BAUD(tty))
Alan Cox4175f3e2009-10-28 21:12:32 +0100485 tty_port_raise_dtr_rts(port);
Peter Hurley807c8d812016-04-09 17:53:22 -0700486 tty_port_set_active(port, 1);
Alan Cox36c621d2009-01-02 13:46:10 +0000487 return 0;
488 }
489
490 if (C_CLOCAL(tty))
491 do_clocal = 1;
492
493 /* Block waiting until we can proceed. We may need to wait for the
494 carrier, but we must also wait for any close that is in progress
495 before the next open may complete */
496
497 retval = 0;
Alan Cox36c621d2009-01-02 13:46:10 +0000498
499 /* The port lock protects the port counts */
500 spin_lock_irqsave(&port->lock, flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400501 port->count--;
Alan Cox36c621d2009-01-02 13:46:10 +0000502 port->blocked_open++;
503 spin_unlock_irqrestore(&port->lock, flags);
504
505 while (1) {
506 /* Indicate we are open */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700507 if (C_BAUD(tty) && tty_port_initialized(port))
Alan Cox78349092009-01-02 13:46:43 +0000508 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000509
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100510 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Alan Coxd774a562009-10-06 16:06:21 +0100511 /* Check for a hangup or uninitialised port.
512 Return accordingly */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700513 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
Alan Cox36c621d2009-01-02 13:46:10 +0000514 if (port->flags & ASYNC_HUP_NOTIFY)
515 retval = -EAGAIN;
516 else
517 retval = -ERESTARTSYS;
518 break;
519 }
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100520 /*
521 * Probe the carrier. For devices with no carrier detect
522 * tty_port_carrier_raised will always return true.
523 * Never ask drivers if CLOCAL is set, this causes troubles
524 * on some hardware.
525 */
Peter Hurleyfef062c2015-10-10 16:00:52 -0400526 if (do_clocal || tty_port_carrier_raised(port))
Alan Cox36c621d2009-01-02 13:46:10 +0000527 break;
528 if (signal_pending(current)) {
529 retval = -ERESTARTSYS;
530 break;
531 }
Alan Cox89c8d912012-08-08 16:30:13 +0100532 tty_unlock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000533 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100534 tty_lock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000535 }
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100536 finish_wait(&port->open_wait, &wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000537
538 /* Update counts. A parallel hangup will have set count to zero and
539 we must not mess that up further */
540 spin_lock_irqsave(&port->lock, flags);
541 if (!tty_hung_up_p(filp))
542 port->count++;
543 port->blocked_open--;
Alan Cox36c621d2009-01-02 13:46:10 +0000544 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700545 if (retval == 0)
546 tty_port_set_active(port, 1);
Alan Coxecc2e052009-07-17 16:17:26 +0100547 return retval;
Alan Cox36c621d2009-01-02 13:46:10 +0000548}
549EXPORT_SYMBOL(tty_port_block_til_ready);
550
Johan Hovoldb74414f2013-03-07 15:55:52 +0100551static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
552{
553 unsigned int bps = tty_get_baud_rate(tty);
554 long timeout;
555
556 if (bps > 1200) {
557 timeout = (HZ * 10 * port->drain_delay) / bps;
558 timeout = max_t(long, timeout, HZ / 10);
559 } else {
560 timeout = 2 * HZ;
561 }
562 schedule_timeout_interruptible(timeout);
563}
564
Peter Hurley79c1faa2015-10-10 16:00:51 -0400565/* Caller holds tty lock. */
Alan Coxd774a562009-10-06 16:06:21 +0100566int tty_port_close_start(struct tty_port *port,
567 struct tty_struct *tty, struct file *filp)
Alan Coxa6614992009-01-02 13:46:50 +0000568{
569 unsigned long flags;
570
Peter Hurley633caba2014-11-05 12:40:03 -0500571 if (tty_hung_up_p(filp))
Alan Coxa6614992009-01-02 13:46:50 +0000572 return 0;
Alan Coxa6614992009-01-02 13:46:50 +0000573
Peter Hurley633caba2014-11-05 12:40:03 -0500574 spin_lock_irqsave(&port->lock, flags);
Alan Coxd774a562009-10-06 16:06:21 +0100575 if (tty->count == 1 && port->count != 1) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500576 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
577 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000578 port->count = 1;
579 }
580 if (--port->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500581 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
582 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000583 port->count = 0;
584 }
585
586 if (port->count) {
587 spin_unlock_irqrestore(&port->lock, flags);
588 return 0;
589 }
Alan Coxa6614992009-01-02 13:46:50 +0000590 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100591
Peter Hurleyddc7b752014-06-16 09:17:03 -0400592 tty->closing = 1;
593
Peter Hurleyd41861c2016-04-09 17:53:25 -0700594 if (tty_port_initialized(port)) {
Johan Hovold0b2588c2013-03-07 15:55:53 +0100595 /* Don't block on a stalled port, just pull the chain */
596 if (tty->flow_stopped)
597 tty_driver_flush_buffer(tty);
598 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Peter Hurley79c1faa2015-10-10 16:00:51 -0400599 tty_wait_until_sent(tty, port->closing_wait);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100600 if (port->drain_delay)
601 tty_port_drain_delay(port, tty);
602 }
Alan Coxe707c352009-11-05 13:27:57 +0000603 /* Flush the ldisc buffering */
604 tty_ldisc_flush(tty);
605
Peter Hurley469d6d02013-09-18 20:47:06 -0400606 /* Report to caller this is the last port reference */
Alan Coxa6614992009-01-02 13:46:50 +0000607 return 1;
608}
609EXPORT_SYMBOL(tty_port_close_start);
610
Peter Hurley0733db912014-06-16 09:16:59 -0400611/* Caller holds tty lock */
Alan Coxa6614992009-01-02 13:46:50 +0000612void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
613{
614 unsigned long flags;
615
Peter Hurley3f40f5b2014-11-05 12:40:05 -0500616 tty_ldisc_flush(tty);
Alan Coxa6614992009-01-02 13:46:50 +0000617 tty->closing = 0;
618
Peter Hurleyddc7b752014-06-16 09:17:03 -0400619 spin_lock_irqsave(&port->lock, flags);
620
Alan Coxa6614992009-01-02 13:46:50 +0000621 if (port->blocked_open) {
622 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley5823323e2016-01-10 20:36:14 -0800623 if (port->close_delay)
624 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Alan Coxa6614992009-01-02 13:46:50 +0000625 spin_lock_irqsave(&port->lock, flags);
626 wake_up_interruptible(&port->open_wait);
627 }
Alan Coxa6614992009-01-02 13:46:50 +0000628 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700629 tty_port_set_active(port, 0);
Alan Coxa6614992009-01-02 13:46:50 +0000630}
631EXPORT_SYMBOL(tty_port_close_end);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700632
Peter Hurley0733db912014-06-16 09:16:59 -0400633/**
634 * tty_port_close
635 *
636 * Caller holds tty lock
Peter Hurley0733db912014-06-16 09:16:59 -0400637 */
Alan Cox7ca0ff92009-09-19 13:13:20 -0700638void tty_port_close(struct tty_port *port, struct tty_struct *tty,
639 struct file *filp)
640{
641 if (tty_port_close_start(port, tty, filp) == 0)
642 return;
Johan Hovold957daca2013-03-07 15:55:51 +0100643 tty_port_shutdown(port, tty);
Alan Coxd74e8282009-11-30 13:16:52 +0000644 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700645 tty_port_close_end(port, tty);
646 tty_port_tty_set(port, NULL);
647}
648EXPORT_SYMBOL(tty_port_close);
Alan Cox64bc3972009-10-06 16:06:11 +0100649
Jiri Slaby72a33bf2012-08-07 21:47:49 +0200650/**
651 * tty_port_install - generic tty->ops->install handler
652 * @port: tty_port of the device
653 * @driver: tty_driver for this device
654 * @tty: tty to be installed
655 *
656 * It is the same as tty_standard_install except the provided @port is linked
657 * to a concrete tty specified by @tty. Use this or tty_port_register_device
658 * (or both). Call tty_port_link_device as a last resort.
659 */
Jiri Slaby695586c2012-06-04 13:35:32 +0200660int tty_port_install(struct tty_port *port, struct tty_driver *driver,
661 struct tty_struct *tty)
662{
663 tty->port = port;
664 return tty_standard_install(driver, tty);
665}
666EXPORT_SYMBOL_GPL(tty_port_install);
667
Peter Hurleyaddd4672014-06-16 09:17:00 -0400668/**
669 * tty_port_open
670 *
671 * Caller holds tty lock.
672 *
673 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
674 * tty and tty_port may have changed state (eg., may be hung up now)
675 */
Alan Cox64bc3972009-10-06 16:06:11 +0100676int tty_port_open(struct tty_port *port, struct tty_struct *tty,
Alan Coxd774a562009-10-06 16:06:21 +0100677 struct file *filp)
Alan Cox64bc3972009-10-06 16:06:11 +0100678{
679 spin_lock_irq(&port->lock);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400680 ++port->count;
Alan Cox64bc3972009-10-06 16:06:11 +0100681 spin_unlock_irq(&port->lock);
682 tty_port_tty_set(port, tty);
683
684 /*
685 * Do the device-specific open only if the hardware isn't
686 * already initialized. Serialize open and shutdown using the
687 * port mutex.
688 */
689
690 mutex_lock(&port->mutex);
691
Peter Hurleyd41861c2016-04-09 17:53:25 -0700692 if (!tty_port_initialized(port)) {
Alan Coxa9a37ec2009-11-30 13:16:57 +0000693 clear_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox64bc3972009-10-06 16:06:11 +0100694 if (port->ops->activate) {
695 int retval = port->ops->activate(port, tty);
696 if (retval) {
Alan Coxd774a562009-10-06 16:06:21 +0100697 mutex_unlock(&port->mutex);
698 return retval;
699 }
700 }
Peter Hurleyd41861c2016-04-09 17:53:25 -0700701 tty_port_set_initialized(port, 1);
Alan Cox64bc3972009-10-06 16:06:11 +0100702 }
703 mutex_unlock(&port->mutex);
704 return tty_port_block_til_ready(port, tty, filp);
705}
706
707EXPORT_SYMBOL(tty_port_open);