1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TTY_LDISC_H
3#define _LINUX_TTY_LDISC_H
4
5struct 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 */
17struct 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
28void __init_ldsem(struct ld_semaphore *sem, const char *name,
29 struct lock_class_key *key);
30
31#define init_ldsem(sem) \
32do { \
33 static struct lock_class_key __key; \
34 \
35 __init_ldsem((sem), #sem, &__key); \
36} while (0)
37
38
39int ldsem_down_read(struct ld_semaphore *sem, long timeout);
40int ldsem_down_read_trylock(struct ld_semaphore *sem);
41int ldsem_down_write(struct ld_semaphore *sem, long timeout);
42int ldsem_down_write_trylock(struct ld_semaphore *sem);
43void ldsem_up_read(struct ld_semaphore *sem);
44void ldsem_up_write(struct ld_semaphore *sem);
45
46#ifdef CONFIG_DEBUG_LOCK_ALLOC
47int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass,
48 long timeout);
49int 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 * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty,
190 * const unsigned char *cp, const char *fp, int count)``
191 *
192 * This function is called by the low-level tty driver for characters
193 * not eaten by ->receive_buf() or ->receive_buf2(). It is useful for
194 * processing high-priority characters such as software flow-control
195 * characters that could otherwise get stuck into the intermediate
196 * buffer until tty has room to receive them. Ldisc must be able to
197 * handle later a ->receive_buf() or ->receive_buf2() call for the
198 * same characters (e.g. by skipping the actions for high-priority
199 * characters already handled by ->lookahead_buf()).
200 *
201 * @owner: module containting this ldisc (for reference counting)
202 *
203 * This structure defines the interface between the tty line discipline
204 * implementation and the tty routines. The above routines can be defined.
205 * Unless noted otherwise, they are optional, and can be filled in with a %NULL
206 * pointer.
207 *
208 * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
209 * tty_driver side.
210 */
211struct tty_ldisc_ops {
212 char *name;
213 int num;
214
215 /*
216 * The following routines are called from above.
217 */
218 int (*open)(struct tty_struct *tty);
219 void (*close)(struct tty_struct *tty);
220 void (*flush_buffer)(struct tty_struct *tty);
221 ssize_t (*read)(struct tty_struct *tty, struct file *file,
222 unsigned char *buf, size_t nr,
223 void **cookie, unsigned long offset);
224 ssize_t (*write)(struct tty_struct *tty, struct file *file,
225 const unsigned char *buf, size_t nr);
226 int (*ioctl)(struct tty_struct *tty, unsigned int cmd,
227 unsigned long arg);
228 int (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd,
229 unsigned long arg);
230 void (*set_termios)(struct tty_struct *tty, struct ktermios *old);
231 __poll_t (*poll)(struct tty_struct *tty, struct file *file,
232 struct poll_table_struct *wait);
233 void (*hangup)(struct tty_struct *tty);
234
235 /*
236 * The following routines are called from below.
237 */
238 void (*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
239 const char *fp, int count);
240 void (*write_wakeup)(struct tty_struct *tty);
241 void (*dcd_change)(struct tty_struct *tty, unsigned int status);
242 int (*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
243 const char *fp, int count);
244 void (*lookahead_buf)(struct tty_struct *tty, const unsigned char *cp,
245 const unsigned char *fp, unsigned int count);
246
247 struct module *owner;
248};
249
250struct tty_ldisc {
251 struct tty_ldisc_ops *ops;
252 struct tty_struct *tty;
253};
254
255#define MODULE_ALIAS_LDISC(ldisc) \
256 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
257
258extern const struct seq_operations tty_ldiscs_seq_ops;
259
260struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
261void tty_ldisc_deref(struct tty_ldisc *);
262struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
263
264void tty_ldisc_flush(struct tty_struct *tty);
265
266int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc);
267void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc);
268int tty_set_ldisc(struct tty_struct *tty, int disc);
269
270#endif /* _LINUX_TTY_LDISC_H */
271

source code of linux/include/linux/tty_ldisc.h