blob: 90eca2605fbf43516f0dfbd37bc3d4b165d1ee89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
Alan Cox4edf1822008-02-08 04:18:44 -080011 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
Alan Cox4edf1822008-02-08 04:18:44 -080023 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010029 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070048#include <linux/audit.h>
49#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000050#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080051#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050052#include <linux/ratelimit.h>
Peter Hurley86e35ae2013-07-24 09:30:05 -040053#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* number of characters left in xmit buffer before select has we have room */
57#define WAKEUP_CHARS 256
58
59/*
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
63 */
64#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010065#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Joe Petersona88a69c2009-01-02 13:40:53 +000067/*
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
71 * codes.
72 */
73#define ECHO_OP_START 0xff
74#define ECHO_OP_MOVE_BACK_COL 0x80
75#define ECHO_OP_SET_CANON_COL 0x81
76#define ECHO_OP_ERASE_TAB 0x82
77
Peter Hurleycbfd0342013-06-15 10:04:26 -040078#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
Peter Hurley32f13522013-06-15 09:14:17 -040083#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
87# define n_tty_trace(f, args...)
88#endif
89
Jiri Slaby70ece7a2012-10-18 22:26:38 +020090struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040091 /* producer-published */
92 size_t read_head;
Peter Hurley70aca712015-01-16 15:05:37 -050093 size_t commit_head;
Peter Hurleyfb7aa032013-06-15 09:14:30 -040094 size_t canon_head;
Peter Hurley9dfd16d2013-06-15 10:04:29 -040095 size_t echo_head;
96 size_t echo_commit;
Peter Hurley1075a6e2013-12-09 18:06:07 -050097 size_t echo_mark;
Peter Hurley1bb9d562013-06-15 10:21:20 -040098 DECLARE_BITMAP(char_map, 256);
Peter Hurleyfb7aa032013-06-15 09:14:30 -040099
100 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200101 unsigned long overrun_time;
102 int num_overrun;
103
Peter Hurley24a89d12013-06-15 09:14:15 -0400104 /* non-atomic */
105 bool no_room;
106
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400107 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
Peter Hurley4d0ed182013-12-10 17:12:02 -0500109 unsigned char push:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200110
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400111 /* shared by producer and consumer */
Peter Hurley20bafb32013-06-15 10:21:19 -0400112 char read_buf[N_TTY_BUF_SIZE];
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Peter Hurley20bafb32013-06-15 10:21:19 -0400114 unsigned char echo_buf[N_TTY_BUF_SIZE];
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200115
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -0400116 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200117
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400118 /* consumer-published */
119 size_t read_tail;
Peter Hurley40d5e092013-06-15 10:21:17 -0400120 size_t line_start;
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400121
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400122 /* protected by output lock */
123 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200124 unsigned int canon_column;
Peter Hurley9dfd16d2013-06-15 10:04:29 -0400125 size_t echo_tail;
Jiri Slabybddc7152012-10-18 22:26:42 +0200126
127 struct mutex atomic_read_lock;
128 struct mutex output_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200129};
130
Peter Hurleyce741172013-06-15 09:14:20 -0400131static inline size_t read_cnt(struct n_tty_data *ldata)
132{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400133 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400134}
135
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400136static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
137{
138 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
139}
140
141static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
142{
143 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
144}
145
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400146static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
147{
148 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
149}
150
151static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
152{
153 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
154}
155
Miloslav Trmac522ed772007-07-15 23:40:56 -0700156static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
157 unsigned char __user *ptr)
158{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200159 struct n_tty_data *ldata = tty->disc_data;
160
161 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700162 return put_user(x, ptr);
163}
164
Peter Hurley679e7c22015-11-27 14:11:02 -0500165static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
166 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700167{
168 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500169 size_t size = N_TTY_BUF_SIZE - tail;
170 const void *from = read_buf_addr(ldata, tail);
171 int uncopied;
172
173 if (n > size) {
174 tty_audit_add_data(tty, from, size, ldata->icanon);
175 uncopied = copy_to_user(to, from, size);
176 if (uncopied)
177 return uncopied;
178 to += size;
179 n -= size;
180 from = ldata->read_buf;
181 }
Laura Abbott72586c62015-05-14 11:42:17 -0700182
Peter Hurley6b2a3d62015-11-08 08:52:31 -0500183 tty_audit_add_data(tty, from, n, ldata->icanon);
Laura Abbott72586c62015-05-14 11:42:17 -0700184 return copy_to_user(to, from, n);
185}
186
Peter Hurley24a89d12013-06-15 09:14:15 -0400187/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500188 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400189 * @tty: terminal
190 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500191 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400192 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400193 * Caller holds exclusive termios_rwsem
194 * or
195 * n_tty_read()/consumer path:
196 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400197 */
198
Peter Hurley2c5dc462015-01-16 15:05:34 -0500199static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400200{
Peter Hurley24a89d12013-06-15 09:14:15 -0400201 struct n_tty_data *ldata = tty->disc_data;
202
Peter Hurley2c5dc462015-01-16 15:05:34 -0500203 /* Did the input worker stop? Restart it */
204 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400205 ldata->no_room = 0;
206
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200207 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400208 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400209 /* see if ldisc has been killed - if so, this means that
210 * even though the ldisc has been halted and ->buf.work
211 * cancelled, ->buf.work is about to be rescheduled
212 */
213 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
214 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400215 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200216 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900217}
218
Peter Hurley9a4aec22013-06-15 09:14:32 -0400219static ssize_t chars_in_buffer(struct tty_struct *tty)
220{
221 struct n_tty_data *ldata = tty->disc_data;
222 ssize_t n = 0;
223
224 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500225 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400226 else
227 n = ldata->canon_head - ldata->read_tail;
228 return n;
229}
230
Peter Hurleyee0bab82013-06-15 09:14:34 -0400231/**
232 * n_tty_write_wakeup - asynchronous I/O notifier
233 * @tty: tty device
234 *
235 * Required for the ptys, serial driver etc. since processes
236 * that attach themselves to the master and rely on ASYNC
237 * IO must be woken up
238 */
239
240static void n_tty_write_wakeup(struct tty_struct *tty)
241{
242 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
243 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
244}
245
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400246static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400247{
Peter Hurleya3428462015-01-16 15:05:35 -0500248 struct n_tty_data *ldata = tty->disc_data;
249
Peter Hurley6367ca72013-06-15 09:14:33 -0400250 /*
251 * Check the remaining room for the input canonicalization
252 * mode. We don't want to throttle the driver if we're in
253 * canonical mode and don't have a newline yet!
254 */
Peter Hurleya3428462015-01-16 15:05:35 -0500255 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
256 return;
257
Peter Hurley6367ca72013-06-15 09:14:33 -0400258 while (1) {
259 int throttled;
260 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500261 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400262 break;
263 throttled = tty_throttle_safe(tty);
264 if (!throttled)
265 break;
266 }
267 __tty_set_flow_change(tty, 0);
268}
269
Peter Hurley4b293492013-07-24 08:29:55 -0400270static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400271{
Peter Hurley6d27a632016-01-10 22:40:56 -0800272 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400273 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
274 return;
275 if (!tty->count)
276 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500277 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800278 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400279 return;
280 }
281
Peter Hurley6367ca72013-06-15 09:14:33 -0400282 /* If there is enough space in the read buffer now, let the
283 * low-level driver know. We use chars_in_buffer() to
284 * check the buffer, as it now knows about canonical mode.
285 * Otherwise, if the driver is throttled and the line is
286 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
287 * we won't get any more characters.
288 */
289
290 while (1) {
291 int unthrottled;
292 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
293 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
294 break;
295 if (!tty->count)
296 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500297 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400298 unthrottled = tty_unthrottle_safe(tty);
299 if (!unthrottled)
300 break;
301 }
302 __tty_set_flow_change(tty, 0);
303}
304
Alan Cox17b82062008-10-13 10:45:06 +0100305/**
306 * put_tty_queue - add character to tty
307 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200308 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100309 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400310 * Add a character to the tty read_buf queue.
311 *
312 * n_tty_receive_buf()/producer path:
313 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100314 */
315
Peter Hurley19e2ad62013-07-24 08:29:54 -0400316static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100318 *read_buf_addr(ldata, ldata->read_head) = c;
319 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * reset_buffer_flags - reset buffer state
324 * @tty: terminal to reset
325 *
Peter Hurley25518c62013-03-11 16:44:31 -0400326 * Reset the read buffer counters and clear the flags.
327 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100328 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400329 * Locking: caller holds exclusive termios_rwsem
330 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000332
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400333static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400335 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley17bd7902013-06-15 10:04:24 -0400336 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500337 ldata->commit_head = 0;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500338 ldata->echo_mark = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400339 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000340
Peter Hurleya73d3d62013-06-15 09:14:25 -0400341 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200342 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500343 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Peter Hurleya30737a2013-03-11 16:44:22 -0400346static void n_tty_packet_mode_flush(struct tty_struct *tty)
347{
348 unsigned long flags;
349
Peter Hurleya30737a2013-03-11 16:44:22 -0400350 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400351 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400352 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400353 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000354 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400355 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
359 * n_tty_flush_buffer - clean input queue
360 * @tty: terminal device
361 *
Peter Hurley25518c62013-03-11 16:44:31 -0400362 * Flush the input buffer. Called when the tty layer wants the
363 * buffer flushed (eg at hangup) or when the N_TTY line discipline
364 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400366 * Holds termios_rwsem to exclude producer/consumer while
367 * buffer indices are reset.
368 *
369 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Alan Cox4edf1822008-02-08 04:18:44 -0800371
372static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400374 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400375 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500376 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800377
Peter Hurleya30737a2013-03-11 16:44:22 -0400378 if (tty->link)
379 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400380 up_write(&tty->termios_rwsem);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * is_utf8_continuation - utf8 multibyte check
385 * @c: byte to check
386 *
387 * Returns true if the utf8 character 'c' is a multibyte continuation
388 * character. We use this to correctly compute the on screen size
389 * of the character when printing
390 */
Alan Cox4edf1822008-02-08 04:18:44 -0800391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static inline int is_utf8_continuation(unsigned char c)
393{
394 return (c & 0xc0) == 0x80;
395}
396
397/**
398 * is_continuation - multibyte check
399 * @c: byte to check
400 *
401 * Returns true if the utf8 character 'c' is a multibyte continuation
402 * character and the terminal is in unicode mode.
403 */
Alan Cox4edf1822008-02-08 04:18:44 -0800404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static inline int is_continuation(unsigned char c, struct tty_struct *tty)
406{
407 return I_IUTF8(tty) && is_utf8_continuation(c);
408}
409
410/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000411 * do_output_char - output one character
412 * @c: character (or partial unicode symbol)
413 * @tty: terminal device
414 * @space: space available in tty driver write buffer
415 *
416 * This is a helper function that handles one output character
417 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600418 * doing OPOST processing and putting the results in the
419 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000420 *
421 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
422 * and NLDLY. They simply aren't relevant in the world today.
423 * If you ever need them, add them here.
424 *
425 * Returns the number of bytes of buffer space used or -1 if
426 * no space left.
427 *
428 * Locking: should be called under the output_lock to protect
429 * the column state and space left in the buffer
430 */
431
432static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
433{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200434 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000435 int spaces;
436
437 if (!space)
438 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000439
Joe Petersona88a69c2009-01-02 13:40:53 +0000440 switch (c) {
441 case '\n':
442 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200443 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000444 if (O_ONLCR(tty)) {
445 if (space < 2)
446 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200447 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700448 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000449 return 2;
450 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200451 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 break;
453 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200454 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000455 return 0;
456 if (O_OCRNL(tty)) {
457 c = '\n';
458 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200459 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000460 break;
461 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200462 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000463 break;
464 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200465 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000466 if (O_TABDLY(tty) == XTABS) {
467 if (space < spaces)
468 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200469 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000470 tty->ops->write(tty, " ", spaces);
471 return spaces;
472 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200473 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000474 break;
475 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200476 if (ldata->column > 0)
477 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000478 break;
479 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000480 if (!iscntrl(c)) {
481 if (O_OLCUC(tty))
482 c = toupper(c);
483 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200484 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000485 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000486 break;
487 }
488
489 tty_put_char(tty, c);
490 return 1;
491}
492
493/**
494 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * @c: character (or partial unicode symbol)
496 * @tty: terminal device
497 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600498 * Output one character with OPOST processing.
499 * Returns -1 when the output device is full and the character
500 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000502 * Locking: output_lock to protect column state and space left
503 * (also, this is called from n_tty_write under the
504 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Alan Cox4edf1822008-02-08 04:18:44 -0800506
Joe Petersona88a69c2009-01-02 13:40:53 +0000507static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jiri Slabybddc7152012-10-18 22:26:42 +0200509 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000510 int space, retval;
511
Jiri Slabybddc7152012-10-18 22:26:42 +0200512 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Alan Coxf34d7a52008-04-30 00:54:13 -0700514 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000515 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jiri Slabybddc7152012-10-18 22:26:42 +0200517 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000518 if (retval < 0)
519 return -1;
520 else
521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000525 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600527 * @buf: character buffer
528 * @nr: number of bytes to output
529 *
530 * Output a block of characters with OPOST processing.
531 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
533 * This path is used to speed up block console writes, among other
534 * things when processing blocks of output data. It handles only
535 * the simple cases normally found and helps to generate blocks of
536 * symbols for the console driver and thus improve performance.
537 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000538 * Locking: output_lock to protect column state and space left
539 * (also, this is called from n_tty_write under the
540 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Alan Cox4edf1822008-02-08 04:18:44 -0800542
Joe Petersona88a69c2009-01-02 13:40:53 +0000543static ssize_t process_output_block(struct tty_struct *tty,
544 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200546 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100548 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 const unsigned char *cp;
550
Jiri Slabybddc7152012-10-18 22:26:42 +0200551 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000552
Alan Coxf34d7a52008-04-30 00:54:13 -0700553 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000554 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200555 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (nr > space)
559 nr = space;
560
561 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000562 unsigned char c = *cp;
563
564 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 case '\n':
566 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200567 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (O_ONLCR(tty))
569 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200570 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200573 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto break_out;
575 if (O_OCRNL(tty))
576 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200577 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 case '\t':
580 goto break_out;
581 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200582 if (ldata->column > 0)
583 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000586 if (!iscntrl(c)) {
587 if (O_OLCUC(tty))
588 goto break_out;
589 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200590 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 }
594 }
595break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700596 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000597
Jiri Slabybddc7152012-10-18 22:26:42 +0200598 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return i;
600}
601
Joe Petersona88a69c2009-01-02 13:40:53 +0000602/**
603 * process_echoes - write pending echo characters
604 * @tty: terminal device
605 *
606 * Write previously buffered echo (and other ldisc-generated)
607 * characters to the tty.
608 *
609 * Characters generated by the ldisc (including echoes) need to
610 * be buffered because the driver's write buffer can fill during
611 * heavy program output. Echoing straight to the driver will
612 * often fail under these conditions, causing lost characters and
613 * resulting mismatches of ldisc state information.
614 *
615 * Since the ldisc state must represent the characters actually sent
616 * to the driver at the time of the write, operations like certain
617 * changes in column state are also saved in the buffer and executed
618 * here.
619 *
620 * A circular fifo buffer is used so that the most recent characters
621 * are prioritized. Also, when control characters are echoed with a
622 * prefixed "^", the pair is treated atomically and thus not separated.
623 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400624 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000625 */
626
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400627static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000628{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200629 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400630 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400631 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000632 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000633
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400634 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000635
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400636 tail = ldata->echo_tail;
Peter Hurley29c7c5c2013-06-15 10:04:28 -0400637 while (ldata->echo_commit != tail) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400638 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000639 if (c == ECHO_OP_START) {
640 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000641 int no_space_left = 0;
642
643 /*
644 * If the buffer byte is the start of a multi-byte
645 * operation, get the next byte, which is either the
646 * op code or a control character value.
647 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400648 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000649
Joe Petersona88a69c2009-01-02 13:40:53 +0000650 switch (op) {
651 unsigned int num_chars, num_bs;
652
653 case ECHO_OP_ERASE_TAB:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400654 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000655
656 /*
657 * Determine how many columns to go back
658 * in order to erase the tab.
659 * This depends on the number of columns
660 * used by other characters within the tab
661 * area. If this (modulo 8) count is from
662 * the start of input rather than from a
663 * previous tab, we offset by canon column.
664 * Otherwise, tab spacing is normal.
665 */
666 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200667 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000668 num_bs = 8 - (num_chars & 7);
669
670 if (num_bs > space) {
671 no_space_left = 1;
672 break;
673 }
674 space -= num_bs;
675 while (num_bs--) {
676 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200677 if (ldata->column > 0)
678 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000679 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400680 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 break;
682
683 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200684 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400685 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000686 break;
687
688 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200689 if (ldata->column > 0)
690 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400691 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000692 break;
693
694 case ECHO_OP_START:
695 /* This is an escaped echo op start code */
696 if (!space) {
697 no_space_left = 1;
698 break;
699 }
700 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200701 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000702 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400703 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 break;
705
706 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000707 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600708 * If the op is not a special byte code,
709 * it is a ctrl char tagged to be echoed
710 * as "^X" (where X is the letter
711 * representing the control char).
712 * Note that we must ensure there is
713 * enough space for the whole ctrl pair.
714 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000715 */
Joe Peterson62b26352009-09-09 15:03:47 -0600716 if (space < 2) {
717 no_space_left = 1;
718 break;
719 }
720 tty_put_char(tty, '^');
721 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200722 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600723 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400724 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000725 }
726
727 if (no_space_left)
728 break;
729 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400730 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600731 int retval = do_output_char(c, tty, space);
732 if (retval < 0)
733 break;
734 space -= retval;
735 } else {
736 if (!space)
737 break;
738 tty_put_char(tty, c);
739 space -= 1;
740 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400741 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000742 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000743 }
744
Peter Hurleycbfd0342013-06-15 10:04:26 -0400745 /* If the echo buffer is nearly full (so that the possibility exists
746 * of echo overrun before the next commit), then discard enough
747 * data at the tail to prevent a subsequent overrun */
748 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200749 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500750 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400751 tail += 3;
752 else
753 tail += 2;
754 } else
755 tail++;
756 }
757
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400758 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400759 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Peter Hurley17bd7902013-06-15 10:04:24 -0400762static void commit_echoes(struct tty_struct *tty)
763{
764 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400765 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400766 size_t head;
767
768 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500769 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400770 old = ldata->echo_commit - ldata->echo_tail;
771
772 /* Process committed echoes if the accumulated # of bytes
773 * is over the threshold (and try again each time another
774 * block is accumulated) */
775 nr = head - ldata->echo_tail;
776 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
777 return;
Peter Hurley17bd7902013-06-15 10:04:24 -0400778
Peter Hurley019ebdf2013-06-15 10:04:25 -0400779 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400780 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400781 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400782 mutex_unlock(&ldata->output_lock);
783
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400784 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400785 tty->ops->flush_chars(tty);
786}
787
788static void process_echoes(struct tty_struct *tty)
789{
790 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400791 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400792
Peter Hurleye2613be2014-02-11 16:34:55 -0500793 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400794 return;
795
796 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500797 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400798 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400799 mutex_unlock(&ldata->output_lock);
800
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400801 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400802 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400803}
804
Peter Hurley1075a6e2013-12-09 18:06:07 -0500805/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400806static void flush_echoes(struct tty_struct *tty)
807{
808 struct n_tty_data *ldata = tty->disc_data;
809
Peter Hurley39434ab2013-11-29 12:56:10 -0500810 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
811 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400812 return;
813
814 mutex_lock(&ldata->output_lock);
815 ldata->echo_commit = ldata->echo_head;
816 __process_echoes(tty);
817 mutex_unlock(&ldata->output_lock);
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000821 * add_echo_byte - add a byte to the echo buffer
822 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200823 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000824 *
825 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000826 */
827
Peter Hurleycbfd0342013-06-15 10:04:26 -0400828static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000829{
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400830 *echo_buf_addr(ldata, ldata->echo_head++) = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000831}
832
833/**
834 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200835 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 *
837 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 */
839
Jiri Slaby57c94122012-10-18 22:26:43 +0200840static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000841{
Jiri Slaby57c94122012-10-18 22:26:43 +0200842 add_echo_byte(ECHO_OP_START, ldata);
843 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000844}
845
846/**
847 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200848 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 *
850 * Add an operation to the echo buffer to set the canon column
851 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 */
853
Jiri Slaby57c94122012-10-18 22:26:43 +0200854static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000855{
Jiri Slaby57c94122012-10-18 22:26:43 +0200856 add_echo_byte(ECHO_OP_START, ldata);
857 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858}
859
860/**
861 * echo_erase_tab - add operation to erase a tab
862 * @num_chars: number of character columns already used
863 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000865 *
866 * Add an operation to the echo buffer to erase a tab.
867 *
868 * Called by the eraser function, which knows how many character
869 * columns have been used since either a previous tab or the start
870 * of input. This information will be used later, along with
871 * canon column (if applicable), to go back the correct number
872 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000873 */
874
875static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200876 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000877{
Jiri Slaby57c94122012-10-18 22:26:43 +0200878 add_echo_byte(ECHO_OP_START, ldata);
879 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000880
881 /* We only need to know this modulo 8 (tab spacing) */
882 num_chars &= 7;
883
884 /* Set the high bit as a flag if num_chars is after a previous tab */
885 if (after_tab)
886 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000887
Jiri Slaby57c94122012-10-18 22:26:43 +0200888 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000889}
890
891/**
892 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * @c: unicode byte to echo
894 * @tty: terminal device
895 *
Alan Cox4edf1822008-02-08 04:18:44 -0800896 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100898 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000899 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000900 */
901
Jiri Slaby57c94122012-10-18 22:26:43 +0200902static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000903{
Joe Petersona88a69c2009-01-02 13:40:53 +0000904 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200905 add_echo_byte(ECHO_OP_START, ldata);
906 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000907 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200908 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000909 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000910}
911
912/**
913 * echo_char - echo a character
914 * @c: unicode byte to echo
915 * @tty: terminal device
916 *
917 * Echo user input back onto the screen. This must be called only when
918 * L_ECHO(tty) is true. Called from the driver receive_buf path.
919 *
Joe Peterson62b26352009-09-09 15:03:47 -0600920 * This variant tags control characters to be echoed as "^X"
921 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923
924static void echo_char(unsigned char c, struct tty_struct *tty)
925{
Jiri Slabybddc7152012-10-18 22:26:42 +0200926 struct n_tty_data *ldata = tty->disc_data;
927
Joe Petersona88a69c2009-01-02 13:40:53 +0000928 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200929 add_echo_byte(ECHO_OP_START, ldata);
930 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000931 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600932 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200933 add_echo_byte(ECHO_OP_START, ldata);
934 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Alan Cox17b82062008-10-13 10:45:06 +0100938/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000939 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200940 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100941 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000942
Jiri Slaby57c94122012-10-18 22:26:43 +0200943static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200945 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200946 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200947 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949}
950
951/**
952 * eraser - handle erase function
953 * @c: character input
954 * @tty: terminal device
955 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200956 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * present in the stream from the driver layer. Handles the complexities
958 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100959 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400960 * n_tty_receive_buf()/producer path:
961 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
Alan Cox4edf1822008-02-08 04:18:44 -0800963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static void eraser(unsigned char c, struct tty_struct *tty)
965{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200966 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400968 size_t head;
969 size_t cnt;
970 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200972 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000973 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975 }
976 if (c == ERASE_CHAR(tty))
977 kill_type = ERASE;
978 else if (c == WERASE_CHAR(tty))
979 kill_type = WERASE;
980 else {
981 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200982 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return;
984 }
985 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200986 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200987 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 echo_char(KILL_CHAR(tty), tty);
989 /* Add a newline if ECHOK is on and ECHOKE is off. */
990 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200991 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return;
993 }
994 kill_type = KILL;
995 }
996
997 seen_alnums = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200998 while (ldata->read_head != ldata->canon_head) {
999 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* erase a single possibly multibyte character */
1002 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001003 head--;
1004 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001005 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /* do not partially erase */
1008 if (is_continuation(c, tty))
1009 break;
1010
1011 if (kill_type == WERASE) {
1012 /* Equivalent to BSD's ALTWERASE. */
1013 if (isalnum(c) || c == '_')
1014 seen_alnums++;
1015 else if (seen_alnums)
1016 break;
1017 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001018 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001019 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (L_ECHO(tty)) {
1021 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001022 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001023 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001024 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 /* if cnt > 1, output a multi-byte character */
1027 echo_char(c, tty);
1028 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001029 head++;
1030 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001031 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1034 echo_char(ERASE_CHAR(tty), tty);
1035 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001036 unsigned int num_chars = 0;
1037 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001038 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Joe Petersona88a69c2009-01-02 13:40:53 +00001040 /*
1041 * Count the columns used for characters
1042 * since the start of input or after a
1043 * previous tab.
1044 * This info is used to go back the correct
1045 * number of columns.
1046 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001047 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001048 tail--;
1049 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001050 if (c == '\t') {
1051 after_tab = 1;
1052 break;
Alan Cox300a6202009-01-02 13:41:04 +00001053 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001055 num_chars += 2;
1056 } else if (!is_continuation(c, tty)) {
1057 num_chars++;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001060 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else {
1062 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001063 echo_char_raw('\b', ldata);
1064 echo_char_raw(' ', ldata);
1065 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001068 echo_char_raw('\b', ldata);
1069 echo_char_raw(' ', ldata);
1070 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 }
1073 }
1074 if (kill_type == ERASE)
1075 break;
1076 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001077 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001078 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081/**
1082 * isig - handle the ISIG optio
1083 * @sig: signal
1084 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001086 * Called when a signal is being sent due to terminal input.
1087 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001088 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001089 * Performs input and output flush if !NOFLSH. In this context, the echo
1090 * buffer is 'output'. The signal is processed first to alert any current
1091 * readers or writers to discontinue and exit their i/o loops.
1092 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001093 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Alan Cox4edf1822008-02-08 04:18:44 -08001095
Peter Hurley3b19e032015-06-27 09:21:32 -04001096static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Peter Hurley8c985d12013-03-06 08:38:19 -05001098 struct pid *tty_pgrp = tty_get_pgrp(tty);
1099 if (tty_pgrp) {
1100 kill_pgrp(tty_pgrp, sig, 1);
1101 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001103}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001104
Peter Hurley3b19e032015-06-27 09:21:32 -04001105static void isig(int sig, struct tty_struct *tty)
1106{
1107 struct n_tty_data *ldata = tty->disc_data;
1108
1109 if (L_NOFLSH(tty)) {
1110 /* signal only */
1111 __isig(sig, tty);
1112
1113 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001114 up_read(&tty->termios_rwsem);
1115 down_write(&tty->termios_rwsem);
1116
Peter Hurley3b19e032015-06-27 09:21:32 -04001117 __isig(sig, tty);
1118
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001119 /* clear echo buffer */
1120 mutex_lock(&ldata->output_lock);
1121 ldata->echo_head = ldata->echo_tail = 0;
1122 ldata->echo_mark = ldata->echo_commit = 0;
1123 mutex_unlock(&ldata->output_lock);
1124
1125 /* clear output buffer */
1126 tty_driver_flush_buffer(tty);
1127
1128 /* clear input buffer */
1129 reset_buffer_flags(tty->disc_data);
1130
1131 /* notify pty master of flush */
1132 if (tty->link)
1133 n_tty_packet_mode_flush(tty);
1134
1135 up_write(&tty->termios_rwsem);
1136 down_read(&tty->termios_rwsem);
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140/**
1141 * n_tty_receive_break - handle break
1142 * @tty: terminal
1143 *
1144 * An RS232 break event has been hit in the incoming bitstream. This
1145 * can cause a variety of events depending upon the termios settings.
1146 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001147 * n_tty_receive_buf()/producer path:
1148 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001149 *
1150 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Alan Cox4edf1822008-02-08 04:18:44 -08001152
Peter Hurley4b293492013-07-24 08:29:55 -04001153static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Jiri Slaby57c94122012-10-18 22:26:43 +02001155 struct n_tty_data *ldata = tty->disc_data;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (I_IGNBRK(tty))
1158 return;
1159 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001160 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return;
1162 }
1163 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001164 put_tty_queue('\377', ldata);
1165 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001167 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/**
1171 * n_tty_receive_overrun - handle overrun reporting
1172 * @tty: terminal
1173 *
1174 * Data arrived faster than we could process it. While the tty
1175 * driver has flagged this the bits that were missed are gone
1176 * forever.
1177 *
1178 * Called from the receive_buf path so single threaded. Does not
1179 * need locking as num_overrun and overrun_time are function
1180 * private.
1181 */
Alan Cox4edf1822008-02-08 04:18:44 -08001182
Peter Hurley4b293492013-07-24 08:29:55 -04001183static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001185 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001187 ldata->num_overrun++;
1188 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1189 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001190 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001191 ldata->overrun_time = jiffies;
1192 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194}
1195
1196/**
1197 * n_tty_receive_parity_error - error notifier
1198 * @tty: terminal device
1199 * @c: character
1200 *
1201 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001202 * the error case if necessary.
1203 *
1204 * n_tty_receive_buf()/producer path:
1205 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Peter Hurley4b293492013-07-24 08:29:55 -04001207static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jiri Slaby57c94122012-10-18 22:26:43 +02001209 struct n_tty_data *ldata = tty->disc_data;
1210
Peter Hurley66528f92014-06-16 08:10:42 -04001211 if (I_INPCK(tty)) {
1212 if (I_IGNPAR(tty))
1213 return;
1214 if (I_PARMRK(tty)) {
1215 put_tty_queue('\377', ldata);
1216 put_tty_queue('\0', ldata);
1217 put_tty_queue(c, ldata);
1218 } else
1219 put_tty_queue('\0', ldata);
1220 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001221 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001224static void
1225n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1226{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001227 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001228 if (I_IXON(tty))
1229 start_tty(tty);
1230 if (L_ECHO(tty)) {
1231 echo_char(c, tty);
1232 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001233 } else
1234 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001235 return;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/**
1239 * n_tty_receive_char - perform processing
1240 * @tty: terminal device
1241 * @c: character
1242 *
1243 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001244 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001246 *
1247 * n_tty_receive_buf()/producer path:
1248 * caller holds non-exclusive termios_rwsem
1249 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001250 *
1251 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253
Peter Hurleye60d27c2013-07-24 08:29:56 -04001254static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001255n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001257 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (I_IXON(tty)) {
1260 if (c == START_CHAR(tty)) {
1261 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001262 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 if (c == STOP_CHAR(tty)) {
1266 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
Joe Peterson575537b32008-04-30 00:53:30 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001272 if (c == INTR_CHAR(tty)) {
1273 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001274 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001275 } else if (c == QUIT_CHAR(tty)) {
1276 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001277 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001278 } else if (c == SUSP_CHAR(tty)) {
1279 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 }
Joe Peterson575537b32008-04-30 00:53:30 -07001283
Peter Hurley855df3c2013-07-24 08:29:50 -04001284 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1285 start_tty(tty);
1286 process_echoes(tty);
1287 }
1288
Joe Peterson575537b32008-04-30 00:53:30 -07001289 if (c == '\r') {
1290 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001291 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001292 if (I_ICRNL(tty))
1293 c = '\n';
1294 } else if (c == '\n' && I_INLCR(tty))
1295 c = '\r';
1296
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001297 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1299 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1300 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001301 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001305 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001307 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001309 echo_char_raw('^', ldata);
1310 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001311 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001314 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001316 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001317 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jiri Slaby57c94122012-10-18 22:26:43 +02001319 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001321 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001322 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001323 echo_char(read_buf(ldata, tail), tty);
1324 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001326 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001330 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001331 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001332 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 goto handle_newline;
1335 }
1336 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 c = __DISABLED_CHAR;
1338 goto handle_newline;
1339 }
1340 if ((c == EOL_CHAR(tty)) ||
1341 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1342 /*
1343 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1344 */
1345 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001347 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001348 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001350 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 /*
1353 * XXX does PARMRK doubling happen for
1354 * EOL_CHAR and EOL2_CHAR?
1355 */
Peter Hurley001ba922013-12-02 14:24:44 -05001356 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001357 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Alan Cox4edf1822008-02-08 04:18:44 -08001359handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001360 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001361 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001362 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001364 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 }
Alan Cox4edf1822008-02-08 04:18:44 -08001368
Joe Petersonacc71bb2009-01-02 13:43:32 +00001369 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001370 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001372 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 else {
1374 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001375 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001376 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 echo_char(c, tty);
1378 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001379 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Peter Hurley001ba922013-12-02 14:24:44 -05001382 /* PARMRK doubling check */
1383 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001384 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Jiri Slaby57c94122012-10-18 22:26:43 +02001386 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001387 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Peter Hurleye60d27c2013-07-24 08:29:56 -04001390static inline void
1391n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001392{
1393 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001394
Peter Hurleye60d27c2013-07-24 08:29:56 -04001395 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1396 start_tty(tty);
1397 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001398 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001399 if (L_ECHO(tty)) {
1400 finish_erasing(ldata);
1401 /* Record the column of first canon char. */
1402 if (ldata->canon_head == ldata->read_head)
1403 echo_set_canon_col(ldata);
1404 echo_char(c, tty);
1405 commit_echoes(tty);
1406 }
Peter Hurley001ba922013-12-02 14:24:44 -05001407 /* PARMRK doubling check */
1408 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001409 put_tty_queue(c, ldata);
1410 put_tty_queue(c, ldata);
1411}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001412
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001413static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001414{
1415 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001416}
1417
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001418static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001419n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1420{
1421 struct n_tty_data *ldata = tty->disc_data;
1422
Peter Hurleye60d27c2013-07-24 08:29:56 -04001423 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1424 start_tty(tty);
1425 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001426 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001427 if (L_ECHO(tty)) {
1428 finish_erasing(ldata);
1429 /* Record the column of first canon char. */
1430 if (ldata->canon_head == ldata->read_head)
1431 echo_set_canon_col(ldata);
1432 echo_char(c, tty);
1433 commit_echoes(tty);
1434 }
1435 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001436}
1437
Peter Hurley8dc4b252013-12-02 14:24:43 -05001438static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001439{
1440 if (I_ISTRIP(tty))
1441 c &= 0x7f;
1442 if (I_IUCLC(tty) && L_IEXTEN(tty))
1443 c = tolower(c);
1444
1445 if (I_IXON(tty)) {
1446 if (c == STOP_CHAR(tty))
1447 stop_tty(tty);
1448 else if (c == START_CHAR(tty) ||
1449 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1450 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1451 c != SUSP_CHAR(tty))) {
1452 start_tty(tty);
1453 process_echoes(tty);
1454 }
1455 }
1456}
1457
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001458static void
1459n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1460{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001461 switch (flag) {
1462 case TTY_BREAK:
1463 n_tty_receive_break(tty);
1464 break;
1465 case TTY_PARITY:
1466 case TTY_FRAME:
1467 n_tty_receive_parity_error(tty, c);
1468 break;
1469 case TTY_OVERRUN:
1470 n_tty_receive_overrun(tty);
1471 break;
1472 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001473 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001474 break;
1475 }
1476}
1477
Peter Hurleye60d27c2013-07-24 08:29:56 -04001478static void
1479n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1480{
1481 struct n_tty_data *ldata = tty->disc_data;
1482
1483 ldata->lnext = 0;
1484 if (likely(flag == TTY_NORMAL)) {
1485 if (I_ISTRIP(tty))
1486 c &= 0x7f;
1487 if (I_IUCLC(tty) && L_IEXTEN(tty))
1488 c = tolower(c);
1489 n_tty_receive_char(tty, c);
1490 } else
1491 n_tty_receive_char_flagged(tty, c, flag);
1492}
1493
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001494static void
1495n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1496 char *fp, int count)
1497{
1498 struct n_tty_data *ldata = tty->disc_data;
1499 size_t n, head;
1500
1501 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001502 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001503 memcpy(read_buf_addr(ldata, head), cp, n);
1504 ldata->read_head += n;
1505 cp += n;
1506 count -= n;
1507
1508 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001509 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001510 memcpy(read_buf_addr(ldata, head), cp, n);
1511 ldata->read_head += n;
1512}
1513
Peter Hurley554117b2013-06-15 10:21:25 -04001514static void
1515n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1516 char *fp, int count)
1517{
1518 struct n_tty_data *ldata = tty->disc_data;
1519 char flag = TTY_NORMAL;
1520
1521 while (count--) {
1522 if (fp)
1523 flag = *fp++;
1524 if (likely(flag == TTY_NORMAL))
1525 put_tty_queue(*cp++, ldata);
1526 else
1527 n_tty_receive_char_flagged(tty, *cp++, flag);
1528 }
1529}
1530
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001531static void
1532n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1533 char *fp, int count)
1534{
1535 char flag = TTY_NORMAL;
1536
1537 while (count--) {
1538 if (fp)
1539 flag = *fp++;
1540 if (likely(flag == TTY_NORMAL))
1541 n_tty_receive_char_closing(tty, *cp++);
1542 else
1543 n_tty_receive_char_flagged(tty, *cp++, flag);
1544 }
1545}
1546
Peter Hurley7d88d632013-07-24 08:29:49 -04001547static void
1548n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001549 char *fp, int count)
1550{
1551 struct n_tty_data *ldata = tty->disc_data;
1552 char flag = TTY_NORMAL;
1553
1554 while (count--) {
1555 if (fp)
1556 flag = *fp++;
1557 if (likely(flag == TTY_NORMAL)) {
1558 unsigned char c = *cp++;
1559
1560 if (I_ISTRIP(tty))
1561 c &= 0x7f;
1562 if (I_IUCLC(tty) && L_IEXTEN(tty))
1563 c = tolower(c);
1564 if (L_EXTPROC(tty)) {
1565 put_tty_queue(c, ldata);
1566 continue;
1567 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001568 if (!test_bit(c, ldata->char_map))
1569 n_tty_receive_char_inline(tty, c);
1570 else if (n_tty_receive_char_special(tty, c) && count) {
1571 if (fp)
1572 flag = *fp++;
1573 n_tty_receive_char_lnext(tty, *cp++, flag);
1574 count--;
1575 }
Peter Hurley6baad002013-07-24 08:29:52 -04001576 } else
1577 n_tty_receive_char_flagged(tty, *cp++, flag);
1578 }
1579}
1580
1581static void
1582n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1583 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001584{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001585 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001586 char flag = TTY_NORMAL;
1587
1588 while (count--) {
1589 if (fp)
1590 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001591 if (likely(flag == TTY_NORMAL)) {
1592 unsigned char c = *cp++;
1593
1594 if (!test_bit(c, ldata->char_map))
1595 n_tty_receive_char_fast(tty, c);
1596 else if (n_tty_receive_char_special(tty, c) && count) {
1597 if (fp)
1598 flag = *fp++;
1599 n_tty_receive_char_lnext(tty, *cp++, flag);
1600 count--;
1601 }
1602 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001603 n_tty_receive_char_flagged(tty, *cp++, flag);
1604 }
1605}
1606
Peter Hurley24a89d12013-06-15 09:14:15 -04001607static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1608 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001610 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001611 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001613 if (ldata->real_raw)
1614 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001615 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001616 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001617 else if (tty->closing && !L_EXTPROC(tty))
1618 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001619 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001620 if (ldata->lnext) {
1621 char flag = TTY_NORMAL;
1622
1623 if (fp)
1624 flag = *fp++;
1625 n_tty_receive_char_lnext(tty, *cp++, flag);
1626 count--;
1627 }
1628
Peter Hurley7de971b2013-07-24 08:29:53 -04001629 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001630 n_tty_receive_buf_fast(tty, cp, fp, count);
1631 else
1632 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001633
1634 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001635 if (tty->ops->flush_chars)
1636 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
Peter Hurley70aca712015-01-16 15:05:37 -05001639 if (ldata->icanon && !L_EXTPROC(tty))
1640 return;
1641
1642 /* publish read_head to consumer */
1643 smp_store_release(&ldata->commit_head, ldata->read_head);
1644
1645 if ((read_cnt(ldata) >= ldata->minimum_to_wake) || L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001647 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001651/**
1652 * n_tty_receive_buf_common - process input
1653 * @tty: device to receive input
1654 * @cp: input chars
1655 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1656 * @count: number of input chars in @cp
1657 *
1658 * Called by the terminal driver when a block of characters has
1659 * been received. This function must be called from soft contexts
1660 * not from interrupt context. The driver is responsible for making
1661 * calls one at a time and in order (or using flush_to_ldisc)
1662 *
1663 * Returns the # of input chars from @cp which were processed.
1664 *
1665 * In canonical mode, the maximum line length is 4096 chars (including
1666 * the line termination char); lines longer than 4096 chars are
1667 * truncated. After 4095 chars, input data is still processed but
1668 * not stored. Overflow processing ensures the tty can always
1669 * receive more input until at least one line can be read.
1670 *
1671 * In non-canonical mode, the read buffer will only accept 4095 chars;
1672 * this provides the necessary space for a newline char if the input
1673 * mode is switched to canonical.
1674 *
1675 * Note it is possible for the read buffer to _contain_ 4096 chars
1676 * in non-canonical mode: the read buffer could already contain the
1677 * maximum canon line of 4096 chars when the mode is switched to
1678 * non-canonical.
1679 *
1680 * n_tty_receive_buf()/producer path:
1681 * claims non-exclusive termios_rwsem
1682 * publishes commit_head or canon_head
1683 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001684static int
1685n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1686 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001687{
1688 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001689 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001690
Peter Hurley9356b532013-06-15 09:14:24 -04001691 down_read(&tty->termios_rwsem);
1692
Peter Hurley19e2ad62013-07-24 08:29:54 -04001693 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001694 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001695 * When PARMRK is set, each input char may take up to 3 chars
1696 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001697 *
1698 * If we are doing input canonicalization, and there are no
1699 * pending newlines, let characters through without limit, so
1700 * that erase characters will be handled. Other excess
1701 * characters will be beeped.
1702 *
1703 * paired with store in *_copy_from_read_buf() -- guarantees
1704 * the consumer has loaded the data in read_buf up to the new
1705 * read_tail (so this producer will not overwrite unread data)
1706 */
1707 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001708
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001709 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001710 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001711 room = (room + 2) / 3;
1712 room--;
1713 if (room <= 0) {
1714 overflow = ldata->icanon && ldata->canon_head == tail;
1715 if (overflow && room < 0)
1716 ldata->read_head--;
1717 room = overflow;
1718 ldata->no_room = flow && !room;
1719 } else
1720 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001721
Peter Hurley19e2ad62013-07-24 08:29:54 -04001722 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001723 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001724 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001725
1726 /* ignore parity errors if handling overflow */
1727 if (!overflow || !fp || *fp != TTY_PARITY)
1728 __receive_buf(tty, cp, fp, n);
1729
Peter Hurley19e2ad62013-07-24 08:29:54 -04001730 cp += n;
1731 if (fp)
1732 fp += n;
1733 count -= n;
1734 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001735 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001736
Peter Hurley19e2ad62013-07-24 08:29:54 -04001737 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001738
1739 /* Unthrottle if handling overflow on pty */
1740 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1741 if (overflow) {
1742 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1743 tty_unthrottle_safe(tty);
1744 __tty_set_flow_change(tty, 0);
1745 }
1746 } else
1747 n_tty_check_throttle(tty);
1748
Peter Hurley9356b532013-06-15 09:14:24 -04001749 up_read(&tty->termios_rwsem);
1750
Peter Hurley19e2ad62013-07-24 08:29:54 -04001751 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001752}
1753
Peter Hurley5c32d122013-12-02 14:24:41 -05001754static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1755 char *fp, int count)
1756{
1757 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1758}
1759
1760static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1761 char *fp, int count)
1762{
1763 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766int is_ignored(int sig)
1767{
1768 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001769 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772/**
1773 * n_tty_set_termios - termios data changed
1774 * @tty: terminal
1775 * @old: previous data
1776 *
1777 * Called by the tty layer when the user changes termios flags so
1778 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001779 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * guaranteed that this function will not be re-entered or in progress
1781 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001782 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001783 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Alan Cox4edf1822008-02-08 04:18:44 -08001785
1786static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001788 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001789
Peter Hurleyc786f742013-09-17 12:53:35 -04001790 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001791 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001792 ldata->line_start = ldata->read_tail;
1793 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1794 ldata->canon_head = ldata->read_tail;
1795 ldata->push = 0;
1796 } else {
1797 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1798 ldata->read_flags);
1799 ldata->canon_head = ldata->read_head;
1800 ldata->push = 1;
1801 }
Peter Hurley70aca712015-01-16 15:05:37 -05001802 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001803 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001804 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001805 }
1806
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001807 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1810 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1811 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1812 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001813 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001816 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001818 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001821 set_bit(ERASE_CHAR(tty), ldata->char_map);
1822 set_bit(KILL_CHAR(tty), ldata->char_map);
1823 set_bit(EOF_CHAR(tty), ldata->char_map);
1824 set_bit('\n', ldata->char_map);
1825 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001827 set_bit(WERASE_CHAR(tty), ldata->char_map);
1828 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1829 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (L_ECHO(tty))
1831 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001832 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 }
1835 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001836 set_bit(START_CHAR(tty), ldata->char_map);
1837 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001840 set_bit(INTR_CHAR(tty), ldata->char_map);
1841 set_bit(QUIT_CHAR(tty), ldata->char_map);
1842 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001844 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001845 ldata->raw = 0;
1846 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001848 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1850 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1851 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001852 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001854 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001856 /*
1857 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1858 * been stopped by STOP_CHAR(tty) before it.
1859 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001860 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001861 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001862 process_echoes(tty);
1863 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001864
Alan Coxf34d7a52008-04-30 00:54:13 -07001865 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001866 wake_up_interruptible(&tty->write_wait);
1867 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/**
1871 * n_tty_close - close the ldisc for this tty
1872 * @tty: device
1873 *
Alan Cox4edf1822008-02-08 04:18:44 -08001874 * Called from the terminal layer when this line discipline is
1875 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * discipline change. The function will not be called while other
1877 * ldisc methods are in progress.
1878 */
Alan Cox4edf1822008-02-08 04:18:44 -08001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static void n_tty_close(struct tty_struct *tty)
1881{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001882 struct n_tty_data *ldata = tty->disc_data;
1883
Peter Hurley79901312013-03-11 16:44:23 -04001884 if (tty->link)
1885 n_tty_packet_mode_flush(tty);
1886
Peter Hurley20bafb32013-06-15 10:21:19 -04001887 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001888 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
1891/**
1892 * n_tty_open - open an ldisc
1893 * @tty: terminal to open
1894 *
Alan Cox4edf1822008-02-08 04:18:44 -08001895 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 * terminal device. Can sleep. Called serialized so that no
1897 * other events will occur in parallel. No further open will occur
1898 * until a close.
1899 */
1900
1901static int n_tty_open(struct tty_struct *tty)
1902{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001903 struct n_tty_data *ldata;
1904
Peter Hurley20bafb32013-06-15 10:21:19 -04001905 /* Currently a malloc failure here can panic */
1906 ldata = vmalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001907 if (!ldata)
1908 goto err;
1909
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001910 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001911 mutex_init(&ldata->atomic_read_lock);
1912 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001913
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001914 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001915 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001916 ldata->column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001917 ldata->canon_column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001918 ldata->minimum_to_wake = 1;
Peter Hurley20bafb32013-06-15 10:21:19 -04001919 ldata->num_overrun = 0;
1920 ldata->no_room = 0;
1921 ldata->lnext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001923 /* indicate buffer work may resume */
1924 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1925 n_tty_set_termios(tty, NULL);
1926 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001929err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001930 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Peter Hurleyeafbe672013-12-02 14:24:45 -05001933static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001935 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001936 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001937
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001938 if (ldata->icanon && !L_EXTPROC(tty))
1939 return ldata->canon_head != ldata->read_tail;
1940 else
Peter Hurley70aca712015-01-16 15:05:37 -05001941 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Peter Hurley1a486322015-04-13 13:24:34 -04001944static inline int check_other_done(struct tty_struct *tty)
1945{
1946 int done = test_bit(TTY_OTHER_DONE, &tty->flags);
1947 if (done) {
1948 /* paired with cmpxchg() in check_other_closed(); ensures
1949 * read buffer head index is not stale
1950 */
1951 smp_mb__after_atomic();
1952 }
1953 return done;
1954}
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001957 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * @tty: terminal device
1959 * @b: user data
1960 * @nr: size of data
1961 *
Alan Cox11a96d12008-10-13 10:46:24 +01001962 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * ICANON is off; it copies characters straight from the tty queue to
1964 * user space directly. It can be profitably called twice; once to
1965 * drain the space from the tail pointer to the (physical) end of the
1966 * buffer, and once to drain the space from the (physical) beginning of
1967 * the buffer to head pointer.
1968 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001969 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001971 * n_tty_read()/consumer path:
1972 * caller holds non-exclusive termios_rwsem
1973 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Alan Cox4edf1822008-02-08 04:18:44 -08001975
Alan Cox33f0f882006-01-09 20:54:13 -08001976static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 unsigned char __user **b,
1978 size_t *nr)
1979
1980{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001981 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 int retval;
1983 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001984 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001985 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001986 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001989 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001992 const unsigned char *from = read_buf_addr(ldata, tail);
1993 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001995 is_eof = n == 1 && *from == EOF_CHAR(tty);
1996 tty_audit_add_data(tty, from, n, ldata->icanon);
Peter Hurley70aca712015-01-16 15:05:37 -05001997 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001998 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05001999 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2000 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02002001 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 *b += n;
2003 *nr -= n;
2004 }
2005 return retval;
2006}
2007
Peter Hurley88bb0de2013-06-15 09:14:16 -04002008/**
Peter Hurley32f13522013-06-15 09:14:17 -04002009 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04002010 * @tty: terminal device
2011 * @b: user data
2012 * @nr: size of data
2013 *
2014 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04002015 * it copies one line of input up to and including the line-delimiting
2016 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04002017 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05002018 * NB: When termios is changed from non-canonical to canonical mode and
2019 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2020 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2021 * This causes data already processed as input to be immediately available
2022 * as input although a newline has not been received.
2023 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04002024 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04002025 *
2026 * n_tty_read()/consumer path:
2027 * caller holds non-exclusive termios_rwsem
2028 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002029 */
2030
Peter Hurley32f13522013-06-15 09:14:17 -04002031static int canon_copy_from_read_buf(struct tty_struct *tty,
2032 unsigned char __user **b,
2033 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002034{
2035 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002036 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002037 size_t eol;
2038 size_t tail;
2039 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002040
2041 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002042 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002043 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002044
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002045 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2046
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002047 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002048 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2049
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002050 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002051 __func__, *nr, tail, n, size);
2052
2053 eol = find_next_bit(ldata->read_flags, size, tail);
2054 more = n - (size - tail);
2055 if (eol == N_TTY_BUF_SIZE && more) {
2056 /* scan wrapped without finding set bit */
2057 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002058 found = eol != more;
2059 } else
2060 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002061
Peter Hurleyc77569d2013-11-22 07:16:25 -05002062 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002063 if (n > N_TTY_BUF_SIZE)
2064 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002065 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002066
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002067 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2068 c = min(*nr, c);
2069 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002070 }
Peter Hurley32f13522013-06-15 09:14:17 -04002071
Peter Hurley679e7c22015-11-27 14:11:02 -05002072 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2073 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002074
Peter Hurley679e7c22015-11-27 14:11:02 -05002075 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002076 if (ret)
2077 return -EFAULT;
2078 *b += n;
2079 *nr -= n;
2080
Peter Hurleya73d3d62013-06-15 09:14:25 -04002081 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002082 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002083 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002084
Peter Hurley40d5e092013-06-15 10:21:17 -04002085 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002086 if (!ldata->push)
2087 ldata->line_start = ldata->read_tail;
2088 else
2089 ldata->push = 0;
Peter Hurley32f13522013-06-15 09:14:17 -04002090 tty_audit_push(tty);
Peter Hurley40d5e092013-06-15 10:21:17 -04002091 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002092 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002093}
2094
Al Virocc4191d2008-03-29 03:08:48 +00002095extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002096 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098/**
2099 * job_control - check job control
2100 * @tty: tty
2101 * @file: file handle
2102 *
2103 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002104 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002106 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002107 * Locking: redirected write test is safe
2108 * current->signal->tty check is safe
2109 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
Alan Cox4edf1822008-02-08 04:18:44 -08002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112static int job_control(struct tty_struct *tty, struct file *file)
2113{
2114 /* Job control check -- must be done at start and after
2115 every sleep (POSIX.1 7.1.1.4). */
2116 /* NOTE: not yet done after every sleep pending a thorough
2117 check of the logic of this change. -- jlc */
2118 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002119 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002120 return 0;
2121
Peter Hurley2812d9e2015-10-10 20:28:42 -04002122 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
Alan Cox4edf1822008-02-08 04:18:44 -08002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126/**
Alan Cox11a96d12008-10-13 10:46:24 +01002127 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 * @tty: tty device
2129 * @file: file object
2130 * @buf: userspace buffer pointer
2131 * @nr: size of I/O
2132 *
2133 * Perform reads for the line discipline. We are guaranteed that the
2134 * line discipline will not be closed under us but we may get multiple
2135 * parallel readers and must handle this ourselves. We may also get
2136 * a hangup. Always called in user context, may sleep.
2137 *
2138 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002139 *
2140 * n_tty_read()/consumer path:
2141 * claims non-exclusive termios_rwsem
2142 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 */
Alan Cox4edf1822008-02-08 04:18:44 -08002144
Alan Cox11a96d12008-10-13 10:46:24 +01002145static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 unsigned char __user *buf, size_t nr)
2147{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002148 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002150 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Peter Hurley1a486322015-04-13 13:24:34 -04002151 int c, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 int minimum, time;
2153 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002155 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002156 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002159 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002161
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002162 /*
2163 * Internal serialization of reads.
2164 */
2165 if (file->f_flags & O_NONBLOCK) {
2166 if (!mutex_trylock(&ldata->atomic_read_lock))
2167 return -EAGAIN;
2168 } else {
2169 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2170 return -ERESTARTSYS;
2171 }
2172
Peter Hurley9356b532013-06-15 09:14:24 -04002173 down_read(&tty->termios_rwsem);
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 minimum = time = 0;
2176 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002177 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 minimum = MIN_CHAR(tty);
2179 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002180 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002182 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002184 (ldata->minimum_to_wake > minimum))
2185 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002187 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002188 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190 }
2191
Alan Cox04f378b2008-04-30 00:53:29 -07002192 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002193 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 while (nr) {
2197 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002198 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 unsigned char cs;
2200 if (b != buf)
2201 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002202 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 cs = tty->link->ctrl_status;
2204 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002205 spin_unlock_irq(&tty->link->ctrl_lock);
Miloslav Trmac522ed772007-07-15 23:40:56 -07002206 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 retval = -EFAULT;
2208 b--;
2209 break;
2210 }
2211 nr--;
2212 break;
2213 }
Alan Cox4edf1822008-02-08 04:18:44 -08002214
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002215 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002217 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08002218
Peter Hurley1a486322015-04-13 13:24:34 -04002219 done = check_other_done(tty);
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (!input_available_p(tty, 0)) {
Peter Hurley1a486322015-04-13 13:24:34 -04002222 if (done) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002223 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 break;
2225 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002226 if (tty_hung_up_p(file))
2227 break;
2228 if (!timeout)
2229 break;
2230 if (file->f_flags & O_NONBLOCK) {
2231 retval = -EAGAIN;
2232 break;
2233 }
2234 if (signal_pending(current)) {
2235 retval = -ERESTARTSYS;
2236 break;
2237 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002238 up_read(&tty->termios_rwsem);
2239
Linus Torvalds37da7bb2014-12-14 15:23:32 -08002240 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2241 timeout);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002242
2243 down_read(&tty->termios_rwsem);
2244 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002247 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002248 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002249 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
2251 } else {
2252 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002253
2254 /* Deal with packet mode. */
2255 if (packet && b == buf) {
2256 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
2257 retval = -EFAULT;
2258 b--;
2259 break;
2260 }
2261 nr--;
2262 }
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 uncopied = copy_from_read_buf(tty, &b, &nr);
2265 uncopied += copy_from_read_buf(tty, &b, &nr);
2266 if (uncopied) {
2267 retval = -EFAULT;
2268 break;
2269 }
2270 }
2271
Peter Hurley6367ca72013-06-15 09:14:33 -04002272 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 if (b - buf >= minimum)
2275 break;
2276 if (time)
2277 timeout = time;
2278 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002279 if (tail != ldata->read_tail)
2280 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002281 up_read(&tty->termios_rwsem);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 remove_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002285 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Peter Hurleyaebf04532013-11-07 14:01:57 -05002287 mutex_unlock(&ldata->atomic_read_lock);
2288
Peter Hurley40d5e092013-06-15 10:21:17 -04002289 if (b - buf)
2290 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 return retval;
2293}
2294
2295/**
Alan Cox11a96d12008-10-13 10:46:24 +01002296 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * @tty: tty device
2298 * @file: file object
2299 * @buf: userspace buffer pointer
2300 * @nr: size of I/O
2301 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002302 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002304 * and other such events. Since the receive code will echo characters,
2305 * thus calling driver write methods, the output_lock is used in
2306 * the output processing functions called here as well as in the
2307 * echo processing function to protect the column state and space
2308 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 *
2310 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002311 *
2312 * Locking: output_lock to protect column state and space left
2313 * (note that the process_output*() functions take this
2314 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
Alan Cox4edf1822008-02-08 04:18:44 -08002316
Alan Cox11a96d12008-10-13 10:46:24 +01002317static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002318 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002321 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int c;
2323 ssize_t retval = 0;
2324
2325 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2326 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2327 retval = tty_check_change(tty);
2328 if (retval)
2329 return retval;
2330 }
2331
Peter Hurley9356b532013-06-15 09:14:24 -04002332 down_read(&tty->termios_rwsem);
2333
Joe Petersona88a69c2009-01-02 13:40:53 +00002334 /* Write out any echoed characters that are still pending */
2335 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 add_wait_queue(&tty->write_wait, &wait);
2338 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (signal_pending(current)) {
2340 retval = -ERESTARTSYS;
2341 break;
2342 }
2343 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2344 retval = -EIO;
2345 break;
2346 }
Peter Hurley582f5592013-05-17 12:49:48 -04002347 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002349 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (num < 0) {
2351 if (num == -EAGAIN)
2352 break;
2353 retval = num;
2354 goto break_out;
2355 }
2356 b += num;
2357 nr -= num;
2358 if (nr == 0)
2359 break;
2360 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002361 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363 b++; nr--;
2364 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002365 if (tty->ops->flush_chars)
2366 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002368 struct n_tty_data *ldata = tty->disc_data;
2369
Roman Zippeld6afe272005-07-07 17:56:55 -07002370 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002371 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002372 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002373 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002374 if (c < 0) {
2375 retval = c;
2376 goto break_out;
2377 }
2378 if (!c)
2379 break;
2380 b += c;
2381 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 if (!nr)
2385 break;
2386 if (file->f_flags & O_NONBLOCK) {
2387 retval = -EAGAIN;
2388 break;
2389 }
Peter Hurley9356b532013-06-15 09:14:24 -04002390 up_read(&tty->termios_rwsem);
2391
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002392 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002393
2394 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002398 if (b - buf != nr && tty->fasync)
2399 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002400 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return (b - buf) ? b - buf : retval;
2402}
2403
2404/**
Alan Cox11a96d12008-10-13 10:46:24 +01002405 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 * @tty: terminal device
2407 * @file: file accessing it
2408 * @wait: poll table
2409 *
2410 * Called when the line discipline is asked to poll() for data or
2411 * for special events. This code is not serialized with respect to
2412 * other events save open/close.
2413 *
2414 * This code must be sure never to sleep through a hangup.
2415 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 */
Alan Cox4edf1822008-02-08 04:18:44 -08002417
Alan Cox11a96d12008-10-13 10:46:24 +01002418static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002419 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002421 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 unsigned int mask = 0;
2423
2424 poll_wait(file, &tty->read_wait, wait);
2425 poll_wait(file, &tty->write_wait, wait);
Peter Hurley1a486322015-04-13 13:24:34 -04002426 if (check_other_done(tty))
2427 mask |= POLLHUP;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002428 if (input_available_p(tty, 1))
2429 mask |= POLLIN | POLLRDNORM;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002430 if (tty->packet && tty->link->ctrl_status)
2431 mask |= POLLPRI | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (tty_hung_up_p(file))
2433 mask |= POLLHUP;
2434 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2435 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002436 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002438 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002440 if (tty->ops->write && !tty_is_writelocked(tty) &&
2441 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2442 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 mask |= POLLOUT | POLLWRNORM;
2444 return mask;
2445}
2446
Jiri Slaby57c94122012-10-18 22:26:43 +02002447static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002448{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002449 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002450
Peter Hurleya73d3d62013-06-15 09:14:25 -04002451 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002452 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002453 head = ldata->canon_head;
2454 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002455 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002456 /* Skip EOF-chars.. */
2457 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002458 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2459 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002460 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002461 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002462 }
2463 return nr;
2464}
2465
2466static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2467 unsigned int cmd, unsigned long arg)
2468{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002469 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002470 int retval;
2471
2472 switch (cmd) {
2473 case TIOCOUTQ:
2474 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2475 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002476 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002477 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002478 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002479 else
2480 retval = read_cnt(ldata);
2481 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002482 return put_user(retval, (unsigned int __user *) arg);
2483 default:
2484 return n_tty_ioctl_helper(tty, file, cmd, arg);
2485 }
2486}
2487
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002488static void n_tty_fasync(struct tty_struct *tty, int on)
2489{
2490 struct n_tty_data *ldata = tty->disc_data;
2491
2492 if (!waitqueue_active(&tty->read_wait)) {
2493 if (on)
2494 ldata->minimum_to_wake = 1;
2495 else if (!tty->fasync)
2496 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2497 }
2498}
2499
Alan Coxa352def2008-07-16 21:53:12 +01002500struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002501 .magic = TTY_LDISC_MAGIC,
2502 .name = "n_tty",
2503 .open = n_tty_open,
2504 .close = n_tty_close,
2505 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002506 .read = n_tty_read,
2507 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002508 .ioctl = n_tty_ioctl,
2509 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002510 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002511 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002512 .write_wakeup = n_tty_write_wakeup,
2513 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002514 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002516
2517/**
2518 * n_tty_inherit_ops - inherit N_TTY methods
2519 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2520 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002521 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002522 * methods.
2523 */
2524
2525void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2526{
2527 *ops = tty_ldisc_N_TTY;
2528 ops->owner = NULL;
2529 ops->refcount = ops->flags = 0;
2530}
2531EXPORT_SYMBOL_GPL(n_tty_inherit_ops);