blob: a75a6e1eb5105a8c2d197247aa5bceda86d39003 [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
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -0700161/* If we are not echoing the data, perhaps this is a secret so erase it */
162static void zero_buffer(struct tty_struct *tty, u8 *buffer, int size)
163{
164 bool icanon = !!L_ICANON(tty);
165 bool no_echo = !L_ECHO(tty);
166
167 if (icanon && no_echo)
168 memset(buffer, 0x00, size);
169}
170
Peter Hurley679e7c22015-11-27 14:11:02 -0500171static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
172 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700173{
174 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500175 size_t size = N_TTY_BUF_SIZE - tail;
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -0700176 void *from = read_buf_addr(ldata, tail);
Peter Hurley679e7c22015-11-27 14:11:02 -0500177 int uncopied;
178
179 if (n > size) {
Peter Hurley309426a2016-01-09 22:55:27 -0800180 tty_audit_add_data(tty, from, size);
Peter Hurley679e7c22015-11-27 14:11:02 -0500181 uncopied = copy_to_user(to, from, size);
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -0700182 zero_buffer(tty, from, size - uncopied);
Peter Hurley679e7c22015-11-27 14:11:02 -0500183 if (uncopied)
184 return uncopied;
185 to += size;
186 n -= size;
187 from = ldata->read_buf;
188 }
Laura Abbott72586c62015-05-14 11:42:17 -0700189
Peter Hurley309426a2016-01-09 22:55:27 -0800190 tty_audit_add_data(tty, from, n);
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -0700191 uncopied = copy_to_user(to, from, n);
192 zero_buffer(tty, from, n - uncopied);
193 return uncopied;
Laura Abbott72586c62015-05-14 11:42:17 -0700194}
195
Peter Hurley24a89d12013-06-15 09:14:15 -0400196/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500197 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400198 * @tty: terminal
199 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500200 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400201 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400202 * Caller holds exclusive termios_rwsem
203 * or
204 * n_tty_read()/consumer path:
205 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400206 */
207
Peter Hurley2c5dc462015-01-16 15:05:34 -0500208static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400209{
Peter Hurley24a89d12013-06-15 09:14:15 -0400210 struct n_tty_data *ldata = tty->disc_data;
211
Peter Hurley2c5dc462015-01-16 15:05:34 -0500212 /* Did the input worker stop? Restart it */
213 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400214 ldata->no_room = 0;
215
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200216 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400217 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400218 /* see if ldisc has been killed - if so, this means that
219 * even though the ldisc has been halted and ->buf.work
220 * cancelled, ->buf.work is about to be rescheduled
221 */
222 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
223 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400224 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200225 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900226}
227
Peter Hurley9a4aec22013-06-15 09:14:32 -0400228static ssize_t chars_in_buffer(struct tty_struct *tty)
229{
230 struct n_tty_data *ldata = tty->disc_data;
231 ssize_t n = 0;
232
233 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500234 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400235 else
236 n = ldata->canon_head - ldata->read_tail;
237 return n;
238}
239
Peter Hurleyee0bab82013-06-15 09:14:34 -0400240/**
241 * n_tty_write_wakeup - asynchronous I/O notifier
242 * @tty: tty device
243 *
244 * Required for the ptys, serial driver etc. since processes
245 * that attach themselves to the master and rely on ASYNC
246 * IO must be woken up
247 */
248
249static void n_tty_write_wakeup(struct tty_struct *tty)
250{
Peter Hurley7bccc362016-01-09 21:45:12 -0800251 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
252 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Peter Hurleyee0bab82013-06-15 09:14:34 -0400253}
254
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400255static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400256{
Peter Hurleya3428462015-01-16 15:05:35 -0500257 struct n_tty_data *ldata = tty->disc_data;
258
Peter Hurley6367ca72013-06-15 09:14:33 -0400259 /*
260 * Check the remaining room for the input canonicalization
261 * mode. We don't want to throttle the driver if we're in
262 * canonical mode and don't have a newline yet!
263 */
Peter Hurleya3428462015-01-16 15:05:35 -0500264 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
265 return;
266
Peter Hurley6367ca72013-06-15 09:14:33 -0400267 while (1) {
268 int throttled;
269 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500270 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400271 break;
272 throttled = tty_throttle_safe(tty);
273 if (!throttled)
274 break;
275 }
276 __tty_set_flow_change(tty, 0);
277}
278
Peter Hurley4b293492013-07-24 08:29:55 -0400279static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400280{
Peter Hurley6d27a632016-01-10 22:40:56 -0800281 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400282 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
283 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500284 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800285 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400286 return;
287 }
288
Peter Hurley6367ca72013-06-15 09:14:33 -0400289 /* If there is enough space in the read buffer now, let the
290 * low-level driver know. We use chars_in_buffer() to
291 * check the buffer, as it now knows about canonical mode.
292 * Otherwise, if the driver is throttled and the line is
293 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
294 * we won't get any more characters.
295 */
296
297 while (1) {
298 int unthrottled;
299 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
300 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
301 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500302 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400303 unthrottled = tty_unthrottle_safe(tty);
304 if (!unthrottled)
305 break;
306 }
307 __tty_set_flow_change(tty, 0);
308}
309
Alan Cox17b82062008-10-13 10:45:06 +0100310/**
311 * put_tty_queue - add character to tty
312 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200313 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100314 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400315 * Add a character to the tty read_buf queue.
316 *
317 * n_tty_receive_buf()/producer path:
318 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100319 */
320
Peter Hurley19e2ad62013-07-24 08:29:54 -0400321static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100323 *read_buf_addr(ldata, ldata->read_head) = c;
324 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * reset_buffer_flags - reset buffer state
329 * @tty: terminal to reset
330 *
Peter Hurley25518c62013-03-11 16:44:31 -0400331 * Reset the read buffer counters and clear the flags.
332 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100333 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400334 * Locking: caller holds exclusive termios_rwsem
335 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000337
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400338static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400340 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500341 ldata->commit_head = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400342 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000343
Peter Hurleya73d3d62013-06-15 09:14:25 -0400344 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200345 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500346 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Peter Hurleya30737a2013-03-11 16:44:22 -0400349static void n_tty_packet_mode_flush(struct tty_struct *tty)
350{
351 unsigned long flags;
352
Peter Hurleya30737a2013-03-11 16:44:22 -0400353 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400354 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400355 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400356 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000357 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400358 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
362 * n_tty_flush_buffer - clean input queue
363 * @tty: terminal device
364 *
Peter Hurley25518c62013-03-11 16:44:31 -0400365 * Flush the input buffer. Called when the tty layer wants the
366 * buffer flushed (eg at hangup) or when the N_TTY line discipline
367 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400369 * Holds termios_rwsem to exclude producer/consumer while
370 * buffer indices are reset.
371 *
372 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Alan Cox4edf1822008-02-08 04:18:44 -0800374
375static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400377 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400378 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500379 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800380
Peter Hurleya30737a2013-03-11 16:44:22 -0400381 if (tty->link)
382 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400383 up_write(&tty->termios_rwsem);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * is_utf8_continuation - utf8 multibyte check
388 * @c: byte to check
389 *
390 * Returns true if the utf8 character 'c' is a multibyte continuation
391 * character. We use this to correctly compute the on screen size
392 * of the character when printing
393 */
Alan Cox4edf1822008-02-08 04:18:44 -0800394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static inline int is_utf8_continuation(unsigned char c)
396{
397 return (c & 0xc0) == 0x80;
398}
399
400/**
401 * is_continuation - multibyte check
402 * @c: byte to check
403 *
404 * Returns true if the utf8 character 'c' is a multibyte continuation
405 * character and the terminal is in unicode mode.
406 */
Alan Cox4edf1822008-02-08 04:18:44 -0800407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static inline int is_continuation(unsigned char c, struct tty_struct *tty)
409{
410 return I_IUTF8(tty) && is_utf8_continuation(c);
411}
412
413/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000414 * do_output_char - output one character
415 * @c: character (or partial unicode symbol)
416 * @tty: terminal device
417 * @space: space available in tty driver write buffer
418 *
419 * This is a helper function that handles one output character
420 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600421 * doing OPOST processing and putting the results in the
422 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000423 *
424 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
425 * and NLDLY. They simply aren't relevant in the world today.
426 * If you ever need them, add them here.
427 *
428 * Returns the number of bytes of buffer space used or -1 if
429 * no space left.
430 *
431 * Locking: should be called under the output_lock to protect
432 * the column state and space left in the buffer
433 */
434
435static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
436{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200437 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 int spaces;
439
440 if (!space)
441 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000442
Joe Petersona88a69c2009-01-02 13:40:53 +0000443 switch (c) {
444 case '\n':
445 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200446 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000447 if (O_ONLCR(tty)) {
448 if (space < 2)
449 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200450 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700451 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 return 2;
453 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200454 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000455 break;
456 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200457 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000458 return 0;
459 if (O_OCRNL(tty)) {
460 c = '\n';
461 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200462 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000463 break;
464 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200465 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000466 break;
467 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200468 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000469 if (O_TABDLY(tty) == XTABS) {
470 if (space < spaces)
471 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200472 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000473 tty->ops->write(tty, " ", spaces);
474 return spaces;
475 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200476 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000477 break;
478 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200479 if (ldata->column > 0)
480 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000481 break;
482 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000483 if (!iscntrl(c)) {
484 if (O_OLCUC(tty))
485 c = toupper(c);
486 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200487 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000488 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000489 break;
490 }
491
492 tty_put_char(tty, c);
493 return 1;
494}
495
496/**
497 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * @c: character (or partial unicode symbol)
499 * @tty: terminal device
500 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600501 * Output one character with OPOST processing.
502 * Returns -1 when the output device is full and the character
503 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000505 * Locking: output_lock to protect column state and space left
506 * (also, this is called from n_tty_write under the
507 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
Alan Cox4edf1822008-02-08 04:18:44 -0800509
Joe Petersona88a69c2009-01-02 13:40:53 +0000510static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jiri Slabybddc7152012-10-18 22:26:42 +0200512 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000513 int space, retval;
514
Jiri Slabybddc7152012-10-18 22:26:42 +0200515 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alan Coxf34d7a52008-04-30 00:54:13 -0700517 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000518 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jiri Slabybddc7152012-10-18 22:26:42 +0200520 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000521 if (retval < 0)
522 return -1;
523 else
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000528 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600530 * @buf: character buffer
531 * @nr: number of bytes to output
532 *
533 * Output a block of characters with OPOST processing.
534 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
536 * This path is used to speed up block console writes, among other
537 * things when processing blocks of output data. It handles only
538 * the simple cases normally found and helps to generate blocks of
539 * symbols for the console driver and thus improve performance.
540 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000541 * Locking: output_lock to protect column state and space left
542 * (also, this is called from n_tty_write under the
543 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Alan Cox4edf1822008-02-08 04:18:44 -0800545
Joe Petersona88a69c2009-01-02 13:40:53 +0000546static ssize_t process_output_block(struct tty_struct *tty,
547 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200549 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100551 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 const unsigned char *cp;
553
Jiri Slabybddc7152012-10-18 22:26:42 +0200554 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000555
Alan Coxf34d7a52008-04-30 00:54:13 -0700556 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000557 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200558 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (nr > space)
562 nr = space;
563
564 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000565 unsigned char c = *cp;
566
567 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case '\n':
569 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200570 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (O_ONLCR(tty))
572 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200573 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200576 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto break_out;
578 if (O_OCRNL(tty))
579 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200580 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 case '\t':
583 goto break_out;
584 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200585 if (ldata->column > 0)
586 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
588 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000589 if (!iscntrl(c)) {
590 if (O_OLCUC(tty))
591 goto break_out;
592 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200593 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597 }
598break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700599 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000600
Jiri Slabybddc7152012-10-18 22:26:42 +0200601 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return i;
603}
604
Joe Petersona88a69c2009-01-02 13:40:53 +0000605/**
606 * process_echoes - write pending echo characters
607 * @tty: terminal device
608 *
609 * Write previously buffered echo (and other ldisc-generated)
610 * characters to the tty.
611 *
612 * Characters generated by the ldisc (including echoes) need to
613 * be buffered because the driver's write buffer can fill during
614 * heavy program output. Echoing straight to the driver will
615 * often fail under these conditions, causing lost characters and
616 * resulting mismatches of ldisc state information.
617 *
618 * Since the ldisc state must represent the characters actually sent
619 * to the driver at the time of the write, operations like certain
620 * changes in column state are also saved in the buffer and executed
621 * here.
622 *
623 * A circular fifo buffer is used so that the most recent characters
624 * are prioritized. Also, when control characters are echoed with a
625 * prefixed "^", the pair is treated atomically and thus not separated.
626 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400627 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 */
629
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400630static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000631{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200632 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400633 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400634 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000635 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000636
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400637 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000638
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400639 tail = ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900640 while (MASK(ldata->echo_commit) != MASK(tail)) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400641 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000642 if (c == ECHO_OP_START) {
643 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000644 int no_space_left = 0;
645
646 /*
Tetsuo Handa9264e982018-05-26 09:53:14 +0900647 * Since add_echo_byte() is called without holding
648 * output_lock, we might see only portion of multi-byte
649 * operation.
650 */
651 if (MASK(ldata->echo_commit) == MASK(tail + 1))
652 goto not_yet_stored;
653 /*
Joe Petersona88a69c2009-01-02 13:40:53 +0000654 * If the buffer byte is the start of a multi-byte
655 * operation, get the next byte, which is either the
656 * op code or a control character value.
657 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400658 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000659
Joe Petersona88a69c2009-01-02 13:40:53 +0000660 switch (op) {
661 unsigned int num_chars, num_bs;
662
663 case ECHO_OP_ERASE_TAB:
Tetsuo Handa9264e982018-05-26 09:53:14 +0900664 if (MASK(ldata->echo_commit) == MASK(tail + 2))
665 goto not_yet_stored;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400666 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000667
668 /*
669 * Determine how many columns to go back
670 * in order to erase the tab.
671 * This depends on the number of columns
672 * used by other characters within the tab
673 * area. If this (modulo 8) count is from
674 * the start of input rather than from a
675 * previous tab, we offset by canon column.
676 * Otherwise, tab spacing is normal.
677 */
678 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200679 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 num_bs = 8 - (num_chars & 7);
681
682 if (num_bs > space) {
683 no_space_left = 1;
684 break;
685 }
686 space -= num_bs;
687 while (num_bs--) {
688 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200689 if (ldata->column > 0)
690 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000691 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400692 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 break;
694
695 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200696 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400697 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000698 break;
699
700 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200701 if (ldata->column > 0)
702 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400703 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 break;
705
706 case ECHO_OP_START:
707 /* This is an escaped echo op start code */
708 if (!space) {
709 no_space_left = 1;
710 break;
711 }
712 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200713 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000714 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400715 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000716 break;
717
718 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000719 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600720 * If the op is not a special byte code,
721 * it is a ctrl char tagged to be echoed
722 * as "^X" (where X is the letter
723 * representing the control char).
724 * Note that we must ensure there is
725 * enough space for the whole ctrl pair.
726 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 */
Joe Peterson62b26352009-09-09 15:03:47 -0600728 if (space < 2) {
729 no_space_left = 1;
730 break;
731 }
732 tty_put_char(tty, '^');
733 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200734 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600735 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400736 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737 }
738
739 if (no_space_left)
740 break;
741 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400742 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600743 int retval = do_output_char(c, tty, space);
744 if (retval < 0)
745 break;
746 space -= retval;
747 } else {
748 if (!space)
749 break;
750 tty_put_char(tty, c);
751 space -= 1;
752 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400753 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000754 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000755 }
756
Peter Hurleycbfd0342013-06-15 10:04:26 -0400757 /* If the echo buffer is nearly full (so that the possibility exists
758 * of echo overrun before the next commit), then discard enough
759 * data at the tail to prevent a subsequent overrun */
Tetsuo Handa9264e982018-05-26 09:53:14 +0900760 while (ldata->echo_commit > tail &&
761 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200762 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500763 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400764 tail += 3;
765 else
766 tail += 2;
767 } else
768 tail++;
769 }
770
Carl van Schaikb38f1742018-07-03 12:02:30 +1000771#if defined(CONFIG_TTY_FLUSH_LOCAL_ECHO)
772 if (ldata->echo_commit != tail) {
773 if (!tty->delayed_work) {
774 INIT_DELAYED_WORK(&tty->echo_delayed_work, continue_process_echoes);
775 schedule_delayed_work(&tty->echo_delayed_work, 1);
776 }
777 tty->delayed_work = 1;
778 }
779#endif
780
Tetsuo Handa9264e982018-05-26 09:53:14 +0900781 not_yet_stored:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400782 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400783 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Peter Hurley17bd7902013-06-15 10:04:24 -0400786static void commit_echoes(struct tty_struct *tty)
787{
788 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400789 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400790 size_t head;
791
Tetsuo Handa9264e982018-05-26 09:53:14 +0900792 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400793 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500794 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400795 old = ldata->echo_commit - ldata->echo_tail;
796
797 /* Process committed echoes if the accumulated # of bytes
798 * is over the threshold (and try again each time another
799 * block is accumulated) */
800 nr = head - ldata->echo_tail;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900801 if (nr < ECHO_COMMIT_WATERMARK ||
802 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
803 mutex_unlock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400804 return;
Tetsuo Handa9264e982018-05-26 09:53:14 +0900805 }
Peter Hurley17bd7902013-06-15 10:04:24 -0400806
Peter Hurleycbfd0342013-06-15 10:04:26 -0400807 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400808 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400809 mutex_unlock(&ldata->output_lock);
810
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400811 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400812 tty->ops->flush_chars(tty);
813}
814
815static void process_echoes(struct tty_struct *tty)
816{
817 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400818 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400819
Peter Hurleye2613be2014-02-11 16:34:55 -0500820 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400821 return;
822
823 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500824 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400825 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400826 mutex_unlock(&ldata->output_lock);
827
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400828 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400829 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400830}
831
Peter Hurley1075a6e2013-12-09 18:06:07 -0500832/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400833static void flush_echoes(struct tty_struct *tty)
834{
835 struct n_tty_data *ldata = tty->disc_data;
836
Peter Hurley39434ab2013-11-29 12:56:10 -0500837 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
838 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400839 return;
840
841 mutex_lock(&ldata->output_lock);
842 ldata->echo_commit = ldata->echo_head;
843 __process_echoes(tty);
844 mutex_unlock(&ldata->output_lock);
845}
846
Carl van Schaikb38f1742018-07-03 12:02:30 +1000847#if defined(CONFIG_TTY_FLUSH_LOCAL_ECHO)
848static void continue_process_echoes(struct work_struct *work)
849{
850 struct tty_struct *tty =
851 container_of(work, struct tty_struct, echo_delayed_work.work);
852 struct n_tty_data *ldata = tty->disc_data;
853
854 mutex_lock(&ldata->output_lock);
855 tty->delayed_work = 0;
856 __process_echoes(tty);
857 mutex_unlock(&ldata->output_lock);
858}
859#endif
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000862 * add_echo_byte - add a byte to the echo buffer
863 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000865 *
866 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000867 */
868
Peter Hurleycbfd0342013-06-15 10:04:26 -0400869static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000870{
Tetsuo Handa9264e982018-05-26 09:53:14 +0900871 *echo_buf_addr(ldata, ldata->echo_head) = c;
872 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
873 ldata->echo_head++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000874}
875
876/**
877 * echo_move_back_col - add operation to move back a 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 move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000881 */
882
Jiri Slaby57c94122012-10-18 22:26:43 +0200883static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000884{
Jiri Slaby57c94122012-10-18 22:26:43 +0200885 add_echo_byte(ECHO_OP_START, ldata);
886 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000887}
888
889/**
890 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200891 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000892 *
893 * Add an operation to the echo buffer to set the canon column
894 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000895 */
896
Jiri Slaby57c94122012-10-18 22:26:43 +0200897static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000898{
Jiri Slaby57c94122012-10-18 22:26:43 +0200899 add_echo_byte(ECHO_OP_START, ldata);
900 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000901}
902
903/**
904 * echo_erase_tab - add operation to erase a tab
905 * @num_chars: number of character columns already used
906 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200907 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000908 *
909 * Add an operation to the echo buffer to erase a tab.
910 *
911 * Called by the eraser function, which knows how many character
912 * columns have been used since either a previous tab or the start
913 * of input. This information will be used later, along with
914 * canon column (if applicable), to go back the correct number
915 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000916 */
917
918static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200919 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000920{
Jiri Slaby57c94122012-10-18 22:26:43 +0200921 add_echo_byte(ECHO_OP_START, ldata);
922 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000923
924 /* We only need to know this modulo 8 (tab spacing) */
925 num_chars &= 7;
926
927 /* Set the high bit as a flag if num_chars is after a previous tab */
928 if (after_tab)
929 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000930
Jiri Slaby57c94122012-10-18 22:26:43 +0200931 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000932}
933
934/**
935 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * @c: unicode byte to echo
937 * @tty: terminal device
938 *
Alan Cox4edf1822008-02-08 04:18:44 -0800939 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100941 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000942 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000943 */
944
Jiri Slaby57c94122012-10-18 22:26:43 +0200945static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000946{
Joe Petersona88a69c2009-01-02 13:40:53 +0000947 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200948 add_echo_byte(ECHO_OP_START, ldata);
949 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000950 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200951 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000952 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000953}
954
955/**
956 * echo_char - echo a character
957 * @c: unicode byte to echo
958 * @tty: terminal device
959 *
960 * Echo user input back onto the screen. This must be called only when
961 * L_ECHO(tty) is true. Called from the driver receive_buf path.
962 *
Joe Peterson62b26352009-09-09 15:03:47 -0600963 * This variant tags control characters to be echoed as "^X"
964 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
966
967static void echo_char(unsigned char c, struct tty_struct *tty)
968{
Jiri Slabybddc7152012-10-18 22:26:42 +0200969 struct n_tty_data *ldata = tty->disc_data;
970
Joe Petersona88a69c2009-01-02 13:40:53 +0000971 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200972 add_echo_byte(ECHO_OP_START, ldata);
973 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000974 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600975 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200976 add_echo_byte(ECHO_OP_START, ldata);
977 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Alan Cox17b82062008-10-13 10:45:06 +0100981/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000982 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200983 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100984 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000985
Jiri Slaby57c94122012-10-18 22:26:43 +0200986static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200988 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200989 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200990 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992}
993
994/**
995 * eraser - handle erase function
996 * @c: character input
997 * @tty: terminal device
998 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200999 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * present in the stream from the driver layer. Handles the complexities
1001 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +01001002 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001003 * n_tty_receive_buf()/producer path:
1004 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Alan Cox4edf1822008-02-08 04:18:44 -08001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static void eraser(unsigned char c, struct tty_struct *tty)
1008{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001009 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001011 size_t head;
1012 size_t cnt;
1013 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001015 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001016 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return;
1018 }
1019 if (c == ERASE_CHAR(tty))
1020 kill_type = ERASE;
1021 else if (c == WERASE_CHAR(tty))
1022 kill_type = WERASE;
1023 else {
1024 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001025 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return;
1027 }
1028 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001029 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +02001030 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 echo_char(KILL_CHAR(tty), tty);
1032 /* Add a newline if ECHOK is on and ECHOKE is off. */
1033 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001034 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return;
1036 }
1037 kill_type = KILL;
1038 }
1039
1040 seen_alnums = 0;
Tetsuo Handa947dead2018-05-26 09:53:13 +09001041 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001042 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /* erase a single possibly multibyte character */
1045 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001046 head--;
1047 c = read_buf(ldata, head);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001048 } while (is_continuation(c, tty) &&
1049 MASK(head) != MASK(ldata->canon_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* do not partially erase */
1052 if (is_continuation(c, tty))
1053 break;
1054
1055 if (kill_type == WERASE) {
1056 /* Equivalent to BSD's ALTWERASE. */
1057 if (isalnum(c) || c == '_')
1058 seen_alnums++;
1059 else if (seen_alnums)
1060 break;
1061 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001062 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001063 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (L_ECHO(tty)) {
1065 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001066 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001067 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001068 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 /* if cnt > 1, output a multi-byte character */
1071 echo_char(c, tty);
1072 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001073 head++;
1074 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001075 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1078 echo_char(ERASE_CHAR(tty), tty);
1079 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001080 unsigned int num_chars = 0;
1081 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001082 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Joe Petersona88a69c2009-01-02 13:40:53 +00001084 /*
1085 * Count the columns used for characters
1086 * since the start of input or after a
1087 * previous tab.
1088 * This info is used to go back the correct
1089 * number of columns.
1090 */
Tetsuo Handa947dead2018-05-26 09:53:13 +09001091 while (MASK(tail) != MASK(ldata->canon_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001092 tail--;
1093 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001094 if (c == '\t') {
1095 after_tab = 1;
1096 break;
Alan Cox300a6202009-01-02 13:41:04 +00001097 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001099 num_chars += 2;
1100 } else if (!is_continuation(c, tty)) {
1101 num_chars++;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001104 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001107 echo_char_raw('\b', ldata);
1108 echo_char_raw(' ', ldata);
1109 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001112 echo_char_raw('\b', ldata);
1113 echo_char_raw(' ', ldata);
1114 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 }
1117 }
1118 if (kill_type == ERASE)
1119 break;
1120 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001121 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001122 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/**
1126 * isig - handle the ISIG optio
1127 * @sig: signal
1128 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001130 * Called when a signal is being sent due to terminal input.
1131 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001132 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001133 * Performs input and output flush if !NOFLSH. In this context, the echo
1134 * buffer is 'output'. The signal is processed first to alert any current
1135 * readers or writers to discontinue and exit their i/o loops.
1136 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001137 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Alan Cox4edf1822008-02-08 04:18:44 -08001139
Peter Hurley3b19e032015-06-27 09:21:32 -04001140static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Peter Hurley8c985d12013-03-06 08:38:19 -05001142 struct pid *tty_pgrp = tty_get_pgrp(tty);
1143 if (tty_pgrp) {
1144 kill_pgrp(tty_pgrp, sig, 1);
1145 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001147}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001148
Peter Hurley3b19e032015-06-27 09:21:32 -04001149static void isig(int sig, struct tty_struct *tty)
1150{
1151 struct n_tty_data *ldata = tty->disc_data;
1152
1153 if (L_NOFLSH(tty)) {
1154 /* signal only */
1155 __isig(sig, tty);
1156
1157 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001158 up_read(&tty->termios_rwsem);
1159 down_write(&tty->termios_rwsem);
1160
Peter Hurley3b19e032015-06-27 09:21:32 -04001161 __isig(sig, tty);
1162
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001163 /* clear echo buffer */
1164 mutex_lock(&ldata->output_lock);
1165 ldata->echo_head = ldata->echo_tail = 0;
1166 ldata->echo_mark = ldata->echo_commit = 0;
1167 mutex_unlock(&ldata->output_lock);
1168
1169 /* clear output buffer */
1170 tty_driver_flush_buffer(tty);
1171
1172 /* clear input buffer */
1173 reset_buffer_flags(tty->disc_data);
1174
1175 /* notify pty master of flush */
1176 if (tty->link)
1177 n_tty_packet_mode_flush(tty);
1178
1179 up_write(&tty->termios_rwsem);
1180 down_read(&tty->termios_rwsem);
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184/**
1185 * n_tty_receive_break - handle break
1186 * @tty: terminal
1187 *
1188 * An RS232 break event has been hit in the incoming bitstream. This
1189 * can cause a variety of events depending upon the termios settings.
1190 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001191 * n_tty_receive_buf()/producer path:
1192 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001193 *
1194 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Alan Cox4edf1822008-02-08 04:18:44 -08001196
Peter Hurley4b293492013-07-24 08:29:55 -04001197static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Jiri Slaby57c94122012-10-18 22:26:43 +02001199 struct n_tty_data *ldata = tty->disc_data;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (I_IGNBRK(tty))
1202 return;
1203 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001204 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
1206 }
1207 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001208 put_tty_queue('\377', ldata);
1209 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001211 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/**
1215 * n_tty_receive_overrun - handle overrun reporting
1216 * @tty: terminal
1217 *
1218 * Data arrived faster than we could process it. While the tty
1219 * driver has flagged this the bits that were missed are gone
1220 * forever.
1221 *
1222 * Called from the receive_buf path so single threaded. Does not
1223 * need locking as num_overrun and overrun_time are function
1224 * private.
1225 */
Alan Cox4edf1822008-02-08 04:18:44 -08001226
Peter Hurley4b293492013-07-24 08:29:55 -04001227static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001229 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001231 ldata->num_overrun++;
1232 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1233 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001234 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001235 ldata->overrun_time = jiffies;
1236 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
1240/**
1241 * n_tty_receive_parity_error - error notifier
1242 * @tty: terminal device
1243 * @c: character
1244 *
1245 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001246 * the error case if necessary.
1247 *
1248 * n_tty_receive_buf()/producer path:
1249 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Peter Hurley4b293492013-07-24 08:29:55 -04001251static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Jiri Slaby57c94122012-10-18 22:26:43 +02001253 struct n_tty_data *ldata = tty->disc_data;
1254
Peter Hurley66528f92014-06-16 08:10:42 -04001255 if (I_INPCK(tty)) {
1256 if (I_IGNPAR(tty))
1257 return;
1258 if (I_PARMRK(tty)) {
1259 put_tty_queue('\377', ldata);
1260 put_tty_queue('\0', ldata);
1261 put_tty_queue(c, ldata);
1262 } else
1263 put_tty_queue('\0', ldata);
1264 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001265 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001268static void
1269n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1270{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001271 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001272 if (I_IXON(tty))
1273 start_tty(tty);
1274 if (L_ECHO(tty)) {
1275 echo_char(c, tty);
1276 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001277 } else
1278 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001279 return;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/**
1283 * n_tty_receive_char - perform processing
1284 * @tty: terminal device
1285 * @c: character
1286 *
1287 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001288 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001290 *
1291 * n_tty_receive_buf()/producer path:
1292 * caller holds non-exclusive termios_rwsem
1293 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001294 *
1295 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297
Peter Hurleye60d27c2013-07-24 08:29:56 -04001298static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001299n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001301 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (I_IXON(tty)) {
1304 if (c == START_CHAR(tty)) {
1305 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001306 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 if (c == STOP_CHAR(tty)) {
1310 stop_tty(tty);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001316 if (c == INTR_CHAR(tty)) {
1317 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001318 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001319 } else if (c == QUIT_CHAR(tty)) {
1320 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001321 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001322 } else if (c == SUSP_CHAR(tty)) {
1323 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 }
Joe Peterson575537b32008-04-30 00:53:30 -07001327
Peter Hurley855df3c2013-07-24 08:29:50 -04001328 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1329 start_tty(tty);
1330 process_echoes(tty);
1331 }
1332
Joe Peterson575537b32008-04-30 00:53:30 -07001333 if (c == '\r') {
1334 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001335 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001336 if (I_ICRNL(tty))
1337 c = '\n';
1338 } else if (c == '\n' && I_INLCR(tty))
1339 c = '\r';
1340
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001341 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1343 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1344 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001345 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001349 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001351 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001353 echo_char_raw('^', ldata);
1354 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001355 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001358 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001360 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001361 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jiri Slaby57c94122012-10-18 22:26:43 +02001363 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001365 echo_char_raw('\n', ldata);
Tetsuo Handa947dead2018-05-26 09:53:13 +09001366 while (MASK(tail) != MASK(ldata->read_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001367 echo_char(read_buf(ldata, tail), tty);
1368 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001370 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001374 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001375 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001376 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 goto handle_newline;
1379 }
1380 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 c = __DISABLED_CHAR;
1382 goto handle_newline;
1383 }
1384 if ((c == EOL_CHAR(tty)) ||
1385 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1386 /*
1387 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1388 */
1389 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001391 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001392 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001394 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 /*
1397 * XXX does PARMRK doubling happen for
1398 * EOL_CHAR and EOL2_CHAR?
1399 */
Peter Hurley001ba922013-12-02 14:24:44 -05001400 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001401 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Alan Cox4edf1822008-02-08 04:18:44 -08001403handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001404 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001405 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001406 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001408 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 }
Alan Cox4edf1822008-02-08 04:18:44 -08001412
Joe Petersonacc71bb2009-01-02 13:43:32 +00001413 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001414 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001416 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 else {
1418 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001419 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001420 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 echo_char(c, tty);
1422 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001423 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
Peter Hurley001ba922013-12-02 14:24:44 -05001426 /* PARMRK doubling check */
1427 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001428 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jiri Slaby57c94122012-10-18 22:26:43 +02001430 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001431 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Peter Hurleye60d27c2013-07-24 08:29:56 -04001434static inline void
1435n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001436{
1437 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001438
Peter Hurleye60d27c2013-07-24 08:29:56 -04001439 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1440 start_tty(tty);
1441 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001442 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001443 if (L_ECHO(tty)) {
1444 finish_erasing(ldata);
1445 /* Record the column of first canon char. */
1446 if (ldata->canon_head == ldata->read_head)
1447 echo_set_canon_col(ldata);
1448 echo_char(c, tty);
1449 commit_echoes(tty);
1450 }
Peter Hurley001ba922013-12-02 14:24:44 -05001451 /* PARMRK doubling check */
1452 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001453 put_tty_queue(c, ldata);
1454 put_tty_queue(c, ldata);
1455}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001456
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001457static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001458{
1459 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001460}
1461
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001462static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001463n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1464{
1465 struct n_tty_data *ldata = tty->disc_data;
1466
Peter Hurleye60d27c2013-07-24 08:29:56 -04001467 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1468 start_tty(tty);
1469 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001470 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001471 if (L_ECHO(tty)) {
1472 finish_erasing(ldata);
1473 /* Record the column of first canon char. */
1474 if (ldata->canon_head == ldata->read_head)
1475 echo_set_canon_col(ldata);
1476 echo_char(c, tty);
1477 commit_echoes(tty);
1478 }
1479 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001480}
1481
Peter Hurley8dc4b252013-12-02 14:24:43 -05001482static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001483{
1484 if (I_ISTRIP(tty))
1485 c &= 0x7f;
1486 if (I_IUCLC(tty) && L_IEXTEN(tty))
1487 c = tolower(c);
1488
1489 if (I_IXON(tty)) {
1490 if (c == STOP_CHAR(tty))
1491 stop_tty(tty);
1492 else if (c == START_CHAR(tty) ||
1493 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1494 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1495 c != SUSP_CHAR(tty))) {
1496 start_tty(tty);
1497 process_echoes(tty);
1498 }
1499 }
1500}
1501
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001502static void
1503n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1504{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001505 switch (flag) {
1506 case TTY_BREAK:
1507 n_tty_receive_break(tty);
1508 break;
1509 case TTY_PARITY:
1510 case TTY_FRAME:
1511 n_tty_receive_parity_error(tty, c);
1512 break;
1513 case TTY_OVERRUN:
1514 n_tty_receive_overrun(tty);
1515 break;
1516 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001517 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001518 break;
1519 }
1520}
1521
Peter Hurleye60d27c2013-07-24 08:29:56 -04001522static void
1523n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1524{
1525 struct n_tty_data *ldata = tty->disc_data;
1526
1527 ldata->lnext = 0;
1528 if (likely(flag == TTY_NORMAL)) {
1529 if (I_ISTRIP(tty))
1530 c &= 0x7f;
1531 if (I_IUCLC(tty) && L_IEXTEN(tty))
1532 c = tolower(c);
1533 n_tty_receive_char(tty, c);
1534 } else
1535 n_tty_receive_char_flagged(tty, c, flag);
1536}
1537
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001538static void
1539n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1540 char *fp, int count)
1541{
1542 struct n_tty_data *ldata = tty->disc_data;
1543 size_t n, head;
1544
1545 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001546 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001547 memcpy(read_buf_addr(ldata, head), cp, n);
1548 ldata->read_head += n;
1549 cp += n;
1550 count -= n;
1551
1552 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001553 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001554 memcpy(read_buf_addr(ldata, head), cp, n);
1555 ldata->read_head += n;
1556}
1557
Peter Hurley554117b2013-06-15 10:21:25 -04001558static void
1559n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1560 char *fp, int count)
1561{
1562 struct n_tty_data *ldata = tty->disc_data;
1563 char flag = TTY_NORMAL;
1564
1565 while (count--) {
1566 if (fp)
1567 flag = *fp++;
1568 if (likely(flag == TTY_NORMAL))
1569 put_tty_queue(*cp++, ldata);
1570 else
1571 n_tty_receive_char_flagged(tty, *cp++, flag);
1572 }
1573}
1574
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001575static void
1576n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1577 char *fp, int count)
1578{
1579 char flag = TTY_NORMAL;
1580
1581 while (count--) {
1582 if (fp)
1583 flag = *fp++;
1584 if (likely(flag == TTY_NORMAL))
1585 n_tty_receive_char_closing(tty, *cp++);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001586 }
1587}
1588
Peter Hurley7d88d632013-07-24 08:29:49 -04001589static void
1590n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001591 char *fp, int count)
1592{
1593 struct n_tty_data *ldata = tty->disc_data;
1594 char flag = TTY_NORMAL;
1595
1596 while (count--) {
1597 if (fp)
1598 flag = *fp++;
1599 if (likely(flag == TTY_NORMAL)) {
1600 unsigned char c = *cp++;
1601
1602 if (I_ISTRIP(tty))
1603 c &= 0x7f;
1604 if (I_IUCLC(tty) && L_IEXTEN(tty))
1605 c = tolower(c);
1606 if (L_EXTPROC(tty)) {
1607 put_tty_queue(c, ldata);
1608 continue;
1609 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001610 if (!test_bit(c, ldata->char_map))
1611 n_tty_receive_char_inline(tty, c);
1612 else if (n_tty_receive_char_special(tty, c) && count) {
1613 if (fp)
1614 flag = *fp++;
1615 n_tty_receive_char_lnext(tty, *cp++, flag);
1616 count--;
1617 }
Peter Hurley6baad002013-07-24 08:29:52 -04001618 } else
1619 n_tty_receive_char_flagged(tty, *cp++, flag);
1620 }
1621}
1622
1623static void
1624n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1625 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001626{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001627 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001628 char flag = TTY_NORMAL;
1629
1630 while (count--) {
1631 if (fp)
1632 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001633 if (likely(flag == TTY_NORMAL)) {
1634 unsigned char c = *cp++;
1635
1636 if (!test_bit(c, ldata->char_map))
1637 n_tty_receive_char_fast(tty, c);
1638 else if (n_tty_receive_char_special(tty, c) && count) {
1639 if (fp)
1640 flag = *fp++;
1641 n_tty_receive_char_lnext(tty, *cp++, flag);
1642 count--;
1643 }
1644 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001645 n_tty_receive_char_flagged(tty, *cp++, flag);
1646 }
1647}
1648
Peter Hurley24a89d12013-06-15 09:14:15 -04001649static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1650 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001652 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001653 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001655 if (ldata->real_raw)
1656 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001657 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001658 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001659 else if (tty->closing && !L_EXTPROC(tty))
1660 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001661 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001662 if (ldata->lnext) {
1663 char flag = TTY_NORMAL;
1664
1665 if (fp)
1666 flag = *fp++;
1667 n_tty_receive_char_lnext(tty, *cp++, flag);
1668 count--;
1669 }
1670
Peter Hurley7de971b2013-07-24 08:29:53 -04001671 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001672 n_tty_receive_buf_fast(tty, cp, fp, count);
1673 else
1674 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001675
1676 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001677 if (tty->ops->flush_chars)
1678 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Peter Hurley70aca712015-01-16 15:05:37 -05001681 if (ldata->icanon && !L_EXTPROC(tty))
1682 return;
1683
1684 /* publish read_head to consumer */
1685 smp_store_release(&ldata->commit_head, ldata->read_head);
1686
Peter Hurley33d71362016-01-09 21:45:08 -08001687 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001689 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001693/**
1694 * n_tty_receive_buf_common - process input
1695 * @tty: device to receive input
1696 * @cp: input chars
1697 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1698 * @count: number of input chars in @cp
1699 *
1700 * Called by the terminal driver when a block of characters has
1701 * been received. This function must be called from soft contexts
1702 * not from interrupt context. The driver is responsible for making
1703 * calls one at a time and in order (or using flush_to_ldisc)
1704 *
1705 * Returns the # of input chars from @cp which were processed.
1706 *
1707 * In canonical mode, the maximum line length is 4096 chars (including
1708 * the line termination char); lines longer than 4096 chars are
1709 * truncated. After 4095 chars, input data is still processed but
1710 * not stored. Overflow processing ensures the tty can always
1711 * receive more input until at least one line can be read.
1712 *
1713 * In non-canonical mode, the read buffer will only accept 4095 chars;
1714 * this provides the necessary space for a newline char if the input
1715 * mode is switched to canonical.
1716 *
1717 * Note it is possible for the read buffer to _contain_ 4096 chars
1718 * in non-canonical mode: the read buffer could already contain the
1719 * maximum canon line of 4096 chars when the mode is switched to
1720 * non-canonical.
1721 *
1722 * n_tty_receive_buf()/producer path:
1723 * claims non-exclusive termios_rwsem
1724 * publishes commit_head or canon_head
1725 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001726static int
1727n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1728 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001729{
1730 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001731 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001732
Peter Hurley9356b532013-06-15 09:14:24 -04001733 down_read(&tty->termios_rwsem);
1734
Dmitry Safonov95595c82018-11-01 00:24:48 +00001735 do {
Peter Hurley70aca712015-01-16 15:05:37 -05001736 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001737 * When PARMRK is set, each input char may take up to 3 chars
1738 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001739 *
1740 * If we are doing input canonicalization, and there are no
1741 * pending newlines, let characters through without limit, so
1742 * that erase characters will be handled. Other excess
1743 * characters will be beeped.
1744 *
1745 * paired with store in *_copy_from_read_buf() -- guarantees
1746 * the consumer has loaded the data in read_buf up to the new
1747 * read_tail (so this producer will not overwrite unread data)
1748 */
1749 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001750
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001751 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001752 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001753 room = (room + 2) / 3;
1754 room--;
1755 if (room <= 0) {
1756 overflow = ldata->icanon && ldata->canon_head == tail;
1757 if (overflow && room < 0)
1758 ldata->read_head--;
1759 room = overflow;
1760 ldata->no_room = flow && !room;
1761 } else
1762 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001763
Peter Hurley19e2ad62013-07-24 08:29:54 -04001764 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001765 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001766 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001767
1768 /* ignore parity errors if handling overflow */
1769 if (!overflow || !fp || *fp != TTY_PARITY)
1770 __receive_buf(tty, cp, fp, n);
1771
Peter Hurley19e2ad62013-07-24 08:29:54 -04001772 cp += n;
1773 if (fp)
1774 fp += n;
1775 count -= n;
1776 rcvd += n;
Dmitry Safonov95595c82018-11-01 00:24:48 +00001777 } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
Peter Hurley24a89d12013-06-15 09:14:15 -04001778
Peter Hurley19e2ad62013-07-24 08:29:54 -04001779 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001780
1781 /* Unthrottle if handling overflow on pty */
1782 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1783 if (overflow) {
1784 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1785 tty_unthrottle_safe(tty);
1786 __tty_set_flow_change(tty, 0);
1787 }
1788 } else
1789 n_tty_check_throttle(tty);
1790
Peter Hurley9356b532013-06-15 09:14:24 -04001791 up_read(&tty->termios_rwsem);
1792
Peter Hurley19e2ad62013-07-24 08:29:54 -04001793 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001794}
1795
Peter Hurley5c32d122013-12-02 14:24:41 -05001796static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1797 char *fp, int count)
1798{
1799 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1800}
1801
1802static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1803 char *fp, int count)
1804{
1805 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/**
1809 * n_tty_set_termios - termios data changed
1810 * @tty: terminal
1811 * @old: previous data
1812 *
1813 * Called by the tty layer when the user changes termios flags so
1814 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001815 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * guaranteed that this function will not be re-entered or in progress
1817 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001818 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001819 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Alan Cox4edf1822008-02-08 04:18:44 -08001821
1822static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001824 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001825
Linus Torvalds00fc57a2017-12-20 17:57:06 -08001826 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001827 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001828 ldata->line_start = ldata->read_tail;
1829 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1830 ldata->canon_head = ldata->read_tail;
1831 ldata->push = 0;
1832 } else {
1833 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1834 ldata->read_flags);
1835 ldata->canon_head = ldata->read_head;
1836 ldata->push = 1;
1837 }
Peter Hurley70aca712015-01-16 15:05:37 -05001838 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001839 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001840 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001841 }
1842
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001843 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1846 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1847 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1848 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001849 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001852 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001854 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001857 set_bit(ERASE_CHAR(tty), ldata->char_map);
1858 set_bit(KILL_CHAR(tty), ldata->char_map);
1859 set_bit(EOF_CHAR(tty), ldata->char_map);
1860 set_bit('\n', ldata->char_map);
1861 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001863 set_bit(WERASE_CHAR(tty), ldata->char_map);
1864 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1865 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (L_ECHO(tty))
1867 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001868 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 }
1871 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001872 set_bit(START_CHAR(tty), ldata->char_map);
1873 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001876 set_bit(INTR_CHAR(tty), ldata->char_map);
1877 set_bit(QUIT_CHAR(tty), ldata->char_map);
1878 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001880 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001881 ldata->raw = 0;
1882 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001884 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1886 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1887 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001888 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001890 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001892 /*
1893 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1894 * been stopped by STOP_CHAR(tty) before it.
1895 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001896 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001897 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001898 process_echoes(tty);
1899 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001900
Alan Coxf34d7a52008-04-30 00:54:13 -07001901 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001902 wake_up_interruptible(&tty->write_wait);
1903 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
1906/**
1907 * n_tty_close - close the ldisc for this tty
1908 * @tty: device
1909 *
Alan Cox4edf1822008-02-08 04:18:44 -08001910 * Called from the terminal layer when this line discipline is
1911 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * discipline change. The function will not be called while other
1913 * ldisc methods are in progress.
1914 */
Alan Cox4edf1822008-02-08 04:18:44 -08001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static void n_tty_close(struct tty_struct *tty)
1917{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001918 struct n_tty_data *ldata = tty->disc_data;
1919
Peter Hurley79901312013-03-11 16:44:23 -04001920 if (tty->link)
1921 n_tty_packet_mode_flush(tty);
1922
Peter Hurley20bafb32013-06-15 10:21:19 -04001923 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001924 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927/**
1928 * n_tty_open - open an ldisc
1929 * @tty: terminal to open
1930 *
Alan Cox4edf1822008-02-08 04:18:44 -08001931 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * terminal device. Can sleep. Called serialized so that no
1933 * other events will occur in parallel. No further open will occur
1934 * until a close.
1935 */
1936
1937static int n_tty_open(struct tty_struct *tty)
1938{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001939 struct n_tty_data *ldata;
1940
Peter Hurley20bafb32013-06-15 10:21:19 -04001941 /* Currently a malloc failure here can panic */
Tetsuo Handa9264e982018-05-26 09:53:14 +09001942 ldata = vzalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001943 if (!ldata)
Tetsuo Handa9264e982018-05-26 09:53:14 +09001944 return -ENOMEM;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001945
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001946 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001947 mutex_init(&ldata->atomic_read_lock);
1948 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001949
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001950 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001952 /* indicate buffer work may resume */
1953 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1954 n_tty_set_termios(tty, NULL);
1955 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
1957}
1958
Peter Hurleyeafbe672013-12-02 14:24:45 -05001959static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001961 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001962 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001963
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001964 if (ldata->icanon && !L_EXTPROC(tty))
1965 return ldata->canon_head != ldata->read_tail;
1966 else
Peter Hurley70aca712015-01-16 15:05:37 -05001967 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001971 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 * @tty: terminal device
1973 * @b: user data
1974 * @nr: size of data
1975 *
Alan Cox11a96d12008-10-13 10:46:24 +01001976 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 * ICANON is off; it copies characters straight from the tty queue to
1978 * user space directly. It can be profitably called twice; once to
1979 * drain the space from the tail pointer to the (physical) end of the
1980 * buffer, and once to drain the space from the (physical) beginning of
1981 * the buffer to head pointer.
1982 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001983 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001985 * n_tty_read()/consumer path:
1986 * caller holds non-exclusive termios_rwsem
1987 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
Alan Cox4edf1822008-02-08 04:18:44 -08001989
Alan Cox33f0f882006-01-09 20:54:13 -08001990static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 unsigned char __user **b,
1992 size_t *nr)
1993
1994{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001995 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int retval;
1997 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001998 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001999 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002000 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05002003 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (n) {
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -07002006 unsigned char *from = read_buf_addr(ldata, tail);
Peter Hurleye661cf72015-11-27 14:11:03 -05002007 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05002009 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08002010 tty_audit_add_data(tty, from, n);
Greg Kroah-Hartmane2e3a812018-10-04 11:06:14 -07002011 zero_buffer(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05002012 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07002013 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05002014 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2015 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02002016 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 *b += n;
2018 *nr -= n;
2019 }
2020 return retval;
2021}
2022
Peter Hurley88bb0de2013-06-15 09:14:16 -04002023/**
Peter Hurley32f13522013-06-15 09:14:17 -04002024 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04002025 * @tty: terminal device
2026 * @b: user data
2027 * @nr: size of data
2028 *
2029 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04002030 * it copies one line of input up to and including the line-delimiting
2031 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04002032 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05002033 * NB: When termios is changed from non-canonical to canonical mode and
2034 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2035 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2036 * This causes data already processed as input to be immediately available
2037 * as input although a newline has not been received.
2038 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04002039 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04002040 *
2041 * n_tty_read()/consumer path:
2042 * caller holds non-exclusive termios_rwsem
2043 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002044 */
2045
Peter Hurley32f13522013-06-15 09:14:17 -04002046static int canon_copy_from_read_buf(struct tty_struct *tty,
2047 unsigned char __user **b,
2048 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002049{
2050 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002051 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002052 size_t eol;
2053 size_t tail;
2054 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002055
2056 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002057 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002058 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002059
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002060 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2061
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002062 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002063 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2064
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002065 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002066 __func__, *nr, tail, n, size);
2067
2068 eol = find_next_bit(ldata->read_flags, size, tail);
2069 more = n - (size - tail);
2070 if (eol == N_TTY_BUF_SIZE && more) {
2071 /* scan wrapped without finding set bit */
2072 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002073 found = eol != more;
2074 } else
2075 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002076
Peter Hurleyc77569d2013-11-22 07:16:25 -05002077 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002078 if (n > N_TTY_BUF_SIZE)
2079 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002080 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002081
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002082 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2083 c = min(*nr, c);
2084 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002085 }
Peter Hurley32f13522013-06-15 09:14:17 -04002086
Peter Hurley679e7c22015-11-27 14:11:02 -05002087 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2088 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002089
Peter Hurley679e7c22015-11-27 14:11:02 -05002090 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002091 if (ret)
2092 return -EFAULT;
2093 *b += n;
2094 *nr -= n;
2095
Peter Hurleya73d3d62013-06-15 09:14:25 -04002096 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002097 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002098 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002099
Peter Hurley40d5e092013-06-15 10:21:17 -04002100 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002101 if (!ldata->push)
2102 ldata->line_start = ldata->read_tail;
2103 else
2104 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002105 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002106 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002107 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002108}
2109
Al Virocc4191d2008-03-29 03:08:48 +00002110extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002111 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113/**
2114 * job_control - check job control
2115 * @tty: tty
2116 * @file: file handle
2117 *
2118 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002119 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002121 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002122 * Locking: redirected write test is safe
2123 * current->signal->tty check is safe
2124 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
Alan Cox4edf1822008-02-08 04:18:44 -08002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127static int job_control(struct tty_struct *tty, struct file *file)
2128{
2129 /* Job control check -- must be done at start and after
2130 every sleep (POSIX.1 7.1.1.4). */
2131 /* NOTE: not yet done after every sleep pending a thorough
2132 check of the logic of this change. -- jlc */
2133 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002134 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002135 return 0;
2136
Peter Hurley2812d9e2015-10-10 20:28:42 -04002137 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
Alan Cox4edf1822008-02-08 04:18:44 -08002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141/**
Alan Cox11a96d12008-10-13 10:46:24 +01002142 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * @tty: tty device
2144 * @file: file object
2145 * @buf: userspace buffer pointer
2146 * @nr: size of I/O
2147 *
2148 * Perform reads for the line discipline. We are guaranteed that the
2149 * line discipline will not be closed under us but we may get multiple
2150 * parallel readers and must handle this ourselves. We may also get
2151 * a hangup. Always called in user context, may sleep.
2152 *
2153 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002154 *
2155 * n_tty_read()/consumer path:
2156 * claims non-exclusive termios_rwsem
2157 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 */
Alan Cox4edf1822008-02-08 04:18:44 -08002159
Alan Cox11a96d12008-10-13 10:46:24 +01002160static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 unsigned char __user *buf, size_t nr)
2162{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002163 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002165 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002166 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int minimum, time;
2168 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002170 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002171 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002174 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002176
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002177 /*
2178 * Internal serialization of reads.
2179 */
2180 if (file->f_flags & O_NONBLOCK) {
2181 if (!mutex_trylock(&ldata->atomic_read_lock))
2182 return -EAGAIN;
2183 } else {
2184 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2185 return -ERESTARTSYS;
2186 }
2187
Peter Hurley9356b532013-06-15 09:14:24 -04002188 down_read(&tty->termios_rwsem);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 minimum = time = 0;
2191 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002192 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 minimum = MIN_CHAR(tty);
2194 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002195 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002197 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002198 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 }
2201
Alan Cox04f378b2008-04-30 00:53:29 -07002202 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002203 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 while (nr) {
2207 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002208 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 unsigned char cs;
2210 if (b != buf)
2211 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002212 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 cs = tty->link->ctrl_status;
2214 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002215 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002216 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 break;
2219 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002220 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 nr--;
2222 break;
2223 }
Alan Cox4edf1822008-02-08 04:18:44 -08002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!input_available_p(tty, 0)) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002226 up_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002227 tty_buffer_flush_work(tty->port);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002228 down_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002229 if (!input_available_p(tty, 0)) {
2230 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2231 retval = -EIO;
2232 break;
2233 }
2234 if (tty_hung_up_p(file))
2235 break;
Tejun Heoa7e19062018-02-13 07:38:08 -08002236 /*
2237 * Abort readers for ttys which never actually
2238 * get hung up. See __tty_hangup().
2239 */
2240 if (test_bit(TTY_HUPPING, &tty->flags))
2241 break;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002242 if (!timeout)
2243 break;
Dmitry Safonov95595c82018-11-01 00:24:48 +00002244 if (tty_io_nonblock(tty, file)) {
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002245 retval = -EAGAIN;
2246 break;
2247 }
2248 if (signal_pending(current)) {
2249 retval = -ERESTARTSYS;
2250 break;
2251 }
2252 up_read(&tty->termios_rwsem);
2253
2254 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2255 timeout);
2256
2257 down_read(&tty->termios_rwsem);
2258 continue;
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002262 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002263 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002264 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 } else {
2267 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002268
2269 /* Deal with packet mode. */
2270 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002271 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002272 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002273 break;
2274 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002275 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002276 nr--;
2277 }
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 uncopied = copy_from_read_buf(tty, &b, &nr);
2280 uncopied += copy_from_read_buf(tty, &b, &nr);
2281 if (uncopied) {
2282 retval = -EFAULT;
2283 break;
2284 }
2285 }
2286
Peter Hurley6367ca72013-06-15 09:14:33 -04002287 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (b - buf >= minimum)
2290 break;
2291 if (time)
2292 timeout = time;
2293 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002294 if (tail != ldata->read_tail)
2295 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002296 up_read(&tty->termios_rwsem);
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002299 mutex_unlock(&ldata->atomic_read_lock);
2300
Peter Hurley40d5e092013-06-15 10:21:17 -04002301 if (b - buf)
2302 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 return retval;
2305}
2306
2307/**
Alan Cox11a96d12008-10-13 10:46:24 +01002308 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 * @tty: tty device
2310 * @file: file object
2311 * @buf: userspace buffer pointer
2312 * @nr: size of I/O
2313 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002314 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002316 * and other such events. Since the receive code will echo characters,
2317 * thus calling driver write methods, the output_lock is used in
2318 * the output processing functions called here as well as in the
2319 * echo processing function to protect the column state and space
2320 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 *
2322 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002323 *
2324 * Locking: output_lock to protect column state and space left
2325 * (note that the process_output*() functions take this
2326 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
Alan Cox4edf1822008-02-08 04:18:44 -08002328
Alan Cox11a96d12008-10-13 10:46:24 +01002329static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002330 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002333 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int c;
2335 ssize_t retval = 0;
2336
2337 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2338 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2339 retval = tty_check_change(tty);
2340 if (retval)
2341 return retval;
2342 }
2343
Peter Hurley9356b532013-06-15 09:14:24 -04002344 down_read(&tty->termios_rwsem);
2345
Joe Petersona88a69c2009-01-02 13:40:53 +00002346 /* Write out any echoed characters that are still pending */
2347 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 add_wait_queue(&tty->write_wait, &wait);
2350 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (signal_pending(current)) {
2352 retval = -ERESTARTSYS;
2353 break;
2354 }
2355 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2356 retval = -EIO;
2357 break;
2358 }
Peter Hurley582f5592013-05-17 12:49:48 -04002359 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002361 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (num < 0) {
2363 if (num == -EAGAIN)
2364 break;
2365 retval = num;
2366 goto break_out;
2367 }
2368 b += num;
2369 nr -= num;
2370 if (nr == 0)
2371 break;
2372 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002373 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 break;
2375 b++; nr--;
2376 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002377 if (tty->ops->flush_chars)
2378 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002380 struct n_tty_data *ldata = tty->disc_data;
2381
Roman Zippeld6afe272005-07-07 17:56:55 -07002382 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002383 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002384 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002385 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002386 if (c < 0) {
2387 retval = c;
2388 goto break_out;
2389 }
2390 if (!c)
2391 break;
2392 b += c;
2393 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 if (!nr)
2397 break;
Dmitry Safonov95595c82018-11-01 00:24:48 +00002398 if (tty_io_nonblock(tty, file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 retval = -EAGAIN;
2400 break;
2401 }
Peter Hurley9356b532013-06-15 09:14:24 -04002402 up_read(&tty->termios_rwsem);
2403
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002404 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002405
2406 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 remove_wait_queue(&tty->write_wait, &wait);
Peter Hurley87108bc2016-01-09 21:45:14 -08002410 if (nr && tty->fasync)
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002411 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002412 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return (b - buf) ? b - buf : retval;
2414}
2415
2416/**
Alan Cox11a96d12008-10-13 10:46:24 +01002417 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 * @tty: terminal device
2419 * @file: file accessing it
2420 * @wait: poll table
2421 *
2422 * Called when the line discipline is asked to poll() for data or
2423 * for special events. This code is not serialized with respect to
2424 * other events save open/close.
2425 *
2426 * This code must be sure never to sleep through a hangup.
2427 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 */
Alan Cox4edf1822008-02-08 04:18:44 -08002429
Alan Cox11a96d12008-10-13 10:46:24 +01002430static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002431 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 unsigned int mask = 0;
2434
2435 poll_wait(file, &tty->read_wait, wait);
2436 poll_wait(file, &tty->write_wait, wait);
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002437 if (input_available_p(tty, 1))
2438 mask |= POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002439 else {
2440 tty_buffer_flush_work(tty->port);
2441 if (input_available_p(tty, 1))
2442 mask |= POLLIN | POLLRDNORM;
2443 }
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002444 if (tty->packet && tty->link->ctrl_status)
2445 mask |= POLLPRI | POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002446 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2447 mask |= POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (tty_hung_up_p(file))
2449 mask |= POLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002450 if (tty->ops->write && !tty_is_writelocked(tty) &&
2451 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2452 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 mask |= POLLOUT | POLLWRNORM;
2454 return mask;
2455}
2456
Jiri Slaby57c94122012-10-18 22:26:43 +02002457static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002458{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002459 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002460
Peter Hurleya73d3d62013-06-15 09:14:25 -04002461 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002462 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002463 head = ldata->canon_head;
2464 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002465 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002466 /* Skip EOF-chars.. */
Tetsuo Handa947dead2018-05-26 09:53:13 +09002467 while (MASK(head) != MASK(tail)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002468 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2469 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002470 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002471 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002472 }
2473 return nr;
2474}
2475
2476static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2477 unsigned int cmd, unsigned long arg)
2478{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002479 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002480 int retval;
2481
2482 switch (cmd) {
2483 case TIOCOUTQ:
2484 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2485 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002486 down_write(&tty->termios_rwsem);
Linus Torvalds00fc57a2017-12-20 17:57:06 -08002487 if (L_ICANON(tty) && !L_EXTPROC(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002488 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002489 else
2490 retval = read_cnt(ldata);
2491 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002492 return put_user(retval, (unsigned int __user *) arg);
2493 default:
2494 return n_tty_ioctl_helper(tty, file, cmd, arg);
2495 }
2496}
2497
Peter Hurley27228732016-01-09 21:35:19 -08002498static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002499 .magic = TTY_LDISC_MAGIC,
2500 .name = "n_tty",
2501 .open = n_tty_open,
2502 .close = n_tty_close,
2503 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002504 .read = n_tty_read,
2505 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002506 .ioctl = n_tty_ioctl,
2507 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002508 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002509 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002510 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002511 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002513
2514/**
2515 * n_tty_inherit_ops - inherit N_TTY methods
2516 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2517 *
Peter Hurley27228732016-01-09 21:35:19 -08002518 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002519 */
2520
2521void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2522{
Peter Hurley27228732016-01-09 21:35:19 -08002523 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002524 ops->owner = NULL;
2525 ops->refcount = ops->flags = 0;
2526}
2527EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002528
2529void __init n_tty_init(void)
2530{
2531 tty_register_ldisc(N_TTY, &n_tty_ops);
2532}