blob: 15f7195f4b4c888b97f14016608bbd5038fe26c4 [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;
92
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
96
97 int canon_data;
98 unsigned long canon_head;
99 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200100
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100104 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200105};
106
Miloslav Trmac522ed772007-07-15 23:40:56 -0700107static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
109{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200110 struct n_tty_data *ldata = tty->disc_data;
111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700113 return put_user(x, ptr);
114}
115
Linus Torvalds55db4c62011-06-04 06:33:24 +0900116/**
117 * n_tty_set__room - receive space
118 * @tty: terminal
119 *
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
123 * "instant".
124 */
125
126static void n_tty_set_room(struct tty_struct *tty)
127{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200128 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500129 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900130 int old_left;
131
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200132 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500133 if (I_PARMRK(tty)) {
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500138 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500140
Linus Torvalds55db4c62011-06-04 06:33:24 +0900141 /*
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
146 */
147 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200148 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900149 old_left = tty->receive_room;
150 tty->receive_room = left;
151
152 /* Did this open up the receive buffer? We may need to flip */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400155 "scheduling with invalid itty\n");
Peter Hurley21622932013-03-11 16:44:21 -0400156 /* see if ldisc has been killed - if so, this means that
157 * even though the ldisc has been halted and ->buf.work
158 * cancelled, ->buf.work is about to be rescheduled
159 */
160 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
161 "scheduling buffer work for halted ldisc\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200162 schedule_work(&tty->port->buf.work);
163 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900164}
165
Jiri Slaby57c94122012-10-18 22:26:43 +0200166static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200168 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
169 ldata->read_buf[ldata->read_head] = c;
170 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
171 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173}
174
Alan Cox17b82062008-10-13 10:45:06 +0100175/**
176 * put_tty_queue - add character to tty
177 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200178 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100179 *
180 * Add a character to the tty read_buf queue. This is done under the
181 * read_lock to serialize character addition and also to protect us
182 * against parallel reads or flushes
183 */
184
Jiri Slaby57c94122012-10-18 22:26:43 +0200185static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 unsigned long flags;
188 /*
189 * The problem of stomping on the buffers ends here.
190 * Why didn't anyone see this one coming? --AJK
191 */
Ivo Sieben98001212013-01-28 13:32:01 +0100192 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200193 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100194 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * reset_buffer_flags - reset buffer state
199 * @tty: terminal to reset
200 *
Alan Cox4edf1822008-02-08 04:18:44 -0800201 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * and make sure the driver is unthrottled. Called
203 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100204 *
205 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static void reset_buffer_flags(struct tty_struct *tty)
209{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200210 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned long flags;
212
Ivo Sieben98001212013-01-28 13:32:01 +0100213 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200214 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100215 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000216
Jiri Slabybddc7152012-10-18 22:26:42 +0200217 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200218 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200219 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000220
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200221 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200222 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900223 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Peter Hurleya30737a2013-03-11 16:44:22 -0400226static void n_tty_packet_mode_flush(struct tty_struct *tty)
227{
228 unsigned long flags;
229
230 spin_lock_irqsave(&tty->ctrl_lock, flags);
231 if (tty->link->packet) {
232 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
233 wake_up_interruptible(&tty->link->read_wait);
234 }
235 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
239 * n_tty_flush_buffer - clean input queue
240 * @tty: terminal device
241 *
242 * Flush the input buffer. Called when the line discipline is
243 * being closed, when the tty layer wants the buffer flushed (eg
244 * at hangup) or when the N_TTY line discipline internally has to
245 * clean the pending queue (for example some signals).
246 *
Alan Cox17b82062008-10-13 10:45:06 +0100247 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Alan Cox4edf1822008-02-08 04:18:44 -0800249
250static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 /* clear everything and unthrottle the driver */
253 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800254
Peter Hurleya30737a2013-03-11 16:44:22 -0400255 if (tty->link)
256 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/**
260 * n_tty_chars_in_buffer - report available bytes
261 * @tty: tty device
262 *
263 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800264 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100265 *
266 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Alan Cox4edf1822008-02-08 04:18:44 -0800268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
270{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200271 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned long flags;
273 ssize_t n = 0;
274
Ivo Sieben98001212013-01-28 13:32:01 +0100275 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200276 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200277 n = ldata->read_cnt;
278 } else if (ldata->canon_data) {
279 n = (ldata->canon_head > ldata->read_tail) ?
280 ldata->canon_head - ldata->read_tail :
281 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Ivo Sieben98001212013-01-28 13:32:01 +0100283 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return n;
285}
286
287/**
288 * is_utf8_continuation - utf8 multibyte check
289 * @c: byte to check
290 *
291 * Returns true if the utf8 character 'c' is a multibyte continuation
292 * character. We use this to correctly compute the on screen size
293 * of the character when printing
294 */
Alan Cox4edf1822008-02-08 04:18:44 -0800295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline int is_utf8_continuation(unsigned char c)
297{
298 return (c & 0xc0) == 0x80;
299}
300
301/**
302 * is_continuation - multibyte check
303 * @c: byte to check
304 *
305 * Returns true if the utf8 character 'c' is a multibyte continuation
306 * character and the terminal is in unicode mode.
307 */
Alan Cox4edf1822008-02-08 04:18:44 -0800308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static inline int is_continuation(unsigned char c, struct tty_struct *tty)
310{
311 return I_IUTF8(tty) && is_utf8_continuation(c);
312}
313
314/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000315 * do_output_char - output one character
316 * @c: character (or partial unicode symbol)
317 * @tty: terminal device
318 * @space: space available in tty driver write buffer
319 *
320 * This is a helper function that handles one output character
321 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600322 * doing OPOST processing and putting the results in the
323 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000324 *
325 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
326 * and NLDLY. They simply aren't relevant in the world today.
327 * If you ever need them, add them here.
328 *
329 * Returns the number of bytes of buffer space used or -1 if
330 * no space left.
331 *
332 * Locking: should be called under the output_lock to protect
333 * the column state and space left in the buffer
334 */
335
336static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
337{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200338 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000339 int spaces;
340
341 if (!space)
342 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000343
Joe Petersona88a69c2009-01-02 13:40:53 +0000344 switch (c) {
345 case '\n':
346 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200347 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000348 if (O_ONLCR(tty)) {
349 if (space < 2)
350 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200351 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700352 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000353 return 2;
354 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200355 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000356 break;
357 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200358 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000359 return 0;
360 if (O_OCRNL(tty)) {
361 c = '\n';
362 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200363 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000364 break;
365 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200366 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000367 break;
368 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200369 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000370 if (O_TABDLY(tty) == XTABS) {
371 if (space < spaces)
372 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200373 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000374 tty->ops->write(tty, " ", spaces);
375 return spaces;
376 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200377 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 break;
379 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200380 if (ldata->column > 0)
381 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000382 break;
383 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000384 if (!iscntrl(c)) {
385 if (O_OLCUC(tty))
386 c = toupper(c);
387 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200388 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000389 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000390 break;
391 }
392
393 tty_put_char(tty, c);
394 return 1;
395}
396
397/**
398 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * @c: character (or partial unicode symbol)
400 * @tty: terminal device
401 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600402 * Output one character with OPOST processing.
403 * Returns -1 when the output device is full and the character
404 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000406 * Locking: output_lock to protect column state and space left
407 * (also, this is called from n_tty_write under the
408 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Alan Cox4edf1822008-02-08 04:18:44 -0800410
Joe Petersona88a69c2009-01-02 13:40:53 +0000411static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jiri Slabybddc7152012-10-18 22:26:42 +0200413 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000414 int space, retval;
415
Jiri Slabybddc7152012-10-18 22:26:42 +0200416 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alan Coxf34d7a52008-04-30 00:54:13 -0700418 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000419 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jiri Slabybddc7152012-10-18 22:26:42 +0200421 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000422 if (retval < 0)
423 return -1;
424 else
425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000429 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600431 * @buf: character buffer
432 * @nr: number of bytes to output
433 *
434 * Output a block of characters with OPOST processing.
435 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * This path is used to speed up block console writes, among other
438 * things when processing blocks of output data. It handles only
439 * the simple cases normally found and helps to generate blocks of
440 * symbols for the console driver and thus improve performance.
441 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000442 * Locking: output_lock to protect column state and space left
443 * (also, this is called from n_tty_write under the
444 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Alan Cox4edf1822008-02-08 04:18:44 -0800446
Joe Petersona88a69c2009-01-02 13:40:53 +0000447static ssize_t process_output_block(struct tty_struct *tty,
448 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200450 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100452 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 const unsigned char *cp;
454
Jiri Slabybddc7152012-10-18 22:26:42 +0200455 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000456
Alan Coxf34d7a52008-04-30 00:54:13 -0700457 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000458 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200459 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (nr > space)
463 nr = space;
464
465 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000466 unsigned char c = *cp;
467
468 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case '\n':
470 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200471 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (O_ONLCR(tty))
473 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200474 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200477 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto break_out;
479 if (O_OCRNL(tty))
480 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200481 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case '\t':
484 goto break_out;
485 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200486 if (ldata->column > 0)
487 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000490 if (!iscntrl(c)) {
491 if (O_OLCUC(tty))
492 goto break_out;
493 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200494 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497 }
498 }
499break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700500 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000501
Jiri Slabybddc7152012-10-18 22:26:42 +0200502 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return i;
504}
505
Joe Petersona88a69c2009-01-02 13:40:53 +0000506/**
507 * process_echoes - write pending echo characters
508 * @tty: terminal device
509 *
510 * Write previously buffered echo (and other ldisc-generated)
511 * characters to the tty.
512 *
513 * Characters generated by the ldisc (including echoes) need to
514 * be buffered because the driver's write buffer can fill during
515 * heavy program output. Echoing straight to the driver will
516 * often fail under these conditions, causing lost characters and
517 * resulting mismatches of ldisc state information.
518 *
519 * Since the ldisc state must represent the characters actually sent
520 * to the driver at the time of the write, operations like certain
521 * changes in column state are also saved in the buffer and executed
522 * here.
523 *
524 * A circular fifo buffer is used so that the most recent characters
525 * are prioritized. Also, when control characters are echoed with a
526 * prefixed "^", the pair is treated atomically and thus not separated.
527 *
528 * Locking: output_lock to protect column state and space left,
529 * echo_lock to protect the echo buffer
530 */
531
532static void process_echoes(struct tty_struct *tty)
533{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200534 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000535 int space, nr;
536 unsigned char c;
537 unsigned char *cp, *buf_end;
538
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200539 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000540 return;
541
Jiri Slabybddc7152012-10-18 22:26:42 +0200542 mutex_lock(&ldata->output_lock);
543 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000544
545 space = tty_write_room(tty);
546
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200547 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
548 cp = ldata->echo_buf + ldata->echo_pos;
549 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000550 while (nr > 0) {
551 c = *cp;
552 if (c == ECHO_OP_START) {
553 unsigned char op;
554 unsigned char *opp;
555 int no_space_left = 0;
556
557 /*
558 * If the buffer byte is the start of a multi-byte
559 * operation, get the next byte, which is either the
560 * op code or a control character value.
561 */
562 opp = cp + 1;
563 if (opp == buf_end)
564 opp -= N_TTY_BUF_SIZE;
565 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000566
Joe Petersona88a69c2009-01-02 13:40:53 +0000567 switch (op) {
568 unsigned int num_chars, num_bs;
569
570 case ECHO_OP_ERASE_TAB:
571 if (++opp == buf_end)
572 opp -= N_TTY_BUF_SIZE;
573 num_chars = *opp;
574
575 /*
576 * Determine how many columns to go back
577 * in order to erase the tab.
578 * This depends on the number of columns
579 * used by other characters within the tab
580 * area. If this (modulo 8) count is from
581 * the start of input rather than from a
582 * previous tab, we offset by canon column.
583 * Otherwise, tab spacing is normal.
584 */
585 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200586 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000587 num_bs = 8 - (num_chars & 7);
588
589 if (num_bs > space) {
590 no_space_left = 1;
591 break;
592 }
593 space -= num_bs;
594 while (num_bs--) {
595 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200596 if (ldata->column > 0)
597 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000598 }
599 cp += 3;
600 nr -= 3;
601 break;
602
603 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200604 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000605 cp += 2;
606 nr -= 2;
607 break;
608
609 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200610 if (ldata->column > 0)
611 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000612 cp += 2;
613 nr -= 2;
614 break;
615
616 case ECHO_OP_START:
617 /* This is an escaped echo op start code */
618 if (!space) {
619 no_space_left = 1;
620 break;
621 }
622 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200623 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000624 space--;
625 cp += 2;
626 nr -= 2;
627 break;
628
629 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000630 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600631 * If the op is not a special byte code,
632 * it is a ctrl char tagged to be echoed
633 * as "^X" (where X is the letter
634 * representing the control char).
635 * Note that we must ensure there is
636 * enough space for the whole ctrl pair.
637 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000638 */
Joe Peterson62b26352009-09-09 15:03:47 -0600639 if (space < 2) {
640 no_space_left = 1;
641 break;
642 }
643 tty_put_char(tty, '^');
644 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200645 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600646 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000647 cp += 2;
648 nr -= 2;
649 }
650
651 if (no_space_left)
652 break;
653 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600654 if (O_OPOST(tty) &&
655 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
656 int retval = do_output_char(c, tty, space);
657 if (retval < 0)
658 break;
659 space -= retval;
660 } else {
661 if (!space)
662 break;
663 tty_put_char(tty, c);
664 space -= 1;
665 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000666 cp += 1;
667 nr -= 1;
668 }
669
670 /* When end of circular buffer reached, wrap around */
671 if (cp >= buf_end)
672 cp -= N_TTY_BUF_SIZE;
673 }
674
675 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200676 ldata->echo_pos = 0;
677 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200678 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000679 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200680 int num_processed = ldata->echo_cnt - nr;
681 ldata->echo_pos += num_processed;
682 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
683 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000684 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200685 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000686 }
687
Jiri Slabybddc7152012-10-18 22:26:42 +0200688 mutex_unlock(&ldata->echo_lock);
689 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000690
691 if (tty->ops->flush_chars)
692 tty->ops->flush_chars(tty);
693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000696 * add_echo_byte - add a byte to the echo buffer
697 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200698 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000699 *
700 * Add a character or operation byte to the echo buffer.
701 *
702 * Should be called under the echo lock to protect the echo buffer.
703 */
704
Jiri Slaby57c94122012-10-18 22:26:43 +0200705static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000706{
707 int new_byte_pos;
708
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200709 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000710 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200711 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000712
713 /*
714 * Since the buffer start position needs to be advanced,
715 * be sure to step by a whole operation byte group.
716 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200717 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
718 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000719 (N_TTY_BUF_SIZE - 1)] ==
720 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200721 ldata->echo_pos += 3;
722 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000723 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200724 ldata->echo_pos += 2;
725 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000726 }
727 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200728 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000729 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200730 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000731
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200732 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000733 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200734 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000735 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200736 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737 }
738
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200739 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000740}
741
742/**
743 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200744 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000745 *
746 * Add an operation to the echo buffer to move back one column.
747 *
748 * Locking: echo_lock to protect the echo buffer
749 */
750
Jiri Slaby57c94122012-10-18 22:26:43 +0200751static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000752{
Jiri Slabybddc7152012-10-18 22:26:42 +0200753 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200754 add_echo_byte(ECHO_OP_START, ldata);
755 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200756 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000757}
758
759/**
760 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200761 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000762 *
763 * Add an operation to the echo buffer to set the canon column
764 * to the current column.
765 *
766 * Locking: echo_lock to protect the echo buffer
767 */
768
Jiri Slaby57c94122012-10-18 22:26:43 +0200769static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000770{
Jiri Slabybddc7152012-10-18 22:26:42 +0200771 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200772 add_echo_byte(ECHO_OP_START, ldata);
773 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200774 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000775}
776
777/**
778 * echo_erase_tab - add operation to erase a tab
779 * @num_chars: number of character columns already used
780 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200781 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000782 *
783 * Add an operation to the echo buffer to erase a tab.
784 *
785 * Called by the eraser function, which knows how many character
786 * columns have been used since either a previous tab or the start
787 * of input. This information will be used later, along with
788 * canon column (if applicable), to go back the correct number
789 * of columns.
790 *
791 * Locking: echo_lock to protect the echo buffer
792 */
793
794static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200795 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000796{
Jiri Slabybddc7152012-10-18 22:26:42 +0200797 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000798
Jiri Slaby57c94122012-10-18 22:26:43 +0200799 add_echo_byte(ECHO_OP_START, ldata);
800 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000801
802 /* We only need to know this modulo 8 (tab spacing) */
803 num_chars &= 7;
804
805 /* Set the high bit as a flag if num_chars is after a previous tab */
806 if (after_tab)
807 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000808
Jiri Slaby57c94122012-10-18 22:26:43 +0200809 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000810
Jiri Slabybddc7152012-10-18 22:26:42 +0200811 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000812}
813
814/**
815 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * @c: unicode byte to echo
817 * @tty: terminal device
818 *
Alan Cox4edf1822008-02-08 04:18:44 -0800819 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100821 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000822 * This variant does not treat control characters specially.
823 *
824 * Locking: echo_lock to protect the echo buffer
825 */
826
Jiri Slaby57c94122012-10-18 22:26:43 +0200827static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000828{
Jiri Slabybddc7152012-10-18 22:26:42 +0200829 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000830 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200831 add_echo_byte(ECHO_OP_START, ldata);
832 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000833 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200834 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000835 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200836 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000837}
838
839/**
840 * echo_char - echo a character
841 * @c: unicode byte to echo
842 * @tty: terminal device
843 *
844 * Echo user input back onto the screen. This must be called only when
845 * L_ECHO(tty) is true. Called from the driver receive_buf path.
846 *
Joe Peterson62b26352009-09-09 15:03:47 -0600847 * This variant tags control characters to be echoed as "^X"
848 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000849 *
850 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
852
853static void echo_char(unsigned char c, struct tty_struct *tty)
854{
Jiri Slabybddc7152012-10-18 22:26:42 +0200855 struct n_tty_data *ldata = tty->disc_data;
856
857 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858
859 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200860 add_echo_byte(ECHO_OP_START, ldata);
861 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000862 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600863 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 add_echo_byte(ECHO_OP_START, ldata);
865 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000866 }
867
Jiri Slabybddc7152012-10-18 22:26:42 +0200868 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Alan Cox17b82062008-10-13 10:45:06 +0100871/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000872 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200873 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100874 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000875
Jiri Slaby57c94122012-10-18 22:26:43 +0200876static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200878 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200879 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200880 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882}
883
884/**
885 * eraser - handle erase function
886 * @c: character input
887 * @tty: terminal device
888 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200889 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * present in the stream from the driver layer. Handles the complexities
891 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100892 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000893 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Alan Cox4edf1822008-02-08 04:18:44 -0800895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static void eraser(unsigned char c, struct tty_struct *tty)
897{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200898 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 enum { ERASE, WERASE, KILL } kill_type;
900 int head, seen_alnums, cnt;
901 unsigned long flags;
902
Alan Cox17b82062008-10-13 10:45:06 +0100903 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200904 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000905 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
907 }
908 if (c == ERASE_CHAR(tty))
909 kill_type = ERASE;
910 else if (c == WERASE_CHAR(tty))
911 kill_type = WERASE;
912 else {
913 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100914 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200915 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200917 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100918 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return;
920 }
921 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100922 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200923 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200925 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100926 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200927 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 echo_char(KILL_CHAR(tty), tty);
929 /* Add a newline if ECHOK is on and ECHOKE is off. */
930 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200931 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return;
933 }
934 kill_type = KILL;
935 }
936
937 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100938 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200939 while (ldata->read_head != ldata->canon_head) {
940 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* erase a single possibly multibyte character */
943 do {
944 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200945 c = ldata->read_buf[head];
946 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* do not partially erase */
949 if (is_continuation(c, tty))
950 break;
951
952 if (kill_type == WERASE) {
953 /* Equivalent to BSD's ALTWERASE. */
954 if (isalnum(c) || c == '_')
955 seen_alnums++;
956 else if (seen_alnums)
957 break;
958 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200959 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100960 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200961 ldata->read_head = head;
962 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100963 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (L_ECHO(tty)) {
965 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200966 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200967 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200968 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 /* if cnt > 1, output a multi-byte character */
971 echo_char(c, tty);
972 while (--cnt > 0) {
973 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200974 echo_char_raw(ldata->read_buf[head],
975 ldata);
976 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
979 echo_char(ERASE_CHAR(tty), tty);
980 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000981 unsigned int num_chars = 0;
982 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200983 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Joe Petersona88a69c2009-01-02 13:40:53 +0000985 /*
986 * Count the columns used for characters
987 * since the start of input or after a
988 * previous tab.
989 * This info is used to go back the correct
990 * number of columns.
991 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200992 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000993 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200994 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000995 if (c == '\t') {
996 after_tab = 1;
997 break;
Alan Cox300a6202009-01-02 13:41:04 +0000998 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001000 num_chars += 2;
1001 } else if (!is_continuation(c, tty)) {
1002 num_chars++;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001005 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 } else {
1007 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001008 echo_char_raw('\b', ldata);
1009 echo_char_raw(' ', ldata);
1010 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001013 echo_char_raw('\b', ldata);
1014 echo_char_raw(' ', ldata);
1015 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 }
1018 }
1019 if (kill_type == ERASE)
1020 break;
1021 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001022 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001023 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026/**
1027 * isig - handle the ISIG optio
1028 * @sig: signal
1029 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001031 * Called when a signal is being sent due to terminal input.
1032 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001033 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001034 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Alan Cox4edf1822008-02-08 04:18:44 -08001036
Peter Hurley8c985d12013-03-06 08:38:19 -05001037static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Peter Hurley8c985d12013-03-06 08:38:19 -05001039 struct pid *tty_pgrp = tty_get_pgrp(tty);
1040 if (tty_pgrp) {
1041 kill_pgrp(tty_pgrp, sig, 1);
1042 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044}
1045
1046/**
1047 * n_tty_receive_break - handle break
1048 * @tty: terminal
1049 *
1050 * An RS232 break event has been hit in the incoming bitstream. This
1051 * can cause a variety of events depending upon the termios settings.
1052 *
1053 * Called from the receive_buf path so single threaded.
1054 */
Alan Cox4edf1822008-02-08 04:18:44 -08001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static inline void n_tty_receive_break(struct tty_struct *tty)
1057{
Jiri Slaby57c94122012-10-18 22:26:43 +02001058 struct n_tty_data *ldata = tty->disc_data;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (I_IGNBRK(tty))
1061 return;
1062 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001063 isig(SIGINT, tty);
1064 if (!L_NOFLSH(tty)) {
1065 n_tty_flush_buffer(tty);
1066 tty_driver_flush_buffer(tty);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
1069 }
1070 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001071 put_tty_queue('\377', ldata);
1072 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001074 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 wake_up_interruptible(&tty->read_wait);
1076}
1077
1078/**
1079 * n_tty_receive_overrun - handle overrun reporting
1080 * @tty: terminal
1081 *
1082 * Data arrived faster than we could process it. While the tty
1083 * driver has flagged this the bits that were missed are gone
1084 * forever.
1085 *
1086 * Called from the receive_buf path so single threaded. Does not
1087 * need locking as num_overrun and overrun_time are function
1088 * private.
1089 */
Alan Cox4edf1822008-02-08 04:18:44 -08001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static inline void n_tty_receive_overrun(struct tty_struct *tty)
1092{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001093 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 char buf[64];
1095
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001096 ldata->num_overrun++;
1097 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1098 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1100 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001101 ldata->num_overrun);
1102 ldata->overrun_time = jiffies;
1103 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105}
1106
1107/**
1108 * n_tty_receive_parity_error - error notifier
1109 * @tty: terminal device
1110 * @c: character
1111 *
1112 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001113 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1116 unsigned char c)
1117{
Jiri Slaby57c94122012-10-18 22:26:43 +02001118 struct n_tty_data *ldata = tty->disc_data;
1119
Alan Cox4edf1822008-02-08 04:18:44 -08001120 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001123 put_tty_queue('\377', ldata);
1124 put_tty_queue('\0', ldata);
1125 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001127 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001129 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 wake_up_interruptible(&tty->read_wait);
1131}
1132
1133/**
1134 * n_tty_receive_char - perform processing
1135 * @tty: terminal device
1136 * @c: character
1137 *
1138 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001139 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * driver above.
1141 */
1142
1143static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1144{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001145 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001147 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001149 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001150 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return;
1152 }
Alan Cox4edf1822008-02-08 04:18:44 -08001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (I_ISTRIP(tty))
1155 c &= 0x7f;
1156 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001157 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
hyc@symas.com26df6d12010-06-22 10:14:49 -07001159 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001160 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001161 return;
1162 }
1163
Joe Peterson54d2a372008-02-06 01:37:59 -08001164 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001165 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1166 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001167 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001168 process_echoes(tty);
1169 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (tty->closing) {
1172 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001173 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001175 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001176 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 stop_tty(tty);
1178 }
1179 return;
1180 }
1181
1182 /*
1183 * If the previous character was LNEXT, or we know that this
1184 * character is not one of the characters that we'll have to
1185 * handle specially, do shortcut processing to speed things
1186 * up.
1187 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001188 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001189 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001190 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001191 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001192 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001193 if (L_ECHO(tty))
1194 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001195 return;
1196 }
1197 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001198 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001200 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001201 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001203 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001205 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001206 put_tty_queue(c, ldata);
1207 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return;
1209 }
Alan Cox4edf1822008-02-08 04:18:44 -08001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (I_IXON(tty)) {
1212 if (c == START_CHAR(tty)) {
1213 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001214 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return;
1216 }
1217 if (c == STOP_CHAR(tty)) {
1218 stop_tty(tty);
1219 return;
1220 }
1221 }
Joe Peterson575537b32008-04-30 00:53:30 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (L_ISIG(tty)) {
1224 int signal;
1225 signal = SIGINT;
1226 if (c == INTR_CHAR(tty))
1227 goto send_signal;
1228 signal = SIGQUIT;
1229 if (c == QUIT_CHAR(tty))
1230 goto send_signal;
1231 signal = SIGTSTP;
1232 if (c == SUSP_CHAR(tty)) {
1233send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001234 if (!L_NOFLSH(tty)) {
1235 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001236 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001237 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001238 if (I_IXON(tty))
1239 start_tty(tty);
1240 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001241 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001242 process_echoes(tty);
1243 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001244 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 }
1247 }
Joe Peterson575537b32008-04-30 00:53:30 -07001248
1249 if (c == '\r') {
1250 if (I_IGNCR(tty))
1251 return;
1252 if (I_ICRNL(tty))
1253 c = '\n';
1254 } else if (c == '\n' && I_INLCR(tty))
1255 c = '\r';
1256
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001257 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1259 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1260 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001261 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return;
1263 }
1264 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001265 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001267 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001269 echo_char_raw('^', ldata);
1270 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001271 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 }
1274 return;
1275 }
1276 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1277 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001278 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jiri Slaby57c94122012-10-18 22:26:43 +02001280 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001282 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001283 while (tail != ldata->read_head) {
1284 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1286 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001287 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return;
1289 }
1290 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001291 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001292 if (L_ECHO(tty))
1293 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001294 return;
1295 }
1296 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001297 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001298 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 goto handle_newline;
1301 }
1302 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001303 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001304 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001305 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001306 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 c = __DISABLED_CHAR;
1308 goto handle_newline;
1309 }
1310 if ((c == EOL_CHAR(tty)) ||
1311 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001312 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1313 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001314 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001315 if (L_ECHO(tty))
1316 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001317 return;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1321 */
1322 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001324 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001325 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001327 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 /*
1330 * XXX does PARMRK doubling happen for
1331 * EOL_CHAR and EOL2_CHAR?
1332 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001333 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001334 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alan Cox4edf1822008-02-08 04:18:44 -08001336handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001337 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001338 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001339 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001340 ldata->canon_head = ldata->read_head;
1341 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001342 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1344 if (waitqueue_active(&tty->read_wait))
1345 wake_up_interruptible(&tty->read_wait);
1346 return;
1347 }
1348 }
Alan Cox4edf1822008-02-08 04:18:44 -08001349
Joe Petersonacc71bb2009-01-02 13:43:32 +00001350 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001351 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001352 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001353 if (L_ECHO(tty))
1354 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001355 return;
1356 }
1357 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001358 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001360 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 else {
1362 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001363 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001364 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 echo_char(c, tty);
1366 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001367 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Joe Petersonacc71bb2009-01-02 13:43:32 +00001370 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001371 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Jiri Slaby57c94122012-10-18 22:26:43 +02001373 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377/**
1378 * n_tty_write_wakeup - asynchronous I/O notifier
1379 * @tty: tty device
1380 *
1381 * Required for the ptys, serial driver etc. since processes
1382 * that attach themselves to the master and rely on ASYNC
1383 * IO must be woken up
1384 */
1385
1386static void n_tty_write_wakeup(struct tty_struct *tty)
1387{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001388 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/**
1393 * n_tty_receive_buf - data receive
1394 * @tty: terminal device
1395 * @cp: buffer
1396 * @fp: flag buffer
1397 * @count: characters
1398 *
1399 * Called by the terminal driver when a block of characters has
1400 * been received. This function must be called from soft contexts
1401 * not from interrupt context. The driver is responsible for making
1402 * calls one at a time and in order (or using flush_to_ldisc)
1403 */
Alan Cox4edf1822008-02-08 04:18:44 -08001404
Linus Torvalds55db4c62011-06-04 06:33:24 +09001405static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1406 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001408 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 const unsigned char *p;
1410 char *f, flags = TTY_NORMAL;
1411 int i;
1412 char buf[64];
1413 unsigned long cpuflags;
1414
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001415 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001416 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001417 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1418 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001420 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1421 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1422 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 cp += i;
1424 count -= i;
1425
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001426 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1427 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001429 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1430 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1431 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001432 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001434 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (f)
1436 flags = *f++;
1437 switch (flags) {
1438 case TTY_NORMAL:
1439 n_tty_receive_char(tty, *p);
1440 break;
1441 case TTY_BREAK:
1442 n_tty_receive_break(tty);
1443 break;
1444 case TTY_PARITY:
1445 case TTY_FRAME:
1446 n_tty_receive_parity_error(tty, *p);
1447 break;
1448 case TTY_OVERRUN:
1449 n_tty_receive_overrun(tty);
1450 break;
1451 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001452 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 tty_name(tty, buf), flags);
1454 break;
1455 }
1456 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001457 if (tty->ops->flush_chars)
1458 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
Linus Torvalds55db4c62011-06-04 06:33:24 +09001461 n_tty_set_room(tty);
1462
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001463 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001464 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1466 if (waitqueue_active(&tty->read_wait))
1467 wake_up_interruptible(&tty->read_wait);
1468 }
1469
1470 /*
1471 * Check the remaining room for the input canonicalization
1472 * mode. We don't want to throttle the driver if we're in
1473 * canonical mode and don't have a newline yet!
1474 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001475 while (1) {
1476 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1477 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1478 break;
1479 if (!tty_throttle_safe(tty))
1480 break;
1481 }
1482 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485int is_ignored(int sig)
1486{
1487 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001488 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/**
1492 * n_tty_set_termios - termios data changed
1493 * @tty: terminal
1494 * @old: previous data
1495 *
1496 * Called by the tty layer when the user changes termios flags so
1497 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001498 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * guaranteed that this function will not be re-entered or in progress
1500 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001501 *
1502 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
Alan Cox4edf1822008-02-08 04:18:44 -08001504
1505static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001507 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001508 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001509
1510 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001511 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001512 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001513 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001514 ldata->canon_head = ldata->read_tail;
1515 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001516 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001517 }
1518
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001519 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001520 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001521
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001522 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001524 ldata->raw = 1;
1525 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001526 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return;
1528 }
1529 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1530 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1531 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1532 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001533 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001536 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001538 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001541 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1542 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1543 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1544 set_bit('\n', ldata->process_char_map);
1545 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (L_IEXTEN(tty)) {
1547 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001550 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001552 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (L_ECHO(tty))
1554 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001555 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 }
1558 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 set_bit(START_CHAR(tty), ldata->process_char_map);
1560 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1564 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1565 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001567 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001568 ldata->raw = 0;
1569 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001571 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1573 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1574 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001575 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001577 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001579 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001580 /* The termios change make the tty ready for I/O */
1581 wake_up_interruptible(&tty->write_wait);
1582 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/**
1586 * n_tty_close - close the ldisc for this tty
1587 * @tty: device
1588 *
Alan Cox4edf1822008-02-08 04:18:44 -08001589 * Called from the terminal layer when this line discipline is
1590 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * discipline change. The function will not be called while other
1592 * ldisc methods are in progress.
1593 */
Alan Cox4edf1822008-02-08 04:18:44 -08001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static void n_tty_close(struct tty_struct *tty)
1596{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001597 struct n_tty_data *ldata = tty->disc_data;
1598
Peter Hurley79901312013-03-11 16:44:23 -04001599 if (tty->link)
1600 n_tty_packet_mode_flush(tty);
1601
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001602 kfree(ldata->read_buf);
1603 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001604 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001605 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/**
1609 * n_tty_open - open an ldisc
1610 * @tty: terminal to open
1611 *
Alan Cox4edf1822008-02-08 04:18:44 -08001612 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * terminal device. Can sleep. Called serialized so that no
1614 * other events will occur in parallel. No further open will occur
1615 * until a close.
1616 */
1617
1618static int n_tty_open(struct tty_struct *tty)
1619{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001620 struct n_tty_data *ldata;
1621
1622 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1623 if (!ldata)
1624 goto err;
1625
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001626 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001627 mutex_init(&ldata->atomic_read_lock);
1628 mutex_init(&ldata->output_lock);
1629 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001630 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001631
Joe Petersona88a69c2009-01-02 13:40:53 +00001632 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001633 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1634 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1635 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001636 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001637
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001638 tty->disc_data = ldata;
Peter Hurley21622932013-03-11 16:44:21 -04001639 /* indicate buffer work may resume */
1640 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001642 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001643 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 n_tty_set_termios(tty, NULL);
1645 tty->minimum_to_wake = 1;
1646 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001649err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001650 kfree(ldata->read_buf);
1651 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001652 kfree(ldata);
1653err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657static inline int input_available_p(struct tty_struct *tty, int amt)
1658{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001659 struct n_tty_data *ldata = tty->disc_data;
1660
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001661 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001662 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001663 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001665 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return 1;
1667
1668 return 0;
1669}
1670
1671/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001672 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * @tty: terminal device
1674 * @b: user data
1675 * @nr: size of data
1676 *
Alan Cox11a96d12008-10-13 10:46:24 +01001677 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * ICANON is off; it copies characters straight from the tty queue to
1679 * user space directly. It can be profitably called twice; once to
1680 * drain the space from the tail pointer to the (physical) end of the
1681 * buffer, and once to drain the space from the (physical) beginning of
1682 * the buffer to head pointer.
1683 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001684 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
1686 */
Alan Cox4edf1822008-02-08 04:18:44 -08001687
Alan Cox33f0f882006-01-09 20:54:13 -08001688static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 unsigned char __user **b,
1690 size_t *nr)
1691
1692{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001693 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int retval;
1695 size_t n;
1696 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001697 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001700 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001701 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001703 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001705 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001707 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001708 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1709 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001710 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001711 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001712 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1713 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001714 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001715 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001716 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001717 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *b += n;
1719 *nr -= n;
1720 }
1721 return retval;
1722}
1723
Al Virocc4191d2008-03-29 03:08:48 +00001724extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001725 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727/**
1728 * job_control - check job control
1729 * @tty: tty
1730 * @file: file handle
1731 *
1732 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001733 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001735 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001736 * Locking: redirected write test is safe
1737 * current->signal->tty check is safe
1738 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Alan Cox4edf1822008-02-08 04:18:44 -08001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741static int job_control(struct tty_struct *tty, struct file *file)
1742{
1743 /* Job control check -- must be done at start and after
1744 every sleep (POSIX.1 7.1.1.4). */
1745 /* NOTE: not yet done after every sleep pending a thorough
1746 check of the logic of this change. -- jlc */
1747 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001748 if (file->f_op->write == redirected_tty_write ||
1749 current->signal->tty != tty)
1750 return 0;
1751
1752 spin_lock_irq(&tty->ctrl_lock);
1753 if (!tty->pgrp)
1754 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1755 else if (task_pgrp(current) != tty->pgrp) {
1756 spin_unlock_irq(&tty->ctrl_lock);
1757 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1758 return -EIO;
1759 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1760 set_thread_flag(TIF_SIGPENDING);
1761 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001763 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
1765}
Alan Cox4edf1822008-02-08 04:18:44 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768/**
Alan Cox11a96d12008-10-13 10:46:24 +01001769 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * @tty: tty device
1771 * @file: file object
1772 * @buf: userspace buffer pointer
1773 * @nr: size of I/O
1774 *
1775 * Perform reads for the line discipline. We are guaranteed that the
1776 * line discipline will not be closed under us but we may get multiple
1777 * parallel readers and must handle this ourselves. We may also get
1778 * a hangup. Always called in user context, may sleep.
1779 *
1780 * This code must be sure never to sleep through a hangup.
1781 */
Alan Cox4edf1822008-02-08 04:18:44 -08001782
Alan Cox11a96d12008-10-13 10:46:24 +01001783static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 unsigned char __user *buf, size_t nr)
1785{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001786 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 unsigned char __user *b = buf;
1788 DECLARE_WAITQUEUE(wait, current);
1789 int c;
1790 int minimum, time;
1791 ssize_t retval = 0;
1792 ssize_t size;
1793 long timeout;
1794 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001795 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001799 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 minimum = time = 0;
1803 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001804 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 time = (HZ / 10) * TIME_CHAR(tty);
1806 minimum = MIN_CHAR(tty);
1807 if (minimum) {
1808 if (time)
1809 tty->minimum_to_wake = 1;
1810 else if (!waitqueue_active(&tty->read_wait) ||
1811 (tty->minimum_to_wake > minimum))
1812 tty->minimum_to_wake = minimum;
1813 } else {
1814 timeout = 0;
1815 if (time) {
1816 timeout = time;
1817 time = 0;
1818 }
1819 tty->minimum_to_wake = minimum = 1;
1820 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1860 ((minimum - (b - buf)) >= 1))
1861 tty->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 /* FIXME: does n_tty_set_room need locking ? */
1881 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 continue;
1884 }
1885 __set_current_state(TASK_RUNNING);
1886
1887 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001888 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001889 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 retval = -EFAULT;
1891 b--;
1892 break;
1893 }
1894 nr--;
1895 }
1896
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001897 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001899 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001900 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001901 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001903 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001904 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001905 c = ldata->read_buf[ldata->read_tail];
1906 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001908 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (eol) {
1910 /* this test should be redundant:
1911 * we shouldn't be reading data if
1912 * canon_data is 0
1913 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001914 if (--ldata->canon_data < 0)
1915 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Ivo Sieben98001212013-01-28 13:32:01 +01001917 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001920 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 retval = -EFAULT;
1922 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001923 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925 }
1926 nr--;
1927 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001928 if (eol) {
1929 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001930 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001932 }
Ivo Sieben98001212013-01-28 13:32:01 +01001933 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Ivo Sieben98001212013-01-28 13:32:01 +01001935 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (retval)
1937 break;
1938 } else {
1939 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001940 /* The copy function takes the read lock and handles
1941 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 uncopied = copy_from_read_buf(tty, &b, &nr);
1943 uncopied += copy_from_read_buf(tty, &b, &nr);
1944 if (uncopied) {
1945 retval = -EFAULT;
1946 break;
1947 }
1948 }
1949
1950 /* If there is enough space in the read buffer now, let the
1951 * low-level driver know. We use n_tty_chars_in_buffer() to
1952 * check the buffer, as it now knows about canonical mode.
1953 * Otherwise, if the driver is throttled and the line is
1954 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1955 * we won't get any more characters.
1956 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001957 while (1) {
1958 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1959 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1960 break;
1961 if (!tty->count)
1962 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001963 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001964 if (!tty_unthrottle_safe(tty))
1965 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001966 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001967 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 if (b - buf >= minimum)
1970 break;
1971 if (time)
1972 timeout = time;
1973 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001974 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 remove_wait_queue(&tty->read_wait, &wait);
1976
1977 if (!waitqueue_active(&tty->read_wait))
1978 tty->minimum_to_wake = minimum;
1979
1980 __set_current_state(TASK_RUNNING);
1981 size = b - buf;
1982 if (size) {
1983 retval = size;
1984 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001985 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001987 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Linus Torvalds55db4c62011-06-04 06:33:24 +09001989 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return retval;
1991}
1992
1993/**
Alan Cox11a96d12008-10-13 10:46:24 +01001994 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * @tty: tty device
1996 * @file: file object
1997 * @buf: userspace buffer pointer
1998 * @nr: size of I/O
1999 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002000 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002002 * and other such events. Since the receive code will echo characters,
2003 * thus calling driver write methods, the output_lock is used in
2004 * the output processing functions called here as well as in the
2005 * echo processing function to protect the column state and space
2006 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 *
2008 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002009 *
2010 * Locking: output_lock to protect column state and space left
2011 * (note that the process_output*() functions take this
2012 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 */
Alan Cox4edf1822008-02-08 04:18:44 -08002014
Alan Cox11a96d12008-10-13 10:46:24 +01002015static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002016 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 const unsigned char *b = buf;
2019 DECLARE_WAITQUEUE(wait, current);
2020 int c;
2021 ssize_t retval = 0;
2022
2023 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2024 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2025 retval = tty_check_change(tty);
2026 if (retval)
2027 return retval;
2028 }
2029
Joe Petersona88a69c2009-01-02 13:40:53 +00002030 /* Write out any echoed characters that are still pending */
2031 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 add_wait_queue(&tty->write_wait, &wait);
2034 while (1) {
2035 set_current_state(TASK_INTERRUPTIBLE);
2036 if (signal_pending(current)) {
2037 retval = -ERESTARTSYS;
2038 break;
2039 }
2040 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2041 retval = -EIO;
2042 break;
2043 }
2044 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2045 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002046 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (num < 0) {
2048 if (num == -EAGAIN)
2049 break;
2050 retval = num;
2051 goto break_out;
2052 }
2053 b += num;
2054 nr -= num;
2055 if (nr == 0)
2056 break;
2057 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002058 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 break;
2060 b++; nr--;
2061 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002062 if (tty->ops->flush_chars)
2063 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002065 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002066 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002067 if (c < 0) {
2068 retval = c;
2069 goto break_out;
2070 }
2071 if (!c)
2072 break;
2073 b += c;
2074 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 if (!nr)
2078 break;
2079 if (file->f_flags & O_NONBLOCK) {
2080 retval = -EAGAIN;
2081 break;
2082 }
2083 schedule();
2084 }
2085break_out:
2086 __set_current_state(TASK_RUNNING);
2087 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002088 if (b - buf != nr && tty->fasync)
2089 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return (b - buf) ? b - buf : retval;
2091}
2092
2093/**
Alan Cox11a96d12008-10-13 10:46:24 +01002094 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * @tty: terminal device
2096 * @file: file accessing it
2097 * @wait: poll table
2098 *
2099 * Called when the line discipline is asked to poll() for data or
2100 * for special events. This code is not serialized with respect to
2101 * other events save open/close.
2102 *
2103 * This code must be sure never to sleep through a hangup.
2104 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 */
Alan Cox4edf1822008-02-08 04:18:44 -08002106
Alan Cox11a96d12008-10-13 10:46:24 +01002107static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002108 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 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))
2124 tty->minimum_to_wake = MIN_CHAR(tty);
2125 else
2126 tty->minimum_to_wake = 1;
2127 }
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
Alan Coxa352def2008-07-16 21:53:12 +01002174struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002175 .magic = TTY_LDISC_MAGIC,
2176 .name = "n_tty",
2177 .open = n_tty_open,
2178 .close = n_tty_close,
2179 .flush_buffer = n_tty_flush_buffer,
2180 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002181 .read = n_tty_read,
2182 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002183 .ioctl = n_tty_ioctl,
2184 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002185 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002186 .receive_buf = n_tty_receive_buf,
2187 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002189
2190/**
2191 * n_tty_inherit_ops - inherit N_TTY methods
2192 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2193 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002194 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002195 * methods.
2196 */
2197
2198void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2199{
2200 *ops = tty_ldisc_N_TTY;
2201 ops->owner = NULL;
2202 ops->refcount = ops->flags = 0;
2203}
2204EXPORT_SYMBOL_GPL(n_tty_inherit_ops);