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