blob: eddeb7889e627c2f6c931af41f230d1a6eccf360 [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
Jiri Slaby70ece7a2012-10-18 22:26:38 +020077struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020078 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
Peter Hurley24a89d12013-06-15 09:14:15 -040082 /* non-atomic */
83 bool no_room;
84
Jiri Slaby53c5ee22012-10-18 22:26:39 +020085 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
86 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020087
88 DECLARE_BITMAP(process_char_map, 256);
89 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020090
91 char *read_buf;
92 int read_head;
93 int read_tail;
94 int read_cnt;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -040095 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +020096
97 unsigned char *echo_buf;
98 unsigned int echo_pos;
99 unsigned int echo_cnt;
100
101 int canon_data;
102 unsigned long canon_head;
103 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200104
105 struct mutex atomic_read_lock;
106 struct mutex output_lock;
107 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100108 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200109};
110
Miloslav Trmac522ed772007-07-15 23:40:56 -0700111static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
112 unsigned char __user *ptr)
113{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200114 struct n_tty_data *ldata = tty->disc_data;
115
116 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700117 return put_user(x, ptr);
118}
119
Peter Hurley24a89d12013-06-15 09:14:15 -0400120static int receive_room(struct tty_struct *tty)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900121{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200122 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500123 int left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400124
Jaeden Amero090abf72012-07-27 08:43:11 -0500125 if (I_PARMRK(tty)) {
126 /* Multiply read_cnt by 3, since each byte might take up to
127 * three times as many spaces when PARMRK is set (depending on
128 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200129 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500130 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200131 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500132
Linus Torvalds55db4c62011-06-04 06:33:24 +0900133 /*
134 * If we are doing input canonicalization, and there are no
135 * pending newlines, let characters through without limit, so
136 * that erase characters will be handled. Other excess
137 * characters will be beeped.
138 */
139 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200140 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900141
Peter Hurley24a89d12013-06-15 09:14:15 -0400142 return left;
Peter Hurley7879a9f2013-06-15 07:28:31 -0400143}
144
Peter Hurley24a89d12013-06-15 09:14:15 -0400145/**
146 * n_tty_set_room - receive space
147 * @tty: terminal
148 *
149 * Re-schedules the flip buffer work if space just became available.
150 *
151 * Locks: Concurrent update is protected with read_lock
152 */
153
Peter Hurley7879a9f2013-06-15 07:28:31 -0400154static void n_tty_set_room(struct tty_struct *tty)
155{
Peter Hurley24a89d12013-06-15 09:14:15 -0400156 struct n_tty_data *ldata = tty->disc_data;
157
Linus Torvalds55db4c62011-06-04 06:33:24 +0900158 /* Did this open up the receive buffer? We may need to flip */
Peter Hurley24a89d12013-06-15 09:14:15 -0400159 if (unlikely(ldata->no_room) && receive_room(tty)) {
160 ldata->no_room = 0;
161
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200162 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400163 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400164 /* see if ldisc has been killed - if so, this means that
165 * even though the ldisc has been halted and ->buf.work
166 * cancelled, ->buf.work is about to be rescheduled
167 */
168 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
169 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200170 schedule_work(&tty->port->buf.work);
171 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900172}
173
Jiri Slaby57c94122012-10-18 22:26:43 +0200174static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200176 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
177 ldata->read_buf[ldata->read_head] = c;
178 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
179 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181}
182
Alan Cox17b82062008-10-13 10:45:06 +0100183/**
184 * put_tty_queue - add character to tty
185 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200186 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100187 *
188 * Add a character to the tty read_buf queue. This is done under the
189 * read_lock to serialize character addition and also to protect us
190 * against parallel reads or flushes
191 */
192
Jiri Slaby57c94122012-10-18 22:26:43 +0200193static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 unsigned long flags;
196 /*
197 * The problem of stomping on the buffers ends here.
198 * Why didn't anyone see this one coming? --AJK
199 */
Ivo Sieben98001212013-01-28 13:32:01 +0100200 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200201 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100202 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * reset_buffer_flags - reset buffer state
207 * @tty: terminal to reset
208 *
Peter Hurley25518c62013-03-11 16:44:31 -0400209 * Reset the read buffer counters and clear the flags.
210 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100211 *
212 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000214
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400215static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 unsigned long flags;
218
Ivo Sieben98001212013-01-28 13:32:01 +0100219 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200220 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100221 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000222
Jiri Slabybddc7152012-10-18 22:26:42 +0200223 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200224 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200225 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000226
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200227 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200228 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Peter Hurleya30737a2013-03-11 16:44:22 -0400231static void n_tty_packet_mode_flush(struct tty_struct *tty)
232{
233 unsigned long flags;
234
235 spin_lock_irqsave(&tty->ctrl_lock, flags);
236 if (tty->link->packet) {
237 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
238 wake_up_interruptible(&tty->link->read_wait);
239 }
240 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
244 * n_tty_flush_buffer - clean input queue
245 * @tty: terminal device
246 *
Peter Hurley25518c62013-03-11 16:44:31 -0400247 * Flush the input buffer. Called when the tty layer wants the
248 * buffer flushed (eg at hangup) or when the N_TTY line discipline
249 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
Alan Cox17b82062008-10-13 10:45:06 +0100251 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Alan Cox4edf1822008-02-08 04:18:44 -0800253
254static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400256 reset_buffer_flags(tty->disc_data);
257 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800258
Peter Hurleya30737a2013-03-11 16:44:22 -0400259 if (tty->link)
260 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/**
264 * n_tty_chars_in_buffer - report available bytes
265 * @tty: tty device
266 *
267 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800268 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100269 *
270 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Alan Cox4edf1822008-02-08 04:18:44 -0800272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
274{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200275 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned long flags;
277 ssize_t n = 0;
278
Ivo Sieben98001212013-01-28 13:32:01 +0100279 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200280 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200281 n = ldata->read_cnt;
282 } else if (ldata->canon_data) {
283 n = (ldata->canon_head > ldata->read_tail) ?
284 ldata->canon_head - ldata->read_tail :
285 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Ivo Sieben98001212013-01-28 13:32:01 +0100287 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return n;
289}
290
291/**
292 * is_utf8_continuation - utf8 multibyte check
293 * @c: byte to check
294 *
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character. We use this to correctly compute the on screen size
297 * of the character when printing
298 */
Alan Cox4edf1822008-02-08 04:18:44 -0800299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static inline int is_utf8_continuation(unsigned char c)
301{
302 return (c & 0xc0) == 0x80;
303}
304
305/**
306 * is_continuation - multibyte check
307 * @c: byte to check
308 *
309 * Returns true if the utf8 character 'c' is a multibyte continuation
310 * character and the terminal is in unicode mode.
311 */
Alan Cox4edf1822008-02-08 04:18:44 -0800312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static inline int is_continuation(unsigned char c, struct tty_struct *tty)
314{
315 return I_IUTF8(tty) && is_utf8_continuation(c);
316}
317
318/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000319 * do_output_char - output one character
320 * @c: character (or partial unicode symbol)
321 * @tty: terminal device
322 * @space: space available in tty driver write buffer
323 *
324 * This is a helper function that handles one output character
325 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600326 * doing OPOST processing and putting the results in the
327 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000328 *
329 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
330 * and NLDLY. They simply aren't relevant in the world today.
331 * If you ever need them, add them here.
332 *
333 * Returns the number of bytes of buffer space used or -1 if
334 * no space left.
335 *
336 * Locking: should be called under the output_lock to protect
337 * the column state and space left in the buffer
338 */
339
340static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
341{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200342 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000343 int spaces;
344
345 if (!space)
346 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000347
Joe Petersona88a69c2009-01-02 13:40:53 +0000348 switch (c) {
349 case '\n':
350 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200351 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000352 if (O_ONLCR(tty)) {
353 if (space < 2)
354 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200355 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700356 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000357 return 2;
358 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200359 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 break;
361 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200362 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000363 return 0;
364 if (O_OCRNL(tty)) {
365 c = '\n';
366 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200367 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000368 break;
369 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200370 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000371 break;
372 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200373 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000374 if (O_TABDLY(tty) == XTABS) {
375 if (space < spaces)
376 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200377 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 tty->ops->write(tty, " ", spaces);
379 return spaces;
380 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200381 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000382 break;
383 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200384 if (ldata->column > 0)
385 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 break;
387 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000388 if (!iscntrl(c)) {
389 if (O_OLCUC(tty))
390 c = toupper(c);
391 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200392 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000393 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000394 break;
395 }
396
397 tty_put_char(tty, c);
398 return 1;
399}
400
401/**
402 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * @c: character (or partial unicode symbol)
404 * @tty: terminal device
405 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600406 * Output one character with OPOST processing.
407 * Returns -1 when the output device is full and the character
408 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000410 * Locking: output_lock to protect column state and space left
411 * (also, this is called from n_tty_write under the
412 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Alan Cox4edf1822008-02-08 04:18:44 -0800414
Joe Petersona88a69c2009-01-02 13:40:53 +0000415static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jiri Slabybddc7152012-10-18 22:26:42 +0200417 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000418 int space, retval;
419
Jiri Slabybddc7152012-10-18 22:26:42 +0200420 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Alan Coxf34d7a52008-04-30 00:54:13 -0700422 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000423 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jiri Slabybddc7152012-10-18 22:26:42 +0200425 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 if (retval < 0)
427 return -1;
428 else
429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000433 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600435 * @buf: character buffer
436 * @nr: number of bytes to output
437 *
438 * Output a block of characters with OPOST processing.
439 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
441 * This path is used to speed up block console writes, among other
442 * things when processing blocks of output data. It handles only
443 * the simple cases normally found and helps to generate blocks of
444 * symbols for the console driver and thus improve performance.
445 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000446 * Locking: output_lock to protect column state and space left
447 * (also, this is called from n_tty_write under the
448 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Alan Cox4edf1822008-02-08 04:18:44 -0800450
Joe Petersona88a69c2009-01-02 13:40:53 +0000451static ssize_t process_output_block(struct tty_struct *tty,
452 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200454 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100456 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 const unsigned char *cp;
458
Jiri Slabybddc7152012-10-18 22:26:42 +0200459 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000460
Alan Coxf34d7a52008-04-30 00:54:13 -0700461 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000462 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200463 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (nr > space)
467 nr = space;
468
469 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000470 unsigned char c = *cp;
471
472 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 case '\n':
474 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200475 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (O_ONLCR(tty))
477 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200478 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200481 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto break_out;
483 if (O_OCRNL(tty))
484 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200485 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case '\t':
488 goto break_out;
489 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200490 if (ldata->column > 0)
491 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000494 if (!iscntrl(c)) {
495 if (O_OLCUC(tty))
496 goto break_out;
497 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200498 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 }
502 }
503break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700504 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000505
Jiri Slabybddc7152012-10-18 22:26:42 +0200506 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return i;
508}
509
Joe Petersona88a69c2009-01-02 13:40:53 +0000510/**
511 * process_echoes - write pending echo characters
512 * @tty: terminal device
513 *
514 * Write previously buffered echo (and other ldisc-generated)
515 * characters to the tty.
516 *
517 * Characters generated by the ldisc (including echoes) need to
518 * be buffered because the driver's write buffer can fill during
519 * heavy program output. Echoing straight to the driver will
520 * often fail under these conditions, causing lost characters and
521 * resulting mismatches of ldisc state information.
522 *
523 * Since the ldisc state must represent the characters actually sent
524 * to the driver at the time of the write, operations like certain
525 * changes in column state are also saved in the buffer and executed
526 * here.
527 *
528 * A circular fifo buffer is used so that the most recent characters
529 * are prioritized. Also, when control characters are echoed with a
530 * prefixed "^", the pair is treated atomically and thus not separated.
531 *
532 * Locking: output_lock to protect column state and space left,
533 * echo_lock to protect the echo buffer
534 */
535
536static void process_echoes(struct tty_struct *tty)
537{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200538 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000539 int space, nr;
540 unsigned char c;
541 unsigned char *cp, *buf_end;
542
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200543 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000544 return;
545
Jiri Slabybddc7152012-10-18 22:26:42 +0200546 mutex_lock(&ldata->output_lock);
547 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000548
549 space = tty_write_room(tty);
550
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200551 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
552 cp = ldata->echo_buf + ldata->echo_pos;
553 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000554 while (nr > 0) {
555 c = *cp;
556 if (c == ECHO_OP_START) {
557 unsigned char op;
558 unsigned char *opp;
559 int no_space_left = 0;
560
561 /*
562 * If the buffer byte is the start of a multi-byte
563 * operation, get the next byte, which is either the
564 * op code or a control character value.
565 */
566 opp = cp + 1;
567 if (opp == buf_end)
568 opp -= N_TTY_BUF_SIZE;
569 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000570
Joe Petersona88a69c2009-01-02 13:40:53 +0000571 switch (op) {
572 unsigned int num_chars, num_bs;
573
574 case ECHO_OP_ERASE_TAB:
575 if (++opp == buf_end)
576 opp -= N_TTY_BUF_SIZE;
577 num_chars = *opp;
578
579 /*
580 * Determine how many columns to go back
581 * in order to erase the tab.
582 * This depends on the number of columns
583 * used by other characters within the tab
584 * area. If this (modulo 8) count is from
585 * the start of input rather than from a
586 * previous tab, we offset by canon column.
587 * Otherwise, tab spacing is normal.
588 */
589 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200590 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000591 num_bs = 8 - (num_chars & 7);
592
593 if (num_bs > space) {
594 no_space_left = 1;
595 break;
596 }
597 space -= num_bs;
598 while (num_bs--) {
599 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200600 if (ldata->column > 0)
601 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000602 }
603 cp += 3;
604 nr -= 3;
605 break;
606
607 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200608 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 cp += 2;
610 nr -= 2;
611 break;
612
613 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200614 if (ldata->column > 0)
615 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000616 cp += 2;
617 nr -= 2;
618 break;
619
620 case ECHO_OP_START:
621 /* This is an escaped echo op start code */
622 if (!space) {
623 no_space_left = 1;
624 break;
625 }
626 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200627 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 space--;
629 cp += 2;
630 nr -= 2;
631 break;
632
633 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000634 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600635 * If the op is not a special byte code,
636 * it is a ctrl char tagged to be echoed
637 * as "^X" (where X is the letter
638 * representing the control char).
639 * Note that we must ensure there is
640 * enough space for the whole ctrl pair.
641 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000642 */
Joe Peterson62b26352009-09-09 15:03:47 -0600643 if (space < 2) {
644 no_space_left = 1;
645 break;
646 }
647 tty_put_char(tty, '^');
648 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200649 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600650 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000651 cp += 2;
652 nr -= 2;
653 }
654
655 if (no_space_left)
656 break;
657 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400658 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600659 int retval = do_output_char(c, tty, space);
660 if (retval < 0)
661 break;
662 space -= retval;
663 } else {
664 if (!space)
665 break;
666 tty_put_char(tty, c);
667 space -= 1;
668 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000669 cp += 1;
670 nr -= 1;
671 }
672
673 /* When end of circular buffer reached, wrap around */
674 if (cp >= buf_end)
675 cp -= N_TTY_BUF_SIZE;
676 }
677
678 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200679 ldata->echo_pos = 0;
680 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200681 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000682 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200683 int num_processed = ldata->echo_cnt - nr;
684 ldata->echo_pos += num_processed;
685 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
686 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200688 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000689 }
690
Jiri Slabybddc7152012-10-18 22:26:42 +0200691 mutex_unlock(&ldata->echo_lock);
692 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000693
694 if (tty->ops->flush_chars)
695 tty->ops->flush_chars(tty);
696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000699 * add_echo_byte - add a byte to the echo buffer
700 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200701 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000702 *
703 * Add a character or operation byte to the echo buffer.
704 *
705 * Should be called under the echo lock to protect the echo buffer.
706 */
707
Jiri Slaby57c94122012-10-18 22:26:43 +0200708static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000709{
710 int new_byte_pos;
711
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200712 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000713 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200714 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000715
716 /*
717 * Since the buffer start position needs to be advanced,
718 * be sure to step by a whole operation byte group.
719 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200720 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
721 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000722 (N_TTY_BUF_SIZE - 1)] ==
723 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200724 ldata->echo_pos += 3;
725 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000726 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200727 ldata->echo_pos += 2;
728 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000729 }
730 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200731 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000732 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200733 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000734
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200735 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000736 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200737 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200739 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000740 }
741
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200742 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000743}
744
745/**
746 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200747 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000748 *
749 * Add an operation to the echo buffer to move back one column.
750 *
751 * Locking: echo_lock to protect the echo buffer
752 */
753
Jiri Slaby57c94122012-10-18 22:26:43 +0200754static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000755{
Jiri Slabybddc7152012-10-18 22:26:42 +0200756 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200757 add_echo_byte(ECHO_OP_START, ldata);
758 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200759 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000760}
761
762/**
763 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200764 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000765 *
766 * Add an operation to the echo buffer to set the canon column
767 * to the current column.
768 *
769 * Locking: echo_lock to protect the echo buffer
770 */
771
Jiri Slaby57c94122012-10-18 22:26:43 +0200772static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000773{
Jiri Slabybddc7152012-10-18 22:26:42 +0200774 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200775 add_echo_byte(ECHO_OP_START, ldata);
776 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200777 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000778}
779
780/**
781 * echo_erase_tab - add operation to erase a tab
782 * @num_chars: number of character columns already used
783 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200784 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000785 *
786 * Add an operation to the echo buffer to erase a tab.
787 *
788 * Called by the eraser function, which knows how many character
789 * columns have been used since either a previous tab or the start
790 * of input. This information will be used later, along with
791 * canon column (if applicable), to go back the correct number
792 * of columns.
793 *
794 * Locking: echo_lock to protect the echo buffer
795 */
796
797static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200798 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000799{
Jiri Slabybddc7152012-10-18 22:26:42 +0200800 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000801
Jiri Slaby57c94122012-10-18 22:26:43 +0200802 add_echo_byte(ECHO_OP_START, ldata);
803 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000804
805 /* We only need to know this modulo 8 (tab spacing) */
806 num_chars &= 7;
807
808 /* Set the high bit as a flag if num_chars is after a previous tab */
809 if (after_tab)
810 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000811
Jiri Slaby57c94122012-10-18 22:26:43 +0200812 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000813
Jiri Slabybddc7152012-10-18 22:26:42 +0200814 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000815}
816
817/**
818 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * @c: unicode byte to echo
820 * @tty: terminal device
821 *
Alan Cox4edf1822008-02-08 04:18:44 -0800822 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100824 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000825 * This variant does not treat control characters specially.
826 *
827 * Locking: echo_lock to protect the echo buffer
828 */
829
Jiri Slaby57c94122012-10-18 22:26:43 +0200830static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000831{
Jiri Slabybddc7152012-10-18 22:26:42 +0200832 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000833 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200834 add_echo_byte(ECHO_OP_START, ldata);
835 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200837 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200839 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000840}
841
842/**
843 * echo_char - echo a character
844 * @c: unicode byte to echo
845 * @tty: terminal device
846 *
847 * Echo user input back onto the screen. This must be called only when
848 * L_ECHO(tty) is true. Called from the driver receive_buf path.
849 *
Joe Peterson62b26352009-09-09 15:03:47 -0600850 * This variant tags control characters to be echoed as "^X"
851 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000852 *
853 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855
856static void echo_char(unsigned char c, struct tty_struct *tty)
857{
Jiri Slabybddc7152012-10-18 22:26:42 +0200858 struct n_tty_data *ldata = tty->disc_data;
859
860 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000861
862 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200863 add_echo_byte(ECHO_OP_START, ldata);
864 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000865 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600866 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200867 add_echo_byte(ECHO_OP_START, ldata);
868 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000869 }
870
Jiri Slabybddc7152012-10-18 22:26:42 +0200871 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Alan Cox17b82062008-10-13 10:45:06 +0100874/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000875 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200876 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100877 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000878
Jiri Slaby57c94122012-10-18 22:26:43 +0200879static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200881 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200882 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200883 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885}
886
887/**
888 * eraser - handle erase function
889 * @c: character input
890 * @tty: terminal device
891 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200892 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * present in the stream from the driver layer. Handles the complexities
894 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100895 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000896 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Alan Cox4edf1822008-02-08 04:18:44 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static void eraser(unsigned char c, struct tty_struct *tty)
900{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200901 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 enum { ERASE, WERASE, KILL } kill_type;
903 int head, seen_alnums, cnt;
904 unsigned long flags;
905
Alan Cox17b82062008-10-13 10:45:06 +0100906 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200907 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000908 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
910 }
911 if (c == ERASE_CHAR(tty))
912 kill_type = ERASE;
913 else if (c == WERASE_CHAR(tty))
914 kill_type = WERASE;
915 else {
916 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100917 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200918 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200920 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return;
923 }
924 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100925 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200926 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200928 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100929 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200930 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 echo_char(KILL_CHAR(tty), tty);
932 /* Add a newline if ECHOK is on and ECHOKE is off. */
933 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200934 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return;
936 }
937 kill_type = KILL;
938 }
939
940 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100941 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200942 while (ldata->read_head != ldata->canon_head) {
943 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* erase a single possibly multibyte character */
946 do {
947 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200948 c = ldata->read_buf[head];
949 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* do not partially erase */
952 if (is_continuation(c, tty))
953 break;
954
955 if (kill_type == WERASE) {
956 /* Equivalent to BSD's ALTWERASE. */
957 if (isalnum(c) || c == '_')
958 seen_alnums++;
959 else if (seen_alnums)
960 break;
961 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200962 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100963 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200964 ldata->read_head = head;
965 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100966 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (L_ECHO(tty)) {
968 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200969 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200970 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200971 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 /* if cnt > 1, output a multi-byte character */
974 echo_char(c, tty);
975 while (--cnt > 0) {
976 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200977 echo_char_raw(ldata->read_buf[head],
978 ldata);
979 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
982 echo_char(ERASE_CHAR(tty), tty);
983 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000984 unsigned int num_chars = 0;
985 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200986 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Joe Petersona88a69c2009-01-02 13:40:53 +0000988 /*
989 * Count the columns used for characters
990 * since the start of input or after a
991 * previous tab.
992 * This info is used to go back the correct
993 * number of columns.
994 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200995 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000996 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200997 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000998 if (c == '\t') {
999 after_tab = 1;
1000 break;
Alan Cox300a6202009-01-02 13:41:04 +00001001 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001003 num_chars += 2;
1004 } else if (!is_continuation(c, tty)) {
1005 num_chars++;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001008 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
1010 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001011 echo_char_raw('\b', ldata);
1012 echo_char_raw(' ', ldata);
1013 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001016 echo_char_raw('\b', ldata);
1017 echo_char_raw(' ', ldata);
1018 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 }
1021 }
1022 if (kill_type == ERASE)
1023 break;
1024 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001025 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001026 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/**
1030 * isig - handle the ISIG optio
1031 * @sig: signal
1032 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001034 * Called when a signal is being sent due to terminal input.
1035 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001036 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001037 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Alan Cox4edf1822008-02-08 04:18:44 -08001039
Peter Hurley8c985d12013-03-06 08:38:19 -05001040static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Peter Hurley8c985d12013-03-06 08:38:19 -05001042 struct pid *tty_pgrp = tty_get_pgrp(tty);
1043 if (tty_pgrp) {
1044 kill_pgrp(tty_pgrp, sig, 1);
1045 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047}
1048
1049/**
1050 * n_tty_receive_break - handle break
1051 * @tty: terminal
1052 *
1053 * An RS232 break event has been hit in the incoming bitstream. This
1054 * can cause a variety of events depending upon the termios settings.
1055 *
1056 * Called from the receive_buf path so single threaded.
1057 */
Alan Cox4edf1822008-02-08 04:18:44 -08001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059static inline void n_tty_receive_break(struct tty_struct *tty)
1060{
Jiri Slaby57c94122012-10-18 22:26:43 +02001061 struct n_tty_data *ldata = tty->disc_data;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (I_IGNBRK(tty))
1064 return;
1065 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001066 isig(SIGINT, tty);
1067 if (!L_NOFLSH(tty)) {
1068 n_tty_flush_buffer(tty);
1069 tty_driver_flush_buffer(tty);
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return;
1072 }
1073 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001074 put_tty_queue('\377', ldata);
1075 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001077 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 wake_up_interruptible(&tty->read_wait);
1079}
1080
1081/**
1082 * n_tty_receive_overrun - handle overrun reporting
1083 * @tty: terminal
1084 *
1085 * Data arrived faster than we could process it. While the tty
1086 * driver has flagged this the bits that were missed are gone
1087 * forever.
1088 *
1089 * Called from the receive_buf path so single threaded. Does not
1090 * need locking as num_overrun and overrun_time are function
1091 * private.
1092 */
Alan Cox4edf1822008-02-08 04:18:44 -08001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094static inline void n_tty_receive_overrun(struct tty_struct *tty)
1095{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001096 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 char buf[64];
1098
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001099 ldata->num_overrun++;
1100 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1101 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1103 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001104 ldata->num_overrun);
1105 ldata->overrun_time = jiffies;
1106 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108}
1109
1110/**
1111 * n_tty_receive_parity_error - error notifier
1112 * @tty: terminal device
1113 * @c: character
1114 *
1115 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001116 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
1118static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1119 unsigned char c)
1120{
Jiri Slaby57c94122012-10-18 22:26:43 +02001121 struct n_tty_data *ldata = tty->disc_data;
1122
Alan Cox4edf1822008-02-08 04:18:44 -08001123 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001126 put_tty_queue('\377', ldata);
1127 put_tty_queue('\0', ldata);
1128 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001130 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001132 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 wake_up_interruptible(&tty->read_wait);
1134}
1135
1136/**
1137 * n_tty_receive_char - perform processing
1138 * @tty: terminal device
1139 * @c: character
1140 *
1141 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001142 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * driver above.
1144 */
1145
1146static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1147{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001148 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001150 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001152 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001153 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return;
1155 }
Alan Cox4edf1822008-02-08 04:18:44 -08001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (I_ISTRIP(tty))
1158 c &= 0x7f;
1159 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001160 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
hyc@symas.com26df6d12010-06-22 10:14:49 -07001162 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001163 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001164 return;
1165 }
1166
Joe Peterson54d2a372008-02-06 01:37:59 -08001167 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001168 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1169 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001170 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001171 process_echoes(tty);
1172 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (tty->closing) {
1175 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001176 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001178 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001179 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 stop_tty(tty);
1181 }
1182 return;
1183 }
1184
1185 /*
1186 * If the previous character was LNEXT, or we know that this
1187 * character is not one of the characters that we'll have to
1188 * handle specially, do shortcut processing to speed things
1189 * up.
1190 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001191 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001192 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001193 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001194 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001195 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001196 if (L_ECHO(tty))
1197 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001198 return;
1199 }
1200 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001201 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001203 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001204 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001206 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001208 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001209 put_tty_queue(c, ldata);
1210 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 }
Alan Cox4edf1822008-02-08 04:18:44 -08001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (I_IXON(tty)) {
1215 if (c == START_CHAR(tty)) {
1216 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001217 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return;
1219 }
1220 if (c == STOP_CHAR(tty)) {
1221 stop_tty(tty);
1222 return;
1223 }
1224 }
Joe Peterson575537b32008-04-30 00:53:30 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (L_ISIG(tty)) {
1227 int signal;
1228 signal = SIGINT;
1229 if (c == INTR_CHAR(tty))
1230 goto send_signal;
1231 signal = SIGQUIT;
1232 if (c == QUIT_CHAR(tty))
1233 goto send_signal;
1234 signal = SIGTSTP;
1235 if (c == SUSP_CHAR(tty)) {
1236send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001237 if (!L_NOFLSH(tty)) {
1238 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001239 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001240 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001241 if (I_IXON(tty))
1242 start_tty(tty);
1243 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001244 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001245 process_echoes(tty);
1246 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001247 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return;
1249 }
1250 }
Joe Peterson575537b32008-04-30 00:53:30 -07001251
1252 if (c == '\r') {
1253 if (I_IGNCR(tty))
1254 return;
1255 if (I_ICRNL(tty))
1256 c = '\n';
1257 } else if (c == '\n' && I_INLCR(tty))
1258 c = '\r';
1259
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001260 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1262 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1263 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001264 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return;
1266 }
1267 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001268 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001270 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001272 echo_char_raw('^', ldata);
1273 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001274 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 }
1277 return;
1278 }
1279 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1280 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001281 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Jiri Slaby57c94122012-10-18 22:26:43 +02001283 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001285 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001286 while (tail != ldata->read_head) {
1287 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1289 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001290 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return;
1292 }
1293 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001294 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001295 if (L_ECHO(tty))
1296 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001297 return;
1298 }
1299 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001300 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001301 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 goto handle_newline;
1304 }
1305 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001306 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001307 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001308 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001309 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 c = __DISABLED_CHAR;
1311 goto handle_newline;
1312 }
1313 if ((c == EOL_CHAR(tty)) ||
1314 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001315 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1316 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001317 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001318 if (L_ECHO(tty))
1319 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001320 return;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /*
1323 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1324 */
1325 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001327 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001328 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001330 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 /*
1333 * XXX does PARMRK doubling happen for
1334 * EOL_CHAR and EOL2_CHAR?
1335 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001336 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001337 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Alan Cox4edf1822008-02-08 04:18:44 -08001339handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001340 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001341 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001342 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001343 ldata->canon_head = ldata->read_head;
1344 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001345 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1347 if (waitqueue_active(&tty->read_wait))
1348 wake_up_interruptible(&tty->read_wait);
1349 return;
1350 }
1351 }
Alan Cox4edf1822008-02-08 04:18:44 -08001352
Joe Petersonacc71bb2009-01-02 13:43:32 +00001353 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001354 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001355 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001356 if (L_ECHO(tty))
1357 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001358 return;
1359 }
1360 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001361 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001363 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 else {
1365 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001366 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001367 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 echo_char(c, tty);
1369 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001370 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Joe Petersonacc71bb2009-01-02 13:43:32 +00001373 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001374 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jiri Slaby57c94122012-10-18 22:26:43 +02001376 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380/**
1381 * n_tty_write_wakeup - asynchronous I/O notifier
1382 * @tty: tty device
1383 *
1384 * Required for the ptys, serial driver etc. since processes
1385 * that attach themselves to the master and rely on ASYNC
1386 * IO must be woken up
1387 */
1388
1389static void n_tty_write_wakeup(struct tty_struct *tty)
1390{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001391 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395/**
1396 * n_tty_receive_buf - data receive
1397 * @tty: terminal device
1398 * @cp: buffer
1399 * @fp: flag buffer
1400 * @count: characters
1401 *
1402 * Called by the terminal driver when a block of characters has
1403 * been received. This function must be called from soft contexts
1404 * not from interrupt context. The driver is responsible for making
1405 * calls one at a time and in order (or using flush_to_ldisc)
1406 */
Alan Cox4edf1822008-02-08 04:18:44 -08001407
Peter Hurley24a89d12013-06-15 09:14:15 -04001408static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1409 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001411 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 const unsigned char *p;
1413 char *f, flags = TTY_NORMAL;
1414 int i;
1415 char buf[64];
1416 unsigned long cpuflags;
1417
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001418 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001419 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001420 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1421 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001423 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1424 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1425 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 cp += i;
1427 count -= i;
1428
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001429 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1430 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001432 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1433 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1434 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001435 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001437 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (f)
1439 flags = *f++;
1440 switch (flags) {
1441 case TTY_NORMAL:
1442 n_tty_receive_char(tty, *p);
1443 break;
1444 case TTY_BREAK:
1445 n_tty_receive_break(tty);
1446 break;
1447 case TTY_PARITY:
1448 case TTY_FRAME:
1449 n_tty_receive_parity_error(tty, *p);
1450 break;
1451 case TTY_OVERRUN:
1452 n_tty_receive_overrun(tty);
1453 break;
1454 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001455 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 tty_name(tty, buf), flags);
1457 break;
1458 }
1459 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001460 if (tty->ops->flush_chars)
1461 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001464 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001465 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1467 if (waitqueue_active(&tty->read_wait))
1468 wake_up_interruptible(&tty->read_wait);
1469 }
1470
1471 /*
1472 * Check the remaining room for the input canonicalization
1473 * mode. We don't want to throttle the driver if we're in
1474 * canonical mode and don't have a newline yet!
1475 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001476 while (1) {
1477 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
Peter Hurley24a89d12013-06-15 09:14:15 -04001478 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
Peter Hurleye91e52e2013-03-06 08:20:53 -05001479 break;
1480 if (!tty_throttle_safe(tty))
1481 break;
1482 }
1483 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Peter Hurley24a89d12013-06-15 09:14:15 -04001486static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1487 char *fp, int count)
1488{
1489 __receive_buf(tty, cp, fp, count);
1490}
1491
1492static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1493 char *fp, int count)
1494{
1495 struct n_tty_data *ldata = tty->disc_data;
1496 int room;
1497
1498 tty->receive_room = room = receive_room(tty);
1499 if (!room)
1500 ldata->no_room = 1;
1501 count = min(count, room);
1502 if (count)
1503 __receive_buf(tty, cp, fp, count);
1504
1505 return count;
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508int is_ignored(int sig)
1509{
1510 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001511 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514/**
1515 * n_tty_set_termios - termios data changed
1516 * @tty: terminal
1517 * @old: previous data
1518 *
1519 * Called by the tty layer when the user changes termios flags so
1520 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001521 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * guaranteed that this function will not be re-entered or in progress
1523 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001524 *
1525 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Alan Cox4edf1822008-02-08 04:18:44 -08001527
1528static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001530 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001531 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001532
1533 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001534 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001535 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001536 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001537 ldata->canon_head = ldata->read_tail;
1538 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001539 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001540 }
1541
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001542 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001543 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001544
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001545 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1548 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1549 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1550 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001551 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001554 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001556 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1560 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1561 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1562 set_bit('\n', ldata->process_char_map);
1563 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (L_IEXTEN(tty)) {
1565 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001566 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001568 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001570 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (L_ECHO(tty))
1572 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001573 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 }
1576 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001577 set_bit(START_CHAR(tty), ldata->process_char_map);
1578 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001581 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1582 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1583 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001585 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001586 ldata->raw = 0;
1587 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001589 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1591 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1592 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001593 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001595 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001597 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001598 /*
1599 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1600 * been stopped by STOP_CHAR(tty) before it.
1601 */
1602 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1603 start_tty(tty);
1604 }
1605
Alan Coxf34d7a52008-04-30 00:54:13 -07001606 /* The termios change make the tty ready for I/O */
1607 wake_up_interruptible(&tty->write_wait);
1608 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/**
1612 * n_tty_close - close the ldisc for this tty
1613 * @tty: device
1614 *
Alan Cox4edf1822008-02-08 04:18:44 -08001615 * Called from the terminal layer when this line discipline is
1616 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * discipline change. The function will not be called while other
1618 * ldisc methods are in progress.
1619 */
Alan Cox4edf1822008-02-08 04:18:44 -08001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static void n_tty_close(struct tty_struct *tty)
1622{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001623 struct n_tty_data *ldata = tty->disc_data;
1624
Peter Hurley79901312013-03-11 16:44:23 -04001625 if (tty->link)
1626 n_tty_packet_mode_flush(tty);
1627
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001628 kfree(ldata->read_buf);
1629 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001630 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001631 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634/**
1635 * n_tty_open - open an ldisc
1636 * @tty: terminal to open
1637 *
Alan Cox4edf1822008-02-08 04:18:44 -08001638 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * terminal device. Can sleep. Called serialized so that no
1640 * other events will occur in parallel. No further open will occur
1641 * until a close.
1642 */
1643
1644static int n_tty_open(struct tty_struct *tty)
1645{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001646 struct n_tty_data *ldata;
1647
1648 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1649 if (!ldata)
1650 goto err;
1651
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001652 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001653 mutex_init(&ldata->atomic_read_lock);
1654 mutex_init(&ldata->output_lock);
1655 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001656 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001657
Joe Petersona88a69c2009-01-02 13:40:53 +00001658 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001659 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1660 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1661 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001662 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001663
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001664 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001665 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001666 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001667 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001669 /* indicate buffer work may resume */
1670 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1671 n_tty_set_termios(tty, NULL);
1672 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001675err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001676 kfree(ldata->read_buf);
1677 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001678 kfree(ldata);
1679err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683static inline int input_available_p(struct tty_struct *tty, int amt)
1684{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001685 struct n_tty_data *ldata = tty->disc_data;
1686
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001687 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001688 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001689 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001691 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 1;
1693
1694 return 0;
1695}
1696
1697/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001698 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * @tty: terminal device
1700 * @b: user data
1701 * @nr: size of data
1702 *
Alan Cox11a96d12008-10-13 10:46:24 +01001703 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * ICANON is off; it copies characters straight from the tty queue to
1705 * user space directly. It can be profitably called twice; once to
1706 * drain the space from the tail pointer to the (physical) end of the
1707 * buffer, and once to drain the space from the (physical) beginning of
1708 * the buffer to head pointer.
1709 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001710 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
1712 */
Alan Cox4edf1822008-02-08 04:18:44 -08001713
Alan Cox33f0f882006-01-09 20:54:13 -08001714static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned char __user **b,
1716 size_t *nr)
1717
1718{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001719 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 int retval;
1721 size_t n;
1722 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001723 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001726 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001727 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001729 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001731 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001733 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001734 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1735 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001736 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001737 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001738 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1739 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001740 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001741 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001742 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001743 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 *b += n;
1745 *nr -= n;
1746 }
1747 return retval;
1748}
1749
Al Virocc4191d2008-03-29 03:08:48 +00001750extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001751 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753/**
1754 * job_control - check job control
1755 * @tty: tty
1756 * @file: file handle
1757 *
1758 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001759 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001761 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001762 * Locking: redirected write test is safe
1763 * current->signal->tty check is safe
1764 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
Alan Cox4edf1822008-02-08 04:18:44 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767static int job_control(struct tty_struct *tty, struct file *file)
1768{
1769 /* Job control check -- must be done at start and after
1770 every sleep (POSIX.1 7.1.1.4). */
1771 /* NOTE: not yet done after every sleep pending a thorough
1772 check of the logic of this change. -- jlc */
1773 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001774 if (file->f_op->write == redirected_tty_write ||
1775 current->signal->tty != tty)
1776 return 0;
1777
1778 spin_lock_irq(&tty->ctrl_lock);
1779 if (!tty->pgrp)
1780 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1781 else if (task_pgrp(current) != tty->pgrp) {
1782 spin_unlock_irq(&tty->ctrl_lock);
1783 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1784 return -EIO;
1785 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1786 set_thread_flag(TIF_SIGPENDING);
1787 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001789 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791}
Alan Cox4edf1822008-02-08 04:18:44 -08001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794/**
Alan Cox11a96d12008-10-13 10:46:24 +01001795 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * @tty: tty device
1797 * @file: file object
1798 * @buf: userspace buffer pointer
1799 * @nr: size of I/O
1800 *
1801 * Perform reads for the line discipline. We are guaranteed that the
1802 * line discipline will not be closed under us but we may get multiple
1803 * parallel readers and must handle this ourselves. We may also get
1804 * a hangup. Always called in user context, may sleep.
1805 *
1806 * This code must be sure never to sleep through a hangup.
1807 */
Alan Cox4edf1822008-02-08 04:18:44 -08001808
Alan Cox11a96d12008-10-13 10:46:24 +01001809static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 unsigned char __user *buf, size_t nr)
1811{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001812 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 unsigned char __user *b = buf;
1814 DECLARE_WAITQUEUE(wait, current);
1815 int c;
1816 int minimum, time;
1817 ssize_t retval = 0;
1818 ssize_t size;
1819 long timeout;
1820 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001821 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001825 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 minimum = time = 0;
1829 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001830 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 minimum = MIN_CHAR(tty);
1832 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04001833 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001835 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001837 (ldata->minimum_to_wake > minimum))
1838 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04001840 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001841 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 }
1844
1845 /*
1846 * Internal serialization of reads.
1847 */
1848 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001849 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001851 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001852 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -ERESTARTSYS;
1854 }
Alan Cox04f378b2008-04-30 00:53:29 -07001855 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 while (nr) {
1859 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001860 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 unsigned char cs;
1862 if (b != buf)
1863 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001864 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 cs = tty->link->ctrl_status;
1866 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001867 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001868 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 retval = -EFAULT;
1870 b--;
1871 break;
1872 }
1873 nr--;
1874 break;
1875 }
1876 /* This statement must be first before checking for input
1877 so that any interrupt will set the state back to
1878 TASK_RUNNING. */
1879 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001880
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001881 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001883 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (!input_available_p(tty, 0)) {
1886 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1887 retval = -EIO;
1888 break;
1889 }
1890 if (tty_hung_up_p(file))
1891 break;
1892 if (!timeout)
1893 break;
1894 if (file->f_flags & O_NONBLOCK) {
1895 retval = -EAGAIN;
1896 break;
1897 }
1898 if (signal_pending(current)) {
1899 retval = -ERESTARTSYS;
1900 break;
1901 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001902 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 continue;
1905 }
1906 __set_current_state(TASK_RUNNING);
1907
1908 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001909 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001910 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 retval = -EFAULT;
1912 b--;
1913 break;
1914 }
1915 nr--;
1916 }
1917
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001918 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001921 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001922 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001924 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001925 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001926 c = ldata->read_buf[ldata->read_tail];
1927 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001929 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (eol) {
1931 /* this test should be redundant:
1932 * we shouldn't be reading data if
1933 * canon_data is 0
1934 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001935 if (--ldata->canon_data < 0)
1936 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Ivo Sieben98001212013-01-28 13:32:01 +01001938 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001941 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 retval = -EFAULT;
1943 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001944 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
1946 }
1947 nr--;
1948 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001949 if (eol) {
1950 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001951 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001953 }
Ivo Sieben98001212013-01-28 13:32:01 +01001954 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Ivo Sieben98001212013-01-28 13:32:01 +01001956 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (retval)
1958 break;
1959 } else {
1960 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001961 /* The copy function takes the read lock and handles
1962 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 uncopied = copy_from_read_buf(tty, &b, &nr);
1964 uncopied += copy_from_read_buf(tty, &b, &nr);
1965 if (uncopied) {
1966 retval = -EFAULT;
1967 break;
1968 }
1969 }
1970
1971 /* If there is enough space in the read buffer now, let the
1972 * low-level driver know. We use n_tty_chars_in_buffer() to
1973 * check the buffer, as it now knows about canonical mode.
1974 * Otherwise, if the driver is throttled and the line is
1975 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1976 * we won't get any more characters.
1977 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001978 while (1) {
1979 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1980 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1981 break;
1982 if (!tty->count)
1983 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001984 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001985 if (!tty_unthrottle_safe(tty))
1986 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001987 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001988 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (b - buf >= minimum)
1991 break;
1992 if (time)
1993 timeout = time;
1994 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001995 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 remove_wait_queue(&tty->read_wait, &wait);
1997
1998 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001999 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 __set_current_state(TASK_RUNNING);
2002 size = b - buf;
2003 if (size) {
2004 retval = size;
2005 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08002006 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01002008 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Linus Torvalds55db4c62011-06-04 06:33:24 +09002010 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return retval;
2012}
2013
2014/**
Alan Cox11a96d12008-10-13 10:46:24 +01002015 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * @tty: tty device
2017 * @file: file object
2018 * @buf: userspace buffer pointer
2019 * @nr: size of I/O
2020 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002021 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002023 * and other such events. Since the receive code will echo characters,
2024 * thus calling driver write methods, the output_lock is used in
2025 * the output processing functions called here as well as in the
2026 * echo processing function to protect the column state and space
2027 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 *
2029 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002030 *
2031 * Locking: output_lock to protect column state and space left
2032 * (note that the process_output*() functions take this
2033 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Alan Cox4edf1822008-02-08 04:18:44 -08002035
Alan Cox11a96d12008-10-13 10:46:24 +01002036static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002037 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 const unsigned char *b = buf;
2040 DECLARE_WAITQUEUE(wait, current);
2041 int c;
2042 ssize_t retval = 0;
2043
2044 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2045 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2046 retval = tty_check_change(tty);
2047 if (retval)
2048 return retval;
2049 }
2050
Joe Petersona88a69c2009-01-02 13:40:53 +00002051 /* Write out any echoed characters that are still pending */
2052 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 add_wait_queue(&tty->write_wait, &wait);
2055 while (1) {
2056 set_current_state(TASK_INTERRUPTIBLE);
2057 if (signal_pending(current)) {
2058 retval = -ERESTARTSYS;
2059 break;
2060 }
2061 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2062 retval = -EIO;
2063 break;
2064 }
Peter Hurley582f5592013-05-17 12:49:48 -04002065 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002067 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (num < 0) {
2069 if (num == -EAGAIN)
2070 break;
2071 retval = num;
2072 goto break_out;
2073 }
2074 b += num;
2075 nr -= num;
2076 if (nr == 0)
2077 break;
2078 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002079 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 break;
2081 b++; nr--;
2082 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002083 if (tty->ops->flush_chars)
2084 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002086 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002087 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002088 if (c < 0) {
2089 retval = c;
2090 goto break_out;
2091 }
2092 if (!c)
2093 break;
2094 b += c;
2095 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 if (!nr)
2099 break;
2100 if (file->f_flags & O_NONBLOCK) {
2101 retval = -EAGAIN;
2102 break;
2103 }
2104 schedule();
2105 }
2106break_out:
2107 __set_current_state(TASK_RUNNING);
2108 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002109 if (b - buf != nr && tty->fasync)
2110 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return (b - buf) ? b - buf : retval;
2112}
2113
2114/**
Alan Cox11a96d12008-10-13 10:46:24 +01002115 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * @tty: terminal device
2117 * @file: file accessing it
2118 * @wait: poll table
2119 *
2120 * Called when the line discipline is asked to poll() for data or
2121 * for special events. This code is not serialized with respect to
2122 * other events save open/close.
2123 *
2124 * This code must be sure never to sleep through a hangup.
2125 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
Alan Cox4edf1822008-02-08 04:18:44 -08002127
Alan Cox11a96d12008-10-13 10:46:24 +01002128static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002129 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002131 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 unsigned int mask = 0;
2133
2134 poll_wait(file, &tty->read_wait, wait);
2135 poll_wait(file, &tty->write_wait, wait);
2136 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2137 mask |= POLLIN | POLLRDNORM;
2138 if (tty->packet && tty->link->ctrl_status)
2139 mask |= POLLPRI | POLLIN | POLLRDNORM;
2140 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2141 mask |= POLLHUP;
2142 if (tty_hung_up_p(file))
2143 mask |= POLLHUP;
2144 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2145 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002146 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002148 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002150 if (tty->ops->write && !tty_is_writelocked(tty) &&
2151 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2152 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 mask |= POLLOUT | POLLWRNORM;
2154 return mask;
2155}
2156
Jiri Slaby57c94122012-10-18 22:26:43 +02002157static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002158{
2159 int nr, head, tail;
2160
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002161 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002162 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002163 head = ldata->canon_head;
2164 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002165 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2166 /* Skip EOF-chars.. */
2167 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002168 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002169 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002170 nr--;
2171 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2172 }
2173 return nr;
2174}
2175
2176static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2177 unsigned int cmd, unsigned long arg)
2178{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002179 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002180 int retval;
2181
2182 switch (cmd) {
2183 case TIOCOUTQ:
2184 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2185 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002186 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002187 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002188 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002189 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002190 return put_user(retval, (unsigned int __user *) arg);
2191 default:
2192 return n_tty_ioctl_helper(tty, file, cmd, arg);
2193 }
2194}
2195
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002196static void n_tty_fasync(struct tty_struct *tty, int on)
2197{
2198 struct n_tty_data *ldata = tty->disc_data;
2199
2200 if (!waitqueue_active(&tty->read_wait)) {
2201 if (on)
2202 ldata->minimum_to_wake = 1;
2203 else if (!tty->fasync)
2204 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2205 }
2206}
2207
Alan Coxa352def2008-07-16 21:53:12 +01002208struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002209 .magic = TTY_LDISC_MAGIC,
2210 .name = "n_tty",
2211 .open = n_tty_open,
2212 .close = n_tty_close,
2213 .flush_buffer = n_tty_flush_buffer,
2214 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002215 .read = n_tty_read,
2216 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002217 .ioctl = n_tty_ioctl,
2218 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002219 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002220 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002221 .write_wakeup = n_tty_write_wakeup,
2222 .fasync = n_tty_fasync,
Peter Hurley24a89d12013-06-15 09:14:15 -04002223 .receive_buf2 = n_tty_receive_buf2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002225
2226/**
2227 * n_tty_inherit_ops - inherit N_TTY methods
2228 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2229 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002230 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002231 * methods.
2232 */
2233
2234void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2235{
2236 *ops = tty_ldisc_N_TTY;
2237 ops->owner = NULL;
2238 ops->refcount = ops->flags = 0;
2239}
2240EXPORT_SYMBOL_GPL(n_tty_inherit_ops);