1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TTY_PORT_H
3 #define _LINUX_TTY_PORT_H
4
5 #include <linux/kref.h>
6 #include <linux/mutex.h>
7 #include <linux/tty_buffer.h>
8 #include <linux/wait.h>
9 #include <linux/android_kabi.h>
10
11 /*
12 * Port level information. Each device keeps its own port level information
13 * so provide a common structure for those ports wanting to use common support
14 * routines.
15 *
16 * The tty port has a different lifetime to the tty so must be kept apart.
17 * In addition be careful as tty -> port mappings are valid for the life
18 * of the tty object but in many cases port -> tty mappings are valid only
19 * until a hangup so don't use the wrong path.
20 */
21
22 struct attribute_group;
23 struct tty_driver;
24 struct tty_port;
25 struct tty_struct;
26
27 struct tty_port_operations {
28 /* Return 1 if the carrier is raised */
29 int (*carrier_raised)(struct tty_port *port);
30 /* Control the DTR line */
31 void (*dtr_rts)(struct tty_port *port, int raise);
32 /* Called when the last close completes or a hangup finishes
33 IFF the port was initialized. Do not use to free resources. Called
34 under the port mutex to serialize against activate/shutdowns */
35 void (*shutdown)(struct tty_port *port);
36 /* Called under the port mutex from tty_port_open, serialized using
37 the port mutex */
38 /* FIXME: long term getting the tty argument *out* of this would be
39 good for consoles */
40 int (*activate)(struct tty_port *port, struct tty_struct *tty);
41 /* Called on the final put of a port */
42 void (*destruct)(struct tty_port *port);
43
44 ANDROID_KABI_RESERVE(1);
45 };
46
47 struct tty_port_client_operations {
48 int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
49 void (*write_wakeup)(struct tty_port *port);
50 };
51
52 extern const struct tty_port_client_operations tty_port_default_client_ops;
53
54 struct tty_port {
55 struct tty_bufhead buf; /* Locked internally */
56 struct tty_struct *tty; /* Back pointer */
57 struct tty_struct *itty; /* internal back ptr */
58 const struct tty_port_operations *ops; /* Port operations */
59 const struct tty_port_client_operations *client_ops; /* Port client operations */
60 spinlock_t lock; /* Lock protecting tty field */
61 int blocked_open; /* Waiting to open */
62 int count; /* Usage count */
63 wait_queue_head_t open_wait; /* Open waiters */
64 wait_queue_head_t delta_msr_wait; /* Modem status change */
65 unsigned long flags; /* User TTY flags ASYNC_ */
66 unsigned long iflags; /* Internal flags TTY_PORT_ */
67 unsigned char console:1; /* port is a console */
68 struct mutex mutex; /* Locking */
69 struct mutex buf_mutex; /* Buffer alloc lock */
70 unsigned char *xmit_buf; /* Optional buffer */
71 unsigned int close_delay; /* Close port delay */
72 unsigned int closing_wait; /* Delay for output */
73 int drain_delay; /* Set to zero if no pure time
74 based drain is needed else
75 set to size of fifo */
76 struct kref kref; /* Ref counter */
77 void *client_data;
78
79 ANDROID_KABI_RESERVE(1);
80 };
81
82 /* tty_port::iflags bits -- use atomic bit ops */
83 #define TTY_PORT_INITIALIZED 0 /* device is initialized */
84 #define TTY_PORT_SUSPENDED 1 /* device is suspended */
85 #define TTY_PORT_ACTIVE 2 /* device is open */
86
87 /*
88 * uart drivers: use the uart_port::status field and the UPSTAT_* defines
89 * for s/w-based flow control steering and carrier detection status
90 */
91 #define TTY_PORT_CTS_FLOW 3 /* h/w flow control enabled */
92 #define TTY_PORT_CHECK_CD 4 /* carrier detect enabled */
93 #define TTY_PORT_KOPENED 5 /* device exclusively opened by
94 kernel */
95
96 void tty_port_init(struct tty_port *port);
97 void tty_port_link_device(struct tty_port *port, struct tty_driver *driver,
98 unsigned index);
99 struct device *tty_port_register_device(struct tty_port *port,
100 struct tty_driver *driver, unsigned index,
101 struct device *device);
102 struct device *tty_port_register_device_attr(struct tty_port *port,
103 struct tty_driver *driver, unsigned index,
104 struct device *device, void *drvdata,
105 const struct attribute_group **attr_grp);
106 struct device *tty_port_register_device_serdev(struct tty_port *port,
107 struct tty_driver *driver, unsigned index,
108 struct device *device);
109 struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
110 struct tty_driver *driver, unsigned index,
111 struct device *device, void *drvdata,
112 const struct attribute_group **attr_grp);
113 void tty_port_unregister_device(struct tty_port *port,
114 struct tty_driver *driver, unsigned index);
115 int tty_port_alloc_xmit_buf(struct tty_port *port);
116 void tty_port_free_xmit_buf(struct tty_port *port);
117 void tty_port_destroy(struct tty_port *port);
118 void tty_port_put(struct tty_port *port);
119
tty_port_get(struct tty_port * port)120 static inline struct tty_port *tty_port_get(struct tty_port *port)
121 {
122 if (port && kref_get_unless_zero(&port->kref))
123 return port;
124 return NULL;
125 }
126
127 /* If the cts flow control is enabled, return true. */
tty_port_cts_enabled(const struct tty_port * port)128 static inline bool tty_port_cts_enabled(const struct tty_port *port)
129 {
130 return test_bit(TTY_PORT_CTS_FLOW, &port->iflags);
131 }
132
tty_port_set_cts_flow(struct tty_port * port,bool val)133 static inline void tty_port_set_cts_flow(struct tty_port *port, bool val)
134 {
135 assign_bit(TTY_PORT_CTS_FLOW, &port->iflags, val);
136 }
137
tty_port_active(const struct tty_port * port)138 static inline bool tty_port_active(const struct tty_port *port)
139 {
140 return test_bit(TTY_PORT_ACTIVE, &port->iflags);
141 }
142
tty_port_set_active(struct tty_port * port,bool val)143 static inline void tty_port_set_active(struct tty_port *port, bool val)
144 {
145 assign_bit(TTY_PORT_ACTIVE, &port->iflags, val);
146 }
147
tty_port_check_carrier(const struct tty_port * port)148 static inline bool tty_port_check_carrier(const struct tty_port *port)
149 {
150 return test_bit(TTY_PORT_CHECK_CD, &port->iflags);
151 }
152
tty_port_set_check_carrier(struct tty_port * port,bool val)153 static inline void tty_port_set_check_carrier(struct tty_port *port, bool val)
154 {
155 assign_bit(TTY_PORT_CHECK_CD, &port->iflags, val);
156 }
157
tty_port_suspended(const struct tty_port * port)158 static inline bool tty_port_suspended(const struct tty_port *port)
159 {
160 return test_bit(TTY_PORT_SUSPENDED, &port->iflags);
161 }
162
tty_port_set_suspended(struct tty_port * port,bool val)163 static inline void tty_port_set_suspended(struct tty_port *port, bool val)
164 {
165 assign_bit(TTY_PORT_SUSPENDED, &port->iflags, val);
166 }
167
tty_port_initialized(const struct tty_port * port)168 static inline bool tty_port_initialized(const struct tty_port *port)
169 {
170 return test_bit(TTY_PORT_INITIALIZED, &port->iflags);
171 }
172
tty_port_set_initialized(struct tty_port * port,bool val)173 static inline void tty_port_set_initialized(struct tty_port *port, bool val)
174 {
175 assign_bit(TTY_PORT_INITIALIZED, &port->iflags, val);
176 }
177
tty_port_kopened(const struct tty_port * port)178 static inline bool tty_port_kopened(const struct tty_port *port)
179 {
180 return test_bit(TTY_PORT_KOPENED, &port->iflags);
181 }
182
tty_port_set_kopened(struct tty_port * port,bool val)183 static inline void tty_port_set_kopened(struct tty_port *port, bool val)
184 {
185 assign_bit(TTY_PORT_KOPENED, &port->iflags, val);
186 }
187
188 struct tty_struct *tty_port_tty_get(struct tty_port *port);
189 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
190 int tty_port_carrier_raised(struct tty_port *port);
191 void tty_port_raise_dtr_rts(struct tty_port *port);
192 void tty_port_lower_dtr_rts(struct tty_port *port);
193 void tty_port_hangup(struct tty_port *port);
194 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
195 void tty_port_tty_wakeup(struct tty_port *port);
196 int tty_port_block_til_ready(struct tty_port *port, struct tty_struct *tty,
197 struct file *filp);
198 int tty_port_close_start(struct tty_port *port, struct tty_struct *tty,
199 struct file *filp);
200 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
201 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
202 struct file *filp);
203 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
204 struct tty_struct *tty);
205 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
206 struct file *filp);
207
tty_port_users(struct tty_port * port)208 static inline int tty_port_users(struct tty_port *port)
209 {
210 return port->count + port->blocked_open;
211 }
212
213 #endif
214