blob: 0475f9685a41d68d08a02a99e606bd4d3c4c5df9 [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>
Peter Hurley86e35ae2013-07-24 09:30:05 -040053#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* number of characters left in xmit buffer before select has we have room */
57#define WAKEUP_CHARS 256
58
59/*
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
63 */
64#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010065#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Joe Petersona88a69c2009-01-02 13:40:53 +000067/*
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
71 * codes.
72 */
73#define ECHO_OP_START 0xff
74#define ECHO_OP_MOVE_BACK_COL 0x80
75#define ECHO_OP_SET_CANON_COL 0x81
76#define ECHO_OP_ERASE_TAB 0x82
77
Peter Hurleycbfd0342013-06-15 10:04:26 -040078#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
Peter Hurley32f13522013-06-15 09:14:17 -040083#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
87# define n_tty_trace(f, args...)
88#endif
89
Jiri Slaby70ece7a2012-10-18 22:26:38 +020090struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040091 /* producer-published */
92 size_t read_head;
Peter Hurley70aca712015-01-16 15:05:37 -050093 size_t commit_head;
Peter Hurleyfb7aa032013-06-15 09:14:30 -040094 size_t canon_head;
Peter Hurley9dfd16d2013-06-15 10:04:29 -040095 size_t echo_head;
96 size_t echo_commit;
Peter Hurley1075a6e2013-12-09 18:06:07 -050097 size_t echo_mark;
Peter Hurley1bb9d562013-06-15 10:21:20 -040098 DECLARE_BITMAP(char_map, 256);
Peter Hurleyfb7aa032013-06-15 09:14:30 -040099
100 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200101 unsigned long overrun_time;
102 int num_overrun;
103
Peter Hurley24a89d12013-06-15 09:14:15 -0400104 /* non-atomic */
105 bool no_room;
106
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400107 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
Peter Hurley4d0ed182013-12-10 17:12:02 -0500109 unsigned char push:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200110
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400111 /* shared by producer and consumer */
Peter Hurley20bafb32013-06-15 10:21:19 -0400112 char read_buf[N_TTY_BUF_SIZE];
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Peter Hurley20bafb32013-06-15 10:21:19 -0400114 unsigned char echo_buf[N_TTY_BUF_SIZE];
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200115
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400116 /* consumer-published */
117 size_t read_tail;
Peter Hurley40d5e092013-06-15 10:21:17 -0400118 size_t line_start;
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400119
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400120 /* protected by output lock */
121 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200122 unsigned int canon_column;
Peter Hurley9dfd16d2013-06-15 10:04:29 -0400123 size_t echo_tail;
Jiri Slabybddc7152012-10-18 22:26:42 +0200124
125 struct mutex atomic_read_lock;
126 struct mutex output_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200127};
128
Tetsuo Handa947dead2018-05-26 09:53:13 +0900129#define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
130
Peter Hurleyce741172013-06-15 09:14:20 -0400131static inline size_t read_cnt(struct n_tty_data *ldata)
132{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400133 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400134}
135
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400136static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
137{
138 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
139}
140
141static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
142{
143 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
144}
145
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400146static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
147{
Tetsuo Handa9264e982018-05-26 09:53:14 +0900148 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400149 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
150}
151
152static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
153{
154 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
155}
156
Peter Hurley679e7c22015-11-27 14:11:02 -0500157static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
158 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700159{
160 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500161 size_t size = N_TTY_BUF_SIZE - tail;
162 const void *from = read_buf_addr(ldata, tail);
163 int uncopied;
164
165 if (n > size) {
Peter Hurley309426a2016-01-09 22:55:27 -0800166 tty_audit_add_data(tty, from, size);
Peter Hurley679e7c22015-11-27 14:11:02 -0500167 uncopied = copy_to_user(to, from, size);
168 if (uncopied)
169 return uncopied;
170 to += size;
171 n -= size;
172 from = ldata->read_buf;
173 }
Laura Abbott72586c62015-05-14 11:42:17 -0700174
Peter Hurley309426a2016-01-09 22:55:27 -0800175 tty_audit_add_data(tty, from, n);
Laura Abbott72586c62015-05-14 11:42:17 -0700176 return copy_to_user(to, from, n);
177}
178
Peter Hurley24a89d12013-06-15 09:14:15 -0400179/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500180 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400181 * @tty: terminal
182 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500183 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400184 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400185 * Caller holds exclusive termios_rwsem
186 * or
187 * n_tty_read()/consumer path:
188 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400189 */
190
Peter Hurley2c5dc462015-01-16 15:05:34 -0500191static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400192{
Peter Hurley24a89d12013-06-15 09:14:15 -0400193 struct n_tty_data *ldata = tty->disc_data;
194
Peter Hurley2c5dc462015-01-16 15:05:34 -0500195 /* Did the input worker stop? Restart it */
196 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400197 ldata->no_room = 0;
198
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200199 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400200 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400201 /* see if ldisc has been killed - if so, this means that
202 * even though the ldisc has been halted and ->buf.work
203 * cancelled, ->buf.work is about to be rescheduled
204 */
205 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
206 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400207 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200208 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900209}
210
Peter Hurley9a4aec22013-06-15 09:14:32 -0400211static ssize_t chars_in_buffer(struct tty_struct *tty)
212{
213 struct n_tty_data *ldata = tty->disc_data;
214 ssize_t n = 0;
215
216 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500217 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400218 else
219 n = ldata->canon_head - ldata->read_tail;
220 return n;
221}
222
Peter Hurleyee0bab82013-06-15 09:14:34 -0400223/**
224 * n_tty_write_wakeup - asynchronous I/O notifier
225 * @tty: tty device
226 *
227 * Required for the ptys, serial driver etc. since processes
228 * that attach themselves to the master and rely on ASYNC
229 * IO must be woken up
230 */
231
232static void n_tty_write_wakeup(struct tty_struct *tty)
233{
Peter Hurley7bccc362016-01-09 21:45:12 -0800234 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
235 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Peter Hurleyee0bab82013-06-15 09:14:34 -0400236}
237
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400238static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400239{
Peter Hurleya3428462015-01-16 15:05:35 -0500240 struct n_tty_data *ldata = tty->disc_data;
241
Peter Hurley6367ca72013-06-15 09:14:33 -0400242 /*
243 * Check the remaining room for the input canonicalization
244 * mode. We don't want to throttle the driver if we're in
245 * canonical mode and don't have a newline yet!
246 */
Peter Hurleya3428462015-01-16 15:05:35 -0500247 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
248 return;
249
Peter Hurley6367ca72013-06-15 09:14:33 -0400250 while (1) {
251 int throttled;
252 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500253 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400254 break;
255 throttled = tty_throttle_safe(tty);
256 if (!throttled)
257 break;
258 }
259 __tty_set_flow_change(tty, 0);
260}
261
Peter Hurley4b293492013-07-24 08:29:55 -0400262static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400263{
Peter Hurley6d27a632016-01-10 22:40:56 -0800264 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400265 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
266 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500267 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800268 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400269 return;
270 }
271
Peter Hurley6367ca72013-06-15 09:14:33 -0400272 /* If there is enough space in the read buffer now, let the
273 * low-level driver know. We use chars_in_buffer() to
274 * check the buffer, as it now knows about canonical mode.
275 * Otherwise, if the driver is throttled and the line is
276 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
277 * we won't get any more characters.
278 */
279
280 while (1) {
281 int unthrottled;
282 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
283 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
284 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500285 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400286 unthrottled = tty_unthrottle_safe(tty);
287 if (!unthrottled)
288 break;
289 }
290 __tty_set_flow_change(tty, 0);
291}
292
Alan Cox17b82062008-10-13 10:45:06 +0100293/**
294 * put_tty_queue - add character to tty
295 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200296 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100297 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400298 * Add a character to the tty read_buf queue.
299 *
300 * n_tty_receive_buf()/producer path:
301 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100302 */
303
Peter Hurley19e2ad62013-07-24 08:29:54 -0400304static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100306 *read_buf_addr(ldata, ldata->read_head) = c;
307 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * reset_buffer_flags - reset buffer state
312 * @tty: terminal to reset
313 *
Peter Hurley25518c62013-03-11 16:44:31 -0400314 * Reset the read buffer counters and clear the flags.
315 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100316 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400317 * Locking: caller holds exclusive termios_rwsem
318 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000320
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400321static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400323 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500324 ldata->commit_head = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400325 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000326
Peter Hurleya73d3d62013-06-15 09:14:25 -0400327 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200328 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500329 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Peter Hurleya30737a2013-03-11 16:44:22 -0400332static void n_tty_packet_mode_flush(struct tty_struct *tty)
333{
334 unsigned long flags;
335
Peter Hurleya30737a2013-03-11 16:44:22 -0400336 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400337 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400338 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400339 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000340 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400341 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/**
345 * n_tty_flush_buffer - clean input queue
346 * @tty: terminal device
347 *
Peter Hurley25518c62013-03-11 16:44:31 -0400348 * Flush the input buffer. Called when the tty layer wants the
349 * buffer flushed (eg at hangup) or when the N_TTY line discipline
350 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400352 * Holds termios_rwsem to exclude producer/consumer while
353 * buffer indices are reset.
354 *
355 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Alan Cox4edf1822008-02-08 04:18:44 -0800357
358static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400360 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400361 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500362 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800363
Peter Hurleya30737a2013-03-11 16:44:22 -0400364 if (tty->link)
365 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400366 up_write(&tty->termios_rwsem);
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * is_utf8_continuation - utf8 multibyte check
371 * @c: byte to check
372 *
373 * Returns true if the utf8 character 'c' is a multibyte continuation
374 * character. We use this to correctly compute the on screen size
375 * of the character when printing
376 */
Alan Cox4edf1822008-02-08 04:18:44 -0800377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static inline int is_utf8_continuation(unsigned char c)
379{
380 return (c & 0xc0) == 0x80;
381}
382
383/**
384 * is_continuation - multibyte check
385 * @c: byte to check
386 *
387 * Returns true if the utf8 character 'c' is a multibyte continuation
388 * character and the terminal is in unicode mode.
389 */
Alan Cox4edf1822008-02-08 04:18:44 -0800390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static inline int is_continuation(unsigned char c, struct tty_struct *tty)
392{
393 return I_IUTF8(tty) && is_utf8_continuation(c);
394}
395
396/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000397 * do_output_char - output one character
398 * @c: character (or partial unicode symbol)
399 * @tty: terminal device
400 * @space: space available in tty driver write buffer
401 *
402 * This is a helper function that handles one output character
403 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600404 * doing OPOST processing and putting the results in the
405 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000406 *
407 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
408 * and NLDLY. They simply aren't relevant in the world today.
409 * If you ever need them, add them here.
410 *
411 * Returns the number of bytes of buffer space used or -1 if
412 * no space left.
413 *
414 * Locking: should be called under the output_lock to protect
415 * the column state and space left in the buffer
416 */
417
418static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
419{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200420 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000421 int spaces;
422
423 if (!space)
424 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000425
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 switch (c) {
427 case '\n':
428 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200429 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000430 if (O_ONLCR(tty)) {
431 if (space < 2)
432 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200433 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700434 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000435 return 2;
436 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200437 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 break;
439 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200440 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000441 return 0;
442 if (O_OCRNL(tty)) {
443 c = '\n';
444 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200445 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000446 break;
447 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200448 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000449 break;
450 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200451 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 if (O_TABDLY(tty) == XTABS) {
453 if (space < spaces)
454 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200455 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000456 tty->ops->write(tty, " ", spaces);
457 return spaces;
458 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200459 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000460 break;
461 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200462 if (ldata->column > 0)
463 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000464 break;
465 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000466 if (!iscntrl(c)) {
467 if (O_OLCUC(tty))
468 c = toupper(c);
469 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200470 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000471 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000472 break;
473 }
474
475 tty_put_char(tty, c);
476 return 1;
477}
478
479/**
480 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * @c: character (or partial unicode symbol)
482 * @tty: terminal device
483 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600484 * Output one character with OPOST processing.
485 * Returns -1 when the output device is full and the character
486 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000488 * Locking: output_lock to protect column state and space left
489 * (also, this is called from n_tty_write under the
490 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Alan Cox4edf1822008-02-08 04:18:44 -0800492
Joe Petersona88a69c2009-01-02 13:40:53 +0000493static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jiri Slabybddc7152012-10-18 22:26:42 +0200495 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000496 int space, retval;
497
Jiri Slabybddc7152012-10-18 22:26:42 +0200498 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Alan Coxf34d7a52008-04-30 00:54:13 -0700500 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000501 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Jiri Slabybddc7152012-10-18 22:26:42 +0200503 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000504 if (retval < 0)
505 return -1;
506 else
507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000511 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600513 * @buf: character buffer
514 * @nr: number of bytes to output
515 *
516 * Output a block of characters with OPOST processing.
517 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *
519 * This path is used to speed up block console writes, among other
520 * things when processing blocks of output data. It handles only
521 * the simple cases normally found and helps to generate blocks of
522 * symbols for the console driver and thus improve performance.
523 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000524 * Locking: output_lock to protect column state and space left
525 * (also, this is called from n_tty_write under the
526 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Alan Cox4edf1822008-02-08 04:18:44 -0800528
Joe Petersona88a69c2009-01-02 13:40:53 +0000529static ssize_t process_output_block(struct tty_struct *tty,
530 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200532 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100534 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 const unsigned char *cp;
536
Jiri Slabybddc7152012-10-18 22:26:42 +0200537 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000538
Alan Coxf34d7a52008-04-30 00:54:13 -0700539 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000540 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200541 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (nr > space)
545 nr = space;
546
547 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000548 unsigned char c = *cp;
549
550 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 case '\n':
552 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200553 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (O_ONLCR(tty))
555 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200556 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200559 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto break_out;
561 if (O_OCRNL(tty))
562 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200563 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case '\t':
566 goto break_out;
567 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200568 if (ldata->column > 0)
569 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000572 if (!iscntrl(c)) {
573 if (O_OLCUC(tty))
574 goto break_out;
575 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200576 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 }
580 }
581break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700582 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000583
Jiri Slabybddc7152012-10-18 22:26:42 +0200584 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return i;
586}
587
Joe Petersona88a69c2009-01-02 13:40:53 +0000588/**
589 * process_echoes - write pending echo characters
590 * @tty: terminal device
591 *
592 * Write previously buffered echo (and other ldisc-generated)
593 * characters to the tty.
594 *
595 * Characters generated by the ldisc (including echoes) need to
596 * be buffered because the driver's write buffer can fill during
597 * heavy program output. Echoing straight to the driver will
598 * often fail under these conditions, causing lost characters and
599 * resulting mismatches of ldisc state information.
600 *
601 * Since the ldisc state must represent the characters actually sent
602 * to the driver at the time of the write, operations like certain
603 * changes in column state are also saved in the buffer and executed
604 * here.
605 *
606 * A circular fifo buffer is used so that the most recent characters
607 * are prioritized. Also, when control characters are echoed with a
608 * prefixed "^", the pair is treated atomically and thus not separated.
609 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400610 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000611 */
612
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400613static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000614{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200615 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400616 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400617 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000618 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000619
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400620 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000621
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400622 tail = ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900623 while (MASK(ldata->echo_commit) != MASK(tail)) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400624 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000625 if (c == ECHO_OP_START) {
626 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000627 int no_space_left = 0;
628
629 /*
Tetsuo Handa9264e982018-05-26 09:53:14 +0900630 * Since add_echo_byte() is called without holding
631 * output_lock, we might see only portion of multi-byte
632 * operation.
633 */
634 if (MASK(ldata->echo_commit) == MASK(tail + 1))
635 goto not_yet_stored;
636 /*
Joe Petersona88a69c2009-01-02 13:40:53 +0000637 * If the buffer byte is the start of a multi-byte
638 * operation, get the next byte, which is either the
639 * op code or a control character value.
640 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400641 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000642
Joe Petersona88a69c2009-01-02 13:40:53 +0000643 switch (op) {
644 unsigned int num_chars, num_bs;
645
646 case ECHO_OP_ERASE_TAB:
Tetsuo Handa9264e982018-05-26 09:53:14 +0900647 if (MASK(ldata->echo_commit) == MASK(tail + 2))
648 goto not_yet_stored;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400649 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000650
651 /*
652 * Determine how many columns to go back
653 * in order to erase the tab.
654 * This depends on the number of columns
655 * used by other characters within the tab
656 * area. If this (modulo 8) count is from
657 * the start of input rather than from a
658 * previous tab, we offset by canon column.
659 * Otherwise, tab spacing is normal.
660 */
661 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200662 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000663 num_bs = 8 - (num_chars & 7);
664
665 if (num_bs > space) {
666 no_space_left = 1;
667 break;
668 }
669 space -= num_bs;
670 while (num_bs--) {
671 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200672 if (ldata->column > 0)
673 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000674 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400675 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000676 break;
677
678 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200679 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400680 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 break;
682
683 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200684 if (ldata->column > 0)
685 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400686 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 break;
688
689 case ECHO_OP_START:
690 /* This is an escaped echo op start code */
691 if (!space) {
692 no_space_left = 1;
693 break;
694 }
695 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200696 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000697 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400698 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000699 break;
700
701 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000702 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600703 * If the op is not a special byte code,
704 * it is a ctrl char tagged to be echoed
705 * as "^X" (where X is the letter
706 * representing the control char).
707 * Note that we must ensure there is
708 * enough space for the whole ctrl pair.
709 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000710 */
Joe Peterson62b26352009-09-09 15:03:47 -0600711 if (space < 2) {
712 no_space_left = 1;
713 break;
714 }
715 tty_put_char(tty, '^');
716 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200717 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600718 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400719 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 }
721
722 if (no_space_left)
723 break;
724 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400725 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600726 int retval = do_output_char(c, tty, space);
727 if (retval < 0)
728 break;
729 space -= retval;
730 } else {
731 if (!space)
732 break;
733 tty_put_char(tty, c);
734 space -= 1;
735 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400736 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 }
739
Peter Hurleycbfd0342013-06-15 10:04:26 -0400740 /* If the echo buffer is nearly full (so that the possibility exists
741 * of echo overrun before the next commit), then discard enough
742 * data at the tail to prevent a subsequent overrun */
Tetsuo Handa9264e982018-05-26 09:53:14 +0900743 while (ldata->echo_commit > tail &&
744 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200745 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500746 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400747 tail += 3;
748 else
749 tail += 2;
750 } else
751 tail++;
752 }
753
Tetsuo Handa9264e982018-05-26 09:53:14 +0900754 not_yet_stored:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400755 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400756 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000757}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Peter Hurley17bd7902013-06-15 10:04:24 -0400759static void commit_echoes(struct tty_struct *tty)
760{
761 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400762 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400763 size_t head;
764
Tetsuo Handa9264e982018-05-26 09:53:14 +0900765 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400766 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500767 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400768 old = ldata->echo_commit - ldata->echo_tail;
769
770 /* Process committed echoes if the accumulated # of bytes
771 * is over the threshold (and try again each time another
772 * block is accumulated) */
773 nr = head - ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900774 if (nr < ECHO_COMMIT_WATERMARK ||
775 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
776 mutex_unlock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400777 return;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900778 }
Peter Hurley17bd7902013-06-15 10:04:24 -0400779
Peter Hurleycbfd0342013-06-15 10:04:26 -0400780 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400781 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400782 mutex_unlock(&ldata->output_lock);
783
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400784 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400785 tty->ops->flush_chars(tty);
786}
787
788static void process_echoes(struct tty_struct *tty)
789{
790 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400791 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400792
Peter Hurleye2613be2014-02-11 16:34:55 -0500793 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400794 return;
795
796 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500797 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400798 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400799 mutex_unlock(&ldata->output_lock);
800
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400801 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400802 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400803}
804
Peter Hurley1075a6e2013-12-09 18:06:07 -0500805/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400806static void flush_echoes(struct tty_struct *tty)
807{
808 struct n_tty_data *ldata = tty->disc_data;
809
Peter Hurley39434ab2013-11-29 12:56:10 -0500810 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
811 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400812 return;
813
814 mutex_lock(&ldata->output_lock);
815 ldata->echo_commit = ldata->echo_head;
816 __process_echoes(tty);
817 mutex_unlock(&ldata->output_lock);
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000821 * add_echo_byte - add a byte to the echo buffer
822 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200823 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000824 *
825 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000826 */
827
Peter Hurleycbfd0342013-06-15 10:04:26 -0400828static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000829{
Tetsuo Handa9264e982018-05-26 09:53:14 +0900830 *echo_buf_addr(ldata, ldata->echo_head) = c;
831 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
832 ldata->echo_head++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000833}
834
835/**
836 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200837 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 *
839 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000840 */
841
Jiri Slaby57c94122012-10-18 22:26:43 +0200842static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000843{
Jiri Slaby57c94122012-10-18 22:26:43 +0200844 add_echo_byte(ECHO_OP_START, ldata);
845 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000846}
847
848/**
849 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200850 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000851 *
852 * Add an operation to the echo buffer to set the canon column
853 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000854 */
855
Jiri Slaby57c94122012-10-18 22:26:43 +0200856static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000857{
Jiri Slaby57c94122012-10-18 22:26:43 +0200858 add_echo_byte(ECHO_OP_START, ldata);
859 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000860}
861
862/**
863 * echo_erase_tab - add operation to erase a tab
864 * @num_chars: number of character columns already used
865 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200866 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000867 *
868 * Add an operation to the echo buffer to erase a tab.
869 *
870 * Called by the eraser function, which knows how many character
871 * columns have been used since either a previous tab or the start
872 * of input. This information will be used later, along with
873 * canon column (if applicable), to go back the correct number
874 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000875 */
876
877static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200878 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000879{
Jiri Slaby57c94122012-10-18 22:26:43 +0200880 add_echo_byte(ECHO_OP_START, ldata);
881 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000882
883 /* We only need to know this modulo 8 (tab spacing) */
884 num_chars &= 7;
885
886 /* Set the high bit as a flag if num_chars is after a previous tab */
887 if (after_tab)
888 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000889
Jiri Slaby57c94122012-10-18 22:26:43 +0200890 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000891}
892
893/**
894 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * @c: unicode byte to echo
896 * @tty: terminal device
897 *
Alan Cox4edf1822008-02-08 04:18:44 -0800898 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100900 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000901 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000902 */
903
Jiri Slaby57c94122012-10-18 22:26:43 +0200904static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000905{
Joe Petersona88a69c2009-01-02 13:40:53 +0000906 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200907 add_echo_byte(ECHO_OP_START, ldata);
908 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000909 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200910 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000911 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000912}
913
914/**
915 * echo_char - echo a character
916 * @c: unicode byte to echo
917 * @tty: terminal device
918 *
919 * Echo user input back onto the screen. This must be called only when
920 * L_ECHO(tty) is true. Called from the driver receive_buf path.
921 *
Joe Peterson62b26352009-09-09 15:03:47 -0600922 * This variant tags control characters to be echoed as "^X"
923 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
925
926static void echo_char(unsigned char c, struct tty_struct *tty)
927{
Jiri Slabybddc7152012-10-18 22:26:42 +0200928 struct n_tty_data *ldata = tty->disc_data;
929
Joe Petersona88a69c2009-01-02 13:40:53 +0000930 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200931 add_echo_byte(ECHO_OP_START, ldata);
932 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000933 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600934 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200935 add_echo_byte(ECHO_OP_START, ldata);
936 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Alan Cox17b82062008-10-13 10:45:06 +0100940/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000941 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200942 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100943 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000944
Jiri Slaby57c94122012-10-18 22:26:43 +0200945static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200947 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200948 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200949 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951}
952
953/**
954 * eraser - handle erase function
955 * @c: character input
956 * @tty: terminal device
957 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200958 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * present in the stream from the driver layer. Handles the complexities
960 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100961 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400962 * n_tty_receive_buf()/producer path:
963 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Alan Cox4edf1822008-02-08 04:18:44 -0800965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static void eraser(unsigned char c, struct tty_struct *tty)
967{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200968 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400970 size_t head;
971 size_t cnt;
972 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200974 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000975 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return;
977 }
978 if (c == ERASE_CHAR(tty))
979 kill_type = ERASE;
980 else if (c == WERASE_CHAR(tty))
981 kill_type = WERASE;
982 else {
983 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200984 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return;
986 }
987 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200988 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200989 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 echo_char(KILL_CHAR(tty), tty);
991 /* Add a newline if ECHOK is on and ECHOKE is off. */
992 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200993 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return;
995 }
996 kill_type = KILL;
997 }
998
999 seen_alnums = 0;
Tetsuo Handa947dead2018-05-26 09:53:13 +09001000 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001001 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* erase a single possibly multibyte character */
1004 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001005 head--;
1006 c = read_buf(ldata, head);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001007 } while (is_continuation(c, tty) &&
1008 MASK(head) != MASK(ldata->canon_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /* do not partially erase */
1011 if (is_continuation(c, tty))
1012 break;
1013
1014 if (kill_type == WERASE) {
1015 /* Equivalent to BSD's ALTWERASE. */
1016 if (isalnum(c) || c == '_')
1017 seen_alnums++;
1018 else if (seen_alnums)
1019 break;
1020 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001021 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001022 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (L_ECHO(tty)) {
1024 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001025 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001026 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001027 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 /* if cnt > 1, output a multi-byte character */
1030 echo_char(c, tty);
1031 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001032 head++;
1033 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001034 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1037 echo_char(ERASE_CHAR(tty), tty);
1038 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001039 unsigned int num_chars = 0;
1040 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001041 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Joe Petersona88a69c2009-01-02 13:40:53 +00001043 /*
1044 * Count the columns used for characters
1045 * since the start of input or after a
1046 * previous tab.
1047 * This info is used to go back the correct
1048 * number of columns.
1049 */
Tetsuo Handa947dead2018-05-26 09:53:13 +09001050 while (MASK(tail) != MASK(ldata->canon_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001051 tail--;
1052 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001053 if (c == '\t') {
1054 after_tab = 1;
1055 break;
Alan Cox300a6202009-01-02 13:41:04 +00001056 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001058 num_chars += 2;
1059 } else if (!is_continuation(c, tty)) {
1060 num_chars++;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001063 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 } else {
1065 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001066 echo_char_raw('\b', ldata);
1067 echo_char_raw(' ', ldata);
1068 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001071 echo_char_raw('\b', ldata);
1072 echo_char_raw(' ', ldata);
1073 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 }
1076 }
1077 if (kill_type == ERASE)
1078 break;
1079 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001080 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001081 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084/**
1085 * isig - handle the ISIG optio
1086 * @sig: signal
1087 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001089 * Called when a signal is being sent due to terminal input.
1090 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001091 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001092 * Performs input and output flush if !NOFLSH. In this context, the echo
1093 * buffer is 'output'. The signal is processed first to alert any current
1094 * readers or writers to discontinue and exit their i/o loops.
1095 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001096 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Alan Cox4edf1822008-02-08 04:18:44 -08001098
Peter Hurley3b19e032015-06-27 09:21:32 -04001099static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Peter Hurley8c985d12013-03-06 08:38:19 -05001101 struct pid *tty_pgrp = tty_get_pgrp(tty);
1102 if (tty_pgrp) {
1103 kill_pgrp(tty_pgrp, sig, 1);
1104 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001106}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001107
Peter Hurley3b19e032015-06-27 09:21:32 -04001108static void isig(int sig, struct tty_struct *tty)
1109{
1110 struct n_tty_data *ldata = tty->disc_data;
1111
1112 if (L_NOFLSH(tty)) {
1113 /* signal only */
1114 __isig(sig, tty);
1115
1116 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001117 up_read(&tty->termios_rwsem);
1118 down_write(&tty->termios_rwsem);
1119
Peter Hurley3b19e032015-06-27 09:21:32 -04001120 __isig(sig, tty);
1121
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001122 /* clear echo buffer */
1123 mutex_lock(&ldata->output_lock);
1124 ldata->echo_head = ldata->echo_tail = 0;
1125 ldata->echo_mark = ldata->echo_commit = 0;
1126 mutex_unlock(&ldata->output_lock);
1127
1128 /* clear output buffer */
1129 tty_driver_flush_buffer(tty);
1130
1131 /* clear input buffer */
1132 reset_buffer_flags(tty->disc_data);
1133
1134 /* notify pty master of flush */
1135 if (tty->link)
1136 n_tty_packet_mode_flush(tty);
1137
1138 up_write(&tty->termios_rwsem);
1139 down_read(&tty->termios_rwsem);
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143/**
1144 * n_tty_receive_break - handle break
1145 * @tty: terminal
1146 *
1147 * An RS232 break event has been hit in the incoming bitstream. This
1148 * can cause a variety of events depending upon the termios settings.
1149 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001150 * n_tty_receive_buf()/producer path:
1151 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001152 *
1153 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Alan Cox4edf1822008-02-08 04:18:44 -08001155
Peter Hurley4b293492013-07-24 08:29:55 -04001156static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Jiri Slaby57c94122012-10-18 22:26:43 +02001158 struct n_tty_data *ldata = tty->disc_data;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (I_IGNBRK(tty))
1161 return;
1162 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001163 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return;
1165 }
1166 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001167 put_tty_queue('\377', ldata);
1168 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001170 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/**
1174 * n_tty_receive_overrun - handle overrun reporting
1175 * @tty: terminal
1176 *
1177 * Data arrived faster than we could process it. While the tty
1178 * driver has flagged this the bits that were missed are gone
1179 * forever.
1180 *
1181 * Called from the receive_buf path so single threaded. Does not
1182 * need locking as num_overrun and overrun_time are function
1183 * private.
1184 */
Alan Cox4edf1822008-02-08 04:18:44 -08001185
Peter Hurley4b293492013-07-24 08:29:55 -04001186static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001188 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001190 ldata->num_overrun++;
1191 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1192 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001193 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001194 ldata->overrun_time = jiffies;
1195 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197}
1198
1199/**
1200 * n_tty_receive_parity_error - error notifier
1201 * @tty: terminal device
1202 * @c: character
1203 *
1204 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001205 * the error case if necessary.
1206 *
1207 * n_tty_receive_buf()/producer path:
1208 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Peter Hurley4b293492013-07-24 08:29:55 -04001210static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Jiri Slaby57c94122012-10-18 22:26:43 +02001212 struct n_tty_data *ldata = tty->disc_data;
1213
Peter Hurley66528f92014-06-16 08:10:42 -04001214 if (I_INPCK(tty)) {
1215 if (I_IGNPAR(tty))
1216 return;
1217 if (I_PARMRK(tty)) {
1218 put_tty_queue('\377', ldata);
1219 put_tty_queue('\0', ldata);
1220 put_tty_queue(c, ldata);
1221 } else
1222 put_tty_queue('\0', ldata);
1223 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001224 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001227static void
1228n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1229{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001230 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001231 if (I_IXON(tty))
1232 start_tty(tty);
1233 if (L_ECHO(tty)) {
1234 echo_char(c, tty);
1235 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001236 } else
1237 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001238 return;
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/**
1242 * n_tty_receive_char - perform processing
1243 * @tty: terminal device
1244 * @c: character
1245 *
1246 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001247 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001249 *
1250 * n_tty_receive_buf()/producer path:
1251 * caller holds non-exclusive termios_rwsem
1252 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001253 *
1254 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
1256
Peter Hurleye60d27c2013-07-24 08:29:56 -04001257static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001258n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001260 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (I_IXON(tty)) {
1263 if (c == START_CHAR(tty)) {
1264 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001265 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 if (c == STOP_CHAR(tty)) {
1269 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 }
Joe Peterson575537b32008-04-30 00:53:30 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001275 if (c == INTR_CHAR(tty)) {
1276 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001277 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001278 } else if (c == QUIT_CHAR(tty)) {
1279 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001280 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001281 } else if (c == SUSP_CHAR(tty)) {
1282 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 }
Joe Peterson575537b32008-04-30 00:53:30 -07001286
Peter Hurley855df3c2013-07-24 08:29:50 -04001287 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1288 start_tty(tty);
1289 process_echoes(tty);
1290 }
1291
Joe Peterson575537b32008-04-30 00:53:30 -07001292 if (c == '\r') {
1293 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001294 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001295 if (I_ICRNL(tty))
1296 c = '\n';
1297 } else if (c == '\n' && I_INLCR(tty))
1298 c = '\r';
1299
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001300 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1302 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1303 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001304 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001308 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001310 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001312 echo_char_raw('^', ldata);
1313 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001314 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001317 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001319 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001320 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jiri Slaby57c94122012-10-18 22:26:43 +02001322 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001324 echo_char_raw('\n', ldata);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001325 while (MASK(tail) != MASK(ldata->read_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001326 echo_char(read_buf(ldata, tail), tty);
1327 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001329 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001333 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001334 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001335 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 goto handle_newline;
1338 }
1339 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 c = __DISABLED_CHAR;
1341 goto handle_newline;
1342 }
1343 if ((c == EOL_CHAR(tty)) ||
1344 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1345 /*
1346 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1347 */
1348 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001350 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001351 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001353 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 /*
1356 * XXX does PARMRK doubling happen for
1357 * EOL_CHAR and EOL2_CHAR?
1358 */
Peter Hurley001ba922013-12-02 14:24:44 -05001359 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001360 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Alan Cox4edf1822008-02-08 04:18:44 -08001362handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001363 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001364 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001365 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001367 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 }
Alan Cox4edf1822008-02-08 04:18:44 -08001371
Joe Petersonacc71bb2009-01-02 13:43:32 +00001372 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001375 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 else {
1377 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001378 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001379 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 echo_char(c, tty);
1381 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001382 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Peter Hurley001ba922013-12-02 14:24:44 -05001385 /* PARMRK doubling check */
1386 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001387 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Jiri Slaby57c94122012-10-18 22:26:43 +02001389 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001390 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Peter Hurleye60d27c2013-07-24 08:29:56 -04001393static inline void
1394n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001395{
1396 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001397
Peter Hurleye60d27c2013-07-24 08:29:56 -04001398 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1399 start_tty(tty);
1400 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001401 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001402 if (L_ECHO(tty)) {
1403 finish_erasing(ldata);
1404 /* Record the column of first canon char. */
1405 if (ldata->canon_head == ldata->read_head)
1406 echo_set_canon_col(ldata);
1407 echo_char(c, tty);
1408 commit_echoes(tty);
1409 }
Peter Hurley001ba922013-12-02 14:24:44 -05001410 /* PARMRK doubling check */
1411 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001412 put_tty_queue(c, ldata);
1413 put_tty_queue(c, ldata);
1414}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001415
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001416static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001417{
1418 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001419}
1420
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001421static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001422n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1423{
1424 struct n_tty_data *ldata = tty->disc_data;
1425
Peter Hurleye60d27c2013-07-24 08:29:56 -04001426 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1427 start_tty(tty);
1428 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001429 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001430 if (L_ECHO(tty)) {
1431 finish_erasing(ldata);
1432 /* Record the column of first canon char. */
1433 if (ldata->canon_head == ldata->read_head)
1434 echo_set_canon_col(ldata);
1435 echo_char(c, tty);
1436 commit_echoes(tty);
1437 }
1438 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001439}
1440
Peter Hurley8dc4b252013-12-02 14:24:43 -05001441static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001442{
1443 if (I_ISTRIP(tty))
1444 c &= 0x7f;
1445 if (I_IUCLC(tty) && L_IEXTEN(tty))
1446 c = tolower(c);
1447
1448 if (I_IXON(tty)) {
1449 if (c == STOP_CHAR(tty))
1450 stop_tty(tty);
1451 else if (c == START_CHAR(tty) ||
1452 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1453 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1454 c != SUSP_CHAR(tty))) {
1455 start_tty(tty);
1456 process_echoes(tty);
1457 }
1458 }
1459}
1460
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001461static void
1462n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1463{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001464 switch (flag) {
1465 case TTY_BREAK:
1466 n_tty_receive_break(tty);
1467 break;
1468 case TTY_PARITY:
1469 case TTY_FRAME:
1470 n_tty_receive_parity_error(tty, c);
1471 break;
1472 case TTY_OVERRUN:
1473 n_tty_receive_overrun(tty);
1474 break;
1475 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001476 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001477 break;
1478 }
1479}
1480
Peter Hurleye60d27c2013-07-24 08:29:56 -04001481static void
1482n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1483{
1484 struct n_tty_data *ldata = tty->disc_data;
1485
1486 ldata->lnext = 0;
1487 if (likely(flag == TTY_NORMAL)) {
1488 if (I_ISTRIP(tty))
1489 c &= 0x7f;
1490 if (I_IUCLC(tty) && L_IEXTEN(tty))
1491 c = tolower(c);
1492 n_tty_receive_char(tty, c);
1493 } else
1494 n_tty_receive_char_flagged(tty, c, flag);
1495}
1496
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001497static void
1498n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1499 char *fp, int count)
1500{
1501 struct n_tty_data *ldata = tty->disc_data;
1502 size_t n, head;
1503
1504 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001505 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001506 memcpy(read_buf_addr(ldata, head), cp, n);
1507 ldata->read_head += n;
1508 cp += n;
1509 count -= n;
1510
1511 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001512 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001513 memcpy(read_buf_addr(ldata, head), cp, n);
1514 ldata->read_head += n;
1515}
1516
Peter Hurley554117b2013-06-15 10:21:25 -04001517static void
1518n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1519 char *fp, int count)
1520{
1521 struct n_tty_data *ldata = tty->disc_data;
1522 char flag = TTY_NORMAL;
1523
1524 while (count--) {
1525 if (fp)
1526 flag = *fp++;
1527 if (likely(flag == TTY_NORMAL))
1528 put_tty_queue(*cp++, ldata);
1529 else
1530 n_tty_receive_char_flagged(tty, *cp++, flag);
1531 }
1532}
1533
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001534static void
1535n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1536 char *fp, int count)
1537{
1538 char flag = TTY_NORMAL;
1539
1540 while (count--) {
1541 if (fp)
1542 flag = *fp++;
1543 if (likely(flag == TTY_NORMAL))
1544 n_tty_receive_char_closing(tty, *cp++);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001545 }
1546}
1547
Peter Hurley7d88d632013-07-24 08:29:49 -04001548static void
1549n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001550 char *fp, int count)
1551{
1552 struct n_tty_data *ldata = tty->disc_data;
1553 char flag = TTY_NORMAL;
1554
1555 while (count--) {
1556 if (fp)
1557 flag = *fp++;
1558 if (likely(flag == TTY_NORMAL)) {
1559 unsigned char c = *cp++;
1560
1561 if (I_ISTRIP(tty))
1562 c &= 0x7f;
1563 if (I_IUCLC(tty) && L_IEXTEN(tty))
1564 c = tolower(c);
1565 if (L_EXTPROC(tty)) {
1566 put_tty_queue(c, ldata);
1567 continue;
1568 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001569 if (!test_bit(c, ldata->char_map))
1570 n_tty_receive_char_inline(tty, c);
1571 else if (n_tty_receive_char_special(tty, c) && count) {
1572 if (fp)
1573 flag = *fp++;
1574 n_tty_receive_char_lnext(tty, *cp++, flag);
1575 count--;
1576 }
Peter Hurley6baad002013-07-24 08:29:52 -04001577 } else
1578 n_tty_receive_char_flagged(tty, *cp++, flag);
1579 }
1580}
1581
1582static void
1583n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1584 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001585{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001586 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001587 char flag = TTY_NORMAL;
1588
1589 while (count--) {
1590 if (fp)
1591 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001592 if (likely(flag == TTY_NORMAL)) {
1593 unsigned char c = *cp++;
1594
1595 if (!test_bit(c, ldata->char_map))
1596 n_tty_receive_char_fast(tty, c);
1597 else if (n_tty_receive_char_special(tty, c) && count) {
1598 if (fp)
1599 flag = *fp++;
1600 n_tty_receive_char_lnext(tty, *cp++, flag);
1601 count--;
1602 }
1603 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001604 n_tty_receive_char_flagged(tty, *cp++, flag);
1605 }
1606}
1607
Peter Hurley24a89d12013-06-15 09:14:15 -04001608static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1609 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001611 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001612 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001614 if (ldata->real_raw)
1615 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001616 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001617 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001618 else if (tty->closing && !L_EXTPROC(tty))
1619 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001620 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001621 if (ldata->lnext) {
1622 char flag = TTY_NORMAL;
1623
1624 if (fp)
1625 flag = *fp++;
1626 n_tty_receive_char_lnext(tty, *cp++, flag);
1627 count--;
1628 }
1629
Peter Hurley7de971b2013-07-24 08:29:53 -04001630 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001631 n_tty_receive_buf_fast(tty, cp, fp, count);
1632 else
1633 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001634
1635 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001636 if (tty->ops->flush_chars)
1637 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
Peter Hurley70aca712015-01-16 15:05:37 -05001640 if (ldata->icanon && !L_EXTPROC(tty))
1641 return;
1642
1643 /* publish read_head to consumer */
1644 smp_store_release(&ldata->commit_head, ldata->read_head);
1645
Peter Hurley33d71362016-01-09 21:45:08 -08001646 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001648 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001652/**
1653 * n_tty_receive_buf_common - process input
1654 * @tty: device to receive input
1655 * @cp: input chars
1656 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1657 * @count: number of input chars in @cp
1658 *
1659 * Called by the terminal driver when a block of characters has
1660 * been received. This function must be called from soft contexts
1661 * not from interrupt context. The driver is responsible for making
1662 * calls one at a time and in order (or using flush_to_ldisc)
1663 *
1664 * Returns the # of input chars from @cp which were processed.
1665 *
1666 * In canonical mode, the maximum line length is 4096 chars (including
1667 * the line termination char); lines longer than 4096 chars are
1668 * truncated. After 4095 chars, input data is still processed but
1669 * not stored. Overflow processing ensures the tty can always
1670 * receive more input until at least one line can be read.
1671 *
1672 * In non-canonical mode, the read buffer will only accept 4095 chars;
1673 * this provides the necessary space for a newline char if the input
1674 * mode is switched to canonical.
1675 *
1676 * Note it is possible for the read buffer to _contain_ 4096 chars
1677 * in non-canonical mode: the read buffer could already contain the
1678 * maximum canon line of 4096 chars when the mode is switched to
1679 * non-canonical.
1680 *
1681 * n_tty_receive_buf()/producer path:
1682 * claims non-exclusive termios_rwsem
1683 * publishes commit_head or canon_head
1684 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001685static int
1686n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1687 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001688{
1689 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001690 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001691
Peter Hurley9356b532013-06-15 09:14:24 -04001692 down_read(&tty->termios_rwsem);
1693
Peter Hurley19e2ad62013-07-24 08:29:54 -04001694 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001695 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001696 * When PARMRK is set, each input char may take up to 3 chars
1697 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001698 *
1699 * If we are doing input canonicalization, and there are no
1700 * pending newlines, let characters through without limit, so
1701 * that erase characters will be handled. Other excess
1702 * characters will be beeped.
1703 *
1704 * paired with store in *_copy_from_read_buf() -- guarantees
1705 * the consumer has loaded the data in read_buf up to the new
1706 * read_tail (so this producer will not overwrite unread data)
1707 */
1708 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001709
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001710 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001711 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001712 room = (room + 2) / 3;
1713 room--;
1714 if (room <= 0) {
1715 overflow = ldata->icanon && ldata->canon_head == tail;
1716 if (overflow && room < 0)
1717 ldata->read_head--;
1718 room = overflow;
1719 ldata->no_room = flow && !room;
1720 } else
1721 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001722
Peter Hurley19e2ad62013-07-24 08:29:54 -04001723 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001724 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001725 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001726
1727 /* ignore parity errors if handling overflow */
1728 if (!overflow || !fp || *fp != TTY_PARITY)
1729 __receive_buf(tty, cp, fp, n);
1730
Peter Hurley19e2ad62013-07-24 08:29:54 -04001731 cp += n;
1732 if (fp)
1733 fp += n;
1734 count -= n;
1735 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001736 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001737
Peter Hurley19e2ad62013-07-24 08:29:54 -04001738 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001739
1740 /* Unthrottle if handling overflow on pty */
1741 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1742 if (overflow) {
1743 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1744 tty_unthrottle_safe(tty);
1745 __tty_set_flow_change(tty, 0);
1746 }
1747 } else
1748 n_tty_check_throttle(tty);
1749
Peter Hurley9356b532013-06-15 09:14:24 -04001750 up_read(&tty->termios_rwsem);
1751
Peter Hurley19e2ad62013-07-24 08:29:54 -04001752 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001753}
1754
Peter Hurley5c32d122013-12-02 14:24:41 -05001755static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1756 char *fp, int count)
1757{
1758 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1759}
1760
1761static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1762 char *fp, int count)
1763{
1764 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * n_tty_set_termios - termios data changed
1769 * @tty: terminal
1770 * @old: previous data
1771 *
1772 * Called by the tty layer when the user changes termios flags so
1773 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001774 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * guaranteed that this function will not be re-entered or in progress
1776 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001777 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001778 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Alan Cox4edf1822008-02-08 04:18:44 -08001780
1781static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001783 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001784
Linus Torvalds00fc57a2017-12-20 17:57:06 -08001785 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001786 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001787 ldata->line_start = ldata->read_tail;
1788 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1789 ldata->canon_head = ldata->read_tail;
1790 ldata->push = 0;
1791 } else {
1792 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1793 ldata->read_flags);
1794 ldata->canon_head = ldata->read_head;
1795 ldata->push = 1;
1796 }
Peter Hurley70aca712015-01-16 15:05:37 -05001797 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001798 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001799 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001800 }
1801
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001802 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1805 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1806 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1807 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001808 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001811 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001813 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001816 set_bit(ERASE_CHAR(tty), ldata->char_map);
1817 set_bit(KILL_CHAR(tty), ldata->char_map);
1818 set_bit(EOF_CHAR(tty), ldata->char_map);
1819 set_bit('\n', ldata->char_map);
1820 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001822 set_bit(WERASE_CHAR(tty), ldata->char_map);
1823 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1824 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (L_ECHO(tty))
1826 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001827 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
1830 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001831 set_bit(START_CHAR(tty), ldata->char_map);
1832 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001835 set_bit(INTR_CHAR(tty), ldata->char_map);
1836 set_bit(QUIT_CHAR(tty), ldata->char_map);
1837 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001839 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001840 ldata->raw = 0;
1841 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001843 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1845 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1846 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001847 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001849 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001851 /*
1852 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1853 * been stopped by STOP_CHAR(tty) before it.
1854 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001855 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001856 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001857 process_echoes(tty);
1858 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001859
Alan Coxf34d7a52008-04-30 00:54:13 -07001860 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001861 wake_up_interruptible(&tty->write_wait);
1862 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865/**
1866 * n_tty_close - close the ldisc for this tty
1867 * @tty: device
1868 *
Alan Cox4edf1822008-02-08 04:18:44 -08001869 * Called from the terminal layer when this line discipline is
1870 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * discipline change. The function will not be called while other
1872 * ldisc methods are in progress.
1873 */
Alan Cox4edf1822008-02-08 04:18:44 -08001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875static void n_tty_close(struct tty_struct *tty)
1876{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001877 struct n_tty_data *ldata = tty->disc_data;
1878
Peter Hurley79901312013-03-11 16:44:23 -04001879 if (tty->link)
1880 n_tty_packet_mode_flush(tty);
1881
Peter Hurley20bafb32013-06-15 10:21:19 -04001882 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001883 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
1886/**
1887 * n_tty_open - open an ldisc
1888 * @tty: terminal to open
1889 *
Alan Cox4edf1822008-02-08 04:18:44 -08001890 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 * terminal device. Can sleep. Called serialized so that no
1892 * other events will occur in parallel. No further open will occur
1893 * until a close.
1894 */
1895
1896static int n_tty_open(struct tty_struct *tty)
1897{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001898 struct n_tty_data *ldata;
1899
Peter Hurley20bafb32013-06-15 10:21:19 -04001900 /* Currently a malloc failure here can panic */
Tetsuo Handa9264e982018-05-26 09:53:14 +09001901 ldata = vzalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001902 if (!ldata)
Tetsuo Handa9264e982018-05-26 09:53:14 +09001903 return -ENOMEM;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001904
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001905 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001906 mutex_init(&ldata->atomic_read_lock);
1907 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001908
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001909 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001911 /* indicate buffer work may resume */
1912 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1913 n_tty_set_termios(tty, NULL);
1914 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return 0;
1916}
1917
Peter Hurleyeafbe672013-12-02 14:24:45 -05001918static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001920 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001921 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001922
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001923 if (ldata->icanon && !L_EXTPROC(tty))
1924 return ldata->canon_head != ldata->read_tail;
1925 else
Peter Hurley70aca712015-01-16 15:05:37 -05001926 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001930 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * @tty: terminal device
1932 * @b: user data
1933 * @nr: size of data
1934 *
Alan Cox11a96d12008-10-13 10:46:24 +01001935 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * ICANON is off; it copies characters straight from the tty queue to
1937 * user space directly. It can be profitably called twice; once to
1938 * drain the space from the tail pointer to the (physical) end of the
1939 * buffer, and once to drain the space from the (physical) beginning of
1940 * the buffer to head pointer.
1941 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001942 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001944 * n_tty_read()/consumer path:
1945 * caller holds non-exclusive termios_rwsem
1946 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Alan Cox4edf1822008-02-08 04:18:44 -08001948
Alan Cox33f0f882006-01-09 20:54:13 -08001949static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 unsigned char __user **b,
1951 size_t *nr)
1952
1953{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001954 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int retval;
1956 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001957 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001958 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001959 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001962 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001965 const unsigned char *from = read_buf_addr(ldata, tail);
1966 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001968 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08001969 tty_audit_add_data(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05001970 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001971 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05001972 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1973 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001974 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 *b += n;
1976 *nr -= n;
1977 }
1978 return retval;
1979}
1980
Peter Hurley88bb0de2013-06-15 09:14:16 -04001981/**
Peter Hurley32f13522013-06-15 09:14:17 -04001982 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001983 * @tty: terminal device
1984 * @b: user data
1985 * @nr: size of data
1986 *
1987 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001988 * it copies one line of input up to and including the line-delimiting
1989 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001990 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05001991 * NB: When termios is changed from non-canonical to canonical mode and
1992 * the read buffer contains data, n_tty_set_termios() simulates an EOF
1993 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
1994 * This causes data already processed as input to be immediately available
1995 * as input although a newline has not been received.
1996 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04001997 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04001998 *
1999 * n_tty_read()/consumer path:
2000 * caller holds non-exclusive termios_rwsem
2001 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002002 */
2003
Peter Hurley32f13522013-06-15 09:14:17 -04002004static int canon_copy_from_read_buf(struct tty_struct *tty,
2005 unsigned char __user **b,
2006 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002007{
2008 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002009 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002010 size_t eol;
2011 size_t tail;
2012 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002013
2014 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002015 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002016 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002017
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002018 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2019
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002020 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002021 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2022
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002023 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002024 __func__, *nr, tail, n, size);
2025
2026 eol = find_next_bit(ldata->read_flags, size, tail);
2027 more = n - (size - tail);
2028 if (eol == N_TTY_BUF_SIZE && more) {
2029 /* scan wrapped without finding set bit */
2030 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002031 found = eol != more;
2032 } else
2033 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002034
Peter Hurleyc77569d2013-11-22 07:16:25 -05002035 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002036 if (n > N_TTY_BUF_SIZE)
2037 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002038 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002039
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002040 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2041 c = min(*nr, c);
2042 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002043 }
Peter Hurley32f13522013-06-15 09:14:17 -04002044
Peter Hurley679e7c22015-11-27 14:11:02 -05002045 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2046 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002047
Peter Hurley679e7c22015-11-27 14:11:02 -05002048 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002049 if (ret)
2050 return -EFAULT;
2051 *b += n;
2052 *nr -= n;
2053
Peter Hurleya73d3d62013-06-15 09:14:25 -04002054 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002055 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002056 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002057
Peter Hurley40d5e092013-06-15 10:21:17 -04002058 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002059 if (!ldata->push)
2060 ldata->line_start = ldata->read_tail;
2061 else
2062 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002063 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002064 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002065 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002066}
2067
Al Virocc4191d2008-03-29 03:08:48 +00002068extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002069 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071/**
2072 * job_control - check job control
2073 * @tty: tty
2074 * @file: file handle
2075 *
2076 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002077 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002079 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002080 * Locking: redirected write test is safe
2081 * current->signal->tty check is safe
2082 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Alan Cox4edf1822008-02-08 04:18:44 -08002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085static int job_control(struct tty_struct *tty, struct file *file)
2086{
2087 /* Job control check -- must be done at start and after
2088 every sleep (POSIX.1 7.1.1.4). */
2089 /* NOTE: not yet done after every sleep pending a thorough
2090 check of the logic of this change. -- jlc */
2091 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002092 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002093 return 0;
2094
Peter Hurley2812d9e2015-10-10 20:28:42 -04002095 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
Alan Cox4edf1822008-02-08 04:18:44 -08002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099/**
Alan Cox11a96d12008-10-13 10:46:24 +01002100 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * @tty: tty device
2102 * @file: file object
2103 * @buf: userspace buffer pointer
2104 * @nr: size of I/O
2105 *
2106 * Perform reads for the line discipline. We are guaranteed that the
2107 * line discipline will not be closed under us but we may get multiple
2108 * parallel readers and must handle this ourselves. We may also get
2109 * a hangup. Always called in user context, may sleep.
2110 *
2111 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002112 *
2113 * n_tty_read()/consumer path:
2114 * claims non-exclusive termios_rwsem
2115 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
Alan Cox4edf1822008-02-08 04:18:44 -08002117
Alan Cox11a96d12008-10-13 10:46:24 +01002118static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 unsigned char __user *buf, size_t nr)
2120{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002121 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002123 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002124 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int minimum, time;
2126 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002128 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002129 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002132 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002134
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002135 /*
2136 * Internal serialization of reads.
2137 */
2138 if (file->f_flags & O_NONBLOCK) {
2139 if (!mutex_trylock(&ldata->atomic_read_lock))
2140 return -EAGAIN;
2141 } else {
2142 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2143 return -ERESTARTSYS;
2144 }
2145
Peter Hurley9356b532013-06-15 09:14:24 -04002146 down_read(&tty->termios_rwsem);
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 minimum = time = 0;
2149 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002150 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 minimum = MIN_CHAR(tty);
2152 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002153 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002155 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002156 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 }
2159
Alan Cox04f378b2008-04-30 00:53:29 -07002160 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002161 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 while (nr) {
2165 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002166 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 unsigned char cs;
2168 if (b != buf)
2169 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002170 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 cs = tty->link->ctrl_status;
2172 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002173 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002174 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 break;
2177 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002178 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 nr--;
2180 break;
2181 }
Alan Cox4edf1822008-02-08 04:18:44 -08002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!input_available_p(tty, 0)) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002184 up_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002185 tty_buffer_flush_work(tty->port);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002186 down_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002187 if (!input_available_p(tty, 0)) {
2188 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2189 retval = -EIO;
2190 break;
2191 }
2192 if (tty_hung_up_p(file))
2193 break;
Tejun Heoa7e19062018-02-13 07:38:08 -08002194 /*
2195 * Abort readers for ttys which never actually
2196 * get hung up. See __tty_hangup().
2197 */
2198 if (test_bit(TTY_HUPPING, &tty->flags))
2199 break;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002200 if (!timeout)
2201 break;
2202 if (file->f_flags & O_NONBLOCK) {
2203 retval = -EAGAIN;
2204 break;
2205 }
2206 if (signal_pending(current)) {
2207 retval = -ERESTARTSYS;
2208 break;
2209 }
2210 up_read(&tty->termios_rwsem);
2211
2212 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2213 timeout);
2214
2215 down_read(&tty->termios_rwsem);
2216 continue;
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002220 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002221 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002222 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 } else {
2225 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002226
2227 /* Deal with packet mode. */
2228 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002229 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002230 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002231 break;
2232 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002233 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002234 nr--;
2235 }
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 uncopied = copy_from_read_buf(tty, &b, &nr);
2238 uncopied += copy_from_read_buf(tty, &b, &nr);
2239 if (uncopied) {
2240 retval = -EFAULT;
2241 break;
2242 }
2243 }
2244
Peter Hurley6367ca72013-06-15 09:14:33 -04002245 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (b - buf >= minimum)
2248 break;
2249 if (time)
2250 timeout = time;
2251 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002252 if (tail != ldata->read_tail)
2253 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002254 up_read(&tty->termios_rwsem);
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002257 mutex_unlock(&ldata->atomic_read_lock);
2258
Peter Hurley40d5e092013-06-15 10:21:17 -04002259 if (b - buf)
2260 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 return retval;
2263}
2264
2265/**
Alan Cox11a96d12008-10-13 10:46:24 +01002266 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * @tty: tty device
2268 * @file: file object
2269 * @buf: userspace buffer pointer
2270 * @nr: size of I/O
2271 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002272 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002274 * and other such events. Since the receive code will echo characters,
2275 * thus calling driver write methods, the output_lock is used in
2276 * the output processing functions called here as well as in the
2277 * echo processing function to protect the column state and space
2278 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *
2280 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002281 *
2282 * Locking: output_lock to protect column state and space left
2283 * (note that the process_output*() functions take this
2284 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Alan Cox4edf1822008-02-08 04:18:44 -08002286
Alan Cox11a96d12008-10-13 10:46:24 +01002287static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002288 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002291 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int c;
2293 ssize_t retval = 0;
2294
2295 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2296 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2297 retval = tty_check_change(tty);
2298 if (retval)
2299 return retval;
2300 }
2301
Peter Hurley9356b532013-06-15 09:14:24 -04002302 down_read(&tty->termios_rwsem);
2303
Joe Petersona88a69c2009-01-02 13:40:53 +00002304 /* Write out any echoed characters that are still pending */
2305 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 add_wait_queue(&tty->write_wait, &wait);
2308 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (signal_pending(current)) {
2310 retval = -ERESTARTSYS;
2311 break;
2312 }
2313 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2314 retval = -EIO;
2315 break;
2316 }
Peter Hurley582f5592013-05-17 12:49:48 -04002317 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002319 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (num < 0) {
2321 if (num == -EAGAIN)
2322 break;
2323 retval = num;
2324 goto break_out;
2325 }
2326 b += num;
2327 nr -= num;
2328 if (nr == 0)
2329 break;
2330 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002331 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 break;
2333 b++; nr--;
2334 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002335 if (tty->ops->flush_chars)
2336 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002338 struct n_tty_data *ldata = tty->disc_data;
2339
Roman Zippeld6afe272005-07-07 17:56:55 -07002340 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002341 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002342 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002343 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002344 if (c < 0) {
2345 retval = c;
2346 goto break_out;
2347 }
2348 if (!c)
2349 break;
2350 b += c;
2351 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354 if (!nr)
2355 break;
2356 if (file->f_flags & O_NONBLOCK) {
2357 retval = -EAGAIN;
2358 break;
2359 }
Peter Hurley9356b532013-06-15 09:14:24 -04002360 up_read(&tty->termios_rwsem);
2361
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002362 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002363
2364 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 remove_wait_queue(&tty->write_wait, &wait);
Peter Hurley87108bc2016-01-09 21:45:14 -08002368 if (nr && tty->fasync)
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002369 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002370 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return (b - buf) ? b - buf : retval;
2372}
2373
2374/**
Alan Cox11a96d12008-10-13 10:46:24 +01002375 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 * @tty: terminal device
2377 * @file: file accessing it
2378 * @wait: poll table
2379 *
2380 * Called when the line discipline is asked to poll() for data or
2381 * for special events. This code is not serialized with respect to
2382 * other events save open/close.
2383 *
2384 * This code must be sure never to sleep through a hangup.
2385 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 */
Alan Cox4edf1822008-02-08 04:18:44 -08002387
Alan Cox11a96d12008-10-13 10:46:24 +01002388static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002389 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 unsigned int mask = 0;
2392
2393 poll_wait(file, &tty->read_wait, wait);
2394 poll_wait(file, &tty->write_wait, wait);
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002395 if (input_available_p(tty, 1))
2396 mask |= POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002397 else {
2398 tty_buffer_flush_work(tty->port);
2399 if (input_available_p(tty, 1))
2400 mask |= POLLIN | POLLRDNORM;
2401 }
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002402 if (tty->packet && tty->link->ctrl_status)
2403 mask |= POLLPRI | POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002404 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2405 mask |= POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (tty_hung_up_p(file))
2407 mask |= POLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002408 if (tty->ops->write && !tty_is_writelocked(tty) &&
2409 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2410 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 mask |= POLLOUT | POLLWRNORM;
2412 return mask;
2413}
2414
Jiri Slaby57c94122012-10-18 22:26:43 +02002415static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002416{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002417 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002418
Peter Hurleya73d3d62013-06-15 09:14:25 -04002419 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002420 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002421 head = ldata->canon_head;
2422 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002423 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002424 /* Skip EOF-chars.. */
Tetsuo Handa947dead2018-05-26 09:53:13 +09002425 while (MASK(head) != MASK(tail)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002426 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2427 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002428 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002429 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002430 }
2431 return nr;
2432}
2433
2434static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2435 unsigned int cmd, unsigned long arg)
2436{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002437 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002438 int retval;
2439
2440 switch (cmd) {
2441 case TIOCOUTQ:
2442 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2443 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002444 down_write(&tty->termios_rwsem);
Linus Torvalds00fc57a2017-12-20 17:57:06 -08002445 if (L_ICANON(tty) && !L_EXTPROC(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002446 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002447 else
2448 retval = read_cnt(ldata);
2449 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002450 return put_user(retval, (unsigned int __user *) arg);
2451 default:
2452 return n_tty_ioctl_helper(tty, file, cmd, arg);
2453 }
2454}
2455
Peter Hurley27228732016-01-09 21:35:19 -08002456static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002457 .magic = TTY_LDISC_MAGIC,
2458 .name = "n_tty",
2459 .open = n_tty_open,
2460 .close = n_tty_close,
2461 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002462 .read = n_tty_read,
2463 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002464 .ioctl = n_tty_ioctl,
2465 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002466 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002467 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002468 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002469 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002471
2472/**
2473 * n_tty_inherit_ops - inherit N_TTY methods
2474 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2475 *
Peter Hurley27228732016-01-09 21:35:19 -08002476 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002477 */
2478
2479void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2480{
Peter Hurley27228732016-01-09 21:35:19 -08002481 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002482 ops->owner = NULL;
2483 ops->refcount = ops->flags = 0;
2484}
2485EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002486
2487void __init n_tty_init(void)
2488{
2489 tty_register_ldisc(N_TTY, &n_tty_ops);
2490}