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