Commit | Line | Data |
---|---|---|
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 |
5 | struct tty_struct; |
6 | ||
1da177e4 LT |
7 | /* |
8 | * This structure defines the interface between the tty line discipline | |
9 | * implementation and the tty routines. The following routines can be | |
10 | * defined; unless noted otherwise, they are optional, and can be | |
11 | * filled in with a null pointer. | |
12 | * | |
13 | * int (*open)(struct tty_struct *); | |
14 | * | |
6be06e72 PH |
15 | * This function is called when the line discipline is associated |
16 | * with the tty. The line discipline can use this as an | |
17 | * opportunity to initialize any state needed by the ldisc routines. | |
18 | * | |
1da177e4 LT |
19 | * void (*close)(struct tty_struct *); |
20 | * | |
21 | * This function is called when the line discipline is being | |
6be06e72 PH |
22 | * shutdown, either because the tty is being closed or because |
23 | * the tty is being changed to use a new line discipline | |
24 | * | |
1da177e4 LT |
25 | * void (*flush_buffer)(struct tty_struct *tty); |
26 | * | |
6be06e72 PH |
27 | * This function instructs the line discipline to clear its |
28 | * buffers of any input characters it may have queued to be | |
29 | * delivered to the user mode process. | |
30 | * | |
1da177e4 LT |
31 | * ssize_t (*read)(struct tty_struct * tty, struct file * file, |
32 | * unsigned char * buf, size_t nr); | |
33 | * | |
6be06e72 PH |
34 | * This function is called when the user requests to read from |
35 | * the tty. The line discipline will return whatever characters | |
36 | * it has buffered up for the user. If this function is not | |
37 | * defined, the user will receive an EIO error. | |
38 | * | |
1da177e4 | 39 | * ssize_t (*write)(struct tty_struct * tty, struct file * file, |
6be06e72 PH |
40 | * const unsigned char * buf, size_t nr); |
41 | * | |
42 | * This function is called when the user requests to write to the | |
43 | * tty. The line discipline will deliver the characters to the | |
44 | * low-level tty device for transmission, optionally performing | |
45 | * some processing on the characters first. If this function is | |
46 | * not defined, the user will receive an EIO error. | |
47 | * | |
1da177e4 | 48 | * int (*ioctl)(struct tty_struct * tty, struct file * file, |
6be06e72 | 49 | * unsigned int cmd, unsigned long arg); |
1da177e4 LT |
50 | * |
51 | * This function is called when the user requests an ioctl which | |
6be06e72 PH |
52 | * is not handled by the tty layer or the low-level tty driver. |
53 | * It is intended for ioctls which affect line discpline | |
54 | * operation. Note that the search order for ioctls is (1) tty | |
55 | * layer, (2) tty low-level driver, (3) line discpline. So a | |
56 | * low-level driver can "grab" an ioctl request before the line | |
57 | * discpline has a chance to see it. | |
58 | * | |
f0193d3e | 59 | * int (*compat_ioctl)(struct tty_struct * tty, struct file * file, |
6be06e72 | 60 | * unsigned int cmd, unsigned long arg); |
e10cc1df | 61 | * |
6be06e72 | 62 | * Process ioctl calls from 32-bit process on 64-bit system |
e10cc1df | 63 | * |
f0193d3e AV |
64 | * NOTE: only ioctls that are neither "pointer to compatible |
65 | * structure" nor tty-generic. Something private that takes | |
66 | * an integer or a pointer to wordsize-sensitive structure | |
67 | * belongs here, but most of ldiscs will happily leave | |
68 | * it NULL. | |
69 | * | |
edc6afc5 | 70 | * void (*set_termios)(struct tty_struct *tty, struct ktermios * old); |
1da177e4 | 71 | * |
6be06e72 PH |
72 | * This function notifies the line discpline that a change has |
73 | * been made to the termios structure. | |
74 | * | |
1da177e4 | 75 | * int (*poll)(struct tty_struct * tty, struct file * file, |
6be06e72 | 76 | * poll_table *wait); |
1da177e4 | 77 | * |
6be06e72 PH |
78 | * This function is called when a user attempts to select/poll on a |
79 | * tty device. It is solely the responsibility of the line | |
80 | * discipline to handle poll requests. | |
1da177e4 | 81 | * |
55db4c64 | 82 | * void (*receive_buf)(struct tty_struct *, const unsigned char *cp, |
6be06e72 PH |
83 | * char *fp, int count); |
84 | * | |
85 | * This function is called by the low-level tty driver to send | |
86 | * characters received by the hardware to the line discpline for | |
87 | * processing. <cp> is a pointer to the buffer of input | |
88 | * character received by the device. <fp> is a pointer to a | |
89 | * pointer of flag bytes which indicate whether a character was | |
82f91fe0 PH |
90 | * received with a parity error, etc. <fp> may be NULL to indicate |
91 | * all data received is TTY_NORMAL. | |
6be06e72 | 92 | * |
1da177e4 LT |
93 | * void (*write_wakeup)(struct tty_struct *); |
94 | * | |
6be06e72 PH |
95 | * This function is called by the low-level tty driver to signal |
96 | * that line discpline should try to send more characters to the | |
97 | * low-level driver for transmission. If the line discpline does | |
879eb9c3 HS |
98 | * not have any more data to send, it can just return. If the line |
99 | * discipline does have some data to send, please arise a tasklet | |
100 | * or workqueue to do the real data transfer. Do not send data in | |
101 | * this hook, it may leads to a deadlock. | |
1da177e4 LT |
102 | * |
103 | * int (*hangup)(struct tty_struct *) | |
104 | * | |
105 | * Called on a hangup. Tells the discipline that it should | |
106 | * cease I/O to the tty driver. Can sleep. The driver should | |
107 | * seek to perform this action quickly but should wait until | |
108 | * any pending driver I/O is completed. | |
b3e63afe | 109 | * |
593fb1ae | 110 | * void (*dcd_change)(struct tty_struct *tty, unsigned int status) |
b3e63afe | 111 | * |
593fb1ae GS |
112 | * Tells the discipline that the DCD pin has changed its status. |
113 | * Used exclusively by the N_PPS (Pulse-Per-Second) line discipline. | |
24a89d1c PH |
114 | * |
115 | * int (*receive_buf2)(struct tty_struct *, const unsigned char *cp, | |
116 | * char *fp, int count); | |
117 | * | |
118 | * This function is called by the low-level tty driver to send | |
119 | * characters received by the hardware to the line discpline for | |
120 | * processing. <cp> is a pointer to the buffer of input | |
121 | * character received by the device. <fp> is a pointer to a | |
122 | * pointer of flag bytes which indicate whether a character was | |
82f91fe0 PH |
123 | * received with a parity error, etc. <fp> may be NULL to indicate |
124 | * all data received is TTY_NORMAL. | |
24a89d1c | 125 | * If assigned, prefer this function for automatic flow control. |
1da177e4 LT |
126 | */ |
127 | ||
128 | #include <linux/fs.h> | |
129 | #include <linux/wait.h> | |
5fd691af | 130 | #include <linux/atomic.h> |
abca9901 JS |
131 | #include <linux/list.h> |
132 | #include <linux/lockdep.h> | |
a24bc667 | 133 | #include <linux/seq_file.h> |
4898e640 PH |
134 | |
135 | /* | |
136 | * the semaphore definition | |
137 | */ | |
138 | struct ld_semaphore { | |
5fd691af | 139 | atomic_long_t count; |
4898e640 PH |
140 | raw_spinlock_t wait_lock; |
141 | unsigned int wait_readers; | |
142 | struct list_head read_wait; | |
143 | struct list_head write_wait; | |
144 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
145 | struct lockdep_map dep_map; | |
146 | #endif | |
147 | }; | |
148 | ||
78941934 | 149 | void __init_ldsem(struct ld_semaphore *sem, const char *name, |
4898e640 PH |
150 | struct lock_class_key *key); |
151 | ||
152 | #define init_ldsem(sem) \ | |
153 | do { \ | |
154 | static struct lock_class_key __key; \ | |
155 | \ | |
156 | __init_ldsem((sem), #sem, &__key); \ | |
157 | } while (0) | |
158 | ||
159 | ||
78941934 JS |
160 | int ldsem_down_read(struct ld_semaphore *sem, long timeout); |
161 | int ldsem_down_read_trylock(struct ld_semaphore *sem); | |
162 | int ldsem_down_write(struct ld_semaphore *sem, long timeout); | |
163 | int ldsem_down_write_trylock(struct ld_semaphore *sem); | |
164 | void ldsem_up_read(struct ld_semaphore *sem); | |
165 | void ldsem_up_write(struct ld_semaphore *sem); | |
4898e640 PH |
166 | |
167 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
78941934 JS |
168 | int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, |
169 | long timeout); | |
170 | int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass, | |
171 | long timeout); | |
4898e640 PH |
172 | #else |
173 | # define ldsem_down_read_nested(sem, subclass, timeout) \ | |
174 | ldsem_down_read(sem, timeout) | |
175 | # define ldsem_down_write_nested(sem, subclass, timeout) \ | |
176 | ldsem_down_write(sem, timeout) | |
177 | #endif | |
178 | ||
179 | ||
a352def2 | 180 | struct tty_ldisc_ops { |
1da177e4 LT |
181 | char *name; |
182 | int num; | |
6be06e72 | 183 | |
1da177e4 LT |
184 | /* |
185 | * The following routines are called from above. | |
186 | */ | |
187 | int (*open)(struct tty_struct *); | |
188 | void (*close)(struct tty_struct *); | |
189 | void (*flush_buffer)(struct tty_struct *tty); | |
6be06e72 | 190 | ssize_t (*read)(struct tty_struct *tty, struct file *file, |
3b830a9c LT |
191 | unsigned char *buf, size_t nr, |
192 | void **cookie, unsigned long offset); | |
6be06e72 PH |
193 | ssize_t (*write)(struct tty_struct *tty, struct file *file, |
194 | const unsigned char *buf, size_t nr); | |
195 | int (*ioctl)(struct tty_struct *tty, struct file *file, | |
1da177e4 | 196 | unsigned int cmd, unsigned long arg); |
f0193d3e | 197 | int (*compat_ioctl)(struct tty_struct *tty, struct file *file, |
e10cc1df | 198 | unsigned int cmd, unsigned long arg); |
6be06e72 | 199 | void (*set_termios)(struct tty_struct *tty, struct ktermios *old); |
a3f8683b | 200 | __poll_t (*poll)(struct tty_struct *, struct file *, |
1da177e4 | 201 | struct poll_table_struct *); |
28f194da | 202 | void (*hangup)(struct tty_struct *tty); |
6be06e72 | 203 | |
1da177e4 LT |
204 | /* |
205 | * The following routines are called from below. | |
206 | */ | |
55db4c64 | 207 | void (*receive_buf)(struct tty_struct *, const unsigned char *cp, |
0f3dcf3b | 208 | const char *fp, int count); |
1da177e4 | 209 | void (*write_wakeup)(struct tty_struct *); |
593fb1ae | 210 | void (*dcd_change)(struct tty_struct *, unsigned int); |
24a89d1c | 211 | int (*receive_buf2)(struct tty_struct *, const unsigned char *cp, |
0f3dcf3b | 212 | const char *fp, int count); |
1da177e4 LT |
213 | |
214 | struct module *owner; | |
1da177e4 LT |
215 | }; |
216 | ||
a352def2 AC |
217 | struct tty_ldisc { |
218 | struct tty_ldisc_ops *ops; | |
36697529 | 219 | struct tty_struct *tty; |
a352def2 AC |
220 | }; |
221 | ||
1da177e4 LT |
222 | #define MODULE_ALIAS_LDISC(ldisc) \ |
223 | MODULE_ALIAS("tty-ldisc-" __stringify(ldisc)) | |
224 | ||
a24bc667 JS |
225 | extern const struct seq_operations tty_ldiscs_seq_ops; |
226 | ||
227 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *); | |
228 | void tty_ldisc_deref(struct tty_ldisc *); | |
229 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *); | |
230 | ||
231 | void tty_ldisc_flush(struct tty_struct *tty); | |
232 | ||
233 | int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc); | |
234 | void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc); | |
235 | int tty_set_ldisc(struct tty_struct *tty, int disc); | |
236 | ||
1da177e4 | 237 | #endif /* _LINUX_TTY_LDISC_H */ |