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)
134 tty_port_link_device(port, driver, index);
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 */
141 return tty_register_device_attr(driver, index, device, drvdata,
144 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
146 int tty_port_alloc_xmit_buf(struct tty_port *port)
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)
157 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
159 void tty_port_free_xmit_buf(struct tty_port *port)
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;
166 mutex_unlock(&port->buf_mutex);
168 EXPORT_SYMBOL(tty_port_free_xmit_buf);
171 * tty_port_destroy -- destroy inited port
172 * @port: tty port to be doestroyed
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.
178 void tty_port_destroy(struct tty_port *port)
180 tty_buffer_cancel_work(port);
181 tty_buffer_free_all(port);
183 EXPORT_SYMBOL(tty_port_destroy);
185 static void tty_port_destructor(struct kref *kref)
187 struct tty_port *port = container_of(kref, struct tty_port, kref);
189 /* check if last port ref was dropped before tty release */
190 if (WARN_ON(port->itty))
193 serdev_tty_port_unregister(port);
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);
204 void tty_port_put(struct tty_port *port)
207 kref_put(&port->kref, tty_port_destructor);
209 EXPORT_SYMBOL(tty_port_put);
212 * tty_port_tty_get - get a tty reference
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)
219 struct tty_struct *tty_port_tty_get(struct tty_port *port)
222 struct tty_struct *tty;
224 spin_lock_irqsave(&port->lock, flags);
225 tty = tty_kref_get(port->tty);
226 spin_unlock_irqrestore(&port->lock, flags);
229 EXPORT_SYMBOL(tty_port_tty_get);
232 * tty_port_tty_set - set the tty of a port
236 * Associate the port and tty pair. Manages any internal refcounts.
237 * Pass NULL to deassociate a port
240 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
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);
249 EXPORT_SYMBOL(tty_port_tty_set);
251 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
253 mutex_lock(&port->mutex);
257 if (tty_port_initialized(port)) {
258 tty_port_set_initialized(port, 0);
260 * Drop DTR/RTS if HUPCL is set. This causes any attached
261 * modem to hang up the line.
263 if (tty && C_HUPCL(tty))
264 tty_port_lower_dtr_rts(port);
266 if (port->ops->shutdown)
267 port->ops->shutdown(port);
270 mutex_unlock(&port->mutex);
274 * tty_port_hangup - hangup helper
277 * Perform port level tty hangup flag and count changes. Drop the tty
280 * Caller holds tty lock.
283 void tty_port_hangup(struct tty_port *port)
285 struct tty_struct *tty;
288 spin_lock_irqsave(&port->lock, flags);
292 set_bit(TTY_IO_ERROR, &tty->flags);
294 spin_unlock_irqrestore(&port->lock, flags);
295 tty_port_set_active(port, 0);
296 tty_port_shutdown(port, tty);
298 wake_up_interruptible(&port->open_wait);
299 wake_up_interruptible(&port->delta_msr_wait);
301 EXPORT_SYMBOL(tty_port_hangup);
304 * tty_port_tty_hangup - helper to hang up a tty
307 * @check_clocal: hang only ttys with CLOCAL unset?
309 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
311 struct tty_struct *tty = tty_port_tty_get(port);
313 if (tty && (!check_clocal || !C_CLOCAL(tty)))
317 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
320 * tty_port_tty_wakeup - helper to wake up a tty
324 void tty_port_tty_wakeup(struct tty_port *port)
326 port->client_ops->write_wakeup(port);
328 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
331 * tty_port_carrier_raised - carrier raised check
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.
339 int tty_port_carrier_raised(struct tty_port *port)
341 if (port->ops->carrier_raised == NULL)
343 return port->ops->carrier_raised(port);
345 EXPORT_SYMBOL(tty_port_carrier_raised);
348 * tty_port_raise_dtr_rts - Raise DTR/RTS
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.
356 void tty_port_raise_dtr_rts(struct tty_port *port)
358 if (port->ops->dtr_rts)
359 port->ops->dtr_rts(port, 1);
361 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
364 * tty_port_lower_dtr_rts - Lower DTR/RTS
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.
372 void tty_port_lower_dtr_rts(struct tty_port *port)
374 if (port->ops->dtr_rts)
375 port->ops->dtr_rts(port, 0);
377 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
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
385 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
387 * - hangup (both before and during)
388 * - non blocking open
391 * - port flags and counts
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.
398 * Caller holds tty lock.
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).
404 int tty_port_block_til_ready(struct tty_port *port,
405 struct tty_struct *tty, struct file *filp)
407 int do_clocal = 0, retval;
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);
417 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
418 /* Indicate we are open */
420 tty_port_raise_dtr_rts(port);
421 tty_port_set_active(port, 1);
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 */
434 /* The port lock protects the port counts */
435 spin_lock_irqsave(&port->lock, flags);
437 port->blocked_open++;
438 spin_unlock_irqrestore(&port->lock, flags);
441 /* Indicate we are open */
442 if (C_BAUD(tty) && tty_port_initialized(port))
443 tty_port_raise_dtr_rts(port);
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)
452 retval = -ERESTARTSYS;
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
461 if (do_clocal || tty_port_carrier_raised(port))
463 if (signal_pending(current)) {
464 retval = -ERESTARTSYS;
471 finish_wait(&port->open_wait, &wait);
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))
478 port->blocked_open--;
479 spin_unlock_irqrestore(&port->lock, flags);
481 tty_port_set_active(port, 1);
484 EXPORT_SYMBOL(tty_port_block_til_ready);
486 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
488 unsigned int bps = tty_get_baud_rate(tty);
492 timeout = (HZ * 10 * port->drain_delay) / bps;
493 timeout = max_t(long, timeout, HZ / 10);
497 schedule_timeout_interruptible(timeout);
500 /* Caller holds tty lock. */
501 int tty_port_close_start(struct tty_port *port,
502 struct tty_struct *tty, struct file *filp)
506 if (tty_hung_up_p(filp))
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__,
515 if (--port->count < 0) {
516 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
522 spin_unlock_irqrestore(&port->lock, flags);
525 spin_unlock_irqrestore(&port->lock, flags);
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);
538 /* Flush the ldisc buffering */
539 tty_ldisc_flush(tty);
541 /* Report to caller this is the last port reference */
544 EXPORT_SYMBOL(tty_port_close_start);
546 /* Caller holds tty lock */
547 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
551 tty_ldisc_flush(tty);
554 spin_lock_irqsave(&port->lock, flags);
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);
563 spin_unlock_irqrestore(&port->lock, flags);
564 tty_port_set_active(port, 0);
566 EXPORT_SYMBOL(tty_port_close_end);
571 * Caller holds tty lock
573 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
576 if (tty_port_close_start(port, tty, filp) == 0)
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);
583 EXPORT_SYMBOL(tty_port_close);
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
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.
595 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
596 struct tty_struct *tty)
599 return tty_standard_install(driver, tty);
601 EXPORT_SYMBOL_GPL(tty_port_install);
606 * Caller holds tty lock.
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)
611 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
614 spin_lock_irq(&port->lock);
616 spin_unlock_irq(&port->lock);
617 tty_port_tty_set(port, tty);
620 * Do the device-specific open only if the hardware isn't
621 * already initialized. Serialize open and shutdown using the
625 mutex_lock(&port->mutex);
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);
632 mutex_unlock(&port->mutex);
636 tty_port_set_initialized(port, 1);
638 mutex_unlock(&port->mutex);
639 return tty_port_block_til_ready(port, tty, filp);
642 EXPORT_SYMBOL(tty_port_open);