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