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