blob: 095072899b5edea12eee71da689367cce048e234 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* number of characters left in xmit buffer before select has we have room */
55#define WAKEUP_CHARS 256
56
57/*
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
61 */
62#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010063#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Joe Petersona88a69c2009-01-02 13:40:53 +000065/*
66 * Special byte codes used in the echo buffer to represent operations
67 * or special handling of characters. Bytes in the echo buffer that
68 * are not part of such special blocks are treated as normal character
69 * codes.
70 */
71#define ECHO_OP_START 0xff
72#define ECHO_OP_MOVE_BACK_COL 0x80
73#define ECHO_OP_SET_CANON_COL 0x81
74#define ECHO_OP_ERASE_TAB 0x82
75
Jiri Slaby70ece7a2012-10-18 22:26:38 +020076struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020077 unsigned int column;
78 unsigned long overrun_time;
79 int num_overrun;
80
81 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
82 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020083
84 DECLARE_BITMAP(process_char_map, 256);
85 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020086
87 char *read_buf;
88 int read_head;
89 int read_tail;
90 int read_cnt;
91
92 unsigned char *echo_buf;
93 unsigned int echo_pos;
94 unsigned int echo_cnt;
95
96 int canon_data;
97 unsigned long canon_head;
98 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +020099
100 struct mutex atomic_read_lock;
101 struct mutex output_lock;
102 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100103 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200104};
105
Miloslav Trmac522ed772007-07-15 23:40:56 -0700106static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
107 unsigned char __user *ptr)
108{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200109 struct n_tty_data *ldata = tty->disc_data;
110
111 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700112 return put_user(x, ptr);
113}
114
Linus Torvalds55db4c62011-06-04 06:33:24 +0900115/**
116 * n_tty_set__room - receive space
117 * @tty: terminal
118 *
119 * Called by the driver to find out how much data it is
120 * permitted to feed to the line discipline without any being lost
121 * and thus to manage flow control. Not serialized. Answers for the
122 * "instant".
123 */
124
125static void n_tty_set_room(struct tty_struct *tty)
126{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200127 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500128 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900129 int old_left;
130
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200131 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500132 if (I_PARMRK(tty)) {
133 /* Multiply read_cnt by 3, since each byte might take up to
134 * three times as many spaces when PARMRK is set (depending on
135 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200136 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500137 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200138 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500139
Linus Torvalds55db4c62011-06-04 06:33:24 +0900140 /*
141 * If we are doing input canonicalization, and there are no
142 * pending newlines, let characters through without limit, so
143 * that erase characters will be handled. Other excess
144 * characters will be beeped.
145 */
146 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200147 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900148 old_left = tty->receive_room;
149 tty->receive_room = left;
150
151 /* Did this open up the receive buffer? We may need to flip */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200152 if (left && !old_left) {
153 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400154 "scheduling with invalid itty\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200155 schedule_work(&tty->port->buf.work);
156 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900157}
158
Jiri Slaby57c94122012-10-18 22:26:43 +0200159static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200161 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
162 ldata->read_buf[ldata->read_head] = c;
163 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
164 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166}
167
Alan Cox17b82062008-10-13 10:45:06 +0100168/**
169 * put_tty_queue - add character to tty
170 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200171 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100172 *
173 * Add a character to the tty read_buf queue. This is done under the
174 * read_lock to serialize character addition and also to protect us
175 * against parallel reads or flushes
176 */
177
Jiri Slaby57c94122012-10-18 22:26:43 +0200178static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned long flags;
181 /*
182 * The problem of stomping on the buffers ends here.
183 * Why didn't anyone see this one coming? --AJK
184 */
Ivo Sieben98001212013-01-28 13:32:01 +0100185 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200186 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100187 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/**
191 * check_unthrottle - allow new receive data
192 * @tty; tty device
193 *
Alan Cox17b82062008-10-13 10:45:06 +0100194 * Check whether to call the driver unthrottle functions
195 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800196 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * this is not guaranteed.
198 */
Alan Cox4edf1822008-02-08 04:18:44 -0800199static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alan Cox39c2e602008-04-30 00:54:18 -0700201 if (tty->count)
202 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
206 * reset_buffer_flags - reset buffer state
207 * @tty: terminal to reset
208 *
Alan Cox4edf1822008-02-08 04:18:44 -0800209 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * and make sure the driver is unthrottled. Called
211 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100212 *
213 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void reset_buffer_flags(struct tty_struct *tty)
217{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200218 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned long flags;
220
Ivo Sieben98001212013-01-28 13:32:01 +0100221 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200222 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100223 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000224
Jiri Slabybddc7152012-10-18 22:26:42 +0200225 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200226 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200227 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000228
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200229 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200230 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900231 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
235 * n_tty_flush_buffer - clean input queue
236 * @tty: terminal device
237 *
238 * Flush the input buffer. Called when the line discipline is
239 * being closed, when the tty layer wants the buffer flushed (eg
240 * at hangup) or when the N_TTY line discipline internally has to
241 * clean the pending queue (for example some signals).
242 *
Alan Cox17b82062008-10-13 10:45:06 +0100243 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Alan Cox4edf1822008-02-08 04:18:44 -0800245
246static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Alan Cox04f378b2008-04-30 00:53:29 -0700248 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* clear everything and unthrottle the driver */
250 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!tty->link)
253 return;
254
Alan Cox04f378b2008-04-30 00:53:29 -0700255 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (tty->link->packet) {
257 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
258 wake_up_interruptible(&tty->link->read_wait);
259 }
Alan Cox04f378b2008-04-30 00:53:29 -0700260 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/**
264 * n_tty_chars_in_buffer - report available bytes
265 * @tty: tty device
266 *
267 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800268 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100269 *
270 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Alan Cox4edf1822008-02-08 04:18:44 -0800272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
274{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200275 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned long flags;
277 ssize_t n = 0;
278
Ivo Sieben98001212013-01-28 13:32:01 +0100279 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200280 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200281 n = ldata->read_cnt;
282 } else if (ldata->canon_data) {
283 n = (ldata->canon_head > ldata->read_tail) ?
284 ldata->canon_head - ldata->read_tail :
285 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Ivo Sieben98001212013-01-28 13:32:01 +0100287 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return n;
289}
290
291/**
292 * is_utf8_continuation - utf8 multibyte check
293 * @c: byte to check
294 *
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character. We use this to correctly compute the on screen size
297 * of the character when printing
298 */
Alan Cox4edf1822008-02-08 04:18:44 -0800299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static inline int is_utf8_continuation(unsigned char c)
301{
302 return (c & 0xc0) == 0x80;
303}
304
305/**
306 * is_continuation - multibyte check
307 * @c: byte to check
308 *
309 * Returns true if the utf8 character 'c' is a multibyte continuation
310 * character and the terminal is in unicode mode.
311 */
Alan Cox4edf1822008-02-08 04:18:44 -0800312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static inline int is_continuation(unsigned char c, struct tty_struct *tty)
314{
315 return I_IUTF8(tty) && is_utf8_continuation(c);
316}
317
318/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000319 * do_output_char - output one character
320 * @c: character (or partial unicode symbol)
321 * @tty: terminal device
322 * @space: space available in tty driver write buffer
323 *
324 * This is a helper function that handles one output character
325 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600326 * doing OPOST processing and putting the results in the
327 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000328 *
329 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
330 * and NLDLY. They simply aren't relevant in the world today.
331 * If you ever need them, add them here.
332 *
333 * Returns the number of bytes of buffer space used or -1 if
334 * no space left.
335 *
336 * Locking: should be called under the output_lock to protect
337 * the column state and space left in the buffer
338 */
339
340static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
341{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200342 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000343 int spaces;
344
345 if (!space)
346 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000347
Joe Petersona88a69c2009-01-02 13:40:53 +0000348 switch (c) {
349 case '\n':
350 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200351 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000352 if (O_ONLCR(tty)) {
353 if (space < 2)
354 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200355 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700356 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000357 return 2;
358 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200359 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 break;
361 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200362 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000363 return 0;
364 if (O_OCRNL(tty)) {
365 c = '\n';
366 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200367 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000368 break;
369 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200370 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000371 break;
372 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200373 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000374 if (O_TABDLY(tty) == XTABS) {
375 if (space < spaces)
376 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200377 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 tty->ops->write(tty, " ", spaces);
379 return spaces;
380 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200381 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000382 break;
383 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200384 if (ldata->column > 0)
385 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000386 break;
387 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000388 if (!iscntrl(c)) {
389 if (O_OLCUC(tty))
390 c = toupper(c);
391 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200392 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000393 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000394 break;
395 }
396
397 tty_put_char(tty, c);
398 return 1;
399}
400
401/**
402 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * @c: character (or partial unicode symbol)
404 * @tty: terminal device
405 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600406 * Output one character with OPOST processing.
407 * Returns -1 when the output device is full and the character
408 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000410 * Locking: output_lock to protect column state and space left
411 * (also, this is called from n_tty_write under the
412 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Alan Cox4edf1822008-02-08 04:18:44 -0800414
Joe Petersona88a69c2009-01-02 13:40:53 +0000415static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jiri Slabybddc7152012-10-18 22:26:42 +0200417 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000418 int space, retval;
419
Jiri Slabybddc7152012-10-18 22:26:42 +0200420 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Alan Coxf34d7a52008-04-30 00:54:13 -0700422 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000423 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jiri Slabybddc7152012-10-18 22:26:42 +0200425 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 if (retval < 0)
427 return -1;
428 else
429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000433 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600435 * @buf: character buffer
436 * @nr: number of bytes to output
437 *
438 * Output a block of characters with OPOST processing.
439 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
441 * This path is used to speed up block console writes, among other
442 * things when processing blocks of output data. It handles only
443 * the simple cases normally found and helps to generate blocks of
444 * symbols for the console driver and thus improve performance.
445 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000446 * Locking: output_lock to protect column state and space left
447 * (also, this is called from n_tty_write under the
448 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Alan Cox4edf1822008-02-08 04:18:44 -0800450
Joe Petersona88a69c2009-01-02 13:40:53 +0000451static ssize_t process_output_block(struct tty_struct *tty,
452 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200454 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100456 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 const unsigned char *cp;
458
Jiri Slabybddc7152012-10-18 22:26:42 +0200459 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000460
Alan Coxf34d7a52008-04-30 00:54:13 -0700461 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000462 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200463 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (nr > space)
467 nr = space;
468
469 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000470 unsigned char c = *cp;
471
472 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 case '\n':
474 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200475 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (O_ONLCR(tty))
477 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200478 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200481 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto break_out;
483 if (O_OCRNL(tty))
484 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200485 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case '\t':
488 goto break_out;
489 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200490 if (ldata->column > 0)
491 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000494 if (!iscntrl(c)) {
495 if (O_OLCUC(tty))
496 goto break_out;
497 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200498 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 }
502 }
503break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700504 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000505
Jiri Slabybddc7152012-10-18 22:26:42 +0200506 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return i;
508}
509
Joe Petersona88a69c2009-01-02 13:40:53 +0000510/**
511 * process_echoes - write pending echo characters
512 * @tty: terminal device
513 *
514 * Write previously buffered echo (and other ldisc-generated)
515 * characters to the tty.
516 *
517 * Characters generated by the ldisc (including echoes) need to
518 * be buffered because the driver's write buffer can fill during
519 * heavy program output. Echoing straight to the driver will
520 * often fail under these conditions, causing lost characters and
521 * resulting mismatches of ldisc state information.
522 *
523 * Since the ldisc state must represent the characters actually sent
524 * to the driver at the time of the write, operations like certain
525 * changes in column state are also saved in the buffer and executed
526 * here.
527 *
528 * A circular fifo buffer is used so that the most recent characters
529 * are prioritized. Also, when control characters are echoed with a
530 * prefixed "^", the pair is treated atomically and thus not separated.
531 *
532 * Locking: output_lock to protect column state and space left,
533 * echo_lock to protect the echo buffer
534 */
535
536static void process_echoes(struct tty_struct *tty)
537{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200538 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000539 int space, nr;
540 unsigned char c;
541 unsigned char *cp, *buf_end;
542
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200543 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000544 return;
545
Jiri Slabybddc7152012-10-18 22:26:42 +0200546 mutex_lock(&ldata->output_lock);
547 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000548
549 space = tty_write_room(tty);
550
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200551 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
552 cp = ldata->echo_buf + ldata->echo_pos;
553 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000554 while (nr > 0) {
555 c = *cp;
556 if (c == ECHO_OP_START) {
557 unsigned char op;
558 unsigned char *opp;
559 int no_space_left = 0;
560
561 /*
562 * If the buffer byte is the start of a multi-byte
563 * operation, get the next byte, which is either the
564 * op code or a control character value.
565 */
566 opp = cp + 1;
567 if (opp == buf_end)
568 opp -= N_TTY_BUF_SIZE;
569 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000570
Joe Petersona88a69c2009-01-02 13:40:53 +0000571 switch (op) {
572 unsigned int num_chars, num_bs;
573
574 case ECHO_OP_ERASE_TAB:
575 if (++opp == buf_end)
576 opp -= N_TTY_BUF_SIZE;
577 num_chars = *opp;
578
579 /*
580 * Determine how many columns to go back
581 * in order to erase the tab.
582 * This depends on the number of columns
583 * used by other characters within the tab
584 * area. If this (modulo 8) count is from
585 * the start of input rather than from a
586 * previous tab, we offset by canon column.
587 * Otherwise, tab spacing is normal.
588 */
589 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200590 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000591 num_bs = 8 - (num_chars & 7);
592
593 if (num_bs > space) {
594 no_space_left = 1;
595 break;
596 }
597 space -= num_bs;
598 while (num_bs--) {
599 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200600 if (ldata->column > 0)
601 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000602 }
603 cp += 3;
604 nr -= 3;
605 break;
606
607 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200608 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 cp += 2;
610 nr -= 2;
611 break;
612
613 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200614 if (ldata->column > 0)
615 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000616 cp += 2;
617 nr -= 2;
618 break;
619
620 case ECHO_OP_START:
621 /* This is an escaped echo op start code */
622 if (!space) {
623 no_space_left = 1;
624 break;
625 }
626 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200627 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 space--;
629 cp += 2;
630 nr -= 2;
631 break;
632
633 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000634 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600635 * If the op is not a special byte code,
636 * it is a ctrl char tagged to be echoed
637 * as "^X" (where X is the letter
638 * representing the control char).
639 * Note that we must ensure there is
640 * enough space for the whole ctrl pair.
641 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000642 */
Joe Peterson62b26352009-09-09 15:03:47 -0600643 if (space < 2) {
644 no_space_left = 1;
645 break;
646 }
647 tty_put_char(tty, '^');
648 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200649 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600650 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000651 cp += 2;
652 nr -= 2;
653 }
654
655 if (no_space_left)
656 break;
657 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600658 if (O_OPOST(tty) &&
659 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
660 int retval = do_output_char(c, tty, space);
661 if (retval < 0)
662 break;
663 space -= retval;
664 } else {
665 if (!space)
666 break;
667 tty_put_char(tty, c);
668 space -= 1;
669 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000670 cp += 1;
671 nr -= 1;
672 }
673
674 /* When end of circular buffer reached, wrap around */
675 if (cp >= buf_end)
676 cp -= N_TTY_BUF_SIZE;
677 }
678
679 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200680 ldata->echo_pos = 0;
681 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200682 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000683 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200684 int num_processed = ldata->echo_cnt - nr;
685 ldata->echo_pos += num_processed;
686 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
687 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000688 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200689 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000690 }
691
Jiri Slabybddc7152012-10-18 22:26:42 +0200692 mutex_unlock(&ldata->echo_lock);
693 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000694
695 if (tty->ops->flush_chars)
696 tty->ops->flush_chars(tty);
697}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 * add_echo_byte - add a byte to the echo buffer
701 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200702 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000703 *
704 * Add a character or operation byte to the echo buffer.
705 *
706 * Should be called under the echo lock to protect the echo buffer.
707 */
708
Jiri Slaby57c94122012-10-18 22:26:43 +0200709static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000710{
711 int new_byte_pos;
712
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200713 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000714 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200715 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000716
717 /*
718 * Since the buffer start position needs to be advanced,
719 * be sure to step by a whole operation byte group.
720 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200721 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
722 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000723 (N_TTY_BUF_SIZE - 1)] ==
724 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200725 ldata->echo_pos += 3;
726 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200728 ldata->echo_pos += 2;
729 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000730 }
731 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200732 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000733 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200734 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000735
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200736 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000737 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200738 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000739 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200740 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000741 }
742
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200743 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000744}
745
746/**
747 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200748 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000749 *
750 * Add an operation to the echo buffer to move back one column.
751 *
752 * Locking: echo_lock to protect the echo buffer
753 */
754
Jiri Slaby57c94122012-10-18 22:26:43 +0200755static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000756{
Jiri Slabybddc7152012-10-18 22:26:42 +0200757 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200758 add_echo_byte(ECHO_OP_START, ldata);
759 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200760 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000761}
762
763/**
764 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200765 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000766 *
767 * Add an operation to the echo buffer to set the canon column
768 * to the current column.
769 *
770 * Locking: echo_lock to protect the echo buffer
771 */
772
Jiri Slaby57c94122012-10-18 22:26:43 +0200773static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000774{
Jiri Slabybddc7152012-10-18 22:26:42 +0200775 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200776 add_echo_byte(ECHO_OP_START, ldata);
777 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200778 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000779}
780
781/**
782 * echo_erase_tab - add operation to erase a tab
783 * @num_chars: number of character columns already used
784 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200785 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000786 *
787 * Add an operation to the echo buffer to erase a tab.
788 *
789 * Called by the eraser function, which knows how many character
790 * columns have been used since either a previous tab or the start
791 * of input. This information will be used later, along with
792 * canon column (if applicable), to go back the correct number
793 * of columns.
794 *
795 * Locking: echo_lock to protect the echo buffer
796 */
797
798static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200799 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000800{
Jiri Slabybddc7152012-10-18 22:26:42 +0200801 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000802
Jiri Slaby57c94122012-10-18 22:26:43 +0200803 add_echo_byte(ECHO_OP_START, ldata);
804 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000805
806 /* We only need to know this modulo 8 (tab spacing) */
807 num_chars &= 7;
808
809 /* Set the high bit as a flag if num_chars is after a previous tab */
810 if (after_tab)
811 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000812
Jiri Slaby57c94122012-10-18 22:26:43 +0200813 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000814
Jiri Slabybddc7152012-10-18 22:26:42 +0200815 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000816}
817
818/**
819 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * @c: unicode byte to echo
821 * @tty: terminal device
822 *
Alan Cox4edf1822008-02-08 04:18:44 -0800823 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100825 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000826 * This variant does not treat control characters specially.
827 *
828 * Locking: echo_lock to protect the echo buffer
829 */
830
Jiri Slaby57c94122012-10-18 22:26:43 +0200831static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000832{
Jiri Slabybddc7152012-10-18 22:26:42 +0200833 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000834 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200835 add_echo_byte(ECHO_OP_START, ldata);
836 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000837 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200838 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000839 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200840 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000841}
842
843/**
844 * echo_char - echo a character
845 * @c: unicode byte to echo
846 * @tty: terminal device
847 *
848 * Echo user input back onto the screen. This must be called only when
849 * L_ECHO(tty) is true. Called from the driver receive_buf path.
850 *
Joe Peterson62b26352009-09-09 15:03:47 -0600851 * This variant tags control characters to be echoed as "^X"
852 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000853 *
854 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856
857static void echo_char(unsigned char c, struct tty_struct *tty)
858{
Jiri Slabybddc7152012-10-18 22:26:42 +0200859 struct n_tty_data *ldata = tty->disc_data;
860
861 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000862
863 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200864 add_echo_byte(ECHO_OP_START, ldata);
865 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000866 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600867 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200868 add_echo_byte(ECHO_OP_START, ldata);
869 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000870 }
871
Jiri Slabybddc7152012-10-18 22:26:42 +0200872 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Alan Cox17b82062008-10-13 10:45:06 +0100875/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000876 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200877 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100878 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000879
Jiri Slaby57c94122012-10-18 22:26:43 +0200880static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200882 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200883 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200884 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886}
887
888/**
889 * eraser - handle erase function
890 * @c: character input
891 * @tty: terminal device
892 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200893 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * present in the stream from the driver layer. Handles the complexities
895 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100896 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000897 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Alan Cox4edf1822008-02-08 04:18:44 -0800899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static void eraser(unsigned char c, struct tty_struct *tty)
901{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200902 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 enum { ERASE, WERASE, KILL } kill_type;
904 int head, seen_alnums, cnt;
905 unsigned long flags;
906
Alan Cox17b82062008-10-13 10:45:06 +0100907 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200908 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000909 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
911 }
912 if (c == ERASE_CHAR(tty))
913 kill_type = ERASE;
914 else if (c == WERASE_CHAR(tty))
915 kill_type = WERASE;
916 else {
917 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100918 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200919 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200921 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100922 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return;
924 }
925 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100926 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200927 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200929 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100930 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200931 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 echo_char(KILL_CHAR(tty), tty);
933 /* Add a newline if ECHOK is on and ECHOKE is off. */
934 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200935 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return;
937 }
938 kill_type = KILL;
939 }
940
941 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100942 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200943 while (ldata->read_head != ldata->canon_head) {
944 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /* erase a single possibly multibyte character */
947 do {
948 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200949 c = ldata->read_buf[head];
950 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* do not partially erase */
953 if (is_continuation(c, tty))
954 break;
955
956 if (kill_type == WERASE) {
957 /* Equivalent to BSD's ALTWERASE. */
958 if (isalnum(c) || c == '_')
959 seen_alnums++;
960 else if (seen_alnums)
961 break;
962 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200963 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100964 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200965 ldata->read_head = head;
966 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100967 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (L_ECHO(tty)) {
969 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200970 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200971 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200972 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 /* if cnt > 1, output a multi-byte character */
975 echo_char(c, tty);
976 while (--cnt > 0) {
977 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200978 echo_char_raw(ldata->read_buf[head],
979 ldata);
980 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
983 echo_char(ERASE_CHAR(tty), tty);
984 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000985 unsigned int num_chars = 0;
986 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200987 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Joe Petersona88a69c2009-01-02 13:40:53 +0000989 /*
990 * Count the columns used for characters
991 * since the start of input or after a
992 * previous tab.
993 * This info is used to go back the correct
994 * number of columns.
995 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200996 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000997 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200998 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000999 if (c == '\t') {
1000 after_tab = 1;
1001 break;
Alan Cox300a6202009-01-02 13:41:04 +00001002 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001004 num_chars += 2;
1005 } else if (!is_continuation(c, tty)) {
1006 num_chars++;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001009 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 } else {
1011 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001012 echo_char_raw('\b', ldata);
1013 echo_char_raw(' ', ldata);
1014 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001017 echo_char_raw('\b', ldata);
1018 echo_char_raw(' ', ldata);
1019 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
1022 }
1023 if (kill_type == ERASE)
1024 break;
1025 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001026 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001027 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/**
1031 * isig - handle the ISIG optio
1032 * @sig: signal
1033 * @tty: terminal
1034 * @flush: force flush
1035 *
1036 * Called when a signal is being sent due to terminal input. This
1037 * may caus terminal flushing to take place according to the termios
1038 * settings and character used. Called from the driver receive_buf
1039 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001040 *
1041 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Alan Cox4edf1822008-02-08 04:18:44 -08001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static inline void isig(int sig, struct tty_struct *tty, int flush)
1045{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001046 if (tty->pgrp)
1047 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (flush || !L_NOFLSH(tty)) {
1049 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001050 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052}
1053
1054/**
1055 * n_tty_receive_break - handle break
1056 * @tty: terminal
1057 *
1058 * An RS232 break event has been hit in the incoming bitstream. This
1059 * can cause a variety of events depending upon the termios settings.
1060 *
1061 * Called from the receive_buf path so single threaded.
1062 */
Alan Cox4edf1822008-02-08 04:18:44 -08001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static inline void n_tty_receive_break(struct tty_struct *tty)
1065{
Jiri Slaby57c94122012-10-18 22:26:43 +02001066 struct n_tty_data *ldata = tty->disc_data;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (I_IGNBRK(tty))
1069 return;
1070 if (I_BRKINT(tty)) {
1071 isig(SIGINT, tty, 1);
1072 return;
1073 }
1074 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001075 put_tty_queue('\377', ldata);
1076 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001078 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 wake_up_interruptible(&tty->read_wait);
1080}
1081
1082/**
1083 * n_tty_receive_overrun - handle overrun reporting
1084 * @tty: terminal
1085 *
1086 * Data arrived faster than we could process it. While the tty
1087 * driver has flagged this the bits that were missed are gone
1088 * forever.
1089 *
1090 * Called from the receive_buf path so single threaded. Does not
1091 * need locking as num_overrun and overrun_time are function
1092 * private.
1093 */
Alan Cox4edf1822008-02-08 04:18:44 -08001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static inline void n_tty_receive_overrun(struct tty_struct *tty)
1096{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001097 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 char buf[64];
1099
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001100 ldata->num_overrun++;
1101 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1102 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1104 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001105 ldata->num_overrun);
1106 ldata->overrun_time = jiffies;
1107 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109}
1110
1111/**
1112 * n_tty_receive_parity_error - error notifier
1113 * @tty: terminal device
1114 * @c: character
1115 *
1116 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001117 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1120 unsigned char c)
1121{
Jiri Slaby57c94122012-10-18 22:26:43 +02001122 struct n_tty_data *ldata = tty->disc_data;
1123
Alan Cox4edf1822008-02-08 04:18:44 -08001124 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001127 put_tty_queue('\377', ldata);
1128 put_tty_queue('\0', ldata);
1129 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001131 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001133 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 wake_up_interruptible(&tty->read_wait);
1135}
1136
1137/**
1138 * n_tty_receive_char - perform processing
1139 * @tty: terminal device
1140 * @c: character
1141 *
1142 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001143 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * driver above.
1145 */
1146
1147static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1148{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001149 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001151 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001153 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001154 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return;
1156 }
Alan Cox4edf1822008-02-08 04:18:44 -08001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (I_ISTRIP(tty))
1159 c &= 0x7f;
1160 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001161 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
hyc@symas.com26df6d12010-06-22 10:14:49 -07001163 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001164 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001165 return;
1166 }
1167
Joe Peterson54d2a372008-02-06 01:37:59 -08001168 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001169 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1170 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001171 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001172 process_echoes(tty);
1173 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (tty->closing) {
1176 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001177 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001179 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001180 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 stop_tty(tty);
1182 }
1183 return;
1184 }
1185
1186 /*
1187 * If the previous character was LNEXT, or we know that this
1188 * character is not one of the characters that we'll have to
1189 * handle specially, do shortcut processing to speed things
1190 * up.
1191 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001192 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001193 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001194 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001195 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001196 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001197 if (L_ECHO(tty))
1198 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001199 return;
1200 }
1201 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001202 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001204 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001205 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001207 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001209 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001210 put_tty_queue(c, ldata);
1211 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return;
1213 }
Alan Cox4edf1822008-02-08 04:18:44 -08001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (I_IXON(tty)) {
1216 if (c == START_CHAR(tty)) {
1217 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001218 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return;
1220 }
1221 if (c == STOP_CHAR(tty)) {
1222 stop_tty(tty);
1223 return;
1224 }
1225 }
Joe Peterson575537b32008-04-30 00:53:30 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (L_ISIG(tty)) {
1228 int signal;
1229 signal = SIGINT;
1230 if (c == INTR_CHAR(tty))
1231 goto send_signal;
1232 signal = SIGQUIT;
1233 if (c == QUIT_CHAR(tty))
1234 goto send_signal;
1235 signal = SIGTSTP;
1236 if (c == SUSP_CHAR(tty)) {
1237send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001238 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001239 * Note that we do not use isig() here because we want
1240 * the order to be:
1241 * 1) flush, 2) echo, 3) signal
1242 */
1243 if (!L_NOFLSH(tty)) {
1244 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001245 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001246 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001247 if (I_IXON(tty))
1248 start_tty(tty);
1249 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001250 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001251 process_echoes(tty);
1252 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001253 if (tty->pgrp)
1254 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return;
1256 }
1257 }
Joe Peterson575537b32008-04-30 00:53:30 -07001258
1259 if (c == '\r') {
1260 if (I_IGNCR(tty))
1261 return;
1262 if (I_ICRNL(tty))
1263 c = '\n';
1264 } else if (c == '\n' && I_INLCR(tty))
1265 c = '\r';
1266
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001267 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1269 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1270 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001271 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return;
1273 }
1274 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001275 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001277 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001279 echo_char_raw('^', ldata);
1280 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001281 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 }
1284 return;
1285 }
1286 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1287 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001288 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jiri Slaby57c94122012-10-18 22:26:43 +02001290 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001292 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001293 while (tail != ldata->read_head) {
1294 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1296 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001297 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return;
1299 }
1300 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001301 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001302 if (L_ECHO(tty))
1303 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001304 return;
1305 }
1306 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001307 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001308 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 goto handle_newline;
1311 }
1312 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001313 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001314 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001315 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001316 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 c = __DISABLED_CHAR;
1318 goto handle_newline;
1319 }
1320 if ((c == EOL_CHAR(tty)) ||
1321 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001322 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1323 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001324 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001325 if (L_ECHO(tty))
1326 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001327 return;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1331 */
1332 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001334 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001335 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001337 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 /*
1340 * XXX does PARMRK doubling happen for
1341 * EOL_CHAR and EOL2_CHAR?
1342 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001343 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001344 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Alan Cox4edf1822008-02-08 04:18:44 -08001346handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001347 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001348 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001349 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001350 ldata->canon_head = ldata->read_head;
1351 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001352 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1354 if (waitqueue_active(&tty->read_wait))
1355 wake_up_interruptible(&tty->read_wait);
1356 return;
1357 }
1358 }
Alan Cox4edf1822008-02-08 04:18:44 -08001359
Joe Petersonacc71bb2009-01-02 13:43:32 +00001360 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001361 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001362 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001363 if (L_ECHO(tty))
1364 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001365 return;
1366 }
1367 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001368 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001370 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 else {
1372 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001373 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001374 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 echo_char(c, tty);
1376 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001377 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
Joe Petersonacc71bb2009-01-02 13:43:32 +00001380 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001381 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jiri Slaby57c94122012-10-18 22:26:43 +02001383 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/**
1388 * n_tty_write_wakeup - asynchronous I/O notifier
1389 * @tty: tty device
1390 *
1391 * Required for the ptys, serial driver etc. since processes
1392 * that attach themselves to the master and rely on ASYNC
1393 * IO must be woken up
1394 */
1395
1396static void n_tty_write_wakeup(struct tty_struct *tty)
1397{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001398 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/**
1403 * n_tty_receive_buf - data receive
1404 * @tty: terminal device
1405 * @cp: buffer
1406 * @fp: flag buffer
1407 * @count: characters
1408 *
1409 * Called by the terminal driver when a block of characters has
1410 * been received. This function must be called from soft contexts
1411 * not from interrupt context. The driver is responsible for making
1412 * calls one at a time and in order (or using flush_to_ldisc)
1413 */
Alan Cox4edf1822008-02-08 04:18:44 -08001414
Linus Torvalds55db4c62011-06-04 06:33:24 +09001415static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1416 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001418 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 const unsigned char *p;
1420 char *f, flags = TTY_NORMAL;
1421 int i;
1422 char buf[64];
1423 unsigned long cpuflags;
1424
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001425 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001426 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001427 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1428 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001430 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1431 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1432 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 cp += i;
1434 count -= i;
1435
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001436 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1437 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001439 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1440 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1441 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001442 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001444 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (f)
1446 flags = *f++;
1447 switch (flags) {
1448 case TTY_NORMAL:
1449 n_tty_receive_char(tty, *p);
1450 break;
1451 case TTY_BREAK:
1452 n_tty_receive_break(tty);
1453 break;
1454 case TTY_PARITY:
1455 case TTY_FRAME:
1456 n_tty_receive_parity_error(tty, *p);
1457 break;
1458 case TTY_OVERRUN:
1459 n_tty_receive_overrun(tty);
1460 break;
1461 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001462 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 tty_name(tty, buf), flags);
1464 break;
1465 }
1466 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001467 if (tty->ops->flush_chars)
1468 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
Linus Torvalds55db4c62011-06-04 06:33:24 +09001471 n_tty_set_room(tty);
1472
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001473 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001474 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1476 if (waitqueue_active(&tty->read_wait))
1477 wake_up_interruptible(&tty->read_wait);
1478 }
1479
1480 /*
1481 * Check the remaining room for the input canonicalization
1482 * mode. We don't want to throttle the driver if we're in
1483 * canonical mode and don't have a newline yet!
1484 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001485 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001486 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001487
1488 /* FIXME: there is a tiny race here if the receive room check runs
1489 before the other work executes and empties the buffer (upping
1490 the receiving room and unthrottling. We then throttle and get
1491 stuck. This has been observed and traced down by Vincent Pillet/
1492 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495int is_ignored(int sig)
1496{
1497 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001498 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501/**
1502 * n_tty_set_termios - termios data changed
1503 * @tty: terminal
1504 * @old: previous data
1505 *
1506 * Called by the tty layer when the user changes termios flags so
1507 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001508 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * guaranteed that this function will not be re-entered or in progress
1510 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001511 *
1512 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
Alan Cox4edf1822008-02-08 04:18:44 -08001514
1515static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001517 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001518 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001519
1520 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001521 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001522 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001523 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001524 ldata->canon_head = ldata->read_tail;
1525 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001526 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001527 }
1528
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001529 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001530 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001531
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001532 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001534 ldata->raw = 1;
1535 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001536 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return;
1538 }
1539 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1540 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1541 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1542 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001543 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001546 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001551 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1552 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1553 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1554 set_bit('\n', ldata->process_char_map);
1555 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (L_IEXTEN(tty)) {
1557 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001558 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001560 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001562 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (L_ECHO(tty))
1564 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001565 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 }
1568 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001569 set_bit(START_CHAR(tty), ldata->process_char_map);
1570 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001573 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1574 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1575 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001577 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001578 ldata->raw = 0;
1579 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001581 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1583 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1584 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001585 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001587 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001589 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001590 /* The termios change make the tty ready for I/O */
1591 wake_up_interruptible(&tty->write_wait);
1592 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/**
1596 * n_tty_close - close the ldisc for this tty
1597 * @tty: device
1598 *
Alan Cox4edf1822008-02-08 04:18:44 -08001599 * Called from the terminal layer when this line discipline is
1600 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * discipline change. The function will not be called while other
1602 * ldisc methods are in progress.
1603 */
Alan Cox4edf1822008-02-08 04:18:44 -08001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605static void n_tty_close(struct tty_struct *tty)
1606{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001607 struct n_tty_data *ldata = tty->disc_data;
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 n_tty_flush_buffer(tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001610 kfree(ldata->read_buf);
1611 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001612 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001613 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/**
1617 * n_tty_open - open an ldisc
1618 * @tty: terminal to open
1619 *
Alan Cox4edf1822008-02-08 04:18:44 -08001620 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * terminal device. Can sleep. Called serialized so that no
1622 * other events will occur in parallel. No further open will occur
1623 * until a close.
1624 */
1625
1626static int n_tty_open(struct tty_struct *tty)
1627{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001628 struct n_tty_data *ldata;
1629
1630 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1631 if (!ldata)
1632 goto err;
1633
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001634 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001635 mutex_init(&ldata->atomic_read_lock);
1636 mutex_init(&ldata->output_lock);
1637 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001638 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001639
Joe Petersona88a69c2009-01-02 13:40:53 +00001640 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001641 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1642 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001644 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001645
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001646 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001648 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001649 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 n_tty_set_termios(tty, NULL);
1651 tty->minimum_to_wake = 1;
1652 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001655err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001656 kfree(ldata->read_buf);
1657 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001658 kfree(ldata);
1659err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001660 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663static inline int input_available_p(struct tty_struct *tty, int amt)
1664{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001665 struct n_tty_data *ldata = tty->disc_data;
1666
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001667 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001668 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001669 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001671 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 1;
1673
1674 return 0;
1675}
1676
1677/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001678 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * @tty: terminal device
1680 * @b: user data
1681 * @nr: size of data
1682 *
Alan Cox11a96d12008-10-13 10:46:24 +01001683 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * ICANON is off; it copies characters straight from the tty queue to
1685 * user space directly. It can be profitably called twice; once to
1686 * drain the space from the tail pointer to the (physical) end of the
1687 * buffer, and once to drain the space from the (physical) beginning of
1688 * the buffer to head pointer.
1689 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001690 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 */
Alan Cox4edf1822008-02-08 04:18:44 -08001693
Alan Cox33f0f882006-01-09 20:54:13 -08001694static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 unsigned char __user **b,
1696 size_t *nr)
1697
1698{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001699 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int retval;
1701 size_t n;
1702 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001703 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001706 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001707 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001709 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001711 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001713 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001714 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1715 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001716 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001717 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001718 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1719 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001720 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001721 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001722 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001723 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 *b += n;
1725 *nr -= n;
1726 }
1727 return retval;
1728}
1729
Al Virocc4191d2008-03-29 03:08:48 +00001730extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001731 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733/**
1734 * job_control - check job control
1735 * @tty: tty
1736 * @file: file handle
1737 *
1738 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001739 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001741 *
1742 * FIXME:
1743 * Locking: None - redirected write test is safe, testing
1744 * current->signal should possibly lock current->sighand
1745 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
Alan Cox4edf1822008-02-08 04:18:44 -08001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int job_control(struct tty_struct *tty, struct file *file)
1749{
1750 /* Job control check -- must be done at start and after
1751 every sleep (POSIX.1 7.1.1.4). */
1752 /* NOTE: not yet done after every sleep pending a thorough
1753 check of the logic of this change. -- jlc */
1754 /* don't stop on /dev/console */
1755 if (file->f_op->write != redirected_tty_write &&
1756 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001757 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001758 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001759 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001761 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001763 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001764 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return -ERESTARTSYS;
1766 }
1767 }
1768 return 0;
1769}
Alan Cox4edf1822008-02-08 04:18:44 -08001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/**
Alan Cox11a96d12008-10-13 10:46:24 +01001773 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * @tty: tty device
1775 * @file: file object
1776 * @buf: userspace buffer pointer
1777 * @nr: size of I/O
1778 *
1779 * Perform reads for the line discipline. We are guaranteed that the
1780 * line discipline will not be closed under us but we may get multiple
1781 * parallel readers and must handle this ourselves. We may also get
1782 * a hangup. Always called in user context, may sleep.
1783 *
1784 * This code must be sure never to sleep through a hangup.
1785 */
Alan Cox4edf1822008-02-08 04:18:44 -08001786
Alan Cox11a96d12008-10-13 10:46:24 +01001787static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned char __user *buf, size_t nr)
1789{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001790 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned char __user *b = buf;
1792 DECLARE_WAITQUEUE(wait, current);
1793 int c;
1794 int minimum, time;
1795 ssize_t retval = 0;
1796 ssize_t size;
1797 long timeout;
1798 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001799 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001803 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 minimum = time = 0;
1807 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001808 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 time = (HZ / 10) * TIME_CHAR(tty);
1810 minimum = MIN_CHAR(tty);
1811 if (minimum) {
1812 if (time)
1813 tty->minimum_to_wake = 1;
1814 else if (!waitqueue_active(&tty->read_wait) ||
1815 (tty->minimum_to_wake > minimum))
1816 tty->minimum_to_wake = minimum;
1817 } else {
1818 timeout = 0;
1819 if (time) {
1820 timeout = time;
1821 time = 0;
1822 }
1823 tty->minimum_to_wake = minimum = 1;
1824 }
1825 }
1826
1827 /*
1828 * Internal serialization of reads.
1829 */
1830 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001831 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001833 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001834 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -ERESTARTSYS;
1836 }
Alan Cox04f378b2008-04-30 00:53:29 -07001837 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 while (nr) {
1841 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001842 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 unsigned char cs;
1844 if (b != buf)
1845 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001846 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 cs = tty->link->ctrl_status;
1848 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001849 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001850 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 retval = -EFAULT;
1852 b--;
1853 break;
1854 }
1855 nr--;
1856 break;
1857 }
1858 /* This statement must be first before checking for input
1859 so that any interrupt will set the state back to
1860 TASK_RUNNING. */
1861 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1864 ((minimum - (b - buf)) >= 1))
1865 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!input_available_p(tty, 0)) {
1868 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1869 retval = -EIO;
1870 break;
1871 }
1872 if (tty_hung_up_p(file))
1873 break;
1874 if (!timeout)
1875 break;
1876 if (file->f_flags & O_NONBLOCK) {
1877 retval = -EAGAIN;
1878 break;
1879 }
1880 if (signal_pending(current)) {
1881 retval = -ERESTARTSYS;
1882 break;
1883 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001884 /* FIXME: does n_tty_set_room need locking ? */
1885 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 continue;
1888 }
1889 __set_current_state(TASK_RUNNING);
1890
1891 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001892 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001893 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 retval = -EFAULT;
1895 b--;
1896 break;
1897 }
1898 nr--;
1899 }
1900
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001901 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001903 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001904 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001905 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001907 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001908 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001909 c = ldata->read_buf[ldata->read_tail];
1910 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001912 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (eol) {
1914 /* this test should be redundant:
1915 * we shouldn't be reading data if
1916 * canon_data is 0
1917 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001918 if (--ldata->canon_data < 0)
1919 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Ivo Sieben98001212013-01-28 13:32:01 +01001921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001924 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 retval = -EFAULT;
1926 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929 }
1930 nr--;
1931 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001932 if (eol) {
1933 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001934 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001936 }
Ivo Sieben98001212013-01-28 13:32:01 +01001937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Ivo Sieben98001212013-01-28 13:32:01 +01001939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (retval)
1941 break;
1942 } else {
1943 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001944 /* The copy function takes the read lock and handles
1945 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 uncopied = copy_from_read_buf(tty, &b, &nr);
1947 uncopied += copy_from_read_buf(tty, &b, &nr);
1948 if (uncopied) {
1949 retval = -EFAULT;
1950 break;
1951 }
1952 }
1953
1954 /* If there is enough space in the read buffer now, let the
1955 * low-level driver know. We use n_tty_chars_in_buffer() to
1956 * check the buffer, as it now knows about canonical mode.
1957 * Otherwise, if the driver is throttled and the line is
1958 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1959 * we won't get any more characters.
1960 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001961 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1962 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001964 }
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 *
2191 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2192 * 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);