xref: /linux/drivers/tty/tty_port.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
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 /**
344  * tty_port_shutdown - internal helper to shutdown the device
345  * @port: tty port to be shut down
346  * @tty: the associated tty
347  *
348  * It is used by tty_port_hangup() and tty_port_close(). Its task is to
349  * shutdown the device if it was initialized (note consoles remain
350  * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
351  * @port->ops->shutdown().
352  */
353 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
354 {
355 	guard(mutex)(&port->mutex);
356 
357 	if (port->console)
358 		return;
359 
360 	if (!tty_port_initialized(port))
361 		return;
362 
363 	tty_port_set_initialized(port, false);
364 	/*
365 	 * Drop DTR/RTS if HUPCL is set. This causes any attached
366 	 * modem to hang up the line.
367 	 */
368 	if (tty && C_HUPCL(tty))
369 		tty_port_lower_dtr_rts(port);
370 
371 	if (port->ops->shutdown)
372 		port->ops->shutdown(port);
373 }
374 
375 /**
376  * tty_port_hangup		-	hangup helper
377  * @port: tty port
378  *
379  * Perform port level tty hangup flag and count changes. Drop the tty
380  * reference.
381  *
382  * Caller holds tty lock.
383  */
384 void tty_port_hangup(struct tty_port *port)
385 {
386 	struct tty_struct *tty;
387 
388 	scoped_guard(spinlock_irqsave, &port->lock) {
389 		port->count = 0;
390 		tty = port->tty;
391 		if (tty)
392 			set_bit(TTY_IO_ERROR, &tty->flags);
393 		port->tty = NULL;
394 	}
395 
396 	tty_port_set_active(port, false);
397 	tty_port_shutdown(port, tty);
398 	tty_kref_put(tty);
399 	wake_up_interruptible(&port->open_wait);
400 	wake_up_interruptible(&port->delta_msr_wait);
401 }
402 EXPORT_SYMBOL(tty_port_hangup);
403 
404 void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async)
405 {
406 	scoped_guard(tty_port_tty, port) {
407 		struct tty_struct *tty = scoped_tty();
408 
409 		if (!check_clocal || !C_CLOCAL(tty)) {
410 			if (async)
411 				tty_hangup(tty);
412 			else
413 				tty_vhangup(tty);
414 		}
415 	}
416 }
417 EXPORT_SYMBOL_GPL(__tty_port_tty_hangup);
418 
419 /**
420  * tty_port_tty_wakeup - helper to wake up a tty
421  * @port: tty port
422  */
423 void tty_port_tty_wakeup(struct tty_port *port)
424 {
425 	port->client_ops->write_wakeup(port);
426 }
427 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
428 
429 /**
430  * tty_port_carrier_raised	-	carrier raised check
431  * @port: tty port
432  *
433  * Wrapper for the carrier detect logic. For the moment this is used
434  * to hide some internal details. This will eventually become entirely
435  * internal to the tty port.
436  */
437 bool tty_port_carrier_raised(struct tty_port *port)
438 {
439 	if (port->ops->carrier_raised == NULL)
440 		return true;
441 	return port->ops->carrier_raised(port);
442 }
443 EXPORT_SYMBOL(tty_port_carrier_raised);
444 
445 /**
446  * tty_port_raise_dtr_rts	-	Raise DTR/RTS
447  * @port: tty port
448  *
449  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
450  * some internal details. This will eventually become entirely internal to the
451  * tty port.
452  */
453 void tty_port_raise_dtr_rts(struct tty_port *port)
454 {
455 	if (port->ops->dtr_rts)
456 		port->ops->dtr_rts(port, true);
457 }
458 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
459 
460 /**
461  * tty_port_lower_dtr_rts	-	Lower DTR/RTS
462  * @port: tty port
463  *
464  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
465  * some internal details. This will eventually become entirely internal to the
466  * tty port.
467  */
468 void tty_port_lower_dtr_rts(struct tty_port *port)
469 {
470 	if (port->ops->dtr_rts)
471 		port->ops->dtr_rts(port, false);
472 }
473 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
474 
475 /**
476  * tty_port_block_til_ready	-	Waiting logic for tty open
477  * @port: the tty port being opened
478  * @tty: the tty device being bound
479  * @filp: the file pointer of the opener or %NULL
480  *
481  * Implement the core POSIX/SuS tty behaviour when opening a tty device.
482  * Handles:
483  *
484  *	- hangup (both before and during)
485  *	- non blocking open
486  *	- rts/dtr/dcd
487  *	- signals
488  *	- port flags and counts
489  *
490  * The passed @port must implement the @port->ops->carrier_raised method if it
491  * can do carrier detect and the @port->ops->dtr_rts method if it supports
492  * software management of these lines. Note that the dtr/rts raise is done each
493  * iteration as a hangup may have previously dropped them while we wait.
494  *
495  * Caller holds tty lock.
496  *
497  * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
498  * have changed state (eg., may have been hung up).
499  */
500 int tty_port_block_til_ready(struct tty_port *port,
501 				struct tty_struct *tty, struct file *filp)
502 {
503 	int do_clocal = 0, retval;
504 	DEFINE_WAIT(wait);
505 
506 	/* if non-blocking mode is set we can pass directly to open unless
507 	 * the port has just hung up or is in another error state.
508 	 */
509 	if (tty_io_error(tty)) {
510 		tty_port_set_active(port, true);
511 		return 0;
512 	}
513 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
514 		/* Indicate we are open */
515 		if (C_BAUD(tty))
516 			tty_port_raise_dtr_rts(port);
517 		tty_port_set_active(port, true);
518 		return 0;
519 	}
520 
521 	if (C_CLOCAL(tty))
522 		do_clocal = 1;
523 
524 	/* Block waiting until we can proceed. We may need to wait for the
525 	 * carrier, but we must also wait for any close that is in progress
526 	 * before the next open may complete.
527 	 */
528 
529 	retval = 0;
530 
531 	/* The port lock protects the port counts */
532 	scoped_guard(spinlock_irqsave, &port->lock) {
533 		port->count--;
534 		port->blocked_open++;
535 	}
536 
537 	while (1) {
538 		/* Indicate we are open */
539 		if (C_BAUD(tty) && tty_port_initialized(port))
540 			tty_port_raise_dtr_rts(port);
541 
542 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
543 		/* Check for a hangup or uninitialised port.
544 		 * Return accordingly.
545 		 */
546 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
547 			if (port->flags & ASYNC_HUP_NOTIFY)
548 				retval = -EAGAIN;
549 			else
550 				retval = -ERESTARTSYS;
551 			break;
552 		}
553 		/*
554 		 * Probe the carrier. For devices with no carrier detect
555 		 * tty_port_carrier_raised will always return true.
556 		 * Never ask drivers if CLOCAL is set, this causes troubles
557 		 * on some hardware.
558 		 */
559 		if (do_clocal || tty_port_carrier_raised(port))
560 			break;
561 		if (signal_pending(current)) {
562 			retval = -ERESTARTSYS;
563 			break;
564 		}
565 		tty_unlock(tty);
566 		schedule();
567 		tty_lock(tty);
568 	}
569 	finish_wait(&port->open_wait, &wait);
570 
571 	/* Update counts. A parallel hangup will have set count to zero and
572 	 * we must not mess that up further.
573 	 */
574 	scoped_guard(spinlock_irqsave, &port->lock) {
575 		if (!tty_hung_up_p(filp))
576 			port->count++;
577 		port->blocked_open--;
578 	}
579 	if (retval == 0)
580 		tty_port_set_active(port, true);
581 	return retval;
582 }
583 EXPORT_SYMBOL(tty_port_block_til_ready);
584 
585 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
586 {
587 	unsigned int bps = tty_get_baud_rate(tty);
588 	long timeout;
589 
590 	if (bps > 1200) {
591 		timeout = (HZ * 10 * port->drain_delay) / bps;
592 		timeout = max_t(long, timeout, HZ / 10);
593 	} else {
594 		timeout = 2 * HZ;
595 	}
596 	schedule_timeout_interruptible(timeout);
597 }
598 
599 /**
600  * tty_port_close_start - helper for tty->ops->close, part 1/2
601  * @port: tty_port of the device
602  * @tty: tty being closed
603  * @filp: passed file pointer
604  *
605  * Decrements and checks open count. Flushes the port if this is the last
606  * close. That means, dropping the data from the outpu buffer on the device and
607  * waiting for sending logic to finish. The rest of close handling is performed
608  * in tty_port_close_end().
609  *
610  * Locking: Caller holds tty lock.
611  *
612  * Return: 1 if this is the last close, otherwise 0
613  */
614 int tty_port_close_start(struct tty_port *port,
615 				struct tty_struct *tty, struct file *filp)
616 {
617 	if (tty_hung_up_p(filp))
618 		return 0;
619 
620 	scoped_guard(spinlock_irqsave, &port->lock) {
621 		if (tty->count == 1 && port->count != 1) {
622 			tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
623 				 port->count);
624 			port->count = 1;
625 		}
626 		if (--port->count < 0) {
627 			tty_warn(tty, "%s: bad port count (%d)\n", __func__,
628 				 port->count);
629 			port->count = 0;
630 		}
631 
632 		if (port->count)
633 			return 0;
634 	}
635 
636 	tty->closing = 1;
637 
638 	if (tty_port_initialized(port)) {
639 		/* Don't block on a stalled port, just pull the chain */
640 		if (tty->flow.tco_stopped)
641 			tty_driver_flush_buffer(tty);
642 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
643 			tty_wait_until_sent(tty, port->closing_wait);
644 		if (port->drain_delay)
645 			tty_port_drain_delay(port, tty);
646 	}
647 	/* Flush the ldisc buffering */
648 	tty_ldisc_flush(tty);
649 
650 	/* Report to caller this is the last port reference */
651 	return 1;
652 }
653 EXPORT_SYMBOL(tty_port_close_start);
654 
655 /**
656  * tty_port_close_end - helper for tty->ops->close, part 2/2
657  * @port: tty_port of the device
658  * @tty: tty being closed
659  *
660  * This is a continuation of the first part: tty_port_close_start(). This
661  * should be called after turning off the device. It flushes the data from the
662  * line discipline and delays the close by @port->close_delay.
663  *
664  * Locking: Caller holds tty lock.
665  */
666 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
667 {
668 	unsigned long flags;
669 
670 	tty_ldisc_flush(tty);
671 	tty->closing = 0;
672 
673 	spin_lock_irqsave(&port->lock, flags);
674 
675 	if (port->blocked_open) {
676 		spin_unlock_irqrestore(&port->lock, flags);
677 		if (port->close_delay)
678 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
679 		spin_lock_irqsave(&port->lock, flags);
680 		wake_up_interruptible(&port->open_wait);
681 	}
682 	spin_unlock_irqrestore(&port->lock, flags);
683 	tty_port_set_active(port, false);
684 }
685 EXPORT_SYMBOL(tty_port_close_end);
686 
687 /**
688  * tty_port_close - generic tty->ops->close handler
689  * @port: tty_port of the device
690  * @tty: tty being closed
691  * @filp: passed file pointer
692  *
693  * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
694  * sequence of tty_port_close_start(), tty_port_shutdown(), and
695  * tty_port_close_end(). The latter two are called only if this is the last
696  * close. See the respective functions for the details.
697  *
698  * Locking: Caller holds tty lock
699  */
700 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
701 							struct file *filp)
702 {
703 	if (tty_port_close_start(port, tty, filp) == 0)
704 		return;
705 	tty_port_shutdown(port, tty);
706 	if (!port->console)
707 		set_bit(TTY_IO_ERROR, &tty->flags);
708 	tty_port_close_end(port, tty);
709 	tty_port_tty_set(port, NULL);
710 }
711 EXPORT_SYMBOL(tty_port_close);
712 
713 /**
714  * tty_port_install - generic tty->ops->install handler
715  * @port: tty_port of the device
716  * @driver: tty_driver for this device
717  * @tty: tty to be installed
718  *
719  * It is the same as tty_standard_install() except the provided @port is linked
720  * to a concrete tty specified by @tty. Use this or tty_port_register_device()
721  * (or both). Call tty_port_link_device() as a last resort.
722  */
723 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
724 		struct tty_struct *tty)
725 {
726 	tty->port = port;
727 	tty_port_link_driver_wq(port, driver);
728 	return tty_standard_install(driver, tty);
729 }
730 EXPORT_SYMBOL_GPL(tty_port_install);
731 
732 /**
733  * tty_port_open - generic tty->ops->open handler
734  * @port: tty_port of the device
735  * @tty: tty to be opened
736  * @filp: passed file pointer
737  *
738  * It is a generic helper to be used in driver's @tty->ops->open. It activates
739  * the devices using @port->ops->activate if not active already. And waits for
740  * the device to be ready using tty_port_block_til_ready() (e.g.  raises
741  * DTR/CTS and waits for carrier).
742  *
743  * Note that @port->ops->shutdown is not called when @port->ops->activate
744  * returns an error (on the contrary, @tty->ops->close is).
745  *
746  * Locking: Caller holds tty lock.
747  *
748  * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
749  * @tty and @port may have changed state (eg., may be hung up now).
750  */
751 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
752 							struct file *filp)
753 {
754 	scoped_guard(spinlock_irq, &port->lock)
755 		++port->count;
756 	tty_port_tty_set(port, tty);
757 
758 	/*
759 	 * Do the device-specific open only if the hardware isn't
760 	 * already initialized. Serialize open and shutdown using the
761 	 * port mutex.
762 	 */
763 
764 	scoped_guard(mutex, &port->mutex) {
765 		if (tty_port_initialized(port))
766 			break;
767 		clear_bit(TTY_IO_ERROR, &tty->flags);
768 		if (port->ops->activate) {
769 			int retval = port->ops->activate(port, tty);
770 			if (retval)
771 				return retval;
772 		}
773 		tty_port_set_initialized(port, true);
774 	}
775 	return tty_port_block_til_ready(port, tty, filp);
776 }
777 EXPORT_SYMBOL(tty_port_open);
778