Merge tag 'xfs-6.4-rc1-fixes' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-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 *
40f4268c
JS
74 * Can sleep.
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 *
83 * 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 *
40f4268c 91 * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
0c6119f9
JS
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
40f4268c
JS
97 * an %EIO error. Multiple read calls may occur in parallel and the ldisc
98 * must deal with serialization issues.
99 *
100 * Can sleep.
0c6119f9 101 *
40f4268c 102 * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
0c6119f9
JS
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 *
40f4268c
JS
111 * Can sleep.
112 *
113 * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
0c6119f9
JS
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 *
40f4268c 123 * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
0c6119f9
JS
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 *
8b7d2d95 133 * @set_termios: [TTY] ``void ()(struct tty_struct *tty, const struct ktermios *old)``
0c6119f9
JS
134 *
135 * This function notifies the line discpline that a change has been made
136 * to the termios structure.
137 *
40f4268c 138 * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file,
0c6119f9
JS
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 *
40f4268c 145 * @hangup: [TTY] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
146 *
147 * Called on a hangup. Tells the discipline that it should cease I/O to
40f4268c
JS
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.
0c6119f9 153 *
40f4268c
JS
154 * @receive_buf: [DRV] ``void ()(struct tty_struct *tty,
155 * const unsigned char *cp, const char *fp, int count)``
0c6119f9
JS
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 *
40f4268c 164 * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)``
0c6119f9
JS
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 *
0388a152 173 * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, bool active)``
0c6119f9
JS
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 *
40f4268c
JS
178 * @receive_buf2: [DRV] ``int ()(struct tty_struct *tty,
179 * const unsigned char *cp, const char *fp, int count)``
0c6119f9
JS
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 *
6bb6fa69 189 * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty,
ab24a01b 190 * const unsigned char *cp, const char *fp, int count)``
6bb6fa69
IJ
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 *
0c6119f9
JS
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.
40f4268c
JS
207 *
208 * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
209 * tty_driver side.
0c6119f9 210 */
a352def2 211struct tty_ldisc_ops {
1da177e4
LT
212 char *name;
213 int num;
6be06e72 214
1da177e4
LT
215 /*
216 * The following routines are called from above.
217 */
0c6119f9
JS
218 int (*open)(struct tty_struct *tty);
219 void (*close)(struct tty_struct *tty);
1da177e4 220 void (*flush_buffer)(struct tty_struct *tty);
6be06e72 221 ssize_t (*read)(struct tty_struct *tty, struct file *file,
3b830a9c
LT
222 unsigned char *buf, size_t nr,
223 void **cookie, unsigned long offset);
6be06e72
PH
224 ssize_t (*write)(struct tty_struct *tty, struct file *file,
225 const unsigned char *buf, size_t nr);
d78328bc
JS
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);
8b7d2d95 230 void (*set_termios)(struct tty_struct *tty, const struct ktermios *old);
0c6119f9
JS
231 __poll_t (*poll)(struct tty_struct *tty, struct file *file,
232 struct poll_table_struct *wait);
28f194da 233 void (*hangup)(struct tty_struct *tty);
6be06e72 234
1da177e4
LT
235 /*
236 * The following routines are called from below.
237 */
0c6119f9 238 void (*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
0f3dcf3b 239 const char *fp, int count);
0c6119f9 240 void (*write_wakeup)(struct tty_struct *tty);
0388a152 241 void (*dcd_change)(struct tty_struct *tty, bool active);
0c6119f9 242 int (*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
0f3dcf3b 243 const char *fp, int count);
6bb6fa69
IJ
244 void (*lookahead_buf)(struct tty_struct *tty, const unsigned char *cp,
245 const unsigned char *fp, unsigned int count);
1da177e4
LT
246
247 struct module *owner;
1da177e4
LT
248};
249
a352def2
AC
250struct tty_ldisc {
251 struct tty_ldisc_ops *ops;
36697529 252 struct tty_struct *tty;
a352def2
AC
253};
254
1da177e4
LT
255#define MODULE_ALIAS_LDISC(ldisc) \
256 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
257
a24bc667
JS
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
1da177e4 270#endif /* _LINUX_TTY_LDISC_H */