xref: /linux/drivers/tty/tty_port.c (revision d574c5dc8cfe1fd1ddda6edb435f3b3f39155c52)
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 	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 
74 const struct tty_port_client_operations tty_port_default_client_ops = {
75 	.receive_buf = tty_port_default_receive_buf,
76 	.lookahead_buf = tty_port_default_lookahead_buf,
77 	.write_wakeup = tty_port_default_wakeup,
78 };
79 EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
80 
81 /**
82  * tty_port_init - initialize tty_port
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  */
90 void tty_port_init(struct tty_port *port)
91 {
92 	memset(port, 0, sizeof(*port));
93 	tty_buffer_init(port);
94 	init_waitqueue_head(&port->open_wait);
95 	init_waitqueue_head(&port->delta_msr_wait);
96 	mutex_init(&port->mutex);
97 	mutex_init(&port->buf_mutex);
98 	spin_lock_init(&port->lock);
99 	port->close_delay = (50 * HZ) / 100;
100 	port->closing_wait = (3000 * HZ) / 100;
101 	port->client_ops = &tty_port_default_client_ops;
102 	kref_init(&port->kref);
103 }
104 EXPORT_SYMBOL(tty_port_init);
105 
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  *
112  * Provide the tty layer with a link from a tty (specified by @index) to a
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().
116  */
117 void 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;
122 	driver->ports[index] = port;
123 }
124 EXPORT_SYMBOL_GPL(tty_port_link_device);
125 
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  *
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.
136  */
137 struct device *tty_port_register_device(struct tty_port *port,
138 		struct tty_driver *driver, unsigned index,
139 		struct device *device)
140 {
141 	return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
142 }
143 EXPORT_SYMBOL_GPL(tty_port_register_device);
144 
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  *
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.
157  */
158 struct 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 }
167 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
168 
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
174  * @host: serial port hardware device
175  * @parent: parent if exists, otherwise NULL
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  */
182 struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
183 		struct tty_driver *driver, unsigned index,
184 		struct device *host, struct device *parent, void *drvdata,
185 		const struct attribute_group **attr_grp)
186 {
187 	struct device *dev;
188 
189 	tty_port_link_device(port, driver, index);
190 
191 	dev = serdev_tty_port_register(port, host, parent, driver, index);
192 	if (PTR_ERR(dev) != -ENODEV) {
193 		/* Skip creating cdev if we registered a serdev device */
194 		return dev;
195 	}
196 
197 	return tty_register_device_attr(driver, index, parent, drvdata,
198 			attr_grp);
199 }
200 EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
201 
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  */
212 void 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 }
223 EXPORT_SYMBOL_GPL(tty_port_unregister_device);
224 
225 int tty_port_alloc_xmit_buf(struct tty_port *port)
226 {
227 	/* We may sleep in get_zeroed_page() */
228 	mutex_lock(&port->buf_mutex);
229 	if (port->xmit_buf == NULL) {
230 		port->xmit_buf = (u8 *)get_zeroed_page(GFP_KERNEL);
231 		if (port->xmit_buf)
232 			kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
233 	}
234 	mutex_unlock(&port->buf_mutex);
235 	if (port->xmit_buf == NULL)
236 		return -ENOMEM;
237 	return 0;
238 }
239 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
240 
241 void tty_port_free_xmit_buf(struct tty_port *port)
242 {
243 	mutex_lock(&port->buf_mutex);
244 	free_page((unsigned long)port->xmit_buf);
245 	port->xmit_buf = NULL;
246 	INIT_KFIFO(port->xmit_fifo);
247 	mutex_unlock(&port->buf_mutex);
248 }
249 EXPORT_SYMBOL(tty_port_free_xmit_buf);
250 
251 /**
252  * tty_port_destroy - destroy inited port
253  * @port: tty port to be destroyed
254  *
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.
258  */
259 void tty_port_destroy(struct tty_port *port)
260 {
261 	tty_buffer_cancel_work(port);
262 	tty_buffer_free_all(port);
263 }
264 EXPORT_SYMBOL(tty_port_destroy);
265 
266 static void tty_port_destructor(struct kref *kref)
267 {
268 	struct tty_port *port = container_of(kref, struct tty_port, kref);
269 
270 	/* check if last port ref was dropped before tty release */
271 	if (WARN_ON(port->itty))
272 		return;
273 	free_page((unsigned long)port->xmit_buf);
274 	tty_port_destroy(port);
275 	if (port->ops && port->ops->destruct)
276 		port->ops->destruct(port);
277 	else
278 		kfree(port);
279 }
280 
281 /**
282  * tty_port_put - drop a reference to tty_port
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  */
288 void tty_port_put(struct tty_port *port)
289 {
290 	if (port)
291 		kref_put(&port->kref, tty_port_destructor);
292 }
293 EXPORT_SYMBOL(tty_port_put);
294 
295 /**
296  * tty_port_tty_get	-	get a tty reference
297  * @port: tty port
298  *
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).
301  */
302 struct 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 }
312 EXPORT_SYMBOL(tty_port_tty_get);
313 
314 /**
315  * tty_port_tty_set	-	set the tty of a port
316  * @port: tty port
317  * @tty: the tty
318  *
319  * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
320  * to deassociate a port.
321  */
322 void 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);
327 	tty_kref_put(port->tty);
328 	port->tty = tty_kref_get(tty);
329 	spin_unlock_irqrestore(&port->lock, flags);
330 }
331 EXPORT_SYMBOL(tty_port_tty_set);
332 
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  */
343 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
344 {
345 	mutex_lock(&port->mutex);
346 	if (port->console)
347 		goto out;
348 
349 	if (tty_port_initialized(port)) {
350 		tty_port_set_initialized(port, false);
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 
358 		if (port->ops->shutdown)
359 			port->ops->shutdown(port);
360 	}
361 out:
362 	mutex_unlock(&port->mutex);
363 }
364 
365 /**
366  * tty_port_hangup		-	hangup helper
367  * @port: tty port
368  *
369  * Perform port level tty hangup flag and count changes. Drop the tty
370  * reference.
371  *
372  * Caller holds tty lock.
373  */
374 void tty_port_hangup(struct tty_port *port)
375 {
376 	struct tty_struct *tty;
377 	unsigned long flags;
378 
379 	spin_lock_irqsave(&port->lock, flags);
380 	port->count = 0;
381 	tty = port->tty;
382 	if (tty)
383 		set_bit(TTY_IO_ERROR, &tty->flags);
384 	port->tty = NULL;
385 	spin_unlock_irqrestore(&port->lock, flags);
386 	tty_port_set_active(port, false);
387 	tty_port_shutdown(port, tty);
388 	tty_kref_put(tty);
389 	wake_up_interruptible(&port->open_wait);
390 	wake_up_interruptible(&port->delta_msr_wait);
391 }
392 EXPORT_SYMBOL(tty_port_hangup);
393 
394 /**
395  * tty_port_tty_hangup - helper to hang up a tty
396  * @port: tty port
397  * @check_clocal: hang only ttys with %CLOCAL unset?
398  */
399 void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async)
400 {
401 	struct tty_struct *tty = tty_port_tty_get(port);
402 
403 	if (tty && (!check_clocal || !C_CLOCAL(tty))) {
404 		if (async)
405 			tty_hangup(tty);
406 		else
407 			tty_vhangup(tty);
408 	}
409 	tty_kref_put(tty);
410 }
411 EXPORT_SYMBOL_GPL(__tty_port_tty_hangup);
412 
413 /**
414  * tty_port_tty_wakeup - helper to wake up a tty
415  * @port: tty port
416  */
417 void tty_port_tty_wakeup(struct tty_port *port)
418 {
419 	port->client_ops->write_wakeup(port);
420 }
421 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
422 
423 /**
424  * tty_port_carrier_raised	-	carrier raised check
425  * @port: tty port
426  *
427  * Wrapper for the carrier detect logic. For the moment this is used
428  * to hide some internal details. This will eventually become entirely
429  * internal to the tty port.
430  */
431 bool tty_port_carrier_raised(struct tty_port *port)
432 {
433 	if (port->ops->carrier_raised == NULL)
434 		return true;
435 	return port->ops->carrier_raised(port);
436 }
437 EXPORT_SYMBOL(tty_port_carrier_raised);
438 
439 /**
440  * tty_port_raise_dtr_rts	-	Raise 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_raise_dtr_rts(struct tty_port *port)
448 {
449 	if (port->ops->dtr_rts)
450 		port->ops->dtr_rts(port, true);
451 }
452 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
453 
454 /**
455  * tty_port_lower_dtr_rts	-	Lower DTR/RTS
456  * @port: tty port
457  *
458  * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
459  * some internal details. This will eventually become entirely internal to the
460  * tty port.
461  */
462 void tty_port_lower_dtr_rts(struct tty_port *port)
463 {
464 	if (port->ops->dtr_rts)
465 		port->ops->dtr_rts(port, false);
466 }
467 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
468 
469 /**
470  * tty_port_block_til_ready	-	Waiting logic for tty open
471  * @port: the tty port being opened
472  * @tty: the tty device being bound
473  * @filp: the file pointer of the opener or %NULL
474  *
475  * Implement the core POSIX/SuS tty behaviour when opening a tty device.
476  * Handles:
477  *
478  *	- hangup (both before and during)
479  *	- non blocking open
480  *	- rts/dtr/dcd
481  *	- signals
482  *	- port flags and counts
483  *
484  * The passed @port must implement the @port->ops->carrier_raised method if it
485  * can do carrier detect and the @port->ops->dtr_rts method if it supports
486  * software management of these lines. Note that the dtr/rts raise is done each
487  * iteration as a hangup may have previously dropped them while we wait.
488  *
489  * Caller holds tty lock.
490  *
491  * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
492  * have changed state (eg., may have been hung up).
493  */
494 int tty_port_block_til_ready(struct tty_port *port,
495 				struct tty_struct *tty, struct file *filp)
496 {
497 	int do_clocal = 0, retval;
498 	unsigned long flags;
499 	DEFINE_WAIT(wait);
500 
501 	/* if non-blocking mode is set we can pass directly to open unless
502 	 * the port has just hung up or is in another error state.
503 	 */
504 	if (tty_io_error(tty)) {
505 		tty_port_set_active(port, true);
506 		return 0;
507 	}
508 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
509 		/* Indicate we are open */
510 		if (C_BAUD(tty))
511 			tty_port_raise_dtr_rts(port);
512 		tty_port_set_active(port, true);
513 		return 0;
514 	}
515 
516 	if (C_CLOCAL(tty))
517 		do_clocal = 1;
518 
519 	/* Block waiting until we can proceed. We may need to wait for the
520 	 * carrier, but we must also wait for any close that is in progress
521 	 * before the next open may complete.
522 	 */
523 
524 	retval = 0;
525 
526 	/* The port lock protects the port counts */
527 	spin_lock_irqsave(&port->lock, flags);
528 	port->count--;
529 	port->blocked_open++;
530 	spin_unlock_irqrestore(&port->lock, flags);
531 
532 	while (1) {
533 		/* Indicate we are open */
534 		if (C_BAUD(tty) && tty_port_initialized(port))
535 			tty_port_raise_dtr_rts(port);
536 
537 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
538 		/* Check for a hangup or uninitialised port.
539 		 * Return accordingly.
540 		 */
541 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
542 			if (port->flags & ASYNC_HUP_NOTIFY)
543 				retval = -EAGAIN;
544 			else
545 				retval = -ERESTARTSYS;
546 			break;
547 		}
548 		/*
549 		 * Probe the carrier. For devices with no carrier detect
550 		 * tty_port_carrier_raised will always return true.
551 		 * Never ask drivers if CLOCAL is set, this causes troubles
552 		 * on some hardware.
553 		 */
554 		if (do_clocal || tty_port_carrier_raised(port))
555 			break;
556 		if (signal_pending(current)) {
557 			retval = -ERESTARTSYS;
558 			break;
559 		}
560 		tty_unlock(tty);
561 		schedule();
562 		tty_lock(tty);
563 	}
564 	finish_wait(&port->open_wait, &wait);
565 
566 	/* Update counts. A parallel hangup will have set count to zero and
567 	 * we must not mess that up further.
568 	 */
569 	spin_lock_irqsave(&port->lock, flags);
570 	if (!tty_hung_up_p(filp))
571 		port->count++;
572 	port->blocked_open--;
573 	spin_unlock_irqrestore(&port->lock, flags);
574 	if (retval == 0)
575 		tty_port_set_active(port, true);
576 	return retval;
577 }
578 EXPORT_SYMBOL(tty_port_block_til_ready);
579 
580 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
581 {
582 	unsigned int bps = tty_get_baud_rate(tty);
583 	long timeout;
584 
585 	if (bps > 1200) {
586 		timeout = (HZ * 10 * port->drain_delay) / bps;
587 		timeout = max_t(long, timeout, HZ / 10);
588 	} else {
589 		timeout = 2 * HZ;
590 	}
591 	schedule_timeout_interruptible(timeout);
592 }
593 
594 /**
595  * tty_port_close_start - helper for tty->ops->close, part 1/2
596  * @port: tty_port of the device
597  * @tty: tty being closed
598  * @filp: passed file pointer
599  *
600  * Decrements and checks open count. Flushes the port if this is the last
601  * close. That means, dropping the data from the outpu buffer on the device and
602  * waiting for sending logic to finish. The rest of close handling is performed
603  * in tty_port_close_end().
604  *
605  * Locking: Caller holds tty lock.
606  *
607  * Return: 1 if this is the last close, otherwise 0
608  */
609 int tty_port_close_start(struct tty_port *port,
610 				struct tty_struct *tty, struct file *filp)
611 {
612 	unsigned long flags;
613 
614 	if (tty_hung_up_p(filp))
615 		return 0;
616 
617 	spin_lock_irqsave(&port->lock, flags);
618 	if (tty->count == 1 && port->count != 1) {
619 		tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
620 			 port->count);
621 		port->count = 1;
622 	}
623 	if (--port->count < 0) {
624 		tty_warn(tty, "%s: bad port count (%d)\n", __func__,
625 			 port->count);
626 		port->count = 0;
627 	}
628 
629 	if (port->count) {
630 		spin_unlock_irqrestore(&port->lock, flags);
631 		return 0;
632 	}
633 	spin_unlock_irqrestore(&port->lock, flags);
634 
635 	tty->closing = 1;
636 
637 	if (tty_port_initialized(port)) {
638 		/* Don't block on a stalled port, just pull the chain */
639 		if (tty->flow.tco_stopped)
640 			tty_driver_flush_buffer(tty);
641 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
642 			tty_wait_until_sent(tty, port->closing_wait);
643 		if (port->drain_delay)
644 			tty_port_drain_delay(port, tty);
645 	}
646 	/* Flush the ldisc buffering */
647 	tty_ldisc_flush(tty);
648 
649 	/* Report to caller this is the last port reference */
650 	return 1;
651 }
652 EXPORT_SYMBOL(tty_port_close_start);
653 
654 /**
655  * tty_port_close_end - helper for tty->ops->close, part 2/2
656  * @port: tty_port of the device
657  * @tty: tty being closed
658  *
659  * This is a continuation of the first part: tty_port_close_start(). This
660  * should be called after turning off the device. It flushes the data from the
661  * line discipline and delays the close by @port->close_delay.
662  *
663  * Locking: Caller holds tty lock.
664  */
665 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
666 {
667 	unsigned long flags;
668 
669 	tty_ldisc_flush(tty);
670 	tty->closing = 0;
671 
672 	spin_lock_irqsave(&port->lock, flags);
673 
674 	if (port->blocked_open) {
675 		spin_unlock_irqrestore(&port->lock, flags);
676 		if (port->close_delay)
677 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
678 		spin_lock_irqsave(&port->lock, flags);
679 		wake_up_interruptible(&port->open_wait);
680 	}
681 	spin_unlock_irqrestore(&port->lock, flags);
682 	tty_port_set_active(port, false);
683 }
684 EXPORT_SYMBOL(tty_port_close_end);
685 
686 /**
687  * tty_port_close - generic tty->ops->close handler
688  * @port: tty_port of the device
689  * @tty: tty being closed
690  * @filp: passed file pointer
691  *
692  * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
693  * sequence of tty_port_close_start(), tty_port_shutdown(), and
694  * tty_port_close_end(). The latter two are called only if this is the last
695  * close. See the respective functions for the details.
696  *
697  * Locking: Caller holds tty lock
698  */
699 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
700 							struct file *filp)
701 {
702 	if (tty_port_close_start(port, tty, filp) == 0)
703 		return;
704 	tty_port_shutdown(port, tty);
705 	if (!port->console)
706 		set_bit(TTY_IO_ERROR, &tty->flags);
707 	tty_port_close_end(port, tty);
708 	tty_port_tty_set(port, NULL);
709 }
710 EXPORT_SYMBOL(tty_port_close);
711 
712 /**
713  * tty_port_install - generic tty->ops->install handler
714  * @port: tty_port of the device
715  * @driver: tty_driver for this device
716  * @tty: tty to be installed
717  *
718  * It is the same as tty_standard_install() except the provided @port is linked
719  * to a concrete tty specified by @tty. Use this or tty_port_register_device()
720  * (or both). Call tty_port_link_device() as a last resort.
721  */
722 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
723 		struct tty_struct *tty)
724 {
725 	tty->port = port;
726 	return tty_standard_install(driver, tty);
727 }
728 EXPORT_SYMBOL_GPL(tty_port_install);
729 
730 /**
731  * tty_port_open - generic tty->ops->open handler
732  * @port: tty_port of the device
733  * @tty: tty to be opened
734  * @filp: passed file pointer
735  *
736  * It is a generic helper to be used in driver's @tty->ops->open. It activates
737  * the devices using @port->ops->activate if not active already. And waits for
738  * the device to be ready using tty_port_block_til_ready() (e.g.  raises
739  * DTR/CTS and waits for carrier).
740  *
741  * Note that @port->ops->shutdown is not called when @port->ops->activate
742  * returns an error (on the contrary, @tty->ops->close is).
743  *
744  * Locking: Caller holds tty lock.
745  *
746  * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
747  * @tty and @port may have changed state (eg., may be hung up now).
748  */
749 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
750 							struct file *filp)
751 {
752 	spin_lock_irq(&port->lock);
753 	++port->count;
754 	spin_unlock_irq(&port->lock);
755 	tty_port_tty_set(port, tty);
756 
757 	/*
758 	 * Do the device-specific open only if the hardware isn't
759 	 * already initialized. Serialize open and shutdown using the
760 	 * port mutex.
761 	 */
762 
763 	mutex_lock(&port->mutex);
764 
765 	if (!tty_port_initialized(port)) {
766 		clear_bit(TTY_IO_ERROR, &tty->flags);
767 		if (port->ops->activate) {
768 			int retval = port->ops->activate(port, tty);
769 
770 			if (retval) {
771 				mutex_unlock(&port->mutex);
772 				return retval;
773 			}
774 		}
775 		tty_port_set_initialized(port, true);
776 	}
777 	mutex_unlock(&port->mutex);
778 	return tty_port_block_til_ready(port, tty, filp);
779 }
780 EXPORT_SYMBOL(tty_port_open);
781