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