xref: /linux/drivers/tty/tty_port.c (revision 905e46acd3272d04566fec49afbd7ad9e2ed9ae3)
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 	struct device *dev;
133 
134 	tty_port_link_device(port, driver, index);
135 
136 	dev = serdev_tty_port_register(port, device, driver, index);
137 	if (PTR_ERR(dev) != -ENODEV)
138 		/* Skip creating cdev if we registered a serdev device */
139 		return dev;
140 
141 	return tty_register_device_attr(driver, index, device, drvdata,
142 			attr_grp);
143 }
144 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
145 
146 int tty_port_alloc_xmit_buf(struct tty_port *port)
147 {
148 	/* We may sleep in get_zeroed_page() */
149 	mutex_lock(&port->buf_mutex);
150 	if (port->xmit_buf == NULL)
151 		port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
152 	mutex_unlock(&port->buf_mutex);
153 	if (port->xmit_buf == NULL)
154 		return -ENOMEM;
155 	return 0;
156 }
157 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
158 
159 void tty_port_free_xmit_buf(struct tty_port *port)
160 {
161 	mutex_lock(&port->buf_mutex);
162 	if (port->xmit_buf != NULL) {
163 		free_page((unsigned long)port->xmit_buf);
164 		port->xmit_buf = NULL;
165 	}
166 	mutex_unlock(&port->buf_mutex);
167 }
168 EXPORT_SYMBOL(tty_port_free_xmit_buf);
169 
170 /**
171  * tty_port_destroy -- destroy inited port
172  * @port: tty port to be doestroyed
173  *
174  * When a port was initialized using tty_port_init, one has to destroy the
175  * port by this function. Either indirectly by using tty_port refcounting
176  * (tty_port_put) or directly if refcounting is not used.
177  */
178 void tty_port_destroy(struct tty_port *port)
179 {
180 	tty_buffer_cancel_work(port);
181 	tty_buffer_free_all(port);
182 }
183 EXPORT_SYMBOL(tty_port_destroy);
184 
185 static void tty_port_destructor(struct kref *kref)
186 {
187 	struct tty_port *port = container_of(kref, struct tty_port, kref);
188 
189 	/* check if last port ref was dropped before tty release */
190 	if (WARN_ON(port->itty))
191 		return;
192 
193 	serdev_tty_port_unregister(port);
194 
195 	if (port->xmit_buf)
196 		free_page((unsigned long)port->xmit_buf);
197 	tty_port_destroy(port);
198 	if (port->ops && port->ops->destruct)
199 		port->ops->destruct(port);
200 	else
201 		kfree(port);
202 }
203 
204 void tty_port_put(struct tty_port *port)
205 {
206 	if (port)
207 		kref_put(&port->kref, tty_port_destructor);
208 }
209 EXPORT_SYMBOL(tty_port_put);
210 
211 /**
212  *	tty_port_tty_get	-	get a tty reference
213  *	@port: tty port
214  *
215  *	Return a refcount protected tty instance or NULL if the port is not
216  *	associated with a tty (eg due to close or hangup)
217  */
218 
219 struct tty_struct *tty_port_tty_get(struct tty_port *port)
220 {
221 	unsigned long flags;
222 	struct tty_struct *tty;
223 
224 	spin_lock_irqsave(&port->lock, flags);
225 	tty = tty_kref_get(port->tty);
226 	spin_unlock_irqrestore(&port->lock, flags);
227 	return tty;
228 }
229 EXPORT_SYMBOL(tty_port_tty_get);
230 
231 /**
232  *	tty_port_tty_set	-	set the tty of a port
233  *	@port: tty port
234  *	@tty: the tty
235  *
236  *	Associate the port and tty pair. Manages any internal refcounts.
237  *	Pass NULL to deassociate a port
238  */
239 
240 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
241 {
242 	unsigned long flags;
243 
244 	spin_lock_irqsave(&port->lock, flags);
245 	tty_kref_put(port->tty);
246 	port->tty = tty_kref_get(tty);
247 	spin_unlock_irqrestore(&port->lock, flags);
248 }
249 EXPORT_SYMBOL(tty_port_tty_set);
250 
251 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
252 {
253 	mutex_lock(&port->mutex);
254 	if (port->console)
255 		goto out;
256 
257 	if (tty_port_initialized(port)) {
258 		tty_port_set_initialized(port, 0);
259 		/*
260 		 * Drop DTR/RTS if HUPCL is set. This causes any attached
261 		 * modem to hang up the line.
262 		 */
263 		if (tty && C_HUPCL(tty))
264 			tty_port_lower_dtr_rts(port);
265 
266 		if (port->ops->shutdown)
267 			port->ops->shutdown(port);
268 	}
269 out:
270 	mutex_unlock(&port->mutex);
271 }
272 
273 /**
274  *	tty_port_hangup		-	hangup helper
275  *	@port: tty port
276  *
277  *	Perform port level tty hangup flag and count changes. Drop the tty
278  *	reference.
279  *
280  *	Caller holds tty lock.
281  */
282 
283 void tty_port_hangup(struct tty_port *port)
284 {
285 	struct tty_struct *tty;
286 	unsigned long flags;
287 
288 	spin_lock_irqsave(&port->lock, flags);
289 	port->count = 0;
290 	tty = port->tty;
291 	if (tty)
292 		set_bit(TTY_IO_ERROR, &tty->flags);
293 	port->tty = NULL;
294 	spin_unlock_irqrestore(&port->lock, flags);
295 	tty_port_set_active(port, 0);
296 	tty_port_shutdown(port, tty);
297 	tty_kref_put(tty);
298 	wake_up_interruptible(&port->open_wait);
299 	wake_up_interruptible(&port->delta_msr_wait);
300 }
301 EXPORT_SYMBOL(tty_port_hangup);
302 
303 /**
304  * tty_port_tty_hangup - helper to hang up a tty
305  *
306  * @port: tty port
307  * @check_clocal: hang only ttys with CLOCAL unset?
308  */
309 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
310 {
311 	struct tty_struct *tty = tty_port_tty_get(port);
312 
313 	if (tty && (!check_clocal || !C_CLOCAL(tty)))
314 		tty_hangup(tty);
315 	tty_kref_put(tty);
316 }
317 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
318 
319 /**
320  * tty_port_tty_wakeup - helper to wake up a tty
321  *
322  * @port: tty port
323  */
324 void tty_port_tty_wakeup(struct tty_port *port)
325 {
326 	port->client_ops->write_wakeup(port);
327 }
328 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
329 
330 /**
331  *	tty_port_carrier_raised	-	carrier raised check
332  *	@port: tty port
333  *
334  *	Wrapper for the carrier detect logic. For the moment this is used
335  *	to hide some internal details. This will eventually become entirely
336  *	internal to the tty port.
337  */
338 
339 int tty_port_carrier_raised(struct tty_port *port)
340 {
341 	if (port->ops->carrier_raised == NULL)
342 		return 1;
343 	return port->ops->carrier_raised(port);
344 }
345 EXPORT_SYMBOL(tty_port_carrier_raised);
346 
347 /**
348  *	tty_port_raise_dtr_rts	-	Raise DTR/RTS
349  *	@port: tty port
350  *
351  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
352  *	to hide some internal details. This will eventually become entirely
353  *	internal to the tty port.
354  */
355 
356 void tty_port_raise_dtr_rts(struct tty_port *port)
357 {
358 	if (port->ops->dtr_rts)
359 		port->ops->dtr_rts(port, 1);
360 }
361 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
362 
363 /**
364  *	tty_port_lower_dtr_rts	-	Lower DTR/RTS
365  *	@port: tty port
366  *
367  *	Wrapper for the DTR/RTS raise logic. For the moment this is used
368  *	to hide some internal details. This will eventually become entirely
369  *	internal to the tty port.
370  */
371 
372 void tty_port_lower_dtr_rts(struct tty_port *port)
373 {
374 	if (port->ops->dtr_rts)
375 		port->ops->dtr_rts(port, 0);
376 }
377 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
378 
379 /**
380  *	tty_port_block_til_ready	-	Waiting logic for tty open
381  *	@port: the tty port being opened
382  *	@tty: the tty device being bound
383  *	@filp: the file pointer of the opener or NULL
384  *
385  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
386  *	Handles:
387  *		- hangup (both before and during)
388  *		- non blocking open
389  *		- rts/dtr/dcd
390  *		- signals
391  *		- port flags and counts
392  *
393  *	The passed tty_port must implement the carrier_raised method if it can
394  *	do carrier detect and the dtr_rts method if it supports software
395  *	management of these lines. Note that the dtr/rts raise is done each
396  *	iteration as a hangup may have previously dropped them while we wait.
397  *
398  *	Caller holds tty lock.
399  *
400  *      NB: May drop and reacquire tty lock when blocking, so tty and tty_port
401  *      may have changed state (eg., may have been hung up).
402  */
403 
404 int tty_port_block_til_ready(struct tty_port *port,
405 				struct tty_struct *tty, struct file *filp)
406 {
407 	int do_clocal = 0, retval;
408 	unsigned long flags;
409 	DEFINE_WAIT(wait);
410 
411 	/* if non-blocking mode is set we can pass directly to open unless
412 	   the port has just hung up or is in another error state */
413 	if (tty_io_error(tty)) {
414 		tty_port_set_active(port, 1);
415 		return 0;
416 	}
417 	if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
418 		/* Indicate we are open */
419 		if (C_BAUD(tty))
420 			tty_port_raise_dtr_rts(port);
421 		tty_port_set_active(port, 1);
422 		return 0;
423 	}
424 
425 	if (C_CLOCAL(tty))
426 		do_clocal = 1;
427 
428 	/* Block waiting until we can proceed. We may need to wait for the
429 	   carrier, but we must also wait for any close that is in progress
430 	   before the next open may complete */
431 
432 	retval = 0;
433 
434 	/* The port lock protects the port counts */
435 	spin_lock_irqsave(&port->lock, flags);
436 	port->count--;
437 	port->blocked_open++;
438 	spin_unlock_irqrestore(&port->lock, flags);
439 
440 	while (1) {
441 		/* Indicate we are open */
442 		if (C_BAUD(tty) && tty_port_initialized(port))
443 			tty_port_raise_dtr_rts(port);
444 
445 		prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
446 		/* Check for a hangup or uninitialised port.
447 							Return accordingly */
448 		if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
449 			if (port->flags & ASYNC_HUP_NOTIFY)
450 				retval = -EAGAIN;
451 			else
452 				retval = -ERESTARTSYS;
453 			break;
454 		}
455 		/*
456 		 * Probe the carrier. For devices with no carrier detect
457 		 * tty_port_carrier_raised will always return true.
458 		 * Never ask drivers if CLOCAL is set, this causes troubles
459 		 * on some hardware.
460 		 */
461 		if (do_clocal || tty_port_carrier_raised(port))
462 			break;
463 		if (signal_pending(current)) {
464 			retval = -ERESTARTSYS;
465 			break;
466 		}
467 		tty_unlock(tty);
468 		schedule();
469 		tty_lock(tty);
470 	}
471 	finish_wait(&port->open_wait, &wait);
472 
473 	/* Update counts. A parallel hangup will have set count to zero and
474 	   we must not mess that up further */
475 	spin_lock_irqsave(&port->lock, flags);
476 	if (!tty_hung_up_p(filp))
477 		port->count++;
478 	port->blocked_open--;
479 	spin_unlock_irqrestore(&port->lock, flags);
480 	if (retval == 0)
481 		tty_port_set_active(port, 1);
482 	return retval;
483 }
484 EXPORT_SYMBOL(tty_port_block_til_ready);
485 
486 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
487 {
488 	unsigned int bps = tty_get_baud_rate(tty);
489 	long timeout;
490 
491 	if (bps > 1200) {
492 		timeout = (HZ * 10 * port->drain_delay) / bps;
493 		timeout = max_t(long, timeout, HZ / 10);
494 	} else {
495 		timeout = 2 * HZ;
496 	}
497 	schedule_timeout_interruptible(timeout);
498 }
499 
500 /* Caller holds tty lock. */
501 int tty_port_close_start(struct tty_port *port,
502 				struct tty_struct *tty, struct file *filp)
503 {
504 	unsigned long flags;
505 
506 	if (tty_hung_up_p(filp))
507 		return 0;
508 
509 	spin_lock_irqsave(&port->lock, flags);
510 	if (tty->count == 1 && port->count != 1) {
511 		tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
512 			 port->count);
513 		port->count = 1;
514 	}
515 	if (--port->count < 0) {
516 		tty_warn(tty, "%s: bad port count (%d)\n", __func__,
517 			 port->count);
518 		port->count = 0;
519 	}
520 
521 	if (port->count) {
522 		spin_unlock_irqrestore(&port->lock, flags);
523 		return 0;
524 	}
525 	spin_unlock_irqrestore(&port->lock, flags);
526 
527 	tty->closing = 1;
528 
529 	if (tty_port_initialized(port)) {
530 		/* Don't block on a stalled port, just pull the chain */
531 		if (tty->flow_stopped)
532 			tty_driver_flush_buffer(tty);
533 		if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
534 			tty_wait_until_sent(tty, port->closing_wait);
535 		if (port->drain_delay)
536 			tty_port_drain_delay(port, tty);
537 	}
538 	/* Flush the ldisc buffering */
539 	tty_ldisc_flush(tty);
540 
541 	/* Report to caller this is the last port reference */
542 	return 1;
543 }
544 EXPORT_SYMBOL(tty_port_close_start);
545 
546 /* Caller holds tty lock */
547 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
548 {
549 	unsigned long flags;
550 
551 	tty_ldisc_flush(tty);
552 	tty->closing = 0;
553 
554 	spin_lock_irqsave(&port->lock, flags);
555 
556 	if (port->blocked_open) {
557 		spin_unlock_irqrestore(&port->lock, flags);
558 		if (port->close_delay)
559 			msleep_interruptible(jiffies_to_msecs(port->close_delay));
560 		spin_lock_irqsave(&port->lock, flags);
561 		wake_up_interruptible(&port->open_wait);
562 	}
563 	spin_unlock_irqrestore(&port->lock, flags);
564 	tty_port_set_active(port, 0);
565 }
566 EXPORT_SYMBOL(tty_port_close_end);
567 
568 /**
569  * tty_port_close
570  *
571  * Caller holds tty lock
572  */
573 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
574 							struct file *filp)
575 {
576 	if (tty_port_close_start(port, tty, filp) == 0)
577 		return;
578 	tty_port_shutdown(port, tty);
579 	set_bit(TTY_IO_ERROR, &tty->flags);
580 	tty_port_close_end(port, tty);
581 	tty_port_tty_set(port, NULL);
582 }
583 EXPORT_SYMBOL(tty_port_close);
584 
585 /**
586  * tty_port_install - generic tty->ops->install handler
587  * @port: tty_port of the device
588  * @driver: tty_driver for this device
589  * @tty: tty to be installed
590  *
591  * It is the same as tty_standard_install except the provided @port is linked
592  * to a concrete tty specified by @tty. Use this or tty_port_register_device
593  * (or both). Call tty_port_link_device as a last resort.
594  */
595 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
596 		struct tty_struct *tty)
597 {
598 	tty->port = port;
599 	return tty_standard_install(driver, tty);
600 }
601 EXPORT_SYMBOL_GPL(tty_port_install);
602 
603 /**
604  * tty_port_open
605  *
606  * Caller holds tty lock.
607  *
608  * NB: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
609  * tty and tty_port may have changed state (eg., may be hung up now)
610  */
611 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
612 							struct file *filp)
613 {
614 	spin_lock_irq(&port->lock);
615 	++port->count;
616 	spin_unlock_irq(&port->lock);
617 	tty_port_tty_set(port, tty);
618 
619 	/*
620 	 * Do the device-specific open only if the hardware isn't
621 	 * already initialized. Serialize open and shutdown using the
622 	 * port mutex.
623 	 */
624 
625 	mutex_lock(&port->mutex);
626 
627 	if (!tty_port_initialized(port)) {
628 		clear_bit(TTY_IO_ERROR, &tty->flags);
629 		if (port->ops->activate) {
630 			int retval = port->ops->activate(port, tty);
631 			if (retval) {
632 				mutex_unlock(&port->mutex);
633 				return retval;
634 			}
635 		}
636 		tty_port_set_initialized(port, 1);
637 	}
638 	mutex_unlock(&port->mutex);
639 	return tty_port_block_til_ready(port, tty, filp);
640 }
641 
642 EXPORT_SYMBOL(tty_port_open);
643