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