blob: 6c7fe90ad72d48d2834536331e6826ee2719f94f [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 *
Peter Hurley25518c62013-03-11 16:44:31 -0400201 * Reset the read buffer counters and clear the flags.
202 * Called from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100203 *
204 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000206
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400207static void reset_buffer_flags(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long flags;
210
Ivo Sieben98001212013-01-28 13:32:01 +0100211 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200212 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100213 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000214
Jiri Slabybddc7152012-10-18 22:26:42 +0200215 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200216 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200217 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000218
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200219 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200220 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Peter Hurleya30737a2013-03-11 16:44:22 -0400223static void n_tty_packet_mode_flush(struct tty_struct *tty)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&tty->ctrl_lock, flags);
228 if (tty->link->packet) {
229 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
230 wake_up_interruptible(&tty->link->read_wait);
231 }
232 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/**
236 * n_tty_flush_buffer - clean input queue
237 * @tty: terminal device
238 *
Peter Hurley25518c62013-03-11 16:44:31 -0400239 * Flush the input buffer. Called when the tty layer wants the
240 * buffer flushed (eg at hangup) or when the N_TTY line discipline
241 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
Alan Cox17b82062008-10-13 10:45:06 +0100243 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Alan Cox4edf1822008-02-08 04:18:44 -0800245
246static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Peter Hurleyb66f4fa2013-03-11 16:44:32 -0400248 reset_buffer_flags(tty->disc_data);
249 n_tty_set_room(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800250
Peter Hurleya30737a2013-03-11 16:44:22 -0400251 if (tty->link)
252 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * n_tty_chars_in_buffer - report available bytes
257 * @tty: tty device
258 *
259 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800260 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100261 *
262 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Alan Cox4edf1822008-02-08 04:18:44 -0800264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
266{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200267 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned long flags;
269 ssize_t n = 0;
270
Ivo Sieben98001212013-01-28 13:32:01 +0100271 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200272 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200273 n = ldata->read_cnt;
274 } else if (ldata->canon_data) {
275 n = (ldata->canon_head > ldata->read_tail) ?
276 ldata->canon_head - ldata->read_tail :
277 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Ivo Sieben98001212013-01-28 13:32:01 +0100279 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return n;
281}
282
283/**
284 * is_utf8_continuation - utf8 multibyte check
285 * @c: byte to check
286 *
287 * Returns true if the utf8 character 'c' is a multibyte continuation
288 * character. We use this to correctly compute the on screen size
289 * of the character when printing
290 */
Alan Cox4edf1822008-02-08 04:18:44 -0800291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static inline int is_utf8_continuation(unsigned char c)
293{
294 return (c & 0xc0) == 0x80;
295}
296
297/**
298 * is_continuation - multibyte check
299 * @c: byte to check
300 *
301 * Returns true if the utf8 character 'c' is a multibyte continuation
302 * character and the terminal is in unicode mode.
303 */
Alan Cox4edf1822008-02-08 04:18:44 -0800304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static inline int is_continuation(unsigned char c, struct tty_struct *tty)
306{
307 return I_IUTF8(tty) && is_utf8_continuation(c);
308}
309
310/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000311 * do_output_char - output one character
312 * @c: character (or partial unicode symbol)
313 * @tty: terminal device
314 * @space: space available in tty driver write buffer
315 *
316 * This is a helper function that handles one output character
317 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600318 * doing OPOST processing and putting the results in the
319 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000320 *
321 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
322 * and NLDLY. They simply aren't relevant in the world today.
323 * If you ever need them, add them here.
324 *
325 * Returns the number of bytes of buffer space used or -1 if
326 * no space left.
327 *
328 * Locking: should be called under the output_lock to protect
329 * the column state and space left in the buffer
330 */
331
332static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
333{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200334 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000335 int spaces;
336
337 if (!space)
338 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000339
Joe Petersona88a69c2009-01-02 13:40:53 +0000340 switch (c) {
341 case '\n':
342 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200343 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000344 if (O_ONLCR(tty)) {
345 if (space < 2)
346 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200347 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700348 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000349 return 2;
350 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200351 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000352 break;
353 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200354 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000355 return 0;
356 if (O_OCRNL(tty)) {
357 c = '\n';
358 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200359 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 break;
361 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200362 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000363 break;
364 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200365 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000366 if (O_TABDLY(tty) == XTABS) {
367 if (space < spaces)
368 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200369 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000370 tty->ops->write(tty, " ", spaces);
371 return spaces;
372 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200373 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000374 break;
375 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200376 if (ldata->column > 0)
377 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 break;
379 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000380 if (!iscntrl(c)) {
381 if (O_OLCUC(tty))
382 c = toupper(c);
383 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200384 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000385 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 break;
387 }
388
389 tty_put_char(tty, c);
390 return 1;
391}
392
393/**
394 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @c: character (or partial unicode symbol)
396 * @tty: terminal device
397 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600398 * Output one character with OPOST processing.
399 * Returns -1 when the output device is full and the character
400 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000402 * Locking: output_lock to protect column state and space left
403 * (also, this is called from n_tty_write under the
404 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Alan Cox4edf1822008-02-08 04:18:44 -0800406
Joe Petersona88a69c2009-01-02 13:40:53 +0000407static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Jiri Slabybddc7152012-10-18 22:26:42 +0200409 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000410 int space, retval;
411
Jiri Slabybddc7152012-10-18 22:26:42 +0200412 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alan Coxf34d7a52008-04-30 00:54:13 -0700414 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000415 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jiri Slabybddc7152012-10-18 22:26:42 +0200417 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000418 if (retval < 0)
419 return -1;
420 else
421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000425 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600427 * @buf: character buffer
428 * @nr: number of bytes to output
429 *
430 * Output a block of characters with OPOST processing.
431 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
433 * This path is used to speed up block console writes, among other
434 * things when processing blocks of output data. It handles only
435 * the simple cases normally found and helps to generate blocks of
436 * symbols for the console driver and thus improve performance.
437 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000438 * Locking: output_lock to protect column state and space left
439 * (also, this is called from n_tty_write under the
440 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Alan Cox4edf1822008-02-08 04:18:44 -0800442
Joe Petersona88a69c2009-01-02 13:40:53 +0000443static ssize_t process_output_block(struct tty_struct *tty,
444 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200446 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100448 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 const unsigned char *cp;
450
Jiri Slabybddc7152012-10-18 22:26:42 +0200451 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000452
Alan Coxf34d7a52008-04-30 00:54:13 -0700453 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000454 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200455 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (nr > space)
459 nr = space;
460
461 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000462 unsigned char c = *cp;
463
464 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case '\n':
466 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200467 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (O_ONLCR(tty))
469 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200470 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200473 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto break_out;
475 if (O_OCRNL(tty))
476 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200477 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 case '\t':
480 goto break_out;
481 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200482 if (ldata->column > 0)
483 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000486 if (!iscntrl(c)) {
487 if (O_OLCUC(tty))
488 goto break_out;
489 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200490 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 }
494 }
495break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700496 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000497
Jiri Slabybddc7152012-10-18 22:26:42 +0200498 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return i;
500}
501
Joe Petersona88a69c2009-01-02 13:40:53 +0000502/**
503 * process_echoes - write pending echo characters
504 * @tty: terminal device
505 *
506 * Write previously buffered echo (and other ldisc-generated)
507 * characters to the tty.
508 *
509 * Characters generated by the ldisc (including echoes) need to
510 * be buffered because the driver's write buffer can fill during
511 * heavy program output. Echoing straight to the driver will
512 * often fail under these conditions, causing lost characters and
513 * resulting mismatches of ldisc state information.
514 *
515 * Since the ldisc state must represent the characters actually sent
516 * to the driver at the time of the write, operations like certain
517 * changes in column state are also saved in the buffer and executed
518 * here.
519 *
520 * A circular fifo buffer is used so that the most recent characters
521 * are prioritized. Also, when control characters are echoed with a
522 * prefixed "^", the pair is treated atomically and thus not separated.
523 *
524 * Locking: output_lock to protect column state and space left,
525 * echo_lock to protect the echo buffer
526 */
527
528static void process_echoes(struct tty_struct *tty)
529{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200530 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000531 int space, nr;
532 unsigned char c;
533 unsigned char *cp, *buf_end;
534
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200535 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000536 return;
537
Jiri Slabybddc7152012-10-18 22:26:42 +0200538 mutex_lock(&ldata->output_lock);
539 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000540
541 space = tty_write_room(tty);
542
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200543 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
544 cp = ldata->echo_buf + ldata->echo_pos;
545 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000546 while (nr > 0) {
547 c = *cp;
548 if (c == ECHO_OP_START) {
549 unsigned char op;
550 unsigned char *opp;
551 int no_space_left = 0;
552
553 /*
554 * If the buffer byte is the start of a multi-byte
555 * operation, get the next byte, which is either the
556 * op code or a control character value.
557 */
558 opp = cp + 1;
559 if (opp == buf_end)
560 opp -= N_TTY_BUF_SIZE;
561 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000562
Joe Petersona88a69c2009-01-02 13:40:53 +0000563 switch (op) {
564 unsigned int num_chars, num_bs;
565
566 case ECHO_OP_ERASE_TAB:
567 if (++opp == buf_end)
568 opp -= N_TTY_BUF_SIZE;
569 num_chars = *opp;
570
571 /*
572 * Determine how many columns to go back
573 * in order to erase the tab.
574 * This depends on the number of columns
575 * used by other characters within the tab
576 * area. If this (modulo 8) count is from
577 * the start of input rather than from a
578 * previous tab, we offset by canon column.
579 * Otherwise, tab spacing is normal.
580 */
581 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200582 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000583 num_bs = 8 - (num_chars & 7);
584
585 if (num_bs > space) {
586 no_space_left = 1;
587 break;
588 }
589 space -= num_bs;
590 while (num_bs--) {
591 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200592 if (ldata->column > 0)
593 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000594 }
595 cp += 3;
596 nr -= 3;
597 break;
598
599 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200600 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000601 cp += 2;
602 nr -= 2;
603 break;
604
605 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200606 if (ldata->column > 0)
607 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000608 cp += 2;
609 nr -= 2;
610 break;
611
612 case ECHO_OP_START:
613 /* This is an escaped echo op start code */
614 if (!space) {
615 no_space_left = 1;
616 break;
617 }
618 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200619 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000620 space--;
621 cp += 2;
622 nr -= 2;
623 break;
624
625 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000626 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600627 * If the op is not a special byte code,
628 * it is a ctrl char tagged to be echoed
629 * as "^X" (where X is the letter
630 * representing the control char).
631 * Note that we must ensure there is
632 * enough space for the whole ctrl pair.
633 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000634 */
Joe Peterson62b26352009-09-09 15:03:47 -0600635 if (space < 2) {
636 no_space_left = 1;
637 break;
638 }
639 tty_put_char(tty, '^');
640 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200641 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600642 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000643 cp += 2;
644 nr -= 2;
645 }
646
647 if (no_space_left)
648 break;
649 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600650 if (O_OPOST(tty) &&
651 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
652 int retval = do_output_char(c, tty, space);
653 if (retval < 0)
654 break;
655 space -= retval;
656 } else {
657 if (!space)
658 break;
659 tty_put_char(tty, c);
660 space -= 1;
661 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000662 cp += 1;
663 nr -= 1;
664 }
665
666 /* When end of circular buffer reached, wrap around */
667 if (cp >= buf_end)
668 cp -= N_TTY_BUF_SIZE;
669 }
670
671 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200672 ldata->echo_pos = 0;
673 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200674 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000675 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200676 int num_processed = ldata->echo_cnt - nr;
677 ldata->echo_pos += num_processed;
678 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
679 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200681 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000682 }
683
Jiri Slabybddc7152012-10-18 22:26:42 +0200684 mutex_unlock(&ldata->echo_lock);
685 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000686
687 if (tty->ops->flush_chars)
688 tty->ops->flush_chars(tty);
689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000692 * add_echo_byte - add a byte to the echo buffer
693 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200694 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000695 *
696 * Add a character or operation byte to the echo buffer.
697 *
698 * Should be called under the echo lock to protect the echo buffer.
699 */
700
Jiri Slaby57c94122012-10-18 22:26:43 +0200701static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000702{
703 int new_byte_pos;
704
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200705 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000706 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200707 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000708
709 /*
710 * Since the buffer start position needs to be advanced,
711 * be sure to step by a whole operation byte group.
712 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200713 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
714 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000715 (N_TTY_BUF_SIZE - 1)] ==
716 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200717 ldata->echo_pos += 3;
718 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000719 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200720 ldata->echo_pos += 2;
721 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000722 }
723 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200724 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000725 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200726 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200728 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000729 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200730 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000731 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200732 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000733 }
734
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200735 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000736}
737
738/**
739 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200740 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000741 *
742 * Add an operation to the echo buffer to move back one column.
743 *
744 * Locking: echo_lock to protect the echo buffer
745 */
746
Jiri Slaby57c94122012-10-18 22:26:43 +0200747static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000748{
Jiri Slabybddc7152012-10-18 22:26:42 +0200749 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200750 add_echo_byte(ECHO_OP_START, ldata);
751 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200752 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000753}
754
755/**
756 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200757 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000758 *
759 * Add an operation to the echo buffer to set the canon column
760 * to the current column.
761 *
762 * Locking: echo_lock to protect the echo buffer
763 */
764
Jiri Slaby57c94122012-10-18 22:26:43 +0200765static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000766{
Jiri Slabybddc7152012-10-18 22:26:42 +0200767 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200768 add_echo_byte(ECHO_OP_START, ldata);
769 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200770 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000771}
772
773/**
774 * echo_erase_tab - add operation to erase a tab
775 * @num_chars: number of character columns already used
776 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200777 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000778 *
779 * Add an operation to the echo buffer to erase a tab.
780 *
781 * Called by the eraser function, which knows how many character
782 * columns have been used since either a previous tab or the start
783 * of input. This information will be used later, along with
784 * canon column (if applicable), to go back the correct number
785 * of columns.
786 *
787 * Locking: echo_lock to protect the echo buffer
788 */
789
790static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200791 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000792{
Jiri Slabybddc7152012-10-18 22:26:42 +0200793 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000794
Jiri Slaby57c94122012-10-18 22:26:43 +0200795 add_echo_byte(ECHO_OP_START, ldata);
796 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000797
798 /* We only need to know this modulo 8 (tab spacing) */
799 num_chars &= 7;
800
801 /* Set the high bit as a flag if num_chars is after a previous tab */
802 if (after_tab)
803 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000804
Jiri Slaby57c94122012-10-18 22:26:43 +0200805 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000806
Jiri Slabybddc7152012-10-18 22:26:42 +0200807 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000808}
809
810/**
811 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * @c: unicode byte to echo
813 * @tty: terminal device
814 *
Alan Cox4edf1822008-02-08 04:18:44 -0800815 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100817 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000818 * This variant does not treat control characters specially.
819 *
820 * Locking: echo_lock to protect the echo buffer
821 */
822
Jiri Slaby57c94122012-10-18 22:26:43 +0200823static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000824{
Jiri Slabybddc7152012-10-18 22:26:42 +0200825 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000826 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200827 add_echo_byte(ECHO_OP_START, ldata);
828 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000829 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200830 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000831 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200832 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000833}
834
835/**
836 * echo_char - echo a character
837 * @c: unicode byte to echo
838 * @tty: terminal device
839 *
840 * Echo user input back onto the screen. This must be called only when
841 * L_ECHO(tty) is true. Called from the driver receive_buf path.
842 *
Joe Peterson62b26352009-09-09 15:03:47 -0600843 * This variant tags control characters to be echoed as "^X"
844 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000845 *
846 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
848
849static void echo_char(unsigned char c, struct tty_struct *tty)
850{
Jiri Slabybddc7152012-10-18 22:26:42 +0200851 struct n_tty_data *ldata = tty->disc_data;
852
853 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000854
855 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200856 add_echo_byte(ECHO_OP_START, ldata);
857 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000858 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600859 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200860 add_echo_byte(ECHO_OP_START, ldata);
861 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000862 }
863
Jiri Slabybddc7152012-10-18 22:26:42 +0200864 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Alan Cox17b82062008-10-13 10:45:06 +0100867/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000868 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200869 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100870 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000871
Jiri Slaby57c94122012-10-18 22:26:43 +0200872static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200874 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200875 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200876 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878}
879
880/**
881 * eraser - handle erase function
882 * @c: character input
883 * @tty: terminal device
884 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200885 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * present in the stream from the driver layer. Handles the complexities
887 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100888 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000889 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Alan Cox4edf1822008-02-08 04:18:44 -0800891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static void eraser(unsigned char c, struct tty_struct *tty)
893{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200894 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 enum { ERASE, WERASE, KILL } kill_type;
896 int head, seen_alnums, cnt;
897 unsigned long flags;
898
Alan Cox17b82062008-10-13 10:45:06 +0100899 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200900 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000901 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return;
903 }
904 if (c == ERASE_CHAR(tty))
905 kill_type = ERASE;
906 else if (c == WERASE_CHAR(tty))
907 kill_type = WERASE;
908 else {
909 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100910 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200911 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200913 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100914 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return;
916 }
917 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100918 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200919 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200921 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100922 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200923 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 echo_char(KILL_CHAR(tty), tty);
925 /* Add a newline if ECHOK is on and ECHOKE is off. */
926 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200927 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return;
929 }
930 kill_type = KILL;
931 }
932
933 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100934 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200935 while (ldata->read_head != ldata->canon_head) {
936 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* erase a single possibly multibyte character */
939 do {
940 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200941 c = ldata->read_buf[head];
942 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* do not partially erase */
945 if (is_continuation(c, tty))
946 break;
947
948 if (kill_type == WERASE) {
949 /* Equivalent to BSD's ALTWERASE. */
950 if (isalnum(c) || c == '_')
951 seen_alnums++;
952 else if (seen_alnums)
953 break;
954 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200955 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100956 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200957 ldata->read_head = head;
958 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100959 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (L_ECHO(tty)) {
961 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200962 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200963 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200964 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 /* if cnt > 1, output a multi-byte character */
967 echo_char(c, tty);
968 while (--cnt > 0) {
969 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200970 echo_char_raw(ldata->read_buf[head],
971 ldata);
972 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
975 echo_char(ERASE_CHAR(tty), tty);
976 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000977 unsigned int num_chars = 0;
978 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200979 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Joe Petersona88a69c2009-01-02 13:40:53 +0000981 /*
982 * Count the columns used for characters
983 * since the start of input or after a
984 * previous tab.
985 * This info is used to go back the correct
986 * number of columns.
987 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200988 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000989 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200990 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000991 if (c == '\t') {
992 after_tab = 1;
993 break;
Alan Cox300a6202009-01-02 13:41:04 +0000994 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000996 num_chars += 2;
997 } else if (!is_continuation(c, tty)) {
998 num_chars++;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001001 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 } else {
1003 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001004 echo_char_raw('\b', ldata);
1005 echo_char_raw(' ', ldata);
1006 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001009 echo_char_raw('\b', ldata);
1010 echo_char_raw(' ', ldata);
1011 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 }
1014 }
1015 if (kill_type == ERASE)
1016 break;
1017 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001018 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001019 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/**
1023 * isig - handle the ISIG optio
1024 * @sig: signal
1025 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001027 * Called when a signal is being sent due to terminal input.
1028 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001029 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001030 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Alan Cox4edf1822008-02-08 04:18:44 -08001032
Peter Hurley8c985d12013-03-06 08:38:19 -05001033static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Peter Hurley8c985d12013-03-06 08:38:19 -05001035 struct pid *tty_pgrp = tty_get_pgrp(tty);
1036 if (tty_pgrp) {
1037 kill_pgrp(tty_pgrp, sig, 1);
1038 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040}
1041
1042/**
1043 * n_tty_receive_break - handle break
1044 * @tty: terminal
1045 *
1046 * An RS232 break event has been hit in the incoming bitstream. This
1047 * can cause a variety of events depending upon the termios settings.
1048 *
1049 * Called from the receive_buf path so single threaded.
1050 */
Alan Cox4edf1822008-02-08 04:18:44 -08001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052static inline void n_tty_receive_break(struct tty_struct *tty)
1053{
Jiri Slaby57c94122012-10-18 22:26:43 +02001054 struct n_tty_data *ldata = tty->disc_data;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (I_IGNBRK(tty))
1057 return;
1058 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001059 isig(SIGINT, tty);
1060 if (!L_NOFLSH(tty)) {
1061 n_tty_flush_buffer(tty);
1062 tty_driver_flush_buffer(tty);
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return;
1065 }
1066 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001067 put_tty_queue('\377', ldata);
1068 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001070 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 wake_up_interruptible(&tty->read_wait);
1072}
1073
1074/**
1075 * n_tty_receive_overrun - handle overrun reporting
1076 * @tty: terminal
1077 *
1078 * Data arrived faster than we could process it. While the tty
1079 * driver has flagged this the bits that were missed are gone
1080 * forever.
1081 *
1082 * Called from the receive_buf path so single threaded. Does not
1083 * need locking as num_overrun and overrun_time are function
1084 * private.
1085 */
Alan Cox4edf1822008-02-08 04:18:44 -08001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static inline void n_tty_receive_overrun(struct tty_struct *tty)
1088{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001089 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 char buf[64];
1091
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001092 ldata->num_overrun++;
1093 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1094 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1096 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001097 ldata->num_overrun);
1098 ldata->overrun_time = jiffies;
1099 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101}
1102
1103/**
1104 * n_tty_receive_parity_error - error notifier
1105 * @tty: terminal device
1106 * @c: character
1107 *
1108 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001109 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1112 unsigned char c)
1113{
Jiri Slaby57c94122012-10-18 22:26:43 +02001114 struct n_tty_data *ldata = tty->disc_data;
1115
Alan Cox4edf1822008-02-08 04:18:44 -08001116 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001119 put_tty_queue('\377', ldata);
1120 put_tty_queue('\0', ldata);
1121 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001123 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001125 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 wake_up_interruptible(&tty->read_wait);
1127}
1128
1129/**
1130 * n_tty_receive_char - perform processing
1131 * @tty: terminal device
1132 * @c: character
1133 *
1134 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001135 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * driver above.
1137 */
1138
1139static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1140{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001141 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001143 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001145 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001146 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return;
1148 }
Alan Cox4edf1822008-02-08 04:18:44 -08001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (I_ISTRIP(tty))
1151 c &= 0x7f;
1152 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001153 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
hyc@symas.com26df6d12010-06-22 10:14:49 -07001155 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001156 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001157 return;
1158 }
1159
Joe Peterson54d2a372008-02-06 01:37:59 -08001160 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001161 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1162 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001163 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001164 process_echoes(tty);
1165 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (tty->closing) {
1168 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001169 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001171 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001172 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 stop_tty(tty);
1174 }
1175 return;
1176 }
1177
1178 /*
1179 * If the previous character was LNEXT, or we know that this
1180 * character is not one of the characters that we'll have to
1181 * handle specially, do shortcut processing to speed things
1182 * up.
1183 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001184 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001185 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001186 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001187 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001188 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001189 if (L_ECHO(tty))
1190 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001191 return;
1192 }
1193 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001194 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001196 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001197 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001199 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001201 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001202 put_tty_queue(c, ldata);
1203 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return;
1205 }
Alan Cox4edf1822008-02-08 04:18:44 -08001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (I_IXON(tty)) {
1208 if (c == START_CHAR(tty)) {
1209 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001210 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 }
1213 if (c == STOP_CHAR(tty)) {
1214 stop_tty(tty);
1215 return;
1216 }
1217 }
Joe Peterson575537b32008-04-30 00:53:30 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (L_ISIG(tty)) {
1220 int signal;
1221 signal = SIGINT;
1222 if (c == INTR_CHAR(tty))
1223 goto send_signal;
1224 signal = SIGQUIT;
1225 if (c == QUIT_CHAR(tty))
1226 goto send_signal;
1227 signal = SIGTSTP;
1228 if (c == SUSP_CHAR(tty)) {
1229send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001230 if (!L_NOFLSH(tty)) {
1231 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001232 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001233 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001234 if (I_IXON(tty))
1235 start_tty(tty);
1236 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001237 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001238 process_echoes(tty);
1239 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001240 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return;
1242 }
1243 }
Joe Peterson575537b32008-04-30 00:53:30 -07001244
1245 if (c == '\r') {
1246 if (I_IGNCR(tty))
1247 return;
1248 if (I_ICRNL(tty))
1249 c = '\n';
1250 } else if (c == '\n' && I_INLCR(tty))
1251 c = '\r';
1252
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001253 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1255 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1256 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001257 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return;
1259 }
1260 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001261 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001263 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001265 echo_char_raw('^', ldata);
1266 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001267 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
1270 return;
1271 }
1272 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1273 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001274 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jiri Slaby57c94122012-10-18 22:26:43 +02001276 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001278 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001279 while (tail != ldata->read_head) {
1280 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1282 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001283 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285 }
1286 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001287 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001288 if (L_ECHO(tty))
1289 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001290 return;
1291 }
1292 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001293 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001294 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 goto handle_newline;
1297 }
1298 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001299 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001300 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001301 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001302 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 c = __DISABLED_CHAR;
1304 goto handle_newline;
1305 }
1306 if ((c == EOL_CHAR(tty)) ||
1307 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001308 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1309 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001310 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001311 if (L_ECHO(tty))
1312 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001313 return;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1317 */
1318 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001320 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001321 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001323 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 /*
1326 * XXX does PARMRK doubling happen for
1327 * EOL_CHAR and EOL2_CHAR?
1328 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001329 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001330 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Alan Cox4edf1822008-02-08 04:18:44 -08001332handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001333 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001334 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001335 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001336 ldata->canon_head = ldata->read_head;
1337 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001338 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1340 if (waitqueue_active(&tty->read_wait))
1341 wake_up_interruptible(&tty->read_wait);
1342 return;
1343 }
1344 }
Alan Cox4edf1822008-02-08 04:18:44 -08001345
Joe Petersonacc71bb2009-01-02 13:43:32 +00001346 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001347 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001348 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001349 if (L_ECHO(tty))
1350 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001351 return;
1352 }
1353 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001354 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001356 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 else {
1358 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001359 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001360 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 echo_char(c, tty);
1362 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001363 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Joe Petersonacc71bb2009-01-02 13:43:32 +00001366 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001367 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Jiri Slaby57c94122012-10-18 22:26:43 +02001369 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373/**
1374 * n_tty_write_wakeup - asynchronous I/O notifier
1375 * @tty: tty device
1376 *
1377 * Required for the ptys, serial driver etc. since processes
1378 * that attach themselves to the master and rely on ASYNC
1379 * IO must be woken up
1380 */
1381
1382static void n_tty_write_wakeup(struct tty_struct *tty)
1383{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001384 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/**
1389 * n_tty_receive_buf - data receive
1390 * @tty: terminal device
1391 * @cp: buffer
1392 * @fp: flag buffer
1393 * @count: characters
1394 *
1395 * Called by the terminal driver when a block of characters has
1396 * been received. This function must be called from soft contexts
1397 * not from interrupt context. The driver is responsible for making
1398 * calls one at a time and in order (or using flush_to_ldisc)
1399 */
Alan Cox4edf1822008-02-08 04:18:44 -08001400
Linus Torvalds55db4c62011-06-04 06:33:24 +09001401static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1402 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001404 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 const unsigned char *p;
1406 char *f, flags = TTY_NORMAL;
1407 int i;
1408 char buf[64];
1409 unsigned long cpuflags;
1410
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001411 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001412 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001413 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1414 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001416 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1417 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1418 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 cp += i;
1420 count -= i;
1421
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001422 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1423 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001425 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1426 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1427 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001428 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001430 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (f)
1432 flags = *f++;
1433 switch (flags) {
1434 case TTY_NORMAL:
1435 n_tty_receive_char(tty, *p);
1436 break;
1437 case TTY_BREAK:
1438 n_tty_receive_break(tty);
1439 break;
1440 case TTY_PARITY:
1441 case TTY_FRAME:
1442 n_tty_receive_parity_error(tty, *p);
1443 break;
1444 case TTY_OVERRUN:
1445 n_tty_receive_overrun(tty);
1446 break;
1447 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001448 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 tty_name(tty, buf), flags);
1450 break;
1451 }
1452 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001453 if (tty->ops->flush_chars)
1454 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Linus Torvalds55db4c62011-06-04 06:33:24 +09001457 n_tty_set_room(tty);
1458
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001459 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001460 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1462 if (waitqueue_active(&tty->read_wait))
1463 wake_up_interruptible(&tty->read_wait);
1464 }
1465
1466 /*
1467 * Check the remaining room for the input canonicalization
1468 * mode. We don't want to throttle the driver if we're in
1469 * canonical mode and don't have a newline yet!
1470 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001471 while (1) {
1472 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1473 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1474 break;
1475 if (!tty_throttle_safe(tty))
1476 break;
1477 }
1478 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481int is_ignored(int sig)
1482{
1483 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001484 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/**
1488 * n_tty_set_termios - termios data changed
1489 * @tty: terminal
1490 * @old: previous data
1491 *
1492 * Called by the tty layer when the user changes termios flags so
1493 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001494 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * guaranteed that this function will not be re-entered or in progress
1496 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001497 *
1498 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Alan Cox4edf1822008-02-08 04:18:44 -08001500
1501static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001503 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001504 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001505
1506 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001507 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001508 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001509 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001510 ldata->canon_head = ldata->read_tail;
1511 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001512 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001513 }
1514
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001515 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001516 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001517
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001518 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001520 ldata->raw = 1;
1521 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001522 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return;
1524 }
1525 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1526 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1527 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1528 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001529 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001532 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001534 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001537 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1538 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1539 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1540 set_bit('\n', ldata->process_char_map);
1541 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (L_IEXTEN(tty)) {
1543 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001544 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001546 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (L_ECHO(tty))
1550 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001551 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 }
1554 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001555 set_bit(START_CHAR(tty), ldata->process_char_map);
1556 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1560 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1561 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001564 ldata->raw = 0;
1565 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001567 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1569 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1570 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001571 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001573 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001575 n_tty_set_room(tty);
Wang YanQingdab73b42013-05-09 14:16:47 +08001576 /*
1577 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1578 * been stopped by STOP_CHAR(tty) before it.
1579 */
1580 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1581 start_tty(tty);
1582 }
1583
Alan Coxf34d7a52008-04-30 00:54:13 -07001584 /* The termios change make the tty ready for I/O */
1585 wake_up_interruptible(&tty->write_wait);
1586 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/**
1590 * n_tty_close - close the ldisc for this tty
1591 * @tty: device
1592 *
Alan Cox4edf1822008-02-08 04:18:44 -08001593 * Called from the terminal layer when this line discipline is
1594 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * discipline change. The function will not be called while other
1596 * ldisc methods are in progress.
1597 */
Alan Cox4edf1822008-02-08 04:18:44 -08001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static void n_tty_close(struct tty_struct *tty)
1600{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001601 struct n_tty_data *ldata = tty->disc_data;
1602
Peter Hurley79901312013-03-11 16:44:23 -04001603 if (tty->link)
1604 n_tty_packet_mode_flush(tty);
1605
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001606 kfree(ldata->read_buf);
1607 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001608 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001609 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/**
1613 * n_tty_open - open an ldisc
1614 * @tty: terminal to open
1615 *
Alan Cox4edf1822008-02-08 04:18:44 -08001616 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * terminal device. Can sleep. Called serialized so that no
1618 * other events will occur in parallel. No further open will occur
1619 * until a close.
1620 */
1621
1622static int n_tty_open(struct tty_struct *tty)
1623{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001624 struct n_tty_data *ldata;
1625
1626 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1627 if (!ldata)
1628 goto err;
1629
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001630 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001631 mutex_init(&ldata->atomic_read_lock);
1632 mutex_init(&ldata->output_lock);
1633 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001634 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001635
Joe Petersona88a69c2009-01-02 13:40:53 +00001636 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001637 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1638 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1639 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001640 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001641
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001642 tty->disc_data = ldata;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001643 reset_buffer_flags(tty->disc_data);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001644 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 tty->minimum_to_wake = 1;
1646 tty->closing = 0;
Peter Hurleyb66f4fa2013-03-11 16:44:32 -04001647 /* indicate buffer work may resume */
1648 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1649 n_tty_set_termios(tty, NULL);
1650 tty_unthrottle(tty);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001653err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001654 kfree(ldata->read_buf);
1655 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001656 kfree(ldata);
1657err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001658 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661static inline int input_available_p(struct tty_struct *tty, int amt)
1662{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001663 struct n_tty_data *ldata = tty->disc_data;
1664
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001665 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001666 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001667 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001669 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 1;
1671
1672 return 0;
1673}
1674
1675/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001676 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * @tty: terminal device
1678 * @b: user data
1679 * @nr: size of data
1680 *
Alan Cox11a96d12008-10-13 10:46:24 +01001681 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * ICANON is off; it copies characters straight from the tty queue to
1683 * user space directly. It can be profitably called twice; once to
1684 * drain the space from the tail pointer to the (physical) end of the
1685 * buffer, and once to drain the space from the (physical) beginning of
1686 * the buffer to head pointer.
1687 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001688 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *
1690 */
Alan Cox4edf1822008-02-08 04:18:44 -08001691
Alan Cox33f0f882006-01-09 20:54:13 -08001692static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 unsigned char __user **b,
1694 size_t *nr)
1695
1696{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001697 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int retval;
1699 size_t n;
1700 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001701 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001704 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001705 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001707 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001709 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001711 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001712 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1713 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001714 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001715 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001716 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1717 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001718 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001719 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001720 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001721 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *b += n;
1723 *nr -= n;
1724 }
1725 return retval;
1726}
1727
Al Virocc4191d2008-03-29 03:08:48 +00001728extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001729 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731/**
1732 * job_control - check job control
1733 * @tty: tty
1734 * @file: file handle
1735 *
1736 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001737 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001739 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001740 * Locking: redirected write test is safe
1741 * current->signal->tty check is safe
1742 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Alan Cox4edf1822008-02-08 04:18:44 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int job_control(struct tty_struct *tty, struct file *file)
1746{
1747 /* Job control check -- must be done at start and after
1748 every sleep (POSIX.1 7.1.1.4). */
1749 /* NOTE: not yet done after every sleep pending a thorough
1750 check of the logic of this change. -- jlc */
1751 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001752 if (file->f_op->write == redirected_tty_write ||
1753 current->signal->tty != tty)
1754 return 0;
1755
1756 spin_lock_irq(&tty->ctrl_lock);
1757 if (!tty->pgrp)
1758 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1759 else if (task_pgrp(current) != tty->pgrp) {
1760 spin_unlock_irq(&tty->ctrl_lock);
1761 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1762 return -EIO;
1763 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1764 set_thread_flag(TIF_SIGPENDING);
1765 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001767 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769}
Alan Cox4edf1822008-02-08 04:18:44 -08001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/**
Alan Cox11a96d12008-10-13 10:46:24 +01001773 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * @tty: tty device
1775 * @file: file object
1776 * @buf: userspace buffer pointer
1777 * @nr: size of I/O
1778 *
1779 * Perform reads for the line discipline. We are guaranteed that the
1780 * line discipline will not be closed under us but we may get multiple
1781 * parallel readers and must handle this ourselves. We may also get
1782 * a hangup. Always called in user context, may sleep.
1783 *
1784 * This code must be sure never to sleep through a hangup.
1785 */
Alan Cox4edf1822008-02-08 04:18:44 -08001786
Alan Cox11a96d12008-10-13 10:46:24 +01001787static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned char __user *buf, size_t nr)
1789{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001790 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned char __user *b = buf;
1792 DECLARE_WAITQUEUE(wait, current);
1793 int c;
1794 int minimum, time;
1795 ssize_t retval = 0;
1796 ssize_t size;
1797 long timeout;
1798 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001799 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001803 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 minimum = time = 0;
1807 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001808 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 time = (HZ / 10) * TIME_CHAR(tty);
1810 minimum = MIN_CHAR(tty);
1811 if (minimum) {
1812 if (time)
1813 tty->minimum_to_wake = 1;
1814 else if (!waitqueue_active(&tty->read_wait) ||
1815 (tty->minimum_to_wake > minimum))
1816 tty->minimum_to_wake = minimum;
1817 } else {
1818 timeout = 0;
1819 if (time) {
1820 timeout = time;
1821 time = 0;
1822 }
1823 tty->minimum_to_wake = minimum = 1;
1824 }
1825 }
1826
1827 /*
1828 * Internal serialization of reads.
1829 */
1830 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001831 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001833 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001834 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -ERESTARTSYS;
1836 }
Alan Cox04f378b2008-04-30 00:53:29 -07001837 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 while (nr) {
1841 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001842 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 unsigned char cs;
1844 if (b != buf)
1845 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001846 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 cs = tty->link->ctrl_status;
1848 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001849 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001850 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 retval = -EFAULT;
1852 b--;
1853 break;
1854 }
1855 nr--;
1856 break;
1857 }
1858 /* This statement must be first before checking for input
1859 so that any interrupt will set the state back to
1860 TASK_RUNNING. */
1861 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1864 ((minimum - (b - buf)) >= 1))
1865 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!input_available_p(tty, 0)) {
1868 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1869 retval = -EIO;
1870 break;
1871 }
1872 if (tty_hung_up_p(file))
1873 break;
1874 if (!timeout)
1875 break;
1876 if (file->f_flags & O_NONBLOCK) {
1877 retval = -EAGAIN;
1878 break;
1879 }
1880 if (signal_pending(current)) {
1881 retval = -ERESTARTSYS;
1882 break;
1883 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001884 /* FIXME: does n_tty_set_room need locking ? */
1885 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 continue;
1888 }
1889 __set_current_state(TASK_RUNNING);
1890
1891 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001892 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001893 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 retval = -EFAULT;
1895 b--;
1896 break;
1897 }
1898 nr--;
1899 }
1900
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001901 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001903 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001904 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001905 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001907 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001908 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001909 c = ldata->read_buf[ldata->read_tail];
1910 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001912 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (eol) {
1914 /* this test should be redundant:
1915 * we shouldn't be reading data if
1916 * canon_data is 0
1917 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001918 if (--ldata->canon_data < 0)
1919 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Ivo Sieben98001212013-01-28 13:32:01 +01001921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001924 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 retval = -EFAULT;
1926 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929 }
1930 nr--;
1931 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001932 if (eol) {
1933 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001934 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001936 }
Ivo Sieben98001212013-01-28 13:32:01 +01001937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Ivo Sieben98001212013-01-28 13:32:01 +01001939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (retval)
1941 break;
1942 } else {
1943 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001944 /* The copy function takes the read lock and handles
1945 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 uncopied = copy_from_read_buf(tty, &b, &nr);
1947 uncopied += copy_from_read_buf(tty, &b, &nr);
1948 if (uncopied) {
1949 retval = -EFAULT;
1950 break;
1951 }
1952 }
1953
1954 /* If there is enough space in the read buffer now, let the
1955 * low-level driver know. We use n_tty_chars_in_buffer() to
1956 * check the buffer, as it now knows about canonical mode.
1957 * Otherwise, if the driver is throttled and the line is
1958 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1959 * we won't get any more characters.
1960 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001961 while (1) {
1962 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1963 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1964 break;
1965 if (!tty->count)
1966 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001967 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001968 if (!tty_unthrottle_safe(tty))
1969 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001970 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001971 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (b - buf >= minimum)
1974 break;
1975 if (time)
1976 timeout = time;
1977 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001978 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 remove_wait_queue(&tty->read_wait, &wait);
1980
1981 if (!waitqueue_active(&tty->read_wait))
1982 tty->minimum_to_wake = minimum;
1983
1984 __set_current_state(TASK_RUNNING);
1985 size = b - buf;
1986 if (size) {
1987 retval = size;
1988 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001989 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001991 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Linus Torvalds55db4c62011-06-04 06:33:24 +09001993 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return retval;
1995}
1996
1997/**
Alan Cox11a96d12008-10-13 10:46:24 +01001998 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * @tty: tty device
2000 * @file: file object
2001 * @buf: userspace buffer pointer
2002 * @nr: size of I/O
2003 *
Joe Petersona88a69c2009-01-02 13:40:53 +00002004 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002006 * and other such events. Since the receive code will echo characters,
2007 * thus calling driver write methods, the output_lock is used in
2008 * the output processing functions called here as well as in the
2009 * echo processing function to protect the column state and space
2010 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 *
2012 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002013 *
2014 * Locking: output_lock to protect column state and space left
2015 * (note that the process_output*() functions take this
2016 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Alan Cox4edf1822008-02-08 04:18:44 -08002018
Alan Cox11a96d12008-10-13 10:46:24 +01002019static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002020 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 const unsigned char *b = buf;
2023 DECLARE_WAITQUEUE(wait, current);
2024 int c;
2025 ssize_t retval = 0;
2026
2027 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2028 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2029 retval = tty_check_change(tty);
2030 if (retval)
2031 return retval;
2032 }
2033
Joe Petersona88a69c2009-01-02 13:40:53 +00002034 /* Write out any echoed characters that are still pending */
2035 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 add_wait_queue(&tty->write_wait, &wait);
2038 while (1) {
2039 set_current_state(TASK_INTERRUPTIBLE);
2040 if (signal_pending(current)) {
2041 retval = -ERESTARTSYS;
2042 break;
2043 }
2044 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2045 retval = -EIO;
2046 break;
2047 }
2048 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2049 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002050 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (num < 0) {
2052 if (num == -EAGAIN)
2053 break;
2054 retval = num;
2055 goto break_out;
2056 }
2057 b += num;
2058 nr -= num;
2059 if (nr == 0)
2060 break;
2061 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002062 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 break;
2064 b++; nr--;
2065 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002066 if (tty->ops->flush_chars)
2067 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002069 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002070 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002071 if (c < 0) {
2072 retval = c;
2073 goto break_out;
2074 }
2075 if (!c)
2076 break;
2077 b += c;
2078 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 if (!nr)
2082 break;
2083 if (file->f_flags & O_NONBLOCK) {
2084 retval = -EAGAIN;
2085 break;
2086 }
2087 schedule();
2088 }
2089break_out:
2090 __set_current_state(TASK_RUNNING);
2091 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002092 if (b - buf != nr && tty->fasync)
2093 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return (b - buf) ? b - buf : retval;
2095}
2096
2097/**
Alan Cox11a96d12008-10-13 10:46:24 +01002098 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * @tty: terminal device
2100 * @file: file accessing it
2101 * @wait: poll table
2102 *
2103 * Called when the line discipline is asked to poll() for data or
2104 * for special events. This code is not serialized with respect to
2105 * other events save open/close.
2106 *
2107 * This code must be sure never to sleep through a hangup.
2108 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Alan Cox4edf1822008-02-08 04:18:44 -08002110
Alan Cox11a96d12008-10-13 10:46:24 +01002111static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002112 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 unsigned int mask = 0;
2115
2116 poll_wait(file, &tty->read_wait, wait);
2117 poll_wait(file, &tty->write_wait, wait);
2118 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2119 mask |= POLLIN | POLLRDNORM;
2120 if (tty->packet && tty->link->ctrl_status)
2121 mask |= POLLPRI | POLLIN | POLLRDNORM;
2122 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2123 mask |= POLLHUP;
2124 if (tty_hung_up_p(file))
2125 mask |= POLLHUP;
2126 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2127 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2128 tty->minimum_to_wake = MIN_CHAR(tty);
2129 else
2130 tty->minimum_to_wake = 1;
2131 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002132 if (tty->ops->write && !tty_is_writelocked(tty) &&
2133 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2134 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 mask |= POLLOUT | POLLWRNORM;
2136 return mask;
2137}
2138
Jiri Slaby57c94122012-10-18 22:26:43 +02002139static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002140{
2141 int nr, head, tail;
2142
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002143 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002144 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002145 head = ldata->canon_head;
2146 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002147 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2148 /* Skip EOF-chars.. */
2149 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002150 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002151 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002152 nr--;
2153 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2154 }
2155 return nr;
2156}
2157
2158static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2159 unsigned int cmd, unsigned long arg)
2160{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002161 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002162 int retval;
2163
2164 switch (cmd) {
2165 case TIOCOUTQ:
2166 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2167 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002168 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002169 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002170 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002171 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002172 return put_user(retval, (unsigned int __user *) arg);
2173 default:
2174 return n_tty_ioctl_helper(tty, file, cmd, arg);
2175 }
2176}
2177
Alan Coxa352def2008-07-16 21:53:12 +01002178struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002179 .magic = TTY_LDISC_MAGIC,
2180 .name = "n_tty",
2181 .open = n_tty_open,
2182 .close = n_tty_close,
2183 .flush_buffer = n_tty_flush_buffer,
2184 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002185 .read = n_tty_read,
2186 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002187 .ioctl = n_tty_ioctl,
2188 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002189 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002190 .receive_buf = n_tty_receive_buf,
2191 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002193
2194/**
2195 * n_tty_inherit_ops - inherit N_TTY methods
2196 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2197 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002198 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002199 * methods.
2200 */
2201
2202void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2203{
2204 *ops = tty_ldisc_N_TTY;
2205 ops->owner = NULL;
2206 ops->refcount = ops->flags = 0;
2207}
2208EXPORT_SYMBOL_GPL(n_tty_inherit_ops);