blob: bc613b868e711c5fadbd5f6d4ad9585d1981b297 [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 Hurley3afb1b392013-07-23 08:47:30 -0400272 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
273 tty->link->ldisc->ops->write_wakeup == n_tty_write_wakeup) {
274 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
275 return;
276 if (!tty->count)
277 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500278 n_tty_kick_worker(tty);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400279 n_tty_write_wakeup(tty->link);
Peter Hurley6c677162013-12-02 14:24:46 -0500280 if (waitqueue_active(&tty->link->write_wait))
281 wake_up_interruptible_poll(&tty->link->write_wait, POLLOUT);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400282 return;
283 }
284
Peter Hurley6367ca72013-06-15 09:14:33 -0400285 /* If there is enough space in the read buffer now, let the
286 * low-level driver know. We use chars_in_buffer() to
287 * check the buffer, as it now knows about canonical mode.
288 * Otherwise, if the driver is throttled and the line is
289 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
290 * we won't get any more characters.
291 */
292
293 while (1) {
294 int unthrottled;
295 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
296 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
297 break;
298 if (!tty->count)
299 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500300 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400301 unthrottled = tty_unthrottle_safe(tty);
302 if (!unthrottled)
303 break;
304 }
305 __tty_set_flow_change(tty, 0);
306}
307
Alan Cox17b82062008-10-13 10:45:06 +0100308/**
309 * put_tty_queue - add character to tty
310 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200311 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100312 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400313 * Add a character to the tty read_buf queue.
314 *
315 * n_tty_receive_buf()/producer path:
316 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100317 */
318
Peter Hurley19e2ad62013-07-24 08:29:54 -0400319static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100321 *read_buf_addr(ldata, ldata->read_head) = c;
322 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * reset_buffer_flags - reset buffer state
327 * @tty: terminal to reset
328 *
Peter Hurley25518c62013-03-11 16:44:31 -0400329 * Reset the read buffer counters and clear the flags.
330 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100331 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400332 * Locking: caller holds exclusive termios_rwsem
333 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000335
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400336static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400338 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley17bd7902013-06-15 10:04:24 -0400339 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500340 ldata->commit_head = 0;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500341 ldata->echo_mark = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400342 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000343
Peter Hurleya73d3d62013-06-15 09:14:25 -0400344 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200345 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500346 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Peter Hurleya30737a2013-03-11 16:44:22 -0400349static void n_tty_packet_mode_flush(struct tty_struct *tty)
350{
351 unsigned long flags;
352
Peter Hurleya30737a2013-03-11 16:44:22 -0400353 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400354 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400355 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400356 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000357 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400358 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
362 * n_tty_flush_buffer - clean input queue
363 * @tty: terminal device
364 *
Peter Hurley25518c62013-03-11 16:44:31 -0400365 * Flush the input buffer. Called when the tty layer wants the
366 * buffer flushed (eg at hangup) or when the N_TTY line discipline
367 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400369 * Holds termios_rwsem to exclude producer/consumer while
370 * buffer indices are reset.
371 *
372 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Alan Cox4edf1822008-02-08 04:18:44 -0800374
375static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400377 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400378 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500379 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800380
Peter Hurleya30737a2013-03-11 16:44:22 -0400381 if (tty->link)
382 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400383 up_write(&tty->termios_rwsem);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * n_tty_chars_in_buffer - report available bytes
388 * @tty: tty device
389 *
390 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800391 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100392 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400393 * Locking: exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Alan Cox4edf1822008-02-08 04:18:44 -0800395
Peter Hurleya19d0c62013-06-15 09:14:18 -0400396static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
397{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400398 ssize_t n;
399
Peter Hurley47534082013-06-15 09:14:19 -0400400 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400401
402 down_write(&tty->termios_rwsem);
403 n = chars_in_buffer(tty);
404 up_write(&tty->termios_rwsem);
405 return n;
Peter Hurleya19d0c62013-06-15 09:14:18 -0400406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/**
409 * is_utf8_continuation - utf8 multibyte check
410 * @c: byte to check
411 *
412 * Returns true if the utf8 character 'c' is a multibyte continuation
413 * character. We use this to correctly compute the on screen size
414 * of the character when printing
415 */
Alan Cox4edf1822008-02-08 04:18:44 -0800416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static inline int is_utf8_continuation(unsigned char c)
418{
419 return (c & 0xc0) == 0x80;
420}
421
422/**
423 * is_continuation - multibyte check
424 * @c: byte to check
425 *
426 * Returns true if the utf8 character 'c' is a multibyte continuation
427 * character and the terminal is in unicode mode.
428 */
Alan Cox4edf1822008-02-08 04:18:44 -0800429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static inline int is_continuation(unsigned char c, struct tty_struct *tty)
431{
432 return I_IUTF8(tty) && is_utf8_continuation(c);
433}
434
435/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000436 * do_output_char - output one character
437 * @c: character (or partial unicode symbol)
438 * @tty: terminal device
439 * @space: space available in tty driver write buffer
440 *
441 * This is a helper function that handles one output character
442 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600443 * doing OPOST processing and putting the results in the
444 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000445 *
446 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
447 * and NLDLY. They simply aren't relevant in the world today.
448 * If you ever need them, add them here.
449 *
450 * Returns the number of bytes of buffer space used or -1 if
451 * no space left.
452 *
453 * Locking: should be called under the output_lock to protect
454 * the column state and space left in the buffer
455 */
456
457static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
458{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200459 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000460 int spaces;
461
462 if (!space)
463 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000464
Joe Petersona88a69c2009-01-02 13:40:53 +0000465 switch (c) {
466 case '\n':
467 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200468 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000469 if (O_ONLCR(tty)) {
470 if (space < 2)
471 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200472 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700473 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000474 return 2;
475 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200476 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000477 break;
478 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200479 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000480 return 0;
481 if (O_OCRNL(tty)) {
482 c = '\n';
483 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200484 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000485 break;
486 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200487 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000488 break;
489 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200490 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000491 if (O_TABDLY(tty) == XTABS) {
492 if (space < spaces)
493 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200494 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000495 tty->ops->write(tty, " ", spaces);
496 return spaces;
497 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200498 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000499 break;
500 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200501 if (ldata->column > 0)
502 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000503 break;
504 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000505 if (!iscntrl(c)) {
506 if (O_OLCUC(tty))
507 c = toupper(c);
508 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200509 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000510 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000511 break;
512 }
513
514 tty_put_char(tty, c);
515 return 1;
516}
517
518/**
519 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * @c: character (or partial unicode symbol)
521 * @tty: terminal device
522 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600523 * Output one character with OPOST processing.
524 * Returns -1 when the output device is full and the character
525 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000527 * Locking: output_lock to protect column state and space left
528 * (also, this is called from n_tty_write under the
529 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Alan Cox4edf1822008-02-08 04:18:44 -0800531
Joe Petersona88a69c2009-01-02 13:40:53 +0000532static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Jiri Slabybddc7152012-10-18 22:26:42 +0200534 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000535 int space, retval;
536
Jiri Slabybddc7152012-10-18 22:26:42 +0200537 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alan Coxf34d7a52008-04-30 00:54:13 -0700539 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000540 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jiri Slabybddc7152012-10-18 22:26:42 +0200542 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000543 if (retval < 0)
544 return -1;
545 else
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000550 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600552 * @buf: character buffer
553 * @nr: number of bytes to output
554 *
555 * Output a block of characters with OPOST processing.
556 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
558 * This path is used to speed up block console writes, among other
559 * things when processing blocks of output data. It handles only
560 * the simple cases normally found and helps to generate blocks of
561 * symbols for the console driver and thus improve performance.
562 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000563 * Locking: output_lock to protect column state and space left
564 * (also, this is called from n_tty_write under the
565 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Alan Cox4edf1822008-02-08 04:18:44 -0800567
Joe Petersona88a69c2009-01-02 13:40:53 +0000568static ssize_t process_output_block(struct tty_struct *tty,
569 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200571 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100573 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 const unsigned char *cp;
575
Jiri Slabybddc7152012-10-18 22:26:42 +0200576 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000577
Alan Coxf34d7a52008-04-30 00:54:13 -0700578 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000579 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200580 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (nr > space)
584 nr = space;
585
586 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000587 unsigned char c = *cp;
588
589 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case '\n':
591 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200592 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (O_ONLCR(tty))
594 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200595 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200598 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto break_out;
600 if (O_OCRNL(tty))
601 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200602 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 case '\t':
605 goto break_out;
606 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200607 if (ldata->column > 0)
608 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000611 if (!iscntrl(c)) {
612 if (O_OLCUC(tty))
613 goto break_out;
614 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200615 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 }
619 }
620break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700621 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000622
Jiri Slabybddc7152012-10-18 22:26:42 +0200623 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return i;
625}
626
Joe Petersona88a69c2009-01-02 13:40:53 +0000627/**
628 * process_echoes - write pending echo characters
629 * @tty: terminal device
630 *
631 * Write previously buffered echo (and other ldisc-generated)
632 * characters to the tty.
633 *
634 * Characters generated by the ldisc (including echoes) need to
635 * be buffered because the driver's write buffer can fill during
636 * heavy program output. Echoing straight to the driver will
637 * often fail under these conditions, causing lost characters and
638 * resulting mismatches of ldisc state information.
639 *
640 * Since the ldisc state must represent the characters actually sent
641 * to the driver at the time of the write, operations like certain
642 * changes in column state are also saved in the buffer and executed
643 * here.
644 *
645 * A circular fifo buffer is used so that the most recent characters
646 * are prioritized. Also, when control characters are echoed with a
647 * prefixed "^", the pair is treated atomically and thus not separated.
648 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400649 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000650 */
651
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400652static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000653{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200654 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400655 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400656 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000657 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000658
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400659 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000660
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400661 tail = ldata->echo_tail;
Peter Hurley29c7c5c2013-06-15 10:04:28 -0400662 while (ldata->echo_commit != tail) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400663 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000664 if (c == ECHO_OP_START) {
665 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000666 int no_space_left = 0;
667
668 /*
669 * If the buffer byte is the start of a multi-byte
670 * operation, get the next byte, which is either the
671 * op code or a control character value.
672 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400673 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000674
Joe Petersona88a69c2009-01-02 13:40:53 +0000675 switch (op) {
676 unsigned int num_chars, num_bs;
677
678 case ECHO_OP_ERASE_TAB:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400679 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000680
681 /*
682 * Determine how many columns to go back
683 * in order to erase the tab.
684 * This depends on the number of columns
685 * used by other characters within the tab
686 * area. If this (modulo 8) count is from
687 * the start of input rather than from a
688 * previous tab, we offset by canon column.
689 * Otherwise, tab spacing is normal.
690 */
691 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200692 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 num_bs = 8 - (num_chars & 7);
694
695 if (num_bs > space) {
696 no_space_left = 1;
697 break;
698 }
699 space -= num_bs;
700 while (num_bs--) {
701 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200702 if (ldata->column > 0)
703 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400705 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000706 break;
707
708 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200709 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400710 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 break;
712
713 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200714 if (ldata->column > 0)
715 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400716 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000717 break;
718
719 case ECHO_OP_START:
720 /* This is an escaped echo op start code */
721 if (!space) {
722 no_space_left = 1;
723 break;
724 }
725 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200726 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400728 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000729 break;
730
731 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000732 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600733 * If the op is not a special byte code,
734 * it is a ctrl char tagged to be echoed
735 * as "^X" (where X is the letter
736 * representing the control char).
737 * Note that we must ensure there is
738 * enough space for the whole ctrl pair.
739 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000740 */
Joe Peterson62b26352009-09-09 15:03:47 -0600741 if (space < 2) {
742 no_space_left = 1;
743 break;
744 }
745 tty_put_char(tty, '^');
746 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200747 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600748 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400749 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000750 }
751
752 if (no_space_left)
753 break;
754 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400755 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600756 int retval = do_output_char(c, tty, space);
757 if (retval < 0)
758 break;
759 space -= retval;
760 } else {
761 if (!space)
762 break;
763 tty_put_char(tty, c);
764 space -= 1;
765 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400766 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000767 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000768 }
769
Peter Hurleycbfd0342013-06-15 10:04:26 -0400770 /* If the echo buffer is nearly full (so that the possibility exists
771 * of echo overrun before the next commit), then discard enough
772 * data at the tail to prevent a subsequent overrun */
773 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200774 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500775 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400776 tail += 3;
777 else
778 tail += 2;
779 } else
780 tail++;
781 }
782
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400783 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400784 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000785}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Peter Hurley17bd7902013-06-15 10:04:24 -0400787static void commit_echoes(struct tty_struct *tty)
788{
789 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400790 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400791 size_t head;
792
793 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500794 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400795 old = ldata->echo_commit - ldata->echo_tail;
796
797 /* Process committed echoes if the accumulated # of bytes
798 * is over the threshold (and try again each time another
799 * block is accumulated) */
800 nr = head - ldata->echo_tail;
801 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
802 return;
Peter Hurley17bd7902013-06-15 10:04:24 -0400803
Peter Hurley019ebdf2013-06-15 10:04:25 -0400804 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400805 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400806 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400807 mutex_unlock(&ldata->output_lock);
808
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400809 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400810 tty->ops->flush_chars(tty);
811}
812
813static void process_echoes(struct tty_struct *tty)
814{
815 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400816 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400817
Peter Hurleye2613be2014-02-11 16:34:55 -0500818 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400819 return;
820
821 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500822 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400823 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400824 mutex_unlock(&ldata->output_lock);
825
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400826 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400827 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400828}
829
Peter Hurley1075a6e2013-12-09 18:06:07 -0500830/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400831static void flush_echoes(struct tty_struct *tty)
832{
833 struct n_tty_data *ldata = tty->disc_data;
834
Peter Hurley39434ab2013-11-29 12:56:10 -0500835 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
836 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400837 return;
838
839 mutex_lock(&ldata->output_lock);
840 ldata->echo_commit = ldata->echo_head;
841 __process_echoes(tty);
842 mutex_unlock(&ldata->output_lock);
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000846 * add_echo_byte - add a byte to the echo buffer
847 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200848 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 *
850 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000851 */
852
Peter Hurleycbfd0342013-06-15 10:04:26 -0400853static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000854{
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400855 *echo_buf_addr(ldata, ldata->echo_head++) = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000856}
857
858/**
859 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200860 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000861 *
862 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000863 */
864
Jiri Slaby57c94122012-10-18 22:26:43 +0200865static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000866{
Jiri Slaby57c94122012-10-18 22:26:43 +0200867 add_echo_byte(ECHO_OP_START, ldata);
868 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000869}
870
871/**
872 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200873 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000874 *
875 * Add an operation to the echo buffer to set the canon column
876 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000877 */
878
Jiri Slaby57c94122012-10-18 22:26:43 +0200879static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000880{
Jiri Slaby57c94122012-10-18 22:26:43 +0200881 add_echo_byte(ECHO_OP_START, ldata);
882 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000883}
884
885/**
886 * echo_erase_tab - add operation to erase a tab
887 * @num_chars: number of character columns already used
888 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200889 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000890 *
891 * Add an operation to the echo buffer to erase a tab.
892 *
893 * Called by the eraser function, which knows how many character
894 * columns have been used since either a previous tab or the start
895 * of input. This information will be used later, along with
896 * canon column (if applicable), to go back the correct number
897 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000898 */
899
900static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200901 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000902{
Jiri Slaby57c94122012-10-18 22:26:43 +0200903 add_echo_byte(ECHO_OP_START, ldata);
904 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000905
906 /* We only need to know this modulo 8 (tab spacing) */
907 num_chars &= 7;
908
909 /* Set the high bit as a flag if num_chars is after a previous tab */
910 if (after_tab)
911 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000912
Jiri Slaby57c94122012-10-18 22:26:43 +0200913 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000914}
915
916/**
917 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * @c: unicode byte to echo
919 * @tty: terminal device
920 *
Alan Cox4edf1822008-02-08 04:18:44 -0800921 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100923 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000924 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000925 */
926
Jiri Slaby57c94122012-10-18 22:26:43 +0200927static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000928{
Joe Petersona88a69c2009-01-02 13:40:53 +0000929 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200930 add_echo_byte(ECHO_OP_START, ldata);
931 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000932 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200933 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000934 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000935}
936
937/**
938 * echo_char - echo a character
939 * @c: unicode byte to echo
940 * @tty: terminal device
941 *
942 * Echo user input back onto the screen. This must be called only when
943 * L_ECHO(tty) is true. Called from the driver receive_buf path.
944 *
Joe Peterson62b26352009-09-09 15:03:47 -0600945 * This variant tags control characters to be echoed as "^X"
946 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
948
949static void echo_char(unsigned char c, struct tty_struct *tty)
950{
Jiri Slabybddc7152012-10-18 22:26:42 +0200951 struct n_tty_data *ldata = tty->disc_data;
952
Joe Petersona88a69c2009-01-02 13:40:53 +0000953 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200954 add_echo_byte(ECHO_OP_START, ldata);
955 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000956 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600957 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200958 add_echo_byte(ECHO_OP_START, ldata);
959 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Alan Cox17b82062008-10-13 10:45:06 +0100963/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000964 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200965 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100966 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000967
Jiri Slaby57c94122012-10-18 22:26:43 +0200968static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200970 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200971 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200972 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974}
975
976/**
977 * eraser - handle erase function
978 * @c: character input
979 * @tty: terminal device
980 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200981 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * present in the stream from the driver layer. Handles the complexities
983 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100984 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400985 * n_tty_receive_buf()/producer path:
986 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Alan Cox4edf1822008-02-08 04:18:44 -0800988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static void eraser(unsigned char c, struct tty_struct *tty)
990{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200991 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400993 size_t head;
994 size_t cnt;
995 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200997 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000998 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return;
1000 }
1001 if (c == ERASE_CHAR(tty))
1002 kill_type = ERASE;
1003 else if (c == WERASE_CHAR(tty))
1004 kill_type = WERASE;
1005 else {
1006 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001007 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001011 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +02001012 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 echo_char(KILL_CHAR(tty), tty);
1014 /* Add a newline if ECHOK is on and ECHOKE is off. */
1015 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001016 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return;
1018 }
1019 kill_type = KILL;
1020 }
1021
1022 seen_alnums = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001023 while (ldata->read_head != ldata->canon_head) {
1024 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* erase a single possibly multibyte character */
1027 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001028 head--;
1029 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001030 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /* do not partially erase */
1033 if (is_continuation(c, tty))
1034 break;
1035
1036 if (kill_type == WERASE) {
1037 /* Equivalent to BSD's ALTWERASE. */
1038 if (isalnum(c) || c == '_')
1039 seen_alnums++;
1040 else if (seen_alnums)
1041 break;
1042 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001043 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001044 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (L_ECHO(tty)) {
1046 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001047 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001048 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001049 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051 /* if cnt > 1, output a multi-byte character */
1052 echo_char(c, tty);
1053 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001054 head++;
1055 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001056 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1059 echo_char(ERASE_CHAR(tty), tty);
1060 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001061 unsigned int num_chars = 0;
1062 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001063 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Joe Petersona88a69c2009-01-02 13:40:53 +00001065 /*
1066 * Count the columns used for characters
1067 * since the start of input or after a
1068 * previous tab.
1069 * This info is used to go back the correct
1070 * number of columns.
1071 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001072 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001073 tail--;
1074 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001075 if (c == '\t') {
1076 after_tab = 1;
1077 break;
Alan Cox300a6202009-01-02 13:41:04 +00001078 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001080 num_chars += 2;
1081 } else if (!is_continuation(c, tty)) {
1082 num_chars++;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001085 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 } else {
1087 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001088 echo_char_raw('\b', ldata);
1089 echo_char_raw(' ', ldata);
1090 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001093 echo_char_raw('\b', ldata);
1094 echo_char_raw(' ', ldata);
1095 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
1098 }
1099 if (kill_type == ERASE)
1100 break;
1101 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001102 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001103 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/**
1107 * isig - handle the ISIG optio
1108 * @sig: signal
1109 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001111 * Called when a signal is being sent due to terminal input.
1112 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001113 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001114 * Performs input and output flush if !NOFLSH. In this context, the echo
1115 * buffer is 'output'. The signal is processed first to alert any current
1116 * readers or writers to discontinue and exit their i/o loops.
1117 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001118 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Alan Cox4edf1822008-02-08 04:18:44 -08001120
Peter Hurley3b19e032015-06-27 09:21:32 -04001121static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Peter Hurley8c985d12013-03-06 08:38:19 -05001123 struct pid *tty_pgrp = tty_get_pgrp(tty);
1124 if (tty_pgrp) {
1125 kill_pgrp(tty_pgrp, sig, 1);
1126 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001128}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001129
Peter Hurley3b19e032015-06-27 09:21:32 -04001130static void isig(int sig, struct tty_struct *tty)
1131{
1132 struct n_tty_data *ldata = tty->disc_data;
1133
1134 if (L_NOFLSH(tty)) {
1135 /* signal only */
1136 __isig(sig, tty);
1137
1138 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001139 up_read(&tty->termios_rwsem);
1140 down_write(&tty->termios_rwsem);
1141
Peter Hurley3b19e032015-06-27 09:21:32 -04001142 __isig(sig, tty);
1143
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001144 /* clear echo buffer */
1145 mutex_lock(&ldata->output_lock);
1146 ldata->echo_head = ldata->echo_tail = 0;
1147 ldata->echo_mark = ldata->echo_commit = 0;
1148 mutex_unlock(&ldata->output_lock);
1149
1150 /* clear output buffer */
1151 tty_driver_flush_buffer(tty);
1152
1153 /* clear input buffer */
1154 reset_buffer_flags(tty->disc_data);
1155
1156 /* notify pty master of flush */
1157 if (tty->link)
1158 n_tty_packet_mode_flush(tty);
1159
1160 up_write(&tty->termios_rwsem);
1161 down_read(&tty->termios_rwsem);
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165/**
1166 * n_tty_receive_break - handle break
1167 * @tty: terminal
1168 *
1169 * An RS232 break event has been hit in the incoming bitstream. This
1170 * can cause a variety of events depending upon the termios settings.
1171 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001172 * n_tty_receive_buf()/producer path:
1173 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001174 *
1175 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Alan Cox4edf1822008-02-08 04:18:44 -08001177
Peter Hurley4b293492013-07-24 08:29:55 -04001178static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jiri Slaby57c94122012-10-18 22:26:43 +02001180 struct n_tty_data *ldata = tty->disc_data;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (I_IGNBRK(tty))
1183 return;
1184 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001185 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return;
1187 }
1188 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001189 put_tty_queue('\377', ldata);
1190 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001192 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195/**
1196 * n_tty_receive_overrun - handle overrun reporting
1197 * @tty: terminal
1198 *
1199 * Data arrived faster than we could process it. While the tty
1200 * driver has flagged this the bits that were missed are gone
1201 * forever.
1202 *
1203 * Called from the receive_buf path so single threaded. Does not
1204 * need locking as num_overrun and overrun_time are function
1205 * private.
1206 */
Alan Cox4edf1822008-02-08 04:18:44 -08001207
Peter Hurley4b293492013-07-24 08:29:55 -04001208static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001210 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001212 ldata->num_overrun++;
1213 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1214 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001215 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001216 ldata->overrun_time = jiffies;
1217 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219}
1220
1221/**
1222 * n_tty_receive_parity_error - error notifier
1223 * @tty: terminal device
1224 * @c: character
1225 *
1226 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001227 * the error case if necessary.
1228 *
1229 * n_tty_receive_buf()/producer path:
1230 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Peter Hurley4b293492013-07-24 08:29:55 -04001232static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Jiri Slaby57c94122012-10-18 22:26:43 +02001234 struct n_tty_data *ldata = tty->disc_data;
1235
Peter Hurley66528f92014-06-16 08:10:42 -04001236 if (I_INPCK(tty)) {
1237 if (I_IGNPAR(tty))
1238 return;
1239 if (I_PARMRK(tty)) {
1240 put_tty_queue('\377', ldata);
1241 put_tty_queue('\0', ldata);
1242 put_tty_queue(c, ldata);
1243 } else
1244 put_tty_queue('\0', ldata);
1245 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001246 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001249static void
1250n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1251{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001252 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001253 if (I_IXON(tty))
1254 start_tty(tty);
1255 if (L_ECHO(tty)) {
1256 echo_char(c, tty);
1257 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001258 } else
1259 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001260 return;
1261}
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/**
1264 * n_tty_receive_char - perform processing
1265 * @tty: terminal device
1266 * @c: character
1267 *
1268 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001269 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001271 *
1272 * n_tty_receive_buf()/producer path:
1273 * caller holds non-exclusive termios_rwsem
1274 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001275 *
1276 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
1278
Peter Hurleye60d27c2013-07-24 08:29:56 -04001279static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001280n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001282 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (I_IXON(tty)) {
1285 if (c == START_CHAR(tty)) {
1286 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001287 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 if (c == STOP_CHAR(tty)) {
1291 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
Joe Peterson575537b32008-04-30 00:53:30 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001297 if (c == INTR_CHAR(tty)) {
1298 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001299 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001300 } else if (c == QUIT_CHAR(tty)) {
1301 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001302 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001303 } else if (c == SUSP_CHAR(tty)) {
1304 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 }
Joe Peterson575537b32008-04-30 00:53:30 -07001308
Peter Hurley855df3c2013-07-24 08:29:50 -04001309 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1310 start_tty(tty);
1311 process_echoes(tty);
1312 }
1313
Joe Peterson575537b32008-04-30 00:53:30 -07001314 if (c == '\r') {
1315 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001316 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001317 if (I_ICRNL(tty))
1318 c = '\n';
1319 } else if (c == '\n' && I_INLCR(tty))
1320 c = '\r';
1321
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001322 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1324 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1325 eraser(c, tty);
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 == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001330 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001332 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001334 echo_char_raw('^', ldata);
1335 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001336 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001339 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001341 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001342 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jiri Slaby57c94122012-10-18 22:26:43 +02001344 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001346 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001347 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001348 echo_char(read_buf(ldata, tail), tty);
1349 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001351 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001355 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001356 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001357 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 goto handle_newline;
1360 }
1361 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 c = __DISABLED_CHAR;
1363 goto handle_newline;
1364 }
1365 if ((c == EOL_CHAR(tty)) ||
1366 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1367 /*
1368 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1369 */
1370 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001372 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001375 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 /*
1378 * XXX does PARMRK doubling happen for
1379 * EOL_CHAR and EOL2_CHAR?
1380 */
Peter Hurley001ba922013-12-02 14:24:44 -05001381 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001382 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Alan Cox4edf1822008-02-08 04:18:44 -08001384handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001385 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001386 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001387 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001389 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 }
Alan Cox4edf1822008-02-08 04:18:44 -08001393
Joe Petersonacc71bb2009-01-02 13:43:32 +00001394 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001395 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001397 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 else {
1399 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001400 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001401 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 echo_char(c, tty);
1403 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001404 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Peter Hurley001ba922013-12-02 14:24:44 -05001407 /* PARMRK doubling check */
1408 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001409 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jiri Slaby57c94122012-10-18 22:26:43 +02001411 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001412 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Peter Hurleye60d27c2013-07-24 08:29:56 -04001415static inline void
1416n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001417{
1418 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001419
Peter Hurleye60d27c2013-07-24 08:29:56 -04001420 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1421 start_tty(tty);
1422 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001423 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001424 if (L_ECHO(tty)) {
1425 finish_erasing(ldata);
1426 /* Record the column of first canon char. */
1427 if (ldata->canon_head == ldata->read_head)
1428 echo_set_canon_col(ldata);
1429 echo_char(c, tty);
1430 commit_echoes(tty);
1431 }
Peter Hurley001ba922013-12-02 14:24:44 -05001432 /* PARMRK doubling check */
1433 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001434 put_tty_queue(c, ldata);
1435 put_tty_queue(c, ldata);
1436}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001437
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001438static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001439{
1440 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001441}
1442
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001443static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001444n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1445{
1446 struct n_tty_data *ldata = tty->disc_data;
1447
Peter Hurleye60d27c2013-07-24 08:29:56 -04001448 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1449 start_tty(tty);
1450 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001451 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001452 if (L_ECHO(tty)) {
1453 finish_erasing(ldata);
1454 /* Record the column of first canon char. */
1455 if (ldata->canon_head == ldata->read_head)
1456 echo_set_canon_col(ldata);
1457 echo_char(c, tty);
1458 commit_echoes(tty);
1459 }
1460 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001461}
1462
Peter Hurley8dc4b252013-12-02 14:24:43 -05001463static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001464{
1465 if (I_ISTRIP(tty))
1466 c &= 0x7f;
1467 if (I_IUCLC(tty) && L_IEXTEN(tty))
1468 c = tolower(c);
1469
1470 if (I_IXON(tty)) {
1471 if (c == STOP_CHAR(tty))
1472 stop_tty(tty);
1473 else if (c == START_CHAR(tty) ||
1474 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1475 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1476 c != SUSP_CHAR(tty))) {
1477 start_tty(tty);
1478 process_echoes(tty);
1479 }
1480 }
1481}
1482
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001483static void
1484n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1485{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001486 switch (flag) {
1487 case TTY_BREAK:
1488 n_tty_receive_break(tty);
1489 break;
1490 case TTY_PARITY:
1491 case TTY_FRAME:
1492 n_tty_receive_parity_error(tty, c);
1493 break;
1494 case TTY_OVERRUN:
1495 n_tty_receive_overrun(tty);
1496 break;
1497 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001498 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001499 break;
1500 }
1501}
1502
Peter Hurleye60d27c2013-07-24 08:29:56 -04001503static void
1504n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1505{
1506 struct n_tty_data *ldata = tty->disc_data;
1507
1508 ldata->lnext = 0;
1509 if (likely(flag == TTY_NORMAL)) {
1510 if (I_ISTRIP(tty))
1511 c &= 0x7f;
1512 if (I_IUCLC(tty) && L_IEXTEN(tty))
1513 c = tolower(c);
1514 n_tty_receive_char(tty, c);
1515 } else
1516 n_tty_receive_char_flagged(tty, c, flag);
1517}
1518
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001519static void
1520n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1521 char *fp, int count)
1522{
1523 struct n_tty_data *ldata = tty->disc_data;
1524 size_t n, head;
1525
1526 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001527 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001528 memcpy(read_buf_addr(ldata, head), cp, n);
1529 ldata->read_head += n;
1530 cp += n;
1531 count -= n;
1532
1533 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001534 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001535 memcpy(read_buf_addr(ldata, head), cp, n);
1536 ldata->read_head += n;
1537}
1538
Peter Hurley554117b2013-06-15 10:21:25 -04001539static void
1540n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1541 char *fp, int count)
1542{
1543 struct n_tty_data *ldata = tty->disc_data;
1544 char flag = TTY_NORMAL;
1545
1546 while (count--) {
1547 if (fp)
1548 flag = *fp++;
1549 if (likely(flag == TTY_NORMAL))
1550 put_tty_queue(*cp++, ldata);
1551 else
1552 n_tty_receive_char_flagged(tty, *cp++, flag);
1553 }
1554}
1555
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001556static void
1557n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1558 char *fp, int count)
1559{
1560 char flag = TTY_NORMAL;
1561
1562 while (count--) {
1563 if (fp)
1564 flag = *fp++;
1565 if (likely(flag == TTY_NORMAL))
1566 n_tty_receive_char_closing(tty, *cp++);
1567 else
1568 n_tty_receive_char_flagged(tty, *cp++, flag);
1569 }
1570}
1571
Peter Hurley7d88d632013-07-24 08:29:49 -04001572static void
1573n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001574 char *fp, int count)
1575{
1576 struct n_tty_data *ldata = tty->disc_data;
1577 char flag = TTY_NORMAL;
1578
1579 while (count--) {
1580 if (fp)
1581 flag = *fp++;
1582 if (likely(flag == TTY_NORMAL)) {
1583 unsigned char c = *cp++;
1584
1585 if (I_ISTRIP(tty))
1586 c &= 0x7f;
1587 if (I_IUCLC(tty) && L_IEXTEN(tty))
1588 c = tolower(c);
1589 if (L_EXTPROC(tty)) {
1590 put_tty_queue(c, ldata);
1591 continue;
1592 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001593 if (!test_bit(c, ldata->char_map))
1594 n_tty_receive_char_inline(tty, c);
1595 else if (n_tty_receive_char_special(tty, c) && count) {
1596 if (fp)
1597 flag = *fp++;
1598 n_tty_receive_char_lnext(tty, *cp++, flag);
1599 count--;
1600 }
Peter Hurley6baad002013-07-24 08:29:52 -04001601 } else
1602 n_tty_receive_char_flagged(tty, *cp++, flag);
1603 }
1604}
1605
1606static void
1607n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1608 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001609{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001610 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001611 char flag = TTY_NORMAL;
1612
1613 while (count--) {
1614 if (fp)
1615 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001616 if (likely(flag == TTY_NORMAL)) {
1617 unsigned char c = *cp++;
1618
1619 if (!test_bit(c, ldata->char_map))
1620 n_tty_receive_char_fast(tty, c);
1621 else if (n_tty_receive_char_special(tty, c) && count) {
1622 if (fp)
1623 flag = *fp++;
1624 n_tty_receive_char_lnext(tty, *cp++, flag);
1625 count--;
1626 }
1627 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001628 n_tty_receive_char_flagged(tty, *cp++, flag);
1629 }
1630}
1631
Peter Hurley24a89d12013-06-15 09:14:15 -04001632static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1633 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001635 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001636 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001638 if (ldata->real_raw)
1639 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001640 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117b2013-06-15 10:21:25 -04001641 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001642 else if (tty->closing && !L_EXTPROC(tty))
1643 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001644 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001645 if (ldata->lnext) {
1646 char flag = TTY_NORMAL;
1647
1648 if (fp)
1649 flag = *fp++;
1650 n_tty_receive_char_lnext(tty, *cp++, flag);
1651 count--;
1652 }
1653
Peter Hurley7de971b2013-07-24 08:29:53 -04001654 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001655 n_tty_receive_buf_fast(tty, cp, fp, count);
1656 else
1657 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001658
1659 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001660 if (tty->ops->flush_chars)
1661 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
Peter Hurley70aca712015-01-16 15:05:37 -05001664 if (ldata->icanon && !L_EXTPROC(tty))
1665 return;
1666
1667 /* publish read_head to consumer */
1668 smp_store_release(&ldata->commit_head, ldata->read_head);
1669
1670 if ((read_cnt(ldata) >= ldata->minimum_to_wake) || L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001672 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001676/**
1677 * n_tty_receive_buf_common - process input
1678 * @tty: device to receive input
1679 * @cp: input chars
1680 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1681 * @count: number of input chars in @cp
1682 *
1683 * Called by the terminal driver when a block of characters has
1684 * been received. This function must be called from soft contexts
1685 * not from interrupt context. The driver is responsible for making
1686 * calls one at a time and in order (or using flush_to_ldisc)
1687 *
1688 * Returns the # of input chars from @cp which were processed.
1689 *
1690 * In canonical mode, the maximum line length is 4096 chars (including
1691 * the line termination char); lines longer than 4096 chars are
1692 * truncated. After 4095 chars, input data is still processed but
1693 * not stored. Overflow processing ensures the tty can always
1694 * receive more input until at least one line can be read.
1695 *
1696 * In non-canonical mode, the read buffer will only accept 4095 chars;
1697 * this provides the necessary space for a newline char if the input
1698 * mode is switched to canonical.
1699 *
1700 * Note it is possible for the read buffer to _contain_ 4096 chars
1701 * in non-canonical mode: the read buffer could already contain the
1702 * maximum canon line of 4096 chars when the mode is switched to
1703 * non-canonical.
1704 *
1705 * n_tty_receive_buf()/producer path:
1706 * claims non-exclusive termios_rwsem
1707 * publishes commit_head or canon_head
1708 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001709static int
1710n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1711 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001712{
1713 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001714 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001715
Peter Hurley9356b532013-06-15 09:14:24 -04001716 down_read(&tty->termios_rwsem);
1717
Peter Hurley19e2ad62013-07-24 08:29:54 -04001718 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001719 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001720 * When PARMRK is set, each input char may take up to 3 chars
1721 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001722 *
1723 * If we are doing input canonicalization, and there are no
1724 * pending newlines, let characters through without limit, so
1725 * that erase characters will be handled. Other excess
1726 * characters will be beeped.
1727 *
1728 * paired with store in *_copy_from_read_buf() -- guarantees
1729 * the consumer has loaded the data in read_buf up to the new
1730 * read_tail (so this producer will not overwrite unread data)
1731 */
1732 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001733
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001734 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001735 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001736 room = (room + 2) / 3;
1737 room--;
1738 if (room <= 0) {
1739 overflow = ldata->icanon && ldata->canon_head == tail;
1740 if (overflow && room < 0)
1741 ldata->read_head--;
1742 room = overflow;
1743 ldata->no_room = flow && !room;
1744 } else
1745 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001746
Peter Hurley19e2ad62013-07-24 08:29:54 -04001747 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001748 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001749 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001750
1751 /* ignore parity errors if handling overflow */
1752 if (!overflow || !fp || *fp != TTY_PARITY)
1753 __receive_buf(tty, cp, fp, n);
1754
Peter Hurley19e2ad62013-07-24 08:29:54 -04001755 cp += n;
1756 if (fp)
1757 fp += n;
1758 count -= n;
1759 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001760 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001761
Peter Hurley19e2ad62013-07-24 08:29:54 -04001762 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001763
1764 /* Unthrottle if handling overflow on pty */
1765 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1766 if (overflow) {
1767 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1768 tty_unthrottle_safe(tty);
1769 __tty_set_flow_change(tty, 0);
1770 }
1771 } else
1772 n_tty_check_throttle(tty);
1773
Peter Hurley9356b532013-06-15 09:14:24 -04001774 up_read(&tty->termios_rwsem);
1775
Peter Hurley19e2ad62013-07-24 08:29:54 -04001776 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001777}
1778
Peter Hurley5c32d122013-12-02 14:24:41 -05001779static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1780 char *fp, int count)
1781{
1782 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1783}
1784
1785static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1786 char *fp, int count)
1787{
1788 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791int is_ignored(int sig)
1792{
1793 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001794 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797/**
1798 * n_tty_set_termios - termios data changed
1799 * @tty: terminal
1800 * @old: previous data
1801 *
1802 * Called by the tty layer when the user changes termios flags so
1803 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001804 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * guaranteed that this function will not be re-entered or in progress
1806 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001807 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001808 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Alan Cox4edf1822008-02-08 04:18:44 -08001810
1811static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001813 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001814
Peter Hurleyc786f742013-09-17 12:53:35 -04001815 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001816 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001817 ldata->line_start = ldata->read_tail;
1818 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1819 ldata->canon_head = ldata->read_tail;
1820 ldata->push = 0;
1821 } else {
1822 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1823 ldata->read_flags);
1824 ldata->canon_head = ldata->read_head;
1825 ldata->push = 1;
1826 }
Peter Hurley70aca712015-01-16 15:05:37 -05001827 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001828 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001829 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001830 }
1831
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001832 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1835 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1836 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1837 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001838 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001841 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001843 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001846 set_bit(ERASE_CHAR(tty), ldata->char_map);
1847 set_bit(KILL_CHAR(tty), ldata->char_map);
1848 set_bit(EOF_CHAR(tty), ldata->char_map);
1849 set_bit('\n', ldata->char_map);
1850 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001852 set_bit(WERASE_CHAR(tty), ldata->char_map);
1853 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1854 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (L_ECHO(tty))
1856 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001857 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 }
1860 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001861 set_bit(START_CHAR(tty), ldata->char_map);
1862 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001865 set_bit(INTR_CHAR(tty), ldata->char_map);
1866 set_bit(QUIT_CHAR(tty), ldata->char_map);
1867 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001869 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001870 ldata->raw = 0;
1871 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001873 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1875 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1876 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001877 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001879 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001881 /*
1882 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1883 * been stopped by STOP_CHAR(tty) before it.
1884 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001885 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001886 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001887 process_echoes(tty);
1888 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001889
Alan Coxf34d7a52008-04-30 00:54:13 -07001890 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001891 wake_up_interruptible(&tty->write_wait);
1892 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895/**
1896 * n_tty_close - close the ldisc for this tty
1897 * @tty: device
1898 *
Alan Cox4edf1822008-02-08 04:18:44 -08001899 * Called from the terminal layer when this line discipline is
1900 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * discipline change. The function will not be called while other
1902 * ldisc methods are in progress.
1903 */
Alan Cox4edf1822008-02-08 04:18:44 -08001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905static void n_tty_close(struct tty_struct *tty)
1906{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001907 struct n_tty_data *ldata = tty->disc_data;
1908
Peter Hurley79901312013-03-11 16:44:23 -04001909 if (tty->link)
1910 n_tty_packet_mode_flush(tty);
1911
Peter Hurley20bafb32013-06-15 10:21:19 -04001912 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001913 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/**
1917 * n_tty_open - open an ldisc
1918 * @tty: terminal to open
1919 *
Alan Cox4edf1822008-02-08 04:18:44 -08001920 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 * terminal device. Can sleep. Called serialized so that no
1922 * other events will occur in parallel. No further open will occur
1923 * until a close.
1924 */
1925
1926static int n_tty_open(struct tty_struct *tty)
1927{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001928 struct n_tty_data *ldata;
1929
Peter Hurley20bafb32013-06-15 10:21:19 -04001930 /* Currently a malloc failure here can panic */
1931 ldata = vmalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001932 if (!ldata)
1933 goto err;
1934
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001935 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001936 mutex_init(&ldata->atomic_read_lock);
1937 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001938
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001939 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001940 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001941 ldata->column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001942 ldata->canon_column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001943 ldata->minimum_to_wake = 1;
Peter Hurley20bafb32013-06-15 10:21:19 -04001944 ldata->num_overrun = 0;
1945 ldata->no_room = 0;
1946 ldata->lnext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001948 /* indicate buffer work may resume */
1949 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1950 n_tty_set_termios(tty, NULL);
1951 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001954err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001955 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Peter Hurleyeafbe672013-12-02 14:24:45 -05001958static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001960 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001961 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001962
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001963 if (ldata->icanon && !L_EXTPROC(tty))
1964 return ldata->canon_head != ldata->read_tail;
1965 else
Peter Hurley70aca712015-01-16 15:05:37 -05001966 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Peter Hurley1a486322015-04-13 13:24:34 -04001969static inline int check_other_done(struct tty_struct *tty)
1970{
1971 int done = test_bit(TTY_OTHER_DONE, &tty->flags);
1972 if (done) {
1973 /* paired with cmpxchg() in check_other_closed(); ensures
1974 * read buffer head index is not stale
1975 */
1976 smp_mb__after_atomic();
1977 }
1978 return done;
1979}
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001982 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 * @tty: terminal device
1984 * @b: user data
1985 * @nr: size of data
1986 *
Alan Cox11a96d12008-10-13 10:46:24 +01001987 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * ICANON is off; it copies characters straight from the tty queue to
1989 * user space directly. It can be profitably called twice; once to
1990 * drain the space from the tail pointer to the (physical) end of the
1991 * buffer, and once to drain the space from the (physical) beginning of
1992 * the buffer to head pointer.
1993 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001994 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001996 * n_tty_read()/consumer path:
1997 * caller holds non-exclusive termios_rwsem
1998 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Alan Cox4edf1822008-02-08 04:18:44 -08002000
Alan Cox33f0f882006-01-09 20:54:13 -08002001static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unsigned char __user **b,
2003 size_t *nr)
2004
2005{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002006 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int retval;
2008 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02002009 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05002010 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002011 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05002014 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05002017 const unsigned char *from = read_buf_addr(ldata, tail);
2018 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05002020 is_eof = n == 1 && *from == EOF_CHAR(tty);
2021 tty_audit_add_data(tty, from, n, ldata->icanon);
Peter Hurley70aca712015-01-16 15:05:37 -05002022 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07002023 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05002024 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2025 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02002026 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 *b += n;
2028 *nr -= n;
2029 }
2030 return retval;
2031}
2032
Peter Hurley88bb0de2013-06-15 09:14:16 -04002033/**
Peter Hurley32f13522013-06-15 09:14:17 -04002034 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04002035 * @tty: terminal device
2036 * @b: user data
2037 * @nr: size of data
2038 *
2039 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04002040 * it copies one line of input up to and including the line-delimiting
2041 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04002042 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05002043 * NB: When termios is changed from non-canonical to canonical mode and
2044 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2045 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2046 * This causes data already processed as input to be immediately available
2047 * as input although a newline has not been received.
2048 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04002049 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04002050 *
2051 * n_tty_read()/consumer path:
2052 * caller holds non-exclusive termios_rwsem
2053 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002054 */
2055
Peter Hurley32f13522013-06-15 09:14:17 -04002056static int canon_copy_from_read_buf(struct tty_struct *tty,
2057 unsigned char __user **b,
2058 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002059{
2060 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002061 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002062 size_t eol;
2063 size_t tail;
2064 int ret, found = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -04002065 bool eof_push = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002066
2067 /* N.B. avoid overrun if nr == 0 */
Peter Hurley70aca712015-01-16 15:05:37 -05002068 n = min(*nr, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002069 if (!n)
Peter Hurley32f13522013-06-15 09:14:17 -04002070 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002071
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002072 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002073 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2074
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002075 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002076 __func__, *nr, tail, n, size);
2077
2078 eol = find_next_bit(ldata->read_flags, size, tail);
2079 more = n - (size - tail);
2080 if (eol == N_TTY_BUF_SIZE && more) {
2081 /* scan wrapped without finding set bit */
2082 eol = find_next_bit(ldata->read_flags, more, 0);
2083 if (eol != more)
2084 found = 1;
2085 } else if (eol != size)
2086 found = 1;
2087
Peter Hurleyc77569d2013-11-22 07:16:25 -05002088 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002089 if (n > N_TTY_BUF_SIZE)
2090 n += N_TTY_BUF_SIZE;
Peter Hurleyc77569d2013-11-22 07:16:25 -05002091 n += found;
Peter Hurley32f13522013-06-15 09:14:17 -04002092 c = n;
2093
Peter Hurley4d0ed182013-12-10 17:12:02 -05002094 if (found && !ldata->push && read_buf(ldata, eol) == __DISABLED_CHAR) {
Peter Hurley32f13522013-06-15 09:14:17 -04002095 n--;
Peter Hurley40d5e092013-06-15 10:21:17 -04002096 eof_push = !n && ldata->read_tail != ldata->line_start;
2097 }
Peter Hurley32f13522013-06-15 09:14:17 -04002098
Peter Hurley679e7c22015-11-27 14:11:02 -05002099 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2100 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002101
Peter Hurley679e7c22015-11-27 14:11:02 -05002102 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002103 if (ret)
2104 return -EFAULT;
2105 *b += n;
2106 *nr -= n;
2107
Peter Hurleya73d3d62013-06-15 09:14:25 -04002108 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002109 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002110 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002111
Peter Hurley40d5e092013-06-15 10:21:17 -04002112 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002113 if (!ldata->push)
2114 ldata->line_start = ldata->read_tail;
2115 else
2116 ldata->push = 0;
Peter Hurley32f13522013-06-15 09:14:17 -04002117 tty_audit_push(tty);
Peter Hurley40d5e092013-06-15 10:21:17 -04002118 }
2119 return eof_push ? -EAGAIN : 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002120}
2121
Al Virocc4191d2008-03-29 03:08:48 +00002122extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002123 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125/**
2126 * job_control - check job control
2127 * @tty: tty
2128 * @file: file handle
2129 *
2130 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002131 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002133 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002134 * Locking: redirected write test is safe
2135 * current->signal->tty check is safe
2136 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 */
Alan Cox4edf1822008-02-08 04:18:44 -08002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139static int job_control(struct tty_struct *tty, struct file *file)
2140{
2141 /* Job control check -- must be done at start and after
2142 every sleep (POSIX.1 7.1.1.4). */
2143 /* NOTE: not yet done after every sleep pending a thorough
2144 check of the logic of this change. -- jlc */
2145 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002146 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002147 return 0;
2148
Peter Hurley2812d9e2015-10-10 20:28:42 -04002149 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
Alan Cox4edf1822008-02-08 04:18:44 -08002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153/**
Alan Cox11a96d12008-10-13 10:46:24 +01002154 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * @tty: tty device
2156 * @file: file object
2157 * @buf: userspace buffer pointer
2158 * @nr: size of I/O
2159 *
2160 * Perform reads for the line discipline. We are guaranteed that the
2161 * line discipline will not be closed under us but we may get multiple
2162 * parallel readers and must handle this ourselves. We may also get
2163 * a hangup. Always called in user context, may sleep.
2164 *
2165 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002166 *
2167 * n_tty_read()/consumer path:
2168 * claims non-exclusive termios_rwsem
2169 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
Alan Cox4edf1822008-02-08 04:18:44 -08002171
Alan Cox11a96d12008-10-13 10:46:24 +01002172static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 unsigned char __user *buf, size_t nr)
2174{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002175 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002177 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Peter Hurley1a486322015-04-13 13:24:34 -04002178 int c, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int minimum, time;
2180 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002182 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002183 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002186 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002188
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002189 /*
2190 * Internal serialization of reads.
2191 */
2192 if (file->f_flags & O_NONBLOCK) {
2193 if (!mutex_trylock(&ldata->atomic_read_lock))
2194 return -EAGAIN;
2195 } else {
2196 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2197 return -ERESTARTSYS;
2198 }
2199
Peter Hurley9356b532013-06-15 09:14:24 -04002200 down_read(&tty->termios_rwsem);
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 minimum = time = 0;
2203 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002204 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 minimum = MIN_CHAR(tty);
2206 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002207 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002209 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002211 (ldata->minimum_to_wake > minimum))
2212 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002214 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002215 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217 }
2218
Alan Cox04f378b2008-04-30 00:53:29 -07002219 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002220 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 while (nr) {
2224 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002225 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned char cs;
2227 if (b != buf)
2228 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002229 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 cs = tty->link->ctrl_status;
2231 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002232 spin_unlock_irq(&tty->link->ctrl_lock);
Miloslav Trmac522ed772007-07-15 23:40:56 -07002233 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 retval = -EFAULT;
2235 b--;
2236 break;
2237 }
2238 nr--;
2239 break;
2240 }
Alan Cox4edf1822008-02-08 04:18:44 -08002241
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002242 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002244 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08002245
Peter Hurley1a486322015-04-13 13:24:34 -04002246 done = check_other_done(tty);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!input_available_p(tty, 0)) {
Peter Hurley1a486322015-04-13 13:24:34 -04002249 if (done) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002250 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 break;
2252 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002253 if (tty_hung_up_p(file))
2254 break;
2255 if (!timeout)
2256 break;
2257 if (file->f_flags & O_NONBLOCK) {
2258 retval = -EAGAIN;
2259 break;
2260 }
2261 if (signal_pending(current)) {
2262 retval = -ERESTARTSYS;
2263 break;
2264 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002265 up_read(&tty->termios_rwsem);
2266
Linus Torvalds37da7bb2014-12-14 15:23:32 -08002267 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2268 timeout);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002269
2270 down_read(&tty->termios_rwsem);
2271 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002274 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002275 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurley40d5e092013-06-15 10:21:17 -04002276 if (retval == -EAGAIN) {
2277 retval = 0;
2278 continue;
2279 } else if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281 } else {
2282 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002283
2284 /* Deal with packet mode. */
2285 if (packet && b == buf) {
2286 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
2287 retval = -EFAULT;
2288 b--;
2289 break;
2290 }
2291 nr--;
2292 }
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 uncopied = copy_from_read_buf(tty, &b, &nr);
2295 uncopied += copy_from_read_buf(tty, &b, &nr);
2296 if (uncopied) {
2297 retval = -EFAULT;
2298 break;
2299 }
2300 }
2301
Peter Hurley6367ca72013-06-15 09:14:33 -04002302 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (b - buf >= minimum)
2305 break;
2306 if (time)
2307 timeout = time;
2308 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002309 if (tail != ldata->read_tail)
2310 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002311 up_read(&tty->termios_rwsem);
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 remove_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002315 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Peter Hurleyaebf04532013-11-07 14:01:57 -05002317 mutex_unlock(&ldata->atomic_read_lock);
2318
Peter Hurley40d5e092013-06-15 10:21:17 -04002319 if (b - buf)
2320 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 return retval;
2323}
2324
2325/**
Alan Cox11a96d12008-10-13 10:46:24 +01002326 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 * @tty: tty device
2328 * @file: file object
2329 * @buf: userspace buffer pointer
2330 * @nr: size of I/O
2331 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002332 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002334 * and other such events. Since the receive code will echo characters,
2335 * thus calling driver write methods, the output_lock is used in
2336 * the output processing functions called here as well as in the
2337 * echo processing function to protect the column state and space
2338 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 *
2340 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002341 *
2342 * Locking: output_lock to protect column state and space left
2343 * (note that the process_output*() functions take this
2344 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
Alan Cox4edf1822008-02-08 04:18:44 -08002346
Alan Cox11a96d12008-10-13 10:46:24 +01002347static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002348 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002351 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 int c;
2353 ssize_t retval = 0;
2354
2355 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2356 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2357 retval = tty_check_change(tty);
2358 if (retval)
2359 return retval;
2360 }
2361
Peter Hurley9356b532013-06-15 09:14:24 -04002362 down_read(&tty->termios_rwsem);
2363
Joe Petersona88a69c2009-01-02 13:40:53 +00002364 /* Write out any echoed characters that are still pending */
2365 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 add_wait_queue(&tty->write_wait, &wait);
2368 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (signal_pending(current)) {
2370 retval = -ERESTARTSYS;
2371 break;
2372 }
2373 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2374 retval = -EIO;
2375 break;
2376 }
Peter Hurley582f5592013-05-17 12:49:48 -04002377 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002379 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (num < 0) {
2381 if (num == -EAGAIN)
2382 break;
2383 retval = num;
2384 goto break_out;
2385 }
2386 b += num;
2387 nr -= num;
2388 if (nr == 0)
2389 break;
2390 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002391 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
2393 b++; nr--;
2394 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002395 if (tty->ops->flush_chars)
2396 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002398 struct n_tty_data *ldata = tty->disc_data;
2399
Roman Zippeld6afe272005-07-07 17:56:55 -07002400 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002401 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002402 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002403 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002404 if (c < 0) {
2405 retval = c;
2406 goto break_out;
2407 }
2408 if (!c)
2409 break;
2410 b += c;
2411 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
2414 if (!nr)
2415 break;
2416 if (file->f_flags & O_NONBLOCK) {
2417 retval = -EAGAIN;
2418 break;
2419 }
Peter Hurley9356b532013-06-15 09:14:24 -04002420 up_read(&tty->termios_rwsem);
2421
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002422 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002423
2424 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002428 if (b - buf != nr && tty->fasync)
2429 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002430 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return (b - buf) ? b - buf : retval;
2432}
2433
2434/**
Alan Cox11a96d12008-10-13 10:46:24 +01002435 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * @tty: terminal device
2437 * @file: file accessing it
2438 * @wait: poll table
2439 *
2440 * Called when the line discipline is asked to poll() for data or
2441 * for special events. This code is not serialized with respect to
2442 * other events save open/close.
2443 *
2444 * This code must be sure never to sleep through a hangup.
2445 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
Alan Cox4edf1822008-02-08 04:18:44 -08002447
Alan Cox11a96d12008-10-13 10:46:24 +01002448static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002449 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002451 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 unsigned int mask = 0;
2453
2454 poll_wait(file, &tty->read_wait, wait);
2455 poll_wait(file, &tty->write_wait, wait);
Peter Hurley1a486322015-04-13 13:24:34 -04002456 if (check_other_done(tty))
2457 mask |= POLLHUP;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002458 if (input_available_p(tty, 1))
2459 mask |= POLLIN | POLLRDNORM;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002460 if (tty->packet && tty->link->ctrl_status)
2461 mask |= POLLPRI | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tty_hung_up_p(file))
2463 mask |= POLLHUP;
2464 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2465 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002466 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002468 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002470 if (tty->ops->write && !tty_is_writelocked(tty) &&
2471 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2472 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 mask |= POLLOUT | POLLWRNORM;
2474 return mask;
2475}
2476
Jiri Slaby57c94122012-10-18 22:26:43 +02002477static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002478{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002479 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002480
Peter Hurleya73d3d62013-06-15 09:14:25 -04002481 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002482 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002483 head = ldata->canon_head;
2484 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002485 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002486 /* Skip EOF-chars.. */
2487 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002488 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2489 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002490 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002491 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002492 }
2493 return nr;
2494}
2495
2496static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2497 unsigned int cmd, unsigned long arg)
2498{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002499 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002500 int retval;
2501
2502 switch (cmd) {
2503 case TIOCOUTQ:
2504 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2505 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002506 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002507 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002508 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002509 else
2510 retval = read_cnt(ldata);
2511 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002512 return put_user(retval, (unsigned int __user *) arg);
2513 default:
2514 return n_tty_ioctl_helper(tty, file, cmd, arg);
2515 }
2516}
2517
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002518static void n_tty_fasync(struct tty_struct *tty, int on)
2519{
2520 struct n_tty_data *ldata = tty->disc_data;
2521
2522 if (!waitqueue_active(&tty->read_wait)) {
2523 if (on)
2524 ldata->minimum_to_wake = 1;
2525 else if (!tty->fasync)
2526 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2527 }
2528}
2529
Alan Coxa352def2008-07-16 21:53:12 +01002530struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002531 .magic = TTY_LDISC_MAGIC,
2532 .name = "n_tty",
2533 .open = n_tty_open,
2534 .close = n_tty_close,
2535 .flush_buffer = n_tty_flush_buffer,
2536 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002537 .read = n_tty_read,
2538 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002539 .ioctl = n_tty_ioctl,
2540 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002541 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002542 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002543 .write_wakeup = n_tty_write_wakeup,
2544 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002545 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002547
2548/**
2549 * n_tty_inherit_ops - inherit N_TTY methods
2550 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2551 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002552 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002553 * methods.
2554 */
2555
2556void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2557{
2558 *ops = tty_ldisc_N_TTY;
2559 ops->owner = NULL;
2560 ops->refcount = ops->flags = 0;
2561}
2562EXPORT_SYMBOL_GPL(n_tty_inherit_ops);