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