5 #include <linux/types.h>
6 #include <linux/errno.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>
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)
26 struct tty_struct *tty;
27 struct tty_ldisc *disc;
29 tty = READ_ONCE(port->itty);
33 disc = tty_ldisc_ref(tty);
37 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
39 tty_ldisc_deref(disc);
44 static void tty_port_default_wakeup(struct tty_port *port)
46 struct tty_struct *tty = tty_port_tty_get(port);
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,
59 void tty_port_init(struct tty_port *port)
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);
73 EXPORT_SYMBOL(tty_port_init);
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
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.
86 void tty_port_link_device(struct tty_port *port,
87 struct tty_driver *driver, unsigned index)
89 if (WARN_ON(index >= driver->num))
91 driver->ports[index] = port;
93 EXPORT_SYMBOL_GPL(tty_port_link_device);
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
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.
106 struct device *tty_port_register_device(struct tty_port *port,
107 struct tty_driver *driver, unsigned index,
108 struct device *device)
110 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
112 EXPORT_SYMBOL_GPL(tty_port_register_device);
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.
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.
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)
132 tty_port_link_device(port, driver, index);
133 return tty_register_device_attr(driver, index, device, drvdata,
136 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
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
147 * Register a serdev or tty device depending on if the parent device has any
148 * defined serdev clients or not.
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)
157 tty_port_link_device(port, driver, index);
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 */
165 return tty_register_device_attr(driver, index, device, drvdata,
168 EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
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
177 * Register a serdev or tty device depending on if the parent device has any
178 * defined serdev clients or not.
180 struct device *tty_port_register_device_serdev(struct tty_port *port,
181 struct tty_driver *driver, unsigned index,
182 struct device *device)
184 return tty_port_register_device_attr_serdev(port, driver, index,
187 EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
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
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.
199 void tty_port_unregister_device(struct tty_port *port,
200 struct tty_driver *driver, unsigned index)
204 ret = serdev_tty_port_unregister(port);
208 tty_unregister_device(driver, index);
210 EXPORT_SYMBOL_GPL(tty_port_unregister_device);
212 int tty_port_alloc_xmit_buf(struct tty_port *port)
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)
223 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
225 void tty_port_free_xmit_buf(struct tty_port *port)
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;
232 mutex_unlock(&port->buf_mutex);
234 EXPORT_SYMBOL(tty_port_free_xmit_buf);
237 * tty_port_destroy -- destroy inited port
238 * @port: tty port to be doestroyed
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.
244 void tty_port_destroy(struct tty_port *port)
246 tty_buffer_cancel_work(port);
247 tty_buffer_free_all(port);
249 EXPORT_SYMBOL(tty_port_destroy);
251 static void tty_port_destructor(struct kref *kref)
253 struct tty_port *port = container_of(kref, struct tty_port, kref);
255 /* check if last port ref was dropped before tty release */
256 if (WARN_ON(port->itty))
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);
267 void tty_port_put(struct tty_port *port)
270 kref_put(&port->kref, tty_port_destructor);
272 EXPORT_SYMBOL(tty_port_put);
275 * tty_port_tty_get - get a tty reference
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)
282 struct tty_struct *tty_port_tty_get(struct tty_port *port)
285 struct tty_struct *tty;
287 spin_lock_irqsave(&port->lock, flags);
288 tty = tty_kref_get(port->tty);
289 spin_unlock_irqrestore(&port->lock, flags);
292 EXPORT_SYMBOL(tty_port_tty_get);
295 * tty_port_tty_set - set the tty of a port
299 * Associate the port and tty pair. Manages any internal refcounts.
300 * Pass NULL to deassociate a port
303 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
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);
312 EXPORT_SYMBOL(tty_port_tty_set);
314 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
316 mutex_lock(&port->mutex);
320 if (tty_port_initialized(port)) {
321 tty_port_set_initialized(port, 0);
323 * Drop DTR/RTS if HUPCL is set. This causes any attached
324 * modem to hang up the line.
326 if (tty && C_HUPCL(tty))
327 tty_port_lower_dtr_rts(port);
329 if (port->ops->shutdown)
330 port->ops->shutdown(port);
333 mutex_unlock(&port->mutex);
337 * tty_port_hangup - hangup helper
340 * Perform port level tty hangup flag and count changes. Drop the tty
343 * Caller holds tty lock.
346 void tty_port_hangup(struct tty_port *port)
348 struct tty_struct *tty;
351 spin_lock_irqsave(&port->lock, flags);
355 set_bit(TTY_IO_ERROR, &tty->flags);
357 spin_unlock_irqrestore(&port->lock, flags);
358 tty_port_set_active(port, 0);
359 tty_port_shutdown(port, tty);
361 wake_up_interruptible(&port->open_wait);
362 wake_up_interruptible(&port->delta_msr_wait);
364 EXPORT_SYMBOL(tty_port_hangup);
367 * tty_port_tty_hangup - helper to hang up a tty
370 * @check_clocal: hang only ttys with CLOCAL unset?
372 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
374 struct tty_struct *tty = tty_port_tty_get(port);
376 if (tty && (!check_clocal || !C_CLOCAL(tty)))
380 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
383 * tty_port_tty_wakeup - helper to wake up a tty
387 void tty_port_tty_wakeup(struct tty_port *port)
389 port->client_ops->write_wakeup(port);
391 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
394 * tty_port_carrier_raised - carrier raised check
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.
402 int tty_port_carrier_raised(struct tty_port *port)
404 if (port->ops->carrier_raised == NULL)
406 return port->ops->carrier_raised(port);
408 EXPORT_SYMBOL(tty_port_carrier_raised);
411 * tty_port_raise_dtr_rts - Raise DTR/RTS
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.
419 void tty_port_raise_dtr_rts(struct tty_port *port)
421 if (port->ops->dtr_rts)
422 port->ops->dtr_rts(port, 1);
424 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
427 * tty_port_lower_dtr_rts - Lower DTR/RTS
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.
435 void tty_port_lower_dtr_rts(struct tty_port *port)
437 if (port->ops->dtr_rts)
438 port->ops->dtr_rts(port, 0);
440 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
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
448 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
450 * - hangup (both before and during)
451 * - non blocking open
454 * - port flags and counts
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.
461 * Caller holds tty lock.
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).
467 int tty_port_block_til_ready(struct tty_port *port,
468 struct tty_struct *tty, struct file *filp)
470 int do_clocal = 0, retval;
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);
480 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
481 /* Indicate we are open */
483 tty_port_raise_dtr_rts(port);
484 tty_port_set_active(port, 1);
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 */
497 /* The port lock protects the port counts */
498 spin_lock_irqsave(&port->lock, flags);
500 port->blocked_open++;
501 spin_unlock_irqrestore(&port->lock, flags);
504 /* Indicate we are open */
505 if (C_BAUD(tty) && tty_port_initialized(port))
506 tty_port_raise_dtr_rts(port);
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)
515 retval = -ERESTARTSYS;
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
524 if (do_clocal || tty_port_carrier_raised(port))
526 if (signal_pending(current)) {
527 retval = -ERESTARTSYS;
534 finish_wait(&port->open_wait, &wait);
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))
541 port->blocked_open--;
542 spin_unlock_irqrestore(&port->lock, flags);
544 tty_port_set_active(port, 1);
547 EXPORT_SYMBOL(tty_port_block_til_ready);
549 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
551 unsigned int bps = tty_get_baud_rate(tty);
555 timeout = (HZ * 10 * port->drain_delay) / bps;
556 timeout = max_t(long, timeout, HZ / 10);
560 schedule_timeout_interruptible(timeout);
563 /* Caller holds tty lock. */
564 int tty_port_close_start(struct tty_port *port,
565 struct tty_struct *tty, struct file *filp)
569 if (tty_hung_up_p(filp))
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__,
578 if (--port->count < 0) {
579 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
585 spin_unlock_irqrestore(&port->lock, flags);
588 spin_unlock_irqrestore(&port->lock, flags);
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);
601 /* Flush the ldisc buffering */
602 tty_ldisc_flush(tty);
604 /* Report to caller this is the last port reference */
607 EXPORT_SYMBOL(tty_port_close_start);
609 /* Caller holds tty lock */
610 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
614 tty_ldisc_flush(tty);
617 spin_lock_irqsave(&port->lock, flags);
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);
626 spin_unlock_irqrestore(&port->lock, flags);
627 tty_port_set_active(port, 0);
629 EXPORT_SYMBOL(tty_port_close_end);
634 * Caller holds tty lock
636 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
639 if (tty_port_close_start(port, tty, filp) == 0)
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);
646 EXPORT_SYMBOL(tty_port_close);
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
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.
658 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
659 struct tty_struct *tty)
662 return tty_standard_install(driver, tty);
664 EXPORT_SYMBOL_GPL(tty_port_install);
669 * Caller holds tty lock.
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)
674 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
677 spin_lock_irq(&port->lock);
679 spin_unlock_irq(&port->lock);
680 tty_port_tty_set(port, tty);
683 * Do the device-specific open only if the hardware isn't
684 * already initialized. Serialize open and shutdown using the
688 mutex_lock(&port->mutex);
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);
695 mutex_unlock(&port->mutex);
699 tty_port_set_initialized(port, 1);
701 mutex_unlock(&port->mutex);
702 return tty_port_block_til_ready(port, tty, filp);
705 EXPORT_SYMBOL(tty_port_open);