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