blob: 6b137194069fee47bc45b1866865ee27adfd33c5 [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
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{
132 tty_port_link_device(port, driver, index);
133 return tty_register_device_attr(driver, index, device, drvdata,
134 attr_grp);
135}
136EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
137
Johan Hovold8cde11b2017-05-18 17:33:00 +0200138/**
139 * tty_port_register_device_attr_serdev - register tty or serdev device
140 * @port: tty_port of the device
141 * @driver: tty_driver for this device
142 * @index: index of the tty
143 * @device: parent if exists, otherwise NULL
144 * @drvdata: driver data for the device
145 * @attr_grp: attribute group for the device
146 *
147 * Register a serdev or tty device depending on if the parent device has any
148 * defined serdev clients or not.
149 */
150struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
151 struct tty_driver *driver, unsigned index,
152 struct device *device, void *drvdata,
153 const struct attribute_group **attr_grp)
154{
155 struct device *dev;
156
157 tty_port_link_device(port, driver, index);
158
159 dev = serdev_tty_port_register(port, device, driver, index);
160 if (PTR_ERR(dev) != -ENODEV) {
161 /* Skip creating cdev if we registered a serdev device */
162 return dev;
163 }
164
165 return tty_register_device_attr(driver, index, device, drvdata,
166 attr_grp);
167}
168EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
169
170/**
171 * tty_port_register_device_serdev - register tty or serdev device
172 * @port: tty_port of the device
173 * @driver: tty_driver for this device
174 * @index: index of the tty
175 * @device: parent if exists, otherwise NULL
176 *
177 * Register a serdev or tty device depending on if the parent device has any
178 * defined serdev clients or not.
179 */
180struct device *tty_port_register_device_serdev(struct tty_port *port,
181 struct tty_driver *driver, unsigned index,
182 struct device *device)
183{
184 return tty_port_register_device_attr_serdev(port, driver, index,
185 device, NULL, NULL);
186}
187EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
188
189/**
190 * tty_port_unregister_device - deregister a tty or serdev device
191 * @port: tty_port of the device
192 * @driver: tty_driver for this device
193 * @index: index of the tty
194 *
195 * If a tty or serdev device is registered with a call to
196 * tty_port_register_device_serdev() then this function must be called when
197 * the device is gone.
198 */
199void tty_port_unregister_device(struct tty_port *port,
200 struct tty_driver *driver, unsigned index)
201{
202 int ret;
203
204 ret = serdev_tty_port_unregister(port);
205 if (ret == 0)
206 return;
207
208 tty_unregister_device(driver, index);
209}
210EXPORT_SYMBOL_GPL(tty_port_unregister_device);
211
Alan Cox9e485652008-10-13 10:37:07 +0100212int tty_port_alloc_xmit_buf(struct tty_port *port)
213{
214 /* We may sleep in get_zeroed_page() */
Alan Cox44e49092009-11-30 13:16:41 +0000215 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100216 if (port->xmit_buf == NULL)
217 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
Alan Cox44e49092009-11-30 13:16:41 +0000218 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100219 if (port->xmit_buf == NULL)
220 return -ENOMEM;
221 return 0;
222}
223EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
224
225void tty_port_free_xmit_buf(struct tty_port *port)
226{
Alan Cox44e49092009-11-30 13:16:41 +0000227 mutex_lock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100228 if (port->xmit_buf != NULL) {
229 free_page((unsigned long)port->xmit_buf);
230 port->xmit_buf = NULL;
231 }
Alan Cox44e49092009-11-30 13:16:41 +0000232 mutex_unlock(&port->buf_mutex);
Alan Cox9e485652008-10-13 10:37:07 +0100233}
234EXPORT_SYMBOL(tty_port_free_xmit_buf);
235
Jiri Slabyde274bf2012-11-15 09:49:54 +0100236/**
237 * tty_port_destroy -- destroy inited port
238 * @port: tty port to be doestroyed
239 *
240 * When a port was initialized using tty_port_init, one has to destroy the
241 * port by this function. Either indirectly by using tty_port refcounting
242 * (tty_port_put) or directly if refcounting is not used.
243 */
244void tty_port_destroy(struct tty_port *port)
245{
Peter Hurleye1760582015-10-17 16:36:23 -0400246 tty_buffer_cancel_work(port);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100247 tty_buffer_free_all(port);
248}
249EXPORT_SYMBOL(tty_port_destroy);
250
Alan Cox568aafc2009-11-30 13:17:14 +0000251static void tty_port_destructor(struct kref *kref)
252{
253 struct tty_port *port = container_of(kref, struct tty_port, kref);
Peter Hurleye3bfea22013-09-18 20:42:39 -0400254
255 /* check if last port ref was dropped before tty release */
256 if (WARN_ON(port->itty))
257 return;
Alan Cox568aafc2009-11-30 13:17:14 +0000258 if (port->xmit_buf)
259 free_page((unsigned long)port->xmit_buf);
Jiri Slabyde274bf2012-11-15 09:49:54 +0100260 tty_port_destroy(port);
Jiri Slaby81c79832012-11-15 09:49:49 +0100261 if (port->ops && port->ops->destruct)
Alan Cox568aafc2009-11-30 13:17:14 +0000262 port->ops->destruct(port);
263 else
264 kfree(port);
265}
266
267void tty_port_put(struct tty_port *port)
268{
269 if (port)
270 kref_put(&port->kref, tty_port_destructor);
271}
272EXPORT_SYMBOL(tty_port_put);
Alan Cox9e485652008-10-13 10:37:07 +0100273
Alan Cox4a90f092008-10-13 10:39:46 +0100274/**
275 * tty_port_tty_get - get a tty reference
276 * @port: tty port
277 *
278 * Return a refcount protected tty instance or NULL if the port is not
279 * associated with a tty (eg due to close or hangup)
280 */
281
282struct tty_struct *tty_port_tty_get(struct tty_port *port)
283{
284 unsigned long flags;
285 struct tty_struct *tty;
286
287 spin_lock_irqsave(&port->lock, flags);
288 tty = tty_kref_get(port->tty);
289 spin_unlock_irqrestore(&port->lock, flags);
290 return tty;
291}
292EXPORT_SYMBOL(tty_port_tty_get);
293
294/**
295 * tty_port_tty_set - set the tty of a port
296 * @port: tty port
297 * @tty: the tty
298 *
299 * Associate the port and tty pair. Manages any internal refcounts.
300 * Pass NULL to deassociate a port
301 */
302
303void 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 Hovold8bde9652013-03-07 15:55:48 +0100329 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 */
345
346void tty_port_hangup(struct tty_port *port)
347{
Johan Hovold957daca2013-03-07 15:55:51 +0100348 struct tty_struct *tty;
Alan Cox3e616962009-01-02 13:45:26 +0000349 unsigned long flags;
350
351 spin_lock_irqsave(&port->lock, flags);
352 port->count = 0;
Johan Hovold957daca2013-03-07 15:55:51 +0100353 tty = port->tty;
354 if (tty)
355 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox3e616962009-01-02 13:45:26 +0000356 port->tty = NULL;
357 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700358 tty_port_set_active(port, 0);
Johan Hovold957daca2013-03-07 15:55:51 +0100359 tty_port_shutdown(port, tty);
360 tty_kref_put(tty);
Alan Cox3e616962009-01-02 13:45:26 +0000361 wake_up_interruptible(&port->open_wait);
Alan Coxbdc04e32009-09-19 13:13:31 -0700362 wake_up_interruptible(&port->delta_msr_wait);
Alan Cox3e616962009-01-02 13:45:26 +0000363}
364EXPORT_SYMBOL(tty_port_hangup);
365
366/**
Jiri Slabyaa27a092013-03-07 13:12:30 +0100367 * tty_port_tty_hangup - helper to hang up a tty
368 *
369 * @port: tty port
370 * @check_clocal: hang only ttys with CLOCAL unset?
371 */
372void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
373{
374 struct tty_struct *tty = tty_port_tty_get(port);
375
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200376 if (tty && (!check_clocal || !C_CLOCAL(tty)))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100377 tty_hangup(tty);
Gianluca Anzolin1d9e6892013-07-25 07:26:16 +0200378 tty_kref_put(tty);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100379}
380EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
381
382/**
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100383 * tty_port_tty_wakeup - helper to wake up a tty
384 *
385 * @port: tty port
386 */
387void tty_port_tty_wakeup(struct tty_port *port)
388{
Rob Herringc3485ee2017-02-02 13:48:05 -0600389 port->client_ops->write_wakeup(port);
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100390}
391EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
392
393/**
Alan Cox31f35932009-01-02 13:45:05 +0000394 * tty_port_carrier_raised - carrier raised check
395 * @port: tty port
396 *
397 * Wrapper for the carrier detect logic. For the moment this is used
398 * to hide some internal details. This will eventually become entirely
399 * internal to the tty port.
400 */
401
402int tty_port_carrier_raised(struct tty_port *port)
403{
404 if (port->ops->carrier_raised == NULL)
405 return 1;
406 return port->ops->carrier_raised(port);
407}
408EXPORT_SYMBOL(tty_port_carrier_raised);
Alan Cox5d951fb2009-01-02 13:45:19 +0000409
410/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100411 * tty_port_raise_dtr_rts - Raise DTR/RTS
Alan Cox5d951fb2009-01-02 13:45:19 +0000412 * @port: tty port
413 *
414 * Wrapper for the DTR/RTS raise logic. For the moment this is used
415 * to hide some internal details. This will eventually become entirely
416 * internal to the tty port.
417 */
418
419void tty_port_raise_dtr_rts(struct tty_port *port)
420{
Alan Coxfcc8ac12009-06-11 12:24:17 +0100421 if (port->ops->dtr_rts)
422 port->ops->dtr_rts(port, 1);
Alan Cox5d951fb2009-01-02 13:45:19 +0000423}
424EXPORT_SYMBOL(tty_port_raise_dtr_rts);
Alan Cox36c621d2009-01-02 13:46:10 +0000425
426/**
Alan Coxfcc8ac12009-06-11 12:24:17 +0100427 * tty_port_lower_dtr_rts - Lower DTR/RTS
428 * @port: tty port
429 *
430 * Wrapper for the DTR/RTS raise logic. For the moment this is used
431 * to hide some internal details. This will eventually become entirely
432 * internal to the tty port.
433 */
434
435void tty_port_lower_dtr_rts(struct tty_port *port)
436{
437 if (port->ops->dtr_rts)
438 port->ops->dtr_rts(port, 0);
439}
440EXPORT_SYMBOL(tty_port_lower_dtr_rts);
441
442/**
Alan Cox36c621d2009-01-02 13:46:10 +0000443 * tty_port_block_til_ready - Waiting logic for tty open
444 * @port: the tty port being opened
445 * @tty: the tty device being bound
Alan Coxed3f0af2017-01-16 16:54:29 -0600446 * @filp: the file pointer of the opener or NULL
Alan Cox36c621d2009-01-02 13:46:10 +0000447 *
448 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
449 * Handles:
450 * - hangup (both before and during)
451 * - non blocking open
452 * - rts/dtr/dcd
453 * - signals
454 * - port flags and counts
455 *
456 * The passed tty_port must implement the carrier_raised method if it can
Alan Coxfcc8ac12009-06-11 12:24:17 +0100457 * do carrier detect and the dtr_rts method if it supports software
Alan Cox36c621d2009-01-02 13:46:10 +0000458 * management of these lines. Note that the dtr/rts raise is done each
459 * iteration as a hangup may have previously dropped them while we wait.
Peter Hurleyc590f6b2014-06-16 09:17:01 -0400460 *
461 * Caller holds tty lock.
462 *
463 * NB: May drop and reacquire tty lock when blocking, so tty and tty_port
464 * may have changed state (eg., may have been hung up).
Alan Cox36c621d2009-01-02 13:46:10 +0000465 */
Alan Coxd774a562009-10-06 16:06:21 +0100466
Alan Cox36c621d2009-01-02 13:46:10 +0000467int tty_port_block_til_ready(struct tty_port *port,
468 struct tty_struct *tty, struct file *filp)
469{
470 int do_clocal = 0, retval;
471 unsigned long flags;
Jiri Slaby6af9a432009-06-24 18:35:05 +0100472 DEFINE_WAIT(wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000473
Alan Cox36c621d2009-01-02 13:46:10 +0000474 /* if non-blocking mode is set we can pass directly to open unless
475 the port has just hung up or is in another error state */
Peter Hurley18900ca2016-04-09 17:06:48 -0700476 if (tty_io_error(tty)) {
Peter Hurley807c8d812016-04-09 17:53:22 -0700477 tty_port_set_active(port, 1);
Alan Cox8627b962009-11-18 14:12:58 +0000478 return 0;
479 }
Alan Coxed3f0af2017-01-16 16:54:29 -0600480 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
Alan Cox4175f3e2009-10-28 21:12:32 +0100481 /* Indicate we are open */
Peter Hurley9db276f2016-01-10 20:36:15 -0800482 if (C_BAUD(tty))
Alan Cox4175f3e2009-10-28 21:12:32 +0100483 tty_port_raise_dtr_rts(port);
Peter Hurley807c8d812016-04-09 17:53:22 -0700484 tty_port_set_active(port, 1);
Alan Cox36c621d2009-01-02 13:46:10 +0000485 return 0;
486 }
487
488 if (C_CLOCAL(tty))
489 do_clocal = 1;
490
491 /* Block waiting until we can proceed. We may need to wait for the
492 carrier, but we must also wait for any close that is in progress
493 before the next open may complete */
494
495 retval = 0;
Alan Cox36c621d2009-01-02 13:46:10 +0000496
497 /* The port lock protects the port counts */
498 spin_lock_irqsave(&port->lock, flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400499 port->count--;
Alan Cox36c621d2009-01-02 13:46:10 +0000500 port->blocked_open++;
501 spin_unlock_irqrestore(&port->lock, flags);
502
503 while (1) {
504 /* Indicate we are open */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700505 if (C_BAUD(tty) && tty_port_initialized(port))
Alan Cox78349092009-01-02 13:46:43 +0000506 tty_port_raise_dtr_rts(port);
Alan Cox36c621d2009-01-02 13:46:10 +0000507
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100508 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Alan Coxd774a562009-10-06 16:06:21 +0100509 /* Check for a hangup or uninitialised port.
510 Return accordingly */
Peter Hurleyd41861c2016-04-09 17:53:25 -0700511 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
Alan Cox36c621d2009-01-02 13:46:10 +0000512 if (port->flags & ASYNC_HUP_NOTIFY)
513 retval = -EAGAIN;
514 else
515 retval = -ERESTARTSYS;
516 break;
517 }
Jiri Slaby0eee50a2012-01-12 22:55:15 +0100518 /*
519 * Probe the carrier. For devices with no carrier detect
520 * tty_port_carrier_raised will always return true.
521 * Never ask drivers if CLOCAL is set, this causes troubles
522 * on some hardware.
523 */
Peter Hurleyfef062c2015-10-10 16:00:52 -0400524 if (do_clocal || tty_port_carrier_raised(port))
Alan Cox36c621d2009-01-02 13:46:10 +0000525 break;
526 if (signal_pending(current)) {
527 retval = -ERESTARTSYS;
528 break;
529 }
Alan Cox89c8d912012-08-08 16:30:13 +0100530 tty_unlock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000531 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +0100532 tty_lock(tty);
Alan Cox36c621d2009-01-02 13:46:10 +0000533 }
Jiri Slaby3e3b5c02009-06-11 14:33:37 +0100534 finish_wait(&port->open_wait, &wait);
Alan Cox36c621d2009-01-02 13:46:10 +0000535
536 /* Update counts. A parallel hangup will have set count to zero and
537 we must not mess that up further */
538 spin_lock_irqsave(&port->lock, flags);
539 if (!tty_hung_up_p(filp))
540 port->count++;
541 port->blocked_open--;
Alan Cox36c621d2009-01-02 13:46:10 +0000542 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700543 if (retval == 0)
544 tty_port_set_active(port, 1);
Alan Coxecc2e052009-07-17 16:17:26 +0100545 return retval;
Alan Cox36c621d2009-01-02 13:46:10 +0000546}
547EXPORT_SYMBOL(tty_port_block_til_ready);
548
Johan Hovoldb74414f2013-03-07 15:55:52 +0100549static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
550{
551 unsigned int bps = tty_get_baud_rate(tty);
552 long timeout;
553
554 if (bps > 1200) {
555 timeout = (HZ * 10 * port->drain_delay) / bps;
556 timeout = max_t(long, timeout, HZ / 10);
557 } else {
558 timeout = 2 * HZ;
559 }
560 schedule_timeout_interruptible(timeout);
561}
562
Peter Hurley79c1faa2015-10-10 16:00:51 -0400563/* Caller holds tty lock. */
Alan Coxd774a562009-10-06 16:06:21 +0100564int tty_port_close_start(struct tty_port *port,
565 struct tty_struct *tty, struct file *filp)
Alan Coxa6614992009-01-02 13:46:50 +0000566{
567 unsigned long flags;
568
Peter Hurley633caba2014-11-05 12:40:03 -0500569 if (tty_hung_up_p(filp))
Alan Coxa6614992009-01-02 13:46:50 +0000570 return 0;
Alan Coxa6614992009-01-02 13:46:50 +0000571
Peter Hurley633caba2014-11-05 12:40:03 -0500572 spin_lock_irqsave(&port->lock, flags);
Alan Coxd774a562009-10-06 16:06:21 +0100573 if (tty->count == 1 && port->count != 1) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500574 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
575 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000576 port->count = 1;
577 }
578 if (--port->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500579 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
580 port->count);
Alan Coxa6614992009-01-02 13:46:50 +0000581 port->count = 0;
582 }
583
584 if (port->count) {
585 spin_unlock_irqrestore(&port->lock, flags);
586 return 0;
587 }
Alan Coxa6614992009-01-02 13:46:50 +0000588 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100589
Peter Hurleyddc7b752014-06-16 09:17:03 -0400590 tty->closing = 1;
591
Peter Hurleyd41861c2016-04-09 17:53:25 -0700592 if (tty_port_initialized(port)) {
Johan Hovold0b2588c2013-03-07 15:55:53 +0100593 /* Don't block on a stalled port, just pull the chain */
594 if (tty->flow_stopped)
595 tty_driver_flush_buffer(tty);
596 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Peter Hurley79c1faa2015-10-10 16:00:51 -0400597 tty_wait_until_sent(tty, port->closing_wait);
Johan Hovold0b2588c2013-03-07 15:55:53 +0100598 if (port->drain_delay)
599 tty_port_drain_delay(port, tty);
600 }
Alan Coxe707c352009-11-05 13:27:57 +0000601 /* Flush the ldisc buffering */
602 tty_ldisc_flush(tty);
603
Peter Hurley469d6d02013-09-18 20:47:06 -0400604 /* Report to caller this is the last port reference */
Alan Coxa6614992009-01-02 13:46:50 +0000605 return 1;
606}
607EXPORT_SYMBOL(tty_port_close_start);
608
Peter Hurley0733db92014-06-16 09:16:59 -0400609/* Caller holds tty lock */
Alan Coxa6614992009-01-02 13:46:50 +0000610void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
611{
612 unsigned long flags;
613
Peter Hurley3f40f5b2014-11-05 12:40:05 -0500614 tty_ldisc_flush(tty);
Alan Coxa6614992009-01-02 13:46:50 +0000615 tty->closing = 0;
616
Peter Hurleyddc7b752014-06-16 09:17:03 -0400617 spin_lock_irqsave(&port->lock, flags);
618
Alan Coxa6614992009-01-02 13:46:50 +0000619 if (port->blocked_open) {
620 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley5823323e2016-01-10 20:36:14 -0800621 if (port->close_delay)
622 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Alan Coxa6614992009-01-02 13:46:50 +0000623 spin_lock_irqsave(&port->lock, flags);
624 wake_up_interruptible(&port->open_wait);
625 }
Alan Coxa6614992009-01-02 13:46:50 +0000626 spin_unlock_irqrestore(&port->lock, flags);
Peter Hurley807c8d812016-04-09 17:53:22 -0700627 tty_port_set_active(port, 0);
Alan Coxa6614992009-01-02 13:46:50 +0000628}
629EXPORT_SYMBOL(tty_port_close_end);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700630
Peter Hurley0733db92014-06-16 09:16:59 -0400631/**
632 * tty_port_close
633 *
634 * Caller holds tty lock
Peter Hurley0733db92014-06-16 09:16:59 -0400635 */
Alan Cox7ca0ff92009-09-19 13:13:20 -0700636void tty_port_close(struct tty_port *port, struct tty_struct *tty,
637 struct file *filp)
638{
639 if (tty_port_close_start(port, tty, filp) == 0)
640 return;
Johan Hovold957daca2013-03-07 15:55:51 +0100641 tty_port_shutdown(port, tty);
Alan Coxd74e8282009-11-30 13:16:52 +0000642 set_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox7ca0ff92009-09-19 13:13:20 -0700643 tty_port_close_end(port, tty);
644 tty_port_tty_set(port, NULL);
645}
646EXPORT_SYMBOL(tty_port_close);
Alan Cox64bc3972009-10-06 16:06:11 +0100647
Jiri Slaby72a33bf2012-08-07 21:47:49 +0200648/**
649 * tty_port_install - generic tty->ops->install handler
650 * @port: tty_port of the device
651 * @driver: tty_driver for this device
652 * @tty: tty to be installed
653 *
654 * It is the same as tty_standard_install except the provided @port is linked
655 * to a concrete tty specified by @tty. Use this or tty_port_register_device
656 * (or both). Call tty_port_link_device as a last resort.
657 */
Jiri Slaby695586c2012-06-04 13:35:32 +0200658int tty_port_install(struct tty_port *port, struct tty_driver *driver,
659 struct tty_struct *tty)
660{
661 tty->port = port;
662 return tty_standard_install(driver, tty);
663}
664EXPORT_SYMBOL_GPL(tty_port_install);
665
Peter Hurleyaddd4672014-06-16 09:17:00 -0400666/**
667 * tty_port_open
668 *
669 * Caller holds tty lock.
670 *
671 * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
672 * tty and tty_port may have changed state (eg., may be hung up now)
673 */
Alan Cox64bc3972009-10-06 16:06:11 +0100674int tty_port_open(struct tty_port *port, struct tty_struct *tty,
Alan Coxd774a562009-10-06 16:06:21 +0100675 struct file *filp)
Alan Cox64bc3972009-10-06 16:06:11 +0100676{
677 spin_lock_irq(&port->lock);
Peter Hurleye359a4e2014-06-16 09:17:06 -0400678 ++port->count;
Alan Cox64bc3972009-10-06 16:06:11 +0100679 spin_unlock_irq(&port->lock);
680 tty_port_tty_set(port, tty);
681
682 /*
683 * Do the device-specific open only if the hardware isn't
684 * already initialized. Serialize open and shutdown using the
685 * port mutex.
686 */
687
688 mutex_lock(&port->mutex);
689
Peter Hurleyd41861c2016-04-09 17:53:25 -0700690 if (!tty_port_initialized(port)) {
Alan Coxa9a37ec2009-11-30 13:16:57 +0000691 clear_bit(TTY_IO_ERROR, &tty->flags);
Alan Cox64bc3972009-10-06 16:06:11 +0100692 if (port->ops->activate) {
693 int retval = port->ops->activate(port, tty);
694 if (retval) {
Alan Coxd774a562009-10-06 16:06:21 +0100695 mutex_unlock(&port->mutex);
696 return retval;
697 }
698 }
Peter Hurleyd41861c2016-04-09 17:53:25 -0700699 tty_port_set_initialized(port, 1);
Alan Cox64bc3972009-10-06 16:06:11 +0100700 }
701 mutex_unlock(&port->mutex);
702 return tty_port_block_til_ready(port, tty, filp);
703}
704
705EXPORT_SYMBOL(tty_port_open);