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