]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/char/tty_port.c
tty_port: Add a port level carrier detect operation
[mirror_ubuntu-bionic-kernel.git] / drivers / char / tty_port.c
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/timer.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19
20 void tty_port_init(struct tty_port *port)
21 {
22 memset(port, 0, sizeof(*port));
23 init_waitqueue_head(&port->open_wait);
24 init_waitqueue_head(&port->close_wait);
25 mutex_init(&port->mutex);
26 spin_lock_init(&port->lock);
27 port->close_delay = (50 * HZ) / 100;
28 port->closing_wait = (3000 * HZ) / 100;
29 }
30 EXPORT_SYMBOL(tty_port_init);
31
32 int tty_port_alloc_xmit_buf(struct tty_port *port)
33 {
34 /* We may sleep in get_zeroed_page() */
35 mutex_lock(&port->mutex);
36 if (port->xmit_buf == NULL)
37 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
38 mutex_unlock(&port->mutex);
39 if (port->xmit_buf == NULL)
40 return -ENOMEM;
41 return 0;
42 }
43 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
44
45 void tty_port_free_xmit_buf(struct tty_port *port)
46 {
47 mutex_lock(&port->mutex);
48 if (port->xmit_buf != NULL) {
49 free_page((unsigned long)port->xmit_buf);
50 port->xmit_buf = NULL;
51 }
52 mutex_unlock(&port->mutex);
53 }
54 EXPORT_SYMBOL(tty_port_free_xmit_buf);
55
56
57 /**
58 * tty_port_tty_get - get a tty reference
59 * @port: tty port
60 *
61 * Return a refcount protected tty instance or NULL if the port is not
62 * associated with a tty (eg due to close or hangup)
63 */
64
65 struct tty_struct *tty_port_tty_get(struct tty_port *port)
66 {
67 unsigned long flags;
68 struct tty_struct *tty;
69
70 spin_lock_irqsave(&port->lock, flags);
71 tty = tty_kref_get(port->tty);
72 spin_unlock_irqrestore(&port->lock, flags);
73 return tty;
74 }
75 EXPORT_SYMBOL(tty_port_tty_get);
76
77 /**
78 * tty_port_tty_set - set the tty of a port
79 * @port: tty port
80 * @tty: the tty
81 *
82 * Associate the port and tty pair. Manages any internal refcounts.
83 * Pass NULL to deassociate a port
84 */
85
86 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
87 {
88 unsigned long flags;
89
90 spin_lock_irqsave(&port->lock, flags);
91 if (port->tty)
92 tty_kref_put(port->tty);
93 port->tty = tty_kref_get(tty);
94 spin_unlock_irqrestore(&port->lock, flags);
95 }
96 EXPORT_SYMBOL(tty_port_tty_set);
97
98 /**
99 * tty_port_carrier_raised - carrier raised check
100 * @port: tty port
101 *
102 * Wrapper for the carrier detect logic. For the moment this is used
103 * to hide some internal details. This will eventually become entirely
104 * internal to the tty port.
105 */
106
107 int tty_port_carrier_raised(struct tty_port *port)
108 {
109 if (port->ops->carrier_raised == NULL)
110 return 1;
111 return port->ops->carrier_raised(port);
112 }
113 EXPORT_SYMBOL(tty_port_carrier_raised);