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