blob: 594a3c19882b1d2b7af33092111b26b35dfe9b2f [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
Peter Hurley32f13522013-06-15 09:14:17 -040077#undef N_TTY_TRACE
78#ifdef N_TTY_TRACE
79# define n_tty_trace(f, args...) trace_printk(f, ##args)
80#else
81# define n_tty_trace(f, args...)
82#endif
83
Jiri Slaby70ece7a2012-10-18 22:26:38 +020084struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020085 unsigned int column;
86 unsigned long overrun_time;
87 int num_overrun;
88
Peter Hurley24a89d12013-06-15 09:14:15 -040089 /* non-atomic */
90 bool no_room;
91
Jiri Slaby53c5ee22012-10-18 22:26:39 +020092 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
93 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020094
95 DECLARE_BITMAP(process_char_map, 256);
96 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020097
98 char *read_buf;
99 int read_head;
100 int read_tail;
101 int read_cnt;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -0400102 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200103
104 unsigned char *echo_buf;
105 unsigned int echo_pos;
106 unsigned int echo_cnt;
107
108 int canon_data;
109 unsigned long canon_head;
110 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200111
112 struct mutex atomic_read_lock;
113 struct mutex output_lock;
114 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100115 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200116};
117
Miloslav Trmac522ed772007-07-15 23:40:56 -0700118static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
119 unsigned char __user *ptr)
120{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200121 struct n_tty_data *ldata = tty->disc_data;
122
123 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700124 return put_user(x, ptr);
125}
126
Peter Hurley24a89d12013-06-15 09:14:15 -0400127static int receive_room(struct tty_struct *tty)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900128{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200129 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500130 int left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400131
Jaeden Amero090abf72012-07-27 08:43:11 -0500132 if (I_PARMRK(tty)) {
133 /* Multiply read_cnt by 3, since each byte might take up to
134 * three times as many spaces when PARMRK is set (depending on
135 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200136 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500137 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200138 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500139
Linus Torvalds55db4c62011-06-04 06:33:24 +0900140 /*
141 * If we are doing input canonicalization, and there are no
142 * pending newlines, let characters through without limit, so
143 * that erase characters will be handled. Other excess
144 * characters will be beeped.
145 */
146 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200147 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900148
Peter Hurley24a89d12013-06-15 09:14:15 -0400149 return left;
Peter Hurley7879a9f2013-06-15 07:28:31 -0400150}
151
Peter Hurley24a89d12013-06-15 09:14:15 -0400152/**
153 * n_tty_set_room - receive space
154 * @tty: terminal
155 *
156 * Re-schedules the flip buffer work if space just became available.
157 *
158 * Locks: Concurrent update is protected with read_lock
159 */
160
Peter Hurley7879a9f2013-06-15 07:28:31 -0400161static void n_tty_set_room(struct tty_struct *tty)
162{
Peter Hurley24a89d12013-06-15 09:14:15 -0400163 struct n_tty_data *ldata = tty->disc_data;
164
Linus Torvalds55db4c62011-06-04 06:33:24 +0900165 /* Did this open up the receive buffer? We may need to flip */
Peter Hurley24a89d12013-06-15 09:14:15 -0400166 if (unlikely(ldata->no_room) && receive_room(tty)) {
167 ldata->no_room = 0;
168
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200169 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400170 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400171 /* see if ldisc has been killed - if so, this means that
172 * even though the ldisc has been halted and ->buf.work
173 * cancelled, ->buf.work is about to be rescheduled
174 */
175 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
176 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200177 schedule_work(&tty->port->buf.work);
178 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900179}
180
Jiri Slaby57c94122012-10-18 22:26:43 +0200181static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200183 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
184 ldata->read_buf[ldata->read_head] = c;
185 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
186 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188}
189
Alan Cox17b82062008-10-13 10:45:06 +0100190/**
191 * put_tty_queue - add character to tty
192 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200193 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100194 *
195 * Add a character to the tty read_buf queue. This is done under the
196 * read_lock to serialize character addition and also to protect us
197 * against parallel reads or flushes
198 */
199
Jiri Slaby57c94122012-10-18 22:26:43 +0200200static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long flags;
203 /*
204 * The problem of stomping on the buffers ends here.
205 * Why didn't anyone see this one coming? --AJK
206 */
Ivo Sieben98001212013-01-28 13:32:01 +0100207 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200208 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100209 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * reset_buffer_flags - reset buffer state
214 * @tty: terminal to reset
215 *
Peter Hurley25518c62013-03-11 16:44:31 -0400216 * Reset the read buffer counters and clear the flags.
217 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100218 *
219 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000221
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400222static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 unsigned long flags;
225
Ivo Sieben98001212013-01-28 13:32:01 +0100226 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200227 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100228 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000229
Jiri Slabybddc7152012-10-18 22:26:42 +0200230 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200231 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200232 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000233
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200234 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200235 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Peter Hurleya30737a2013-03-11 16:44:22 -0400238static void n_tty_packet_mode_flush(struct tty_struct *tty)
239{
240 unsigned long flags;
241
242 spin_lock_irqsave(&tty->ctrl_lock, flags);
243 if (tty->link->packet) {
244 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
245 wake_up_interruptible(&tty->link->read_wait);
246 }
247 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
251 * n_tty_flush_buffer - clean input queue
252 * @tty: terminal device
253 *
Peter Hurley25518c62013-03-11 16:44:31 -0400254 * Flush the input buffer. Called when the tty layer wants the
255 * buffer flushed (eg at hangup) or when the N_TTY line discipline
256 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
Alan Cox17b82062008-10-13 10:45:06 +0100258 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Alan Cox4edf1822008-02-08 04:18:44 -0800260
261static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400263 reset_buffer_flags(tty->disc_data);
264 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800265
Peter Hurleya30737a2013-03-11 16:44:22 -0400266 if (tty->link)
267 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
271 * n_tty_chars_in_buffer - report available bytes
272 * @tty: tty device
273 *
274 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800275 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100276 *
277 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Alan Cox4edf1822008-02-08 04:18:44 -0800279
Peter Hurleya19d0c62013-06-15 09:14:18 -0400280static ssize_t chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200282 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned long flags;
284 ssize_t n = 0;
285
Ivo Sieben98001212013-01-28 13:32:01 +0100286 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200287 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200288 n = ldata->read_cnt;
289 } else if (ldata->canon_data) {
290 n = (ldata->canon_head > ldata->read_tail) ?
291 ldata->canon_head - ldata->read_tail :
292 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Ivo Sieben98001212013-01-28 13:32:01 +0100294 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return n;
296}
297
Peter Hurleya19d0c62013-06-15 09:14:18 -0400298static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
299{
300 return chars_in_buffer(tty);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/**
304 * is_utf8_continuation - utf8 multibyte check
305 * @c: byte to check
306 *
307 * Returns true if the utf8 character 'c' is a multibyte continuation
308 * character. We use this to correctly compute the on screen size
309 * of the character when printing
310 */
Alan Cox4edf1822008-02-08 04:18:44 -0800311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static inline int is_utf8_continuation(unsigned char c)
313{
314 return (c & 0xc0) == 0x80;
315}
316
317/**
318 * is_continuation - multibyte check
319 * @c: byte to check
320 *
321 * Returns true if the utf8 character 'c' is a multibyte continuation
322 * character and the terminal is in unicode mode.
323 */
Alan Cox4edf1822008-02-08 04:18:44 -0800324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static inline int is_continuation(unsigned char c, struct tty_struct *tty)
326{
327 return I_IUTF8(tty) && is_utf8_continuation(c);
328}
329
330/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000331 * do_output_char - output one character
332 * @c: character (or partial unicode symbol)
333 * @tty: terminal device
334 * @space: space available in tty driver write buffer
335 *
336 * This is a helper function that handles one output character
337 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600338 * doing OPOST processing and putting the results in the
339 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000340 *
341 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
342 * and NLDLY. They simply aren't relevant in the world today.
343 * If you ever need them, add them here.
344 *
345 * Returns the number of bytes of buffer space used or -1 if
346 * no space left.
347 *
348 * Locking: should be called under the output_lock to protect
349 * the column state and space left in the buffer
350 */
351
352static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
353{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200354 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000355 int spaces;
356
357 if (!space)
358 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000359
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 switch (c) {
361 case '\n':
362 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200363 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000364 if (O_ONLCR(tty)) {
365 if (space < 2)
366 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200367 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700368 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000369 return 2;
370 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200371 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000372 break;
373 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200374 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 return 0;
376 if (O_OCRNL(tty)) {
377 c = '\n';
378 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200379 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000380 break;
381 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200382 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000383 break;
384 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200385 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 if (O_TABDLY(tty) == XTABS) {
387 if (space < spaces)
388 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200389 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000390 tty->ops->write(tty, " ", spaces);
391 return spaces;
392 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200393 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000394 break;
395 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200396 if (ldata->column > 0)
397 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000398 break;
399 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000400 if (!iscntrl(c)) {
401 if (O_OLCUC(tty))
402 c = toupper(c);
403 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200404 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000405 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000406 break;
407 }
408
409 tty_put_char(tty, c);
410 return 1;
411}
412
413/**
414 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * @c: character (or partial unicode symbol)
416 * @tty: terminal device
417 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600418 * Output one character with OPOST processing.
419 * Returns -1 when the output device is full and the character
420 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000422 * Locking: output_lock to protect column state and space left
423 * (also, this is called from n_tty_write under the
424 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Alan Cox4edf1822008-02-08 04:18:44 -0800426
Joe Petersona88a69c2009-01-02 13:40:53 +0000427static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Jiri Slabybddc7152012-10-18 22:26:42 +0200429 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000430 int space, retval;
431
Jiri Slabybddc7152012-10-18 22:26:42 +0200432 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Alan Coxf34d7a52008-04-30 00:54:13 -0700434 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000435 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jiri Slabybddc7152012-10-18 22:26:42 +0200437 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 if (retval < 0)
439 return -1;
440 else
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000445 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600447 * @buf: character buffer
448 * @nr: number of bytes to output
449 *
450 * Output a block of characters with OPOST processing.
451 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
453 * This path is used to speed up block console writes, among other
454 * things when processing blocks of output data. It handles only
455 * the simple cases normally found and helps to generate blocks of
456 * symbols for the console driver and thus improve performance.
457 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000458 * Locking: output_lock to protect column state and space left
459 * (also, this is called from n_tty_write under the
460 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Alan Cox4edf1822008-02-08 04:18:44 -0800462
Joe Petersona88a69c2009-01-02 13:40:53 +0000463static ssize_t process_output_block(struct tty_struct *tty,
464 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200466 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100468 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 const unsigned char *cp;
470
Jiri Slabybddc7152012-10-18 22:26:42 +0200471 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000472
Alan Coxf34d7a52008-04-30 00:54:13 -0700473 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000474 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200475 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (nr > space)
479 nr = space;
480
481 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000482 unsigned char c = *cp;
483
484 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 case '\n':
486 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200487 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (O_ONLCR(tty))
489 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200490 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200493 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto break_out;
495 if (O_OCRNL(tty))
496 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200497 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 case '\t':
500 goto break_out;
501 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200502 if (ldata->column > 0)
503 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000506 if (!iscntrl(c)) {
507 if (O_OLCUC(tty))
508 goto break_out;
509 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200510 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 }
514 }
515break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700516 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000517
Jiri Slabybddc7152012-10-18 22:26:42 +0200518 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return i;
520}
521
Joe Petersona88a69c2009-01-02 13:40:53 +0000522/**
523 * process_echoes - write pending echo characters
524 * @tty: terminal device
525 *
526 * Write previously buffered echo (and other ldisc-generated)
527 * characters to the tty.
528 *
529 * Characters generated by the ldisc (including echoes) need to
530 * be buffered because the driver's write buffer can fill during
531 * heavy program output. Echoing straight to the driver will
532 * often fail under these conditions, causing lost characters and
533 * resulting mismatches of ldisc state information.
534 *
535 * Since the ldisc state must represent the characters actually sent
536 * to the driver at the time of the write, operations like certain
537 * changes in column state are also saved in the buffer and executed
538 * here.
539 *
540 * A circular fifo buffer is used so that the most recent characters
541 * are prioritized. Also, when control characters are echoed with a
542 * prefixed "^", the pair is treated atomically and thus not separated.
543 *
544 * Locking: output_lock to protect column state and space left,
545 * echo_lock to protect the echo buffer
546 */
547
548static void process_echoes(struct tty_struct *tty)
549{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200550 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000551 int space, nr;
552 unsigned char c;
553 unsigned char *cp, *buf_end;
554
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200555 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000556 return;
557
Jiri Slabybddc7152012-10-18 22:26:42 +0200558 mutex_lock(&ldata->output_lock);
559 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000560
561 space = tty_write_room(tty);
562
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200563 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
564 cp = ldata->echo_buf + ldata->echo_pos;
565 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000566 while (nr > 0) {
567 c = *cp;
568 if (c == ECHO_OP_START) {
569 unsigned char op;
570 unsigned char *opp;
571 int no_space_left = 0;
572
573 /*
574 * If the buffer byte is the start of a multi-byte
575 * operation, get the next byte, which is either the
576 * op code or a control character value.
577 */
578 opp = cp + 1;
579 if (opp == buf_end)
580 opp -= N_TTY_BUF_SIZE;
581 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000582
Joe Petersona88a69c2009-01-02 13:40:53 +0000583 switch (op) {
584 unsigned int num_chars, num_bs;
585
586 case ECHO_OP_ERASE_TAB:
587 if (++opp == buf_end)
588 opp -= N_TTY_BUF_SIZE;
589 num_chars = *opp;
590
591 /*
592 * Determine how many columns to go back
593 * in order to erase the tab.
594 * This depends on the number of columns
595 * used by other characters within the tab
596 * area. If this (modulo 8) count is from
597 * the start of input rather than from a
598 * previous tab, we offset by canon column.
599 * Otherwise, tab spacing is normal.
600 */
601 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200602 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000603 num_bs = 8 - (num_chars & 7);
604
605 if (num_bs > space) {
606 no_space_left = 1;
607 break;
608 }
609 space -= num_bs;
610 while (num_bs--) {
611 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200612 if (ldata->column > 0)
613 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000614 }
615 cp += 3;
616 nr -= 3;
617 break;
618
619 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200620 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000621 cp += 2;
622 nr -= 2;
623 break;
624
625 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200626 if (ldata->column > 0)
627 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 cp += 2;
629 nr -= 2;
630 break;
631
632 case ECHO_OP_START:
633 /* This is an escaped echo op start code */
634 if (!space) {
635 no_space_left = 1;
636 break;
637 }
638 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200639 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000640 space--;
641 cp += 2;
642 nr -= 2;
643 break;
644
645 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000646 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600647 * If the op is not a special byte code,
648 * it is a ctrl char tagged to be echoed
649 * as "^X" (where X is the letter
650 * representing the control char).
651 * Note that we must ensure there is
652 * enough space for the whole ctrl pair.
653 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000654 */
Joe Peterson62b26352009-09-09 15:03:47 -0600655 if (space < 2) {
656 no_space_left = 1;
657 break;
658 }
659 tty_put_char(tty, '^');
660 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200661 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600662 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000663 cp += 2;
664 nr -= 2;
665 }
666
667 if (no_space_left)
668 break;
669 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400670 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600671 int retval = do_output_char(c, tty, space);
672 if (retval < 0)
673 break;
674 space -= retval;
675 } else {
676 if (!space)
677 break;
678 tty_put_char(tty, c);
679 space -= 1;
680 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 cp += 1;
682 nr -= 1;
683 }
684
685 /* When end of circular buffer reached, wrap around */
686 if (cp >= buf_end)
687 cp -= N_TTY_BUF_SIZE;
688 }
689
690 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200691 ldata->echo_pos = 0;
692 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200693 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000694 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200695 int num_processed = ldata->echo_cnt - nr;
696 ldata->echo_pos += num_processed;
697 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
698 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000699 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200700 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000701 }
702
Jiri Slabybddc7152012-10-18 22:26:42 +0200703 mutex_unlock(&ldata->echo_lock);
704 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000705
706 if (tty->ops->flush_chars)
707 tty->ops->flush_chars(tty);
708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 * add_echo_byte - add a byte to the echo buffer
712 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200713 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000714 *
715 * Add a character or operation byte to the echo buffer.
716 *
717 * Should be called under the echo lock to protect the echo buffer.
718 */
719
Jiri Slaby57c94122012-10-18 22:26:43 +0200720static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000721{
722 int new_byte_pos;
723
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200724 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000725 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200726 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727
728 /*
729 * Since the buffer start position needs to be advanced,
730 * be sure to step by a whole operation byte group.
731 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200732 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
733 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000734 (N_TTY_BUF_SIZE - 1)] ==
735 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200736 ldata->echo_pos += 3;
737 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200739 ldata->echo_pos += 2;
740 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000741 }
742 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200743 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000744 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200745 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000746
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200747 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000748 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200749 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000750 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200751 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000752 }
753
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200754 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000755}
756
757/**
758 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200759 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000760 *
761 * Add an operation to the echo buffer to move back one column.
762 *
763 * Locking: echo_lock to protect the echo buffer
764 */
765
Jiri Slaby57c94122012-10-18 22:26:43 +0200766static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000767{
Jiri Slabybddc7152012-10-18 22:26:42 +0200768 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200769 add_echo_byte(ECHO_OP_START, ldata);
770 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200771 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000772}
773
774/**
775 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200776 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000777 *
778 * Add an operation to the echo buffer to set the canon column
779 * to the current column.
780 *
781 * Locking: echo_lock to protect the echo buffer
782 */
783
Jiri Slaby57c94122012-10-18 22:26:43 +0200784static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000785{
Jiri Slabybddc7152012-10-18 22:26:42 +0200786 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200787 add_echo_byte(ECHO_OP_START, ldata);
788 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200789 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000790}
791
792/**
793 * echo_erase_tab - add operation to erase a tab
794 * @num_chars: number of character columns already used
795 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200796 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000797 *
798 * Add an operation to the echo buffer to erase a tab.
799 *
800 * Called by the eraser function, which knows how many character
801 * columns have been used since either a previous tab or the start
802 * of input. This information will be used later, along with
803 * canon column (if applicable), to go back the correct number
804 * of columns.
805 *
806 * Locking: echo_lock to protect the echo buffer
807 */
808
809static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200810 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000811{
Jiri Slabybddc7152012-10-18 22:26:42 +0200812 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000813
Jiri Slaby57c94122012-10-18 22:26:43 +0200814 add_echo_byte(ECHO_OP_START, ldata);
815 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000816
817 /* We only need to know this modulo 8 (tab spacing) */
818 num_chars &= 7;
819
820 /* Set the high bit as a flag if num_chars is after a previous tab */
821 if (after_tab)
822 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000823
Jiri Slaby57c94122012-10-18 22:26:43 +0200824 add_echo_byte(num_chars, 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_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * @c: unicode byte to echo
832 * @tty: terminal device
833 *
Alan Cox4edf1822008-02-08 04:18:44 -0800834 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100836 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000837 * This variant does not treat control characters specially.
838 *
839 * Locking: echo_lock to protect the echo buffer
840 */
841
Jiri Slaby57c94122012-10-18 22:26:43 +0200842static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000843{
Jiri Slabybddc7152012-10-18 22:26:42 +0200844 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000845 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200846 add_echo_byte(ECHO_OP_START, ldata);
847 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000848 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200849 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000850 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200851 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000852}
853
854/**
855 * echo_char - echo a character
856 * @c: unicode byte to echo
857 * @tty: terminal device
858 *
859 * Echo user input back onto the screen. This must be called only when
860 * L_ECHO(tty) is true. Called from the driver receive_buf path.
861 *
Joe Peterson62b26352009-09-09 15:03:47 -0600862 * This variant tags control characters to be echoed as "^X"
863 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000864 *
865 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
867
868static void echo_char(unsigned char c, struct tty_struct *tty)
869{
Jiri Slabybddc7152012-10-18 22:26:42 +0200870 struct n_tty_data *ldata = tty->disc_data;
871
872 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000873
874 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200875 add_echo_byte(ECHO_OP_START, ldata);
876 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000877 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600878 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200879 add_echo_byte(ECHO_OP_START, ldata);
880 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000881 }
882
Jiri Slabybddc7152012-10-18 22:26:42 +0200883 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Alan Cox17b82062008-10-13 10:45:06 +0100886/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000887 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200888 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100889 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000890
Jiri Slaby57c94122012-10-18 22:26:43 +0200891static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200893 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200894 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200895 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897}
898
899/**
900 * eraser - handle erase function
901 * @c: character input
902 * @tty: terminal device
903 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200904 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * present in the stream from the driver layer. Handles the complexities
906 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100907 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000908 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Alan Cox4edf1822008-02-08 04:18:44 -0800910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void eraser(unsigned char c, struct tty_struct *tty)
912{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200913 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 enum { ERASE, WERASE, KILL } kill_type;
915 int head, seen_alnums, cnt;
916 unsigned long flags;
917
Alan Cox17b82062008-10-13 10:45:06 +0100918 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200919 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000920 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return;
922 }
923 if (c == ERASE_CHAR(tty))
924 kill_type = ERASE;
925 else if (c == WERASE_CHAR(tty))
926 kill_type = WERASE;
927 else {
928 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100929 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200930 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200932 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100933 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return;
935 }
936 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200938 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200940 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100941 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200942 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 echo_char(KILL_CHAR(tty), tty);
944 /* Add a newline if ECHOK is on and ECHOKE is off. */
945 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200946 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948 }
949 kill_type = KILL;
950 }
951
952 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100953 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200954 while (ldata->read_head != ldata->canon_head) {
955 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* erase a single possibly multibyte character */
958 do {
959 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200960 c = ldata->read_buf[head];
961 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* do not partially erase */
964 if (is_continuation(c, tty))
965 break;
966
967 if (kill_type == WERASE) {
968 /* Equivalent to BSD's ALTWERASE. */
969 if (isalnum(c) || c == '_')
970 seen_alnums++;
971 else if (seen_alnums)
972 break;
973 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200974 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100975 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200976 ldata->read_head = head;
977 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100978 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (L_ECHO(tty)) {
980 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200981 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200982 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200983 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 /* if cnt > 1, output a multi-byte character */
986 echo_char(c, tty);
987 while (--cnt > 0) {
988 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200989 echo_char_raw(ldata->read_buf[head],
990 ldata);
991 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
994 echo_char(ERASE_CHAR(tty), tty);
995 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000996 unsigned int num_chars = 0;
997 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200998 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Joe Petersona88a69c2009-01-02 13:40:53 +00001000 /*
1001 * Count the columns used for characters
1002 * since the start of input or after a
1003 * previous tab.
1004 * This info is used to go back the correct
1005 * number of columns.
1006 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001007 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001008 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001009 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +00001010 if (c == '\t') {
1011 after_tab = 1;
1012 break;
Alan Cox300a6202009-01-02 13:41:04 +00001013 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001015 num_chars += 2;
1016 } else if (!is_continuation(c, tty)) {
1017 num_chars++;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001020 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else {
1022 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001023 echo_char_raw('\b', ldata);
1024 echo_char_raw(' ', ldata);
1025 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001028 echo_char_raw('\b', ldata);
1029 echo_char_raw(' ', ldata);
1030 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 }
1033 }
1034 if (kill_type == ERASE)
1035 break;
1036 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001037 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001038 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041/**
1042 * isig - handle the ISIG optio
1043 * @sig: signal
1044 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001046 * Called when a signal is being sent due to terminal input.
1047 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001048 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001049 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Alan Cox4edf1822008-02-08 04:18:44 -08001051
Peter Hurley8c985d12013-03-06 08:38:19 -05001052static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Peter Hurley8c985d12013-03-06 08:38:19 -05001054 struct pid *tty_pgrp = tty_get_pgrp(tty);
1055 if (tty_pgrp) {
1056 kill_pgrp(tty_pgrp, sig, 1);
1057 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059}
1060
1061/**
1062 * n_tty_receive_break - handle break
1063 * @tty: terminal
1064 *
1065 * An RS232 break event has been hit in the incoming bitstream. This
1066 * can cause a variety of events depending upon the termios settings.
1067 *
1068 * Called from the receive_buf path so single threaded.
1069 */
Alan Cox4edf1822008-02-08 04:18:44 -08001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static inline void n_tty_receive_break(struct tty_struct *tty)
1072{
Jiri Slaby57c94122012-10-18 22:26:43 +02001073 struct n_tty_data *ldata = tty->disc_data;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (I_IGNBRK(tty))
1076 return;
1077 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001078 isig(SIGINT, tty);
1079 if (!L_NOFLSH(tty)) {
1080 n_tty_flush_buffer(tty);
1081 tty_driver_flush_buffer(tty);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return;
1084 }
1085 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001086 put_tty_queue('\377', ldata);
1087 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001089 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 wake_up_interruptible(&tty->read_wait);
1091}
1092
1093/**
1094 * n_tty_receive_overrun - handle overrun reporting
1095 * @tty: terminal
1096 *
1097 * Data arrived faster than we could process it. While the tty
1098 * driver has flagged this the bits that were missed are gone
1099 * forever.
1100 *
1101 * Called from the receive_buf path so single threaded. Does not
1102 * need locking as num_overrun and overrun_time are function
1103 * private.
1104 */
Alan Cox4edf1822008-02-08 04:18:44 -08001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static inline void n_tty_receive_overrun(struct tty_struct *tty)
1107{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001108 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 char buf[64];
1110
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001111 ldata->num_overrun++;
1112 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1113 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1115 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001116 ldata->num_overrun);
1117 ldata->overrun_time = jiffies;
1118 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120}
1121
1122/**
1123 * n_tty_receive_parity_error - error notifier
1124 * @tty: terminal device
1125 * @c: character
1126 *
1127 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001128 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
1130static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1131 unsigned char c)
1132{
Jiri Slaby57c94122012-10-18 22:26:43 +02001133 struct n_tty_data *ldata = tty->disc_data;
1134
Alan Cox4edf1822008-02-08 04:18:44 -08001135 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001138 put_tty_queue('\377', ldata);
1139 put_tty_queue('\0', ldata);
1140 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001142 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001144 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 wake_up_interruptible(&tty->read_wait);
1146}
1147
1148/**
1149 * n_tty_receive_char - perform processing
1150 * @tty: terminal device
1151 * @c: character
1152 *
1153 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001154 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * driver above.
1156 */
1157
1158static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1159{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001160 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001162 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001164 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001165 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return;
1167 }
Alan Cox4edf1822008-02-08 04:18:44 -08001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (I_ISTRIP(tty))
1170 c &= 0x7f;
1171 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001172 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
hyc@symas.com26df6d12010-06-22 10:14:49 -07001174 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001175 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001176 return;
1177 }
1178
Joe Peterson54d2a372008-02-06 01:37:59 -08001179 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001180 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1181 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001182 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001183 process_echoes(tty);
1184 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (tty->closing) {
1187 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001188 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001190 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001191 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 stop_tty(tty);
1193 }
1194 return;
1195 }
1196
1197 /*
1198 * If the previous character was LNEXT, or we know that this
1199 * character is not one of the characters that we'll have to
1200 * handle specially, do shortcut processing to speed things
1201 * up.
1202 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001203 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001204 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001205 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001206 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001207 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001208 if (L_ECHO(tty))
1209 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001210 return;
1211 }
1212 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001213 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001215 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001216 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001218 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001220 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001221 put_tty_queue(c, ldata);
1222 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return;
1224 }
Alan Cox4edf1822008-02-08 04:18:44 -08001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (I_IXON(tty)) {
1227 if (c == START_CHAR(tty)) {
1228 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001229 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
1231 }
1232 if (c == STOP_CHAR(tty)) {
1233 stop_tty(tty);
1234 return;
1235 }
1236 }
Joe Peterson575537b32008-04-30 00:53:30 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (L_ISIG(tty)) {
1239 int signal;
1240 signal = SIGINT;
1241 if (c == INTR_CHAR(tty))
1242 goto send_signal;
1243 signal = SIGQUIT;
1244 if (c == QUIT_CHAR(tty))
1245 goto send_signal;
1246 signal = SIGTSTP;
1247 if (c == SUSP_CHAR(tty)) {
1248send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001249 if (!L_NOFLSH(tty)) {
1250 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001251 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001252 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001253 if (I_IXON(tty))
1254 start_tty(tty);
1255 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001256 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001257 process_echoes(tty);
1258 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001259 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return;
1261 }
1262 }
Joe Peterson575537b32008-04-30 00:53:30 -07001263
1264 if (c == '\r') {
1265 if (I_IGNCR(tty))
1266 return;
1267 if (I_ICRNL(tty))
1268 c = '\n';
1269 } else if (c == '\n' && I_INLCR(tty))
1270 c = '\r';
1271
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001272 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1274 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1275 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001276 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return;
1278 }
1279 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001280 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001282 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001284 echo_char_raw('^', ldata);
1285 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001286 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 }
1289 return;
1290 }
1291 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1292 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001293 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Jiri Slaby57c94122012-10-18 22:26:43 +02001295 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001297 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001298 while (tail != ldata->read_head) {
1299 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1301 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001302 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return;
1304 }
1305 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001306 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001307 if (L_ECHO(tty))
1308 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001309 return;
1310 }
1311 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001312 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001313 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 goto handle_newline;
1316 }
1317 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001318 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001320 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001321 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 c = __DISABLED_CHAR;
1323 goto handle_newline;
1324 }
1325 if ((c == EOL_CHAR(tty)) ||
1326 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001327 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1328 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001329 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001330 if (L_ECHO(tty))
1331 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001332 return;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1336 */
1337 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001339 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001340 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001342 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 /*
1345 * XXX does PARMRK doubling happen for
1346 * EOL_CHAR and EOL2_CHAR?
1347 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001348 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001349 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Alan Cox4edf1822008-02-08 04:18:44 -08001351handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001352 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001353 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001354 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001355 ldata->canon_head = ldata->read_head;
1356 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001357 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1359 if (waitqueue_active(&tty->read_wait))
1360 wake_up_interruptible(&tty->read_wait);
1361 return;
1362 }
1363 }
Alan Cox4edf1822008-02-08 04:18:44 -08001364
Joe Petersonacc71bb2009-01-02 13:43:32 +00001365 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001366 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001367 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001368 if (L_ECHO(tty))
1369 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001370 return;
1371 }
1372 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001375 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 else {
1377 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001378 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001379 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 echo_char(c, tty);
1381 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001382 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Joe Petersonacc71bb2009-01-02 13:43:32 +00001385 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001386 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Jiri Slaby57c94122012-10-18 22:26:43 +02001388 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392/**
1393 * n_tty_write_wakeup - asynchronous I/O notifier
1394 * @tty: tty device
1395 *
1396 * Required for the ptys, serial driver etc. since processes
1397 * that attach themselves to the master and rely on ASYNC
1398 * IO must be woken up
1399 */
1400
1401static void n_tty_write_wakeup(struct tty_struct *tty)
1402{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001403 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
1407/**
1408 * n_tty_receive_buf - data receive
1409 * @tty: terminal device
1410 * @cp: buffer
1411 * @fp: flag buffer
1412 * @count: characters
1413 *
1414 * Called by the terminal driver when a block of characters has
1415 * been received. This function must be called from soft contexts
1416 * not from interrupt context. The driver is responsible for making
1417 * calls one at a time and in order (or using flush_to_ldisc)
1418 */
Alan Cox4edf1822008-02-08 04:18:44 -08001419
Peter Hurley24a89d12013-06-15 09:14:15 -04001420static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1421 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001423 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 const unsigned char *p;
1425 char *f, flags = TTY_NORMAL;
1426 int i;
1427 char buf[64];
1428 unsigned long cpuflags;
1429
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001430 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001431 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001432 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1433 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001435 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1436 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1437 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 cp += i;
1439 count -= i;
1440
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001441 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1442 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001444 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1445 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1446 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001447 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001449 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (f)
1451 flags = *f++;
1452 switch (flags) {
1453 case TTY_NORMAL:
1454 n_tty_receive_char(tty, *p);
1455 break;
1456 case TTY_BREAK:
1457 n_tty_receive_break(tty);
1458 break;
1459 case TTY_PARITY:
1460 case TTY_FRAME:
1461 n_tty_receive_parity_error(tty, *p);
1462 break;
1463 case TTY_OVERRUN:
1464 n_tty_receive_overrun(tty);
1465 break;
1466 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001467 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 tty_name(tty, buf), flags);
1469 break;
1470 }
1471 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001472 if (tty->ops->flush_chars)
1473 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001476 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001477 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1479 if (waitqueue_active(&tty->read_wait))
1480 wake_up_interruptible(&tty->read_wait);
1481 }
1482
1483 /*
1484 * Check the remaining room for the input canonicalization
1485 * mode. We don't want to throttle the driver if we're in
1486 * canonical mode and don't have a newline yet!
1487 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001488 while (1) {
1489 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley24a89d12013-06-15 09:14:15 -04001490 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05001491 break;
1492 if (!tty_throttle_safe(tty))
1493 break;
1494 }
1495 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Peter Hurley24a89d12013-06-15 09:14:15 -04001498static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1499 char *fp, int count)
1500{
1501 __receive_buf(tty, cp, fp, count);
1502}
1503
1504static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1505 char *fp, int count)
1506{
1507 struct n_tty_data *ldata = tty->disc_data;
1508 int room;
1509
1510 tty->receive_room = room = receive_room(tty);
1511 if (!room)
1512 ldata->no_room = 1;
1513 count = min(count, room);
1514 if (count)
1515 __receive_buf(tty, cp, fp, count);
1516
1517 return count;
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520int is_ignored(int sig)
1521{
1522 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001523 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/**
1527 * n_tty_set_termios - termios data changed
1528 * @tty: terminal
1529 * @old: previous data
1530 *
1531 * Called by the tty layer when the user changes termios flags so
1532 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001533 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * guaranteed that this function will not be re-entered or in progress
1535 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001536 *
1537 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Alan Cox4edf1822008-02-08 04:18:44 -08001539
1540static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001542 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001543 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001544
1545 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001546 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001547 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001549 ldata->canon_head = ldata->read_tail;
1550 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001551 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001552 }
1553
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001554 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001555 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001556
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001557 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1560 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1561 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1562 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001566 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001568 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001571 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1572 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1573 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1574 set_bit('\n', ldata->process_char_map);
1575 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (L_IEXTEN(tty)) {
1577 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001578 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001580 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001582 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (L_ECHO(tty))
1584 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001585 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 }
1588 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001589 set_bit(START_CHAR(tty), ldata->process_char_map);
1590 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001593 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1594 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1595 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001597 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001598 ldata->raw = 0;
1599 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001601 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1603 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1604 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001605 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001607 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001609 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001610 /*
1611 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1612 * been stopped by STOP_CHAR(tty) before it.
1613 */
1614 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1615 start_tty(tty);
1616 }
1617
Alan Coxf34d7a52008-04-30 00:54:13 -07001618 /* The termios change make the tty ready for I/O */
1619 wake_up_interruptible(&tty->write_wait);
1620 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623/**
1624 * n_tty_close - close the ldisc for this tty
1625 * @tty: device
1626 *
Alan Cox4edf1822008-02-08 04:18:44 -08001627 * Called from the terminal layer when this line discipline is
1628 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * discipline change. The function will not be called while other
1630 * ldisc methods are in progress.
1631 */
Alan Cox4edf1822008-02-08 04:18:44 -08001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static void n_tty_close(struct tty_struct *tty)
1634{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001635 struct n_tty_data *ldata = tty->disc_data;
1636
Peter Hurley79901312013-03-11 16:44:23 -04001637 if (tty->link)
1638 n_tty_packet_mode_flush(tty);
1639
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001640 kfree(ldata->read_buf);
1641 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001642 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001643 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
1646/**
1647 * n_tty_open - open an ldisc
1648 * @tty: terminal to open
1649 *
Alan Cox4edf1822008-02-08 04:18:44 -08001650 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * terminal device. Can sleep. Called serialized so that no
1652 * other events will occur in parallel. No further open will occur
1653 * until a close.
1654 */
1655
1656static int n_tty_open(struct tty_struct *tty)
1657{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001658 struct n_tty_data *ldata;
1659
1660 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1661 if (!ldata)
1662 goto err;
1663
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001664 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001665 mutex_init(&ldata->atomic_read_lock);
1666 mutex_init(&ldata->output_lock);
1667 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001668 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001669
Joe Petersona88a69c2009-01-02 13:40:53 +00001670 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001671 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1672 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1673 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001674 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001675
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001676 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001677 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001678 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001679 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001681 /* indicate buffer work may resume */
1682 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1683 n_tty_set_termios(tty, NULL);
1684 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001687err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001688 kfree(ldata->read_buf);
1689 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001690 kfree(ldata);
1691err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001692 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695static inline int input_available_p(struct tty_struct *tty, int amt)
1696{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001697 struct n_tty_data *ldata = tty->disc_data;
1698
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001699 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001700 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001701 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001703 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 1;
1705
1706 return 0;
1707}
1708
1709/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001710 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * @tty: terminal device
1712 * @b: user data
1713 * @nr: size of data
1714 *
Alan Cox11a96d12008-10-13 10:46:24 +01001715 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * ICANON is off; it copies characters straight from the tty queue to
1717 * user space directly. It can be profitably called twice; once to
1718 * drain the space from the tail pointer to the (physical) end of the
1719 * buffer, and once to drain the space from the (physical) beginning of
1720 * the buffer to head pointer.
1721 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001722 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
1724 */
Alan Cox4edf1822008-02-08 04:18:44 -08001725
Alan Cox33f0f882006-01-09 20:54:13 -08001726static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 unsigned char __user **b,
1728 size_t *nr)
1729
1730{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001731 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int retval;
1733 size_t n;
1734 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001735 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001738 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001739 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001741 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001743 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001745 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001746 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1747 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001748 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001749 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001750 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1751 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001752 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001753 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001754 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001755 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 *b += n;
1757 *nr -= n;
1758 }
1759 return retval;
1760}
1761
Peter Hurley88bb0de2013-06-15 09:14:16 -04001762/**
Peter Hurley32f13522013-06-15 09:14:17 -04001763 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001764 * @tty: terminal device
1765 * @b: user data
1766 * @nr: size of data
1767 *
1768 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001769 * it copies one line of input up to and including the line-delimiting
1770 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001771 *
1772 * Called under the atomic_read_lock mutex
1773 */
1774
Peter Hurley32f13522013-06-15 09:14:17 -04001775static int canon_copy_from_read_buf(struct tty_struct *tty,
1776 unsigned char __user **b,
1777 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04001778{
1779 struct n_tty_data *ldata = tty->disc_data;
1780 unsigned long flags;
Peter Hurley32f13522013-06-15 09:14:17 -04001781 size_t n, size, more, c;
1782 unsigned long eol;
1783 int ret, tail, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001784
1785 /* N.B. avoid overrun if nr == 0 */
Peter Hurley32f13522013-06-15 09:14:17 -04001786
Peter Hurley88bb0de2013-06-15 09:14:16 -04001787 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001788
Peter Hurley32f13522013-06-15 09:14:17 -04001789 n = min_t(size_t, *nr, ldata->read_cnt);
1790 if (!n) {
Peter Hurley88bb0de2013-06-15 09:14:16 -04001791 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Peter Hurley32f13522013-06-15 09:14:17 -04001792 return 0;
1793 }
Peter Hurley88bb0de2013-06-15 09:14:16 -04001794
Peter Hurley32f13522013-06-15 09:14:17 -04001795 tail = ldata->read_tail;
1796 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1797
1798 n_tty_trace("%s: nr:%zu tail:%d n:%zu size:%zu\n",
1799 __func__, *nr, tail, n, size);
1800
1801 eol = find_next_bit(ldata->read_flags, size, tail);
1802 more = n - (size - tail);
1803 if (eol == N_TTY_BUF_SIZE && more) {
1804 /* scan wrapped without finding set bit */
1805 eol = find_next_bit(ldata->read_flags, more, 0);
1806 if (eol != more)
1807 found = 1;
1808 } else if (eol != size)
1809 found = 1;
1810
1811 size = N_TTY_BUF_SIZE - tail;
1812 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
1813 c = n;
1814
1815 if (found && ldata->read_buf[eol] == __DISABLED_CHAR)
1816 n--;
1817
1818 n_tty_trace("%s: eol:%lu found:%d n:%zu c:%zu size:%zu more:%zu\n",
1819 __func__, eol, found, n, c, size, more);
1820
1821 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1822
1823 if (n > size) {
1824 ret = copy_to_user(*b, &ldata->read_buf[tail], size);
1825 if (ret)
1826 return -EFAULT;
1827 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
1828 } else
1829 ret = copy_to_user(*b, &ldata->read_buf[tail], n);
1830
1831 if (ret)
1832 return -EFAULT;
1833 *b += n;
1834 *nr -= n;
1835
1836 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1837 ldata->read_tail = (ldata->read_tail + c) & (N_TTY_BUF_SIZE - 1);
1838 ldata->read_cnt -= c;
1839 if (found) {
1840 __clear_bit(eol, ldata->read_flags);
1841 /* this test should be redundant:
1842 * we shouldn't be reading data if
1843 * canon_data is 0
1844 */
1845 if (--ldata->canon_data < 0)
1846 ldata->canon_data = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001847 }
1848 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1849
Peter Hurley32f13522013-06-15 09:14:17 -04001850 if (found)
1851 tty_audit_push(tty);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001852 return 0;
1853}
1854
Al Virocc4191d2008-03-29 03:08:48 +00001855extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001856 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858/**
1859 * job_control - check job control
1860 * @tty: tty
1861 * @file: file handle
1862 *
1863 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001864 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001866 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001867 * Locking: redirected write test is safe
1868 * current->signal->tty check is safe
1869 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Alan Cox4edf1822008-02-08 04:18:44 -08001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872static int job_control(struct tty_struct *tty, struct file *file)
1873{
1874 /* Job control check -- must be done at start and after
1875 every sleep (POSIX.1 7.1.1.4). */
1876 /* NOTE: not yet done after every sleep pending a thorough
1877 check of the logic of this change. -- jlc */
1878 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001879 if (file->f_op->write == redirected_tty_write ||
1880 current->signal->tty != tty)
1881 return 0;
1882
1883 spin_lock_irq(&tty->ctrl_lock);
1884 if (!tty->pgrp)
1885 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1886 else if (task_pgrp(current) != tty->pgrp) {
1887 spin_unlock_irq(&tty->ctrl_lock);
1888 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1889 return -EIO;
1890 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1891 set_thread_flag(TIF_SIGPENDING);
1892 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001894 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return 0;
1896}
Alan Cox4edf1822008-02-08 04:18:44 -08001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899/**
Alan Cox11a96d12008-10-13 10:46:24 +01001900 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * @tty: tty device
1902 * @file: file object
1903 * @buf: userspace buffer pointer
1904 * @nr: size of I/O
1905 *
1906 * Perform reads for the line discipline. We are guaranteed that the
1907 * line discipline will not be closed under us but we may get multiple
1908 * parallel readers and must handle this ourselves. We may also get
1909 * a hangup. Always called in user context, may sleep.
1910 *
1911 * This code must be sure never to sleep through a hangup.
1912 */
Alan Cox4edf1822008-02-08 04:18:44 -08001913
Alan Cox11a96d12008-10-13 10:46:24 +01001914static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 unsigned char __user *buf, size_t nr)
1916{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001917 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned char __user *b = buf;
1919 DECLARE_WAITQUEUE(wait, current);
1920 int c;
1921 int minimum, time;
1922 ssize_t retval = 0;
1923 ssize_t size;
1924 long timeout;
1925 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001926 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001930 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 minimum = time = 0;
1934 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001935 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 minimum = MIN_CHAR(tty);
1937 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04001938 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001940 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001942 (ldata->minimum_to_wake > minimum))
1943 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04001945 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001946 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 }
1949
1950 /*
1951 * Internal serialization of reads.
1952 */
1953 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001954 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001956 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001957 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -ERESTARTSYS;
1959 }
Alan Cox04f378b2008-04-30 00:53:29 -07001960 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 while (nr) {
1964 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001965 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 unsigned char cs;
1967 if (b != buf)
1968 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001969 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 cs = tty->link->ctrl_status;
1971 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001972 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001973 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 retval = -EFAULT;
1975 b--;
1976 break;
1977 }
1978 nr--;
1979 break;
1980 }
1981 /* This statement must be first before checking for input
1982 so that any interrupt will set the state back to
1983 TASK_RUNNING. */
1984 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001985
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001986 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001988 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (!input_available_p(tty, 0)) {
1991 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1992 retval = -EIO;
1993 break;
1994 }
1995 if (tty_hung_up_p(file))
1996 break;
1997 if (!timeout)
1998 break;
1999 if (file->f_flags & O_NONBLOCK) {
2000 retval = -EAGAIN;
2001 break;
2002 }
2003 if (signal_pending(current)) {
2004 retval = -ERESTARTSYS;
2005 break;
2006 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09002007 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 continue;
2010 }
2011 __set_current_state(TASK_RUNNING);
2012
2013 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07002014 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07002015 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 retval = -EFAULT;
2017 b--;
2018 break;
2019 }
2020 nr--;
2021 }
2022
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002023 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002024 retval = canon_copy_from_read_buf(tty, &b, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (retval)
2026 break;
2027 } else {
2028 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07002029 /* The copy function takes the read lock and handles
2030 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 uncopied = copy_from_read_buf(tty, &b, &nr);
2032 uncopied += copy_from_read_buf(tty, &b, &nr);
2033 if (uncopied) {
2034 retval = -EFAULT;
2035 break;
2036 }
2037 }
2038
2039 /* If there is enough space in the read buffer now, let the
Peter Hurleya19d0c62013-06-15 09:14:18 -04002040 * low-level driver know. We use chars_in_buffer() to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 * check the buffer, as it now knows about canonical mode.
2042 * Otherwise, if the driver is throttled and the line is
2043 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
2044 * we won't get any more characters.
2045 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05002046 while (1) {
2047 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
Peter Hurleya19d0c62013-06-15 09:14:18 -04002048 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05002049 break;
2050 if (!tty->count)
2051 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002052 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05002053 if (!tty_unthrottle_safe(tty))
2054 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002055 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05002056 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 if (b - buf >= minimum)
2059 break;
2060 if (time)
2061 timeout = time;
2062 }
Jiri Slabybddc7152012-10-18 22:26:42 +02002063 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 remove_wait_queue(&tty->read_wait, &wait);
2065
2066 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002067 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 __set_current_state(TASK_RUNNING);
2070 size = b - buf;
2071 if (size) {
2072 retval = size;
2073 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08002074 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01002076 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Linus Torvalds55db4c62011-06-04 06:33:24 +09002078 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return retval;
2080}
2081
2082/**
Alan Cox11a96d12008-10-13 10:46:24 +01002083 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * @tty: tty device
2085 * @file: file object
2086 * @buf: userspace buffer pointer
2087 * @nr: size of I/O
2088 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002089 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002091 * and other such events. Since the receive code will echo characters,
2092 * thus calling driver write methods, the output_lock is used in
2093 * the output processing functions called here as well as in the
2094 * echo processing function to protect the column state and space
2095 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *
2097 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002098 *
2099 * Locking: output_lock to protect column state and space left
2100 * (note that the process_output*() functions take this
2101 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
Alan Cox4edf1822008-02-08 04:18:44 -08002103
Alan Cox11a96d12008-10-13 10:46:24 +01002104static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002105 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 const unsigned char *b = buf;
2108 DECLARE_WAITQUEUE(wait, current);
2109 int c;
2110 ssize_t retval = 0;
2111
2112 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2113 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2114 retval = tty_check_change(tty);
2115 if (retval)
2116 return retval;
2117 }
2118
Joe Petersona88a69c2009-01-02 13:40:53 +00002119 /* Write out any echoed characters that are still pending */
2120 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 add_wait_queue(&tty->write_wait, &wait);
2123 while (1) {
2124 set_current_state(TASK_INTERRUPTIBLE);
2125 if (signal_pending(current)) {
2126 retval = -ERESTARTSYS;
2127 break;
2128 }
2129 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2130 retval = -EIO;
2131 break;
2132 }
Peter Hurley582f5592013-05-17 12:49:48 -04002133 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002135 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (num < 0) {
2137 if (num == -EAGAIN)
2138 break;
2139 retval = num;
2140 goto break_out;
2141 }
2142 b += num;
2143 nr -= num;
2144 if (nr == 0)
2145 break;
2146 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002147 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 break;
2149 b++; nr--;
2150 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002151 if (tty->ops->flush_chars)
2152 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002154 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002155 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002156 if (c < 0) {
2157 retval = c;
2158 goto break_out;
2159 }
2160 if (!c)
2161 break;
2162 b += c;
2163 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 if (!nr)
2167 break;
2168 if (file->f_flags & O_NONBLOCK) {
2169 retval = -EAGAIN;
2170 break;
2171 }
2172 schedule();
2173 }
2174break_out:
2175 __set_current_state(TASK_RUNNING);
2176 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002177 if (b - buf != nr && tty->fasync)
2178 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return (b - buf) ? b - buf : retval;
2180}
2181
2182/**
Alan Cox11a96d12008-10-13 10:46:24 +01002183 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * @tty: terminal device
2185 * @file: file accessing it
2186 * @wait: poll table
2187 *
2188 * Called when the line discipline is asked to poll() for data or
2189 * for special events. This code is not serialized with respect to
2190 * other events save open/close.
2191 *
2192 * This code must be sure never to sleep through a hangup.
2193 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
Alan Cox4edf1822008-02-08 04:18:44 -08002195
Alan Cox11a96d12008-10-13 10:46:24 +01002196static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002197 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002199 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 unsigned int mask = 0;
2201
2202 poll_wait(file, &tty->read_wait, wait);
2203 poll_wait(file, &tty->write_wait, wait);
2204 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2205 mask |= POLLIN | POLLRDNORM;
2206 if (tty->packet && tty->link->ctrl_status)
2207 mask |= POLLPRI | POLLIN | POLLRDNORM;
2208 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2209 mask |= POLLHUP;
2210 if (tty_hung_up_p(file))
2211 mask |= POLLHUP;
2212 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2213 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002214 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002216 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002218 if (tty->ops->write && !tty_is_writelocked(tty) &&
2219 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2220 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 mask |= POLLOUT | POLLWRNORM;
2222 return mask;
2223}
2224
Jiri Slaby57c94122012-10-18 22:26:43 +02002225static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002226{
2227 int nr, head, tail;
2228
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002229 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002230 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002231 head = ldata->canon_head;
2232 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002233 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2234 /* Skip EOF-chars.. */
2235 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002236 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002237 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002238 nr--;
2239 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2240 }
2241 return nr;
2242}
2243
2244static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2245 unsigned int cmd, unsigned long arg)
2246{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002247 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002248 int retval;
2249
2250 switch (cmd) {
2251 case TIOCOUTQ:
2252 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2253 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002254 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002255 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002256 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002257 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002258 return put_user(retval, (unsigned int __user *) arg);
2259 default:
2260 return n_tty_ioctl_helper(tty, file, cmd, arg);
2261 }
2262}
2263
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002264static void n_tty_fasync(struct tty_struct *tty, int on)
2265{
2266 struct n_tty_data *ldata = tty->disc_data;
2267
2268 if (!waitqueue_active(&tty->read_wait)) {
2269 if (on)
2270 ldata->minimum_to_wake = 1;
2271 else if (!tty->fasync)
2272 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2273 }
2274}
2275
Alan Coxa352def2008-07-16 21:53:12 +01002276struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002277 .magic = TTY_LDISC_MAGIC,
2278 .name = "n_tty",
2279 .open = n_tty_open,
2280 .close = n_tty_close,
2281 .flush_buffer = n_tty_flush_buffer,
2282 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002283 .read = n_tty_read,
2284 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002285 .ioctl = n_tty_ioctl,
2286 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002287 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002288 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002289 .write_wakeup = n_tty_write_wakeup,
2290 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002291 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002293
2294/**
2295 * n_tty_inherit_ops - inherit N_TTY methods
2296 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2297 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002298 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002299 * methods.
2300 */
2301
2302void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2303{
2304 *ops = tty_ldisc_N_TTY;
2305 ops->owner = NULL;
2306 ops->refcount = ops->flags = 0;
2307}
2308EXPORT_SYMBOL_GPL(n_tty_inherit_ops);