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