xref: /openbmc/linux/include/linux/tty_ldisc.h (revision 49b8220c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TTY_LDISC_H
3 #define _LINUX_TTY_LDISC_H
4 
5 struct tty_struct;
6 
7 #include <linux/fs.h>
8 #include <linux/wait.h>
9 #include <linux/atomic.h>
10 #include <linux/list.h>
11 #include <linux/lockdep.h>
12 #include <linux/seq_file.h>
13 
14 /*
15  * the semaphore definition
16  */
17 struct ld_semaphore {
18 	atomic_long_t		count;
19 	raw_spinlock_t		wait_lock;
20 	unsigned int		wait_readers;
21 	struct list_head	read_wait;
22 	struct list_head	write_wait;
23 #ifdef CONFIG_DEBUG_LOCK_ALLOC
24 	struct lockdep_map	dep_map;
25 #endif
26 };
27 
28 void __init_ldsem(struct ld_semaphore *sem, const char *name,
29 			 struct lock_class_key *key);
30 
31 #define init_ldsem(sem)						\
32 do {								\
33 	static struct lock_class_key __key;			\
34 								\
35 	__init_ldsem((sem), #sem, &__key);			\
36 } while (0)
37 
38 
39 int ldsem_down_read(struct ld_semaphore *sem, long timeout);
40 int ldsem_down_read_trylock(struct ld_semaphore *sem);
41 int ldsem_down_write(struct ld_semaphore *sem, long timeout);
42 int ldsem_down_write_trylock(struct ld_semaphore *sem);
43 void ldsem_up_read(struct ld_semaphore *sem);
44 void ldsem_up_write(struct ld_semaphore *sem);
45 
46 #ifdef CONFIG_DEBUG_LOCK_ALLOC
47 int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass,
48 		long timeout);
49 int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
50 		long timeout);
51 #else
52 # define ldsem_down_read_nested(sem, subclass, timeout)		\
53 		ldsem_down_read(sem, timeout)
54 # define ldsem_down_write_nested(sem, subclass, timeout)	\
55 		ldsem_down_write(sem, timeout)
56 #endif
57 
58 /**
59  * struct tty_ldisc_ops - ldisc operations
60  *
61  * @name: name of this ldisc rendered in /proc/tty/ldiscs
62  * @num: ``N_*`` number (%N_TTY, %N_HDLC, ...) reserved to this ldisc
63  *
64  * @open: [TTY] ``int ()(struct tty_struct *tty)``
65  *
66  *	This function is called when the line discipline is associated with the
67  *	@tty. No other call into the line discipline for this tty will occur
68  *	until it completes successfully. It should initialize any state needed
69  *	by the ldisc, and set @tty->receive_room to the maximum amount of data
70  *	the line discipline is willing to accept from the driver with a single
71  *	call to @receive_buf(). Returning an error will prevent the ldisc from
72  *	being attached.
73  *
74  *	Optional. Can sleep.
75  *
76  * @close: [TTY] ``void ()(struct tty_struct *tty)``
77  *
78  *	This function is called when the line discipline is being shutdown,
79  *	either because the @tty is being closed or because the @tty is being
80  *	changed to use a new line discipline. At the point of execution no
81  *	further users will enter the ldisc code for this tty.
82  *
83  *	Optional. Can sleep.
84  *
85  * @flush_buffer: [TTY] ``void ()(struct tty_struct *tty)``
86  *
87  *	This function instructs the line discipline to clear its buffers of any
88  *	input characters it may have queued to be delivered to the user mode
89  *	process. It may be called at any point between open and close.
90  *
91  *	Optional.
92  *
93  * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file, u8 *buf,
94  *		size_t nr)``
95  *
96  *	This function is called when the user requests to read from the @tty.
97  *	The line discipline will return whatever characters it has buffered up
98  *	for the user. If this function is not defined, the user will receive
99  *	an %EIO error. Multiple read calls may occur in parallel and the ldisc
100  *	must deal with serialization issues.
101  *
102  *	Optional: %EIO unless provided. Can sleep.
103  *
104  * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
105  *		 const u8 *buf, size_t nr)``
106  *
107  *	This function is called when the user requests to write to the @tty.
108  *	The line discipline will deliver the characters to the low-level tty
109  *	device for transmission, optionally performing some processing on the
110  *	characters first. If this function is not defined, the user will
111  *	receive an %EIO error.
112  *
113  *	Optional: %EIO unless provided. Can sleep.
114  *
115  * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
116  *		unsigned long arg)``
117  *
118  *	This function is called when the user requests an ioctl which is not
119  *	handled by the tty layer or the low-level tty driver. It is intended
120  *	for ioctls which affect line discpline operation.  Note that the search
121  *	order for ioctls is (1) tty layer, (2) tty low-level driver, (3) line
122  *	discpline. So a low-level driver can "grab" an ioctl request before
123  *	the line discpline has a chance to see it.
124  *
125  *	Optional.
126  *
127  * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
128  *		unsigned long arg)``
129  *
130  *	Process ioctl calls from 32-bit process on 64-bit system.
131  *
132  *	Note that only ioctls that are neither "pointer to compatible
133  *	structure" nor tty-generic.  Something private that takes an integer or
134  *	a pointer to wordsize-sensitive structure belongs here, but most of
135  *	ldiscs will happily leave it %NULL.
136  *
137  *	Optional.
138  *
139  * @set_termios: [TTY] ``void ()(struct tty_struct *tty, const struct ktermios *old)``
140  *
141  *	This function notifies the line discpline that a change has been made
142  *	to the termios structure.
143  *
144  *	Optional.
145  *
146  * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file,
147  *		  struct poll_table_struct *wait)``
148  *
149  *	This function is called when a user attempts to select/poll on a @tty
150  *	device. It is solely the responsibility of the line discipline to
151  *	handle poll requests.
152  *
153  *	Optional.
154  *
155  * @hangup: [TTY] ``void ()(struct tty_struct *tty)``
156  *
157  *	Called on a hangup. Tells the discipline that it should cease I/O to
158  *	the tty driver. The driver should seek to perform this action quickly
159  *	but should wait until any pending driver I/O is completed. No further
160  *	calls into the ldisc code will occur.
161  *
162  *	Optional. Can sleep.
163  *
164  * @receive_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp,
165  *		       const u8 *fp, size_t count)``
166  *
167  *	This function is called by the low-level tty driver to send characters
168  *	received by the hardware to the line discpline for processing. @cp is
169  *	a pointer to the buffer of input character received by the device. @fp
170  *	is a pointer to an array of flag bytes which indicate whether a
171  *	character was received with a parity error, etc. @fp may be %NULL to
172  *	indicate all data received is %TTY_NORMAL.
173  *
174  *	Optional.
175  *
176  * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)``
177  *
178  *	This function is called by the low-level tty driver to signal that line
179  *	discpline should try to send more characters to the low-level driver
180  *	for transmission. If the line discpline does not have any more data to
181  *	send, it can just return. If the line discipline does have some data to
182  *	send, please arise a tasklet or workqueue to do the real data transfer.
183  *	Do not send data in this hook, it may lead to a deadlock.
184  *
185  *	Optional.
186  *
187  * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, bool active)``
188  *
189  *	Tells the discipline that the DCD pin has changed its status. Used
190  *	exclusively by the %N_PPS (Pulse-Per-Second) line discipline.
191  *
192  *	Optional.
193  *
194  * @receive_buf2: [DRV] ``ssize_t ()(struct tty_struct *tty, const u8 *cp,
195  *			const u8 *fp, size_t count)``
196  *
197  *	This function is called by the low-level tty driver to send characters
198  *	received by the hardware to the line discpline for processing. @cp is a
199  *	pointer to the buffer of input character received by the device.  @fp
200  *	is a pointer to an array of flag bytes which indicate whether a
201  *	character was received with a parity error, etc. @fp may be %NULL to
202  *	indicate all data received is %TTY_NORMAL. If assigned, prefer this
203  *	function for automatic flow control.
204  *
205  *	Optional.
206  *
207  * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp,
208  *			 const u8 *fp, size_t count)``
209  *
210  *	This function is called by the low-level tty driver for characters
211  *	not eaten by ->receive_buf() or ->receive_buf2(). It is useful for
212  *	processing high-priority characters such as software flow-control
213  *	characters that could otherwise get stuck into the intermediate
214  *	buffer until tty has room to receive them. Ldisc must be able to
215  *	handle later a ->receive_buf() or ->receive_buf2() call for the
216  *	same characters (e.g. by skipping the actions for high-priority
217  *	characters already handled by ->lookahead_buf()).
218  *
219  *	Optional.
220  *
221  * @owner: module containting this ldisc (for reference counting)
222  *
223  * This structure defines the interface between the tty line discipline
224  * implementation and the tty routines. The above routines can be defined.
225  * Unless noted otherwise, they are optional, and can be filled in with a %NULL
226  * pointer.
227  *
228  * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
229  * tty_driver side.
230  */
231 struct tty_ldisc_ops {
232 	char	*name;
233 	int	num;
234 
235 	/*
236 	 * The following routines are called from above.
237 	 */
238 	int	(*open)(struct tty_struct *tty);
239 	void	(*close)(struct tty_struct *tty);
240 	void	(*flush_buffer)(struct tty_struct *tty);
241 	ssize_t	(*read)(struct tty_struct *tty, struct file *file, u8 *buf,
242 			size_t nr, void **cookie, unsigned long offset);
243 	ssize_t	(*write)(struct tty_struct *tty, struct file *file,
244 			 const u8 *buf, size_t nr);
245 	int	(*ioctl)(struct tty_struct *tty, unsigned int cmd,
246 			unsigned long arg);
247 	int	(*compat_ioctl)(struct tty_struct *tty, unsigned int cmd,
248 			unsigned long arg);
249 	void	(*set_termios)(struct tty_struct *tty, const struct ktermios *old);
250 	__poll_t (*poll)(struct tty_struct *tty, struct file *file,
251 			     struct poll_table_struct *wait);
252 	void	(*hangup)(struct tty_struct *tty);
253 
254 	/*
255 	 * The following routines are called from below.
256 	 */
257 	void	(*receive_buf)(struct tty_struct *tty, const u8 *cp,
258 			       const u8 *fp, size_t count);
259 	void	(*write_wakeup)(struct tty_struct *tty);
260 	void	(*dcd_change)(struct tty_struct *tty, bool active);
261 	size_t	(*receive_buf2)(struct tty_struct *tty, const u8 *cp,
262 				const u8 *fp, size_t count);
263 	void	(*lookahead_buf)(struct tty_struct *tty, const u8 *cp,
264 				 const u8 *fp, size_t count);
265 
266 	struct  module *owner;
267 };
268 
269 struct tty_ldisc {
270 	struct tty_ldisc_ops *ops;
271 	struct tty_struct *tty;
272 };
273 
274 #define MODULE_ALIAS_LDISC(ldisc) \
275 	MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
276 
277 extern const struct seq_operations tty_ldiscs_seq_ops;
278 
279 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
280 void tty_ldisc_deref(struct tty_ldisc *);
281 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
282 
283 void tty_ldisc_flush(struct tty_struct *tty);
284 
285 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc);
286 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc);
287 int tty_set_ldisc(struct tty_struct *tty, int disc);
288 
289 #endif /* _LINUX_TTY_LDISC_H */
290