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