blob: 59eb444fe17c7d95a3cb64c8fc8c839c706aab42 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010064#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Joe Petersona88a69c2009-01-02 13:40:53 +000066/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
Peter Hurley32f13522013-06-15 09:14:17 -040077#undef N_TTY_TRACE
78#ifdef N_TTY_TRACE
79# define n_tty_trace(f, args...) trace_printk(f, ##args)
80#else
81# define n_tty_trace(f, args...)
82#endif
83
Jiri Slaby70ece7a2012-10-18 22:26:38 +020084struct n_tty_data {
Peter Hurleyfb7aa032013-06-15 09:14:30 -040085 /* producer-published */
86 size_t read_head;
87 size_t canon_head;
88 DECLARE_BITMAP(process_char_map, 256);
89
90 /* private to n_tty_receive_overrun (single-threaded) */
Jiri Slaby53c5ee22012-10-18 22:26:39 +020091 unsigned long overrun_time;
92 int num_overrun;
93
Peter Hurley24a89d12013-06-15 09:14:15 -040094 /* non-atomic */
95 bool no_room;
96
Peter Hurleyfb7aa032013-06-15 09:14:30 -040097 /* must hold exclusive termios_rwsem to reset these */
Jiri Slaby53c5ee22012-10-18 22:26:39 +020098 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
99 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200100
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400101 /* shared by producer and consumer */
102 char *read_buf;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200103 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200104
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -0400105 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200106
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400107 /* consumer-published */
108 size_t read_tail;
109
110 /* protected by echo_lock */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200111 unsigned char *echo_buf;
112 unsigned int echo_pos;
113 unsigned int echo_cnt;
114
Peter Hurleyfb7aa032013-06-15 09:14:30 -0400115 /* protected by output lock */
116 unsigned int column;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200117 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200118
119 struct mutex atomic_read_lock;
120 struct mutex output_lock;
121 struct mutex echo_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200122};
123
Peter Hurleyce741172013-06-15 09:14:20 -0400124static inline size_t read_cnt(struct n_tty_data *ldata)
125{
Peter Hurleya2f73be2013-06-15 09:14:22 -0400126 return ldata->read_head - ldata->read_tail;
Peter Hurleyce741172013-06-15 09:14:20 -0400127}
128
Peter Hurleybc5a5e32013-06-15 09:14:21 -0400129static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
130{
131 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
132}
133
134static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
135{
136 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
137}
138
Miloslav Trmac522ed772007-07-15 23:40:56 -0700139static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
140 unsigned char __user *ptr)
141{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200142 struct n_tty_data *ldata = tty->disc_data;
143
144 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700145 return put_user(x, ptr);
146}
147
Peter Hurley24a89d12013-06-15 09:14:15 -0400148static int receive_room(struct tty_struct *tty)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900149{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200150 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500151 int left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400152
Jaeden Amero090abf72012-07-27 08:43:11 -0500153 if (I_PARMRK(tty)) {
154 /* Multiply read_cnt by 3, since each byte might take up to
155 * three times as many spaces when PARMRK is set (depending on
156 * its flags, e.g. parity error). */
Peter Hurleyce741172013-06-15 09:14:20 -0400157 left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500158 } else
Peter Hurleyce741172013-06-15 09:14:20 -0400159 left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500160
Linus Torvalds55db4c62011-06-04 06:33:24 +0900161 /*
162 * If we are doing input canonicalization, and there are no
163 * pending newlines, let characters through without limit, so
164 * that erase characters will be handled. Other excess
165 * characters will be beeped.
166 */
167 if (left <= 0)
Peter Hurleya73d3d62013-06-15 09:14:25 -0400168 left = ldata->icanon && ldata->canon_head == ldata->read_tail;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900169
Peter Hurley24a89d12013-06-15 09:14:15 -0400170 return left;
Peter Hurley7879a9f2013-06-15 07:28:31 -0400171}
172
Peter Hurley24a89d12013-06-15 09:14:15 -0400173/**
174 * n_tty_set_room - receive space
175 * @tty: terminal
176 *
177 * Re-schedules the flip buffer work if space just became available.
178 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400179 * Caller holds exclusive termios_rwsem
180 * or
181 * n_tty_read()/consumer path:
182 * holds non-exclusive termios_rwsem
Peter Hurley24a89d12013-06-15 09:14:15 -0400183 */
184
Peter Hurley7879a9f2013-06-15 07:28:31 -0400185static void n_tty_set_room(struct tty_struct *tty)
186{
Peter Hurley24a89d12013-06-15 09:14:15 -0400187 struct n_tty_data *ldata = tty->disc_data;
188
Linus Torvalds55db4c62011-06-04 06:33:24 +0900189 /* Did this open up the receive buffer? We may need to flip */
Peter Hurley24a89d12013-06-15 09:14:15 -0400190 if (unlikely(ldata->no_room) && receive_room(tty)) {
191 ldata->no_room = 0;
192
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200193 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400194 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400195 /* see if ldisc has been killed - if so, this means that
196 * even though the ldisc has been halted and ->buf.work
197 * cancelled, ->buf.work is about to be rescheduled
198 */
199 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
200 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200201 schedule_work(&tty->port->buf.work);
202 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900203}
204
Peter Hurley9a4aec22013-06-15 09:14:32 -0400205static ssize_t chars_in_buffer(struct tty_struct *tty)
206{
207 struct n_tty_data *ldata = tty->disc_data;
208 ssize_t n = 0;
209
210 if (!ldata->icanon)
211 n = read_cnt(ldata);
212 else
213 n = ldata->canon_head - ldata->read_tail;
214 return n;
215}
216
Peter Hurleyee0bab82013-06-15 09:14:34 -0400217/**
218 * n_tty_write_wakeup - asynchronous I/O notifier
219 * @tty: tty device
220 *
221 * Required for the ptys, serial driver etc. since processes
222 * that attach themselves to the master and rely on ASYNC
223 * IO must be woken up
224 */
225
226static void n_tty_write_wakeup(struct tty_struct *tty)
227{
228 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
229 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
230}
231
Peter Hurley6367ca72013-06-15 09:14:33 -0400232static inline void n_tty_check_throttle(struct tty_struct *tty)
233{
Peter Hurley3afb1b392013-07-23 08:47:30 -0400234 if (tty->driver->type == TTY_DRIVER_TYPE_PTY)
235 return;
Peter Hurley6367ca72013-06-15 09:14:33 -0400236 /*
237 * Check the remaining room for the input canonicalization
238 * mode. We don't want to throttle the driver if we're in
239 * canonical mode and don't have a newline yet!
240 */
241 while (1) {
242 int throttled;
243 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
244 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
245 break;
246 throttled = tty_throttle_safe(tty);
247 if (!throttled)
248 break;
249 }
250 __tty_set_flow_change(tty, 0);
251}
252
253static inline void n_tty_check_unthrottle(struct tty_struct *tty)
254{
Peter Hurley3afb1b392013-07-23 08:47:30 -0400255 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
256 tty->link->ldisc->ops->write_wakeup == n_tty_write_wakeup) {
257 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
258 return;
259 if (!tty->count)
260 return;
261 n_tty_set_room(tty);
262 n_tty_write_wakeup(tty->link);
263 wake_up_interruptible_poll(&tty->link->write_wait, POLLOUT);
264 return;
265 }
266
Peter Hurley6367ca72013-06-15 09:14:33 -0400267 /* If there is enough space in the read buffer now, let the
268 * low-level driver know. We use chars_in_buffer() to
269 * check the buffer, as it now knows about canonical mode.
270 * Otherwise, if the driver is throttled and the line is
271 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
272 * we won't get any more characters.
273 */
274
275 while (1) {
276 int unthrottled;
277 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
278 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
279 break;
280 if (!tty->count)
281 break;
282 n_tty_set_room(tty);
283 unthrottled = tty_unthrottle_safe(tty);
284 if (!unthrottled)
285 break;
286 }
287 __tty_set_flow_change(tty, 0);
288}
289
Alan Cox17b82062008-10-13 10:45:06 +0100290/**
291 * put_tty_queue - add character to tty
292 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200293 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100294 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400295 * Add a character to the tty read_buf queue.
296 *
297 * n_tty_receive_buf()/producer path:
298 * caller holds non-exclusive termios_rwsem
299 * modifies read_head
300 *
301 * read_head is only considered 'published' if canonical mode is
302 * not active.
Alan Cox17b82062008-10-13 10:45:06 +0100303 */
304
Jiri Slaby57c94122012-10-18 22:26:43 +0200305static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400307 if (read_cnt(ldata) < N_TTY_BUF_SIZE) {
308 *read_buf_addr(ldata, ldata->read_head) = c;
309 ldata->read_head++;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * reset_buffer_flags - reset buffer state
315 * @tty: terminal to reset
316 *
Peter Hurley25518c62013-03-11 16:44:31 -0400317 * Reset the read buffer counters and clear the flags.
318 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100319 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400320 * Locking: caller holds exclusive termios_rwsem
321 * (or locking is not required)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000323
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400324static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Peter Hurleya73d3d62013-06-15 09:14:25 -0400326 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000327
Jiri Slabybddc7152012-10-18 22:26:42 +0200328 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200329 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200330 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000331
Peter Hurleya73d3d62013-06-15 09:14:25 -0400332 ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200333 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Peter Hurleya30737a2013-03-11 16:44:22 -0400336static void n_tty_packet_mode_flush(struct tty_struct *tty)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(&tty->ctrl_lock, flags);
341 if (tty->link->packet) {
342 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
343 wake_up_interruptible(&tty->link->read_wait);
344 }
345 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/**
349 * n_tty_flush_buffer - clean input queue
350 * @tty: terminal device
351 *
Peter Hurley25518c62013-03-11 16:44:31 -0400352 * Flush the input buffer. Called when the tty layer wants the
353 * buffer flushed (eg at hangup) or when the N_TTY line discipline
354 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400356 * Holds termios_rwsem to exclude producer/consumer while
357 * buffer indices are reset.
358 *
359 * Locking: ctrl_lock, exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Alan Cox4edf1822008-02-08 04:18:44 -0800361
362static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400364 down_write(&tty->termios_rwsem);
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400365 reset_buffer_flags(tty->disc_data);
366 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800367
Peter Hurleya30737a2013-03-11 16:44:22 -0400368 if (tty->link)
369 n_tty_packet_mode_flush(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400370 up_write(&tty->termios_rwsem);
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/**
374 * n_tty_chars_in_buffer - report available bytes
375 * @tty: tty device
376 *
377 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800378 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100379 *
Peter Hurley6d76bd22013-06-15 09:14:26 -0400380 * Locking: exclusive termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Alan Cox4edf1822008-02-08 04:18:44 -0800382
Peter Hurleya19d0c62013-06-15 09:14:18 -0400383static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
384{
Peter Hurley6d76bd22013-06-15 09:14:26 -0400385 ssize_t n;
386
Peter Hurley47534082013-06-15 09:14:19 -0400387 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
Peter Hurley6d76bd22013-06-15 09:14:26 -0400388
389 down_write(&tty->termios_rwsem);
390 n = chars_in_buffer(tty);
391 up_write(&tty->termios_rwsem);
392 return n;
Peter Hurleya19d0c62013-06-15 09:14:18 -0400393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * is_utf8_continuation - utf8 multibyte check
397 * @c: byte to check
398 *
399 * Returns true if the utf8 character 'c' is a multibyte continuation
400 * character. We use this to correctly compute the on screen size
401 * of the character when printing
402 */
Alan Cox4edf1822008-02-08 04:18:44 -0800403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static inline int is_utf8_continuation(unsigned char c)
405{
406 return (c & 0xc0) == 0x80;
407}
408
409/**
410 * is_continuation - multibyte check
411 * @c: byte to check
412 *
413 * Returns true if the utf8 character 'c' is a multibyte continuation
414 * character and the terminal is in unicode mode.
415 */
Alan Cox4edf1822008-02-08 04:18:44 -0800416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static inline int is_continuation(unsigned char c, struct tty_struct *tty)
418{
419 return I_IUTF8(tty) && is_utf8_continuation(c);
420}
421
422/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000423 * do_output_char - output one character
424 * @c: character (or partial unicode symbol)
425 * @tty: terminal device
426 * @space: space available in tty driver write buffer
427 *
428 * This is a helper function that handles one output character
429 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600430 * doing OPOST processing and putting the results in the
431 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000432 *
433 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
434 * and NLDLY. They simply aren't relevant in the world today.
435 * If you ever need them, add them here.
436 *
437 * Returns the number of bytes of buffer space used or -1 if
438 * no space left.
439 *
440 * Locking: should be called under the output_lock to protect
441 * the column state and space left in the buffer
442 */
443
444static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
445{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200446 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000447 int spaces;
448
449 if (!space)
450 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000451
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 switch (c) {
453 case '\n':
454 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200455 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000456 if (O_ONLCR(tty)) {
457 if (space < 2)
458 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200459 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700460 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000461 return 2;
462 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200463 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000464 break;
465 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200466 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000467 return 0;
468 if (O_OCRNL(tty)) {
469 c = '\n';
470 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200471 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000472 break;
473 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200474 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000475 break;
476 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200477 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000478 if (O_TABDLY(tty) == XTABS) {
479 if (space < spaces)
480 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200481 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000482 tty->ops->write(tty, " ", spaces);
483 return spaces;
484 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200485 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000486 break;
487 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200488 if (ldata->column > 0)
489 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000490 break;
491 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000492 if (!iscntrl(c)) {
493 if (O_OLCUC(tty))
494 c = toupper(c);
495 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200496 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000497 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000498 break;
499 }
500
501 tty_put_char(tty, c);
502 return 1;
503}
504
505/**
506 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * @c: character (or partial unicode symbol)
508 * @tty: terminal device
509 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600510 * Output one character with OPOST processing.
511 * Returns -1 when the output device is full and the character
512 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000514 * Locking: output_lock to protect column state and space left
515 * (also, this is called from n_tty_write under the
516 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Alan Cox4edf1822008-02-08 04:18:44 -0800518
Joe Petersona88a69c2009-01-02 13:40:53 +0000519static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jiri Slabybddc7152012-10-18 22:26:42 +0200521 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000522 int space, retval;
523
Jiri Slabybddc7152012-10-18 22:26:42 +0200524 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Alan Coxf34d7a52008-04-30 00:54:13 -0700526 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000527 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jiri Slabybddc7152012-10-18 22:26:42 +0200529 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000530 if (retval < 0)
531 return -1;
532 else
533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000537 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600539 * @buf: character buffer
540 * @nr: number of bytes to output
541 *
542 * Output a block of characters with OPOST processing.
543 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
545 * This path is used to speed up block console writes, among other
546 * things when processing blocks of output data. It handles only
547 * the simple cases normally found and helps to generate blocks of
548 * symbols for the console driver and thus improve performance.
549 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000550 * Locking: output_lock to protect column state and space left
551 * (also, this is called from n_tty_write under the
552 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
Alan Cox4edf1822008-02-08 04:18:44 -0800554
Joe Petersona88a69c2009-01-02 13:40:53 +0000555static ssize_t process_output_block(struct tty_struct *tty,
556 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200558 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100560 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 const unsigned char *cp;
562
Jiri Slabybddc7152012-10-18 22:26:42 +0200563 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000564
Alan Coxf34d7a52008-04-30 00:54:13 -0700565 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000566 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200567 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (nr > space)
571 nr = space;
572
573 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000574 unsigned char c = *cp;
575
576 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case '\n':
578 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200579 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (O_ONLCR(tty))
581 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200582 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200585 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto break_out;
587 if (O_OCRNL(tty))
588 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200589 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
591 case '\t':
592 goto break_out;
593 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200594 if (ldata->column > 0)
595 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000598 if (!iscntrl(c)) {
599 if (O_OLCUC(tty))
600 goto break_out;
601 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200602 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 }
606 }
607break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700608 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000609
Jiri Slabybddc7152012-10-18 22:26:42 +0200610 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return i;
612}
613
Joe Petersona88a69c2009-01-02 13:40:53 +0000614/**
615 * process_echoes - write pending echo characters
616 * @tty: terminal device
617 *
618 * Write previously buffered echo (and other ldisc-generated)
619 * characters to the tty.
620 *
621 * Characters generated by the ldisc (including echoes) need to
622 * be buffered because the driver's write buffer can fill during
623 * heavy program output. Echoing straight to the driver will
624 * often fail under these conditions, causing lost characters and
625 * resulting mismatches of ldisc state information.
626 *
627 * Since the ldisc state must represent the characters actually sent
628 * to the driver at the time of the write, operations like certain
629 * changes in column state are also saved in the buffer and executed
630 * here.
631 *
632 * A circular fifo buffer is used so that the most recent characters
633 * are prioritized. Also, when control characters are echoed with a
634 * prefixed "^", the pair is treated atomically and thus not separated.
635 *
636 * Locking: output_lock to protect column state and space left,
637 * echo_lock to protect the echo buffer
638 */
639
640static void process_echoes(struct tty_struct *tty)
641{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200642 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000643 int space, nr;
644 unsigned char c;
645 unsigned char *cp, *buf_end;
646
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200647 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000648 return;
649
Jiri Slabybddc7152012-10-18 22:26:42 +0200650 mutex_lock(&ldata->output_lock);
651 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000652
653 space = tty_write_room(tty);
654
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200655 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
656 cp = ldata->echo_buf + ldata->echo_pos;
657 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000658 while (nr > 0) {
659 c = *cp;
660 if (c == ECHO_OP_START) {
661 unsigned char op;
662 unsigned char *opp;
663 int no_space_left = 0;
664
665 /*
666 * If the buffer byte is the start of a multi-byte
667 * operation, get the next byte, which is either the
668 * op code or a control character value.
669 */
670 opp = cp + 1;
671 if (opp == buf_end)
672 opp -= N_TTY_BUF_SIZE;
673 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000674
Joe Petersona88a69c2009-01-02 13:40:53 +0000675 switch (op) {
676 unsigned int num_chars, num_bs;
677
678 case ECHO_OP_ERASE_TAB:
679 if (++opp == buf_end)
680 opp -= N_TTY_BUF_SIZE;
681 num_chars = *opp;
682
683 /*
684 * Determine how many columns to go back
685 * in order to erase the tab.
686 * This depends on the number of columns
687 * used by other characters within the tab
688 * area. If this (modulo 8) count is from
689 * the start of input rather than from a
690 * previous tab, we offset by canon column.
691 * Otherwise, tab spacing is normal.
692 */
693 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200694 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000695 num_bs = 8 - (num_chars & 7);
696
697 if (num_bs > space) {
698 no_space_left = 1;
699 break;
700 }
701 space -= num_bs;
702 while (num_bs--) {
703 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200704 if (ldata->column > 0)
705 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000706 }
707 cp += 3;
708 nr -= 3;
709 break;
710
711 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200712 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000713 cp += 2;
714 nr -= 2;
715 break;
716
717 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200718 if (ldata->column > 0)
719 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 cp += 2;
721 nr -= 2;
722 break;
723
724 case ECHO_OP_START:
725 /* This is an escaped echo op start code */
726 if (!space) {
727 no_space_left = 1;
728 break;
729 }
730 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200731 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000732 space--;
733 cp += 2;
734 nr -= 2;
735 break;
736
737 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600739 * If the op is not a special byte code,
740 * it is a ctrl char tagged to be echoed
741 * as "^X" (where X is the letter
742 * representing the control char).
743 * Note that we must ensure there is
744 * enough space for the whole ctrl pair.
745 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000746 */
Joe Peterson62b26352009-09-09 15:03:47 -0600747 if (space < 2) {
748 no_space_left = 1;
749 break;
750 }
751 tty_put_char(tty, '^');
752 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200753 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600754 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000755 cp += 2;
756 nr -= 2;
757 }
758
759 if (no_space_left)
760 break;
761 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400762 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600763 int retval = do_output_char(c, tty, space);
764 if (retval < 0)
765 break;
766 space -= retval;
767 } else {
768 if (!space)
769 break;
770 tty_put_char(tty, c);
771 space -= 1;
772 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000773 cp += 1;
774 nr -= 1;
775 }
776
777 /* When end of circular buffer reached, wrap around */
778 if (cp >= buf_end)
779 cp -= N_TTY_BUF_SIZE;
780 }
781
782 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200783 ldata->echo_pos = 0;
784 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200785 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000786 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200787 int num_processed = ldata->echo_cnt - nr;
788 ldata->echo_pos += num_processed;
789 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
790 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000791 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200792 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000793 }
794
Jiri Slabybddc7152012-10-18 22:26:42 +0200795 mutex_unlock(&ldata->echo_lock);
796 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000797
798 if (tty->ops->flush_chars)
799 tty->ops->flush_chars(tty);
800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000803 * add_echo_byte - add a byte to the echo buffer
804 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200805 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000806 *
807 * Add a character or operation byte to the echo buffer.
808 *
809 * Should be called under the echo lock to protect the echo buffer.
810 */
811
Jiri Slaby57c94122012-10-18 22:26:43 +0200812static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000813{
814 int new_byte_pos;
815
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200816 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000817 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200818 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000819
820 /*
821 * Since the buffer start position needs to be advanced,
822 * be sure to step by a whole operation byte group.
823 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200824 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
825 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000826 (N_TTY_BUF_SIZE - 1)] ==
827 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200828 ldata->echo_pos += 3;
829 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000830 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200831 ldata->echo_pos += 2;
832 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000833 }
834 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200835 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200837 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000838
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200839 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000840 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200841 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000842 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200843 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000844 }
845
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200846 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000847}
848
849/**
850 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200851 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 *
853 * Add an operation to the echo buffer to move back one column.
854 *
855 * Locking: echo_lock to protect the echo buffer
856 */
857
Jiri Slaby57c94122012-10-18 22:26:43 +0200858static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000859{
Jiri Slabybddc7152012-10-18 22:26:42 +0200860 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200863 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000864}
865
866/**
867 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200868 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000869 *
870 * Add an operation to the echo buffer to set the canon column
871 * to the current column.
872 *
873 * Locking: echo_lock to protect the echo buffer
874 */
875
Jiri Slaby57c94122012-10-18 22:26:43 +0200876static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000877{
Jiri Slabybddc7152012-10-18 22:26:42 +0200878 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200879 add_echo_byte(ECHO_OP_START, ldata);
880 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200881 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000882}
883
884/**
885 * echo_erase_tab - add operation to erase a tab
886 * @num_chars: number of character columns already used
887 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200888 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000889 *
890 * Add an operation to the echo buffer to erase a tab.
891 *
892 * Called by the eraser function, which knows how many character
893 * columns have been used since either a previous tab or the start
894 * of input. This information will be used later, along with
895 * canon column (if applicable), to go back the correct number
896 * of columns.
897 *
898 * Locking: echo_lock to protect the echo buffer
899 */
900
901static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200902 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000903{
Jiri Slabybddc7152012-10-18 22:26:42 +0200904 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000905
Jiri Slaby57c94122012-10-18 22:26:43 +0200906 add_echo_byte(ECHO_OP_START, ldata);
907 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000908
909 /* We only need to know this modulo 8 (tab spacing) */
910 num_chars &= 7;
911
912 /* Set the high bit as a flag if num_chars is after a previous tab */
913 if (after_tab)
914 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000915
Jiri Slaby57c94122012-10-18 22:26:43 +0200916 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000917
Jiri Slabybddc7152012-10-18 22:26:42 +0200918 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000919}
920
921/**
922 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * @c: unicode byte to echo
924 * @tty: terminal device
925 *
Alan Cox4edf1822008-02-08 04:18:44 -0800926 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100928 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000929 * This variant does not treat control characters specially.
930 *
931 * Locking: echo_lock to protect the echo buffer
932 */
933
Jiri Slaby57c94122012-10-18 22:26:43 +0200934static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000935{
Jiri Slabybddc7152012-10-18 22:26:42 +0200936 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000937 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200938 add_echo_byte(ECHO_OP_START, ldata);
939 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000940 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200941 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000942 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200943 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000944}
945
946/**
947 * echo_char - echo a character
948 * @c: unicode byte to echo
949 * @tty: terminal device
950 *
951 * Echo user input back onto the screen. This must be called only when
952 * L_ECHO(tty) is true. Called from the driver receive_buf path.
953 *
Joe Peterson62b26352009-09-09 15:03:47 -0600954 * This variant tags control characters to be echoed as "^X"
955 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000956 *
957 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
959
960static void echo_char(unsigned char c, struct tty_struct *tty)
961{
Jiri Slabybddc7152012-10-18 22:26:42 +0200962 struct n_tty_data *ldata = tty->disc_data;
963
964 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000965
966 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200967 add_echo_byte(ECHO_OP_START, ldata);
968 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000969 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600970 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200971 add_echo_byte(ECHO_OP_START, ldata);
972 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000973 }
974
Jiri Slabybddc7152012-10-18 22:26:42 +0200975 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Alan Cox17b82062008-10-13 10:45:06 +0100978/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000979 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200980 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100981 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000982
Jiri Slaby57c94122012-10-18 22:26:43 +0200983static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200985 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200986 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200987 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
991/**
992 * eraser - handle erase function
993 * @c: character input
994 * @tty: terminal device
995 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200996 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * present in the stream from the driver layer. Handles the complexities
998 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100999 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001000 * n_tty_receive_buf()/producer path:
1001 * caller holds non-exclusive termios_rwsem
1002 * modifies read_head
1003 *
1004 * Modifying the read_head is not considered a publish in this context
1005 * because canonical mode is active -- only canon_head publishes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
Alan Cox4edf1822008-02-08 04:18:44 -08001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static void eraser(unsigned char c, struct tty_struct *tty)
1009{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001010 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 enum { ERASE, WERASE, KILL } kill_type;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001012 size_t head;
1013 size_t cnt;
1014 int seen_alnums;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001016 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001017 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
1019 }
1020 if (c == ERASE_CHAR(tty))
1021 kill_type = ERASE;
1022 else if (c == WERASE_CHAR(tty))
1023 kill_type = WERASE;
1024 else {
1025 if (!L_ECHO(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001026 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return;
1028 }
1029 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001030 ldata->read_head = ldata->canon_head;
Jiri Slaby57c94122012-10-18 22:26:43 +02001031 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 echo_char(KILL_CHAR(tty), tty);
1033 /* Add a newline if ECHOK is on and ECHOKE is off. */
1034 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001035 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return;
1037 }
1038 kill_type = KILL;
1039 }
1040
1041 seen_alnums = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001042 while (ldata->read_head != ldata->canon_head) {
1043 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /* erase a single possibly multibyte character */
1046 do {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001047 head--;
1048 c = read_buf(ldata, head);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001049 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* do not partially erase */
1052 if (is_continuation(c, tty))
1053 break;
1054
1055 if (kill_type == WERASE) {
1056 /* Equivalent to BSD's ALTWERASE. */
1057 if (isalnum(c) || c == '_')
1058 seen_alnums++;
1059 else if (seen_alnums)
1060 break;
1061 }
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001062 cnt = ldata->read_head - head;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001063 ldata->read_head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (L_ECHO(tty)) {
1065 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001066 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001067 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001068 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 /* if cnt > 1, output a multi-byte character */
1071 echo_char(c, tty);
1072 while (--cnt > 0) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001073 head++;
1074 echo_char_raw(read_buf(ldata, head), ldata);
Jiri Slaby57c94122012-10-18 22:26:43 +02001075 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1078 echo_char(ERASE_CHAR(tty), tty);
1079 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +00001080 unsigned int num_chars = 0;
1081 int after_tab = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001082 size_t tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Joe Petersona88a69c2009-01-02 13:40:53 +00001084 /*
1085 * Count the columns used for characters
1086 * since the start of input or after a
1087 * previous tab.
1088 * This info is used to go back the correct
1089 * number of columns.
1090 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001091 while (tail != ldata->canon_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001092 tail--;
1093 c = read_buf(ldata, tail);
Joe Petersona88a69c2009-01-02 13:40:53 +00001094 if (c == '\t') {
1095 after_tab = 1;
1096 break;
Alan Cox300a6202009-01-02 13:41:04 +00001097 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001099 num_chars += 2;
1100 } else if (!is_continuation(c, tty)) {
1101 num_chars++;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001104 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001107 echo_char_raw('\b', ldata);
1108 echo_char_raw(' ', ldata);
1109 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001112 echo_char_raw('\b', ldata);
1113 echo_char_raw(' ', ldata);
1114 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 }
1117 }
1118 if (kill_type == ERASE)
1119 break;
1120 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001121 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001122 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/**
1126 * isig - handle the ISIG optio
1127 * @sig: signal
1128 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001130 * Called when a signal is being sent due to terminal input.
1131 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001132 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001133 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
Alan Cox4edf1822008-02-08 04:18:44 -08001135
Peter Hurley8c985d12013-03-06 08:38:19 -05001136static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Peter Hurley8c985d12013-03-06 08:38:19 -05001138 struct pid *tty_pgrp = tty_get_pgrp(tty);
1139 if (tty_pgrp) {
1140 kill_pgrp(tty_pgrp, sig, 1);
1141 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143}
1144
1145/**
1146 * n_tty_receive_break - handle break
1147 * @tty: terminal
1148 *
1149 * An RS232 break event has been hit in the incoming bitstream. This
1150 * can cause a variety of events depending upon the termios settings.
1151 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001152 * n_tty_receive_buf()/producer path:
1153 * caller holds non-exclusive termios_rwsem
1154 * publishes read_head via put_tty_queue()
1155 *
1156 * Note: may get exclusive termios_rwsem if flushing input buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Alan Cox4edf1822008-02-08 04:18:44 -08001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159static inline void n_tty_receive_break(struct tty_struct *tty)
1160{
Jiri Slaby57c94122012-10-18 22:26:43 +02001161 struct n_tty_data *ldata = tty->disc_data;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (I_IGNBRK(tty))
1164 return;
1165 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001166 isig(SIGINT, tty);
1167 if (!L_NOFLSH(tty)) {
Peter Hurley6d76bd22013-06-15 09:14:26 -04001168 /* flushing needs exclusive termios_rwsem */
1169 up_read(&tty->termios_rwsem);
Peter Hurley8c985d12013-03-06 08:38:19 -05001170 n_tty_flush_buffer(tty);
1171 tty_driver_flush_buffer(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001172 down_read(&tty->termios_rwsem);
Peter Hurley8c985d12013-03-06 08:38:19 -05001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return;
1175 }
1176 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001177 put_tty_queue('\377', ldata);
1178 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001180 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 wake_up_interruptible(&tty->read_wait);
1182}
1183
1184/**
1185 * n_tty_receive_overrun - handle overrun reporting
1186 * @tty: terminal
1187 *
1188 * Data arrived faster than we could process it. While the tty
1189 * driver has flagged this the bits that were missed are gone
1190 * forever.
1191 *
1192 * Called from the receive_buf path so single threaded. Does not
1193 * need locking as num_overrun and overrun_time are function
1194 * private.
1195 */
Alan Cox4edf1822008-02-08 04:18:44 -08001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static inline void n_tty_receive_overrun(struct tty_struct *tty)
1198{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001199 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 char buf[64];
1201
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001202 ldata->num_overrun++;
1203 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1204 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1206 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001207 ldata->num_overrun);
1208 ldata->overrun_time = jiffies;
1209 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211}
1212
1213/**
1214 * n_tty_receive_parity_error - error notifier
1215 * @tty: terminal device
1216 * @c: character
1217 *
1218 * Process a parity error and queue the right data to indicate
Peter Hurley6d76bd22013-06-15 09:14:26 -04001219 * the error case if necessary.
1220 *
1221 * n_tty_receive_buf()/producer path:
1222 * caller holds non-exclusive termios_rwsem
1223 * publishes read_head via put_tty_queue()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
1225static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1226 unsigned char c)
1227{
Jiri Slaby57c94122012-10-18 22:26:43 +02001228 struct n_tty_data *ldata = tty->disc_data;
1229
Alan Cox4edf1822008-02-08 04:18:44 -08001230 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001233 put_tty_queue('\377', ldata);
1234 put_tty_queue('\0', ldata);
1235 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001237 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001239 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 wake_up_interruptible(&tty->read_wait);
1241}
1242
1243/**
1244 * n_tty_receive_char - perform processing
1245 * @tty: terminal device
1246 * @c: character
1247 *
1248 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001249 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * driver above.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001251 *
1252 * n_tty_receive_buf()/producer path:
1253 * caller holds non-exclusive termios_rwsem
1254 * publishes canon_head if canonical mode is active
1255 * otherwise, publishes read_head via put_tty_queue()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257
1258static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1259{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001260 struct n_tty_data *ldata = tty->disc_data;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001261 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001263 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001264 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return;
1266 }
Alan Cox4edf1822008-02-08 04:18:44 -08001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (I_ISTRIP(tty))
1269 c &= 0x7f;
1270 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001271 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
hyc@symas.com26df6d12010-06-22 10:14:49 -07001273 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001274 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001275 return;
1276 }
1277
Joe Peterson54d2a372008-02-06 01:37:59 -08001278 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001279 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1280 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001281 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001282 process_echoes(tty);
1283 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (tty->closing) {
1286 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001287 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001289 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001290 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 stop_tty(tty);
1292 }
1293 return;
1294 }
1295
1296 /*
1297 * If the previous character was LNEXT, or we know that this
1298 * character is not one of the characters that we'll have to
1299 * handle specially, do shortcut processing to speed things
1300 * up.
1301 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001302 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001303 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001304 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001305 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001306 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001307 if (L_ECHO(tty))
1308 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001309 return;
1310 }
1311 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001312 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001314 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001315 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001317 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001320 put_tty_queue(c, ldata);
1321 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return;
1323 }
Alan Cox4edf1822008-02-08 04:18:44 -08001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (I_IXON(tty)) {
1326 if (c == START_CHAR(tty)) {
1327 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001328 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return;
1330 }
1331 if (c == STOP_CHAR(tty)) {
1332 stop_tty(tty);
1333 return;
1334 }
1335 }
Joe Peterson575537b32008-04-30 00:53:30 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (L_ISIG(tty)) {
1338 int signal;
1339 signal = SIGINT;
1340 if (c == INTR_CHAR(tty))
1341 goto send_signal;
1342 signal = SIGQUIT;
1343 if (c == QUIT_CHAR(tty))
1344 goto send_signal;
1345 signal = SIGTSTP;
1346 if (c == SUSP_CHAR(tty)) {
1347send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001348 if (!L_NOFLSH(tty)) {
Peter Hurley6d76bd22013-06-15 09:14:26 -04001349 /* flushing needs exclusive termios_rwsem */
1350 up_read(&tty->termios_rwsem);
Joe Petersonec5b1152008-02-06 01:37:38 -08001351 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001352 tty_driver_flush_buffer(tty);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001353 down_read(&tty->termios_rwsem);
Joe Petersonec5b1152008-02-06 01:37:38 -08001354 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001355 if (I_IXON(tty))
1356 start_tty(tty);
1357 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001358 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001359 process_echoes(tty);
1360 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001361 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return;
1363 }
1364 }
Joe Peterson575537b32008-04-30 00:53:30 -07001365
1366 if (c == '\r') {
1367 if (I_IGNCR(tty))
1368 return;
1369 if (I_ICRNL(tty))
1370 c = '\n';
1371 } else if (c == '\n' && I_INLCR(tty))
1372 c = '\r';
1373
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001374 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1376 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1377 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001378 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return;
1380 }
1381 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001382 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001384 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001386 echo_char_raw('^', ldata);
1387 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001388 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 }
1391 return;
1392 }
1393 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1394 L_IEXTEN(tty)) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001395 size_t tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jiri Slaby57c94122012-10-18 22:26:43 +02001397 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001399 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001400 while (tail != ldata->read_head) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001401 echo_char(read_buf(ldata, tail), tty);
1402 tail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001404 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return;
1406 }
1407 if (c == '\n') {
Peter Hurleyce741172013-06-15 09:14:20 -04001408 if (read_cnt(ldata) >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001409 if (L_ECHO(tty))
1410 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001411 return;
1412 }
1413 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001414 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001415 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 goto handle_newline;
1418 }
1419 if (c == EOF_CHAR(tty)) {
Peter Hurleyce741172013-06-15 09:14:20 -04001420 if (read_cnt(ldata) >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001421 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001422 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001423 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 c = __DISABLED_CHAR;
1425 goto handle_newline;
1426 }
1427 if ((c == EOL_CHAR(tty)) ||
1428 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001429 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1430 ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001431 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001432 if (L_ECHO(tty))
1433 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001434 return;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /*
1437 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1438 */
1439 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001441 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001442 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001444 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 /*
1447 * XXX does PARMRK doubling happen for
1448 * EOL_CHAR and EOL2_CHAR?
1449 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001450 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001451 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alan Cox4edf1822008-02-08 04:18:44 -08001453handle_newline:
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001454 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
Peter Hurley6d76bd22013-06-15 09:14:26 -04001455 put_tty_queue(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001456 ldata->canon_head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1458 if (waitqueue_active(&tty->read_wait))
1459 wake_up_interruptible(&tty->read_wait);
1460 return;
1461 }
1462 }
Alan Cox4edf1822008-02-08 04:18:44 -08001463
Joe Petersonacc71bb2009-01-02 13:43:32 +00001464 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Peter Hurleyce741172013-06-15 09:14:20 -04001465 if (read_cnt(ldata) >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001466 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001467 if (L_ECHO(tty))
1468 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001469 return;
1470 }
1471 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001472 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001474 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else {
1476 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001477 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001478 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 echo_char(c, tty);
1480 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001481 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
Joe Petersonacc71bb2009-01-02 13:43:32 +00001484 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001485 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Jiri Slaby57c94122012-10-18 22:26:43 +02001487 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/**
1491 * n_tty_receive_buf - data receive
1492 * @tty: terminal device
1493 * @cp: buffer
1494 * @fp: flag buffer
1495 * @count: characters
1496 *
1497 * Called by the terminal driver when a block of characters has
1498 * been received. This function must be called from soft contexts
1499 * not from interrupt context. The driver is responsible for making
1500 * calls one at a time and in order (or using flush_to_ldisc)
Peter Hurley6d76bd22013-06-15 09:14:26 -04001501 *
1502 * n_tty_receive_buf()/producer path:
1503 * claims non-exclusive termios_rwsem
1504 * publishes read_head and canon_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 */
Alan Cox4edf1822008-02-08 04:18:44 -08001506
Peter Hurley24a89d12013-06-15 09:14:15 -04001507static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1508 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001510 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 const unsigned char *p;
1512 char *f, flags = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001515 if (ldata->real_raw) {
Peter Hurleyd1913e32013-06-15 09:14:28 -04001516 size_t n, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Peter Hurleyd1913e32013-06-15 09:14:28 -04001518 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1519 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1520 n = min_t(size_t, count, n);
1521 memcpy(read_buf_addr(ldata, head), cp, n);
1522 ldata->read_head += n;
1523 cp += n;
1524 count -= n;
1525
1526 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1527 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1528 n = min_t(size_t, count, n);
1529 memcpy(read_buf_addr(ldata, head), cp, n);
1530 ldata->read_head += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Peter Hurleyd1913e32013-06-15 09:14:28 -04001532 int i;
1533
Alan Cox4edf1822008-02-08 04:18:44 -08001534 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (f)
1536 flags = *f++;
1537 switch (flags) {
1538 case TTY_NORMAL:
1539 n_tty_receive_char(tty, *p);
1540 break;
1541 case TTY_BREAK:
1542 n_tty_receive_break(tty);
1543 break;
1544 case TTY_PARITY:
1545 case TTY_FRAME:
1546 n_tty_receive_parity_error(tty, *p);
1547 break;
1548 case TTY_OVERRUN:
1549 n_tty_receive_overrun(tty);
1550 break;
1551 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001552 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 tty_name(tty, buf), flags);
1554 break;
1555 }
1556 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001557 if (tty->ops->flush_chars)
1558 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Peter Hurleyce741172013-06-15 09:14:20 -04001561 if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001562 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1564 if (waitqueue_active(&tty->read_wait))
1565 wake_up_interruptible(&tty->read_wait);
1566 }
1567
Peter Hurley6367ca72013-06-15 09:14:33 -04001568 n_tty_check_throttle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Peter Hurley24a89d12013-06-15 09:14:15 -04001571static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1572 char *fp, int count)
1573{
Peter Hurley9356b532013-06-15 09:14:24 -04001574 down_read(&tty->termios_rwsem);
Peter Hurley24a89d12013-06-15 09:14:15 -04001575 __receive_buf(tty, cp, fp, count);
Peter Hurley9356b532013-06-15 09:14:24 -04001576 up_read(&tty->termios_rwsem);
Peter Hurley24a89d12013-06-15 09:14:15 -04001577}
1578
1579static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1580 char *fp, int count)
1581{
1582 struct n_tty_data *ldata = tty->disc_data;
1583 int room;
1584
Peter Hurley9356b532013-06-15 09:14:24 -04001585 down_read(&tty->termios_rwsem);
1586
Peter Hurley24a89d12013-06-15 09:14:15 -04001587 tty->receive_room = room = receive_room(tty);
1588 if (!room)
1589 ldata->no_room = 1;
1590 count = min(count, room);
1591 if (count)
1592 __receive_buf(tty, cp, fp, count);
1593
Peter Hurley9356b532013-06-15 09:14:24 -04001594 up_read(&tty->termios_rwsem);
1595
Peter Hurley24a89d12013-06-15 09:14:15 -04001596 return count;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599int is_ignored(int sig)
1600{
1601 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001602 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605/**
1606 * n_tty_set_termios - termios data changed
1607 * @tty: terminal
1608 * @old: previous data
1609 *
1610 * Called by the tty layer when the user changes termios flags so
1611 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001612 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * guaranteed that this function will not be re-entered or in progress
1614 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001615 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001616 * Locking: Caller holds tty->termios_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Alan Cox4edf1822008-02-08 04:18:44 -08001618
1619static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001621 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001622 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001623
1624 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001625 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001626 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001627 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001628 ldata->canon_head = ldata->read_tail;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001629 ldata->erasing = 0;
Peter Hurley6f9b0282013-06-15 09:14:27 -04001630 ldata->lnext = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001631 }
1632
Peter Hurleyce741172013-06-15 09:14:20 -04001633 if (canon_change && !L_ICANON(tty) && read_cnt(ldata))
Alan Cox47afa7a2008-10-13 10:44:17 +01001634 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001635
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001636 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1639 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1640 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1641 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001642 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001645 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001647 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001650 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1651 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1652 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1653 set_bit('\n', ldata->process_char_map);
1654 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (L_IEXTEN(tty)) {
1656 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001657 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001659 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001661 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (L_ECHO(tty))
1663 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001664 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666 }
1667 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001668 set_bit(START_CHAR(tty), ldata->process_char_map);
1669 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001672 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1673 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1674 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001676 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001677 ldata->raw = 0;
1678 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001680 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1682 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1683 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001684 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001686 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001688 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001689 /*
1690 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1691 * been stopped by STOP_CHAR(tty) before it.
1692 */
1693 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1694 start_tty(tty);
1695 }
1696
Alan Coxf34d7a52008-04-30 00:54:13 -07001697 /* The termios change make the tty ready for I/O */
1698 wake_up_interruptible(&tty->write_wait);
1699 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702/**
1703 * n_tty_close - close the ldisc for this tty
1704 * @tty: device
1705 *
Alan Cox4edf1822008-02-08 04:18:44 -08001706 * Called from the terminal layer when this line discipline is
1707 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * discipline change. The function will not be called while other
1709 * ldisc methods are in progress.
1710 */
Alan Cox4edf1822008-02-08 04:18:44 -08001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static void n_tty_close(struct tty_struct *tty)
1713{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001714 struct n_tty_data *ldata = tty->disc_data;
1715
Peter Hurley79901312013-03-11 16:44:23 -04001716 if (tty->link)
1717 n_tty_packet_mode_flush(tty);
1718
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001719 kfree(ldata->read_buf);
1720 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001721 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001722 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725/**
1726 * n_tty_open - open an ldisc
1727 * @tty: terminal to open
1728 *
Alan Cox4edf1822008-02-08 04:18:44 -08001729 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * terminal device. Can sleep. Called serialized so that no
1731 * other events will occur in parallel. No further open will occur
1732 * until a close.
1733 */
1734
1735static int n_tty_open(struct tty_struct *tty)
1736{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001737 struct n_tty_data *ldata;
1738
1739 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1740 if (!ldata)
1741 goto err;
1742
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001743 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001744 mutex_init(&ldata->atomic_read_lock);
1745 mutex_init(&ldata->output_lock);
1746 mutex_init(&ldata->echo_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001747
Joe Petersona88a69c2009-01-02 13:40:53 +00001748 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001749 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1750 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1751 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001752 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001753
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001754 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001755 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001756 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001757 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001759 /* indicate buffer work may resume */
1760 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1761 n_tty_set_termios(tty, NULL);
1762 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001765err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001766 kfree(ldata->read_buf);
1767 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001768 kfree(ldata);
1769err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001770 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773static inline int input_available_p(struct tty_struct *tty, int amt)
1774{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001775 struct n_tty_data *ldata = tty->disc_data;
1776
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001777 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurleya73d3d62013-06-15 09:14:25 -04001778 if (ldata->canon_head != ldata->read_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return 1;
Peter Hurleyce741172013-06-15 09:14:20 -04001780 } else if (read_cnt(ldata) >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return 1;
1782
1783 return 0;
1784}
1785
1786/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001787 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * @tty: terminal device
1789 * @b: user data
1790 * @nr: size of data
1791 *
Alan Cox11a96d12008-10-13 10:46:24 +01001792 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * ICANON is off; it copies characters straight from the tty queue to
1794 * user space directly. It can be profitably called twice; once to
1795 * drain the space from the tail pointer to the (physical) end of the
1796 * buffer, and once to drain the space from the (physical) beginning of
1797 * the buffer to head pointer.
1798 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001799 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *
Peter Hurley6d76bd22013-06-15 09:14:26 -04001801 * n_tty_read()/consumer path:
1802 * caller holds non-exclusive termios_rwsem
1803 * read_tail published
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 */
Alan Cox4edf1822008-02-08 04:18:44 -08001805
Alan Cox33f0f882006-01-09 20:54:13 -08001806static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 unsigned char __user **b,
1808 size_t *nr)
1809
1810{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001811 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int retval;
1813 size_t n;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001814 bool is_eof;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001815 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 retval = 0;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001818 n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 n = min(*nr, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (n) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001821 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 n -= retval;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001823 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
1824 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001825 ldata->icanon);
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001826 ldata->read_tail += n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001827 /* Turn single EOF into zero-length read */
Peter Hurleyce741172013-06-15 09:14:20 -04001828 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001829 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *b += n;
1831 *nr -= n;
1832 }
1833 return retval;
1834}
1835
Peter Hurley88bb0de2013-06-15 09:14:16 -04001836/**
Peter Hurley32f13522013-06-15 09:14:17 -04001837 * canon_copy_from_read_buf - copy read data in canonical mode
Peter Hurley88bb0de2013-06-15 09:14:16 -04001838 * @tty: terminal device
1839 * @b: user data
1840 * @nr: size of data
1841 *
1842 * Helper function for n_tty_read. It is only called when ICANON is on;
Peter Hurley32f13522013-06-15 09:14:17 -04001843 * it copies one line of input up to and including the line-delimiting
1844 * character into the user-space buffer.
Peter Hurley88bb0de2013-06-15 09:14:16 -04001845 *
1846 * Called under the atomic_read_lock mutex
Peter Hurley6d76bd22013-06-15 09:14:26 -04001847 *
1848 * n_tty_read()/consumer path:
1849 * caller holds non-exclusive termios_rwsem
1850 * read_tail published
Peter Hurley88bb0de2013-06-15 09:14:16 -04001851 */
1852
Peter Hurley32f13522013-06-15 09:14:17 -04001853static int canon_copy_from_read_buf(struct tty_struct *tty,
1854 unsigned char __user **b,
1855 size_t *nr)
Peter Hurley88bb0de2013-06-15 09:14:16 -04001856{
1857 struct n_tty_data *ldata = tty->disc_data;
Peter Hurley32f13522013-06-15 09:14:17 -04001858 size_t n, size, more, c;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001859 size_t eol;
1860 size_t tail;
1861 int ret, found = 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001862
1863 /* N.B. avoid overrun if nr == 0 */
Peter Hurleyce741172013-06-15 09:14:20 -04001864 n = min(*nr, read_cnt(ldata));
Peter Hurley6d76bd22013-06-15 09:14:26 -04001865 if (!n)
Peter Hurley32f13522013-06-15 09:14:17 -04001866 return 0;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001867
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001868 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
Peter Hurley32f13522013-06-15 09:14:17 -04001869 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1870
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001871 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04001872 __func__, *nr, tail, n, size);
1873
1874 eol = find_next_bit(ldata->read_flags, size, tail);
1875 more = n - (size - tail);
1876 if (eol == N_TTY_BUF_SIZE && more) {
1877 /* scan wrapped without finding set bit */
1878 eol = find_next_bit(ldata->read_flags, more, 0);
1879 if (eol != more)
1880 found = 1;
1881 } else if (eol != size)
1882 found = 1;
1883
1884 size = N_TTY_BUF_SIZE - tail;
1885 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
1886 c = n;
1887
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001888 if (found && read_buf(ldata, eol) == __DISABLED_CHAR)
Peter Hurley32f13522013-06-15 09:14:17 -04001889 n--;
1890
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001891 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
Peter Hurley32f13522013-06-15 09:14:17 -04001892 __func__, eol, found, n, c, size, more);
1893
Peter Hurley32f13522013-06-15 09:14:17 -04001894 if (n > size) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001895 ret = copy_to_user(*b, read_buf_addr(ldata, tail), size);
Peter Hurley32f13522013-06-15 09:14:17 -04001896 if (ret)
1897 return -EFAULT;
1898 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
1899 } else
Peter Hurleybc5a5e32013-06-15 09:14:21 -04001900 ret = copy_to_user(*b, read_buf_addr(ldata, tail), n);
Peter Hurley32f13522013-06-15 09:14:17 -04001901
1902 if (ret)
1903 return -EFAULT;
1904 *b += n;
1905 *nr -= n;
1906
Peter Hurleya73d3d62013-06-15 09:14:25 -04001907 if (found)
Peter Hurley6d76bd22013-06-15 09:14:26 -04001908 clear_bit(eol, ldata->read_flags);
1909 smp_mb__after_clear_bit();
1910 ldata->read_tail += c;
Peter Hurley88bb0de2013-06-15 09:14:16 -04001911
Peter Hurley32f13522013-06-15 09:14:17 -04001912 if (found)
1913 tty_audit_push(tty);
Peter Hurley88bb0de2013-06-15 09:14:16 -04001914 return 0;
1915}
1916
Al Virocc4191d2008-03-29 03:08:48 +00001917extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001918 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920/**
1921 * job_control - check job control
1922 * @tty: tty
1923 * @file: file handle
1924 *
1925 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001926 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001928 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001929 * Locking: redirected write test is safe
1930 * current->signal->tty check is safe
1931 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Alan Cox4edf1822008-02-08 04:18:44 -08001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static int job_control(struct tty_struct *tty, struct file *file)
1935{
1936 /* Job control check -- must be done at start and after
1937 every sleep (POSIX.1 7.1.1.4). */
1938 /* NOTE: not yet done after every sleep pending a thorough
1939 check of the logic of this change. -- jlc */
1940 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001941 if (file->f_op->write == redirected_tty_write ||
1942 current->signal->tty != tty)
1943 return 0;
1944
1945 spin_lock_irq(&tty->ctrl_lock);
1946 if (!tty->pgrp)
1947 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1948 else if (task_pgrp(current) != tty->pgrp) {
1949 spin_unlock_irq(&tty->ctrl_lock);
1950 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1951 return -EIO;
1952 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1953 set_thread_flag(TIF_SIGPENDING);
1954 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001956 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
1958}
Alan Cox4edf1822008-02-08 04:18:44 -08001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961/**
Alan Cox11a96d12008-10-13 10:46:24 +01001962 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * @tty: tty device
1964 * @file: file object
1965 * @buf: userspace buffer pointer
1966 * @nr: size of I/O
1967 *
1968 * Perform reads for the line discipline. We are guaranteed that the
1969 * line discipline will not be closed under us but we may get multiple
1970 * parallel readers and must handle this ourselves. We may also get
1971 * a hangup. Always called in user context, may sleep.
1972 *
1973 * This code must be sure never to sleep through a hangup.
Peter Hurley6d76bd22013-06-15 09:14:26 -04001974 *
1975 * n_tty_read()/consumer path:
1976 * claims non-exclusive termios_rwsem
1977 * publishes read_tail
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Alan Cox4edf1822008-02-08 04:18:44 -08001979
Alan Cox11a96d12008-10-13 10:46:24 +01001980static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 unsigned char __user *buf, size_t nr)
1982{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001983 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 unsigned char __user *b = buf;
1985 DECLARE_WAITQUEUE(wait, current);
1986 int c;
1987 int minimum, time;
1988 ssize_t retval = 0;
1989 ssize_t size;
1990 long timeout;
1991 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001992 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001996 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001998
Peter Hurley9356b532013-06-15 09:14:24 -04001999 down_read(&tty->termios_rwsem);
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 minimum = time = 0;
2002 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002003 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 minimum = MIN_CHAR(tty);
2005 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04002006 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002008 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002010 (ldata->minimum_to_wake > minimum))
2011 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04002013 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002014 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016 }
2017
2018 /*
2019 * Internal serialization of reads.
2020 */
2021 if (file->f_flags & O_NONBLOCK) {
Peter Hurley9356b532013-06-15 09:14:24 -04002022 if (!mutex_trylock(&ldata->atomic_read_lock)) {
2023 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return -EAGAIN;
Peter Hurley9356b532013-06-15 09:14:24 -04002025 }
Alan Cox4edf1822008-02-08 04:18:44 -08002026 } else {
Peter Hurley9356b532013-06-15 09:14:24 -04002027 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) {
2028 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -ERESTARTSYS;
Peter Hurley9356b532013-06-15 09:14:24 -04002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
Alan Cox04f378b2008-04-30 00:53:29 -07002032 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 while (nr) {
2036 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07002037 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 unsigned char cs;
2039 if (b != buf)
2040 break;
Alan Cox04f378b2008-04-30 00:53:29 -07002041 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 cs = tty->link->ctrl_status;
2043 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002044 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07002045 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 retval = -EFAULT;
2047 b--;
2048 break;
2049 }
2050 nr--;
2051 break;
2052 }
2053 /* This statement must be first before checking for input
2054 so that any interrupt will set the state back to
2055 TASK_RUNNING. */
2056 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08002057
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002058 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002060 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!input_available_p(tty, 0)) {
2063 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2064 retval = -EIO;
2065 break;
2066 }
2067 if (tty_hung_up_p(file))
2068 break;
2069 if (!timeout)
2070 break;
2071 if (file->f_flags & O_NONBLOCK) {
2072 retval = -EAGAIN;
2073 break;
2074 }
2075 if (signal_pending(current)) {
2076 retval = -ERESTARTSYS;
2077 break;
2078 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09002079 n_tty_set_room(tty);
Peter Hurley9356b532013-06-15 09:14:24 -04002080 up_read(&tty->termios_rwsem);
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 timeout = schedule_timeout(timeout);
Peter Hurley9356b532013-06-15 09:14:24 -04002083
2084 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 continue;
2086 }
2087 __set_current_state(TASK_RUNNING);
2088
2089 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07002090 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07002091 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 retval = -EFAULT;
2093 b--;
2094 break;
2095 }
2096 nr--;
2097 }
2098
Jiri Slaby53c5ee22012-10-18 22:26:39 +02002099 if (ldata->icanon && !L_EXTPROC(tty)) {
Peter Hurley32f13522013-06-15 09:14:17 -04002100 retval = canon_copy_from_read_buf(tty, &b, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (retval)
2102 break;
2103 } else {
2104 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07002105 /* The copy function takes the read lock and handles
2106 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 uncopied = copy_from_read_buf(tty, &b, &nr);
2108 uncopied += copy_from_read_buf(tty, &b, &nr);
2109 if (uncopied) {
2110 retval = -EFAULT;
2111 break;
2112 }
2113 }
2114
Peter Hurley6367ca72013-06-15 09:14:33 -04002115 n_tty_check_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (b - buf >= minimum)
2118 break;
2119 if (time)
2120 timeout = time;
2121 }
Jiri Slabybddc7152012-10-18 22:26:42 +02002122 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 remove_wait_queue(&tty->read_wait, &wait);
2124
2125 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002126 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 __set_current_state(TASK_RUNNING);
2129 size = b - buf;
2130 if (size) {
2131 retval = size;
2132 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08002133 clear_bit(TTY_PUSH, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002134 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) {
2135 up_read(&tty->termios_rwsem);
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01002136 goto do_it_again;
Peter Hurley9356b532013-06-15 09:14:24 -04002137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Linus Torvalds55db4c62011-06-04 06:33:24 +09002139 n_tty_set_room(tty);
Peter Hurley9356b532013-06-15 09:14:24 -04002140 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return retval;
2142}
2143
2144/**
Alan Cox11a96d12008-10-13 10:46:24 +01002145 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 * @tty: tty device
2147 * @file: file object
2148 * @buf: userspace buffer pointer
2149 * @nr: size of I/O
2150 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002151 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002153 * and other such events. Since the receive code will echo characters,
2154 * thus calling driver write methods, the output_lock is used in
2155 * the output processing functions called here as well as in the
2156 * echo processing function to protect the column state and space
2157 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 *
2159 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002160 *
2161 * Locking: output_lock to protect column state and space left
2162 * (note that the process_output*() functions take this
2163 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Alan Cox4edf1822008-02-08 04:18:44 -08002165
Alan Cox11a96d12008-10-13 10:46:24 +01002166static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002167 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 const unsigned char *b = buf;
2170 DECLARE_WAITQUEUE(wait, current);
2171 int c;
2172 ssize_t retval = 0;
2173
2174 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2175 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2176 retval = tty_check_change(tty);
2177 if (retval)
2178 return retval;
2179 }
2180
Peter Hurley9356b532013-06-15 09:14:24 -04002181 down_read(&tty->termios_rwsem);
2182
Joe Petersona88a69c2009-01-02 13:40:53 +00002183 /* Write out any echoed characters that are still pending */
2184 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 add_wait_queue(&tty->write_wait, &wait);
2187 while (1) {
2188 set_current_state(TASK_INTERRUPTIBLE);
2189 if (signal_pending(current)) {
2190 retval = -ERESTARTSYS;
2191 break;
2192 }
2193 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2194 retval = -EIO;
2195 break;
2196 }
Peter Hurley582f5592013-05-17 12:49:48 -04002197 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002199 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (num < 0) {
2201 if (num == -EAGAIN)
2202 break;
2203 retval = num;
2204 goto break_out;
2205 }
2206 b += num;
2207 nr -= num;
2208 if (nr == 0)
2209 break;
2210 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002211 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 break;
2213 b++; nr--;
2214 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002215 if (tty->ops->flush_chars)
2216 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002218 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002219 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002220 if (c < 0) {
2221 retval = c;
2222 goto break_out;
2223 }
2224 if (!c)
2225 break;
2226 b += c;
2227 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230 if (!nr)
2231 break;
2232 if (file->f_flags & O_NONBLOCK) {
2233 retval = -EAGAIN;
2234 break;
2235 }
Peter Hurley9356b532013-06-15 09:14:24 -04002236 up_read(&tty->termios_rwsem);
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 schedule();
Peter Hurley9356b532013-06-15 09:14:24 -04002239
2240 down_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242break_out:
2243 __set_current_state(TASK_RUNNING);
2244 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002245 if (b - buf != nr && tty->fasync)
2246 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Peter Hurley9356b532013-06-15 09:14:24 -04002247 up_read(&tty->termios_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return (b - buf) ? b - buf : retval;
2249}
2250
2251/**
Alan Cox11a96d12008-10-13 10:46:24 +01002252 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 * @tty: terminal device
2254 * @file: file accessing it
2255 * @wait: poll table
2256 *
2257 * Called when the line discipline is asked to poll() for data or
2258 * for special events. This code is not serialized with respect to
2259 * other events save open/close.
2260 *
2261 * This code must be sure never to sleep through a hangup.
2262 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
Alan Cox4edf1822008-02-08 04:18:44 -08002264
Alan Cox11a96d12008-10-13 10:46:24 +01002265static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002266 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002268 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 unsigned int mask = 0;
2270
2271 poll_wait(file, &tty->read_wait, wait);
2272 poll_wait(file, &tty->write_wait, wait);
2273 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2274 mask |= POLLIN | POLLRDNORM;
2275 if (tty->packet && tty->link->ctrl_status)
2276 mask |= POLLPRI | POLLIN | POLLRDNORM;
2277 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2278 mask |= POLLHUP;
2279 if (tty_hung_up_p(file))
2280 mask |= POLLHUP;
2281 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2282 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002283 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002285 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002287 if (tty->ops->write && !tty_is_writelocked(tty) &&
2288 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2289 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 mask |= POLLOUT | POLLWRNORM;
2291 return mask;
2292}
2293
Jiri Slaby57c94122012-10-18 22:26:43 +02002294static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002295{
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002296 size_t nr, head, tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002297
Peter Hurleya73d3d62013-06-15 09:14:25 -04002298 if (ldata->canon_head == ldata->read_tail)
Alan Cox47afa7a2008-10-13 10:44:17 +01002299 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002300 head = ldata->canon_head;
2301 tail = ldata->read_tail;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002302 nr = head - tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002303 /* Skip EOF-chars.. */
2304 while (head != tail) {
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002305 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2306 read_buf(ldata, tail) == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002307 nr--;
Peter Hurleybc5a5e32013-06-15 09:14:21 -04002308 tail++;
Alan Cox47afa7a2008-10-13 10:44:17 +01002309 }
2310 return nr;
2311}
2312
2313static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2314 unsigned int cmd, unsigned long arg)
2315{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002316 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002317 int retval;
2318
2319 switch (cmd) {
2320 case TIOCOUTQ:
2321 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2322 case TIOCINQ:
Peter Hurley6d76bd22013-06-15 09:14:26 -04002323 down_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002324 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002325 retval = inq_canon(ldata);
Peter Hurley6d76bd22013-06-15 09:14:26 -04002326 else
2327 retval = read_cnt(ldata);
2328 up_write(&tty->termios_rwsem);
Alan Cox47afa7a2008-10-13 10:44:17 +01002329 return put_user(retval, (unsigned int __user *) arg);
2330 default:
2331 return n_tty_ioctl_helper(tty, file, cmd, arg);
2332 }
2333}
2334
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002335static void n_tty_fasync(struct tty_struct *tty, int on)
2336{
2337 struct n_tty_data *ldata = tty->disc_data;
2338
2339 if (!waitqueue_active(&tty->read_wait)) {
2340 if (on)
2341 ldata->minimum_to_wake = 1;
2342 else if (!tty->fasync)
2343 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2344 }
2345}
2346
Alan Coxa352def2008-07-16 21:53:12 +01002347struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002348 .magic = TTY_LDISC_MAGIC,
2349 .name = "n_tty",
2350 .open = n_tty_open,
2351 .close = n_tty_close,
2352 .flush_buffer = n_tty_flush_buffer,
2353 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002354 .read = n_tty_read,
2355 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002356 .ioctl = n_tty_ioctl,
2357 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002358 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002359 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002360 .write_wakeup = n_tty_write_wakeup,
2361 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002362 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002364
2365/**
2366 * n_tty_inherit_ops - inherit N_TTY methods
2367 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2368 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002369 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002370 * methods.
2371 */
2372
2373void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2374{
2375 *ops = tty_ldisc_N_TTY;
2376 ops->owner = NULL;
2377 ops->refcount = ops->flags = 0;
2378}
2379EXPORT_SYMBOL_GPL(n_tty_inherit_ops);