blob: 43174220170924e094567ad6271703bd881e2a6f [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-1.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This code used to be in tty_io.c, but things are getting hairy
6 * enough that it made sense to split things off. (The N_TTY
7 * processing has changed so much that it's hardly recognizable,
8 * anyway...)
9 *
10 * Note that the open routine for N_TTY is guaranteed never to return
11 * an error. This is because Linux will fall back to setting a line
Alan Cox4edf1822008-02-08 04:18:44 -080012 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * This file also contains code originally written by Linus Torvalds,
17 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Reduced memory usage for older ARM systems - Russell King.
20 *
Alan Cox4edf1822008-02-08 04:18:44 -080021 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
23 * who actually finally proved there really was a race.
24 *
25 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
26 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010027 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * EAGAIN
29 */
30
31#include <linux/types.h>
32#include <linux/major.h>
33#include <linux/errno.h>
34#include <linux/signal.h>
35#include <linux/fcntl.h>
36#include <linux/sched.h>
37#include <linux/interrupt.h>
38#include <linux/tty.h>
39#include <linux/timer.h>
40#include <linux/ctype.h>
41#include <linux/mm.h>
42#include <linux/string.h>
43#include <linux/slab.h>
44#include <linux/poll.h>
45#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070046#include <linux/audit.h>
47#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000048#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080049#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050050#include <linux/ratelimit.h>
Peter Hurley86e35ae2013-07-24 09:30:05 -040051#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* number of characters left in xmit buffer before select has we have room */
55#define WAKEUP_CHARS 256
56
57/*
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
61 */
62#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010063#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Joe Petersona88a69c2009-01-02 13:40:53 +000065/*
66 * Special byte codes used in the echo buffer to represent operations
67 * or special handling of characters. Bytes in the echo buffer that
68 * are not part of such special blocks are treated as normal character
69 * codes.
70 */
71#define ECHO_OP_START 0xff
72#define ECHO_OP_MOVE_BACK_COL 0x80
73#define ECHO_OP_SET_CANON_COL 0x81
74#define ECHO_OP_ERASE_TAB 0x82
75
Peter Hurleycbfd0342013-06-15 10:04:26 -040076#define ECHO_COMMIT_WATERMARK 256
77#define ECHO_BLOCK 256
78#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
79
80
Peter Hurley32f13522013-06-15 09:14:17 -040081#undef N_TTY_TRACE
82#ifdef N_TTY_TRACE
83# define n_tty_trace(f, args...) trace_printk(f, ##args)
84#else
85# define n_tty_trace(f, args...)
86#endif
87
Jiri Slaby70ece7a2012-10-18 22:26:38 +020088struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040089 /* producer-published */
90 size_t read_head;
Peter Hurley70aca712015-01-16 15:05:37 -050091 size_t commit_head;
Peter Hurleyfb7aa032013-06-15 09:14:30 -040092 size_t canon_head;
Peter Hurley9dfd16d2013-06-15 10:04:29 -040093 size_t echo_head;
94 size_t echo_commit;
Peter Hurley1075a6e2013-12-09 18:06:07 -050095 size_t echo_mark;
Peter Hurley1bb9d562013-06-15 10:21:20 -040096 DECLARE_BITMAP(char_map, 256);
Peter Hurleyfb7aa032013-06-15 09:14:30 -040097
98 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +020099 unsigned long overrun_time;
100 int num_overrun;
101
Peter Hurley24a89d12013-06-15 09:14:15 -0400102 /* non-atomic */
103 bool no_room;
104
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400105 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200106 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
Peter Hurley4d0ed182013-12-10 17:12:02 -0500107 unsigned char push:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200108
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400109 /* shared by producer and consumer */
Peter Hurley20bafb32013-06-15 10:21:19 -0400110 char read_buf[N_TTY_BUF_SIZE];
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200111 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Peter Hurley20bafb32013-06-15 10:21:19 -0400112 unsigned char echo_buf[N_TTY_BUF_SIZE];
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200113
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400114 /* consumer-published */
115 size_t read_tail;
Peter Hurley40d5e092013-06-15 10:21:17 -0400116 size_t line_start;
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400117
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400118 /* protected by output lock */
119 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200120 unsigned int canon_column;
Peter Hurley9dfd16d2013-06-15 10:04:29 -0400121 size_t echo_tail;
Jiri Slabybddc7152012-10-18 22:26:42 +0200122
123 struct mutex atomic_read_lock;
124 struct mutex output_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200125};
126
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +0900127#define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
128
Peter Hurleyce741172013-06-15 09:14:20 -0400129static inline size_t read_cnt(struct n_tty_data *ldata)
130{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400131 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400132}
133
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400134static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
135{
136 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
137}
138
139static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
140{
141 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
142}
143
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400144static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
145{
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900146 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400147 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
148}
149
150static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
151{
152 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
153}
154
Peter Hurley679e7c22015-11-27 14:11:02 -0500155static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
156 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700157{
158 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500159 size_t size = N_TTY_BUF_SIZE - tail;
160 const void *from = read_buf_addr(ldata, tail);
161 int uncopied;
162
163 if (n > size) {
Peter Hurley309426a2016-01-09 22:55:27 -0800164 tty_audit_add_data(tty, from, size);
Peter Hurley679e7c22015-11-27 14:11:02 -0500165 uncopied = copy_to_user(to, from, size);
166 if (uncopied)
167 return uncopied;
168 to += size;
169 n -= size;
170 from = ldata->read_buf;
171 }
Laura Abbott72586c62015-05-14 11:42:17 -0700172
Peter Hurley309426a2016-01-09 22:55:27 -0800173 tty_audit_add_data(tty, from, n);
Laura Abbott72586c62015-05-14 11:42:17 -0700174 return copy_to_user(to, from, n);
175}
176
Peter Hurley24a89d12013-06-15 09:14:15 -0400177/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500178 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400179 * @tty: terminal
180 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500181 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400182 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400183 * Caller holds exclusive termios_rwsem
184 * or
185 * n_tty_read()/consumer path:
186 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400187 */
188
Peter Hurley2c5dc462015-01-16 15:05:34 -0500189static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400190{
Peter Hurley24a89d12013-06-15 09:14:15 -0400191 struct n_tty_data *ldata = tty->disc_data;
192
Peter Hurley2c5dc462015-01-16 15:05:34 -0500193 /* Did the input worker stop? Restart it */
194 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400195 ldata->no_room = 0;
196
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200197 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400198 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400199 /* see if ldisc has been killed - if so, this means that
200 * even though the ldisc has been halted and ->buf.work
201 * cancelled, ->buf.work is about to be rescheduled
202 */
203 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
204 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400205 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200206 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900207}
208
Peter Hurley9a4aec22013-06-15 09:14:32 -0400209static ssize_t chars_in_buffer(struct tty_struct *tty)
210{
211 struct n_tty_data *ldata = tty->disc_data;
212 ssize_t n = 0;
213
214 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500215 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400216 else
217 n = ldata->canon_head - ldata->read_tail;
218 return n;
219}
220
Peter Hurleyee0bab82013-06-15 09:14:34 -0400221/**
222 * n_tty_write_wakeup - asynchronous I/O notifier
223 * @tty: tty device
224 *
225 * Required for the ptys, serial driver etc. since processes
226 * that attach themselves to the master and rely on ASYNC
227 * IO must be woken up
228 */
229
230static void n_tty_write_wakeup(struct tty_struct *tty)
231{
Peter Hurley7bccc362016-01-09 21:45:12 -0800232 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
233 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Peter Hurleyee0bab82013-06-15 09:14:34 -0400234}
235
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400236static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400237{
Peter Hurleya3428462015-01-16 15:05:35 -0500238 struct n_tty_data *ldata = tty->disc_data;
239
Peter Hurley6367ca72013-06-15 09:14:33 -0400240 /*
241 * Check the remaining room for the input canonicalization
242 * mode. We don't want to throttle the driver if we're in
243 * canonical mode and don't have a newline yet!
244 */
Peter Hurleya3428462015-01-16 15:05:35 -0500245 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
246 return;
247
Peter Hurley6367ca72013-06-15 09:14:33 -0400248 while (1) {
249 int throttled;
250 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500251 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400252 break;
253 throttled = tty_throttle_safe(tty);
254 if (!throttled)
255 break;
256 }
257 __tty_set_flow_change(tty, 0);
258}
259
Peter Hurley4b293492013-07-24 08:29:55 -0400260static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400261{
Peter Hurley6d27a632016-01-10 22:40:56 -0800262 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400263 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
264 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500265 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800266 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400267 return;
268 }
269
Peter Hurley6367ca72013-06-15 09:14:33 -0400270 /* If there is enough space in the read buffer now, let the
271 * low-level driver know. We use chars_in_buffer() to
272 * check the buffer, as it now knows about canonical mode.
273 * Otherwise, if the driver is throttled and the line is
274 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
275 * we won't get any more characters.
276 */
277
278 while (1) {
279 int unthrottled;
280 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
281 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
282 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500283 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400284 unthrottled = tty_unthrottle_safe(tty);
285 if (!unthrottled)
286 break;
287 }
288 __tty_set_flow_change(tty, 0);
289}
290
Alan Cox17b82062008-10-13 10:45:06 +0100291/**
292 * put_tty_queue - add character to tty
293 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200294 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100295 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400296 * Add a character to the tty read_buf queue.
297 *
298 * n_tty_receive_buf()/producer path:
299 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100300 */
301
Peter Hurley19e2ad62013-07-24 08:29:54 -0400302static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100304 *read_buf_addr(ldata, ldata->read_head) = c;
305 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * reset_buffer_flags - reset buffer state
310 * @tty: terminal to reset
311 *
Peter Hurley25518c62013-03-11 16:44:31 -0400312 * Reset the read buffer counters and clear the flags.
313 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100314 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400315 * Locking: caller holds exclusive termios_rwsem
316 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000318
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400319static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400321 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500322 ldata->commit_head = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400323 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000324
Peter Hurleya73d3d62013-06-15 09:14:25 -0400325 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200326 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500327 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Peter Hurleya30737a2013-03-11 16:44:22 -0400330static void n_tty_packet_mode_flush(struct tty_struct *tty)
331{
332 unsigned long flags;
333
Peter Hurleya30737a2013-03-11 16:44:22 -0400334 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400335 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400336 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400337 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000338 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400339 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/**
343 * n_tty_flush_buffer - clean input queue
344 * @tty: terminal device
345 *
Peter Hurley25518c62013-03-11 16:44:31 -0400346 * Flush the input buffer. Called when the tty layer wants the
347 * buffer flushed (eg at hangup) or when the N_TTY line discipline
348 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400350 * Holds termios_rwsem to exclude producer/consumer while
351 * buffer indices are reset.
352 *
353 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Alan Cox4edf1822008-02-08 04:18:44 -0800355
356static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400358 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400359 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500360 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800361
Peter Hurleya30737a2013-03-11 16:44:22 -0400362 if (tty->link)
363 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400364 up_write(&tty->termios_rwsem);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * is_utf8_continuation - utf8 multibyte check
369 * @c: byte to check
370 *
371 * Returns true if the utf8 character 'c' is a multibyte continuation
372 * character. We use this to correctly compute the on screen size
373 * of the character when printing
374 */
Alan Cox4edf1822008-02-08 04:18:44 -0800375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static inline int is_utf8_continuation(unsigned char c)
377{
378 return (c & 0xc0) == 0x80;
379}
380
381/**
382 * is_continuation - multibyte check
383 * @c: byte to check
384 *
385 * Returns true if the utf8 character 'c' is a multibyte continuation
386 * character and the terminal is in unicode mode.
387 */
Alan Cox4edf1822008-02-08 04:18:44 -0800388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static inline int is_continuation(unsigned char c, struct tty_struct *tty)
390{
391 return I_IUTF8(tty) && is_utf8_continuation(c);
392}
393
394/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000395 * do_output_char - output one character
396 * @c: character (or partial unicode symbol)
397 * @tty: terminal device
398 * @space: space available in tty driver write buffer
399 *
400 * This is a helper function that handles one output character
401 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600402 * doing OPOST processing and putting the results in the
403 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000404 *
405 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
406 * and NLDLY. They simply aren't relevant in the world today.
407 * If you ever need them, add them here.
408 *
409 * Returns the number of bytes of buffer space used or -1 if
410 * no space left.
411 *
412 * Locking: should be called under the output_lock to protect
413 * the column state and space left in the buffer
414 */
415
416static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
417{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200418 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000419 int spaces;
420
421 if (!space)
422 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000423
Joe Petersona88a69c2009-01-02 13:40:53 +0000424 switch (c) {
425 case '\n':
426 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200427 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000428 if (O_ONLCR(tty)) {
429 if (space < 2)
430 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200431 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700432 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000433 return 2;
434 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200435 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000436 break;
437 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200438 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000439 return 0;
440 if (O_OCRNL(tty)) {
441 c = '\n';
442 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200443 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000444 break;
445 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200446 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000447 break;
448 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200449 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000450 if (O_TABDLY(tty) == XTABS) {
451 if (space < spaces)
452 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200453 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000454 tty->ops->write(tty, " ", spaces);
455 return spaces;
456 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200457 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000458 break;
459 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200460 if (ldata->column > 0)
461 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000462 break;
463 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000464 if (!iscntrl(c)) {
465 if (O_OLCUC(tty))
466 c = toupper(c);
467 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200468 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000469 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000470 break;
471 }
472
473 tty_put_char(tty, c);
474 return 1;
475}
476
477/**
478 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * @c: character (or partial unicode symbol)
480 * @tty: terminal device
481 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600482 * Output one character with OPOST processing.
483 * Returns -1 when the output device is full and the character
484 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000486 * Locking: output_lock to protect column state and space left
487 * (also, this is called from n_tty_write under the
488 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
Alan Cox4edf1822008-02-08 04:18:44 -0800490
Joe Petersona88a69c2009-01-02 13:40:53 +0000491static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jiri Slabybddc7152012-10-18 22:26:42 +0200493 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000494 int space, retval;
495
Jiri Slabybddc7152012-10-18 22:26:42 +0200496 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Alan Coxf34d7a52008-04-30 00:54:13 -0700498 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000499 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jiri Slabybddc7152012-10-18 22:26:42 +0200501 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000502 if (retval < 0)
503 return -1;
504 else
505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000509 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600511 * @buf: character buffer
512 * @nr: number of bytes to output
513 *
514 * Output a block of characters with OPOST processing.
515 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
517 * This path is used to speed up block console writes, among other
518 * things when processing blocks of output data. It handles only
519 * the simple cases normally found and helps to generate blocks of
520 * symbols for the console driver and thus improve performance.
521 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000522 * Locking: output_lock to protect column state and space left
523 * (also, this is called from n_tty_write under the
524 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Alan Cox4edf1822008-02-08 04:18:44 -0800526
Joe Petersona88a69c2009-01-02 13:40:53 +0000527static ssize_t process_output_block(struct tty_struct *tty,
528 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200530 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100532 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 const unsigned char *cp;
534
Jiri Slabybddc7152012-10-18 22:26:42 +0200535 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000536
Alan Coxf34d7a52008-04-30 00:54:13 -0700537 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000538 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200539 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (nr > space)
543 nr = space;
544
545 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000546 unsigned char c = *cp;
547
548 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 case '\n':
550 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200551 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (O_ONLCR(tty))
553 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200554 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200557 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto break_out;
559 if (O_OCRNL(tty))
560 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200561 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 case '\t':
564 goto break_out;
565 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200566 if (ldata->column > 0)
567 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000570 if (!iscntrl(c)) {
571 if (O_OLCUC(tty))
572 goto break_out;
573 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200574 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 }
578 }
579break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700580 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000581
Jiri Slabybddc7152012-10-18 22:26:42 +0200582 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return i;
584}
585
Joe Petersona88a69c2009-01-02 13:40:53 +0000586/**
587 * process_echoes - write pending echo characters
588 * @tty: terminal device
589 *
590 * Write previously buffered echo (and other ldisc-generated)
591 * characters to the tty.
592 *
593 * Characters generated by the ldisc (including echoes) need to
594 * be buffered because the driver's write buffer can fill during
595 * heavy program output. Echoing straight to the driver will
596 * often fail under these conditions, causing lost characters and
597 * resulting mismatches of ldisc state information.
598 *
599 * Since the ldisc state must represent the characters actually sent
600 * to the driver at the time of the write, operations like certain
601 * changes in column state are also saved in the buffer and executed
602 * here.
603 *
604 * A circular fifo buffer is used so that the most recent characters
605 * are prioritized. Also, when control characters are echoed with a
606 * prefixed "^", the pair is treated atomically and thus not separated.
607 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400608 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 */
610
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400611static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000612{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200613 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400614 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400615 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000616 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000617
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400618 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000619
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400620 tail = ldata->echo_tail;
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900621 while (MASK(ldata->echo_commit) != MASK(tail)) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400622 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000623 if (c == ECHO_OP_START) {
624 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000625 int no_space_left = 0;
626
627 /*
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900628 * Since add_echo_byte() is called without holding
629 * output_lock, we might see only portion of multi-byte
630 * operation.
631 */
632 if (MASK(ldata->echo_commit) == MASK(tail + 1))
633 goto not_yet_stored;
634 /*
Joe Petersona88a69c2009-01-02 13:40:53 +0000635 * If the buffer byte is the start of a multi-byte
636 * operation, get the next byte, which is either the
637 * op code or a control character value.
638 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400639 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000640
Joe Petersona88a69c2009-01-02 13:40:53 +0000641 switch (op) {
642 unsigned int num_chars, num_bs;
643
644 case ECHO_OP_ERASE_TAB:
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900645 if (MASK(ldata->echo_commit) == MASK(tail + 2))
646 goto not_yet_stored;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400647 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000648
649 /*
650 * Determine how many columns to go back
651 * in order to erase the tab.
652 * This depends on the number of columns
653 * used by other characters within the tab
654 * area. If this (modulo 8) count is from
655 * the start of input rather than from a
656 * previous tab, we offset by canon column.
657 * Otherwise, tab spacing is normal.
658 */
659 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200660 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000661 num_bs = 8 - (num_chars & 7);
662
663 if (num_bs > space) {
664 no_space_left = 1;
665 break;
666 }
667 space -= num_bs;
668 while (num_bs--) {
669 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200670 if (ldata->column > 0)
671 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000672 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400673 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000674 break;
675
676 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200677 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400678 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000679 break;
680
681 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200682 if (ldata->column > 0)
683 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400684 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000685 break;
686
687 case ECHO_OP_START:
688 /* This is an escaped echo op start code */
689 if (!space) {
690 no_space_left = 1;
691 break;
692 }
693 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200694 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000695 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400696 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000697 break;
698
699 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600701 * If the op is not a special byte code,
702 * it is a ctrl char tagged to be echoed
703 * as "^X" (where X is the letter
704 * representing the control char).
705 * Note that we must ensure there is
706 * enough space for the whole ctrl pair.
707 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000708 */
Joe Peterson62b26352009-09-09 15:03:47 -0600709 if (space < 2) {
710 no_space_left = 1;
711 break;
712 }
713 tty_put_char(tty, '^');
714 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200715 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600716 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400717 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000718 }
719
720 if (no_space_left)
721 break;
722 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400723 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600724 int retval = do_output_char(c, tty, space);
725 if (retval < 0)
726 break;
727 space -= retval;
728 } else {
729 if (!space)
730 break;
731 tty_put_char(tty, c);
732 space -= 1;
733 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400734 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000735 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000736 }
737
Peter Hurleycbfd0342013-06-15 10:04:26 -0400738 /* If the echo buffer is nearly full (so that the possibility exists
739 * of echo overrun before the next commit), then discard enough
740 * data at the tail to prevent a subsequent overrun */
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900741 while (ldata->echo_commit > tail &&
742 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200743 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500744 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400745 tail += 3;
746 else
747 tail += 2;
748 } else
749 tail++;
750 }
751
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900752 not_yet_stored:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400753 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400754 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Peter Hurley17bd7902013-06-15 10:04:24 -0400757static void commit_echoes(struct tty_struct *tty)
758{
759 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400760 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400761 size_t head;
762
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900763 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400764 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500765 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400766 old = ldata->echo_commit - ldata->echo_tail;
767
768 /* Process committed echoes if the accumulated # of bytes
769 * is over the threshold (and try again each time another
770 * block is accumulated) */
771 nr = head - ldata->echo_tail;
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900772 if (nr < ECHO_COMMIT_WATERMARK ||
773 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
774 mutex_unlock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400775 return;
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900776 }
Peter Hurley17bd7902013-06-15 10:04:24 -0400777
Peter Hurleycbfd0342013-06-15 10:04:26 -0400778 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400779 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400780 mutex_unlock(&ldata->output_lock);
781
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400782 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400783 tty->ops->flush_chars(tty);
784}
785
786static void process_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 echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400790
Peter Hurleye2613be2014-02-11 16:34:55 -0500791 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400792 return;
793
794 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500795 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400796 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400797 mutex_unlock(&ldata->output_lock);
798
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400799 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400800 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400801}
802
Peter Hurley1075a6e2013-12-09 18:06:07 -0500803/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400804static void flush_echoes(struct tty_struct *tty)
805{
806 struct n_tty_data *ldata = tty->disc_data;
807
Peter Hurley39434ab2013-11-29 12:56:10 -0500808 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
809 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400810 return;
811
812 mutex_lock(&ldata->output_lock);
813 ldata->echo_commit = ldata->echo_head;
814 __process_echoes(tty);
815 mutex_unlock(&ldata->output_lock);
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000819 * add_echo_byte - add a byte to the echo buffer
820 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200821 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000822 *
823 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000824 */
825
Peter Hurleycbfd0342013-06-15 10:04:26 -0400826static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000827{
Tetsuo Handaebec3f82018-05-26 09:53:14 +0900828 *echo_buf_addr(ldata, ldata->echo_head) = c;
829 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
830 ldata->echo_head++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000831}
832
833/**
834 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200835 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 *
837 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 */
839
Jiri Slaby57c94122012-10-18 22:26:43 +0200840static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000841{
Jiri Slaby57c94122012-10-18 22:26:43 +0200842 add_echo_byte(ECHO_OP_START, ldata);
843 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000844}
845
846/**
847 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200848 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 *
850 * Add an operation to the echo buffer to set the canon column
851 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 */
853
Jiri Slaby57c94122012-10-18 22:26:43 +0200854static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000855{
Jiri Slaby57c94122012-10-18 22:26:43 +0200856 add_echo_byte(ECHO_OP_START, ldata);
857 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858}
859
860/**
861 * echo_erase_tab - add operation to erase a tab
862 * @num_chars: number of character columns already used
863 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000865 *
866 * Add an operation to the echo buffer to erase a tab.
867 *
868 * Called by the eraser function, which knows how many character
869 * columns have been used since either a previous tab or the start
870 * of input. This information will be used later, along with
871 * canon column (if applicable), to go back the correct number
872 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000873 */
874
875static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200876 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000877{
Jiri Slaby57c94122012-10-18 22:26:43 +0200878 add_echo_byte(ECHO_OP_START, ldata);
879 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000880
881 /* We only need to know this modulo 8 (tab spacing) */
882 num_chars &= 7;
883
884 /* Set the high bit as a flag if num_chars is after a previous tab */
885 if (after_tab)
886 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000887
Jiri Slaby57c94122012-10-18 22:26:43 +0200888 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000889}
890
891/**
892 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * @c: unicode byte to echo
894 * @tty: terminal device
895 *
Alan Cox4edf1822008-02-08 04:18:44 -0800896 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100898 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000899 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000900 */
901
Jiri Slaby57c94122012-10-18 22:26:43 +0200902static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000903{
Joe Petersona88a69c2009-01-02 13:40:53 +0000904 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200905 add_echo_byte(ECHO_OP_START, ldata);
906 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000907 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200908 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000909 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000910}
911
912/**
913 * echo_char - echo a character
914 * @c: unicode byte to echo
915 * @tty: terminal device
916 *
917 * Echo user input back onto the screen. This must be called only when
918 * L_ECHO(tty) is true. Called from the driver receive_buf path.
919 *
Joe Peterson62b26352009-09-09 15:03:47 -0600920 * This variant tags control characters to be echoed as "^X"
921 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923
924static void echo_char(unsigned char c, struct tty_struct *tty)
925{
Jiri Slabybddc7152012-10-18 22:26:42 +0200926 struct n_tty_data *ldata = tty->disc_data;
927
Joe Petersona88a69c2009-01-02 13:40:53 +0000928 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200929 add_echo_byte(ECHO_OP_START, ldata);
930 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000931 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600932 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200933 add_echo_byte(ECHO_OP_START, ldata);
934 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Alan Cox17b82062008-10-13 10:45:06 +0100938/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000939 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200940 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100941 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000942
Jiri Slaby57c94122012-10-18 22:26:43 +0200943static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200945 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200946 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200947 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949}
950
951/**
952 * eraser - handle erase function
953 * @c: character input
954 * @tty: terminal device
955 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200956 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * present in the stream from the driver layer. Handles the complexities
958 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100959 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400960 * n_tty_receive_buf()/producer path:
961 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
Alan Cox4edf1822008-02-08 04:18:44 -0800963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static void eraser(unsigned char c, struct tty_struct *tty)
965{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200966 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400968 size_t head;
969 size_t cnt;
970 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200972 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000973 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975 }
976 if (c == ERASE_CHAR(tty))
977 kill_type = ERASE;
978 else if (c == WERASE_CHAR(tty))
979 kill_type = WERASE;
980 else {
981 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200982 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return;
984 }
985 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200986 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200987 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 echo_char(KILL_CHAR(tty), tty);
989 /* Add a newline if ECHOK is on and ECHOKE is off. */
990 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200991 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return;
993 }
994 kill_type = KILL;
995 }
996
997 seen_alnums = 0;
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +0900998 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200999 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* erase a single possibly multibyte character */
1002 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001003 head--;
1004 c = read_buf(ldata, head);
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +09001005 } while (is_continuation(c, tty) &&
1006 MASK(head) != MASK(ldata->canon_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* do not partially erase */
1009 if (is_continuation(c, tty))
1010 break;
1011
1012 if (kill_type == WERASE) {
1013 /* Equivalent to BSD's ALTWERASE. */
1014 if (isalnum(c) || c == '_')
1015 seen_alnums++;
1016 else if (seen_alnums)
1017 break;
1018 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001019 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001020 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (L_ECHO(tty)) {
1022 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001023 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001024 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001025 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 /* if cnt > 1, output a multi-byte character */
1028 echo_char(c, tty);
1029 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001030 head++;
1031 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001032 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1035 echo_char(ERASE_CHAR(tty), tty);
1036 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001037 unsigned int num_chars = 0;
1038 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001039 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Joe Petersona88a69c2009-01-02 13:40:53 +00001041 /*
1042 * Count the columns used for characters
1043 * since the start of input or after a
1044 * previous tab.
1045 * This info is used to go back the correct
1046 * number of columns.
1047 */
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +09001048 while (MASK(tail) != MASK(ldata->canon_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001049 tail--;
1050 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001051 if (c == '\t') {
1052 after_tab = 1;
1053 break;
Alan Cox300a6202009-01-02 13:41:04 +00001054 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001056 num_chars += 2;
1057 } else if (!is_continuation(c, tty)) {
1058 num_chars++;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001061 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else {
1063 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001064 echo_char_raw('\b', ldata);
1065 echo_char_raw(' ', ldata);
1066 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001069 echo_char_raw('\b', ldata);
1070 echo_char_raw(' ', ldata);
1071 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 }
1074 }
1075 if (kill_type == ERASE)
1076 break;
1077 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001078 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001079 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/**
1083 * isig - handle the ISIG optio
1084 * @sig: signal
1085 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001087 * Called when a signal is being sent due to terminal input.
1088 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001089 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001090 * Performs input and output flush if !NOFLSH. In this context, the echo
1091 * buffer is 'output'. The signal is processed first to alert any current
1092 * readers or writers to discontinue and exit their i/o loops.
1093 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001094 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Alan Cox4edf1822008-02-08 04:18:44 -08001096
Peter Hurley3b19e032015-06-27 09:21:32 -04001097static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Peter Hurley8c985d12013-03-06 08:38:19 -05001099 struct pid *tty_pgrp = tty_get_pgrp(tty);
1100 if (tty_pgrp) {
1101 kill_pgrp(tty_pgrp, sig, 1);
1102 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001104}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001105
Peter Hurley3b19e032015-06-27 09:21:32 -04001106static void isig(int sig, struct tty_struct *tty)
1107{
1108 struct n_tty_data *ldata = tty->disc_data;
1109
1110 if (L_NOFLSH(tty)) {
1111 /* signal only */
1112 __isig(sig, tty);
1113
1114 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001115 up_read(&tty->termios_rwsem);
1116 down_write(&tty->termios_rwsem);
1117
Peter Hurley3b19e032015-06-27 09:21:32 -04001118 __isig(sig, tty);
1119
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001120 /* clear echo buffer */
1121 mutex_lock(&ldata->output_lock);
1122 ldata->echo_head = ldata->echo_tail = 0;
1123 ldata->echo_mark = ldata->echo_commit = 0;
1124 mutex_unlock(&ldata->output_lock);
1125
1126 /* clear output buffer */
1127 tty_driver_flush_buffer(tty);
1128
1129 /* clear input buffer */
1130 reset_buffer_flags(tty->disc_data);
1131
1132 /* notify pty master of flush */
1133 if (tty->link)
1134 n_tty_packet_mode_flush(tty);
1135
1136 up_write(&tty->termios_rwsem);
1137 down_read(&tty->termios_rwsem);
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/**
1142 * n_tty_receive_break - handle break
1143 * @tty: terminal
1144 *
1145 * An RS232 break event has been hit in the incoming bitstream. This
1146 * can cause a variety of events depending upon the termios settings.
1147 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001148 * n_tty_receive_buf()/producer path:
1149 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001150 *
1151 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
Alan Cox4edf1822008-02-08 04:18:44 -08001153
Peter Hurley4b293492013-07-24 08:29:55 -04001154static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Jiri Slaby57c94122012-10-18 22:26:43 +02001156 struct n_tty_data *ldata = tty->disc_data;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (I_IGNBRK(tty))
1159 return;
1160 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001161 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return;
1163 }
1164 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001165 put_tty_queue('\377', ldata);
1166 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001168 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/**
1172 * n_tty_receive_overrun - handle overrun reporting
1173 * @tty: terminal
1174 *
1175 * Data arrived faster than we could process it. While the tty
1176 * driver has flagged this the bits that were missed are gone
1177 * forever.
1178 *
1179 * Called from the receive_buf path so single threaded. Does not
1180 * need locking as num_overrun and overrun_time are function
1181 * private.
1182 */
Alan Cox4edf1822008-02-08 04:18:44 -08001183
Peter Hurley4b293492013-07-24 08:29:55 -04001184static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001186 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001188 ldata->num_overrun++;
1189 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1190 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001191 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001192 ldata->overrun_time = jiffies;
1193 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195}
1196
1197/**
1198 * n_tty_receive_parity_error - error notifier
1199 * @tty: terminal device
1200 * @c: character
1201 *
1202 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001203 * the error case if necessary.
1204 *
1205 * n_tty_receive_buf()/producer path:
1206 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Peter Hurley4b293492013-07-24 08:29:55 -04001208static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Jiri Slaby57c94122012-10-18 22:26:43 +02001210 struct n_tty_data *ldata = tty->disc_data;
1211
Peter Hurley66528f92014-06-16 08:10:42 -04001212 if (I_INPCK(tty)) {
1213 if (I_IGNPAR(tty))
1214 return;
1215 if (I_PARMRK(tty)) {
1216 put_tty_queue('\377', ldata);
1217 put_tty_queue('\0', ldata);
1218 put_tty_queue(c, ldata);
1219 } else
1220 put_tty_queue('\0', ldata);
1221 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001222 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001225static void
1226n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1227{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001228 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001229 if (I_IXON(tty))
1230 start_tty(tty);
1231 if (L_ECHO(tty)) {
1232 echo_char(c, tty);
1233 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001234 } else
1235 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001236 return;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/**
1240 * n_tty_receive_char - perform processing
1241 * @tty: terminal device
1242 * @c: character
1243 *
1244 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001245 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001247 *
1248 * n_tty_receive_buf()/producer path:
1249 * caller holds non-exclusive termios_rwsem
1250 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001251 *
1252 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254
Peter Hurleye60d27c2013-07-24 08:29:56 -04001255static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001256n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001258 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (I_IXON(tty)) {
1261 if (c == START_CHAR(tty)) {
1262 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001263 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 if (c == STOP_CHAR(tty)) {
1267 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 }
Joe Peterson575537b32008-04-30 00:53:30 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001273 if (c == INTR_CHAR(tty)) {
1274 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001275 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001276 } else if (c == QUIT_CHAR(tty)) {
1277 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001278 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001279 } else if (c == SUSP_CHAR(tty)) {
1280 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 }
Joe Peterson575537b32008-04-30 00:53:30 -07001284
Peter Hurley855df3c2013-07-24 08:29:50 -04001285 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1286 start_tty(tty);
1287 process_echoes(tty);
1288 }
1289
Joe Peterson575537b32008-04-30 00:53:30 -07001290 if (c == '\r') {
1291 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001292 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001293 if (I_ICRNL(tty))
1294 c = '\n';
1295 } else if (c == '\n' && I_INLCR(tty))
1296 c = '\r';
1297
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001298 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1300 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1301 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001302 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001306 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001308 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001310 echo_char_raw('^', ldata);
1311 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001312 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001315 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001317 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001318 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Jiri Slaby57c94122012-10-18 22:26:43 +02001320 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001322 echo_char_raw('\n', ldata);
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +09001323 while (MASK(tail) != MASK(ldata->read_head)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001324 echo_char(read_buf(ldata, tail), tty);
1325 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001327 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001331 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001332 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001333 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 goto handle_newline;
1336 }
1337 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 c = __DISABLED_CHAR;
1339 goto handle_newline;
1340 }
1341 if ((c == EOL_CHAR(tty)) ||
1342 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1343 /*
1344 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1345 */
1346 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001348 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001349 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001351 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 /*
1354 * XXX does PARMRK doubling happen for
1355 * EOL_CHAR and EOL2_CHAR?
1356 */
Peter Hurley001ba922013-12-02 14:24:44 -05001357 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001358 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Alan Cox4edf1822008-02-08 04:18:44 -08001360handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001361 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001362 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001363 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001365 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
Alan Cox4edf1822008-02-08 04:18:44 -08001369
Joe Petersonacc71bb2009-01-02 13:43:32 +00001370 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001371 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 else {
1375 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001376 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001377 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 echo_char(c, tty);
1379 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001380 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Peter Hurley001ba922013-12-02 14:24:44 -05001383 /* PARMRK doubling check */
1384 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001385 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jiri Slaby57c94122012-10-18 22:26:43 +02001387 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001388 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Peter Hurleye60d27c2013-07-24 08:29:56 -04001391static inline void
1392n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001393{
1394 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001395
Peter Hurleye60d27c2013-07-24 08:29:56 -04001396 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1397 start_tty(tty);
1398 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001399 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001400 if (L_ECHO(tty)) {
1401 finish_erasing(ldata);
1402 /* Record the column of first canon char. */
1403 if (ldata->canon_head == ldata->read_head)
1404 echo_set_canon_col(ldata);
1405 echo_char(c, tty);
1406 commit_echoes(tty);
1407 }
Peter Hurley001ba922013-12-02 14:24:44 -05001408 /* PARMRK doubling check */
1409 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001410 put_tty_queue(c, ldata);
1411 put_tty_queue(c, ldata);
1412}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001413
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001414static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001415{
1416 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001417}
1418
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001419static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001420n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1421{
1422 struct n_tty_data *ldata = tty->disc_data;
1423
Peter Hurleye60d27c2013-07-24 08:29:56 -04001424 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1425 start_tty(tty);
1426 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001427 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001428 if (L_ECHO(tty)) {
1429 finish_erasing(ldata);
1430 /* Record the column of first canon char. */
1431 if (ldata->canon_head == ldata->read_head)
1432 echo_set_canon_col(ldata);
1433 echo_char(c, tty);
1434 commit_echoes(tty);
1435 }
1436 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001437}
1438
Peter Hurley8dc4b252013-12-02 14:24:43 -05001439static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001440{
1441 if (I_ISTRIP(tty))
1442 c &= 0x7f;
1443 if (I_IUCLC(tty) && L_IEXTEN(tty))
1444 c = tolower(c);
1445
1446 if (I_IXON(tty)) {
1447 if (c == STOP_CHAR(tty))
1448 stop_tty(tty);
1449 else if (c == START_CHAR(tty) ||
1450 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1451 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1452 c != SUSP_CHAR(tty))) {
1453 start_tty(tty);
1454 process_echoes(tty);
1455 }
1456 }
1457}
1458
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001459static void
1460n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1461{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001462 switch (flag) {
1463 case TTY_BREAK:
1464 n_tty_receive_break(tty);
1465 break;
1466 case TTY_PARITY:
1467 case TTY_FRAME:
1468 n_tty_receive_parity_error(tty, c);
1469 break;
1470 case TTY_OVERRUN:
1471 n_tty_receive_overrun(tty);
1472 break;
1473 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001474 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001475 break;
1476 }
1477}
1478
Peter Hurleye60d27c2013-07-24 08:29:56 -04001479static void
1480n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1481{
1482 struct n_tty_data *ldata = tty->disc_data;
1483
1484 ldata->lnext = 0;
1485 if (likely(flag == TTY_NORMAL)) {
1486 if (I_ISTRIP(tty))
1487 c &= 0x7f;
1488 if (I_IUCLC(tty) && L_IEXTEN(tty))
1489 c = tolower(c);
1490 n_tty_receive_char(tty, c);
1491 } else
1492 n_tty_receive_char_flagged(tty, c, flag);
1493}
1494
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001495static void
1496n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1497 char *fp, int count)
1498{
1499 struct n_tty_data *ldata = tty->disc_data;
1500 size_t n, head;
1501
1502 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001503 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001504 memcpy(read_buf_addr(ldata, head), cp, n);
1505 ldata->read_head += n;
1506 cp += n;
1507 count -= n;
1508
1509 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001510 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001511 memcpy(read_buf_addr(ldata, head), cp, n);
1512 ldata->read_head += n;
1513}
1514
Peter Hurley554117b2013-06-15 10:21:25 -04001515static void
1516n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1517 char *fp, int count)
1518{
1519 struct n_tty_data *ldata = tty->disc_data;
1520 char flag = TTY_NORMAL;
1521
1522 while (count--) {
1523 if (fp)
1524 flag = *fp++;
1525 if (likely(flag == TTY_NORMAL))
1526 put_tty_queue(*cp++, ldata);
1527 else
1528 n_tty_receive_char_flagged(tty, *cp++, flag);
1529 }
1530}
1531
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001532static void
1533n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1534 char *fp, int count)
1535{
1536 char flag = TTY_NORMAL;
1537
1538 while (count--) {
1539 if (fp)
1540 flag = *fp++;
1541 if (likely(flag == TTY_NORMAL))
1542 n_tty_receive_char_closing(tty, *cp++);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001543 }
1544}
1545
Peter Hurley7d88d632013-07-24 08:29:49 -04001546static void
1547n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001548 char *fp, int count)
1549{
1550 struct n_tty_data *ldata = tty->disc_data;
1551 char flag = TTY_NORMAL;
1552
1553 while (count--) {
1554 if (fp)
1555 flag = *fp++;
1556 if (likely(flag == TTY_NORMAL)) {
1557 unsigned char c = *cp++;
1558
1559 if (I_ISTRIP(tty))
1560 c &= 0x7f;
1561 if (I_IUCLC(tty) && L_IEXTEN(tty))
1562 c = tolower(c);
1563 if (L_EXTPROC(tty)) {
1564 put_tty_queue(c, ldata);
1565 continue;
1566 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001567 if (!test_bit(c, ldata->char_map))
1568 n_tty_receive_char_inline(tty, c);
1569 else if (n_tty_receive_char_special(tty, c) && count) {
1570 if (fp)
1571 flag = *fp++;
1572 n_tty_receive_char_lnext(tty, *cp++, flag);
1573 count--;
1574 }
Peter Hurley6baad002013-07-24 08:29:52 -04001575 } else
1576 n_tty_receive_char_flagged(tty, *cp++, flag);
1577 }
1578}
1579
1580static void
1581n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1582 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001583{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001584 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001585 char flag = TTY_NORMAL;
1586
1587 while (count--) {
1588 if (fp)
1589 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001590 if (likely(flag == TTY_NORMAL)) {
1591 unsigned char c = *cp++;
1592
1593 if (!test_bit(c, ldata->char_map))
1594 n_tty_receive_char_fast(tty, c);
1595 else if (n_tty_receive_char_special(tty, c) && count) {
1596 if (fp)
1597 flag = *fp++;
1598 n_tty_receive_char_lnext(tty, *cp++, flag);
1599 count--;
1600 }
1601 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001602 n_tty_receive_char_flagged(tty, *cp++, flag);
1603 }
1604}
1605
Peter Hurley24a89d12013-06-15 09:14:15 -04001606static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1607 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001609 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001610 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001612 if (ldata->real_raw)
1613 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001614 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001615 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001616 else if (tty->closing && !L_EXTPROC(tty))
1617 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001618 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001619 if (ldata->lnext) {
1620 char flag = TTY_NORMAL;
1621
1622 if (fp)
1623 flag = *fp++;
1624 n_tty_receive_char_lnext(tty, *cp++, flag);
1625 count--;
1626 }
1627
Peter Hurley7de971b2013-07-24 08:29:53 -04001628 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001629 n_tty_receive_buf_fast(tty, cp, fp, count);
1630 else
1631 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001632
1633 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001634 if (tty->ops->flush_chars)
1635 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
Peter Hurley70aca712015-01-16 15:05:37 -05001638 if (ldata->icanon && !L_EXTPROC(tty))
1639 return;
1640
1641 /* publish read_head to consumer */
1642 smp_store_release(&ldata->commit_head, ldata->read_head);
1643
Peter Hurley33d71362016-01-09 21:45:08 -08001644 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001646 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001650/**
1651 * n_tty_receive_buf_common - process input
1652 * @tty: device to receive input
1653 * @cp: input chars
1654 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1655 * @count: number of input chars in @cp
1656 *
1657 * Called by the terminal driver when a block of characters has
1658 * been received. This function must be called from soft contexts
1659 * not from interrupt context. The driver is responsible for making
1660 * calls one at a time and in order (or using flush_to_ldisc)
1661 *
1662 * Returns the # of input chars from @cp which were processed.
1663 *
1664 * In canonical mode, the maximum line length is 4096 chars (including
1665 * the line termination char); lines longer than 4096 chars are
1666 * truncated. After 4095 chars, input data is still processed but
1667 * not stored. Overflow processing ensures the tty can always
1668 * receive more input until at least one line can be read.
1669 *
1670 * In non-canonical mode, the read buffer will only accept 4095 chars;
1671 * this provides the necessary space for a newline char if the input
1672 * mode is switched to canonical.
1673 *
1674 * Note it is possible for the read buffer to _contain_ 4096 chars
1675 * in non-canonical mode: the read buffer could already contain the
1676 * maximum canon line of 4096 chars when the mode is switched to
1677 * non-canonical.
1678 *
1679 * n_tty_receive_buf()/producer path:
1680 * claims non-exclusive termios_rwsem
1681 * publishes commit_head or canon_head
1682 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001683static int
1684n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1685 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001686{
1687 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001688 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001689
Peter Hurley9356b532013-06-15 09:14:24 -04001690 down_read(&tty->termios_rwsem);
1691
Peter Hurley19e2ad62013-07-24 08:29:54 -04001692 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001693 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001694 * When PARMRK is set, each input char may take up to 3 chars
1695 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001696 *
1697 * If we are doing input canonicalization, and there are no
1698 * pending newlines, let characters through without limit, so
1699 * that erase characters will be handled. Other excess
1700 * characters will be beeped.
1701 *
1702 * paired with store in *_copy_from_read_buf() -- guarantees
1703 * the consumer has loaded the data in read_buf up to the new
1704 * read_tail (so this producer will not overwrite unread data)
1705 */
1706 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001707
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001708 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001709 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001710 room = (room + 2) / 3;
1711 room--;
1712 if (room <= 0) {
1713 overflow = ldata->icanon && ldata->canon_head == tail;
1714 if (overflow && room < 0)
1715 ldata->read_head--;
1716 room = overflow;
1717 ldata->no_room = flow && !room;
1718 } else
1719 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001720
Peter Hurley19e2ad62013-07-24 08:29:54 -04001721 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001722 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001723 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001724
1725 /* ignore parity errors if handling overflow */
1726 if (!overflow || !fp || *fp != TTY_PARITY)
1727 __receive_buf(tty, cp, fp, n);
1728
Peter Hurley19e2ad62013-07-24 08:29:54 -04001729 cp += n;
1730 if (fp)
1731 fp += n;
1732 count -= n;
1733 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001734 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001735
Peter Hurley19e2ad62013-07-24 08:29:54 -04001736 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001737
1738 /* Unthrottle if handling overflow on pty */
1739 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1740 if (overflow) {
1741 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1742 tty_unthrottle_safe(tty);
1743 __tty_set_flow_change(tty, 0);
1744 }
1745 } else
1746 n_tty_check_throttle(tty);
1747
Peter Hurley9356b532013-06-15 09:14:24 -04001748 up_read(&tty->termios_rwsem);
1749
Peter Hurley19e2ad62013-07-24 08:29:54 -04001750 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001751}
1752
Peter Hurley5c32d122013-12-02 14:24:41 -05001753static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1754 char *fp, int count)
1755{
1756 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1757}
1758
1759static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1760 char *fp, int count)
1761{
1762 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/**
1766 * n_tty_set_termios - termios data changed
1767 * @tty: terminal
1768 * @old: previous data
1769 *
1770 * Called by the tty layer when the user changes termios flags so
1771 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001772 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 * guaranteed that this function will not be re-entered or in progress
1774 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001775 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001776 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Alan Cox4edf1822008-02-08 04:18:44 -08001778
1779static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001781 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001782
Linus Torvalds966031f2017-12-20 17:57:06 -08001783 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001784 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001785 ldata->line_start = ldata->read_tail;
1786 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1787 ldata->canon_head = ldata->read_tail;
1788 ldata->push = 0;
1789 } else {
1790 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1791 ldata->read_flags);
1792 ldata->canon_head = ldata->read_head;
1793 ldata->push = 1;
1794 }
Peter Hurley70aca712015-01-16 15:05:37 -05001795 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001796 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001797 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001798 }
1799
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001800 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1803 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1804 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1805 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001806 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001809 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001811 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001814 set_bit(ERASE_CHAR(tty), ldata->char_map);
1815 set_bit(KILL_CHAR(tty), ldata->char_map);
1816 set_bit(EOF_CHAR(tty), ldata->char_map);
1817 set_bit('\n', ldata->char_map);
1818 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001820 set_bit(WERASE_CHAR(tty), ldata->char_map);
1821 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1822 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (L_ECHO(tty))
1824 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001825 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 }
1828 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001829 set_bit(START_CHAR(tty), ldata->char_map);
1830 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001833 set_bit(INTR_CHAR(tty), ldata->char_map);
1834 set_bit(QUIT_CHAR(tty), ldata->char_map);
1835 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001837 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001838 ldata->raw = 0;
1839 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001841 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1843 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1844 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001845 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001847 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001849 /*
1850 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1851 * been stopped by STOP_CHAR(tty) before it.
1852 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001853 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001854 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001855 process_echoes(tty);
1856 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001857
Alan Coxf34d7a52008-04-30 00:54:13 -07001858 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001859 wake_up_interruptible(&tty->write_wait);
1860 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863/**
1864 * n_tty_close - close the ldisc for this tty
1865 * @tty: device
1866 *
Alan Cox4edf1822008-02-08 04:18:44 -08001867 * Called from the terminal layer when this line discipline is
1868 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * discipline change. The function will not be called while other
1870 * ldisc methods are in progress.
1871 */
Alan Cox4edf1822008-02-08 04:18:44 -08001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873static void n_tty_close(struct tty_struct *tty)
1874{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001875 struct n_tty_data *ldata = tty->disc_data;
1876
Peter Hurley79901312013-03-11 16:44:23 -04001877 if (tty->link)
1878 n_tty_packet_mode_flush(tty);
1879
Peter Hurley20bafb32013-06-15 10:21:19 -04001880 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001881 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884/**
1885 * n_tty_open - open an ldisc
1886 * @tty: terminal to open
1887 *
Alan Cox4edf1822008-02-08 04:18:44 -08001888 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * terminal device. Can sleep. Called serialized so that no
1890 * other events will occur in parallel. No further open will occur
1891 * until a close.
1892 */
1893
1894static int n_tty_open(struct tty_struct *tty)
1895{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001896 struct n_tty_data *ldata;
1897
Peter Hurley20bafb32013-06-15 10:21:19 -04001898 /* Currently a malloc failure here can panic */
Tetsuo Handaebec3f82018-05-26 09:53:14 +09001899 ldata = vzalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001900 if (!ldata)
Tetsuo Handaebec3f82018-05-26 09:53:14 +09001901 return -ENOMEM;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001902
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001903 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001904 mutex_init(&ldata->atomic_read_lock);
1905 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001906
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001907 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001909 /* indicate buffer work may resume */
1910 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1911 n_tty_set_termios(tty, NULL);
1912 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return 0;
1914}
1915
Peter Hurleyeafbe672013-12-02 14:24:45 -05001916static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001918 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001919 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001920
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001921 if (ldata->icanon && !L_EXTPROC(tty))
1922 return ldata->canon_head != ldata->read_tail;
1923 else
Peter Hurley70aca712015-01-16 15:05:37 -05001924 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001928 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * @tty: terminal device
1930 * @b: user data
1931 * @nr: size of data
1932 *
Alan Cox11a96d12008-10-13 10:46:24 +01001933 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * ICANON is off; it copies characters straight from the tty queue to
1935 * user space directly. It can be profitably called twice; once to
1936 * drain the space from the tail pointer to the (physical) end of the
1937 * buffer, and once to drain the space from the (physical) beginning of
1938 * the buffer to head pointer.
1939 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001940 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001942 * n_tty_read()/consumer path:
1943 * caller holds non-exclusive termios_rwsem
1944 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
Alan Cox4edf1822008-02-08 04:18:44 -08001946
Alan Cox33f0f882006-01-09 20:54:13 -08001947static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 unsigned char __user **b,
1949 size_t *nr)
1950
1951{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001952 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int retval;
1954 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001955 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001956 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001957 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001960 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001963 const unsigned char *from = read_buf_addr(ldata, tail);
1964 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001966 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08001967 tty_audit_add_data(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05001968 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001969 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05001970 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1971 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001972 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *b += n;
1974 *nr -= n;
1975 }
1976 return retval;
1977}
1978
Peter Hurley88bb0de2013-06-15 09:14:16 -04001979/**
Peter Hurley32f13522013-06-15 09:14:17 -04001980 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001981 * @tty: terminal device
1982 * @b: user data
1983 * @nr: size of data
1984 *
1985 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001986 * it copies one line of input up to and including the line-delimiting
1987 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001988 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05001989 * NB: When termios is changed from non-canonical to canonical mode and
1990 * the read buffer contains data, n_tty_set_termios() simulates an EOF
1991 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
1992 * This causes data already processed as input to be immediately available
1993 * as input although a newline has not been received.
1994 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04001995 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04001996 *
1997 * n_tty_read()/consumer path:
1998 * caller holds non-exclusive termios_rwsem
1999 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002000 */
2001
Peter Hurley32f13522013-06-15 09:14:17 -04002002static int canon_copy_from_read_buf(struct tty_struct *tty,
2003 unsigned char __user **b,
2004 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002005{
2006 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002007 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002008 size_t eol;
2009 size_t tail;
2010 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002011
2012 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002013 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002014 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002015
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002016 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2017
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002018 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002019 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2020
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002021 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002022 __func__, *nr, tail, n, size);
2023
2024 eol = find_next_bit(ldata->read_flags, size, tail);
2025 more = n - (size - tail);
2026 if (eol == N_TTY_BUF_SIZE && more) {
2027 /* scan wrapped without finding set bit */
2028 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002029 found = eol != more;
2030 } else
2031 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002032
Peter Hurleyc77569d2013-11-22 07:16:25 -05002033 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002034 if (n > N_TTY_BUF_SIZE)
2035 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002036 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002037
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002038 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2039 c = min(*nr, c);
2040 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002041 }
Peter Hurley32f13522013-06-15 09:14:17 -04002042
Peter Hurley679e7c22015-11-27 14:11:02 -05002043 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2044 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002045
Peter Hurley679e7c22015-11-27 14:11:02 -05002046 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002047 if (ret)
2048 return -EFAULT;
2049 *b += n;
2050 *nr -= n;
2051
Peter Hurleya73d3d62013-06-15 09:14:25 -04002052 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002053 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002054 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002055
Peter Hurley40d5e092013-06-15 10:21:17 -04002056 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002057 if (!ldata->push)
2058 ldata->line_start = ldata->read_tail;
2059 else
2060 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002061 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002062 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002063 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002064}
2065
Al Virocc4191d2008-03-29 03:08:48 +00002066extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002067 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069/**
2070 * job_control - check job control
2071 * @tty: tty
2072 * @file: file handle
2073 *
2074 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002075 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002077 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002078 * Locking: redirected write test is safe
2079 * current->signal->tty check is safe
2080 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
Alan Cox4edf1822008-02-08 04:18:44 -08002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static int job_control(struct tty_struct *tty, struct file *file)
2084{
2085 /* Job control check -- must be done at start and after
2086 every sleep (POSIX.1 7.1.1.4). */
2087 /* NOTE: not yet done after every sleep pending a thorough
2088 check of the logic of this change. -- jlc */
2089 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002090 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002091 return 0;
2092
Peter Hurley2812d9e2015-10-10 20:28:42 -04002093 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
Alan Cox4edf1822008-02-08 04:18:44 -08002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097/**
Alan Cox11a96d12008-10-13 10:46:24 +01002098 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * @tty: tty device
2100 * @file: file object
2101 * @buf: userspace buffer pointer
2102 * @nr: size of I/O
2103 *
2104 * Perform reads for the line discipline. We are guaranteed that the
2105 * line discipline will not be closed under us but we may get multiple
2106 * parallel readers and must handle this ourselves. We may also get
2107 * a hangup. Always called in user context, may sleep.
2108 *
2109 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002110 *
2111 * n_tty_read()/consumer path:
2112 * claims non-exclusive termios_rwsem
2113 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Alan Cox4edf1822008-02-08 04:18:44 -08002115
Alan Cox11a96d12008-10-13 10:46:24 +01002116static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned char __user *buf, size_t nr)
2118{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002119 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002121 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002122 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int minimum, time;
2124 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002126 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002127 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002130 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002132
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002133 /*
2134 * Internal serialization of reads.
2135 */
2136 if (file->f_flags & O_NONBLOCK) {
2137 if (!mutex_trylock(&ldata->atomic_read_lock))
2138 return -EAGAIN;
2139 } else {
2140 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2141 return -ERESTARTSYS;
2142 }
2143
Peter Hurley9356b532013-06-15 09:14:24 -04002144 down_read(&tty->termios_rwsem);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 minimum = time = 0;
2147 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002148 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 minimum = MIN_CHAR(tty);
2150 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002151 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002153 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002154 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 }
2157
Alan Cox04f378b2008-04-30 00:53:29 -07002158 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002159 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 while (nr) {
2163 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002164 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 unsigned char cs;
2166 if (b != buf)
2167 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002168 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 cs = tty->link->ctrl_status;
2170 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002171 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002172 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002176 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 nr--;
2178 break;
2179 }
Alan Cox4edf1822008-02-08 04:18:44 -08002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!input_available_p(tty, 0)) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002182 up_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002183 tty_buffer_flush_work(tty->port);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002184 down_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002185 if (!input_available_p(tty, 0)) {
2186 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2187 retval = -EIO;
2188 break;
2189 }
2190 if (tty_hung_up_p(file))
2191 break;
Tejun Heo28b0f8a2018-02-13 07:38:08 -08002192 /*
2193 * Abort readers for ttys which never actually
2194 * get hung up. See __tty_hangup().
2195 */
2196 if (test_bit(TTY_HUPPING, &tty->flags))
2197 break;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002198 if (!timeout)
2199 break;
2200 if (file->f_flags & O_NONBLOCK) {
2201 retval = -EAGAIN;
2202 break;
2203 }
2204 if (signal_pending(current)) {
2205 retval = -ERESTARTSYS;
2206 break;
2207 }
2208 up_read(&tty->termios_rwsem);
2209
2210 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2211 timeout);
2212
2213 down_read(&tty->termios_rwsem);
2214 continue;
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002218 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002219 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002220 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 break;
2222 } else {
2223 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002224
2225 /* Deal with packet mode. */
2226 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002227 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002228 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002229 break;
2230 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002231 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002232 nr--;
2233 }
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 uncopied = copy_from_read_buf(tty, &b, &nr);
2236 uncopied += copy_from_read_buf(tty, &b, &nr);
2237 if (uncopied) {
2238 retval = -EFAULT;
2239 break;
2240 }
2241 }
2242
Peter Hurley6367ca72013-06-15 09:14:33 -04002243 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 if (b - buf >= minimum)
2246 break;
2247 if (time)
2248 timeout = time;
2249 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002250 if (tail != ldata->read_tail)
2251 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002252 up_read(&tty->termios_rwsem);
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002255 mutex_unlock(&ldata->atomic_read_lock);
2256
Peter Hurley40d5e092013-06-15 10:21:17 -04002257 if (b - buf)
2258 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 return retval;
2261}
2262
2263/**
Alan Cox11a96d12008-10-13 10:46:24 +01002264 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * @tty: tty device
2266 * @file: file object
2267 * @buf: userspace buffer pointer
2268 * @nr: size of I/O
2269 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002270 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002272 * and other such events. Since the receive code will echo characters,
2273 * thus calling driver write methods, the output_lock is used in
2274 * the output processing functions called here as well as in the
2275 * echo processing function to protect the column state and space
2276 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 *
2278 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002279 *
2280 * Locking: output_lock to protect column state and space left
2281 * (note that the process_output*() functions take this
2282 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Alan Cox4edf1822008-02-08 04:18:44 -08002284
Alan Cox11a96d12008-10-13 10:46:24 +01002285static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002286 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002289 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int c;
2291 ssize_t retval = 0;
2292
2293 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2294 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2295 retval = tty_check_change(tty);
2296 if (retval)
2297 return retval;
2298 }
2299
Peter Hurley9356b532013-06-15 09:14:24 -04002300 down_read(&tty->termios_rwsem);
2301
Joe Petersona88a69c2009-01-02 13:40:53 +00002302 /* Write out any echoed characters that are still pending */
2303 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 add_wait_queue(&tty->write_wait, &wait);
2306 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (signal_pending(current)) {
2308 retval = -ERESTARTSYS;
2309 break;
2310 }
2311 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2312 retval = -EIO;
2313 break;
2314 }
Peter Hurley582f5592013-05-17 12:49:48 -04002315 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002317 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (num < 0) {
2319 if (num == -EAGAIN)
2320 break;
2321 retval = num;
2322 goto break_out;
2323 }
2324 b += num;
2325 nr -= num;
2326 if (nr == 0)
2327 break;
2328 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002329 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 break;
2331 b++; nr--;
2332 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002333 if (tty->ops->flush_chars)
2334 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002336 struct n_tty_data *ldata = tty->disc_data;
2337
Roman Zippeld6afe272005-07-07 17:56:55 -07002338 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002339 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002340 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002341 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002342 if (c < 0) {
2343 retval = c;
2344 goto break_out;
2345 }
2346 if (!c)
2347 break;
2348 b += c;
2349 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352 if (!nr)
2353 break;
2354 if (file->f_flags & O_NONBLOCK) {
2355 retval = -EAGAIN;
2356 break;
2357 }
Peter Hurley9356b532013-06-15 09:14:24 -04002358 up_read(&tty->termios_rwsem);
2359
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002360 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002361
2362 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 remove_wait_queue(&tty->write_wait, &wait);
Peter Hurley87108bc2016-01-09 21:45:14 -08002366 if (nr && tty->fasync)
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002367 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002368 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return (b - buf) ? b - buf : retval;
2370}
2371
2372/**
Alan Cox11a96d12008-10-13 10:46:24 +01002373 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 * @tty: terminal device
2375 * @file: file accessing it
2376 * @wait: poll table
2377 *
2378 * Called when the line discipline is asked to poll() for data or
2379 * for special events. This code is not serialized with respect to
2380 * other events save open/close.
2381 *
2382 * This code must be sure never to sleep through a hangup.
2383 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 */
Alan Cox4edf1822008-02-08 04:18:44 -08002385
Al Viroafc9a422017-07-03 06:39:46 -04002386static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002387 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Al Viroafc9a422017-07-03 06:39:46 -04002389 __poll_t mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 poll_wait(file, &tty->read_wait, wait);
2392 poll_wait(file, &tty->write_wait, wait);
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002393 if (input_available_p(tty, 1))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002394 mask |= EPOLLIN | EPOLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002395 else {
2396 tty_buffer_flush_work(tty->port);
2397 if (input_available_p(tty, 1))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002398 mask |= EPOLLIN | EPOLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002399 }
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002400 if (tty->packet && tty->link->ctrl_status)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002401 mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002402 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002403 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (tty_hung_up_p(file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002405 mask |= EPOLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002406 if (tty->ops->write && !tty_is_writelocked(tty) &&
2407 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2408 tty_write_room(tty) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002409 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return mask;
2411}
2412
Jiri Slaby57c94122012-10-18 22:26:43 +02002413static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002414{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002415 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002416
Peter Hurleya73d3d62013-06-15 09:14:25 -04002417 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002418 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002419 head = ldata->canon_head;
2420 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002421 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002422 /* Skip EOF-chars.. */
Tetsuo Handa3d63b7e2018-05-26 09:53:13 +09002423 while (MASK(head) != MASK(tail)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002424 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2425 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002426 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002427 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002428 }
2429 return nr;
2430}
2431
2432static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2433 unsigned int cmd, unsigned long arg)
2434{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002435 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002436 int retval;
2437
2438 switch (cmd) {
2439 case TIOCOUTQ:
2440 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2441 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002442 down_write(&tty->termios_rwsem);
Linus Torvalds966031f2017-12-20 17:57:06 -08002443 if (L_ICANON(tty) && !L_EXTPROC(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002444 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002445 else
2446 retval = read_cnt(ldata);
2447 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002448 return put_user(retval, (unsigned int __user *) arg);
2449 default:
2450 return n_tty_ioctl_helper(tty, file, cmd, arg);
2451 }
2452}
2453
Peter Hurley27228732016-01-09 21:35:19 -08002454static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002455 .magic = TTY_LDISC_MAGIC,
2456 .name = "n_tty",
2457 .open = n_tty_open,
2458 .close = n_tty_close,
2459 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002460 .read = n_tty_read,
2461 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002462 .ioctl = n_tty_ioctl,
2463 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002464 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002465 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002466 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002467 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002469
2470/**
2471 * n_tty_inherit_ops - inherit N_TTY methods
2472 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2473 *
Peter Hurley27228732016-01-09 21:35:19 -08002474 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002475 */
2476
2477void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2478{
Peter Hurley27228732016-01-09 21:35:19 -08002479 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002480 ops->owner = NULL;
2481 ops->refcount = ops->flags = 0;
2482}
2483EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002484
2485void __init n_tty_init(void)
2486{
2487 tty_register_ldisc(N_TTY, &n_tty_ops);
2488}