blob: bd775a7c0629b7a9e431b5585dce988423cf5ce3 [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 {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020077 unsigned int column;
78 unsigned long overrun_time;
79 int num_overrun;
80
81 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
82 unsigned char echo_overrun:1;
Jiri Slaby70ece7a2012-10-18 22:26:38 +020083};
84
Miloslav Trmac522ed772007-07-15 23:40:56 -070085static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
86 unsigned char __user *ptr)
87{
Jiri Slaby53c5ee22012-10-18 22:26:39 +020088 struct n_tty_data *ldata = tty->disc_data;
89
90 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -070091 return put_user(x, ptr);
92}
93
Linus Torvalds55db4c62011-06-04 06:33:24 +090094/**
95 * n_tty_set__room - receive space
96 * @tty: terminal
97 *
98 * Called by the driver to find out how much data it is
99 * permitted to feed to the line discipline without any being lost
100 * and thus to manage flow control. Not serialized. Answers for the
101 * "instant".
102 */
103
104static void n_tty_set_room(struct tty_struct *tty)
105{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200106 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500107 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900108 int old_left;
109
Jaeden Amero090abf72012-07-27 08:43:11 -0500110 /* tty->read_cnt is not read locked ? */
111 if (I_PARMRK(tty)) {
112 /* Multiply read_cnt by 3, since each byte might take up to
113 * three times as many spaces when PARMRK is set (depending on
114 * its flags, e.g. parity error). */
115 left = N_TTY_BUF_SIZE - tty->read_cnt * 3 - 1;
116 } else
117 left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
118
Linus Torvalds55db4c62011-06-04 06:33:24 +0900119 /*
120 * If we are doing input canonicalization, and there are no
121 * pending newlines, let characters through without limit, so
122 * that erase characters will be handled. Other excess
123 * characters will be beeped.
124 */
125 if (left <= 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200126 left = ldata->icanon && !tty->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900127 old_left = tty->receive_room;
128 tty->receive_room = left;
129
130 /* Did this open up the receive buffer? We may need to flip */
131 if (left && !old_left)
132 schedule_work(&tty->buf.work);
133}
134
Alan Cox33f0f882006-01-09 20:54:13 -0800135static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 if (tty->read_cnt < N_TTY_BUF_SIZE) {
138 tty->read_buf[tty->read_head] = c;
139 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
140 tty->read_cnt++;
141 }
142}
143
Alan Cox17b82062008-10-13 10:45:06 +0100144/**
145 * put_tty_queue - add character to tty
146 * @c: character
147 * @tty: tty device
148 *
149 * Add a character to the tty read_buf queue. This is done under the
150 * read_lock to serialize character addition and also to protect us
151 * against parallel reads or flushes
152 */
153
Alan Cox33f0f882006-01-09 20:54:13 -0800154static void put_tty_queue(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 unsigned long flags;
157 /*
158 * The problem of stomping on the buffers ends here.
159 * Why didn't anyone see this one coming? --AJK
160 */
161 spin_lock_irqsave(&tty->read_lock, flags);
162 put_tty_queue_nolock(c, tty);
163 spin_unlock_irqrestore(&tty->read_lock, flags);
164}
165
166/**
167 * check_unthrottle - allow new receive data
168 * @tty; tty device
169 *
Alan Cox17b82062008-10-13 10:45:06 +0100170 * Check whether to call the driver unthrottle functions
171 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800172 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * this is not guaranteed.
174 */
Alan Cox4edf1822008-02-08 04:18:44 -0800175static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Alan Cox39c2e602008-04-30 00:54:18 -0700177 if (tty->count)
178 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**
182 * reset_buffer_flags - reset buffer state
183 * @tty: terminal to reset
184 *
Alan Cox4edf1822008-02-08 04:18:44 -0800185 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * and make sure the driver is unthrottled. Called
187 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100188 *
189 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void reset_buffer_flags(struct tty_struct *tty)
193{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200194 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long flags;
196
197 spin_lock_irqsave(&tty->read_lock, flags);
198 tty->read_head = tty->read_tail = tty->read_cnt = 0;
199 spin_unlock_irqrestore(&tty->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000200
201 mutex_lock(&tty->echo_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200202 tty->echo_pos = tty->echo_cnt = ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000203 mutex_unlock(&tty->echo_lock);
204
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200205 tty->canon_head = tty->canon_data = ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memset(&tty->read_flags, 0, sizeof tty->read_flags);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900207 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/**
211 * n_tty_flush_buffer - clean input queue
212 * @tty: terminal device
213 *
214 * Flush the input buffer. Called when the line discipline is
215 * being closed, when the tty layer wants the buffer flushed (eg
216 * at hangup) or when the N_TTY line discipline internally has to
217 * clean the pending queue (for example some signals).
218 *
Alan Cox17b82062008-10-13 10:45:06 +0100219 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Alan Cox4edf1822008-02-08 04:18:44 -0800221
222static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Alan Cox04f378b2008-04-30 00:53:29 -0700224 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* clear everything and unthrottle the driver */
226 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!tty->link)
229 return;
230
Alan Cox04f378b2008-04-30 00:53:29 -0700231 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (tty->link->packet) {
233 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
234 wake_up_interruptible(&tty->link->read_wait);
235 }
Alan Cox04f378b2008-04-30 00:53:29 -0700236 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/**
240 * n_tty_chars_in_buffer - report available bytes
241 * @tty: tty device
242 *
243 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800244 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100245 *
246 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
Alan Cox4edf1822008-02-08 04:18:44 -0800248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
250{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200251 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long flags;
253 ssize_t n = 0;
254
255 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200256 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 n = tty->read_cnt;
258 } else if (tty->canon_data) {
259 n = (tty->canon_head > tty->read_tail) ?
260 tty->canon_head - tty->read_tail :
261 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
262 }
263 spin_unlock_irqrestore(&tty->read_lock, flags);
264 return n;
265}
266
267/**
268 * is_utf8_continuation - utf8 multibyte check
269 * @c: byte to check
270 *
271 * Returns true if the utf8 character 'c' is a multibyte continuation
272 * character. We use this to correctly compute the on screen size
273 * of the character when printing
274 */
Alan Cox4edf1822008-02-08 04:18:44 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static inline int is_utf8_continuation(unsigned char c)
277{
278 return (c & 0xc0) == 0x80;
279}
280
281/**
282 * is_continuation - multibyte check
283 * @c: byte to check
284 *
285 * Returns true if the utf8 character 'c' is a multibyte continuation
286 * character and the terminal is in unicode mode.
287 */
Alan Cox4edf1822008-02-08 04:18:44 -0800288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static inline int is_continuation(unsigned char c, struct tty_struct *tty)
290{
291 return I_IUTF8(tty) && is_utf8_continuation(c);
292}
293
294/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000295 * do_output_char - output one character
296 * @c: character (or partial unicode symbol)
297 * @tty: terminal device
298 * @space: space available in tty driver write buffer
299 *
300 * This is a helper function that handles one output character
301 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600302 * doing OPOST processing and putting the results in the
303 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000304 *
305 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
306 * and NLDLY. They simply aren't relevant in the world today.
307 * If you ever need them, add them here.
308 *
309 * Returns the number of bytes of buffer space used or -1 if
310 * no space left.
311 *
312 * Locking: should be called under the output_lock to protect
313 * the column state and space left in the buffer
314 */
315
316static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
317{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200318 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000319 int spaces;
320
321 if (!space)
322 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000323
Joe Petersona88a69c2009-01-02 13:40:53 +0000324 switch (c) {
325 case '\n':
326 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200327 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000328 if (O_ONLCR(tty)) {
329 if (space < 2)
330 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200331 tty->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700332 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000333 return 2;
334 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200335 tty->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000336 break;
337 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200338 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000339 return 0;
340 if (O_OCRNL(tty)) {
341 c = '\n';
342 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200343 tty->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000344 break;
345 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200346 tty->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000347 break;
348 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200349 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000350 if (O_TABDLY(tty) == XTABS) {
351 if (space < spaces)
352 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200353 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000354 tty->ops->write(tty, " ", spaces);
355 return spaces;
356 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200357 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000358 break;
359 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200360 if (ldata->column > 0)
361 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000362 break;
363 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000364 if (!iscntrl(c)) {
365 if (O_OLCUC(tty))
366 c = toupper(c);
367 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200368 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000369 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000370 break;
371 }
372
373 tty_put_char(tty, c);
374 return 1;
375}
376
377/**
378 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * @c: character (or partial unicode symbol)
380 * @tty: terminal device
381 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600382 * Output one character with OPOST processing.
383 * Returns -1 when the output device is full and the character
384 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 * Locking: output_lock to protect column state and space left
387 * (also, this is called from n_tty_write under the
388 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Alan Cox4edf1822008-02-08 04:18:44 -0800390
Joe Petersona88a69c2009-01-02 13:40:53 +0000391static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Joe Petersona88a69c2009-01-02 13:40:53 +0000393 int space, retval;
394
395 mutex_lock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Alan Coxf34d7a52008-04-30 00:54:13 -0700397 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000398 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Joe Petersona88a69c2009-01-02 13:40:53 +0000400 mutex_unlock(&tty->output_lock);
401 if (retval < 0)
402 return -1;
403 else
404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000408 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600410 * @buf: character buffer
411 * @nr: number of bytes to output
412 *
413 * Output a block of characters with OPOST processing.
414 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
416 * This path is used to speed up block console writes, among other
417 * things when processing blocks of output data. It handles only
418 * the simple cases normally found and helps to generate blocks of
419 * symbols for the console driver and thus improve performance.
420 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000421 * Locking: output_lock to protect column state and space left
422 * (also, this is called from n_tty_write under the
423 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Alan Cox4edf1822008-02-08 04:18:44 -0800425
Joe Petersona88a69c2009-01-02 13:40:53 +0000426static ssize_t process_output_block(struct tty_struct *tty,
427 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200429 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100431 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 const unsigned char *cp;
433
Joe Petersona88a69c2009-01-02 13:40:53 +0000434 mutex_lock(&tty->output_lock);
435
Alan Coxf34d7a52008-04-30 00:54:13 -0700436 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000437 if (!space) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (nr > space)
442 nr = space;
443
444 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000445 unsigned char c = *cp;
446
447 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case '\n':
449 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200450 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (O_ONLCR(tty))
452 goto break_out;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200453 tty->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200456 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto break_out;
458 if (O_OCRNL(tty))
459 goto break_out;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200460 tty->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case '\t':
463 goto break_out;
464 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200465 if (ldata->column > 0)
466 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000469 if (!iscntrl(c)) {
470 if (O_OLCUC(tty))
471 goto break_out;
472 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200473 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 }
477 }
478break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700479 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000480
481 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return i;
483}
484
Joe Petersona88a69c2009-01-02 13:40:53 +0000485/**
486 * process_echoes - write pending echo characters
487 * @tty: terminal device
488 *
489 * Write previously buffered echo (and other ldisc-generated)
490 * characters to the tty.
491 *
492 * Characters generated by the ldisc (including echoes) need to
493 * be buffered because the driver's write buffer can fill during
494 * heavy program output. Echoing straight to the driver will
495 * often fail under these conditions, causing lost characters and
496 * resulting mismatches of ldisc state information.
497 *
498 * Since the ldisc state must represent the characters actually sent
499 * to the driver at the time of the write, operations like certain
500 * changes in column state are also saved in the buffer and executed
501 * here.
502 *
503 * A circular fifo buffer is used so that the most recent characters
504 * are prioritized. Also, when control characters are echoed with a
505 * prefixed "^", the pair is treated atomically and thus not separated.
506 *
507 * Locking: output_lock to protect column state and space left,
508 * echo_lock to protect the echo buffer
509 */
510
511static void process_echoes(struct tty_struct *tty)
512{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200513 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000514 int space, nr;
515 unsigned char c;
516 unsigned char *cp, *buf_end;
517
518 if (!tty->echo_cnt)
519 return;
520
521 mutex_lock(&tty->output_lock);
522 mutex_lock(&tty->echo_lock);
523
524 space = tty_write_room(tty);
525
526 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
527 cp = tty->echo_buf + tty->echo_pos;
528 nr = tty->echo_cnt;
529 while (nr > 0) {
530 c = *cp;
531 if (c == ECHO_OP_START) {
532 unsigned char op;
533 unsigned char *opp;
534 int no_space_left = 0;
535
536 /*
537 * If the buffer byte is the start of a multi-byte
538 * operation, get the next byte, which is either the
539 * op code or a control character value.
540 */
541 opp = cp + 1;
542 if (opp == buf_end)
543 opp -= N_TTY_BUF_SIZE;
544 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000545
Joe Petersona88a69c2009-01-02 13:40:53 +0000546 switch (op) {
547 unsigned int num_chars, num_bs;
548
549 case ECHO_OP_ERASE_TAB:
550 if (++opp == buf_end)
551 opp -= N_TTY_BUF_SIZE;
552 num_chars = *opp;
553
554 /*
555 * Determine how many columns to go back
556 * in order to erase the tab.
557 * This depends on the number of columns
558 * used by other characters within the tab
559 * area. If this (modulo 8) count is from
560 * the start of input rather than from a
561 * previous tab, we offset by canon column.
562 * Otherwise, tab spacing is normal.
563 */
564 if (!(num_chars & 0x80))
565 num_chars += tty->canon_column;
566 num_bs = 8 - (num_chars & 7);
567
568 if (num_bs > space) {
569 no_space_left = 1;
570 break;
571 }
572 space -= num_bs;
573 while (num_bs--) {
574 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200575 if (ldata->column > 0)
576 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000577 }
578 cp += 3;
579 nr -= 3;
580 break;
581
582 case ECHO_OP_SET_CANON_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200583 tty->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000584 cp += 2;
585 nr -= 2;
586 break;
587
588 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200589 if (ldata->column > 0)
590 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000591 cp += 2;
592 nr -= 2;
593 break;
594
595 case ECHO_OP_START:
596 /* This is an escaped echo op start code */
597 if (!space) {
598 no_space_left = 1;
599 break;
600 }
601 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200602 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000603 space--;
604 cp += 2;
605 nr -= 2;
606 break;
607
608 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600610 * If the op is not a special byte code,
611 * it is a ctrl char tagged to be echoed
612 * as "^X" (where X is the letter
613 * representing the control char).
614 * Note that we must ensure there is
615 * enough space for the whole ctrl pair.
616 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000617 */
Joe Peterson62b26352009-09-09 15:03:47 -0600618 if (space < 2) {
619 no_space_left = 1;
620 break;
621 }
622 tty_put_char(tty, '^');
623 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200624 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600625 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000626 cp += 2;
627 nr -= 2;
628 }
629
630 if (no_space_left)
631 break;
632 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600633 if (O_OPOST(tty) &&
634 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
635 int retval = do_output_char(c, tty, space);
636 if (retval < 0)
637 break;
638 space -= retval;
639 } else {
640 if (!space)
641 break;
642 tty_put_char(tty, c);
643 space -= 1;
644 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000645 cp += 1;
646 nr -= 1;
647 }
648
649 /* When end of circular buffer reached, wrap around */
650 if (cp >= buf_end)
651 cp -= N_TTY_BUF_SIZE;
652 }
653
654 if (nr == 0) {
655 tty->echo_pos = 0;
656 tty->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200657 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000658 } else {
659 int num_processed = tty->echo_cnt - nr;
660 tty->echo_pos += num_processed;
661 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
662 tty->echo_cnt = nr;
663 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200664 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000665 }
666
667 mutex_unlock(&tty->echo_lock);
668 mutex_unlock(&tty->output_lock);
669
670 if (tty->ops->flush_chars)
671 tty->ops->flush_chars(tty);
672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000675 * add_echo_byte - add a byte to the echo buffer
676 * @c: unicode byte to echo
677 * @tty: terminal device
678 *
679 * Add a character or operation byte to the echo buffer.
680 *
681 * Should be called under the echo lock to protect the echo buffer.
682 */
683
684static void add_echo_byte(unsigned char c, struct tty_struct *tty)
685{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200686 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 int new_byte_pos;
688
689 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
690 /* Circular buffer is already at capacity */
691 new_byte_pos = tty->echo_pos;
692
693 /*
694 * Since the buffer start position needs to be advanced,
695 * be sure to step by a whole operation byte group.
696 */
Alan Cox300a6202009-01-02 13:41:04 +0000697 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000698 if (tty->echo_buf[(tty->echo_pos + 1) &
699 (N_TTY_BUF_SIZE - 1)] ==
700 ECHO_OP_ERASE_TAB) {
701 tty->echo_pos += 3;
702 tty->echo_cnt -= 2;
703 } else {
704 tty->echo_pos += 2;
705 tty->echo_cnt -= 1;
706 }
707 } else {
708 tty->echo_pos++;
709 }
710 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
711
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200712 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000713 } else {
714 new_byte_pos = tty->echo_pos + tty->echo_cnt;
715 new_byte_pos &= N_TTY_BUF_SIZE - 1;
716 tty->echo_cnt++;
717 }
718
719 tty->echo_buf[new_byte_pos] = c;
720}
721
722/**
723 * echo_move_back_col - add operation to move back a column
724 * @tty: terminal device
725 *
726 * Add an operation to the echo buffer to move back one column.
727 *
728 * Locking: echo_lock to protect the echo buffer
729 */
730
731static void echo_move_back_col(struct tty_struct *tty)
732{
733 mutex_lock(&tty->echo_lock);
734
735 add_echo_byte(ECHO_OP_START, tty);
736 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
737
738 mutex_unlock(&tty->echo_lock);
739}
740
741/**
742 * echo_set_canon_col - add operation to set the canon column
743 * @tty: terminal device
744 *
745 * Add an operation to the echo buffer to set the canon column
746 * to the current column.
747 *
748 * Locking: echo_lock to protect the echo buffer
749 */
750
751static void echo_set_canon_col(struct tty_struct *tty)
752{
753 mutex_lock(&tty->echo_lock);
754
755 add_echo_byte(ECHO_OP_START, tty);
756 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
757
758 mutex_unlock(&tty->echo_lock);
759}
760
761/**
762 * echo_erase_tab - add operation to erase a tab
763 * @num_chars: number of character columns already used
764 * @after_tab: true if num_chars starts after a previous tab
765 * @tty: terminal device
766 *
767 * Add an operation to the echo buffer to erase a tab.
768 *
769 * Called by the eraser function, which knows how many character
770 * columns have been used since either a previous tab or the start
771 * of input. This information will be used later, along with
772 * canon column (if applicable), to go back the correct number
773 * of columns.
774 *
775 * Locking: echo_lock to protect the echo buffer
776 */
777
778static void echo_erase_tab(unsigned int num_chars, int after_tab,
779 struct tty_struct *tty)
780{
781 mutex_lock(&tty->echo_lock);
782
783 add_echo_byte(ECHO_OP_START, tty);
784 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
785
786 /* We only need to know this modulo 8 (tab spacing) */
787 num_chars &= 7;
788
789 /* Set the high bit as a flag if num_chars is after a previous tab */
790 if (after_tab)
791 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000792
Joe Petersona88a69c2009-01-02 13:40:53 +0000793 add_echo_byte(num_chars, tty);
794
795 mutex_unlock(&tty->echo_lock);
796}
797
798/**
799 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * @c: unicode byte to echo
801 * @tty: terminal device
802 *
Alan Cox4edf1822008-02-08 04:18:44 -0800803 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100805 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000806 * This variant does not treat control characters specially.
807 *
808 * Locking: echo_lock to protect the echo buffer
809 */
810
811static void echo_char_raw(unsigned char c, struct tty_struct *tty)
812{
813 mutex_lock(&tty->echo_lock);
814
815 if (c == ECHO_OP_START) {
816 add_echo_byte(ECHO_OP_START, tty);
817 add_echo_byte(ECHO_OP_START, tty);
818 } else {
819 add_echo_byte(c, tty);
820 }
821
822 mutex_unlock(&tty->echo_lock);
823}
824
825/**
826 * echo_char - echo a character
827 * @c: unicode byte to echo
828 * @tty: terminal device
829 *
830 * Echo user input back onto the screen. This must be called only when
831 * L_ECHO(tty) is true. Called from the driver receive_buf path.
832 *
Joe Peterson62b26352009-09-09 15:03:47 -0600833 * This variant tags control characters to be echoed as "^X"
834 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000835 *
836 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
838
839static void echo_char(unsigned char c, struct tty_struct *tty)
840{
Joe Petersona88a69c2009-01-02 13:40:53 +0000841 mutex_lock(&tty->echo_lock);
842
843 if (c == ECHO_OP_START) {
844 add_echo_byte(ECHO_OP_START, tty);
845 add_echo_byte(ECHO_OP_START, tty);
846 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600847 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Joe Petersona88a69c2009-01-02 13:40:53 +0000848 add_echo_byte(ECHO_OP_START, tty);
849 add_echo_byte(c, tty);
850 }
851
852 mutex_unlock(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Alan Cox17b82062008-10-13 10:45:06 +0100855/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000856 * finish_erasing - complete erase
Alan Cox17b82062008-10-13 10:45:06 +0100857 * @tty: tty doing the erase
Alan Cox17b82062008-10-13 10:45:06 +0100858 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static inline void finish_erasing(struct tty_struct *tty)
861{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200862 struct n_tty_data *ldata = tty->disc_data;
863 if (ldata->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000864 echo_char_raw('/', tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200865 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867}
868
869/**
870 * eraser - handle erase function
871 * @c: character input
872 * @tty: terminal device
873 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200874 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * present in the stream from the driver layer. Handles the complexities
876 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100877 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000878 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Alan Cox4edf1822008-02-08 04:18:44 -0800880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static void eraser(unsigned char c, struct tty_struct *tty)
882{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200883 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 enum { ERASE, WERASE, KILL } kill_type;
885 int head, seen_alnums, cnt;
886 unsigned long flags;
887
Alan Cox17b82062008-10-13 10:45:06 +0100888 /* FIXME: locking needed ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (tty->read_head == tty->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000890 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return;
892 }
893 if (c == ERASE_CHAR(tty))
894 kill_type = ERASE;
895 else if (c == WERASE_CHAR(tty))
896 kill_type = WERASE;
897 else {
898 if (!L_ECHO(tty)) {
899 spin_lock_irqsave(&tty->read_lock, flags);
900 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
901 (N_TTY_BUF_SIZE - 1));
902 tty->read_head = tty->canon_head;
903 spin_unlock_irqrestore(&tty->read_lock, flags);
904 return;
905 }
906 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
907 spin_lock_irqsave(&tty->read_lock, flags);
908 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
909 (N_TTY_BUF_SIZE - 1));
910 tty->read_head = tty->canon_head;
911 spin_unlock_irqrestore(&tty->read_lock, flags);
912 finish_erasing(tty);
913 echo_char(KILL_CHAR(tty), tty);
914 /* Add a newline if ECHOK is on and ECHOKE is off. */
915 if (L_ECHOK(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000916 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return;
918 }
919 kill_type = KILL;
920 }
921
922 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100923 /* FIXME: Locking ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 while (tty->read_head != tty->canon_head) {
925 head = tty->read_head;
926
927 /* erase a single possibly multibyte character */
928 do {
929 head = (head - 1) & (N_TTY_BUF_SIZE-1);
930 c = tty->read_buf[head];
931 } while (is_continuation(c, tty) && head != tty->canon_head);
932
933 /* do not partially erase */
934 if (is_continuation(c, tty))
935 break;
936
937 if (kill_type == WERASE) {
938 /* Equivalent to BSD's ALTWERASE. */
939 if (isalnum(c) || c == '_')
940 seen_alnums++;
941 else if (seen_alnums)
942 break;
943 }
944 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
945 spin_lock_irqsave(&tty->read_lock, flags);
946 tty->read_head = head;
947 tty->read_cnt -= cnt;
948 spin_unlock_irqrestore(&tty->read_lock, flags);
949 if (L_ECHO(tty)) {
950 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200951 if (!ldata->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000952 echo_char_raw('\\', tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200953 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 /* if cnt > 1, output a multi-byte character */
956 echo_char(c, tty);
957 while (--cnt > 0) {
958 head = (head+1) & (N_TTY_BUF_SIZE-1);
Joe Petersona88a69c2009-01-02 13:40:53 +0000959 echo_char_raw(tty->read_buf[head], tty);
960 echo_move_back_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
963 echo_char(ERASE_CHAR(tty), tty);
964 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000965 unsigned int num_chars = 0;
966 int after_tab = 0;
967 unsigned long tail = tty->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Joe Petersona88a69c2009-01-02 13:40:53 +0000969 /*
970 * Count the columns used for characters
971 * since the start of input or after a
972 * previous tab.
973 * This info is used to go back the correct
974 * number of columns.
975 */
976 while (tail != tty->canon_head) {
977 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 c = tty->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000979 if (c == '\t') {
980 after_tab = 1;
981 break;
Alan Cox300a6202009-01-02 13:41:04 +0000982 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000984 num_chars += 2;
985 } else if (!is_continuation(c, tty)) {
986 num_chars++;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000989 echo_erase_tab(num_chars, after_tab, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 } else {
991 if (iscntrl(c) && L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000992 echo_char_raw('\b', tty);
993 echo_char_raw(' ', tty);
994 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000997 echo_char_raw('\b', tty);
998 echo_char_raw(' ', tty);
999 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 }
1002 }
1003 if (kill_type == ERASE)
1004 break;
1005 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001006 if (tty->read_head == tty->canon_head && L_ECHO(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 finish_erasing(tty);
1008}
1009
1010/**
1011 * isig - handle the ISIG optio
1012 * @sig: signal
1013 * @tty: terminal
1014 * @flush: force flush
1015 *
1016 * Called when a signal is being sent due to terminal input. This
1017 * may caus terminal flushing to take place according to the termios
1018 * settings and character used. Called from the driver receive_buf
1019 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001020 *
1021 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Alan Cox4edf1822008-02-08 04:18:44 -08001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static inline void isig(int sig, struct tty_struct *tty, int flush)
1025{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001026 if (tty->pgrp)
1027 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (flush || !L_NOFLSH(tty)) {
1029 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001030 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032}
1033
1034/**
1035 * n_tty_receive_break - handle break
1036 * @tty: terminal
1037 *
1038 * An RS232 break event has been hit in the incoming bitstream. This
1039 * can cause a variety of events depending upon the termios settings.
1040 *
1041 * Called from the receive_buf path so single threaded.
1042 */
Alan Cox4edf1822008-02-08 04:18:44 -08001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static inline void n_tty_receive_break(struct tty_struct *tty)
1045{
1046 if (I_IGNBRK(tty))
1047 return;
1048 if (I_BRKINT(tty)) {
1049 isig(SIGINT, tty, 1);
1050 return;
1051 }
1052 if (I_PARMRK(tty)) {
1053 put_tty_queue('\377', tty);
1054 put_tty_queue('\0', tty);
1055 }
1056 put_tty_queue('\0', tty);
1057 wake_up_interruptible(&tty->read_wait);
1058}
1059
1060/**
1061 * n_tty_receive_overrun - handle overrun reporting
1062 * @tty: terminal
1063 *
1064 * Data arrived faster than we could process it. While the tty
1065 * driver has flagged this the bits that were missed are gone
1066 * forever.
1067 *
1068 * Called from the receive_buf path so single threaded. Does not
1069 * need locking as num_overrun and overrun_time are function
1070 * private.
1071 */
Alan Cox4edf1822008-02-08 04:18:44 -08001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073static inline void n_tty_receive_overrun(struct tty_struct *tty)
1074{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001075 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 char buf[64];
1077
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001078 ldata->num_overrun++;
1079 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1080 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1082 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001083 ldata->num_overrun);
1084 ldata->overrun_time = jiffies;
1085 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087}
1088
1089/**
1090 * n_tty_receive_parity_error - error notifier
1091 * @tty: terminal device
1092 * @c: character
1093 *
1094 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001095 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
1097static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1098 unsigned char c)
1099{
Alan Cox4edf1822008-02-08 04:18:44 -08001100 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (I_PARMRK(tty)) {
1103 put_tty_queue('\377', tty);
1104 put_tty_queue('\0', tty);
1105 put_tty_queue(c, tty);
1106 } else if (I_INPCK(tty))
1107 put_tty_queue('\0', tty);
1108 else
1109 put_tty_queue(c, tty);
1110 wake_up_interruptible(&tty->read_wait);
1111}
1112
1113/**
1114 * n_tty_receive_char - perform processing
1115 * @tty: terminal device
1116 * @c: character
1117 *
1118 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001119 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * driver above.
1121 */
1122
1123static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1124{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001125 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001127 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001129 if (ldata->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 put_tty_queue(c, tty);
1131 return;
1132 }
Alan Cox4edf1822008-02-08 04:18:44 -08001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (I_ISTRIP(tty))
1135 c &= 0x7f;
1136 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001137 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
hyc@symas.com26df6d12010-06-22 10:14:49 -07001139 if (L_EXTPROC(tty)) {
1140 put_tty_queue(c, tty);
1141 return;
1142 }
1143
Joe Peterson54d2a372008-02-06 01:37:59 -08001144 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001145 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1146 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001147 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001148 process_echoes(tty);
1149 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (tty->closing) {
1152 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001153 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001155 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001156 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 stop_tty(tty);
1158 }
1159 return;
1160 }
1161
1162 /*
1163 * If the previous character was LNEXT, or we know that this
1164 * character is not one of the characters that we'll have to
1165 * handle specially, do shortcut processing to speed things
1166 * up.
1167 */
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001168 if (!test_bit(c, tty->process_char_map) || ldata->lnext) {
1169 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001170 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1171 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1172 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001173 if (L_ECHO(tty))
1174 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001175 return;
1176 }
1177 if (L_ECHO(tty)) {
1178 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* Record the column of first canon char. */
1180 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001181 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001183 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001185 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 put_tty_queue(c, tty);
1187 put_tty_queue(c, tty);
1188 return;
1189 }
Alan Cox4edf1822008-02-08 04:18:44 -08001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (I_IXON(tty)) {
1192 if (c == START_CHAR(tty)) {
1193 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001194 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return;
1196 }
1197 if (c == STOP_CHAR(tty)) {
1198 stop_tty(tty);
1199 return;
1200 }
1201 }
Joe Peterson575537b32008-04-30 00:53:30 -07001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (L_ISIG(tty)) {
1204 int signal;
1205 signal = SIGINT;
1206 if (c == INTR_CHAR(tty))
1207 goto send_signal;
1208 signal = SIGQUIT;
1209 if (c == QUIT_CHAR(tty))
1210 goto send_signal;
1211 signal = SIGTSTP;
1212 if (c == SUSP_CHAR(tty)) {
1213send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001214 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001215 * Note that we do not use isig() here because we want
1216 * the order to be:
1217 * 1) flush, 2) echo, 3) signal
1218 */
1219 if (!L_NOFLSH(tty)) {
1220 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001221 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001222 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001223 if (I_IXON(tty))
1224 start_tty(tty);
1225 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001226 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001227 process_echoes(tty);
1228 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001229 if (tty->pgrp)
1230 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233 }
Joe Peterson575537b32008-04-30 00:53:30 -07001234
1235 if (c == '\r') {
1236 if (I_IGNCR(tty))
1237 return;
1238 if (I_ICRNL(tty))
1239 c = '\n';
1240 } else if (c == '\n' && I_INLCR(tty))
1241 c = '\r';
1242
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001243 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1245 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1246 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001247 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return;
1249 }
1250 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001251 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (L_ECHO(tty)) {
1253 finish_erasing(tty);
1254 if (L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001255 echo_char_raw('^', tty);
1256 echo_char_raw('\b', tty);
1257 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 }
1260 return;
1261 }
1262 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1263 L_IEXTEN(tty)) {
1264 unsigned long tail = tty->canon_head;
1265
1266 finish_erasing(tty);
1267 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001268 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 while (tail != tty->read_head) {
1270 echo_char(tty->read_buf[tail], tty);
1271 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1272 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001273 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return;
1275 }
1276 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001277 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001278 if (L_ECHO(tty))
1279 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001280 return;
1281 }
1282 if (L_ECHO(tty) || L_ECHONL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001283 echo_char_raw('\n', tty);
1284 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 goto handle_newline;
1287 }
1288 if (c == EOF_CHAR(tty)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001289 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1290 return;
Alan Cox4edf1822008-02-08 04:18:44 -08001291 if (tty->canon_head != tty->read_head)
1292 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 c = __DISABLED_CHAR;
1294 goto handle_newline;
1295 }
1296 if ((c == EOL_CHAR(tty)) ||
1297 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001298 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1299 ? 1 : 0;
1300 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001301 if (L_ECHO(tty))
1302 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001303 return;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /*
1306 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1307 */
1308 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* Record the column of first canon char. */
1310 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001311 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001313 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 /*
1316 * XXX does PARMRK doubling happen for
1317 * EOL_CHAR and EOL2_CHAR?
1318 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 put_tty_queue(c, tty);
1321
Alan Cox4edf1822008-02-08 04:18:44 -08001322handle_newline:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 spin_lock_irqsave(&tty->read_lock, flags);
1324 set_bit(tty->read_head, tty->read_flags);
1325 put_tty_queue_nolock(c, tty);
1326 tty->canon_head = tty->read_head;
1327 tty->canon_data++;
1328 spin_unlock_irqrestore(&tty->read_lock, flags);
1329 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1330 if (waitqueue_active(&tty->read_wait))
1331 wake_up_interruptible(&tty->read_wait);
1332 return;
1333 }
1334 }
Alan Cox4edf1822008-02-08 04:18:44 -08001335
Joe Petersonacc71bb2009-01-02 13:43:32 +00001336 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1337 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1338 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001339 if (L_ECHO(tty))
1340 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001341 return;
1342 }
1343 if (L_ECHO(tty)) {
1344 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (c == '\n')
Joe Petersona88a69c2009-01-02 13:40:53 +00001346 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 else {
1348 /* Record the column of first canon char. */
1349 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001350 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 echo_char(c, tty);
1352 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001353 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Joe Petersonacc71bb2009-01-02 13:43:32 +00001356 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 put_tty_queue(c, tty);
1358
1359 put_tty_queue(c, tty);
Alan Cox4edf1822008-02-08 04:18:44 -08001360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363/**
1364 * n_tty_write_wakeup - asynchronous I/O notifier
1365 * @tty: tty device
1366 *
1367 * Required for the ptys, serial driver etc. since processes
1368 * that attach themselves to the master and rely on ASYNC
1369 * IO must be woken up
1370 */
1371
1372static void n_tty_write_wakeup(struct tty_struct *tty)
1373{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001374 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378/**
1379 * n_tty_receive_buf - data receive
1380 * @tty: terminal device
1381 * @cp: buffer
1382 * @fp: flag buffer
1383 * @count: characters
1384 *
1385 * Called by the terminal driver when a block of characters has
1386 * been received. This function must be called from soft contexts
1387 * not from interrupt context. The driver is responsible for making
1388 * calls one at a time and in order (or using flush_to_ldisc)
1389 */
Alan Cox4edf1822008-02-08 04:18:44 -08001390
Linus Torvalds55db4c62011-06-04 06:33:24 +09001391static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1392 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001394 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 const unsigned char *p;
1396 char *f, flags = TTY_NORMAL;
1397 int i;
1398 char buf[64];
1399 unsigned long cpuflags;
1400
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001401 if (ldata->real_raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 spin_lock_irqsave(&tty->read_lock, cpuflags);
1403 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1404 N_TTY_BUF_SIZE - tty->read_head);
1405 i = min(count, i);
1406 memcpy(tty->read_buf + tty->read_head, cp, i);
1407 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1408 tty->read_cnt += i;
1409 cp += i;
1410 count -= i;
1411
1412 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1413 N_TTY_BUF_SIZE - tty->read_head);
1414 i = min(count, i);
1415 memcpy(tty->read_buf + tty->read_head, cp, i);
1416 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1417 tty->read_cnt += i;
1418 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1419 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001420 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (f)
1422 flags = *f++;
1423 switch (flags) {
1424 case TTY_NORMAL:
1425 n_tty_receive_char(tty, *p);
1426 break;
1427 case TTY_BREAK:
1428 n_tty_receive_break(tty);
1429 break;
1430 case TTY_PARITY:
1431 case TTY_FRAME:
1432 n_tty_receive_parity_error(tty, *p);
1433 break;
1434 case TTY_OVERRUN:
1435 n_tty_receive_overrun(tty);
1436 break;
1437 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001438 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 tty_name(tty, buf), flags);
1440 break;
1441 }
1442 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001443 if (tty->ops->flush_chars)
1444 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Linus Torvalds55db4c62011-06-04 06:33:24 +09001447 n_tty_set_room(tty);
1448
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001449 if ((!ldata->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001450 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1452 if (waitqueue_active(&tty->read_wait))
1453 wake_up_interruptible(&tty->read_wait);
1454 }
1455
1456 /*
1457 * Check the remaining room for the input canonicalization
1458 * mode. We don't want to throttle the driver if we're in
1459 * canonical mode and don't have a newline yet!
1460 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001461 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001462 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001463
1464 /* FIXME: there is a tiny race here if the receive room check runs
1465 before the other work executes and empties the buffer (upping
1466 the receiving room and unthrottling. We then throttle and get
1467 stuck. This has been observed and traced down by Vincent Pillet/
1468 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471int is_ignored(int sig)
1472{
1473 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001474 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477/**
1478 * n_tty_set_termios - termios data changed
1479 * @tty: terminal
1480 * @old: previous data
1481 *
1482 * Called by the tty layer when the user changes termios flags so
1483 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001484 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * guaranteed that this function will not be re-entered or in progress
1486 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001487 *
1488 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
Alan Cox4edf1822008-02-08 04:18:44 -08001490
1491static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001493 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001494 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001495
1496 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001497 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001498 if (canon_change) {
1499 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1500 tty->canon_head = tty->read_tail;
1501 tty->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001502 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001503 }
1504
1505 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1506 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001507
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001508 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001510 ldata->raw = 1;
1511 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001512 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return;
1514 }
1515 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1516 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1517 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1518 I_PARMRK(tty)) {
1519 memset(tty->process_char_map, 0, 256/8);
1520
1521 if (I_IGNCR(tty) || I_ICRNL(tty))
1522 set_bit('\r', tty->process_char_map);
1523 if (I_INLCR(tty))
1524 set_bit('\n', tty->process_char_map);
1525
1526 if (L_ICANON(tty)) {
1527 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1528 set_bit(KILL_CHAR(tty), tty->process_char_map);
1529 set_bit(EOF_CHAR(tty), tty->process_char_map);
1530 set_bit('\n', tty->process_char_map);
1531 set_bit(EOL_CHAR(tty), tty->process_char_map);
1532 if (L_IEXTEN(tty)) {
1533 set_bit(WERASE_CHAR(tty),
1534 tty->process_char_map);
1535 set_bit(LNEXT_CHAR(tty),
1536 tty->process_char_map);
1537 set_bit(EOL2_CHAR(tty),
1538 tty->process_char_map);
1539 if (L_ECHO(tty))
1540 set_bit(REPRINT_CHAR(tty),
1541 tty->process_char_map);
1542 }
1543 }
1544 if (I_IXON(tty)) {
1545 set_bit(START_CHAR(tty), tty->process_char_map);
1546 set_bit(STOP_CHAR(tty), tty->process_char_map);
1547 }
1548 if (L_ISIG(tty)) {
1549 set_bit(INTR_CHAR(tty), tty->process_char_map);
1550 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1551 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1552 }
1553 clear_bit(__DISABLED_CHAR, tty->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001554 ldata->raw = 0;
1555 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001557 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1559 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1560 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001561 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001563 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001565 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001566 /* The termios change make the tty ready for I/O */
1567 wake_up_interruptible(&tty->write_wait);
1568 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/**
1572 * n_tty_close - close the ldisc for this tty
1573 * @tty: device
1574 *
Alan Cox4edf1822008-02-08 04:18:44 -08001575 * Called from the terminal layer when this line discipline is
1576 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * discipline change. The function will not be called while other
1578 * ldisc methods are in progress.
1579 */
Alan Cox4edf1822008-02-08 04:18:44 -08001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static void n_tty_close(struct tty_struct *tty)
1582{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001583 struct n_tty_data *ldata = tty->disc_data;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 n_tty_flush_buffer(tty);
Jiri Slabyb91939f2012-10-18 22:26:35 +02001586 kfree(tty->read_buf);
1587 kfree(tty->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001588 kfree(ldata);
Jiri Slabyb91939f2012-10-18 22:26:35 +02001589 tty->read_buf = NULL;
1590 tty->echo_buf = NULL;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001591 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/**
1595 * n_tty_open - open an ldisc
1596 * @tty: terminal to open
1597 *
Alan Cox4edf1822008-02-08 04:18:44 -08001598 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * terminal device. Can sleep. Called serialized so that no
1600 * other events will occur in parallel. No further open will occur
1601 * until a close.
1602 */
1603
1604static int n_tty_open(struct tty_struct *tty)
1605{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001606 struct n_tty_data *ldata;
1607
1608 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1609 if (!ldata)
1610 goto err;
1611
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001612 ldata->overrun_time = jiffies;
1613
Joe Petersona88a69c2009-01-02 13:40:53 +00001614 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyb91939f2012-10-18 22:26:35 +02001615 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1616 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1617 if (!tty->read_buf || !tty->echo_buf)
1618 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001619
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001620 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001622 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001623 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 n_tty_set_termios(tty, NULL);
1625 tty->minimum_to_wake = 1;
1626 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001629err_free_bufs:
1630 kfree(tty->read_buf);
1631 kfree(tty->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001632 kfree(ldata);
1633err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001634 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637static inline int input_available_p(struct tty_struct *tty, int amt)
1638{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001639 struct n_tty_data *ldata = tty->disc_data;
1640
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001641 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001642 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (tty->canon_data)
1644 return 1;
1645 } else if (tty->read_cnt >= (amt ? amt : 1))
1646 return 1;
1647
1648 return 0;
1649}
1650
1651/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001652 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * @tty: terminal device
1654 * @b: user data
1655 * @nr: size of data
1656 *
Alan Cox11a96d12008-10-13 10:46:24 +01001657 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * ICANON is off; it copies characters straight from the tty queue to
1659 * user space directly. It can be profitably called twice; once to
1660 * drain the space from the tail pointer to the (physical) end of the
1661 * buffer, and once to drain the space from the (physical) beginning of
1662 * the buffer to head pointer.
1663 *
Paul Fulghum817d6d32006-06-28 04:26:47 -07001664 * Called under the tty->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 *
1666 */
Alan Cox4edf1822008-02-08 04:18:44 -08001667
Alan Cox33f0f882006-01-09 20:54:13 -08001668static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 unsigned char __user **b,
1670 size_t *nr)
1671
1672{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001673 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int retval;
1675 size_t n;
1676 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001677 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 retval = 0;
1680 spin_lock_irqsave(&tty->read_lock, flags);
1681 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1682 n = min(*nr, n);
1683 spin_unlock_irqrestore(&tty->read_lock, flags);
1684 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1686 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001687 is_eof = n == 1 &&
1688 tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
Jiri Slaby6c633f22012-10-18 22:26:37 +02001689 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001690 ldata->icanon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 spin_lock_irqsave(&tty->read_lock, flags);
1692 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1693 tty->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001694 /* Turn single EOF into zero-length read */
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001695 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !tty->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001696 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 spin_unlock_irqrestore(&tty->read_lock, flags);
1698 *b += n;
1699 *nr -= n;
1700 }
1701 return retval;
1702}
1703
Al Virocc4191d2008-03-29 03:08:48 +00001704extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001705 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707/**
1708 * job_control - check job control
1709 * @tty: tty
1710 * @file: file handle
1711 *
1712 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001713 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001715 *
1716 * FIXME:
1717 * Locking: None - redirected write test is safe, testing
1718 * current->signal should possibly lock current->sighand
1719 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
Alan Cox4edf1822008-02-08 04:18:44 -08001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static int job_control(struct tty_struct *tty, struct file *file)
1723{
1724 /* Job control check -- must be done at start and after
1725 every sleep (POSIX.1 7.1.1.4). */
1726 /* NOTE: not yet done after every sleep pending a thorough
1727 check of the logic of this change. -- jlc */
1728 /* don't stop on /dev/console */
1729 if (file->f_op->write != redirected_tty_write &&
1730 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001731 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001732 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001733 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001735 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001737 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001738 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -ERESTARTSYS;
1740 }
1741 }
1742 return 0;
1743}
Alan Cox4edf1822008-02-08 04:18:44 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746/**
Alan Cox11a96d12008-10-13 10:46:24 +01001747 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * @tty: tty device
1749 * @file: file object
1750 * @buf: userspace buffer pointer
1751 * @nr: size of I/O
1752 *
1753 * Perform reads for the line discipline. We are guaranteed that the
1754 * line discipline will not be closed under us but we may get multiple
1755 * parallel readers and must handle this ourselves. We may also get
1756 * a hangup. Always called in user context, may sleep.
1757 *
1758 * This code must be sure never to sleep through a hangup.
1759 */
Alan Cox4edf1822008-02-08 04:18:44 -08001760
Alan Cox11a96d12008-10-13 10:46:24 +01001761static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned char __user *buf, size_t nr)
1763{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001764 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unsigned char __user *b = buf;
1766 DECLARE_WAITQUEUE(wait, current);
1767 int c;
1768 int minimum, time;
1769 ssize_t retval = 0;
1770 ssize_t size;
1771 long timeout;
1772 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001773 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001777 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 minimum = time = 0;
1781 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001782 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 time = (HZ / 10) * TIME_CHAR(tty);
1784 minimum = MIN_CHAR(tty);
1785 if (minimum) {
1786 if (time)
1787 tty->minimum_to_wake = 1;
1788 else if (!waitqueue_active(&tty->read_wait) ||
1789 (tty->minimum_to_wake > minimum))
1790 tty->minimum_to_wake = minimum;
1791 } else {
1792 timeout = 0;
1793 if (time) {
1794 timeout = time;
1795 time = 0;
1796 }
1797 tty->minimum_to_wake = minimum = 1;
1798 }
1799 }
1800
1801 /*
1802 * Internal serialization of reads.
1803 */
1804 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001805 if (!mutex_trylock(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001807 } else {
Ingo Molnar70522e12006-03-23 03:00:31 -08001808 if (mutex_lock_interruptible(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -ERESTARTSYS;
1810 }
Alan Cox04f378b2008-04-30 00:53:29 -07001811 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 while (nr) {
1815 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001816 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 unsigned char cs;
1818 if (b != buf)
1819 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001820 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 cs = tty->link->ctrl_status;
1822 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001823 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001824 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 retval = -EFAULT;
1826 b--;
1827 break;
1828 }
1829 nr--;
1830 break;
1831 }
1832 /* This statement must be first before checking for input
1833 so that any interrupt will set the state back to
1834 TASK_RUNNING. */
1835 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1838 ((minimum - (b - buf)) >= 1))
1839 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!input_available_p(tty, 0)) {
1842 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1843 retval = -EIO;
1844 break;
1845 }
1846 if (tty_hung_up_p(file))
1847 break;
1848 if (!timeout)
1849 break;
1850 if (file->f_flags & O_NONBLOCK) {
1851 retval = -EAGAIN;
1852 break;
1853 }
1854 if (signal_pending(current)) {
1855 retval = -ERESTARTSYS;
1856 break;
1857 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001858 /* FIXME: does n_tty_set_room need locking ? */
1859 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 continue;
1862 }
1863 __set_current_state(TASK_RUNNING);
1864
1865 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001866 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001867 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 retval = -EFAULT;
1869 b--;
1870 break;
1871 }
1872 nr--;
1873 }
1874
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001875 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* N.B. avoid overrun if nr == 0 */
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001877 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 while (nr && tty->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001879 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 eol = test_and_clear_bit(tty->read_tail,
1882 tty->read_flags);
1883 c = tty->read_buf[tty->read_tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 tty->read_tail = ((tty->read_tail+1) &
1885 (N_TTY_BUF_SIZE-1));
1886 tty->read_cnt--;
1887 if (eol) {
1888 /* this test should be redundant:
1889 * we shouldn't be reading data if
1890 * canon_data is 0
1891 */
1892 if (--tty->canon_data < 0)
1893 tty->canon_data = 0;
1894 }
1895 spin_unlock_irqrestore(&tty->read_lock, flags);
1896
1897 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001898 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 retval = -EFAULT;
1900 b--;
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001901 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
1903 }
1904 nr--;
1905 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001906 if (eol) {
1907 tty_audit_push(tty);
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001908 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001910 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001911 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001913 spin_unlock_irqrestore(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (retval)
1915 break;
1916 } else {
1917 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001918 /* The copy function takes the read lock and handles
1919 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 uncopied = copy_from_read_buf(tty, &b, &nr);
1921 uncopied += copy_from_read_buf(tty, &b, &nr);
1922 if (uncopied) {
1923 retval = -EFAULT;
1924 break;
1925 }
1926 }
1927
1928 /* If there is enough space in the read buffer now, let the
1929 * low-level driver know. We use n_tty_chars_in_buffer() to
1930 * check the buffer, as it now knows about canonical mode.
1931 * Otherwise, if the driver is throttled and the line is
1932 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1933 * we won't get any more characters.
1934 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001935 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1936 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (b - buf >= minimum)
1941 break;
1942 if (time)
1943 timeout = time;
1944 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001945 mutex_unlock(&tty->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 remove_wait_queue(&tty->read_wait, &wait);
1947
1948 if (!waitqueue_active(&tty->read_wait))
1949 tty->minimum_to_wake = minimum;
1950
1951 __set_current_state(TASK_RUNNING);
1952 size = b - buf;
1953 if (size) {
1954 retval = size;
1955 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001956 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001958 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Linus Torvalds55db4c62011-06-04 06:33:24 +09001960 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return retval;
1962}
1963
1964/**
Alan Cox11a96d12008-10-13 10:46:24 +01001965 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * @tty: tty device
1967 * @file: file object
1968 * @buf: userspace buffer pointer
1969 * @nr: size of I/O
1970 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001971 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001973 * and other such events. Since the receive code will echo characters,
1974 * thus calling driver write methods, the output_lock is used in
1975 * the output processing functions called here as well as in the
1976 * echo processing function to protect the column state and space
1977 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 *
1979 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001980 *
1981 * Locking: output_lock to protect column state and space left
1982 * (note that the process_output*() functions take this
1983 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
Alan Cox4edf1822008-02-08 04:18:44 -08001985
Alan Cox11a96d12008-10-13 10:46:24 +01001986static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00001987 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 const unsigned char *b = buf;
1990 DECLARE_WAITQUEUE(wait, current);
1991 int c;
1992 ssize_t retval = 0;
1993
1994 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1995 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1996 retval = tty_check_change(tty);
1997 if (retval)
1998 return retval;
1999 }
2000
Joe Petersona88a69c2009-01-02 13:40:53 +00002001 /* Write out any echoed characters that are still pending */
2002 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 add_wait_queue(&tty->write_wait, &wait);
2005 while (1) {
2006 set_current_state(TASK_INTERRUPTIBLE);
2007 if (signal_pending(current)) {
2008 retval = -ERESTARTSYS;
2009 break;
2010 }
2011 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2012 retval = -EIO;
2013 break;
2014 }
2015 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2016 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002017 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (num < 0) {
2019 if (num == -EAGAIN)
2020 break;
2021 retval = num;
2022 goto break_out;
2023 }
2024 b += num;
2025 nr -= num;
2026 if (nr == 0)
2027 break;
2028 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002029 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031 b++; nr--;
2032 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002033 if (tty->ops->flush_chars)
2034 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002036 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002037 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002038 if (c < 0) {
2039 retval = c;
2040 goto break_out;
2041 }
2042 if (!c)
2043 break;
2044 b += c;
2045 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 if (!nr)
2049 break;
2050 if (file->f_flags & O_NONBLOCK) {
2051 retval = -EAGAIN;
2052 break;
2053 }
2054 schedule();
2055 }
2056break_out:
2057 __set_current_state(TASK_RUNNING);
2058 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002059 if (b - buf != nr && tty->fasync)
2060 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return (b - buf) ? b - buf : retval;
2062}
2063
2064/**
Alan Cox11a96d12008-10-13 10:46:24 +01002065 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 * @tty: terminal device
2067 * @file: file accessing it
2068 * @wait: poll table
2069 *
2070 * Called when the line discipline is asked to poll() for data or
2071 * for special events. This code is not serialized with respect to
2072 * other events save open/close.
2073 *
2074 * This code must be sure never to sleep through a hangup.
2075 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Alan Cox4edf1822008-02-08 04:18:44 -08002077
Alan Cox11a96d12008-10-13 10:46:24 +01002078static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002079 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 unsigned int mask = 0;
2082
2083 poll_wait(file, &tty->read_wait, wait);
2084 poll_wait(file, &tty->write_wait, wait);
2085 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2086 mask |= POLLIN | POLLRDNORM;
2087 if (tty->packet && tty->link->ctrl_status)
2088 mask |= POLLPRI | POLLIN | POLLRDNORM;
2089 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2090 mask |= POLLHUP;
2091 if (tty_hung_up_p(file))
2092 mask |= POLLHUP;
2093 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2094 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2095 tty->minimum_to_wake = MIN_CHAR(tty);
2096 else
2097 tty->minimum_to_wake = 1;
2098 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002099 if (tty->ops->write && !tty_is_writelocked(tty) &&
2100 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2101 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 mask |= POLLOUT | POLLWRNORM;
2103 return mask;
2104}
2105
Alan Cox47afa7a2008-10-13 10:44:17 +01002106static unsigned long inq_canon(struct tty_struct *tty)
2107{
2108 int nr, head, tail;
2109
Alan Cox17b82062008-10-13 10:45:06 +01002110 if (!tty->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002111 return 0;
2112 head = tty->canon_head;
2113 tail = tty->read_tail;
2114 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2115 /* Skip EOF-chars.. */
2116 while (head != tail) {
2117 if (test_bit(tail, tty->read_flags) &&
2118 tty->read_buf[tail] == __DISABLED_CHAR)
2119 nr--;
2120 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2121 }
2122 return nr;
2123}
2124
2125static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2126 unsigned int cmd, unsigned long arg)
2127{
2128 int retval;
2129
2130 switch (cmd) {
2131 case TIOCOUTQ:
2132 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2133 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002134 /* FIXME: Locking */
Alan Cox47afa7a2008-10-13 10:44:17 +01002135 retval = tty->read_cnt;
2136 if (L_ICANON(tty))
2137 retval = inq_canon(tty);
2138 return put_user(retval, (unsigned int __user *) arg);
2139 default:
2140 return n_tty_ioctl_helper(tty, file, cmd, arg);
2141 }
2142}
2143
Alan Coxa352def2008-07-16 21:53:12 +01002144struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002145 .magic = TTY_LDISC_MAGIC,
2146 .name = "n_tty",
2147 .open = n_tty_open,
2148 .close = n_tty_close,
2149 .flush_buffer = n_tty_flush_buffer,
2150 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002151 .read = n_tty_read,
2152 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002153 .ioctl = n_tty_ioctl,
2154 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002155 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002156 .receive_buf = n_tty_receive_buf,
2157 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002159
2160/**
2161 * n_tty_inherit_ops - inherit N_TTY methods
2162 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2163 *
2164 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2165 * methods.
2166 */
2167
2168void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2169{
2170 *ops = tty_ldisc_N_TTY;
2171 ops->owner = NULL;
2172 ops->refcount = ops->flags = 0;
2173}
2174EXPORT_SYMBOL_GPL(n_tty_inherit_ops);