blob: 371612b42238c39e56c304c137291f90d5c37898 [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
Peter Hurleyce741172013-06-15 09:14:20 -0400118static inline size_t read_cnt(struct n_tty_data *ldata)
119{
120 return ldata->read_cnt;
121}
122
Miloslav Trmac522ed772007-07-15 23:40:56 -0700123static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
124 unsigned char __user *ptr)
125{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200126 struct n_tty_data *ldata = tty->disc_data;
127
128 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700129 return put_user(x, ptr);
130}
131
Peter Hurley24a89d12013-06-15 09:14:15 -0400132static int receive_room(struct tty_struct *tty)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900133{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200134 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500135 int left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400136
Jaeden Amero090abf72012-07-27 08:43:11 -0500137 if (I_PARMRK(tty)) {
138 /* Multiply read_cnt by 3, since each byte might take up to
139 * three times as many spaces when PARMRK is set (depending on
140 * its flags, e.g. parity error). */
Peter Hurleyce741172013-06-15 09:14:20 -0400141 left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500142 } else
Peter Hurleyce741172013-06-15 09:14:20 -0400143 left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500144
Linus Torvalds55db4c62011-06-04 06:33:24 +0900145 /*
146 * If we are doing input canonicalization, and there are no
147 * pending newlines, let characters through without limit, so
148 * that erase characters will be handled. Other excess
149 * characters will be beeped.
150 */
151 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200152 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900153
Peter Hurley24a89d12013-06-15 09:14:15 -0400154 return left;
Peter Hurley7879a9f2013-06-15 07:28:31 -0400155}
156
Peter Hurley24a89d12013-06-15 09:14:15 -0400157/**
158 * n_tty_set_room - receive space
159 * @tty: terminal
160 *
161 * Re-schedules the flip buffer work if space just became available.
162 *
163 * Locks: Concurrent update is protected with read_lock
164 */
165
Peter Hurley7879a9f2013-06-15 07:28:31 -0400166static void n_tty_set_room(struct tty_struct *tty)
167{
Peter Hurley24a89d12013-06-15 09:14:15 -0400168 struct n_tty_data *ldata = tty->disc_data;
169
Linus Torvalds55db4c62011-06-04 06:33:24 +0900170 /* Did this open up the receive buffer? We may need to flip */
Peter Hurley24a89d12013-06-15 09:14:15 -0400171 if (unlikely(ldata->no_room) && receive_room(tty)) {
172 ldata->no_room = 0;
173
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200174 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400175 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400176 /* see if ldisc has been killed - if so, this means that
177 * even though the ldisc has been halted and ->buf.work
178 * cancelled, ->buf.work is about to be rescheduled
179 */
180 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
181 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200182 schedule_work(&tty->port->buf.work);
183 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900184}
185
Jiri Slaby57c94122012-10-18 22:26:43 +0200186static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Peter Hurleyce741172013-06-15 09:14:20 -0400188 if (read_cnt(ldata) < N_TTY_BUF_SIZE) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200189 ldata->read_buf[ldata->read_head] = c;
190 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
191 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193}
194
Alan Cox17b82062008-10-13 10:45:06 +0100195/**
196 * put_tty_queue - add character to tty
197 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200198 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100199 *
200 * Add a character to the tty read_buf queue. This is done under the
201 * read_lock to serialize character addition and also to protect us
202 * against parallel reads or flushes
203 */
204
Jiri Slaby57c94122012-10-18 22:26:43 +0200205static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 unsigned long flags;
208 /*
209 * The problem of stomping on the buffers ends here.
210 * Why didn't anyone see this one coming? --AJK
211 */
Ivo Sieben98001212013-01-28 13:32:01 +0100212 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200213 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100214 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * reset_buffer_flags - reset buffer state
219 * @tty: terminal to reset
220 *
Peter Hurley25518c62013-03-11 16:44:31 -0400221 * Reset the read buffer counters and clear the flags.
222 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100223 *
224 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000226
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400227static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned long flags;
230
Ivo Sieben98001212013-01-28 13:32:01 +0100231 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200232 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100233 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000234
Jiri Slabybddc7152012-10-18 22:26:42 +0200235 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200236 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200237 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000238
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200239 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200240 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Peter Hurleya30737a2013-03-11 16:44:22 -0400243static void n_tty_packet_mode_flush(struct tty_struct *tty)
244{
245 unsigned long flags;
246
247 spin_lock_irqsave(&tty->ctrl_lock, flags);
248 if (tty->link->packet) {
249 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
250 wake_up_interruptible(&tty->link->read_wait);
251 }
252 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/**
256 * n_tty_flush_buffer - clean input queue
257 * @tty: terminal device
258 *
Peter Hurley25518c62013-03-11 16:44:31 -0400259 * Flush the input buffer. Called when the tty layer wants the
260 * buffer flushed (eg at hangup) or when the N_TTY line discipline
261 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
Alan Cox17b82062008-10-13 10:45:06 +0100263 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Alan Cox4edf1822008-02-08 04:18:44 -0800265
266static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400268 reset_buffer_flags(tty->disc_data);
269 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800270
Peter Hurleya30737a2013-03-11 16:44:22 -0400271 if (tty->link)
272 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/**
276 * n_tty_chars_in_buffer - report available bytes
277 * @tty: tty device
278 *
279 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800280 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100281 *
282 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Alan Cox4edf1822008-02-08 04:18:44 -0800284
Peter Hurleya19d0c62013-06-15 09:14:18 -0400285static ssize_t chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200287 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned long flags;
289 ssize_t n = 0;
290
Ivo Sieben98001212013-01-28 13:32:01 +0100291 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200292 if (!ldata->icanon) {
Peter Hurleyce741172013-06-15 09:14:20 -0400293 n = read_cnt(ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200294 } else if (ldata->canon_data) {
295 n = (ldata->canon_head > ldata->read_tail) ?
296 ldata->canon_head - ldata->read_tail :
297 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Ivo Sieben98001212013-01-28 13:32:01 +0100299 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return n;
301}
302
Peter Hurleya19d0c62013-06-15 09:14:18 -0400303static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
304{
Peter Hurley47534082013-06-15 09:14:19 -0400305 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
Peter Hurleya19d0c62013-06-15 09:14:18 -0400306 return chars_in_buffer(tty);
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/**
310 * is_utf8_continuation - utf8 multibyte check
311 * @c: byte to check
312 *
313 * Returns true if the utf8 character 'c' is a multibyte continuation
314 * character. We use this to correctly compute the on screen size
315 * of the character when printing
316 */
Alan Cox4edf1822008-02-08 04:18:44 -0800317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static inline int is_utf8_continuation(unsigned char c)
319{
320 return (c & 0xc0) == 0x80;
321}
322
323/**
324 * is_continuation - multibyte check
325 * @c: byte to check
326 *
327 * Returns true if the utf8 character 'c' is a multibyte continuation
328 * character and the terminal is in unicode mode.
329 */
Alan Cox4edf1822008-02-08 04:18:44 -0800330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static inline int is_continuation(unsigned char c, struct tty_struct *tty)
332{
333 return I_IUTF8(tty) && is_utf8_continuation(c);
334}
335
336/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000337 * do_output_char - output one character
338 * @c: character (or partial unicode symbol)
339 * @tty: terminal device
340 * @space: space available in tty driver write buffer
341 *
342 * This is a helper function that handles one output character
343 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600344 * doing OPOST processing and putting the results in the
345 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000346 *
347 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
348 * and NLDLY. They simply aren't relevant in the world today.
349 * If you ever need them, add them here.
350 *
351 * Returns the number of bytes of buffer space used or -1 if
352 * no space left.
353 *
354 * Locking: should be called under the output_lock to protect
355 * the column state and space left in the buffer
356 */
357
358static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
359{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200360 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000361 int spaces;
362
363 if (!space)
364 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000365
Joe Petersona88a69c2009-01-02 13:40:53 +0000366 switch (c) {
367 case '\n':
368 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200369 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000370 if (O_ONLCR(tty)) {
371 if (space < 2)
372 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200373 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700374 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 return 2;
376 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200377 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 break;
379 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200380 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000381 return 0;
382 if (O_OCRNL(tty)) {
383 c = '\n';
384 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200385 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 break;
387 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200388 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000389 break;
390 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200391 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000392 if (O_TABDLY(tty) == XTABS) {
393 if (space < spaces)
394 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200395 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000396 tty->ops->write(tty, " ", spaces);
397 return spaces;
398 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200399 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000400 break;
401 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200402 if (ldata->column > 0)
403 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000404 break;
405 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000406 if (!iscntrl(c)) {
407 if (O_OLCUC(tty))
408 c = toupper(c);
409 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200410 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000411 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000412 break;
413 }
414
415 tty_put_char(tty, c);
416 return 1;
417}
418
419/**
420 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * @c: character (or partial unicode symbol)
422 * @tty: terminal device
423 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600424 * Output one character with OPOST processing.
425 * Returns -1 when the output device is full and the character
426 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000428 * Locking: output_lock to protect column state and space left
429 * (also, this is called from n_tty_write under the
430 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Alan Cox4edf1822008-02-08 04:18:44 -0800432
Joe Petersona88a69c2009-01-02 13:40:53 +0000433static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jiri Slabybddc7152012-10-18 22:26:42 +0200435 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000436 int space, retval;
437
Jiri Slabybddc7152012-10-18 22:26:42 +0200438 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alan Coxf34d7a52008-04-30 00:54:13 -0700440 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000441 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jiri Slabybddc7152012-10-18 22:26:42 +0200443 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000444 if (retval < 0)
445 return -1;
446 else
447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000451 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600453 * @buf: character buffer
454 * @nr: number of bytes to output
455 *
456 * Output a block of characters with OPOST processing.
457 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
459 * This path is used to speed up block console writes, among other
460 * things when processing blocks of output data. It handles only
461 * the simple cases normally found and helps to generate blocks of
462 * symbols for the console driver and thus improve performance.
463 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000464 * Locking: output_lock to protect column state and space left
465 * (also, this is called from n_tty_write under the
466 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
Alan Cox4edf1822008-02-08 04:18:44 -0800468
Joe Petersona88a69c2009-01-02 13:40:53 +0000469static ssize_t process_output_block(struct tty_struct *tty,
470 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200472 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100474 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 const unsigned char *cp;
476
Jiri Slabybddc7152012-10-18 22:26:42 +0200477 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000478
Alan Coxf34d7a52008-04-30 00:54:13 -0700479 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000480 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200481 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (nr > space)
485 nr = space;
486
487 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000488 unsigned char c = *cp;
489
490 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 case '\n':
492 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200493 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (O_ONLCR(tty))
495 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200496 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200499 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto break_out;
501 if (O_OCRNL(tty))
502 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200503 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case '\t':
506 goto break_out;
507 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200508 if (ldata->column > 0)
509 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000512 if (!iscntrl(c)) {
513 if (O_OLCUC(tty))
514 goto break_out;
515 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200516 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519 }
520 }
521break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700522 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000523
Jiri Slabybddc7152012-10-18 22:26:42 +0200524 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return i;
526}
527
Joe Petersona88a69c2009-01-02 13:40:53 +0000528/**
529 * process_echoes - write pending echo characters
530 * @tty: terminal device
531 *
532 * Write previously buffered echo (and other ldisc-generated)
533 * characters to the tty.
534 *
535 * Characters generated by the ldisc (including echoes) need to
536 * be buffered because the driver's write buffer can fill during
537 * heavy program output. Echoing straight to the driver will
538 * often fail under these conditions, causing lost characters and
539 * resulting mismatches of ldisc state information.
540 *
541 * Since the ldisc state must represent the characters actually sent
542 * to the driver at the time of the write, operations like certain
543 * changes in column state are also saved in the buffer and executed
544 * here.
545 *
546 * A circular fifo buffer is used so that the most recent characters
547 * are prioritized. Also, when control characters are echoed with a
548 * prefixed "^", the pair is treated atomically and thus not separated.
549 *
550 * Locking: output_lock to protect column state and space left,
551 * echo_lock to protect the echo buffer
552 */
553
554static void process_echoes(struct tty_struct *tty)
555{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200556 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000557 int space, nr;
558 unsigned char c;
559 unsigned char *cp, *buf_end;
560
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200561 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000562 return;
563
Jiri Slabybddc7152012-10-18 22:26:42 +0200564 mutex_lock(&ldata->output_lock);
565 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000566
567 space = tty_write_room(tty);
568
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200569 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
570 cp = ldata->echo_buf + ldata->echo_pos;
571 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000572 while (nr > 0) {
573 c = *cp;
574 if (c == ECHO_OP_START) {
575 unsigned char op;
576 unsigned char *opp;
577 int no_space_left = 0;
578
579 /*
580 * If the buffer byte is the start of a multi-byte
581 * operation, get the next byte, which is either the
582 * op code or a control character value.
583 */
584 opp = cp + 1;
585 if (opp == buf_end)
586 opp -= N_TTY_BUF_SIZE;
587 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000588
Joe Petersona88a69c2009-01-02 13:40:53 +0000589 switch (op) {
590 unsigned int num_chars, num_bs;
591
592 case ECHO_OP_ERASE_TAB:
593 if (++opp == buf_end)
594 opp -= N_TTY_BUF_SIZE;
595 num_chars = *opp;
596
597 /*
598 * Determine how many columns to go back
599 * in order to erase the tab.
600 * This depends on the number of columns
601 * used by other characters within the tab
602 * area. If this (modulo 8) count is from
603 * the start of input rather than from a
604 * previous tab, we offset by canon column.
605 * Otherwise, tab spacing is normal.
606 */
607 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200608 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 num_bs = 8 - (num_chars & 7);
610
611 if (num_bs > space) {
612 no_space_left = 1;
613 break;
614 }
615 space -= num_bs;
616 while (num_bs--) {
617 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200618 if (ldata->column > 0)
619 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000620 }
621 cp += 3;
622 nr -= 3;
623 break;
624
625 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200626 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000627 cp += 2;
628 nr -= 2;
629 break;
630
631 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200632 if (ldata->column > 0)
633 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000634 cp += 2;
635 nr -= 2;
636 break;
637
638 case ECHO_OP_START:
639 /* This is an escaped echo op start code */
640 if (!space) {
641 no_space_left = 1;
642 break;
643 }
644 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200645 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000646 space--;
647 cp += 2;
648 nr -= 2;
649 break;
650
651 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000652 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600653 * If the op is not a special byte code,
654 * it is a ctrl char tagged to be echoed
655 * as "^X" (where X is the letter
656 * representing the control char).
657 * Note that we must ensure there is
658 * enough space for the whole ctrl pair.
659 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000660 */
Joe Peterson62b26352009-09-09 15:03:47 -0600661 if (space < 2) {
662 no_space_left = 1;
663 break;
664 }
665 tty_put_char(tty, '^');
666 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200667 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600668 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000669 cp += 2;
670 nr -= 2;
671 }
672
673 if (no_space_left)
674 break;
675 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400676 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600677 int retval = do_output_char(c, tty, space);
678 if (retval < 0)
679 break;
680 space -= retval;
681 } else {
682 if (!space)
683 break;
684 tty_put_char(tty, c);
685 space -= 1;
686 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 cp += 1;
688 nr -= 1;
689 }
690
691 /* When end of circular buffer reached, wrap around */
692 if (cp >= buf_end)
693 cp -= N_TTY_BUF_SIZE;
694 }
695
696 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200697 ldata->echo_pos = 0;
698 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200699 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200701 int num_processed = ldata->echo_cnt - nr;
702 ldata->echo_pos += num_processed;
703 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
704 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000705 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200706 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000707 }
708
Jiri Slabybddc7152012-10-18 22:26:42 +0200709 mutex_unlock(&ldata->echo_lock);
710 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000711
712 if (tty->ops->flush_chars)
713 tty->ops->flush_chars(tty);
714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000717 * add_echo_byte - add a byte to the echo buffer
718 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200719 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 *
721 * Add a character or operation byte to the echo buffer.
722 *
723 * Should be called under the echo lock to protect the echo buffer.
724 */
725
Jiri Slaby57c94122012-10-18 22:26:43 +0200726static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000727{
728 int new_byte_pos;
729
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200730 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000731 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200732 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000733
734 /*
735 * Since the buffer start position needs to be advanced,
736 * be sure to step by a whole operation byte group.
737 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200738 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
739 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000740 (N_TTY_BUF_SIZE - 1)] ==
741 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200742 ldata->echo_pos += 3;
743 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000744 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200745 ldata->echo_pos += 2;
746 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000747 }
748 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200749 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000750 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200751 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000752
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200753 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000754 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200755 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000756 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200757 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000758 }
759
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200760 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000761}
762
763/**
764 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200765 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000766 *
767 * Add an operation to the echo buffer to move back one column.
768 *
769 * Locking: echo_lock to protect the echo buffer
770 */
771
Jiri Slaby57c94122012-10-18 22:26:43 +0200772static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000773{
Jiri Slabybddc7152012-10-18 22:26:42 +0200774 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200775 add_echo_byte(ECHO_OP_START, ldata);
776 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200777 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000778}
779
780/**
781 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200782 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000783 *
784 * Add an operation to the echo buffer to set the canon column
785 * to the current column.
786 *
787 * Locking: echo_lock to protect the echo buffer
788 */
789
Jiri Slaby57c94122012-10-18 22:26:43 +0200790static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000791{
Jiri Slabybddc7152012-10-18 22:26:42 +0200792 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200793 add_echo_byte(ECHO_OP_START, ldata);
794 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200795 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000796}
797
798/**
799 * echo_erase_tab - add operation to erase a tab
800 * @num_chars: number of character columns already used
801 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200802 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000803 *
804 * Add an operation to the echo buffer to erase a tab.
805 *
806 * Called by the eraser function, which knows how many character
807 * columns have been used since either a previous tab or the start
808 * of input. This information will be used later, along with
809 * canon column (if applicable), to go back the correct number
810 * of columns.
811 *
812 * Locking: echo_lock to protect the echo buffer
813 */
814
815static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200816 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000817{
Jiri Slabybddc7152012-10-18 22:26:42 +0200818 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000819
Jiri Slaby57c94122012-10-18 22:26:43 +0200820 add_echo_byte(ECHO_OP_START, ldata);
821 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000822
823 /* We only need to know this modulo 8 (tab spacing) */
824 num_chars &= 7;
825
826 /* Set the high bit as a flag if num_chars is after a previous tab */
827 if (after_tab)
828 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000829
Jiri Slaby57c94122012-10-18 22:26:43 +0200830 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000831
Jiri Slabybddc7152012-10-18 22:26:42 +0200832 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000833}
834
835/**
836 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * @c: unicode byte to echo
838 * @tty: terminal device
839 *
Alan Cox4edf1822008-02-08 04:18:44 -0800840 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100842 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000843 * This variant does not treat control characters specially.
844 *
845 * Locking: echo_lock to protect the echo buffer
846 */
847
Jiri Slaby57c94122012-10-18 22:26:43 +0200848static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000849{
Jiri Slabybddc7152012-10-18 22:26:42 +0200850 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000851 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200852 add_echo_byte(ECHO_OP_START, ldata);
853 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000854 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200855 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000856 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200857 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858}
859
860/**
861 * echo_char - echo a character
862 * @c: unicode byte to echo
863 * @tty: terminal device
864 *
865 * Echo user input back onto the screen. This must be called only when
866 * L_ECHO(tty) is true. Called from the driver receive_buf path.
867 *
Joe Peterson62b26352009-09-09 15:03:47 -0600868 * This variant tags control characters to be echoed as "^X"
869 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000870 *
871 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
873
874static void echo_char(unsigned char c, struct tty_struct *tty)
875{
Jiri Slabybddc7152012-10-18 22:26:42 +0200876 struct n_tty_data *ldata = tty->disc_data;
877
878 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000879
880 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200881 add_echo_byte(ECHO_OP_START, ldata);
882 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000883 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600884 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200885 add_echo_byte(ECHO_OP_START, ldata);
886 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000887 }
888
Jiri Slabybddc7152012-10-18 22:26:42 +0200889 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Alan Cox17b82062008-10-13 10:45:06 +0100892/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000893 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200894 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100895 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000896
Jiri Slaby57c94122012-10-18 22:26:43 +0200897static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200899 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200900 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200901 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
905/**
906 * eraser - handle erase function
907 * @c: character input
908 * @tty: terminal device
909 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200910 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * present in the stream from the driver layer. Handles the complexities
912 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100913 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000914 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Alan Cox4edf1822008-02-08 04:18:44 -0800916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static void eraser(unsigned char c, struct tty_struct *tty)
918{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200919 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 enum { ERASE, WERASE, KILL } kill_type;
921 int head, seen_alnums, cnt;
922 unsigned long flags;
923
Alan Cox17b82062008-10-13 10:45:06 +0100924 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200925 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000926 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return;
928 }
929 if (c == ERASE_CHAR(tty))
930 kill_type = ERASE;
931 else if (c == WERASE_CHAR(tty))
932 kill_type = WERASE;
933 else {
934 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100935 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200936 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200938 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return;
941 }
942 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100943 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200944 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200946 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100947 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200948 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 echo_char(KILL_CHAR(tty), tty);
950 /* Add a newline if ECHOK is on and ECHOKE is off. */
951 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200952 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return;
954 }
955 kill_type = KILL;
956 }
957
958 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100959 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200960 while (ldata->read_head != ldata->canon_head) {
961 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* erase a single possibly multibyte character */
964 do {
965 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200966 c = ldata->read_buf[head];
967 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* do not partially erase */
970 if (is_continuation(c, tty))
971 break;
972
973 if (kill_type == WERASE) {
974 /* Equivalent to BSD's ALTWERASE. */
975 if (isalnum(c) || c == '_')
976 seen_alnums++;
977 else if (seen_alnums)
978 break;
979 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200980 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100981 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200982 ldata->read_head = head;
983 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100984 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (L_ECHO(tty)) {
986 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200987 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200988 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200989 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 /* if cnt > 1, output a multi-byte character */
992 echo_char(c, tty);
993 while (--cnt > 0) {
994 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200995 echo_char_raw(ldata->read_buf[head],
996 ldata);
997 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1000 echo_char(ERASE_CHAR(tty), tty);
1001 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001002 unsigned int num_chars = 0;
1003 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001004 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Joe Petersona88a69c2009-01-02 13:40:53 +00001006 /*
1007 * Count the columns used for characters
1008 * since the start of input or after a
1009 * previous tab.
1010 * This info is used to go back the correct
1011 * number of columns.
1012 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001013 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001014 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001015 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +00001016 if (c == '\t') {
1017 after_tab = 1;
1018 break;
Alan Cox300a6202009-01-02 13:41:04 +00001019 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001021 num_chars += 2;
1022 } else if (!is_continuation(c, tty)) {
1023 num_chars++;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001026 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
1028 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001029 echo_char_raw('\b', ldata);
1030 echo_char_raw(' ', ldata);
1031 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001034 echo_char_raw('\b', ldata);
1035 echo_char_raw(' ', ldata);
1036 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 }
1039 }
1040 if (kill_type == ERASE)
1041 break;
1042 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001043 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001044 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/**
1048 * isig - handle the ISIG optio
1049 * @sig: signal
1050 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001052 * Called when a signal is being sent due to terminal input.
1053 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001054 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001055 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
Alan Cox4edf1822008-02-08 04:18:44 -08001057
Peter Hurley8c985d12013-03-06 08:38:19 -05001058static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Peter Hurley8c985d12013-03-06 08:38:19 -05001060 struct pid *tty_pgrp = tty_get_pgrp(tty);
1061 if (tty_pgrp) {
1062 kill_pgrp(tty_pgrp, sig, 1);
1063 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065}
1066
1067/**
1068 * n_tty_receive_break - handle break
1069 * @tty: terminal
1070 *
1071 * An RS232 break event has been hit in the incoming bitstream. This
1072 * can cause a variety of events depending upon the termios settings.
1073 *
1074 * Called from the receive_buf path so single threaded.
1075 */
Alan Cox4edf1822008-02-08 04:18:44 -08001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static inline void n_tty_receive_break(struct tty_struct *tty)
1078{
Jiri Slaby57c94122012-10-18 22:26:43 +02001079 struct n_tty_data *ldata = tty->disc_data;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (I_IGNBRK(tty))
1082 return;
1083 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001084 isig(SIGINT, tty);
1085 if (!L_NOFLSH(tty)) {
1086 n_tty_flush_buffer(tty);
1087 tty_driver_flush_buffer(tty);
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return;
1090 }
1091 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001092 put_tty_queue('\377', ldata);
1093 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001095 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 wake_up_interruptible(&tty->read_wait);
1097}
1098
1099/**
1100 * n_tty_receive_overrun - handle overrun reporting
1101 * @tty: terminal
1102 *
1103 * Data arrived faster than we could process it. While the tty
1104 * driver has flagged this the bits that were missed are gone
1105 * forever.
1106 *
1107 * Called from the receive_buf path so single threaded. Does not
1108 * need locking as num_overrun and overrun_time are function
1109 * private.
1110 */
Alan Cox4edf1822008-02-08 04:18:44 -08001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112static inline void n_tty_receive_overrun(struct tty_struct *tty)
1113{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001114 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 char buf[64];
1116
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001117 ldata->num_overrun++;
1118 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1119 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1121 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001122 ldata->num_overrun);
1123 ldata->overrun_time = jiffies;
1124 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126}
1127
1128/**
1129 * n_tty_receive_parity_error - error notifier
1130 * @tty: terminal device
1131 * @c: character
1132 *
1133 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001134 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
1136static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1137 unsigned char c)
1138{
Jiri Slaby57c94122012-10-18 22:26:43 +02001139 struct n_tty_data *ldata = tty->disc_data;
1140
Alan Cox4edf1822008-02-08 04:18:44 -08001141 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001144 put_tty_queue('\377', ldata);
1145 put_tty_queue('\0', ldata);
1146 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001148 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001150 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 wake_up_interruptible(&tty->read_wait);
1152}
1153
1154/**
1155 * n_tty_receive_char - perform processing
1156 * @tty: terminal device
1157 * @c: character
1158 *
1159 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001160 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * driver above.
1162 */
1163
1164static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1165{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001166 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001168 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001170 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001171 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return;
1173 }
Alan Cox4edf1822008-02-08 04:18:44 -08001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (I_ISTRIP(tty))
1176 c &= 0x7f;
1177 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001178 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
hyc@symas.com26df6d12010-06-22 10:14:49 -07001180 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001181 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001182 return;
1183 }
1184
Joe Peterson54d2a372008-02-06 01:37:59 -08001185 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001186 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1187 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001188 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001189 process_echoes(tty);
1190 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (tty->closing) {
1193 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001194 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001196 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001197 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 stop_tty(tty);
1199 }
1200 return;
1201 }
1202
1203 /*
1204 * If the previous character was LNEXT, or we know that this
1205 * character is not one of the characters that we'll have to
1206 * handle specially, do shortcut processing to speed things
1207 * up.
1208 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001209 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001210 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001211 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001212 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001213 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001214 if (L_ECHO(tty))
1215 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001216 return;
1217 }
1218 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001219 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001221 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001222 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001224 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001226 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001227 put_tty_queue(c, ldata);
1228 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return;
1230 }
Alan Cox4edf1822008-02-08 04:18:44 -08001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (I_IXON(tty)) {
1233 if (c == START_CHAR(tty)) {
1234 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001235 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
1237 }
1238 if (c == STOP_CHAR(tty)) {
1239 stop_tty(tty);
1240 return;
1241 }
1242 }
Joe Peterson575537b32008-04-30 00:53:30 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (L_ISIG(tty)) {
1245 int signal;
1246 signal = SIGINT;
1247 if (c == INTR_CHAR(tty))
1248 goto send_signal;
1249 signal = SIGQUIT;
1250 if (c == QUIT_CHAR(tty))
1251 goto send_signal;
1252 signal = SIGTSTP;
1253 if (c == SUSP_CHAR(tty)) {
1254send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001255 if (!L_NOFLSH(tty)) {
1256 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001257 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001258 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001259 if (I_IXON(tty))
1260 start_tty(tty);
1261 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001262 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001263 process_echoes(tty);
1264 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001265 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
1267 }
1268 }
Joe Peterson575537b32008-04-30 00:53:30 -07001269
1270 if (c == '\r') {
1271 if (I_IGNCR(tty))
1272 return;
1273 if (I_ICRNL(tty))
1274 c = '\n';
1275 } else if (c == '\n' && I_INLCR(tty))
1276 c = '\r';
1277
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001278 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1280 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1281 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001282 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return;
1284 }
1285 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001286 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001288 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001290 echo_char_raw('^', ldata);
1291 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001292 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
1295 return;
1296 }
1297 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1298 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001299 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jiri Slaby57c94122012-10-18 22:26:43 +02001301 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001303 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001304 while (tail != ldata->read_head) {
1305 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1307 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001308 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return;
1310 }
1311 if (c == '\n') {
Peter Hurleyce741172013-06-15 09:14:20 -04001312 if (read_cnt(ldata) >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001313 if (L_ECHO(tty))
1314 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001315 return;
1316 }
1317 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001318 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001319 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 goto handle_newline;
1322 }
1323 if (c == EOF_CHAR(tty)) {
Peter Hurleyce741172013-06-15 09:14:20 -04001324 if (read_cnt(ldata) >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001325 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001326 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001327 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 c = __DISABLED_CHAR;
1329 goto handle_newline;
1330 }
1331 if ((c == EOL_CHAR(tty)) ||
1332 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001333 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1334 ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001335 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001336 if (L_ECHO(tty))
1337 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001338 return;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1342 */
1343 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001345 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001346 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001348 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 /*
1351 * XXX does PARMRK doubling happen for
1352 * EOL_CHAR and EOL2_CHAR?
1353 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001354 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001355 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Alan Cox4edf1822008-02-08 04:18:44 -08001357handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001358 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001359 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001360 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001361 ldata->canon_head = ldata->read_head;
1362 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001363 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1365 if (waitqueue_active(&tty->read_wait))
1366 wake_up_interruptible(&tty->read_wait);
1367 return;
1368 }
1369 }
Alan Cox4edf1822008-02-08 04:18:44 -08001370
Joe Petersonacc71bb2009-01-02 13:43:32 +00001371 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001372 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001373 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001374 if (L_ECHO(tty))
1375 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001376 return;
1377 }
1378 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001379 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001381 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 else {
1383 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001384 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001385 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 echo_char(c, tty);
1387 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001388 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Joe Petersonacc71bb2009-01-02 13:43:32 +00001391 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001392 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jiri Slaby57c94122012-10-18 22:26:43 +02001394 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/**
1399 * n_tty_write_wakeup - asynchronous I/O notifier
1400 * @tty: tty device
1401 *
1402 * Required for the ptys, serial driver etc. since processes
1403 * that attach themselves to the master and rely on ASYNC
1404 * IO must be woken up
1405 */
1406
1407static void n_tty_write_wakeup(struct tty_struct *tty)
1408{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001409 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/**
1414 * n_tty_receive_buf - data receive
1415 * @tty: terminal device
1416 * @cp: buffer
1417 * @fp: flag buffer
1418 * @count: characters
1419 *
1420 * Called by the terminal driver when a block of characters has
1421 * been received. This function must be called from soft contexts
1422 * not from interrupt context. The driver is responsible for making
1423 * calls one at a time and in order (or using flush_to_ldisc)
1424 */
Alan Cox4edf1822008-02-08 04:18:44 -08001425
Peter Hurley24a89d12013-06-15 09:14:15 -04001426static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1427 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001429 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 const unsigned char *p;
1431 char *f, flags = TTY_NORMAL;
1432 int i;
1433 char buf[64];
1434 unsigned long cpuflags;
1435
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001436 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001437 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Peter Hurleyce741172013-06-15 09:14:20 -04001438 i = min(N_TTY_BUF_SIZE - read_cnt(ldata),
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001439 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001441 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1442 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1443 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 cp += i;
1445 count -= i;
1446
Peter Hurleyce741172013-06-15 09:14:20 -04001447 i = min(N_TTY_BUF_SIZE - read_cnt(ldata),
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001448 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001450 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1451 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1452 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001453 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001455 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (f)
1457 flags = *f++;
1458 switch (flags) {
1459 case TTY_NORMAL:
1460 n_tty_receive_char(tty, *p);
1461 break;
1462 case TTY_BREAK:
1463 n_tty_receive_break(tty);
1464 break;
1465 case TTY_PARITY:
1466 case TTY_FRAME:
1467 n_tty_receive_parity_error(tty, *p);
1468 break;
1469 case TTY_OVERRUN:
1470 n_tty_receive_overrun(tty);
1471 break;
1472 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001473 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 tty_name(tty, buf), flags);
1475 break;
1476 }
1477 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001478 if (tty->ops->flush_chars)
1479 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
Peter Hurleyce741172013-06-15 09:14:20 -04001482 if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001483 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1485 if (waitqueue_active(&tty->read_wait))
1486 wake_up_interruptible(&tty->read_wait);
1487 }
1488
1489 /*
1490 * Check the remaining room for the input canonicalization
1491 * mode. We don't want to throttle the driver if we're in
1492 * canonical mode and don't have a newline yet!
1493 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001494 while (1) {
1495 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley24a89d12013-06-15 09:14:15 -04001496 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05001497 break;
1498 if (!tty_throttle_safe(tty))
1499 break;
1500 }
1501 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Peter Hurley24a89d12013-06-15 09:14:15 -04001504static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1505 char *fp, int count)
1506{
1507 __receive_buf(tty, cp, fp, count);
1508}
1509
1510static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1511 char *fp, int count)
1512{
1513 struct n_tty_data *ldata = tty->disc_data;
1514 int room;
1515
1516 tty->receive_room = room = receive_room(tty);
1517 if (!room)
1518 ldata->no_room = 1;
1519 count = min(count, room);
1520 if (count)
1521 __receive_buf(tty, cp, fp, count);
1522
1523 return count;
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526int is_ignored(int sig)
1527{
1528 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001529 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532/**
1533 * n_tty_set_termios - termios data changed
1534 * @tty: terminal
1535 * @old: previous data
1536 *
1537 * Called by the tty layer when the user changes termios flags so
1538 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001539 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * guaranteed that this function will not be re-entered or in progress
1541 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001542 *
1543 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Alan Cox4edf1822008-02-08 04:18:44 -08001545
1546static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001548 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001549 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001550
1551 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001552 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001553 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001554 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001555 ldata->canon_head = ldata->read_tail;
1556 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001557 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001558 }
1559
Peter Hurleyce741172013-06-15 09:14:20 -04001560 if (canon_change && !L_ICANON(tty) && read_cnt(ldata))
Alan Cox47afa7a2008-10-13 10:44:17 +01001561 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001562
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001563 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1566 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1567 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1568 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001569 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001572 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001574 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001577 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1578 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1579 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1580 set_bit('\n', ldata->process_char_map);
1581 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (L_IEXTEN(tty)) {
1583 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001584 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001586 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001588 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (L_ECHO(tty))
1590 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001591 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 }
1594 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001595 set_bit(START_CHAR(tty), ldata->process_char_map);
1596 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001599 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1600 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1601 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001603 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001604 ldata->raw = 0;
1605 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001607 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1609 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1610 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001611 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001613 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001615 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001616 /*
1617 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1618 * been stopped by STOP_CHAR(tty) before it.
1619 */
1620 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1621 start_tty(tty);
1622 }
1623
Alan Coxf34d7a52008-04-30 00:54:13 -07001624 /* The termios change make the tty ready for I/O */
1625 wake_up_interruptible(&tty->write_wait);
1626 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
1629/**
1630 * n_tty_close - close the ldisc for this tty
1631 * @tty: device
1632 *
Alan Cox4edf1822008-02-08 04:18:44 -08001633 * Called from the terminal layer when this line discipline is
1634 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * discipline change. The function will not be called while other
1636 * ldisc methods are in progress.
1637 */
Alan Cox4edf1822008-02-08 04:18:44 -08001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639static void n_tty_close(struct tty_struct *tty)
1640{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001641 struct n_tty_data *ldata = tty->disc_data;
1642
Peter Hurley79901312013-03-11 16:44:23 -04001643 if (tty->link)
1644 n_tty_packet_mode_flush(tty);
1645
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001646 kfree(ldata->read_buf);
1647 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001648 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001649 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652/**
1653 * n_tty_open - open an ldisc
1654 * @tty: terminal to open
1655 *
Alan Cox4edf1822008-02-08 04:18:44 -08001656 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 * terminal device. Can sleep. Called serialized so that no
1658 * other events will occur in parallel. No further open will occur
1659 * until a close.
1660 */
1661
1662static int n_tty_open(struct tty_struct *tty)
1663{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001664 struct n_tty_data *ldata;
1665
1666 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1667 if (!ldata)
1668 goto err;
1669
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001670 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001671 mutex_init(&ldata->atomic_read_lock);
1672 mutex_init(&ldata->output_lock);
1673 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001674 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001675
Joe Petersona88a69c2009-01-02 13:40:53 +00001676 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001677 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1678 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1679 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001680 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001681
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001682 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001683 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001684 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001685 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001687 /* indicate buffer work may resume */
1688 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1689 n_tty_set_termios(tty, NULL);
1690 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001693err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001694 kfree(ldata->read_buf);
1695 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001696 kfree(ldata);
1697err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001698 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701static inline int input_available_p(struct tty_struct *tty, int amt)
1702{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001703 struct n_tty_data *ldata = tty->disc_data;
1704
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001705 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001706 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001707 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return 1;
Peter Hurleyce741172013-06-15 09:14:20 -04001709 } else if (read_cnt(ldata) >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 1;
1711
1712 return 0;
1713}
1714
1715/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001716 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * @tty: terminal device
1718 * @b: user data
1719 * @nr: size of data
1720 *
Alan Cox11a96d12008-10-13 10:46:24 +01001721 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * ICANON is off; it copies characters straight from the tty queue to
1723 * user space directly. It can be profitably called twice; once to
1724 * drain the space from the tail pointer to the (physical) end of the
1725 * buffer, and once to drain the space from the (physical) beginning of
1726 * the buffer to head pointer.
1727 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001728 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
1730 */
Alan Cox4edf1822008-02-08 04:18:44 -08001731
Alan Cox33f0f882006-01-09 20:54:13 -08001732static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned char __user **b,
1734 size_t *nr)
1735
1736{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001737 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int retval;
1739 size_t n;
1740 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001741 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001744 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Peter Hurleyce741172013-06-15 09:14:20 -04001745 n = min(read_cnt(ldata), N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001747 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001749 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001751 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001752 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1753 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001754 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001755 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001756 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1757 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001758 /* Turn single EOF into zero-length read */
Peter Hurleyce741172013-06-15 09:14:20 -04001759 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001760 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001761 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *b += n;
1763 *nr -= n;
1764 }
1765 return retval;
1766}
1767
Peter Hurley88bb0de2013-06-15 09:14:16 -04001768/**
Peter Hurley32f13522013-06-15 09:14:17 -04001769 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001770 * @tty: terminal device
1771 * @b: user data
1772 * @nr: size of data
1773 *
1774 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001775 * it copies one line of input up to and including the line-delimiting
1776 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001777 *
1778 * Called under the atomic_read_lock mutex
1779 */
1780
Peter Hurley32f13522013-06-15 09:14:17 -04001781static int canon_copy_from_read_buf(struct tty_struct *tty,
1782 unsigned char __user **b,
1783 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04001784{
1785 struct n_tty_data *ldata = tty->disc_data;
1786 unsigned long flags;
Peter Hurley32f13522013-06-15 09:14:17 -04001787 size_t n, size, more, c;
1788 unsigned long eol;
1789 int ret, tail, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001790
1791 /* N.B. avoid overrun if nr == 0 */
Peter Hurley32f13522013-06-15 09:14:17 -04001792
Peter Hurley88bb0de2013-06-15 09:14:16 -04001793 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001794
Peter Hurleyce741172013-06-15 09:14:20 -04001795 n = min(*nr, read_cnt(ldata));
Peter Hurley32f13522013-06-15 09:14:17 -04001796 if (!n) {
Peter Hurley88bb0de2013-06-15 09:14:16 -04001797 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Peter Hurley32f13522013-06-15 09:14:17 -04001798 return 0;
1799 }
Peter Hurley88bb0de2013-06-15 09:14:16 -04001800
Peter Hurley32f13522013-06-15 09:14:17 -04001801 tail = ldata->read_tail;
1802 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1803
1804 n_tty_trace("%s: nr:%zu tail:%d n:%zu size:%zu\n",
1805 __func__, *nr, tail, n, size);
1806
1807 eol = find_next_bit(ldata->read_flags, size, tail);
1808 more = n - (size - tail);
1809 if (eol == N_TTY_BUF_SIZE && more) {
1810 /* scan wrapped without finding set bit */
1811 eol = find_next_bit(ldata->read_flags, more, 0);
1812 if (eol != more)
1813 found = 1;
1814 } else if (eol != size)
1815 found = 1;
1816
1817 size = N_TTY_BUF_SIZE - tail;
1818 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
1819 c = n;
1820
1821 if (found && ldata->read_buf[eol] == __DISABLED_CHAR)
1822 n--;
1823
1824 n_tty_trace("%s: eol:%lu found:%d n:%zu c:%zu size:%zu more:%zu\n",
1825 __func__, eol, found, n, c, size, more);
1826
1827 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1828
1829 if (n > size) {
1830 ret = copy_to_user(*b, &ldata->read_buf[tail], size);
1831 if (ret)
1832 return -EFAULT;
1833 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
1834 } else
1835 ret = copy_to_user(*b, &ldata->read_buf[tail], n);
1836
1837 if (ret)
1838 return -EFAULT;
1839 *b += n;
1840 *nr -= n;
1841
1842 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1843 ldata->read_tail = (ldata->read_tail + c) & (N_TTY_BUF_SIZE - 1);
1844 ldata->read_cnt -= c;
1845 if (found) {
1846 __clear_bit(eol, ldata->read_flags);
1847 /* this test should be redundant:
1848 * we shouldn't be reading data if
1849 * canon_data is 0
1850 */
1851 if (--ldata->canon_data < 0)
1852 ldata->canon_data = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001853 }
1854 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1855
Peter Hurley32f13522013-06-15 09:14:17 -04001856 if (found)
1857 tty_audit_push(tty);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001858 return 0;
1859}
1860
Al Virocc4191d2008-03-29 03:08:48 +00001861extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001862 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864/**
1865 * job_control - check job control
1866 * @tty: tty
1867 * @file: file handle
1868 *
1869 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001870 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001872 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001873 * Locking: redirected write test is safe
1874 * current->signal->tty check is safe
1875 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
Alan Cox4edf1822008-02-08 04:18:44 -08001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878static int job_control(struct tty_struct *tty, struct file *file)
1879{
1880 /* Job control check -- must be done at start and after
1881 every sleep (POSIX.1 7.1.1.4). */
1882 /* NOTE: not yet done after every sleep pending a thorough
1883 check of the logic of this change. -- jlc */
1884 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001885 if (file->f_op->write == redirected_tty_write ||
1886 current->signal->tty != tty)
1887 return 0;
1888
1889 spin_lock_irq(&tty->ctrl_lock);
1890 if (!tty->pgrp)
1891 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1892 else if (task_pgrp(current) != tty->pgrp) {
1893 spin_unlock_irq(&tty->ctrl_lock);
1894 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1895 return -EIO;
1896 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1897 set_thread_flag(TIF_SIGPENDING);
1898 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001900 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
Alan Cox4edf1822008-02-08 04:18:44 -08001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905/**
Alan Cox11a96d12008-10-13 10:46:24 +01001906 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 * @tty: tty device
1908 * @file: file object
1909 * @buf: userspace buffer pointer
1910 * @nr: size of I/O
1911 *
1912 * Perform reads for the line discipline. We are guaranteed that the
1913 * line discipline will not be closed under us but we may get multiple
1914 * parallel readers and must handle this ourselves. We may also get
1915 * a hangup. Always called in user context, may sleep.
1916 *
1917 * This code must be sure never to sleep through a hangup.
1918 */
Alan Cox4edf1822008-02-08 04:18:44 -08001919
Alan Cox11a96d12008-10-13 10:46:24 +01001920static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned char __user *buf, size_t nr)
1922{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001923 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 unsigned char __user *b = buf;
1925 DECLARE_WAITQUEUE(wait, current);
1926 int c;
1927 int minimum, time;
1928 ssize_t retval = 0;
1929 ssize_t size;
1930 long timeout;
1931 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001932 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001936 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 minimum = time = 0;
1940 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001941 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 minimum = MIN_CHAR(tty);
1943 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04001944 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001946 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001948 (ldata->minimum_to_wake > minimum))
1949 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04001951 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001952 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 }
1955
1956 /*
1957 * Internal serialization of reads.
1958 */
1959 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001960 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001962 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001963 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -ERESTARTSYS;
1965 }
Alan Cox04f378b2008-04-30 00:53:29 -07001966 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 while (nr) {
1970 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001971 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 unsigned char cs;
1973 if (b != buf)
1974 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001975 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 cs = tty->link->ctrl_status;
1977 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001978 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001979 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 retval = -EFAULT;
1981 b--;
1982 break;
1983 }
1984 nr--;
1985 break;
1986 }
1987 /* This statement must be first before checking for input
1988 so that any interrupt will set the state back to
1989 TASK_RUNNING. */
1990 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001991
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001992 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001994 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (!input_available_p(tty, 0)) {
1997 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1998 retval = -EIO;
1999 break;
2000 }
2001 if (tty_hung_up_p(file))
2002 break;
2003 if (!timeout)
2004 break;
2005 if (file->f_flags & O_NONBLOCK) {
2006 retval = -EAGAIN;
2007 break;
2008 }
2009 if (signal_pending(current)) {
2010 retval = -ERESTARTSYS;
2011 break;
2012 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09002013 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 continue;
2016 }
2017 __set_current_state(TASK_RUNNING);
2018
2019 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07002020 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07002021 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 retval = -EFAULT;
2023 b--;
2024 break;
2025 }
2026 nr--;
2027 }
2028
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002029 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002030 retval = canon_copy_from_read_buf(tty, &b, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (retval)
2032 break;
2033 } else {
2034 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07002035 /* The copy function takes the read lock and handles
2036 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 uncopied = copy_from_read_buf(tty, &b, &nr);
2038 uncopied += copy_from_read_buf(tty, &b, &nr);
2039 if (uncopied) {
2040 retval = -EFAULT;
2041 break;
2042 }
2043 }
2044
2045 /* If there is enough space in the read buffer now, let the
Peter Hurleya19d0c62013-06-15 09:14:18 -04002046 * low-level driver know. We use chars_in_buffer() to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 * check the buffer, as it now knows about canonical mode.
2048 * Otherwise, if the driver is throttled and the line is
2049 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
2050 * we won't get any more characters.
2051 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05002052 while (1) {
2053 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
Peter Hurleya19d0c62013-06-15 09:14:18 -04002054 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05002055 break;
2056 if (!tty->count)
2057 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002058 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05002059 if (!tty_unthrottle_safe(tty))
2060 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002061 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05002062 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (b - buf >= minimum)
2065 break;
2066 if (time)
2067 timeout = time;
2068 }
Jiri Slabybddc7152012-10-18 22:26:42 +02002069 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 remove_wait_queue(&tty->read_wait, &wait);
2071
2072 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002073 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 __set_current_state(TASK_RUNNING);
2076 size = b - buf;
2077 if (size) {
2078 retval = size;
2079 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08002080 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01002082 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Linus Torvalds55db4c62011-06-04 06:33:24 +09002084 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return retval;
2086}
2087
2088/**
Alan Cox11a96d12008-10-13 10:46:24 +01002089 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 * @tty: tty device
2091 * @file: file object
2092 * @buf: userspace buffer pointer
2093 * @nr: size of I/O
2094 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002095 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002097 * and other such events. Since the receive code will echo characters,
2098 * thus calling driver write methods, the output_lock is used in
2099 * the output processing functions called here as well as in the
2100 * echo processing function to protect the column state and space
2101 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 *
2103 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002104 *
2105 * Locking: output_lock to protect column state and space left
2106 * (note that the process_output*() functions take this
2107 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
Alan Cox4edf1822008-02-08 04:18:44 -08002109
Alan Cox11a96d12008-10-13 10:46:24 +01002110static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002111 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 const unsigned char *b = buf;
2114 DECLARE_WAITQUEUE(wait, current);
2115 int c;
2116 ssize_t retval = 0;
2117
2118 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2119 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2120 retval = tty_check_change(tty);
2121 if (retval)
2122 return retval;
2123 }
2124
Joe Petersona88a69c2009-01-02 13:40:53 +00002125 /* Write out any echoed characters that are still pending */
2126 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 add_wait_queue(&tty->write_wait, &wait);
2129 while (1) {
2130 set_current_state(TASK_INTERRUPTIBLE);
2131 if (signal_pending(current)) {
2132 retval = -ERESTARTSYS;
2133 break;
2134 }
2135 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2136 retval = -EIO;
2137 break;
2138 }
Peter Hurley582f5592013-05-17 12:49:48 -04002139 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002141 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (num < 0) {
2143 if (num == -EAGAIN)
2144 break;
2145 retval = num;
2146 goto break_out;
2147 }
2148 b += num;
2149 nr -= num;
2150 if (nr == 0)
2151 break;
2152 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002153 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 break;
2155 b++; nr--;
2156 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002157 if (tty->ops->flush_chars)
2158 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002160 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002161 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002162 if (c < 0) {
2163 retval = c;
2164 goto break_out;
2165 }
2166 if (!c)
2167 break;
2168 b += c;
2169 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 if (!nr)
2173 break;
2174 if (file->f_flags & O_NONBLOCK) {
2175 retval = -EAGAIN;
2176 break;
2177 }
2178 schedule();
2179 }
2180break_out:
2181 __set_current_state(TASK_RUNNING);
2182 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002183 if (b - buf != nr && tty->fasync)
2184 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return (b - buf) ? b - buf : retval;
2186}
2187
2188/**
Alan Cox11a96d12008-10-13 10:46:24 +01002189 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * @tty: terminal device
2191 * @file: file accessing it
2192 * @wait: poll table
2193 *
2194 * Called when the line discipline is asked to poll() for data or
2195 * for special events. This code is not serialized with respect to
2196 * other events save open/close.
2197 *
2198 * This code must be sure never to sleep through a hangup.
2199 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Alan Cox4edf1822008-02-08 04:18:44 -08002201
Alan Cox11a96d12008-10-13 10:46:24 +01002202static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002203 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002205 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 unsigned int mask = 0;
2207
2208 poll_wait(file, &tty->read_wait, wait);
2209 poll_wait(file, &tty->write_wait, wait);
2210 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2211 mask |= POLLIN | POLLRDNORM;
2212 if (tty->packet && tty->link->ctrl_status)
2213 mask |= POLLPRI | POLLIN | POLLRDNORM;
2214 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2215 mask |= POLLHUP;
2216 if (tty_hung_up_p(file))
2217 mask |= POLLHUP;
2218 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2219 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002220 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002222 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002224 if (tty->ops->write && !tty_is_writelocked(tty) &&
2225 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2226 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 mask |= POLLOUT | POLLWRNORM;
2228 return mask;
2229}
2230
Jiri Slaby57c94122012-10-18 22:26:43 +02002231static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002232{
2233 int nr, head, tail;
2234
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002235 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002236 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002237 head = ldata->canon_head;
2238 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002239 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2240 /* Skip EOF-chars.. */
2241 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002242 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002243 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002244 nr--;
2245 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2246 }
2247 return nr;
2248}
2249
2250static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2251 unsigned int cmd, unsigned long arg)
2252{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002253 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002254 int retval;
2255
2256 switch (cmd) {
2257 case TIOCOUTQ:
2258 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2259 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002260 /* FIXME: Locking */
Peter Hurleyce741172013-06-15 09:14:20 -04002261 retval = read_cnt(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002262 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002263 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002264 return put_user(retval, (unsigned int __user *) arg);
2265 default:
2266 return n_tty_ioctl_helper(tty, file, cmd, arg);
2267 }
2268}
2269
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002270static void n_tty_fasync(struct tty_struct *tty, int on)
2271{
2272 struct n_tty_data *ldata = tty->disc_data;
2273
2274 if (!waitqueue_active(&tty->read_wait)) {
2275 if (on)
2276 ldata->minimum_to_wake = 1;
2277 else if (!tty->fasync)
2278 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2279 }
2280}
2281
Alan Coxa352def2008-07-16 21:53:12 +01002282struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002283 .magic = TTY_LDISC_MAGIC,
2284 .name = "n_tty",
2285 .open = n_tty_open,
2286 .close = n_tty_close,
2287 .flush_buffer = n_tty_flush_buffer,
2288 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002289 .read = n_tty_read,
2290 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002291 .ioctl = n_tty_ioctl,
2292 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002293 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002294 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002295 .write_wakeup = n_tty_write_wakeup,
2296 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002297 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002299
2300/**
2301 * n_tty_inherit_ops - inherit N_TTY methods
2302 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2303 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002304 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002305 * methods.
2306 */
2307
2308void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2309{
2310 *ops = tty_ldisc_N_TTY;
2311 ops->owner = NULL;
2312 ops->refcount = ops->flags = 0;
2313}
2314EXPORT_SYMBOL_GPL(n_tty_inherit_ops);