Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / drivers / tty / tty_port.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-2.0
9e48565d
AC
2/*
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>
3e61696b 11#include <linux/serial.h>
9e48565d
AC
12#include <linux/timer.h>
13#include <linux/string.h>
14#include <linux/slab.h>
174cd4b1 15#include <linux/sched/signal.h>
9e48565d
AC
16#include <linux/wait.h>
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/module.h>
8cde11b2 20#include <linux/serdev.h>
98602c01 21#include "tty.h"
9e48565d 22
0468a807
JSS
23static size_t tty_port_default_receive_buf(struct tty_port *port, const u8 *p,
24 const u8 *f, size_t count)
c3485ee0 25{
c3485ee0 26 struct tty_struct *tty;
c6e37fe0 27 struct tty_ldisc *ld;
c3485ee0
RH
28
29 tty = READ_ONCE(port->itty);
30 if (!tty)
31 return 0;
32
c6e37fe0
JSS
33 ld = tty_ldisc_ref(tty);
34 if (!ld)
c3485ee0
RH
35 return 0;
36
892bc209 37 count = tty_ldisc_receive_buf(ld, p, f, count);
c3485ee0 38
c6e37fe0 39 tty_ldisc_deref(ld);
c3485ee0 40
0468a807 41 return count;
c3485ee0
RH
42}
43
0b7a2b28 44static void tty_port_default_lookahead_buf(struct tty_port *port, const u8 *p,
0468a807 45 const u8 *f, size_t count)
6bb6fa69
IJ
46{
47 struct tty_struct *tty;
c6e37fe0 48 struct tty_ldisc *ld;
6bb6fa69
IJ
49
50 tty = READ_ONCE(port->itty);
51 if (!tty)
52 return;
53
c6e37fe0
JSS
54 ld = tty_ldisc_ref(tty);
55 if (!ld)
6bb6fa69
IJ
56 return;
57
c6e37fe0
JSS
58 if (ld->ops->lookahead_buf)
59 ld->ops->lookahead_buf(ld->tty, p, f, count);
6bb6fa69 60
c6e37fe0 61 tty_ldisc_deref(ld);
6bb6fa69
IJ
62}
63
c3485ee0
RH
64static void tty_port_default_wakeup(struct tty_port *port)
65{
66 struct tty_struct *tty = tty_port_tty_get(port);
67
68 if (tty) {
69 tty_wakeup(tty);
70 tty_kref_put(tty);
71 }
72}
73
0c5aae59 74const struct tty_port_client_operations tty_port_default_client_ops = {
c3485ee0 75 .receive_buf = tty_port_default_receive_buf,
6bb6fa69 76 .lookahead_buf = tty_port_default_lookahead_buf,
c3485ee0
RH
77 .write_wakeup = tty_port_default_wakeup,
78};
0c5aae59 79EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
c3485ee0 80
3be491d7 81/**
c38f45ef 82 * tty_port_init - initialize tty_port
3be491d7
JS
83 * @port: tty_port to initialize
84 *
85 * Initializes the state of struct tty_port. When a port was initialized using
86 * this function, one has to destroy the port by tty_port_destroy(). Either
87 * indirectly by using &tty_port refcounting (tty_port_put()) or directly if
88 * refcounting is not used.
89 */
9e48565d
AC
90void tty_port_init(struct tty_port *port)
91{
92 memset(port, 0, sizeof(*port));
ecbbfd44 93 tty_buffer_init(port);
9e48565d 94 init_waitqueue_head(&port->open_wait);
bdc04e31 95 init_waitqueue_head(&port->delta_msr_wait);
9e48565d 96 mutex_init(&port->mutex);
44e4909e 97 mutex_init(&port->buf_mutex);
4a90f09b 98 spin_lock_init(&port->lock);
9e48565d
AC
99 port->close_delay = (50 * HZ) / 100;
100 port->closing_wait = (3000 * HZ) / 100;
0c5aae59 101 port->client_ops = &tty_port_default_client_ops;
568aafc6 102 kref_init(&port->kref);
9e48565d
AC
103}
104EXPORT_SYMBOL(tty_port_init);
105
2cb4ca02
JS
106/**
107 * tty_port_link_device - link tty and tty_port
108 * @port: tty_port of the device
109 * @driver: tty_driver for this device
110 * @index: index of the tty
111 *
1926e5d3 112 * Provide the tty layer with a link from a tty (specified by @index) to a
cb6f6f98
JS
113 * tty_port (@port). Use this only if neither tty_port_register_device() nor
114 * tty_port_install() is used in the driver. If used, this has to be called
115 * before tty_register_driver().
2cb4ca02
JS
116 */
117void tty_port_link_device(struct tty_port *port,
118 struct tty_driver *driver, unsigned index)
119{
120 if (WARN_ON(index >= driver->num))
121 return;
273f6329 122 driver->ports[index] = port;
2cb4ca02
JS
123}
124EXPORT_SYMBOL_GPL(tty_port_link_device);
125
72a33bf5
JS
126/**
127 * tty_port_register_device - register tty device
128 * @port: tty_port of the device
129 * @driver: tty_driver for this device
130 * @index: index of the tty
131 * @device: parent if exists, otherwise NULL
132 *
cb6f6f98
JS
133 * It is the same as tty_register_device() except the provided @port is linked
134 * to a concrete tty specified by @index. Use this or tty_port_install() (or
135 * both). Call tty_port_link_device() as a last resort.
72a33bf5 136 */
057eb856
JS
137struct device *tty_port_register_device(struct tty_port *port,
138 struct tty_driver *driver, unsigned index,
139 struct device *device)
140{
3086365d 141 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
057eb856
JS
142}
143EXPORT_SYMBOL_GPL(tty_port_register_device);
144
b1b79916
TH
145/**
146 * tty_port_register_device_attr - register tty device
147 * @port: tty_port of the device
148 * @driver: tty_driver for this device
149 * @index: index of the tty
150 * @device: parent if exists, otherwise NULL
151 * @drvdata: Driver data to be set to device.
152 * @attr_grp: Attribute group to be set on device.
153 *
cb6f6f98
JS
154 * It is the same as tty_register_device_attr() except the provided @port is
155 * linked to a concrete tty specified by @index. Use this or tty_port_install()
156 * (or both). Call tty_port_link_device() as a last resort.
b1b79916
TH
157 */
158struct device *tty_port_register_device_attr(struct tty_port *port,
159 struct tty_driver *driver, unsigned index,
160 struct device *device, void *drvdata,
161 const struct attribute_group **attr_grp)
162{
163 tty_port_link_device(port, driver, index);
164 return tty_register_device_attr(driver, index, device, drvdata,
165 attr_grp);
166}
167EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
168
8cde11b2
JH
169/**
170 * tty_port_register_device_attr_serdev - register tty or serdev device
171 * @port: tty_port of the device
172 * @driver: tty_driver for this device
173 * @index: index of the tty
b286f4e8
TL
174 * @host: serial port hardware device
175 * @parent: parent if exists, otherwise NULL
8cde11b2
JH
176 * @drvdata: driver data for the device
177 * @attr_grp: attribute group for the device
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_attr_serdev(struct tty_port *port,
183 struct tty_driver *driver, unsigned index,
b286f4e8 184 struct device *host, struct device *parent, void *drvdata,
8cde11b2
JH
185 const struct attribute_group **attr_grp)
186{
187 struct device *dev;
188
189 tty_port_link_device(port, driver, index);
190
b286f4e8 191 dev = serdev_tty_port_register(port, host, parent, driver, index);
8cde11b2
JH
192 if (PTR_ERR(dev) != -ENODEV) {
193 /* Skip creating cdev if we registered a serdev device */
194 return dev;
195 }
196
b286f4e8 197 return tty_register_device_attr(driver, index, parent, drvdata,
8cde11b2
JH
198 attr_grp);
199}
200EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
201
8cde11b2
JH
202/**
203 * tty_port_unregister_device - deregister a tty or serdev device
204 * @port: tty_port of the device
205 * @driver: tty_driver for this device
206 * @index: index of the tty
207 *
208 * If a tty or serdev device is registered with a call to
209 * tty_port_register_device_serdev() then this function must be called when
210 * the device is gone.
211 */
212void tty_port_unregister_device(struct tty_port *port,
213 struct tty_driver *driver, unsigned index)
214{
215 int ret;
216
217 ret = serdev_tty_port_unregister(port);
218 if (ret == 0)
219 return;
220
221 tty_unregister_device(driver, index);
222}
223EXPORT_SYMBOL_GPL(tty_port_unregister_device);
224
9e48565d
AC
225int tty_port_alloc_xmit_buf(struct tty_port *port)
226{
227 /* We may sleep in get_zeroed_page() */
44e4909e 228 mutex_lock(&port->buf_mutex);
4e2a44c1 229 if (port->xmit_buf == NULL) {
49943393 230 port->xmit_buf = (u8 *)get_zeroed_page(GFP_KERNEL);
4e2a44c1
JS
231 if (port->xmit_buf)
232 kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
233 }
44e4909e 234 mutex_unlock(&port->buf_mutex);
9e48565d
AC
235 if (port->xmit_buf == NULL)
236 return -ENOMEM;
237 return 0;
238}
239EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
240
241void tty_port_free_xmit_buf(struct tty_port *port)
242{
44e4909e 243 mutex_lock(&port->buf_mutex);
3c5b2f5b
AS
244 free_page((unsigned long)port->xmit_buf);
245 port->xmit_buf = NULL;
246 INIT_KFIFO(port->xmit_fifo);
44e4909e 247 mutex_unlock(&port->buf_mutex);
9e48565d
AC
248}
249EXPORT_SYMBOL(tty_port_free_xmit_buf);
250
de274bfe 251/**
c38f45ef 252 * tty_port_destroy - destroy inited port
1926e5d3 253 * @port: tty port to be destroyed
de274bfe 254 *
cb6f6f98
JS
255 * When a port was initialized using tty_port_init(), one has to destroy the
256 * port by this function. Either indirectly by using &tty_port refcounting
257 * (tty_port_put()) or directly if refcounting is not used.
de274bfe
JS
258 */
259void tty_port_destroy(struct tty_port *port)
260{
e176058f 261 tty_buffer_cancel_work(port);
de274bfe
JS
262 tty_buffer_free_all(port);
263}
264EXPORT_SYMBOL(tty_port_destroy);
265
568aafc6
AC
266static void tty_port_destructor(struct kref *kref)
267{
268 struct tty_port *port = container_of(kref, struct tty_port, kref);
e3bfea23
PH
269
270 /* check if last port ref was dropped before tty release */
271 if (WARN_ON(port->itty))
272 return;
3c5b2f5b 273 free_page((unsigned long)port->xmit_buf);
de274bfe 274 tty_port_destroy(port);
81c79838 275 if (port->ops && port->ops->destruct)
568aafc6
AC
276 port->ops->destruct(port);
277 else
278 kfree(port);
279}
280
3be491d7 281/**
c38f45ef 282 * tty_port_put - drop a reference to tty_port
3be491d7
JS
283 * @port: port to drop a reference of (can be NULL)
284 *
285 * The final put will destroy and free up the @port using
286 * @port->ops->destruct() hook, or using kfree() if not provided.
287 */
568aafc6
AC
288void tty_port_put(struct tty_port *port)
289{
290 if (port)
291 kref_put(&port->kref, tty_port_destructor);
292}
293EXPORT_SYMBOL(tty_port_put);
9e48565d 294
4a90f09b 295/**
cb6f6f98
JS
296 * tty_port_tty_get - get a tty reference
297 * @port: tty port
4a90f09b 298 *
cb6f6f98
JS
299 * Return a refcount protected tty instance or %NULL if the port is not
300 * associated with a tty (eg due to close or hangup).
4a90f09b 301 */
4a90f09b
AC
302struct tty_struct *tty_port_tty_get(struct tty_port *port)
303{
304 unsigned long flags;
305 struct tty_struct *tty;
306
307 spin_lock_irqsave(&port->lock, flags);
308 tty = tty_kref_get(port->tty);
309 spin_unlock_irqrestore(&port->lock, flags);
310 return tty;
311}
312EXPORT_SYMBOL(tty_port_tty_get);
313
314/**
cb6f6f98
JS
315 * tty_port_tty_set - set the tty of a port
316 * @port: tty port
317 * @tty: the tty
4a90f09b 318 *
cb6f6f98
JS
319 * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
320 * to deassociate a port.
4a90f09b 321 */
4a90f09b
AC
322void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
323{
324 unsigned long flags;
325
326 spin_lock_irqsave(&port->lock, flags);
a211b1af 327 tty_kref_put(port->tty);
cb4bca35 328 port->tty = tty_kref_get(tty);
4a90f09b
AC
329 spin_unlock_irqrestore(&port->lock, flags);
330}
331EXPORT_SYMBOL(tty_port_tty_set);
31f35939 332
3be491d7
JS
333/**
334 * tty_port_shutdown - internal helper to shutdown the device
335 * @port: tty port to be shut down
336 * @tty: the associated tty
337 *
338 * It is used by tty_port_hangup() and tty_port_close(). Its task is to
339 * shutdown the device if it was initialized (note consoles remain
340 * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
341 * @port->ops->shutdown().
342 */
957dacae 343static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
7ca0ff9a 344{
64bc3979 345 mutex_lock(&port->mutex);
8bde9658
JH
346 if (port->console)
347 goto out;
348
d41861ca 349 if (tty_port_initialized(port)) {
515be7ba 350 tty_port_set_initialized(port, false);
957dacae
JH
351 /*
352 * Drop DTR/RTS if HUPCL is set. This causes any attached
353 * modem to hang up the line.
354 */
355 if (tty && C_HUPCL(tty))
356 tty_port_lower_dtr_rts(port);
357
0d3cb6f6 358 if (port->ops->shutdown)
7ca0ff9a 359 port->ops->shutdown(port);
8bde9658
JH
360 }
361out:
64bc3979 362 mutex_unlock(&port->mutex);
7ca0ff9a
AC
363}
364
3e61696b 365/**
cb6f6f98
JS
366 * tty_port_hangup - hangup helper
367 * @port: tty port
3e61696b 368 *
cb6f6f98
JS
369 * Perform port level tty hangup flag and count changes. Drop the tty
370 * reference.
9c9928bd 371 *
cb6f6f98 372 * Caller holds tty lock.
3e61696b 373 */
3e61696b
AC
374void tty_port_hangup(struct tty_port *port)
375{
957dacae 376 struct tty_struct *tty;
3e61696b
AC
377 unsigned long flags;
378
379 spin_lock_irqsave(&port->lock, flags);
380 port->count = 0;
957dacae
JH
381 tty = port->tty;
382 if (tty)
383 set_bit(TTY_IO_ERROR, &tty->flags);
3e61696b
AC
384 port->tty = NULL;
385 spin_unlock_irqrestore(&port->lock, flags);
9b5aa549 386 tty_port_set_active(port, false);
957dacae
JH
387 tty_port_shutdown(port, tty);
388 tty_kref_put(tty);
3e61696b 389 wake_up_interruptible(&port->open_wait);
bdc04e31 390 wake_up_interruptible(&port->delta_msr_wait);
3e61696b
AC
391}
392EXPORT_SYMBOL(tty_port_hangup);
393
aa27a094
JS
394/**
395 * tty_port_tty_hangup - helper to hang up a tty
aa27a094 396 * @port: tty port
cb6f6f98 397 * @check_clocal: hang only ttys with %CLOCAL unset?
aa27a094
JS
398 */
399void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
400{
401 struct tty_struct *tty = tty_port_tty_get(port);
402
1d9e689c 403 if (tty && (!check_clocal || !C_CLOCAL(tty)))
aa27a094 404 tty_hangup(tty);
1d9e689c 405 tty_kref_put(tty);
aa27a094
JS
406}
407EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
408
6aad04f2
JS
409/**
410 * tty_port_tty_wakeup - helper to wake up a tty
6aad04f2
JS
411 * @port: tty port
412 */
413void tty_port_tty_wakeup(struct tty_port *port)
414{
c3485ee0 415 port->client_ops->write_wakeup(port);
6aad04f2
JS
416}
417EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
418
31f35939 419/**
cb6f6f98
JS
420 * tty_port_carrier_raised - carrier raised check
421 * @port: tty port
31f35939 422 *
cb6f6f98
JS
423 * Wrapper for the carrier detect logic. For the moment this is used
424 * to hide some internal details. This will eventually become entirely
425 * internal to the tty port.
31f35939 426 */
b300fb26 427bool tty_port_carrier_raised(struct tty_port *port)
31f35939 428{
0d3cb6f6 429 if (port->ops->carrier_raised == NULL)
b300fb26 430 return true;
31f35939
AC
431 return port->ops->carrier_raised(port);
432}
433EXPORT_SYMBOL(tty_port_carrier_raised);
5d951fb4
AC
434
435/**
cb6f6f98
JS
436 * tty_port_raise_dtr_rts - Raise DTR/RTS
437 * @port: tty port
5d951fb4 438 *
cb6f6f98
JS
439 * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
440 * some internal details. This will eventually become entirely internal to the
441 * tty port.
5d951fb4 442 */
5d951fb4
AC
443void tty_port_raise_dtr_rts(struct tty_port *port)
444{
0d3cb6f6 445 if (port->ops->dtr_rts)
5d420399 446 port->ops->dtr_rts(port, true);
5d951fb4
AC
447}
448EXPORT_SYMBOL(tty_port_raise_dtr_rts);
36c621d8 449
fcc8ac18 450/**
cb6f6f98
JS
451 * tty_port_lower_dtr_rts - Lower DTR/RTS
452 * @port: tty port
fcc8ac18 453 *
cb6f6f98
JS
454 * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
455 * some internal details. This will eventually become entirely internal to the
456 * tty port.
fcc8ac18 457 */
fcc8ac18
AC
458void tty_port_lower_dtr_rts(struct tty_port *port)
459{
0d3cb6f6 460 if (port->ops->dtr_rts)
5d420399 461 port->ops->dtr_rts(port, false);
fcc8ac18
AC
462}
463EXPORT_SYMBOL(tty_port_lower_dtr_rts);
464
36c621d8 465/**
cb6f6f98
JS
466 * tty_port_block_til_ready - Waiting logic for tty open
467 * @port: the tty port being opened
468 * @tty: the tty device being bound
469 * @filp: the file pointer of the opener or %NULL
470 *
471 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
472 * Handles:
36c621d8 473 *
cb6f6f98
JS
474 * - hangup (both before and during)
475 * - non blocking open
476 * - rts/dtr/dcd
477 * - signals
478 * - port flags and counts
36c621d8 479 *
cb6f6f98
JS
480 * The passed @port must implement the @port->ops->carrier_raised method if it
481 * can do carrier detect and the @port->ops->dtr_rts method if it supports
482 * software management of these lines. Note that the dtr/rts raise is done each
483 * iteration as a hangup may have previously dropped them while we wait.
c590f6b6 484 *
cb6f6f98 485 * Caller holds tty lock.
c590f6b6 486 *
cb6f6f98
JS
487 * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
488 * have changed state (eg., may have been hung up).
36c621d8 489 */
36c621d8
AC
490int tty_port_block_til_ready(struct tty_port *port,
491 struct tty_struct *tty, struct file *filp)
492{
493 int do_clocal = 0, retval;
494 unsigned long flags;
6af9a43d 495 DEFINE_WAIT(wait);
36c621d8 496
36c621d8 497 /* if non-blocking mode is set we can pass directly to open unless
1df92640
XT
498 * the port has just hung up or is in another error state.
499 */
18900ca6 500 if (tty_io_error(tty)) {
9b5aa549 501 tty_port_set_active(port, true);
8627b96d
AC
502 return 0;
503 }
ed3f0af8 504 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
4175f3e3 505 /* Indicate we are open */
9db276f8 506 if (C_BAUD(tty))
4175f3e3 507 tty_port_raise_dtr_rts(port);
9b5aa549 508 tty_port_set_active(port, true);
36c621d8
AC
509 return 0;
510 }
511
512 if (C_CLOCAL(tty))
513 do_clocal = 1;
514
515 /* Block waiting until we can proceed. We may need to wait for the
1df92640
XT
516 * carrier, but we must also wait for any close that is in progress
517 * before the next open may complete.
518 */
36c621d8
AC
519
520 retval = 0;
36c621d8
AC
521
522 /* The port lock protects the port counts */
523 spin_lock_irqsave(&port->lock, flags);
e359a4e3 524 port->count--;
36c621d8
AC
525 port->blocked_open++;
526 spin_unlock_irqrestore(&port->lock, flags);
527
528 while (1) {
529 /* Indicate we are open */
d41861ca 530 if (C_BAUD(tty) && tty_port_initialized(port))
7834909f 531 tty_port_raise_dtr_rts(port);
36c621d8 532
3e3b5c08 533 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
d774a56d 534 /* Check for a hangup or uninitialised port.
1df92640
XT
535 * Return accordingly.
536 */
d41861ca 537 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
36c621d8
AC
538 if (port->flags & ASYNC_HUP_NOTIFY)
539 retval = -EAGAIN;
540 else
541 retval = -ERESTARTSYS;
542 break;
543 }
0eee50af
JS
544 /*
545 * Probe the carrier. For devices with no carrier detect
546 * tty_port_carrier_raised will always return true.
547 * Never ask drivers if CLOCAL is set, this causes troubles
548 * on some hardware.
549 */
fef062cb 550 if (do_clocal || tty_port_carrier_raised(port))
36c621d8
AC
551 break;
552 if (signal_pending(current)) {
553 retval = -ERESTARTSYS;
554 break;
555 }
89c8d91e 556 tty_unlock(tty);
36c621d8 557 schedule();
89c8d91e 558 tty_lock(tty);
36c621d8 559 }
3e3b5c08 560 finish_wait(&port->open_wait, &wait);
36c621d8
AC
561
562 /* Update counts. A parallel hangup will have set count to zero and
1df92640
XT
563 * we must not mess that up further.
564 */
36c621d8
AC
565 spin_lock_irqsave(&port->lock, flags);
566 if (!tty_hung_up_p(filp))
567 port->count++;
568 port->blocked_open--;
36c621d8 569 spin_unlock_irqrestore(&port->lock, flags);
807c8d81 570 if (retval == 0)
9b5aa549 571 tty_port_set_active(port, true);
ecc2e05e 572 return retval;
36c621d8
AC
573}
574EXPORT_SYMBOL(tty_port_block_til_ready);
575
b74414f5
JH
576static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
577{
578 unsigned int bps = tty_get_baud_rate(tty);
579 long timeout;
580
581 if (bps > 1200) {
582 timeout = (HZ * 10 * port->drain_delay) / bps;
583 timeout = max_t(long, timeout, HZ / 10);
584 } else {
585 timeout = 2 * HZ;
586 }
587 schedule_timeout_interruptible(timeout);
588}
589
3be491d7
JS
590/**
591 * tty_port_close_start - helper for tty->ops->close, part 1/2
592 * @port: tty_port of the device
593 * @tty: tty being closed
594 * @filp: passed file pointer
595 *
596 * Decrements and checks open count. Flushes the port if this is the last
597 * close. That means, dropping the data from the outpu buffer on the device and
598 * waiting for sending logic to finish. The rest of close handling is performed
599 * in tty_port_close_end().
600 *
601 * Locking: Caller holds tty lock.
602 *
603 * Return: 1 if this is the last close, otherwise 0
604 */
d774a56d
AC
605int tty_port_close_start(struct tty_port *port,
606 struct tty_struct *tty, struct file *filp)
a6614999
AC
607{
608 unsigned long flags;
609
633caba8 610 if (tty_hung_up_p(filp))
a6614999 611 return 0;
a6614999 612
633caba8 613 spin_lock_irqsave(&port->lock, flags);
d774a56d 614 if (tty->count == 1 && port->count != 1) {
339f36ba
PH
615 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
616 port->count);
a6614999
AC
617 port->count = 1;
618 }
619 if (--port->count < 0) {
339f36ba
PH
620 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
621 port->count);
a6614999
AC
622 port->count = 0;
623 }
624
625 if (port->count) {
626 spin_unlock_irqrestore(&port->lock, flags);
627 return 0;
628 }
a6614999 629 spin_unlock_irqrestore(&port->lock, flags);
0b2588ca 630
ddc7b758
PH
631 tty->closing = 1;
632
d41861ca 633 if (tty_port_initialized(port)) {
0b2588ca 634 /* Don't block on a stalled port, just pull the chain */
6e94dbc7 635 if (tty->flow.tco_stopped)
0b2588ca
JH
636 tty_driver_flush_buffer(tty);
637 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
79c1faa4 638 tty_wait_until_sent(tty, port->closing_wait);
0b2588ca
JH
639 if (port->drain_delay)
640 tty_port_drain_delay(port, tty);
641 }
e707c35c
AC
642 /* Flush the ldisc buffering */
643 tty_ldisc_flush(tty);
644
469d6d06 645 /* Report to caller this is the last port reference */
a6614999
AC
646 return 1;
647}
648EXPORT_SYMBOL(tty_port_close_start);
649
3be491d7
JS
650/**
651 * tty_port_close_end - helper for tty->ops->close, part 2/2
652 * @port: tty_port of the device
653 * @tty: tty being closed
654 *
655 * This is a continuation of the first part: tty_port_close_start(). This
656 * should be called after turning off the device. It flushes the data from the
657 * line discipline and delays the close by @port->close_delay.
658 *
659 * Locking: Caller holds tty lock.
660 */
a6614999
AC
661void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
662{
663 unsigned long flags;
664
3f40f5b2 665 tty_ldisc_flush(tty);
a6614999
AC
666 tty->closing = 0;
667
ddc7b758
PH
668 spin_lock_irqsave(&port->lock, flags);
669
a6614999
AC
670 if (port->blocked_open) {
671 spin_unlock_irqrestore(&port->lock, flags);
5823323e
PH
672 if (port->close_delay)
673 msleep_interruptible(jiffies_to_msecs(port->close_delay));
a6614999
AC
674 spin_lock_irqsave(&port->lock, flags);
675 wake_up_interruptible(&port->open_wait);
676 }
a6614999 677 spin_unlock_irqrestore(&port->lock, flags);
9b5aa549 678 tty_port_set_active(port, false);
a6614999
AC
679}
680EXPORT_SYMBOL(tty_port_close_end);
7ca0ff9a 681
cb6f6f98
JS
682/**
683 * tty_port_close - generic tty->ops->close handler
684 * @port: tty_port of the device
685 * @tty: tty being closed
686 * @filp: passed file pointer
687 *
688 * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
689 * sequence of tty_port_close_start(), tty_port_shutdown(), and
690 * tty_port_close_end(). The latter two are called only if this is the last
691 * close. See the respective functions for the details.
0733db91 692 *
cb6f6f98 693 * Locking: Caller holds tty lock
0733db91 694 */
7ca0ff9a
AC
695void tty_port_close(struct tty_port *port, struct tty_struct *tty,
696 struct file *filp)
697{
698 if (tty_port_close_start(port, tty, filp) == 0)
699 return;
957dacae 700 tty_port_shutdown(port, tty);
2a486026
CP
701 if (!port->console)
702 set_bit(TTY_IO_ERROR, &tty->flags);
7ca0ff9a
AC
703 tty_port_close_end(port, tty);
704 tty_port_tty_set(port, NULL);
705}
706EXPORT_SYMBOL(tty_port_close);
64bc3979 707
72a33bf5
JS
708/**
709 * tty_port_install - generic tty->ops->install handler
710 * @port: tty_port of the device
711 * @driver: tty_driver for this device
712 * @tty: tty to be installed
713 *
cb6f6f98
JS
714 * It is the same as tty_standard_install() except the provided @port is linked
715 * to a concrete tty specified by @tty. Use this or tty_port_register_device()
716 * (or both). Call tty_port_link_device() as a last resort.
72a33bf5 717 */
695586ca
JS
718int tty_port_install(struct tty_port *port, struct tty_driver *driver,
719 struct tty_struct *tty)
720{
721 tty->port = port;
722 return tty_standard_install(driver, tty);
723}
724EXPORT_SYMBOL_GPL(tty_port_install);
725
cb6f6f98
JS
726/**
727 * tty_port_open - generic tty->ops->open handler
728 * @port: tty_port of the device
729 * @tty: tty to be opened
730 * @filp: passed file pointer
addd4672 731 *
cb6f6f98
JS
732 * It is a generic helper to be used in driver's @tty->ops->open. It activates
733 * the devices using @port->ops->activate if not active already. And waits for
734 * the device to be ready using tty_port_block_til_ready() (e.g. raises
735 * DTR/CTS and waits for carrier).
736 *
d56738a3
JS
737 * Note that @port->ops->shutdown is not called when @port->ops->activate
738 * returns an error (on the contrary, @tty->ops->close is).
739 *
cb6f6f98 740 * Locking: Caller holds tty lock.
addd4672 741 *
cb6f6f98
JS
742 * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
743 * @tty and @port may have changed state (eg., may be hung up now).
addd4672 744 */
64bc3979 745int tty_port_open(struct tty_port *port, struct tty_struct *tty,
d774a56d 746 struct file *filp)
64bc3979
AC
747{
748 spin_lock_irq(&port->lock);
e359a4e3 749 ++port->count;
64bc3979
AC
750 spin_unlock_irq(&port->lock);
751 tty_port_tty_set(port, tty);
752
753 /*
754 * Do the device-specific open only if the hardware isn't
755 * already initialized. Serialize open and shutdown using the
756 * port mutex.
757 */
758
759 mutex_lock(&port->mutex);
760
d41861ca 761 if (!tty_port_initialized(port)) {
a9a37ec3 762 clear_bit(TTY_IO_ERROR, &tty->flags);
0d3cb6f6 763 if (port->ops->activate) {
64bc3979 764 int retval = port->ops->activate(port, tty);
54ad59a2 765
64bc3979 766 if (retval) {
d774a56d
AC
767 mutex_unlock(&port->mutex);
768 return retval;
769 }
770 }
515be7ba 771 tty_port_set_initialized(port, true);
64bc3979
AC
772 }
773 mutex_unlock(&port->mutex);
774 return tty_port_block_til_ready(port, tty, filp);
775}
64bc3979 776EXPORT_SYMBOL(tty_port_open);