blob: 3d1594e10d0062e82765e6eb84b36bb7162957c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Alan Cox4edf1822008-02-08 04:18:44 -080011 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 *
Alan Cox4edf1822008-02-08 04:18:44 -080023 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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>.
Alan Cox11a96d12008-10-13 10:46:24 +010029 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 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>
Miloslav Trmac522ed772007-07-15 23:40:56 -070048#include <linux/audit.h>
49#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000050#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080051#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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 */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010063#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Joe Petersona88a69c2009-01-02 13:40:53 +000065/*
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
Jiri Slaby70ece7a2012-10-18 22:26:38 +020076struct n_tty_data {
77 char dummy;
78};
79
Miloslav Trmac522ed772007-07-15 23:40:56 -070080static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
81 unsigned char __user *ptr)
82{
Jiri Slaby6c633f22012-10-18 22:26:37 +020083 tty_audit_add_data(tty, &x, 1, tty->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -070084 return put_user(x, ptr);
85}
86
Linus Torvalds55db4c62011-06-04 06:33:24 +090087/**
88 * n_tty_set__room - receive space
89 * @tty: terminal
90 *
91 * Called by the driver to find out how much data it is
92 * permitted to feed to the line discipline without any being lost
93 * and thus to manage flow control. Not serialized. Answers for the
94 * "instant".
95 */
96
97static void n_tty_set_room(struct tty_struct *tty)
98{
Jaeden Amero090abf72012-07-27 08:43:11 -050099 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900100 int old_left;
101
Jaeden Amero090abf72012-07-27 08:43:11 -0500102 /* tty->read_cnt is not read locked ? */
103 if (I_PARMRK(tty)) {
104 /* Multiply read_cnt by 3, since each byte might take up to
105 * three times as many spaces when PARMRK is set (depending on
106 * its flags, e.g. parity error). */
107 left = N_TTY_BUF_SIZE - tty->read_cnt * 3 - 1;
108 } else
109 left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
110
Linus Torvalds55db4c62011-06-04 06:33:24 +0900111 /*
112 * If we are doing input canonicalization, and there are no
113 * pending newlines, let characters through without limit, so
114 * that erase characters will be handled. Other excess
115 * characters will be beeped.
116 */
117 if (left <= 0)
118 left = tty->icanon && !tty->canon_data;
119 old_left = tty->receive_room;
120 tty->receive_room = left;
121
122 /* Did this open up the receive buffer? We may need to flip */
123 if (left && !old_left)
124 schedule_work(&tty->buf.work);
125}
126
Alan Cox33f0f882006-01-09 20:54:13 -0800127static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 if (tty->read_cnt < N_TTY_BUF_SIZE) {
130 tty->read_buf[tty->read_head] = c;
131 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
132 tty->read_cnt++;
133 }
134}
135
Alan Cox17b82062008-10-13 10:45:06 +0100136/**
137 * put_tty_queue - add character to tty
138 * @c: character
139 * @tty: tty device
140 *
141 * Add a character to the tty read_buf queue. This is done under the
142 * read_lock to serialize character addition and also to protect us
143 * against parallel reads or flushes
144 */
145
Alan Cox33f0f882006-01-09 20:54:13 -0800146static void put_tty_queue(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 unsigned long flags;
149 /*
150 * The problem of stomping on the buffers ends here.
151 * Why didn't anyone see this one coming? --AJK
152 */
153 spin_lock_irqsave(&tty->read_lock, flags);
154 put_tty_queue_nolock(c, tty);
155 spin_unlock_irqrestore(&tty->read_lock, flags);
156}
157
158/**
159 * check_unthrottle - allow new receive data
160 * @tty; tty device
161 *
Alan Cox17b82062008-10-13 10:45:06 +0100162 * Check whether to call the driver unthrottle functions
163 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800164 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * this is not guaranteed.
166 */
Alan Cox4edf1822008-02-08 04:18:44 -0800167static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Alan Cox39c2e602008-04-30 00:54:18 -0700169 if (tty->count)
170 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
174 * reset_buffer_flags - reset buffer state
175 * @tty: terminal to reset
176 *
Alan Cox4edf1822008-02-08 04:18:44 -0800177 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * and make sure the driver is unthrottled. Called
179 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100180 *
181 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void reset_buffer_flags(struct tty_struct *tty)
185{
186 unsigned long flags;
187
188 spin_lock_irqsave(&tty->read_lock, flags);
189 tty->read_head = tty->read_tail = tty->read_cnt = 0;
190 spin_unlock_irqrestore(&tty->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000191
192 mutex_lock(&tty->echo_lock);
193 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
194 mutex_unlock(&tty->echo_lock);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 tty->canon_head = tty->canon_data = tty->erasing = 0;
197 memset(&tty->read_flags, 0, sizeof tty->read_flags);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900198 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/**
202 * n_tty_flush_buffer - clean input queue
203 * @tty: terminal device
204 *
205 * Flush the input buffer. Called when the line discipline is
206 * being closed, when the tty layer wants the buffer flushed (eg
207 * at hangup) or when the N_TTY line discipline internally has to
208 * clean the pending queue (for example some signals).
209 *
Alan Cox17b82062008-10-13 10:45:06 +0100210 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Alan Cox4edf1822008-02-08 04:18:44 -0800212
213static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Alan Cox04f378b2008-04-30 00:53:29 -0700215 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* clear everything and unthrottle the driver */
217 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!tty->link)
220 return;
221
Alan Cox04f378b2008-04-30 00:53:29 -0700222 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (tty->link->packet) {
224 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
225 wake_up_interruptible(&tty->link->read_wait);
226 }
Alan Cox04f378b2008-04-30 00:53:29 -0700227 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/**
231 * n_tty_chars_in_buffer - report available bytes
232 * @tty: tty device
233 *
234 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800235 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100236 *
237 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Alan Cox4edf1822008-02-08 04:18:44 -0800239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
241{
242 unsigned long flags;
243 ssize_t n = 0;
244
245 spin_lock_irqsave(&tty->read_lock, flags);
246 if (!tty->icanon) {
247 n = tty->read_cnt;
248 } else if (tty->canon_data) {
249 n = (tty->canon_head > tty->read_tail) ?
250 tty->canon_head - tty->read_tail :
251 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
252 }
253 spin_unlock_irqrestore(&tty->read_lock, flags);
254 return n;
255}
256
257/**
258 * is_utf8_continuation - utf8 multibyte check
259 * @c: byte to check
260 *
261 * Returns true if the utf8 character 'c' is a multibyte continuation
262 * character. We use this to correctly compute the on screen size
263 * of the character when printing
264 */
Alan Cox4edf1822008-02-08 04:18:44 -0800265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static inline int is_utf8_continuation(unsigned char c)
267{
268 return (c & 0xc0) == 0x80;
269}
270
271/**
272 * is_continuation - multibyte check
273 * @c: byte to check
274 *
275 * Returns true if the utf8 character 'c' is a multibyte continuation
276 * character and the terminal is in unicode mode.
277 */
Alan Cox4edf1822008-02-08 04:18:44 -0800278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static inline int is_continuation(unsigned char c, struct tty_struct *tty)
280{
281 return I_IUTF8(tty) && is_utf8_continuation(c);
282}
283
284/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000285 * do_output_char - output one character
286 * @c: character (or partial unicode symbol)
287 * @tty: terminal device
288 * @space: space available in tty driver write buffer
289 *
290 * This is a helper function that handles one output character
291 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600292 * doing OPOST processing and putting the results in the
293 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000294 *
295 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
296 * and NLDLY. They simply aren't relevant in the world today.
297 * If you ever need them, add them here.
298 *
299 * Returns the number of bytes of buffer space used or -1 if
300 * no space left.
301 *
302 * Locking: should be called under the output_lock to protect
303 * the column state and space left in the buffer
304 */
305
306static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
307{
308 int spaces;
309
310 if (!space)
311 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000312
Joe Petersona88a69c2009-01-02 13:40:53 +0000313 switch (c) {
314 case '\n':
315 if (O_ONLRET(tty))
316 tty->column = 0;
317 if (O_ONLCR(tty)) {
318 if (space < 2)
319 return -1;
320 tty->canon_column = tty->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700321 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000322 return 2;
323 }
324 tty->canon_column = tty->column;
325 break;
326 case '\r':
327 if (O_ONOCR(tty) && tty->column == 0)
328 return 0;
329 if (O_OCRNL(tty)) {
330 c = '\n';
331 if (O_ONLRET(tty))
332 tty->canon_column = tty->column = 0;
333 break;
334 }
335 tty->canon_column = tty->column = 0;
336 break;
337 case '\t':
338 spaces = 8 - (tty->column & 7);
339 if (O_TABDLY(tty) == XTABS) {
340 if (space < spaces)
341 return -1;
342 tty->column += spaces;
343 tty->ops->write(tty, " ", spaces);
344 return spaces;
345 }
346 tty->column += spaces;
347 break;
348 case '\b':
349 if (tty->column > 0)
350 tty->column--;
351 break;
352 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000353 if (!iscntrl(c)) {
354 if (O_OLCUC(tty))
355 c = toupper(c);
356 if (!is_continuation(c, tty))
357 tty->column++;
358 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000359 break;
360 }
361
362 tty_put_char(tty, c);
363 return 1;
364}
365
366/**
367 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @c: character (or partial unicode symbol)
369 * @tty: terminal device
370 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600371 * Output one character with OPOST processing.
372 * Returns -1 when the output device is full and the character
373 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 * Locking: output_lock to protect column state and space left
376 * (also, this is called from n_tty_write under the
377 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Alan Cox4edf1822008-02-08 04:18:44 -0800379
Joe Petersona88a69c2009-01-02 13:40:53 +0000380static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Joe Petersona88a69c2009-01-02 13:40:53 +0000382 int space, retval;
383
384 mutex_lock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Alan Coxf34d7a52008-04-30 00:54:13 -0700386 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000387 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Joe Petersona88a69c2009-01-02 13:40:53 +0000389 mutex_unlock(&tty->output_lock);
390 if (retval < 0)
391 return -1;
392 else
393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000397 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600399 * @buf: character buffer
400 * @nr: number of bytes to output
401 *
402 * Output a block of characters with OPOST processing.
403 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 *
405 * This path is used to speed up block console writes, among other
406 * things when processing blocks of output data. It handles only
407 * the simple cases normally found and helps to generate blocks of
408 * symbols for the console driver and thus improve performance.
409 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000410 * Locking: output_lock to protect column state and space left
411 * (also, this is called from n_tty_write under the
412 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Alan Cox4edf1822008-02-08 04:18:44 -0800414
Joe Petersona88a69c2009-01-02 13:40:53 +0000415static ssize_t process_output_block(struct tty_struct *tty,
416 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100419 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 const unsigned char *cp;
421
Joe Petersona88a69c2009-01-02 13:40:53 +0000422 mutex_lock(&tty->output_lock);
423
Alan Coxf34d7a52008-04-30 00:54:13 -0700424 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000425 if (!space) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (nr > space)
430 nr = space;
431
432 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000433 unsigned char c = *cp;
434
435 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 case '\n':
437 if (O_ONLRET(tty))
438 tty->column = 0;
439 if (O_ONLCR(tty))
440 goto break_out;
441 tty->canon_column = tty->column;
442 break;
443 case '\r':
444 if (O_ONOCR(tty) && tty->column == 0)
445 goto break_out;
446 if (O_OCRNL(tty))
447 goto break_out;
448 tty->canon_column = tty->column = 0;
449 break;
450 case '\t':
451 goto break_out;
452 case '\b':
453 if (tty->column > 0)
454 tty->column--;
455 break;
456 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000457 if (!iscntrl(c)) {
458 if (O_OLCUC(tty))
459 goto break_out;
460 if (!is_continuation(c, tty))
461 tty->column++;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 }
465 }
466break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700467 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000468
469 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return i;
471}
472
Joe Petersona88a69c2009-01-02 13:40:53 +0000473/**
474 * process_echoes - write pending echo characters
475 * @tty: terminal device
476 *
477 * Write previously buffered echo (and other ldisc-generated)
478 * characters to the tty.
479 *
480 * Characters generated by the ldisc (including echoes) need to
481 * be buffered because the driver's write buffer can fill during
482 * heavy program output. Echoing straight to the driver will
483 * often fail under these conditions, causing lost characters and
484 * resulting mismatches of ldisc state information.
485 *
486 * Since the ldisc state must represent the characters actually sent
487 * to the driver at the time of the write, operations like certain
488 * changes in column state are also saved in the buffer and executed
489 * here.
490 *
491 * A circular fifo buffer is used so that the most recent characters
492 * are prioritized. Also, when control characters are echoed with a
493 * prefixed "^", the pair is treated atomically and thus not separated.
494 *
495 * Locking: output_lock to protect column state and space left,
496 * echo_lock to protect the echo buffer
497 */
498
499static void process_echoes(struct tty_struct *tty)
500{
501 int space, nr;
502 unsigned char c;
503 unsigned char *cp, *buf_end;
504
505 if (!tty->echo_cnt)
506 return;
507
508 mutex_lock(&tty->output_lock);
509 mutex_lock(&tty->echo_lock);
510
511 space = tty_write_room(tty);
512
513 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
514 cp = tty->echo_buf + tty->echo_pos;
515 nr = tty->echo_cnt;
516 while (nr > 0) {
517 c = *cp;
518 if (c == ECHO_OP_START) {
519 unsigned char op;
520 unsigned char *opp;
521 int no_space_left = 0;
522
523 /*
524 * If the buffer byte is the start of a multi-byte
525 * operation, get the next byte, which is either the
526 * op code or a control character value.
527 */
528 opp = cp + 1;
529 if (opp == buf_end)
530 opp -= N_TTY_BUF_SIZE;
531 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000532
Joe Petersona88a69c2009-01-02 13:40:53 +0000533 switch (op) {
534 unsigned int num_chars, num_bs;
535
536 case ECHO_OP_ERASE_TAB:
537 if (++opp == buf_end)
538 opp -= N_TTY_BUF_SIZE;
539 num_chars = *opp;
540
541 /*
542 * Determine how many columns to go back
543 * in order to erase the tab.
544 * This depends on the number of columns
545 * used by other characters within the tab
546 * area. If this (modulo 8) count is from
547 * the start of input rather than from a
548 * previous tab, we offset by canon column.
549 * Otherwise, tab spacing is normal.
550 */
551 if (!(num_chars & 0x80))
552 num_chars += tty->canon_column;
553 num_bs = 8 - (num_chars & 7);
554
555 if (num_bs > space) {
556 no_space_left = 1;
557 break;
558 }
559 space -= num_bs;
560 while (num_bs--) {
561 tty_put_char(tty, '\b');
562 if (tty->column > 0)
563 tty->column--;
564 }
565 cp += 3;
566 nr -= 3;
567 break;
568
569 case ECHO_OP_SET_CANON_COL:
570 tty->canon_column = tty->column;
571 cp += 2;
572 nr -= 2;
573 break;
574
575 case ECHO_OP_MOVE_BACK_COL:
576 if (tty->column > 0)
577 tty->column--;
578 cp += 2;
579 nr -= 2;
580 break;
581
582 case ECHO_OP_START:
583 /* This is an escaped echo op start code */
584 if (!space) {
585 no_space_left = 1;
586 break;
587 }
588 tty_put_char(tty, ECHO_OP_START);
589 tty->column++;
590 space--;
591 cp += 2;
592 nr -= 2;
593 break;
594
595 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000596 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600597 * If the op is not a special byte code,
598 * it is a ctrl char tagged to be echoed
599 * as "^X" (where X is the letter
600 * representing the control char).
601 * Note that we must ensure there is
602 * enough space for the whole ctrl pair.
603 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000604 */
Joe Peterson62b26352009-09-09 15:03:47 -0600605 if (space < 2) {
606 no_space_left = 1;
607 break;
608 }
609 tty_put_char(tty, '^');
610 tty_put_char(tty, op ^ 0100);
611 tty->column += 2;
612 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000613 cp += 2;
614 nr -= 2;
615 }
616
617 if (no_space_left)
618 break;
619 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600620 if (O_OPOST(tty) &&
621 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
622 int retval = do_output_char(c, tty, space);
623 if (retval < 0)
624 break;
625 space -= retval;
626 } else {
627 if (!space)
628 break;
629 tty_put_char(tty, c);
630 space -= 1;
631 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000632 cp += 1;
633 nr -= 1;
634 }
635
636 /* When end of circular buffer reached, wrap around */
637 if (cp >= buf_end)
638 cp -= N_TTY_BUF_SIZE;
639 }
640
641 if (nr == 0) {
642 tty->echo_pos = 0;
643 tty->echo_cnt = 0;
644 tty->echo_overrun = 0;
645 } else {
646 int num_processed = tty->echo_cnt - nr;
647 tty->echo_pos += num_processed;
648 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
649 tty->echo_cnt = nr;
650 if (num_processed > 0)
651 tty->echo_overrun = 0;
652 }
653
654 mutex_unlock(&tty->echo_lock);
655 mutex_unlock(&tty->output_lock);
656
657 if (tty->ops->flush_chars)
658 tty->ops->flush_chars(tty);
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000662 * add_echo_byte - add a byte to the echo buffer
663 * @c: unicode byte to echo
664 * @tty: terminal device
665 *
666 * Add a character or operation byte to the echo buffer.
667 *
668 * Should be called under the echo lock to protect the echo buffer.
669 */
670
671static void add_echo_byte(unsigned char c, struct tty_struct *tty)
672{
673 int new_byte_pos;
674
675 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
676 /* Circular buffer is already at capacity */
677 new_byte_pos = tty->echo_pos;
678
679 /*
680 * Since the buffer start position needs to be advanced,
681 * be sure to step by a whole operation byte group.
682 */
Alan Cox300a6202009-01-02 13:41:04 +0000683 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000684 if (tty->echo_buf[(tty->echo_pos + 1) &
685 (N_TTY_BUF_SIZE - 1)] ==
686 ECHO_OP_ERASE_TAB) {
687 tty->echo_pos += 3;
688 tty->echo_cnt -= 2;
689 } else {
690 tty->echo_pos += 2;
691 tty->echo_cnt -= 1;
692 }
693 } else {
694 tty->echo_pos++;
695 }
696 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
697
698 tty->echo_overrun = 1;
699 } else {
700 new_byte_pos = tty->echo_pos + tty->echo_cnt;
701 new_byte_pos &= N_TTY_BUF_SIZE - 1;
702 tty->echo_cnt++;
703 }
704
705 tty->echo_buf[new_byte_pos] = c;
706}
707
708/**
709 * echo_move_back_col - add operation to move back a column
710 * @tty: terminal device
711 *
712 * Add an operation to the echo buffer to move back one column.
713 *
714 * Locking: echo_lock to protect the echo buffer
715 */
716
717static void echo_move_back_col(struct tty_struct *tty)
718{
719 mutex_lock(&tty->echo_lock);
720
721 add_echo_byte(ECHO_OP_START, tty);
722 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
723
724 mutex_unlock(&tty->echo_lock);
725}
726
727/**
728 * echo_set_canon_col - add operation to set the canon column
729 * @tty: terminal device
730 *
731 * Add an operation to the echo buffer to set the canon column
732 * to the current column.
733 *
734 * Locking: echo_lock to protect the echo buffer
735 */
736
737static void echo_set_canon_col(struct tty_struct *tty)
738{
739 mutex_lock(&tty->echo_lock);
740
741 add_echo_byte(ECHO_OP_START, tty);
742 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
743
744 mutex_unlock(&tty->echo_lock);
745}
746
747/**
748 * echo_erase_tab - add operation to erase a tab
749 * @num_chars: number of character columns already used
750 * @after_tab: true if num_chars starts after a previous tab
751 * @tty: terminal device
752 *
753 * Add an operation to the echo buffer to erase a tab.
754 *
755 * Called by the eraser function, which knows how many character
756 * columns have been used since either a previous tab or the start
757 * of input. This information will be used later, along with
758 * canon column (if applicable), to go back the correct number
759 * of columns.
760 *
761 * Locking: echo_lock to protect the echo buffer
762 */
763
764static void echo_erase_tab(unsigned int num_chars, int after_tab,
765 struct tty_struct *tty)
766{
767 mutex_lock(&tty->echo_lock);
768
769 add_echo_byte(ECHO_OP_START, tty);
770 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
771
772 /* We only need to know this modulo 8 (tab spacing) */
773 num_chars &= 7;
774
775 /* Set the high bit as a flag if num_chars is after a previous tab */
776 if (after_tab)
777 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000778
Joe Petersona88a69c2009-01-02 13:40:53 +0000779 add_echo_byte(num_chars, tty);
780
781 mutex_unlock(&tty->echo_lock);
782}
783
784/**
785 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * @c: unicode byte to echo
787 * @tty: terminal device
788 *
Alan Cox4edf1822008-02-08 04:18:44 -0800789 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100791 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000792 * This variant does not treat control characters specially.
793 *
794 * Locking: echo_lock to protect the echo buffer
795 */
796
797static void echo_char_raw(unsigned char c, struct tty_struct *tty)
798{
799 mutex_lock(&tty->echo_lock);
800
801 if (c == ECHO_OP_START) {
802 add_echo_byte(ECHO_OP_START, tty);
803 add_echo_byte(ECHO_OP_START, tty);
804 } else {
805 add_echo_byte(c, tty);
806 }
807
808 mutex_unlock(&tty->echo_lock);
809}
810
811/**
812 * echo_char - echo a character
813 * @c: unicode byte to echo
814 * @tty: terminal device
815 *
816 * Echo user input back onto the screen. This must be called only when
817 * L_ECHO(tty) is true. Called from the driver receive_buf path.
818 *
Joe Peterson62b26352009-09-09 15:03:47 -0600819 * This variant tags control characters to be echoed as "^X"
820 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000821 *
822 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
824
825static void echo_char(unsigned char c, struct tty_struct *tty)
826{
Joe Petersona88a69c2009-01-02 13:40:53 +0000827 mutex_lock(&tty->echo_lock);
828
829 if (c == ECHO_OP_START) {
830 add_echo_byte(ECHO_OP_START, tty);
831 add_echo_byte(ECHO_OP_START, tty);
832 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600833 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Joe Petersona88a69c2009-01-02 13:40:53 +0000834 add_echo_byte(ECHO_OP_START, tty);
835 add_echo_byte(c, tty);
836 }
837
838 mutex_unlock(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Alan Cox17b82062008-10-13 10:45:06 +0100841/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000842 * finish_erasing - complete erase
Alan Cox17b82062008-10-13 10:45:06 +0100843 * @tty: tty doing the erase
Alan Cox17b82062008-10-13 10:45:06 +0100844 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static inline void finish_erasing(struct tty_struct *tty)
847{
848 if (tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 echo_char_raw('/', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 tty->erasing = 0;
851 }
852}
853
854/**
855 * eraser - handle erase function
856 * @c: character input
857 * @tty: terminal device
858 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200859 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * present in the stream from the driver layer. Handles the complexities
861 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100862 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000863 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Alan Cox4edf1822008-02-08 04:18:44 -0800865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866static void eraser(unsigned char c, struct tty_struct *tty)
867{
868 enum { ERASE, WERASE, KILL } kill_type;
869 int head, seen_alnums, cnt;
870 unsigned long flags;
871
Alan Cox17b82062008-10-13 10:45:06 +0100872 /* FIXME: locking needed ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (tty->read_head == tty->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000874 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return;
876 }
877 if (c == ERASE_CHAR(tty))
878 kill_type = ERASE;
879 else if (c == WERASE_CHAR(tty))
880 kill_type = WERASE;
881 else {
882 if (!L_ECHO(tty)) {
883 spin_lock_irqsave(&tty->read_lock, flags);
884 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
885 (N_TTY_BUF_SIZE - 1));
886 tty->read_head = tty->canon_head;
887 spin_unlock_irqrestore(&tty->read_lock, flags);
888 return;
889 }
890 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
891 spin_lock_irqsave(&tty->read_lock, flags);
892 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
893 (N_TTY_BUF_SIZE - 1));
894 tty->read_head = tty->canon_head;
895 spin_unlock_irqrestore(&tty->read_lock, flags);
896 finish_erasing(tty);
897 echo_char(KILL_CHAR(tty), tty);
898 /* Add a newline if ECHOK is on and ECHOKE is off. */
899 if (L_ECHOK(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000900 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return;
902 }
903 kill_type = KILL;
904 }
905
906 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100907 /* FIXME: Locking ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 while (tty->read_head != tty->canon_head) {
909 head = tty->read_head;
910
911 /* erase a single possibly multibyte character */
912 do {
913 head = (head - 1) & (N_TTY_BUF_SIZE-1);
914 c = tty->read_buf[head];
915 } while (is_continuation(c, tty) && head != tty->canon_head);
916
917 /* do not partially erase */
918 if (is_continuation(c, tty))
919 break;
920
921 if (kill_type == WERASE) {
922 /* Equivalent to BSD's ALTWERASE. */
923 if (isalnum(c) || c == '_')
924 seen_alnums++;
925 else if (seen_alnums)
926 break;
927 }
928 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
929 spin_lock_irqsave(&tty->read_lock, flags);
930 tty->read_head = head;
931 tty->read_cnt -= cnt;
932 spin_unlock_irqrestore(&tty->read_lock, flags);
933 if (L_ECHO(tty)) {
934 if (L_ECHOPRT(tty)) {
935 if (!tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000936 echo_char_raw('\\', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 tty->erasing = 1;
938 }
939 /* if cnt > 1, output a multi-byte character */
940 echo_char(c, tty);
941 while (--cnt > 0) {
942 head = (head+1) & (N_TTY_BUF_SIZE-1);
Joe Petersona88a69c2009-01-02 13:40:53 +0000943 echo_char_raw(tty->read_buf[head], tty);
944 echo_move_back_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
947 echo_char(ERASE_CHAR(tty), tty);
948 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000949 unsigned int num_chars = 0;
950 int after_tab = 0;
951 unsigned long tail = tty->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Joe Petersona88a69c2009-01-02 13:40:53 +0000953 /*
954 * Count the columns used for characters
955 * since the start of input or after a
956 * previous tab.
957 * This info is used to go back the correct
958 * number of columns.
959 */
960 while (tail != tty->canon_head) {
961 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 c = tty->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000963 if (c == '\t') {
964 after_tab = 1;
965 break;
Alan Cox300a6202009-01-02 13:41:04 +0000966 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000968 num_chars += 2;
969 } else if (!is_continuation(c, tty)) {
970 num_chars++;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000973 echo_erase_tab(num_chars, after_tab, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 } else {
975 if (iscntrl(c) && L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000976 echo_char_raw('\b', tty);
977 echo_char_raw(' ', tty);
978 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000981 echo_char_raw('\b', tty);
982 echo_char_raw(' ', tty);
983 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 }
986 }
987 if (kill_type == ERASE)
988 break;
989 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000990 if (tty->read_head == tty->canon_head && L_ECHO(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 finish_erasing(tty);
992}
993
994/**
995 * isig - handle the ISIG optio
996 * @sig: signal
997 * @tty: terminal
998 * @flush: force flush
999 *
1000 * Called when a signal is being sent due to terminal input. This
1001 * may caus terminal flushing to take place according to the termios
1002 * settings and character used. Called from the driver receive_buf
1003 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001004 *
1005 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Alan Cox4edf1822008-02-08 04:18:44 -08001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static inline void isig(int sig, struct tty_struct *tty, int flush)
1009{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001010 if (tty->pgrp)
1011 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (flush || !L_NOFLSH(tty)) {
1013 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001014 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017
1018/**
1019 * n_tty_receive_break - handle break
1020 * @tty: terminal
1021 *
1022 * An RS232 break event has been hit in the incoming bitstream. This
1023 * can cause a variety of events depending upon the termios settings.
1024 *
1025 * Called from the receive_buf path so single threaded.
1026 */
Alan Cox4edf1822008-02-08 04:18:44 -08001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static inline void n_tty_receive_break(struct tty_struct *tty)
1029{
1030 if (I_IGNBRK(tty))
1031 return;
1032 if (I_BRKINT(tty)) {
1033 isig(SIGINT, tty, 1);
1034 return;
1035 }
1036 if (I_PARMRK(tty)) {
1037 put_tty_queue('\377', tty);
1038 put_tty_queue('\0', tty);
1039 }
1040 put_tty_queue('\0', tty);
1041 wake_up_interruptible(&tty->read_wait);
1042}
1043
1044/**
1045 * n_tty_receive_overrun - handle overrun reporting
1046 * @tty: terminal
1047 *
1048 * Data arrived faster than we could process it. While the tty
1049 * driver has flagged this the bits that were missed are gone
1050 * forever.
1051 *
1052 * Called from the receive_buf path so single threaded. Does not
1053 * need locking as num_overrun and overrun_time are function
1054 * private.
1055 */
Alan Cox4edf1822008-02-08 04:18:44 -08001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static inline void n_tty_receive_overrun(struct tty_struct *tty)
1058{
1059 char buf[64];
1060
1061 tty->num_overrun++;
1062 if (time_before(tty->overrun_time, jiffies - HZ) ||
1063 time_after(tty->overrun_time, jiffies)) {
1064 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1065 tty_name(tty, buf),
1066 tty->num_overrun);
1067 tty->overrun_time = jiffies;
1068 tty->num_overrun = 0;
1069 }
1070}
1071
1072/**
1073 * n_tty_receive_parity_error - error notifier
1074 * @tty: terminal device
1075 * @c: character
1076 *
1077 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001078 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
1080static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1081 unsigned char c)
1082{
Alan Cox4edf1822008-02-08 04:18:44 -08001083 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (I_PARMRK(tty)) {
1086 put_tty_queue('\377', tty);
1087 put_tty_queue('\0', tty);
1088 put_tty_queue(c, tty);
1089 } else if (I_INPCK(tty))
1090 put_tty_queue('\0', tty);
1091 else
1092 put_tty_queue(c, tty);
1093 wake_up_interruptible(&tty->read_wait);
1094}
1095
1096/**
1097 * n_tty_receive_char - perform processing
1098 * @tty: terminal device
1099 * @c: character
1100 *
1101 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001102 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * driver above.
1104 */
1105
1106static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1107{
1108 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001109 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (tty->raw) {
1112 put_tty_queue(c, tty);
1113 return;
1114 }
Alan Cox4edf1822008-02-08 04:18:44 -08001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (I_ISTRIP(tty))
1117 c &= 0x7f;
1118 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001119 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
hyc@symas.com26df6d12010-06-22 10:14:49 -07001121 if (L_EXTPROC(tty)) {
1122 put_tty_queue(c, tty);
1123 return;
1124 }
1125
Joe Peterson54d2a372008-02-06 01:37:59 -08001126 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001127 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1128 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001129 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001130 process_echoes(tty);
1131 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (tty->closing) {
1134 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001135 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001137 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001138 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 stop_tty(tty);
1140 }
1141 return;
1142 }
1143
1144 /*
1145 * If the previous character was LNEXT, or we know that this
1146 * character is not one of the characters that we'll have to
1147 * handle specially, do shortcut processing to speed things
1148 * up.
1149 */
1150 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 tty->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001152 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1153 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1154 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001155 if (L_ECHO(tty))
1156 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001157 return;
1158 }
1159 if (L_ECHO(tty)) {
1160 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /* Record the column of first canon char. */
1162 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001163 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001165 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001167 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 put_tty_queue(c, tty);
1169 put_tty_queue(c, tty);
1170 return;
1171 }
Alan Cox4edf1822008-02-08 04:18:44 -08001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (I_IXON(tty)) {
1174 if (c == START_CHAR(tty)) {
1175 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001176 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return;
1178 }
1179 if (c == STOP_CHAR(tty)) {
1180 stop_tty(tty);
1181 return;
1182 }
1183 }
Joe Peterson575537b32008-04-30 00:53:30 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (L_ISIG(tty)) {
1186 int signal;
1187 signal = SIGINT;
1188 if (c == INTR_CHAR(tty))
1189 goto send_signal;
1190 signal = SIGQUIT;
1191 if (c == QUIT_CHAR(tty))
1192 goto send_signal;
1193 signal = SIGTSTP;
1194 if (c == SUSP_CHAR(tty)) {
1195send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001196 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001197 * Note that we do not use isig() here because we want
1198 * the order to be:
1199 * 1) flush, 2) echo, 3) signal
1200 */
1201 if (!L_NOFLSH(tty)) {
1202 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001203 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001204 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001205 if (I_IXON(tty))
1206 start_tty(tty);
1207 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001208 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001209 process_echoes(tty);
1210 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001211 if (tty->pgrp)
1212 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return;
1214 }
1215 }
Joe Peterson575537b32008-04-30 00:53:30 -07001216
1217 if (c == '\r') {
1218 if (I_IGNCR(tty))
1219 return;
1220 if (I_ICRNL(tty))
1221 c = '\n';
1222 } else if (c == '\n' && I_INLCR(tty))
1223 c = '\r';
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (tty->icanon) {
1226 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1227 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1228 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001229 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
1231 }
1232 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1233 tty->lnext = 1;
1234 if (L_ECHO(tty)) {
1235 finish_erasing(tty);
1236 if (L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001237 echo_char_raw('^', tty);
1238 echo_char_raw('\b', tty);
1239 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 }
1242 return;
1243 }
1244 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1245 L_IEXTEN(tty)) {
1246 unsigned long tail = tty->canon_head;
1247
1248 finish_erasing(tty);
1249 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001250 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 while (tail != tty->read_head) {
1252 echo_char(tty->read_buf[tail], tty);
1253 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1254 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001255 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return;
1257 }
1258 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001259 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001260 if (L_ECHO(tty))
1261 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001262 return;
1263 }
1264 if (L_ECHO(tty) || L_ECHONL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001265 echo_char_raw('\n', tty);
1266 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 goto handle_newline;
1269 }
1270 if (c == EOF_CHAR(tty)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001271 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1272 return;
Alan Cox4edf1822008-02-08 04:18:44 -08001273 if (tty->canon_head != tty->read_head)
1274 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 c = __DISABLED_CHAR;
1276 goto handle_newline;
1277 }
1278 if ((c == EOL_CHAR(tty)) ||
1279 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001280 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1281 ? 1 : 0;
1282 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001283 if (L_ECHO(tty))
1284 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001285 return;
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /*
1288 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1289 */
1290 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* Record the column of first canon char. */
1292 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001293 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001295 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 /*
1298 * XXX does PARMRK doubling happen for
1299 * EOL_CHAR and EOL2_CHAR?
1300 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001301 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 put_tty_queue(c, tty);
1303
Alan Cox4edf1822008-02-08 04:18:44 -08001304handle_newline:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 spin_lock_irqsave(&tty->read_lock, flags);
1306 set_bit(tty->read_head, tty->read_flags);
1307 put_tty_queue_nolock(c, tty);
1308 tty->canon_head = tty->read_head;
1309 tty->canon_data++;
1310 spin_unlock_irqrestore(&tty->read_lock, flags);
1311 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1312 if (waitqueue_active(&tty->read_wait))
1313 wake_up_interruptible(&tty->read_wait);
1314 return;
1315 }
1316 }
Alan Cox4edf1822008-02-08 04:18:44 -08001317
Joe Petersonacc71bb2009-01-02 13:43:32 +00001318 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1319 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1320 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001321 if (L_ECHO(tty))
1322 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001323 return;
1324 }
1325 if (L_ECHO(tty)) {
1326 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (c == '\n')
Joe Petersona88a69c2009-01-02 13:40:53 +00001328 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 else {
1330 /* Record the column of first canon char. */
1331 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001332 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 echo_char(c, tty);
1334 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001335 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
Joe Petersonacc71bb2009-01-02 13:43:32 +00001338 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 put_tty_queue(c, tty);
1340
1341 put_tty_queue(c, tty);
Alan Cox4edf1822008-02-08 04:18:44 -08001342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345/**
1346 * n_tty_write_wakeup - asynchronous I/O notifier
1347 * @tty: tty device
1348 *
1349 * Required for the ptys, serial driver etc. since processes
1350 * that attach themselves to the master and rely on ASYNC
1351 * IO must be woken up
1352 */
1353
1354static void n_tty_write_wakeup(struct tty_struct *tty)
1355{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001356 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360/**
1361 * n_tty_receive_buf - data receive
1362 * @tty: terminal device
1363 * @cp: buffer
1364 * @fp: flag buffer
1365 * @count: characters
1366 *
1367 * Called by the terminal driver when a block of characters has
1368 * been received. This function must be called from soft contexts
1369 * not from interrupt context. The driver is responsible for making
1370 * calls one at a time and in order (or using flush_to_ldisc)
1371 */
Alan Cox4edf1822008-02-08 04:18:44 -08001372
Linus Torvalds55db4c62011-06-04 06:33:24 +09001373static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1374 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 const unsigned char *p;
1377 char *f, flags = TTY_NORMAL;
1378 int i;
1379 char buf[64];
1380 unsigned long cpuflags;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (tty->real_raw) {
1383 spin_lock_irqsave(&tty->read_lock, cpuflags);
1384 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1385 N_TTY_BUF_SIZE - tty->read_head);
1386 i = min(count, i);
1387 memcpy(tty->read_buf + tty->read_head, cp, i);
1388 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1389 tty->read_cnt += i;
1390 cp += i;
1391 count -= i;
1392
1393 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1394 N_TTY_BUF_SIZE - tty->read_head);
1395 i = min(count, i);
1396 memcpy(tty->read_buf + tty->read_head, cp, i);
1397 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1398 tty->read_cnt += i;
1399 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1400 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001401 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (f)
1403 flags = *f++;
1404 switch (flags) {
1405 case TTY_NORMAL:
1406 n_tty_receive_char(tty, *p);
1407 break;
1408 case TTY_BREAK:
1409 n_tty_receive_break(tty);
1410 break;
1411 case TTY_PARITY:
1412 case TTY_FRAME:
1413 n_tty_receive_parity_error(tty, *p);
1414 break;
1415 case TTY_OVERRUN:
1416 n_tty_receive_overrun(tty);
1417 break;
1418 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001419 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 tty_name(tty, buf), flags);
1421 break;
1422 }
1423 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001424 if (tty->ops->flush_chars)
1425 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Linus Torvalds55db4c62011-06-04 06:33:24 +09001428 n_tty_set_room(tty);
1429
hyc@symas.com26df6d12010-06-22 10:14:49 -07001430 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1431 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1433 if (waitqueue_active(&tty->read_wait))
1434 wake_up_interruptible(&tty->read_wait);
1435 }
1436
1437 /*
1438 * Check the remaining room for the input canonicalization
1439 * mode. We don't want to throttle the driver if we're in
1440 * canonical mode and don't have a newline yet!
1441 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001442 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001443 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001444
1445 /* FIXME: there is a tiny race here if the receive room check runs
1446 before the other work executes and empties the buffer (upping
1447 the receiving room and unthrottling. We then throttle and get
1448 stuck. This has been observed and traced down by Vincent Pillet/
1449 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
1452int is_ignored(int sig)
1453{
1454 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001455 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458/**
1459 * n_tty_set_termios - termios data changed
1460 * @tty: terminal
1461 * @old: previous data
1462 *
1463 * Called by the tty layer when the user changes termios flags so
1464 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001465 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * guaranteed that this function will not be re-entered or in progress
1467 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001468 *
1469 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Alan Cox4edf1822008-02-08 04:18:44 -08001471
1472static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Alan Cox47afa7a2008-10-13 10:44:17 +01001474 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001475
1476 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001477 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001478 if (canon_change) {
1479 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1480 tty->canon_head = tty->read_tail;
1481 tty->canon_data = 0;
1482 tty->erasing = 0;
1483 }
1484
1485 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1486 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 tty->icanon = (L_ICANON(tty) != 0);
1489 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1490 tty->raw = 1;
1491 tty->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001492 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return;
1494 }
1495 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1496 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1497 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1498 I_PARMRK(tty)) {
1499 memset(tty->process_char_map, 0, 256/8);
1500
1501 if (I_IGNCR(tty) || I_ICRNL(tty))
1502 set_bit('\r', tty->process_char_map);
1503 if (I_INLCR(tty))
1504 set_bit('\n', tty->process_char_map);
1505
1506 if (L_ICANON(tty)) {
1507 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1508 set_bit(KILL_CHAR(tty), tty->process_char_map);
1509 set_bit(EOF_CHAR(tty), tty->process_char_map);
1510 set_bit('\n', tty->process_char_map);
1511 set_bit(EOL_CHAR(tty), tty->process_char_map);
1512 if (L_IEXTEN(tty)) {
1513 set_bit(WERASE_CHAR(tty),
1514 tty->process_char_map);
1515 set_bit(LNEXT_CHAR(tty),
1516 tty->process_char_map);
1517 set_bit(EOL2_CHAR(tty),
1518 tty->process_char_map);
1519 if (L_ECHO(tty))
1520 set_bit(REPRINT_CHAR(tty),
1521 tty->process_char_map);
1522 }
1523 }
1524 if (I_IXON(tty)) {
1525 set_bit(START_CHAR(tty), tty->process_char_map);
1526 set_bit(STOP_CHAR(tty), tty->process_char_map);
1527 }
1528 if (L_ISIG(tty)) {
1529 set_bit(INTR_CHAR(tty), tty->process_char_map);
1530 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1531 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1532 }
1533 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1534 tty->raw = 0;
1535 tty->real_raw = 0;
1536 } else {
1537 tty->raw = 1;
1538 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1539 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1540 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1541 tty->real_raw = 1;
1542 else
1543 tty->real_raw = 0;
1544 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001545 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001546 /* The termios change make the tty ready for I/O */
1547 wake_up_interruptible(&tty->write_wait);
1548 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551/**
1552 * n_tty_close - close the ldisc for this tty
1553 * @tty: device
1554 *
Alan Cox4edf1822008-02-08 04:18:44 -08001555 * Called from the terminal layer when this line discipline is
1556 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * discipline change. The function will not be called while other
1558 * ldisc methods are in progress.
1559 */
Alan Cox4edf1822008-02-08 04:18:44 -08001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static void n_tty_close(struct tty_struct *tty)
1562{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001563 struct n_tty_data *ldata = tty->disc_data;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 n_tty_flush_buffer(tty);
Jiri Slabyb91939f2012-10-18 22:26:35 +02001566 kfree(tty->read_buf);
1567 kfree(tty->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001568 kfree(ldata);
Jiri Slabyb91939f2012-10-18 22:26:35 +02001569 tty->read_buf = NULL;
1570 tty->echo_buf = NULL;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001571 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574/**
1575 * n_tty_open - open an ldisc
1576 * @tty: terminal to open
1577 *
Alan Cox4edf1822008-02-08 04:18:44 -08001578 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * terminal device. Can sleep. Called serialized so that no
1580 * other events will occur in parallel. No further open will occur
1581 * until a close.
1582 */
1583
1584static int n_tty_open(struct tty_struct *tty)
1585{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001586 struct n_tty_data *ldata;
1587
1588 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1589 if (!ldata)
1590 goto err;
1591
Joe Petersona88a69c2009-01-02 13:40:53 +00001592 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyb91939f2012-10-18 22:26:35 +02001593 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1594 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1595 if (!tty->read_buf || !tty->echo_buf)
1596 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001597
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001598 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001600 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 tty->column = 0;
1602 n_tty_set_termios(tty, NULL);
1603 tty->minimum_to_wake = 1;
1604 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001607err_free_bufs:
1608 kfree(tty->read_buf);
1609 kfree(tty->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001610 kfree(ldata);
1611err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001612 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615static inline int input_available_p(struct tty_struct *tty, int amt)
1616{
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001617 tty_flush_to_ldisc(tty);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001618 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (tty->canon_data)
1620 return 1;
1621 } else if (tty->read_cnt >= (amt ? amt : 1))
1622 return 1;
1623
1624 return 0;
1625}
1626
1627/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001628 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * @tty: terminal device
1630 * @b: user data
1631 * @nr: size of data
1632 *
Alan Cox11a96d12008-10-13 10:46:24 +01001633 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * ICANON is off; it copies characters straight from the tty queue to
1635 * user space directly. It can be profitably called twice; once to
1636 * drain the space from the tail pointer to the (physical) end of the
1637 * buffer, and once to drain the space from the (physical) beginning of
1638 * the buffer to head pointer.
1639 *
Paul Fulghum817d6d32006-06-28 04:26:47 -07001640 * Called under the tty->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 *
1642 */
Alan Cox4edf1822008-02-08 04:18:44 -08001643
Alan Cox33f0f882006-01-09 20:54:13 -08001644static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 unsigned char __user **b,
1646 size_t *nr)
1647
1648{
1649 int retval;
1650 size_t n;
1651 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001652 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 retval = 0;
1655 spin_lock_irqsave(&tty->read_lock, flags);
1656 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1657 n = min(*nr, n);
1658 spin_unlock_irqrestore(&tty->read_lock, flags);
1659 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1661 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001662 is_eof = n == 1 &&
1663 tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
Jiri Slaby6c633f22012-10-18 22:26:37 +02001664 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n,
1665 tty->icanon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 spin_lock_irqsave(&tty->read_lock, flags);
1667 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1668 tty->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001669 /* Turn single EOF into zero-length read */
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001670 if (L_EXTPROC(tty) && tty->icanon && is_eof && !tty->read_cnt)
1671 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 spin_unlock_irqrestore(&tty->read_lock, flags);
1673 *b += n;
1674 *nr -= n;
1675 }
1676 return retval;
1677}
1678
Al Virocc4191d2008-03-29 03:08:48 +00001679extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001680 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/**
1683 * job_control - check job control
1684 * @tty: tty
1685 * @file: file handle
1686 *
1687 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001688 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001690 *
1691 * FIXME:
1692 * Locking: None - redirected write test is safe, testing
1693 * current->signal should possibly lock current->sighand
1694 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Alan Cox4edf1822008-02-08 04:18:44 -08001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697static int job_control(struct tty_struct *tty, struct file *file)
1698{
1699 /* Job control check -- must be done at start and after
1700 every sleep (POSIX.1 7.1.1.4). */
1701 /* NOTE: not yet done after every sleep pending a thorough
1702 check of the logic of this change. -- jlc */
1703 /* don't stop on /dev/console */
1704 if (file->f_op->write != redirected_tty_write &&
1705 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001706 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001707 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001708 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001710 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001712 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001713 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return -ERESTARTSYS;
1715 }
1716 }
1717 return 0;
1718}
Alan Cox4edf1822008-02-08 04:18:44 -08001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721/**
Alan Cox11a96d12008-10-13 10:46:24 +01001722 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 * @tty: tty device
1724 * @file: file object
1725 * @buf: userspace buffer pointer
1726 * @nr: size of I/O
1727 *
1728 * Perform reads for the line discipline. We are guaranteed that the
1729 * line discipline will not be closed under us but we may get multiple
1730 * parallel readers and must handle this ourselves. We may also get
1731 * a hangup. Always called in user context, may sleep.
1732 *
1733 * This code must be sure never to sleep through a hangup.
1734 */
Alan Cox4edf1822008-02-08 04:18:44 -08001735
Alan Cox11a96d12008-10-13 10:46:24 +01001736static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 unsigned char __user *buf, size_t nr)
1738{
1739 unsigned char __user *b = buf;
1740 DECLARE_WAITQUEUE(wait, current);
1741 int c;
1742 int minimum, time;
1743 ssize_t retval = 0;
1744 ssize_t size;
1745 long timeout;
1746 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001747 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001751 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 minimum = time = 0;
1755 timeout = MAX_SCHEDULE_TIMEOUT;
1756 if (!tty->icanon) {
1757 time = (HZ / 10) * TIME_CHAR(tty);
1758 minimum = MIN_CHAR(tty);
1759 if (minimum) {
1760 if (time)
1761 tty->minimum_to_wake = 1;
1762 else if (!waitqueue_active(&tty->read_wait) ||
1763 (tty->minimum_to_wake > minimum))
1764 tty->minimum_to_wake = minimum;
1765 } else {
1766 timeout = 0;
1767 if (time) {
1768 timeout = time;
1769 time = 0;
1770 }
1771 tty->minimum_to_wake = minimum = 1;
1772 }
1773 }
1774
1775 /*
1776 * Internal serialization of reads.
1777 */
1778 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001779 if (!mutex_trylock(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001781 } else {
Ingo Molnar70522e12006-03-23 03:00:31 -08001782 if (mutex_lock_interruptible(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -ERESTARTSYS;
1784 }
Alan Cox04f378b2008-04-30 00:53:29 -07001785 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 while (nr) {
1789 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001790 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned char cs;
1792 if (b != buf)
1793 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001794 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 cs = tty->link->ctrl_status;
1796 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001797 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001798 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 retval = -EFAULT;
1800 b--;
1801 break;
1802 }
1803 nr--;
1804 break;
1805 }
1806 /* This statement must be first before checking for input
1807 so that any interrupt will set the state back to
1808 TASK_RUNNING. */
1809 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1812 ((minimum - (b - buf)) >= 1))
1813 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!input_available_p(tty, 0)) {
1816 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1817 retval = -EIO;
1818 break;
1819 }
1820 if (tty_hung_up_p(file))
1821 break;
1822 if (!timeout)
1823 break;
1824 if (file->f_flags & O_NONBLOCK) {
1825 retval = -EAGAIN;
1826 break;
1827 }
1828 if (signal_pending(current)) {
1829 retval = -ERESTARTSYS;
1830 break;
1831 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001832 /* FIXME: does n_tty_set_room need locking ? */
1833 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 continue;
1836 }
1837 __set_current_state(TASK_RUNNING);
1838
1839 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001840 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001841 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 retval = -EFAULT;
1843 b--;
1844 break;
1845 }
1846 nr--;
1847 }
1848
hyc@symas.com26df6d12010-06-22 10:14:49 -07001849 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* N.B. avoid overrun if nr == 0 */
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001851 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 while (nr && tty->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001853 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 eol = test_and_clear_bit(tty->read_tail,
1856 tty->read_flags);
1857 c = tty->read_buf[tty->read_tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 tty->read_tail = ((tty->read_tail+1) &
1859 (N_TTY_BUF_SIZE-1));
1860 tty->read_cnt--;
1861 if (eol) {
1862 /* this test should be redundant:
1863 * we shouldn't be reading data if
1864 * canon_data is 0
1865 */
1866 if (--tty->canon_data < 0)
1867 tty->canon_data = 0;
1868 }
1869 spin_unlock_irqrestore(&tty->read_lock, flags);
1870
1871 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001872 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 retval = -EFAULT;
1874 b--;
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001875 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
1877 }
1878 nr--;
1879 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001880 if (eol) {
1881 tty_audit_push(tty);
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001882 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001884 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001885 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001887 spin_unlock_irqrestore(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (retval)
1889 break;
1890 } else {
1891 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001892 /* The copy function takes the read lock and handles
1893 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 uncopied = copy_from_read_buf(tty, &b, &nr);
1895 uncopied += copy_from_read_buf(tty, &b, &nr);
1896 if (uncopied) {
1897 retval = -EFAULT;
1898 break;
1899 }
1900 }
1901
1902 /* If there is enough space in the read buffer now, let the
1903 * low-level driver know. We use n_tty_chars_in_buffer() to
1904 * check the buffer, as it now knows about canonical mode.
1905 * Otherwise, if the driver is throttled and the line is
1906 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1907 * we won't get any more characters.
1908 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001909 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1910 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (b - buf >= minimum)
1915 break;
1916 if (time)
1917 timeout = time;
1918 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001919 mutex_unlock(&tty->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 remove_wait_queue(&tty->read_wait, &wait);
1921
1922 if (!waitqueue_active(&tty->read_wait))
1923 tty->minimum_to_wake = minimum;
1924
1925 __set_current_state(TASK_RUNNING);
1926 size = b - buf;
1927 if (size) {
1928 retval = size;
1929 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001930 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001932 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Linus Torvalds55db4c62011-06-04 06:33:24 +09001934 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return retval;
1936}
1937
1938/**
Alan Cox11a96d12008-10-13 10:46:24 +01001939 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * @tty: tty device
1941 * @file: file object
1942 * @buf: userspace buffer pointer
1943 * @nr: size of I/O
1944 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001945 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001947 * and other such events. Since the receive code will echo characters,
1948 * thus calling driver write methods, the output_lock is used in
1949 * the output processing functions called here as well as in the
1950 * echo processing function to protect the column state and space
1951 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 *
1953 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001954 *
1955 * Locking: output_lock to protect column state and space left
1956 * (note that the process_output*() functions take this
1957 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Alan Cox4edf1822008-02-08 04:18:44 -08001959
Alan Cox11a96d12008-10-13 10:46:24 +01001960static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00001961 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 const unsigned char *b = buf;
1964 DECLARE_WAITQUEUE(wait, current);
1965 int c;
1966 ssize_t retval = 0;
1967
1968 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1969 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1970 retval = tty_check_change(tty);
1971 if (retval)
1972 return retval;
1973 }
1974
Joe Petersona88a69c2009-01-02 13:40:53 +00001975 /* Write out any echoed characters that are still pending */
1976 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 add_wait_queue(&tty->write_wait, &wait);
1979 while (1) {
1980 set_current_state(TASK_INTERRUPTIBLE);
1981 if (signal_pending(current)) {
1982 retval = -ERESTARTSYS;
1983 break;
1984 }
1985 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1986 retval = -EIO;
1987 break;
1988 }
1989 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1990 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001991 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (num < 0) {
1993 if (num == -EAGAIN)
1994 break;
1995 retval = num;
1996 goto break_out;
1997 }
1998 b += num;
1999 nr -= num;
2000 if (nr == 0)
2001 break;
2002 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002003 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
2005 b++; nr--;
2006 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002007 if (tty->ops->flush_chars)
2008 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002010 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002011 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002012 if (c < 0) {
2013 retval = c;
2014 goto break_out;
2015 }
2016 if (!c)
2017 break;
2018 b += c;
2019 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 if (!nr)
2023 break;
2024 if (file->f_flags & O_NONBLOCK) {
2025 retval = -EAGAIN;
2026 break;
2027 }
2028 schedule();
2029 }
2030break_out:
2031 __set_current_state(TASK_RUNNING);
2032 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002033 if (b - buf != nr && tty->fasync)
2034 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return (b - buf) ? b - buf : retval;
2036}
2037
2038/**
Alan Cox11a96d12008-10-13 10:46:24 +01002039 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 * @tty: terminal device
2041 * @file: file accessing it
2042 * @wait: poll table
2043 *
2044 * Called when the line discipline is asked to poll() for data or
2045 * for special events. This code is not serialized with respect to
2046 * other events save open/close.
2047 *
2048 * This code must be sure never to sleep through a hangup.
2049 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Alan Cox4edf1822008-02-08 04:18:44 -08002051
Alan Cox11a96d12008-10-13 10:46:24 +01002052static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002053 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 unsigned int mask = 0;
2056
2057 poll_wait(file, &tty->read_wait, wait);
2058 poll_wait(file, &tty->write_wait, wait);
2059 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2060 mask |= POLLIN | POLLRDNORM;
2061 if (tty->packet && tty->link->ctrl_status)
2062 mask |= POLLPRI | POLLIN | POLLRDNORM;
2063 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2064 mask |= POLLHUP;
2065 if (tty_hung_up_p(file))
2066 mask |= POLLHUP;
2067 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2068 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2069 tty->minimum_to_wake = MIN_CHAR(tty);
2070 else
2071 tty->minimum_to_wake = 1;
2072 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002073 if (tty->ops->write && !tty_is_writelocked(tty) &&
2074 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2075 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 mask |= POLLOUT | POLLWRNORM;
2077 return mask;
2078}
2079
Alan Cox47afa7a2008-10-13 10:44:17 +01002080static unsigned long inq_canon(struct tty_struct *tty)
2081{
2082 int nr, head, tail;
2083
Alan Cox17b82062008-10-13 10:45:06 +01002084 if (!tty->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002085 return 0;
2086 head = tty->canon_head;
2087 tail = tty->read_tail;
2088 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2089 /* Skip EOF-chars.. */
2090 while (head != tail) {
2091 if (test_bit(tail, tty->read_flags) &&
2092 tty->read_buf[tail] == __DISABLED_CHAR)
2093 nr--;
2094 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2095 }
2096 return nr;
2097}
2098
2099static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2100 unsigned int cmd, unsigned long arg)
2101{
2102 int retval;
2103
2104 switch (cmd) {
2105 case TIOCOUTQ:
2106 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2107 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002108 /* FIXME: Locking */
Alan Cox47afa7a2008-10-13 10:44:17 +01002109 retval = tty->read_cnt;
2110 if (L_ICANON(tty))
2111 retval = inq_canon(tty);
2112 return put_user(retval, (unsigned int __user *) arg);
2113 default:
2114 return n_tty_ioctl_helper(tty, file, cmd, arg);
2115 }
2116}
2117
Alan Coxa352def2008-07-16 21:53:12 +01002118struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002119 .magic = TTY_LDISC_MAGIC,
2120 .name = "n_tty",
2121 .open = n_tty_open,
2122 .close = n_tty_close,
2123 .flush_buffer = n_tty_flush_buffer,
2124 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002125 .read = n_tty_read,
2126 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002127 .ioctl = n_tty_ioctl,
2128 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002129 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002130 .receive_buf = n_tty_receive_buf,
2131 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002133
2134/**
2135 * n_tty_inherit_ops - inherit N_TTY methods
2136 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2137 *
2138 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2139 * methods.
2140 */
2141
2142void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2143{
2144 *ops = tty_ldisc_N_TTY;
2145 ops->owner = NULL;
2146 ops->refcount = ops->flags = 0;
2147}
2148EXPORT_SYMBOL_GPL(n_tty_inherit_ops);