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