blob: 0d3f715de7d2c990416ebcdf7850977ce5a6b914 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static void reset_buffer_flags(struct tty_struct *tty)
208{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200209 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long flags;
211
Ivo Sieben98001212013-01-28 13:32:01 +0100212 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200213 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100214 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000215
Jiri Slabybddc7152012-10-18 22:26:42 +0200216 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200217 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200218 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000219
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200220 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200221 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900222 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Peter Hurleya30737a2013-03-11 16:44:22 -0400225static void n_tty_packet_mode_flush(struct tty_struct *tty)
226{
227 unsigned long flags;
228
229 spin_lock_irqsave(&tty->ctrl_lock, flags);
230 if (tty->link->packet) {
231 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
232 wake_up_interruptible(&tty->link->read_wait);
233 }
234 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * n_tty_flush_buffer - clean input queue
239 * @tty: terminal device
240 *
Peter Hurley25518c62013-03-11 16:44:31 -0400241 * Flush the input buffer. Called when the tty layer wants the
242 * buffer flushed (eg at hangup) or when the N_TTY line discipline
243 * internally has to clean the pending queue (for example some signals).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
Alan Cox17b82062008-10-13 10:45:06 +0100245 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Alan Cox4edf1822008-02-08 04:18:44 -0800247
248static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800251
Peter Hurleya30737a2013-03-11 16:44:22 -0400252 if (tty->link)
253 n_tty_packet_mode_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
257 * n_tty_chars_in_buffer - report available bytes
258 * @tty: tty device
259 *
260 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800261 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100262 *
263 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Alan Cox4edf1822008-02-08 04:18:44 -0800265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
267{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200268 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 unsigned long flags;
270 ssize_t n = 0;
271
Ivo Sieben98001212013-01-28 13:32:01 +0100272 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200273 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200274 n = ldata->read_cnt;
275 } else if (ldata->canon_data) {
276 n = (ldata->canon_head > ldata->read_tail) ?
277 ldata->canon_head - ldata->read_tail :
278 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Ivo Sieben98001212013-01-28 13:32:01 +0100280 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return n;
282}
283
284/**
285 * is_utf8_continuation - utf8 multibyte check
286 * @c: byte to check
287 *
288 * Returns true if the utf8 character 'c' is a multibyte continuation
289 * character. We use this to correctly compute the on screen size
290 * of the character when printing
291 */
Alan Cox4edf1822008-02-08 04:18:44 -0800292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static inline int is_utf8_continuation(unsigned char c)
294{
295 return (c & 0xc0) == 0x80;
296}
297
298/**
299 * is_continuation - multibyte check
300 * @c: byte to check
301 *
302 * Returns true if the utf8 character 'c' is a multibyte continuation
303 * character and the terminal is in unicode mode.
304 */
Alan Cox4edf1822008-02-08 04:18:44 -0800305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static inline int is_continuation(unsigned char c, struct tty_struct *tty)
307{
308 return I_IUTF8(tty) && is_utf8_continuation(c);
309}
310
311/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000312 * do_output_char - output one character
313 * @c: character (or partial unicode symbol)
314 * @tty: terminal device
315 * @space: space available in tty driver write buffer
316 *
317 * This is a helper function that handles one output character
318 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600319 * doing OPOST processing and putting the results in the
320 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000321 *
322 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
323 * and NLDLY. They simply aren't relevant in the world today.
324 * If you ever need them, add them here.
325 *
326 * Returns the number of bytes of buffer space used or -1 if
327 * no space left.
328 *
329 * Locking: should be called under the output_lock to protect
330 * the column state and space left in the buffer
331 */
332
333static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
334{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200335 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000336 int spaces;
337
338 if (!space)
339 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000340
Joe Petersona88a69c2009-01-02 13:40:53 +0000341 switch (c) {
342 case '\n':
343 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200344 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000345 if (O_ONLCR(tty)) {
346 if (space < 2)
347 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200348 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700349 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000350 return 2;
351 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200352 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000353 break;
354 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200355 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000356 return 0;
357 if (O_OCRNL(tty)) {
358 c = '\n';
359 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200360 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000361 break;
362 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200363 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000364 break;
365 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200366 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000367 if (O_TABDLY(tty) == XTABS) {
368 if (space < spaces)
369 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200370 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000371 tty->ops->write(tty, " ", spaces);
372 return spaces;
373 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200374 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 break;
376 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200377 if (ldata->column > 0)
378 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000379 break;
380 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000381 if (!iscntrl(c)) {
382 if (O_OLCUC(tty))
383 c = toupper(c);
384 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200385 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000386 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000387 break;
388 }
389
390 tty_put_char(tty, c);
391 return 1;
392}
393
394/**
395 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * @c: character (or partial unicode symbol)
397 * @tty: terminal device
398 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600399 * Output one character with OPOST processing.
400 * Returns -1 when the output device is full and the character
401 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000403 * Locking: output_lock to protect column state and space left
404 * (also, this is called from n_tty_write under the
405 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Alan Cox4edf1822008-02-08 04:18:44 -0800407
Joe Petersona88a69c2009-01-02 13:40:53 +0000408static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jiri Slabybddc7152012-10-18 22:26:42 +0200410 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000411 int space, retval;
412
Jiri Slabybddc7152012-10-18 22:26:42 +0200413 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alan Coxf34d7a52008-04-30 00:54:13 -0700415 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000416 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jiri Slabybddc7152012-10-18 22:26:42 +0200418 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000419 if (retval < 0)
420 return -1;
421 else
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600428 * @buf: character buffer
429 * @nr: number of bytes to output
430 *
431 * Output a block of characters with OPOST processing.
432 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *
434 * This path is used to speed up block console writes, among other
435 * things when processing blocks of output data. It handles only
436 * the simple cases normally found and helps to generate blocks of
437 * symbols for the console driver and thus improve performance.
438 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000439 * Locking: output_lock to protect column state and space left
440 * (also, this is called from n_tty_write under the
441 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Alan Cox4edf1822008-02-08 04:18:44 -0800443
Joe Petersona88a69c2009-01-02 13:40:53 +0000444static ssize_t process_output_block(struct tty_struct *tty,
445 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200447 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100449 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 const unsigned char *cp;
451
Jiri Slabybddc7152012-10-18 22:26:42 +0200452 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000453
Alan Coxf34d7a52008-04-30 00:54:13 -0700454 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000455 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200456 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (nr > space)
460 nr = space;
461
462 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000463 unsigned char c = *cp;
464
465 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case '\n':
467 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200468 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (O_ONLCR(tty))
470 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200471 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200474 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto break_out;
476 if (O_OCRNL(tty))
477 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200478 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case '\t':
481 goto break_out;
482 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200483 if (ldata->column > 0)
484 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000487 if (!iscntrl(c)) {
488 if (O_OLCUC(tty))
489 goto break_out;
490 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200491 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 }
495 }
496break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700497 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000498
Jiri Slabybddc7152012-10-18 22:26:42 +0200499 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return i;
501}
502
Joe Petersona88a69c2009-01-02 13:40:53 +0000503/**
504 * process_echoes - write pending echo characters
505 * @tty: terminal device
506 *
507 * Write previously buffered echo (and other ldisc-generated)
508 * characters to the tty.
509 *
510 * Characters generated by the ldisc (including echoes) need to
511 * be buffered because the driver's write buffer can fill during
512 * heavy program output. Echoing straight to the driver will
513 * often fail under these conditions, causing lost characters and
514 * resulting mismatches of ldisc state information.
515 *
516 * Since the ldisc state must represent the characters actually sent
517 * to the driver at the time of the write, operations like certain
518 * changes in column state are also saved in the buffer and executed
519 * here.
520 *
521 * A circular fifo buffer is used so that the most recent characters
522 * are prioritized. Also, when control characters are echoed with a
523 * prefixed "^", the pair is treated atomically and thus not separated.
524 *
525 * Locking: output_lock to protect column state and space left,
526 * echo_lock to protect the echo buffer
527 */
528
529static void process_echoes(struct tty_struct *tty)
530{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200531 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000532 int space, nr;
533 unsigned char c;
534 unsigned char *cp, *buf_end;
535
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200536 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000537 return;
538
Jiri Slabybddc7152012-10-18 22:26:42 +0200539 mutex_lock(&ldata->output_lock);
540 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000541
542 space = tty_write_room(tty);
543
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200544 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
545 cp = ldata->echo_buf + ldata->echo_pos;
546 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000547 while (nr > 0) {
548 c = *cp;
549 if (c == ECHO_OP_START) {
550 unsigned char op;
551 unsigned char *opp;
552 int no_space_left = 0;
553
554 /*
555 * If the buffer byte is the start of a multi-byte
556 * operation, get the next byte, which is either the
557 * op code or a control character value.
558 */
559 opp = cp + 1;
560 if (opp == buf_end)
561 opp -= N_TTY_BUF_SIZE;
562 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000563
Joe Petersona88a69c2009-01-02 13:40:53 +0000564 switch (op) {
565 unsigned int num_chars, num_bs;
566
567 case ECHO_OP_ERASE_TAB:
568 if (++opp == buf_end)
569 opp -= N_TTY_BUF_SIZE;
570 num_chars = *opp;
571
572 /*
573 * Determine how many columns to go back
574 * in order to erase the tab.
575 * This depends on the number of columns
576 * used by other characters within the tab
577 * area. If this (modulo 8) count is from
578 * the start of input rather than from a
579 * previous tab, we offset by canon column.
580 * Otherwise, tab spacing is normal.
581 */
582 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200583 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000584 num_bs = 8 - (num_chars & 7);
585
586 if (num_bs > space) {
587 no_space_left = 1;
588 break;
589 }
590 space -= num_bs;
591 while (num_bs--) {
592 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200593 if (ldata->column > 0)
594 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000595 }
596 cp += 3;
597 nr -= 3;
598 break;
599
600 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200601 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000602 cp += 2;
603 nr -= 2;
604 break;
605
606 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200607 if (ldata->column > 0)
608 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 cp += 2;
610 nr -= 2;
611 break;
612
613 case ECHO_OP_START:
614 /* This is an escaped echo op start code */
615 if (!space) {
616 no_space_left = 1;
617 break;
618 }
619 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200620 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000621 space--;
622 cp += 2;
623 nr -= 2;
624 break;
625
626 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000627 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600628 * If the op is not a special byte code,
629 * it is a ctrl char tagged to be echoed
630 * as "^X" (where X is the letter
631 * representing the control char).
632 * Note that we must ensure there is
633 * enough space for the whole ctrl pair.
634 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000635 */
Joe Peterson62b26352009-09-09 15:03:47 -0600636 if (space < 2) {
637 no_space_left = 1;
638 break;
639 }
640 tty_put_char(tty, '^');
641 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200642 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600643 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000644 cp += 2;
645 nr -= 2;
646 }
647
648 if (no_space_left)
649 break;
650 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600651 if (O_OPOST(tty) &&
652 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
653 int retval = do_output_char(c, tty, space);
654 if (retval < 0)
655 break;
656 space -= retval;
657 } else {
658 if (!space)
659 break;
660 tty_put_char(tty, c);
661 space -= 1;
662 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000663 cp += 1;
664 nr -= 1;
665 }
666
667 /* When end of circular buffer reached, wrap around */
668 if (cp >= buf_end)
669 cp -= N_TTY_BUF_SIZE;
670 }
671
672 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200673 ldata->echo_pos = 0;
674 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200675 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000676 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200677 int num_processed = ldata->echo_cnt - nr;
678 ldata->echo_pos += num_processed;
679 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
680 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200682 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000683 }
684
Jiri Slabybddc7152012-10-18 22:26:42 +0200685 mutex_unlock(&ldata->echo_lock);
686 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000687
688 if (tty->ops->flush_chars)
689 tty->ops->flush_chars(tty);
690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 * add_echo_byte - add a byte to the echo buffer
694 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200695 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000696 *
697 * Add a character or operation byte to the echo buffer.
698 *
699 * Should be called under the echo lock to protect the echo buffer.
700 */
701
Jiri Slaby57c94122012-10-18 22:26:43 +0200702static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000703{
704 int new_byte_pos;
705
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200706 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000707 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200708 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000709
710 /*
711 * Since the buffer start position needs to be advanced,
712 * be sure to step by a whole operation byte group.
713 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200714 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
715 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000716 (N_TTY_BUF_SIZE - 1)] ==
717 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200718 ldata->echo_pos += 3;
719 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200721 ldata->echo_pos += 2;
722 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000723 }
724 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200725 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000726 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200727 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000728
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200729 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000730 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200731 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000732 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200733 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000734 }
735
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200736 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737}
738
739/**
740 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200741 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000742 *
743 * Add an operation to the echo buffer to move back one column.
744 *
745 * Locking: echo_lock to protect the echo buffer
746 */
747
Jiri Slaby57c94122012-10-18 22:26:43 +0200748static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000749{
Jiri Slabybddc7152012-10-18 22:26:42 +0200750 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200751 add_echo_byte(ECHO_OP_START, ldata);
752 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200753 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000754}
755
756/**
757 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200758 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000759 *
760 * Add an operation to the echo buffer to set the canon column
761 * to the current column.
762 *
763 * Locking: echo_lock to protect the echo buffer
764 */
765
Jiri Slaby57c94122012-10-18 22:26:43 +0200766static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000767{
Jiri Slabybddc7152012-10-18 22:26:42 +0200768 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200769 add_echo_byte(ECHO_OP_START, ldata);
770 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200771 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000772}
773
774/**
775 * echo_erase_tab - add operation to erase a tab
776 * @num_chars: number of character columns already used
777 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200778 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000779 *
780 * Add an operation to the echo buffer to erase a tab.
781 *
782 * Called by the eraser function, which knows how many character
783 * columns have been used since either a previous tab or the start
784 * of input. This information will be used later, along with
785 * canon column (if applicable), to go back the correct number
786 * of columns.
787 *
788 * Locking: echo_lock to protect the echo buffer
789 */
790
791static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200792 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000793{
Jiri Slabybddc7152012-10-18 22:26:42 +0200794 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000795
Jiri Slaby57c94122012-10-18 22:26:43 +0200796 add_echo_byte(ECHO_OP_START, ldata);
797 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000798
799 /* We only need to know this modulo 8 (tab spacing) */
800 num_chars &= 7;
801
802 /* Set the high bit as a flag if num_chars is after a previous tab */
803 if (after_tab)
804 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000805
Jiri Slaby57c94122012-10-18 22:26:43 +0200806 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000807
Jiri Slabybddc7152012-10-18 22:26:42 +0200808 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000809}
810
811/**
812 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * @c: unicode byte to echo
814 * @tty: terminal device
815 *
Alan Cox4edf1822008-02-08 04:18:44 -0800816 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100818 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000819 * This variant does not treat control characters specially.
820 *
821 * Locking: echo_lock to protect the echo buffer
822 */
823
Jiri Slaby57c94122012-10-18 22:26:43 +0200824static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000825{
Jiri Slabybddc7152012-10-18 22:26:42 +0200826 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000827 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200828 add_echo_byte(ECHO_OP_START, ldata);
829 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000830 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200831 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000832 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200833 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000834}
835
836/**
837 * echo_char - echo a character
838 * @c: unicode byte to echo
839 * @tty: terminal device
840 *
841 * Echo user input back onto the screen. This must be called only when
842 * L_ECHO(tty) is true. Called from the driver receive_buf path.
843 *
Joe Peterson62b26352009-09-09 15:03:47 -0600844 * This variant tags control characters to be echoed as "^X"
845 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000846 *
847 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849
850static void echo_char(unsigned char c, struct tty_struct *tty)
851{
Jiri Slabybddc7152012-10-18 22:26:42 +0200852 struct n_tty_data *ldata = tty->disc_data;
853
854 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000855
856 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200857 add_echo_byte(ECHO_OP_START, ldata);
858 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000859 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600860 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000863 }
864
Jiri Slabybddc7152012-10-18 22:26:42 +0200865 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Alan Cox17b82062008-10-13 10:45:06 +0100868/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000869 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200870 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100871 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000872
Jiri Slaby57c94122012-10-18 22:26:43 +0200873static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200875 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200876 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200877 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879}
880
881/**
882 * eraser - handle erase function
883 * @c: character input
884 * @tty: terminal device
885 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200886 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * present in the stream from the driver layer. Handles the complexities
888 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100889 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000890 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Alan Cox4edf1822008-02-08 04:18:44 -0800892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static void eraser(unsigned char c, struct tty_struct *tty)
894{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200895 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 enum { ERASE, WERASE, KILL } kill_type;
897 int head, seen_alnums, cnt;
898 unsigned long flags;
899
Alan Cox17b82062008-10-13 10:45:06 +0100900 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200901 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000902 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return;
904 }
905 if (c == ERASE_CHAR(tty))
906 kill_type = ERASE;
907 else if (c == WERASE_CHAR(tty))
908 kill_type = WERASE;
909 else {
910 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100911 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200912 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200914 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100915 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return;
917 }
918 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100919 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200920 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200922 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100923 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200924 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 echo_char(KILL_CHAR(tty), tty);
926 /* Add a newline if ECHOK is on and ECHOKE is off. */
927 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200928 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return;
930 }
931 kill_type = KILL;
932 }
933
934 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100935 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200936 while (ldata->read_head != ldata->canon_head) {
937 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* erase a single possibly multibyte character */
940 do {
941 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200942 c = ldata->read_buf[head];
943 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* do not partially erase */
946 if (is_continuation(c, tty))
947 break;
948
949 if (kill_type == WERASE) {
950 /* Equivalent to BSD's ALTWERASE. */
951 if (isalnum(c) || c == '_')
952 seen_alnums++;
953 else if (seen_alnums)
954 break;
955 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200956 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100957 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200958 ldata->read_head = head;
959 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100960 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (L_ECHO(tty)) {
962 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200963 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200964 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200965 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 /* if cnt > 1, output a multi-byte character */
968 echo_char(c, tty);
969 while (--cnt > 0) {
970 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200971 echo_char_raw(ldata->read_buf[head],
972 ldata);
973 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
976 echo_char(ERASE_CHAR(tty), tty);
977 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000978 unsigned int num_chars = 0;
979 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200980 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Joe Petersona88a69c2009-01-02 13:40:53 +0000982 /*
983 * Count the columns used for characters
984 * since the start of input or after a
985 * previous tab.
986 * This info is used to go back the correct
987 * number of columns.
988 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200989 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000990 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200991 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000992 if (c == '\t') {
993 after_tab = 1;
994 break;
Alan Cox300a6202009-01-02 13:41:04 +0000995 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000997 num_chars += 2;
998 } else if (!is_continuation(c, tty)) {
999 num_chars++;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001002 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else {
1004 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001005 echo_char_raw('\b', ldata);
1006 echo_char_raw(' ', ldata);
1007 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001010 echo_char_raw('\b', ldata);
1011 echo_char_raw(' ', ldata);
1012 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 }
1015 }
1016 if (kill_type == ERASE)
1017 break;
1018 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001019 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001020 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023/**
1024 * isig - handle the ISIG optio
1025 * @sig: signal
1026 * @tty: terminal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001028 * Called when a signal is being sent due to terminal input.
1029 * Called from the driver receive_buf path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001030 *
Peter Hurley8c985d12013-03-06 08:38:19 -05001031 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
Alan Cox4edf1822008-02-08 04:18:44 -08001033
Peter Hurley8c985d12013-03-06 08:38:19 -05001034static inline void isig(int sig, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Peter Hurley8c985d12013-03-06 08:38:19 -05001036 struct pid *tty_pgrp = tty_get_pgrp(tty);
1037 if (tty_pgrp) {
1038 kill_pgrp(tty_pgrp, sig, 1);
1039 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041}
1042
1043/**
1044 * n_tty_receive_break - handle break
1045 * @tty: terminal
1046 *
1047 * An RS232 break event has been hit in the incoming bitstream. This
1048 * can cause a variety of events depending upon the termios settings.
1049 *
1050 * Called from the receive_buf path so single threaded.
1051 */
Alan Cox4edf1822008-02-08 04:18:44 -08001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static inline void n_tty_receive_break(struct tty_struct *tty)
1054{
Jiri Slaby57c94122012-10-18 22:26:43 +02001055 struct n_tty_data *ldata = tty->disc_data;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (I_IGNBRK(tty))
1058 return;
1059 if (I_BRKINT(tty)) {
Peter Hurley8c985d12013-03-06 08:38:19 -05001060 isig(SIGINT, tty);
1061 if (!L_NOFLSH(tty)) {
1062 n_tty_flush_buffer(tty);
1063 tty_driver_flush_buffer(tty);
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return;
1066 }
1067 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001068 put_tty_queue('\377', ldata);
1069 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001071 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 wake_up_interruptible(&tty->read_wait);
1073}
1074
1075/**
1076 * n_tty_receive_overrun - handle overrun reporting
1077 * @tty: terminal
1078 *
1079 * Data arrived faster than we could process it. While the tty
1080 * driver has flagged this the bits that were missed are gone
1081 * forever.
1082 *
1083 * Called from the receive_buf path so single threaded. Does not
1084 * need locking as num_overrun and overrun_time are function
1085 * private.
1086 */
Alan Cox4edf1822008-02-08 04:18:44 -08001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static inline void n_tty_receive_overrun(struct tty_struct *tty)
1089{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001090 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 char buf[64];
1092
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001093 ldata->num_overrun++;
1094 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1095 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1097 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001098 ldata->num_overrun);
1099 ldata->overrun_time = jiffies;
1100 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102}
1103
1104/**
1105 * n_tty_receive_parity_error - error notifier
1106 * @tty: terminal device
1107 * @c: character
1108 *
1109 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001110 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
1112static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1113 unsigned char c)
1114{
Jiri Slaby57c94122012-10-18 22:26:43 +02001115 struct n_tty_data *ldata = tty->disc_data;
1116
Alan Cox4edf1822008-02-08 04:18:44 -08001117 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001120 put_tty_queue('\377', ldata);
1121 put_tty_queue('\0', ldata);
1122 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001124 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001126 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 wake_up_interruptible(&tty->read_wait);
1128}
1129
1130/**
1131 * n_tty_receive_char - perform processing
1132 * @tty: terminal device
1133 * @c: character
1134 *
1135 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001136 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * driver above.
1138 */
1139
1140static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1141{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001142 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001144 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001146 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001147 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return;
1149 }
Alan Cox4edf1822008-02-08 04:18:44 -08001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (I_ISTRIP(tty))
1152 c &= 0x7f;
1153 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001154 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
hyc@symas.com26df6d12010-06-22 10:14:49 -07001156 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001157 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001158 return;
1159 }
1160
Joe Peterson54d2a372008-02-06 01:37:59 -08001161 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001162 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1163 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001164 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001165 process_echoes(tty);
1166 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (tty->closing) {
1169 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001170 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001172 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001173 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 stop_tty(tty);
1175 }
1176 return;
1177 }
1178
1179 /*
1180 * If the previous character was LNEXT, or we know that this
1181 * character is not one of the characters that we'll have to
1182 * handle specially, do shortcut processing to speed things
1183 * up.
1184 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001185 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001186 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001187 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001188 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001189 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001190 if (L_ECHO(tty))
1191 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001192 return;
1193 }
1194 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001195 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001197 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001198 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001200 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001202 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001203 put_tty_queue(c, ldata);
1204 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
1206 }
Alan Cox4edf1822008-02-08 04:18:44 -08001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (I_IXON(tty)) {
1209 if (c == START_CHAR(tty)) {
1210 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001211 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return;
1213 }
1214 if (c == STOP_CHAR(tty)) {
1215 stop_tty(tty);
1216 return;
1217 }
1218 }
Joe Peterson575537b32008-04-30 00:53:30 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (L_ISIG(tty)) {
1221 int signal;
1222 signal = SIGINT;
1223 if (c == INTR_CHAR(tty))
1224 goto send_signal;
1225 signal = SIGQUIT;
1226 if (c == QUIT_CHAR(tty))
1227 goto send_signal;
1228 signal = SIGTSTP;
1229 if (c == SUSP_CHAR(tty)) {
1230send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001231 if (!L_NOFLSH(tty)) {
1232 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001233 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001234 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001235 if (I_IXON(tty))
1236 start_tty(tty);
1237 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001238 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001239 process_echoes(tty);
1240 }
Peter Hurley8c985d12013-03-06 08:38:19 -05001241 isig(signal, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
1243 }
1244 }
Joe Peterson575537b32008-04-30 00:53:30 -07001245
1246 if (c == '\r') {
1247 if (I_IGNCR(tty))
1248 return;
1249 if (I_ICRNL(tty))
1250 c = '\n';
1251 } else if (c == '\n' && I_INLCR(tty))
1252 c = '\r';
1253
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001254 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1256 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1257 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001258 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return;
1260 }
1261 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001262 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001264 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001266 echo_char_raw('^', ldata);
1267 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001268 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 }
1271 return;
1272 }
1273 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1274 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001275 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Jiri Slaby57c94122012-10-18 22:26:43 +02001277 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001279 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001280 while (tail != ldata->read_head) {
1281 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1283 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001284 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return;
1286 }
1287 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001288 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001289 if (L_ECHO(tty))
1290 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001291 return;
1292 }
1293 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001294 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001295 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 goto handle_newline;
1298 }
1299 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001300 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001301 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001302 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001303 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 c = __DISABLED_CHAR;
1305 goto handle_newline;
1306 }
1307 if ((c == EOL_CHAR(tty)) ||
1308 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001309 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1310 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001311 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001312 if (L_ECHO(tty))
1313 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001314 return;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
1317 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1318 */
1319 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001321 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001322 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001324 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 /*
1327 * XXX does PARMRK doubling happen for
1328 * EOL_CHAR and EOL2_CHAR?
1329 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001330 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001331 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Alan Cox4edf1822008-02-08 04:18:44 -08001333handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001334 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001335 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001336 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001337 ldata->canon_head = ldata->read_head;
1338 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001339 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1341 if (waitqueue_active(&tty->read_wait))
1342 wake_up_interruptible(&tty->read_wait);
1343 return;
1344 }
1345 }
Alan Cox4edf1822008-02-08 04:18:44 -08001346
Joe Petersonacc71bb2009-01-02 13:43:32 +00001347 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001348 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001349 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001350 if (L_ECHO(tty))
1351 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001352 return;
1353 }
1354 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001355 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001357 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 else {
1359 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001360 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001361 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 echo_char(c, tty);
1363 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001364 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Joe Petersonacc71bb2009-01-02 13:43:32 +00001367 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001368 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jiri Slaby57c94122012-10-18 22:26:43 +02001370 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374/**
1375 * n_tty_write_wakeup - asynchronous I/O notifier
1376 * @tty: tty device
1377 *
1378 * Required for the ptys, serial driver etc. since processes
1379 * that attach themselves to the master and rely on ASYNC
1380 * IO must be woken up
1381 */
1382
1383static void n_tty_write_wakeup(struct tty_struct *tty)
1384{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001385 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/**
1390 * n_tty_receive_buf - data receive
1391 * @tty: terminal device
1392 * @cp: buffer
1393 * @fp: flag buffer
1394 * @count: characters
1395 *
1396 * Called by the terminal driver when a block of characters has
1397 * been received. This function must be called from soft contexts
1398 * not from interrupt context. The driver is responsible for making
1399 * calls one at a time and in order (or using flush_to_ldisc)
1400 */
Alan Cox4edf1822008-02-08 04:18:44 -08001401
Linus Torvalds55db4c62011-06-04 06:33:24 +09001402static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1403 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001405 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 const unsigned char *p;
1407 char *f, flags = TTY_NORMAL;
1408 int i;
1409 char buf[64];
1410 unsigned long cpuflags;
1411
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001412 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001413 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001414 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1415 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001417 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1418 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1419 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 cp += i;
1421 count -= i;
1422
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001423 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1424 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001426 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1427 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1428 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001429 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001431 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (f)
1433 flags = *f++;
1434 switch (flags) {
1435 case TTY_NORMAL:
1436 n_tty_receive_char(tty, *p);
1437 break;
1438 case TTY_BREAK:
1439 n_tty_receive_break(tty);
1440 break;
1441 case TTY_PARITY:
1442 case TTY_FRAME:
1443 n_tty_receive_parity_error(tty, *p);
1444 break;
1445 case TTY_OVERRUN:
1446 n_tty_receive_overrun(tty);
1447 break;
1448 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001449 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 tty_name(tty, buf), flags);
1451 break;
1452 }
1453 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001454 if (tty->ops->flush_chars)
1455 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Linus Torvalds55db4c62011-06-04 06:33:24 +09001458 n_tty_set_room(tty);
1459
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001460 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001461 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1463 if (waitqueue_active(&tty->read_wait))
1464 wake_up_interruptible(&tty->read_wait);
1465 }
1466
1467 /*
1468 * Check the remaining room for the input canonicalization
1469 * mode. We don't want to throttle the driver if we're in
1470 * canonical mode and don't have a newline yet!
1471 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001472 while (1) {
1473 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1474 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1475 break;
1476 if (!tty_throttle_safe(tty))
1477 break;
1478 }
1479 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482int is_ignored(int sig)
1483{
1484 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001485 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/**
1489 * n_tty_set_termios - termios data changed
1490 * @tty: terminal
1491 * @old: previous data
1492 *
1493 * Called by the tty layer when the user changes termios flags so
1494 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001495 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * guaranteed that this function will not be re-entered or in progress
1497 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001498 *
1499 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
Alan Cox4edf1822008-02-08 04:18:44 -08001501
1502static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001504 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001505 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001506
1507 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001508 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001509 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001510 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001511 ldata->canon_head = ldata->read_tail;
1512 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001513 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001514 }
1515
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001516 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001517 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001518
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001519 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001521 ldata->raw = 1;
1522 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001523 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return;
1525 }
1526 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1527 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1528 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1529 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001530 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001533 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001535 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001538 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1539 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1540 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1541 set_bit('\n', ldata->process_char_map);
1542 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (L_IEXTEN(tty)) {
1544 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001545 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001547 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001549 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (L_ECHO(tty))
1551 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001552 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 }
1555 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001556 set_bit(START_CHAR(tty), ldata->process_char_map);
1557 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001560 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1561 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1562 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001564 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001565 ldata->raw = 0;
1566 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001568 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1570 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1571 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001572 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001574 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001576 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001577 /* The termios change make the tty ready for I/O */
1578 wake_up_interruptible(&tty->write_wait);
1579 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/**
1583 * n_tty_close - close the ldisc for this tty
1584 * @tty: device
1585 *
Alan Cox4edf1822008-02-08 04:18:44 -08001586 * Called from the terminal layer when this line discipline is
1587 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * discipline change. The function will not be called while other
1589 * ldisc methods are in progress.
1590 */
Alan Cox4edf1822008-02-08 04:18:44 -08001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592static void n_tty_close(struct tty_struct *tty)
1593{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001594 struct n_tty_data *ldata = tty->disc_data;
1595
Peter Hurley79901312013-03-11 16:44:23 -04001596 if (tty->link)
1597 n_tty_packet_mode_flush(tty);
1598
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001599 kfree(ldata->read_buf);
1600 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001601 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001602 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605/**
1606 * n_tty_open - open an ldisc
1607 * @tty: terminal to open
1608 *
Alan Cox4edf1822008-02-08 04:18:44 -08001609 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * terminal device. Can sleep. Called serialized so that no
1611 * other events will occur in parallel. No further open will occur
1612 * until a close.
1613 */
1614
1615static int n_tty_open(struct tty_struct *tty)
1616{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001617 struct n_tty_data *ldata;
1618
1619 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1620 if (!ldata)
1621 goto err;
1622
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001623 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001624 mutex_init(&ldata->atomic_read_lock);
1625 mutex_init(&ldata->output_lock);
1626 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001627 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001628
Joe Petersona88a69c2009-01-02 13:40:53 +00001629 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001630 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1631 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1632 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001633 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001634
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001635 tty->disc_data = ldata;
Peter Hurley21622932013-03-11 16:44:21 -04001636 /* indicate buffer work may resume */
1637 clear_bit(TTY_LDISC_HALTED, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001639 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001640 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 n_tty_set_termios(tty, NULL);
1642 tty->minimum_to_wake = 1;
1643 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001646err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001647 kfree(ldata->read_buf);
1648 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001649 kfree(ldata);
1650err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001651 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static inline int input_available_p(struct tty_struct *tty, int amt)
1655{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001656 struct n_tty_data *ldata = tty->disc_data;
1657
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001658 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001659 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001660 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001662 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 1;
1664
1665 return 0;
1666}
1667
1668/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001669 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * @tty: terminal device
1671 * @b: user data
1672 * @nr: size of data
1673 *
Alan Cox11a96d12008-10-13 10:46:24 +01001674 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * ICANON is off; it copies characters straight from the tty queue to
1676 * user space directly. It can be profitably called twice; once to
1677 * drain the space from the tail pointer to the (physical) end of the
1678 * buffer, and once to drain the space from the (physical) beginning of
1679 * the buffer to head pointer.
1680 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001681 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *
1683 */
Alan Cox4edf1822008-02-08 04:18:44 -08001684
Alan Cox33f0f882006-01-09 20:54:13 -08001685static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 unsigned char __user **b,
1687 size_t *nr)
1688
1689{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001690 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int retval;
1692 size_t n;
1693 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001694 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001697 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001698 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001700 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001702 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001704 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001705 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1706 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001707 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001708 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001709 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1710 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001711 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001712 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001713 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001714 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 *b += n;
1716 *nr -= n;
1717 }
1718 return retval;
1719}
1720
Al Virocc4191d2008-03-29 03:08:48 +00001721extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001722 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724/**
1725 * job_control - check job control
1726 * @tty: tty
1727 * @file: file handle
1728 *
1729 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001730 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001732 *
Peter Hurley01a5e442013-03-06 08:38:20 -05001733 * Locking: redirected write test is safe
1734 * current->signal->tty check is safe
1735 * ctrl_lock to safely reference tty->pgrp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Alan Cox4edf1822008-02-08 04:18:44 -08001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static int job_control(struct tty_struct *tty, struct file *file)
1739{
1740 /* Job control check -- must be done at start and after
1741 every sleep (POSIX.1 7.1.1.4). */
1742 /* NOTE: not yet done after every sleep pending a thorough
1743 check of the logic of this change. -- jlc */
1744 /* don't stop on /dev/console */
Peter Hurley01a5e442013-03-06 08:38:20 -05001745 if (file->f_op->write == redirected_tty_write ||
1746 current->signal->tty != tty)
1747 return 0;
1748
1749 spin_lock_irq(&tty->ctrl_lock);
1750 if (!tty->pgrp)
1751 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1752 else if (task_pgrp(current) != tty->pgrp) {
1753 spin_unlock_irq(&tty->ctrl_lock);
1754 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1755 return -EIO;
1756 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1757 set_thread_flag(TIF_SIGPENDING);
1758 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Peter Hurley01a5e442013-03-06 08:38:20 -05001760 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
Alan Cox4edf1822008-02-08 04:18:44 -08001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765/**
Alan Cox11a96d12008-10-13 10:46:24 +01001766 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * @tty: tty device
1768 * @file: file object
1769 * @buf: userspace buffer pointer
1770 * @nr: size of I/O
1771 *
1772 * Perform reads for the line discipline. We are guaranteed that the
1773 * line discipline will not be closed under us but we may get multiple
1774 * parallel readers and must handle this ourselves. We may also get
1775 * a hangup. Always called in user context, may sleep.
1776 *
1777 * This code must be sure never to sleep through a hangup.
1778 */
Alan Cox4edf1822008-02-08 04:18:44 -08001779
Alan Cox11a96d12008-10-13 10:46:24 +01001780static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned char __user *buf, size_t nr)
1782{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001783 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 unsigned char __user *b = buf;
1785 DECLARE_WAITQUEUE(wait, current);
1786 int c;
1787 int minimum, time;
1788 ssize_t retval = 0;
1789 ssize_t size;
1790 long timeout;
1791 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001792 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001796 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 minimum = time = 0;
1800 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001801 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 time = (HZ / 10) * TIME_CHAR(tty);
1803 minimum = MIN_CHAR(tty);
1804 if (minimum) {
1805 if (time)
1806 tty->minimum_to_wake = 1;
1807 else if (!waitqueue_active(&tty->read_wait) ||
1808 (tty->minimum_to_wake > minimum))
1809 tty->minimum_to_wake = minimum;
1810 } else {
1811 timeout = 0;
1812 if (time) {
1813 timeout = time;
1814 time = 0;
1815 }
1816 tty->minimum_to_wake = minimum = 1;
1817 }
1818 }
1819
1820 /*
1821 * Internal serialization of reads.
1822 */
1823 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001824 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001826 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001827 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -ERESTARTSYS;
1829 }
Alan Cox04f378b2008-04-30 00:53:29 -07001830 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 while (nr) {
1834 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001835 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 unsigned char cs;
1837 if (b != buf)
1838 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001839 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 cs = tty->link->ctrl_status;
1841 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001842 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001843 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 retval = -EFAULT;
1845 b--;
1846 break;
1847 }
1848 nr--;
1849 break;
1850 }
1851 /* This statement must be first before checking for input
1852 so that any interrupt will set the state back to
1853 TASK_RUNNING. */
1854 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1857 ((minimum - (b - buf)) >= 1))
1858 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (!input_available_p(tty, 0)) {
1861 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1862 retval = -EIO;
1863 break;
1864 }
1865 if (tty_hung_up_p(file))
1866 break;
1867 if (!timeout)
1868 break;
1869 if (file->f_flags & O_NONBLOCK) {
1870 retval = -EAGAIN;
1871 break;
1872 }
1873 if (signal_pending(current)) {
1874 retval = -ERESTARTSYS;
1875 break;
1876 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001877 /* FIXME: does n_tty_set_room need locking ? */
1878 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 continue;
1881 }
1882 __set_current_state(TASK_RUNNING);
1883
1884 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001885 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001886 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 retval = -EFAULT;
1888 b--;
1889 break;
1890 }
1891 nr--;
1892 }
1893
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001894 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001896 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001897 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001898 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001900 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001901 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001902 c = ldata->read_buf[ldata->read_tail];
1903 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001905 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (eol) {
1907 /* this test should be redundant:
1908 * we shouldn't be reading data if
1909 * canon_data is 0
1910 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001911 if (--ldata->canon_data < 0)
1912 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Ivo Sieben98001212013-01-28 13:32:01 +01001914 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001917 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 retval = -EFAULT;
1919 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 break;
1922 }
1923 nr--;
1924 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001925 if (eol) {
1926 tty_audit_push(tty);
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;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001929 }
Ivo Sieben98001212013-01-28 13:32:01 +01001930 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Ivo Sieben98001212013-01-28 13:32:01 +01001932 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (retval)
1934 break;
1935 } else {
1936 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001937 /* The copy function takes the read lock and handles
1938 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 uncopied = copy_from_read_buf(tty, &b, &nr);
1940 uncopied += copy_from_read_buf(tty, &b, &nr);
1941 if (uncopied) {
1942 retval = -EFAULT;
1943 break;
1944 }
1945 }
1946
1947 /* If there is enough space in the read buffer now, let the
1948 * low-level driver know. We use n_tty_chars_in_buffer() to
1949 * check the buffer, as it now knows about canonical mode.
1950 * Otherwise, if the driver is throttled and the line is
1951 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1952 * we won't get any more characters.
1953 */
Peter Hurleye91e52e2013-03-06 08:20:53 -05001954 while (1) {
1955 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1956 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1957 break;
1958 if (!tty->count)
1959 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001960 n_tty_set_room(tty);
Peter Hurleye91e52e2013-03-06 08:20:53 -05001961 if (!tty_unthrottle_safe(tty))
1962 break;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001963 }
Peter Hurleye91e52e2013-03-06 08:20:53 -05001964 __tty_set_flow_change(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 if (b - buf >= minimum)
1967 break;
1968 if (time)
1969 timeout = time;
1970 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001971 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 remove_wait_queue(&tty->read_wait, &wait);
1973
1974 if (!waitqueue_active(&tty->read_wait))
1975 tty->minimum_to_wake = minimum;
1976
1977 __set_current_state(TASK_RUNNING);
1978 size = b - buf;
1979 if (size) {
1980 retval = size;
1981 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001982 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001984 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Linus Torvalds55db4c62011-06-04 06:33:24 +09001986 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return retval;
1988}
1989
1990/**
Alan Cox11a96d12008-10-13 10:46:24 +01001991 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 * @tty: tty device
1993 * @file: file object
1994 * @buf: userspace buffer pointer
1995 * @nr: size of I/O
1996 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001997 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001999 * and other such events. Since the receive code will echo characters,
2000 * thus calling driver write methods, the output_lock is used in
2001 * the output processing functions called here as well as in the
2002 * echo processing function to protect the column state and space
2003 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *
2005 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002006 *
2007 * Locking: output_lock to protect column state and space left
2008 * (note that the process_output*() functions take this
2009 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 */
Alan Cox4edf1822008-02-08 04:18:44 -08002011
Alan Cox11a96d12008-10-13 10:46:24 +01002012static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002013 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 const unsigned char *b = buf;
2016 DECLARE_WAITQUEUE(wait, current);
2017 int c;
2018 ssize_t retval = 0;
2019
2020 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2021 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2022 retval = tty_check_change(tty);
2023 if (retval)
2024 return retval;
2025 }
2026
Joe Petersona88a69c2009-01-02 13:40:53 +00002027 /* Write out any echoed characters that are still pending */
2028 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 add_wait_queue(&tty->write_wait, &wait);
2031 while (1) {
2032 set_current_state(TASK_INTERRUPTIBLE);
2033 if (signal_pending(current)) {
2034 retval = -ERESTARTSYS;
2035 break;
2036 }
2037 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2038 retval = -EIO;
2039 break;
2040 }
2041 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2042 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002043 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (num < 0) {
2045 if (num == -EAGAIN)
2046 break;
2047 retval = num;
2048 goto break_out;
2049 }
2050 b += num;
2051 nr -= num;
2052 if (nr == 0)
2053 break;
2054 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002055 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 b++; nr--;
2058 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002059 if (tty->ops->flush_chars)
2060 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002062 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002063 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002064 if (c < 0) {
2065 retval = c;
2066 goto break_out;
2067 }
2068 if (!c)
2069 break;
2070 b += c;
2071 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 if (!nr)
2075 break;
2076 if (file->f_flags & O_NONBLOCK) {
2077 retval = -EAGAIN;
2078 break;
2079 }
2080 schedule();
2081 }
2082break_out:
2083 __set_current_state(TASK_RUNNING);
2084 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002085 if (b - buf != nr && tty->fasync)
2086 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return (b - buf) ? b - buf : retval;
2088}
2089
2090/**
Alan Cox11a96d12008-10-13 10:46:24 +01002091 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * @tty: terminal device
2093 * @file: file accessing it
2094 * @wait: poll table
2095 *
2096 * Called when the line discipline is asked to poll() for data or
2097 * for special events. This code is not serialized with respect to
2098 * other events save open/close.
2099 *
2100 * This code must be sure never to sleep through a hangup.
2101 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
Alan Cox4edf1822008-02-08 04:18:44 -08002103
Alan Cox11a96d12008-10-13 10:46:24 +01002104static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002105 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 unsigned int mask = 0;
2108
2109 poll_wait(file, &tty->read_wait, wait);
2110 poll_wait(file, &tty->write_wait, wait);
2111 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2112 mask |= POLLIN | POLLRDNORM;
2113 if (tty->packet && tty->link->ctrl_status)
2114 mask |= POLLPRI | POLLIN | POLLRDNORM;
2115 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2116 mask |= POLLHUP;
2117 if (tty_hung_up_p(file))
2118 mask |= POLLHUP;
2119 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2120 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2121 tty->minimum_to_wake = MIN_CHAR(tty);
2122 else
2123 tty->minimum_to_wake = 1;
2124 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002125 if (tty->ops->write && !tty_is_writelocked(tty) &&
2126 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2127 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 mask |= POLLOUT | POLLWRNORM;
2129 return mask;
2130}
2131
Jiri Slaby57c94122012-10-18 22:26:43 +02002132static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002133{
2134 int nr, head, tail;
2135
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002136 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002137 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002138 head = ldata->canon_head;
2139 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002140 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2141 /* Skip EOF-chars.. */
2142 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002143 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002144 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002145 nr--;
2146 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2147 }
2148 return nr;
2149}
2150
2151static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2152 unsigned int cmd, unsigned long arg)
2153{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002154 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002155 int retval;
2156
2157 switch (cmd) {
2158 case TIOCOUTQ:
2159 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2160 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002161 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002162 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002163 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002164 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002165 return put_user(retval, (unsigned int __user *) arg);
2166 default:
2167 return n_tty_ioctl_helper(tty, file, cmd, arg);
2168 }
2169}
2170
Alan Coxa352def2008-07-16 21:53:12 +01002171struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002172 .magic = TTY_LDISC_MAGIC,
2173 .name = "n_tty",
2174 .open = n_tty_open,
2175 .close = n_tty_close,
2176 .flush_buffer = n_tty_flush_buffer,
2177 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002178 .read = n_tty_read,
2179 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002180 .ioctl = n_tty_ioctl,
2181 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002182 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002183 .receive_buf = n_tty_receive_buf,
2184 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002186
2187/**
2188 * n_tty_inherit_ops - inherit N_TTY methods
2189 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2190 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002191 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002192 * methods.
2193 */
2194
2195void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2196{
2197 *ops = tty_ldisc_N_TTY;
2198 ops->owner = NULL;
2199 ops->refcount = ops->flags = 0;
2200}
2201EXPORT_SYMBOL_GPL(n_tty_inherit_ops);