Merge tag 'perf-tools-fixes-for-v6.9-2024-04-19' of git://git.kernel.org/pub/scm...
[linux-2.6-block.git] / include / linux / tty_ldisc.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_TTY_LDISC_H
3#define _LINUX_TTY_LDISC_H
4
a24bc667
JS
5struct tty_struct;
6
1da177e4
LT
7#include <linux/fs.h>
8#include <linux/wait.h>
5fd691af 9#include <linux/atomic.h>
abca9901
JS
10#include <linux/list.h>
11#include <linux/lockdep.h>
a24bc667 12#include <linux/seq_file.h>
4898e640
PH
13
14/*
15 * the semaphore definition
16 */
17struct ld_semaphore {
5fd691af 18 atomic_long_t count;
4898e640
PH
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
78941934 28void __init_ldsem(struct ld_semaphore *sem, const char *name,
4898e640
PH
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
78941934
JS
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);
4898e640
PH
45
46#ifdef CONFIG_DEBUG_LOCK_ALLOC
78941934
JS
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);
4898e640
PH
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
0c6119f9
JS
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 *
40f4268c 64 * @open: [TTY] ``int ()(struct tty_struct *tty)``
0c6119f9
JS
65 *
66 * This function is called when the line discipline is associated with the
40f4268c
JS
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.
0c6119f9 73 *
abb05ac9 74 * Optional. Can sleep.
40f4268c
JS
75 *
76 * @close: [TTY] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
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
40f4268c
JS
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 *
abb05ac9 83 * Optional. Can sleep.
0c6119f9 84 *
40f4268c 85 * @flush_buffer: [TTY] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
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
40f4268c 89 * process. It may be called at any point between open and close.
0c6119f9 90 *
abb05ac9
JSS
91 * Optional.
92 *
49b8220c
JSS
93 * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file, u8 *buf,
94 * size_t nr)``
0c6119f9
JS
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
40f4268c
JS
99 * an %EIO error. Multiple read calls may occur in parallel and the ldisc
100 * must deal with serialization issues.
101 *
abb05ac9 102 * Optional: %EIO unless provided. Can sleep.
0c6119f9 103 *
40f4268c 104 * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
49b8220c 105 * const u8 *buf, size_t nr)``
0c6119f9
JS
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 *
abb05ac9 113 * Optional: %EIO unless provided. Can sleep.
40f4268c
JS
114 *
115 * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
0c6119f9
JS
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 *
abb05ac9
JSS
125 * Optional.
126 *
40f4268c 127 * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
0c6119f9
JS
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 *
abb05ac9
JSS
137 * Optional.
138 *
8b7d2d95 139 * @set_termios: [TTY] ``void ()(struct tty_struct *tty, const struct ktermios *old)``
0c6119f9
JS
140 *
141 * This function notifies the line discpline that a change has been made
142 * to the termios structure.
143 *
abb05ac9
JSS
144 * Optional.
145 *
40f4268c 146 * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file,
0c6119f9
JS
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 *
abb05ac9
JSS
153 * Optional.
154 *
40f4268c 155 * @hangup: [TTY] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
156 *
157 * Called on a hangup. Tells the discipline that it should cease I/O to
40f4268c
JS
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 *
abb05ac9 162 * Optional. Can sleep.
0c6119f9 163 *
a8d9cd23 164 * @receive_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp,
892bc209 165 * const u8 *fp, size_t count)``
0c6119f9
JS
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 *
abb05ac9
JSS
174 * Optional.
175 *
40f4268c 176 * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
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 *
abb05ac9
JSS
185 * Optional.
186 *
0388a152 187 * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, bool active)``
0c6119f9
JS
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 *
abb05ac9
JSS
192 * Optional.
193 *
a8d9cd23 194 * @receive_buf2: [DRV] ``ssize_t ()(struct tty_struct *tty, const u8 *cp,
892bc209 195 * const u8 *fp, size_t count)``
0c6119f9
JS
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 *
abb05ac9
JSS
205 * Optional.
206 *
a8d9cd23 207 * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp,
892bc209 208 * const u8 *fp, size_t count)``
6bb6fa69
IJ
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 *
abb05ac9
JSS
219 * Optional.
220 *
0c6119f9
JS
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.
40f4268c
JS
227 *
228 * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
229 * tty_driver side.
0c6119f9 230 */
a352def2 231struct tty_ldisc_ops {
1da177e4
LT
232 char *name;
233 int num;
6be06e72 234
1da177e4
LT
235 /*
236 * The following routines are called from above.
237 */
0c6119f9
JS
238 int (*open)(struct tty_struct *tty);
239 void (*close)(struct tty_struct *tty);
1da177e4 240 void (*flush_buffer)(struct tty_struct *tty);
49b8220c
JSS
241 ssize_t (*read)(struct tty_struct *tty, struct file *file, u8 *buf,
242 size_t nr, void **cookie, unsigned long offset);
6be06e72 243 ssize_t (*write)(struct tty_struct *tty, struct file *file,
49b8220c 244 const u8 *buf, size_t nr);
d78328bc
JS
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);
8b7d2d95 249 void (*set_termios)(struct tty_struct *tty, const struct ktermios *old);
0c6119f9
JS
250 __poll_t (*poll)(struct tty_struct *tty, struct file *file,
251 struct poll_table_struct *wait);
28f194da 252 void (*hangup)(struct tty_struct *tty);
6be06e72 253
1da177e4
LT
254 /*
255 * The following routines are called from below.
256 */
a8d9cd23 257 void (*receive_buf)(struct tty_struct *tty, const u8 *cp,
892bc209 258 const u8 *fp, size_t count);
0c6119f9 259 void (*write_wakeup)(struct tty_struct *tty);
0388a152 260 void (*dcd_change)(struct tty_struct *tty, bool active);
a8d9cd23 261 size_t (*receive_buf2)(struct tty_struct *tty, const u8 *cp,
892bc209 262 const u8 *fp, size_t count);
a8d9cd23 263 void (*lookahead_buf)(struct tty_struct *tty, const u8 *cp,
892bc209 264 const u8 *fp, size_t count);
1da177e4
LT
265
266 struct module *owner;
1da177e4
LT
267};
268
a352def2
AC
269struct tty_ldisc {
270 struct tty_ldisc_ops *ops;
36697529 271 struct tty_struct *tty;
a352def2
AC
272};
273
1da177e4
LT
274#define MODULE_ALIAS_LDISC(ldisc) \
275 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
276
a24bc667
JS
277extern const struct seq_operations tty_ldiscs_seq_ops;
278
279struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
280void tty_ldisc_deref(struct tty_ldisc *);
281struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
282
283void tty_ldisc_flush(struct tty_struct *tty);
284
285int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc);
286void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc);
287int tty_set_ldisc(struct tty_struct *tty, int disc);
288
1da177e4 289#endif /* _LINUX_TTY_LDISC_H */