blob: 064616542cb5a9f8a5851960dc419dab98b6af59 [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
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020084
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020087
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -040092 int minimum_to_wake;
Jiri Slabyba2e68a2012-10-18 22:26:41 +020093
94 unsigned char *echo_buf;
95 unsigned int echo_pos;
96 unsigned int echo_cnt;
97
98 int canon_data;
99 unsigned long canon_head;
100 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200101
102 struct mutex atomic_read_lock;
103 struct mutex output_lock;
104 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100105 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200106};
107
Miloslav Trmac522ed772007-07-15 23:40:56 -0700108static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
109 unsigned char __user *ptr)
110{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200111 struct n_tty_data *ldata = tty->disc_data;
112
113 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700114 return put_user(x, ptr);
115}
116
Linus Torvalds55db4c62011-06-04 06:33:24 +0900117/**
Peter Hurleyb8483052013-06-15 07:28:30 -0400118 * n_tty_set_room - receive space
Linus Torvalds55db4c62011-06-04 06:33:24 +0900119 * @tty: terminal
120 *
Peter Hurleyb8483052013-06-15 07:28:30 -0400121 * Sets tty->receive_room to reflect the currently available space
122 * in the input buffer, and re-schedules the flip buffer work if space
123 * just became available.
124 *
125 * Locks: Concurrent update is protected with read_lock
Linus Torvalds55db4c62011-06-04 06:33:24 +0900126 */
127
128static void n_tty_set_room(struct tty_struct *tty)
129{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200130 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500131 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900132 int old_left;
Peter Hurleyb8483052013-06-15 07:28:30 -0400133 unsigned long flags;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900134
Peter Hurleyb8483052013-06-15 07:28:30 -0400135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
136
Jaeden Amero090abf72012-07-27 08:43:11 -0500137 if (I_PARMRK(tty)) {
138 /* Multiply read_cnt by 3, since each byte might take up to
139 * three times as many spaces when PARMRK is set (depending on
140 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200141 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500142 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200143 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500144
Linus Torvalds55db4c62011-06-04 06:33:24 +0900145 /*
146 * If we are doing input canonicalization, and there are no
147 * pending newlines, let characters through without limit, so
148 * that erase characters will be handled. Other excess
149 * characters will be beeped.
150 */
151 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200152 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900153 old_left = tty->receive_room;
154 tty->receive_room = left;
155
Peter Hurleyb8483052013-06-15 07:28:30 -0400156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
157
Linus Torvalds55db4c62011-06-04 06:33:24 +0900158 /* Did this open up the receive buffer? We may need to flip */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200159 if (left && !old_left) {
160 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400161 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400162 /* see if ldisc has been killed - if so, this means that
163 * even though the ldisc has been halted and ->buf.work
164 * cancelled, ->buf.work is about to be rescheduled
165 */
166 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
167 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200168 schedule_work(&tty->port->buf.work);
169 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900170}
171
Jiri Slaby57c94122012-10-18 22:26:43 +0200172static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200174 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
175 ldata->read_buf[ldata->read_head] = c;
176 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
177 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179}
180
Alan Cox17b82062008-10-13 10:45:06 +0100181/**
182 * put_tty_queue - add character to tty
183 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200184 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100185 *
186 * Add a character to the tty read_buf queue. This is done under the
187 * read_lock to serialize character addition and also to protect us
188 * against parallel reads or flushes
189 */
190
Jiri Slaby57c94122012-10-18 22:26:43 +0200191static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 unsigned long flags;
194 /*
195 * The problem of stomping on the buffers ends here.
196 * Why didn't anyone see this one coming? --AJK
197 */
Ivo Sieben98001212013-01-28 13:32:01 +0100198 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200199 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100200 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * reset_buffer_flags - reset buffer state
205 * @tty: terminal to reset
206 *
Peter Hurley25518c62013-03-11 16:44:31 -0400207 * Reset the read buffer counters and clear the flags.
208 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100209 *
210 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000212
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400213static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned long flags;
216
Ivo Sieben98001212013-01-28 13:32:01 +0100217 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200218 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100219 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000220
Jiri Slabybddc7152012-10-18 22:26:42 +0200221 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200222 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200223 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000224
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200225 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200226 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Peter Hurleya30737a2013-03-11 16:44:22 -0400229static void n_tty_packet_mode_flush(struct tty_struct *tty)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(&tty->ctrl_lock, flags);
234 if (tty->link->packet) {
235 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
236 wake_up_interruptible(&tty->link->read_wait);
237 }
238 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/**
242 * n_tty_flush_buffer - clean input queue
243 * @tty: terminal device
244 *
Peter Hurley25518c62013-03-11 16:44:31 -0400245 * Flush the input buffer. Called when the tty layer wants the
246 * buffer flushed (eg at hangup) or when the N_TTY line discipline
247 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *
Alan Cox17b82062008-10-13 10:45:06 +0100249 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Alan Cox4edf1822008-02-08 04:18:44 -0800251
252static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400254 reset_buffer_flags(tty->disc_data);
255 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800256
Peter Hurleya30737a2013-03-11 16:44:22 -0400257 if (tty->link)
258 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/**
262 * n_tty_chars_in_buffer - report available bytes
263 * @tty: tty device
264 *
265 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800266 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100267 *
268 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Alan Cox4edf1822008-02-08 04:18:44 -0800270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
272{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200273 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned long flags;
275 ssize_t n = 0;
276
Ivo Sieben98001212013-01-28 13:32:01 +0100277 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200278 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200279 n = ldata->read_cnt;
280 } else if (ldata->canon_data) {
281 n = (ldata->canon_head > ldata->read_tail) ?
282 ldata->canon_head - ldata->read_tail :
283 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Ivo Sieben98001212013-01-28 13:32:01 +0100285 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return n;
287}
288
289/**
290 * is_utf8_continuation - utf8 multibyte check
291 * @c: byte to check
292 *
293 * Returns true if the utf8 character 'c' is a multibyte continuation
294 * character. We use this to correctly compute the on screen size
295 * of the character when printing
296 */
Alan Cox4edf1822008-02-08 04:18:44 -0800297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static inline int is_utf8_continuation(unsigned char c)
299{
300 return (c & 0xc0) == 0x80;
301}
302
303/**
304 * is_continuation - multibyte check
305 * @c: byte to check
306 *
307 * Returns true if the utf8 character 'c' is a multibyte continuation
308 * character and the terminal is in unicode mode.
309 */
Alan Cox4edf1822008-02-08 04:18:44 -0800310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static inline int is_continuation(unsigned char c, struct tty_struct *tty)
312{
313 return I_IUTF8(tty) && is_utf8_continuation(c);
314}
315
316/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000317 * do_output_char - output one character
318 * @c: character (or partial unicode symbol)
319 * @tty: terminal device
320 * @space: space available in tty driver write buffer
321 *
322 * This is a helper function that handles one output character
323 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600324 * doing OPOST processing and putting the results in the
325 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000326 *
327 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
328 * and NLDLY. They simply aren't relevant in the world today.
329 * If you ever need them, add them here.
330 *
331 * Returns the number of bytes of buffer space used or -1 if
332 * no space left.
333 *
334 * Locking: should be called under the output_lock to protect
335 * the column state and space left in the buffer
336 */
337
338static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
339{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200340 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000341 int spaces;
342
343 if (!space)
344 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000345
Joe Petersona88a69c2009-01-02 13:40:53 +0000346 switch (c) {
347 case '\n':
348 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200349 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000350 if (O_ONLCR(tty)) {
351 if (space < 2)
352 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200353 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700354 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000355 return 2;
356 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200357 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000358 break;
359 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200360 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000361 return 0;
362 if (O_OCRNL(tty)) {
363 c = '\n';
364 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200365 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000366 break;
367 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200368 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000369 break;
370 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200371 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000372 if (O_TABDLY(tty) == XTABS) {
373 if (space < spaces)
374 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200375 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000376 tty->ops->write(tty, " ", spaces);
377 return spaces;
378 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200379 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000380 break;
381 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200382 if (ldata->column > 0)
383 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000384 break;
385 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000386 if (!iscntrl(c)) {
387 if (O_OLCUC(tty))
388 c = toupper(c);
389 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200390 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000391 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000392 break;
393 }
394
395 tty_put_char(tty, c);
396 return 1;
397}
398
399/**
400 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * @c: character (or partial unicode symbol)
402 * @tty: terminal device
403 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600404 * Output one character with OPOST processing.
405 * Returns -1 when the output device is full and the character
406 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000408 * Locking: output_lock to protect column state and space left
409 * (also, this is called from n_tty_write under the
410 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
Alan Cox4edf1822008-02-08 04:18:44 -0800412
Joe Petersona88a69c2009-01-02 13:40:53 +0000413static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Jiri Slabybddc7152012-10-18 22:26:42 +0200415 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000416 int space, retval;
417
Jiri Slabybddc7152012-10-18 22:26:42 +0200418 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Alan Coxf34d7a52008-04-30 00:54:13 -0700420 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000421 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jiri Slabybddc7152012-10-18 22:26:42 +0200423 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000424 if (retval < 0)
425 return -1;
426 else
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000431 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600433 * @buf: character buffer
434 * @nr: number of bytes to output
435 *
436 * Output a block of characters with OPOST processing.
437 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
439 * This path is used to speed up block console writes, among other
440 * things when processing blocks of output data. It handles only
441 * the simple cases normally found and helps to generate blocks of
442 * symbols for the console driver and thus improve performance.
443 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000444 * Locking: output_lock to protect column state and space left
445 * (also, this is called from n_tty_write under the
446 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Alan Cox4edf1822008-02-08 04:18:44 -0800448
Joe Petersona88a69c2009-01-02 13:40:53 +0000449static ssize_t process_output_block(struct tty_struct *tty,
450 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200452 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100454 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 const unsigned char *cp;
456
Jiri Slabybddc7152012-10-18 22:26:42 +0200457 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000458
Alan Coxf34d7a52008-04-30 00:54:13 -0700459 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000460 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200461 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (nr > space)
465 nr = space;
466
467 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000468 unsigned char c = *cp;
469
470 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 case '\n':
472 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200473 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (O_ONLCR(tty))
475 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200476 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200479 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto break_out;
481 if (O_OCRNL(tty))
482 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200483 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case '\t':
486 goto break_out;
487 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200488 if (ldata->column > 0)
489 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000492 if (!iscntrl(c)) {
493 if (O_OLCUC(tty))
494 goto break_out;
495 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200496 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 }
500 }
501break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700502 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000503
Jiri Slabybddc7152012-10-18 22:26:42 +0200504 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return i;
506}
507
Joe Petersona88a69c2009-01-02 13:40:53 +0000508/**
509 * process_echoes - write pending echo characters
510 * @tty: terminal device
511 *
512 * Write previously buffered echo (and other ldisc-generated)
513 * characters to the tty.
514 *
515 * Characters generated by the ldisc (including echoes) need to
516 * be buffered because the driver's write buffer can fill during
517 * heavy program output. Echoing straight to the driver will
518 * often fail under these conditions, causing lost characters and
519 * resulting mismatches of ldisc state information.
520 *
521 * Since the ldisc state must represent the characters actually sent
522 * to the driver at the time of the write, operations like certain
523 * changes in column state are also saved in the buffer and executed
524 * here.
525 *
526 * A circular fifo buffer is used so that the most recent characters
527 * are prioritized. Also, when control characters are echoed with a
528 * prefixed "^", the pair is treated atomically and thus not separated.
529 *
530 * Locking: output_lock to protect column state and space left,
531 * echo_lock to protect the echo buffer
532 */
533
534static void process_echoes(struct tty_struct *tty)
535{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200536 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000537 int space, nr;
538 unsigned char c;
539 unsigned char *cp, *buf_end;
540
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200541 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000542 return;
543
Jiri Slabybddc7152012-10-18 22:26:42 +0200544 mutex_lock(&ldata->output_lock);
545 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000546
547 space = tty_write_room(tty);
548
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200549 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
550 cp = ldata->echo_buf + ldata->echo_pos;
551 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000552 while (nr > 0) {
553 c = *cp;
554 if (c == ECHO_OP_START) {
555 unsigned char op;
556 unsigned char *opp;
557 int no_space_left = 0;
558
559 /*
560 * If the buffer byte is the start of a multi-byte
561 * operation, get the next byte, which is either the
562 * op code or a control character value.
563 */
564 opp = cp + 1;
565 if (opp == buf_end)
566 opp -= N_TTY_BUF_SIZE;
567 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000568
Joe Petersona88a69c2009-01-02 13:40:53 +0000569 switch (op) {
570 unsigned int num_chars, num_bs;
571
572 case ECHO_OP_ERASE_TAB:
573 if (++opp == buf_end)
574 opp -= N_TTY_BUF_SIZE;
575 num_chars = *opp;
576
577 /*
578 * Determine how many columns to go back
579 * in order to erase the tab.
580 * This depends on the number of columns
581 * used by other characters within the tab
582 * area. If this (modulo 8) count is from
583 * the start of input rather than from a
584 * previous tab, we offset by canon column.
585 * Otherwise, tab spacing is normal.
586 */
587 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200588 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000589 num_bs = 8 - (num_chars & 7);
590
591 if (num_bs > space) {
592 no_space_left = 1;
593 break;
594 }
595 space -= num_bs;
596 while (num_bs--) {
597 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200598 if (ldata->column > 0)
599 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000600 }
601 cp += 3;
602 nr -= 3;
603 break;
604
605 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200606 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000607 cp += 2;
608 nr -= 2;
609 break;
610
611 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200612 if (ldata->column > 0)
613 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000614 cp += 2;
615 nr -= 2;
616 break;
617
618 case ECHO_OP_START:
619 /* This is an escaped echo op start code */
620 if (!space) {
621 no_space_left = 1;
622 break;
623 }
624 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200625 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000626 space--;
627 cp += 2;
628 nr -= 2;
629 break;
630
631 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000632 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600633 * If the op is not a special byte code,
634 * it is a ctrl char tagged to be echoed
635 * as "^X" (where X is the letter
636 * representing the control char).
637 * Note that we must ensure there is
638 * enough space for the whole ctrl pair.
639 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000640 */
Joe Peterson62b26352009-09-09 15:03:47 -0600641 if (space < 2) {
642 no_space_left = 1;
643 break;
644 }
645 tty_put_char(tty, '^');
646 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200647 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600648 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000649 cp += 2;
650 nr -= 2;
651 }
652
653 if (no_space_left)
654 break;
655 } else {
Peter Hurley582f5592013-05-17 12:49:48 -0400656 if (O_OPOST(tty)) {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600657 int retval = do_output_char(c, tty, space);
658 if (retval < 0)
659 break;
660 space -= retval;
661 } else {
662 if (!space)
663 break;
664 tty_put_char(tty, c);
665 space -= 1;
666 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000667 cp += 1;
668 nr -= 1;
669 }
670
671 /* When end of circular buffer reached, wrap around */
672 if (cp >= buf_end)
673 cp -= N_TTY_BUF_SIZE;
674 }
675
676 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200677 ldata->echo_pos = 0;
678 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200679 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200681 int num_processed = ldata->echo_cnt - nr;
682 ldata->echo_pos += num_processed;
683 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
684 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000685 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200686 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 }
688
Jiri Slabybddc7152012-10-18 22:26:42 +0200689 mutex_unlock(&ldata->echo_lock);
690 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000691
692 if (tty->ops->flush_chars)
693 tty->ops->flush_chars(tty);
694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000697 * add_echo_byte - add a byte to the echo buffer
698 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200699 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 *
701 * Add a character or operation byte to the echo buffer.
702 *
703 * Should be called under the echo lock to protect the echo buffer.
704 */
705
Jiri Slaby57c94122012-10-18 22:26:43 +0200706static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000707{
708 int new_byte_pos;
709
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200710 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200712 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000713
714 /*
715 * Since the buffer start position needs to be advanced,
716 * be sure to step by a whole operation byte group.
717 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200718 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
719 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 (N_TTY_BUF_SIZE - 1)] ==
721 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200722 ldata->echo_pos += 3;
723 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200725 ldata->echo_pos += 2;
726 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 }
728 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200729 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000730 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200731 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000732
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200733 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000734 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200735 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000736 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200737 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 }
739
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200740 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000741}
742
743/**
744 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200745 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000746 *
747 * Add an operation to the echo buffer to move back one column.
748 *
749 * Locking: echo_lock to protect the echo buffer
750 */
751
Jiri Slaby57c94122012-10-18 22:26:43 +0200752static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000753{
Jiri Slabybddc7152012-10-18 22:26:42 +0200754 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200755 add_echo_byte(ECHO_OP_START, ldata);
756 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200757 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000758}
759
760/**
761 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200762 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000763 *
764 * Add an operation to the echo buffer to set the canon column
765 * to the current column.
766 *
767 * Locking: echo_lock to protect the echo buffer
768 */
769
Jiri Slaby57c94122012-10-18 22:26:43 +0200770static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000771{
Jiri Slabybddc7152012-10-18 22:26:42 +0200772 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200773 add_echo_byte(ECHO_OP_START, ldata);
774 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200775 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000776}
777
778/**
779 * echo_erase_tab - add operation to erase a tab
780 * @num_chars: number of character columns already used
781 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200782 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000783 *
784 * Add an operation to the echo buffer to erase a tab.
785 *
786 * Called by the eraser function, which knows how many character
787 * columns have been used since either a previous tab or the start
788 * of input. This information will be used later, along with
789 * canon column (if applicable), to go back the correct number
790 * of columns.
791 *
792 * Locking: echo_lock to protect the echo buffer
793 */
794
795static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200796 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000797{
Jiri Slabybddc7152012-10-18 22:26:42 +0200798 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000799
Jiri Slaby57c94122012-10-18 22:26:43 +0200800 add_echo_byte(ECHO_OP_START, ldata);
801 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000802
803 /* We only need to know this modulo 8 (tab spacing) */
804 num_chars &= 7;
805
806 /* Set the high bit as a flag if num_chars is after a previous tab */
807 if (after_tab)
808 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000809
Jiri Slaby57c94122012-10-18 22:26:43 +0200810 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000811
Jiri Slabybddc7152012-10-18 22:26:42 +0200812 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000813}
814
815/**
816 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * @c: unicode byte to echo
818 * @tty: terminal device
819 *
Alan Cox4edf1822008-02-08 04:18:44 -0800820 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100822 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000823 * This variant does not treat control characters specially.
824 *
825 * Locking: echo_lock to protect the echo buffer
826 */
827
Jiri Slaby57c94122012-10-18 22:26:43 +0200828static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000829{
Jiri Slabybddc7152012-10-18 22:26:42 +0200830 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000831 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200832 add_echo_byte(ECHO_OP_START, ldata);
833 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000834 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200835 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200837 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000838}
839
840/**
841 * echo_char - echo a character
842 * @c: unicode byte to echo
843 * @tty: terminal device
844 *
845 * Echo user input back onto the screen. This must be called only when
846 * L_ECHO(tty) is true. Called from the driver receive_buf path.
847 *
Joe Peterson62b26352009-09-09 15:03:47 -0600848 * This variant tags control characters to be echoed as "^X"
849 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000850 *
851 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
853
854static void echo_char(unsigned char c, struct tty_struct *tty)
855{
Jiri Slabybddc7152012-10-18 22:26:42 +0200856 struct n_tty_data *ldata = tty->disc_data;
857
858 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000859
860 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000863 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600864 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200865 add_echo_byte(ECHO_OP_START, ldata);
866 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000867 }
868
Jiri Slabybddc7152012-10-18 22:26:42 +0200869 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Alan Cox17b82062008-10-13 10:45:06 +0100872/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000873 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200874 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100875 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000876
Jiri Slaby57c94122012-10-18 22:26:43 +0200877static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200879 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200880 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200881 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883}
884
885/**
886 * eraser - handle erase function
887 * @c: character input
888 * @tty: terminal device
889 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200890 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * present in the stream from the driver layer. Handles the complexities
892 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100893 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000894 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Alan Cox4edf1822008-02-08 04:18:44 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static void eraser(unsigned char c, struct tty_struct *tty)
898{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200899 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 enum { ERASE, WERASE, KILL } kill_type;
901 int head, seen_alnums, cnt;
902 unsigned long flags;
903
Alan Cox17b82062008-10-13 10:45:06 +0100904 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200905 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000906 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return;
908 }
909 if (c == ERASE_CHAR(tty))
910 kill_type = ERASE;
911 else if (c == WERASE_CHAR(tty))
912 kill_type = WERASE;
913 else {
914 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100915 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200916 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200918 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100919 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return;
921 }
922 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100923 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200924 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200926 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100927 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200928 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 echo_char(KILL_CHAR(tty), tty);
930 /* Add a newline if ECHOK is on and ECHOKE is off. */
931 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200932 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return;
934 }
935 kill_type = KILL;
936 }
937
938 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100939 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200940 while (ldata->read_head != ldata->canon_head) {
941 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* erase a single possibly multibyte character */
944 do {
945 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200946 c = ldata->read_buf[head];
947 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* do not partially erase */
950 if (is_continuation(c, tty))
951 break;
952
953 if (kill_type == WERASE) {
954 /* Equivalent to BSD's ALTWERASE. */
955 if (isalnum(c) || c == '_')
956 seen_alnums++;
957 else if (seen_alnums)
958 break;
959 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200960 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100961 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200962 ldata->read_head = head;
963 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100964 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (L_ECHO(tty)) {
966 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200967 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200968 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200969 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 /* if cnt > 1, output a multi-byte character */
972 echo_char(c, tty);
973 while (--cnt > 0) {
974 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200975 echo_char_raw(ldata->read_buf[head],
976 ldata);
977 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
980 echo_char(ERASE_CHAR(tty), tty);
981 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000982 unsigned int num_chars = 0;
983 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200984 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Joe Petersona88a69c2009-01-02 13:40:53 +0000986 /*
987 * Count the columns used for characters
988 * since the start of input or after a
989 * previous tab.
990 * This info is used to go back the correct
991 * number of columns.
992 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200993 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000994 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200995 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000996 if (c == '\t') {
997 after_tab = 1;
998 break;
Alan Cox300a6202009-01-02 13:41:04 +0000999 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001001 num_chars += 2;
1002 } else if (!is_continuation(c, tty)) {
1003 num_chars++;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001006 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
1008 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001009 echo_char_raw('\b', ldata);
1010 echo_char_raw(' ', ldata);
1011 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001014 echo_char_raw('\b', ldata);
1015 echo_char_raw(' ', ldata);
1016 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 }
1019 }
1020 if (kill_type == ERASE)
1021 break;
1022 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001023 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001024 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/**
1028 * isig - handle the ISIG optio
1029 * @sig: signal
1030 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001032 * Called when a signal is being sent due to terminal input.
1033 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001034 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001035 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Alan Cox4edf1822008-02-08 04:18:44 -08001037
Peter Hurley8c985d12013-03-06 08:38:19 -05001038static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Peter Hurley8c985d12013-03-06 08:38:19 -05001040 struct pid *tty_pgrp = tty_get_pgrp(tty);
1041 if (tty_pgrp) {
1042 kill_pgrp(tty_pgrp, sig, 1);
1043 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045}
1046
1047/**
1048 * n_tty_receive_break - handle break
1049 * @tty: terminal
1050 *
1051 * An RS232 break event has been hit in the incoming bitstream. This
1052 * can cause a variety of events depending upon the termios settings.
1053 *
1054 * Called from the receive_buf path so single threaded.
1055 */
Alan Cox4edf1822008-02-08 04:18:44 -08001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static inline void n_tty_receive_break(struct tty_struct *tty)
1058{
Jiri Slaby57c94122012-10-18 22:26:43 +02001059 struct n_tty_data *ldata = tty->disc_data;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (I_IGNBRK(tty))
1062 return;
1063 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001064 isig(SIGINT, tty);
1065 if (!L_NOFLSH(tty)) {
1066 n_tty_flush_buffer(tty);
1067 tty_driver_flush_buffer(tty);
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return;
1070 }
1071 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001072 put_tty_queue('\377', ldata);
1073 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001075 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 wake_up_interruptible(&tty->read_wait);
1077}
1078
1079/**
1080 * n_tty_receive_overrun - handle overrun reporting
1081 * @tty: terminal
1082 *
1083 * Data arrived faster than we could process it. While the tty
1084 * driver has flagged this the bits that were missed are gone
1085 * forever.
1086 *
1087 * Called from the receive_buf path so single threaded. Does not
1088 * need locking as num_overrun and overrun_time are function
1089 * private.
1090 */
Alan Cox4edf1822008-02-08 04:18:44 -08001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static inline void n_tty_receive_overrun(struct tty_struct *tty)
1093{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001094 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 char buf[64];
1096
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001097 ldata->num_overrun++;
1098 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1099 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1101 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001102 ldata->num_overrun);
1103 ldata->overrun_time = jiffies;
1104 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
1108/**
1109 * n_tty_receive_parity_error - error notifier
1110 * @tty: terminal device
1111 * @c: character
1112 *
1113 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001114 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1117 unsigned char c)
1118{
Jiri Slaby57c94122012-10-18 22:26:43 +02001119 struct n_tty_data *ldata = tty->disc_data;
1120
Alan Cox4edf1822008-02-08 04:18:44 -08001121 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001124 put_tty_queue('\377', ldata);
1125 put_tty_queue('\0', ldata);
1126 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001128 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001130 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 wake_up_interruptible(&tty->read_wait);
1132}
1133
1134/**
1135 * n_tty_receive_char - perform processing
1136 * @tty: terminal device
1137 * @c: character
1138 *
1139 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001140 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * driver above.
1142 */
1143
1144static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1145{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001146 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001148 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001150 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001151 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return;
1153 }
Alan Cox4edf1822008-02-08 04:18:44 -08001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (I_ISTRIP(tty))
1156 c &= 0x7f;
1157 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001158 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
hyc@symas.com26df6d12010-06-22 10:14:49 -07001160 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001161 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001162 return;
1163 }
1164
Joe Peterson54d2a372008-02-06 01:37:59 -08001165 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001166 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1167 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001168 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001169 process_echoes(tty);
1170 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (tty->closing) {
1173 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001174 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001176 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001177 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 stop_tty(tty);
1179 }
1180 return;
1181 }
1182
1183 /*
1184 * If the previous character was LNEXT, or we know that this
1185 * character is not one of the characters that we'll have to
1186 * handle specially, do shortcut processing to speed things
1187 * up.
1188 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001189 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001190 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001191 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001192 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001193 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001194 if (L_ECHO(tty))
1195 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001196 return;
1197 }
1198 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001199 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001201 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001202 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001204 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001206 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001207 put_tty_queue(c, ldata);
1208 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return;
1210 }
Alan Cox4edf1822008-02-08 04:18:44 -08001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (I_IXON(tty)) {
1213 if (c == START_CHAR(tty)) {
1214 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001215 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return;
1217 }
1218 if (c == STOP_CHAR(tty)) {
1219 stop_tty(tty);
1220 return;
1221 }
1222 }
Joe Peterson575537b32008-04-30 00:53:30 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (L_ISIG(tty)) {
1225 int signal;
1226 signal = SIGINT;
1227 if (c == INTR_CHAR(tty))
1228 goto send_signal;
1229 signal = SIGQUIT;
1230 if (c == QUIT_CHAR(tty))
1231 goto send_signal;
1232 signal = SIGTSTP;
1233 if (c == SUSP_CHAR(tty)) {
1234send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001235 if (!L_NOFLSH(tty)) {
1236 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001237 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001238 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001239 if (I_IXON(tty))
1240 start_tty(tty);
1241 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001242 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001243 process_echoes(tty);
1244 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001245 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return;
1247 }
1248 }
Joe Peterson575537b32008-04-30 00:53:30 -07001249
1250 if (c == '\r') {
1251 if (I_IGNCR(tty))
1252 return;
1253 if (I_ICRNL(tty))
1254 c = '\n';
1255 } else if (c == '\n' && I_INLCR(tty))
1256 c = '\r';
1257
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001258 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1260 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1261 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001262 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
1264 }
1265 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001266 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001268 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001270 echo_char_raw('^', ldata);
1271 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001272 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 }
1275 return;
1276 }
1277 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1278 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001279 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jiri Slaby57c94122012-10-18 22:26:43 +02001281 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001283 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001284 while (tail != ldata->read_head) {
1285 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1287 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001288 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return;
1290 }
1291 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001292 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001293 if (L_ECHO(tty))
1294 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001295 return;
1296 }
1297 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001298 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001299 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 goto handle_newline;
1302 }
1303 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001304 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001305 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001306 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001307 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 c = __DISABLED_CHAR;
1309 goto handle_newline;
1310 }
1311 if ((c == EOL_CHAR(tty)) ||
1312 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001313 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1314 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001315 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001316 if (L_ECHO(tty))
1317 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001318 return;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1322 */
1323 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001325 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001326 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001328 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330 /*
1331 * XXX does PARMRK doubling happen for
1332 * EOL_CHAR and EOL2_CHAR?
1333 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001334 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001335 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Alan Cox4edf1822008-02-08 04:18:44 -08001337handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001338 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001339 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001340 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001341 ldata->canon_head = ldata->read_head;
1342 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001343 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1345 if (waitqueue_active(&tty->read_wait))
1346 wake_up_interruptible(&tty->read_wait);
1347 return;
1348 }
1349 }
Alan Cox4edf1822008-02-08 04:18:44 -08001350
Joe Petersonacc71bb2009-01-02 13:43:32 +00001351 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001352 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001353 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001354 if (L_ECHO(tty))
1355 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001356 return;
1357 }
1358 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001359 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001361 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 else {
1363 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001364 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001365 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 echo_char(c, tty);
1367 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001368 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Joe Petersonacc71bb2009-01-02 13:43:32 +00001371 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001372 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Jiri Slaby57c94122012-10-18 22:26:43 +02001374 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378/**
1379 * n_tty_write_wakeup - asynchronous I/O notifier
1380 * @tty: tty device
1381 *
1382 * Required for the ptys, serial driver etc. since processes
1383 * that attach themselves to the master and rely on ASYNC
1384 * IO must be woken up
1385 */
1386
1387static void n_tty_write_wakeup(struct tty_struct *tty)
1388{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001389 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/**
1394 * n_tty_receive_buf - data receive
1395 * @tty: terminal device
1396 * @cp: buffer
1397 * @fp: flag buffer
1398 * @count: characters
1399 *
1400 * Called by the terminal driver when a block of characters has
1401 * been received. This function must be called from soft contexts
1402 * not from interrupt context. The driver is responsible for making
1403 * calls one at a time and in order (or using flush_to_ldisc)
1404 */
Alan Cox4edf1822008-02-08 04:18:44 -08001405
Linus Torvalds55db4c62011-06-04 06:33:24 +09001406static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1407 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001409 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 const unsigned char *p;
1411 char *f, flags = TTY_NORMAL;
1412 int i;
1413 char buf[64];
1414 unsigned long cpuflags;
1415
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001416 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001417 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001418 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1419 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001421 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1422 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1423 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 cp += i;
1425 count -= i;
1426
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001427 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1428 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001430 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1431 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1432 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001433 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001435 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (f)
1437 flags = *f++;
1438 switch (flags) {
1439 case TTY_NORMAL:
1440 n_tty_receive_char(tty, *p);
1441 break;
1442 case TTY_BREAK:
1443 n_tty_receive_break(tty);
1444 break;
1445 case TTY_PARITY:
1446 case TTY_FRAME:
1447 n_tty_receive_parity_error(tty, *p);
1448 break;
1449 case TTY_OVERRUN:
1450 n_tty_receive_overrun(tty);
1451 break;
1452 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001453 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 tty_name(tty, buf), flags);
1455 break;
1456 }
1457 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001458 if (tty->ops->flush_chars)
1459 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Linus Torvalds55db4c62011-06-04 06:33:24 +09001462 n_tty_set_room(tty);
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);
1478 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1479 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
1486int is_ignored(int sig)
1487{
1488 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001489 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/**
1493 * n_tty_set_termios - termios data changed
1494 * @tty: terminal
1495 * @old: previous data
1496 *
1497 * Called by the tty layer when the user changes termios flags so
1498 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001499 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 * guaranteed that this function will not be re-entered or in progress
1501 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001502 *
1503 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
Alan Cox4edf1822008-02-08 04:18:44 -08001505
1506static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001508 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001509 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001510
1511 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001512 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001513 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001514 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001515 ldata->canon_head = ldata->read_tail;
1516 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001517 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001518 }
1519
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001520 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001521 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001522
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001523 ldata->icanon = (L_ICANON(tty) != 0);
Peter Hurley582f5592013-05-17 12:49:48 -04001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1526 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1527 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1528 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001529 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001532 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001534 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001537 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1538 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1539 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1540 set_bit('\n', ldata->process_char_map);
1541 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (L_IEXTEN(tty)) {
1543 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001544 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001546 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (L_ECHO(tty))
1550 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001551 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 }
1554 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001555 set_bit(START_CHAR(tty), ldata->process_char_map);
1556 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1560 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1561 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001564 ldata->raw = 0;
1565 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001567 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1569 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1570 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001571 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001573 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001575 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001576 /*
1577 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1578 * been stopped by STOP_CHAR(tty) before it.
1579 */
1580 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1581 start_tty(tty);
1582 }
1583
Alan Coxf34d7a52008-04-30 00:54:13 -07001584 /* The termios change make the tty ready for I/O */
1585 wake_up_interruptible(&tty->write_wait);
1586 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/**
1590 * n_tty_close - close the ldisc for this tty
1591 * @tty: device
1592 *
Alan Cox4edf1822008-02-08 04:18:44 -08001593 * Called from the terminal layer when this line discipline is
1594 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * discipline change. The function will not be called while other
1596 * ldisc methods are in progress.
1597 */
Alan Cox4edf1822008-02-08 04:18:44 -08001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static void n_tty_close(struct tty_struct *tty)
1600{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001601 struct n_tty_data *ldata = tty->disc_data;
1602
Peter Hurley79901312013-03-11 16:44:23 -04001603 if (tty->link)
1604 n_tty_packet_mode_flush(tty);
1605
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001606 kfree(ldata->read_buf);
1607 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001608 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001609 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/**
1613 * n_tty_open - open an ldisc
1614 * @tty: terminal to open
1615 *
Alan Cox4edf1822008-02-08 04:18:44 -08001616 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * terminal device. Can sleep. Called serialized so that no
1618 * other events will occur in parallel. No further open will occur
1619 * until a close.
1620 */
1621
1622static int n_tty_open(struct tty_struct *tty)
1623{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001624 struct n_tty_data *ldata;
1625
1626 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1627 if (!ldata)
1628 goto err;
1629
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001630 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001631 mutex_init(&ldata->atomic_read_lock);
1632 mutex_init(&ldata->output_lock);
1633 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001634 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001635
Joe Petersona88a69c2009-01-02 13:40:53 +00001636 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001637 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1638 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1639 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001640 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001641
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001642 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001643 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001644 ldata->column = 0;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001645 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001647 /* indicate buffer work may resume */
1648 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1649 n_tty_set_termios(tty, NULL);
1650 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001653err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001654 kfree(ldata->read_buf);
1655 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001656 kfree(ldata);
1657err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001658 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661static inline int input_available_p(struct tty_struct *tty, int amt)
1662{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001663 struct n_tty_data *ldata = tty->disc_data;
1664
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001665 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001666 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001667 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001669 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 1;
1671
1672 return 0;
1673}
1674
1675/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001676 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * @tty: terminal device
1678 * @b: user data
1679 * @nr: size of data
1680 *
Alan Cox11a96d12008-10-13 10:46:24 +01001681 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * ICANON is off; it copies characters straight from the tty queue to
1683 * user space directly. It can be profitably called twice; once to
1684 * drain the space from the tail pointer to the (physical) end of the
1685 * buffer, and once to drain the space from the (physical) beginning of
1686 * the buffer to head pointer.
1687 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001688 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *
1690 */
Alan Cox4edf1822008-02-08 04:18:44 -08001691
Alan Cox33f0f882006-01-09 20:54:13 -08001692static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 unsigned char __user **b,
1694 size_t *nr)
1695
1696{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001697 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int retval;
1699 size_t n;
1700 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001701 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001704 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001705 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001707 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001709 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001711 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001712 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1713 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001714 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001715 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001716 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1717 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001718 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001719 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001720 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001721 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *b += n;
1723 *nr -= n;
1724 }
1725 return retval;
1726}
1727
Al Virocc4191d2008-03-29 03:08:48 +00001728extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001729 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731/**
1732 * job_control - check job control
1733 * @tty: tty
1734 * @file: file handle
1735 *
1736 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001737 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001739 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001740 * Locking: redirected write test is safe
1741 * current->signal->tty check is safe
1742 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Alan Cox4edf1822008-02-08 04:18:44 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int job_control(struct tty_struct *tty, struct file *file)
1746{
1747 /* Job control check -- must be done at start and after
1748 every sleep (POSIX.1 7.1.1.4). */
1749 /* NOTE: not yet done after every sleep pending a thorough
1750 check of the logic of this change. -- jlc */
1751 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001752 if (file->f_op->write == redirected_tty_write ||
1753 current->signal->tty != tty)
1754 return 0;
1755
1756 spin_lock_irq(&tty->ctrl_lock);
1757 if (!tty->pgrp)
1758 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1759 else if (task_pgrp(current) != tty->pgrp) {
1760 spin_unlock_irq(&tty->ctrl_lock);
1761 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1762 return -EIO;
1763 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1764 set_thread_flag(TIF_SIGPENDING);
1765 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001767 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769}
Alan Cox4edf1822008-02-08 04:18:44 -08001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/**
Alan Cox11a96d12008-10-13 10:46:24 +01001773 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * @tty: tty device
1775 * @file: file object
1776 * @buf: userspace buffer pointer
1777 * @nr: size of I/O
1778 *
1779 * Perform reads for the line discipline. We are guaranteed that the
1780 * line discipline will not be closed under us but we may get multiple
1781 * parallel readers and must handle this ourselves. We may also get
1782 * a hangup. Always called in user context, may sleep.
1783 *
1784 * This code must be sure never to sleep through a hangup.
1785 */
Alan Cox4edf1822008-02-08 04:18:44 -08001786
Alan Cox11a96d12008-10-13 10:46:24 +01001787static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned char __user *buf, size_t nr)
1789{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001790 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned char __user *b = buf;
1792 DECLARE_WAITQUEUE(wait, current);
1793 int c;
1794 int minimum, time;
1795 ssize_t retval = 0;
1796 ssize_t size;
1797 long timeout;
1798 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001799 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001803 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 minimum = time = 0;
1807 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001808 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 minimum = MIN_CHAR(tty);
1810 if (minimum) {
Peter Hurleya6e54312013-06-15 07:28:29 -04001811 time = (HZ / 10) * TIME_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (time)
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001813 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 else if (!waitqueue_active(&tty->read_wait) ||
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001815 (ldata->minimum_to_wake > minimum))
1816 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 } else {
Peter Hurleya6e54312013-06-15 07:28:29 -04001818 timeout = (HZ / 10) * TIME_CHAR(tty);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001819 ldata->minimum_to_wake = minimum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821 }
1822
1823 /*
1824 * Internal serialization of reads.
1825 */
1826 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001827 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001829 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001830 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -ERESTARTSYS;
1832 }
Alan Cox04f378b2008-04-30 00:53:29 -07001833 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 while (nr) {
1837 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001838 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned char cs;
1840 if (b != buf)
1841 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001842 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 cs = tty->link->ctrl_status;
1844 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001845 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001846 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 retval = -EFAULT;
1848 b--;
1849 break;
1850 }
1851 nr--;
1852 break;
1853 }
1854 /* This statement must be first before checking for input
1855 so that any interrupt will set the state back to
1856 TASK_RUNNING. */
1857 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001858
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001859 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 ((minimum - (b - buf)) >= 1))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001861 ldata->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (!input_available_p(tty, 0)) {
1864 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1865 retval = -EIO;
1866 break;
1867 }
1868 if (tty_hung_up_p(file))
1869 break;
1870 if (!timeout)
1871 break;
1872 if (file->f_flags & O_NONBLOCK) {
1873 retval = -EAGAIN;
1874 break;
1875 }
1876 if (signal_pending(current)) {
1877 retval = -ERESTARTSYS;
1878 break;
1879 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001880 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 continue;
1883 }
1884 __set_current_state(TASK_RUNNING);
1885
1886 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001887 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001888 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 retval = -EFAULT;
1890 b--;
1891 break;
1892 }
1893 nr--;
1894 }
1895
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001896 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001898 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001899 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001900 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001902 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001903 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001904 c = ldata->read_buf[ldata->read_tail];
1905 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001907 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (eol) {
1909 /* this test should be redundant:
1910 * we shouldn't be reading data if
1911 * canon_data is 0
1912 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001913 if (--ldata->canon_data < 0)
1914 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Ivo Sieben98001212013-01-28 13:32:01 +01001916 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001919 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 retval = -EFAULT;
1921 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001922 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
1924 }
1925 nr--;
1926 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001927 if (eol) {
1928 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001929 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001931 }
Ivo Sieben98001212013-01-28 13:32:01 +01001932 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Ivo Sieben98001212013-01-28 13:32:01 +01001934 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (retval)
1936 break;
1937 } else {
1938 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001939 /* The copy function takes the read lock and handles
1940 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 uncopied = copy_from_read_buf(tty, &b, &nr);
1942 uncopied += copy_from_read_buf(tty, &b, &nr);
1943 if (uncopied) {
1944 retval = -EFAULT;
1945 break;
1946 }
1947 }
1948
1949 /* If there is enough space in the read buffer now, let the
1950 * low-level driver know. We use n_tty_chars_in_buffer() to
1951 * check the buffer, as it now knows about canonical mode.
1952 * Otherwise, if the driver is throttled and the line is
1953 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1954 * we won't get any more characters.
1955 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001956 while (1) {
1957 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1958 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1959 break;
1960 if (!tty->count)
1961 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001962 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001963 if (!tty_unthrottle_safe(tty))
1964 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001965 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001966 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (b - buf >= minimum)
1969 break;
1970 if (time)
1971 timeout = time;
1972 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001973 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 remove_wait_queue(&tty->read_wait, &wait);
1975
1976 if (!waitqueue_active(&tty->read_wait))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04001977 ldata->minimum_to_wake = minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 __set_current_state(TASK_RUNNING);
1980 size = b - buf;
1981 if (size) {
1982 retval = size;
1983 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001984 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001986 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvalds55db4c62011-06-04 06:33:24 +09001988 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return retval;
1990}
1991
1992/**
Alan Cox11a96d12008-10-13 10:46:24 +01001993 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 * @tty: tty device
1995 * @file: file object
1996 * @buf: userspace buffer pointer
1997 * @nr: size of I/O
1998 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001999 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002001 * and other such events. Since the receive code will echo characters,
2002 * thus calling driver write methods, the output_lock is used in
2003 * the output processing functions called here as well as in the
2004 * echo processing function to protect the column state and space
2005 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 *
2007 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002008 *
2009 * Locking: output_lock to protect column state and space left
2010 * (note that the process_output*() functions take this
2011 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 */
Alan Cox4edf1822008-02-08 04:18:44 -08002013
Alan Cox11a96d12008-10-13 10:46:24 +01002014static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002015 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 const unsigned char *b = buf;
2018 DECLARE_WAITQUEUE(wait, current);
2019 int c;
2020 ssize_t retval = 0;
2021
2022 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2023 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2024 retval = tty_check_change(tty);
2025 if (retval)
2026 return retval;
2027 }
2028
Joe Petersona88a69c2009-01-02 13:40:53 +00002029 /* Write out any echoed characters that are still pending */
2030 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 add_wait_queue(&tty->write_wait, &wait);
2033 while (1) {
2034 set_current_state(TASK_INTERRUPTIBLE);
2035 if (signal_pending(current)) {
2036 retval = -ERESTARTSYS;
2037 break;
2038 }
2039 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2040 retval = -EIO;
2041 break;
2042 }
Peter Hurley582f5592013-05-17 12:49:48 -04002043 if (O_OPOST(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002045 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (num < 0) {
2047 if (num == -EAGAIN)
2048 break;
2049 retval = num;
2050 goto break_out;
2051 }
2052 b += num;
2053 nr -= num;
2054 if (nr == 0)
2055 break;
2056 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002057 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 b++; nr--;
2060 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002061 if (tty->ops->flush_chars)
2062 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002064 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002065 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002066 if (c < 0) {
2067 retval = c;
2068 goto break_out;
2069 }
2070 if (!c)
2071 break;
2072 b += c;
2073 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 if (!nr)
2077 break;
2078 if (file->f_flags & O_NONBLOCK) {
2079 retval = -EAGAIN;
2080 break;
2081 }
2082 schedule();
2083 }
2084break_out:
2085 __set_current_state(TASK_RUNNING);
2086 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002087 if (b - buf != nr && tty->fasync)
2088 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return (b - buf) ? b - buf : retval;
2090}
2091
2092/**
Alan Cox11a96d12008-10-13 10:46:24 +01002093 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * @tty: terminal device
2095 * @file: file accessing it
2096 * @wait: poll table
2097 *
2098 * Called when the line discipline is asked to poll() for data or
2099 * for special events. This code is not serialized with respect to
2100 * other events save open/close.
2101 *
2102 * This code must be sure never to sleep through a hangup.
2103 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 */
Alan Cox4edf1822008-02-08 04:18:44 -08002105
Alan Cox11a96d12008-10-13 10:46:24 +01002106static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002107 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002109 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 unsigned int mask = 0;
2111
2112 poll_wait(file, &tty->read_wait, wait);
2113 poll_wait(file, &tty->write_wait, wait);
2114 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2115 mask |= POLLIN | POLLRDNORM;
2116 if (tty->packet && tty->link->ctrl_status)
2117 mask |= POLLPRI | POLLIN | POLLRDNORM;
2118 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2119 mask |= POLLHUP;
2120 if (tty_hung_up_p(file))
2121 mask |= POLLHUP;
2122 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2123 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002124 ldata->minimum_to_wake = MIN_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 else
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002126 ldata->minimum_to_wake = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002128 if (tty->ops->write && !tty_is_writelocked(tty) &&
2129 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2130 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 mask |= POLLOUT | POLLWRNORM;
2132 return mask;
2133}
2134
Jiri Slaby57c94122012-10-18 22:26:43 +02002135static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002136{
2137 int nr, head, tail;
2138
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002139 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002140 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002141 head = ldata->canon_head;
2142 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002143 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2144 /* Skip EOF-chars.. */
2145 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002146 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002147 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002148 nr--;
2149 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2150 }
2151 return nr;
2152}
2153
2154static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2155 unsigned int cmd, unsigned long arg)
2156{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002157 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002158 int retval;
2159
2160 switch (cmd) {
2161 case TIOCOUTQ:
2162 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2163 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002164 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002165 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002166 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002167 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002168 return put_user(retval, (unsigned int __user *) arg);
2169 default:
2170 return n_tty_ioctl_helper(tty, file, cmd, arg);
2171 }
2172}
2173
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002174static void n_tty_fasync(struct tty_struct *tty, int on)
2175{
2176 struct n_tty_data *ldata = tty->disc_data;
2177
2178 if (!waitqueue_active(&tty->read_wait)) {
2179 if (on)
2180 ldata->minimum_to_wake = 1;
2181 else if (!tty->fasync)
2182 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2183 }
2184}
2185
Alan Coxa352def2008-07-16 21:53:12 +01002186struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002187 .magic = TTY_LDISC_MAGIC,
2188 .name = "n_tty",
2189 .open = n_tty_open,
2190 .close = n_tty_close,
2191 .flush_buffer = n_tty_flush_buffer,
2192 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002193 .read = n_tty_read,
2194 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002195 .ioctl = n_tty_ioctl,
2196 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002197 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002198 .receive_buf = n_tty_receive_buf,
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002199 .write_wakeup = n_tty_write_wakeup,
2200 .fasync = n_tty_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002202
2203/**
2204 * n_tty_inherit_ops - inherit N_TTY methods
2205 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2206 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002207 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002208 * methods.
2209 */
2210
2211void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2212{
2213 *ops = tty_ldisc_N_TTY;
2214 ops->owner = NULL;
2215 ops->refcount = ops->flags = 0;
2216}
2217EXPORT_SYMBOL_GPL(n_tty_inherit_ops);