blob: 61f1bc97ccd93ef4df095d5f55ef7a82b20ade11 [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>
George Spelvin593fb1ae42013-02-12 02:00:43 -050052#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010064#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Joe Petersona88a69c2009-01-02 13:40:53 +000066/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
Jiri Slaby70ece7a2012-10-18 22:26:38 +020077struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020078 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020084
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020087
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
96
97 int canon_data;
98 unsigned long canon_head;
99 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200100
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100104 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200105};
106
Miloslav Trmac522ed772007-07-15 23:40:56 -0700107static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
109{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200110 struct n_tty_data *ldata = tty->disc_data;
111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700113 return put_user(x, ptr);
114}
115
Linus Torvalds55db4c62011-06-04 06:33:24 +0900116/**
117 * n_tty_set__room - receive space
118 * @tty: terminal
119 *
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
123 * "instant".
124 */
125
126static void n_tty_set_room(struct tty_struct *tty)
127{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200128 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500129 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900130 int old_left;
131
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200132 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500133 if (I_PARMRK(tty)) {
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500138 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500140
Linus Torvalds55db4c62011-06-04 06:33:24 +0900141 /*
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
146 */
147 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200148 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900149 old_left = tty->receive_room;
150 tty->receive_room = left;
151
152 /* Did this open up the receive buffer? We may need to flip */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400155 "scheduling with invalid itty\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200156 schedule_work(&tty->port->buf.work);
157 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900158}
159
Jiri Slaby57c94122012-10-18 22:26:43 +0200160static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200162 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
163 ldata->read_buf[ldata->read_head] = c;
164 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
165 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167}
168
Alan Cox17b82062008-10-13 10:45:06 +0100169/**
170 * put_tty_queue - add character to tty
171 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200172 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100173 *
174 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes
177 */
178
Jiri Slaby57c94122012-10-18 22:26:43 +0200179static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned long flags;
182 /*
183 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK
185 */
Ivo Sieben98001212013-01-28 13:32:01 +0100186 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200187 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100188 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * reset_buffer_flags - reset buffer state
193 * @tty: terminal to reset
194 *
Alan Cox4edf1822008-02-08 04:18:44 -0800195 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * and make sure the driver is unthrottled. Called
197 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100198 *
199 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static void reset_buffer_flags(struct tty_struct *tty)
203{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200204 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned long flags;
206
Ivo Sieben98001212013-01-28 13:32:01 +0100207 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200208 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100209 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000210
Jiri Slabybddc7152012-10-18 22:26:42 +0200211 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200212 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200213 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000214
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200215 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200216 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900217 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/**
221 * n_tty_flush_buffer - clean input queue
222 * @tty: terminal device
223 *
224 * Flush the input buffer. Called when the line discipline is
225 * being closed, when the tty layer wants the buffer flushed (eg
226 * at hangup) or when the N_TTY line discipline internally has to
227 * clean the pending queue (for example some signals).
228 *
Alan Cox17b82062008-10-13 10:45:06 +0100229 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Alan Cox4edf1822008-02-08 04:18:44 -0800231
232static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Alan Cox04f378b2008-04-30 00:53:29 -0700234 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* clear everything and unthrottle the driver */
236 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!tty->link)
239 return;
240
Alan Cox04f378b2008-04-30 00:53:29 -0700241 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (tty->link->packet) {
243 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
244 wake_up_interruptible(&tty->link->read_wait);
245 }
Alan Cox04f378b2008-04-30 00:53:29 -0700246 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * n_tty_chars_in_buffer - report available bytes
251 * @tty: tty device
252 *
253 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800254 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100255 *
256 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Alan Cox4edf1822008-02-08 04:18:44 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
260{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200261 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long flags;
263 ssize_t n = 0;
264
Ivo Sieben98001212013-01-28 13:32:01 +0100265 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200266 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200267 n = ldata->read_cnt;
268 } else if (ldata->canon_data) {
269 n = (ldata->canon_head > ldata->read_tail) ?
270 ldata->canon_head - ldata->read_tail :
271 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Ivo Sieben98001212013-01-28 13:32:01 +0100273 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return n;
275}
276
277/**
278 * is_utf8_continuation - utf8 multibyte check
279 * @c: byte to check
280 *
281 * Returns true if the utf8 character 'c' is a multibyte continuation
282 * character. We use this to correctly compute the on screen size
283 * of the character when printing
284 */
Alan Cox4edf1822008-02-08 04:18:44 -0800285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static inline int is_utf8_continuation(unsigned char c)
287{
288 return (c & 0xc0) == 0x80;
289}
290
291/**
292 * is_continuation - multibyte check
293 * @c: byte to check
294 *
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character and the terminal is in unicode mode.
297 */
Alan Cox4edf1822008-02-08 04:18:44 -0800298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static inline int is_continuation(unsigned char c, struct tty_struct *tty)
300{
301 return I_IUTF8(tty) && is_utf8_continuation(c);
302}
303
304/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000305 * do_output_char - output one character
306 * @c: character (or partial unicode symbol)
307 * @tty: terminal device
308 * @space: space available in tty driver write buffer
309 *
310 * This is a helper function that handles one output character
311 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600312 * doing OPOST processing and putting the results in the
313 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000314 *
315 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
316 * and NLDLY. They simply aren't relevant in the world today.
317 * If you ever need them, add them here.
318 *
319 * Returns the number of bytes of buffer space used or -1 if
320 * no space left.
321 *
322 * Locking: should be called under the output_lock to protect
323 * the column state and space left in the buffer
324 */
325
326static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
327{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200328 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000329 int spaces;
330
331 if (!space)
332 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000333
Joe Petersona88a69c2009-01-02 13:40:53 +0000334 switch (c) {
335 case '\n':
336 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200337 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000338 if (O_ONLCR(tty)) {
339 if (space < 2)
340 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200341 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700342 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000343 return 2;
344 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200345 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000346 break;
347 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200348 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000349 return 0;
350 if (O_OCRNL(tty)) {
351 c = '\n';
352 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200353 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000354 break;
355 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200356 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000357 break;
358 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200359 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 if (O_TABDLY(tty) == XTABS) {
361 if (space < spaces)
362 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200363 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000364 tty->ops->write(tty, " ", spaces);
365 return spaces;
366 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200367 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000368 break;
369 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200370 if (ldata->column > 0)
371 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000372 break;
373 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000374 if (!iscntrl(c)) {
375 if (O_OLCUC(tty))
376 c = toupper(c);
377 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200378 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000379 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000380 break;
381 }
382
383 tty_put_char(tty, c);
384 return 1;
385}
386
387/**
388 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * @c: character (or partial unicode symbol)
390 * @tty: terminal device
391 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600392 * Output one character with OPOST processing.
393 * Returns -1 when the output device is full and the character
394 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000396 * Locking: output_lock to protect column state and space left
397 * (also, this is called from n_tty_write under the
398 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Alan Cox4edf1822008-02-08 04:18:44 -0800400
Joe Petersona88a69c2009-01-02 13:40:53 +0000401static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Jiri Slabybddc7152012-10-18 22:26:42 +0200403 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000404 int space, retval;
405
Jiri Slabybddc7152012-10-18 22:26:42 +0200406 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alan Coxf34d7a52008-04-30 00:54:13 -0700408 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000409 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jiri Slabybddc7152012-10-18 22:26:42 +0200411 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000412 if (retval < 0)
413 return -1;
414 else
415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000419 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600421 * @buf: character buffer
422 * @nr: number of bytes to output
423 *
424 * Output a block of characters with OPOST processing.
425 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
427 * This path is used to speed up block console writes, among other
428 * things when processing blocks of output data. It handles only
429 * the simple cases normally found and helps to generate blocks of
430 * symbols for the console driver and thus improve performance.
431 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000432 * Locking: output_lock to protect column state and space left
433 * (also, this is called from n_tty_write under the
434 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Alan Cox4edf1822008-02-08 04:18:44 -0800436
Joe Petersona88a69c2009-01-02 13:40:53 +0000437static ssize_t process_output_block(struct tty_struct *tty,
438 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200440 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 const unsigned char *cp;
444
Jiri Slabybddc7152012-10-18 22:26:42 +0200445 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000446
Alan Coxf34d7a52008-04-30 00:54:13 -0700447 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000448 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200449 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (nr > space)
453 nr = space;
454
455 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000456 unsigned char c = *cp;
457
458 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case '\n':
460 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200461 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (O_ONLCR(tty))
463 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200464 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200467 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto break_out;
469 if (O_OCRNL(tty))
470 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200471 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case '\t':
474 goto break_out;
475 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200476 if (ldata->column > 0)
477 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000480 if (!iscntrl(c)) {
481 if (O_OLCUC(tty))
482 goto break_out;
483 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200484 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 }
488 }
489break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700490 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000491
Jiri Slabybddc7152012-10-18 22:26:42 +0200492 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return i;
494}
495
Joe Petersona88a69c2009-01-02 13:40:53 +0000496/**
497 * process_echoes - write pending echo characters
498 * @tty: terminal device
499 *
500 * Write previously buffered echo (and other ldisc-generated)
501 * characters to the tty.
502 *
503 * Characters generated by the ldisc (including echoes) need to
504 * be buffered because the driver's write buffer can fill during
505 * heavy program output. Echoing straight to the driver will
506 * often fail under these conditions, causing lost characters and
507 * resulting mismatches of ldisc state information.
508 *
509 * Since the ldisc state must represent the characters actually sent
510 * to the driver at the time of the write, operations like certain
511 * changes in column state are also saved in the buffer and executed
512 * here.
513 *
514 * A circular fifo buffer is used so that the most recent characters
515 * are prioritized. Also, when control characters are echoed with a
516 * prefixed "^", the pair is treated atomically and thus not separated.
517 *
518 * Locking: output_lock to protect column state and space left,
519 * echo_lock to protect the echo buffer
520 */
521
522static void process_echoes(struct tty_struct *tty)
523{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200524 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000525 int space, nr;
526 unsigned char c;
527 unsigned char *cp, *buf_end;
528
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200529 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000530 return;
531
Jiri Slabybddc7152012-10-18 22:26:42 +0200532 mutex_lock(&ldata->output_lock);
533 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000534
535 space = tty_write_room(tty);
536
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200537 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
538 cp = ldata->echo_buf + ldata->echo_pos;
539 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000540 while (nr > 0) {
541 c = *cp;
542 if (c == ECHO_OP_START) {
543 unsigned char op;
544 unsigned char *opp;
545 int no_space_left = 0;
546
547 /*
548 * If the buffer byte is the start of a multi-byte
549 * operation, get the next byte, which is either the
550 * op code or a control character value.
551 */
552 opp = cp + 1;
553 if (opp == buf_end)
554 opp -= N_TTY_BUF_SIZE;
555 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000556
Joe Petersona88a69c2009-01-02 13:40:53 +0000557 switch (op) {
558 unsigned int num_chars, num_bs;
559
560 case ECHO_OP_ERASE_TAB:
561 if (++opp == buf_end)
562 opp -= N_TTY_BUF_SIZE;
563 num_chars = *opp;
564
565 /*
566 * Determine how many columns to go back
567 * in order to erase the tab.
568 * This depends on the number of columns
569 * used by other characters within the tab
570 * area. If this (modulo 8) count is from
571 * the start of input rather than from a
572 * previous tab, we offset by canon column.
573 * Otherwise, tab spacing is normal.
574 */
575 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200576 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000577 num_bs = 8 - (num_chars & 7);
578
579 if (num_bs > space) {
580 no_space_left = 1;
581 break;
582 }
583 space -= num_bs;
584 while (num_bs--) {
585 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200586 if (ldata->column > 0)
587 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000588 }
589 cp += 3;
590 nr -= 3;
591 break;
592
593 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200594 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000595 cp += 2;
596 nr -= 2;
597 break;
598
599 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200600 if (ldata->column > 0)
601 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000602 cp += 2;
603 nr -= 2;
604 break;
605
606 case ECHO_OP_START:
607 /* This is an escaped echo op start code */
608 if (!space) {
609 no_space_left = 1;
610 break;
611 }
612 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200613 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000614 space--;
615 cp += 2;
616 nr -= 2;
617 break;
618
619 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000620 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600621 * If the op is not a special byte code,
622 * it is a ctrl char tagged to be echoed
623 * as "^X" (where X is the letter
624 * representing the control char).
625 * Note that we must ensure there is
626 * enough space for the whole ctrl pair.
627 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 */
Joe Peterson62b26352009-09-09 15:03:47 -0600629 if (space < 2) {
630 no_space_left = 1;
631 break;
632 }
633 tty_put_char(tty, '^');
634 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200635 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600636 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000637 cp += 2;
638 nr -= 2;
639 }
640
641 if (no_space_left)
642 break;
643 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600644 if (O_OPOST(tty) &&
645 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
646 int retval = do_output_char(c, tty, space);
647 if (retval < 0)
648 break;
649 space -= retval;
650 } else {
651 if (!space)
652 break;
653 tty_put_char(tty, c);
654 space -= 1;
655 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000656 cp += 1;
657 nr -= 1;
658 }
659
660 /* When end of circular buffer reached, wrap around */
661 if (cp >= buf_end)
662 cp -= N_TTY_BUF_SIZE;
663 }
664
665 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200666 ldata->echo_pos = 0;
667 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200668 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000669 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200670 int num_processed = ldata->echo_cnt - nr;
671 ldata->echo_pos += num_processed;
672 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
673 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000674 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200675 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000676 }
677
Jiri Slabybddc7152012-10-18 22:26:42 +0200678 mutex_unlock(&ldata->echo_lock);
679 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000680
681 if (tty->ops->flush_chars)
682 tty->ops->flush_chars(tty);
683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000686 * add_echo_byte - add a byte to the echo buffer
687 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200688 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000689 *
690 * Add a character or operation byte to the echo buffer.
691 *
692 * Should be called under the echo lock to protect the echo buffer.
693 */
694
Jiri Slaby57c94122012-10-18 22:26:43 +0200695static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000696{
697 int new_byte_pos;
698
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200699 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200701 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000702
703 /*
704 * Since the buffer start position needs to be advanced,
705 * be sure to step by a whole operation byte group.
706 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200707 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
708 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000709 (N_TTY_BUF_SIZE - 1)] ==
710 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200711 ldata->echo_pos += 3;
712 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000713 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200714 ldata->echo_pos += 2;
715 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000716 }
717 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200718 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000719 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200720 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000721
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200722 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000723 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200724 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000725 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200726 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 }
728
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200729 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000730}
731
732/**
733 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200734 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000735 *
736 * Add an operation to the echo buffer to move back one column.
737 *
738 * Locking: echo_lock to protect the echo buffer
739 */
740
Jiri Slaby57c94122012-10-18 22:26:43 +0200741static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000742{
Jiri Slabybddc7152012-10-18 22:26:42 +0200743 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200744 add_echo_byte(ECHO_OP_START, ldata);
745 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200746 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000747}
748
749/**
750 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200751 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000752 *
753 * Add an operation to the echo buffer to set the canon column
754 * to the current column.
755 *
756 * Locking: echo_lock to protect the echo buffer
757 */
758
Jiri Slaby57c94122012-10-18 22:26:43 +0200759static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000760{
Jiri Slabybddc7152012-10-18 22:26:42 +0200761 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200762 add_echo_byte(ECHO_OP_START, ldata);
763 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200764 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000765}
766
767/**
768 * echo_erase_tab - add operation to erase a tab
769 * @num_chars: number of character columns already used
770 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200771 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000772 *
773 * Add an operation to the echo buffer to erase a tab.
774 *
775 * Called by the eraser function, which knows how many character
776 * columns have been used since either a previous tab or the start
777 * of input. This information will be used later, along with
778 * canon column (if applicable), to go back the correct number
779 * of columns.
780 *
781 * Locking: echo_lock to protect the echo buffer
782 */
783
784static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200785 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000786{
Jiri Slabybddc7152012-10-18 22:26:42 +0200787 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000788
Jiri Slaby57c94122012-10-18 22:26:43 +0200789 add_echo_byte(ECHO_OP_START, ldata);
790 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000791
792 /* We only need to know this modulo 8 (tab spacing) */
793 num_chars &= 7;
794
795 /* Set the high bit as a flag if num_chars is after a previous tab */
796 if (after_tab)
797 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000798
Jiri Slaby57c94122012-10-18 22:26:43 +0200799 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000800
Jiri Slabybddc7152012-10-18 22:26:42 +0200801 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000802}
803
804/**
805 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * @c: unicode byte to echo
807 * @tty: terminal device
808 *
Alan Cox4edf1822008-02-08 04:18:44 -0800809 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100811 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000812 * This variant does not treat control characters specially.
813 *
814 * Locking: echo_lock to protect the echo buffer
815 */
816
Jiri Slaby57c94122012-10-18 22:26:43 +0200817static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000818{
Jiri Slabybddc7152012-10-18 22:26:42 +0200819 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000820 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200821 add_echo_byte(ECHO_OP_START, ldata);
822 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000823 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200824 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000825 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200826 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000827}
828
829/**
830 * echo_char - echo a character
831 * @c: unicode byte to echo
832 * @tty: terminal device
833 *
834 * Echo user input back onto the screen. This must be called only when
835 * L_ECHO(tty) is true. Called from the driver receive_buf path.
836 *
Joe Peterson62b26352009-09-09 15:03:47 -0600837 * This variant tags control characters to be echoed as "^X"
838 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000839 *
840 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
842
843static void echo_char(unsigned char c, struct tty_struct *tty)
844{
Jiri Slabybddc7152012-10-18 22:26:42 +0200845 struct n_tty_data *ldata = tty->disc_data;
846
847 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000848
849 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200850 add_echo_byte(ECHO_OP_START, ldata);
851 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600853 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200854 add_echo_byte(ECHO_OP_START, ldata);
855 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000856 }
857
Jiri Slabybddc7152012-10-18 22:26:42 +0200858 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Alan Cox17b82062008-10-13 10:45:06 +0100861/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000862 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200863 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100864 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000865
Jiri Slaby57c94122012-10-18 22:26:43 +0200866static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200868 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200869 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200870 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872}
873
874/**
875 * eraser - handle erase function
876 * @c: character input
877 * @tty: terminal device
878 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200879 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * present in the stream from the driver layer. Handles the complexities
881 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100882 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000883 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Alan Cox4edf1822008-02-08 04:18:44 -0800885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static void eraser(unsigned char c, struct tty_struct *tty)
887{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200888 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 enum { ERASE, WERASE, KILL } kill_type;
890 int head, seen_alnums, cnt;
891 unsigned long flags;
892
Alan Cox17b82062008-10-13 10:45:06 +0100893 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200894 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000895 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return;
897 }
898 if (c == ERASE_CHAR(tty))
899 kill_type = ERASE;
900 else if (c == WERASE_CHAR(tty))
901 kill_type = WERASE;
902 else {
903 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100904 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200905 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200907 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100908 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
910 }
911 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100912 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200913 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200915 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100916 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200917 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 echo_char(KILL_CHAR(tty), tty);
919 /* Add a newline if ECHOK is on and ECHOKE is off. */
920 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200921 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return;
923 }
924 kill_type = KILL;
925 }
926
927 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100928 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200929 while (ldata->read_head != ldata->canon_head) {
930 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* erase a single possibly multibyte character */
933 do {
934 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200935 c = ldata->read_buf[head];
936 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* do not partially erase */
939 if (is_continuation(c, tty))
940 break;
941
942 if (kill_type == WERASE) {
943 /* Equivalent to BSD's ALTWERASE. */
944 if (isalnum(c) || c == '_')
945 seen_alnums++;
946 else if (seen_alnums)
947 break;
948 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200949 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100950 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200951 ldata->read_head = head;
952 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100953 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (L_ECHO(tty)) {
955 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200956 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200957 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200958 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 /* if cnt > 1, output a multi-byte character */
961 echo_char(c, tty);
962 while (--cnt > 0) {
963 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200964 echo_char_raw(ldata->read_buf[head],
965 ldata);
966 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
969 echo_char(ERASE_CHAR(tty), tty);
970 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000971 unsigned int num_chars = 0;
972 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200973 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Joe Petersona88a69c2009-01-02 13:40:53 +0000975 /*
976 * Count the columns used for characters
977 * since the start of input or after a
978 * previous tab.
979 * This info is used to go back the correct
980 * number of columns.
981 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200982 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000983 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200984 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000985 if (c == '\t') {
986 after_tab = 1;
987 break;
Alan Cox300a6202009-01-02 13:41:04 +0000988 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000990 num_chars += 2;
991 } else if (!is_continuation(c, tty)) {
992 num_chars++;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Jiri Slaby57c94122012-10-18 22:26:43 +0200995 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 } else {
997 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200998 echo_char_raw('\b', ldata);
999 echo_char_raw(' ', ldata);
1000 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001003 echo_char_raw('\b', ldata);
1004 echo_char_raw(' ', ldata);
1005 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 }
1008 }
1009 if (kill_type == ERASE)
1010 break;
1011 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001012 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001013 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016/**
1017 * isig - handle the ISIG optio
1018 * @sig: signal
1019 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001021 * Called when a signal is being sent due to terminal input.
1022 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001023 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001024 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Alan Cox4edf1822008-02-08 04:18:44 -08001026
Peter Hurley8c985d12013-03-06 08:38:19 -05001027static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Peter Hurley8c985d12013-03-06 08:38:19 -05001029 struct pid *tty_pgrp = tty_get_pgrp(tty);
1030 if (tty_pgrp) {
1031 kill_pgrp(tty_pgrp, sig, 1);
1032 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034}
1035
1036/**
1037 * n_tty_receive_break - handle break
1038 * @tty: terminal
1039 *
1040 * An RS232 break event has been hit in the incoming bitstream. This
1041 * can cause a variety of events depending upon the termios settings.
1042 *
1043 * Called from the receive_buf path so single threaded.
1044 */
Alan Cox4edf1822008-02-08 04:18:44 -08001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static inline void n_tty_receive_break(struct tty_struct *tty)
1047{
Jiri Slaby57c94122012-10-18 22:26:43 +02001048 struct n_tty_data *ldata = tty->disc_data;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (I_IGNBRK(tty))
1051 return;
1052 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001053 isig(SIGINT, tty);
1054 if (!L_NOFLSH(tty)) {
1055 n_tty_flush_buffer(tty);
1056 tty_driver_flush_buffer(tty);
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return;
1059 }
1060 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001061 put_tty_queue('\377', ldata);
1062 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001064 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 wake_up_interruptible(&tty->read_wait);
1066}
1067
1068/**
1069 * n_tty_receive_overrun - handle overrun reporting
1070 * @tty: terminal
1071 *
1072 * Data arrived faster than we could process it. While the tty
1073 * driver has flagged this the bits that were missed are gone
1074 * forever.
1075 *
1076 * Called from the receive_buf path so single threaded. Does not
1077 * need locking as num_overrun and overrun_time are function
1078 * private.
1079 */
Alan Cox4edf1822008-02-08 04:18:44 -08001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static inline void n_tty_receive_overrun(struct tty_struct *tty)
1082{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001083 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 char buf[64];
1085
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001086 ldata->num_overrun++;
1087 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1088 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1090 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001091 ldata->num_overrun);
1092 ldata->overrun_time = jiffies;
1093 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095}
1096
1097/**
1098 * n_tty_receive_parity_error - error notifier
1099 * @tty: terminal device
1100 * @c: character
1101 *
1102 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001103 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
1105static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1106 unsigned char c)
1107{
Jiri Slaby57c94122012-10-18 22:26:43 +02001108 struct n_tty_data *ldata = tty->disc_data;
1109
Alan Cox4edf1822008-02-08 04:18:44 -08001110 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001113 put_tty_queue('\377', ldata);
1114 put_tty_queue('\0', ldata);
1115 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001117 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001119 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 wake_up_interruptible(&tty->read_wait);
1121}
1122
1123/**
1124 * n_tty_receive_char - perform processing
1125 * @tty: terminal device
1126 * @c: character
1127 *
1128 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001129 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * driver above.
1131 */
1132
1133static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1134{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001135 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001137 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001139 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001140 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return;
1142 }
Alan Cox4edf1822008-02-08 04:18:44 -08001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (I_ISTRIP(tty))
1145 c &= 0x7f;
1146 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001147 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
hyc@symas.com26df6d12010-06-22 10:14:49 -07001149 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001150 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001151 return;
1152 }
1153
Joe Peterson54d2a372008-02-06 01:37:59 -08001154 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001155 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1156 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001157 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001158 process_echoes(tty);
1159 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (tty->closing) {
1162 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001163 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001165 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001166 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 stop_tty(tty);
1168 }
1169 return;
1170 }
1171
1172 /*
1173 * If the previous character was LNEXT, or we know that this
1174 * character is not one of the characters that we'll have to
1175 * handle specially, do shortcut processing to speed things
1176 * up.
1177 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001178 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001179 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001180 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001181 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001182 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001183 if (L_ECHO(tty))
1184 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001185 return;
1186 }
1187 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001188 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001190 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001191 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001193 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001195 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001196 put_tty_queue(c, ldata);
1197 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
1199 }
Alan Cox4edf1822008-02-08 04:18:44 -08001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (I_IXON(tty)) {
1202 if (c == START_CHAR(tty)) {
1203 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001204 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
1206 }
1207 if (c == STOP_CHAR(tty)) {
1208 stop_tty(tty);
1209 return;
1210 }
1211 }
Joe Peterson575537b32008-04-30 00:53:30 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (L_ISIG(tty)) {
1214 int signal;
1215 signal = SIGINT;
1216 if (c == INTR_CHAR(tty))
1217 goto send_signal;
1218 signal = SIGQUIT;
1219 if (c == QUIT_CHAR(tty))
1220 goto send_signal;
1221 signal = SIGTSTP;
1222 if (c == SUSP_CHAR(tty)) {
1223send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001224 if (!L_NOFLSH(tty)) {
1225 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001226 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001227 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001228 if (I_IXON(tty))
1229 start_tty(tty);
1230 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001231 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001232 process_echoes(tty);
1233 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001234 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return;
1236 }
1237 }
Joe Peterson575537b32008-04-30 00:53:30 -07001238
1239 if (c == '\r') {
1240 if (I_IGNCR(tty))
1241 return;
1242 if (I_ICRNL(tty))
1243 c = '\n';
1244 } else if (c == '\n' && I_INLCR(tty))
1245 c = '\r';
1246
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001247 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1249 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1250 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001251 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
1253 }
1254 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001255 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001257 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001259 echo_char_raw('^', ldata);
1260 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001261 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 }
1264 return;
1265 }
1266 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1267 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001268 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jiri Slaby57c94122012-10-18 22:26:43 +02001270 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001272 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001273 while (tail != ldata->read_head) {
1274 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1276 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001277 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
1279 }
1280 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001281 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001282 if (L_ECHO(tty))
1283 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001284 return;
1285 }
1286 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001287 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001288 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 goto handle_newline;
1291 }
1292 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001293 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001294 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001295 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001296 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 c = __DISABLED_CHAR;
1298 goto handle_newline;
1299 }
1300 if ((c == EOL_CHAR(tty)) ||
1301 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001302 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1303 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001304 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001305 if (L_ECHO(tty))
1306 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001307 return;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1311 */
1312 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001314 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001315 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001317 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 /*
1320 * XXX does PARMRK doubling happen for
1321 * EOL_CHAR and EOL2_CHAR?
1322 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001323 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001324 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alan Cox4edf1822008-02-08 04:18:44 -08001326handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001327 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001328 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001329 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001330 ldata->canon_head = ldata->read_head;
1331 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001332 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1334 if (waitqueue_active(&tty->read_wait))
1335 wake_up_interruptible(&tty->read_wait);
1336 return;
1337 }
1338 }
Alan Cox4edf1822008-02-08 04:18:44 -08001339
Joe Petersonacc71bb2009-01-02 13:43:32 +00001340 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001341 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001342 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001343 if (L_ECHO(tty))
1344 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001345 return;
1346 }
1347 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001348 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001350 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 else {
1352 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001353 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001354 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 echo_char(c, tty);
1356 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001357 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
Joe Petersonacc71bb2009-01-02 13:43:32 +00001360 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001361 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jiri Slaby57c94122012-10-18 22:26:43 +02001363 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367/**
1368 * n_tty_write_wakeup - asynchronous I/O notifier
1369 * @tty: tty device
1370 *
1371 * Required for the ptys, serial driver etc. since processes
1372 * that attach themselves to the master and rely on ASYNC
1373 * IO must be woken up
1374 */
1375
1376static void n_tty_write_wakeup(struct tty_struct *tty)
1377{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001378 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382/**
1383 * n_tty_receive_buf - data receive
1384 * @tty: terminal device
1385 * @cp: buffer
1386 * @fp: flag buffer
1387 * @count: characters
1388 *
1389 * Called by the terminal driver when a block of characters has
1390 * been received. This function must be called from soft contexts
1391 * not from interrupt context. The driver is responsible for making
1392 * calls one at a time and in order (or using flush_to_ldisc)
1393 */
Alan Cox4edf1822008-02-08 04:18:44 -08001394
Linus Torvalds55db4c62011-06-04 06:33:24 +09001395static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1396 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001398 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 const unsigned char *p;
1400 char *f, flags = TTY_NORMAL;
1401 int i;
1402 char buf[64];
1403 unsigned long cpuflags;
1404
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001405 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001406 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001407 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1408 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001410 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1411 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1412 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 cp += i;
1414 count -= i;
1415
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001416 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1417 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001419 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1420 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1421 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001422 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001424 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (f)
1426 flags = *f++;
1427 switch (flags) {
1428 case TTY_NORMAL:
1429 n_tty_receive_char(tty, *p);
1430 break;
1431 case TTY_BREAK:
1432 n_tty_receive_break(tty);
1433 break;
1434 case TTY_PARITY:
1435 case TTY_FRAME:
1436 n_tty_receive_parity_error(tty, *p);
1437 break;
1438 case TTY_OVERRUN:
1439 n_tty_receive_overrun(tty);
1440 break;
1441 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001442 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 tty_name(tty, buf), flags);
1444 break;
1445 }
1446 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001447 if (tty->ops->flush_chars)
1448 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Linus Torvalds55db4c62011-06-04 06:33:24 +09001451 n_tty_set_room(tty);
1452
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001453 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001454 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1456 if (waitqueue_active(&tty->read_wait))
1457 wake_up_interruptible(&tty->read_wait);
1458 }
1459
1460 /*
1461 * Check the remaining room for the input canonicalization
1462 * mode. We don't want to throttle the driver if we're in
1463 * canonical mode and don't have a newline yet!
1464 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001465 while (1) {
1466 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1467 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1468 break;
1469 if (!tty_throttle_safe(tty))
1470 break;
1471 }
1472 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475int is_ignored(int sig)
1476{
1477 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001478 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481/**
1482 * n_tty_set_termios - termios data changed
1483 * @tty: terminal
1484 * @old: previous data
1485 *
1486 * Called by the tty layer when the user changes termios flags so
1487 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001488 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 * guaranteed that this function will not be re-entered or in progress
1490 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001491 *
1492 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
Alan Cox4edf1822008-02-08 04:18:44 -08001494
1495static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001497 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001498 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001499
1500 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001501 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001502 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001503 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001504 ldata->canon_head = ldata->read_tail;
1505 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001506 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001507 }
1508
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001509 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001510 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001511
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001512 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001514 ldata->raw = 1;
1515 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001516 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return;
1518 }
1519 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1520 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1521 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1522 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001523 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001526 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001528 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001531 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1532 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1533 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1534 set_bit('\n', ldata->process_char_map);
1535 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (L_IEXTEN(tty)) {
1537 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001538 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001540 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001542 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (L_ECHO(tty))
1544 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001545 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547 }
1548 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001549 set_bit(START_CHAR(tty), ldata->process_char_map);
1550 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001553 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1554 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1555 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001557 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001558 ldata->raw = 0;
1559 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001561 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1563 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1564 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001565 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001567 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001569 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001570 /* The termios change make the tty ready for I/O */
1571 wake_up_interruptible(&tty->write_wait);
1572 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575/**
1576 * n_tty_close - close the ldisc for this tty
1577 * @tty: device
1578 *
Alan Cox4edf1822008-02-08 04:18:44 -08001579 * Called from the terminal layer when this line discipline is
1580 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * discipline change. The function will not be called while other
1582 * ldisc methods are in progress.
1583 */
Alan Cox4edf1822008-02-08 04:18:44 -08001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585static void n_tty_close(struct tty_struct *tty)
1586{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001587 struct n_tty_data *ldata = tty->disc_data;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 n_tty_flush_buffer(tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001590 kfree(ldata->read_buf);
1591 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001592 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001593 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596/**
1597 * n_tty_open - open an ldisc
1598 * @tty: terminal to open
1599 *
Alan Cox4edf1822008-02-08 04:18:44 -08001600 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * terminal device. Can sleep. Called serialized so that no
1602 * other events will occur in parallel. No further open will occur
1603 * until a close.
1604 */
1605
1606static int n_tty_open(struct tty_struct *tty)
1607{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001608 struct n_tty_data *ldata;
1609
1610 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1611 if (!ldata)
1612 goto err;
1613
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001614 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001615 mutex_init(&ldata->atomic_read_lock);
1616 mutex_init(&ldata->output_lock);
1617 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001618 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001619
Joe Petersona88a69c2009-01-02 13:40:53 +00001620 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001621 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1622 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1623 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001624 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001625
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001626 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001628 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001629 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 n_tty_set_termios(tty, NULL);
1631 tty->minimum_to_wake = 1;
1632 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001635err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001636 kfree(ldata->read_buf);
1637 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001638 kfree(ldata);
1639err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001640 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static inline int input_available_p(struct tty_struct *tty, int amt)
1644{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001645 struct n_tty_data *ldata = tty->disc_data;
1646
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001647 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001648 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001649 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001651 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 1;
1653
1654 return 0;
1655}
1656
1657/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001658 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 * @tty: terminal device
1660 * @b: user data
1661 * @nr: size of data
1662 *
Alan Cox11a96d12008-10-13 10:46:24 +01001663 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * ICANON is off; it copies characters straight from the tty queue to
1665 * user space directly. It can be profitably called twice; once to
1666 * drain the space from the tail pointer to the (physical) end of the
1667 * buffer, and once to drain the space from the (physical) beginning of
1668 * the buffer to head pointer.
1669 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001670 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 *
1672 */
Alan Cox4edf1822008-02-08 04:18:44 -08001673
Alan Cox33f0f882006-01-09 20:54:13 -08001674static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 unsigned char __user **b,
1676 size_t *nr)
1677
1678{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001679 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int retval;
1681 size_t n;
1682 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001683 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001686 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001687 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001689 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001691 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001693 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001694 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1695 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001696 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001697 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001698 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1699 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001700 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001701 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001702 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001703 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 *b += n;
1705 *nr -= n;
1706 }
1707 return retval;
1708}
1709
Al Virocc4191d2008-03-29 03:08:48 +00001710extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001711 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713/**
1714 * job_control - check job control
1715 * @tty: tty
1716 * @file: file handle
1717 *
1718 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001719 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001721 *
1722 * FIXME:
1723 * Locking: None - redirected write test is safe, testing
1724 * current->signal should possibly lock current->sighand
1725 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
Alan Cox4edf1822008-02-08 04:18:44 -08001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728static int job_control(struct tty_struct *tty, struct file *file)
1729{
1730 /* Job control check -- must be done at start and after
1731 every sleep (POSIX.1 7.1.1.4). */
1732 /* NOTE: not yet done after every sleep pending a thorough
1733 check of the logic of this change. -- jlc */
1734 /* don't stop on /dev/console */
1735 if (file->f_op->write != redirected_tty_write &&
1736 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001737 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001738 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001739 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001741 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001743 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001744 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return -ERESTARTSYS;
1746 }
1747 }
1748 return 0;
1749}
Alan Cox4edf1822008-02-08 04:18:44 -08001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752/**
Alan Cox11a96d12008-10-13 10:46:24 +01001753 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 * @tty: tty device
1755 * @file: file object
1756 * @buf: userspace buffer pointer
1757 * @nr: size of I/O
1758 *
1759 * Perform reads for the line discipline. We are guaranteed that the
1760 * line discipline will not be closed under us but we may get multiple
1761 * parallel readers and must handle this ourselves. We may also get
1762 * a hangup. Always called in user context, may sleep.
1763 *
1764 * This code must be sure never to sleep through a hangup.
1765 */
Alan Cox4edf1822008-02-08 04:18:44 -08001766
Alan Cox11a96d12008-10-13 10:46:24 +01001767static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 unsigned char __user *buf, size_t nr)
1769{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001770 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 unsigned char __user *b = buf;
1772 DECLARE_WAITQUEUE(wait, current);
1773 int c;
1774 int minimum, time;
1775 ssize_t retval = 0;
1776 ssize_t size;
1777 long timeout;
1778 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001779 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001783 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 minimum = time = 0;
1787 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001788 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 time = (HZ / 10) * TIME_CHAR(tty);
1790 minimum = MIN_CHAR(tty);
1791 if (minimum) {
1792 if (time)
1793 tty->minimum_to_wake = 1;
1794 else if (!waitqueue_active(&tty->read_wait) ||
1795 (tty->minimum_to_wake > minimum))
1796 tty->minimum_to_wake = minimum;
1797 } else {
1798 timeout = 0;
1799 if (time) {
1800 timeout = time;
1801 time = 0;
1802 }
1803 tty->minimum_to_wake = minimum = 1;
1804 }
1805 }
1806
1807 /*
1808 * Internal serialization of reads.
1809 */
1810 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001811 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001813 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001814 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return -ERESTARTSYS;
1816 }
Alan Cox04f378b2008-04-30 00:53:29 -07001817 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 while (nr) {
1821 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001822 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned char cs;
1824 if (b != buf)
1825 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001826 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cs = tty->link->ctrl_status;
1828 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001829 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001830 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 retval = -EFAULT;
1832 b--;
1833 break;
1834 }
1835 nr--;
1836 break;
1837 }
1838 /* This statement must be first before checking for input
1839 so that any interrupt will set the state back to
1840 TASK_RUNNING. */
1841 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1844 ((minimum - (b - buf)) >= 1))
1845 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (!input_available_p(tty, 0)) {
1848 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1849 retval = -EIO;
1850 break;
1851 }
1852 if (tty_hung_up_p(file))
1853 break;
1854 if (!timeout)
1855 break;
1856 if (file->f_flags & O_NONBLOCK) {
1857 retval = -EAGAIN;
1858 break;
1859 }
1860 if (signal_pending(current)) {
1861 retval = -ERESTARTSYS;
1862 break;
1863 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001864 /* FIXME: does n_tty_set_room need locking ? */
1865 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 continue;
1868 }
1869 __set_current_state(TASK_RUNNING);
1870
1871 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001872 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001873 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 retval = -EFAULT;
1875 b--;
1876 break;
1877 }
1878 nr--;
1879 }
1880
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001881 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001883 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001884 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001885 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001887 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001888 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001889 c = ldata->read_buf[ldata->read_tail];
1890 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001892 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (eol) {
1894 /* this test should be redundant:
1895 * we shouldn't be reading data if
1896 * canon_data is 0
1897 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001898 if (--ldata->canon_data < 0)
1899 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Ivo Sieben98001212013-01-28 13:32:01 +01001901 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001904 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 retval = -EFAULT;
1906 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001907 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
1909 }
1910 nr--;
1911 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001912 if (eol) {
1913 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001914 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001916 }
Ivo Sieben98001212013-01-28 13:32:01 +01001917 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Ivo Sieben98001212013-01-28 13:32:01 +01001919 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (retval)
1921 break;
1922 } else {
1923 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001924 /* The copy function takes the read lock and handles
1925 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 uncopied = copy_from_read_buf(tty, &b, &nr);
1927 uncopied += copy_from_read_buf(tty, &b, &nr);
1928 if (uncopied) {
1929 retval = -EFAULT;
1930 break;
1931 }
1932 }
1933
1934 /* If there is enough space in the read buffer now, let the
1935 * low-level driver know. We use n_tty_chars_in_buffer() to
1936 * check the buffer, as it now knows about canonical mode.
1937 * Otherwise, if the driver is throttled and the line is
1938 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1939 * we won't get any more characters.
1940 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001941 while (1) {
1942 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1943 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1944 break;
1945 if (!tty->count)
1946 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001947 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001948 if (!tty_unthrottle_safe(tty))
1949 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001950 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001951 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 if (b - buf >= minimum)
1954 break;
1955 if (time)
1956 timeout = time;
1957 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001958 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 remove_wait_queue(&tty->read_wait, &wait);
1960
1961 if (!waitqueue_active(&tty->read_wait))
1962 tty->minimum_to_wake = minimum;
1963
1964 __set_current_state(TASK_RUNNING);
1965 size = b - buf;
1966 if (size) {
1967 retval = size;
1968 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001969 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001971 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvalds55db4c62011-06-04 06:33:24 +09001973 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return retval;
1975}
1976
1977/**
Alan Cox11a96d12008-10-13 10:46:24 +01001978 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 * @tty: tty device
1980 * @file: file object
1981 * @buf: userspace buffer pointer
1982 * @nr: size of I/O
1983 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001984 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001986 * and other such events. Since the receive code will echo characters,
1987 * thus calling driver write methods, the output_lock is used in
1988 * the output processing functions called here as well as in the
1989 * echo processing function to protect the column state and space
1990 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *
1992 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001993 *
1994 * Locking: output_lock to protect column state and space left
1995 * (note that the process_output*() functions take this
1996 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 */
Alan Cox4edf1822008-02-08 04:18:44 -08001998
Alan Cox11a96d12008-10-13 10:46:24 +01001999static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002000 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 const unsigned char *b = buf;
2003 DECLARE_WAITQUEUE(wait, current);
2004 int c;
2005 ssize_t retval = 0;
2006
2007 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2008 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2009 retval = tty_check_change(tty);
2010 if (retval)
2011 return retval;
2012 }
2013
Joe Petersona88a69c2009-01-02 13:40:53 +00002014 /* Write out any echoed characters that are still pending */
2015 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 add_wait_queue(&tty->write_wait, &wait);
2018 while (1) {
2019 set_current_state(TASK_INTERRUPTIBLE);
2020 if (signal_pending(current)) {
2021 retval = -ERESTARTSYS;
2022 break;
2023 }
2024 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2025 retval = -EIO;
2026 break;
2027 }
2028 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2029 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002030 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (num < 0) {
2032 if (num == -EAGAIN)
2033 break;
2034 retval = num;
2035 goto break_out;
2036 }
2037 b += num;
2038 nr -= num;
2039 if (nr == 0)
2040 break;
2041 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002042 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
2044 b++; nr--;
2045 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002046 if (tty->ops->flush_chars)
2047 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002049 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002050 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002051 if (c < 0) {
2052 retval = c;
2053 goto break_out;
2054 }
2055 if (!c)
2056 break;
2057 b += c;
2058 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 if (!nr)
2062 break;
2063 if (file->f_flags & O_NONBLOCK) {
2064 retval = -EAGAIN;
2065 break;
2066 }
2067 schedule();
2068 }
2069break_out:
2070 __set_current_state(TASK_RUNNING);
2071 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002072 if (b - buf != nr && tty->fasync)
2073 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return (b - buf) ? b - buf : retval;
2075}
2076
2077/**
Alan Cox11a96d12008-10-13 10:46:24 +01002078 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 * @tty: terminal device
2080 * @file: file accessing it
2081 * @wait: poll table
2082 *
2083 * Called when the line discipline is asked to poll() for data or
2084 * for special events. This code is not serialized with respect to
2085 * other events save open/close.
2086 *
2087 * This code must be sure never to sleep through a hangup.
2088 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Alan Cox4edf1822008-02-08 04:18:44 -08002090
Alan Cox11a96d12008-10-13 10:46:24 +01002091static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002092 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 unsigned int mask = 0;
2095
2096 poll_wait(file, &tty->read_wait, wait);
2097 poll_wait(file, &tty->write_wait, wait);
2098 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2099 mask |= POLLIN | POLLRDNORM;
2100 if (tty->packet && tty->link->ctrl_status)
2101 mask |= POLLPRI | POLLIN | POLLRDNORM;
2102 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2103 mask |= POLLHUP;
2104 if (tty_hung_up_p(file))
2105 mask |= POLLHUP;
2106 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2107 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2108 tty->minimum_to_wake = MIN_CHAR(tty);
2109 else
2110 tty->minimum_to_wake = 1;
2111 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002112 if (tty->ops->write && !tty_is_writelocked(tty) &&
2113 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2114 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 mask |= POLLOUT | POLLWRNORM;
2116 return mask;
2117}
2118
Jiri Slaby57c94122012-10-18 22:26:43 +02002119static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002120{
2121 int nr, head, tail;
2122
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002123 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002124 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002125 head = ldata->canon_head;
2126 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002127 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2128 /* Skip EOF-chars.. */
2129 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002130 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002131 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002132 nr--;
2133 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2134 }
2135 return nr;
2136}
2137
2138static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2139 unsigned int cmd, unsigned long arg)
2140{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002141 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002142 int retval;
2143
2144 switch (cmd) {
2145 case TIOCOUTQ:
2146 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2147 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002148 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002149 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002150 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002151 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002152 return put_user(retval, (unsigned int __user *) arg);
2153 default:
2154 return n_tty_ioctl_helper(tty, file, cmd, arg);
2155 }
2156}
2157
Alan Coxa352def2008-07-16 21:53:12 +01002158struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002159 .magic = TTY_LDISC_MAGIC,
2160 .name = "n_tty",
2161 .open = n_tty_open,
2162 .close = n_tty_close,
2163 .flush_buffer = n_tty_flush_buffer,
2164 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002165 .read = n_tty_read,
2166 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002167 .ioctl = n_tty_ioctl,
2168 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002169 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002170 .receive_buf = n_tty_receive_buf,
2171 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002173
2174/**
2175 * n_tty_inherit_ops - inherit N_TTY methods
2176 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2177 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002178 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002179 * methods.
2180 */
2181
2182void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2183{
2184 *ops = tty_ldisc_N_TTY;
2185 ops->owner = NULL;
2186 ops->refcount = ops->flags = 0;
2187}
2188EXPORT_SYMBOL_GPL(n_tty_inherit_ops);