1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/types.h>
7 #include <linux/errno.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>
23 static int tty_port_default_receive_buf(struct tty_port *port,
24 const unsigned char *p,
25 const unsigned char *f, size_t count)
28 struct tty_struct *tty;
29 struct tty_ldisc *disc;
31 tty = READ_ONCE(port->itty);
35 disc = tty_ldisc_ref(tty);
39 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
41 tty_ldisc_deref(disc);
46 static void tty_port_default_wakeup(struct tty_port *port)
48 struct tty_struct *tty = tty_port_tty_get(port);
56 const struct tty_port_client_operations tty_port_default_client_ops = {
57 .receive_buf = tty_port_default_receive_buf,
58 .write_wakeup = tty_port_default_wakeup,
60 EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
63 * tty_port_init -- initialize tty_port
64 * @port: tty_port to initialize
66 * Initializes the state of struct tty_port. When a port was initialized using
67 * this function, one has to destroy the port by tty_port_destroy(). Either
68 * indirectly by using &tty_port refcounting (tty_port_put()) or directly if
69 * refcounting is not used.
71 void tty_port_init(struct tty_port *port)
73 memset(port, 0, sizeof(*port));
74 tty_buffer_init(port);
75 init_waitqueue_head(&port->open_wait);
76 init_waitqueue_head(&port->delta_msr_wait);
77 mutex_init(&port->mutex);
78 mutex_init(&port->buf_mutex);
79 spin_lock_init(&port->lock);
80 port->close_delay = (50 * HZ) / 100;
81 port->closing_wait = (3000 * HZ) / 100;
82 port->client_ops = &tty_port_default_client_ops;
83 kref_init(&port->kref);
85 EXPORT_SYMBOL(tty_port_init);
88 * tty_port_link_device - link tty and tty_port
89 * @port: tty_port of the device
90 * @driver: tty_driver for this device
91 * @index: index of the tty
93 * Provide the tty layer with a link from a tty (specified by @index) to a
94 * tty_port (@port). Use this only if neither tty_port_register_device() nor
95 * tty_port_install() is used in the driver. If used, this has to be called
96 * before tty_register_driver().
98 void tty_port_link_device(struct tty_port *port,
99 struct tty_driver *driver, unsigned index)
101 if (WARN_ON(index >= driver->num))
103 driver->ports[index] = port;
105 EXPORT_SYMBOL_GPL(tty_port_link_device);
108 * tty_port_register_device - register tty device
109 * @port: tty_port of the device
110 * @driver: tty_driver for this device
111 * @index: index of the tty
112 * @device: parent if exists, otherwise NULL
114 * It is the same as tty_register_device() except the provided @port is linked
115 * to a concrete tty specified by @index. Use this or tty_port_install() (or
116 * both). Call tty_port_link_device() as a last resort.
118 struct device *tty_port_register_device(struct tty_port *port,
119 struct tty_driver *driver, unsigned index,
120 struct device *device)
122 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
124 EXPORT_SYMBOL_GPL(tty_port_register_device);
127 * tty_port_register_device_attr - 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 * @drvdata: Driver data to be set to device.
133 * @attr_grp: Attribute group to be set on device.
135 * It is the same as tty_register_device_attr() except the provided @port is
136 * linked to a concrete tty specified by @index. Use this or tty_port_install()
137 * (or both). Call tty_port_link_device() as a last resort.
139 struct device *tty_port_register_device_attr(struct tty_port *port,
140 struct tty_driver *driver, unsigned index,
141 struct device *device, void *drvdata,
142 const struct attribute_group **attr_grp)
144 tty_port_link_device(port, driver, index);
145 return tty_register_device_attr(driver, index, device, drvdata,
148 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
151 * tty_port_register_device_attr_serdev - register tty or serdev device
152 * @port: tty_port of the device
153 * @driver: tty_driver for this device
154 * @index: index of the tty
155 * @device: parent if exists, otherwise NULL
156 * @drvdata: driver data for the device
157 * @attr_grp: attribute group for the device
159 * Register a serdev or tty device depending on if the parent device has any
160 * defined serdev clients or not.
162 struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
163 struct tty_driver *driver, unsigned index,
164 struct device *device, void *drvdata,
165 const struct attribute_group **attr_grp)
169 tty_port_link_device(port, driver, index);
171 dev = serdev_tty_port_register(port, device, driver, index);
172 if (PTR_ERR(dev) != -ENODEV) {
173 /* Skip creating cdev if we registered a serdev device */
177 return tty_register_device_attr(driver, index, device, drvdata,
180 EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
183 * tty_port_register_device_serdev - register tty or serdev device
184 * @port: tty_port of the device
185 * @driver: tty_driver for this device
186 * @index: index of the tty
187 * @device: parent if exists, otherwise NULL
189 * Register a serdev or tty device depending on if the parent device has any
190 * defined serdev clients or not.
192 struct device *tty_port_register_device_serdev(struct tty_port *port,
193 struct tty_driver *driver, unsigned index,
194 struct device *device)
196 return tty_port_register_device_attr_serdev(port, driver, index,
199 EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
202 * tty_port_unregister_device - deregister a tty or serdev device
203 * @port: tty_port of the device
204 * @driver: tty_driver for this device
205 * @index: index of the tty
207 * If a tty or serdev device is registered with a call to
208 * tty_port_register_device_serdev() then this function must be called when
209 * the device is gone.
211 void tty_port_unregister_device(struct tty_port *port,
212 struct tty_driver *driver, unsigned index)
216 ret = serdev_tty_port_unregister(port);
220 tty_unregister_device(driver, index);
222 EXPORT_SYMBOL_GPL(tty_port_unregister_device);
224 int tty_port_alloc_xmit_buf(struct tty_port *port)
226 /* We may sleep in get_zeroed_page() */
227 mutex_lock(&port->buf_mutex);
228 if (port->xmit_buf == NULL)
229 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
230 mutex_unlock(&port->buf_mutex);
231 if (port->xmit_buf == NULL)
235 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
237 void tty_port_free_xmit_buf(struct tty_port *port)
239 mutex_lock(&port->buf_mutex);
240 if (port->xmit_buf != NULL) {
241 free_page((unsigned long)port->xmit_buf);
242 port->xmit_buf = NULL;
244 mutex_unlock(&port->buf_mutex);
246 EXPORT_SYMBOL(tty_port_free_xmit_buf);
249 * tty_port_destroy -- destroy inited port
250 * @port: tty port to be destroyed
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.
256 void tty_port_destroy(struct tty_port *port)
258 tty_buffer_cancel_work(port);
259 tty_buffer_free_all(port);
261 EXPORT_SYMBOL(tty_port_destroy);
263 static void tty_port_destructor(struct kref *kref)
265 struct tty_port *port = container_of(kref, struct tty_port, kref);
267 /* check if last port ref was dropped before tty release */
268 if (WARN_ON(port->itty))
271 free_page((unsigned long)port->xmit_buf);
272 tty_port_destroy(port);
273 if (port->ops && port->ops->destruct)
274 port->ops->destruct(port);
280 * tty_port_put -- drop a reference to tty_port
281 * @port: port to drop a reference of (can be NULL)
283 * The final put will destroy and free up the @port using
284 * @port->ops->destruct() hook, or using kfree() if not provided.
286 void tty_port_put(struct tty_port *port)
289 kref_put(&port->kref, tty_port_destructor);
291 EXPORT_SYMBOL(tty_port_put);
294 * tty_port_tty_get - get a tty reference
297 * Return a refcount protected tty instance or %NULL if the port is not
298 * associated with a tty (eg due to close or hangup).
300 struct tty_struct *tty_port_tty_get(struct tty_port *port)
303 struct tty_struct *tty;
305 spin_lock_irqsave(&port->lock, flags);
306 tty = tty_kref_get(port->tty);
307 spin_unlock_irqrestore(&port->lock, flags);
310 EXPORT_SYMBOL(tty_port_tty_get);
313 * tty_port_tty_set - set the tty of a port
317 * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL
318 * to deassociate a port.
320 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
324 spin_lock_irqsave(&port->lock, flags);
325 tty_kref_put(port->tty);
326 port->tty = tty_kref_get(tty);
327 spin_unlock_irqrestore(&port->lock, flags);
329 EXPORT_SYMBOL(tty_port_tty_set);
332 * tty_port_shutdown - internal helper to shutdown the device
333 * @port: tty port to be shut down
334 * @tty: the associated tty
336 * It is used by tty_port_hangup() and tty_port_close(). Its task is to
337 * shutdown the device if it was initialized (note consoles remain
338 * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes
339 * @port->ops->shutdown().
341 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
343 mutex_lock(&port->mutex);
347 if (tty_port_initialized(port)) {
348 tty_port_set_initialized(port, 0);
350 * Drop DTR/RTS if HUPCL is set. This causes any attached
351 * modem to hang up the line.
353 if (tty && C_HUPCL(tty))
354 tty_port_lower_dtr_rts(port);
356 if (port->ops->shutdown)
357 port->ops->shutdown(port);
360 mutex_unlock(&port->mutex);
364 * tty_port_hangup - hangup helper
367 * Perform port level tty hangup flag and count changes. Drop the tty
370 * Caller holds tty lock.
372 void tty_port_hangup(struct tty_port *port)
374 struct tty_struct *tty;
377 spin_lock_irqsave(&port->lock, flags);
381 set_bit(TTY_IO_ERROR, &tty->flags);
383 spin_unlock_irqrestore(&port->lock, flags);
384 tty_port_set_active(port, 0);
385 tty_port_shutdown(port, tty);
387 wake_up_interruptible(&port->open_wait);
388 wake_up_interruptible(&port->delta_msr_wait);
390 EXPORT_SYMBOL(tty_port_hangup);
393 * tty_port_tty_hangup - helper to hang up a tty
395 * @check_clocal: hang only ttys with %CLOCAL unset?
397 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
399 struct tty_struct *tty = tty_port_tty_get(port);
401 if (tty && (!check_clocal || !C_CLOCAL(tty)))
405 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
408 * tty_port_tty_wakeup - helper to wake up a tty
411 void tty_port_tty_wakeup(struct tty_port *port)
413 port->client_ops->write_wakeup(port);
415 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
418 * tty_port_carrier_raised - carrier raised check
421 * Wrapper for the carrier detect logic. For the moment this is used
422 * to hide some internal details. This will eventually become entirely
423 * internal to the tty port.
425 int tty_port_carrier_raised(struct tty_port *port)
427 if (port->ops->carrier_raised == NULL)
429 return port->ops->carrier_raised(port);
431 EXPORT_SYMBOL(tty_port_carrier_raised);
434 * tty_port_raise_dtr_rts - Raise DTR/RTS
437 * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
438 * some internal details. This will eventually become entirely internal to the
441 void tty_port_raise_dtr_rts(struct tty_port *port)
443 if (port->ops->dtr_rts)
444 port->ops->dtr_rts(port, 1);
446 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
449 * tty_port_lower_dtr_rts - Lower DTR/RTS
452 * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide
453 * some internal details. This will eventually become entirely internal to the
456 void tty_port_lower_dtr_rts(struct tty_port *port)
458 if (port->ops->dtr_rts)
459 port->ops->dtr_rts(port, 0);
461 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
464 * tty_port_block_til_ready - Waiting logic for tty open
465 * @port: the tty port being opened
466 * @tty: the tty device being bound
467 * @filp: the file pointer of the opener or %NULL
469 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
472 * - hangup (both before and during)
473 * - non blocking open
476 * - port flags and counts
478 * The passed @port must implement the @port->ops->carrier_raised method if it
479 * can do carrier detect and the @port->ops->dtr_rts method if it supports
480 * software management of these lines. Note that the dtr/rts raise is done each
481 * iteration as a hangup may have previously dropped them while we wait.
483 * Caller holds tty lock.
485 * Note: May drop and reacquire tty lock when blocking, so @tty and @port may
486 * have changed state (eg., may have been hung up).
488 int tty_port_block_til_ready(struct tty_port *port,
489 struct tty_struct *tty, struct file *filp)
491 int do_clocal = 0, retval;
495 /* if non-blocking mode is set we can pass directly to open unless
496 * the port has just hung up or is in another error state.
498 if (tty_io_error(tty)) {
499 tty_port_set_active(port, 1);
502 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
503 /* Indicate we are open */
505 tty_port_raise_dtr_rts(port);
506 tty_port_set_active(port, 1);
513 /* Block waiting until we can proceed. We may need to wait for the
514 * carrier, but we must also wait for any close that is in progress
515 * before the next open may complete.
520 /* The port lock protects the port counts */
521 spin_lock_irqsave(&port->lock, flags);
523 port->blocked_open++;
524 spin_unlock_irqrestore(&port->lock, flags);
527 /* Indicate we are open */
528 if (C_BAUD(tty) && tty_port_initialized(port))
529 tty_port_raise_dtr_rts(port);
531 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
532 /* Check for a hangup or uninitialised port.
533 * Return accordingly.
535 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
536 if (port->flags & ASYNC_HUP_NOTIFY)
539 retval = -ERESTARTSYS;
543 * Probe the carrier. For devices with no carrier detect
544 * tty_port_carrier_raised will always return true.
545 * Never ask drivers if CLOCAL is set, this causes troubles
548 if (do_clocal || tty_port_carrier_raised(port))
550 if (signal_pending(current)) {
551 retval = -ERESTARTSYS;
558 finish_wait(&port->open_wait, &wait);
560 /* Update counts. A parallel hangup will have set count to zero and
561 * we must not mess that up further.
563 spin_lock_irqsave(&port->lock, flags);
564 if (!tty_hung_up_p(filp))
566 port->blocked_open--;
567 spin_unlock_irqrestore(&port->lock, flags);
569 tty_port_set_active(port, 1);
572 EXPORT_SYMBOL(tty_port_block_til_ready);
574 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
576 unsigned int bps = tty_get_baud_rate(tty);
580 timeout = (HZ * 10 * port->drain_delay) / bps;
581 timeout = max_t(long, timeout, HZ / 10);
585 schedule_timeout_interruptible(timeout);
589 * tty_port_close_start - helper for tty->ops->close, part 1/2
590 * @port: tty_port of the device
591 * @tty: tty being closed
592 * @filp: passed file pointer
594 * Decrements and checks open count. Flushes the port if this is the last
595 * close. That means, dropping the data from the outpu buffer on the device and
596 * waiting for sending logic to finish. The rest of close handling is performed
597 * in tty_port_close_end().
599 * Locking: Caller holds tty lock.
601 * Return: 1 if this is the last close, otherwise 0
603 int tty_port_close_start(struct tty_port *port,
604 struct tty_struct *tty, struct file *filp)
608 if (tty_hung_up_p(filp))
611 spin_lock_irqsave(&port->lock, flags);
612 if (tty->count == 1 && port->count != 1) {
613 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
617 if (--port->count < 0) {
618 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
624 spin_unlock_irqrestore(&port->lock, flags);
627 spin_unlock_irqrestore(&port->lock, flags);
631 if (tty_port_initialized(port)) {
632 /* Don't block on a stalled port, just pull the chain */
633 if (tty->flow.tco_stopped)
634 tty_driver_flush_buffer(tty);
635 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
636 tty_wait_until_sent(tty, port->closing_wait);
637 if (port->drain_delay)
638 tty_port_drain_delay(port, tty);
640 /* Flush the ldisc buffering */
641 tty_ldisc_flush(tty);
643 /* Report to caller this is the last port reference */
646 EXPORT_SYMBOL(tty_port_close_start);
649 * tty_port_close_end - helper for tty->ops->close, part 2/2
650 * @port: tty_port of the device
651 * @tty: tty being closed
653 * This is a continuation of the first part: tty_port_close_start(). This
654 * should be called after turning off the device. It flushes the data from the
655 * line discipline and delays the close by @port->close_delay.
657 * Locking: Caller holds tty lock.
659 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
663 tty_ldisc_flush(tty);
666 spin_lock_irqsave(&port->lock, flags);
668 if (port->blocked_open) {
669 spin_unlock_irqrestore(&port->lock, flags);
670 if (port->close_delay)
671 msleep_interruptible(jiffies_to_msecs(port->close_delay));
672 spin_lock_irqsave(&port->lock, flags);
673 wake_up_interruptible(&port->open_wait);
675 spin_unlock_irqrestore(&port->lock, flags);
676 tty_port_set_active(port, 0);
678 EXPORT_SYMBOL(tty_port_close_end);
681 * tty_port_close - generic tty->ops->close handler
682 * @port: tty_port of the device
683 * @tty: tty being closed
684 * @filp: passed file pointer
686 * It is a generic helper to be used in driver's @tty->ops->close. It wraps a
687 * sequence of tty_port_close_start(), tty_port_shutdown(), and
688 * tty_port_close_end(). The latter two are called only if this is the last
689 * close. See the respective functions for the details.
691 * Locking: Caller holds tty lock
693 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
696 if (tty_port_close_start(port, tty, filp) == 0)
698 tty_port_shutdown(port, tty);
700 set_bit(TTY_IO_ERROR, &tty->flags);
701 tty_port_close_end(port, tty);
702 tty_port_tty_set(port, NULL);
704 EXPORT_SYMBOL(tty_port_close);
707 * tty_port_install - generic tty->ops->install handler
708 * @port: tty_port of the device
709 * @driver: tty_driver for this device
710 * @tty: tty to be installed
712 * It is the same as tty_standard_install() except the provided @port is linked
713 * to a concrete tty specified by @tty. Use this or tty_port_register_device()
714 * (or both). Call tty_port_link_device() as a last resort.
716 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
717 struct tty_struct *tty)
720 return tty_standard_install(driver, tty);
722 EXPORT_SYMBOL_GPL(tty_port_install);
725 * tty_port_open - generic tty->ops->open handler
726 * @port: tty_port of the device
727 * @tty: tty to be opened
728 * @filp: passed file pointer
730 * It is a generic helper to be used in driver's @tty->ops->open. It activates
731 * the devices using @port->ops->activate if not active already. And waits for
732 * the device to be ready using tty_port_block_til_ready() (e.g. raises
733 * DTR/CTS and waits for carrier).
735 * Locking: Caller holds tty lock.
737 * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so
738 * @tty and @port may have changed state (eg., may be hung up now).
740 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
743 spin_lock_irq(&port->lock);
745 spin_unlock_irq(&port->lock);
746 tty_port_tty_set(port, tty);
749 * Do the device-specific open only if the hardware isn't
750 * already initialized. Serialize open and shutdown using the
754 mutex_lock(&port->mutex);
756 if (!tty_port_initialized(port)) {
757 clear_bit(TTY_IO_ERROR, &tty->flags);
758 if (port->ops->activate) {
759 int retval = port->ops->activate(port, tty);
762 mutex_unlock(&port->mutex);
766 tty_port_set_initialized(port, 1);
768 mutex_unlock(&port->mutex);
769 return tty_port_block_til_ready(port, tty, filp);
771 EXPORT_SYMBOL(tty_port_open);