blob: 086fd9983caa4139c401506883687f02b297292f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
Alan Cox4edf1822008-02-08 04:18:44 -080011 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
Alan Cox4edf1822008-02-08 04:18:44 -080023 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010029 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070048#include <linux/audit.h>
49#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000050#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080051#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050052#include <linux/ratelimit.h>
Peter Hurley86e35ae2013-07-24 09:30:05 -040053#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* number of characters left in xmit buffer before select has we have room */
57#define WAKEUP_CHARS 256
58
59/*
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
63 */
64#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010065#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Joe Petersona88a69c2009-01-02 13:40:53 +000067/*
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
71 * codes.
72 */
73#define ECHO_OP_START 0xff
74#define ECHO_OP_MOVE_BACK_COL 0x80
75#define ECHO_OP_SET_CANON_COL 0x81
76#define ECHO_OP_ERASE_TAB 0x82
77
Peter Hurleycbfd0342013-06-15 10:04:26 -040078#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
Peter Hurley32f13522013-06-15 09:14:17 -040083#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
87# define n_tty_trace(f, args...)
88#endif
89
Jiri Slaby70ece7a2012-10-18 22:26:38 +020090struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040091 /* producer-published */
92 size_t read_head;
Peter Hurley70aca712015-01-16 15:05:37 -050093 size_t commit_head;
Peter Hurleyfb7aa032013-06-15 09:14:30 -040094 size_t canon_head;
Peter Hurley9dfd16d2013-06-15 10:04:29 -040095 size_t echo_head;
96 size_t echo_commit;
Peter Hurley1075a6e2013-12-09 18:06:07 -050097 size_t echo_mark;
Peter Hurley1bb9d562013-06-15 10:21:20 -040098 DECLARE_BITMAP(char_map, 256);
Peter Hurleyfb7aa032013-06-15 09:14:30 -040099
100 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200101 unsigned long overrun_time;
102 int num_overrun;
103
Peter Hurley24a89d12013-06-15 09:14:15 -0400104 /* non-atomic */
105 bool no_room;
106
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400107 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
Peter Hurley4d0ed182013-12-10 17:12:02 -0500109 unsigned char push:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200110
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400111 /* shared by producer and consumer */
Peter Hurley20bafb32013-06-15 10:21:19 -0400112 char read_buf[N_TTY_BUF_SIZE];
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Peter Hurley20bafb32013-06-15 10:21:19 -0400114 unsigned char echo_buf[N_TTY_BUF_SIZE];
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200115
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400116 /* consumer-published */
117 size_t read_tail;
Peter Hurley40d5e092013-06-15 10:21:17 -0400118 size_t line_start;
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400119
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400120 /* protected by output lock */
121 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200122 unsigned int canon_column;
Peter Hurley9dfd16d2013-06-15 10:04:29 -0400123 size_t echo_tail;
Jiri Slabybddc7152012-10-18 22:26:42 +0200124
125 struct mutex atomic_read_lock;
126 struct mutex output_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200127};
128
Peter Hurleyce741172013-06-15 09:14:20 -0400129static inline size_t read_cnt(struct n_tty_data *ldata)
130{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400131 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400132}
133
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400134static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
135{
136 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
137}
138
139static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
140{
141 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
142}
143
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400144static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
145{
146 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
147}
148
149static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
150{
151 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
152}
153
Peter Hurley679e7c22015-11-27 14:11:02 -0500154static int tty_copy_to_user(struct tty_struct *tty, void __user *to,
155 size_t tail, size_t n)
Laura Abbott72586c62015-05-14 11:42:17 -0700156{
157 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley679e7c22015-11-27 14:11:02 -0500158 size_t size = N_TTY_BUF_SIZE - tail;
159 const void *from = read_buf_addr(ldata, tail);
160 int uncopied;
161
162 if (n > size) {
Peter Hurley309426a2016-01-09 22:55:27 -0800163 tty_audit_add_data(tty, from, size);
Peter Hurley679e7c22015-11-27 14:11:02 -0500164 uncopied = copy_to_user(to, from, size);
165 if (uncopied)
166 return uncopied;
167 to += size;
168 n -= size;
169 from = ldata->read_buf;
170 }
Laura Abbott72586c62015-05-14 11:42:17 -0700171
Peter Hurley309426a2016-01-09 22:55:27 -0800172 tty_audit_add_data(tty, from, n);
Laura Abbott72586c62015-05-14 11:42:17 -0700173 return copy_to_user(to, from, n);
174}
175
Peter Hurley24a89d12013-06-15 09:14:15 -0400176/**
Peter Hurley2c5dc462015-01-16 15:05:34 -0500177 * n_tty_kick_worker - start input worker (if required)
Peter Hurley24a89d12013-06-15 09:14:15 -0400178 * @tty: terminal
179 *
Peter Hurley2c5dc462015-01-16 15:05:34 -0500180 * Re-schedules the flip buffer work if it may have stopped
Peter Hurley24a89d12013-06-15 09:14:15 -0400181 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400182 * Caller holds exclusive termios_rwsem
183 * or
184 * n_tty_read()/consumer path:
185 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400186 */
187
Peter Hurley2c5dc462015-01-16 15:05:34 -0500188static void n_tty_kick_worker(struct tty_struct *tty)
Peter Hurley7879a9f2013-06-15 07:28:31 -0400189{
Peter Hurley24a89d12013-06-15 09:14:15 -0400190 struct n_tty_data *ldata = tty->disc_data;
191
Peter Hurley2c5dc462015-01-16 15:05:34 -0500192 /* Did the input worker stop? Restart it */
193 if (unlikely(ldata->no_room)) {
Peter Hurley24a89d12013-06-15 09:14:15 -0400194 ldata->no_room = 0;
195
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200196 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400197 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400198 /* see if ldisc has been killed - if so, this means that
199 * even though the ldisc has been halted and ->buf.work
200 * cancelled, ->buf.work is about to be rescheduled
201 */
202 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
203 "scheduling buffer work for halted ldisc\n");
Peter Hurleye1760582015-10-17 16:36:23 -0400204 tty_buffer_restart_work(tty->port);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200205 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900206}
207
Peter Hurley9a4aec22013-06-15 09:14:32 -0400208static ssize_t chars_in_buffer(struct tty_struct *tty)
209{
210 struct n_tty_data *ldata = tty->disc_data;
211 ssize_t n = 0;
212
213 if (!ldata->icanon)
Peter Hurley70aca712015-01-16 15:05:37 -0500214 n = ldata->commit_head - ldata->read_tail;
Peter Hurley9a4aec22013-06-15 09:14:32 -0400215 else
216 n = ldata->canon_head - ldata->read_tail;
217 return n;
218}
219
Peter Hurleyee0bab82013-06-15 09:14:34 -0400220/**
221 * n_tty_write_wakeup - asynchronous I/O notifier
222 * @tty: tty device
223 *
224 * Required for the ptys, serial driver etc. since processes
225 * that attach themselves to the master and rely on ASYNC
226 * IO must be woken up
227 */
228
229static void n_tty_write_wakeup(struct tty_struct *tty)
230{
Peter Hurley7bccc362016-01-09 21:45:12 -0800231 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
232 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Peter Hurleyee0bab82013-06-15 09:14:34 -0400233}
234
Peter Hurley4a23a4d2013-06-15 10:21:22 -0400235static void n_tty_check_throttle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400236{
Peter Hurleya3428462015-01-16 15:05:35 -0500237 struct n_tty_data *ldata = tty->disc_data;
238
Peter Hurley6367ca72013-06-15 09:14:33 -0400239 /*
240 * Check the remaining room for the input canonicalization
241 * mode. We don't want to throttle the driver if we're in
242 * canonical mode and don't have a newline yet!
243 */
Peter Hurleya3428462015-01-16 15:05:35 -0500244 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
245 return;
246
Peter Hurley6367ca72013-06-15 09:14:33 -0400247 while (1) {
248 int throttled;
249 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley5e28cca2015-01-16 15:05:36 -0500250 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
Peter Hurley6367ca72013-06-15 09:14:33 -0400251 break;
252 throttled = tty_throttle_safe(tty);
253 if (!throttled)
254 break;
255 }
256 __tty_set_flow_change(tty, 0);
257}
258
Peter Hurley4b293492013-07-24 08:29:55 -0400259static void n_tty_check_unthrottle(struct tty_struct *tty)
Peter Hurley6367ca72013-06-15 09:14:33 -0400260{
Peter Hurley6d27a632016-01-10 22:40:56 -0800261 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
Peter Hurley3afb1b392013-07-23 08:47:30 -0400262 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
263 return;
264 if (!tty->count)
265 return;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500266 n_tty_kick_worker(tty);
Peter Hurley6d27a632016-01-10 22:40:56 -0800267 tty_wakeup(tty->link);
Peter Hurley3afb1b392013-07-23 08:47:30 -0400268 return;
269 }
270
Peter Hurley6367ca72013-06-15 09:14:33 -0400271 /* If there is enough space in the read buffer now, let the
272 * low-level driver know. We use chars_in_buffer() to
273 * check the buffer, as it now knows about canonical mode.
274 * Otherwise, if the driver is throttled and the line is
275 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
276 * we won't get any more characters.
277 */
278
279 while (1) {
280 int unthrottled;
281 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
282 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
283 break;
284 if (!tty->count)
285 break;
Peter Hurley2c5dc462015-01-16 15:05:34 -0500286 n_tty_kick_worker(tty);
Peter Hurley6367ca72013-06-15 09:14:33 -0400287 unthrottled = tty_unthrottle_safe(tty);
288 if (!unthrottled)
289 break;
290 }
291 __tty_set_flow_change(tty, 0);
292}
293
Alan Cox17b82062008-10-13 10:45:06 +0100294/**
295 * put_tty_queue - add character to tty
296 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200297 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100298 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400299 * Add a character to the tty read_buf queue.
300 *
301 * n_tty_receive_buf()/producer path:
302 * caller holds non-exclusive termios_rwsem
Alan Cox17b82062008-10-13 10:45:06 +0100303 */
304
Peter Hurley19e2ad62013-07-24 08:29:54 -0400305static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Christian Riesch8bfbe2d2014-11-13 05:53:26 +0100307 *read_buf_addr(ldata, ldata->read_head) = c;
308 ldata->read_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * reset_buffer_flags - reset buffer state
313 * @tty: terminal to reset
314 *
Peter Hurley25518c62013-03-11 16:44:31 -0400315 * Reset the read buffer counters and clear the flags.
316 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100317 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400318 * Locking: caller holds exclusive termios_rwsem
319 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000321
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400322static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400324 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Peter Hurley17bd7902013-06-15 10:04:24 -0400325 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
Peter Hurley70aca712015-01-16 15:05:37 -0500326 ldata->commit_head = 0;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500327 ldata->echo_mark = 0;
Peter Hurley40d5e092013-06-15 10:21:17 -0400328 ldata->line_start = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000329
Peter Hurleya73d3d62013-06-15 09:14:25 -0400330 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200331 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -0500332 ldata->push = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Peter Hurleya30737a2013-03-11 16:44:22 -0400335static void n_tty_packet_mode_flush(struct tty_struct *tty)
336{
337 unsigned long flags;
338
Peter Hurleya30737a2013-03-11 16:44:22 -0400339 if (tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400340 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya30737a2013-03-11 16:44:22 -0400341 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400342 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +0000343 wake_up_interruptible(&tty->link->read_wait);
Peter Hurleya30737a2013-03-11 16:44:22 -0400344 }
Peter Hurleya30737a2013-03-11 16:44:22 -0400345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/**
348 * n_tty_flush_buffer - clean input queue
349 * @tty: terminal device
350 *
Peter Hurley25518c62013-03-11 16:44:31 -0400351 * Flush the input buffer. Called when the tty layer wants the
352 * buffer flushed (eg at hangup) or when the N_TTY line discipline
353 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400355 * Holds termios_rwsem to exclude producer/consumer while
356 * buffer indices are reset.
357 *
358 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Alan Cox4edf1822008-02-08 04:18:44 -0800360
361static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400363 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400364 reset_buffer_flags(tty->disc_data);
Peter Hurley2c5dc462015-01-16 15:05:34 -0500365 n_tty_kick_worker(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800366
Peter Hurleya30737a2013-03-11 16:44:22 -0400367 if (tty->link)
368 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400369 up_write(&tty->termios_rwsem);
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * is_utf8_continuation - utf8 multibyte check
374 * @c: byte to check
375 *
376 * Returns true if the utf8 character 'c' is a multibyte continuation
377 * character. We use this to correctly compute the on screen size
378 * of the character when printing
379 */
Alan Cox4edf1822008-02-08 04:18:44 -0800380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static inline int is_utf8_continuation(unsigned char c)
382{
383 return (c & 0xc0) == 0x80;
384}
385
386/**
387 * is_continuation - multibyte check
388 * @c: byte to check
389 *
390 * Returns true if the utf8 character 'c' is a multibyte continuation
391 * character and the terminal is in unicode mode.
392 */
Alan Cox4edf1822008-02-08 04:18:44 -0800393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static inline int is_continuation(unsigned char c, struct tty_struct *tty)
395{
396 return I_IUTF8(tty) && is_utf8_continuation(c);
397}
398
399/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000400 * do_output_char - output one character
401 * @c: character (or partial unicode symbol)
402 * @tty: terminal device
403 * @space: space available in tty driver write buffer
404 *
405 * This is a helper function that handles one output character
406 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600407 * doing OPOST processing and putting the results in the
408 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000409 *
410 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
411 * and NLDLY. They simply aren't relevant in the world today.
412 * If you ever need them, add them here.
413 *
414 * Returns the number of bytes of buffer space used or -1 if
415 * no space left.
416 *
417 * Locking: should be called under the output_lock to protect
418 * the column state and space left in the buffer
419 */
420
421static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
422{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200423 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000424 int spaces;
425
426 if (!space)
427 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000428
Joe Petersona88a69c2009-01-02 13:40:53 +0000429 switch (c) {
430 case '\n':
431 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200432 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000433 if (O_ONLCR(tty)) {
434 if (space < 2)
435 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200436 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700437 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 return 2;
439 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200440 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000441 break;
442 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200443 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000444 return 0;
445 if (O_OCRNL(tty)) {
446 c = '\n';
447 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200448 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000449 break;
450 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200451 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 break;
453 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200454 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000455 if (O_TABDLY(tty) == XTABS) {
456 if (space < spaces)
457 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200458 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000459 tty->ops->write(tty, " ", spaces);
460 return spaces;
461 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200462 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000463 break;
464 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200465 if (ldata->column > 0)
466 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000467 break;
468 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000469 if (!iscntrl(c)) {
470 if (O_OLCUC(tty))
471 c = toupper(c);
472 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200473 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000474 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000475 break;
476 }
477
478 tty_put_char(tty, c);
479 return 1;
480}
481
482/**
483 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * @c: character (or partial unicode symbol)
485 * @tty: terminal device
486 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600487 * Output one character with OPOST processing.
488 * Returns -1 when the output device is full and the character
489 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000491 * Locking: output_lock to protect column state and space left
492 * (also, this is called from n_tty_write under the
493 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Alan Cox4edf1822008-02-08 04:18:44 -0800495
Joe Petersona88a69c2009-01-02 13:40:53 +0000496static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jiri Slabybddc7152012-10-18 22:26:42 +0200498 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000499 int space, retval;
500
Jiri Slabybddc7152012-10-18 22:26:42 +0200501 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alan Coxf34d7a52008-04-30 00:54:13 -0700503 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000504 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jiri Slabybddc7152012-10-18 22:26:42 +0200506 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000507 if (retval < 0)
508 return -1;
509 else
510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000514 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600516 * @buf: character buffer
517 * @nr: number of bytes to output
518 *
519 * Output a block of characters with OPOST processing.
520 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *
522 * This path is used to speed up block console writes, among other
523 * things when processing blocks of output data. It handles only
524 * the simple cases normally found and helps to generate blocks of
525 * symbols for the console driver and thus improve performance.
526 *
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 ssize_t process_output_block(struct tty_struct *tty,
533 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200535 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 const unsigned char *cp;
539
Jiri Slabybddc7152012-10-18 22:26:42 +0200540 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000541
Alan Coxf34d7a52008-04-30 00:54:13 -0700542 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000543 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200544 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (nr > space)
548 nr = space;
549
550 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000551 unsigned char c = *cp;
552
553 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 case '\n':
555 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200556 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (O_ONLCR(tty))
558 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200559 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200562 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto break_out;
564 if (O_OCRNL(tty))
565 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200566 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 case '\t':
569 goto break_out;
570 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200571 if (ldata->column > 0)
572 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000575 if (!iscntrl(c)) {
576 if (O_OLCUC(tty))
577 goto break_out;
578 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200579 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 }
583 }
584break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700585 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000586
Jiri Slabybddc7152012-10-18 22:26:42 +0200587 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return i;
589}
590
Joe Petersona88a69c2009-01-02 13:40:53 +0000591/**
592 * process_echoes - write pending echo characters
593 * @tty: terminal device
594 *
595 * Write previously buffered echo (and other ldisc-generated)
596 * characters to the tty.
597 *
598 * Characters generated by the ldisc (including echoes) need to
599 * be buffered because the driver's write buffer can fill during
600 * heavy program output. Echoing straight to the driver will
601 * often fail under these conditions, causing lost characters and
602 * resulting mismatches of ldisc state information.
603 *
604 * Since the ldisc state must represent the characters actually sent
605 * to the driver at the time of the write, operations like certain
606 * changes in column state are also saved in the buffer and executed
607 * here.
608 *
609 * A circular fifo buffer is used so that the most recent characters
610 * are prioritized. Also, when control characters are echoed with a
611 * prefixed "^", the pair is treated atomically and thus not separated.
612 *
Peter Hurley019ebdf2013-06-15 10:04:25 -0400613 * Locking: callers must hold output_lock
Joe Petersona88a69c2009-01-02 13:40:53 +0000614 */
615
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400616static size_t __process_echoes(struct tty_struct *tty)
Joe Petersona88a69c2009-01-02 13:40:53 +0000617{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200618 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400619 int space, old_space;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400620 size_t tail;
Joe Petersona88a69c2009-01-02 13:40:53 +0000621 unsigned char c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000622
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400623 old_space = space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000624
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400625 tail = ldata->echo_tail;
Peter Hurley29c7c5c2013-06-15 10:04:28 -0400626 while (ldata->echo_commit != tail) {
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400627 c = echo_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 if (c == ECHO_OP_START) {
629 unsigned char op;
Joe Petersona88a69c2009-01-02 13:40:53 +0000630 int no_space_left = 0;
631
632 /*
633 * If the buffer byte is the start of a multi-byte
634 * operation, get the next byte, which is either the
635 * op code or a control character value.
636 */
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400637 op = echo_buf(ldata, tail + 1);
Alan Cox300a6202009-01-02 13:41:04 +0000638
Joe Petersona88a69c2009-01-02 13:40:53 +0000639 switch (op) {
640 unsigned int num_chars, num_bs;
641
642 case ECHO_OP_ERASE_TAB:
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400643 num_chars = echo_buf(ldata, tail + 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000644
645 /*
646 * Determine how many columns to go back
647 * in order to erase the tab.
648 * This depends on the number of columns
649 * used by other characters within the tab
650 * area. If this (modulo 8) count is from
651 * the start of input rather than from a
652 * previous tab, we offset by canon column.
653 * Otherwise, tab spacing is normal.
654 */
655 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200656 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000657 num_bs = 8 - (num_chars & 7);
658
659 if (num_bs > space) {
660 no_space_left = 1;
661 break;
662 }
663 space -= num_bs;
664 while (num_bs--) {
665 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200666 if (ldata->column > 0)
667 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000668 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400669 tail += 3;
Joe Petersona88a69c2009-01-02 13:40:53 +0000670 break;
671
672 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200673 ldata->canon_column = ldata->column;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400674 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000675 break;
676
677 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200678 if (ldata->column > 0)
679 ldata->column--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400680 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 break;
682
683 case ECHO_OP_START:
684 /* This is an escaped echo op start code */
685 if (!space) {
686 no_space_left = 1;
687 break;
688 }
689 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200690 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000691 space--;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400692 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 break;
694
695 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000696 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600697 * If the op is not a special byte code,
698 * it is a ctrl char tagged to be echoed
699 * as "^X" (where X is the letter
700 * representing the control char).
701 * Note that we must ensure there is
702 * enough space for the whole ctrl pair.
703 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 */
Joe Peterson62b26352009-09-09 15:03:47 -0600705 if (space < 2) {
706 no_space_left = 1;
707 break;
708 }
709 tty_put_char(tty, '^');
710 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200711 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600712 space -= 2;
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400713 tail += 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000714 }
715
716 if (no_space_left)
717 break;
718 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400719 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600720 int retval = do_output_char(c, tty, space);
721 if (retval < 0)
722 break;
723 space -= retval;
724 } else {
725 if (!space)
726 break;
727 tty_put_char(tty, c);
728 space -= 1;
729 }
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400730 tail += 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000731 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000732 }
733
Peter Hurleycbfd0342013-06-15 10:04:26 -0400734 /* If the echo buffer is nearly full (so that the possibility exists
735 * of echo overrun before the next commit), then discard enough
736 * data at the tail to prevent a subsequent overrun */
737 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
Roel Kluinc476f652013-10-11 22:08:49 +0200738 if (echo_buf(ldata, tail) == ECHO_OP_START) {
Peter Hurley6f222532013-11-08 09:42:18 -0500739 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400740 tail += 3;
741 else
742 tail += 2;
743 } else
744 tail++;
745 }
746
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400747 ldata->echo_tail = tail;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400748 return old_space - space;
Joe Petersona88a69c2009-01-02 13:40:53 +0000749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Peter Hurley17bd7902013-06-15 10:04:24 -0400751static void commit_echoes(struct tty_struct *tty)
752{
753 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400754 size_t nr, old, echoed;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400755 size_t head;
756
757 head = ldata->echo_head;
Peter Hurley1075a6e2013-12-09 18:06:07 -0500758 ldata->echo_mark = head;
Peter Hurleycbfd0342013-06-15 10:04:26 -0400759 old = ldata->echo_commit - ldata->echo_tail;
760
761 /* Process committed echoes if the accumulated # of bytes
762 * is over the threshold (and try again each time another
763 * block is accumulated) */
764 nr = head - ldata->echo_tail;
765 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
766 return;
Peter Hurley17bd7902013-06-15 10:04:24 -0400767
Peter Hurley019ebdf2013-06-15 10:04:25 -0400768 mutex_lock(&ldata->output_lock);
Peter Hurleycbfd0342013-06-15 10:04:26 -0400769 ldata->echo_commit = head;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400770 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400771 mutex_unlock(&ldata->output_lock);
772
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400773 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400774 tty->ops->flush_chars(tty);
775}
776
777static void process_echoes(struct tty_struct *tty)
778{
779 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400780 size_t echoed;
Peter Hurley019ebdf2013-06-15 10:04:25 -0400781
Peter Hurleye2613be2014-02-11 16:34:55 -0500782 if (ldata->echo_mark == ldata->echo_tail)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400783 return;
784
785 mutex_lock(&ldata->output_lock);
Peter Hurley1075a6e2013-12-09 18:06:07 -0500786 ldata->echo_commit = ldata->echo_mark;
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400787 echoed = __process_echoes(tty);
Peter Hurley019ebdf2013-06-15 10:04:25 -0400788 mutex_unlock(&ldata->output_lock);
789
Peter Hurleybc5b1ec2013-06-15 10:04:27 -0400790 if (echoed && tty->ops->flush_chars)
Peter Hurley019ebdf2013-06-15 10:04:25 -0400791 tty->ops->flush_chars(tty);
Peter Hurley17bd7902013-06-15 10:04:24 -0400792}
793
Peter Hurley1075a6e2013-12-09 18:06:07 -0500794/* NB: echo_mark and echo_head should be equivalent here */
Peter Hurleycbfd0342013-06-15 10:04:26 -0400795static void flush_echoes(struct tty_struct *tty)
796{
797 struct n_tty_data *ldata = tty->disc_data;
798
Peter Hurley39434ab2013-11-29 12:56:10 -0500799 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
800 ldata->echo_commit == ldata->echo_head)
Peter Hurleycbfd0342013-06-15 10:04:26 -0400801 return;
802
803 mutex_lock(&ldata->output_lock);
804 ldata->echo_commit = ldata->echo_head;
805 __process_echoes(tty);
806 mutex_unlock(&ldata->output_lock);
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000810 * add_echo_byte - add a byte to the echo buffer
811 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200812 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000813 *
814 * Add a character or operation byte to the echo buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000815 */
816
Peter Hurleycbfd0342013-06-15 10:04:26 -0400817static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000818{
Peter Hurleyaddaebc2013-06-15 10:04:22 -0400819 *echo_buf_addr(ldata, ldata->echo_head++) = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000820}
821
822/**
823 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200824 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000825 *
826 * Add an operation to the echo buffer to move back one column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000827 */
828
Jiri Slaby57c94122012-10-18 22:26:43 +0200829static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000830{
Jiri Slaby57c94122012-10-18 22:26:43 +0200831 add_echo_byte(ECHO_OP_START, ldata);
832 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000833}
834
835/**
836 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200837 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 *
839 * Add an operation to the echo buffer to set the canon column
840 * to the current column.
Joe Petersona88a69c2009-01-02 13:40:53 +0000841 */
842
Jiri Slaby57c94122012-10-18 22:26:43 +0200843static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000844{
Jiri Slaby57c94122012-10-18 22:26:43 +0200845 add_echo_byte(ECHO_OP_START, ldata);
846 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000847}
848
849/**
850 * echo_erase_tab - add operation to erase a tab
851 * @num_chars: number of character columns already used
852 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200853 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000854 *
855 * Add an operation to the echo buffer to erase a tab.
856 *
857 * Called by the eraser function, which knows how many character
858 * columns have been used since either a previous tab or the start
859 * of input. This information will be used later, along with
860 * canon column (if applicable), to go back the correct number
861 * of columns.
Joe Petersona88a69c2009-01-02 13:40:53 +0000862 */
863
864static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200865 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_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000869
870 /* We only need to know this modulo 8 (tab spacing) */
871 num_chars &= 7;
872
873 /* Set the high bit as a flag if num_chars is after a previous tab */
874 if (after_tab)
875 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000876
Jiri Slaby57c94122012-10-18 22:26:43 +0200877 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000878}
879
880/**
881 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * @c: unicode byte to echo
883 * @tty: terminal device
884 *
Alan Cox4edf1822008-02-08 04:18:44 -0800885 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100887 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000888 * This variant does not treat control characters specially.
Joe Petersona88a69c2009-01-02 13:40:53 +0000889 */
890
Jiri Slaby57c94122012-10-18 22:26:43 +0200891static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000892{
Joe Petersona88a69c2009-01-02 13:40:53 +0000893 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200894 add_echo_byte(ECHO_OP_START, ldata);
895 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000896 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200897 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000898 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000899}
900
901/**
902 * echo_char - echo a character
903 * @c: unicode byte to echo
904 * @tty: terminal device
905 *
906 * Echo user input back onto the screen. This must be called only when
907 * L_ECHO(tty) is true. Called from the driver receive_buf path.
908 *
Joe Peterson62b26352009-09-09 15:03:47 -0600909 * This variant tags control characters to be echoed as "^X"
910 * (where X is the letter representing the control char).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912
913static void echo_char(unsigned char c, struct tty_struct *tty)
914{
Jiri Slabybddc7152012-10-18 22:26:42 +0200915 struct n_tty_data *ldata = tty->disc_data;
916
Joe Petersona88a69c2009-01-02 13:40:53 +0000917 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200918 add_echo_byte(ECHO_OP_START, ldata);
919 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000920 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600921 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200922 add_echo_byte(ECHO_OP_START, ldata);
923 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Alan Cox17b82062008-10-13 10:45:06 +0100927/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000928 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200929 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100930 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000931
Jiri Slaby57c94122012-10-18 22:26:43 +0200932static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200934 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200935 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200936 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938}
939
940/**
941 * eraser - handle erase function
942 * @c: character input
943 * @tty: terminal device
944 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200945 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * present in the stream from the driver layer. Handles the complexities
947 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100948 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400949 * n_tty_receive_buf()/producer path:
950 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Alan Cox4edf1822008-02-08 04:18:44 -0800952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static void eraser(unsigned char c, struct tty_struct *tty)
954{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200955 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400957 size_t head;
958 size_t cnt;
959 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200961 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000962 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return;
964 }
965 if (c == ERASE_CHAR(tty))
966 kill_type = ERASE;
967 else if (c == WERASE_CHAR(tty))
968 kill_type = WERASE;
969 else {
970 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200971 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return;
973 }
974 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200975 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +0200976 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 echo_char(KILL_CHAR(tty), tty);
978 /* Add a newline if ECHOK is on and ECHOKE is off. */
979 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200980 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return;
982 }
983 kill_type = KILL;
984 }
985
986 seen_alnums = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200987 while (ldata->read_head != ldata->canon_head) {
988 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* erase a single possibly multibyte character */
991 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400992 head--;
993 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200994 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* do not partially erase */
997 if (is_continuation(c, tty))
998 break;
999
1000 if (kill_type == WERASE) {
1001 /* Equivalent to BSD's ALTWERASE. */
1002 if (isalnum(c) || c == '_')
1003 seen_alnums++;
1004 else if (seen_alnums)
1005 break;
1006 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001007 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001008 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (L_ECHO(tty)) {
1010 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001011 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001012 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001013 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 /* if cnt > 1, output a multi-byte character */
1016 echo_char(c, tty);
1017 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001018 head++;
1019 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001020 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1023 echo_char(ERASE_CHAR(tty), tty);
1024 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001025 unsigned int num_chars = 0;
1026 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001027 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Joe Petersona88a69c2009-01-02 13:40:53 +00001029 /*
1030 * Count the columns used for characters
1031 * since the start of input or after a
1032 * previous tab.
1033 * This info is used to go back the correct
1034 * number of columns.
1035 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001036 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001037 tail--;
1038 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001039 if (c == '\t') {
1040 after_tab = 1;
1041 break;
Alan Cox300a6202009-01-02 13:41:04 +00001042 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001044 num_chars += 2;
1045 } else if (!is_continuation(c, tty)) {
1046 num_chars++;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001049 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 } else {
1051 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001052 echo_char_raw('\b', ldata);
1053 echo_char_raw(' ', ldata);
1054 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001057 echo_char_raw('\b', ldata);
1058 echo_char_raw(' ', ldata);
1059 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
1062 }
1063 if (kill_type == ERASE)
1064 break;
1065 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001066 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001067 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/**
1071 * isig - handle the ISIG optio
1072 * @sig: signal
1073 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001075 * Called when a signal is being sent due to terminal input.
1076 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001077 *
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001078 * Performs input and output flush if !NOFLSH. In this context, the echo
1079 * buffer is 'output'. The signal is processed first to alert any current
1080 * readers or writers to discontinue and exit their i/o loops.
1081 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001082 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Alan Cox4edf1822008-02-08 04:18:44 -08001084
Peter Hurley3b19e032015-06-27 09:21:32 -04001085static void __isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Peter Hurley8c985d12013-03-06 08:38:19 -05001087 struct pid *tty_pgrp = tty_get_pgrp(tty);
1088 if (tty_pgrp) {
1089 kill_pgrp(tty_pgrp, sig, 1);
1090 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Peter Hurley3b19e032015-06-27 09:21:32 -04001092}
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001093
Peter Hurley3b19e032015-06-27 09:21:32 -04001094static void isig(int sig, struct tty_struct *tty)
1095{
1096 struct n_tty_data *ldata = tty->disc_data;
1097
1098 if (L_NOFLSH(tty)) {
1099 /* signal only */
1100 __isig(sig, tty);
1101
1102 } else { /* signal and flush */
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001103 up_read(&tty->termios_rwsem);
1104 down_write(&tty->termios_rwsem);
1105
Peter Hurley3b19e032015-06-27 09:21:32 -04001106 __isig(sig, tty);
1107
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001108 /* clear echo buffer */
1109 mutex_lock(&ldata->output_lock);
1110 ldata->echo_head = ldata->echo_tail = 0;
1111 ldata->echo_mark = ldata->echo_commit = 0;
1112 mutex_unlock(&ldata->output_lock);
1113
1114 /* clear output buffer */
1115 tty_driver_flush_buffer(tty);
1116
1117 /* clear input buffer */
1118 reset_buffer_flags(tty->disc_data);
1119
1120 /* notify pty master of flush */
1121 if (tty->link)
1122 n_tty_packet_mode_flush(tty);
1123
1124 up_write(&tty->termios_rwsem);
1125 down_read(&tty->termios_rwsem);
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129/**
1130 * n_tty_receive_break - handle break
1131 * @tty: terminal
1132 *
1133 * An RS232 break event has been hit in the incoming bitstream. This
1134 * can cause a variety of events depending upon the termios settings.
1135 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001136 * n_tty_receive_buf()/producer path:
1137 * caller holds non-exclusive termios_rwsem
Peter Hurley6d76bd22013-06-15 09:14:26 -04001138 *
1139 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Alan Cox4edf1822008-02-08 04:18:44 -08001141
Peter Hurley4b293492013-07-24 08:29:55 -04001142static void n_tty_receive_break(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jiri Slaby57c94122012-10-18 22:26:43 +02001144 struct n_tty_data *ldata = tty->disc_data;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (I_IGNBRK(tty))
1147 return;
1148 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001149 isig(SIGINT, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return;
1151 }
1152 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001153 put_tty_queue('\377', ldata);
1154 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001156 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159/**
1160 * n_tty_receive_overrun - handle overrun reporting
1161 * @tty: terminal
1162 *
1163 * Data arrived faster than we could process it. While the tty
1164 * driver has flagged this the bits that were missed are gone
1165 * forever.
1166 *
1167 * Called from the receive_buf path so single threaded. Does not
1168 * need locking as num_overrun and overrun_time are function
1169 * private.
1170 */
Alan Cox4edf1822008-02-08 04:18:44 -08001171
Peter Hurley4b293492013-07-24 08:29:55 -04001172static void n_tty_receive_overrun(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001174 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001176 ldata->num_overrun++;
1177 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1178 time_after(ldata->overrun_time, jiffies)) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001179 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001180 ldata->overrun_time = jiffies;
1181 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183}
1184
1185/**
1186 * n_tty_receive_parity_error - error notifier
1187 * @tty: terminal device
1188 * @c: character
1189 *
1190 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001191 * the error case if necessary.
1192 *
1193 * n_tty_receive_buf()/producer path:
1194 * caller holds non-exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Peter Hurley4b293492013-07-24 08:29:55 -04001196static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jiri Slaby57c94122012-10-18 22:26:43 +02001198 struct n_tty_data *ldata = tty->disc_data;
1199
Peter Hurley66528f92014-06-16 08:10:42 -04001200 if (I_INPCK(tty)) {
1201 if (I_IGNPAR(tty))
1202 return;
1203 if (I_PARMRK(tty)) {
1204 put_tty_queue('\377', ldata);
1205 put_tty_queue('\0', ldata);
1206 put_tty_queue(c, ldata);
1207 } else
1208 put_tty_queue('\0', ldata);
1209 } else
Jiri Slaby57c94122012-10-18 22:26:43 +02001210 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001213static void
1214n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1215{
Peter Hurleyd2b6f442015-01-17 15:42:06 -05001216 isig(signal, tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001217 if (I_IXON(tty))
1218 start_tty(tty);
1219 if (L_ECHO(tty)) {
1220 echo_char(c, tty);
1221 commit_echoes(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001222 } else
1223 process_echoes(tty);
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001224 return;
1225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/**
1228 * n_tty_receive_char - perform processing
1229 * @tty: terminal device
1230 * @c: character
1231 *
1232 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001233 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001235 *
1236 * n_tty_receive_buf()/producer path:
1237 * caller holds non-exclusive termios_rwsem
1238 * publishes canon_head if canonical mode is active
Peter Hurleye60d27c2013-07-24 08:29:56 -04001239 *
1240 * Returns 1 if LNEXT was received, else returns 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
1242
Peter Hurleye60d27c2013-07-24 08:29:56 -04001243static int
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001244n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001246 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (I_IXON(tty)) {
1249 if (c == START_CHAR(tty)) {
1250 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001251 process_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 if (c == STOP_CHAR(tty)) {
1255 stop_tty(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 }
Joe Peterson575537b32008-04-30 00:53:30 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (L_ISIG(tty)) {
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001261 if (c == INTR_CHAR(tty)) {
1262 n_tty_receive_signal_char(tty, SIGINT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001263 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001264 } else if (c == QUIT_CHAR(tty)) {
1265 n_tty_receive_signal_char(tty, SIGQUIT, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001266 return 0;
Peter Hurleyb0ac50b2013-06-15 10:21:23 -04001267 } else if (c == SUSP_CHAR(tty)) {
1268 n_tty_receive_signal_char(tty, SIGTSTP, c);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 }
Joe Peterson575537b32008-04-30 00:53:30 -07001272
Peter Hurley855df3c2013-07-24 08:29:50 -04001273 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1274 start_tty(tty);
1275 process_echoes(tty);
1276 }
1277
Joe Peterson575537b32008-04-30 00:53:30 -07001278 if (c == '\r') {
1279 if (I_IGNCR(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001280 return 0;
Joe Peterson575537b32008-04-30 00:53:30 -07001281 if (I_ICRNL(tty))
1282 c = '\n';
1283 } else if (c == '\n' && I_INLCR(tty))
1284 c = '\r';
1285
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001286 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1288 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1289 eraser(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001290 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001294 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001296 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001298 echo_char_raw('^', ldata);
1299 echo_char_raw('\b', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001300 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001303 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001305 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001306 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Jiri Slaby57c94122012-10-18 22:26:43 +02001308 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001310 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001311 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001312 echo_char(read_buf(ldata, tail), tty);
1313 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001315 commit_echoes(tty);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001320 echo_char_raw('\n', ldata);
Peter Hurley17bd7902013-06-15 10:04:24 -04001321 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 goto handle_newline;
1324 }
1325 if (c == EOF_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 c = __DISABLED_CHAR;
1327 goto handle_newline;
1328 }
1329 if ((c == EOL_CHAR(tty)) ||
1330 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1331 /*
1332 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1333 */
1334 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001336 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001337 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 echo_char(c, tty);
Peter Hurley17bd7902013-06-15 10:04:24 -04001339 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 /*
1342 * XXX does PARMRK doubling happen for
1343 * EOL_CHAR and EOL2_CHAR?
1344 */
Peter Hurley001ba922013-12-02 14:24:44 -05001345 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001346 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Alan Cox4edf1822008-02-08 04:18:44 -08001348handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001349 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001350 put_tty_queue(c, ldata);
Peter Hurley70aca712015-01-16 15:05:37 -05001351 smp_store_release(&ldata->canon_head, ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001353 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 }
Alan Cox4edf1822008-02-08 04:18:44 -08001357
Joe Petersonacc71bb2009-01-02 13:43:32 +00001358 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001359 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001361 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 else {
1363 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001364 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001365 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 echo_char(c, tty);
1367 }
Peter Hurley17bd7902013-06-15 10:04:24 -04001368 commit_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Peter Hurley001ba922013-12-02 14:24:44 -05001371 /* PARMRK doubling check */
1372 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Jiri Slaby57c94122012-10-18 22:26:43 +02001375 put_tty_queue(c, ldata);
Peter Hurleye60d27c2013-07-24 08:29:56 -04001376 return 0;
Alan Cox4edf1822008-02-08 04:18:44 -08001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Peter Hurleye60d27c2013-07-24 08:29:56 -04001379static inline void
1380n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001381{
1382 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001383
Peter Hurleye60d27c2013-07-24 08:29:56 -04001384 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1385 start_tty(tty);
1386 process_echoes(tty);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001387 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001388 if (L_ECHO(tty)) {
1389 finish_erasing(ldata);
1390 /* Record the column of first canon char. */
1391 if (ldata->canon_head == ldata->read_head)
1392 echo_set_canon_col(ldata);
1393 echo_char(c, tty);
1394 commit_echoes(tty);
1395 }
Peter Hurley001ba922013-12-02 14:24:44 -05001396 /* PARMRK doubling check */
1397 if (c == (unsigned char) '\377' && I_PARMRK(tty))
Peter Hurleye60d27c2013-07-24 08:29:56 -04001398 put_tty_queue(c, ldata);
1399 put_tty_queue(c, ldata);
1400}
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001401
Peter Hurleyeb3e4662013-12-02 14:24:42 -05001402static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
Peter Hurleye60d27c2013-07-24 08:29:56 -04001403{
1404 n_tty_receive_char_inline(tty, c);
Peter Hurley4b1f79c2013-07-24 08:29:51 -04001405}
1406
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001407static inline void
Peter Hurley7de971b2013-07-24 08:29:53 -04001408n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1409{
1410 struct n_tty_data *ldata = tty->disc_data;
1411
Peter Hurleye60d27c2013-07-24 08:29:56 -04001412 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1413 start_tty(tty);
1414 process_echoes(tty);
Peter Hurley7de971b2013-07-24 08:29:53 -04001415 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001416 if (L_ECHO(tty)) {
1417 finish_erasing(ldata);
1418 /* Record the column of first canon char. */
1419 if (ldata->canon_head == ldata->read_head)
1420 echo_set_canon_col(ldata);
1421 echo_char(c, tty);
1422 commit_echoes(tty);
1423 }
1424 put_tty_queue(c, ldata);
Peter Hurley7de971b2013-07-24 08:29:53 -04001425}
1426
Peter Hurley8dc4b252013-12-02 14:24:43 -05001427static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001428{
1429 if (I_ISTRIP(tty))
1430 c &= 0x7f;
1431 if (I_IUCLC(tty) && L_IEXTEN(tty))
1432 c = tolower(c);
1433
1434 if (I_IXON(tty)) {
1435 if (c == STOP_CHAR(tty))
1436 stop_tty(tty);
1437 else if (c == START_CHAR(tty) ||
1438 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1439 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1440 c != SUSP_CHAR(tty))) {
1441 start_tty(tty);
1442 process_echoes(tty);
1443 }
1444 }
1445}
1446
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001447static void
1448n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1449{
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001450 switch (flag) {
1451 case TTY_BREAK:
1452 n_tty_receive_break(tty);
1453 break;
1454 case TTY_PARITY:
1455 case TTY_FRAME:
1456 n_tty_receive_parity_error(tty, c);
1457 break;
1458 case TTY_OVERRUN:
1459 n_tty_receive_overrun(tty);
1460 break;
1461 default:
Peter Hurley339f36b2015-11-08 13:01:13 -05001462 tty_err(tty, "unknown flag %d\n", flag);
Peter Hurleyd2f8d7a2013-06-15 10:21:24 -04001463 break;
1464 }
1465}
1466
Peter Hurleye60d27c2013-07-24 08:29:56 -04001467static void
1468n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1469{
1470 struct n_tty_data *ldata = tty->disc_data;
1471
1472 ldata->lnext = 0;
1473 if (likely(flag == TTY_NORMAL)) {
1474 if (I_ISTRIP(tty))
1475 c &= 0x7f;
1476 if (I_IUCLC(tty) && L_IEXTEN(tty))
1477 c = tolower(c);
1478 n_tty_receive_char(tty, c);
1479 } else
1480 n_tty_receive_char_flagged(tty, c, flag);
1481}
1482
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001483static void
1484n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1485 char *fp, int count)
1486{
1487 struct n_tty_data *ldata = tty->disc_data;
1488 size_t n, head;
1489
1490 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001491 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001492 memcpy(read_buf_addr(ldata, head), cp, n);
1493 ldata->read_head += n;
1494 cp += n;
1495 count -= n;
1496
1497 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
Peter Hurley70aca712015-01-16 15:05:37 -05001498 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001499 memcpy(read_buf_addr(ldata, head), cp, n);
1500 ldata->read_head += n;
1501}
1502
Peter Hurley554117bd2013-06-15 10:21:25 -04001503static void
1504n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1505 char *fp, int count)
1506{
1507 struct n_tty_data *ldata = tty->disc_data;
1508 char flag = TTY_NORMAL;
1509
1510 while (count--) {
1511 if (fp)
1512 flag = *fp++;
1513 if (likely(flag == TTY_NORMAL))
1514 put_tty_queue(*cp++, ldata);
1515 else
1516 n_tty_receive_char_flagged(tty, *cp++, flag);
1517 }
1518}
1519
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001520static void
1521n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1522 char *fp, int count)
1523{
1524 char flag = TTY_NORMAL;
1525
1526 while (count--) {
1527 if (fp)
1528 flag = *fp++;
1529 if (likely(flag == TTY_NORMAL))
1530 n_tty_receive_char_closing(tty, *cp++);
1531 else
1532 n_tty_receive_char_flagged(tty, *cp++, flag);
1533 }
1534}
1535
Peter Hurley7d88d632013-07-24 08:29:49 -04001536static void
1537n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
Peter Hurley6baad002013-07-24 08:29:52 -04001538 char *fp, int count)
1539{
1540 struct n_tty_data *ldata = tty->disc_data;
1541 char flag = TTY_NORMAL;
1542
1543 while (count--) {
1544 if (fp)
1545 flag = *fp++;
1546 if (likely(flag == TTY_NORMAL)) {
1547 unsigned char c = *cp++;
1548
1549 if (I_ISTRIP(tty))
1550 c &= 0x7f;
1551 if (I_IUCLC(tty) && L_IEXTEN(tty))
1552 c = tolower(c);
1553 if (L_EXTPROC(tty)) {
1554 put_tty_queue(c, ldata);
1555 continue;
1556 }
Peter Hurleye60d27c2013-07-24 08:29:56 -04001557 if (!test_bit(c, ldata->char_map))
1558 n_tty_receive_char_inline(tty, c);
1559 else if (n_tty_receive_char_special(tty, c) && count) {
1560 if (fp)
1561 flag = *fp++;
1562 n_tty_receive_char_lnext(tty, *cp++, flag);
1563 count--;
1564 }
Peter Hurley6baad002013-07-24 08:29:52 -04001565 } else
1566 n_tty_receive_char_flagged(tty, *cp++, flag);
1567 }
1568}
1569
1570static void
1571n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1572 char *fp, int count)
Peter Hurley7d88d632013-07-24 08:29:49 -04001573{
Peter Hurleye60d27c2013-07-24 08:29:56 -04001574 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley7d88d632013-07-24 08:29:49 -04001575 char flag = TTY_NORMAL;
1576
1577 while (count--) {
1578 if (fp)
1579 flag = *fp++;
Peter Hurleye60d27c2013-07-24 08:29:56 -04001580 if (likely(flag == TTY_NORMAL)) {
1581 unsigned char c = *cp++;
1582
1583 if (!test_bit(c, ldata->char_map))
1584 n_tty_receive_char_fast(tty, c);
1585 else if (n_tty_receive_char_special(tty, c) && count) {
1586 if (fp)
1587 flag = *fp++;
1588 n_tty_receive_char_lnext(tty, *cp++, flag);
1589 count--;
1590 }
1591 } else
Peter Hurley7d88d632013-07-24 08:29:49 -04001592 n_tty_receive_char_flagged(tty, *cp++, flag);
1593 }
1594}
1595
Peter Hurley24a89d12013-06-15 09:14:15 -04001596static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1597 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001599 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001600 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001602 if (ldata->real_raw)
1603 n_tty_receive_buf_real_raw(tty, cp, fp, count);
Peter Hurleya1dd30e2013-06-15 10:21:26 -04001604 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
Peter Hurley554117bd2013-06-15 10:21:25 -04001605 n_tty_receive_buf_raw(tty, cp, fp, count);
Peter Hurleyad0cc7b2013-06-15 10:21:27 -04001606 else if (tty->closing && !L_EXTPROC(tty))
1607 n_tty_receive_buf_closing(tty, cp, fp, count);
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001608 else {
Peter Hurleye60d27c2013-07-24 08:29:56 -04001609 if (ldata->lnext) {
1610 char flag = TTY_NORMAL;
1611
1612 if (fp)
1613 flag = *fp++;
1614 n_tty_receive_char_lnext(tty, *cp++, flag);
1615 count--;
1616 }
1617
Peter Hurley7de971b2013-07-24 08:29:53 -04001618 if (!preops && !I_PARMRK(tty))
Peter Hurley6baad002013-07-24 08:29:52 -04001619 n_tty_receive_buf_fast(tty, cp, fp, count);
1620 else
1621 n_tty_receive_buf_standard(tty, cp, fp, count);
Peter Hurleycbfd0342013-06-15 10:04:26 -04001622
1623 flush_echoes(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001624 if (tty->ops->flush_chars)
1625 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
Peter Hurley70aca712015-01-16 15:05:37 -05001628 if (ldata->icanon && !L_EXTPROC(tty))
1629 return;
1630
1631 /* publish read_head to consumer */
1632 smp_store_release(&ldata->commit_head, ldata->read_head);
1633
Peter Hurley33d71362016-01-09 21:45:08 -08001634 if (read_cnt(ldata)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001636 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001640/**
1641 * n_tty_receive_buf_common - process input
1642 * @tty: device to receive input
1643 * @cp: input chars
1644 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1645 * @count: number of input chars in @cp
1646 *
1647 * Called by the terminal driver when a block of characters has
1648 * been received. This function must be called from soft contexts
1649 * not from interrupt context. The driver is responsible for making
1650 * calls one at a time and in order (or using flush_to_ldisc)
1651 *
1652 * Returns the # of input chars from @cp which were processed.
1653 *
1654 * In canonical mode, the maximum line length is 4096 chars (including
1655 * the line termination char); lines longer than 4096 chars are
1656 * truncated. After 4095 chars, input data is still processed but
1657 * not stored. Overflow processing ensures the tty can always
1658 * receive more input until at least one line can be read.
1659 *
1660 * In non-canonical mode, the read buffer will only accept 4095 chars;
1661 * this provides the necessary space for a newline char if the input
1662 * mode is switched to canonical.
1663 *
1664 * Note it is possible for the read buffer to _contain_ 4096 chars
1665 * in non-canonical mode: the read buffer could already contain the
1666 * maximum canon line of 4096 chars when the mode is switched to
1667 * non-canonical.
1668 *
1669 * n_tty_receive_buf()/producer path:
1670 * claims non-exclusive termios_rwsem
1671 * publishes commit_head or canon_head
1672 */
Peter Hurley5c32d122013-12-02 14:24:41 -05001673static int
1674n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1675 char *fp, int count, int flow)
Peter Hurley24a89d12013-06-15 09:14:15 -04001676{
1677 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001678 int room, n, rcvd = 0, overflow;
Peter Hurley24a89d12013-06-15 09:14:15 -04001679
Peter Hurley9356b532013-06-15 09:14:24 -04001680 down_read(&tty->termios_rwsem);
1681
Peter Hurley19e2ad62013-07-24 08:29:54 -04001682 while (1) {
Peter Hurley70aca712015-01-16 15:05:37 -05001683 /*
Peter Hurley06c49f92015-01-16 15:05:38 -05001684 * When PARMRK is set, each input char may take up to 3 chars
1685 * in the read buf; reduce the buffer space avail by 3x
Peter Hurley70aca712015-01-16 15:05:37 -05001686 *
1687 * If we are doing input canonicalization, and there are no
1688 * pending newlines, let characters through without limit, so
1689 * that erase characters will be handled. Other excess
1690 * characters will be beeped.
1691 *
1692 * paired with store in *_copy_from_read_buf() -- guarantees
1693 * the consumer has loaded the data in read_buf up to the new
1694 * read_tail (so this producer will not overwrite unread data)
1695 */
1696 size_t tail = smp_load_acquire(&ldata->read_tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001697
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001698 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
Peter Hurley70aca712015-01-16 15:05:37 -05001699 if (I_PARMRK(tty))
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001700 room = (room + 2) / 3;
1701 room--;
1702 if (room <= 0) {
1703 overflow = ldata->icanon && ldata->canon_head == tail;
1704 if (overflow && room < 0)
1705 ldata->read_head--;
1706 room = overflow;
1707 ldata->no_room = flow && !room;
1708 } else
1709 overflow = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001710
Peter Hurley19e2ad62013-07-24 08:29:54 -04001711 n = min(count, room);
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001712 if (!n)
Peter Hurley19e2ad62013-07-24 08:29:54 -04001713 break;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001714
1715 /* ignore parity errors if handling overflow */
1716 if (!overflow || !fp || *fp != TTY_PARITY)
1717 __receive_buf(tty, cp, fp, n);
1718
Peter Hurley19e2ad62013-07-24 08:29:54 -04001719 cp += n;
1720 if (fp)
1721 fp += n;
1722 count -= n;
1723 rcvd += n;
Peter Hurley4a23a4d2013-06-15 10:21:22 -04001724 }
Peter Hurley24a89d12013-06-15 09:14:15 -04001725
Peter Hurley19e2ad62013-07-24 08:29:54 -04001726 tty->receive_room = room;
Peter Hurleyfb5ef9e2015-01-16 15:05:39 -05001727
1728 /* Unthrottle if handling overflow on pty */
1729 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1730 if (overflow) {
1731 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1732 tty_unthrottle_safe(tty);
1733 __tty_set_flow_change(tty, 0);
1734 }
1735 } else
1736 n_tty_check_throttle(tty);
1737
Peter Hurley9356b532013-06-15 09:14:24 -04001738 up_read(&tty->termios_rwsem);
1739
Peter Hurley19e2ad62013-07-24 08:29:54 -04001740 return rcvd;
Peter Hurley24a89d12013-06-15 09:14:15 -04001741}
1742
Peter Hurley5c32d122013-12-02 14:24:41 -05001743static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1744 char *fp, int count)
1745{
1746 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1747}
1748
1749static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1750 char *fp, int count)
1751{
1752 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/**
1756 * n_tty_set_termios - termios data changed
1757 * @tty: terminal
1758 * @old: previous data
1759 *
1760 * Called by the tty layer when the user changes termios flags so
1761 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001762 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * guaranteed that this function will not be re-entered or in progress
1764 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001765 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001766 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Alan Cox4edf1822008-02-08 04:18:44 -08001768
1769static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001771 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001772
Peter Hurleyc786f742013-09-17 12:53:35 -04001773 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001774 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Peter Hurley4d0ed182013-12-10 17:12:02 -05001775 ldata->line_start = ldata->read_tail;
1776 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1777 ldata->canon_head = ldata->read_tail;
1778 ldata->push = 0;
1779 } else {
1780 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1781 ldata->read_flags);
1782 ldata->canon_head = ldata->read_head;
1783 ldata->push = 1;
1784 }
Peter Hurley70aca712015-01-16 15:05:37 -05001785 ldata->commit_head = ldata->read_head;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001786 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001787 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001788 }
1789
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001790 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1793 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1794 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1795 I_PARMRK(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001796 bitmap_zero(ldata->char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (I_IGNCR(tty) || I_ICRNL(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001799 set_bit('\r', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (I_INLCR(tty))
Peter Hurley1bb9d562013-06-15 10:21:20 -04001801 set_bit('\n', ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (L_ICANON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001804 set_bit(ERASE_CHAR(tty), ldata->char_map);
1805 set_bit(KILL_CHAR(tty), ldata->char_map);
1806 set_bit(EOF_CHAR(tty), ldata->char_map);
1807 set_bit('\n', ldata->char_map);
1808 set_bit(EOL_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (L_IEXTEN(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001810 set_bit(WERASE_CHAR(tty), ldata->char_map);
1811 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1812 set_bit(EOL2_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (L_ECHO(tty))
1814 set_bit(REPRINT_CHAR(tty),
Peter Hurley1bb9d562013-06-15 10:21:20 -04001815 ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 }
1818 if (I_IXON(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001819 set_bit(START_CHAR(tty), ldata->char_map);
1820 set_bit(STOP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 if (L_ISIG(tty)) {
Peter Hurley1bb9d562013-06-15 10:21:20 -04001823 set_bit(INTR_CHAR(tty), ldata->char_map);
1824 set_bit(QUIT_CHAR(tty), ldata->char_map);
1825 set_bit(SUSP_CHAR(tty), ldata->char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Peter Hurley1bb9d562013-06-15 10:21:20 -04001827 clear_bit(__DISABLED_CHAR, ldata->char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001828 ldata->raw = 0;
1829 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001831 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1833 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1834 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001835 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001837 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001839 /*
1840 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1841 * been stopped by STOP_CHAR(tty) before it.
1842 */
Peter Hurleye2613be2014-02-11 16:34:55 -05001843 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
Wang YanQingdab73b42013-05-09 14:16:47 +08001844 start_tty(tty);
Peter Hurleye2613be2014-02-11 16:34:55 -05001845 process_echoes(tty);
1846 }
Wang YanQingdab73b42013-05-09 14:16:47 +08001847
Alan Coxf34d7a52008-04-30 00:54:13 -07001848 /* The termios change make the tty ready for I/O */
Kosuke Tatsukawae81107d2015-10-02 08:27:05 +00001849 wake_up_interruptible(&tty->write_wait);
1850 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853/**
1854 * n_tty_close - close the ldisc for this tty
1855 * @tty: device
1856 *
Alan Cox4edf1822008-02-08 04:18:44 -08001857 * Called from the terminal layer when this line discipline is
1858 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * discipline change. The function will not be called while other
1860 * ldisc methods are in progress.
1861 */
Alan Cox4edf1822008-02-08 04:18:44 -08001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static void n_tty_close(struct tty_struct *tty)
1864{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001865 struct n_tty_data *ldata = tty->disc_data;
1866
Peter Hurley79901312013-03-11 16:44:23 -04001867 if (tty->link)
1868 n_tty_packet_mode_flush(tty);
1869
Peter Hurley20bafb32013-06-15 10:21:19 -04001870 vfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001871 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874/**
1875 * n_tty_open - open an ldisc
1876 * @tty: terminal to open
1877 *
Alan Cox4edf1822008-02-08 04:18:44 -08001878 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * terminal device. Can sleep. Called serialized so that no
1880 * other events will occur in parallel. No further open will occur
1881 * until a close.
1882 */
1883
1884static int n_tty_open(struct tty_struct *tty)
1885{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001886 struct n_tty_data *ldata;
1887
Peter Hurley20bafb32013-06-15 10:21:19 -04001888 /* Currently a malloc failure here can panic */
1889 ldata = vmalloc(sizeof(*ldata));
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001890 if (!ldata)
1891 goto err;
1892
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001893 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001894 mutex_init(&ldata->atomic_read_lock);
1895 mutex_init(&ldata->output_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001896
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001897 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001898 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001899 ldata->column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001900 ldata->canon_column = 0;
Peter Hurley20bafb32013-06-15 10:21:19 -04001901 ldata->num_overrun = 0;
1902 ldata->no_room = 0;
1903 ldata->lnext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001905 /* indicate buffer work may resume */
1906 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1907 n_tty_set_termios(tty, NULL);
1908 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001911err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001912 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Peter Hurleyeafbe672013-12-02 14:24:45 -05001915static inline int input_available_p(struct tty_struct *tty, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001917 struct n_tty_data *ldata = tty->disc_data;
Peter Hurleya5934802014-02-11 11:49:58 -05001918 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001919
Peter Hurley25e8d0e2014-02-11 18:55:30 -05001920 if (ldata->icanon && !L_EXTPROC(tty))
1921 return ldata->canon_head != ldata->read_tail;
1922 else
Peter Hurley70aca712015-01-16 15:05:37 -05001923 return ldata->commit_head - ldata->read_tail >= amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Peter Hurley1a486322015-04-13 13:24:34 -04001926static inline int check_other_done(struct tty_struct *tty)
1927{
1928 int done = test_bit(TTY_OTHER_DONE, &tty->flags);
1929 if (done) {
1930 /* paired with cmpxchg() in check_other_closed(); ensures
1931 * read buffer head index is not stale
1932 */
1933 smp_mb__after_atomic();
1934 }
1935 return done;
1936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001939 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * @tty: terminal device
1941 * @b: user data
1942 * @nr: size of data
1943 *
Alan Cox11a96d12008-10-13 10:46:24 +01001944 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * ICANON is off; it copies characters straight from the tty queue to
1946 * user space directly. It can be profitably called twice; once to
1947 * drain the space from the tail pointer to the (physical) end of the
1948 * buffer, and once to drain the space from the (physical) beginning of
1949 * the buffer to head pointer.
1950 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001951 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001953 * n_tty_read()/consumer path:
1954 * caller holds non-exclusive termios_rwsem
1955 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 */
Alan Cox4edf1822008-02-08 04:18:44 -08001957
Alan Cox33f0f882006-01-09 20:54:13 -08001958static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 unsigned char __user **b,
1960 size_t *nr)
1961
1962{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001963 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 int retval;
1965 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001966 bool is_eof;
Peter Hurley70aca712015-01-16 15:05:37 -05001967 size_t head = smp_load_acquire(&ldata->commit_head);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001968 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 retval = 0;
Peter Hurley70aca712015-01-16 15:05:37 -05001971 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (n) {
Peter Hurleye661cf72015-11-27 14:11:03 -05001974 const unsigned char *from = read_buf_addr(ldata, tail);
1975 retval = copy_to_user(*b, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 n -= retval;
Peter Hurleye661cf72015-11-27 14:11:03 -05001977 is_eof = n == 1 && *from == EOF_CHAR(tty);
Peter Hurley309426a2016-01-09 22:55:27 -08001978 tty_audit_add_data(tty, from, n);
Peter Hurley70aca712015-01-16 15:05:37 -05001979 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001980 /* Turn single EOF into zero-length read */
Peter Hurley70aca712015-01-16 15:05:37 -05001981 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1982 (head == ldata->read_tail))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001983 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *b += n;
1985 *nr -= n;
1986 }
1987 return retval;
1988}
1989
Peter Hurley88bb0de2013-06-15 09:14:16 -04001990/**
Peter Hurley32f13522013-06-15 09:14:17 -04001991 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001992 * @tty: terminal device
1993 * @b: user data
1994 * @nr: size of data
1995 *
1996 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001997 * it copies one line of input up to and including the line-delimiting
1998 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001999 *
Peter Hurley4d0ed182013-12-10 17:12:02 -05002000 * NB: When termios is changed from non-canonical to canonical mode and
2001 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2002 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2003 * This causes data already processed as input to be immediately available
2004 * as input although a newline has not been received.
2005 *
Peter Hurley88bb0de2013-06-15 09:14:16 -04002006 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04002007 *
2008 * n_tty_read()/consumer path:
2009 * caller holds non-exclusive termios_rwsem
2010 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04002011 */
2012
Peter Hurley32f13522013-06-15 09:14:17 -04002013static int canon_copy_from_read_buf(struct tty_struct *tty,
2014 unsigned char __user **b,
2015 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04002016{
2017 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04002018 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002019 size_t eol;
2020 size_t tail;
2021 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002022
2023 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002024 if (!*nr)
Peter Hurley32f13522013-06-15 09:14:17 -04002025 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002026
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002027 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2028
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002029 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04002030 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2031
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002032 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04002033 __func__, *nr, tail, n, size);
2034
2035 eol = find_next_bit(ldata->read_flags, size, tail);
2036 more = n - (size - tail);
2037 if (eol == N_TTY_BUF_SIZE && more) {
2038 /* scan wrapped without finding set bit */
2039 eol = find_next_bit(ldata->read_flags, more, 0);
Peter Hurleyb985e9e2015-11-27 14:11:04 -05002040 found = eol != more;
2041 } else
2042 found = eol != size;
Peter Hurley32f13522013-06-15 09:14:17 -04002043
Peter Hurleyc77569d2013-11-22 07:16:25 -05002044 n = eol - tail;
Mark Tomlinsonda555db2015-05-18 12:01:48 +12002045 if (n > N_TTY_BUF_SIZE)
2046 n += N_TTY_BUF_SIZE;
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002047 c = n + found;
Peter Hurley32f13522013-06-15 09:14:17 -04002048
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002049 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2050 c = min(*nr, c);
2051 n = c;
Peter Hurley40d5e092013-06-15 10:21:17 -04002052 }
Peter Hurley32f13522013-06-15 09:14:17 -04002053
Peter Hurley679e7c22015-11-27 14:11:02 -05002054 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2055 __func__, eol, found, n, c, tail, more);
Peter Hurley32f13522013-06-15 09:14:17 -04002056
Peter Hurley679e7c22015-11-27 14:11:02 -05002057 ret = tty_copy_to_user(tty, *b, tail, n);
Peter Hurley32f13522013-06-15 09:14:17 -04002058 if (ret)
2059 return -EFAULT;
2060 *b += n;
2061 *nr -= n;
2062
Peter Hurleya73d3d62013-06-15 09:14:25 -04002063 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04002064 clear_bit(eol, ldata->read_flags);
Peter Hurley70aca712015-01-16 15:05:37 -05002065 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
Peter Hurley88bb0de2013-06-15 09:14:16 -04002066
Peter Hurley40d5e092013-06-15 10:21:17 -04002067 if (found) {
Peter Hurley4d0ed182013-12-10 17:12:02 -05002068 if (!ldata->push)
2069 ldata->line_start = ldata->read_tail;
2070 else
2071 ldata->push = 0;
Peter Hurleyb50819f2016-01-09 22:55:30 -08002072 tty_audit_push();
Peter Hurley40d5e092013-06-15 10:21:17 -04002073 }
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002074 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04002075}
2076
Al Virocc4191d2008-03-29 03:08:48 +00002077extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08002078 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080/**
2081 * job_control - check job control
2082 * @tty: tty
2083 * @file: file handle
2084 *
2085 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08002086 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07002088 *
Peter Hurley01a5e442013-03-06 08:38:20 -05002089 * Locking: redirected write test is safe
2090 * current->signal->tty check is safe
2091 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Alan Cox4edf1822008-02-08 04:18:44 -08002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094static int job_control(struct tty_struct *tty, struct file *file)
2095{
2096 /* Job control check -- must be done at start and after
2097 every sleep (POSIX.1 7.1.1.4). */
2098 /* NOTE: not yet done after every sleep pending a thorough
2099 check of the logic of this change. -- jlc */
2100 /* don't stop on /dev/console */
Peter Hurley2812d9e2015-10-10 20:28:42 -04002101 if (file->f_op->write == redirected_tty_write)
Peter Hurley01a5e442013-03-06 08:38:20 -05002102 return 0;
2103
Peter Hurley2812d9e2015-10-10 20:28:42 -04002104 return __tty_check_change(tty, SIGTTIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
Alan Cox4edf1822008-02-08 04:18:44 -08002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108/**
Alan Cox11a96d12008-10-13 10:46:24 +01002109 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 * @tty: tty device
2111 * @file: file object
2112 * @buf: userspace buffer pointer
2113 * @nr: size of I/O
2114 *
2115 * Perform reads for the line discipline. We are guaranteed that the
2116 * line discipline will not be closed under us but we may get multiple
2117 * parallel readers and must handle this ourselves. We may also get
2118 * a hangup. Always called in user context, may sleep.
2119 *
2120 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04002121 *
2122 * n_tty_read()/consumer path:
2123 * claims non-exclusive termios_rwsem
2124 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
Alan Cox4edf1822008-02-08 04:18:44 -08002126
Alan Cox11a96d12008-10-13 10:46:24 +01002127static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 unsigned char __user *buf, size_t nr)
2129{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002130 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 unsigned char __user *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002132 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Peter Hurley1a486322015-04-13 13:24:34 -04002133 int c, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 int minimum, time;
2135 ssize_t retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 long timeout;
Alan Cox04f378b2008-04-30 00:53:29 -07002137 int packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002138 size_t tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08002141 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08002143
Peter Hurleyaefceaf2013-08-11 08:04:23 -04002144 /*
2145 * Internal serialization of reads.
2146 */
2147 if (file->f_flags & O_NONBLOCK) {
2148 if (!mutex_trylock(&ldata->atomic_read_lock))
2149 return -EAGAIN;
2150 } else {
2151 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2152 return -ERESTARTSYS;
2153 }
2154
Peter Hurley9356b532013-06-15 09:14:24 -04002155 down_read(&tty->termios_rwsem);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 minimum = time = 0;
2158 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002159 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 minimum = MIN_CHAR(tty);
2161 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002162 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002164 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurley33d71362016-01-09 21:45:08 -08002165 minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 }
2168
Alan Cox04f378b2008-04-30 00:53:29 -07002169 packet = tty->packet;
Peter Hurley2c5dc462015-01-16 15:05:34 -05002170 tail = ldata->read_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 while (nr) {
2174 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002175 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 unsigned char cs;
2177 if (b != buf)
2178 break;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002179 spin_lock_irq(&tty->link->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 cs = tty->link->ctrl_status;
2181 tty->link->ctrl_status = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -04002182 spin_unlock_irq(&tty->link->ctrl_lock);
Peter Hurleyeab25a52016-01-09 22:55:26 -08002183 if (put_user(cs, b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 break;
2186 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002187 b++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 nr--;
2189 break;
2190 }
Alan Cox4edf1822008-02-08 04:18:44 -08002191
Peter Hurley1a486322015-04-13 13:24:34 -04002192 done = check_other_done(tty);
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!input_available_p(tty, 0)) {
Peter Hurley1a486322015-04-13 13:24:34 -04002195 if (done) {
Peter Hurley52bce7f2014-11-05 12:13:05 -05002196 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 break;
2198 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002199 if (tty_hung_up_p(file))
2200 break;
2201 if (!timeout)
2202 break;
2203 if (file->f_flags & O_NONBLOCK) {
2204 retval = -EAGAIN;
2205 break;
2206 }
2207 if (signal_pending(current)) {
2208 retval = -ERESTARTSYS;
2209 break;
2210 }
Peter Hurley52bce7f2014-11-05 12:13:05 -05002211 up_read(&tty->termios_rwsem);
2212
Linus Torvalds37da7bb2014-12-14 15:23:32 -08002213 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2214 timeout);
Peter Hurley52bce7f2014-11-05 12:13:05 -05002215
2216 down_read(&tty->termios_rwsem);
2217 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002220 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002221 retval = canon_copy_from_read_buf(tty, &b, &nr);
Peter Hurleyac8f3bf2015-11-27 13:59:20 -05002222 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 } else {
2225 int uncopied;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002226
2227 /* Deal with packet mode. */
2228 if (packet && b == buf) {
Peter Hurleyeab25a52016-01-09 22:55:26 -08002229 if (put_user(TIOCPKT_DATA, b)) {
Peter Hurley95ea90d2014-10-16 15:33:30 -04002230 retval = -EFAULT;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002231 break;
2232 }
Peter Hurleyeab25a52016-01-09 22:55:26 -08002233 b++;
Peter Hurley95ea90d2014-10-16 15:33:30 -04002234 nr--;
2235 }
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 uncopied = copy_from_read_buf(tty, &b, &nr);
2238 uncopied += copy_from_read_buf(tty, &b, &nr);
2239 if (uncopied) {
2240 retval = -EFAULT;
2241 break;
2242 }
2243 }
2244
Peter Hurley6367ca72013-06-15 09:14:33 -04002245 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (b - buf >= minimum)
2248 break;
2249 if (time)
2250 timeout = time;
2251 }
Peter Hurley2c5dc462015-01-16 15:05:34 -05002252 if (tail != ldata->read_tail)
2253 n_tty_kick_worker(tty);
Peter Hurley42458f42013-11-07 13:59:46 -05002254 up_read(&tty->termios_rwsem);
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 remove_wait_queue(&tty->read_wait, &wait);
Peter Hurleyaebf04532013-11-07 14:01:57 -05002257 mutex_unlock(&ldata->atomic_read_lock);
2258
Peter Hurley40d5e092013-06-15 10:21:17 -04002259 if (b - buf)
2260 retval = b - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 return retval;
2263}
2264
2265/**
Alan Cox11a96d12008-10-13 10:46:24 +01002266 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * @tty: tty device
2268 * @file: file object
2269 * @buf: userspace buffer pointer
2270 * @nr: size of I/O
2271 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002272 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002274 * and other such events. Since the receive code will echo characters,
2275 * thus calling driver write methods, the output_lock is used in
2276 * the output processing functions called here as well as in the
2277 * echo processing function to protect the column state and space
2278 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *
2280 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002281 *
2282 * Locking: output_lock to protect column state and space left
2283 * (note that the process_output*() functions take this
2284 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Alan Cox4edf1822008-02-08 04:18:44 -08002286
Alan Cox11a96d12008-10-13 10:46:24 +01002287static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002288 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 const unsigned char *b = buf;
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002291 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int c;
2293 ssize_t retval = 0;
2294
2295 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2296 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2297 retval = tty_check_change(tty);
2298 if (retval)
2299 return retval;
2300 }
2301
Peter Hurley9356b532013-06-15 09:14:24 -04002302 down_read(&tty->termios_rwsem);
2303
Joe Petersona88a69c2009-01-02 13:40:53 +00002304 /* Write out any echoed characters that are still pending */
2305 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 add_wait_queue(&tty->write_wait, &wait);
2308 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (signal_pending(current)) {
2310 retval = -ERESTARTSYS;
2311 break;
2312 }
2313 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2314 retval = -EIO;
2315 break;
2316 }
Peter Hurley582f5592013-05-17 12:49:48 -04002317 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002319 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (num < 0) {
2321 if (num == -EAGAIN)
2322 break;
2323 retval = num;
2324 goto break_out;
2325 }
2326 b += num;
2327 nr -= num;
2328 if (nr == 0)
2329 break;
2330 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002331 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 break;
2333 b++; nr--;
2334 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002335 if (tty->ops->flush_chars)
2336 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else {
Peter Hurley42910862014-05-03 14:04:59 +02002338 struct n_tty_data *ldata = tty->disc_data;
2339
Roman Zippeld6afe272005-07-07 17:56:55 -07002340 while (nr > 0) {
Peter Hurley42910862014-05-03 14:04:59 +02002341 mutex_lock(&ldata->output_lock);
Alan Coxf34d7a52008-04-30 00:54:13 -07002342 c = tty->ops->write(tty, b, nr);
Peter Hurley42910862014-05-03 14:04:59 +02002343 mutex_unlock(&ldata->output_lock);
Roman Zippeld6afe272005-07-07 17:56:55 -07002344 if (c < 0) {
2345 retval = c;
2346 goto break_out;
2347 }
2348 if (!c)
2349 break;
2350 b += c;
2351 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354 if (!nr)
2355 break;
2356 if (file->f_flags & O_NONBLOCK) {
2357 retval = -EAGAIN;
2358 break;
2359 }
Peter Hurley9356b532013-06-15 09:14:24 -04002360 up_read(&tty->termios_rwsem);
2361
Peter Zijlstra97d9e282014-09-24 10:18:51 +02002362 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Peter Hurley9356b532013-06-15 09:14:24 -04002363
2364 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366break_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002368 if (b - buf != nr && tty->fasync)
2369 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002370 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return (b - buf) ? b - buf : retval;
2372}
2373
2374/**
Alan Cox11a96d12008-10-13 10:46:24 +01002375 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 * @tty: terminal device
2377 * @file: file accessing it
2378 * @wait: poll table
2379 *
2380 * Called when the line discipline is asked to poll() for data or
2381 * for special events. This code is not serialized with respect to
2382 * other events save open/close.
2383 *
2384 * This code must be sure never to sleep through a hangup.
2385 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 */
Alan Cox4edf1822008-02-08 04:18:44 -08002387
Alan Cox11a96d12008-10-13 10:46:24 +01002388static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002389 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 unsigned int mask = 0;
2392
2393 poll_wait(file, &tty->read_wait, wait);
2394 poll_wait(file, &tty->write_wait, wait);
Peter Hurley1a486322015-04-13 13:24:34 -04002395 if (check_other_done(tty))
2396 mask |= POLLHUP;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002397 if (input_available_p(tty, 1))
2398 mask |= POLLIN | POLLRDNORM;
Francesco Ruggeric4dc3042014-10-10 13:09:53 -07002399 if (tty->packet && tty->link->ctrl_status)
2400 mask |= POLLPRI | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (tty_hung_up_p(file))
2402 mask |= POLLHUP;
Alan Coxf34d7a52008-04-30 00:54:13 -07002403 if (tty->ops->write && !tty_is_writelocked(tty) &&
2404 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2405 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 mask |= POLLOUT | POLLWRNORM;
2407 return mask;
2408}
2409
Jiri Slaby57c94122012-10-18 22:26:43 +02002410static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002411{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002412 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002413
Peter Hurleya73d3d62013-06-15 09:14:25 -04002414 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002415 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002416 head = ldata->canon_head;
2417 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002418 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002419 /* Skip EOF-chars.. */
2420 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002421 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2422 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002423 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002424 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002425 }
2426 return nr;
2427}
2428
2429static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2430 unsigned int cmd, unsigned long arg)
2431{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002432 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002433 int retval;
2434
2435 switch (cmd) {
2436 case TIOCOUTQ:
2437 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2438 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002439 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002440 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002441 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002442 else
2443 retval = read_cnt(ldata);
2444 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002445 return put_user(retval, (unsigned int __user *) arg);
2446 default:
2447 return n_tty_ioctl_helper(tty, file, cmd, arg);
2448 }
2449}
2450
Peter Hurley27228732016-01-09 21:35:19 -08002451static struct tty_ldisc_ops n_tty_ops = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002452 .magic = TTY_LDISC_MAGIC,
2453 .name = "n_tty",
2454 .open = n_tty_open,
2455 .close = n_tty_close,
2456 .flush_buffer = n_tty_flush_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002457 .read = n_tty_read,
2458 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002459 .ioctl = n_tty_ioctl,
2460 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002461 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002462 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002463 .write_wakeup = n_tty_write_wakeup,
Peter Hurley24a89d12013-06-15 09:14:15 -04002464 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002466
2467/**
2468 * n_tty_inherit_ops - inherit N_TTY methods
2469 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2470 *
Peter Hurley27228732016-01-09 21:35:19 -08002471 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002472 */
2473
2474void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2475{
Peter Hurley27228732016-01-09 21:35:19 -08002476 *ops = n_tty_ops;
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002477 ops->owner = NULL;
2478 ops->refcount = ops->flags = 0;
2479}
2480EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Peter Hurley27228732016-01-09 21:35:19 -08002481
2482void __init n_tty_init(void)
2483{
2484 tty_register_ldisc(N_TTY, &n_tty_ops);
2485}