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  *	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  *	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  * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
92  *		unsigned char *buf, size_t nr)``
93  *
94  *	This function is called when the user requests to read from the @tty.
95  *	The line discipline will return whatever characters it has buffered up
96  *	for the user. If this function is not defined, the user will receive
97  *	an %EIO error. Multiple read calls may occur in parallel and the ldisc
98  *	must deal with serialization issues.
99  *
100  *	Can sleep.
101  *
102  * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
103  *		const unsigned char *buf, size_t nr)``
104  *
105  *	This function is called when the user requests to write to the @tty.
106  *	The line discipline will deliver the characters to the low-level tty
107  *	device for transmission, optionally performing some processing on the
108  *	characters first. If this function is not defined, the user will
109  *	receive an %EIO error.
110  *
111  *	Can sleep.
112  *
113  * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
114  *		unsigned long arg)``
115  *
116  *	This function is called when the user requests an ioctl which is not
117  *	handled by the tty layer or the low-level tty driver. It is intended
118  *	for ioctls which affect line discpline operation.  Note that the search
119  *	order for ioctls is (1) tty layer, (2) tty low-level driver, (3) line
120  *	discpline. So a low-level driver can "grab" an ioctl request before
121  *	the line discpline has a chance to see it.
122  *
123  * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
124  *		unsigned long arg)``
125  *
126  *	Process ioctl calls from 32-bit process on 64-bit system.
127  *
128  *	Note that only ioctls that are neither "pointer to compatible
129  *	structure" nor tty-generic.  Something private that takes an integer or
130  *	a pointer to wordsize-sensitive structure belongs here, but most of
131  *	ldiscs will happily leave it %NULL.
132  *
133  * @set_termios: [TTY] ``void ()(struct tty_struct *tty, struct ktermios *old)``
134  *
135  *	This function notifies the line discpline that a change has been made
136  *	to the termios structure.
137  *
138  * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file,
139  *		  struct poll_table_struct *wait)``
140  *
141  *	This function is called when a user attempts to select/poll on a @tty
142  *	device. It is solely the responsibility of the line discipline to
143  *	handle poll requests.
144  *
145  * @hangup: [TTY] ``void ()(struct tty_struct *tty)``
146  *
147  *	Called on a hangup. Tells the discipline that it should cease I/O to
148  *	the tty driver. The driver should seek to perform this action quickly
149  *	but should wait until any pending driver I/O is completed. No further
150  *	calls into the ldisc code will occur.
151  *
152  *	Can sleep.
153  *
154  * @receive_buf: [DRV] ``void ()(struct tty_struct *tty,
155  *		       const unsigned char *cp, const char *fp, int count)``
156  *
157  *	This function is called by the low-level tty driver to send characters
158  *	received by the hardware to the line discpline for processing. @cp is
159  *	a pointer to the buffer of input character received by the device. @fp
160  *	is a pointer to an array of flag bytes which indicate whether a
161  *	character was received with a parity error, etc. @fp may be %NULL to
162  *	indicate all data received is %TTY_NORMAL.
163  *
164  * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)``
165  *
166  *	This function is called by the low-level tty driver to signal that line
167  *	discpline should try to send more characters to the low-level driver
168  *	for transmission. If the line discpline does not have any more data to
169  *	send, it can just return. If the line discipline does have some data to
170  *	send, please arise a tasklet or workqueue to do the real data transfer.
171  *	Do not send data in this hook, it may lead to a deadlock.
172  *
173  * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, unsigned int status)``
174  *
175  *	Tells the discipline that the DCD pin has changed its status. Used
176  *	exclusively by the %N_PPS (Pulse-Per-Second) line discipline.
177  *
178  * @receive_buf2: [DRV] ``int ()(struct tty_struct *tty,
179  *			const unsigned char *cp, const char *fp, int count)``
180  *
181  *	This function is called by the low-level tty driver to send characters
182  *	received by the hardware to the line discpline for processing. @cp is a
183  *	pointer to the buffer of input character received by the device.  @fp
184  *	is a pointer to an array of flag bytes which indicate whether a
185  *	character was received with a parity error, etc. @fp may be %NULL to
186  *	indicate all data received is %TTY_NORMAL. If assigned, prefer this
187  *	function for automatic flow control.
188  *
189  * @owner: module containting this ldisc (for reference counting)
190  *
191  * This structure defines the interface between the tty line discipline
192  * implementation and the tty routines. The above routines can be defined.
193  * Unless noted otherwise, they are optional, and can be filled in with a %NULL
194  * pointer.
195  *
196  * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
197  * tty_driver side.
198  */
199 struct tty_ldisc_ops {
200 	char	*name;
201 	int	num;
202 
203 	/*
204 	 * The following routines are called from above.
205 	 */
206 	int	(*open)(struct tty_struct *tty);
207 	void	(*close)(struct tty_struct *tty);
208 	void	(*flush_buffer)(struct tty_struct *tty);
209 	ssize_t	(*read)(struct tty_struct *tty, struct file *file,
210 			unsigned char *buf, size_t nr,
211 			void **cookie, unsigned long offset);
212 	ssize_t	(*write)(struct tty_struct *tty, struct file *file,
213 			 const unsigned char *buf, size_t nr);
214 	int	(*ioctl)(struct tty_struct *tty, unsigned int cmd,
215 			unsigned long arg);
216 	int	(*compat_ioctl)(struct tty_struct *tty, unsigned int cmd,
217 			unsigned long arg);
218 	void	(*set_termios)(struct tty_struct *tty, struct ktermios *old);
219 	__poll_t (*poll)(struct tty_struct *tty, struct file *file,
220 			     struct poll_table_struct *wait);
221 	void	(*hangup)(struct tty_struct *tty);
222 
223 	/*
224 	 * The following routines are called from below.
225 	 */
226 	void	(*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
227 			       const char *fp, int count);
228 	void	(*write_wakeup)(struct tty_struct *tty);
229 	void	(*dcd_change)(struct tty_struct *tty, unsigned int status);
230 	int	(*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
231 				const char *fp, int count);
232 
233 	struct  module *owner;
234 };
235 
236 struct tty_ldisc {
237 	struct tty_ldisc_ops *ops;
238 	struct tty_struct *tty;
239 };
240 
241 #define MODULE_ALIAS_LDISC(ldisc) \
242 	MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
243 
244 extern const struct seq_operations tty_ldiscs_seq_ops;
245 
246 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
247 void tty_ldisc_deref(struct tty_ldisc *);
248 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
249 
250 void tty_ldisc_flush(struct tty_struct *tty);
251 
252 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc);
253 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc);
254 int tty_set_ldisc(struct tty_struct *tty, int disc);
255 
256 #endif /* _LINUX_TTY_LDISC_H */
257