blob: 531e539dbfcfe503169ccd46b98472c69f25f1d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * n_tty.c --- implements the N_TTY line discipline.
Alan Cox4edf1822008-02-08 04:18:44 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
Alan Cox4edf1822008-02-08 04:18:44 -080011 * to N_TTY if it can not switch to any other line discipline.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Written by Theodore Ts'o, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
Alan Cox4edf1822008-02-08 04:18:44 -080017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
Alan Cox4edf1822008-02-08 04:18:44 -080023 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
Alan Cox11a96d12008-10-13 10:46:24 +010029 * Also fixed a bug in BLOCKING mode where n_tty_write returns
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
Miloslav Trmac522ed772007-07-15 23:40:56 -070048#include <linux/audit.h>
49#include <linux/file.h>
Alan Cox300a6202009-01-02 13:41:04 +000050#include <linux/uaccess.h>
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -080051#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* number of characters left in xmit buffer before select has we have room */
55#define WAKEUP_CHARS 256
56
57/*
58 * This defines the low- and high-watermarks for throttling and
59 * unthrottling the TTY driver. These watermarks are used for
60 * controlling the space in the read buffer.
61 */
62#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
Thorsten Wißmannbbd20752011-12-08 17:47:33 +010063#define TTY_THRESHOLD_UNTHROTTLE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Joe Petersona88a69c2009-01-02 13:40:53 +000065/*
66 * Special byte codes used in the echo buffer to represent operations
67 * or special handling of characters. Bytes in the echo buffer that
68 * are not part of such special blocks are treated as normal character
69 * codes.
70 */
71#define ECHO_OP_START 0xff
72#define ECHO_OP_MOVE_BACK_COL 0x80
73#define ECHO_OP_SET_CANON_COL 0x81
74#define ECHO_OP_ERASE_TAB 0x82
75
Jiri Slaby70ece7a2012-10-18 22:26:38 +020076struct n_tty_data {
Jiri Slaby53c5ee22012-10-18 22:26:39 +020077 unsigned int column;
78 unsigned long overrun_time;
79 int num_overrun;
80
81 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
82 unsigned char echo_overrun:1;
Jiri Slaby3fe780b2012-10-18 22:26:40 +020083
84 DECLARE_BITMAP(process_char_map, 256);
85 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +020086
87 char *read_buf;
88 int read_head;
89 int read_tail;
90 int read_cnt;
91
92 unsigned char *echo_buf;
93 unsigned int echo_pos;
94 unsigned int echo_cnt;
95
96 int canon_data;
97 unsigned long canon_head;
98 unsigned int canon_column;
Jiri Slabybddc7152012-10-18 22:26:42 +020099
100 struct mutex atomic_read_lock;
101 struct mutex output_lock;
102 struct mutex echo_lock;
103 spinlock_t read_lock;
Jiri Slaby70ece7a2012-10-18 22:26:38 +0200104};
105
Miloslav Trmac522ed772007-07-15 23:40:56 -0700106static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
107 unsigned char __user *ptr)
108{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200109 struct n_tty_data *ldata = tty->disc_data;
110
111 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700112 return put_user(x, ptr);
113}
114
Linus Torvalds55db4c62011-06-04 06:33:24 +0900115/**
116 * n_tty_set__room - receive space
117 * @tty: terminal
118 *
119 * Called by the driver to find out how much data it is
120 * permitted to feed to the line discipline without any being lost
121 * and thus to manage flow control. Not serialized. Answers for the
122 * "instant".
123 */
124
125static void n_tty_set_room(struct tty_struct *tty)
126{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200127 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500128 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900129 int old_left;
130
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200131 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500132 if (I_PARMRK(tty)) {
133 /* Multiply read_cnt by 3, since each byte might take up to
134 * three times as many spaces when PARMRK is set (depending on
135 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200136 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500137 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200138 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500139
Linus Torvalds55db4c62011-06-04 06:33:24 +0900140 /*
141 * If we are doing input canonicalization, and there are no
142 * pending newlines, let characters through without limit, so
143 * that erase characters will be handled. Other excess
144 * characters will be beeped.
145 */
146 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200147 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900148 old_left = tty->receive_room;
149 tty->receive_room = left;
150
151 /* Did this open up the receive buffer? We may need to flip */
152 if (left && !old_left)
153 schedule_work(&tty->buf.work);
154}
155
Jiri Slaby57c94122012-10-18 22:26:43 +0200156static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200158 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
159 ldata->read_buf[ldata->read_head] = c;
160 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
161 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163}
164
Alan Cox17b82062008-10-13 10:45:06 +0100165/**
166 * put_tty_queue - add character to tty
167 * @c: character
Jiri Slaby57c94122012-10-18 22:26:43 +0200168 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100169 *
170 * Add a character to the tty read_buf queue. This is done under the
171 * read_lock to serialize character addition and also to protect us
172 * against parallel reads or flushes
173 */
174
Jiri Slaby57c94122012-10-18 22:26:43 +0200175static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned long flags;
178 /*
179 * The problem of stomping on the buffers ends here.
180 * Why didn't anyone see this one coming? --AJK
181 */
Jiri Slabybddc7152012-10-18 22:26:42 +0200182 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200183 put_tty_queue_nolock(c, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200184 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/**
188 * check_unthrottle - allow new receive data
189 * @tty; tty device
190 *
Alan Cox17b82062008-10-13 10:45:06 +0100191 * Check whether to call the driver unthrottle functions
192 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800193 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * this is not guaranteed.
195 */
Alan Cox4edf1822008-02-08 04:18:44 -0800196static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Alan Cox39c2e602008-04-30 00:54:18 -0700198 if (tty->count)
199 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
203 * reset_buffer_flags - reset buffer state
204 * @tty: terminal to reset
205 *
Alan Cox4edf1822008-02-08 04:18:44 -0800206 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * and make sure the driver is unthrottled. Called
208 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100209 *
210 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static void reset_buffer_flags(struct tty_struct *tty)
214{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200215 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned long flags;
217
Jiri Slabybddc7152012-10-18 22:26:42 +0200218 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200219 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200220 spin_unlock_irqrestore(&ldata->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000221
Jiri Slabybddc7152012-10-18 22:26:42 +0200222 mutex_lock(&ldata->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200223 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +0200224 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000225
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200226 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200227 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900228 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/**
232 * n_tty_flush_buffer - clean input queue
233 * @tty: terminal device
234 *
235 * Flush the input buffer. Called when the line discipline is
236 * being closed, when the tty layer wants the buffer flushed (eg
237 * at hangup) or when the N_TTY line discipline internally has to
238 * clean the pending queue (for example some signals).
239 *
Alan Cox17b82062008-10-13 10:45:06 +0100240 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Alan Cox4edf1822008-02-08 04:18:44 -0800242
243static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Alan Cox04f378b2008-04-30 00:53:29 -0700245 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* clear everything and unthrottle the driver */
247 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!tty->link)
250 return;
251
Alan Cox04f378b2008-04-30 00:53:29 -0700252 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (tty->link->packet) {
254 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
255 wake_up_interruptible(&tty->link->read_wait);
256 }
Alan Cox04f378b2008-04-30 00:53:29 -0700257 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/**
261 * n_tty_chars_in_buffer - report available bytes
262 * @tty: tty device
263 *
264 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800265 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100266 *
267 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Alan Cox4edf1822008-02-08 04:18:44 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
271{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200272 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 unsigned long flags;
274 ssize_t n = 0;
275
Jiri Slabybddc7152012-10-18 22:26:42 +0200276 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200277 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200278 n = ldata->read_cnt;
279 } else if (ldata->canon_data) {
280 n = (ldata->canon_head > ldata->read_tail) ?
281 ldata->canon_head - ldata->read_tail :
282 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200284 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return n;
286}
287
288/**
289 * is_utf8_continuation - utf8 multibyte check
290 * @c: byte to check
291 *
292 * Returns true if the utf8 character 'c' is a multibyte continuation
293 * character. We use this to correctly compute the on screen size
294 * of the character when printing
295 */
Alan Cox4edf1822008-02-08 04:18:44 -0800296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static inline int is_utf8_continuation(unsigned char c)
298{
299 return (c & 0xc0) == 0x80;
300}
301
302/**
303 * is_continuation - multibyte check
304 * @c: byte to check
305 *
306 * Returns true if the utf8 character 'c' is a multibyte continuation
307 * character and the terminal is in unicode mode.
308 */
Alan Cox4edf1822008-02-08 04:18:44 -0800309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static inline int is_continuation(unsigned char c, struct tty_struct *tty)
311{
312 return I_IUTF8(tty) && is_utf8_continuation(c);
313}
314
315/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000316 * do_output_char - output one character
317 * @c: character (or partial unicode symbol)
318 * @tty: terminal device
319 * @space: space available in tty driver write buffer
320 *
321 * This is a helper function that handles one output character
322 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600323 * doing OPOST processing and putting the results in the
324 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000325 *
326 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
327 * and NLDLY. They simply aren't relevant in the world today.
328 * If you ever need them, add them here.
329 *
330 * Returns the number of bytes of buffer space used or -1 if
331 * no space left.
332 *
333 * Locking: should be called under the output_lock to protect
334 * the column state and space left in the buffer
335 */
336
337static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
338{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200339 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000340 int spaces;
341
342 if (!space)
343 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000344
Joe Petersona88a69c2009-01-02 13:40:53 +0000345 switch (c) {
346 case '\n':
347 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200348 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000349 if (O_ONLCR(tty)) {
350 if (space < 2)
351 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200352 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700353 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000354 return 2;
355 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200356 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000357 break;
358 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200359 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000360 return 0;
361 if (O_OCRNL(tty)) {
362 c = '\n';
363 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200364 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000365 break;
366 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200367 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000368 break;
369 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200370 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000371 if (O_TABDLY(tty) == XTABS) {
372 if (space < spaces)
373 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200374 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000375 tty->ops->write(tty, " ", spaces);
376 return spaces;
377 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200378 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000379 break;
380 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200381 if (ldata->column > 0)
382 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000383 break;
384 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000385 if (!iscntrl(c)) {
386 if (O_OLCUC(tty))
387 c = toupper(c);
388 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200389 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000390 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000391 break;
392 }
393
394 tty_put_char(tty, c);
395 return 1;
396}
397
398/**
399 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * @c: character (or partial unicode symbol)
401 * @tty: terminal device
402 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600403 * Output one character with OPOST processing.
404 * Returns -1 when the output device is full and the character
405 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000407 * Locking: output_lock to protect column state and space left
408 * (also, this is called from n_tty_write under the
409 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Alan Cox4edf1822008-02-08 04:18:44 -0800411
Joe Petersona88a69c2009-01-02 13:40:53 +0000412static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Jiri Slabybddc7152012-10-18 22:26:42 +0200414 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000415 int space, retval;
416
Jiri Slabybddc7152012-10-18 22:26:42 +0200417 mutex_lock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alan Coxf34d7a52008-04-30 00:54:13 -0700419 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000420 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jiri Slabybddc7152012-10-18 22:26:42 +0200422 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000423 if (retval < 0)
424 return -1;
425 else
426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000430 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600432 * @buf: character buffer
433 * @nr: number of bytes to output
434 *
435 * Output a block of characters with OPOST processing.
436 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * This path is used to speed up block console writes, among other
439 * things when processing blocks of output data. It handles only
440 * the simple cases normally found and helps to generate blocks of
441 * symbols for the console driver and thus improve performance.
442 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000443 * Locking: output_lock to protect column state and space left
444 * (also, this is called from n_tty_write under the
445 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Alan Cox4edf1822008-02-08 04:18:44 -0800447
Joe Petersona88a69c2009-01-02 13:40:53 +0000448static ssize_t process_output_block(struct tty_struct *tty,
449 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200451 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100453 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 const unsigned char *cp;
455
Jiri Slabybddc7152012-10-18 22:26:42 +0200456 mutex_lock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000457
Alan Coxf34d7a52008-04-30 00:54:13 -0700458 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000459 if (!space) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200460 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (nr > space)
464 nr = space;
465
466 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000467 unsigned char c = *cp;
468
469 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 case '\n':
471 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200472 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (O_ONLCR(tty))
474 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200475 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200478 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto break_out;
480 if (O_OCRNL(tty))
481 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200482 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case '\t':
485 goto break_out;
486 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200487 if (ldata->column > 0)
488 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000491 if (!iscntrl(c)) {
492 if (O_OLCUC(tty))
493 goto break_out;
494 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200495 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 }
499 }
500break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700501 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000502
Jiri Slabybddc7152012-10-18 22:26:42 +0200503 mutex_unlock(&ldata->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return i;
505}
506
Joe Petersona88a69c2009-01-02 13:40:53 +0000507/**
508 * process_echoes - write pending echo characters
509 * @tty: terminal device
510 *
511 * Write previously buffered echo (and other ldisc-generated)
512 * characters to the tty.
513 *
514 * Characters generated by the ldisc (including echoes) need to
515 * be buffered because the driver's write buffer can fill during
516 * heavy program output. Echoing straight to the driver will
517 * often fail under these conditions, causing lost characters and
518 * resulting mismatches of ldisc state information.
519 *
520 * Since the ldisc state must represent the characters actually sent
521 * to the driver at the time of the write, operations like certain
522 * changes in column state are also saved in the buffer and executed
523 * here.
524 *
525 * A circular fifo buffer is used so that the most recent characters
526 * are prioritized. Also, when control characters are echoed with a
527 * prefixed "^", the pair is treated atomically and thus not separated.
528 *
529 * Locking: output_lock to protect column state and space left,
530 * echo_lock to protect the echo buffer
531 */
532
533static void process_echoes(struct tty_struct *tty)
534{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200535 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000536 int space, nr;
537 unsigned char c;
538 unsigned char *cp, *buf_end;
539
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200540 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000541 return;
542
Jiri Slabybddc7152012-10-18 22:26:42 +0200543 mutex_lock(&ldata->output_lock);
544 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000545
546 space = tty_write_room(tty);
547
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200548 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
549 cp = ldata->echo_buf + ldata->echo_pos;
550 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000551 while (nr > 0) {
552 c = *cp;
553 if (c == ECHO_OP_START) {
554 unsigned char op;
555 unsigned char *opp;
556 int no_space_left = 0;
557
558 /*
559 * If the buffer byte is the start of a multi-byte
560 * operation, get the next byte, which is either the
561 * op code or a control character value.
562 */
563 opp = cp + 1;
564 if (opp == buf_end)
565 opp -= N_TTY_BUF_SIZE;
566 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000567
Joe Petersona88a69c2009-01-02 13:40:53 +0000568 switch (op) {
569 unsigned int num_chars, num_bs;
570
571 case ECHO_OP_ERASE_TAB:
572 if (++opp == buf_end)
573 opp -= N_TTY_BUF_SIZE;
574 num_chars = *opp;
575
576 /*
577 * Determine how many columns to go back
578 * in order to erase the tab.
579 * This depends on the number of columns
580 * used by other characters within the tab
581 * area. If this (modulo 8) count is from
582 * the start of input rather than from a
583 * previous tab, we offset by canon column.
584 * Otherwise, tab spacing is normal.
585 */
586 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200587 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000588 num_bs = 8 - (num_chars & 7);
589
590 if (num_bs > space) {
591 no_space_left = 1;
592 break;
593 }
594 space -= num_bs;
595 while (num_bs--) {
596 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200597 if (ldata->column > 0)
598 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000599 }
600 cp += 3;
601 nr -= 3;
602 break;
603
604 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200605 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000606 cp += 2;
607 nr -= 2;
608 break;
609
610 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200611 if (ldata->column > 0)
612 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000613 cp += 2;
614 nr -= 2;
615 break;
616
617 case ECHO_OP_START:
618 /* This is an escaped echo op start code */
619 if (!space) {
620 no_space_left = 1;
621 break;
622 }
623 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200624 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000625 space--;
626 cp += 2;
627 nr -= 2;
628 break;
629
630 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000631 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600632 * If the op is not a special byte code,
633 * it is a ctrl char tagged to be echoed
634 * as "^X" (where X is the letter
635 * representing the control char).
636 * Note that we must ensure there is
637 * enough space for the whole ctrl pair.
638 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000639 */
Joe Peterson62b26352009-09-09 15:03:47 -0600640 if (space < 2) {
641 no_space_left = 1;
642 break;
643 }
644 tty_put_char(tty, '^');
645 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200646 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600647 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000648 cp += 2;
649 nr -= 2;
650 }
651
652 if (no_space_left)
653 break;
654 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600655 if (O_OPOST(tty) &&
656 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
657 int retval = do_output_char(c, tty, space);
658 if (retval < 0)
659 break;
660 space -= retval;
661 } else {
662 if (!space)
663 break;
664 tty_put_char(tty, c);
665 space -= 1;
666 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000667 cp += 1;
668 nr -= 1;
669 }
670
671 /* When end of circular buffer reached, wrap around */
672 if (cp >= buf_end)
673 cp -= N_TTY_BUF_SIZE;
674 }
675
676 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200677 ldata->echo_pos = 0;
678 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200679 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200681 int num_processed = ldata->echo_cnt - nr;
682 ldata->echo_pos += num_processed;
683 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
684 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000685 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200686 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000687 }
688
Jiri Slabybddc7152012-10-18 22:26:42 +0200689 mutex_unlock(&ldata->echo_lock);
690 mutex_unlock(&ldata->output_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000691
692 if (tty->ops->flush_chars)
693 tty->ops->flush_chars(tty);
694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000697 * add_echo_byte - add a byte to the echo buffer
698 * @c: unicode byte to echo
Jiri Slaby57c94122012-10-18 22:26:43 +0200699 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000700 *
701 * Add a character or operation byte to the echo buffer.
702 *
703 * Should be called under the echo lock to protect the echo buffer.
704 */
705
Jiri Slaby57c94122012-10-18 22:26:43 +0200706static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000707{
708 int new_byte_pos;
709
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200710 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000711 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200712 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000713
714 /*
715 * Since the buffer start position needs to be advanced,
716 * be sure to step by a whole operation byte group.
717 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200718 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
719 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000720 (N_TTY_BUF_SIZE - 1)] ==
721 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200722 ldata->echo_pos += 3;
723 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200725 ldata->echo_pos += 2;
726 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 }
728 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200729 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000730 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200731 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000732
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200733 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000734 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200735 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000736 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200737 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738 }
739
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200740 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000741}
742
743/**
744 * echo_move_back_col - add operation to move back a column
Jiri Slaby57c94122012-10-18 22:26:43 +0200745 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000746 *
747 * Add an operation to the echo buffer to move back one column.
748 *
749 * Locking: echo_lock to protect the echo buffer
750 */
751
Jiri Slaby57c94122012-10-18 22:26:43 +0200752static void echo_move_back_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000753{
Jiri Slabybddc7152012-10-18 22:26:42 +0200754 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200755 add_echo_byte(ECHO_OP_START, ldata);
756 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200757 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000758}
759
760/**
761 * echo_set_canon_col - add operation to set the canon column
Jiri Slaby57c94122012-10-18 22:26:43 +0200762 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000763 *
764 * Add an operation to the echo buffer to set the canon column
765 * to the current column.
766 *
767 * Locking: echo_lock to protect the echo buffer
768 */
769
Jiri Slaby57c94122012-10-18 22:26:43 +0200770static void echo_set_canon_col(struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000771{
Jiri Slabybddc7152012-10-18 22:26:42 +0200772 mutex_lock(&ldata->echo_lock);
Jiri Slaby57c94122012-10-18 22:26:43 +0200773 add_echo_byte(ECHO_OP_START, ldata);
774 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
Jiri Slabybddc7152012-10-18 22:26:42 +0200775 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000776}
777
778/**
779 * echo_erase_tab - add operation to erase a tab
780 * @num_chars: number of character columns already used
781 * @after_tab: true if num_chars starts after a previous tab
Jiri Slaby57c94122012-10-18 22:26:43 +0200782 * @ldata: n_tty data
Joe Petersona88a69c2009-01-02 13:40:53 +0000783 *
784 * Add an operation to the echo buffer to erase a tab.
785 *
786 * Called by the eraser function, which knows how many character
787 * columns have been used since either a previous tab or the start
788 * of input. This information will be used later, along with
789 * canon column (if applicable), to go back the correct number
790 * of columns.
791 *
792 * Locking: echo_lock to protect the echo buffer
793 */
794
795static void echo_erase_tab(unsigned int num_chars, int after_tab,
Jiri Slaby57c94122012-10-18 22:26:43 +0200796 struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000797{
Jiri Slabybddc7152012-10-18 22:26:42 +0200798 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000799
Jiri Slaby57c94122012-10-18 22:26:43 +0200800 add_echo_byte(ECHO_OP_START, ldata);
801 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000802
803 /* We only need to know this modulo 8 (tab spacing) */
804 num_chars &= 7;
805
806 /* Set the high bit as a flag if num_chars is after a previous tab */
807 if (after_tab)
808 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000809
Jiri Slaby57c94122012-10-18 22:26:43 +0200810 add_echo_byte(num_chars, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000811
Jiri Slabybddc7152012-10-18 22:26:42 +0200812 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000813}
814
815/**
816 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * @c: unicode byte to echo
818 * @tty: terminal device
819 *
Alan Cox4edf1822008-02-08 04:18:44 -0800820 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100822 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000823 * This variant does not treat control characters specially.
824 *
825 * Locking: echo_lock to protect the echo buffer
826 */
827
Jiri Slaby57c94122012-10-18 22:26:43 +0200828static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
Joe Petersona88a69c2009-01-02 13:40:53 +0000829{
Jiri Slabybddc7152012-10-18 22:26:42 +0200830 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000831 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200832 add_echo_byte(ECHO_OP_START, ldata);
833 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000834 } else {
Jiri Slaby57c94122012-10-18 22:26:43 +0200835 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000836 }
Jiri Slabybddc7152012-10-18 22:26:42 +0200837 mutex_unlock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000838}
839
840/**
841 * echo_char - echo a character
842 * @c: unicode byte to echo
843 * @tty: terminal device
844 *
845 * Echo user input back onto the screen. This must be called only when
846 * L_ECHO(tty) is true. Called from the driver receive_buf path.
847 *
Joe Peterson62b26352009-09-09 15:03:47 -0600848 * This variant tags control characters to be echoed as "^X"
849 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000850 *
851 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
853
854static void echo_char(unsigned char c, struct tty_struct *tty)
855{
Jiri Slabybddc7152012-10-18 22:26:42 +0200856 struct n_tty_data *ldata = tty->disc_data;
857
858 mutex_lock(&ldata->echo_lock);
Joe Petersona88a69c2009-01-02 13:40:53 +0000859
860 if (c == ECHO_OP_START) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(ECHO_OP_START, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000863 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600864 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Jiri Slaby57c94122012-10-18 22:26:43 +0200865 add_echo_byte(ECHO_OP_START, ldata);
866 add_echo_byte(c, ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +0000867 }
868
Jiri Slabybddc7152012-10-18 22:26:42 +0200869 mutex_unlock(&ldata->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Alan Cox17b82062008-10-13 10:45:06 +0100872/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000873 * finish_erasing - complete erase
Jiri Slaby57c94122012-10-18 22:26:43 +0200874 * @ldata: n_tty data
Alan Cox17b82062008-10-13 10:45:06 +0100875 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000876
Jiri Slaby57c94122012-10-18 22:26:43 +0200877static inline void finish_erasing(struct n_tty_data *ldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200879 if (ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200880 echo_char_raw('/', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200881 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883}
884
885/**
886 * eraser - handle erase function
887 * @c: character input
888 * @tty: terminal device
889 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200890 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * present in the stream from the driver layer. Handles the complexities
892 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100893 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000894 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Alan Cox4edf1822008-02-08 04:18:44 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static void eraser(unsigned char c, struct tty_struct *tty)
898{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200899 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 enum { ERASE, WERASE, KILL } kill_type;
901 int head, seen_alnums, cnt;
902 unsigned long flags;
903
Alan Cox17b82062008-10-13 10:45:06 +0100904 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200905 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000906 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return;
908 }
909 if (c == ERASE_CHAR(tty))
910 kill_type = ERASE;
911 else if (c == WERASE_CHAR(tty))
912 kill_type = WERASE;
913 else {
914 if (!L_ECHO(tty)) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200915 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200916 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200918 ldata->read_head = ldata->canon_head;
Jiri Slabybddc7152012-10-18 22:26:42 +0200919 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return;
921 }
922 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
Jiri Slabybddc7152012-10-18 22:26:42 +0200923 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200924 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200926 ldata->read_head = ldata->canon_head;
Jiri Slabybddc7152012-10-18 22:26:42 +0200927 spin_unlock_irqrestore(&ldata->read_lock, flags);
Jiri Slaby57c94122012-10-18 22:26:43 +0200928 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 echo_char(KILL_CHAR(tty), tty);
930 /* Add a newline if ECHOK is on and ECHOKE is off. */
931 if (L_ECHOK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +0200932 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return;
934 }
935 kill_type = KILL;
936 }
937
938 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100939 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200940 while (ldata->read_head != ldata->canon_head) {
941 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* erase a single possibly multibyte character */
944 do {
945 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200946 c = ldata->read_buf[head];
947 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* do not partially erase */
950 if (is_continuation(c, tty))
951 break;
952
953 if (kill_type == WERASE) {
954 /* Equivalent to BSD's ALTWERASE. */
955 if (isalnum(c) || c == '_')
956 seen_alnums++;
957 else if (seen_alnums)
958 break;
959 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200960 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Jiri Slabybddc7152012-10-18 22:26:42 +0200961 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200962 ldata->read_head = head;
963 ldata->read_cnt -= cnt;
Jiri Slabybddc7152012-10-18 22:26:42 +0200964 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (L_ECHO(tty)) {
966 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200967 if (!ldata->erasing) {
Jiri Slaby57c94122012-10-18 22:26:43 +0200968 echo_char_raw('\\', ldata);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200969 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 /* if cnt > 1, output a multi-byte character */
972 echo_char(c, tty);
973 while (--cnt > 0) {
974 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slaby57c94122012-10-18 22:26:43 +0200975 echo_char_raw(ldata->read_buf[head],
976 ldata);
977 echo_move_back_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
980 echo_char(ERASE_CHAR(tty), tty);
981 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000982 unsigned int num_chars = 0;
983 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200984 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Joe Petersona88a69c2009-01-02 13:40:53 +0000986 /*
987 * Count the columns used for characters
988 * since the start of input or after a
989 * previous tab.
990 * This info is used to go back the correct
991 * number of columns.
992 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200993 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000994 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200995 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000996 if (c == '\t') {
997 after_tab = 1;
998 break;
Alan Cox300a6202009-01-02 13:41:04 +0000999 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001001 num_chars += 2;
1002 } else if (!is_continuation(c, tty)) {
1003 num_chars++;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001006 echo_erase_tab(num_chars, after_tab, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
1008 if (iscntrl(c) && L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001009 echo_char_raw('\b', ldata);
1010 echo_char_raw(' ', ldata);
1011 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001014 echo_char_raw('\b', ldata);
1015 echo_char_raw(' ', ldata);
1016 echo_char_raw('\b', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 }
1019 }
1020 if (kill_type == ERASE)
1021 break;
1022 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001023 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001024 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/**
1028 * isig - handle the ISIG optio
1029 * @sig: signal
1030 * @tty: terminal
1031 * @flush: force flush
1032 *
1033 * Called when a signal is being sent due to terminal input. This
1034 * may caus terminal flushing to take place according to the termios
1035 * settings and character used. Called from the driver receive_buf
1036 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001037 *
1038 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Alan Cox4edf1822008-02-08 04:18:44 -08001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041static inline void isig(int sig, struct tty_struct *tty, int flush)
1042{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001043 if (tty->pgrp)
1044 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (flush || !L_NOFLSH(tty)) {
1046 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001047 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
1050
1051/**
1052 * n_tty_receive_break - handle break
1053 * @tty: terminal
1054 *
1055 * An RS232 break event has been hit in the incoming bitstream. This
1056 * can cause a variety of events depending upon the termios settings.
1057 *
1058 * Called from the receive_buf path so single threaded.
1059 */
Alan Cox4edf1822008-02-08 04:18:44 -08001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static inline void n_tty_receive_break(struct tty_struct *tty)
1062{
Jiri Slaby57c94122012-10-18 22:26:43 +02001063 struct n_tty_data *ldata = tty->disc_data;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (I_IGNBRK(tty))
1066 return;
1067 if (I_BRKINT(tty)) {
1068 isig(SIGINT, tty, 1);
1069 return;
1070 }
1071 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001072 put_tty_queue('\377', ldata);
1073 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Jiri Slaby57c94122012-10-18 22:26:43 +02001075 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 wake_up_interruptible(&tty->read_wait);
1077}
1078
1079/**
1080 * n_tty_receive_overrun - handle overrun reporting
1081 * @tty: terminal
1082 *
1083 * Data arrived faster than we could process it. While the tty
1084 * driver has flagged this the bits that were missed are gone
1085 * forever.
1086 *
1087 * Called from the receive_buf path so single threaded. Does not
1088 * need locking as num_overrun and overrun_time are function
1089 * private.
1090 */
Alan Cox4edf1822008-02-08 04:18:44 -08001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static inline void n_tty_receive_overrun(struct tty_struct *tty)
1093{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001094 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 char buf[64];
1096
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001097 ldata->num_overrun++;
1098 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1099 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1101 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001102 ldata->num_overrun);
1103 ldata->overrun_time = jiffies;
1104 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
1108/**
1109 * n_tty_receive_parity_error - error notifier
1110 * @tty: terminal device
1111 * @c: character
1112 *
1113 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001114 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1117 unsigned char c)
1118{
Jiri Slaby57c94122012-10-18 22:26:43 +02001119 struct n_tty_data *ldata = tty->disc_data;
1120
Alan Cox4edf1822008-02-08 04:18:44 -08001121 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (I_PARMRK(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001124 put_tty_queue('\377', ldata);
1125 put_tty_queue('\0', ldata);
1126 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 } else if (I_INPCK(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02001128 put_tty_queue('\0', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else
Jiri Slaby57c94122012-10-18 22:26:43 +02001130 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 wake_up_interruptible(&tty->read_wait);
1132}
1133
1134/**
1135 * n_tty_receive_char - perform processing
1136 * @tty: terminal device
1137 * @c: character
1138 *
1139 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001140 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * driver above.
1142 */
1143
1144static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1145{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001146 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001148 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001150 if (ldata->raw) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001151 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return;
1153 }
Alan Cox4edf1822008-02-08 04:18:44 -08001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (I_ISTRIP(tty))
1156 c &= 0x7f;
1157 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001158 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
hyc@symas.com26df6d12010-06-22 10:14:49 -07001160 if (L_EXTPROC(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001161 put_tty_queue(c, ldata);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001162 return;
1163 }
1164
Joe Peterson54d2a372008-02-06 01:37:59 -08001165 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001166 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1167 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001168 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001169 process_echoes(tty);
1170 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (tty->closing) {
1173 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001174 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001176 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001177 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 stop_tty(tty);
1179 }
1180 return;
1181 }
1182
1183 /*
1184 * If the previous character was LNEXT, or we know that this
1185 * character is not one of the characters that we'll have to
1186 * handle specially, do shortcut processing to speed things
1187 * up.
1188 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001189 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001190 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001191 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001192 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001193 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001194 if (L_ECHO(tty))
1195 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001196 return;
1197 }
1198 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001199 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001201 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001202 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001204 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001206 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001207 put_tty_queue(c, ldata);
1208 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return;
1210 }
Alan Cox4edf1822008-02-08 04:18:44 -08001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (I_IXON(tty)) {
1213 if (c == START_CHAR(tty)) {
1214 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001215 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return;
1217 }
1218 if (c == STOP_CHAR(tty)) {
1219 stop_tty(tty);
1220 return;
1221 }
1222 }
Joe Peterson575537b32008-04-30 00:53:30 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (L_ISIG(tty)) {
1225 int signal;
1226 signal = SIGINT;
1227 if (c == INTR_CHAR(tty))
1228 goto send_signal;
1229 signal = SIGQUIT;
1230 if (c == QUIT_CHAR(tty))
1231 goto send_signal;
1232 signal = SIGTSTP;
1233 if (c == SUSP_CHAR(tty)) {
1234send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001235 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001236 * Note that we do not use isig() here because we want
1237 * the order to be:
1238 * 1) flush, 2) echo, 3) signal
1239 */
1240 if (!L_NOFLSH(tty)) {
1241 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001242 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001243 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001244 if (I_IXON(tty))
1245 start_tty(tty);
1246 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001247 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001248 process_echoes(tty);
1249 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001250 if (tty->pgrp)
1251 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
1253 }
1254 }
Joe Peterson575537b32008-04-30 00:53:30 -07001255
1256 if (c == '\r') {
1257 if (I_IGNCR(tty))
1258 return;
1259 if (I_ICRNL(tty))
1260 c = '\n';
1261 } else if (c == '\n' && I_INLCR(tty))
1262 c = '\r';
1263
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001264 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1266 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1267 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001268 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return;
1270 }
1271 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001272 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001274 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (L_ECHOCTL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001276 echo_char_raw('^', ldata);
1277 echo_char_raw('\b', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001278 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 }
1281 return;
1282 }
1283 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1284 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001285 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Jiri Slaby57c94122012-10-18 22:26:43 +02001287 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 echo_char(c, tty);
Jiri Slaby57c94122012-10-18 22:26:43 +02001289 echo_char_raw('\n', ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001290 while (tail != ldata->read_head) {
1291 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1293 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001294 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return;
1296 }
1297 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001298 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001299 if (L_ECHO(tty))
1300 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001301 return;
1302 }
1303 if (L_ECHO(tty) || L_ECHONL(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001304 echo_char_raw('\n', ldata);
Joe Petersona88a69c2009-01-02 13:40:53 +00001305 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 goto handle_newline;
1308 }
1309 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001310 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001311 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001312 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001313 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 c = __DISABLED_CHAR;
1315 goto handle_newline;
1316 }
1317 if ((c == EOL_CHAR(tty)) ||
1318 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1320 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001321 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001322 if (L_ECHO(tty))
1323 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001324 return;
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /*
1327 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1328 */
1329 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001331 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001332 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001334 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 /*
1337 * XXX does PARMRK doubling happen for
1338 * EOL_CHAR and EOL2_CHAR?
1339 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001340 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001341 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Alan Cox4edf1822008-02-08 04:18:44 -08001343handle_newline:
Jiri Slabybddc7152012-10-18 22:26:42 +02001344 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001345 set_bit(ldata->read_head, ldata->read_flags);
Jiri Slaby57c94122012-10-18 22:26:43 +02001346 put_tty_queue_nolock(c, ldata);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001347 ldata->canon_head = ldata->read_head;
1348 ldata->canon_data++;
Jiri Slabybddc7152012-10-18 22:26:42 +02001349 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1351 if (waitqueue_active(&tty->read_wait))
1352 wake_up_interruptible(&tty->read_wait);
1353 return;
1354 }
1355 }
Alan Cox4edf1822008-02-08 04:18:44 -08001356
Joe Petersonacc71bb2009-01-02 13:43:32 +00001357 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001358 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001359 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001360 if (L_ECHO(tty))
1361 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001362 return;
1363 }
1364 if (L_ECHO(tty)) {
Jiri Slaby57c94122012-10-18 22:26:43 +02001365 finish_erasing(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (c == '\n')
Jiri Slaby57c94122012-10-18 22:26:43 +02001367 echo_char_raw('\n', ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 else {
1369 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001370 if (ldata->canon_head == ldata->read_head)
Jiri Slaby57c94122012-10-18 22:26:43 +02001371 echo_set_canon_col(ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 echo_char(c, tty);
1373 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001374 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
Joe Petersonacc71bb2009-01-02 13:43:32 +00001377 if (parmrk)
Jiri Slaby57c94122012-10-18 22:26:43 +02001378 put_tty_queue(c, ldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Jiri Slaby57c94122012-10-18 22:26:43 +02001380 put_tty_queue(c, ldata);
Alan Cox4edf1822008-02-08 04:18:44 -08001381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384/**
1385 * n_tty_write_wakeup - asynchronous I/O notifier
1386 * @tty: tty device
1387 *
1388 * Required for the ptys, serial driver etc. since processes
1389 * that attach themselves to the master and rely on ASYNC
1390 * IO must be woken up
1391 */
1392
1393static void n_tty_write_wakeup(struct tty_struct *tty)
1394{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001395 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399/**
1400 * n_tty_receive_buf - data receive
1401 * @tty: terminal device
1402 * @cp: buffer
1403 * @fp: flag buffer
1404 * @count: characters
1405 *
1406 * Called by the terminal driver when a block of characters has
1407 * been received. This function must be called from soft contexts
1408 * not from interrupt context. The driver is responsible for making
1409 * calls one at a time and in order (or using flush_to_ldisc)
1410 */
Alan Cox4edf1822008-02-08 04:18:44 -08001411
Linus Torvalds55db4c62011-06-04 06:33:24 +09001412static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1413 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001415 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 const unsigned char *p;
1417 char *f, flags = TTY_NORMAL;
1418 int i;
1419 char buf[64];
1420 unsigned long cpuflags;
1421
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001422 if (ldata->real_raw) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001423 spin_lock_irqsave(&ldata->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001424 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1425 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001427 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1428 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1429 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 cp += i;
1431 count -= i;
1432
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001433 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1434 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001436 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1437 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1438 ldata->read_cnt += i;
Jiri Slabybddc7152012-10-18 22:26:42 +02001439 spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001441 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (f)
1443 flags = *f++;
1444 switch (flags) {
1445 case TTY_NORMAL:
1446 n_tty_receive_char(tty, *p);
1447 break;
1448 case TTY_BREAK:
1449 n_tty_receive_break(tty);
1450 break;
1451 case TTY_PARITY:
1452 case TTY_FRAME:
1453 n_tty_receive_parity_error(tty, *p);
1454 break;
1455 case TTY_OVERRUN:
1456 n_tty_receive_overrun(tty);
1457 break;
1458 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001459 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 tty_name(tty, buf), flags);
1461 break;
1462 }
1463 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001464 if (tty->ops->flush_chars)
1465 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Linus Torvalds55db4c62011-06-04 06:33:24 +09001468 n_tty_set_room(tty);
1469
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001470 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001471 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1473 if (waitqueue_active(&tty->read_wait))
1474 wake_up_interruptible(&tty->read_wait);
1475 }
1476
1477 /*
1478 * Check the remaining room for the input canonicalization
1479 * mode. We don't want to throttle the driver if we're in
1480 * canonical mode and don't have a newline yet!
1481 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001482 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001483 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001484
1485 /* FIXME: there is a tiny race here if the receive room check runs
1486 before the other work executes and empties the buffer (upping
1487 the receiving room and unthrottling. We then throttle and get
1488 stuck. This has been observed and traced down by Vincent Pillet/
1489 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492int is_ignored(int sig)
1493{
1494 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001495 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/**
1499 * n_tty_set_termios - termios data changed
1500 * @tty: terminal
1501 * @old: previous data
1502 *
1503 * Called by the tty layer when the user changes termios flags so
1504 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001505 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 * guaranteed that this function will not be re-entered or in progress
1507 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001508 *
1509 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
Alan Cox4edf1822008-02-08 04:18:44 -08001511
1512static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001514 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001515 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001516
1517 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001518 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001519 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001520 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001521 ldata->canon_head = ldata->read_tail;
1522 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001523 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001524 }
1525
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001526 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001527 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001528
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001529 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001531 ldata->raw = 1;
1532 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001533 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535 }
1536 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1537 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1538 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1539 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001540 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001543 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001545 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001548 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1549 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1550 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1551 set_bit('\n', ldata->process_char_map);
1552 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (L_IEXTEN(tty)) {
1554 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001555 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001557 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (L_ECHO(tty))
1561 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001562 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 }
1565 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001566 set_bit(START_CHAR(tty), ldata->process_char_map);
1567 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001570 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1571 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1572 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001574 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001575 ldata->raw = 0;
1576 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001578 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1580 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1581 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001582 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001584 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001586 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001587 /* The termios change make the tty ready for I/O */
1588 wake_up_interruptible(&tty->write_wait);
1589 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/**
1593 * n_tty_close - close the ldisc for this tty
1594 * @tty: device
1595 *
Alan Cox4edf1822008-02-08 04:18:44 -08001596 * Called from the terminal layer when this line discipline is
1597 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * discipline change. The function will not be called while other
1599 * ldisc methods are in progress.
1600 */
Alan Cox4edf1822008-02-08 04:18:44 -08001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static void n_tty_close(struct tty_struct *tty)
1603{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001604 struct n_tty_data *ldata = tty->disc_data;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 n_tty_flush_buffer(tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001607 kfree(ldata->read_buf);
1608 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001609 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001610 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/**
1614 * n_tty_open - open an ldisc
1615 * @tty: terminal to open
1616 *
Alan Cox4edf1822008-02-08 04:18:44 -08001617 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * terminal device. Can sleep. Called serialized so that no
1619 * other events will occur in parallel. No further open will occur
1620 * until a close.
1621 */
1622
1623static int n_tty_open(struct tty_struct *tty)
1624{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001625 struct n_tty_data *ldata;
1626
1627 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1628 if (!ldata)
1629 goto err;
1630
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001631 ldata->overrun_time = jiffies;
Jiri Slabybddc7152012-10-18 22:26:42 +02001632 mutex_init(&ldata->atomic_read_lock);
1633 mutex_init(&ldata->output_lock);
1634 mutex_init(&ldata->echo_lock);
1635 spin_lock_init(&ldata->read_lock);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001636
Joe Petersona88a69c2009-01-02 13:40:53 +00001637 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001638 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1639 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1640 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001641 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001642
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001643 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001645 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001646 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 n_tty_set_termios(tty, NULL);
1648 tty->minimum_to_wake = 1;
1649 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001652err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001653 kfree(ldata->read_buf);
1654 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001655 kfree(ldata);
1656err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001657 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660static inline int input_available_p(struct tty_struct *tty, int amt)
1661{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001662 struct n_tty_data *ldata = tty->disc_data;
1663
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001664 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001665 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001666 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001668 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 1;
1670
1671 return 0;
1672}
1673
1674/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001675 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * @tty: terminal device
1677 * @b: user data
1678 * @nr: size of data
1679 *
Alan Cox11a96d12008-10-13 10:46:24 +01001680 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * ICANON is off; it copies characters straight from the tty queue to
1682 * user space directly. It can be profitably called twice; once to
1683 * drain the space from the tail pointer to the (physical) end of the
1684 * buffer, and once to drain the space from the (physical) beginning of
1685 * the buffer to head pointer.
1686 *
Jiri Slabybddc7152012-10-18 22:26:42 +02001687 * Called under the ldata->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *
1689 */
Alan Cox4edf1822008-02-08 04:18:44 -08001690
Alan Cox33f0f882006-01-09 20:54:13 -08001691static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 unsigned char __user **b,
1693 size_t *nr)
1694
1695{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001696 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int retval;
1698 size_t n;
1699 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001700 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 retval = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +02001703 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001704 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 n = min(*nr, n);
Jiri Slabybddc7152012-10-18 22:26:42 +02001706 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001708 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001710 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001711 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1712 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001713 ldata->icanon);
Jiri Slabybddc7152012-10-18 22:26:42 +02001714 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001715 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1716 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001717 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001718 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001719 n = 0;
Jiri Slabybddc7152012-10-18 22:26:42 +02001720 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *b += n;
1722 *nr -= n;
1723 }
1724 return retval;
1725}
1726
Al Virocc4191d2008-03-29 03:08:48 +00001727extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001728 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730/**
1731 * job_control - check job control
1732 * @tty: tty
1733 * @file: file handle
1734 *
1735 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001736 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001738 *
1739 * FIXME:
1740 * Locking: None - redirected write test is safe, testing
1741 * current->signal should possibly lock current->sighand
1742 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Alan Cox4edf1822008-02-08 04:18:44 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int job_control(struct tty_struct *tty, struct file *file)
1746{
1747 /* Job control check -- must be done at start and after
1748 every sleep (POSIX.1 7.1.1.4). */
1749 /* NOTE: not yet done after every sleep pending a thorough
1750 check of the logic of this change. -- jlc */
1751 /* don't stop on /dev/console */
1752 if (file->f_op->write != redirected_tty_write &&
1753 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001754 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001755 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001756 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001758 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001760 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001761 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -ERESTARTSYS;
1763 }
1764 }
1765 return 0;
1766}
Alan Cox4edf1822008-02-08 04:18:44 -08001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769/**
Alan Cox11a96d12008-10-13 10:46:24 +01001770 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * @tty: tty device
1772 * @file: file object
1773 * @buf: userspace buffer pointer
1774 * @nr: size of I/O
1775 *
1776 * Perform reads for the line discipline. We are guaranteed that the
1777 * line discipline will not be closed under us but we may get multiple
1778 * parallel readers and must handle this ourselves. We may also get
1779 * a hangup. Always called in user context, may sleep.
1780 *
1781 * This code must be sure never to sleep through a hangup.
1782 */
Alan Cox4edf1822008-02-08 04:18:44 -08001783
Alan Cox11a96d12008-10-13 10:46:24 +01001784static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 unsigned char __user *buf, size_t nr)
1786{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001787 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 unsigned char __user *b = buf;
1789 DECLARE_WAITQUEUE(wait, current);
1790 int c;
1791 int minimum, time;
1792 ssize_t retval = 0;
1793 ssize_t size;
1794 long timeout;
1795 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001796 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001800 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 minimum = time = 0;
1804 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001805 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 time = (HZ / 10) * TIME_CHAR(tty);
1807 minimum = MIN_CHAR(tty);
1808 if (minimum) {
1809 if (time)
1810 tty->minimum_to_wake = 1;
1811 else if (!waitqueue_active(&tty->read_wait) ||
1812 (tty->minimum_to_wake > minimum))
1813 tty->minimum_to_wake = minimum;
1814 } else {
1815 timeout = 0;
1816 if (time) {
1817 timeout = time;
1818 time = 0;
1819 }
1820 tty->minimum_to_wake = minimum = 1;
1821 }
1822 }
1823
1824 /*
1825 * Internal serialization of reads.
1826 */
1827 if (file->f_flags & O_NONBLOCK) {
Jiri Slabybddc7152012-10-18 22:26:42 +02001828 if (!mutex_trylock(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001830 } else {
Jiri Slabybddc7152012-10-18 22:26:42 +02001831 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -ERESTARTSYS;
1833 }
Alan Cox04f378b2008-04-30 00:53:29 -07001834 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 while (nr) {
1838 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001839 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 unsigned char cs;
1841 if (b != buf)
1842 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001843 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 cs = tty->link->ctrl_status;
1845 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001846 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001847 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 retval = -EFAULT;
1849 b--;
1850 break;
1851 }
1852 nr--;
1853 break;
1854 }
1855 /* This statement must be first before checking for input
1856 so that any interrupt will set the state back to
1857 TASK_RUNNING. */
1858 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1861 ((minimum - (b - buf)) >= 1))
1862 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (!input_available_p(tty, 0)) {
1865 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1866 retval = -EIO;
1867 break;
1868 }
1869 if (tty_hung_up_p(file))
1870 break;
1871 if (!timeout)
1872 break;
1873 if (file->f_flags & O_NONBLOCK) {
1874 retval = -EAGAIN;
1875 break;
1876 }
1877 if (signal_pending(current)) {
1878 retval = -ERESTARTSYS;
1879 break;
1880 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001881 /* FIXME: does n_tty_set_room need locking ? */
1882 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 continue;
1885 }
1886 __set_current_state(TASK_RUNNING);
1887
1888 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001889 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001890 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 retval = -EFAULT;
1892 b--;
1893 break;
1894 }
1895 nr--;
1896 }
1897
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001898 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* N.B. avoid overrun if nr == 0 */
Jiri Slabybddc7152012-10-18 22:26:42 +02001900 spin_lock_irqsave(&ldata->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001901 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001902 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001904 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001905 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001906 c = ldata->read_buf[ldata->read_tail];
1907 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001909 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (eol) {
1911 /* this test should be redundant:
1912 * we shouldn't be reading data if
1913 * canon_data is 0
1914 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001915 if (--ldata->canon_data < 0)
1916 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001918 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001921 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 retval = -EFAULT;
1923 b--;
Jiri Slabybddc7152012-10-18 22:26:42 +02001924 spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 break;
1926 }
1927 nr--;
1928 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001929 if (eol) {
1930 tty_audit_push(tty);
Jiri Slabybddc7152012-10-18 22:26:42 +02001931 spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001933 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001934 spin_lock_irqsave(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001936 spin_unlock_irqrestore(&ldata->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (retval)
1938 break;
1939 } else {
1940 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001941 /* The copy function takes the read lock and handles
1942 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 uncopied = copy_from_read_buf(tty, &b, &nr);
1944 uncopied += copy_from_read_buf(tty, &b, &nr);
1945 if (uncopied) {
1946 retval = -EFAULT;
1947 break;
1948 }
1949 }
1950
1951 /* If there is enough space in the read buffer now, let the
1952 * low-level driver know. We use n_tty_chars_in_buffer() to
1953 * check the buffer, as it now knows about canonical mode.
1954 * Otherwise, if the driver is throttled and the line is
1955 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1956 * we won't get any more characters.
1957 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001958 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1959 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (b - buf >= minimum)
1964 break;
1965 if (time)
1966 timeout = time;
1967 }
Jiri Slabybddc7152012-10-18 22:26:42 +02001968 mutex_unlock(&ldata->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 remove_wait_queue(&tty->read_wait, &wait);
1970
1971 if (!waitqueue_active(&tty->read_wait))
1972 tty->minimum_to_wake = minimum;
1973
1974 __set_current_state(TASK_RUNNING);
1975 size = b - buf;
1976 if (size) {
1977 retval = size;
1978 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001979 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001981 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Linus Torvalds55db4c62011-06-04 06:33:24 +09001983 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return retval;
1985}
1986
1987/**
Alan Cox11a96d12008-10-13 10:46:24 +01001988 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 * @tty: tty device
1990 * @file: file object
1991 * @buf: userspace buffer pointer
1992 * @nr: size of I/O
1993 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001994 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001996 * and other such events. Since the receive code will echo characters,
1997 * thus calling driver write methods, the output_lock is used in
1998 * the output processing functions called here as well as in the
1999 * echo processing function to protect the column state and space
2000 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 *
2002 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00002003 *
2004 * Locking: output_lock to protect column state and space left
2005 * (note that the process_output*() functions take this
2006 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Alan Cox4edf1822008-02-08 04:18:44 -08002008
Alan Cox11a96d12008-10-13 10:46:24 +01002009static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002010 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 const unsigned char *b = buf;
2013 DECLARE_WAITQUEUE(wait, current);
2014 int c;
2015 ssize_t retval = 0;
2016
2017 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2018 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2019 retval = tty_check_change(tty);
2020 if (retval)
2021 return retval;
2022 }
2023
Joe Petersona88a69c2009-01-02 13:40:53 +00002024 /* Write out any echoed characters that are still pending */
2025 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 add_wait_queue(&tty->write_wait, &wait);
2028 while (1) {
2029 set_current_state(TASK_INTERRUPTIBLE);
2030 if (signal_pending(current)) {
2031 retval = -ERESTARTSYS;
2032 break;
2033 }
2034 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2035 retval = -EIO;
2036 break;
2037 }
2038 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2039 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002040 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (num < 0) {
2042 if (num == -EAGAIN)
2043 break;
2044 retval = num;
2045 goto break_out;
2046 }
2047 b += num;
2048 nr -= num;
2049 if (nr == 0)
2050 break;
2051 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002052 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 break;
2054 b++; nr--;
2055 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002056 if (tty->ops->flush_chars)
2057 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002059 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002060 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002061 if (c < 0) {
2062 retval = c;
2063 goto break_out;
2064 }
2065 if (!c)
2066 break;
2067 b += c;
2068 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 if (!nr)
2072 break;
2073 if (file->f_flags & O_NONBLOCK) {
2074 retval = -EAGAIN;
2075 break;
2076 }
2077 schedule();
2078 }
2079break_out:
2080 __set_current_state(TASK_RUNNING);
2081 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002082 if (b - buf != nr && tty->fasync)
2083 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return (b - buf) ? b - buf : retval;
2085}
2086
2087/**
Alan Cox11a96d12008-10-13 10:46:24 +01002088 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * @tty: terminal device
2090 * @file: file accessing it
2091 * @wait: poll table
2092 *
2093 * Called when the line discipline is asked to poll() for data or
2094 * for special events. This code is not serialized with respect to
2095 * other events save open/close.
2096 *
2097 * This code must be sure never to sleep through a hangup.
2098 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Alan Cox4edf1822008-02-08 04:18:44 -08002100
Alan Cox11a96d12008-10-13 10:46:24 +01002101static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002102 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 unsigned int mask = 0;
2105
2106 poll_wait(file, &tty->read_wait, wait);
2107 poll_wait(file, &tty->write_wait, wait);
2108 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2109 mask |= POLLIN | POLLRDNORM;
2110 if (tty->packet && tty->link->ctrl_status)
2111 mask |= POLLPRI | POLLIN | POLLRDNORM;
2112 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2113 mask |= POLLHUP;
2114 if (tty_hung_up_p(file))
2115 mask |= POLLHUP;
2116 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2117 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2118 tty->minimum_to_wake = MIN_CHAR(tty);
2119 else
2120 tty->minimum_to_wake = 1;
2121 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002122 if (tty->ops->write && !tty_is_writelocked(tty) &&
2123 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2124 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 mask |= POLLOUT | POLLWRNORM;
2126 return mask;
2127}
2128
Jiri Slaby57c94122012-10-18 22:26:43 +02002129static unsigned long inq_canon(struct n_tty_data *ldata)
Alan Cox47afa7a2008-10-13 10:44:17 +01002130{
2131 int nr, head, tail;
2132
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002133 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002134 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002135 head = ldata->canon_head;
2136 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002137 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2138 /* Skip EOF-chars.. */
2139 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002140 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002141 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002142 nr--;
2143 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2144 }
2145 return nr;
2146}
2147
2148static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2149 unsigned int cmd, unsigned long arg)
2150{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002151 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002152 int retval;
2153
2154 switch (cmd) {
2155 case TIOCOUTQ:
2156 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2157 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002158 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002159 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002160 if (L_ICANON(tty))
Jiri Slaby57c94122012-10-18 22:26:43 +02002161 retval = inq_canon(ldata);
Alan Cox47afa7a2008-10-13 10:44:17 +01002162 return put_user(retval, (unsigned int __user *) arg);
2163 default:
2164 return n_tty_ioctl_helper(tty, file, cmd, arg);
2165 }
2166}
2167
Alan Coxa352def2008-07-16 21:53:12 +01002168struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002169 .magic = TTY_LDISC_MAGIC,
2170 .name = "n_tty",
2171 .open = n_tty_open,
2172 .close = n_tty_close,
2173 .flush_buffer = n_tty_flush_buffer,
2174 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002175 .read = n_tty_read,
2176 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002177 .ioctl = n_tty_ioctl,
2178 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002179 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002180 .receive_buf = n_tty_receive_buf,
2181 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002183
2184/**
2185 * n_tty_inherit_ops - inherit N_TTY methods
2186 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2187 *
2188 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2189 * methods.
2190 */
2191
2192void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2193{
2194 *ops = tty_ldisc_N_TTY;
2195 ops->owner = NULL;
2196 ops->refcount = ops->flags = 0;
2197}
2198EXPORT_SYMBOL_GPL(n_tty_inherit_ops);