blob: 05e72bea9b07d87e31c69e291844428cb642d4be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
Alan Cox4edf1822008-02-08 04:18:44 -080011 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
Alan Cox4edf1822008-02-08 04:18:44 -080023 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010029 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070048#include <linux/audit.h>
49#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000050#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080051#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050052#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010064#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Joe Petersona88a69c2009-01-02 13:40:53 +000066/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
Jiri Slaby70ece7a2012-10-18 22:26:38 +020077struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020078 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020084
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020087
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
96
97 int canon_data;
98 unsigned long canon_head;
99 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +0200100
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
Ivo Sieben98001212013-01-28 13:32:01 +0100104 raw_spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200105};
106
Miloslav Trmac522ed772007-07-15 23:40:56 -0700107static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
109{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200110 struct n_tty_data *ldata = tty->disc_data;
111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700113 return put_user(x, ptr);
114}
115
Linus Torvalds55db4c62011-06-04 06:33:24 +0900116/**
117 * n_tty_set__room - receive space
118 * @tty: terminal
119 *
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
123 * "instant".
124 */
125
126static void n_tty_set_room(struct tty_struct *tty)
127{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200128 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500129 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900130 int old_left;
131
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200132 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500133 if (I_PARMRK(tty)) {
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500138 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500140
Linus Torvalds55db4c62011-06-04 06:33:24 +0900141 /*
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
146 */
147 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200148 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900149 old_left = tty->receive_room;
150 tty->receive_room = left;
151
152 /* Did this open up the receive buffer? We may need to flip */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL,
Sasha Levincadf7482012-10-25 14:26:35 -0400155 "scheduling with invalid itty\n");
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200156 schedule_work(&tty->port->buf.work);
157 }
Linus Torvalds55db4c62011-06-04 06:33:24 +0900158}
159
Jiri Slaby57c94122012-10-18 22:26:43 +0200160static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200162 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
163 ldata->read_buf[ldata->read_head] = c;
164 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
165 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167}
168
Alan Cox17b82062008-10-13 10:45:06 +0100169/**
170 * put_tty_queue - add character to tty
171 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200172 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100173 *
174 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes
177 */
178
Jiri Slaby57c94122012-10-18 22:26:43 +0200179static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned long flags;
182 /*
183 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK
185 */
Ivo Sieben98001212013-01-28 13:32:01 +0100186 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200187 put_tty_queue_nolock(c, ldata);
Ivo Sieben98001212013-01-28 13:32:01 +0100188 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/**
192 * check_unthrottle - allow new receive data
193 * @tty; tty device
194 *
Alan Cox17b82062008-10-13 10:45:06 +0100195 * Check whether to call the driver unthrottle functions
196 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800197 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * this is not guaranteed.
199 */
Alan Cox4edf1822008-02-08 04:18:44 -0800200static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alan Cox39c2e602008-04-30 00:54:18 -0700202 if (tty->count)
203 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
207 * reset_buffer_flags - reset buffer state
208 * @tty: terminal to reset
209 *
Alan Cox4edf1822008-02-08 04:18:44 -0800210 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * and make sure the driver is unthrottled. Called
212 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100213 *
214 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void reset_buffer_flags(struct tty_struct *tty)
218{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200219 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned long flags;
221
Ivo Sieben98001212013-01-28 13:32:01 +0100222 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200223 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Ivo Sieben98001212013-01-28 13:32:01 +0100224 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000225
Jiri Slabybddc7152012-10-18 22:26:42 +0200226 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200227 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200228 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000229
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900232 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/**
236 * n_tty_flush_buffer - clean input queue
237 * @tty: terminal device
238 *
239 * Flush the input buffer. Called when the line discipline is
240 * being closed, when the tty layer wants the buffer flushed (eg
241 * at hangup) or when the N_TTY line discipline internally has to
242 * clean the pending queue (for example some signals).
243 *
Alan Cox17b82062008-10-13 10:45:06 +0100244 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Alan Cox4edf1822008-02-08 04:18:44 -0800246
247static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Alan Cox04f378b2008-04-30 00:53:29 -0700249 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* clear everything and unthrottle the driver */
251 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!tty->link)
254 return;
255
Alan Cox04f378b2008-04-30 00:53:29 -0700256 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (tty->link->packet) {
258 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
259 wake_up_interruptible(&tty->link->read_wait);
260 }
Alan Cox04f378b2008-04-30 00:53:29 -0700261 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/**
265 * n_tty_chars_in_buffer - report available bytes
266 * @tty: tty device
267 *
268 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800269 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100270 *
271 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Alan Cox4edf1822008-02-08 04:18:44 -0800273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
275{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200276 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 unsigned long flags;
278 ssize_t n = 0;
279
Ivo Sieben98001212013-01-28 13:32:01 +0100280 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200281 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200282 n = ldata->read_cnt;
283 } else if (ldata->canon_data) {
284 n = (ldata->canon_head > ldata->read_tail) ?
285 ldata->canon_head - ldata->read_tail :
286 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Ivo Sieben98001212013-01-28 13:32:01 +0100288 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return n;
290}
291
292/**
293 * is_utf8_continuation - utf8 multibyte check
294 * @c: byte to check
295 *
296 * Returns true if the utf8 character 'c' is a multibyte continuation
297 * character. We use this to correctly compute the on screen size
298 * of the character when printing
299 */
Alan Cox4edf1822008-02-08 04:18:44 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static inline int is_utf8_continuation(unsigned char c)
302{
303 return (c & 0xc0) == 0x80;
304}
305
306/**
307 * is_continuation - multibyte check
308 * @c: byte to check
309 *
310 * Returns true if the utf8 character 'c' is a multibyte continuation
311 * character and the terminal is in unicode mode.
312 */
Alan Cox4edf1822008-02-08 04:18:44 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static inline int is_continuation(unsigned char c, struct tty_struct *tty)
315{
316 return I_IUTF8(tty) && is_utf8_continuation(c);
317}
318
319/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000320 * do_output_char - output one character
321 * @c: character (or partial unicode symbol)
322 * @tty: terminal device
323 * @space: space available in tty driver write buffer
324 *
325 * This is a helper function that handles one output character
326 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600327 * doing OPOST processing and putting the results in the
328 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000329 *
330 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
331 * and NLDLY. They simply aren't relevant in the world today.
332 * If you ever need them, add them here.
333 *
334 * Returns the number of bytes of buffer space used or -1 if
335 * no space left.
336 *
337 * Locking: should be called under the output_lock to protect
338 * the column state and space left in the buffer
339 */
340
341static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
342{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200343 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000344 int spaces;
345
346 if (!space)
347 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000348
Joe Petersona88a69c2009-01-02 13:40:53 +0000349 switch (c) {
350 case '\n':
351 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200352 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000353 if (O_ONLCR(tty)) {
354 if (space < 2)
355 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200356 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700357 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000358 return 2;
359 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200360 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000361 break;
362 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200363 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000364 return 0;
365 if (O_OCRNL(tty)) {
366 c = '\n';
367 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200368 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000369 break;
370 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200371 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000372 break;
373 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200374 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 if (O_TABDLY(tty) == XTABS) {
376 if (space < spaces)
377 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200378 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000379 tty->ops->write(tty, " ", spaces);
380 return spaces;
381 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200382 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000383 break;
384 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200385 if (ldata->column > 0)
386 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000387 break;
388 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000389 if (!iscntrl(c)) {
390 if (O_OLCUC(tty))
391 c = toupper(c);
392 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200393 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000394 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000395 break;
396 }
397
398 tty_put_char(tty, c);
399 return 1;
400}
401
402/**
403 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * @c: character (or partial unicode symbol)
405 * @tty: terminal device
406 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600407 * Output one character with OPOST processing.
408 * Returns -1 when the output device is full and the character
409 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000411 * Locking: output_lock to protect column state and space left
412 * (also, this is called from n_tty_write under the
413 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
Alan Cox4edf1822008-02-08 04:18:44 -0800415
Joe Petersona88a69c2009-01-02 13:40:53 +0000416static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jiri Slabybddc7152012-10-18 22:26:42 +0200418 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000419 int space, retval;
420
Jiri Slabybddc7152012-10-18 22:26:42 +0200421 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alan Coxf34d7a52008-04-30 00:54:13 -0700423 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000424 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jiri Slabybddc7152012-10-18 22:26:42 +0200426 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000427 if (retval < 0)
428 return -1;
429 else
430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000434 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600436 * @buf: character buffer
437 * @nr: number of bytes to output
438 *
439 * Output a block of characters with OPOST processing.
440 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * This path is used to speed up block console writes, among other
443 * things when processing blocks of output data. It handles only
444 * the simple cases normally found and helps to generate blocks of
445 * symbols for the console driver and thus improve performance.
446 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000447 * Locking: output_lock to protect column state and space left
448 * (also, this is called from n_tty_write under the
449 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Alan Cox4edf1822008-02-08 04:18:44 -0800451
Joe Petersona88a69c2009-01-02 13:40:53 +0000452static ssize_t process_output_block(struct tty_struct *tty,
453 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200455 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100457 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 const unsigned char *cp;
459
Jiri Slabybddc7152012-10-18 22:26:42 +0200460 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000461
Alan Coxf34d7a52008-04-30 00:54:13 -0700462 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000463 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200464 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (nr > space)
468 nr = space;
469
470 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000471 unsigned char c = *cp;
472
473 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case '\n':
475 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200476 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (O_ONLCR(tty))
478 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200479 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200482 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto break_out;
484 if (O_OCRNL(tty))
485 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200486 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 case '\t':
489 goto break_out;
490 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200491 if (ldata->column > 0)
492 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000495 if (!iscntrl(c)) {
496 if (O_OLCUC(tty))
497 goto break_out;
498 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200499 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 }
503 }
504break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700505 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000506
Jiri Slabybddc7152012-10-18 22:26:42 +0200507 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return i;
509}
510
Joe Petersona88a69c2009-01-02 13:40:53 +0000511/**
512 * process_echoes - write pending echo characters
513 * @tty: terminal device
514 *
515 * Write previously buffered echo (and other ldisc-generated)
516 * characters to the tty.
517 *
518 * Characters generated by the ldisc (including echoes) need to
519 * be buffered because the driver's write buffer can fill during
520 * heavy program output. Echoing straight to the driver will
521 * often fail under these conditions, causing lost characters and
522 * resulting mismatches of ldisc state information.
523 *
524 * Since the ldisc state must represent the characters actually sent
525 * to the driver at the time of the write, operations like certain
526 * changes in column state are also saved in the buffer and executed
527 * here.
528 *
529 * A circular fifo buffer is used so that the most recent characters
530 * are prioritized. Also, when control characters are echoed with a
531 * prefixed "^", the pair is treated atomically and thus not separated.
532 *
533 * Locking: output_lock to protect column state and space left,
534 * echo_lock to protect the echo buffer
535 */
536
537static void process_echoes(struct tty_struct *tty)
538{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200539 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000540 int space, nr;
541 unsigned char c;
542 unsigned char *cp, *buf_end;
543
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200544 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000545 return;
546
Jiri Slabybddc7152012-10-18 22:26:42 +0200547 mutex_lock(&ldata->output_lock);
548 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000549
550 space = tty_write_room(tty);
551
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200552 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
553 cp = ldata->echo_buf + ldata->echo_pos;
554 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000555 while (nr > 0) {
556 c = *cp;
557 if (c == ECHO_OP_START) {
558 unsigned char op;
559 unsigned char *opp;
560 int no_space_left = 0;
561
562 /*
563 * If the buffer byte is the start of a multi-byte
564 * operation, get the next byte, which is either the
565 * op code or a control character value.
566 */
567 opp = cp + 1;
568 if (opp == buf_end)
569 opp -= N_TTY_BUF_SIZE;
570 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000571
Joe Petersona88a69c2009-01-02 13:40:53 +0000572 switch (op) {
573 unsigned int num_chars, num_bs;
574
575 case ECHO_OP_ERASE_TAB:
576 if (++opp == buf_end)
577 opp -= N_TTY_BUF_SIZE;
578 num_chars = *opp;
579
580 /*
581 * Determine how many columns to go back
582 * in order to erase the tab.
583 * This depends on the number of columns
584 * used by other characters within the tab
585 * area. If this (modulo 8) count is from
586 * the start of input rather than from a
587 * previous tab, we offset by canon column.
588 * Otherwise, tab spacing is normal.
589 */
590 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200591 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000592 num_bs = 8 - (num_chars & 7);
593
594 if (num_bs > space) {
595 no_space_left = 1;
596 break;
597 }
598 space -= num_bs;
599 while (num_bs--) {
600 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200601 if (ldata->column > 0)
602 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000603 }
604 cp += 3;
605 nr -= 3;
606 break;
607
608 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200609 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000610 cp += 2;
611 nr -= 2;
612 break;
613
614 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200615 if (ldata->column > 0)
616 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000617 cp += 2;
618 nr -= 2;
619 break;
620
621 case ECHO_OP_START:
622 /* This is an escaped echo op start code */
623 if (!space) {
624 no_space_left = 1;
625 break;
626 }
627 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200628 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000629 space--;
630 cp += 2;
631 nr -= 2;
632 break;
633
634 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000635 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600636 * If the op is not a special byte code,
637 * it is a ctrl char tagged to be echoed
638 * as "^X" (where X is the letter
639 * representing the control char).
640 * Note that we must ensure there is
641 * enough space for the whole ctrl pair.
642 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000643 */
Joe Peterson62b26352009-09-09 15:03:47 -0600644 if (space < 2) {
645 no_space_left = 1;
646 break;
647 }
648 tty_put_char(tty, '^');
649 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200650 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600651 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000652 cp += 2;
653 nr -= 2;
654 }
655
656 if (no_space_left)
657 break;
658 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600659 if (O_OPOST(tty) &&
660 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
661 int retval = do_output_char(c, tty, space);
662 if (retval < 0)
663 break;
664 space -= retval;
665 } else {
666 if (!space)
667 break;
668 tty_put_char(tty, c);
669 space -= 1;
670 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000671 cp += 1;
672 nr -= 1;
673 }
674
675 /* When end of circular buffer reached, wrap around */
676 if (cp >= buf_end)
677 cp -= N_TTY_BUF_SIZE;
678 }
679
680 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200681 ldata->echo_pos = 0;
682 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200683 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000684 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200685 int num_processed = ldata->echo_cnt - nr;
686 ldata->echo_pos += num_processed;
687 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
688 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000689 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200690 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000691 }
692
Jiri Slabybddc7152012-10-18 22:26:42 +0200693 mutex_unlock(&ldata->echo_lock);
694 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000695
696 if (tty->ops->flush_chars)
697 tty->ops->flush_chars(tty);
698}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000701 * add_echo_byte - add a byte to the echo buffer
702 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200703 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000704 *
705 * Add a character or operation byte to the echo buffer.
706 *
707 * Should be called under the echo lock to protect the echo buffer.
708 */
709
Jiri Slaby57c94122012-10-18 22:26:43 +0200710static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000711{
712 int new_byte_pos;
713
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200714 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000715 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200716 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000717
718 /*
719 * Since the buffer start position needs to be advanced,
720 * be sure to step by a whole operation byte group.
721 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200722 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
723 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 (N_TTY_BUF_SIZE - 1)] ==
725 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200726 ldata->echo_pos += 3;
727 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000728 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200729 ldata->echo_pos += 2;
730 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000731 }
732 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200733 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000734 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200735 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000736
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200737 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200739 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000740 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200741 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000742 }
743
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200744 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000745}
746
747/**
748 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200749 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000750 *
751 * Add an operation to the echo buffer to move back one column.
752 *
753 * Locking: echo_lock to protect the echo buffer
754 */
755
Jiri Slaby57c94122012-10-18 22:26:43 +0200756static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000757{
Jiri Slabybddc7152012-10-18 22:26:42 +0200758 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200759 add_echo_byte(ECHO_OP_START, ldata);
760 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200761 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000762}
763
764/**
765 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200766 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000767 *
768 * Add an operation to the echo buffer to set the canon column
769 * to the current column.
770 *
771 * Locking: echo_lock to protect the echo buffer
772 */
773
Jiri Slaby57c94122012-10-18 22:26:43 +0200774static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000775{
Jiri Slabybddc7152012-10-18 22:26:42 +0200776 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200777 add_echo_byte(ECHO_OP_START, ldata);
778 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200779 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000780}
781
782/**
783 * echo_erase_tab - add operation to erase a tab
784 * @num_chars: number of character columns already used
785 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200786 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000787 *
788 * Add an operation to the echo buffer to erase a tab.
789 *
790 * Called by the eraser function, which knows how many character
791 * columns have been used since either a previous tab or the start
792 * of input. This information will be used later, along with
793 * canon column (if applicable), to go back the correct number
794 * of columns.
795 *
796 * Locking: echo_lock to protect the echo buffer
797 */
798
799static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200800 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000801{
Jiri Slabybddc7152012-10-18 22:26:42 +0200802 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000803
Jiri Slaby57c94122012-10-18 22:26:43 +0200804 add_echo_byte(ECHO_OP_START, ldata);
805 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000806
807 /* We only need to know this modulo 8 (tab spacing) */
808 num_chars &= 7;
809
810 /* Set the high bit as a flag if num_chars is after a previous tab */
811 if (after_tab)
812 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000813
Jiri Slaby57c94122012-10-18 22:26:43 +0200814 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000815
Jiri Slabybddc7152012-10-18 22:26:42 +0200816 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000817}
818
819/**
820 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * @c: unicode byte to echo
822 * @tty: terminal device
823 *
Alan Cox4edf1822008-02-08 04:18:44 -0800824 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100826 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000827 * This variant does not treat control characters specially.
828 *
829 * Locking: echo_lock to protect the echo buffer
830 */
831
Jiri Slaby57c94122012-10-18 22:26:43 +0200832static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000833{
Jiri Slabybddc7152012-10-18 22:26:42 +0200834 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000835 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200836 add_echo_byte(ECHO_OP_START, ldata);
837 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200839 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000840 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200841 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000842}
843
844/**
845 * echo_char - echo a character
846 * @c: unicode byte to echo
847 * @tty: terminal device
848 *
849 * Echo user input back onto the screen. This must be called only when
850 * L_ECHO(tty) is true. Called from the driver receive_buf path.
851 *
Joe Peterson62b26352009-09-09 15:03:47 -0600852 * This variant tags control characters to be echoed as "^X"
853 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000854 *
855 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857
858static void echo_char(unsigned char c, struct tty_struct *tty)
859{
Jiri Slabybddc7152012-10-18 22:26:42 +0200860 struct n_tty_data *ldata = tty->disc_data;
861
862 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000863
864 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200865 add_echo_byte(ECHO_OP_START, ldata);
866 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000867 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600868 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200869 add_echo_byte(ECHO_OP_START, ldata);
870 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000871 }
872
Jiri Slabybddc7152012-10-18 22:26:42 +0200873 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Alan Cox17b82062008-10-13 10:45:06 +0100876/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000877 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200878 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100879 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000880
Jiri Slaby57c94122012-10-18 22:26:43 +0200881static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200883 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200884 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200885 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887}
888
889/**
890 * eraser - handle erase function
891 * @c: character input
892 * @tty: terminal device
893 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200894 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * present in the stream from the driver layer. Handles the complexities
896 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100897 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000898 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Alan Cox4edf1822008-02-08 04:18:44 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static void eraser(unsigned char c, struct tty_struct *tty)
902{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200903 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 enum { ERASE, WERASE, KILL } kill_type;
905 int head, seen_alnums, cnt;
906 unsigned long flags;
907
Alan Cox17b82062008-10-13 10:45:06 +0100908 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200909 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000910 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return;
912 }
913 if (c == ERASE_CHAR(tty))
914 kill_type = ERASE;
915 else if (c == WERASE_CHAR(tty))
916 kill_type = WERASE;
917 else {
918 if (!L_ECHO(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100919 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200920 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200922 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100923 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
925 }
926 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Ivo Sieben98001212013-01-28 13:32:01 +0100927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200928 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200930 ldata->read_head = ldata->canon_head;
Ivo Sieben98001212013-01-28 13:32:01 +0100931 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200932 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 echo_char(KILL_CHAR(tty), tty);
934 /* Add a newline if ECHOK is on and ECHOKE is off. */
935 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200936 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return;
938 }
939 kill_type = KILL;
940 }
941
942 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100943 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200944 while (ldata->read_head != ldata->canon_head) {
945 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* erase a single possibly multibyte character */
948 do {
949 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200950 c = ldata->read_buf[head];
951 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* do not partially erase */
954 if (is_continuation(c, tty))
955 break;
956
957 if (kill_type == WERASE) {
958 /* Equivalent to BSD's ALTWERASE. */
959 if (isalnum(c) || c == '_')
960 seen_alnums++;
961 else if (seen_alnums)
962 break;
963 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200964 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Ivo Sieben98001212013-01-28 13:32:01 +0100965 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200966 ldata->read_head = head;
967 ldata->read_cnt -= cnt;
Ivo Sieben98001212013-01-28 13:32:01 +0100968 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (L_ECHO(tty)) {
970 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200971 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200972 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200973 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 /* if cnt > 1, output a multi-byte character */
976 echo_char(c, tty);
977 while (--cnt > 0) {
978 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200979 echo_char_raw(ldata->read_buf[head],
980 ldata);
981 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
984 echo_char(ERASE_CHAR(tty), tty);
985 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000986 unsigned int num_chars = 0;
987 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200988 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Joe Petersona88a69c2009-01-02 13:40:53 +0000990 /*
991 * Count the columns used for characters
992 * since the start of input or after a
993 * previous tab.
994 * This info is used to go back the correct
995 * number of columns.
996 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200997 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000998 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200999 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +00001000 if (c == '\t') {
1001 after_tab = 1;
1002 break;
Alan Cox300a6202009-01-02 13:41:04 +00001003 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001005 num_chars += 2;
1006 } else if (!is_continuation(c, tty)) {
1007 num_chars++;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001010 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else {
1012 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001013 echo_char_raw('\b', ldata);
1014 echo_char_raw(' ', ldata);
1015 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001018 echo_char_raw('\b', ldata);
1019 echo_char_raw(' ', ldata);
1020 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
1023 }
1024 if (kill_type == ERASE)
1025 break;
1026 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001027 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001028 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/**
1032 * isig - handle the ISIG optio
1033 * @sig: signal
1034 * @tty: terminal
1035 * @flush: force flush
1036 *
1037 * Called when a signal is being sent due to terminal input. This
1038 * may caus terminal flushing to take place according to the termios
1039 * settings and character used. Called from the driver receive_buf
1040 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001041 *
1042 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Alan Cox4edf1822008-02-08 04:18:44 -08001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static inline void isig(int sig, struct tty_struct *tty, int flush)
1046{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001047 if (tty->pgrp)
1048 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (flush || !L_NOFLSH(tty)) {
1050 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001051 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
1054
1055/**
1056 * n_tty_receive_break - handle break
1057 * @tty: terminal
1058 *
1059 * An RS232 break event has been hit in the incoming bitstream. This
1060 * can cause a variety of events depending upon the termios settings.
1061 *
1062 * Called from the receive_buf path so single threaded.
1063 */
Alan Cox4edf1822008-02-08 04:18:44 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static inline void n_tty_receive_break(struct tty_struct *tty)
1066{
Jiri Slaby57c94122012-10-18 22:26:43 +02001067 struct n_tty_data *ldata = tty->disc_data;
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (I_IGNBRK(tty))
1070 return;
1071 if (I_BRKINT(tty)) {
1072 isig(SIGINT, tty, 1);
1073 return;
1074 }
1075 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001076 put_tty_queue('\377', ldata);
1077 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001079 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 wake_up_interruptible(&tty->read_wait);
1081}
1082
1083/**
1084 * n_tty_receive_overrun - handle overrun reporting
1085 * @tty: terminal
1086 *
1087 * Data arrived faster than we could process it. While the tty
1088 * driver has flagged this the bits that were missed are gone
1089 * forever.
1090 *
1091 * Called from the receive_buf path so single threaded. Does not
1092 * need locking as num_overrun and overrun_time are function
1093 * private.
1094 */
Alan Cox4edf1822008-02-08 04:18:44 -08001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static inline void n_tty_receive_overrun(struct tty_struct *tty)
1097{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001098 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 char buf[64];
1100
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001101 ldata->num_overrun++;
1102 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1103 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1105 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001106 ldata->num_overrun);
1107 ldata->overrun_time = jiffies;
1108 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110}
1111
1112/**
1113 * n_tty_receive_parity_error - error notifier
1114 * @tty: terminal device
1115 * @c: character
1116 *
1117 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001118 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
1120static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1121 unsigned char c)
1122{
Jiri Slaby57c94122012-10-18 22:26:43 +02001123 struct n_tty_data *ldata = tty->disc_data;
1124
Alan Cox4edf1822008-02-08 04:18:44 -08001125 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001128 put_tty_queue('\377', ldata);
1129 put_tty_queue('\0', ldata);
1130 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001132 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001134 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 wake_up_interruptible(&tty->read_wait);
1136}
1137
1138/**
1139 * n_tty_receive_char - perform processing
1140 * @tty: terminal device
1141 * @c: character
1142 *
1143 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001144 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * driver above.
1146 */
1147
1148static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1149{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001150 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001152 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001154 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001155 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return;
1157 }
Alan Cox4edf1822008-02-08 04:18:44 -08001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (I_ISTRIP(tty))
1160 c &= 0x7f;
1161 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001162 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
hyc@symas.com26df6d12010-06-22 10:14:49 -07001164 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001165 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001166 return;
1167 }
1168
Joe Peterson54d2a372008-02-06 01:37:59 -08001169 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001170 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1171 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001172 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001173 process_echoes(tty);
1174 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (tty->closing) {
1177 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001178 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001180 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001181 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 stop_tty(tty);
1183 }
1184 return;
1185 }
1186
1187 /*
1188 * If the previous character was LNEXT, or we know that this
1189 * character is not one of the characters that we'll have to
1190 * handle specially, do shortcut processing to speed things
1191 * up.
1192 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001193 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001194 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001195 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001196 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001197 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001198 if (L_ECHO(tty))
1199 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001200 return;
1201 }
1202 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001203 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001205 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001206 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001208 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001210 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001211 put_tty_queue(c, ldata);
1212 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return;
1214 }
Alan Cox4edf1822008-02-08 04:18:44 -08001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (I_IXON(tty)) {
1217 if (c == START_CHAR(tty)) {
1218 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001219 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return;
1221 }
1222 if (c == STOP_CHAR(tty)) {
1223 stop_tty(tty);
1224 return;
1225 }
1226 }
Joe Peterson575537b32008-04-30 00:53:30 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (L_ISIG(tty)) {
1229 int signal;
1230 signal = SIGINT;
1231 if (c == INTR_CHAR(tty))
1232 goto send_signal;
1233 signal = SIGQUIT;
1234 if (c == QUIT_CHAR(tty))
1235 goto send_signal;
1236 signal = SIGTSTP;
1237 if (c == SUSP_CHAR(tty)) {
1238send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001239 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001240 * Note that we do not use isig() here because we want
1241 * the order to be:
1242 * 1) flush, 2) echo, 3) signal
1243 */
1244 if (!L_NOFLSH(tty)) {
1245 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001246 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001247 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001248 if (I_IXON(tty))
1249 start_tty(tty);
1250 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001251 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001252 process_echoes(tty);
1253 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001254 if (tty->pgrp)
1255 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return;
1257 }
1258 }
Joe Peterson575537b32008-04-30 00:53:30 -07001259
1260 if (c == '\r') {
1261 if (I_IGNCR(tty))
1262 return;
1263 if (I_ICRNL(tty))
1264 c = '\n';
1265 } else if (c == '\n' && I_INLCR(tty))
1266 c = '\r';
1267
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001268 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1270 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1271 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001272 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
1274 }
1275 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001276 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001278 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001280 echo_char_raw('^', ldata);
1281 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001282 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 }
1285 return;
1286 }
1287 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1288 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001289 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Jiri Slaby57c94122012-10-18 22:26:43 +02001291 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001293 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001294 while (tail != ldata->read_head) {
1295 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1297 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001298 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return;
1300 }
1301 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001302 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001303 if (L_ECHO(tty))
1304 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001305 return;
1306 }
1307 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001308 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001309 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 goto handle_newline;
1312 }
1313 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001314 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001315 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001316 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001317 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 c = __DISABLED_CHAR;
1319 goto handle_newline;
1320 }
1321 if ((c == EOL_CHAR(tty)) ||
1322 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001323 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1324 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001325 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001326 if (L_ECHO(tty))
1327 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001328 return;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1332 */
1333 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001335 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001336 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001338 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 /*
1341 * XXX does PARMRK doubling happen for
1342 * EOL_CHAR and EOL2_CHAR?
1343 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001344 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001345 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Alan Cox4edf1822008-02-08 04:18:44 -08001347handle_newline:
Ivo Sieben98001212013-01-28 13:32:01 +01001348 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001349 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001350 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001351 ldata->canon_head = ldata->read_head;
1352 ldata->canon_data++;
Ivo Sieben98001212013-01-28 13:32:01 +01001353 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1355 if (waitqueue_active(&tty->read_wait))
1356 wake_up_interruptible(&tty->read_wait);
1357 return;
1358 }
1359 }
Alan Cox4edf1822008-02-08 04:18:44 -08001360
Joe Petersonacc71bb2009-01-02 13:43:32 +00001361 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001362 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001363 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001364 if (L_ECHO(tty))
1365 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001366 return;
1367 }
1368 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001369 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001371 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 else {
1373 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001374 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001375 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 echo_char(c, tty);
1377 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001378 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Joe Petersonacc71bb2009-01-02 13:43:32 +00001381 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001382 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Jiri Slaby57c94122012-10-18 22:26:43 +02001384 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388/**
1389 * n_tty_write_wakeup - asynchronous I/O notifier
1390 * @tty: tty device
1391 *
1392 * Required for the ptys, serial driver etc. since processes
1393 * that attach themselves to the master and rely on ASYNC
1394 * IO must be woken up
1395 */
1396
1397static void n_tty_write_wakeup(struct tty_struct *tty)
1398{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001399 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403/**
1404 * n_tty_receive_buf - data receive
1405 * @tty: terminal device
1406 * @cp: buffer
1407 * @fp: flag buffer
1408 * @count: characters
1409 *
1410 * Called by the terminal driver when a block of characters has
1411 * been received. This function must be called from soft contexts
1412 * not from interrupt context. The driver is responsible for making
1413 * calls one at a time and in order (or using flush_to_ldisc)
1414 */
Alan Cox4edf1822008-02-08 04:18:44 -08001415
Linus Torvalds55db4c62011-06-04 06:33:24 +09001416static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1417 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001419 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 const unsigned char *p;
1421 char *f, flags = TTY_NORMAL;
1422 int i;
1423 char buf[64];
1424 unsigned long cpuflags;
1425
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001426 if (ldata->real_raw) {
Ivo Sieben98001212013-01-28 13:32:01 +01001427 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001428 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1429 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001431 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1432 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1433 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 cp += i;
1435 count -= i;
1436
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001437 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1438 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001440 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1441 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1442 ldata->read_cnt += i;
Ivo Sieben98001212013-01-28 13:32:01 +01001443 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001445 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (f)
1447 flags = *f++;
1448 switch (flags) {
1449 case TTY_NORMAL:
1450 n_tty_receive_char(tty, *p);
1451 break;
1452 case TTY_BREAK:
1453 n_tty_receive_break(tty);
1454 break;
1455 case TTY_PARITY:
1456 case TTY_FRAME:
1457 n_tty_receive_parity_error(tty, *p);
1458 break;
1459 case TTY_OVERRUN:
1460 n_tty_receive_overrun(tty);
1461 break;
1462 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001463 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 tty_name(tty, buf), flags);
1465 break;
1466 }
1467 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001468 if (tty->ops->flush_chars)
1469 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
Linus Torvalds55db4c62011-06-04 06:33:24 +09001472 n_tty_set_room(tty);
1473
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001474 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001475 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1477 if (waitqueue_active(&tty->read_wait))
1478 wake_up_interruptible(&tty->read_wait);
1479 }
1480
1481 /*
1482 * Check the remaining room for the input canonicalization
1483 * mode. We don't want to throttle the driver if we're in
1484 * canonical mode and don't have a newline yet!
1485 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001486 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001487 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001488
1489 /* FIXME: there is a tiny race here if the receive room check runs
1490 before the other work executes and empties the buffer (upping
1491 the receiving room and unthrottling. We then throttle and get
1492 stuck. This has been observed and traced down by Vincent Pillet/
1493 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496int is_ignored(int sig)
1497{
1498 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001499 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/**
1503 * n_tty_set_termios - termios data changed
1504 * @tty: terminal
1505 * @old: previous data
1506 *
1507 * Called by the tty layer when the user changes termios flags so
1508 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001509 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * guaranteed that this function will not be re-entered or in progress
1511 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001512 *
1513 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Alan Cox4edf1822008-02-08 04:18:44 -08001515
1516static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001518 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001519 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001520
1521 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001522 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001523 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001524 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001525 ldata->canon_head = ldata->read_tail;
1526 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001527 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001528 }
1529
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001530 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001531 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001532
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001533 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001535 ldata->raw = 1;
1536 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001537 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return;
1539 }
1540 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1541 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1542 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1543 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001544 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001547 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001549 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001552 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1553 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1554 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1555 set_bit('\n', ldata->process_char_map);
1556 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (L_IEXTEN(tty)) {
1558 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001561 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (L_ECHO(tty))
1565 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001566 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 }
1569 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001570 set_bit(START_CHAR(tty), ldata->process_char_map);
1571 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001574 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1575 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1576 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001578 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001579 ldata->raw = 0;
1580 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001582 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1584 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1585 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001586 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001588 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001590 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001591 /* The termios change make the tty ready for I/O */
1592 wake_up_interruptible(&tty->write_wait);
1593 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596/**
1597 * n_tty_close - close the ldisc for this tty
1598 * @tty: device
1599 *
Alan Cox4edf1822008-02-08 04:18:44 -08001600 * Called from the terminal layer when this line discipline is
1601 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 * discipline change. The function will not be called while other
1603 * ldisc methods are in progress.
1604 */
Alan Cox4edf1822008-02-08 04:18:44 -08001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static void n_tty_close(struct tty_struct *tty)
1607{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001608 struct n_tty_data *ldata = tty->disc_data;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 n_tty_flush_buffer(tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001611 kfree(ldata->read_buf);
1612 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001613 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001614 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617/**
1618 * n_tty_open - open an ldisc
1619 * @tty: terminal to open
1620 *
Alan Cox4edf1822008-02-08 04:18:44 -08001621 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * terminal device. Can sleep. Called serialized so that no
1623 * other events will occur in parallel. No further open will occur
1624 * until a close.
1625 */
1626
1627static int n_tty_open(struct tty_struct *tty)
1628{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001629 struct n_tty_data *ldata;
1630
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1632 if (!ldata)
1633 goto err;
1634
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001635 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001636 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
Ivo Sieben98001212013-01-28 13:32:01 +01001639 raw_spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001640
Joe Petersona88a69c2009-01-02 13:40:53 +00001641 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001645 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001646
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001647 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001649 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001650 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 n_tty_set_termios(tty, NULL);
1652 tty->minimum_to_wake = 1;
1653 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001656err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001657 kfree(ldata->read_buf);
1658 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001659 kfree(ldata);
1660err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001661 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664static inline int input_available_p(struct tty_struct *tty, int amt)
1665{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001666 struct n_tty_data *ldata = tty->disc_data;
1667
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001668 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001669 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001670 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001672 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 1;
1674
1675 return 0;
1676}
1677
1678/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001679 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * @tty: terminal device
1681 * @b: user data
1682 * @nr: size of data
1683 *
Alan Cox11a96d12008-10-13 10:46:24 +01001684 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * ICANON is off; it copies characters straight from the tty queue to
1686 * user space directly. It can be profitably called twice; once to
1687 * drain the space from the tail pointer to the (physical) end of the
1688 * buffer, and once to drain the space from the (physical) beginning of
1689 * the buffer to head pointer.
1690 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001691 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 */
Alan Cox4edf1822008-02-08 04:18:44 -08001694
Alan Cox33f0f882006-01-09 20:54:13 -08001695static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 unsigned char __user **b,
1697 size_t *nr)
1698
1699{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001700 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 int retval;
1702 size_t n;
1703 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001704 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 retval = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001707 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001708 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 n = min(*nr, n);
Ivo Sieben98001212013-01-28 13:32:01 +01001710 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001712 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001714 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001715 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1716 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001717 ldata->icanon);
Ivo Sieben98001212013-01-28 13:32:01 +01001718 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001719 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1720 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001721 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001722 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001723 n = 0;
Ivo Sieben98001212013-01-28 13:32:01 +01001724 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *b += n;
1726 *nr -= n;
1727 }
1728 return retval;
1729}
1730
Al Virocc4191d2008-03-29 03:08:48 +00001731extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001732 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734/**
1735 * job_control - check job control
1736 * @tty: tty
1737 * @file: file handle
1738 *
1739 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001740 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001742 *
1743 * FIXME:
1744 * Locking: None - redirected write test is safe, testing
1745 * current->signal should possibly lock current->sighand
1746 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 */
Alan Cox4edf1822008-02-08 04:18:44 -08001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749static int job_control(struct tty_struct *tty, struct file *file)
1750{
1751 /* Job control check -- must be done at start and after
1752 every sleep (POSIX.1 7.1.1.4). */
1753 /* NOTE: not yet done after every sleep pending a thorough
1754 check of the logic of this change. -- jlc */
1755 /* don't stop on /dev/console */
1756 if (file->f_op->write != redirected_tty_write &&
1757 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001758 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001759 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001760 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001762 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001764 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001765 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -ERESTARTSYS;
1767 }
1768 }
1769 return 0;
1770}
Alan Cox4edf1822008-02-08 04:18:44 -08001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773/**
Alan Cox11a96d12008-10-13 10:46:24 +01001774 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * @tty: tty device
1776 * @file: file object
1777 * @buf: userspace buffer pointer
1778 * @nr: size of I/O
1779 *
1780 * Perform reads for the line discipline. We are guaranteed that the
1781 * line discipline will not be closed under us but we may get multiple
1782 * parallel readers and must handle this ourselves. We may also get
1783 * a hangup. Always called in user context, may sleep.
1784 *
1785 * This code must be sure never to sleep through a hangup.
1786 */
Alan Cox4edf1822008-02-08 04:18:44 -08001787
Alan Cox11a96d12008-10-13 10:46:24 +01001788static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 unsigned char __user *buf, size_t nr)
1790{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001791 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned char __user *b = buf;
1793 DECLARE_WAITQUEUE(wait, current);
1794 int c;
1795 int minimum, time;
1796 ssize_t retval = 0;
1797 ssize_t size;
1798 long timeout;
1799 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001800 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001804 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 minimum = time = 0;
1808 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001809 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 time = (HZ / 10) * TIME_CHAR(tty);
1811 minimum = MIN_CHAR(tty);
1812 if (minimum) {
1813 if (time)
1814 tty->minimum_to_wake = 1;
1815 else if (!waitqueue_active(&tty->read_wait) ||
1816 (tty->minimum_to_wake > minimum))
1817 tty->minimum_to_wake = minimum;
1818 } else {
1819 timeout = 0;
1820 if (time) {
1821 timeout = time;
1822 time = 0;
1823 }
1824 tty->minimum_to_wake = minimum = 1;
1825 }
1826 }
1827
1828 /*
1829 * Internal serialization of reads.
1830 */
1831 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001832 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001834 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001835 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -ERESTARTSYS;
1837 }
Alan Cox04f378b2008-04-30 00:53:29 -07001838 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 while (nr) {
1842 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001843 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 unsigned char cs;
1845 if (b != buf)
1846 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001847 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 cs = tty->link->ctrl_status;
1849 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001850 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001851 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 retval = -EFAULT;
1853 b--;
1854 break;
1855 }
1856 nr--;
1857 break;
1858 }
1859 /* This statement must be first before checking for input
1860 so that any interrupt will set the state back to
1861 TASK_RUNNING. */
1862 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1865 ((minimum - (b - buf)) >= 1))
1866 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (!input_available_p(tty, 0)) {
1869 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1870 retval = -EIO;
1871 break;
1872 }
1873 if (tty_hung_up_p(file))
1874 break;
1875 if (!timeout)
1876 break;
1877 if (file->f_flags & O_NONBLOCK) {
1878 retval = -EAGAIN;
1879 break;
1880 }
1881 if (signal_pending(current)) {
1882 retval = -ERESTARTSYS;
1883 break;
1884 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001885 /* FIXME: does n_tty_set_room need locking ? */
1886 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 continue;
1889 }
1890 __set_current_state(TASK_RUNNING);
1891
1892 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001893 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001894 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 retval = -EFAULT;
1896 b--;
1897 break;
1898 }
1899 nr--;
1900 }
1901
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001902 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* N.B. avoid overrun if nr == 0 */
Ivo Sieben98001212013-01-28 13:32:01 +01001904 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001905 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001906 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001908 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001909 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001910 c = ldata->read_buf[ldata->read_tail];
1911 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001913 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (eol) {
1915 /* this test should be redundant:
1916 * we shouldn't be reading data if
1917 * canon_data is 0
1918 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001919 if (--ldata->canon_data < 0)
1920 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Ivo Sieben98001212013-01-28 13:32:01 +01001922 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001925 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 retval = -EFAULT;
1927 b--;
Ivo Sieben98001212013-01-28 13:32:01 +01001928 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930 }
1931 nr--;
1932 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001933 if (eol) {
1934 tty_audit_push(tty);
Ivo Sieben98001212013-01-28 13:32:01 +01001935 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001937 }
Ivo Sieben98001212013-01-28 13:32:01 +01001938 raw_spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Ivo Sieben98001212013-01-28 13:32:01 +01001940 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (retval)
1942 break;
1943 } else {
1944 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001945 /* The copy function takes the read lock and handles
1946 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 uncopied = copy_from_read_buf(tty, &b, &nr);
1948 uncopied += copy_from_read_buf(tty, &b, &nr);
1949 if (uncopied) {
1950 retval = -EFAULT;
1951 break;
1952 }
1953 }
1954
1955 /* If there is enough space in the read buffer now, let the
1956 * low-level driver know. We use n_tty_chars_in_buffer() to
1957 * check the buffer, as it now knows about canonical mode.
1958 * Otherwise, if the driver is throttled and the line is
1959 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1960 * we won't get any more characters.
1961 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001962 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1963 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 if (b - buf >= minimum)
1968 break;
1969 if (time)
1970 timeout = time;
1971 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001972 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 remove_wait_queue(&tty->read_wait, &wait);
1974
1975 if (!waitqueue_active(&tty->read_wait))
1976 tty->minimum_to_wake = minimum;
1977
1978 __set_current_state(TASK_RUNNING);
1979 size = b - buf;
1980 if (size) {
1981 retval = size;
1982 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001983 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001985 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds55db4c62011-06-04 06:33:24 +09001987 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return retval;
1989}
1990
1991/**
Alan Cox11a96d12008-10-13 10:46:24 +01001992 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 * @tty: tty device
1994 * @file: file object
1995 * @buf: userspace buffer pointer
1996 * @nr: size of I/O
1997 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001998 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00002000 * and other such events. Since the receive code will echo characters,
2001 * thus calling driver write methods, the output_lock is used in
2002 * the output processing functions called here as well as in the
2003 * echo processing function to protect the column state and space
2004 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 *
2006 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002007 *
2008 * Locking: output_lock to protect column state and space left
2009 * (note that the process_output*() functions take this
2010 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 */
Alan Cox4edf1822008-02-08 04:18:44 -08002012
Alan Cox11a96d12008-10-13 10:46:24 +01002013static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002014 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 const unsigned char *b = buf;
2017 DECLARE_WAITQUEUE(wait, current);
2018 int c;
2019 ssize_t retval = 0;
2020
2021 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2022 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2023 retval = tty_check_change(tty);
2024 if (retval)
2025 return retval;
2026 }
2027
Joe Petersona88a69c2009-01-02 13:40:53 +00002028 /* Write out any echoed characters that are still pending */
2029 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 add_wait_queue(&tty->write_wait, &wait);
2032 while (1) {
2033 set_current_state(TASK_INTERRUPTIBLE);
2034 if (signal_pending(current)) {
2035 retval = -ERESTARTSYS;
2036 break;
2037 }
2038 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2039 retval = -EIO;
2040 break;
2041 }
2042 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2043 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002044 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (num < 0) {
2046 if (num == -EAGAIN)
2047 break;
2048 retval = num;
2049 goto break_out;
2050 }
2051 b += num;
2052 nr -= num;
2053 if (nr == 0)
2054 break;
2055 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002056 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 break;
2058 b++; nr--;
2059 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002060 if (tty->ops->flush_chars)
2061 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002063 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002064 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002065 if (c < 0) {
2066 retval = c;
2067 goto break_out;
2068 }
2069 if (!c)
2070 break;
2071 b += c;
2072 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 if (!nr)
2076 break;
2077 if (file->f_flags & O_NONBLOCK) {
2078 retval = -EAGAIN;
2079 break;
2080 }
2081 schedule();
2082 }
2083break_out:
2084 __set_current_state(TASK_RUNNING);
2085 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002086 if (b - buf != nr && tty->fasync)
2087 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return (b - buf) ? b - buf : retval;
2089}
2090
2091/**
Alan Cox11a96d12008-10-13 10:46:24 +01002092 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * @tty: terminal device
2094 * @file: file accessing it
2095 * @wait: poll table
2096 *
2097 * Called when the line discipline is asked to poll() for data or
2098 * for special events. This code is not serialized with respect to
2099 * other events save open/close.
2100 *
2101 * This code must be sure never to sleep through a hangup.
2102 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 */
Alan Cox4edf1822008-02-08 04:18:44 -08002104
Alan Cox11a96d12008-10-13 10:46:24 +01002105static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002106 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 unsigned int mask = 0;
2109
2110 poll_wait(file, &tty->read_wait, wait);
2111 poll_wait(file, &tty->write_wait, wait);
2112 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2113 mask |= POLLIN | POLLRDNORM;
2114 if (tty->packet && tty->link->ctrl_status)
2115 mask |= POLLPRI | POLLIN | POLLRDNORM;
2116 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2117 mask |= POLLHUP;
2118 if (tty_hung_up_p(file))
2119 mask |= POLLHUP;
2120 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2121 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2122 tty->minimum_to_wake = MIN_CHAR(tty);
2123 else
2124 tty->minimum_to_wake = 1;
2125 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002126 if (tty->ops->write && !tty_is_writelocked(tty) &&
2127 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2128 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 mask |= POLLOUT | POLLWRNORM;
2130 return mask;
2131}
2132
Jiri Slaby57c94122012-10-18 22:26:43 +02002133static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002134{
2135 int nr, head, tail;
2136
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002137 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002138 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002139 head = ldata->canon_head;
2140 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002141 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2142 /* Skip EOF-chars.. */
2143 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002144 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002145 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002146 nr--;
2147 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2148 }
2149 return nr;
2150}
2151
2152static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2153 unsigned int cmd, unsigned long arg)
2154{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002155 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002156 int retval;
2157
2158 switch (cmd) {
2159 case TIOCOUTQ:
2160 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2161 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002162 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002163 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002164 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002165 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002166 return put_user(retval, (unsigned int __user *) arg);
2167 default:
2168 return n_tty_ioctl_helper(tty, file, cmd, arg);
2169 }
2170}
2171
Alan Coxa352def2008-07-16 21:53:12 +01002172struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002173 .magic = TTY_LDISC_MAGIC,
2174 .name = "n_tty",
2175 .open = n_tty_open,
2176 .close = n_tty_close,
2177 .flush_buffer = n_tty_flush_buffer,
2178 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002179 .read = n_tty_read,
2180 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002181 .ioctl = n_tty_ioctl,
2182 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002183 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002184 .receive_buf = n_tty_receive_buf,
2185 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002187
2188/**
2189 * n_tty_inherit_ops - inherit N_TTY methods
2190 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2191 *
George Spelvin593fb1ae42013-02-12 02:00:43 -05002192 * Enables a 'subclass' line discipline to 'inherit' N_TTY
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002193 * methods.
2194 */
2195
2196void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2197{
2198 *ops = tty_ldisc_N_TTY;
2199 ops->owner = NULL;
2200 ops->refcount = ops->flags = 0;
2201}
2202EXPORT_SYMBOL_GPL(n_tty_inherit_ops);