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