Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-1.0+ |
1da177e4 LT |
2 | /* |
3 | * n_tty.c --- implements the N_TTY line discipline. | |
4edf1827 | 4 | * |
1da177e4 LT |
5 | * This code used to be in tty_io.c, but things are getting hairy |
6 | * enough that it made sense to split things off. (The N_TTY | |
7 | * processing has changed so much that it's hardly recognizable, | |
8 | * anyway...) | |
9 | * | |
10 | * Note that the open routine for N_TTY is guaranteed never to return | |
11 | * an error. This is because Linux will fall back to setting a line | |
4edf1827 | 12 | * to N_TTY if it can not switch to any other line discipline. |
1da177e4 LT |
13 | * |
14 | * Written by Theodore Ts'o, Copyright 1994. | |
4edf1827 | 15 | * |
1da177e4 LT |
16 | * This file also contains code originally written by Linus Torvalds, |
17 | * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. | |
4edf1827 | 18 | * |
1da177e4 LT |
19 | * Reduced memory usage for older ARM systems - Russell King. |
20 | * | |
4edf1827 | 21 | * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of |
1da177e4 LT |
22 | * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> |
23 | * who actually finally proved there really was a race. | |
24 | * | |
25 | * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to | |
26 | * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. | |
11a96d18 | 27 | * Also fixed a bug in BLOCKING mode where n_tty_write returns |
1da177e4 LT |
28 | * EAGAIN |
29 | */ | |
30 | ||
8ed012d1 | 31 | #include <linux/bitmap.h> |
9db1be84 IJ |
32 | #include <linux/bitops.h> |
33 | #include <linux/ctype.h> | |
1da177e4 | 34 | #include <linux/errno.h> |
8ed012d1 | 35 | #include <linux/export.h> |
1da177e4 | 36 | #include <linux/fcntl.h> |
9db1be84 | 37 | #include <linux/file.h> |
8ed012d1 | 38 | #include <linux/jiffies.h> |
7e26c84d | 39 | #include <linux/math.h> |
9db1be84 | 40 | #include <linux/poll.h> |
593fb1ae | 41 | #include <linux/ratelimit.h> |
9db1be84 IJ |
42 | #include <linux/sched.h> |
43 | #include <linux/signal.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/string.h> | |
9db1be84 IJ |
46 | #include <linux/tty.h> |
47 | #include <linux/types.h> | |
48 | #include <linux/uaccess.h> | |
86e35aea | 49 | #include <linux/vmalloc.h> |
9db1be84 | 50 | |
98602c01 | 51 | #include "tty.h" |
1da177e4 | 52 | |
a5db4826 VV |
53 | /* |
54 | * Until this number of characters is queued in the xmit buffer, select will | |
55 | * return "we have room for writes". | |
56 | */ | |
1da177e4 LT |
57 | #define WAKEUP_CHARS 256 |
58 | ||
d2e38e71 JSS |
59 | #define N_TTY_BUF_SIZE 4096 |
60 | ||
1da177e4 LT |
61 | /* |
62 | * This defines the low- and high-watermarks for throttling and | |
63 | * unthrottling the TTY driver. These watermarks are used for | |
64 | * controlling the space in the read buffer. | |
65 | */ | |
66 | #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ | |
bbd20759 | 67 | #define TTY_THRESHOLD_UNTHROTTLE 128 |
1da177e4 | 68 | |
a88a69c9 JP |
69 | /* |
70 | * Special byte codes used in the echo buffer to represent operations | |
71 | * or special handling of characters. Bytes in the echo buffer that | |
72 | * are not part of such special blocks are treated as normal character | |
73 | * codes. | |
74 | */ | |
75 | #define ECHO_OP_START 0xff | |
76 | #define ECHO_OP_MOVE_BACK_COL 0x80 | |
77 | #define ECHO_OP_SET_CANON_COL 0x81 | |
78 | #define ECHO_OP_ERASE_TAB 0x82 | |
79 | ||
cbfd0340 PH |
80 | #define ECHO_COMMIT_WATERMARK 256 |
81 | #define ECHO_BLOCK 256 | |
82 | #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32) | |
83 | ||
70ece7a7 | 84 | struct n_tty_data { |
fb7aa03d PH |
85 | /* producer-published */ |
86 | size_t read_head; | |
70aca71f | 87 | size_t commit_head; |
fb7aa03d | 88 | size_t canon_head; |
9dfd16dd PH |
89 | size_t echo_head; |
90 | size_t echo_commit; | |
1075a6e2 | 91 | size_t echo_mark; |
1bb9d562 | 92 | DECLARE_BITMAP(char_map, 256); |
fb7aa03d PH |
93 | |
94 | /* private to n_tty_receive_overrun (single-threaded) */ | |
53c5ee2c | 95 | unsigned long overrun_time; |
c3b2b26f | 96 | unsigned int num_overrun; |
53c5ee2c | 97 | |
24a89d1c PH |
98 | /* non-atomic */ |
99 | bool no_room; | |
100 | ||
fb7aa03d | 101 | /* must hold exclusive termios_rwsem to reset these */ |
53c5ee2c | 102 | unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; |
4d0ed182 | 103 | unsigned char push:1; |
3fe780b3 | 104 | |
fb7aa03d | 105 | /* shared by producer and consumer */ |
b9b96b20 | 106 | u8 read_buf[N_TTY_BUF_SIZE]; |
3fe780b3 | 107 | DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); |
b9b96b20 | 108 | u8 echo_buf[N_TTY_BUF_SIZE]; |
ba2e68ac | 109 | |
fb7aa03d PH |
110 | /* consumer-published */ |
111 | size_t read_tail; | |
40d5e090 | 112 | size_t line_start; |
fb7aa03d | 113 | |
6bb6fa69 IJ |
114 | /* # of chars looked ahead (to find software flow control chars) */ |
115 | size_t lookahead_count; | |
116 | ||
fb7aa03d PH |
117 | /* protected by output lock */ |
118 | unsigned int column; | |
ba2e68ac | 119 | unsigned int canon_column; |
9dfd16dd | 120 | size_t echo_tail; |
bddc7152 JS |
121 | |
122 | struct mutex atomic_read_lock; | |
123 | struct mutex output_lock; | |
70ece7a7 JS |
124 | }; |
125 | ||
3d63b7e4 TH |
126 | #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1)) |
127 | ||
ce74117a PH |
128 | static inline size_t read_cnt(struct n_tty_data *ldata) |
129 | { | |
a2f73be8 | 130 | return ldata->read_head - ldata->read_tail; |
ce74117a PH |
131 | } |
132 | ||
b9b96b20 | 133 | static inline u8 read_buf(struct n_tty_data *ldata, size_t i) |
bc5a5e3f | 134 | { |
819287f0 | 135 | return ldata->read_buf[MASK(i)]; |
bc5a5e3f PH |
136 | } |
137 | ||
b9b96b20 | 138 | static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i) |
bc5a5e3f | 139 | { |
819287f0 | 140 | return &ldata->read_buf[MASK(i)]; |
bc5a5e3f PH |
141 | } |
142 | ||
b9b96b20 | 143 | static inline u8 echo_buf(struct n_tty_data *ldata, size_t i) |
addaebcc | 144 | { |
ebec3f8f | 145 | smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */ |
819287f0 | 146 | return ldata->echo_buf[MASK(i)]; |
addaebcc PH |
147 | } |
148 | ||
b9b96b20 | 149 | static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i) |
addaebcc | 150 | { |
819287f0 | 151 | return &ldata->echo_buf[MASK(i)]; |
addaebcc PH |
152 | } |
153 | ||
b97b3d9f | 154 | /* If we are not echoing the data, perhaps this is a secret so erase it */ |
f6f847ff | 155 | static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size) |
b97b3d9f | 156 | { |
f6f847ff JS |
157 | if (L_ICANON(tty) && !L_ECHO(tty)) |
158 | memset(buffer, 0, size); | |
b97b3d9f GK |
159 | } |
160 | ||
5bedcf70 JS |
161 | static void tty_copy(const struct tty_struct *tty, void *to, size_t tail, |
162 | size_t n) | |
72586c60 LA |
163 | { |
164 | struct n_tty_data *ldata = tty->disc_data; | |
679e7c29 | 165 | size_t size = N_TTY_BUF_SIZE - tail; |
b97b3d9f | 166 | void *from = read_buf_addr(ldata, tail); |
679e7c29 PH |
167 | |
168 | if (n > size) { | |
309426ae | 169 | tty_audit_add_data(tty, from, size); |
3b830a9c LT |
170 | memcpy(to, from, size); |
171 | zero_buffer(tty, from, size); | |
679e7c29 PH |
172 | to += size; |
173 | n -= size; | |
174 | from = ldata->read_buf; | |
175 | } | |
72586c60 | 176 | |
309426ae | 177 | tty_audit_add_data(tty, from, n); |
3b830a9c LT |
178 | memcpy(to, from, n); |
179 | zero_buffer(tty, from, n); | |
72586c60 LA |
180 | } |
181 | ||
24a89d1c | 182 | /** |
98629663 JS |
183 | * n_tty_kick_worker - start input worker (if required) |
184 | * @tty: terminal | |
24a89d1c | 185 | * |
98629663 | 186 | * Re-schedules the flip buffer work if it may have stopped. |
24a89d1c | 187 | * |
98629663 JS |
188 | * Locking: |
189 | * * Caller holds exclusive %termios_rwsem, or | |
190 | * * n_tty_read()/consumer path: | |
191 | * holds non-exclusive %termios_rwsem | |
24a89d1c | 192 | */ |
5bedcf70 | 193 | static void n_tty_kick_worker(const struct tty_struct *tty) |
7879a9f9 | 194 | { |
24a89d1c PH |
195 | struct n_tty_data *ldata = tty->disc_data; |
196 | ||
2c5dc464 | 197 | /* Did the input worker stop? Restart it */ |
4903fde8 HL |
198 | if (unlikely(READ_ONCE(ldata->no_room))) { |
199 | WRITE_ONCE(ldata->no_room, 0); | |
24a89d1c | 200 | |
ecbbfd44 | 201 | WARN_RATELIMIT(tty->port->itty == NULL, |
cadf7486 | 202 | "scheduling with invalid itty\n"); |
21622939 PH |
203 | /* see if ldisc has been killed - if so, this means that |
204 | * even though the ldisc has been halted and ->buf.work | |
205 | * cancelled, ->buf.work is about to be rescheduled | |
206 | */ | |
207 | WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags), | |
208 | "scheduling buffer work for halted ldisc\n"); | |
e176058f | 209 | tty_buffer_restart_work(tty->port); |
ecbbfd44 | 210 | } |
55db4c64 LT |
211 | } |
212 | ||
5bedcf70 | 213 | static ssize_t chars_in_buffer(const struct tty_struct *tty) |
9a4aec2d | 214 | { |
5bedcf70 | 215 | const struct n_tty_data *ldata = tty->disc_data; |
d88c3c26 | 216 | size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head; |
9a4aec2d | 217 | |
d88c3c26 | 218 | return head - ldata->read_tail; |
9a4aec2d PH |
219 | } |
220 | ||
ee0bab83 | 221 | /** |
98629663 JS |
222 | * n_tty_write_wakeup - asynchronous I/O notifier |
223 | * @tty: tty device | |
ee0bab83 | 224 | * |
98629663 JS |
225 | * Required for the ptys, serial driver etc. since processes that attach |
226 | * themselves to the master and rely on ASYNC IO must be woken up. | |
ee0bab83 | 227 | */ |
ee0bab83 PH |
228 | static void n_tty_write_wakeup(struct tty_struct *tty) |
229 | { | |
7bccc365 PH |
230 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
231 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); | |
ee0bab83 PH |
232 | } |
233 | ||
4a23a4df | 234 | static void n_tty_check_throttle(struct tty_struct *tty) |
6367ca72 | 235 | { |
a342846f PH |
236 | struct n_tty_data *ldata = tty->disc_data; |
237 | ||
6367ca72 PH |
238 | /* |
239 | * Check the remaining room for the input canonicalization | |
240 | * mode. We don't want to throttle the driver if we're in | |
241 | * canonical mode and don't have a newline yet! | |
242 | */ | |
a342846f PH |
243 | if (ldata->icanon && ldata->canon_head == ldata->read_tail) |
244 | return; | |
245 | ||
043c8a7c | 246 | do { |
6367ca72 | 247 | tty_set_flow_change(tty, TTY_THROTTLE_SAFE); |
5e28cca1 | 248 | if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE) |
6367ca72 | 249 | break; |
5b4f9cf3 | 250 | } while (!tty_throttle_safe(tty)); |
043c8a7c | 251 | |
6367ca72 PH |
252 | __tty_set_flow_change(tty, 0); |
253 | } | |
254 | ||
4b293492 | 255 | static void n_tty_check_unthrottle(struct tty_struct *tty) |
6367ca72 | 256 | { |
6d27a63c | 257 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY) { |
3afb1b39 PH |
258 | if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) |
259 | return; | |
2c5dc464 | 260 | n_tty_kick_worker(tty); |
6d27a63c | 261 | tty_wakeup(tty->link); |
3afb1b39 PH |
262 | return; |
263 | } | |
264 | ||
6367ca72 PH |
265 | /* If there is enough space in the read buffer now, let the |
266 | * low-level driver know. We use chars_in_buffer() to | |
267 | * check the buffer, as it now knows about canonical mode. | |
268 | * Otherwise, if the driver is throttled and the line is | |
269 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | |
270 | * we won't get any more characters. | |
271 | */ | |
272 | ||
043c8a7c | 273 | do { |
6367ca72 PH |
274 | tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); |
275 | if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) | |
276 | break; | |
043c8a7c | 277 | |
2c5dc464 | 278 | n_tty_kick_worker(tty); |
5b4f9cf3 | 279 | } while (!tty_unthrottle_safe(tty)); |
043c8a7c | 280 | |
6367ca72 PH |
281 | __tty_set_flow_change(tty, 0); |
282 | } | |
283 | ||
17b82060 | 284 | /** |
98629663 JS |
285 | * put_tty_queue - add character to tty |
286 | * @c: character | |
287 | * @ldata: n_tty data | |
17b82060 | 288 | * |
98629663 | 289 | * Add a character to the tty read_buf queue. |
6d76bd26 | 290 | * |
98629663 JS |
291 | * Locking: |
292 | * * n_tty_receive_buf()/producer path: | |
293 | * caller holds non-exclusive %termios_rwsem | |
17b82060 | 294 | */ |
b9b96b20 | 295 | static inline void put_tty_queue(u8 c, struct n_tty_data *ldata) |
1da177e4 | 296 | { |
8bfbe2de CR |
297 | *read_buf_addr(ldata, ldata->read_head) = c; |
298 | ldata->read_head++; | |
1da177e4 LT |
299 | } |
300 | ||
1da177e4 | 301 | /** |
98629663 JS |
302 | * reset_buffer_flags - reset buffer state |
303 | * @ldata: line disc data to reset | |
1da177e4 | 304 | * |
98629663 JS |
305 | * Reset the read buffer counters and clear the flags. Called from |
306 | * n_tty_open() and n_tty_flush_buffer(). | |
17b82060 | 307 | * |
98629663 JS |
308 | * Locking: |
309 | * * caller holds exclusive %termios_rwsem, or | |
310 | * * (locking is not required) | |
1da177e4 | 311 | */ |
b66f4fa5 | 312 | static void reset_buffer_flags(struct n_tty_data *ldata) |
1da177e4 | 313 | { |
a73d3d69 | 314 | ldata->read_head = ldata->canon_head = ldata->read_tail = 0; |
70aca71f | 315 | ldata->commit_head = 0; |
40d5e090 | 316 | ldata->line_start = 0; |
a88a69c9 | 317 | |
a73d3d69 | 318 | ldata->erasing = 0; |
3fe780b3 | 319 | bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); |
4d0ed182 | 320 | ldata->push = 0; |
6bb6fa69 IJ |
321 | |
322 | ldata->lookahead_count = 0; | |
1da177e4 LT |
323 | } |
324 | ||
a30737ab PH |
325 | static void n_tty_packet_mode_flush(struct tty_struct *tty) |
326 | { | |
327 | unsigned long flags; | |
328 | ||
64d608db JS |
329 | if (tty->link->ctrl.packet) { |
330 | spin_lock_irqsave(&tty->ctrl.lock, flags); | |
331 | tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; | |
332 | spin_unlock_irqrestore(&tty->ctrl.lock, flags); | |
e81107d4 | 333 | wake_up_interruptible(&tty->link->read_wait); |
a30737ab | 334 | } |
a30737ab PH |
335 | } |
336 | ||
1da177e4 | 337 | /** |
98629663 JS |
338 | * n_tty_flush_buffer - clean input queue |
339 | * @tty: terminal device | |
1da177e4 | 340 | * |
98629663 JS |
341 | * Flush the input buffer. Called when the tty layer wants the buffer flushed |
342 | * (eg at hangup) or when the %N_TTY line discipline internally has to clean | |
343 | * the pending queue (for example some signals). | |
1da177e4 | 344 | * |
98629663 JS |
345 | * Holds %termios_rwsem to exclude producer/consumer while buffer indices are |
346 | * reset. | |
6d76bd26 | 347 | * |
98629663 | 348 | * Locking: %ctrl.lock, exclusive %termios_rwsem |
1da177e4 | 349 | */ |
4edf1827 | 350 | static void n_tty_flush_buffer(struct tty_struct *tty) |
1da177e4 | 351 | { |
6d76bd26 | 352 | down_write(&tty->termios_rwsem); |
b66f4fa5 | 353 | reset_buffer_flags(tty->disc_data); |
2c5dc464 | 354 | n_tty_kick_worker(tty); |
4edf1827 | 355 | |
a30737ab PH |
356 | if (tty->link) |
357 | n_tty_packet_mode_flush(tty); | |
6d76bd26 | 358 | up_write(&tty->termios_rwsem); |
1da177e4 LT |
359 | } |
360 | ||
1da177e4 | 361 | /** |
98629663 JS |
362 | * is_utf8_continuation - utf8 multibyte check |
363 | * @c: byte to check | |
1da177e4 | 364 | * |
98629663 JS |
365 | * Returns: true if the utf8 character @c is a multibyte continuation |
366 | * character. We use this to correctly compute the on-screen size of the | |
367 | * character when printing. | |
1da177e4 | 368 | */ |
b9b96b20 | 369 | static inline int is_utf8_continuation(u8 c) |
1da177e4 LT |
370 | { |
371 | return (c & 0xc0) == 0x80; | |
372 | } | |
373 | ||
374 | /** | |
98629663 JS |
375 | * is_continuation - multibyte check |
376 | * @c: byte to check | |
377 | * @tty: terminal device | |
1da177e4 | 378 | * |
98629663 JS |
379 | * Returns: true if the utf8 character @c is a multibyte continuation character |
380 | * and the terminal is in unicode mode. | |
1da177e4 | 381 | */ |
b9b96b20 | 382 | static inline int is_continuation(u8 c, const struct tty_struct *tty) |
1da177e4 LT |
383 | { |
384 | return I_IUTF8(tty) && is_utf8_continuation(c); | |
385 | } | |
386 | ||
387 | /** | |
98629663 JS |
388 | * do_output_char - output one character |
389 | * @c: character (or partial unicode symbol) | |
390 | * @tty: terminal device | |
391 | * @space: space available in tty driver write buffer | |
1da177e4 | 392 | * |
98629663 JS |
393 | * This is a helper function that handles one output character (including |
394 | * special characters like TAB, CR, LF, etc.), doing OPOST processing and | |
395 | * putting the results in the tty driver's write buffer. | |
a88a69c9 | 396 | * |
98629663 JS |
397 | * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. |
398 | * They simply aren't relevant in the world today. If you ever need them, add | |
399 | * them here. | |
1da177e4 | 400 | * |
98629663 | 401 | * Returns: the number of bytes of buffer space used or -1 if no space left. |
a88a69c9 | 402 | * |
98629663 JS |
403 | * Locking: should be called under the %output_lock to protect the column state |
404 | * and space left in the buffer. | |
1da177e4 | 405 | */ |
b9b96b20 | 406 | static int do_output_char(u8 c, struct tty_struct *tty, int space) |
1da177e4 | 407 | { |
53c5ee2c | 408 | struct n_tty_data *ldata = tty->disc_data; |
a88a69c9 | 409 | int spaces; |
1da177e4 | 410 | |
1da177e4 LT |
411 | if (!space) |
412 | return -1; | |
300a6204 | 413 | |
a88a69c9 JP |
414 | switch (c) { |
415 | case '\n': | |
416 | if (O_ONLRET(tty)) | |
53c5ee2c | 417 | ldata->column = 0; |
a88a69c9 JP |
418 | if (O_ONLCR(tty)) { |
419 | if (space < 2) | |
420 | return -1; | |
ba2e68ac | 421 | ldata->canon_column = ldata->column = 0; |
37f81fa1 | 422 | tty->ops->write(tty, "\r\n", 2); |
a88a69c9 JP |
423 | return 2; |
424 | } | |
ba2e68ac | 425 | ldata->canon_column = ldata->column; |
a88a69c9 JP |
426 | break; |
427 | case '\r': | |
53c5ee2c | 428 | if (O_ONOCR(tty) && ldata->column == 0) |
a88a69c9 JP |
429 | return 0; |
430 | if (O_OCRNL(tty)) { | |
431 | c = '\n'; | |
432 | if (O_ONLRET(tty)) | |
ba2e68ac | 433 | ldata->canon_column = ldata->column = 0; |
1da177e4 | 434 | break; |
a88a69c9 | 435 | } |
ba2e68ac | 436 | ldata->canon_column = ldata->column = 0; |
a88a69c9 JP |
437 | break; |
438 | case '\t': | |
53c5ee2c | 439 | spaces = 8 - (ldata->column & 7); |
a88a69c9 JP |
440 | if (O_TABDLY(tty) == XTABS) { |
441 | if (space < spaces) | |
442 | return -1; | |
53c5ee2c | 443 | ldata->column += spaces; |
a88a69c9 JP |
444 | tty->ops->write(tty, " ", spaces); |
445 | return spaces; | |
1da177e4 | 446 | } |
53c5ee2c | 447 | ldata->column += spaces; |
a88a69c9 JP |
448 | break; |
449 | case '\b': | |
53c5ee2c JS |
450 | if (ldata->column > 0) |
451 | ldata->column--; | |
a88a69c9 JP |
452 | break; |
453 | default: | |
a59c0d6f JP |
454 | if (!iscntrl(c)) { |
455 | if (O_OLCUC(tty)) | |
456 | c = toupper(c); | |
457 | if (!is_continuation(c, tty)) | |
53c5ee2c | 458 | ldata->column++; |
a59c0d6f | 459 | } |
a88a69c9 | 460 | break; |
1da177e4 | 461 | } |
a88a69c9 | 462 | |
f34d7a5b | 463 | tty_put_char(tty, c); |
a88a69c9 JP |
464 | return 1; |
465 | } | |
466 | ||
467 | /** | |
98629663 JS |
468 | * process_output - output post processor |
469 | * @c: character (or partial unicode symbol) | |
470 | * @tty: terminal device | |
471 | * | |
472 | * Output one character with OPOST processing. | |
a88a69c9 | 473 | * |
98629663 JS |
474 | * Returns: -1 when the output device is full and the character must be |
475 | * retried. | |
a88a69c9 | 476 | * |
98629663 JS |
477 | * Locking: %output_lock to protect column state and space left (also, this is |
478 | *called from n_tty_write() under the tty layer write lock). | |
a88a69c9 | 479 | */ |
b9b96b20 | 480 | static int process_output(u8 c, struct tty_struct *tty) |
a88a69c9 | 481 | { |
bddc7152 | 482 | struct n_tty_data *ldata = tty->disc_data; |
a88a69c9 | 483 | |
fdfa49a8 | 484 | guard(mutex)(&ldata->output_lock); |
a88a69c9 | 485 | |
fdfa49a8 | 486 | if (do_output_char(c, tty, tty_write_room(tty)) < 0) |
a88a69c9 | 487 | return -1; |
fdfa49a8 JSS |
488 | |
489 | return 0; | |
1da177e4 LT |
490 | } |
491 | ||
492 | /** | |
98629663 JS |
493 | * process_output_block - block post processor |
494 | * @tty: terminal device | |
495 | * @buf: character buffer | |
496 | * @nr: number of bytes to output | |
497 | * | |
498 | * Output a block of characters with OPOST processing. | |
499 | * | |
500 | * This path is used to speed up block console writes, among other things when | |
501 | * processing blocks of output data. It handles only the simple cases normally | |
502 | * found and helps to generate blocks of symbols for the console driver and | |
503 | * thus improve performance. | |
504 | * | |
505 | * Returns: the number of characters output. | |
506 | * | |
507 | * Locking: %output_lock to protect column state and space left (also, this is | |
508 | * called from n_tty_write() under the tty layer write lock). | |
1da177e4 | 509 | */ |
a88a69c9 | 510 | static ssize_t process_output_block(struct tty_struct *tty, |
b9b96b20 | 511 | const u8 *buf, unsigned int nr) |
1da177e4 | 512 | { |
53c5ee2c | 513 | struct n_tty_data *ldata = tty->disc_data; |
e287d646 | 514 | unsigned int space, i; |
b9b96b20 | 515 | const u8 *cp; |
1da177e4 | 516 | |
e287d646 | 517 | guard(mutex)(&ldata->output_lock); |
a88a69c9 | 518 | |
f34d7a5b | 519 | space = tty_write_room(tty); |
e287d646 | 520 | if (space == 0) |
d97aa066 | 521 | return 0; |
e287d646 | 522 | |
1da177e4 LT |
523 | if (nr > space) |
524 | nr = space; | |
525 | ||
526 | for (i = 0, cp = buf; i < nr; i++, cp++) { | |
b9b96b20 | 527 | u8 c = *cp; |
a59c0d6f JP |
528 | |
529 | switch (c) { | |
1da177e4 LT |
530 | case '\n': |
531 | if (O_ONLRET(tty)) | |
53c5ee2c | 532 | ldata->column = 0; |
1da177e4 | 533 | if (O_ONLCR(tty)) |
e287d646 | 534 | goto do_write; |
ba2e68ac | 535 | ldata->canon_column = ldata->column; |
1da177e4 LT |
536 | break; |
537 | case '\r': | |
53c5ee2c | 538 | if (O_ONOCR(tty) && ldata->column == 0) |
e287d646 | 539 | goto do_write; |
1da177e4 | 540 | if (O_OCRNL(tty)) |
e287d646 | 541 | goto do_write; |
ba2e68ac | 542 | ldata->canon_column = ldata->column = 0; |
1da177e4 LT |
543 | break; |
544 | case '\t': | |
e287d646 | 545 | goto do_write; |
1da177e4 | 546 | case '\b': |
53c5ee2c JS |
547 | if (ldata->column > 0) |
548 | ldata->column--; | |
1da177e4 LT |
549 | break; |
550 | default: | |
a59c0d6f JP |
551 | if (!iscntrl(c)) { |
552 | if (O_OLCUC(tty)) | |
e287d646 | 553 | goto do_write; |
a59c0d6f | 554 | if (!is_continuation(c, tty)) |
53c5ee2c | 555 | ldata->column++; |
a59c0d6f | 556 | } |
1da177e4 LT |
557 | break; |
558 | } | |
559 | } | |
e287d646 JSS |
560 | do_write: |
561 | return tty->ops->write(tty, buf, i); | |
1da177e4 LT |
562 | } |
563 | ||
2aa91851 JSS |
564 | static int n_tty_process_echo_ops(struct tty_struct *tty, size_t *tail, |
565 | int space) | |
566 | { | |
567 | struct n_tty_data *ldata = tty->disc_data; | |
568 | u8 op; | |
569 | ||
570 | /* | |
571 | * Since add_echo_byte() is called without holding output_lock, we | |
572 | * might see only portion of multi-byte operation. | |
573 | */ | |
574 | if (MASK(ldata->echo_commit) == MASK(*tail + 1)) | |
575 | return -ENODATA; | |
576 | ||
577 | /* | |
578 | * If the buffer byte is the start of a multi-byte operation, get the | |
579 | * next byte, which is either the op code or a control character value. | |
580 | */ | |
581 | op = echo_buf(ldata, *tail + 1); | |
582 | ||
583 | switch (op) { | |
584 | case ECHO_OP_ERASE_TAB: { | |
585 | unsigned int num_chars, num_bs; | |
586 | ||
587 | if (MASK(ldata->echo_commit) == MASK(*tail + 2)) | |
588 | return -ENODATA; | |
589 | ||
590 | num_chars = echo_buf(ldata, *tail + 2); | |
591 | ||
592 | /* | |
593 | * Determine how many columns to go back in order to erase the | |
594 | * tab. This depends on the number of columns used by other | |
595 | * characters within the tab area. If this (modulo 8) count is | |
596 | * from the start of input rather than from a previous tab, we | |
597 | * offset by canon column. Otherwise, tab spacing is normal. | |
598 | */ | |
599 | if (!(num_chars & 0x80)) | |
600 | num_chars += ldata->canon_column; | |
601 | num_bs = 8 - (num_chars & 7); | |
602 | ||
603 | if (num_bs > space) | |
604 | return -ENOSPC; | |
605 | ||
606 | space -= num_bs; | |
607 | while (num_bs--) { | |
608 | tty_put_char(tty, '\b'); | |
609 | if (ldata->column > 0) | |
610 | ldata->column--; | |
611 | } | |
612 | *tail += 3; | |
613 | break; | |
614 | } | |
615 | case ECHO_OP_SET_CANON_COL: | |
616 | ldata->canon_column = ldata->column; | |
617 | *tail += 2; | |
618 | break; | |
619 | ||
620 | case ECHO_OP_MOVE_BACK_COL: | |
621 | if (ldata->column > 0) | |
622 | ldata->column--; | |
623 | *tail += 2; | |
624 | break; | |
625 | ||
626 | case ECHO_OP_START: | |
627 | /* This is an escaped echo op start code */ | |
628 | if (!space) | |
629 | return -ENOSPC; | |
630 | ||
631 | tty_put_char(tty, ECHO_OP_START); | |
632 | ldata->column++; | |
633 | space--; | |
634 | *tail += 2; | |
635 | break; | |
636 | ||
637 | default: | |
638 | /* | |
639 | * If the op is not a special byte code, it is a ctrl char | |
640 | * tagged to be echoed as "^X" (where X is the letter | |
641 | * representing the control char). Note that we must ensure | |
642 | * there is enough space for the whole ctrl pair. | |
643 | */ | |
644 | if (space < 2) | |
645 | return -ENOSPC; | |
646 | ||
647 | tty_put_char(tty, '^'); | |
648 | tty_put_char(tty, op ^ 0100); | |
649 | ldata->column += 2; | |
650 | space -= 2; | |
651 | *tail += 2; | |
652 | break; | |
653 | } | |
654 | ||
655 | return space; | |
656 | } | |
657 | ||
a88a69c9 | 658 | /** |
98629663 JS |
659 | * __process_echoes - write pending echo characters |
660 | * @tty: terminal device | |
a88a69c9 | 661 | * |
98629663 JS |
662 | * Write previously buffered echo (and other ldisc-generated) characters to the |
663 | * tty. | |
a88a69c9 | 664 | * |
98629663 JS |
665 | * Characters generated by the ldisc (including echoes) need to be buffered |
666 | * because the driver's write buffer can fill during heavy program output. | |
667 | * Echoing straight to the driver will often fail under these conditions, | |
668 | * causing lost characters and resulting mismatches of ldisc state information. | |
a88a69c9 | 669 | * |
98629663 JS |
670 | * Since the ldisc state must represent the characters actually sent to the |
671 | * driver at the time of the write, operations like certain changes in column | |
672 | * state are also saved in the buffer and executed here. | |
a88a69c9 | 673 | * |
98629663 JS |
674 | * A circular fifo buffer is used so that the most recent characters are |
675 | * prioritized. Also, when control characters are echoed with a prefixed "^", | |
676 | * the pair is treated atomically and thus not separated. | |
a88a69c9 | 677 | * |
98629663 | 678 | * Locking: callers must hold %output_lock. |
a88a69c9 | 679 | */ |
bc5b1ec5 | 680 | static size_t __process_echoes(struct tty_struct *tty) |
a88a69c9 | 681 | { |
53c5ee2c | 682 | struct n_tty_data *ldata = tty->disc_data; |
d97aa066 | 683 | unsigned int space, old_space; |
addaebcc | 684 | size_t tail; |
b9b96b20 | 685 | u8 c; |
a88a69c9 | 686 | |
bc5b1ec5 | 687 | old_space = space = tty_write_room(tty); |
a88a69c9 | 688 | |
addaebcc | 689 | tail = ldata->echo_tail; |
ebec3f8f | 690 | while (MASK(ldata->echo_commit) != MASK(tail)) { |
addaebcc | 691 | c = echo_buf(ldata, tail); |
a88a69c9 | 692 | if (c == ECHO_OP_START) { |
2aa91851 JSS |
693 | int ret = n_tty_process_echo_ops(tty, &tail, space); |
694 | if (ret == -ENODATA) | |
ebec3f8f | 695 | goto not_yet_stored; |
2aa91851 | 696 | if (ret < 0) |
a88a69c9 | 697 | break; |
2aa91851 | 698 | space = ret; |
a88a69c9 | 699 | } else { |
582f5590 | 700 | if (O_OPOST(tty)) { |
ee5aa7b8 JP |
701 | int retval = do_output_char(c, tty, space); |
702 | if (retval < 0) | |
703 | break; | |
704 | space -= retval; | |
705 | } else { | |
706 | if (!space) | |
707 | break; | |
708 | tty_put_char(tty, c); | |
709 | space -= 1; | |
710 | } | |
addaebcc | 711 | tail += 1; |
a88a69c9 | 712 | } |
a88a69c9 JP |
713 | } |
714 | ||
cbfd0340 PH |
715 | /* If the echo buffer is nearly full (so that the possibility exists |
716 | * of echo overrun before the next commit), then discard enough | |
717 | * data at the tail to prevent a subsequent overrun */ | |
ebec3f8f TH |
718 | while (ldata->echo_commit > tail && |
719 | ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) { | |
c476f658 | 720 | if (echo_buf(ldata, tail) == ECHO_OP_START) { |
6f222536 | 721 | if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB) |
cbfd0340 PH |
722 | tail += 3; |
723 | else | |
724 | tail += 2; | |
725 | } else | |
726 | tail++; | |
727 | } | |
728 | ||
ebec3f8f | 729 | not_yet_stored: |
addaebcc | 730 | ldata->echo_tail = tail; |
bc5b1ec5 | 731 | return old_space - space; |
019ebdf9 PH |
732 | } |
733 | ||
734 | static void commit_echoes(struct tty_struct *tty) | |
735 | { | |
736 | struct n_tty_data *ldata = tty->disc_data; | |
bc5b1ec5 | 737 | size_t nr, old, echoed; |
cbfd0340 PH |
738 | size_t head; |
739 | ||
ebec3f8f | 740 | mutex_lock(&ldata->output_lock); |
cbfd0340 | 741 | head = ldata->echo_head; |
1075a6e2 | 742 | ldata->echo_mark = head; |
cbfd0340 PH |
743 | old = ldata->echo_commit - ldata->echo_tail; |
744 | ||
745 | /* Process committed echoes if the accumulated # of bytes | |
746 | * is over the threshold (and try again each time another | |
747 | * block is accumulated) */ | |
748 | nr = head - ldata->echo_tail; | |
ebec3f8f TH |
749 | if (nr < ECHO_COMMIT_WATERMARK || |
750 | (nr % ECHO_BLOCK > old % ECHO_BLOCK)) { | |
751 | mutex_unlock(&ldata->output_lock); | |
cbfd0340 | 752 | return; |
ebec3f8f | 753 | } |
a88a69c9 | 754 | |
cbfd0340 | 755 | ldata->echo_commit = head; |
bc5b1ec5 | 756 | echoed = __process_echoes(tty); |
bddc7152 | 757 | mutex_unlock(&ldata->output_lock); |
a88a69c9 | 758 | |
bc5b1ec5 | 759 | if (echoed && tty->ops->flush_chars) |
a88a69c9 JP |
760 | tty->ops->flush_chars(tty); |
761 | } | |
762 | ||
019ebdf9 | 763 | static void process_echoes(struct tty_struct *tty) |
17bd7907 PH |
764 | { |
765 | struct n_tty_data *ldata = tty->disc_data; | |
bc5b1ec5 | 766 | size_t echoed; |
17bd7907 | 767 | |
e2613be5 | 768 | if (ldata->echo_mark == ldata->echo_tail) |
019ebdf9 PH |
769 | return; |
770 | ||
771 | mutex_lock(&ldata->output_lock); | |
1075a6e2 | 772 | ldata->echo_commit = ldata->echo_mark; |
bc5b1ec5 | 773 | echoed = __process_echoes(tty); |
019ebdf9 PH |
774 | mutex_unlock(&ldata->output_lock); |
775 | ||
bc5b1ec5 | 776 | if (echoed && tty->ops->flush_chars) |
019ebdf9 | 777 | tty->ops->flush_chars(tty); |
17bd7907 PH |
778 | } |
779 | ||
1075a6e2 | 780 | /* NB: echo_mark and echo_head should be equivalent here */ |
cbfd0340 PH |
781 | static void flush_echoes(struct tty_struct *tty) |
782 | { | |
783 | struct n_tty_data *ldata = tty->disc_data; | |
784 | ||
39434abd PH |
785 | if ((!L_ECHO(tty) && !L_ECHONL(tty)) || |
786 | ldata->echo_commit == ldata->echo_head) | |
cbfd0340 PH |
787 | return; |
788 | ||
789 | mutex_lock(&ldata->output_lock); | |
790 | ldata->echo_commit = ldata->echo_head; | |
791 | __process_echoes(tty); | |
792 | mutex_unlock(&ldata->output_lock); | |
793 | } | |
794 | ||
a88a69c9 | 795 | /** |
98629663 JS |
796 | * add_echo_byte - add a byte to the echo buffer |
797 | * @c: unicode byte to echo | |
798 | * @ldata: n_tty data | |
a88a69c9 | 799 | * |
98629663 | 800 | * Add a character or operation byte to the echo buffer. |
a88a69c9 | 801 | */ |
b9b96b20 | 802 | static inline void add_echo_byte(u8 c, struct n_tty_data *ldata) |
a88a69c9 | 803 | { |
ebec3f8f TH |
804 | *echo_buf_addr(ldata, ldata->echo_head) = c; |
805 | smp_wmb(); /* Matches smp_rmb() in echo_buf(). */ | |
806 | ldata->echo_head++; | |
a88a69c9 JP |
807 | } |
808 | ||
809 | /** | |
98629663 JS |
810 | * echo_move_back_col - add operation to move back a column |
811 | * @ldata: n_tty data | |
a88a69c9 | 812 | * |
98629663 | 813 | * Add an operation to the echo buffer to move back one column. |
a88a69c9 | 814 | */ |
57c94121 | 815 | static void echo_move_back_col(struct n_tty_data *ldata) |
a88a69c9 | 816 | { |
57c94121 JS |
817 | add_echo_byte(ECHO_OP_START, ldata); |
818 | add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); | |
a88a69c9 JP |
819 | } |
820 | ||
821 | /** | |
98629663 JS |
822 | * echo_set_canon_col - add operation to set the canon column |
823 | * @ldata: n_tty data | |
a88a69c9 | 824 | * |
98629663 JS |
825 | * Add an operation to the echo buffer to set the canon column to the current |
826 | * column. | |
a88a69c9 | 827 | */ |
57c94121 | 828 | static void echo_set_canon_col(struct n_tty_data *ldata) |
a88a69c9 | 829 | { |
57c94121 JS |
830 | add_echo_byte(ECHO_OP_START, ldata); |
831 | add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); | |
a88a69c9 JP |
832 | } |
833 | ||
834 | /** | |
98629663 JS |
835 | * echo_erase_tab - add operation to erase a tab |
836 | * @num_chars: number of character columns already used | |
837 | * @after_tab: true if num_chars starts after a previous tab | |
838 | * @ldata: n_tty data | |
839 | * | |
840 | * Add an operation to the echo buffer to erase a tab. | |
841 | * | |
842 | * Called by the eraser function, which knows how many character columns have | |
843 | * been used since either a previous tab or the start of input. This | |
844 | * information will be used later, along with canon column (if applicable), to | |
845 | * go back the correct number of columns. | |
a88a69c9 | 846 | */ |
a88a69c9 | 847 | static void echo_erase_tab(unsigned int num_chars, int after_tab, |
57c94121 | 848 | struct n_tty_data *ldata) |
a88a69c9 | 849 | { |
57c94121 JS |
850 | add_echo_byte(ECHO_OP_START, ldata); |
851 | add_echo_byte(ECHO_OP_ERASE_TAB, ldata); | |
a88a69c9 JP |
852 | |
853 | /* We only need to know this modulo 8 (tab spacing) */ | |
854 | num_chars &= 7; | |
855 | ||
856 | /* Set the high bit as a flag if num_chars is after a previous tab */ | |
857 | if (after_tab) | |
858 | num_chars |= 0x80; | |
300a6204 | 859 | |
57c94121 | 860 | add_echo_byte(num_chars, ldata); |
a88a69c9 JP |
861 | } |
862 | ||
863 | /** | |
98629663 JS |
864 | * echo_char_raw - echo a character raw |
865 | * @c: unicode byte to echo | |
866 | * @ldata: line disc data | |
a88a69c9 | 867 | * |
98629663 JS |
868 | * Echo user input back onto the screen. This must be called only when |
869 | * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path. | |
a88a69c9 | 870 | * |
98629663 | 871 | * This variant does not treat control characters specially. |
a88a69c9 | 872 | */ |
b9b96b20 | 873 | static void echo_char_raw(u8 c, struct n_tty_data *ldata) |
a88a69c9 | 874 | { |
a88a69c9 | 875 | if (c == ECHO_OP_START) { |
57c94121 JS |
876 | add_echo_byte(ECHO_OP_START, ldata); |
877 | add_echo_byte(ECHO_OP_START, ldata); | |
a88a69c9 | 878 | } else { |
57c94121 | 879 | add_echo_byte(c, ldata); |
a88a69c9 | 880 | } |
a88a69c9 | 881 | } |
1da177e4 | 882 | |
1da177e4 | 883 | /** |
98629663 JS |
884 | * echo_char - echo a character |
885 | * @c: unicode byte to echo | |
886 | * @tty: terminal device | |
1da177e4 | 887 | * |
98629663 JS |
888 | * Echo user input back onto the screen. This must be called only when |
889 | * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path. | |
17b82060 | 890 | * |
98629663 JS |
891 | * This variant tags control characters to be echoed as "^X" (where X is the |
892 | * letter representing the control char). | |
1da177e4 | 893 | */ |
b9b96b20 | 894 | static void echo_char(u8 c, const struct tty_struct *tty) |
1da177e4 | 895 | { |
bddc7152 JS |
896 | struct n_tty_data *ldata = tty->disc_data; |
897 | ||
a88a69c9 | 898 | if (c == ECHO_OP_START) { |
57c94121 JS |
899 | add_echo_byte(ECHO_OP_START, ldata); |
900 | add_echo_byte(ECHO_OP_START, ldata); | |
a88a69c9 | 901 | } else { |
62b26358 | 902 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') |
57c94121 JS |
903 | add_echo_byte(ECHO_OP_START, ldata); |
904 | add_echo_byte(c, ldata); | |
a88a69c9 | 905 | } |
1da177e4 LT |
906 | } |
907 | ||
17b82060 | 908 | /** |
98629663 JS |
909 | * finish_erasing - complete erase |
910 | * @ldata: n_tty data | |
17b82060 | 911 | */ |
57c94121 | 912 | static inline void finish_erasing(struct n_tty_data *ldata) |
1da177e4 | 913 | { |
53c5ee2c | 914 | if (ldata->erasing) { |
57c94121 | 915 | echo_char_raw('/', ldata); |
53c5ee2c | 916 | ldata->erasing = 0; |
1da177e4 LT |
917 | } |
918 | } | |
919 | ||
920 | /** | |
98629663 JS |
921 | * eraser - handle erase function |
922 | * @c: character input | |
923 | * @tty: terminal device | |
1da177e4 | 924 | * |
98629663 JS |
925 | * Perform erase and necessary output when an erase character is present in the |
926 | * stream from the driver layer. Handles the complexities of UTF-8 multibyte | |
927 | * symbols. | |
17b82060 | 928 | * |
98629663 JS |
929 | * Locking: n_tty_receive_buf()/producer path: |
930 | * caller holds non-exclusive %termios_rwsem | |
1da177e4 | 931 | */ |
b9b96b20 | 932 | static void eraser(u8 c, const struct tty_struct *tty) |
1da177e4 | 933 | { |
53c5ee2c | 934 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 | 935 | enum { ERASE, WERASE, KILL } kill_type; |
bc5a5e3f PH |
936 | size_t head; |
937 | size_t cnt; | |
938 | int seen_alnums; | |
1da177e4 | 939 | |
ba2e68ac | 940 | if (ldata->read_head == ldata->canon_head) { |
7e94b1d9 | 941 | /* process_output('\a', tty); */ /* what do you think? */ |
1da177e4 LT |
942 | return; |
943 | } | |
944 | if (c == ERASE_CHAR(tty)) | |
945 | kill_type = ERASE; | |
946 | else if (c == WERASE_CHAR(tty)) | |
947 | kill_type = WERASE; | |
948 | else { | |
949 | if (!L_ECHO(tty)) { | |
ba2e68ac | 950 | ldata->read_head = ldata->canon_head; |
1da177e4 LT |
951 | return; |
952 | } | |
953 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { | |
ba2e68ac | 954 | ldata->read_head = ldata->canon_head; |
57c94121 | 955 | finish_erasing(ldata); |
1da177e4 LT |
956 | echo_char(KILL_CHAR(tty), tty); |
957 | /* Add a newline if ECHOK is on and ECHOKE is off. */ | |
958 | if (L_ECHOK(tty)) | |
57c94121 | 959 | echo_char_raw('\n', ldata); |
1da177e4 LT |
960 | return; |
961 | } | |
962 | kill_type = KILL; | |
963 | } | |
964 | ||
965 | seen_alnums = 0; | |
3d63b7e4 | 966 | while (MASK(ldata->read_head) != MASK(ldata->canon_head)) { |
ba2e68ac | 967 | head = ldata->read_head; |
1da177e4 LT |
968 | |
969 | /* erase a single possibly multibyte character */ | |
970 | do { | |
bc5a5e3f PH |
971 | head--; |
972 | c = read_buf(ldata, head); | |
3d63b7e4 TH |
973 | } while (is_continuation(c, tty) && |
974 | MASK(head) != MASK(ldata->canon_head)); | |
1da177e4 LT |
975 | |
976 | /* do not partially erase */ | |
977 | if (is_continuation(c, tty)) | |
978 | break; | |
979 | ||
980 | if (kill_type == WERASE) { | |
981 | /* Equivalent to BSD's ALTWERASE. */ | |
982 | if (isalnum(c) || c == '_') | |
983 | seen_alnums++; | |
984 | else if (seen_alnums) | |
985 | break; | |
986 | } | |
bc5a5e3f | 987 | cnt = ldata->read_head - head; |
ba2e68ac | 988 | ldata->read_head = head; |
1da177e4 LT |
989 | if (L_ECHO(tty)) { |
990 | if (L_ECHOPRT(tty)) { | |
53c5ee2c | 991 | if (!ldata->erasing) { |
57c94121 | 992 | echo_char_raw('\\', ldata); |
53c5ee2c | 993 | ldata->erasing = 1; |
1da177e4 LT |
994 | } |
995 | /* if cnt > 1, output a multi-byte character */ | |
996 | echo_char(c, tty); | |
997 | while (--cnt > 0) { | |
bc5a5e3f PH |
998 | head++; |
999 | echo_char_raw(read_buf(ldata, head), ldata); | |
57c94121 | 1000 | echo_move_back_col(ldata); |
1da177e4 LT |
1001 | } |
1002 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { | |
1003 | echo_char(ERASE_CHAR(tty), tty); | |
1004 | } else if (c == '\t') { | |
a88a69c9 JP |
1005 | unsigned int num_chars = 0; |
1006 | int after_tab = 0; | |
bc5a5e3f | 1007 | size_t tail = ldata->read_head; |
a88a69c9 JP |
1008 | |
1009 | /* | |
1010 | * Count the columns used for characters | |
1011 | * since the start of input or after a | |
1012 | * previous tab. | |
1013 | * This info is used to go back the correct | |
1014 | * number of columns. | |
1015 | */ | |
3d63b7e4 | 1016 | while (MASK(tail) != MASK(ldata->canon_head)) { |
bc5a5e3f PH |
1017 | tail--; |
1018 | c = read_buf(ldata, tail); | |
a88a69c9 JP |
1019 | if (c == '\t') { |
1020 | after_tab = 1; | |
1021 | break; | |
300a6204 | 1022 | } else if (iscntrl(c)) { |
1da177e4 | 1023 | if (L_ECHOCTL(tty)) |
a88a69c9 JP |
1024 | num_chars += 2; |
1025 | } else if (!is_continuation(c, tty)) { | |
1026 | num_chars++; | |
1027 | } | |
1da177e4 | 1028 | } |
57c94121 | 1029 | echo_erase_tab(num_chars, after_tab, ldata); |
1da177e4 LT |
1030 | } else { |
1031 | if (iscntrl(c) && L_ECHOCTL(tty)) { | |
57c94121 JS |
1032 | echo_char_raw('\b', ldata); |
1033 | echo_char_raw(' ', ldata); | |
1034 | echo_char_raw('\b', ldata); | |
1da177e4 LT |
1035 | } |
1036 | if (!iscntrl(c) || L_ECHOCTL(tty)) { | |
57c94121 JS |
1037 | echo_char_raw('\b', ldata); |
1038 | echo_char_raw(' ', ldata); | |
1039 | echo_char_raw('\b', ldata); | |
1da177e4 LT |
1040 | } |
1041 | } | |
1042 | } | |
1043 | if (kill_type == ERASE) | |
1044 | break; | |
1045 | } | |
ba2e68ac | 1046 | if (ldata->read_head == ldata->canon_head && L_ECHO(tty)) |
57c94121 | 1047 | finish_erasing(ldata); |
1da177e4 LT |
1048 | } |
1049 | ||
c66453ce JS |
1050 | |
1051 | static void __isig(int sig, struct tty_struct *tty) | |
1052 | { | |
1053 | struct pid *tty_pgrp = tty_get_pgrp(tty); | |
1054 | if (tty_pgrp) { | |
1055 | kill_pgrp(tty_pgrp, sig, 1); | |
1056 | put_pid(tty_pgrp); | |
1057 | } | |
1058 | } | |
1059 | ||
1da177e4 | 1060 | /** |
98629663 JS |
1061 | * isig - handle the ISIG optio |
1062 | * @sig: signal | |
1063 | * @tty: terminal | |
1da177e4 | 1064 | * |
98629663 JS |
1065 | * Called when a signal is being sent due to terminal input. Called from the |
1066 | * &tty_driver.receive_buf() path, so serialized. | |
17b82060 | 1067 | * |
98629663 JS |
1068 | * Performs input and output flush if !NOFLSH. In this context, the echo |
1069 | * buffer is 'output'. The signal is processed first to alert any current | |
1070 | * readers or writers to discontinue and exit their i/o loops. | |
d2b6f447 | 1071 | * |
98629663 | 1072 | * Locking: %ctrl.lock |
1da177e4 | 1073 | */ |
3b19e032 PH |
1074 | static void isig(int sig, struct tty_struct *tty) |
1075 | { | |
1076 | struct n_tty_data *ldata = tty->disc_data; | |
1077 | ||
1078 | if (L_NOFLSH(tty)) { | |
1079 | /* signal only */ | |
1080 | __isig(sig, tty); | |
1081 | ||
1082 | } else { /* signal and flush */ | |
d2b6f447 PH |
1083 | up_read(&tty->termios_rwsem); |
1084 | down_write(&tty->termios_rwsem); | |
1085 | ||
3b19e032 PH |
1086 | __isig(sig, tty); |
1087 | ||
d2b6f447 PH |
1088 | /* clear echo buffer */ |
1089 | mutex_lock(&ldata->output_lock); | |
1090 | ldata->echo_head = ldata->echo_tail = 0; | |
1091 | ldata->echo_mark = ldata->echo_commit = 0; | |
1092 | mutex_unlock(&ldata->output_lock); | |
1093 | ||
1094 | /* clear output buffer */ | |
1095 | tty_driver_flush_buffer(tty); | |
1096 | ||
1097 | /* clear input buffer */ | |
1098 | reset_buffer_flags(tty->disc_data); | |
1099 | ||
1100 | /* notify pty master of flush */ | |
1101 | if (tty->link) | |
1102 | n_tty_packet_mode_flush(tty); | |
1103 | ||
1104 | up_write(&tty->termios_rwsem); | |
1105 | down_read(&tty->termios_rwsem); | |
1106 | } | |
1da177e4 LT |
1107 | } |
1108 | ||
1109 | /** | |
98629663 JS |
1110 | * n_tty_receive_break - handle break |
1111 | * @tty: terminal | |
1da177e4 | 1112 | * |
98629663 JS |
1113 | * An RS232 break event has been hit in the incoming bitstream. This can cause |
1114 | * a variety of events depending upon the termios settings. | |
1da177e4 | 1115 | * |
98629663 JS |
1116 | * Locking: n_tty_receive_buf()/producer path: |
1117 | * caller holds non-exclusive termios_rwsem | |
6d76bd26 | 1118 | * |
98629663 | 1119 | * Note: may get exclusive %termios_rwsem if flushing input buffer |
1da177e4 | 1120 | */ |
4b293492 | 1121 | static void n_tty_receive_break(struct tty_struct *tty) |
1da177e4 | 1122 | { |
57c94121 JS |
1123 | struct n_tty_data *ldata = tty->disc_data; |
1124 | ||
1da177e4 LT |
1125 | if (I_IGNBRK(tty)) |
1126 | return; | |
1127 | if (I_BRKINT(tty)) { | |
8c985d18 | 1128 | isig(SIGINT, tty); |
1da177e4 LT |
1129 | return; |
1130 | } | |
1131 | if (I_PARMRK(tty)) { | |
57c94121 JS |
1132 | put_tty_queue('\377', ldata); |
1133 | put_tty_queue('\0', ldata); | |
1da177e4 | 1134 | } |
57c94121 | 1135 | put_tty_queue('\0', ldata); |
1da177e4 LT |
1136 | } |
1137 | ||
1138 | /** | |
98629663 JS |
1139 | * n_tty_receive_overrun - handle overrun reporting |
1140 | * @tty: terminal | |
1da177e4 | 1141 | * |
98629663 JS |
1142 | * Data arrived faster than we could process it. While the tty driver has |
1143 | * flagged this the bits that were missed are gone forever. | |
1da177e4 | 1144 | * |
98629663 JS |
1145 | * Called from the receive_buf path so single threaded. Does not need locking |
1146 | * as num_overrun and overrun_time are function private. | |
1da177e4 | 1147 | */ |
5bedcf70 | 1148 | static void n_tty_receive_overrun(const struct tty_struct *tty) |
1da177e4 | 1149 | { |
53c5ee2c | 1150 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 | 1151 | |
53c5ee2c | 1152 | ldata->num_overrun++; |
73276e3a | 1153 | if (time_is_before_jiffies(ldata->overrun_time + HZ)) { |
c3b2b26f | 1154 | tty_warn(tty, "%u input overrun(s)\n", ldata->num_overrun); |
53c5ee2c JS |
1155 | ldata->overrun_time = jiffies; |
1156 | ldata->num_overrun = 0; | |
1da177e4 LT |
1157 | } |
1158 | } | |
1159 | ||
1160 | /** | |
98629663 JS |
1161 | * n_tty_receive_parity_error - error notifier |
1162 | * @tty: terminal device | |
1163 | * @c: character | |
1da177e4 | 1164 | * |
98629663 JS |
1165 | * Process a parity error and queue the right data to indicate the error case |
1166 | * if necessary. | |
6d76bd26 | 1167 | * |
98629663 JS |
1168 | * Locking: n_tty_receive_buf()/producer path: |
1169 | * caller holds non-exclusive %termios_rwsem | |
1da177e4 | 1170 | */ |
5bedcf70 | 1171 | static void n_tty_receive_parity_error(const struct tty_struct *tty, |
b9b96b20 | 1172 | u8 c) |
1da177e4 | 1173 | { |
57c94121 JS |
1174 | struct n_tty_data *ldata = tty->disc_data; |
1175 | ||
66528f90 PH |
1176 | if (I_INPCK(tty)) { |
1177 | if (I_IGNPAR(tty)) | |
1178 | return; | |
1179 | if (I_PARMRK(tty)) { | |
1180 | put_tty_queue('\377', ldata); | |
1181 | put_tty_queue('\0', ldata); | |
1182 | put_tty_queue(c, ldata); | |
1183 | } else | |
1184 | put_tty_queue('\0', ldata); | |
1185 | } else | |
57c94121 | 1186 | put_tty_queue(c, ldata); |
1da177e4 LT |
1187 | } |
1188 | ||
b0ac50be | 1189 | static void |
b9b96b20 | 1190 | n_tty_receive_signal_char(struct tty_struct *tty, int signal, u8 c) |
b0ac50be | 1191 | { |
d2b6f447 | 1192 | isig(signal, tty); |
b0ac50be PH |
1193 | if (I_IXON(tty)) |
1194 | start_tty(tty); | |
1195 | if (L_ECHO(tty)) { | |
1196 | echo_char(c, tty); | |
1197 | commit_echoes(tty); | |
e2613be5 PH |
1198 | } else |
1199 | process_echoes(tty); | |
b0ac50be PH |
1200 | } |
1201 | ||
b9b96b20 | 1202 | static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, u8 c) |
25e02ba6 IJ |
1203 | { |
1204 | return c == START_CHAR(tty) || c == STOP_CHAR(tty); | |
1205 | } | |
1206 | ||
6bb6fa69 IJ |
1207 | /** |
1208 | * n_tty_receive_char_flow_ctrl - receive flow control chars | |
1209 | * @tty: terminal device | |
1210 | * @c: character | |
1211 | * @lookahead_done: lookahead has processed this character already | |
1212 | * | |
1213 | * Receive and process flow control character actions. | |
1214 | * | |
1215 | * In case lookahead for flow control chars already handled the character in | |
1216 | * advance to the normal receive, the actions are skipped during normal | |
1217 | * receive. | |
1218 | * | |
1219 | * Returns true if @c is consumed as flow-control character, the character | |
1220 | * must not be treated as normal character. | |
1221 | */ | |
b9b96b20 | 1222 | static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, u8 c, |
6bb6fa69 | 1223 | bool lookahead_done) |
ec66b8cf | 1224 | { |
25e02ba6 IJ |
1225 | if (!n_tty_is_char_flow_ctrl(tty, c)) |
1226 | return false; | |
1227 | ||
6bb6fa69 IJ |
1228 | if (lookahead_done) |
1229 | return true; | |
1230 | ||
ec66b8cf IJ |
1231 | if (c == START_CHAR(tty)) { |
1232 | start_tty(tty); | |
1233 | process_echoes(tty); | |
1234 | return true; | |
1235 | } | |
ec66b8cf | 1236 | |
25e02ba6 IJ |
1237 | /* STOP_CHAR */ |
1238 | stop_tty(tty); | |
1239 | return true; | |
ec66b8cf IJ |
1240 | } |
1241 | ||
00830407 JSS |
1242 | static void n_tty_receive_handle_newline(struct tty_struct *tty, u8 c) |
1243 | { | |
1244 | struct n_tty_data *ldata = tty->disc_data; | |
1245 | ||
1246 | set_bit(MASK(ldata->read_head), ldata->read_flags); | |
1247 | put_tty_queue(c, ldata); | |
1248 | smp_store_release(&ldata->canon_head, ldata->read_head); | |
1249 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | |
1250 | wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); | |
1251 | } | |
1252 | ||
102dc8aa JSS |
1253 | static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) |
1254 | { | |
1255 | struct n_tty_data *ldata = tty->disc_data; | |
1256 | ||
1257 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || | |
1258 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { | |
1259 | eraser(c, tty); | |
1260 | commit_echoes(tty); | |
1261 | ||
1262 | return true; | |
1263 | } | |
1264 | ||
1265 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { | |
1266 | ldata->lnext = 1; | |
1267 | if (L_ECHO(tty)) { | |
1268 | finish_erasing(ldata); | |
1269 | if (L_ECHOCTL(tty)) { | |
1270 | echo_char_raw('^', ldata); | |
1271 | echo_char_raw('\b', ldata); | |
1272 | commit_echoes(tty); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | return true; | |
1277 | } | |
1278 | ||
1279 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) { | |
1280 | size_t tail = ldata->canon_head; | |
1281 | ||
1282 | finish_erasing(ldata); | |
1283 | echo_char(c, tty); | |
1284 | echo_char_raw('\n', ldata); | |
1285 | while (MASK(tail) != MASK(ldata->read_head)) { | |
1286 | echo_char(read_buf(ldata, tail), tty); | |
1287 | tail++; | |
1288 | } | |
1289 | commit_echoes(tty); | |
1290 | ||
1291 | return true; | |
1292 | } | |
1293 | ||
1294 | if (c == '\n') { | |
1295 | if (L_ECHO(tty) || L_ECHONL(tty)) { | |
1296 | echo_char_raw('\n', ldata); | |
1297 | commit_echoes(tty); | |
1298 | } | |
00830407 JSS |
1299 | n_tty_receive_handle_newline(tty, c); |
1300 | ||
1301 | return true; | |
102dc8aa JSS |
1302 | } |
1303 | ||
1304 | if (c == EOF_CHAR(tty)) { | |
1305 | c = __DISABLED_CHAR; | |
00830407 JSS |
1306 | n_tty_receive_handle_newline(tty, c); |
1307 | ||
1308 | return true; | |
102dc8aa JSS |
1309 | } |
1310 | ||
1311 | if ((c == EOL_CHAR(tty)) || | |
1312 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { | |
1313 | /* | |
1314 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? | |
1315 | */ | |
1316 | if (L_ECHO(tty)) { | |
1317 | /* Record the column of first canon char. */ | |
1318 | if (ldata->canon_head == ldata->read_head) | |
1319 | echo_set_canon_col(ldata); | |
1320 | echo_char(c, tty); | |
1321 | commit_echoes(tty); | |
1322 | } | |
1323 | /* | |
1324 | * XXX does PARMRK doubling happen for | |
1325 | * EOL_CHAR and EOL2_CHAR? | |
1326 | */ | |
046b44ab | 1327 | if (c == '\377' && I_PARMRK(tty)) |
102dc8aa JSS |
1328 | put_tty_queue(c, ldata); |
1329 | ||
00830407 JSS |
1330 | n_tty_receive_handle_newline(tty, c); |
1331 | ||
102dc8aa JSS |
1332 | return true; |
1333 | } | |
1334 | ||
1335 | return false; | |
1336 | } | |
1337 | ||
b9b96b20 | 1338 | static void n_tty_receive_char_special(struct tty_struct *tty, u8 c, |
6bb6fa69 | 1339 | bool lookahead_done) |
1da177e4 | 1340 | { |
53c5ee2c | 1341 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 | 1342 | |
6bb6fa69 | 1343 | if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done)) |
ec66b8cf | 1344 | return; |
575537b3 | 1345 | |
1da177e4 | 1346 | if (L_ISIG(tty)) { |
b0ac50be PH |
1347 | if (c == INTR_CHAR(tty)) { |
1348 | n_tty_receive_signal_char(tty, SIGINT, c); | |
16765365 | 1349 | return; |
b0ac50be PH |
1350 | } else if (c == QUIT_CHAR(tty)) { |
1351 | n_tty_receive_signal_char(tty, SIGQUIT, c); | |
16765365 | 1352 | return; |
b0ac50be PH |
1353 | } else if (c == SUSP_CHAR(tty)) { |
1354 | n_tty_receive_signal_char(tty, SIGTSTP, c); | |
16765365 | 1355 | return; |
1da177e4 LT |
1356 | } |
1357 | } | |
575537b3 | 1358 | |
6e94dbc7 | 1359 | if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) { |
855df3c0 PH |
1360 | start_tty(tty); |
1361 | process_echoes(tty); | |
1362 | } | |
1363 | ||
575537b3 JP |
1364 | if (c == '\r') { |
1365 | if (I_IGNCR(tty)) | |
16765365 | 1366 | return; |
575537b3 JP |
1367 | if (I_ICRNL(tty)) |
1368 | c = '\n'; | |
1369 | } else if (c == '\n' && I_INLCR(tty)) | |
1370 | c = '\r'; | |
1371 | ||
102dc8aa JSS |
1372 | if (ldata->icanon && n_tty_receive_char_canon(tty, c)) |
1373 | return; | |
4edf1827 | 1374 | |
acc71bba | 1375 | if (L_ECHO(tty)) { |
57c94121 | 1376 | finish_erasing(ldata); |
1da177e4 | 1377 | if (c == '\n') |
57c94121 | 1378 | echo_char_raw('\n', ldata); |
1da177e4 LT |
1379 | else { |
1380 | /* Record the column of first canon char. */ | |
ba2e68ac | 1381 | if (ldata->canon_head == ldata->read_head) |
57c94121 | 1382 | echo_set_canon_col(ldata); |
1da177e4 LT |
1383 | echo_char(c, tty); |
1384 | } | |
17bd7907 | 1385 | commit_echoes(tty); |
1da177e4 LT |
1386 | } |
1387 | ||
001ba923 | 1388 | /* PARMRK doubling check */ |
046b44ab | 1389 | if (c == '\377' && I_PARMRK(tty)) |
57c94121 | 1390 | put_tty_queue(c, ldata); |
1da177e4 | 1391 | |
57c94121 | 1392 | put_tty_queue(c, ldata); |
4edf1827 | 1393 | } |
1da177e4 | 1394 | |
c66453ce | 1395 | /** |
98629663 JS |
1396 | * n_tty_receive_char - perform processing |
1397 | * @tty: terminal device | |
1398 | * @c: character | |
c66453ce | 1399 | * |
98629663 JS |
1400 | * Process an individual character of input received from the driver. This is |
1401 | * serialized with respect to itself by the rules for the driver above. | |
c66453ce | 1402 | * |
98629663 JS |
1403 | * Locking: n_tty_receive_buf()/producer path: |
1404 | * caller holds non-exclusive %termios_rwsem | |
1405 | * publishes canon_head if canonical mode is active | |
c66453ce | 1406 | */ |
b9b96b20 | 1407 | static void n_tty_receive_char(struct tty_struct *tty, u8 c) |
4b1f79c2 PH |
1408 | { |
1409 | struct n_tty_data *ldata = tty->disc_data; | |
4b1f79c2 | 1410 | |
6e94dbc7 | 1411 | if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) { |
e60d27c4 PH |
1412 | start_tty(tty); |
1413 | process_echoes(tty); | |
1414 | } | |
1415 | if (L_ECHO(tty)) { | |
1416 | finish_erasing(ldata); | |
1417 | /* Record the column of first canon char. */ | |
1418 | if (ldata->canon_head == ldata->read_head) | |
1419 | echo_set_canon_col(ldata); | |
1420 | echo_char(c, tty); | |
1421 | commit_echoes(tty); | |
4b1f79c2 | 1422 | } |
001ba923 | 1423 | /* PARMRK doubling check */ |
046b44ab | 1424 | if (c == '\377' && I_PARMRK(tty)) |
e60d27c4 PH |
1425 | put_tty_queue(c, ldata); |
1426 | put_tty_queue(c, ldata); | |
1427 | } | |
4b1f79c2 | 1428 | |
b9b96b20 | 1429 | static void n_tty_receive_char_closing(struct tty_struct *tty, u8 c, |
6bb6fa69 | 1430 | bool lookahead_done) |
ad0cc7ba PH |
1431 | { |
1432 | if (I_ISTRIP(tty)) | |
1433 | c &= 0x7f; | |
1434 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | |
1435 | c = tolower(c); | |
1436 | ||
1437 | if (I_IXON(tty)) { | |
65534736 IJ |
1438 | if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) && |
1439 | tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) && | |
1440 | c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && | |
1441 | c != SUSP_CHAR(tty)) { | |
ad0cc7ba PH |
1442 | start_tty(tty); |
1443 | process_echoes(tty); | |
1444 | } | |
1445 | } | |
1446 | } | |
1447 | ||
d2f8d7ab | 1448 | static void |
b9b96b20 | 1449 | n_tty_receive_char_flagged(struct tty_struct *tty, u8 c, u8 flag) |
d2f8d7ab | 1450 | { |
d2f8d7ab PH |
1451 | switch (flag) { |
1452 | case TTY_BREAK: | |
1453 | n_tty_receive_break(tty); | |
1454 | break; | |
1455 | case TTY_PARITY: | |
1456 | case TTY_FRAME: | |
1457 | n_tty_receive_parity_error(tty, c); | |
1458 | break; | |
1459 | case TTY_OVERRUN: | |
1460 | n_tty_receive_overrun(tty); | |
1461 | break; | |
1462 | default: | |
b9b96b20 | 1463 | tty_err(tty, "unknown flag %u\n", flag); |
d2f8d7ab PH |
1464 | break; |
1465 | } | |
1466 | } | |
1467 | ||
e60d27c4 | 1468 | static void |
b9b96b20 | 1469 | n_tty_receive_char_lnext(struct tty_struct *tty, u8 c, u8 flag) |
e60d27c4 PH |
1470 | { |
1471 | struct n_tty_data *ldata = tty->disc_data; | |
1472 | ||
1473 | ldata->lnext = 0; | |
1474 | if (likely(flag == TTY_NORMAL)) { | |
1475 | if (I_ISTRIP(tty)) | |
1476 | c &= 0x7f; | |
1477 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | |
1478 | c = tolower(c); | |
95aafe32 | 1479 | n_tty_receive_char(tty, c); |
e60d27c4 PH |
1480 | } else |
1481 | n_tty_receive_char_flagged(tty, c, flag); | |
1482 | } | |
1483 | ||
6bb6fa69 | 1484 | /* Caller must ensure count > 0 */ |
a8d9cd23 | 1485 | static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp, |
892bc209 | 1486 | const u8 *fp, size_t count) |
6bb6fa69 IJ |
1487 | { |
1488 | struct n_tty_data *ldata = tty->disc_data; | |
b9b96b20 | 1489 | u8 flag = TTY_NORMAL; |
6bb6fa69 IJ |
1490 | |
1491 | ldata->lookahead_count += count; | |
1492 | ||
1493 | if (!I_IXON(tty)) | |
1494 | return; | |
1495 | ||
1496 | while (count--) { | |
1497 | if (fp) | |
1498 | flag = *fp++; | |
1499 | if (likely(flag == TTY_NORMAL)) | |
1500 | n_tty_receive_char_flow_ctrl(tty, *cp, false); | |
1501 | cp++; | |
1502 | } | |
1503 | } | |
1504 | ||
4a23a4df | 1505 | static void |
a8d9cd23 | 1506 | n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp, |
e30364c7 | 1507 | size_t count) |
4a23a4df PH |
1508 | { |
1509 | struct n_tty_data *ldata = tty->disc_data; | |
a84853c5 JSS |
1510 | |
1511 | /* handle buffer wrap-around by a loop */ | |
1512 | for (unsigned int i = 0; i < 2; i++) { | |
1513 | size_t head = MASK(ldata->read_head); | |
1514 | size_t n = min(count, N_TTY_BUF_SIZE - head); | |
1515 | ||
1516 | memcpy(read_buf_addr(ldata, head), cp, n); | |
1517 | ||
1518 | ldata->read_head += n; | |
1519 | cp += n; | |
1520 | count -= n; | |
1521 | } | |
4a23a4df PH |
1522 | } |
1523 | ||
554117bd | 1524 | static void |
892bc209 | 1525 | n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp, |
e30364c7 | 1526 | size_t count) |
554117bd PH |
1527 | { |
1528 | struct n_tty_data *ldata = tty->disc_data; | |
892bc209 | 1529 | u8 flag = TTY_NORMAL; |
554117bd PH |
1530 | |
1531 | while (count--) { | |
1532 | if (fp) | |
1533 | flag = *fp++; | |
1534 | if (likely(flag == TTY_NORMAL)) | |
1535 | put_tty_queue(*cp++, ldata); | |
1536 | else | |
1537 | n_tty_receive_char_flagged(tty, *cp++, flag); | |
1538 | } | |
1539 | } | |
1540 | ||
ad0cc7ba | 1541 | static void |
892bc209 | 1542 | n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp, |
e30364c7 | 1543 | size_t count, bool lookahead_done) |
ad0cc7ba | 1544 | { |
b9b96b20 | 1545 | u8 flag = TTY_NORMAL; |
ad0cc7ba PH |
1546 | |
1547 | while (count--) { | |
1548 | if (fp) | |
1549 | flag = *fp++; | |
1550 | if (likely(flag == TTY_NORMAL)) | |
6bb6fa69 | 1551 | n_tty_receive_char_closing(tty, *cp++, lookahead_done); |
ad0cc7ba PH |
1552 | } |
1553 | } | |
1554 | ||
a8d9cd23 | 1555 | static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp, |
e30364c7 | 1556 | const u8 *fp, size_t count, |
a8d9cd23 | 1557 | bool lookahead_done) |
6baad008 PH |
1558 | { |
1559 | struct n_tty_data *ldata = tty->disc_data; | |
892bc209 | 1560 | u8 flag = TTY_NORMAL; |
6baad008 PH |
1561 | |
1562 | while (count--) { | |
a8d9cd23 | 1563 | u8 c = *cp++; |
3a7d530a | 1564 | |
6baad008 PH |
1565 | if (fp) |
1566 | flag = *fp++; | |
67a620d5 JS |
1567 | |
1568 | if (ldata->lnext) { | |
3a7d530a | 1569 | n_tty_receive_char_lnext(tty, c, flag); |
67a620d5 JS |
1570 | continue; |
1571 | } | |
1572 | ||
e8f2a139 | 1573 | if (unlikely(flag != TTY_NORMAL)) { |
3a7d530a | 1574 | n_tty_receive_char_flagged(tty, c, flag); |
e8f2a139 JS |
1575 | continue; |
1576 | } | |
1577 | ||
1578 | if (I_ISTRIP(tty)) | |
1579 | c &= 0x7f; | |
1580 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | |
1581 | c = tolower(c); | |
1582 | if (L_EXTPROC(tty)) { | |
1583 | put_tty_queue(c, ldata); | |
1584 | continue; | |
1585 | } | |
1586 | ||
1587 | if (test_bit(c, ldata->char_map)) | |
6bb6fa69 | 1588 | n_tty_receive_char_special(tty, c, lookahead_done); |
e8f2a139 JS |
1589 | else |
1590 | n_tty_receive_char(tty, c); | |
6baad008 PH |
1591 | } |
1592 | } | |
1593 | ||
892bc209 | 1594 | static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp, |
e30364c7 | 1595 | size_t count) |
1da177e4 | 1596 | { |
53c5ee2c | 1597 | struct n_tty_data *ldata = tty->disc_data; |
a1dd30e9 | 1598 | bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty)); |
e30364c7 | 1599 | size_t la_count = min(ldata->lookahead_count, count); |
1da177e4 | 1600 | |
4a23a4df | 1601 | if (ldata->real_raw) |
b30a3d39 | 1602 | n_tty_receive_buf_real_raw(tty, cp, count); |
a1dd30e9 | 1603 | else if (ldata->raw || (L_EXTPROC(tty) && !preops)) |
554117bd | 1604 | n_tty_receive_buf_raw(tty, cp, fp, count); |
6bb6fa69 | 1605 | else if (tty->closing && !L_EXTPROC(tty)) { |
b19ab7ee | 1606 | if (la_count > 0) { |
6bb6fa69 | 1607 | n_tty_receive_buf_closing(tty, cp, fp, la_count, true); |
b19ab7ee IJ |
1608 | cp += la_count; |
1609 | if (fp) | |
1610 | fp += la_count; | |
1611 | count -= la_count; | |
1612 | } | |
1613 | if (count > 0) | |
1614 | n_tty_receive_buf_closing(tty, cp, fp, count, false); | |
6bb6fa69 | 1615 | } else { |
b19ab7ee | 1616 | if (la_count > 0) { |
6bb6fa69 | 1617 | n_tty_receive_buf_standard(tty, cp, fp, la_count, true); |
b19ab7ee IJ |
1618 | cp += la_count; |
1619 | if (fp) | |
1620 | fp += la_count; | |
1621 | count -= la_count; | |
1622 | } | |
1623 | if (count > 0) | |
1624 | n_tty_receive_buf_standard(tty, cp, fp, count, false); | |
cbfd0340 PH |
1625 | |
1626 | flush_echoes(tty); | |
f34d7a5b AC |
1627 | if (tty->ops->flush_chars) |
1628 | tty->ops->flush_chars(tty); | |
1da177e4 LT |
1629 | } |
1630 | ||
6bb6fa69 IJ |
1631 | ldata->lookahead_count -= la_count; |
1632 | ||
70aca71f PH |
1633 | if (ldata->icanon && !L_EXTPROC(tty)) |
1634 | return; | |
1635 | ||
1636 | /* publish read_head to consumer */ | |
1637 | smp_store_release(&ldata->commit_head, ldata->read_head); | |
1638 | ||
33d71363 | 1639 | if (read_cnt(ldata)) { |
1da177e4 | 1640 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
c816b2e6 | 1641 | wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); |
1da177e4 | 1642 | } |
1da177e4 LT |
1643 | } |
1644 | ||
fb5ef9e7 | 1645 | /** |
98629663 JS |
1646 | * n_tty_receive_buf_common - process input |
1647 | * @tty: device to receive input | |
1648 | * @cp: input chars | |
1649 | * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL) | |
1650 | * @count: number of input chars in @cp | |
1651 | * @flow: enable flow control | |
1652 | * | |
1653 | * Called by the terminal driver when a block of characters has been received. | |
1654 | * This function must be called from soft contexts not from interrupt context. | |
1655 | * The driver is responsible for making calls one at a time and in order (or | |
1656 | * using flush_to_ldisc()). | |
1657 | * | |
1658 | * Returns: the # of input chars from @cp which were processed. | |
1659 | * | |
1660 | * In canonical mode, the maximum line length is 4096 chars (including the line | |
1661 | * termination char); lines longer than 4096 chars are truncated. After 4095 | |
1662 | * chars, input data is still processed but not stored. Overflow processing | |
1663 | * ensures the tty can always receive more input until at least one line can be | |
1664 | * read. | |
1665 | * | |
1666 | * In non-canonical mode, the read buffer will only accept 4095 chars; this | |
1667 | * provides the necessary space for a newline char if the input mode is | |
1668 | * switched to canonical. | |
1669 | * | |
1670 | * Note it is possible for the read buffer to _contain_ 4096 chars in | |
1671 | * non-canonical mode: the read buffer could already contain the maximum canon | |
1672 | * line of 4096 chars when the mode is switched to non-canonical. | |
1673 | * | |
1674 | * Locking: n_tty_receive_buf()/producer path: | |
1675 | * claims non-exclusive %termios_rwsem | |
1676 | * publishes commit_head or canon_head | |
fb5ef9e7 | 1677 | */ |
e8161447 | 1678 | static size_t |
892bc209 | 1679 | n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp, |
e30364c7 | 1680 | size_t count, bool flow) |
24a89d1c PH |
1681 | { |
1682 | struct n_tty_data *ldata = tty->disc_data; | |
e30364c7 JSS |
1683 | size_t n, rcvd = 0; |
1684 | int room, overflow; | |
24a89d1c | 1685 | |
9356b535 PH |
1686 | down_read(&tty->termios_rwsem); |
1687 | ||
c96cf923 | 1688 | do { |
70aca71f | 1689 | /* |
06c49f9f PH |
1690 | * When PARMRK is set, each input char may take up to 3 chars |
1691 | * in the read buf; reduce the buffer space avail by 3x | |
70aca71f PH |
1692 | * |
1693 | * If we are doing input canonicalization, and there are no | |
1694 | * pending newlines, let characters through without limit, so | |
1695 | * that erase characters will be handled. Other excess | |
1696 | * characters will be beeped. | |
1697 | * | |
1698 | * paired with store in *_copy_from_read_buf() -- guarantees | |
1699 | * the consumer has loaded the data in read_buf up to the new | |
1700 | * read_tail (so this producer will not overwrite unread data) | |
1701 | */ | |
1702 | size_t tail = smp_load_acquire(&ldata->read_tail); | |
70aca71f | 1703 | |
fb5ef9e7 | 1704 | room = N_TTY_BUF_SIZE - (ldata->read_head - tail); |
70aca71f | 1705 | if (I_PARMRK(tty)) |
7e26c84d | 1706 | room = DIV_ROUND_UP(room, 3); |
fb5ef9e7 PH |
1707 | room--; |
1708 | if (room <= 0) { | |
1709 | overflow = ldata->icanon && ldata->canon_head == tail; | |
1710 | if (overflow && room < 0) | |
1711 | ldata->read_head--; | |
1712 | room = overflow; | |
4903fde8 | 1713 | WRITE_ONCE(ldata->no_room, flow && !room); |
fb5ef9e7 PH |
1714 | } else |
1715 | overflow = 0; | |
70aca71f | 1716 | |
e30364c7 | 1717 | n = min_t(size_t, count, room); |
fb5ef9e7 | 1718 | if (!n) |
19e2ad6a | 1719 | break; |
fb5ef9e7 PH |
1720 | |
1721 | /* ignore parity errors if handling overflow */ | |
1722 | if (!overflow || !fp || *fp != TTY_PARITY) | |
1723 | __receive_buf(tty, cp, fp, n); | |
1724 | ||
19e2ad6a PH |
1725 | cp += n; |
1726 | if (fp) | |
1727 | fp += n; | |
1728 | count -= n; | |
1729 | rcvd += n; | |
c96cf923 | 1730 | } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags)); |
24a89d1c | 1731 | |
19e2ad6a | 1732 | tty->receive_room = room; |
fb5ef9e7 PH |
1733 | |
1734 | /* Unthrottle if handling overflow on pty */ | |
1735 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY) { | |
1736 | if (overflow) { | |
1737 | tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); | |
1738 | tty_unthrottle_safe(tty); | |
1739 | __tty_set_flow_change(tty, 0); | |
1740 | } | |
1741 | } else | |
1742 | n_tty_check_throttle(tty); | |
1743 | ||
4903fde8 HL |
1744 | if (unlikely(ldata->no_room)) { |
1745 | /* | |
1746 | * Barrier here is to ensure to read the latest read_tail in | |
1747 | * chars_in_buffer() and to make sure that read_tail is not loaded | |
1748 | * before ldata->no_room is set. | |
1749 | */ | |
1750 | smp_mb(); | |
1751 | if (!chars_in_buffer(tty)) | |
1752 | n_tty_kick_worker(tty); | |
1753 | } | |
1754 | ||
9356b535 PH |
1755 | up_read(&tty->termios_rwsem); |
1756 | ||
19e2ad6a | 1757 | return rcvd; |
24a89d1c PH |
1758 | } |
1759 | ||
a8d9cd23 | 1760 | static void n_tty_receive_buf(struct tty_struct *tty, const u8 *cp, |
892bc209 | 1761 | const u8 *fp, size_t count) |
5c32d123 | 1762 | { |
0d029ab8 | 1763 | n_tty_receive_buf_common(tty, cp, fp, count, false); |
5c32d123 PH |
1764 | } |
1765 | ||
a8d9cd23 | 1766 | static size_t n_tty_receive_buf2(struct tty_struct *tty, const u8 *cp, |
892bc209 | 1767 | const u8 *fp, size_t count) |
5c32d123 | 1768 | { |
0d029ab8 | 1769 | return n_tty_receive_buf_common(tty, cp, fp, count, true); |
5c32d123 PH |
1770 | } |
1771 | ||
1da177e4 | 1772 | /** |
98629663 JS |
1773 | * n_tty_set_termios - termios data changed |
1774 | * @tty: terminal | |
1775 | * @old: previous data | |
1da177e4 | 1776 | * |
98629663 JS |
1777 | * Called by the tty layer when the user changes termios flags so that the line |
1778 | * discipline can plan ahead. This function cannot sleep and is protected from | |
1779 | * re-entry by the tty layer. The user is guaranteed that this function will | |
1780 | * not be re-entered or in progress when the ldisc is closed. | |
17b82060 | 1781 | * |
98629663 | 1782 | * Locking: Caller holds @tty->termios_rwsem |
1da177e4 | 1783 | */ |
8b7d2d95 | 1784 | static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old) |
1da177e4 | 1785 | { |
53c5ee2c | 1786 | struct n_tty_data *ldata = tty->disc_data; |
47afa7a5 | 1787 | |
966031f3 | 1788 | if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) { |
3fe780b3 | 1789 | bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); |
4d0ed182 PH |
1790 | ldata->line_start = ldata->read_tail; |
1791 | if (!L_ICANON(tty) || !read_cnt(ldata)) { | |
1792 | ldata->canon_head = ldata->read_tail; | |
1793 | ldata->push = 0; | |
1794 | } else { | |
819287f0 | 1795 | set_bit(MASK(ldata->read_head - 1), ldata->read_flags); |
4d0ed182 PH |
1796 | ldata->canon_head = ldata->read_head; |
1797 | ldata->push = 1; | |
1798 | } | |
70aca71f | 1799 | ldata->commit_head = ldata->read_head; |
53c5ee2c | 1800 | ldata->erasing = 0; |
6f9b028a | 1801 | ldata->lnext = 0; |
47afa7a5 AC |
1802 | } |
1803 | ||
53c5ee2c | 1804 | ldata->icanon = (L_ICANON(tty) != 0); |
582f5590 | 1805 | |
1da177e4 LT |
1806 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || |
1807 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | |
1808 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | |
1809 | I_PARMRK(tty)) { | |
1bb9d562 | 1810 | bitmap_zero(ldata->char_map, 256); |
1da177e4 LT |
1811 | |
1812 | if (I_IGNCR(tty) || I_ICRNL(tty)) | |
1bb9d562 | 1813 | set_bit('\r', ldata->char_map); |
1da177e4 | 1814 | if (I_INLCR(tty)) |
1bb9d562 | 1815 | set_bit('\n', ldata->char_map); |
1da177e4 LT |
1816 | |
1817 | if (L_ICANON(tty)) { | |
1bb9d562 PH |
1818 | set_bit(ERASE_CHAR(tty), ldata->char_map); |
1819 | set_bit(KILL_CHAR(tty), ldata->char_map); | |
1820 | set_bit(EOF_CHAR(tty), ldata->char_map); | |
1821 | set_bit('\n', ldata->char_map); | |
1822 | set_bit(EOL_CHAR(tty), ldata->char_map); | |
1da177e4 | 1823 | if (L_IEXTEN(tty)) { |
1bb9d562 PH |
1824 | set_bit(WERASE_CHAR(tty), ldata->char_map); |
1825 | set_bit(LNEXT_CHAR(tty), ldata->char_map); | |
1826 | set_bit(EOL2_CHAR(tty), ldata->char_map); | |
1da177e4 LT |
1827 | if (L_ECHO(tty)) |
1828 | set_bit(REPRINT_CHAR(tty), | |
1bb9d562 | 1829 | ldata->char_map); |
1da177e4 LT |
1830 | } |
1831 | } | |
1832 | if (I_IXON(tty)) { | |
1bb9d562 PH |
1833 | set_bit(START_CHAR(tty), ldata->char_map); |
1834 | set_bit(STOP_CHAR(tty), ldata->char_map); | |
1da177e4 LT |
1835 | } |
1836 | if (L_ISIG(tty)) { | |
1bb9d562 PH |
1837 | set_bit(INTR_CHAR(tty), ldata->char_map); |
1838 | set_bit(QUIT_CHAR(tty), ldata->char_map); | |
1839 | set_bit(SUSP_CHAR(tty), ldata->char_map); | |
1da177e4 | 1840 | } |
1bb9d562 | 1841 | clear_bit(__DISABLED_CHAR, ldata->char_map); |
53c5ee2c JS |
1842 | ldata->raw = 0; |
1843 | ldata->real_raw = 0; | |
1da177e4 | 1844 | } else { |
53c5ee2c | 1845 | ldata->raw = 1; |
1da177e4 LT |
1846 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && |
1847 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | |
1848 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | |
53c5ee2c | 1849 | ldata->real_raw = 1; |
1da177e4 | 1850 | else |
53c5ee2c | 1851 | ldata->real_raw = 0; |
1da177e4 | 1852 | } |
dab73b4e WY |
1853 | /* |
1854 | * Fix tty hang when I_IXON(tty) is cleared, but the tty | |
1855 | * been stopped by STOP_CHAR(tty) before it. | |
1856 | */ | |
6e94dbc7 | 1857 | if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) { |
dab73b4e | 1858 | start_tty(tty); |
e2613be5 PH |
1859 | process_echoes(tty); |
1860 | } | |
dab73b4e | 1861 | |
f34d7a5b | 1862 | /* The termios change make the tty ready for I/O */ |
e81107d4 KT |
1863 | wake_up_interruptible(&tty->write_wait); |
1864 | wake_up_interruptible(&tty->read_wait); | |
1da177e4 LT |
1865 | } |
1866 | ||
1867 | /** | |
98629663 JS |
1868 | * n_tty_close - close the ldisc for this tty |
1869 | * @tty: device | |
1da177e4 | 1870 | * |
98629663 JS |
1871 | * Called from the terminal layer when this line discipline is being shut down, |
1872 | * either because of a close or becsuse of a discipline change. The function | |
1873 | * will not be called while other ldisc methods are in progress. | |
1da177e4 | 1874 | */ |
1da177e4 LT |
1875 | static void n_tty_close(struct tty_struct *tty) |
1876 | { | |
70ece7a7 JS |
1877 | struct n_tty_data *ldata = tty->disc_data; |
1878 | ||
79901317 PH |
1879 | if (tty->link) |
1880 | n_tty_packet_mode_flush(tty); | |
1881 | ||
c9cd57bf | 1882 | down_write(&tty->termios_rwsem); |
20bafb3d | 1883 | vfree(ldata); |
70ece7a7 | 1884 | tty->disc_data = NULL; |
c9cd57bf | 1885 | up_write(&tty->termios_rwsem); |
1da177e4 LT |
1886 | } |
1887 | ||
1888 | /** | |
98629663 JS |
1889 | * n_tty_open - open an ldisc |
1890 | * @tty: terminal to open | |
1da177e4 | 1891 | * |
98629663 JS |
1892 | * Called when this line discipline is being attached to the terminal device. |
1893 | * Can sleep. Called serialized so that no other events will occur in parallel. | |
1894 | * No further open will occur until a close. | |
1da177e4 | 1895 | */ |
1da177e4 LT |
1896 | static int n_tty_open(struct tty_struct *tty) |
1897 | { | |
70ece7a7 JS |
1898 | struct n_tty_data *ldata; |
1899 | ||
20bafb3d | 1900 | /* Currently a malloc failure here can panic */ |
ebec3f8f | 1901 | ldata = vzalloc(sizeof(*ldata)); |
70ece7a7 | 1902 | if (!ldata) |
ebec3f8f | 1903 | return -ENOMEM; |
70ece7a7 | 1904 | |
53c5ee2c | 1905 | ldata->overrun_time = jiffies; |
bddc7152 JS |
1906 | mutex_init(&ldata->atomic_read_lock); |
1907 | mutex_init(&ldata->output_lock); | |
53c5ee2c | 1908 | |
70ece7a7 | 1909 | tty->disc_data = ldata; |
1da177e4 | 1910 | tty->closing = 0; |
b66f4fa5 PH |
1911 | /* indicate buffer work may resume */ |
1912 | clear_bit(TTY_LDISC_HALTED, &tty->flags); | |
1913 | n_tty_set_termios(tty, NULL); | |
1914 | tty_unthrottle(tty); | |
1da177e4 LT |
1915 | return 0; |
1916 | } | |
1917 | ||
5bedcf70 | 1918 | static inline int input_available_p(const struct tty_struct *tty, int poll) |
1da177e4 | 1919 | { |
5bedcf70 | 1920 | const struct n_tty_data *ldata = tty->disc_data; |
a5934804 | 1921 | int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1; |
53c5ee2c | 1922 | |
25e8d0ed PH |
1923 | if (ldata->icanon && !L_EXTPROC(tty)) |
1924 | return ldata->canon_head != ldata->read_tail; | |
1925 | else | |
70aca71f | 1926 | return ldata->commit_head - ldata->read_tail >= amt; |
1da177e4 LT |
1927 | } |
1928 | ||
1929 | /** | |
98629663 JS |
1930 | * copy_from_read_buf - copy read data directly |
1931 | * @tty: terminal device | |
1932 | * @kbp: data | |
1933 | * @nr: size of data | |
1da177e4 | 1934 | * |
98629663 JS |
1935 | * Helper function to speed up n_tty_read(). It is only called when %ICANON is |
1936 | * off; it copies characters straight from the tty queue. | |
1da177e4 | 1937 | * |
98629663 JS |
1938 | * Returns: true if it successfully copied data, but there is still more data |
1939 | * to be had. | |
15ea8ae8 | 1940 | * |
98629663 JS |
1941 | * Locking: |
1942 | * * called under the @ldata->atomic_read_lock sem | |
1943 | * * n_tty_read()/consumer path: | |
1944 | * caller holds non-exclusive %termios_rwsem; | |
6d76bd26 | 1945 | * read_tail published |
1da177e4 | 1946 | */ |
e30364c7 JSS |
1947 | static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp, |
1948 | size_t *nr) | |
1da177e4 LT |
1949 | |
1950 | { | |
53c5ee2c | 1951 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 | 1952 | size_t n; |
3fa10cc8 | 1953 | bool is_eof; |
70aca71f | 1954 | size_t head = smp_load_acquire(&ldata->commit_head); |
819287f0 | 1955 | size_t tail = MASK(ldata->read_tail); |
1da177e4 | 1956 | |
72369f2d | 1957 | n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr); |
c2b0fb9f JSS |
1958 | if (!n) |
1959 | return false; | |
1960 | ||
1961 | u8 *from = read_buf_addr(ldata, tail); | |
1962 | memcpy(*kbp, from, n); | |
1963 | is_eof = n == 1 && *from == EOF_CHAR(tty); | |
1964 | tty_audit_add_data(tty, from, n); | |
1965 | zero_buffer(tty, from, n); | |
1966 | smp_store_release(&ldata->read_tail, ldata->read_tail + n); | |
1967 | ||
1968 | /* Turn single EOF into zero-length read */ | |
1969 | if (L_EXTPROC(tty) && ldata->icanon && is_eof && | |
1970 | head == ldata->read_tail) | |
1971 | return false; | |
1972 | ||
1973 | *kbp += n; | |
1974 | *nr -= n; | |
1975 | ||
1976 | /* If we have more to copy, let the caller know */ | |
1977 | return head != ldata->read_tail; | |
1da177e4 LT |
1978 | } |
1979 | ||
88bb0de3 | 1980 | /** |
98629663 JS |
1981 | * canon_copy_from_read_buf - copy read data in canonical mode |
1982 | * @tty: terminal device | |
1983 | * @kbp: data | |
1984 | * @nr: size of data | |
1985 | * | |
1986 | * Helper function for n_tty_read(). It is only called when %ICANON is on; it | |
1987 | * copies one line of input up to and including the line-delimiting character | |
1988 | * into the result buffer. | |
1989 | * | |
1990 | * Note: When termios is changed from non-canonical to canonical mode and the | |
1991 | * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if | |
1992 | * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data | |
1993 | * already processed as input to be immediately available as input although a | |
1994 | * newline has not been received. | |
1995 | * | |
1996 | * Locking: | |
1997 | * * called under the %atomic_read_lock mutex | |
1998 | * * n_tty_read()/consumer path: | |
1999 | * caller holds non-exclusive %termios_rwsem; | |
2000 | * read_tail published | |
88bb0de3 | 2001 | */ |
e30364c7 | 2002 | static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp, |
64a69892 | 2003 | size_t *nr) |
88bb0de3 PH |
2004 | { |
2005 | struct n_tty_data *ldata = tty->disc_data; | |
32f13521 | 2006 | size_t n, size, more, c; |
bc5a5e3f | 2007 | size_t eol; |
d7fe75cb | 2008 | size_t tail, canon_head; |
3b830a9c | 2009 | int found = 0; |
88bb0de3 PH |
2010 | |
2011 | /* N.B. avoid overrun if nr == 0 */ | |
ac8f3bf8 | 2012 | if (!*nr) |
d7fe75cb | 2013 | return false; |
88bb0de3 | 2014 | |
d7fe75cb | 2015 | canon_head = smp_load_acquire(&ldata->canon_head); |
35930307 | 2016 | n = min(*nr, canon_head - ldata->read_tail); |
ac8f3bf8 | 2017 | |
819287f0 | 2018 | tail = MASK(ldata->read_tail); |
32f13521 PH |
2019 | size = min_t(size_t, tail + n, N_TTY_BUF_SIZE); |
2020 | ||
32f13521 PH |
2021 | eol = find_next_bit(ldata->read_flags, size, tail); |
2022 | more = n - (size - tail); | |
2023 | if (eol == N_TTY_BUF_SIZE && more) { | |
2024 | /* scan wrapped without finding set bit */ | |
b5c7e7ec | 2025 | eol = find_first_bit(ldata->read_flags, more); |
b985e9e3 PH |
2026 | found = eol != more; |
2027 | } else | |
2028 | found = eol != size; | |
32f13521 | 2029 | |
c77569d2 | 2030 | n = eol - tail; |
da555db6 MT |
2031 | if (n > N_TTY_BUF_SIZE) |
2032 | n += N_TTY_BUF_SIZE; | |
ac8f3bf8 | 2033 | c = n + found; |
32f13521 | 2034 | |
35930307 | 2035 | if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) |
ac8f3bf8 | 2036 | n = c; |
32f13521 | 2037 | |
3b830a9c LT |
2038 | tty_copy(tty, *kbp, tail, n); |
2039 | *kbp += n; | |
32f13521 PH |
2040 | *nr -= n; |
2041 | ||
a73d3d69 | 2042 | if (found) |
6d76bd26 | 2043 | clear_bit(eol, ldata->read_flags); |
70aca71f | 2044 | smp_store_release(&ldata->read_tail, ldata->read_tail + c); |
88bb0de3 | 2045 | |
40d5e090 | 2046 | if (found) { |
4d0ed182 PH |
2047 | if (!ldata->push) |
2048 | ldata->line_start = ldata->read_tail; | |
2049 | else | |
2050 | ldata->push = 0; | |
b50819f4 | 2051 | tty_audit_push(); |
d7fe75cb | 2052 | return false; |
40d5e090 | 2053 | } |
d7fe75cb LT |
2054 | |
2055 | /* No EOL found - do a continuation retry if there is more data */ | |
2056 | return ldata->read_tail != canon_head; | |
88bb0de3 PH |
2057 | } |
2058 | ||
65a8b287 DG |
2059 | /* |
2060 | * If we finished a read at the exact location of an | |
2061 | * EOF (special EOL character that's a __DISABLED_CHAR) | |
2062 | * in the stream, silently eat the EOF. | |
2063 | */ | |
32042446 | 2064 | static void canon_skip_eof(struct n_tty_data *ldata) |
65a8b287 | 2065 | { |
65a8b287 DG |
2066 | size_t tail, canon_head; |
2067 | ||
2068 | canon_head = smp_load_acquire(&ldata->canon_head); | |
2069 | tail = ldata->read_tail; | |
2070 | ||
2071 | // No data? | |
2072 | if (tail == canon_head) | |
2073 | return; | |
2074 | ||
2075 | // See if the tail position is EOF in the circular buffer | |
2076 | tail &= (N_TTY_BUF_SIZE - 1); | |
2077 | if (!test_bit(tail, ldata->read_flags)) | |
2078 | return; | |
2079 | if (read_buf(ldata, tail) != __DISABLED_CHAR) | |
2080 | return; | |
2081 | ||
2082 | // Clear the EOL bit, skip the EOF char. | |
2083 | clear_bit(tail, ldata->read_flags); | |
2084 | smp_store_release(&ldata->read_tail, ldata->read_tail + 1); | |
2085 | } | |
2086 | ||
1da177e4 | 2087 | /** |
98629663 JS |
2088 | * job_control - check job control |
2089 | * @tty: tty | |
2090 | * @file: file handle | |
2091 | * | |
2092 | * Perform job control management checks on this @file/@tty descriptor and if | |
2093 | * appropriate send any needed signals and return a negative error code if | |
2094 | * action should be taken. | |
2095 | * | |
2096 | * Locking: | |
2097 | * * redirected write test is safe | |
2098 | * * current->signal->tty check is safe | |
2099 | * * ctrl.lock to safely reference @tty->ctrl.pgrp | |
1da177e4 | 2100 | */ |
1da177e4 LT |
2101 | static int job_control(struct tty_struct *tty, struct file *file) |
2102 | { | |
2103 | /* Job control check -- must be done at start and after | |
2104 | every sleep (POSIX.1 7.1.1.4). */ | |
2105 | /* NOTE: not yet done after every sleep pending a thorough | |
2106 | check of the logic of this change. -- jlc */ | |
2107 | /* don't stop on /dev/console */ | |
9f12e37c | 2108 | if (file->f_op->write_iter == redirected_tty_write) |
01a5e440 PH |
2109 | return 0; |
2110 | ||
2812d9e9 | 2111 | return __tty_check_change(tty, SIGTTIN); |
1da177e4 | 2112 | } |
4edf1827 | 2113 | |
aa1ebc9c JSS |
2114 | /* |
2115 | * We still hold the atomic_read_lock and the termios_rwsem, and can just | |
2116 | * continue to copy data. | |
2117 | */ | |
2118 | static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf, | |
2119 | size_t nr, void **cookie) | |
2120 | { | |
2121 | struct n_tty_data *ldata = tty->disc_data; | |
2122 | u8 *kb = kbuf; | |
2123 | ||
2124 | if (ldata->icanon && !L_EXTPROC(tty)) { | |
2125 | /* | |
2126 | * If we have filled the user buffer, see if we should skip an | |
2127 | * EOF character before releasing the lock and returning done. | |
2128 | */ | |
2129 | if (!nr) | |
2130 | canon_skip_eof(ldata); | |
2131 | else if (canon_copy_from_read_buf(tty, &kb, &nr)) | |
2132 | return kb - kbuf; | |
2133 | } else { | |
2134 | if (copy_from_read_buf(tty, &kb, &nr)) | |
2135 | return kb - kbuf; | |
2136 | } | |
2137 | ||
2138 | /* No more data - release locks and stop retries */ | |
2139 | n_tty_kick_worker(tty); | |
2140 | n_tty_check_unthrottle(tty); | |
2141 | up_read(&tty->termios_rwsem); | |
2142 | mutex_unlock(&ldata->atomic_read_lock); | |
2143 | *cookie = NULL; | |
2144 | ||
2145 | return kb - kbuf; | |
2146 | } | |
1da177e4 | 2147 | |
40315ce5 JSS |
2148 | static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file, |
2149 | struct wait_queue_entry *wait, long *timeout) | |
2150 | { | |
2151 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
2152 | return -EIO; | |
2153 | if (tty_hung_up_p(file)) | |
2154 | return 0; | |
2155 | /* | |
2156 | * Abort readers for ttys which never actually get hung up. | |
2157 | * See __tty_hangup(). | |
2158 | */ | |
2159 | if (test_bit(TTY_HUPPING, &tty->flags)) | |
2160 | return 0; | |
2161 | if (!*timeout) | |
2162 | return 0; | |
2163 | if (tty_io_nonblock(tty, file)) | |
2164 | return -EAGAIN; | |
2165 | if (signal_pending(current)) | |
2166 | return -ERESTARTSYS; | |
2167 | ||
2168 | up_read(&tty->termios_rwsem); | |
2169 | *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout); | |
2170 | down_read(&tty->termios_rwsem); | |
2171 | ||
2172 | return 1; | |
2173 | } | |
2174 | ||
1da177e4 | 2175 | /** |
98629663 JS |
2176 | * n_tty_read - read function for tty |
2177 | * @tty: tty device | |
2178 | * @file: file object | |
2179 | * @kbuf: kernelspace buffer pointer | |
2180 | * @nr: size of I/O | |
2181 | * @cookie: if non-%NULL, this is a continuation read | |
2182 | * @offset: where to continue reading from (unused in n_tty) | |
2183 | * | |
2184 | * Perform reads for the line discipline. We are guaranteed that the line | |
2185 | * discipline will not be closed under us but we may get multiple parallel | |
2186 | * readers and must handle this ourselves. We may also get a hangup. Always | |
2187 | * called in user context, may sleep. | |
2188 | * | |
2189 | * This code must be sure never to sleep through a hangup. | |
2190 | * | |
2191 | * Locking: n_tty_read()/consumer path: | |
2192 | * claims non-exclusive termios_rwsem; | |
2193 | * publishes read_tail | |
1da177e4 | 2194 | */ |
49b8220c JSS |
2195 | static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf, |
2196 | size_t nr, void **cookie, unsigned long offset) | |
1da177e4 | 2197 | { |
53c5ee2c | 2198 | struct n_tty_data *ldata = tty->disc_data; |
49b8220c | 2199 | u8 *kb = kbuf; |
97d9e28d | 2200 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
1da177e4 | 2201 | int minimum, time; |
4a2ad266 | 2202 | ssize_t retval; |
1da177e4 | 2203 | long timeout; |
64d608db | 2204 | bool packet; |
947d66b6 | 2205 | size_t old_tail; |
1da177e4 | 2206 | |
aa1ebc9c JSS |
2207 | /* Is this a continuation of a read started earlier? */ |
2208 | if (*cookie) | |
2209 | return n_tty_continue_cookie(tty, kbuf, nr, cookie); | |
15ea8ae8 | 2210 | |
4a2ad266 JSS |
2211 | retval = job_control(tty, file); |
2212 | if (retval < 0) | |
2213 | return retval; | |
4edf1827 | 2214 | |
aefceaf4 PH |
2215 | /* |
2216 | * Internal serialization of reads. | |
2217 | */ | |
2218 | if (file->f_flags & O_NONBLOCK) { | |
2219 | if (!mutex_trylock(&ldata->atomic_read_lock)) | |
2220 | return -EAGAIN; | |
2221 | } else { | |
2222 | if (mutex_lock_interruptible(&ldata->atomic_read_lock)) | |
2223 | return -ERESTARTSYS; | |
2224 | } | |
2225 | ||
9356b535 PH |
2226 | down_read(&tty->termios_rwsem); |
2227 | ||
1da177e4 LT |
2228 | minimum = time = 0; |
2229 | timeout = MAX_SCHEDULE_TIMEOUT; | |
53c5ee2c | 2230 | if (!ldata->icanon) { |
1da177e4 LT |
2231 | minimum = MIN_CHAR(tty); |
2232 | if (minimum) { | |
a6e54319 | 2233 | time = (HZ / 10) * TIME_CHAR(tty); |
1da177e4 | 2234 | } else { |
a6e54319 | 2235 | timeout = (HZ / 10) * TIME_CHAR(tty); |
33d71363 | 2236 | minimum = 1; |
1da177e4 LT |
2237 | } |
2238 | } | |
2239 | ||
64d608db | 2240 | packet = tty->ctrl.packet; |
947d66b6 | 2241 | old_tail = ldata->read_tail; |
1da177e4 LT |
2242 | |
2243 | add_wait_queue(&tty->read_wait, &wait); | |
1da177e4 LT |
2244 | while (nr) { |
2245 | /* First test for status change. */ | |
64d608db | 2246 | if (packet && tty->link->ctrl.pktstatus) { |
b9b96b20 | 2247 | u8 cs; |
3b830a9c | 2248 | if (kb != kbuf) |
1da177e4 | 2249 | break; |
64d608db JS |
2250 | spin_lock_irq(&tty->link->ctrl.lock); |
2251 | cs = tty->link->ctrl.pktstatus; | |
2252 | tty->link->ctrl.pktstatus = 0; | |
2253 | spin_unlock_irq(&tty->link->ctrl.lock); | |
3b830a9c | 2254 | *kb++ = cs; |
1da177e4 LT |
2255 | nr--; |
2256 | break; | |
2257 | } | |
4edf1827 | 2258 | |
1da177e4 | 2259 | if (!input_available_p(tty, 0)) { |
52bce7f8 | 2260 | up_read(&tty->termios_rwsem); |
0f40fbbc BB |
2261 | tty_buffer_flush_work(tty->port); |
2262 | down_read(&tty->termios_rwsem); | |
2263 | if (!input_available_p(tty, 0)) { | |
40315ce5 JSS |
2264 | int ret = n_tty_wait_for_input(tty, file, &wait, |
2265 | &timeout); | |
2266 | if (ret <= 0) { | |
2267 | retval = ret; | |
0f40fbbc BB |
2268 | break; |
2269 | } | |
0f40fbbc BB |
2270 | continue; |
2271 | } | |
1da177e4 LT |
2272 | } |
2273 | ||
53c5ee2c | 2274 | if (ldata->icanon && !L_EXTPROC(tty)) { |
d7fe75cb LT |
2275 | if (canon_copy_from_read_buf(tty, &kb, &nr)) |
2276 | goto more_to_be_read; | |
1da177e4 | 2277 | } else { |
95ea90db | 2278 | /* Deal with packet mode. */ |
3b830a9c LT |
2279 | if (packet && kb == kbuf) { |
2280 | *kb++ = TIOCPKT_DATA; | |
95ea90db PH |
2281 | nr--; |
2282 | } | |
2283 | ||
f812af36 JSS |
2284 | if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) |
2285 | goto more_to_be_read; | |
1da177e4 LT |
2286 | } |
2287 | ||
6367ca72 | 2288 | n_tty_check_unthrottle(tty); |
1da177e4 | 2289 | |
3b830a9c | 2290 | if (kb - kbuf >= minimum) |
1da177e4 LT |
2291 | break; |
2292 | if (time) | |
2293 | timeout = time; | |
2294 | } | |
4903fde8 HL |
2295 | if (old_tail != ldata->read_tail) { |
2296 | /* | |
2297 | * Make sure no_room is not read in n_tty_kick_worker() | |
2298 | * before setting ldata->read_tail in copy_from_read_buf(). | |
2299 | */ | |
2300 | smp_mb(); | |
2c5dc464 | 2301 | n_tty_kick_worker(tty); |
4903fde8 | 2302 | } |
42458f41 PH |
2303 | up_read(&tty->termios_rwsem); |
2304 | ||
1da177e4 | 2305 | remove_wait_queue(&tty->read_wait, &wait); |
aebf0453 PH |
2306 | mutex_unlock(&ldata->atomic_read_lock); |
2307 | ||
3b830a9c LT |
2308 | if (kb - kbuf) |
2309 | retval = kb - kbuf; | |
1da177e4 LT |
2310 | |
2311 | return retval; | |
f812af36 JSS |
2312 | more_to_be_read: |
2313 | /* | |
2314 | * There is more to be had and we have nothing more to wait for, so | |
2315 | * let's mark us for retries. | |
2316 | * | |
2317 | * NOTE! We return here with both the termios_sem and atomic_read_lock | |
2318 | * still held, the retries will release them when done. | |
2319 | */ | |
2320 | remove_wait_queue(&tty->read_wait, &wait); | |
2321 | *cookie = cookie; | |
2322 | ||
2323 | return kb - kbuf; | |
1da177e4 LT |
2324 | } |
2325 | ||
2326 | /** | |
98629663 JS |
2327 | * n_tty_write - write function for tty |
2328 | * @tty: tty device | |
2329 | * @file: file object | |
2330 | * @buf: userspace buffer pointer | |
2331 | * @nr: size of I/O | |
2332 | * | |
2333 | * Write function of the terminal device. This is serialized with respect to | |
2334 | * other write callers but not to termios changes, reads and other such events. | |
2335 | * Since the receive code will echo characters, thus calling driver write | |
2336 | * methods, the %output_lock is used in the output processing functions called | |
2337 | * here as well as in the echo processing function to protect the column state | |
2338 | * and space left in the buffer. | |
2339 | * | |
2340 | * This code must be sure never to sleep through a hangup. | |
2341 | * | |
2342 | * Locking: output_lock to protect column state and space left | |
2343 | * (note that the process_output*() functions take this lock themselves) | |
1da177e4 | 2344 | */ |
4edf1827 | 2345 | |
11a96d18 | 2346 | static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, |
49b8220c | 2347 | const u8 *buf, size_t nr) |
1da177e4 | 2348 | { |
49b8220c | 2349 | const u8 *b = buf; |
97d9e28d | 2350 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
68d90d5f | 2351 | ssize_t num, retval = 0; |
1da177e4 LT |
2352 | |
2353 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | |
9f12e37c | 2354 | if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) { |
1da177e4 LT |
2355 | retval = tty_check_change(tty); |
2356 | if (retval) | |
2357 | return retval; | |
2358 | } | |
2359 | ||
9356b535 PH |
2360 | down_read(&tty->termios_rwsem); |
2361 | ||
a88a69c9 JP |
2362 | /* Write out any echoed characters that are still pending */ |
2363 | process_echoes(tty); | |
300a6204 | 2364 | |
1da177e4 LT |
2365 | add_wait_queue(&tty->write_wait, &wait); |
2366 | while (1) { | |
1da177e4 LT |
2367 | if (signal_pending(current)) { |
2368 | retval = -ERESTARTSYS; | |
2369 | break; | |
2370 | } | |
2371 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | |
2372 | retval = -EIO; | |
2373 | break; | |
2374 | } | |
582f5590 | 2375 | if (O_OPOST(tty)) { |
1da177e4 | 2376 | while (nr > 0) { |
68d90d5f | 2377 | num = process_output_block(tty, b, nr); |
1da177e4 LT |
2378 | if (num < 0) { |
2379 | if (num == -EAGAIN) | |
2380 | break; | |
2381 | retval = num; | |
2382 | goto break_out; | |
2383 | } | |
2384 | b += num; | |
2385 | nr -= num; | |
2386 | if (nr == 0) | |
2387 | break; | |
d414034e | 2388 | if (process_output(*b, tty) < 0) |
1da177e4 LT |
2389 | break; |
2390 | b++; nr--; | |
2391 | } | |
f34d7a5b AC |
2392 | if (tty->ops->flush_chars) |
2393 | tty->ops->flush_chars(tty); | |
1da177e4 | 2394 | } else { |
4291086b PH |
2395 | struct n_tty_data *ldata = tty->disc_data; |
2396 | ||
d6afe27b | 2397 | while (nr > 0) { |
4291086b | 2398 | mutex_lock(&ldata->output_lock); |
68d90d5f | 2399 | num = tty->ops->write(tty, b, nr); |
4291086b | 2400 | mutex_unlock(&ldata->output_lock); |
68d90d5f JSS |
2401 | if (num < 0) { |
2402 | retval = num; | |
d6afe27b RZ |
2403 | goto break_out; |
2404 | } | |
68d90d5f | 2405 | if (!num) |
d6afe27b | 2406 | break; |
68d90d5f JSS |
2407 | b += num; |
2408 | nr -= num; | |
1da177e4 | 2409 | } |
1da177e4 LT |
2410 | } |
2411 | if (!nr) | |
2412 | break; | |
c96cf923 | 2413 | if (tty_io_nonblock(tty, file)) { |
1da177e4 LT |
2414 | retval = -EAGAIN; |
2415 | break; | |
2416 | } | |
9356b535 PH |
2417 | up_read(&tty->termios_rwsem); |
2418 | ||
97d9e28d | 2419 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
9356b535 PH |
2420 | |
2421 | down_read(&tty->termios_rwsem); | |
1da177e4 LT |
2422 | } |
2423 | break_out: | |
1da177e4 | 2424 | remove_wait_queue(&tty->write_wait, &wait); |
87108bc9 | 2425 | if (nr && tty->fasync) |
ff8cb0fd | 2426 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
9356b535 | 2427 | up_read(&tty->termios_rwsem); |
1da177e4 LT |
2428 | return (b - buf) ? b - buf : retval; |
2429 | } | |
2430 | ||
2431 | /** | |
98629663 JS |
2432 | * n_tty_poll - poll method for N_TTY |
2433 | * @tty: terminal device | |
2434 | * @file: file accessing it | |
2435 | * @wait: poll table | |
1da177e4 | 2436 | * |
98629663 JS |
2437 | * Called when the line discipline is asked to poll() for data or for special |
2438 | * events. This code is not serialized with respect to other events save | |
2439 | * open/close. | |
1da177e4 | 2440 | * |
98629663 JS |
2441 | * This code must be sure never to sleep through a hangup. |
2442 | * | |
2443 | * Locking: called without the kernel lock held -- fine. | |
1da177e4 | 2444 | */ |
afc9a42b | 2445 | static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file, |
4edf1827 | 2446 | poll_table *wait) |
1da177e4 | 2447 | { |
afc9a42b | 2448 | __poll_t mask = 0; |
1da177e4 LT |
2449 | |
2450 | poll_wait(file, &tty->read_wait, wait); | |
2451 | poll_wait(file, &tty->write_wait, wait); | |
eafbe67f | 2452 | if (input_available_p(tty, 1)) |
a9a08845 | 2453 | mask |= EPOLLIN | EPOLLRDNORM; |
0f40fbbc BB |
2454 | else { |
2455 | tty_buffer_flush_work(tty->port); | |
2456 | if (input_available_p(tty, 1)) | |
a9a08845 | 2457 | mask |= EPOLLIN | EPOLLRDNORM; |
0f40fbbc | 2458 | } |
64d608db | 2459 | if (tty->ctrl.packet && tty->link->ctrl.pktstatus) |
a9a08845 | 2460 | mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM; |
0f40fbbc | 2461 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
a9a08845 | 2462 | mask |= EPOLLHUP; |
1da177e4 | 2463 | if (tty_hung_up_p(file)) |
a9a08845 | 2464 | mask |= EPOLLHUP; |
f34d7a5b AC |
2465 | if (tty->ops->write && !tty_is_writelocked(tty) && |
2466 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && | |
2467 | tty_write_room(tty) > 0) | |
a9a08845 | 2468 | mask |= EPOLLOUT | EPOLLWRNORM; |
1da177e4 LT |
2469 | return mask; |
2470 | } | |
2471 | ||
57c94121 | 2472 | static unsigned long inq_canon(struct n_tty_data *ldata) |
47afa7a5 | 2473 | { |
bc5a5e3f | 2474 | size_t nr, head, tail; |
47afa7a5 | 2475 | |
a73d3d69 | 2476 | if (ldata->canon_head == ldata->read_tail) |
47afa7a5 | 2477 | return 0; |
ba2e68ac JS |
2478 | head = ldata->canon_head; |
2479 | tail = ldata->read_tail; | |
bc5a5e3f | 2480 | nr = head - tail; |
47afa7a5 | 2481 | /* Skip EOF-chars.. */ |
3d63b7e4 | 2482 | while (MASK(head) != MASK(tail)) { |
819287f0 | 2483 | if (test_bit(MASK(tail), ldata->read_flags) && |
bc5a5e3f | 2484 | read_buf(ldata, tail) == __DISABLED_CHAR) |
47afa7a5 | 2485 | nr--; |
bc5a5e3f | 2486 | tail++; |
47afa7a5 AC |
2487 | } |
2488 | return nr; | |
2489 | } | |
2490 | ||
d78328bc JS |
2491 | static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd, |
2492 | unsigned long arg) | |
47afa7a5 | 2493 | { |
ba2e68ac | 2494 | struct n_tty_data *ldata = tty->disc_data; |
1e619477 | 2495 | unsigned int num; |
47afa7a5 AC |
2496 | |
2497 | switch (cmd) { | |
2498 | case TIOCOUTQ: | |
2499 | return put_user(tty_chars_in_buffer(tty), (int __user *) arg); | |
2500 | case TIOCINQ: | |
6d76bd26 | 2501 | down_write(&tty->termios_rwsem); |
966031f3 | 2502 | if (L_ICANON(tty) && !L_EXTPROC(tty)) |
1e619477 | 2503 | num = inq_canon(ldata); |
6d76bd26 | 2504 | else |
1e619477 | 2505 | num = read_cnt(ldata); |
6d76bd26 | 2506 | up_write(&tty->termios_rwsem); |
1e619477 | 2507 | return put_user(num, (unsigned int __user *) arg); |
47afa7a5 | 2508 | default: |
7c783601 | 2509 | return n_tty_ioctl_helper(tty, cmd, arg); |
47afa7a5 AC |
2510 | } |
2511 | } | |
2512 | ||
27228732 | 2513 | static struct tty_ldisc_ops n_tty_ops = { |
5e30d3bf | 2514 | .owner = THIS_MODULE, |
fbadf70a | 2515 | .num = N_TTY, |
e10cc1df PF |
2516 | .name = "n_tty", |
2517 | .open = n_tty_open, | |
2518 | .close = n_tty_close, | |
2519 | .flush_buffer = n_tty_flush_buffer, | |
11a96d18 AC |
2520 | .read = n_tty_read, |
2521 | .write = n_tty_write, | |
e10cc1df PF |
2522 | .ioctl = n_tty_ioctl, |
2523 | .set_termios = n_tty_set_termios, | |
11a96d18 | 2524 | .poll = n_tty_poll, |
e10cc1df | 2525 | .receive_buf = n_tty_receive_buf, |
f6c8dbe6 | 2526 | .write_wakeup = n_tty_write_wakeup, |
24a89d1c | 2527 | .receive_buf2 = n_tty_receive_buf2, |
6bb6fa69 | 2528 | .lookahead_buf = n_tty_lookahead_flow_ctrl, |
1da177e4 | 2529 | }; |
572b9adb RG |
2530 | |
2531 | /** | |
2532 | * n_tty_inherit_ops - inherit N_TTY methods | |
2533 | * @ops: struct tty_ldisc_ops where to save N_TTY methods | |
2534 | * | |
27228732 | 2535 | * Enables a 'subclass' line discipline to 'inherit' N_TTY methods. |
572b9adb RG |
2536 | */ |
2537 | ||
2538 | void n_tty_inherit_ops(struct tty_ldisc_ops *ops) | |
2539 | { | |
27228732 | 2540 | *ops = n_tty_ops; |
572b9adb | 2541 | ops->owner = NULL; |
572b9adb RG |
2542 | } |
2543 | EXPORT_SYMBOL_GPL(n_tty_inherit_ops); | |
27228732 PH |
2544 | |
2545 | void __init n_tty_init(void) | |
2546 | { | |
fbadf70a | 2547 | tty_register_ldisc(&n_tty_ops); |
27228732 | 2548 | } |