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