blob: e63beab3d1c3484a00defae0cb5778c02b515592 [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;
Peter Hurleybc5a5e32013-06-15 09:14:21 -040099 size_t read_head;
100 size_t read_tail;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -0400101 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200102
103 unsigned char *echo_buf;
104 unsigned int echo_pos;
105 unsigned int echo_cnt;
106
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400107 size_t canon_head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200108 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200109
110 struct mutex atomic_read_lock;
111 struct mutex output_lock;
112 struct mutex echo_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200113};
114
Peter Hurleyce741172013-06-15 09:14:20 -0400115static inline size_t read_cnt(struct n_tty_data *ldata)
116{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400117 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400118}
119
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400120static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
121{
122 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
123}
124
125static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
126{
127 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
128}
129
Miloslav Trmac522ed772007-07-15 23:40:56 -0700130static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
131 unsigned char __user *ptr)
132{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200133 struct n_tty_data *ldata = tty->disc_data;
134
135 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700136 return put_user(x, ptr);
137}
138
Peter Hurley24a89d12013-06-15 09:14:15 -0400139static int receive_room(struct tty_struct *tty)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900140{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200141 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500142 int left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400143
Jaeden Amero090abf72012-07-27 08:43:11 -0500144 if (I_PARMRK(tty)) {
145 /* Multiply read_cnt by 3, since each byte might take up to
146 * three times as many spaces when PARMRK is set (depending on
147 * its flags, e.g. parity error). */
Peter Hurleyce741172013-06-15 09:14:20 -0400148 left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500149 } else
Peter Hurleyce741172013-06-15 09:14:20 -0400150 left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500151
Linus Torvalds55db4c62011-06-04 06:33:24 +0900152 /*
153 * If we are doing input canonicalization, and there are no
154 * pending newlines, let characters through without limit, so
155 * that erase characters will be handled. Other excess
156 * characters will be beeped.
157 */
158 if (left <= 0)
Peter Hurleya73d3d62013-06-15 09:14:25 -0400159 left = ldata->icanon && ldata->canon_head == ldata->read_tail;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900160
Peter Hurley24a89d12013-06-15 09:14:15 -0400161 return left;
Peter Hurley7879a9f2013-06-15 07:28:31 -0400162}
163
Peter Hurley24a89d12013-06-15 09:14:15 -0400164/**
165 * n_tty_set_room - receive space
166 * @tty: terminal
167 *
168 * Re-schedules the flip buffer work if space just became available.
169 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400170 * Caller holds exclusive termios_rwsem
171 * or
172 * n_tty_read()/consumer path:
173 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400174 */
175
Peter Hurley7879a9f2013-06-15 07:28:31 -0400176static void n_tty_set_room(struct tty_struct *tty)
177{
Peter Hurley24a89d12013-06-15 09:14:15 -0400178 struct n_tty_data *ldata = tty->disc_data;
179
Linus Torvalds55db4c62011-06-04 06:33:24 +0900180 /* Did this open up the receive buffer? We may need to flip */
Peter Hurley24a89d12013-06-15 09:14:15 -0400181 if (unlikely(ldata->no_room) && receive_room(tty)) {
182 ldata->no_room = 0;
183
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200184 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400185 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400186 /* see if ldisc has been killed - if so, this means that
187 * even though the ldisc has been halted and ->buf.work
188 * cancelled, ->buf.work is about to be rescheduled
189 */
190 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
191 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200192 schedule_work(&tty->port->buf.work);
193 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900194}
195
Alan Cox17b82062008-10-13 10:45:06 +0100196/**
197 * put_tty_queue - add character to tty
198 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200199 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100200 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400201 * Add a character to the tty read_buf queue.
202 *
203 * n_tty_receive_buf()/producer path:
204 * caller holds non-exclusive termios_rwsem
205 * modifies read_head
206 *
207 * read_head is only considered 'published' if canonical mode is
208 * not active.
Alan Cox17b82062008-10-13 10:45:06 +0100209 */
210
Jiri Slaby57c94122012-10-18 22:26:43 +0200211static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400213 if (read_cnt(ldata) < N_TTY_BUF_SIZE) {
214 *read_buf_addr(ldata, ldata->read_head) = c;
215 ldata->read_head++;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * reset_buffer_flags - reset buffer state
221 * @tty: terminal to reset
222 *
Peter Hurley25518c62013-03-11 16:44:31 -0400223 * Reset the read buffer counters and clear the flags.
224 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100225 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400226 * Locking: caller holds exclusive termios_rwsem
227 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000229
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400230static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400232 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000233
Jiri Slabybddc7152012-10-18 22:26:42 +0200234 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200235 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200236 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000237
Peter Hurleya73d3d62013-06-15 09:14:25 -0400238 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200239 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Peter Hurleya30737a2013-03-11 16:44:22 -0400242static void n_tty_packet_mode_flush(struct tty_struct *tty)
243{
244 unsigned long flags;
245
246 spin_lock_irqsave(&tty->ctrl_lock, flags);
247 if (tty->link->packet) {
248 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
249 wake_up_interruptible(&tty->link->read_wait);
250 }
251 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * n_tty_flush_buffer - clean input queue
256 * @tty: terminal device
257 *
Peter Hurley25518c62013-03-11 16:44:31 -0400258 * Flush the input buffer. Called when the tty layer wants the
259 * buffer flushed (eg at hangup) or when the N_TTY line discipline
260 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400262 * Holds termios_rwsem to exclude producer/consumer while
263 * buffer indices are reset.
264 *
265 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Alan Cox4edf1822008-02-08 04:18:44 -0800267
268static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400270 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400271 reset_buffer_flags(tty->disc_data);
272 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800273
Peter Hurleya30737a2013-03-11 16:44:22 -0400274 if (tty->link)
275 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400276 up_write(&tty->termios_rwsem);
277}
278
279static ssize_t chars_in_buffer(struct tty_struct *tty)
280{
281 struct n_tty_data *ldata = tty->disc_data;
282 ssize_t n = 0;
283
284 if (!ldata->icanon)
285 n = read_cnt(ldata);
286 else
287 n = ldata->canon_head - ldata->read_tail;
288 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/**
292 * n_tty_chars_in_buffer - report available bytes
293 * @tty: tty device
294 *
295 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800296 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100297 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400298 * Locking: exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Alan Cox4edf1822008-02-08 04:18:44 -0800300
Peter Hurleya19d0c62013-06-15 09:14:18 -0400301static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
302{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400303 ssize_t n;
304
Peter Hurley47534082013-06-15 09:14:19 -0400305 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400306
307 down_write(&tty->termios_rwsem);
308 n = chars_in_buffer(tty);
309 up_write(&tty->termios_rwsem);
310 return n;
Peter Hurleya19d0c62013-06-15 09:14:18 -0400311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/**
314 * is_utf8_continuation - utf8 multibyte check
315 * @c: byte to check
316 *
317 * Returns true if the utf8 character 'c' is a multibyte continuation
318 * character. We use this to correctly compute the on screen size
319 * of the character when printing
320 */
Alan Cox4edf1822008-02-08 04:18:44 -0800321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static inline int is_utf8_continuation(unsigned char c)
323{
324 return (c & 0xc0) == 0x80;
325}
326
327/**
328 * is_continuation - multibyte check
329 * @c: byte to check
330 *
331 * Returns true if the utf8 character 'c' is a multibyte continuation
332 * character and the terminal is in unicode mode.
333 */
Alan Cox4edf1822008-02-08 04:18:44 -0800334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static inline int is_continuation(unsigned char c, struct tty_struct *tty)
336{
337 return I_IUTF8(tty) && is_utf8_continuation(c);
338}
339
340/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000341 * do_output_char - output one character
342 * @c: character (or partial unicode symbol)
343 * @tty: terminal device
344 * @space: space available in tty driver write buffer
345 *
346 * This is a helper function that handles one output character
347 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600348 * doing OPOST processing and putting the results in the
349 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000350 *
351 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
352 * and NLDLY. They simply aren't relevant in the world today.
353 * If you ever need them, add them here.
354 *
355 * Returns the number of bytes of buffer space used or -1 if
356 * no space left.
357 *
358 * Locking: should be called under the output_lock to protect
359 * the column state and space left in the buffer
360 */
361
362static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
363{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200364 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000365 int spaces;
366
367 if (!space)
368 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000369
Joe Petersona88a69c2009-01-02 13:40:53 +0000370 switch (c) {
371 case '\n':
372 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200373 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000374 if (O_ONLCR(tty)) {
375 if (space < 2)
376 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200377 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700378 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000379 return 2;
380 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200381 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000382 break;
383 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200384 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000385 return 0;
386 if (O_OCRNL(tty)) {
387 c = '\n';
388 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200389 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000390 break;
391 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200392 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000393 break;
394 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200395 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000396 if (O_TABDLY(tty) == XTABS) {
397 if (space < spaces)
398 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200399 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000400 tty->ops->write(tty, " ", spaces);
401 return spaces;
402 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200403 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000404 break;
405 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200406 if (ldata->column > 0)
407 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000408 break;
409 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000410 if (!iscntrl(c)) {
411 if (O_OLCUC(tty))
412 c = toupper(c);
413 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200414 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000415 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000416 break;
417 }
418
419 tty_put_char(tty, c);
420 return 1;
421}
422
423/**
424 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * @c: character (or partial unicode symbol)
426 * @tty: terminal device
427 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600428 * Output one character with OPOST processing.
429 * Returns -1 when the output device is full and the character
430 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000432 * Locking: output_lock to protect column state and space left
433 * (also, this is called from n_tty_write under the
434 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Alan Cox4edf1822008-02-08 04:18:44 -0800436
Joe Petersona88a69c2009-01-02 13:40:53 +0000437static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jiri Slabybddc7152012-10-18 22:26:42 +0200439 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000440 int space, retval;
441
Jiri Slabybddc7152012-10-18 22:26:42 +0200442 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alan Coxf34d7a52008-04-30 00:54:13 -0700444 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000445 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jiri Slabybddc7152012-10-18 22:26:42 +0200447 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000448 if (retval < 0)
449 return -1;
450 else
451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000455 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600457 * @buf: character buffer
458 * @nr: number of bytes to output
459 *
460 * Output a block of characters with OPOST processing.
461 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
463 * This path is used to speed up block console writes, among other
464 * things when processing blocks of output data. It handles only
465 * the simple cases normally found and helps to generate blocks of
466 * symbols for the console driver and thus improve performance.
467 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000468 * Locking: output_lock to protect column state and space left
469 * (also, this is called from n_tty_write under the
470 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Alan Cox4edf1822008-02-08 04:18:44 -0800472
Joe Petersona88a69c2009-01-02 13:40:53 +0000473static ssize_t process_output_block(struct tty_struct *tty,
474 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200476 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100478 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 const unsigned char *cp;
480
Jiri Slabybddc7152012-10-18 22:26:42 +0200481 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000482
Alan Coxf34d7a52008-04-30 00:54:13 -0700483 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000484 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200485 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (nr > space)
489 nr = space;
490
491 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000492 unsigned char c = *cp;
493
494 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case '\n':
496 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200497 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (O_ONLCR(tty))
499 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200500 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200503 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto break_out;
505 if (O_OCRNL(tty))
506 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200507 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 case '\t':
510 goto break_out;
511 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200512 if (ldata->column > 0)
513 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000516 if (!iscntrl(c)) {
517 if (O_OLCUC(tty))
518 goto break_out;
519 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200520 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 }
524 }
525break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700526 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000527
Jiri Slabybddc7152012-10-18 22:26:42 +0200528 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return i;
530}
531
Joe Petersona88a69c2009-01-02 13:40:53 +0000532/**
533 * process_echoes - write pending echo characters
534 * @tty: terminal device
535 *
536 * Write previously buffered echo (and other ldisc-generated)
537 * characters to the tty.
538 *
539 * Characters generated by the ldisc (including echoes) need to
540 * be buffered because the driver's write buffer can fill during
541 * heavy program output. Echoing straight to the driver will
542 * often fail under these conditions, causing lost characters and
543 * resulting mismatches of ldisc state information.
544 *
545 * Since the ldisc state must represent the characters actually sent
546 * to the driver at the time of the write, operations like certain
547 * changes in column state are also saved in the buffer and executed
548 * here.
549 *
550 * A circular fifo buffer is used so that the most recent characters
551 * are prioritized. Also, when control characters are echoed with a
552 * prefixed "^", the pair is treated atomically and thus not separated.
553 *
554 * Locking: output_lock to protect column state and space left,
555 * echo_lock to protect the echo buffer
556 */
557
558static void process_echoes(struct tty_struct *tty)
559{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200560 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000561 int space, nr;
562 unsigned char c;
563 unsigned char *cp, *buf_end;
564
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200565 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000566 return;
567
Jiri Slabybddc7152012-10-18 22:26:42 +0200568 mutex_lock(&ldata->output_lock);
569 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000570
571 space = tty_write_room(tty);
572
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200573 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
574 cp = ldata->echo_buf + ldata->echo_pos;
575 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000576 while (nr > 0) {
577 c = *cp;
578 if (c == ECHO_OP_START) {
579 unsigned char op;
580 unsigned char *opp;
581 int no_space_left = 0;
582
583 /*
584 * If the buffer byte is the start of a multi-byte
585 * operation, get the next byte, which is either the
586 * op code or a control character value.
587 */
588 opp = cp + 1;
589 if (opp == buf_end)
590 opp -= N_TTY_BUF_SIZE;
591 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000592
Joe Petersona88a69c2009-01-02 13:40:53 +0000593 switch (op) {
594 unsigned int num_chars, num_bs;
595
596 case ECHO_OP_ERASE_TAB:
597 if (++opp == buf_end)
598 opp -= N_TTY_BUF_SIZE;
599 num_chars = *opp;
600
601 /*
602 * Determine how many columns to go back
603 * in order to erase the tab.
604 * This depends on the number of columns
605 * used by other characters within the tab
606 * area. If this (modulo 8) count is from
607 * the start of input rather than from a
608 * previous tab, we offset by canon column.
609 * Otherwise, tab spacing is normal.
610 */
611 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200612 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000613 num_bs = 8 - (num_chars & 7);
614
615 if (num_bs > space) {
616 no_space_left = 1;
617 break;
618 }
619 space -= num_bs;
620 while (num_bs--) {
621 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200622 if (ldata->column > 0)
623 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000624 }
625 cp += 3;
626 nr -= 3;
627 break;
628
629 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200630 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000631 cp += 2;
632 nr -= 2;
633 break;
634
635 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200636 if (ldata->column > 0)
637 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000638 cp += 2;
639 nr -= 2;
640 break;
641
642 case ECHO_OP_START:
643 /* This is an escaped echo op start code */
644 if (!space) {
645 no_space_left = 1;
646 break;
647 }
648 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200649 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000650 space--;
651 cp += 2;
652 nr -= 2;
653 break;
654
655 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000656 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600657 * If the op is not a special byte code,
658 * it is a ctrl char tagged to be echoed
659 * as "^X" (where X is the letter
660 * representing the control char).
661 * Note that we must ensure there is
662 * enough space for the whole ctrl pair.
663 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000664 */
Joe Peterson62b26352009-09-09 15:03:47 -0600665 if (space < 2) {
666 no_space_left = 1;
667 break;
668 }
669 tty_put_char(tty, '^');
670 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200671 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600672 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000673 cp += 2;
674 nr -= 2;
675 }
676
677 if (no_space_left)
678 break;
679 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400680 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600681 int retval = do_output_char(c, tty, space);
682 if (retval < 0)
683 break;
684 space -= retval;
685 } else {
686 if (!space)
687 break;
688 tty_put_char(tty, c);
689 space -= 1;
690 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000691 cp += 1;
692 nr -= 1;
693 }
694
695 /* When end of circular buffer reached, wrap around */
696 if (cp >= buf_end)
697 cp -= N_TTY_BUF_SIZE;
698 }
699
700 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200701 ldata->echo_pos = 0;
702 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200703 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200705 int num_processed = ldata->echo_cnt - nr;
706 ldata->echo_pos += num_processed;
707 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
708 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000709 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200710 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 }
712
Jiri Slabybddc7152012-10-18 22:26:42 +0200713 mutex_unlock(&ldata->echo_lock);
714 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000715
716 if (tty->ops->flush_chars)
717 tty->ops->flush_chars(tty);
718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000721 * add_echo_byte - add a byte to the echo buffer
722 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200723 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 *
725 * Add a character or operation byte to the echo buffer.
726 *
727 * Should be called under the echo lock to protect the echo buffer.
728 */
729
Jiri Slaby57c94122012-10-18 22:26:43 +0200730static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000731{
732 int new_byte_pos;
733
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200734 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000735 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200736 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737
738 /*
739 * Since the buffer start position needs to be advanced,
740 * be sure to step by a whole operation byte group.
741 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200742 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
743 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000744 (N_TTY_BUF_SIZE - 1)] ==
745 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200746 ldata->echo_pos += 3;
747 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000748 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200749 ldata->echo_pos += 2;
750 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000751 }
752 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200753 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000754 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200755 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000756
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200757 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000758 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200759 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000760 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200761 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000762 }
763
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200764 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000765}
766
767/**
768 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200769 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000770 *
771 * Add an operation to the echo buffer to move back one column.
772 *
773 * Locking: echo_lock to protect the echo buffer
774 */
775
Jiri Slaby57c94122012-10-18 22:26:43 +0200776static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000777{
Jiri Slabybddc7152012-10-18 22:26:42 +0200778 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200779 add_echo_byte(ECHO_OP_START, ldata);
780 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200781 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000782}
783
784/**
785 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200786 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000787 *
788 * Add an operation to the echo buffer to set the canon column
789 * to the current column.
790 *
791 * Locking: echo_lock to protect the echo buffer
792 */
793
Jiri Slaby57c94122012-10-18 22:26:43 +0200794static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000795{
Jiri Slabybddc7152012-10-18 22:26:42 +0200796 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200797 add_echo_byte(ECHO_OP_START, ldata);
798 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200799 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000800}
801
802/**
803 * echo_erase_tab - add operation to erase a tab
804 * @num_chars: number of character columns already used
805 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200806 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000807 *
808 * Add an operation to the echo buffer to erase a tab.
809 *
810 * Called by the eraser function, which knows how many character
811 * columns have been used since either a previous tab or the start
812 * of input. This information will be used later, along with
813 * canon column (if applicable), to go back the correct number
814 * of columns.
815 *
816 * Locking: echo_lock to protect the echo buffer
817 */
818
819static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200820 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000821{
Jiri Slabybddc7152012-10-18 22:26:42 +0200822 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000823
Jiri Slaby57c94122012-10-18 22:26:43 +0200824 add_echo_byte(ECHO_OP_START, ldata);
825 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000826
827 /* We only need to know this modulo 8 (tab spacing) */
828 num_chars &= 7;
829
830 /* Set the high bit as a flag if num_chars is after a previous tab */
831 if (after_tab)
832 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000833
Jiri Slaby57c94122012-10-18 22:26:43 +0200834 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000835
Jiri Slabybddc7152012-10-18 22:26:42 +0200836 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000837}
838
839/**
840 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * @c: unicode byte to echo
842 * @tty: terminal device
843 *
Alan Cox4edf1822008-02-08 04:18:44 -0800844 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100846 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000847 * This variant does not treat control characters specially.
848 *
849 * Locking: echo_lock to protect the echo buffer
850 */
851
Jiri Slaby57c94122012-10-18 22:26:43 +0200852static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000853{
Jiri Slabybddc7152012-10-18 22:26:42 +0200854 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000855 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200856 add_echo_byte(ECHO_OP_START, ldata);
857 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200859 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000860 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200861 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000862}
863
864/**
865 * echo_char - echo a character
866 * @c: unicode byte to echo
867 * @tty: terminal device
868 *
869 * Echo user input back onto the screen. This must be called only when
870 * L_ECHO(tty) is true. Called from the driver receive_buf path.
871 *
Joe Peterson62b26352009-09-09 15:03:47 -0600872 * This variant tags control characters to be echoed as "^X"
873 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000874 *
875 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877
878static void echo_char(unsigned char c, struct tty_struct *tty)
879{
Jiri Slabybddc7152012-10-18 22:26:42 +0200880 struct n_tty_data *ldata = tty->disc_data;
881
882 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000883
884 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200885 add_echo_byte(ECHO_OP_START, ldata);
886 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000887 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600888 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200889 add_echo_byte(ECHO_OP_START, ldata);
890 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000891 }
892
Jiri Slabybddc7152012-10-18 22:26:42 +0200893 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Alan Cox17b82062008-10-13 10:45:06 +0100896/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000897 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200898 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100899 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000900
Jiri Slaby57c94122012-10-18 22:26:43 +0200901static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200903 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200904 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200905 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907}
908
909/**
910 * eraser - handle erase function
911 * @c: character input
912 * @tty: terminal device
913 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200914 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * present in the stream from the driver layer. Handles the complexities
916 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100917 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400918 * n_tty_receive_buf()/producer path:
919 * caller holds non-exclusive termios_rwsem
920 * modifies read_head
921 *
922 * Modifying the read_head is not considered a publish in this context
923 * because canonical mode is active -- only canon_head publishes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
Alan Cox4edf1822008-02-08 04:18:44 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static void eraser(unsigned char c, struct tty_struct *tty)
927{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200928 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400930 size_t head;
931 size_t cnt;
932 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200934 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000935 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return;
937 }
938 if (c == ERASE_CHAR(tty))
939 kill_type = ERASE;
940 else if (c == WERASE_CHAR(tty))
941 kill_type = WERASE;
942 else {
943 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200944 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
946 }
947 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200948 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200949 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 echo_char(KILL_CHAR(tty), tty);
951 /* Add a newline if ECHOK is on and ECHOKE is off. */
952 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200953 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return;
955 }
956 kill_type = KILL;
957 }
958
959 seen_alnums = 0;
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 {
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400965 head--;
966 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200967 } 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 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400980 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200981 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (L_ECHO(tty)) {
983 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200984 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200985 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200986 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 /* if cnt > 1, output a multi-byte character */
989 echo_char(c, tty);
990 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400991 head++;
992 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +0200993 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
996 echo_char(ERASE_CHAR(tty), tty);
997 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000998 unsigned int num_chars = 0;
999 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001000 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Joe Petersona88a69c2009-01-02 13:40:53 +00001002 /*
1003 * Count the columns used for characters
1004 * since the start of input or after a
1005 * previous tab.
1006 * This info is used to go back the correct
1007 * number of columns.
1008 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001009 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001010 tail--;
1011 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001012 if (c == '\t') {
1013 after_tab = 1;
1014 break;
Alan Cox300a6202009-01-02 13:41:04 +00001015 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001017 num_chars += 2;
1018 } else if (!is_continuation(c, tty)) {
1019 num_chars++;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001022 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
1024 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001025 echo_char_raw('\b', ldata);
1026 echo_char_raw(' ', ldata);
1027 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001030 echo_char_raw('\b', ldata);
1031 echo_char_raw(' ', ldata);
1032 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 }
1035 }
1036 if (kill_type == ERASE)
1037 break;
1038 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001039 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001040 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/**
1044 * isig - handle the ISIG optio
1045 * @sig: signal
1046 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001048 * Called when a signal is being sent due to terminal input.
1049 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001050 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001051 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Alan Cox4edf1822008-02-08 04:18:44 -08001053
Peter Hurley8c985d12013-03-06 08:38:19 -05001054static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Peter Hurley8c985d12013-03-06 08:38:19 -05001056 struct pid *tty_pgrp = tty_get_pgrp(tty);
1057 if (tty_pgrp) {
1058 kill_pgrp(tty_pgrp, sig, 1);
1059 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061}
1062
1063/**
1064 * n_tty_receive_break - handle break
1065 * @tty: terminal
1066 *
1067 * An RS232 break event has been hit in the incoming bitstream. This
1068 * can cause a variety of events depending upon the termios settings.
1069 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001070 * n_tty_receive_buf()/producer path:
1071 * caller holds non-exclusive termios_rwsem
1072 * publishes read_head via put_tty_queue()
1073 *
1074 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
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)) {
Peter Hurley6d76bd22013-06-15 09:14:26 -04001086 /* flushing needs exclusive termios_rwsem */
1087 up_read(&tty->termios_rwsem);
Peter Hurley8c985d12013-03-06 08:38:19 -05001088 n_tty_flush_buffer(tty);
1089 tty_driver_flush_buffer(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001090 down_read(&tty->termios_rwsem);
Peter Hurley8c985d12013-03-06 08:38:19 -05001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return;
1093 }
1094 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001095 put_tty_queue('\377', ldata);
1096 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001098 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 wake_up_interruptible(&tty->read_wait);
1100}
1101
1102/**
1103 * n_tty_receive_overrun - handle overrun reporting
1104 * @tty: terminal
1105 *
1106 * Data arrived faster than we could process it. While the tty
1107 * driver has flagged this the bits that were missed are gone
1108 * forever.
1109 *
1110 * Called from the receive_buf path so single threaded. Does not
1111 * need locking as num_overrun and overrun_time are function
1112 * private.
1113 */
Alan Cox4edf1822008-02-08 04:18:44 -08001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115static inline void n_tty_receive_overrun(struct tty_struct *tty)
1116{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001117 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 char buf[64];
1119
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001120 ldata->num_overrun++;
1121 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1122 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1124 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001125 ldata->num_overrun);
1126 ldata->overrun_time = jiffies;
1127 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129}
1130
1131/**
1132 * n_tty_receive_parity_error - error notifier
1133 * @tty: terminal device
1134 * @c: character
1135 *
1136 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001137 * the error case if necessary.
1138 *
1139 * n_tty_receive_buf()/producer path:
1140 * caller holds non-exclusive termios_rwsem
1141 * publishes read_head via put_tty_queue()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
1143static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1144 unsigned char c)
1145{
Jiri Slaby57c94122012-10-18 22:26:43 +02001146 struct n_tty_data *ldata = tty->disc_data;
1147
Alan Cox4edf1822008-02-08 04:18:44 -08001148 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001151 put_tty_queue('\377', ldata);
1152 put_tty_queue('\0', ldata);
1153 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001155 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001157 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 wake_up_interruptible(&tty->read_wait);
1159}
1160
1161/**
1162 * n_tty_receive_char - perform processing
1163 * @tty: terminal device
1164 * @c: character
1165 *
1166 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001167 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001169 *
1170 * n_tty_receive_buf()/producer path:
1171 * caller holds non-exclusive termios_rwsem
1172 * publishes canon_head if canonical mode is active
1173 * otherwise, publishes read_head via put_tty_queue()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
1175
1176static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1177{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001178 struct n_tty_data *ldata = tty->disc_data;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001179 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001181 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001182 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return;
1184 }
Alan Cox4edf1822008-02-08 04:18:44 -08001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (I_ISTRIP(tty))
1187 c &= 0x7f;
1188 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001189 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
hyc@symas.com26df6d12010-06-22 10:14:49 -07001191 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001192 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001193 return;
1194 }
1195
Joe Peterson54d2a372008-02-06 01:37:59 -08001196 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001197 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1198 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001199 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001200 process_echoes(tty);
1201 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (tty->closing) {
1204 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001205 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001207 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001208 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 stop_tty(tty);
1210 }
1211 return;
1212 }
1213
1214 /*
1215 * If the previous character was LNEXT, or we know that this
1216 * character is not one of the characters that we'll have to
1217 * handle specially, do shortcut processing to speed things
1218 * up.
1219 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001220 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001221 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001222 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001223 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001224 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001225 if (L_ECHO(tty))
1226 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001227 return;
1228 }
1229 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001230 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001232 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001233 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001235 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001237 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001238 put_tty_queue(c, ldata);
1239 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
Alan Cox4edf1822008-02-08 04:18:44 -08001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (I_IXON(tty)) {
1244 if (c == START_CHAR(tty)) {
1245 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001246 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return;
1248 }
1249 if (c == STOP_CHAR(tty)) {
1250 stop_tty(tty);
1251 return;
1252 }
1253 }
Joe Peterson575537b32008-04-30 00:53:30 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (L_ISIG(tty)) {
1256 int signal;
1257 signal = SIGINT;
1258 if (c == INTR_CHAR(tty))
1259 goto send_signal;
1260 signal = SIGQUIT;
1261 if (c == QUIT_CHAR(tty))
1262 goto send_signal;
1263 signal = SIGTSTP;
1264 if (c == SUSP_CHAR(tty)) {
1265send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001266 if (!L_NOFLSH(tty)) {
Peter Hurley6d76bd22013-06-15 09:14:26 -04001267 /* flushing needs exclusive termios_rwsem */
1268 up_read(&tty->termios_rwsem);
Joe Petersonec5b1152008-02-06 01:37:38 -08001269 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001270 tty_driver_flush_buffer(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001271 down_read(&tty->termios_rwsem);
Joe Petersonec5b1152008-02-06 01:37:38 -08001272 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001273 if (I_IXON(tty))
1274 start_tty(tty);
1275 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001276 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001277 process_echoes(tty);
1278 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001279 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281 }
1282 }
Joe Peterson575537b32008-04-30 00:53:30 -07001283
1284 if (c == '\r') {
1285 if (I_IGNCR(tty))
1286 return;
1287 if (I_ICRNL(tty))
1288 c = '\n';
1289 } else if (c == '\n' && I_INLCR(tty))
1290 c = '\r';
1291
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001292 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1294 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1295 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001296 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298 }
1299 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001300 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001302 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001304 echo_char_raw('^', ldata);
1305 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001306 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 }
1309 return;
1310 }
1311 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1312 L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001313 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Jiri Slaby57c94122012-10-18 22:26:43 +02001315 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001317 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001318 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001319 echo_char(read_buf(ldata, tail), tty);
1320 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001322 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return;
1324 }
1325 if (c == '\n') {
Peter Hurleyce741172013-06-15 09:14:20 -04001326 if (read_cnt(ldata) >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001327 if (L_ECHO(tty))
1328 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001329 return;
1330 }
1331 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001332 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001333 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 goto handle_newline;
1336 }
1337 if (c == EOF_CHAR(tty)) {
Peter Hurleyce741172013-06-15 09:14:20 -04001338 if (read_cnt(ldata) >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001339 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001340 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001341 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 c = __DISABLED_CHAR;
1343 goto handle_newline;
1344 }
1345 if ((c == EOL_CHAR(tty)) ||
1346 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001347 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1348 ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001349 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001350 if (L_ECHO(tty))
1351 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001352 return;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1356 */
1357 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001359 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001360 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001362 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 /*
1365 * XXX does PARMRK doubling happen for
1366 * EOL_CHAR and EOL2_CHAR?
1367 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001368 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001369 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Alan Cox4edf1822008-02-08 04:18:44 -08001371handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001372 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001373 put_tty_queue(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001374 ldata->canon_head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1376 if (waitqueue_active(&tty->read_wait))
1377 wake_up_interruptible(&tty->read_wait);
1378 return;
1379 }
1380 }
Alan Cox4edf1822008-02-08 04:18:44 -08001381
Joe Petersonacc71bb2009-01-02 13:43:32 +00001382 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001383 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001384 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001385 if (L_ECHO(tty))
1386 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001387 return;
1388 }
1389 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001390 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001392 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 else {
1394 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001395 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001396 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 echo_char(c, tty);
1398 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001399 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Joe Petersonacc71bb2009-01-02 13:43:32 +00001402 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001403 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jiri Slaby57c94122012-10-18 22:26:43 +02001405 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409/**
1410 * n_tty_write_wakeup - asynchronous I/O notifier
1411 * @tty: tty device
1412 *
1413 * Required for the ptys, serial driver etc. since processes
1414 * that attach themselves to the master and rely on ASYNC
1415 * IO must be woken up
1416 */
1417
1418static void n_tty_write_wakeup(struct tty_struct *tty)
1419{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001420 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424/**
1425 * n_tty_receive_buf - data receive
1426 * @tty: terminal device
1427 * @cp: buffer
1428 * @fp: flag buffer
1429 * @count: characters
1430 *
1431 * Called by the terminal driver when a block of characters has
1432 * been received. This function must be called from soft contexts
1433 * not from interrupt context. The driver is responsible for making
1434 * calls one at a time and in order (or using flush_to_ldisc)
Peter Hurley6d76bd22013-06-15 09:14:26 -04001435 *
1436 * n_tty_receive_buf()/producer path:
1437 * claims non-exclusive termios_rwsem
1438 * publishes read_head and canon_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 */
Alan Cox4edf1822008-02-08 04:18:44 -08001440
Peter Hurley24a89d12013-06-15 09:14:15 -04001441static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1442 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001444 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 const unsigned char *p;
1446 char *f, flags = TTY_NORMAL;
1447 int i;
1448 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001450 if (ldata->real_raw) {
Peter Hurleyce741172013-06-15 09:14:20 -04001451 i = min(N_TTY_BUF_SIZE - read_cnt(ldata),
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001452 N_TTY_BUF_SIZE - (ldata->read_head & (N_TTY_BUF_SIZE - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 i = min(count, i);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001454 memcpy(read_buf_addr(ldata, ldata->read_head), cp, i);
1455 ldata->read_head += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 cp += i;
1457 count -= i;
1458
Peter Hurleyce741172013-06-15 09:14:20 -04001459 i = min(N_TTY_BUF_SIZE - read_cnt(ldata),
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001460 N_TTY_BUF_SIZE - (ldata->read_head & (N_TTY_BUF_SIZE - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 i = min(count, i);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001462 memcpy(read_buf_addr(ldata, ldata->read_head), cp, i);
1463 ldata->read_head += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001465 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (f)
1467 flags = *f++;
1468 switch (flags) {
1469 case TTY_NORMAL:
1470 n_tty_receive_char(tty, *p);
1471 break;
1472 case TTY_BREAK:
1473 n_tty_receive_break(tty);
1474 break;
1475 case TTY_PARITY:
1476 case TTY_FRAME:
1477 n_tty_receive_parity_error(tty, *p);
1478 break;
1479 case TTY_OVERRUN:
1480 n_tty_receive_overrun(tty);
1481 break;
1482 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001483 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 tty_name(tty, buf), flags);
1485 break;
1486 }
1487 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001488 if (tty->ops->flush_chars)
1489 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
Peter Hurleyce741172013-06-15 09:14:20 -04001492 if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001493 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1495 if (waitqueue_active(&tty->read_wait))
1496 wake_up_interruptible(&tty->read_wait);
1497 }
1498
1499 /*
1500 * Check the remaining room for the input canonicalization
1501 * mode. We don't want to throttle the driver if we're in
1502 * canonical mode and don't have a newline yet!
1503 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001504 while (1) {
Peter Hurley9356b532013-06-15 09:14:24 -04001505 int throttled;
Peter Hurleye91e52e2013-03-06 08:20:53 -05001506 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley24a89d12013-06-15 09:14:15 -04001507 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05001508 break;
Peter Hurley9356b532013-06-15 09:14:24 -04001509 up_read(&tty->termios_rwsem);
1510 throttled = tty_throttle_safe(tty);
1511 down_read(&tty->termios_rwsem);
1512 if (!throttled)
Peter Hurleye91e52e2013-03-06 08:20:53 -05001513 break;
1514 }
1515 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Peter Hurley24a89d12013-06-15 09:14:15 -04001518static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1519 char *fp, int count)
1520{
Peter Hurley9356b532013-06-15 09:14:24 -04001521 down_read(&tty->termios_rwsem);
Peter Hurley24a89d12013-06-15 09:14:15 -04001522 __receive_buf(tty, cp, fp, count);
Peter Hurley9356b532013-06-15 09:14:24 -04001523 up_read(&tty->termios_rwsem);
Peter Hurley24a89d12013-06-15 09:14:15 -04001524}
1525
1526static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1527 char *fp, int count)
1528{
1529 struct n_tty_data *ldata = tty->disc_data;
1530 int room;
1531
Peter Hurley9356b532013-06-15 09:14:24 -04001532 down_read(&tty->termios_rwsem);
1533
Peter Hurley24a89d12013-06-15 09:14:15 -04001534 tty->receive_room = room = receive_room(tty);
1535 if (!room)
1536 ldata->no_room = 1;
1537 count = min(count, room);
1538 if (count)
1539 __receive_buf(tty, cp, fp, count);
1540
Peter Hurley9356b532013-06-15 09:14:24 -04001541 up_read(&tty->termios_rwsem);
1542
Peter Hurley24a89d12013-06-15 09:14:15 -04001543 return count;
1544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546int is_ignored(int sig)
1547{
1548 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001549 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/**
1553 * n_tty_set_termios - termios data changed
1554 * @tty: terminal
1555 * @old: previous data
1556 *
1557 * Called by the tty layer when the user changes termios flags so
1558 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001559 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * guaranteed that this function will not be re-entered or in progress
1561 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001562 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001563 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 */
Alan Cox4edf1822008-02-08 04:18:44 -08001565
1566static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001568 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001569 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001570
1571 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001572 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001573 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001574 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001575 ldata->canon_head = ldata->read_tail;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001576 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001577 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001578 }
1579
Peter Hurleyce741172013-06-15 09:14:20 -04001580 if (canon_change && !L_ICANON(tty) && read_cnt(ldata))
Alan Cox47afa7a2008-10-13 10:44:17 +01001581 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001582
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001583 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1586 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1587 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1588 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001589 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001592 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001594 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001597 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1598 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1599 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1600 set_bit('\n', ldata->process_char_map);
1601 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (L_IEXTEN(tty)) {
1603 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001604 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001606 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001608 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (L_ECHO(tty))
1610 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001611 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613 }
1614 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001615 set_bit(START_CHAR(tty), ldata->process_char_map);
1616 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001619 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1620 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1621 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001623 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001624 ldata->raw = 0;
1625 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001627 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1629 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1630 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001631 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001633 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001635 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001636 /*
1637 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1638 * been stopped by STOP_CHAR(tty) before it.
1639 */
1640 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1641 start_tty(tty);
1642 }
1643
Alan Coxf34d7a52008-04-30 00:54:13 -07001644 /* The termios change make the tty ready for I/O */
1645 wake_up_interruptible(&tty->write_wait);
1646 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/**
1650 * n_tty_close - close the ldisc for this tty
1651 * @tty: device
1652 *
Alan Cox4edf1822008-02-08 04:18:44 -08001653 * Called from the terminal layer when this line discipline is
1654 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * discipline change. The function will not be called while other
1656 * ldisc methods are in progress.
1657 */
Alan Cox4edf1822008-02-08 04:18:44 -08001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static void n_tty_close(struct tty_struct *tty)
1660{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001661 struct n_tty_data *ldata = tty->disc_data;
1662
Peter Hurley79901312013-03-11 16:44:23 -04001663 if (tty->link)
1664 n_tty_packet_mode_flush(tty);
1665
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001666 kfree(ldata->read_buf);
1667 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001668 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001669 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/**
1673 * n_tty_open - open an ldisc
1674 * @tty: terminal to open
1675 *
Alan Cox4edf1822008-02-08 04:18:44 -08001676 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * terminal device. Can sleep. Called serialized so that no
1678 * other events will occur in parallel. No further open will occur
1679 * until a close.
1680 */
1681
1682static int n_tty_open(struct tty_struct *tty)
1683{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001684 struct n_tty_data *ldata;
1685
1686 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1687 if (!ldata)
1688 goto err;
1689
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001690 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001691 mutex_init(&ldata->atomic_read_lock);
1692 mutex_init(&ldata->output_lock);
1693 mutex_init(&ldata->echo_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001694
Joe Petersona88a69c2009-01-02 13:40:53 +00001695 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001696 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1697 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1698 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001699 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001700
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001701 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001702 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001703 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001704 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001706 /* indicate buffer work may resume */
1707 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1708 n_tty_set_termios(tty, NULL);
1709 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001712err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001713 kfree(ldata->read_buf);
1714 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001715 kfree(ldata);
1716err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001717 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720static inline int input_available_p(struct tty_struct *tty, int amt)
1721{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001722 struct n_tty_data *ldata = tty->disc_data;
1723
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001724 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001725 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurleya73d3d62013-06-15 09:14:25 -04001726 if (ldata->canon_head != ldata->read_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return 1;
Peter Hurleyce741172013-06-15 09:14:20 -04001728 } else if (read_cnt(ldata) >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 1;
1730
1731 return 0;
1732}
1733
1734/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001735 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * @tty: terminal device
1737 * @b: user data
1738 * @nr: size of data
1739 *
Alan Cox11a96d12008-10-13 10:46:24 +01001740 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * ICANON is off; it copies characters straight from the tty queue to
1742 * user space directly. It can be profitably called twice; once to
1743 * drain the space from the tail pointer to the (physical) end of the
1744 * buffer, and once to drain the space from the (physical) beginning of
1745 * the buffer to head pointer.
1746 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001747 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001749 * n_tty_read()/consumer path:
1750 * caller holds non-exclusive termios_rwsem
1751 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Alan Cox4edf1822008-02-08 04:18:44 -08001753
Alan Cox33f0f882006-01-09 20:54:13 -08001754static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 unsigned char __user **b,
1756 size_t *nr)
1757
1758{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001759 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int retval;
1761 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001762 bool is_eof;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001763 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 retval = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001766 n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (n) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001769 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 n -= retval;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001771 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
1772 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001773 ldata->icanon);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001774 ldata->read_tail += n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001775 /* Turn single EOF into zero-length read */
Peter Hurleyce741172013-06-15 09:14:20 -04001776 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001777 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 *b += n;
1779 *nr -= n;
1780 }
1781 return retval;
1782}
1783
Peter Hurley88bb0de2013-06-15 09:14:16 -04001784/**
Peter Hurley32f13522013-06-15 09:14:17 -04001785 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001786 * @tty: terminal device
1787 * @b: user data
1788 * @nr: size of data
1789 *
1790 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001791 * it copies one line of input up to and including the line-delimiting
1792 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001793 *
1794 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04001795 *
1796 * n_tty_read()/consumer path:
1797 * caller holds non-exclusive termios_rwsem
1798 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04001799 */
1800
Peter Hurley32f13522013-06-15 09:14:17 -04001801static int canon_copy_from_read_buf(struct tty_struct *tty,
1802 unsigned char __user **b,
1803 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04001804{
1805 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04001806 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001807 size_t eol;
1808 size_t tail;
1809 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001810
1811 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyce741172013-06-15 09:14:20 -04001812 n = min(*nr, read_cnt(ldata));
Peter Hurley6d76bd22013-06-15 09:14:26 -04001813 if (!n)
Peter Hurley32f13522013-06-15 09:14:17 -04001814 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001815
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001816 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04001817 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1818
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001819 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04001820 __func__, *nr, tail, n, size);
1821
1822 eol = find_next_bit(ldata->read_flags, size, tail);
1823 more = n - (size - tail);
1824 if (eol == N_TTY_BUF_SIZE && more) {
1825 /* scan wrapped without finding set bit */
1826 eol = find_next_bit(ldata->read_flags, more, 0);
1827 if (eol != more)
1828 found = 1;
1829 } else if (eol != size)
1830 found = 1;
1831
1832 size = N_TTY_BUF_SIZE - tail;
1833 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
1834 c = n;
1835
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001836 if (found && read_buf(ldata, eol) == __DISABLED_CHAR)
Peter Hurley32f13522013-06-15 09:14:17 -04001837 n--;
1838
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001839 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04001840 __func__, eol, found, n, c, size, more);
1841
Peter Hurley32f13522013-06-15 09:14:17 -04001842 if (n > size) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001843 ret = copy_to_user(*b, read_buf_addr(ldata, tail), size);
Peter Hurley32f13522013-06-15 09:14:17 -04001844 if (ret)
1845 return -EFAULT;
1846 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
1847 } else
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001848 ret = copy_to_user(*b, read_buf_addr(ldata, tail), n);
Peter Hurley32f13522013-06-15 09:14:17 -04001849
1850 if (ret)
1851 return -EFAULT;
1852 *b += n;
1853 *nr -= n;
1854
Peter Hurleya73d3d62013-06-15 09:14:25 -04001855 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04001856 clear_bit(eol, ldata->read_flags);
1857 smp_mb__after_clear_bit();
1858 ldata->read_tail += c;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001859
Peter Hurley32f13522013-06-15 09:14:17 -04001860 if (found)
1861 tty_audit_push(tty);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001862 return 0;
1863}
1864
Al Virocc4191d2008-03-29 03:08:48 +00001865extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001866 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868/**
1869 * job_control - check job control
1870 * @tty: tty
1871 * @file: file handle
1872 *
1873 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001874 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001876 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001877 * Locking: redirected write test is safe
1878 * current->signal->tty check is safe
1879 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Alan Cox4edf1822008-02-08 04:18:44 -08001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882static int job_control(struct tty_struct *tty, struct file *file)
1883{
1884 /* Job control check -- must be done at start and after
1885 every sleep (POSIX.1 7.1.1.4). */
1886 /* NOTE: not yet done after every sleep pending a thorough
1887 check of the logic of this change. -- jlc */
1888 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001889 if (file->f_op->write == redirected_tty_write ||
1890 current->signal->tty != tty)
1891 return 0;
1892
1893 spin_lock_irq(&tty->ctrl_lock);
1894 if (!tty->pgrp)
1895 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1896 else if (task_pgrp(current) != tty->pgrp) {
1897 spin_unlock_irq(&tty->ctrl_lock);
1898 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1899 return -EIO;
1900 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1901 set_thread_flag(TIF_SIGPENDING);
1902 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001904 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 0;
1906}
Alan Cox4edf1822008-02-08 04:18:44 -08001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909/**
Alan Cox11a96d12008-10-13 10:46:24 +01001910 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * @tty: tty device
1912 * @file: file object
1913 * @buf: userspace buffer pointer
1914 * @nr: size of I/O
1915 *
1916 * Perform reads for the line discipline. We are guaranteed that the
1917 * line discipline will not be closed under us but we may get multiple
1918 * parallel readers and must handle this ourselves. We may also get
1919 * a hangup. Always called in user context, may sleep.
1920 *
1921 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001922 *
1923 * n_tty_read()/consumer path:
1924 * claims non-exclusive termios_rwsem
1925 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Alan Cox4edf1822008-02-08 04:18:44 -08001927
Alan Cox11a96d12008-10-13 10:46:24 +01001928static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 unsigned char __user *buf, size_t nr)
1930{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001931 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 unsigned char __user *b = buf;
1933 DECLARE_WAITQUEUE(wait, current);
1934 int c;
1935 int minimum, time;
1936 ssize_t retval = 0;
1937 ssize_t size;
1938 long timeout;
1939 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001940 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001944 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001946
Peter Hurley9356b532013-06-15 09:14:24 -04001947 down_read(&tty->termios_rwsem);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 minimum = time = 0;
1950 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001951 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 minimum = MIN_CHAR(tty);
1953 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04001954 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001956 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001958 (ldata->minimum_to_wake > minimum))
1959 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04001961 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001962 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 }
1965
1966 /*
1967 * Internal serialization of reads.
1968 */
1969 if (file->f_flags & O_NONBLOCK) {
Peter Hurley9356b532013-06-15 09:14:24 -04001970 if (!mutex_trylock(&ldata->atomic_read_lock)) {
1971 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EAGAIN;
Peter Hurley9356b532013-06-15 09:14:24 -04001973 }
Alan Cox4edf1822008-02-08 04:18:44 -08001974 } else {
Peter Hurley9356b532013-06-15 09:14:24 -04001975 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) {
1976 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -ERESTARTSYS;
Peter Hurley9356b532013-06-15 09:14:24 -04001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Alan Cox04f378b2008-04-30 00:53:29 -07001980 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 while (nr) {
1984 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001985 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned char cs;
1987 if (b != buf)
1988 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001989 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 cs = tty->link->ctrl_status;
1991 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001992 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001993 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 retval = -EFAULT;
1995 b--;
1996 break;
1997 }
1998 nr--;
1999 break;
2000 }
2001 /* This statement must be first before checking for input
2002 so that any interrupt will set the state back to
2003 TASK_RUNNING. */
2004 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08002005
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002006 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002008 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (!input_available_p(tty, 0)) {
2011 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2012 retval = -EIO;
2013 break;
2014 }
2015 if (tty_hung_up_p(file))
2016 break;
2017 if (!timeout)
2018 break;
2019 if (file->f_flags & O_NONBLOCK) {
2020 retval = -EAGAIN;
2021 break;
2022 }
2023 if (signal_pending(current)) {
2024 retval = -ERESTARTSYS;
2025 break;
2026 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09002027 n_tty_set_room(tty);
Peter Hurley9356b532013-06-15 09:14:24 -04002028 up_read(&tty->termios_rwsem);
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 timeout = schedule_timeout(timeout);
Peter Hurley9356b532013-06-15 09:14:24 -04002031
2032 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 continue;
2034 }
2035 __set_current_state(TASK_RUNNING);
2036
2037 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07002038 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07002039 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 retval = -EFAULT;
2041 b--;
2042 break;
2043 }
2044 nr--;
2045 }
2046
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002047 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002048 retval = canon_copy_from_read_buf(tty, &b, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (retval)
2050 break;
2051 } else {
2052 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07002053 /* The copy function takes the read lock and handles
2054 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 uncopied = copy_from_read_buf(tty, &b, &nr);
2056 uncopied += copy_from_read_buf(tty, &b, &nr);
2057 if (uncopied) {
2058 retval = -EFAULT;
2059 break;
2060 }
2061 }
2062
2063 /* If there is enough space in the read buffer now, let the
Peter Hurleya19d0c62013-06-15 09:14:18 -04002064 * low-level driver know. We use chars_in_buffer() to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * check the buffer, as it now knows about canonical mode.
2066 * Otherwise, if the driver is throttled and the line is
2067 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
2068 * we won't get any more characters.
2069 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05002070 while (1) {
Peter Hurley9356b532013-06-15 09:14:24 -04002071 int unthrottled;
Peter Hurleye91e52e2013-03-06 08:20:53 -05002072 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
Peter Hurleya19d0c62013-06-15 09:14:18 -04002073 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05002074 break;
2075 if (!tty->count)
2076 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002077 n_tty_set_room(tty);
Peter Hurley9356b532013-06-15 09:14:24 -04002078 up_read(&tty->termios_rwsem);
2079 unthrottled = tty_unthrottle_safe(tty);
2080 down_read(&tty->termios_rwsem);
2081 if (!unthrottled)
Peter Hurleye91e52e2013-03-06 08:20:53 -05002082 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09002083 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05002084 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 if (b - buf >= minimum)
2087 break;
2088 if (time)
2089 timeout = time;
2090 }
Jiri Slabybddc7152012-10-18 22:26:42 +02002091 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 remove_wait_queue(&tty->read_wait, &wait);
2093
2094 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002095 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 __set_current_state(TASK_RUNNING);
2098 size = b - buf;
2099 if (size) {
2100 retval = size;
2101 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08002102 clear_bit(TTY_PUSH, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002103 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) {
2104 up_read(&tty->termios_rwsem);
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01002105 goto do_it_again;
Peter Hurley9356b532013-06-15 09:14:24 -04002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Linus Torvalds55db4c62011-06-04 06:33:24 +09002108 n_tty_set_room(tty);
Peter Hurley9356b532013-06-15 09:14:24 -04002109 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return retval;
2111}
2112
2113/**
Alan Cox11a96d12008-10-13 10:46:24 +01002114 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * @tty: tty device
2116 * @file: file object
2117 * @buf: userspace buffer pointer
2118 * @nr: size of I/O
2119 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002120 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002122 * and other such events. Since the receive code will echo characters,
2123 * thus calling driver write methods, the output_lock is used in
2124 * the output processing functions called here as well as in the
2125 * echo processing function to protect the column state and space
2126 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 *
2128 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002129 *
2130 * Locking: output_lock to protect column state and space left
2131 * (note that the process_output*() functions take this
2132 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 */
Alan Cox4edf1822008-02-08 04:18:44 -08002134
Alan Cox11a96d12008-10-13 10:46:24 +01002135static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002136 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 const unsigned char *b = buf;
2139 DECLARE_WAITQUEUE(wait, current);
2140 int c;
2141 ssize_t retval = 0;
2142
2143 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2144 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2145 retval = tty_check_change(tty);
2146 if (retval)
2147 return retval;
2148 }
2149
Peter Hurley9356b532013-06-15 09:14:24 -04002150 down_read(&tty->termios_rwsem);
2151
Joe Petersona88a69c2009-01-02 13:40:53 +00002152 /* Write out any echoed characters that are still pending */
2153 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 add_wait_queue(&tty->write_wait, &wait);
2156 while (1) {
2157 set_current_state(TASK_INTERRUPTIBLE);
2158 if (signal_pending(current)) {
2159 retval = -ERESTARTSYS;
2160 break;
2161 }
2162 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2163 retval = -EIO;
2164 break;
2165 }
Peter Hurley582f5592013-05-17 12:49:48 -04002166 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002168 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (num < 0) {
2170 if (num == -EAGAIN)
2171 break;
2172 retval = num;
2173 goto break_out;
2174 }
2175 b += num;
2176 nr -= num;
2177 if (nr == 0)
2178 break;
2179 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002180 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 break;
2182 b++; nr--;
2183 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002184 if (tty->ops->flush_chars)
2185 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002187 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002188 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002189 if (c < 0) {
2190 retval = c;
2191 goto break_out;
2192 }
2193 if (!c)
2194 break;
2195 b += c;
2196 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199 if (!nr)
2200 break;
2201 if (file->f_flags & O_NONBLOCK) {
2202 retval = -EAGAIN;
2203 break;
2204 }
Peter Hurley9356b532013-06-15 09:14:24 -04002205 up_read(&tty->termios_rwsem);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 schedule();
Peter Hurley9356b532013-06-15 09:14:24 -04002208
2209 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211break_out:
2212 __set_current_state(TASK_RUNNING);
2213 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002214 if (b - buf != nr && tty->fasync)
2215 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002216 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return (b - buf) ? b - buf : retval;
2218}
2219
2220/**
Alan Cox11a96d12008-10-13 10:46:24 +01002221 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * @tty: terminal device
2223 * @file: file accessing it
2224 * @wait: poll table
2225 *
2226 * Called when the line discipline is asked to poll() for data or
2227 * for special events. This code is not serialized with respect to
2228 * other events save open/close.
2229 *
2230 * This code must be sure never to sleep through a hangup.
2231 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 */
Alan Cox4edf1822008-02-08 04:18:44 -08002233
Alan Cox11a96d12008-10-13 10:46:24 +01002234static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002235 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002237 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 unsigned int mask = 0;
2239
2240 poll_wait(file, &tty->read_wait, wait);
2241 poll_wait(file, &tty->write_wait, wait);
2242 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2243 mask |= POLLIN | POLLRDNORM;
2244 if (tty->packet && tty->link->ctrl_status)
2245 mask |= POLLPRI | POLLIN | POLLRDNORM;
2246 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2247 mask |= POLLHUP;
2248 if (tty_hung_up_p(file))
2249 mask |= POLLHUP;
2250 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2251 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002252 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002254 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002256 if (tty->ops->write && !tty_is_writelocked(tty) &&
2257 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2258 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 mask |= POLLOUT | POLLWRNORM;
2260 return mask;
2261}
2262
Jiri Slaby57c94122012-10-18 22:26:43 +02002263static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002264{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002265 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002266
Peter Hurleya73d3d62013-06-15 09:14:25 -04002267 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002268 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002269 head = ldata->canon_head;
2270 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002271 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002272 /* Skip EOF-chars.. */
2273 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002274 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2275 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002276 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002277 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002278 }
2279 return nr;
2280}
2281
2282static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2283 unsigned int cmd, unsigned long arg)
2284{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002285 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002286 int retval;
2287
2288 switch (cmd) {
2289 case TIOCOUTQ:
2290 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2291 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002292 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002293 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002294 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002295 else
2296 retval = read_cnt(ldata);
2297 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002298 return put_user(retval, (unsigned int __user *) arg);
2299 default:
2300 return n_tty_ioctl_helper(tty, file, cmd, arg);
2301 }
2302}
2303
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002304static void n_tty_fasync(struct tty_struct *tty, int on)
2305{
2306 struct n_tty_data *ldata = tty->disc_data;
2307
2308 if (!waitqueue_active(&tty->read_wait)) {
2309 if (on)
2310 ldata->minimum_to_wake = 1;
2311 else if (!tty->fasync)
2312 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2313 }
2314}
2315
Alan Coxa352def2008-07-16 21:53:12 +01002316struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002317 .magic = TTY_LDISC_MAGIC,
2318 .name = "n_tty",
2319 .open = n_tty_open,
2320 .close = n_tty_close,
2321 .flush_buffer = n_tty_flush_buffer,
2322 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002323 .read = n_tty_read,
2324 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002325 .ioctl = n_tty_ioctl,
2326 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002327 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002328 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002329 .write_wakeup = n_tty_write_wakeup,
2330 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002331 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002333
2334/**
2335 * n_tty_inherit_ops - inherit N_TTY methods
2336 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2337 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002338 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002339 * methods.
2340 */
2341
2342void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2343{
2344 *ops = tty_ldisc_N_TTY;
2345 ops->owner = NULL;
2346 ops->refcount = ops->flags = 0;
2347}
2348EXPORT_SYMBOL_GPL(n_tty_inherit_ops);