blob: 442a31306491ebaa72df4c4a4604c35b3fb93d2e [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
Carl van Schaikb38f1742018-07-03 12:02:30 +1000131#if defined(CONFIG_TTY_FLUSH_LOCAL_ECHO)
132static void continue_process_echoes(struct work_struct *work);
133#endif
134
Peter Hurleyce741172013-06-15 09:14:20 -0400135static inline size_t read_cnt(struct n_tty_data *ldata)
136{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400137 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400138}
139
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400140static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
141{
142 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
143}
144
145static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
146{
147 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
148}
149
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400150static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
151{
Tetsuo Handa9264e982018-05-26 09:53:14 +0900152 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400153 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
154}
155
156static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
157{
158 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
159}
160
Peter Hurley679e7c22015-11-27 14:11:02 -0500161static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
162 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700163{
164 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500165 size_t size = N_TTY_BUF_SIZE - tail;
166 const void *from = read_buf_addr(ldata, tail);
167 int uncopied;
168
169 if (n > size) {
Peter Hurley309426a2016-01-09 22:55:27 -0800170 tty_audit_add_data(tty, from, size);
Peter Hurley679e7c22015-11-27 14:11:02 -0500171 uncopied = copy_to_user(to, from, size);
172 if (uncopied)
173 return uncopied;
174 to += size;
175 n -= size;
176 from = ldata->read_buf;
177 }
Laura Abbott72586c62015-05-14 11:42:17 -0700178
Peter Hurley309426a2016-01-09 22:55:27 -0800179 tty_audit_add_data(tty, from, n);
Laura Abbott72586c62015-05-14 11:42:17 -0700180 return copy_to_user(to, from, n);
181}
182
Peter Hurley24a89d12013-06-15 09:14:15 -0400183/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500184 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400185 * @tty: terminal
186 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500187 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400188 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400189 * Caller holds exclusive termios_rwsem
190 * or
191 * n_tty_read()/consumer path:
192 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400193 */
194
Peter Hurley2c5dc462015-01-16 15:05:34 -0500195static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400196{
Peter Hurley24a89d12013-06-15 09:14:15 -0400197 struct n_tty_data *ldata = tty->disc_data;
198
Peter Hurley2c5dc462015-01-16 15:05:34 -0500199 /* Did the input worker stop? Restart it */
200 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400201 ldata->no_room = 0;
202
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200203 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400204 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400205 /* see if ldisc has been killed - if so, this means that
206 * even though the ldisc has been halted and ->buf.work
207 * cancelled, ->buf.work is about to be rescheduled
208 */
209 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
210 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400211 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200212 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900213}
214
Peter Hurley9a4aec22013-06-15 09:14:32 -0400215static ssize_t chars_in_buffer(struct tty_struct *tty)
216{
217 struct n_tty_data *ldata = tty->disc_data;
218 ssize_t n = 0;
219
220 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500221 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400222 else
223 n = ldata->canon_head - ldata->read_tail;
224 return n;
225}
226
Peter Hurleyee0bab82013-06-15 09:14:34 -0400227/**
228 * n_tty_write_wakeup - asynchronous I/O notifier
229 * @tty: tty device
230 *
231 * Required for the ptys, serial driver etc. since processes
232 * that attach themselves to the master and rely on ASYNC
233 * IO must be woken up
234 */
235
236static void n_tty_write_wakeup(struct tty_struct *tty)
237{
Peter Hurley7bccc362016-01-09 21:45:12 -0800238 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
239 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Peter Hurleyee0bab82013-06-15 09:14:34 -0400240}
241
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400242static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400243{
Peter Hurleya3428462015-01-16 15:05:35 -0500244 struct n_tty_data *ldata = tty->disc_data;
245
Peter Hurley6367ca72013-06-15 09:14:33 -0400246 /*
247 * Check the remaining room for the input canonicalization
248 * mode. We don't want to throttle the driver if we're in
249 * canonical mode and don't have a newline yet!
250 */
Peter Hurleya3428462015-01-16 15:05:35 -0500251 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
252 return;
253
Peter Hurley6367ca72013-06-15 09:14:33 -0400254 while (1) {
255 int throttled;
256 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500257 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400258 break;
259 throttled = tty_throttle_safe(tty);
260 if (!throttled)
261 break;
262 }
263 __tty_set_flow_change(tty, 0);
264}
265
Peter Hurley4b293492013-07-24 08:29:55 -0400266static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400267{
Peter Hurley6d27a632016-01-10 22:40:56 -0800268 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400269 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
270 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500271 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800272 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400273 return;
274 }
275
Peter Hurley6367ca72013-06-15 09:14:33 -0400276 /* If there is enough space in the read buffer now, let the
277 * low-level driver know. We use chars_in_buffer() to
278 * check the buffer, as it now knows about canonical mode.
279 * Otherwise, if the driver is throttled and the line is
280 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
281 * we won't get any more characters.
282 */
283
284 while (1) {
285 int unthrottled;
286 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
287 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
288 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500289 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400290 unthrottled = tty_unthrottle_safe(tty);
291 if (!unthrottled)
292 break;
293 }
294 __tty_set_flow_change(tty, 0);
295}
296
Alan Cox17b82062008-10-13 10:45:06 +0100297/**
298 * put_tty_queue - add character to tty
299 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200300 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100301 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400302 * Add a character to the tty read_buf queue.
303 *
304 * n_tty_receive_buf()/producer path:
305 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100306 */
307
Peter Hurley19e2ad62013-07-24 08:29:54 -0400308static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100310 *read_buf_addr(ldata, ldata->read_head) = c;
311 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * reset_buffer_flags - reset buffer state
316 * @tty: terminal to reset
317 *
Peter Hurley25518c62013-03-11 16:44:31 -0400318 * Reset the read buffer counters and clear the flags.
319 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100320 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400321 * Locking: caller holds exclusive termios_rwsem
322 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000324
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400325static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400327 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500328 ldata->commit_head = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400329 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000330
Peter Hurleya73d3d62013-06-15 09:14:25 -0400331 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200332 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500333 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Peter Hurleya30737a2013-03-11 16:44:22 -0400336static void n_tty_packet_mode_flush(struct tty_struct *tty)
337{
338 unsigned long flags;
339
Peter Hurleya30737a2013-03-11 16:44:22 -0400340 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400341 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400342 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400343 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000344 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400345 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/**
349 * n_tty_flush_buffer - clean input queue
350 * @tty: terminal device
351 *
Peter Hurley25518c62013-03-11 16:44:31 -0400352 * Flush the input buffer. Called when the tty layer wants the
353 * buffer flushed (eg at hangup) or when the N_TTY line discipline
354 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400356 * Holds termios_rwsem to exclude producer/consumer while
357 * buffer indices are reset.
358 *
359 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Alan Cox4edf1822008-02-08 04:18:44 -0800361
362static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400364 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400365 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500366 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800367
Peter Hurleya30737a2013-03-11 16:44:22 -0400368 if (tty->link)
369 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400370 up_write(&tty->termios_rwsem);
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * is_utf8_continuation - utf8 multibyte check
375 * @c: byte to check
376 *
377 * Returns true if the utf8 character 'c' is a multibyte continuation
378 * character. We use this to correctly compute the on screen size
379 * of the character when printing
380 */
Alan Cox4edf1822008-02-08 04:18:44 -0800381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static inline int is_utf8_continuation(unsigned char c)
383{
384 return (c & 0xc0) == 0x80;
385}
386
387/**
388 * is_continuation - multibyte check
389 * @c: byte to check
390 *
391 * Returns true if the utf8 character 'c' is a multibyte continuation
392 * character and the terminal is in unicode mode.
393 */
Alan Cox4edf1822008-02-08 04:18:44 -0800394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static inline int is_continuation(unsigned char c, struct tty_struct *tty)
396{
397 return I_IUTF8(tty) && is_utf8_continuation(c);
398}
399
400/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000401 * do_output_char - output one character
402 * @c: character (or partial unicode symbol)
403 * @tty: terminal device
404 * @space: space available in tty driver write buffer
405 *
406 * This is a helper function that handles one output character
407 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600408 * doing OPOST processing and putting the results in the
409 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000410 *
411 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
412 * and NLDLY. They simply aren't relevant in the world today.
413 * If you ever need them, add them here.
414 *
415 * Returns the number of bytes of buffer space used or -1 if
416 * no space left.
417 *
418 * Locking: should be called under the output_lock to protect
419 * the column state and space left in the buffer
420 */
421
422static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
423{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200424 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000425 int spaces;
426
427 if (!space)
428 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000429
Joe Petersona88a69c2009-01-02 13:40:53 +0000430 switch (c) {
431 case '\n':
432 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200433 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000434 if (O_ONLCR(tty)) {
435 if (space < 2)
436 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200437 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700438 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000439 return 2;
440 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200441 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000442 break;
443 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200444 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000445 return 0;
446 if (O_OCRNL(tty)) {
447 c = '\n';
448 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200449 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000450 break;
451 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200452 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000453 break;
454 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200455 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000456 if (O_TABDLY(tty) == XTABS) {
457 if (space < spaces)
458 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200459 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000460 tty->ops->write(tty, " ", spaces);
461 return spaces;
462 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200463 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000464 break;
465 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200466 if (ldata->column > 0)
467 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000468 break;
469 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000470 if (!iscntrl(c)) {
471 if (O_OLCUC(tty))
472 c = toupper(c);
473 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200474 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000475 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000476 break;
477 }
478
479 tty_put_char(tty, c);
480 return 1;
481}
482
483/**
484 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * @c: character (or partial unicode symbol)
486 * @tty: terminal device
487 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600488 * Output one character with OPOST processing.
489 * Returns -1 when the output device is full and the character
490 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000492 * Locking: output_lock to protect column state and space left
493 * (also, this is called from n_tty_write under the
494 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Alan Cox4edf1822008-02-08 04:18:44 -0800496
Joe Petersona88a69c2009-01-02 13:40:53 +0000497static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jiri Slabybddc7152012-10-18 22:26:42 +0200499 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000500 int space, retval;
501
Jiri Slabybddc7152012-10-18 22:26:42 +0200502 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Alan Coxf34d7a52008-04-30 00:54:13 -0700504 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000505 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jiri Slabybddc7152012-10-18 22:26:42 +0200507 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000508 if (retval < 0)
509 return -1;
510 else
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000515 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600517 * @buf: character buffer
518 * @nr: number of bytes to output
519 *
520 * Output a block of characters with OPOST processing.
521 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
523 * This path is used to speed up block console writes, among other
524 * things when processing blocks of output data. It handles only
525 * the simple cases normally found and helps to generate blocks of
526 * symbols for the console driver and thus improve performance.
527 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000528 * Locking: output_lock to protect column state and space left
529 * (also, this is called from n_tty_write under the
530 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Alan Cox4edf1822008-02-08 04:18:44 -0800532
Joe Petersona88a69c2009-01-02 13:40:53 +0000533static ssize_t process_output_block(struct tty_struct *tty,
534 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200536 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100538 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 const unsigned char *cp;
540
Jiri Slabybddc7152012-10-18 22:26:42 +0200541 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000542
Alan Coxf34d7a52008-04-30 00:54:13 -0700543 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000544 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200545 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (nr > space)
549 nr = space;
550
551 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000552 unsigned char c = *cp;
553
554 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 case '\n':
556 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200557 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (O_ONLCR(tty))
559 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200560 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200563 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto break_out;
565 if (O_OCRNL(tty))
566 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200567 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 case '\t':
570 goto break_out;
571 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200572 if (ldata->column > 0)
573 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000576 if (!iscntrl(c)) {
577 if (O_OLCUC(tty))
578 goto break_out;
579 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200580 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 }
584 }
585break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700586 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000587
Jiri Slabybddc7152012-10-18 22:26:42 +0200588 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return i;
590}
591
Joe Petersona88a69c2009-01-02 13:40:53 +0000592/**
593 * process_echoes - write pending echo characters
594 * @tty: terminal device
595 *
596 * Write previously buffered echo (and other ldisc-generated)
597 * characters to the tty.
598 *
599 * Characters generated by the ldisc (including echoes) need to
600 * be buffered because the driver's write buffer can fill during
601 * heavy program output. Echoing straight to the driver will
602 * often fail under these conditions, causing lost characters and
603 * resulting mismatches of ldisc state information.
604 *
605 * Since the ldisc state must represent the characters actually sent
606 * to the driver at the time of the write, operations like certain
607 * changes in column state are also saved in the buffer and executed
608 * here.
609 *
610 * A circular fifo buffer is used so that the most recent characters
611 * are prioritized. Also, when control characters are echoed with a
612 * prefixed "^", the pair is treated atomically and thus not separated.
613 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400614 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000615 */
616
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400617static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000618{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200619 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400620 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400621 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000622 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000623
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400624 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000625
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400626 tail = ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900627 while (MASK(ldata->echo_commit) != MASK(tail)) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400628 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000629 if (c == ECHO_OP_START) {
630 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000631 int no_space_left = 0;
632
633 /*
Tetsuo Handa9264e982018-05-26 09:53:14 +0900634 * Since add_echo_byte() is called without holding
635 * output_lock, we might see only portion of multi-byte
636 * operation.
637 */
638 if (MASK(ldata->echo_commit) == MASK(tail + 1))
639 goto not_yet_stored;
640 /*
Joe Petersona88a69c2009-01-02 13:40:53 +0000641 * If the buffer byte is the start of a multi-byte
642 * operation, get the next byte, which is either the
643 * op code or a control character value.
644 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400645 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000646
Joe Petersona88a69c2009-01-02 13:40:53 +0000647 switch (op) {
648 unsigned int num_chars, num_bs;
649
650 case ECHO_OP_ERASE_TAB:
Tetsuo Handa9264e982018-05-26 09:53:14 +0900651 if (MASK(ldata->echo_commit) == MASK(tail + 2))
652 goto not_yet_stored;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400653 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000654
655 /*
656 * Determine how many columns to go back
657 * in order to erase the tab.
658 * This depends on the number of columns
659 * used by other characters within the tab
660 * area. If this (modulo 8) count is from
661 * the start of input rather than from a
662 * previous tab, we offset by canon column.
663 * Otherwise, tab spacing is normal.
664 */
665 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200666 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000667 num_bs = 8 - (num_chars & 7);
668
669 if (num_bs > space) {
670 no_space_left = 1;
671 break;
672 }
673 space -= num_bs;
674 while (num_bs--) {
675 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200676 if (ldata->column > 0)
677 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000678 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400679 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 break;
681
682 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200683 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400684 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000685 break;
686
687 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200688 if (ldata->column > 0)
689 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400690 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000691 break;
692
693 case ECHO_OP_START:
694 /* This is an escaped echo op start code */
695 if (!space) {
696 no_space_left = 1;
697 break;
698 }
699 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200700 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000701 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400702 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000703 break;
704
705 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000706 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600707 * If the op is not a special byte code,
708 * it is a ctrl char tagged to be echoed
709 * as "^X" (where X is the letter
710 * representing the control char).
711 * Note that we must ensure there is
712 * enough space for the whole ctrl pair.
713 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000714 */
Joe Peterson62b26352009-09-09 15:03:47 -0600715 if (space < 2) {
716 no_space_left = 1;
717 break;
718 }
719 tty_put_char(tty, '^');
720 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200721 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600722 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400723 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 }
725
726 if (no_space_left)
727 break;
728 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400729 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600730 int retval = do_output_char(c, tty, space);
731 if (retval < 0)
732 break;
733 space -= retval;
734 } else {
735 if (!space)
736 break;
737 tty_put_char(tty, c);
738 space -= 1;
739 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400740 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000741 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000742 }
743
Peter Hurleycbfd0342013-06-15 10:04:26 -0400744 /* If the echo buffer is nearly full (so that the possibility exists
745 * of echo overrun before the next commit), then discard enough
746 * data at the tail to prevent a subsequent overrun */
Tetsuo Handa9264e982018-05-26 09:53:14 +0900747 while (ldata->echo_commit > tail &&
748 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200749 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500750 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400751 tail += 3;
752 else
753 tail += 2;
754 } else
755 tail++;
756 }
757
Carl van Schaikb38f1742018-07-03 12:02:30 +1000758#if defined(CONFIG_TTY_FLUSH_LOCAL_ECHO)
759 if (ldata->echo_commit != tail) {
760 if (!tty->delayed_work) {
761 INIT_DELAYED_WORK(&tty->echo_delayed_work, continue_process_echoes);
762 schedule_delayed_work(&tty->echo_delayed_work, 1);
763 }
764 tty->delayed_work = 1;
765 }
766#endif
767
Tetsuo Handa9264e982018-05-26 09:53:14 +0900768 not_yet_stored:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400769 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400770 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000771}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Peter Hurley17bd7902013-06-15 10:04:24 -0400773static void commit_echoes(struct tty_struct *tty)
774{
775 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400776 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400777 size_t head;
778
Tetsuo Handa9264e982018-05-26 09:53:14 +0900779 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400780 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500781 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400782 old = ldata->echo_commit - ldata->echo_tail;
783
784 /* Process committed echoes if the accumulated # of bytes
785 * is over the threshold (and try again each time another
786 * block is accumulated) */
787 nr = head - ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900788 if (nr < ECHO_COMMIT_WATERMARK ||
789 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
790 mutex_unlock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400791 return;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900792 }
Peter Hurley17bd7902013-06-15 10:04:24 -0400793
Peter Hurleycbfd0342013-06-15 10:04:26 -0400794 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400795 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400796 mutex_unlock(&ldata->output_lock);
797
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400798 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400799 tty->ops->flush_chars(tty);
800}
801
802static void process_echoes(struct tty_struct *tty)
803{
804 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400805 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400806
Peter Hurleye2613be2014-02-11 16:34:55 -0500807 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400808 return;
809
810 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500811 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400812 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400813 mutex_unlock(&ldata->output_lock);
814
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400815 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400816 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400817}
818
Peter Hurley1075a6e2013-12-09 18:06:07 -0500819/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400820static void flush_echoes(struct tty_struct *tty)
821{
822 struct n_tty_data *ldata = tty->disc_data;
823
Peter Hurley39434ab2013-11-29 12:56:10 -0500824 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
825 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400826 return;
827
828 mutex_lock(&ldata->output_lock);
829 ldata->echo_commit = ldata->echo_head;
830 __process_echoes(tty);
831 mutex_unlock(&ldata->output_lock);
832}
833
Carl van Schaikb38f1742018-07-03 12:02:30 +1000834#if defined(CONFIG_TTY_FLUSH_LOCAL_ECHO)
835static void continue_process_echoes(struct work_struct *work)
836{
837 struct tty_struct *tty =
838 container_of(work, struct tty_struct, echo_delayed_work.work);
839 struct n_tty_data *ldata = tty->disc_data;
840
841 mutex_lock(&ldata->output_lock);
842 tty->delayed_work = 0;
843 __process_echoes(tty);
844 mutex_unlock(&ldata->output_lock);
845}
846#endif
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 * add_echo_byte - add a byte to the echo buffer
850 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200851 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 *
853 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000854 */
855
Peter Hurleycbfd0342013-06-15 10:04:26 -0400856static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000857{
Tetsuo Handa9264e982018-05-26 09:53:14 +0900858 *echo_buf_addr(ldata, ldata->echo_head) = c;
859 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
860 ldata->echo_head++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000861}
862
863/**
864 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200865 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000866 *
867 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000868 */
869
Jiri Slaby57c94122012-10-18 22:26:43 +0200870static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000871{
Jiri Slaby57c94122012-10-18 22:26:43 +0200872 add_echo_byte(ECHO_OP_START, ldata);
873 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000874}
875
876/**
877 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200878 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000879 *
880 * Add an operation to the echo buffer to set the canon column
881 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000882 */
883
Jiri Slaby57c94122012-10-18 22:26:43 +0200884static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000885{
Jiri Slaby57c94122012-10-18 22:26:43 +0200886 add_echo_byte(ECHO_OP_START, ldata);
887 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000888}
889
890/**
891 * echo_erase_tab - add operation to erase a tab
892 * @num_chars: number of character columns already used
893 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200894 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000895 *
896 * Add an operation to the echo buffer to erase a tab.
897 *
898 * Called by the eraser function, which knows how many character
899 * columns have been used since either a previous tab or the start
900 * of input. This information will be used later, along with
901 * canon column (if applicable), to go back the correct number
902 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000903 */
904
905static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200906 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000907{
Jiri Slaby57c94122012-10-18 22:26:43 +0200908 add_echo_byte(ECHO_OP_START, ldata);
909 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000910
911 /* We only need to know this modulo 8 (tab spacing) */
912 num_chars &= 7;
913
914 /* Set the high bit as a flag if num_chars is after a previous tab */
915 if (after_tab)
916 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000917
Jiri Slaby57c94122012-10-18 22:26:43 +0200918 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000919}
920
921/**
922 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * @c: unicode byte to echo
924 * @tty: terminal device
925 *
Alan Cox4edf1822008-02-08 04:18:44 -0800926 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100928 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000929 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000930 */
931
Jiri Slaby57c94122012-10-18 22:26:43 +0200932static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000933{
Joe Petersona88a69c2009-01-02 13:40:53 +0000934 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200935 add_echo_byte(ECHO_OP_START, ldata);
936 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000937 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200938 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000939 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000940}
941
942/**
943 * echo_char - echo a character
944 * @c: unicode byte to echo
945 * @tty: terminal device
946 *
947 * Echo user input back onto the screen. This must be called only when
948 * L_ECHO(tty) is true. Called from the driver receive_buf path.
949 *
Joe Peterson62b26352009-09-09 15:03:47 -0600950 * This variant tags control characters to be echoed as "^X"
951 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
953
954static void echo_char(unsigned char c, struct tty_struct *tty)
955{
Jiri Slabybddc7152012-10-18 22:26:42 +0200956 struct n_tty_data *ldata = tty->disc_data;
957
Joe Petersona88a69c2009-01-02 13:40:53 +0000958 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200959 add_echo_byte(ECHO_OP_START, ldata);
960 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000961 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600962 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200963 add_echo_byte(ECHO_OP_START, ldata);
964 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Alan Cox17b82062008-10-13 10:45:06 +0100968/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000969 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200970 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100971 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000972
Jiri Slaby57c94122012-10-18 22:26:43 +0200973static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200975 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200976 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200977 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979}
980
981/**
982 * eraser - handle erase function
983 * @c: character input
984 * @tty: terminal device
985 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200986 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * present in the stream from the driver layer. Handles the complexities
988 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100989 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400990 * n_tty_receive_buf()/producer path:
991 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Alan Cox4edf1822008-02-08 04:18:44 -0800993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static void eraser(unsigned char c, struct tty_struct *tty)
995{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200996 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400998 size_t head;
999 size_t cnt;
1000 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001002 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001003 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006 if (c == ERASE_CHAR(tty))
1007 kill_type = ERASE;
1008 else if (c == WERASE_CHAR(tty))
1009 kill_type = WERASE;
1010 else {
1011 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001012 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return;
1014 }
1015 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001016 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +02001017 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 echo_char(KILL_CHAR(tty), tty);
1019 /* Add a newline if ECHOK is on and ECHOKE is off. */
1020 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001021 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return;
1023 }
1024 kill_type = KILL;
1025 }
1026
1027 seen_alnums = 0;
Tetsuo Handa947dead2018-05-26 09:53:13 +09001028 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001029 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* erase a single possibly multibyte character */
1032 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001033 head--;
1034 c = read_buf(ldata, head);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001035 } while (is_continuation(c, tty) &&
1036 MASK(head) != MASK(ldata->canon_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* do not partially erase */
1039 if (is_continuation(c, tty))
1040 break;
1041
1042 if (kill_type == WERASE) {
1043 /* Equivalent to BSD's ALTWERASE. */
1044 if (isalnum(c) || c == '_')
1045 seen_alnums++;
1046 else if (seen_alnums)
1047 break;
1048 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001049 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001050 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (L_ECHO(tty)) {
1052 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001053 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001054 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001055 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 /* if cnt > 1, output a multi-byte character */
1058 echo_char(c, tty);
1059 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001060 head++;
1061 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001062 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1065 echo_char(ERASE_CHAR(tty), tty);
1066 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001067 unsigned int num_chars = 0;
1068 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001069 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Joe Petersona88a69c2009-01-02 13:40:53 +00001071 /*
1072 * Count the columns used for characters
1073 * since the start of input or after a
1074 * previous tab.
1075 * This info is used to go back the correct
1076 * number of columns.
1077 */
Tetsuo Handa947dead2018-05-26 09:53:13 +09001078 while (MASK(tail) != MASK(ldata->canon_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001079 tail--;
1080 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001081 if (c == '\t') {
1082 after_tab = 1;
1083 break;
Alan Cox300a6202009-01-02 13:41:04 +00001084 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001086 num_chars += 2;
1087 } else if (!is_continuation(c, tty)) {
1088 num_chars++;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001091 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 } else {
1093 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001094 echo_char_raw('\b', ldata);
1095 echo_char_raw(' ', ldata);
1096 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001099 echo_char_raw('\b', ldata);
1100 echo_char_raw(' ', ldata);
1101 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 }
1104 }
1105 if (kill_type == ERASE)
1106 break;
1107 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001108 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001109 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112/**
1113 * isig - handle the ISIG optio
1114 * @sig: signal
1115 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001117 * Called when a signal is being sent due to terminal input.
1118 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001119 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001120 * Performs input and output flush if !NOFLSH. In this context, the echo
1121 * buffer is 'output'. The signal is processed first to alert any current
1122 * readers or writers to discontinue and exit their i/o loops.
1123 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001124 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Alan Cox4edf1822008-02-08 04:18:44 -08001126
Peter Hurley3b19e032015-06-27 09:21:32 -04001127static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Peter Hurley8c985d12013-03-06 08:38:19 -05001129 struct pid *tty_pgrp = tty_get_pgrp(tty);
1130 if (tty_pgrp) {
1131 kill_pgrp(tty_pgrp, sig, 1);
1132 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001134}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001135
Peter Hurley3b19e032015-06-27 09:21:32 -04001136static void isig(int sig, struct tty_struct *tty)
1137{
1138 struct n_tty_data *ldata = tty->disc_data;
1139
1140 if (L_NOFLSH(tty)) {
1141 /* signal only */
1142 __isig(sig, tty);
1143
1144 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001145 up_read(&tty->termios_rwsem);
1146 down_write(&tty->termios_rwsem);
1147
Peter Hurley3b19e032015-06-27 09:21:32 -04001148 __isig(sig, tty);
1149
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001150 /* clear echo buffer */
1151 mutex_lock(&ldata->output_lock);
1152 ldata->echo_head = ldata->echo_tail = 0;
1153 ldata->echo_mark = ldata->echo_commit = 0;
1154 mutex_unlock(&ldata->output_lock);
1155
1156 /* clear output buffer */
1157 tty_driver_flush_buffer(tty);
1158
1159 /* clear input buffer */
1160 reset_buffer_flags(tty->disc_data);
1161
1162 /* notify pty master of flush */
1163 if (tty->link)
1164 n_tty_packet_mode_flush(tty);
1165
1166 up_write(&tty->termios_rwsem);
1167 down_read(&tty->termios_rwsem);
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/**
1172 * n_tty_receive_break - handle break
1173 * @tty: terminal
1174 *
1175 * An RS232 break event has been hit in the incoming bitstream. This
1176 * can cause a variety of events depending upon the termios settings.
1177 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001178 * n_tty_receive_buf()/producer path:
1179 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001180 *
1181 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
Alan Cox4edf1822008-02-08 04:18:44 -08001183
Peter Hurley4b293492013-07-24 08:29:55 -04001184static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jiri Slaby57c94122012-10-18 22:26:43 +02001186 struct n_tty_data *ldata = tty->disc_data;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (I_IGNBRK(tty))
1189 return;
1190 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001191 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return;
1193 }
1194 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001195 put_tty_queue('\377', ldata);
1196 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001198 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/**
1202 * n_tty_receive_overrun - handle overrun reporting
1203 * @tty: terminal
1204 *
1205 * Data arrived faster than we could process it. While the tty
1206 * driver has flagged this the bits that were missed are gone
1207 * forever.
1208 *
1209 * Called from the receive_buf path so single threaded. Does not
1210 * need locking as num_overrun and overrun_time are function
1211 * private.
1212 */
Alan Cox4edf1822008-02-08 04:18:44 -08001213
Peter Hurley4b293492013-07-24 08:29:55 -04001214static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001216 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001218 ldata->num_overrun++;
1219 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1220 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001221 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001222 ldata->overrun_time = jiffies;
1223 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225}
1226
1227/**
1228 * n_tty_receive_parity_error - error notifier
1229 * @tty: terminal device
1230 * @c: character
1231 *
1232 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001233 * the error case if necessary.
1234 *
1235 * n_tty_receive_buf()/producer path:
1236 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Peter Hurley4b293492013-07-24 08:29:55 -04001238static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Jiri Slaby57c94122012-10-18 22:26:43 +02001240 struct n_tty_data *ldata = tty->disc_data;
1241
Peter Hurley66528f92014-06-16 08:10:42 -04001242 if (I_INPCK(tty)) {
1243 if (I_IGNPAR(tty))
1244 return;
1245 if (I_PARMRK(tty)) {
1246 put_tty_queue('\377', ldata);
1247 put_tty_queue('\0', ldata);
1248 put_tty_queue(c, ldata);
1249 } else
1250 put_tty_queue('\0', ldata);
1251 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001252 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001255static void
1256n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1257{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001258 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001259 if (I_IXON(tty))
1260 start_tty(tty);
1261 if (L_ECHO(tty)) {
1262 echo_char(c, tty);
1263 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001264 } else
1265 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001266 return;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/**
1270 * n_tty_receive_char - perform processing
1271 * @tty: terminal device
1272 * @c: character
1273 *
1274 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001275 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001277 *
1278 * n_tty_receive_buf()/producer path:
1279 * caller holds non-exclusive termios_rwsem
1280 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001281 *
1282 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
1284
Peter Hurleye60d27c2013-07-24 08:29:56 -04001285static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001286n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001288 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (I_IXON(tty)) {
1291 if (c == START_CHAR(tty)) {
1292 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001293 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 if (c == STOP_CHAR(tty)) {
1297 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 }
Joe Peterson575537b32008-04-30 00:53:30 -07001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001303 if (c == INTR_CHAR(tty)) {
1304 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001305 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001306 } else if (c == QUIT_CHAR(tty)) {
1307 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001308 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001309 } else if (c == SUSP_CHAR(tty)) {
1310 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 }
Joe Peterson575537b32008-04-30 00:53:30 -07001314
Peter Hurley855df3c2013-07-24 08:29:50 -04001315 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1316 start_tty(tty);
1317 process_echoes(tty);
1318 }
1319
Joe Peterson575537b32008-04-30 00:53:30 -07001320 if (c == '\r') {
1321 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001322 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001323 if (I_ICRNL(tty))
1324 c = '\n';
1325 } else if (c == '\n' && I_INLCR(tty))
1326 c = '\r';
1327
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001328 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1330 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1331 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001332 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001336 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001338 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001340 echo_char_raw('^', ldata);
1341 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001342 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001345 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001347 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001348 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jiri Slaby57c94122012-10-18 22:26:43 +02001350 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001352 echo_char_raw('\n', ldata);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001353 while (MASK(tail) != MASK(ldata->read_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001354 echo_char(read_buf(ldata, tail), tty);
1355 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001357 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001361 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001362 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001363 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 goto handle_newline;
1366 }
1367 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 c = __DISABLED_CHAR;
1369 goto handle_newline;
1370 }
1371 if ((c == EOL_CHAR(tty)) ||
1372 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1373 /*
1374 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1375 */
1376 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* 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);
Peter Hurley17bd7902013-06-15 10:04:24 -04001381 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 /*
1384 * XXX does PARMRK doubling happen for
1385 * EOL_CHAR and EOL2_CHAR?
1386 */
Peter Hurley001ba922013-12-02 14:24:44 -05001387 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001388 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Alan Cox4edf1822008-02-08 04:18:44 -08001390handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001391 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001392 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001393 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001395 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 }
Alan Cox4edf1822008-02-08 04:18:44 -08001399
Joe Petersonacc71bb2009-01-02 13:43:32 +00001400 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001401 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001403 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 else {
1405 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001406 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001407 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 echo_char(c, tty);
1409 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001410 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Peter Hurley001ba922013-12-02 14:24:44 -05001413 /* PARMRK doubling check */
1414 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001415 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Jiri Slaby57c94122012-10-18 22:26:43 +02001417 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001418 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Peter Hurleye60d27c2013-07-24 08:29:56 -04001421static inline void
1422n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001423{
1424 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001425
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 Hurley4b1f79c2013-07-24 08:29:51 -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 }
Peter Hurley001ba922013-12-02 14:24:44 -05001438 /* PARMRK doubling check */
1439 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001440 put_tty_queue(c, ldata);
1441 put_tty_queue(c, ldata);
1442}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001443
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001444static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001445{
1446 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001447}
1448
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001449static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001450n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1451{
1452 struct n_tty_data *ldata = tty->disc_data;
1453
Peter Hurleye60d27c2013-07-24 08:29:56 -04001454 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1455 start_tty(tty);
1456 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001457 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001458 if (L_ECHO(tty)) {
1459 finish_erasing(ldata);
1460 /* Record the column of first canon char. */
1461 if (ldata->canon_head == ldata->read_head)
1462 echo_set_canon_col(ldata);
1463 echo_char(c, tty);
1464 commit_echoes(tty);
1465 }
1466 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001467}
1468
Peter Hurley8dc4b252013-12-02 14:24:43 -05001469static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001470{
1471 if (I_ISTRIP(tty))
1472 c &= 0x7f;
1473 if (I_IUCLC(tty) && L_IEXTEN(tty))
1474 c = tolower(c);
1475
1476 if (I_IXON(tty)) {
1477 if (c == STOP_CHAR(tty))
1478 stop_tty(tty);
1479 else if (c == START_CHAR(tty) ||
1480 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1481 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1482 c != SUSP_CHAR(tty))) {
1483 start_tty(tty);
1484 process_echoes(tty);
1485 }
1486 }
1487}
1488
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001489static void
1490n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1491{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001492 switch (flag) {
1493 case TTY_BREAK:
1494 n_tty_receive_break(tty);
1495 break;
1496 case TTY_PARITY:
1497 case TTY_FRAME:
1498 n_tty_receive_parity_error(tty, c);
1499 break;
1500 case TTY_OVERRUN:
1501 n_tty_receive_overrun(tty);
1502 break;
1503 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001504 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001505 break;
1506 }
1507}
1508
Peter Hurleye60d27c2013-07-24 08:29:56 -04001509static void
1510n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1511{
1512 struct n_tty_data *ldata = tty->disc_data;
1513
1514 ldata->lnext = 0;
1515 if (likely(flag == TTY_NORMAL)) {
1516 if (I_ISTRIP(tty))
1517 c &= 0x7f;
1518 if (I_IUCLC(tty) && L_IEXTEN(tty))
1519 c = tolower(c);
1520 n_tty_receive_char(tty, c);
1521 } else
1522 n_tty_receive_char_flagged(tty, c, flag);
1523}
1524
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001525static void
1526n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1527 char *fp, int count)
1528{
1529 struct n_tty_data *ldata = tty->disc_data;
1530 size_t n, head;
1531
1532 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001533 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001534 memcpy(read_buf_addr(ldata, head), cp, n);
1535 ldata->read_head += n;
1536 cp += n;
1537 count -= n;
1538
1539 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001540 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001541 memcpy(read_buf_addr(ldata, head), cp, n);
1542 ldata->read_head += n;
1543}
1544
Peter Hurley554117b2013-06-15 10:21:25 -04001545static void
1546n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1547 char *fp, int count)
1548{
1549 struct n_tty_data *ldata = tty->disc_data;
1550 char flag = TTY_NORMAL;
1551
1552 while (count--) {
1553 if (fp)
1554 flag = *fp++;
1555 if (likely(flag == TTY_NORMAL))
1556 put_tty_queue(*cp++, ldata);
1557 else
1558 n_tty_receive_char_flagged(tty, *cp++, flag);
1559 }
1560}
1561
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001562static void
1563n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1564 char *fp, int count)
1565{
1566 char flag = TTY_NORMAL;
1567
1568 while (count--) {
1569 if (fp)
1570 flag = *fp++;
1571 if (likely(flag == TTY_NORMAL))
1572 n_tty_receive_char_closing(tty, *cp++);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001573 }
1574}
1575
Peter Hurley7d88d632013-07-24 08:29:49 -04001576static void
1577n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001578 char *fp, int count)
1579{
1580 struct n_tty_data *ldata = tty->disc_data;
1581 char flag = TTY_NORMAL;
1582
1583 while (count--) {
1584 if (fp)
1585 flag = *fp++;
1586 if (likely(flag == TTY_NORMAL)) {
1587 unsigned char c = *cp++;
1588
1589 if (I_ISTRIP(tty))
1590 c &= 0x7f;
1591 if (I_IUCLC(tty) && L_IEXTEN(tty))
1592 c = tolower(c);
1593 if (L_EXTPROC(tty)) {
1594 put_tty_queue(c, ldata);
1595 continue;
1596 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001597 if (!test_bit(c, ldata->char_map))
1598 n_tty_receive_char_inline(tty, c);
1599 else if (n_tty_receive_char_special(tty, c) && count) {
1600 if (fp)
1601 flag = *fp++;
1602 n_tty_receive_char_lnext(tty, *cp++, flag);
1603 count--;
1604 }
Peter Hurley6baad002013-07-24 08:29:52 -04001605 } else
1606 n_tty_receive_char_flagged(tty, *cp++, flag);
1607 }
1608}
1609
1610static void
1611n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1612 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001613{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001614 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001615 char flag = TTY_NORMAL;
1616
1617 while (count--) {
1618 if (fp)
1619 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001620 if (likely(flag == TTY_NORMAL)) {
1621 unsigned char c = *cp++;
1622
1623 if (!test_bit(c, ldata->char_map))
1624 n_tty_receive_char_fast(tty, c);
1625 else if (n_tty_receive_char_special(tty, c) && count) {
1626 if (fp)
1627 flag = *fp++;
1628 n_tty_receive_char_lnext(tty, *cp++, flag);
1629 count--;
1630 }
1631 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001632 n_tty_receive_char_flagged(tty, *cp++, flag);
1633 }
1634}
1635
Peter Hurley24a89d12013-06-15 09:14:15 -04001636static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1637 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001639 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001640 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001642 if (ldata->real_raw)
1643 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001644 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001645 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001646 else if (tty->closing && !L_EXTPROC(tty))
1647 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001648 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001649 if (ldata->lnext) {
1650 char flag = TTY_NORMAL;
1651
1652 if (fp)
1653 flag = *fp++;
1654 n_tty_receive_char_lnext(tty, *cp++, flag);
1655 count--;
1656 }
1657
Peter Hurley7de971b2013-07-24 08:29:53 -04001658 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001659 n_tty_receive_buf_fast(tty, cp, fp, count);
1660 else
1661 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001662
1663 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001664 if (tty->ops->flush_chars)
1665 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Peter Hurley70aca712015-01-16 15:05:37 -05001668 if (ldata->icanon && !L_EXTPROC(tty))
1669 return;
1670
1671 /* publish read_head to consumer */
1672 smp_store_release(&ldata->commit_head, ldata->read_head);
1673
Peter Hurley33d71362016-01-09 21:45:08 -08001674 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001676 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001680/**
1681 * n_tty_receive_buf_common - process input
1682 * @tty: device to receive input
1683 * @cp: input chars
1684 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1685 * @count: number of input chars in @cp
1686 *
1687 * Called by the terminal driver when a block of characters has
1688 * been received. This function must be called from soft contexts
1689 * not from interrupt context. The driver is responsible for making
1690 * calls one at a time and in order (or using flush_to_ldisc)
1691 *
1692 * Returns the # of input chars from @cp which were processed.
1693 *
1694 * In canonical mode, the maximum line length is 4096 chars (including
1695 * the line termination char); lines longer than 4096 chars are
1696 * truncated. After 4095 chars, input data is still processed but
1697 * not stored. Overflow processing ensures the tty can always
1698 * receive more input until at least one line can be read.
1699 *
1700 * In non-canonical mode, the read buffer will only accept 4095 chars;
1701 * this provides the necessary space for a newline char if the input
1702 * mode is switched to canonical.
1703 *
1704 * Note it is possible for the read buffer to _contain_ 4096 chars
1705 * in non-canonical mode: the read buffer could already contain the
1706 * maximum canon line of 4096 chars when the mode is switched to
1707 * non-canonical.
1708 *
1709 * n_tty_receive_buf()/producer path:
1710 * claims non-exclusive termios_rwsem
1711 * publishes commit_head or canon_head
1712 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001713static int
1714n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1715 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001716{
1717 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001718 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001719
Peter Hurley9356b532013-06-15 09:14:24 -04001720 down_read(&tty->termios_rwsem);
1721
Peter Hurley19e2ad62013-07-24 08:29:54 -04001722 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001723 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001724 * When PARMRK is set, each input char may take up to 3 chars
1725 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001726 *
1727 * If we are doing input canonicalization, and there are no
1728 * pending newlines, let characters through without limit, so
1729 * that erase characters will be handled. Other excess
1730 * characters will be beeped.
1731 *
1732 * paired with store in *_copy_from_read_buf() -- guarantees
1733 * the consumer has loaded the data in read_buf up to the new
1734 * read_tail (so this producer will not overwrite unread data)
1735 */
1736 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001737
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001738 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001739 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001740 room = (room + 2) / 3;
1741 room--;
1742 if (room <= 0) {
1743 overflow = ldata->icanon && ldata->canon_head == tail;
1744 if (overflow && room < 0)
1745 ldata->read_head--;
1746 room = overflow;
1747 ldata->no_room = flow && !room;
1748 } else
1749 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001750
Peter Hurley19e2ad62013-07-24 08:29:54 -04001751 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001752 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001753 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001754
1755 /* ignore parity errors if handling overflow */
1756 if (!overflow || !fp || *fp != TTY_PARITY)
1757 __receive_buf(tty, cp, fp, n);
1758
Peter Hurley19e2ad62013-07-24 08:29:54 -04001759 cp += n;
1760 if (fp)
1761 fp += n;
1762 count -= n;
1763 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001764 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001765
Peter Hurley19e2ad62013-07-24 08:29:54 -04001766 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001767
1768 /* Unthrottle if handling overflow on pty */
1769 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1770 if (overflow) {
1771 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1772 tty_unthrottle_safe(tty);
1773 __tty_set_flow_change(tty, 0);
1774 }
1775 } else
1776 n_tty_check_throttle(tty);
1777
Peter Hurley9356b532013-06-15 09:14:24 -04001778 up_read(&tty->termios_rwsem);
1779
Peter Hurley19e2ad62013-07-24 08:29:54 -04001780 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001781}
1782
Peter Hurley5c32d122013-12-02 14:24:41 -05001783static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1784 char *fp, int count)
1785{
1786 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1787}
1788
1789static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1790 char *fp, int count)
1791{
1792 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/**
1796 * n_tty_set_termios - termios data changed
1797 * @tty: terminal
1798 * @old: previous data
1799 *
1800 * Called by the tty layer when the user changes termios flags so
1801 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001802 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * guaranteed that this function will not be re-entered or in progress
1804 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001805 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001806 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Alan Cox4edf1822008-02-08 04:18:44 -08001808
1809static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001811 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001812
Linus Torvalds00fc57a2017-12-20 17:57:06 -08001813 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001814 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001815 ldata->line_start = ldata->read_tail;
1816 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1817 ldata->canon_head = ldata->read_tail;
1818 ldata->push = 0;
1819 } else {
1820 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1821 ldata->read_flags);
1822 ldata->canon_head = ldata->read_head;
1823 ldata->push = 1;
1824 }
Peter Hurley70aca712015-01-16 15:05:37 -05001825 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001826 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001827 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001828 }
1829
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001830 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1833 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1834 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1835 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001836 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001839 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001841 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001844 set_bit(ERASE_CHAR(tty), ldata->char_map);
1845 set_bit(KILL_CHAR(tty), ldata->char_map);
1846 set_bit(EOF_CHAR(tty), ldata->char_map);
1847 set_bit('\n', ldata->char_map);
1848 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001850 set_bit(WERASE_CHAR(tty), ldata->char_map);
1851 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1852 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (L_ECHO(tty))
1854 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001855 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
1858 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001859 set_bit(START_CHAR(tty), ldata->char_map);
1860 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001863 set_bit(INTR_CHAR(tty), ldata->char_map);
1864 set_bit(QUIT_CHAR(tty), ldata->char_map);
1865 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001867 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001868 ldata->raw = 0;
1869 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001871 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1873 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1874 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001875 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001877 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001879 /*
1880 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1881 * been stopped by STOP_CHAR(tty) before it.
1882 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001883 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001884 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001885 process_echoes(tty);
1886 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001887
Alan Coxf34d7a52008-04-30 00:54:13 -07001888 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001889 wake_up_interruptible(&tty->write_wait);
1890 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893/**
1894 * n_tty_close - close the ldisc for this tty
1895 * @tty: device
1896 *
Alan Cox4edf1822008-02-08 04:18:44 -08001897 * Called from the terminal layer when this line discipline is
1898 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * discipline change. The function will not be called while other
1900 * ldisc methods are in progress.
1901 */
Alan Cox4edf1822008-02-08 04:18:44 -08001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static void n_tty_close(struct tty_struct *tty)
1904{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001905 struct n_tty_data *ldata = tty->disc_data;
1906
Peter Hurley79901312013-03-11 16:44:23 -04001907 if (tty->link)
1908 n_tty_packet_mode_flush(tty);
1909
Peter Hurley20bafb32013-06-15 10:21:19 -04001910 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001911 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
1914/**
1915 * n_tty_open - open an ldisc
1916 * @tty: terminal to open
1917 *
Alan Cox4edf1822008-02-08 04:18:44 -08001918 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * terminal device. Can sleep. Called serialized so that no
1920 * other events will occur in parallel. No further open will occur
1921 * until a close.
1922 */
1923
1924static int n_tty_open(struct tty_struct *tty)
1925{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001926 struct n_tty_data *ldata;
1927
Peter Hurley20bafb32013-06-15 10:21:19 -04001928 /* Currently a malloc failure here can panic */
Tetsuo Handa9264e982018-05-26 09:53:14 +09001929 ldata = vzalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001930 if (!ldata)
Tetsuo Handa9264e982018-05-26 09:53:14 +09001931 return -ENOMEM;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001932
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001933 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001934 mutex_init(&ldata->atomic_read_lock);
1935 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001936
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001937 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001939 /* indicate buffer work may resume */
1940 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1941 n_tty_set_termios(tty, NULL);
1942 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
1944}
1945
Peter Hurleyeafbe672013-12-02 14:24:45 -05001946static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001948 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001949 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001950
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001951 if (ldata->icanon && !L_EXTPROC(tty))
1952 return ldata->canon_head != ldata->read_tail;
1953 else
Peter Hurley70aca712015-01-16 15:05:37 -05001954 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001958 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * @tty: terminal device
1960 * @b: user data
1961 * @nr: size of data
1962 *
Alan Cox11a96d12008-10-13 10:46:24 +01001963 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 * ICANON is off; it copies characters straight from the tty queue to
1965 * user space directly. It can be profitably called twice; once to
1966 * drain the space from the tail pointer to the (physical) end of the
1967 * buffer, and once to drain the space from the (physical) beginning of
1968 * the buffer to head pointer.
1969 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001970 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001972 * n_tty_read()/consumer path:
1973 * caller holds non-exclusive termios_rwsem
1974 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 */
Alan Cox4edf1822008-02-08 04:18:44 -08001976
Alan Cox33f0f882006-01-09 20:54:13 -08001977static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 unsigned char __user **b,
1979 size_t *nr)
1980
1981{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001982 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int retval;
1984 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001985 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001986 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001987 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001990 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001993 const unsigned char *from = read_buf_addr(ldata, tail);
1994 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001996 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08001997 tty_audit_add_data(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05001998 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001999 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05002000 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2001 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02002002 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 *b += n;
2004 *nr -= n;
2005 }
2006 return retval;
2007}
2008
Peter Hurley88bb0de2013-06-15 09:14:16 -04002009/**
Peter Hurley32f13522013-06-15 09:14:17 -04002010 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04002011 * @tty: terminal device
2012 * @b: user data
2013 * @nr: size of data
2014 *
2015 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04002016 * it copies one line of input up to and including the line-delimiting
2017 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04002018 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05002019 * NB: When termios is changed from non-canonical to canonical mode and
2020 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2021 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2022 * This causes data already processed as input to be immediately available
2023 * as input although a newline has not been received.
2024 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04002025 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04002026 *
2027 * n_tty_read()/consumer path:
2028 * caller holds non-exclusive termios_rwsem
2029 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002030 */
2031
Peter Hurley32f13522013-06-15 09:14:17 -04002032static int canon_copy_from_read_buf(struct tty_struct *tty,
2033 unsigned char __user **b,
2034 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002035{
2036 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002037 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002038 size_t eol;
2039 size_t tail;
2040 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002041
2042 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002043 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002044 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002045
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002046 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2047
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002048 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002049 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2050
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002051 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002052 __func__, *nr, tail, n, size);
2053
2054 eol = find_next_bit(ldata->read_flags, size, tail);
2055 more = n - (size - tail);
2056 if (eol == N_TTY_BUF_SIZE && more) {
2057 /* scan wrapped without finding set bit */
2058 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002059 found = eol != more;
2060 } else
2061 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002062
Peter Hurleyc77569d2013-11-22 07:16:25 -05002063 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002064 if (n > N_TTY_BUF_SIZE)
2065 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002066 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002067
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002068 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2069 c = min(*nr, c);
2070 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002071 }
Peter Hurley32f13522013-06-15 09:14:17 -04002072
Peter Hurley679e7c22015-11-27 14:11:02 -05002073 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2074 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002075
Peter Hurley679e7c22015-11-27 14:11:02 -05002076 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002077 if (ret)
2078 return -EFAULT;
2079 *b += n;
2080 *nr -= n;
2081
Peter Hurleya73d3d62013-06-15 09:14:25 -04002082 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002083 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002084 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002085
Peter Hurley40d5e092013-06-15 10:21:17 -04002086 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002087 if (!ldata->push)
2088 ldata->line_start = ldata->read_tail;
2089 else
2090 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002091 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002092 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002093 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002094}
2095
Al Virocc4191d2008-03-29 03:08:48 +00002096extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002097 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099/**
2100 * job_control - check job control
2101 * @tty: tty
2102 * @file: file handle
2103 *
2104 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002105 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002107 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002108 * Locking: redirected write test is safe
2109 * current->signal->tty check is safe
2110 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 */
Alan Cox4edf1822008-02-08 04:18:44 -08002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113static int job_control(struct tty_struct *tty, struct file *file)
2114{
2115 /* Job control check -- must be done at start and after
2116 every sleep (POSIX.1 7.1.1.4). */
2117 /* NOTE: not yet done after every sleep pending a thorough
2118 check of the logic of this change. -- jlc */
2119 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002120 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002121 return 0;
2122
Peter Hurley2812d9e2015-10-10 20:28:42 -04002123 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
Alan Cox4edf1822008-02-08 04:18:44 -08002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127/**
Alan Cox11a96d12008-10-13 10:46:24 +01002128 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 * @tty: tty device
2130 * @file: file object
2131 * @buf: userspace buffer pointer
2132 * @nr: size of I/O
2133 *
2134 * Perform reads for the line discipline. We are guaranteed that the
2135 * line discipline will not be closed under us but we may get multiple
2136 * parallel readers and must handle this ourselves. We may also get
2137 * a hangup. Always called in user context, may sleep.
2138 *
2139 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002140 *
2141 * n_tty_read()/consumer path:
2142 * claims non-exclusive termios_rwsem
2143 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
Alan Cox4edf1822008-02-08 04:18:44 -08002145
Alan Cox11a96d12008-10-13 10:46:24 +01002146static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 unsigned char __user *buf, size_t nr)
2148{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002149 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002151 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002152 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int minimum, time;
2154 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002156 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002157 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002160 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002162
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002163 /*
2164 * Internal serialization of reads.
2165 */
2166 if (file->f_flags & O_NONBLOCK) {
2167 if (!mutex_trylock(&ldata->atomic_read_lock))
2168 return -EAGAIN;
2169 } else {
2170 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2171 return -ERESTARTSYS;
2172 }
2173
Peter Hurley9356b532013-06-15 09:14:24 -04002174 down_read(&tty->termios_rwsem);
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 minimum = time = 0;
2177 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002178 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 minimum = MIN_CHAR(tty);
2180 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002181 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002183 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002184 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 }
2187
Alan Cox04f378b2008-04-30 00:53:29 -07002188 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002189 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 while (nr) {
2193 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002194 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 unsigned char cs;
2196 if (b != buf)
2197 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002198 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 cs = tty->link->ctrl_status;
2200 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002201 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002202 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 break;
2205 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002206 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 nr--;
2208 break;
2209 }
Alan Cox4edf1822008-02-08 04:18:44 -08002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (!input_available_p(tty, 0)) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002212 up_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002213 tty_buffer_flush_work(tty->port);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002214 down_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002215 if (!input_available_p(tty, 0)) {
2216 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2217 retval = -EIO;
2218 break;
2219 }
2220 if (tty_hung_up_p(file))
2221 break;
Tejun Heoa7e19062018-02-13 07:38:08 -08002222 /*
2223 * Abort readers for ttys which never actually
2224 * get hung up. See __tty_hangup().
2225 */
2226 if (test_bit(TTY_HUPPING, &tty->flags))
2227 break;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002228 if (!timeout)
2229 break;
2230 if (file->f_flags & O_NONBLOCK) {
2231 retval = -EAGAIN;
2232 break;
2233 }
2234 if (signal_pending(current)) {
2235 retval = -ERESTARTSYS;
2236 break;
2237 }
2238 up_read(&tty->termios_rwsem);
2239
2240 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2241 timeout);
2242
2243 down_read(&tty->termios_rwsem);
2244 continue;
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002248 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002249 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002250 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 break;
2252 } else {
2253 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002254
2255 /* Deal with packet mode. */
2256 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002257 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002258 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002259 break;
2260 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002261 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002262 nr--;
2263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 uncopied = copy_from_read_buf(tty, &b, &nr);
2266 uncopied += copy_from_read_buf(tty, &b, &nr);
2267 if (uncopied) {
2268 retval = -EFAULT;
2269 break;
2270 }
2271 }
2272
Peter Hurley6367ca72013-06-15 09:14:33 -04002273 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (b - buf >= minimum)
2276 break;
2277 if (time)
2278 timeout = time;
2279 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002280 if (tail != ldata->read_tail)
2281 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002282 up_read(&tty->termios_rwsem);
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002285 mutex_unlock(&ldata->atomic_read_lock);
2286
Peter Hurley40d5e092013-06-15 10:21:17 -04002287 if (b - buf)
2288 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 return retval;
2291}
2292
2293/**
Alan Cox11a96d12008-10-13 10:46:24 +01002294 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 * @tty: tty device
2296 * @file: file object
2297 * @buf: userspace buffer pointer
2298 * @nr: size of I/O
2299 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002300 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002302 * and other such events. Since the receive code will echo characters,
2303 * thus calling driver write methods, the output_lock is used in
2304 * the output processing functions called here as well as in the
2305 * echo processing function to protect the column state and space
2306 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 *
2308 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002309 *
2310 * Locking: output_lock to protect column state and space left
2311 * (note that the process_output*() functions take this
2312 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 */
Alan Cox4edf1822008-02-08 04:18:44 -08002314
Alan Cox11a96d12008-10-13 10:46:24 +01002315static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002316 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002319 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int c;
2321 ssize_t retval = 0;
2322
2323 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2324 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2325 retval = tty_check_change(tty);
2326 if (retval)
2327 return retval;
2328 }
2329
Peter Hurley9356b532013-06-15 09:14:24 -04002330 down_read(&tty->termios_rwsem);
2331
Joe Petersona88a69c2009-01-02 13:40:53 +00002332 /* Write out any echoed characters that are still pending */
2333 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 add_wait_queue(&tty->write_wait, &wait);
2336 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (signal_pending(current)) {
2338 retval = -ERESTARTSYS;
2339 break;
2340 }
2341 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2342 retval = -EIO;
2343 break;
2344 }
Peter Hurley582f5592013-05-17 12:49:48 -04002345 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002347 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (num < 0) {
2349 if (num == -EAGAIN)
2350 break;
2351 retval = num;
2352 goto break_out;
2353 }
2354 b += num;
2355 nr -= num;
2356 if (nr == 0)
2357 break;
2358 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002359 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 break;
2361 b++; nr--;
2362 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002363 if (tty->ops->flush_chars)
2364 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002366 struct n_tty_data *ldata = tty->disc_data;
2367
Roman Zippeld6afe272005-07-07 17:56:55 -07002368 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002369 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002370 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002371 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002372 if (c < 0) {
2373 retval = c;
2374 goto break_out;
2375 }
2376 if (!c)
2377 break;
2378 b += c;
2379 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 if (!nr)
2383 break;
2384 if (file->f_flags & O_NONBLOCK) {
2385 retval = -EAGAIN;
2386 break;
2387 }
Peter Hurley9356b532013-06-15 09:14:24 -04002388 up_read(&tty->termios_rwsem);
2389
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002390 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002391
2392 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 remove_wait_queue(&tty->write_wait, &wait);
Peter Hurley87108bc2016-01-09 21:45:14 -08002396 if (nr && tty->fasync)
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002397 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002398 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return (b - buf) ? b - buf : retval;
2400}
2401
2402/**
Alan Cox11a96d12008-10-13 10:46:24 +01002403 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * @tty: terminal device
2405 * @file: file accessing it
2406 * @wait: poll table
2407 *
2408 * Called when the line discipline is asked to poll() for data or
2409 * for special events. This code is not serialized with respect to
2410 * other events save open/close.
2411 *
2412 * This code must be sure never to sleep through a hangup.
2413 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 */
Alan Cox4edf1822008-02-08 04:18:44 -08002415
Alan Cox11a96d12008-10-13 10:46:24 +01002416static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002417 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 unsigned int mask = 0;
2420
2421 poll_wait(file, &tty->read_wait, wait);
2422 poll_wait(file, &tty->write_wait, wait);
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002423 if (input_available_p(tty, 1))
2424 mask |= POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002425 else {
2426 tty_buffer_flush_work(tty->port);
2427 if (input_available_p(tty, 1))
2428 mask |= POLLIN | POLLRDNORM;
2429 }
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002430 if (tty->packet && tty->link->ctrl_status)
2431 mask |= POLLPRI | POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002432 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2433 mask |= POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (tty_hung_up_p(file))
2435 mask |= POLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002436 if (tty->ops->write && !tty_is_writelocked(tty) &&
2437 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2438 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 mask |= POLLOUT | POLLWRNORM;
2440 return mask;
2441}
2442
Jiri Slaby57c94122012-10-18 22:26:43 +02002443static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002444{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002445 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002446
Peter Hurleya73d3d62013-06-15 09:14:25 -04002447 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002448 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002449 head = ldata->canon_head;
2450 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002451 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002452 /* Skip EOF-chars.. */
Tetsuo Handa947dead2018-05-26 09:53:13 +09002453 while (MASK(head) != MASK(tail)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002454 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2455 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002456 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002457 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002458 }
2459 return nr;
2460}
2461
2462static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2463 unsigned int cmd, unsigned long arg)
2464{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002465 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002466 int retval;
2467
2468 switch (cmd) {
2469 case TIOCOUTQ:
2470 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2471 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002472 down_write(&tty->termios_rwsem);
Linus Torvalds00fc57a2017-12-20 17:57:06 -08002473 if (L_ICANON(tty) && !L_EXTPROC(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002474 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002475 else
2476 retval = read_cnt(ldata);
2477 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002478 return put_user(retval, (unsigned int __user *) arg);
2479 default:
2480 return n_tty_ioctl_helper(tty, file, cmd, arg);
2481 }
2482}
2483
Peter Hurley27228732016-01-09 21:35:19 -08002484static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002485 .magic = TTY_LDISC_MAGIC,
2486 .name = "n_tty",
2487 .open = n_tty_open,
2488 .close = n_tty_close,
2489 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002490 .read = n_tty_read,
2491 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002492 .ioctl = n_tty_ioctl,
2493 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002494 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002495 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002496 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002497 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002499
2500/**
2501 * n_tty_inherit_ops - inherit N_TTY methods
2502 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2503 *
Peter Hurley27228732016-01-09 21:35:19 -08002504 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002505 */
2506
2507void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2508{
Peter Hurley27228732016-01-09 21:35:19 -08002509 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002510 ops->owner = NULL;
2511 ops->refcount = ops->flags = 0;
2512}
2513EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002514
2515void __init n_tty_init(void)
2516{
2517 tty_register_ldisc(N_TTY, &n_tty_ops);
2518}