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; | |
1447 | int i; | |
1448 | char buf[64]; | |
1da177e4 | 1449 | |
53c5ee2c | 1450 | if (ldata->real_raw) { |
ce74117a | 1451 | i = min(N_TTY_BUF_SIZE - read_cnt(ldata), |
bc5a5e3f | 1452 | N_TTY_BUF_SIZE - (ldata->read_head & (N_TTY_BUF_SIZE - 1))); |
1da177e4 | 1453 | i = min(count, i); |
bc5a5e3f PH |
1454 | memcpy(read_buf_addr(ldata, ldata->read_head), cp, i); |
1455 | ldata->read_head += i; | |
1da177e4 LT |
1456 | cp += i; |
1457 | count -= i; | |
1458 | ||
ce74117a | 1459 | i = min(N_TTY_BUF_SIZE - read_cnt(ldata), |
bc5a5e3f | 1460 | N_TTY_BUF_SIZE - (ldata->read_head & (N_TTY_BUF_SIZE - 1))); |
1da177e4 | 1461 | i = min(count, i); |
bc5a5e3f PH |
1462 | memcpy(read_buf_addr(ldata, ldata->read_head), cp, i); |
1463 | ldata->read_head += i; | |
1da177e4 | 1464 | } else { |
4edf1827 | 1465 | for (i = count, p = cp, f = fp; i; i--, p++) { |
1da177e4 LT |
1466 | if (f) |
1467 | flags = *f++; | |
1468 | switch (flags) { | |
1469 | case TTY_NORMAL: | |
1470 | n_tty_receive_char(tty, *p); | |
1471 | break; | |
1472 | case TTY_BREAK: | |
1473 | n_tty_receive_break(tty); | |
1474 | break; | |
1475 | case TTY_PARITY: | |
1476 | case TTY_FRAME: | |
1477 | n_tty_receive_parity_error(tty, *p); | |
1478 | break; | |
1479 | case TTY_OVERRUN: | |
1480 | n_tty_receive_overrun(tty); | |
1481 | break; | |
1482 | default: | |
4edf1827 | 1483 | printk(KERN_ERR "%s: unknown flag %d\n", |
1da177e4 LT |
1484 | tty_name(tty, buf), flags); |
1485 | break; | |
1486 | } | |
1487 | } | |
f34d7a5b AC |
1488 | if (tty->ops->flush_chars) |
1489 | tty->ops->flush_chars(tty); | |
1da177e4 LT |
1490 | } |
1491 | ||
ce74117a | 1492 | if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) || |
26df6d13 | 1493 | L_EXTPROC(tty)) { |
1da177e4 LT |
1494 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
1495 | if (waitqueue_active(&tty->read_wait)) | |
1496 | wake_up_interruptible(&tty->read_wait); | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * Check the remaining room for the input canonicalization | |
1501 | * mode. We don't want to throttle the driver if we're in | |
1502 | * canonical mode and don't have a newline yet! | |
1503 | */ | |
e91e52e4 | 1504 | while (1) { |
9356b535 | 1505 | int throttled; |
e91e52e4 | 1506 | tty_set_flow_change(tty, TTY_THROTTLE_SAFE); |
24a89d1c | 1507 | if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE) |
e91e52e4 | 1508 | break; |
9356b535 PH |
1509 | up_read(&tty->termios_rwsem); |
1510 | throttled = tty_throttle_safe(tty); | |
1511 | down_read(&tty->termios_rwsem); | |
1512 | if (!throttled) | |
e91e52e4 PH |
1513 | break; |
1514 | } | |
1515 | __tty_set_flow_change(tty, 0); | |
1da177e4 LT |
1516 | } |
1517 | ||
24a89d1c PH |
1518 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, |
1519 | char *fp, int count) | |
1520 | { | |
9356b535 | 1521 | down_read(&tty->termios_rwsem); |
24a89d1c | 1522 | __receive_buf(tty, cp, fp, count); |
9356b535 | 1523 | up_read(&tty->termios_rwsem); |
24a89d1c PH |
1524 | } |
1525 | ||
1526 | static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp, | |
1527 | char *fp, int count) | |
1528 | { | |
1529 | struct n_tty_data *ldata = tty->disc_data; | |
1530 | int room; | |
1531 | ||
9356b535 PH |
1532 | down_read(&tty->termios_rwsem); |
1533 | ||
24a89d1c PH |
1534 | tty->receive_room = room = receive_room(tty); |
1535 | if (!room) | |
1536 | ldata->no_room = 1; | |
1537 | count = min(count, room); | |
1538 | if (count) | |
1539 | __receive_buf(tty, cp, fp, count); | |
1540 | ||
9356b535 PH |
1541 | up_read(&tty->termios_rwsem); |
1542 | ||
24a89d1c PH |
1543 | return count; |
1544 | } | |
1545 | ||
1da177e4 LT |
1546 | int is_ignored(int sig) |
1547 | { | |
1548 | return (sigismember(¤t->blocked, sig) || | |
4edf1827 | 1549 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); |
1da177e4 LT |
1550 | } |
1551 | ||
1552 | /** | |
1553 | * n_tty_set_termios - termios data changed | |
1554 | * @tty: terminal | |
1555 | * @old: previous data | |
1556 | * | |
1557 | * Called by the tty layer when the user changes termios flags so | |
1558 | * that the line discipline can plan ahead. This function cannot sleep | |
4edf1827 | 1559 | * and is protected from re-entry by the tty layer. The user is |
1da177e4 LT |
1560 | * guaranteed that this function will not be re-entered or in progress |
1561 | * when the ldisc is closed. | |
17b82060 | 1562 | * |
6a1c0680 | 1563 | * Locking: Caller holds tty->termios_rwsem |
1da177e4 | 1564 | */ |
4edf1827 AC |
1565 | |
1566 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) | |
1da177e4 | 1567 | { |
53c5ee2c | 1568 | struct n_tty_data *ldata = tty->disc_data; |
47afa7a5 | 1569 | int canon_change = 1; |
47afa7a5 AC |
1570 | |
1571 | if (old) | |
adc8d746 | 1572 | canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON; |
47afa7a5 | 1573 | if (canon_change) { |
3fe780b3 | 1574 | bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); |
ba2e68ac | 1575 | ldata->canon_head = ldata->read_tail; |
53c5ee2c | 1576 | ldata->erasing = 0; |
47afa7a5 AC |
1577 | } |
1578 | ||
ce74117a | 1579 | if (canon_change && !L_ICANON(tty) && read_cnt(ldata)) |
47afa7a5 | 1580 | wake_up_interruptible(&tty->read_wait); |
4edf1827 | 1581 | |
53c5ee2c | 1582 | ldata->icanon = (L_ICANON(tty) != 0); |
582f5590 | 1583 | |
1da177e4 LT |
1584 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || |
1585 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | |
1586 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | |
1587 | I_PARMRK(tty)) { | |
3fe780b3 | 1588 | bitmap_zero(ldata->process_char_map, 256); |
1da177e4 LT |
1589 | |
1590 | if (I_IGNCR(tty) || I_ICRNL(tty)) | |
3fe780b3 | 1591 | set_bit('\r', ldata->process_char_map); |
1da177e4 | 1592 | if (I_INLCR(tty)) |
3fe780b3 | 1593 | set_bit('\n', ldata->process_char_map); |
1da177e4 LT |
1594 | |
1595 | if (L_ICANON(tty)) { | |
3fe780b3 JS |
1596 | set_bit(ERASE_CHAR(tty), ldata->process_char_map); |
1597 | set_bit(KILL_CHAR(tty), ldata->process_char_map); | |
1598 | set_bit(EOF_CHAR(tty), ldata->process_char_map); | |
1599 | set_bit('\n', ldata->process_char_map); | |
1600 | set_bit(EOL_CHAR(tty), ldata->process_char_map); | |
1da177e4 LT |
1601 | if (L_IEXTEN(tty)) { |
1602 | set_bit(WERASE_CHAR(tty), | |
3fe780b3 | 1603 | ldata->process_char_map); |
1da177e4 | 1604 | set_bit(LNEXT_CHAR(tty), |
3fe780b3 | 1605 | ldata->process_char_map); |
1da177e4 | 1606 | set_bit(EOL2_CHAR(tty), |
3fe780b3 | 1607 | ldata->process_char_map); |
1da177e4 LT |
1608 | if (L_ECHO(tty)) |
1609 | set_bit(REPRINT_CHAR(tty), | |
3fe780b3 | 1610 | ldata->process_char_map); |
1da177e4 LT |
1611 | } |
1612 | } | |
1613 | if (I_IXON(tty)) { | |
3fe780b3 JS |
1614 | set_bit(START_CHAR(tty), ldata->process_char_map); |
1615 | set_bit(STOP_CHAR(tty), ldata->process_char_map); | |
1da177e4 LT |
1616 | } |
1617 | if (L_ISIG(tty)) { | |
3fe780b3 JS |
1618 | set_bit(INTR_CHAR(tty), ldata->process_char_map); |
1619 | set_bit(QUIT_CHAR(tty), ldata->process_char_map); | |
1620 | set_bit(SUSP_CHAR(tty), ldata->process_char_map); | |
1da177e4 | 1621 | } |
3fe780b3 | 1622 | clear_bit(__DISABLED_CHAR, ldata->process_char_map); |
53c5ee2c JS |
1623 | ldata->raw = 0; |
1624 | ldata->real_raw = 0; | |
1da177e4 | 1625 | } else { |
53c5ee2c | 1626 | ldata->raw = 1; |
1da177e4 LT |
1627 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && |
1628 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | |
1629 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | |
53c5ee2c | 1630 | ldata->real_raw = 1; |
1da177e4 | 1631 | else |
53c5ee2c | 1632 | ldata->real_raw = 0; |
1da177e4 | 1633 | } |
55db4c64 | 1634 | n_tty_set_room(tty); |
dab73b4e WY |
1635 | /* |
1636 | * Fix tty hang when I_IXON(tty) is cleared, but the tty | |
1637 | * been stopped by STOP_CHAR(tty) before it. | |
1638 | */ | |
1639 | if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) { | |
1640 | start_tty(tty); | |
1641 | } | |
1642 | ||
f34d7a5b AC |
1643 | /* The termios change make the tty ready for I/O */ |
1644 | wake_up_interruptible(&tty->write_wait); | |
1645 | wake_up_interruptible(&tty->read_wait); | |
1da177e4 LT |
1646 | } |
1647 | ||
1648 | /** | |
1649 | * n_tty_close - close the ldisc for this tty | |
1650 | * @tty: device | |
1651 | * | |
4edf1827 AC |
1652 | * Called from the terminal layer when this line discipline is |
1653 | * being shut down, either because of a close or becsuse of a | |
1da177e4 LT |
1654 | * discipline change. The function will not be called while other |
1655 | * ldisc methods are in progress. | |
1656 | */ | |
4edf1827 | 1657 | |
1da177e4 LT |
1658 | static void n_tty_close(struct tty_struct *tty) |
1659 | { | |
70ece7a7 JS |
1660 | struct n_tty_data *ldata = tty->disc_data; |
1661 | ||
79901317 PH |
1662 | if (tty->link) |
1663 | n_tty_packet_mode_flush(tty); | |
1664 | ||
ba2e68ac JS |
1665 | kfree(ldata->read_buf); |
1666 | kfree(ldata->echo_buf); | |
70ece7a7 | 1667 | kfree(ldata); |
70ece7a7 | 1668 | tty->disc_data = NULL; |
1da177e4 LT |
1669 | } |
1670 | ||
1671 | /** | |
1672 | * n_tty_open - open an ldisc | |
1673 | * @tty: terminal to open | |
1674 | * | |
4edf1827 | 1675 | * Called when this line discipline is being attached to the |
1da177e4 LT |
1676 | * terminal device. Can sleep. Called serialized so that no |
1677 | * other events will occur in parallel. No further open will occur | |
1678 | * until a close. | |
1679 | */ | |
1680 | ||
1681 | static int n_tty_open(struct tty_struct *tty) | |
1682 | { | |
70ece7a7 JS |
1683 | struct n_tty_data *ldata; |
1684 | ||
1685 | ldata = kzalloc(sizeof(*ldata), GFP_KERNEL); | |
1686 | if (!ldata) | |
1687 | goto err; | |
1688 | ||
53c5ee2c | 1689 | ldata->overrun_time = jiffies; |
bddc7152 JS |
1690 | mutex_init(&ldata->atomic_read_lock); |
1691 | mutex_init(&ldata->output_lock); | |
1692 | mutex_init(&ldata->echo_lock); | |
53c5ee2c | 1693 | |
a88a69c9 | 1694 | /* These are ugly. Currently a malloc failure here can panic */ |
ba2e68ac JS |
1695 | ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
1696 | ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL); | |
1697 | if (!ldata->read_buf || !ldata->echo_buf) | |
b91939f5 | 1698 | goto err_free_bufs; |
0b4068a1 | 1699 | |
70ece7a7 | 1700 | tty->disc_data = ldata; |
b66f4fa5 | 1701 | reset_buffer_flags(tty->disc_data); |
53c5ee2c | 1702 | ldata->column = 0; |
f6c8dbe6 | 1703 | ldata->minimum_to_wake = 1; |
1da177e4 | 1704 | tty->closing = 0; |
b66f4fa5 PH |
1705 | /* indicate buffer work may resume */ |
1706 | clear_bit(TTY_LDISC_HALTED, &tty->flags); | |
1707 | n_tty_set_termios(tty, NULL); | |
1708 | tty_unthrottle(tty); | |
70ece7a7 | 1709 | |
1da177e4 | 1710 | return 0; |
b91939f5 | 1711 | err_free_bufs: |
ba2e68ac JS |
1712 | kfree(ldata->read_buf); |
1713 | kfree(ldata->echo_buf); | |
70ece7a7 JS |
1714 | kfree(ldata); |
1715 | err: | |
b91939f5 | 1716 | return -ENOMEM; |
1da177e4 LT |
1717 | } |
1718 | ||
1719 | static inline int input_available_p(struct tty_struct *tty, int amt) | |
1720 | { | |
53c5ee2c JS |
1721 | struct n_tty_data *ldata = tty->disc_data; |
1722 | ||
e043e42b | 1723 | tty_flush_to_ldisc(tty); |
53c5ee2c | 1724 | if (ldata->icanon && !L_EXTPROC(tty)) { |
a73d3d69 | 1725 | if (ldata->canon_head != ldata->read_tail) |
1da177e4 | 1726 | return 1; |
ce74117a | 1727 | } else if (read_cnt(ldata) >= (amt ? amt : 1)) |
1da177e4 LT |
1728 | return 1; |
1729 | ||
1730 | return 0; | |
1731 | } | |
1732 | ||
1733 | /** | |
bbd20759 | 1734 | * copy_from_read_buf - copy read data directly |
1da177e4 LT |
1735 | * @tty: terminal device |
1736 | * @b: user data | |
1737 | * @nr: size of data | |
1738 | * | |
11a96d18 | 1739 | * Helper function to speed up n_tty_read. It is only called when |
1da177e4 LT |
1740 | * ICANON is off; it copies characters straight from the tty queue to |
1741 | * user space directly. It can be profitably called twice; once to | |
1742 | * drain the space from the tail pointer to the (physical) end of the | |
1743 | * buffer, and once to drain the space from the (physical) beginning of | |
1744 | * the buffer to head pointer. | |
1745 | * | |
bddc7152 | 1746 | * Called under the ldata->atomic_read_lock sem |
1da177e4 | 1747 | * |
6d76bd26 PH |
1748 | * n_tty_read()/consumer path: |
1749 | * caller holds non-exclusive termios_rwsem | |
1750 | * read_tail published | |
1da177e4 | 1751 | */ |
4edf1827 | 1752 | |
33f0f88f | 1753 | static int copy_from_read_buf(struct tty_struct *tty, |
1da177e4 LT |
1754 | unsigned char __user **b, |
1755 | size_t *nr) | |
1756 | ||
1757 | { | |
53c5ee2c | 1758 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 LT |
1759 | int retval; |
1760 | size_t n; | |
3fa10cc8 | 1761 | bool is_eof; |
bc5a5e3f | 1762 | size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); |
1da177e4 LT |
1763 | |
1764 | retval = 0; | |
bc5a5e3f | 1765 | n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail); |
1da177e4 | 1766 | n = min(*nr, n); |
1da177e4 | 1767 | if (n) { |
bc5a5e3f | 1768 | retval = copy_to_user(*b, read_buf_addr(ldata, tail), n); |
1da177e4 | 1769 | n -= retval; |
bc5a5e3f PH |
1770 | is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty); |
1771 | tty_audit_add_data(tty, read_buf_addr(ldata, tail), n, | |
53c5ee2c | 1772 | ldata->icanon); |
bc5a5e3f | 1773 | ldata->read_tail += n; |
26df6d13 | 1774 | /* Turn single EOF into zero-length read */ |
ce74117a | 1775 | if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata)) |
3fa10cc8 | 1776 | n = 0; |
1da177e4 LT |
1777 | *b += n; |
1778 | *nr -= n; | |
1779 | } | |
1780 | return retval; | |
1781 | } | |
1782 | ||
88bb0de3 | 1783 | /** |
32f13521 | 1784 | * canon_copy_from_read_buf - copy read data in canonical mode |
88bb0de3 PH |
1785 | * @tty: terminal device |
1786 | * @b: user data | |
1787 | * @nr: size of data | |
1788 | * | |
1789 | * Helper function for n_tty_read. It is only called when ICANON is on; | |
32f13521 PH |
1790 | * it copies one line of input up to and including the line-delimiting |
1791 | * character into the user-space buffer. | |
88bb0de3 PH |
1792 | * |
1793 | * Called under the atomic_read_lock mutex | |
6d76bd26 PH |
1794 | * |
1795 | * n_tty_read()/consumer path: | |
1796 | * caller holds non-exclusive termios_rwsem | |
1797 | * read_tail published | |
88bb0de3 PH |
1798 | */ |
1799 | ||
32f13521 PH |
1800 | static int canon_copy_from_read_buf(struct tty_struct *tty, |
1801 | unsigned char __user **b, | |
1802 | size_t *nr) | |
88bb0de3 PH |
1803 | { |
1804 | struct n_tty_data *ldata = tty->disc_data; | |
32f13521 | 1805 | size_t n, size, more, c; |
bc5a5e3f PH |
1806 | size_t eol; |
1807 | size_t tail; | |
1808 | int ret, found = 0; | |
88bb0de3 PH |
1809 | |
1810 | /* N.B. avoid overrun if nr == 0 */ | |
ce74117a | 1811 | n = min(*nr, read_cnt(ldata)); |
6d76bd26 | 1812 | if (!n) |
32f13521 | 1813 | return 0; |
88bb0de3 | 1814 | |
bc5a5e3f | 1815 | tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); |
32f13521 PH |
1816 | size = min_t(size_t, tail + n, N_TTY_BUF_SIZE); |
1817 | ||
bc5a5e3f | 1818 | n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n", |
32f13521 PH |
1819 | __func__, *nr, tail, n, size); |
1820 | ||
1821 | eol = find_next_bit(ldata->read_flags, size, tail); | |
1822 | more = n - (size - tail); | |
1823 | if (eol == N_TTY_BUF_SIZE && more) { | |
1824 | /* scan wrapped without finding set bit */ | |
1825 | eol = find_next_bit(ldata->read_flags, more, 0); | |
1826 | if (eol != more) | |
1827 | found = 1; | |
1828 | } else if (eol != size) | |
1829 | found = 1; | |
1830 | ||
1831 | size = N_TTY_BUF_SIZE - tail; | |
1832 | n = (found + eol + size) & (N_TTY_BUF_SIZE - 1); | |
1833 | c = n; | |
1834 | ||
bc5a5e3f | 1835 | if (found && read_buf(ldata, eol) == __DISABLED_CHAR) |
32f13521 PH |
1836 | n--; |
1837 | ||
bc5a5e3f | 1838 | n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n", |
32f13521 PH |
1839 | __func__, eol, found, n, c, size, more); |
1840 | ||
32f13521 | 1841 | if (n > size) { |
bc5a5e3f | 1842 | ret = copy_to_user(*b, read_buf_addr(ldata, tail), size); |
32f13521 PH |
1843 | if (ret) |
1844 | return -EFAULT; | |
1845 | ret = copy_to_user(*b + size, ldata->read_buf, n - size); | |
1846 | } else | |
bc5a5e3f | 1847 | ret = copy_to_user(*b, read_buf_addr(ldata, tail), n); |
32f13521 PH |
1848 | |
1849 | if (ret) | |
1850 | return -EFAULT; | |
1851 | *b += n; | |
1852 | *nr -= n; | |
1853 | ||
a73d3d69 | 1854 | if (found) |
6d76bd26 PH |
1855 | clear_bit(eol, ldata->read_flags); |
1856 | smp_mb__after_clear_bit(); | |
1857 | ldata->read_tail += c; | |
88bb0de3 | 1858 | |
32f13521 PH |
1859 | if (found) |
1860 | tty_audit_push(tty); | |
88bb0de3 PH |
1861 | return 0; |
1862 | } | |
1863 | ||
cc4191dc | 1864 | extern ssize_t redirected_tty_write(struct file *, const char __user *, |
4edf1827 | 1865 | size_t, loff_t *); |
1da177e4 LT |
1866 | |
1867 | /** | |
1868 | * job_control - check job control | |
1869 | * @tty: tty | |
1870 | * @file: file handle | |
1871 | * | |
1872 | * Perform job control management checks on this file/tty descriptor | |
4edf1827 | 1873 | * and if appropriate send any needed signals and return a negative |
1da177e4 | 1874 | * error code if action should be taken. |
04f378b1 | 1875 | * |
01a5e440 PH |
1876 | * Locking: redirected write test is safe |
1877 | * current->signal->tty check is safe | |
1878 | * ctrl_lock to safely reference tty->pgrp | |
1da177e4 | 1879 | */ |
4edf1827 | 1880 | |
1da177e4 LT |
1881 | static int job_control(struct tty_struct *tty, struct file *file) |
1882 | { | |
1883 | /* Job control check -- must be done at start and after | |
1884 | every sleep (POSIX.1 7.1.1.4). */ | |
1885 | /* NOTE: not yet done after every sleep pending a thorough | |
1886 | check of the logic of this change. -- jlc */ | |
1887 | /* don't stop on /dev/console */ | |
01a5e440 PH |
1888 | if (file->f_op->write == redirected_tty_write || |
1889 | current->signal->tty != tty) | |
1890 | return 0; | |
1891 | ||
1892 | spin_lock_irq(&tty->ctrl_lock); | |
1893 | if (!tty->pgrp) | |
1894 | printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); | |
1895 | else if (task_pgrp(current) != tty->pgrp) { | |
1896 | spin_unlock_irq(&tty->ctrl_lock); | |
1897 | if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned()) | |
1898 | return -EIO; | |
1899 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); | |
1900 | set_thread_flag(TIF_SIGPENDING); | |
1901 | return -ERESTARTSYS; | |
1da177e4 | 1902 | } |
01a5e440 | 1903 | spin_unlock_irq(&tty->ctrl_lock); |
1da177e4 LT |
1904 | return 0; |
1905 | } | |
4edf1827 | 1906 | |
1da177e4 LT |
1907 | |
1908 | /** | |
11a96d18 | 1909 | * n_tty_read - read function for tty |
1da177e4 LT |
1910 | * @tty: tty device |
1911 | * @file: file object | |
1912 | * @buf: userspace buffer pointer | |
1913 | * @nr: size of I/O | |
1914 | * | |
1915 | * Perform reads for the line discipline. We are guaranteed that the | |
1916 | * line discipline will not be closed under us but we may get multiple | |
1917 | * parallel readers and must handle this ourselves. We may also get | |
1918 | * a hangup. Always called in user context, may sleep. | |
1919 | * | |
1920 | * This code must be sure never to sleep through a hangup. | |
6d76bd26 PH |
1921 | * |
1922 | * n_tty_read()/consumer path: | |
1923 | * claims non-exclusive termios_rwsem | |
1924 | * publishes read_tail | |
1da177e4 | 1925 | */ |
4edf1827 | 1926 | |
11a96d18 | 1927 | static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, |
1da177e4 LT |
1928 | unsigned char __user *buf, size_t nr) |
1929 | { | |
53c5ee2c | 1930 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 LT |
1931 | unsigned char __user *b = buf; |
1932 | DECLARE_WAITQUEUE(wait, current); | |
1933 | int c; | |
1934 | int minimum, time; | |
1935 | ssize_t retval = 0; | |
1936 | ssize_t size; | |
1937 | long timeout; | |
1938 | unsigned long flags; | |
04f378b1 | 1939 | int packet; |
1da177e4 LT |
1940 | |
1941 | do_it_again: | |
1da177e4 | 1942 | c = job_control(tty, file); |
4edf1827 | 1943 | if (c < 0) |
1da177e4 | 1944 | return c; |
4edf1827 | 1945 | |
9356b535 PH |
1946 | down_read(&tty->termios_rwsem); |
1947 | ||
1da177e4 LT |
1948 | minimum = time = 0; |
1949 | timeout = MAX_SCHEDULE_TIMEOUT; | |
53c5ee2c | 1950 | if (!ldata->icanon) { |
1da177e4 LT |
1951 | minimum = MIN_CHAR(tty); |
1952 | if (minimum) { | |
a6e54319 | 1953 | time = (HZ / 10) * TIME_CHAR(tty); |
1da177e4 | 1954 | if (time) |
f6c8dbe6 | 1955 | ldata->minimum_to_wake = 1; |
1da177e4 | 1956 | else if (!waitqueue_active(&tty->read_wait) || |
f6c8dbe6 PH |
1957 | (ldata->minimum_to_wake > minimum)) |
1958 | ldata->minimum_to_wake = minimum; | |
1da177e4 | 1959 | } else { |
a6e54319 | 1960 | timeout = (HZ / 10) * TIME_CHAR(tty); |
f6c8dbe6 | 1961 | ldata->minimum_to_wake = minimum = 1; |
1da177e4 LT |
1962 | } |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Internal serialization of reads. | |
1967 | */ | |
1968 | if (file->f_flags & O_NONBLOCK) { | |
9356b535 PH |
1969 | if (!mutex_trylock(&ldata->atomic_read_lock)) { |
1970 | up_read(&tty->termios_rwsem); | |
1da177e4 | 1971 | return -EAGAIN; |
9356b535 | 1972 | } |
4edf1827 | 1973 | } else { |
9356b535 PH |
1974 | if (mutex_lock_interruptible(&ldata->atomic_read_lock)) { |
1975 | up_read(&tty->termios_rwsem); | |
1da177e4 | 1976 | return -ERESTARTSYS; |
9356b535 | 1977 | } |
1da177e4 | 1978 | } |
04f378b1 | 1979 | packet = tty->packet; |
1da177e4 LT |
1980 | |
1981 | add_wait_queue(&tty->read_wait, &wait); | |
1da177e4 LT |
1982 | while (nr) { |
1983 | /* First test for status change. */ | |
04f378b1 | 1984 | if (packet && tty->link->ctrl_status) { |
1da177e4 LT |
1985 | unsigned char cs; |
1986 | if (b != buf) | |
1987 | break; | |
04f378b1 | 1988 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); |
1da177e4 LT |
1989 | cs = tty->link->ctrl_status; |
1990 | tty->link->ctrl_status = 0; | |
04f378b1 | 1991 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); |
522ed776 | 1992 | if (tty_put_user(tty, cs, b++)) { |
1da177e4 LT |
1993 | retval = -EFAULT; |
1994 | b--; | |
1995 | break; | |
1996 | } | |
1997 | nr--; | |
1998 | break; | |
1999 | } | |
2000 | /* This statement must be first before checking for input | |
2001 | so that any interrupt will set the state back to | |
2002 | TASK_RUNNING. */ | |
2003 | set_current_state(TASK_INTERRUPTIBLE); | |
4edf1827 | 2004 | |
f6c8dbe6 | 2005 | if (((minimum - (b - buf)) < ldata->minimum_to_wake) && |
1da177e4 | 2006 | ((minimum - (b - buf)) >= 1)) |
f6c8dbe6 | 2007 | ldata->minimum_to_wake = (minimum - (b - buf)); |
4edf1827 | 2008 | |
1da177e4 LT |
2009 | if (!input_available_p(tty, 0)) { |
2010 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | |
2011 | retval = -EIO; | |
2012 | break; | |
2013 | } | |
2014 | if (tty_hung_up_p(file)) | |
2015 | break; | |
2016 | if (!timeout) | |
2017 | break; | |
2018 | if (file->f_flags & O_NONBLOCK) { | |
2019 | retval = -EAGAIN; | |
2020 | break; | |
2021 | } | |
2022 | if (signal_pending(current)) { | |
2023 | retval = -ERESTARTSYS; | |
2024 | break; | |
2025 | } | |
55db4c64 | 2026 | n_tty_set_room(tty); |
9356b535 PH |
2027 | up_read(&tty->termios_rwsem); |
2028 | ||
1da177e4 | 2029 | timeout = schedule_timeout(timeout); |
9356b535 PH |
2030 | |
2031 | down_read(&tty->termios_rwsem); | |
1da177e4 LT |
2032 | continue; |
2033 | } | |
2034 | __set_current_state(TASK_RUNNING); | |
2035 | ||
2036 | /* Deal with packet mode. */ | |
04f378b1 | 2037 | if (packet && b == buf) { |
522ed776 | 2038 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { |
1da177e4 LT |
2039 | retval = -EFAULT; |
2040 | b--; | |
2041 | break; | |
2042 | } | |
2043 | nr--; | |
2044 | } | |
2045 | ||
53c5ee2c | 2046 | if (ldata->icanon && !L_EXTPROC(tty)) { |
32f13521 | 2047 | retval = canon_copy_from_read_buf(tty, &b, &nr); |
1da177e4 LT |
2048 | if (retval) |
2049 | break; | |
2050 | } else { | |
2051 | int uncopied; | |
04f378b1 AC |
2052 | /* The copy function takes the read lock and handles |
2053 | locking internally for this case */ | |
1da177e4 LT |
2054 | uncopied = copy_from_read_buf(tty, &b, &nr); |
2055 | uncopied += copy_from_read_buf(tty, &b, &nr); | |
2056 | if (uncopied) { | |
2057 | retval = -EFAULT; | |
2058 | break; | |
2059 | } | |
2060 | } | |
2061 | ||
2062 | /* If there is enough space in the read buffer now, let the | |
a19d0c6a | 2063 | * low-level driver know. We use chars_in_buffer() to |
1da177e4 LT |
2064 | * check the buffer, as it now knows about canonical mode. |
2065 | * Otherwise, if the driver is throttled and the line is | |
2066 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | |
2067 | * we won't get any more characters. | |
2068 | */ | |
e91e52e4 | 2069 | while (1) { |
9356b535 | 2070 | int unthrottled; |
e91e52e4 | 2071 | tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); |
a19d0c6a | 2072 | if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) |
e91e52e4 PH |
2073 | break; |
2074 | if (!tty->count) | |
2075 | break; | |
55db4c64 | 2076 | n_tty_set_room(tty); |
9356b535 PH |
2077 | up_read(&tty->termios_rwsem); |
2078 | unthrottled = tty_unthrottle_safe(tty); | |
2079 | down_read(&tty->termios_rwsem); | |
2080 | if (!unthrottled) | |
e91e52e4 | 2081 | break; |
55db4c64 | 2082 | } |
e91e52e4 | 2083 | __tty_set_flow_change(tty, 0); |
1da177e4 LT |
2084 | |
2085 | if (b - buf >= minimum) | |
2086 | break; | |
2087 | if (time) | |
2088 | timeout = time; | |
2089 | } | |
bddc7152 | 2090 | mutex_unlock(&ldata->atomic_read_lock); |
1da177e4 LT |
2091 | remove_wait_queue(&tty->read_wait, &wait); |
2092 | ||
2093 | if (!waitqueue_active(&tty->read_wait)) | |
f6c8dbe6 | 2094 | ldata->minimum_to_wake = minimum; |
1da177e4 LT |
2095 | |
2096 | __set_current_state(TASK_RUNNING); | |
2097 | size = b - buf; | |
2098 | if (size) { | |
2099 | retval = size; | |
2100 | if (nr) | |
4edf1827 | 2101 | clear_bit(TTY_PUSH, &tty->flags); |
9356b535 PH |
2102 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) { |
2103 | up_read(&tty->termios_rwsem); | |
bbd20759 | 2104 | goto do_it_again; |
9356b535 | 2105 | } |
1da177e4 | 2106 | |
55db4c64 | 2107 | n_tty_set_room(tty); |
9356b535 | 2108 | up_read(&tty->termios_rwsem); |
1da177e4 LT |
2109 | return retval; |
2110 | } | |
2111 | ||
2112 | /** | |
11a96d18 | 2113 | * n_tty_write - write function for tty |
1da177e4 LT |
2114 | * @tty: tty device |
2115 | * @file: file object | |
2116 | * @buf: userspace buffer pointer | |
2117 | * @nr: size of I/O | |
2118 | * | |
a88a69c9 | 2119 | * Write function of the terminal device. This is serialized with |
1da177e4 | 2120 | * respect to other write callers but not to termios changes, reads |
a88a69c9 JP |
2121 | * and other such events. Since the receive code will echo characters, |
2122 | * thus calling driver write methods, the output_lock is used in | |
2123 | * the output processing functions called here as well as in the | |
2124 | * echo processing function to protect the column state and space | |
2125 | * left in the buffer. | |
1da177e4 LT |
2126 | * |
2127 | * This code must be sure never to sleep through a hangup. | |
a88a69c9 JP |
2128 | * |
2129 | * Locking: output_lock to protect column state and space left | |
2130 | * (note that the process_output*() functions take this | |
2131 | * lock themselves) | |
1da177e4 | 2132 | */ |
4edf1827 | 2133 | |
11a96d18 | 2134 | static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, |
a88a69c9 | 2135 | const unsigned char *buf, size_t nr) |
1da177e4 LT |
2136 | { |
2137 | const unsigned char *b = buf; | |
2138 | DECLARE_WAITQUEUE(wait, current); | |
2139 | int c; | |
2140 | ssize_t retval = 0; | |
2141 | ||
2142 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | |
2143 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { | |
2144 | retval = tty_check_change(tty); | |
2145 | if (retval) | |
2146 | return retval; | |
2147 | } | |
2148 | ||
9356b535 PH |
2149 | down_read(&tty->termios_rwsem); |
2150 | ||
a88a69c9 JP |
2151 | /* Write out any echoed characters that are still pending */ |
2152 | process_echoes(tty); | |
300a6204 | 2153 | |
1da177e4 LT |
2154 | add_wait_queue(&tty->write_wait, &wait); |
2155 | while (1) { | |
2156 | set_current_state(TASK_INTERRUPTIBLE); | |
2157 | if (signal_pending(current)) { | |
2158 | retval = -ERESTARTSYS; | |
2159 | break; | |
2160 | } | |
2161 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | |
2162 | retval = -EIO; | |
2163 | break; | |
2164 | } | |
582f5590 | 2165 | if (O_OPOST(tty)) { |
1da177e4 | 2166 | while (nr > 0) { |
a88a69c9 | 2167 | ssize_t num = process_output_block(tty, b, nr); |
1da177e4 LT |
2168 | if (num < 0) { |
2169 | if (num == -EAGAIN) | |
2170 | break; | |
2171 | retval = num; | |
2172 | goto break_out; | |
2173 | } | |
2174 | b += num; | |
2175 | nr -= num; | |
2176 | if (nr == 0) | |
2177 | break; | |
2178 | c = *b; | |
a88a69c9 | 2179 | if (process_output(c, tty) < 0) |
1da177e4 LT |
2180 | break; |
2181 | b++; nr--; | |
2182 | } | |
f34d7a5b AC |
2183 | if (tty->ops->flush_chars) |
2184 | tty->ops->flush_chars(tty); | |
1da177e4 | 2185 | } else { |
d6afe27b | 2186 | while (nr > 0) { |
f34d7a5b | 2187 | c = tty->ops->write(tty, b, nr); |
d6afe27b RZ |
2188 | if (c < 0) { |
2189 | retval = c; | |
2190 | goto break_out; | |
2191 | } | |
2192 | if (!c) | |
2193 | break; | |
2194 | b += c; | |
2195 | nr -= c; | |
1da177e4 | 2196 | } |
1da177e4 LT |
2197 | } |
2198 | if (!nr) | |
2199 | break; | |
2200 | if (file->f_flags & O_NONBLOCK) { | |
2201 | retval = -EAGAIN; | |
2202 | break; | |
2203 | } | |
9356b535 PH |
2204 | up_read(&tty->termios_rwsem); |
2205 | ||
1da177e4 | 2206 | schedule(); |
9356b535 PH |
2207 | |
2208 | down_read(&tty->termios_rwsem); | |
1da177e4 LT |
2209 | } |
2210 | break_out: | |
2211 | __set_current_state(TASK_RUNNING); | |
2212 | remove_wait_queue(&tty->write_wait, &wait); | |
ff8cb0fd TP |
2213 | if (b - buf != nr && tty->fasync) |
2214 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | |
9356b535 | 2215 | up_read(&tty->termios_rwsem); |
1da177e4 LT |
2216 | return (b - buf) ? b - buf : retval; |
2217 | } | |
2218 | ||
2219 | /** | |
11a96d18 | 2220 | * n_tty_poll - poll method for N_TTY |
1da177e4 LT |
2221 | * @tty: terminal device |
2222 | * @file: file accessing it | |
2223 | * @wait: poll table | |
2224 | * | |
2225 | * Called when the line discipline is asked to poll() for data or | |
2226 | * for special events. This code is not serialized with respect to | |
2227 | * other events save open/close. | |
2228 | * | |
2229 | * This code must be sure never to sleep through a hangup. | |
2230 | * Called without the kernel lock held - fine | |
1da177e4 | 2231 | */ |
4edf1827 | 2232 | |
11a96d18 | 2233 | static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, |
4edf1827 | 2234 | poll_table *wait) |
1da177e4 | 2235 | { |
f6c8dbe6 | 2236 | struct n_tty_data *ldata = tty->disc_data; |
1da177e4 LT |
2237 | unsigned int mask = 0; |
2238 | ||
2239 | poll_wait(file, &tty->read_wait, wait); | |
2240 | poll_wait(file, &tty->write_wait, wait); | |
2241 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) | |
2242 | mask |= POLLIN | POLLRDNORM; | |
2243 | if (tty->packet && tty->link->ctrl_status) | |
2244 | mask |= POLLPRI | POLLIN | POLLRDNORM; | |
2245 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
2246 | mask |= POLLHUP; | |
2247 | if (tty_hung_up_p(file)) | |
2248 | mask |= POLLHUP; | |
2249 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { | |
2250 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) | |
f6c8dbe6 | 2251 | ldata->minimum_to_wake = MIN_CHAR(tty); |
1da177e4 | 2252 | else |
f6c8dbe6 | 2253 | ldata->minimum_to_wake = 1; |
1da177e4 | 2254 | } |
f34d7a5b AC |
2255 | if (tty->ops->write && !tty_is_writelocked(tty) && |
2256 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && | |
2257 | tty_write_room(tty) > 0) | |
1da177e4 LT |
2258 | mask |= POLLOUT | POLLWRNORM; |
2259 | return mask; | |
2260 | } | |
2261 | ||
57c94121 | 2262 | static unsigned long inq_canon(struct n_tty_data *ldata) |
47afa7a5 | 2263 | { |
bc5a5e3f | 2264 | size_t nr, head, tail; |
47afa7a5 | 2265 | |
a73d3d69 | 2266 | if (ldata->canon_head == ldata->read_tail) |
47afa7a5 | 2267 | return 0; |
ba2e68ac JS |
2268 | head = ldata->canon_head; |
2269 | tail = ldata->read_tail; | |
bc5a5e3f | 2270 | nr = head - tail; |
47afa7a5 AC |
2271 | /* Skip EOF-chars.. */ |
2272 | while (head != tail) { | |
bc5a5e3f PH |
2273 | if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) && |
2274 | read_buf(ldata, tail) == __DISABLED_CHAR) | |
47afa7a5 | 2275 | nr--; |
bc5a5e3f | 2276 | tail++; |
47afa7a5 AC |
2277 | } |
2278 | return nr; | |
2279 | } | |
2280 | ||
2281 | static int n_tty_ioctl(struct tty_struct *tty, struct file *file, | |
2282 | unsigned int cmd, unsigned long arg) | |
2283 | { | |
ba2e68ac | 2284 | struct n_tty_data *ldata = tty->disc_data; |
47afa7a5 AC |
2285 | int retval; |
2286 | ||
2287 | switch (cmd) { | |
2288 | case TIOCOUTQ: | |
2289 | return put_user(tty_chars_in_buffer(tty), (int __user *) arg); | |
2290 | case TIOCINQ: | |
6d76bd26 | 2291 | down_write(&tty->termios_rwsem); |
47afa7a5 | 2292 | if (L_ICANON(tty)) |
57c94121 | 2293 | retval = inq_canon(ldata); |
6d76bd26 PH |
2294 | else |
2295 | retval = read_cnt(ldata); | |
2296 | up_write(&tty->termios_rwsem); | |
47afa7a5 AC |
2297 | return put_user(retval, (unsigned int __user *) arg); |
2298 | default: | |
2299 | return n_tty_ioctl_helper(tty, file, cmd, arg); | |
2300 | } | |
2301 | } | |
2302 | ||
f6c8dbe6 PH |
2303 | static void n_tty_fasync(struct tty_struct *tty, int on) |
2304 | { | |
2305 | struct n_tty_data *ldata = tty->disc_data; | |
2306 | ||
2307 | if (!waitqueue_active(&tty->read_wait)) { | |
2308 | if (on) | |
2309 | ldata->minimum_to_wake = 1; | |
2310 | else if (!tty->fasync) | |
2311 | ldata->minimum_to_wake = N_TTY_BUF_SIZE; | |
2312 | } | |
2313 | } | |
2314 | ||
a352def2 | 2315 | struct tty_ldisc_ops tty_ldisc_N_TTY = { |
e10cc1df PF |
2316 | .magic = TTY_LDISC_MAGIC, |
2317 | .name = "n_tty", | |
2318 | .open = n_tty_open, | |
2319 | .close = n_tty_close, | |
2320 | .flush_buffer = n_tty_flush_buffer, | |
2321 | .chars_in_buffer = n_tty_chars_in_buffer, | |
11a96d18 AC |
2322 | .read = n_tty_read, |
2323 | .write = n_tty_write, | |
e10cc1df PF |
2324 | .ioctl = n_tty_ioctl, |
2325 | .set_termios = n_tty_set_termios, | |
11a96d18 | 2326 | .poll = n_tty_poll, |
e10cc1df | 2327 | .receive_buf = n_tty_receive_buf, |
f6c8dbe6 PH |
2328 | .write_wakeup = n_tty_write_wakeup, |
2329 | .fasync = n_tty_fasync, | |
24a89d1c | 2330 | .receive_buf2 = n_tty_receive_buf2, |
1da177e4 | 2331 | }; |
572b9adb RG |
2332 | |
2333 | /** | |
2334 | * n_tty_inherit_ops - inherit N_TTY methods | |
2335 | * @ops: struct tty_ldisc_ops where to save N_TTY methods | |
2336 | * | |
593fb1ae | 2337 | * Enables a 'subclass' line discipline to 'inherit' N_TTY |
572b9adb RG |
2338 | * methods. |
2339 | */ | |
2340 | ||
2341 | void n_tty_inherit_ops(struct tty_ldisc_ops *ops) | |
2342 | { | |
2343 | *ops = tty_ldisc_N_TTY; | |
2344 | ops->owner = NULL; | |
2345 | ops->refcount = ops->flags = 0; | |
2346 | } | |
2347 | EXPORT_SYMBOL_GPL(n_tty_inherit_ops); |