blob: 385b6a5161b2820df22336f0b1c96dc908c9bc47 [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 * This file may be redistributed under the terms of the GNU General Public
20 * License.
21 *
22 * Reduced memory usage for older ARM systems - Russell King.
23 *
Alan Cox4edf1822008-02-08 04:18:44 -080024 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
26 * who actually finally proved there really was a race.
27 *
28 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
29 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010030 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * EAGAIN
32 */
33
34#include <linux/types.h>
35#include <linux/major.h>
36#include <linux/errno.h>
37#include <linux/signal.h>
38#include <linux/fcntl.h>
39#include <linux/sched.h>
40#include <linux/interrupt.h>
41#include <linux/tty.h>
42#include <linux/timer.h>
43#include <linux/ctype.h>
44#include <linux/mm.h>
45#include <linux/string.h>
46#include <linux/slab.h>
47#include <linux/poll.h>
48#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070049#include <linux/audit.h>
50#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000051#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080052#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050053#include <linux/ratelimit.h>
Peter Hurley86e35ae2013-07-24 09:30:05 -040054#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* number of characters left in xmit buffer before select has we have room */
58#define WAKEUP_CHARS 256
59
60/*
61 * This defines the low- and high-watermarks for throttling and
62 * unthrottling the TTY driver. These watermarks are used for
63 * controlling the space in the read buffer.
64 */
65#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010066#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Joe Petersona88a69c2009-01-02 13:40:53 +000068/*
69 * Special byte codes used in the echo buffer to represent operations
70 * or special handling of characters. Bytes in the echo buffer that
71 * are not part of such special blocks are treated as normal character
72 * codes.
73 */
74#define ECHO_OP_START 0xff
75#define ECHO_OP_MOVE_BACK_COL 0x80
76#define ECHO_OP_SET_CANON_COL 0x81
77#define ECHO_OP_ERASE_TAB 0x82
78
Peter Hurleycbfd0342013-06-15 10:04:26 -040079#define ECHO_COMMIT_WATERMARK 256
80#define ECHO_BLOCK 256
81#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
82
83
Peter Hurley32f13522013-06-15 09:14:17 -040084#undef N_TTY_TRACE
85#ifdef N_TTY_TRACE
86# define n_tty_trace(f, args...) trace_printk(f, ##args)
87#else
88# define n_tty_trace(f, args...)
89#endif
90
Jiri Slaby70ece7a2012-10-18 22:26:38 +020091struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040092 /* producer-published */
93 size_t read_head;
Peter Hurley70aca712015-01-16 15:05:37 -050094 size_t commit_head;
Peter Hurleyfb7aa032013-06-15 09:14:30 -040095 size_t canon_head;
Peter Hurley9dfd16d2013-06-15 10:04:29 -040096 size_t echo_head;
97 size_t echo_commit;
Peter Hurley1075a6e2013-12-09 18:06:07 -050098 size_t echo_mark;
Peter Hurley1bb9d562013-06-15 10:21:20 -040099 DECLARE_BITMAP(char_map, 256);
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400100
101 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200102 unsigned long overrun_time;
103 int num_overrun;
104
Peter Hurley24a89d12013-06-15 09:14:15 -0400105 /* non-atomic */
106 bool no_room;
107
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400108 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200109 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
Peter Hurley4d0ed182013-12-10 17:12:02 -0500110 unsigned char push:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200111
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400112 /* shared by producer and consumer */
Peter Hurley20bafb32013-06-15 10:21:19 -0400113 char read_buf[N_TTY_BUF_SIZE];
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200114 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Peter Hurley20bafb32013-06-15 10:21:19 -0400115 unsigned char echo_buf[N_TTY_BUF_SIZE];
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200116
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400117 /* consumer-published */
118 size_t read_tail;
Peter Hurley40d5e092013-06-15 10:21:17 -0400119 size_t line_start;
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400120
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400121 /* protected by output lock */
122 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200123 unsigned int canon_column;
Peter Hurley9dfd16d2013-06-15 10:04:29 -0400124 size_t echo_tail;
Jiri Slabybddc7152012-10-18 22:26:42 +0200125
126 struct mutex atomic_read_lock;
127 struct mutex output_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200128};
129
Peter Hurleyce741172013-06-15 09:14:20 -0400130static inline size_t read_cnt(struct n_tty_data *ldata)
131{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400132 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400133}
134
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400135static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
136{
137 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
138}
139
140static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
141{
142 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
143}
144
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400145static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
146{
147 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 Hurley17bd7902013-06-15 10:04:24 -0400322 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500323 ldata->commit_head = 0;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500324 ldata->echo_mark = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400325 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000326
Peter Hurleya73d3d62013-06-15 09:14:25 -0400327 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200328 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500329 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Peter Hurleya30737a2013-03-11 16:44:22 -0400332static void n_tty_packet_mode_flush(struct tty_struct *tty)
333{
334 unsigned long flags;
335
Peter Hurleya30737a2013-03-11 16:44:22 -0400336 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400337 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400338 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400339 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000340 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400341 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/**
345 * n_tty_flush_buffer - clean input queue
346 * @tty: terminal device
347 *
Peter Hurley25518c62013-03-11 16:44:31 -0400348 * Flush the input buffer. Called when the tty layer wants the
349 * buffer flushed (eg at hangup) or when the N_TTY line discipline
350 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400352 * Holds termios_rwsem to exclude producer/consumer while
353 * buffer indices are reset.
354 *
355 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Alan Cox4edf1822008-02-08 04:18:44 -0800357
358static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400360 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400361 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500362 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800363
Peter Hurleya30737a2013-03-11 16:44:22 -0400364 if (tty->link)
365 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400366 up_write(&tty->termios_rwsem);
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * is_utf8_continuation - utf8 multibyte check
371 * @c: byte to check
372 *
373 * Returns true if the utf8 character 'c' is a multibyte continuation
374 * character. We use this to correctly compute the on screen size
375 * of the character when printing
376 */
Alan Cox4edf1822008-02-08 04:18:44 -0800377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static inline int is_utf8_continuation(unsigned char c)
379{
380 return (c & 0xc0) == 0x80;
381}
382
383/**
384 * is_continuation - multibyte check
385 * @c: byte to check
386 *
387 * Returns true if the utf8 character 'c' is a multibyte continuation
388 * character and the terminal is in unicode mode.
389 */
Alan Cox4edf1822008-02-08 04:18:44 -0800390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static inline int is_continuation(unsigned char c, struct tty_struct *tty)
392{
393 return I_IUTF8(tty) && is_utf8_continuation(c);
394}
395
396/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000397 * do_output_char - output one character
398 * @c: character (or partial unicode symbol)
399 * @tty: terminal device
400 * @space: space available in tty driver write buffer
401 *
402 * This is a helper function that handles one output character
403 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600404 * doing OPOST processing and putting the results in the
405 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000406 *
407 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
408 * and NLDLY. They simply aren't relevant in the world today.
409 * If you ever need them, add them here.
410 *
411 * Returns the number of bytes of buffer space used or -1 if
412 * no space left.
413 *
414 * Locking: should be called under the output_lock to protect
415 * the column state and space left in the buffer
416 */
417
418static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
419{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200420 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000421 int spaces;
422
423 if (!space)
424 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000425
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 switch (c) {
427 case '\n':
428 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200429 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000430 if (O_ONLCR(tty)) {
431 if (space < 2)
432 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200433 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700434 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000435 return 2;
436 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200437 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 break;
439 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200440 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000441 return 0;
442 if (O_OCRNL(tty)) {
443 c = '\n';
444 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200445 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000446 break;
447 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200448 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000449 break;
450 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200451 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 if (O_TABDLY(tty) == XTABS) {
453 if (space < spaces)
454 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200455 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000456 tty->ops->write(tty, " ", spaces);
457 return spaces;
458 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200459 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000460 break;
461 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200462 if (ldata->column > 0)
463 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000464 break;
465 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000466 if (!iscntrl(c)) {
467 if (O_OLCUC(tty))
468 c = toupper(c);
469 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200470 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000471 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000472 break;
473 }
474
475 tty_put_char(tty, c);
476 return 1;
477}
478
479/**
480 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * @c: character (or partial unicode symbol)
482 * @tty: terminal device
483 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600484 * Output one character with OPOST processing.
485 * Returns -1 when the output device is full and the character
486 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000488 * Locking: output_lock to protect column state and space left
489 * (also, this is called from n_tty_write under the
490 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Alan Cox4edf1822008-02-08 04:18:44 -0800492
Joe Petersona88a69c2009-01-02 13:40:53 +0000493static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jiri Slabybddc7152012-10-18 22:26:42 +0200495 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000496 int space, retval;
497
Jiri Slabybddc7152012-10-18 22:26:42 +0200498 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Alan Coxf34d7a52008-04-30 00:54:13 -0700500 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000501 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Jiri Slabybddc7152012-10-18 22:26:42 +0200503 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000504 if (retval < 0)
505 return -1;
506 else
507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000511 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600513 * @buf: character buffer
514 * @nr: number of bytes to output
515 *
516 * Output a block of characters with OPOST processing.
517 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *
519 * This path is used to speed up block console writes, among other
520 * things when processing blocks of output data. It handles only
521 * the simple cases normally found and helps to generate blocks of
522 * symbols for the console driver and thus improve performance.
523 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000524 * Locking: output_lock to protect column state and space left
525 * (also, this is called from n_tty_write under the
526 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Alan Cox4edf1822008-02-08 04:18:44 -0800528
Joe Petersona88a69c2009-01-02 13:40:53 +0000529static ssize_t process_output_block(struct tty_struct *tty,
530 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200532 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100534 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 const unsigned char *cp;
536
Jiri Slabybddc7152012-10-18 22:26:42 +0200537 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000538
Alan Coxf34d7a52008-04-30 00:54:13 -0700539 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000540 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200541 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (nr > space)
545 nr = space;
546
547 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000548 unsigned char c = *cp;
549
550 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 case '\n':
552 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200553 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (O_ONLCR(tty))
555 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200556 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200559 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto break_out;
561 if (O_OCRNL(tty))
562 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200563 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case '\t':
566 goto break_out;
567 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200568 if (ldata->column > 0)
569 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000572 if (!iscntrl(c)) {
573 if (O_OLCUC(tty))
574 goto break_out;
575 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200576 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 }
580 }
581break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700582 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000583
Jiri Slabybddc7152012-10-18 22:26:42 +0200584 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return i;
586}
587
Joe Petersona88a69c2009-01-02 13:40:53 +0000588/**
589 * process_echoes - write pending echo characters
590 * @tty: terminal device
591 *
592 * Write previously buffered echo (and other ldisc-generated)
593 * characters to the tty.
594 *
595 * Characters generated by the ldisc (including echoes) need to
596 * be buffered because the driver's write buffer can fill during
597 * heavy program output. Echoing straight to the driver will
598 * often fail under these conditions, causing lost characters and
599 * resulting mismatches of ldisc state information.
600 *
601 * Since the ldisc state must represent the characters actually sent
602 * to the driver at the time of the write, operations like certain
603 * changes in column state are also saved in the buffer and executed
604 * here.
605 *
606 * A circular fifo buffer is used so that the most recent characters
607 * are prioritized. Also, when control characters are echoed with a
608 * prefixed "^", the pair is treated atomically and thus not separated.
609 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400610 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000611 */
612
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400613static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000614{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200615 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400616 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400617 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000618 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000619
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400620 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000621
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400622 tail = ldata->echo_tail;
Peter Hurley29c7c5c2013-06-15 10:04:28 -0400623 while (ldata->echo_commit != tail) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400624 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000625 if (c == ECHO_OP_START) {
626 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000627 int no_space_left = 0;
628
629 /*
630 * If the buffer byte is the start of a multi-byte
631 * operation, get the next byte, which is either the
632 * op code or a control character value.
633 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400634 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000635
Joe Petersona88a69c2009-01-02 13:40:53 +0000636 switch (op) {
637 unsigned int num_chars, num_bs;
638
639 case ECHO_OP_ERASE_TAB:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400640 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000641
642 /*
643 * Determine how many columns to go back
644 * in order to erase the tab.
645 * This depends on the number of columns
646 * used by other characters within the tab
647 * area. If this (modulo 8) count is from
648 * the start of input rather than from a
649 * previous tab, we offset by canon column.
650 * Otherwise, tab spacing is normal.
651 */
652 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200653 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000654 num_bs = 8 - (num_chars & 7);
655
656 if (num_bs > space) {
657 no_space_left = 1;
658 break;
659 }
660 space -= num_bs;
661 while (num_bs--) {
662 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200663 if (ldata->column > 0)
664 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000665 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400666 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000667 break;
668
669 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200670 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400671 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000672 break;
673
674 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200675 if (ldata->column > 0)
676 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400677 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000678 break;
679
680 case ECHO_OP_START:
681 /* This is an escaped echo op start code */
682 if (!space) {
683 no_space_left = 1;
684 break;
685 }
686 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200687 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000688 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400689 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000690 break;
691
692 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600694 * If the op is not a special byte code,
695 * it is a ctrl char tagged to be echoed
696 * as "^X" (where X is the letter
697 * representing the control char).
698 * Note that we must ensure there is
699 * enough space for the whole ctrl pair.
700 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000701 */
Joe Peterson62b26352009-09-09 15:03:47 -0600702 if (space < 2) {
703 no_space_left = 1;
704 break;
705 }
706 tty_put_char(tty, '^');
707 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200708 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600709 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400710 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 }
712
713 if (no_space_left)
714 break;
715 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400716 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600717 int retval = do_output_char(c, tty, space);
718 if (retval < 0)
719 break;
720 space -= retval;
721 } else {
722 if (!space)
723 break;
724 tty_put_char(tty, c);
725 space -= 1;
726 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400727 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000728 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000729 }
730
Peter Hurleycbfd0342013-06-15 10:04:26 -0400731 /* If the echo buffer is nearly full (so that the possibility exists
732 * of echo overrun before the next commit), then discard enough
733 * data at the tail to prevent a subsequent overrun */
734 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200735 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500736 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400737 tail += 3;
738 else
739 tail += 2;
740 } else
741 tail++;
742 }
743
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400744 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400745 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Peter Hurley17bd7902013-06-15 10:04:24 -0400748static void commit_echoes(struct tty_struct *tty)
749{
750 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400751 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400752 size_t head;
753
754 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500755 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400756 old = ldata->echo_commit - ldata->echo_tail;
757
758 /* Process committed echoes if the accumulated # of bytes
759 * is over the threshold (and try again each time another
760 * block is accumulated) */
761 nr = head - ldata->echo_tail;
762 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
763 return;
Peter Hurley17bd7902013-06-15 10:04:24 -0400764
Peter Hurley019ebdf2013-06-15 10:04:25 -0400765 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400766 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400767 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400768 mutex_unlock(&ldata->output_lock);
769
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400770 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400771 tty->ops->flush_chars(tty);
772}
773
774static void process_echoes(struct tty_struct *tty)
775{
776 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400777 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400778
Peter Hurleye2613be2014-02-11 16:34:55 -0500779 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400780 return;
781
782 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500783 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400784 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400785 mutex_unlock(&ldata->output_lock);
786
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400787 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400788 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400789}
790
Peter Hurley1075a6e2013-12-09 18:06:07 -0500791/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400792static void flush_echoes(struct tty_struct *tty)
793{
794 struct n_tty_data *ldata = tty->disc_data;
795
Peter Hurley39434ab2013-11-29 12:56:10 -0500796 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
797 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400798 return;
799
800 mutex_lock(&ldata->output_lock);
801 ldata->echo_commit = ldata->echo_head;
802 __process_echoes(tty);
803 mutex_unlock(&ldata->output_lock);
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000807 * add_echo_byte - add a byte to the echo buffer
808 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200809 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000810 *
811 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000812 */
813
Peter Hurleycbfd0342013-06-15 10:04:26 -0400814static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000815{
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400816 *echo_buf_addr(ldata, ldata->echo_head++) = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000817}
818
819/**
820 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200821 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000822 *
823 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000824 */
825
Jiri Slaby57c94122012-10-18 22:26:43 +0200826static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000827{
Jiri Slaby57c94122012-10-18 22:26:43 +0200828 add_echo_byte(ECHO_OP_START, ldata);
829 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000830}
831
832/**
833 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200834 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000835 *
836 * Add an operation to the echo buffer to set the canon column
837 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 */
839
Jiri Slaby57c94122012-10-18 22:26:43 +0200840static void echo_set_canon_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_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000844}
845
846/**
847 * echo_erase_tab - add operation to erase a tab
848 * @num_chars: number of character columns already used
849 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200850 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000851 *
852 * Add an operation to the echo buffer to erase a tab.
853 *
854 * Called by the eraser function, which knows how many character
855 * columns have been used since either a previous tab or the start
856 * of input. This information will be used later, along with
857 * canon column (if applicable), to go back the correct number
858 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000859 */
860
861static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200862 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000863{
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 add_echo_byte(ECHO_OP_START, ldata);
865 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000866
867 /* We only need to know this modulo 8 (tab spacing) */
868 num_chars &= 7;
869
870 /* Set the high bit as a flag if num_chars is after a previous tab */
871 if (after_tab)
872 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000873
Jiri Slaby57c94122012-10-18 22:26:43 +0200874 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000875}
876
877/**
878 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * @c: unicode byte to echo
880 * @tty: terminal device
881 *
Alan Cox4edf1822008-02-08 04:18:44 -0800882 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100884 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000885 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000886 */
887
Jiri Slaby57c94122012-10-18 22:26:43 +0200888static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000889{
Joe Petersona88a69c2009-01-02 13:40:53 +0000890 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200891 add_echo_byte(ECHO_OP_START, ldata);
892 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000893 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200894 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000895 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000896}
897
898/**
899 * echo_char - echo a character
900 * @c: unicode byte to echo
901 * @tty: terminal device
902 *
903 * Echo user input back onto the screen. This must be called only when
904 * L_ECHO(tty) is true. Called from the driver receive_buf path.
905 *
Joe Peterson62b26352009-09-09 15:03:47 -0600906 * This variant tags control characters to be echoed as "^X"
907 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909
910static void echo_char(unsigned char c, struct tty_struct *tty)
911{
Jiri Slabybddc7152012-10-18 22:26:42 +0200912 struct n_tty_data *ldata = tty->disc_data;
913
Joe Petersona88a69c2009-01-02 13:40:53 +0000914 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200915 add_echo_byte(ECHO_OP_START, ldata);
916 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000917 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600918 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200919 add_echo_byte(ECHO_OP_START, ldata);
920 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Alan Cox17b82062008-10-13 10:45:06 +0100924/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000925 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200926 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100927 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000928
Jiri Slaby57c94122012-10-18 22:26:43 +0200929static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200931 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200932 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200933 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935}
936
937/**
938 * eraser - handle erase function
939 * @c: character input
940 * @tty: terminal device
941 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200942 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * present in the stream from the driver layer. Handles the complexities
944 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100945 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400946 * n_tty_receive_buf()/producer path:
947 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Alan Cox4edf1822008-02-08 04:18:44 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static void eraser(unsigned char c, struct tty_struct *tty)
951{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200952 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400954 size_t head;
955 size_t cnt;
956 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200958 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000959 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961 }
962 if (c == ERASE_CHAR(tty))
963 kill_type = ERASE;
964 else if (c == WERASE_CHAR(tty))
965 kill_type = WERASE;
966 else {
967 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200968 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
970 }
971 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200972 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200973 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 echo_char(KILL_CHAR(tty), tty);
975 /* Add a newline if ECHOK is on and ECHOKE is off. */
976 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200977 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 }
980 kill_type = KILL;
981 }
982
983 seen_alnums = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200984 while (ldata->read_head != ldata->canon_head) {
985 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* erase a single possibly multibyte character */
988 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400989 head--;
990 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200991 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* do not partially erase */
994 if (is_continuation(c, tty))
995 break;
996
997 if (kill_type == WERASE) {
998 /* Equivalent to BSD's ALTWERASE. */
999 if (isalnum(c) || c == '_')
1000 seen_alnums++;
1001 else if (seen_alnums)
1002 break;
1003 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001004 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001005 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (L_ECHO(tty)) {
1007 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001008 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001009 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001010 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 /* if cnt > 1, output a multi-byte character */
1013 echo_char(c, tty);
1014 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001015 head++;
1016 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001017 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1020 echo_char(ERASE_CHAR(tty), tty);
1021 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001022 unsigned int num_chars = 0;
1023 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001024 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Joe Petersona88a69c2009-01-02 13:40:53 +00001026 /*
1027 * Count the columns used for characters
1028 * since the start of input or after a
1029 * previous tab.
1030 * This info is used to go back the correct
1031 * number of columns.
1032 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001033 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001034 tail--;
1035 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001036 if (c == '\t') {
1037 after_tab = 1;
1038 break;
Alan Cox300a6202009-01-02 13:41:04 +00001039 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001041 num_chars += 2;
1042 } else if (!is_continuation(c, tty)) {
1043 num_chars++;
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001046 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 } else {
1048 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001049 echo_char_raw('\b', ldata);
1050 echo_char_raw(' ', ldata);
1051 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001054 echo_char_raw('\b', ldata);
1055 echo_char_raw(' ', ldata);
1056 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 }
1059 }
1060 if (kill_type == ERASE)
1061 break;
1062 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001063 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001064 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067/**
1068 * isig - handle the ISIG optio
1069 * @sig: signal
1070 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001072 * Called when a signal is being sent due to terminal input.
1073 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001074 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001075 * Performs input and output flush if !NOFLSH. In this context, the echo
1076 * buffer is 'output'. The signal is processed first to alert any current
1077 * readers or writers to discontinue and exit their i/o loops.
1078 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001079 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
Alan Cox4edf1822008-02-08 04:18:44 -08001081
Peter Hurley3b19e032015-06-27 09:21:32 -04001082static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Peter Hurley8c985d12013-03-06 08:38:19 -05001084 struct pid *tty_pgrp = tty_get_pgrp(tty);
1085 if (tty_pgrp) {
1086 kill_pgrp(tty_pgrp, sig, 1);
1087 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001089}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001090
Peter Hurley3b19e032015-06-27 09:21:32 -04001091static void isig(int sig, struct tty_struct *tty)
1092{
1093 struct n_tty_data *ldata = tty->disc_data;
1094
1095 if (L_NOFLSH(tty)) {
1096 /* signal only */
1097 __isig(sig, tty);
1098
1099 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001100 up_read(&tty->termios_rwsem);
1101 down_write(&tty->termios_rwsem);
1102
Peter Hurley3b19e032015-06-27 09:21:32 -04001103 __isig(sig, tty);
1104
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001105 /* clear echo buffer */
1106 mutex_lock(&ldata->output_lock);
1107 ldata->echo_head = ldata->echo_tail = 0;
1108 ldata->echo_mark = ldata->echo_commit = 0;
1109 mutex_unlock(&ldata->output_lock);
1110
1111 /* clear output buffer */
1112 tty_driver_flush_buffer(tty);
1113
1114 /* clear input buffer */
1115 reset_buffer_flags(tty->disc_data);
1116
1117 /* notify pty master of flush */
1118 if (tty->link)
1119 n_tty_packet_mode_flush(tty);
1120
1121 up_write(&tty->termios_rwsem);
1122 down_read(&tty->termios_rwsem);
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/**
1127 * n_tty_receive_break - handle break
1128 * @tty: terminal
1129 *
1130 * An RS232 break event has been hit in the incoming bitstream. This
1131 * can cause a variety of events depending upon the termios settings.
1132 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001133 * n_tty_receive_buf()/producer path:
1134 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001135 *
1136 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Alan Cox4edf1822008-02-08 04:18:44 -08001138
Peter Hurley4b293492013-07-24 08:29:55 -04001139static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Jiri Slaby57c94122012-10-18 22:26:43 +02001141 struct n_tty_data *ldata = tty->disc_data;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (I_IGNBRK(tty))
1144 return;
1145 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001146 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return;
1148 }
1149 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001150 put_tty_queue('\377', ldata);
1151 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001153 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/**
1157 * n_tty_receive_overrun - handle overrun reporting
1158 * @tty: terminal
1159 *
1160 * Data arrived faster than we could process it. While the tty
1161 * driver has flagged this the bits that were missed are gone
1162 * forever.
1163 *
1164 * Called from the receive_buf path so single threaded. Does not
1165 * need locking as num_overrun and overrun_time are function
1166 * private.
1167 */
Alan Cox4edf1822008-02-08 04:18:44 -08001168
Peter Hurley4b293492013-07-24 08:29:55 -04001169static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001171 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001173 ldata->num_overrun++;
1174 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1175 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001176 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001177 ldata->overrun_time = jiffies;
1178 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180}
1181
1182/**
1183 * n_tty_receive_parity_error - error notifier
1184 * @tty: terminal device
1185 * @c: character
1186 *
1187 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001188 * the error case if necessary.
1189 *
1190 * n_tty_receive_buf()/producer path:
1191 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
Peter Hurley4b293492013-07-24 08:29:55 -04001193static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Jiri Slaby57c94122012-10-18 22:26:43 +02001195 struct n_tty_data *ldata = tty->disc_data;
1196
Peter Hurley66528f92014-06-16 08:10:42 -04001197 if (I_INPCK(tty)) {
1198 if (I_IGNPAR(tty))
1199 return;
1200 if (I_PARMRK(tty)) {
1201 put_tty_queue('\377', ldata);
1202 put_tty_queue('\0', ldata);
1203 put_tty_queue(c, ldata);
1204 } else
1205 put_tty_queue('\0', ldata);
1206 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001207 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001210static void
1211n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1212{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001213 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001214 if (I_IXON(tty))
1215 start_tty(tty);
1216 if (L_ECHO(tty)) {
1217 echo_char(c, tty);
1218 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001219 } else
1220 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001221 return;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/**
1225 * n_tty_receive_char - perform processing
1226 * @tty: terminal device
1227 * @c: character
1228 *
1229 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001230 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001232 *
1233 * n_tty_receive_buf()/producer path:
1234 * caller holds non-exclusive termios_rwsem
1235 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001236 *
1237 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
1239
Peter Hurleye60d27c2013-07-24 08:29:56 -04001240static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001241n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001243 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (I_IXON(tty)) {
1246 if (c == START_CHAR(tty)) {
1247 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001248 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 if (c == STOP_CHAR(tty)) {
1252 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 }
Joe Peterson575537b32008-04-30 00:53:30 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001258 if (c == INTR_CHAR(tty)) {
1259 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001260 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001261 } else if (c == QUIT_CHAR(tty)) {
1262 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001263 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001264 } else if (c == SUSP_CHAR(tty)) {
1265 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 }
Joe Peterson575537b32008-04-30 00:53:30 -07001269
Peter Hurley855df3c2013-07-24 08:29:50 -04001270 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1271 start_tty(tty);
1272 process_echoes(tty);
1273 }
1274
Joe Peterson575537b32008-04-30 00:53:30 -07001275 if (c == '\r') {
1276 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001277 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001278 if (I_ICRNL(tty))
1279 c = '\n';
1280 } else if (c == '\n' && I_INLCR(tty))
1281 c = '\r';
1282
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001283 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1285 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1286 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001287 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001291 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001293 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001295 echo_char_raw('^', ldata);
1296 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001297 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001300 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001302 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001303 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Jiri Slaby57c94122012-10-18 22:26:43 +02001305 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001307 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001308 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001309 echo_char(read_buf(ldata, tail), tty);
1310 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001312 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001316 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001317 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001318 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 goto handle_newline;
1321 }
1322 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 c = __DISABLED_CHAR;
1324 goto handle_newline;
1325 }
1326 if ((c == EOL_CHAR(tty)) ||
1327 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1328 /*
1329 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1330 */
1331 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001333 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001334 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001336 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 /*
1339 * XXX does PARMRK doubling happen for
1340 * EOL_CHAR and EOL2_CHAR?
1341 */
Peter Hurley001ba922013-12-02 14:24:44 -05001342 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001343 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Alan Cox4edf1822008-02-08 04:18:44 -08001345handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001346 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001347 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001348 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001350 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 }
Alan Cox4edf1822008-02-08 04:18:44 -08001354
Joe Petersonacc71bb2009-01-02 13:43:32 +00001355 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001356 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001358 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 else {
1360 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001361 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001362 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 echo_char(c, tty);
1364 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001365 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Peter Hurley001ba922013-12-02 14:24:44 -05001368 /* PARMRK doubling check */
1369 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001370 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Jiri Slaby57c94122012-10-18 22:26:43 +02001372 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001373 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Peter Hurleye60d27c2013-07-24 08:29:56 -04001376static inline void
1377n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001378{
1379 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001380
Peter Hurleye60d27c2013-07-24 08:29:56 -04001381 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1382 start_tty(tty);
1383 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001384 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001385 if (L_ECHO(tty)) {
1386 finish_erasing(ldata);
1387 /* Record the column of first canon char. */
1388 if (ldata->canon_head == ldata->read_head)
1389 echo_set_canon_col(ldata);
1390 echo_char(c, tty);
1391 commit_echoes(tty);
1392 }
Peter Hurley001ba922013-12-02 14:24:44 -05001393 /* PARMRK doubling check */
1394 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001395 put_tty_queue(c, ldata);
1396 put_tty_queue(c, ldata);
1397}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001398
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001399static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001400{
1401 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001402}
1403
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001404static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001405n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1406{
1407 struct n_tty_data *ldata = tty->disc_data;
1408
Peter Hurleye60d27c2013-07-24 08:29:56 -04001409 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1410 start_tty(tty);
1411 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001412 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001413 if (L_ECHO(tty)) {
1414 finish_erasing(ldata);
1415 /* Record the column of first canon char. */
1416 if (ldata->canon_head == ldata->read_head)
1417 echo_set_canon_col(ldata);
1418 echo_char(c, tty);
1419 commit_echoes(tty);
1420 }
1421 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001422}
1423
Peter Hurley8dc4b252013-12-02 14:24:43 -05001424static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001425{
1426 if (I_ISTRIP(tty))
1427 c &= 0x7f;
1428 if (I_IUCLC(tty) && L_IEXTEN(tty))
1429 c = tolower(c);
1430
1431 if (I_IXON(tty)) {
1432 if (c == STOP_CHAR(tty))
1433 stop_tty(tty);
1434 else if (c == START_CHAR(tty) ||
1435 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1436 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1437 c != SUSP_CHAR(tty))) {
1438 start_tty(tty);
1439 process_echoes(tty);
1440 }
1441 }
1442}
1443
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001444static void
1445n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1446{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001447 switch (flag) {
1448 case TTY_BREAK:
1449 n_tty_receive_break(tty);
1450 break;
1451 case TTY_PARITY:
1452 case TTY_FRAME:
1453 n_tty_receive_parity_error(tty, c);
1454 break;
1455 case TTY_OVERRUN:
1456 n_tty_receive_overrun(tty);
1457 break;
1458 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001459 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001460 break;
1461 }
1462}
1463
Peter Hurleye60d27c2013-07-24 08:29:56 -04001464static void
1465n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1466{
1467 struct n_tty_data *ldata = tty->disc_data;
1468
1469 ldata->lnext = 0;
1470 if (likely(flag == TTY_NORMAL)) {
1471 if (I_ISTRIP(tty))
1472 c &= 0x7f;
1473 if (I_IUCLC(tty) && L_IEXTEN(tty))
1474 c = tolower(c);
1475 n_tty_receive_char(tty, c);
1476 } else
1477 n_tty_receive_char_flagged(tty, c, flag);
1478}
1479
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001480static void
1481n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1482 char *fp, int count)
1483{
1484 struct n_tty_data *ldata = tty->disc_data;
1485 size_t n, head;
1486
1487 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001488 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001489 memcpy(read_buf_addr(ldata, head), cp, n);
1490 ldata->read_head += n;
1491 cp += n;
1492 count -= n;
1493
1494 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001495 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001496 memcpy(read_buf_addr(ldata, head), cp, n);
1497 ldata->read_head += n;
1498}
1499
Peter Hurley554117bd2013-06-15 10:21:25 -04001500static void
1501n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1502 char *fp, int count)
1503{
1504 struct n_tty_data *ldata = tty->disc_data;
1505 char flag = TTY_NORMAL;
1506
1507 while (count--) {
1508 if (fp)
1509 flag = *fp++;
1510 if (likely(flag == TTY_NORMAL))
1511 put_tty_queue(*cp++, ldata);
1512 else
1513 n_tty_receive_char_flagged(tty, *cp++, flag);
1514 }
1515}
1516
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001517static void
1518n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1519 char *fp, int count)
1520{
1521 char flag = TTY_NORMAL;
1522
1523 while (count--) {
1524 if (fp)
1525 flag = *fp++;
1526 if (likely(flag == TTY_NORMAL))
1527 n_tty_receive_char_closing(tty, *cp++);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001528 }
1529}
1530
Peter Hurley7d88d632013-07-24 08:29:49 -04001531static void
1532n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001533 char *fp, int count)
1534{
1535 struct n_tty_data *ldata = tty->disc_data;
1536 char flag = TTY_NORMAL;
1537
1538 while (count--) {
1539 if (fp)
1540 flag = *fp++;
1541 if (likely(flag == TTY_NORMAL)) {
1542 unsigned char c = *cp++;
1543
1544 if (I_ISTRIP(tty))
1545 c &= 0x7f;
1546 if (I_IUCLC(tty) && L_IEXTEN(tty))
1547 c = tolower(c);
1548 if (L_EXTPROC(tty)) {
1549 put_tty_queue(c, ldata);
1550 continue;
1551 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001552 if (!test_bit(c, ldata->char_map))
1553 n_tty_receive_char_inline(tty, c);
1554 else if (n_tty_receive_char_special(tty, c) && count) {
1555 if (fp)
1556 flag = *fp++;
1557 n_tty_receive_char_lnext(tty, *cp++, flag);
1558 count--;
1559 }
Peter Hurley6baad002013-07-24 08:29:52 -04001560 } else
1561 n_tty_receive_char_flagged(tty, *cp++, flag);
1562 }
1563}
1564
1565static void
1566n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1567 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001568{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001569 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001570 char flag = TTY_NORMAL;
1571
1572 while (count--) {
1573 if (fp)
1574 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001575 if (likely(flag == TTY_NORMAL)) {
1576 unsigned char c = *cp++;
1577
1578 if (!test_bit(c, ldata->char_map))
1579 n_tty_receive_char_fast(tty, c);
1580 else if (n_tty_receive_char_special(tty, c) && count) {
1581 if (fp)
1582 flag = *fp++;
1583 n_tty_receive_char_lnext(tty, *cp++, flag);
1584 count--;
1585 }
1586 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001587 n_tty_receive_char_flagged(tty, *cp++, flag);
1588 }
1589}
1590
Peter Hurley24a89d12013-06-15 09:14:15 -04001591static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1592 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001594 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001595 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001597 if (ldata->real_raw)
1598 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001599 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117bd2013-06-15 10:21:25 -04001600 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001601 else if (tty->closing && !L_EXTPROC(tty))
1602 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001603 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001604 if (ldata->lnext) {
1605 char flag = TTY_NORMAL;
1606
1607 if (fp)
1608 flag = *fp++;
1609 n_tty_receive_char_lnext(tty, *cp++, flag);
1610 count--;
1611 }
1612
Peter Hurley7de971b2013-07-24 08:29:53 -04001613 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001614 n_tty_receive_buf_fast(tty, cp, fp, count);
1615 else
1616 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001617
1618 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001619 if (tty->ops->flush_chars)
1620 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622
Peter Hurley70aca712015-01-16 15:05:37 -05001623 if (ldata->icanon && !L_EXTPROC(tty))
1624 return;
1625
1626 /* publish read_head to consumer */
1627 smp_store_release(&ldata->commit_head, ldata->read_head);
1628
Peter Hurley33d71362016-01-09 21:45:08 -08001629 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001631 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001635/**
1636 * n_tty_receive_buf_common - process input
1637 * @tty: device to receive input
1638 * @cp: input chars
1639 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1640 * @count: number of input chars in @cp
1641 *
1642 * Called by the terminal driver when a block of characters has
1643 * been received. This function must be called from soft contexts
1644 * not from interrupt context. The driver is responsible for making
1645 * calls one at a time and in order (or using flush_to_ldisc)
1646 *
1647 * Returns the # of input chars from @cp which were processed.
1648 *
1649 * In canonical mode, the maximum line length is 4096 chars (including
1650 * the line termination char); lines longer than 4096 chars are
1651 * truncated. After 4095 chars, input data is still processed but
1652 * not stored. Overflow processing ensures the tty can always
1653 * receive more input until at least one line can be read.
1654 *
1655 * In non-canonical mode, the read buffer will only accept 4095 chars;
1656 * this provides the necessary space for a newline char if the input
1657 * mode is switched to canonical.
1658 *
1659 * Note it is possible for the read buffer to _contain_ 4096 chars
1660 * in non-canonical mode: the read buffer could already contain the
1661 * maximum canon line of 4096 chars when the mode is switched to
1662 * non-canonical.
1663 *
1664 * n_tty_receive_buf()/producer path:
1665 * claims non-exclusive termios_rwsem
1666 * publishes commit_head or canon_head
1667 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001668static int
1669n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1670 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001671{
1672 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001673 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001674
Peter Hurley9356b532013-06-15 09:14:24 -04001675 down_read(&tty->termios_rwsem);
1676
Peter Hurley19e2ad62013-07-24 08:29:54 -04001677 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001678 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001679 * When PARMRK is set, each input char may take up to 3 chars
1680 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001681 *
1682 * If we are doing input canonicalization, and there are no
1683 * pending newlines, let characters through without limit, so
1684 * that erase characters will be handled. Other excess
1685 * characters will be beeped.
1686 *
1687 * paired with store in *_copy_from_read_buf() -- guarantees
1688 * the consumer has loaded the data in read_buf up to the new
1689 * read_tail (so this producer will not overwrite unread data)
1690 */
1691 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001692
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001693 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001694 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001695 room = (room + 2) / 3;
1696 room--;
1697 if (room <= 0) {
1698 overflow = ldata->icanon && ldata->canon_head == tail;
1699 if (overflow && room < 0)
1700 ldata->read_head--;
1701 room = overflow;
1702 ldata->no_room = flow && !room;
1703 } else
1704 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001705
Peter Hurley19e2ad62013-07-24 08:29:54 -04001706 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001707 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001708 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001709
1710 /* ignore parity errors if handling overflow */
1711 if (!overflow || !fp || *fp != TTY_PARITY)
1712 __receive_buf(tty, cp, fp, n);
1713
Peter Hurley19e2ad62013-07-24 08:29:54 -04001714 cp += n;
1715 if (fp)
1716 fp += n;
1717 count -= n;
1718 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001719 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001720
Peter Hurley19e2ad62013-07-24 08:29:54 -04001721 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001722
1723 /* Unthrottle if handling overflow on pty */
1724 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1725 if (overflow) {
1726 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1727 tty_unthrottle_safe(tty);
1728 __tty_set_flow_change(tty, 0);
1729 }
1730 } else
1731 n_tty_check_throttle(tty);
1732
Peter Hurley9356b532013-06-15 09:14:24 -04001733 up_read(&tty->termios_rwsem);
1734
Peter Hurley19e2ad62013-07-24 08:29:54 -04001735 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001736}
1737
Peter Hurley5c32d122013-12-02 14:24:41 -05001738static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1739 char *fp, int count)
1740{
1741 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1742}
1743
1744static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1745 char *fp, int count)
1746{
1747 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/**
1751 * n_tty_set_termios - termios data changed
1752 * @tty: terminal
1753 * @old: previous data
1754 *
1755 * Called by the tty layer when the user changes termios flags so
1756 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001757 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * guaranteed that this function will not be re-entered or in progress
1759 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001760 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001761 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Alan Cox4edf1822008-02-08 04:18:44 -08001763
1764static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001766 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001767
Peter Hurleyc786f742013-09-17 12:53:35 -04001768 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001769 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001770 ldata->line_start = ldata->read_tail;
1771 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1772 ldata->canon_head = ldata->read_tail;
1773 ldata->push = 0;
1774 } else {
1775 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1776 ldata->read_flags);
1777 ldata->canon_head = ldata->read_head;
1778 ldata->push = 1;
1779 }
Peter Hurley70aca712015-01-16 15:05:37 -05001780 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001781 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001782 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001783 }
1784
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001785 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1788 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1789 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1790 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001791 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001794 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001796 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001799 set_bit(ERASE_CHAR(tty), ldata->char_map);
1800 set_bit(KILL_CHAR(tty), ldata->char_map);
1801 set_bit(EOF_CHAR(tty), ldata->char_map);
1802 set_bit('\n', ldata->char_map);
1803 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001805 set_bit(WERASE_CHAR(tty), ldata->char_map);
1806 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1807 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (L_ECHO(tty))
1809 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001810 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 }
1813 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001814 set_bit(START_CHAR(tty), ldata->char_map);
1815 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001818 set_bit(INTR_CHAR(tty), ldata->char_map);
1819 set_bit(QUIT_CHAR(tty), ldata->char_map);
1820 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001822 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001823 ldata->raw = 0;
1824 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001826 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1828 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1829 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001830 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001832 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001834 /*
1835 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1836 * been stopped by STOP_CHAR(tty) before it.
1837 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001838 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001839 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001840 process_echoes(tty);
1841 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001842
Alan Coxf34d7a52008-04-30 00:54:13 -07001843 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001844 wake_up_interruptible(&tty->write_wait);
1845 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/**
1849 * n_tty_close - close the ldisc for this tty
1850 * @tty: device
1851 *
Alan Cox4edf1822008-02-08 04:18:44 -08001852 * Called from the terminal layer when this line discipline is
1853 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * discipline change. The function will not be called while other
1855 * ldisc methods are in progress.
1856 */
Alan Cox4edf1822008-02-08 04:18:44 -08001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858static void n_tty_close(struct tty_struct *tty)
1859{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001860 struct n_tty_data *ldata = tty->disc_data;
1861
Peter Hurley79901312013-03-11 16:44:23 -04001862 if (tty->link)
1863 n_tty_packet_mode_flush(tty);
1864
Peter Hurley20bafb32013-06-15 10:21:19 -04001865 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001866 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
1869/**
1870 * n_tty_open - open an ldisc
1871 * @tty: terminal to open
1872 *
Alan Cox4edf1822008-02-08 04:18:44 -08001873 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * terminal device. Can sleep. Called serialized so that no
1875 * other events will occur in parallel. No further open will occur
1876 * until a close.
1877 */
1878
1879static int n_tty_open(struct tty_struct *tty)
1880{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001881 struct n_tty_data *ldata;
1882
Peter Hurley20bafb32013-06-15 10:21:19 -04001883 /* Currently a malloc failure here can panic */
1884 ldata = vmalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001885 if (!ldata)
1886 goto err;
1887
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001888 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001889 mutex_init(&ldata->atomic_read_lock);
1890 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001891
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001892 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001893 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001894 ldata->column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001895 ldata->canon_column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001896 ldata->num_overrun = 0;
1897 ldata->no_room = 0;
1898 ldata->lnext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001900 /* indicate buffer work may resume */
1901 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1902 n_tty_set_termios(tty, NULL);
1903 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001906err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001907 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Peter Hurleyeafbe672013-12-02 14:24:45 -05001910static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001912 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001913 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001914
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001915 if (ldata->icanon && !L_EXTPROC(tty))
1916 return ldata->canon_head != ldata->read_tail;
1917 else
Peter Hurley70aca712015-01-16 15:05:37 -05001918 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001922 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * @tty: terminal device
1924 * @b: user data
1925 * @nr: size of data
1926 *
Alan Cox11a96d12008-10-13 10:46:24 +01001927 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * ICANON is off; it copies characters straight from the tty queue to
1929 * user space directly. It can be profitably called twice; once to
1930 * drain the space from the tail pointer to the (physical) end of the
1931 * buffer, and once to drain the space from the (physical) beginning of
1932 * the buffer to head pointer.
1933 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001934 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001936 * n_tty_read()/consumer path:
1937 * caller holds non-exclusive termios_rwsem
1938 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Alan Cox4edf1822008-02-08 04:18:44 -08001940
Alan Cox33f0f882006-01-09 20:54:13 -08001941static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 unsigned char __user **b,
1943 size_t *nr)
1944
1945{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001946 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int retval;
1948 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001949 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001950 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001951 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001954 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001957 const unsigned char *from = read_buf_addr(ldata, tail);
1958 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001960 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08001961 tty_audit_add_data(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05001962 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001963 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05001964 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1965 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001966 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *b += n;
1968 *nr -= n;
1969 }
1970 return retval;
1971}
1972
Peter Hurley88bb0de2013-06-15 09:14:16 -04001973/**
Peter Hurley32f13522013-06-15 09:14:17 -04001974 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001975 * @tty: terminal device
1976 * @b: user data
1977 * @nr: size of data
1978 *
1979 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001980 * it copies one line of input up to and including the line-delimiting
1981 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001982 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05001983 * NB: When termios is changed from non-canonical to canonical mode and
1984 * the read buffer contains data, n_tty_set_termios() simulates an EOF
1985 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
1986 * This causes data already processed as input to be immediately available
1987 * as input although a newline has not been received.
1988 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04001989 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04001990 *
1991 * n_tty_read()/consumer path:
1992 * caller holds non-exclusive termios_rwsem
1993 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04001994 */
1995
Peter Hurley32f13522013-06-15 09:14:17 -04001996static int canon_copy_from_read_buf(struct tty_struct *tty,
1997 unsigned char __user **b,
1998 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04001999{
2000 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002001 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002002 size_t eol;
2003 size_t tail;
2004 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002005
2006 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002007 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002008 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002009
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002010 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2011
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002012 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002013 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2014
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002015 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002016 __func__, *nr, tail, n, size);
2017
2018 eol = find_next_bit(ldata->read_flags, size, tail);
2019 more = n - (size - tail);
2020 if (eol == N_TTY_BUF_SIZE && more) {
2021 /* scan wrapped without finding set bit */
2022 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002023 found = eol != more;
2024 } else
2025 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002026
Peter Hurleyc77569d2013-11-22 07:16:25 -05002027 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002028 if (n > N_TTY_BUF_SIZE)
2029 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002030 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002031
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002032 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2033 c = min(*nr, c);
2034 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002035 }
Peter Hurley32f13522013-06-15 09:14:17 -04002036
Peter Hurley679e7c22015-11-27 14:11:02 -05002037 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2038 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002039
Peter Hurley679e7c22015-11-27 14:11:02 -05002040 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002041 if (ret)
2042 return -EFAULT;
2043 *b += n;
2044 *nr -= n;
2045
Peter Hurleya73d3d62013-06-15 09:14:25 -04002046 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002047 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002048 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002049
Peter Hurley40d5e092013-06-15 10:21:17 -04002050 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002051 if (!ldata->push)
2052 ldata->line_start = ldata->read_tail;
2053 else
2054 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002055 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002056 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002057 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002058}
2059
Al Virocc4191d2008-03-29 03:08:48 +00002060extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002061 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063/**
2064 * job_control - check job control
2065 * @tty: tty
2066 * @file: file handle
2067 *
2068 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002069 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002071 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002072 * Locking: redirected write test is safe
2073 * current->signal->tty check is safe
2074 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Alan Cox4edf1822008-02-08 04:18:44 -08002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077static int job_control(struct tty_struct *tty, struct file *file)
2078{
2079 /* Job control check -- must be done at start and after
2080 every sleep (POSIX.1 7.1.1.4). */
2081 /* NOTE: not yet done after every sleep pending a thorough
2082 check of the logic of this change. -- jlc */
2083 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002084 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002085 return 0;
2086
Peter Hurley2812d9e2015-10-10 20:28:42 -04002087 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
Alan Cox4edf1822008-02-08 04:18:44 -08002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091/**
Alan Cox11a96d12008-10-13 10:46:24 +01002092 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * @tty: tty device
2094 * @file: file object
2095 * @buf: userspace buffer pointer
2096 * @nr: size of I/O
2097 *
2098 * Perform reads for the line discipline. We are guaranteed that the
2099 * line discipline will not be closed under us but we may get multiple
2100 * parallel readers and must handle this ourselves. We may also get
2101 * a hangup. Always called in user context, may sleep.
2102 *
2103 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002104 *
2105 * n_tty_read()/consumer path:
2106 * claims non-exclusive termios_rwsem
2107 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
Alan Cox4edf1822008-02-08 04:18:44 -08002109
Alan Cox11a96d12008-10-13 10:46:24 +01002110static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 unsigned char __user *buf, size_t nr)
2112{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002113 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002115 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002116 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int minimum, time;
2118 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002120 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002121 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002124 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002126
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002127 /*
2128 * Internal serialization of reads.
2129 */
2130 if (file->f_flags & O_NONBLOCK) {
2131 if (!mutex_trylock(&ldata->atomic_read_lock))
2132 return -EAGAIN;
2133 } else {
2134 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2135 return -ERESTARTSYS;
2136 }
2137
Peter Hurley9356b532013-06-15 09:14:24 -04002138 down_read(&tty->termios_rwsem);
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 minimum = time = 0;
2141 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002142 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 minimum = MIN_CHAR(tty);
2144 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002145 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002147 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002148 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150 }
2151
Alan Cox04f378b2008-04-30 00:53:29 -07002152 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002153 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 while (nr) {
2157 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002158 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 unsigned char cs;
2160 if (b != buf)
2161 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002162 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 cs = tty->link->ctrl_status;
2164 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002165 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002166 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002170 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 nr--;
2172 break;
2173 }
Alan Cox4edf1822008-02-08 04:18:44 -08002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!input_available_p(tty, 0)) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002176 up_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002177 tty_buffer_flush_work(tty->port);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002178 down_read(&tty->termios_rwsem);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002179 if (!input_available_p(tty, 0)) {
2180 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2181 retval = -EIO;
2182 break;
2183 }
2184 if (tty_hung_up_p(file))
2185 break;
2186 if (!timeout)
2187 break;
2188 if (file->f_flags & O_NONBLOCK) {
2189 retval = -EAGAIN;
2190 break;
2191 }
2192 if (signal_pending(current)) {
2193 retval = -ERESTARTSYS;
2194 break;
2195 }
2196 up_read(&tty->termios_rwsem);
2197
2198 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2199 timeout);
2200
2201 down_read(&tty->termios_rwsem);
2202 continue;
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002206 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002207 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002208 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 break;
2210 } else {
2211 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002212
2213 /* Deal with packet mode. */
2214 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002215 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002216 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002217 break;
2218 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002219 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002220 nr--;
2221 }
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 uncopied = copy_from_read_buf(tty, &b, &nr);
2224 uncopied += copy_from_read_buf(tty, &b, &nr);
2225 if (uncopied) {
2226 retval = -EFAULT;
2227 break;
2228 }
2229 }
2230
Peter Hurley6367ca72013-06-15 09:14:33 -04002231 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if (b - buf >= minimum)
2234 break;
2235 if (time)
2236 timeout = time;
2237 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002238 if (tail != ldata->read_tail)
2239 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002240 up_read(&tty->termios_rwsem);
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002243 mutex_unlock(&ldata->atomic_read_lock);
2244
Peter Hurley40d5e092013-06-15 10:21:17 -04002245 if (b - buf)
2246 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 return retval;
2249}
2250
2251/**
Alan Cox11a96d12008-10-13 10:46:24 +01002252 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 * @tty: tty device
2254 * @file: file object
2255 * @buf: userspace buffer pointer
2256 * @nr: size of I/O
2257 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002258 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002260 * and other such events. Since the receive code will echo characters,
2261 * thus calling driver write methods, the output_lock is used in
2262 * the output processing functions called here as well as in the
2263 * echo processing function to protect the column state and space
2264 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
2266 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002267 *
2268 * Locking: output_lock to protect column state and space left
2269 * (note that the process_output*() functions take this
2270 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
Alan Cox4edf1822008-02-08 04:18:44 -08002272
Alan Cox11a96d12008-10-13 10:46:24 +01002273static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002274 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002277 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int c;
2279 ssize_t retval = 0;
2280
2281 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2282 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2283 retval = tty_check_change(tty);
2284 if (retval)
2285 return retval;
2286 }
2287
Peter Hurley9356b532013-06-15 09:14:24 -04002288 down_read(&tty->termios_rwsem);
2289
Joe Petersona88a69c2009-01-02 13:40:53 +00002290 /* Write out any echoed characters that are still pending */
2291 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 add_wait_queue(&tty->write_wait, &wait);
2294 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (signal_pending(current)) {
2296 retval = -ERESTARTSYS;
2297 break;
2298 }
2299 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2300 retval = -EIO;
2301 break;
2302 }
Peter Hurley582f5592013-05-17 12:49:48 -04002303 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002305 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (num < 0) {
2307 if (num == -EAGAIN)
2308 break;
2309 retval = num;
2310 goto break_out;
2311 }
2312 b += num;
2313 nr -= num;
2314 if (nr == 0)
2315 break;
2316 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002317 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 break;
2319 b++; nr--;
2320 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002321 if (tty->ops->flush_chars)
2322 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002324 struct n_tty_data *ldata = tty->disc_data;
2325
Roman Zippeld6afe272005-07-07 17:56:55 -07002326 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002327 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002328 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002329 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002330 if (c < 0) {
2331 retval = c;
2332 goto break_out;
2333 }
2334 if (!c)
2335 break;
2336 b += c;
2337 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 if (!nr)
2341 break;
2342 if (file->f_flags & O_NONBLOCK) {
2343 retval = -EAGAIN;
2344 break;
2345 }
Peter Hurley9356b532013-06-15 09:14:24 -04002346 up_read(&tty->termios_rwsem);
2347
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002348 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002349
2350 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 remove_wait_queue(&tty->write_wait, &wait);
Peter Hurley87108bc2016-01-09 21:45:14 -08002354 if (nr && tty->fasync)
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002355 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002356 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return (b - buf) ? b - buf : retval;
2358}
2359
2360/**
Alan Cox11a96d12008-10-13 10:46:24 +01002361 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * @tty: terminal device
2363 * @file: file accessing it
2364 * @wait: poll table
2365 *
2366 * Called when the line discipline is asked to poll() for data or
2367 * for special events. This code is not serialized with respect to
2368 * other events save open/close.
2369 *
2370 * This code must be sure never to sleep through a hangup.
2371 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
Alan Cox4edf1822008-02-08 04:18:44 -08002373
Alan Cox11a96d12008-10-13 10:46:24 +01002374static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002375 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 unsigned int mask = 0;
2378
2379 poll_wait(file, &tty->read_wait, wait);
2380 poll_wait(file, &tty->write_wait, wait);
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002381 if (input_available_p(tty, 1))
2382 mask |= POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002383 else {
2384 tty_buffer_flush_work(tty->port);
2385 if (input_available_p(tty, 1))
2386 mask |= POLLIN | POLLRDNORM;
2387 }
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002388 if (tty->packet && tty->link->ctrl_status)
2389 mask |= POLLPRI | POLLIN | POLLRDNORM;
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -08002390 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2391 mask |= POLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (tty_hung_up_p(file))
2393 mask |= POLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002394 if (tty->ops->write && !tty_is_writelocked(tty) &&
2395 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2396 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 mask |= POLLOUT | POLLWRNORM;
2398 return mask;
2399}
2400
Jiri Slaby57c94122012-10-18 22:26:43 +02002401static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002402{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002403 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002404
Peter Hurleya73d3d62013-06-15 09:14:25 -04002405 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002406 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002407 head = ldata->canon_head;
2408 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002409 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002410 /* Skip EOF-chars.. */
2411 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002412 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2413 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002414 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002415 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002416 }
2417 return nr;
2418}
2419
2420static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2421 unsigned int cmd, unsigned long arg)
2422{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002423 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002424 int retval;
2425
2426 switch (cmd) {
2427 case TIOCOUTQ:
2428 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2429 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002430 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002431 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002432 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002433 else
2434 retval = read_cnt(ldata);
2435 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002436 return put_user(retval, (unsigned int __user *) arg);
2437 default:
2438 return n_tty_ioctl_helper(tty, file, cmd, arg);
2439 }
2440}
2441
Peter Hurley27228732016-01-09 21:35:19 -08002442static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002443 .magic = TTY_LDISC_MAGIC,
2444 .name = "n_tty",
2445 .open = n_tty_open,
2446 .close = n_tty_close,
2447 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002448 .read = n_tty_read,
2449 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002450 .ioctl = n_tty_ioctl,
2451 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002452 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002453 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002454 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002455 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002457
2458/**
2459 * n_tty_inherit_ops - inherit N_TTY methods
2460 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2461 *
Peter Hurley27228732016-01-09 21:35:19 -08002462 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002463 */
2464
2465void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2466{
Peter Hurley27228732016-01-09 21:35:19 -08002467 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002468 ops->owner = NULL;
2469 ops->refcount = ops->flags = 0;
2470}
2471EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002472
2473void __init n_tty_init(void)
2474{
2475 tty_register_ldisc(N_TTY, &n_tty_ops);
2476}