blob: 4794537a50ff1c2f8789f69312a0be7a25a40aa4 [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 Slaby70ece7a2012-10-18 22:26:38 +020099};
100
Miloslav Trmac522ed772007-07-15 23:40:56 -0700101static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
102 unsigned char __user *ptr)
103{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200104 struct n_tty_data *ldata = tty->disc_data;
105
106 tty_audit_add_data(tty, &x, 1, ldata->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -0700107 return put_user(x, ptr);
108}
109
Linus Torvalds55db4c62011-06-04 06:33:24 +0900110/**
111 * n_tty_set__room - receive space
112 * @tty: terminal
113 *
114 * Called by the driver to find out how much data it is
115 * permitted to feed to the line discipline without any being lost
116 * and thus to manage flow control. Not serialized. Answers for the
117 * "instant".
118 */
119
120static void n_tty_set_room(struct tty_struct *tty)
121{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200122 struct n_tty_data *ldata = tty->disc_data;
Jaeden Amero090abf72012-07-27 08:43:11 -0500123 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900124 int old_left;
125
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200126 /* ldata->read_cnt is not read locked ? */
Jaeden Amero090abf72012-07-27 08:43:11 -0500127 if (I_PARMRK(tty)) {
128 /* Multiply read_cnt by 3, since each byte might take up to
129 * three times as many spaces when PARMRK is set (depending on
130 * its flags, e.g. parity error). */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200131 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500132 } else
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200133 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
Jaeden Amero090abf72012-07-27 08:43:11 -0500134
Linus Torvalds55db4c62011-06-04 06:33:24 +0900135 /*
136 * If we are doing input canonicalization, and there are no
137 * pending newlines, let characters through without limit, so
138 * that erase characters will be handled. Other excess
139 * characters will be beeped.
140 */
141 if (left <= 0)
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200142 left = ldata->icanon && !ldata->canon_data;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900143 old_left = tty->receive_room;
144 tty->receive_room = left;
145
146 /* Did this open up the receive buffer? We may need to flip */
147 if (left && !old_left)
148 schedule_work(&tty->buf.work);
149}
150
Alan Cox33f0f882006-01-09 20:54:13 -0800151static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200153 struct n_tty_data *ldata = tty->disc_data;
154
155 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
156 ldata->read_buf[ldata->read_head] = c;
157 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
158 ldata->read_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160}
161
Alan Cox17b82062008-10-13 10:45:06 +0100162/**
163 * put_tty_queue - add character to tty
164 * @c: character
165 * @tty: tty device
166 *
167 * Add a character to the tty read_buf queue. This is done under the
168 * read_lock to serialize character addition and also to protect us
169 * against parallel reads or flushes
170 */
171
Alan Cox33f0f882006-01-09 20:54:13 -0800172static void put_tty_queue(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
175 /*
176 * The problem of stomping on the buffers ends here.
177 * Why didn't anyone see this one coming? --AJK
178 */
179 spin_lock_irqsave(&tty->read_lock, flags);
180 put_tty_queue_nolock(c, tty);
181 spin_unlock_irqrestore(&tty->read_lock, flags);
182}
183
184/**
185 * check_unthrottle - allow new receive data
186 * @tty; tty device
187 *
Alan Cox17b82062008-10-13 10:45:06 +0100188 * Check whether to call the driver unthrottle functions
189 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800190 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * this is not guaranteed.
192 */
Alan Cox4edf1822008-02-08 04:18:44 -0800193static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Alan Cox39c2e602008-04-30 00:54:18 -0700195 if (tty->count)
196 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
200 * reset_buffer_flags - reset buffer state
201 * @tty: terminal to reset
202 *
Alan Cox4edf1822008-02-08 04:18:44 -0800203 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * and make sure the driver is unthrottled. Called
205 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100206 *
207 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static void reset_buffer_flags(struct tty_struct *tty)
211{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200212 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned long flags;
214
215 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200216 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 spin_unlock_irqrestore(&tty->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000218
219 mutex_lock(&tty->echo_lock);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200220 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000221 mutex_unlock(&tty->echo_lock);
222
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200223 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
Jiri Slaby3fe780b2012-10-18 22:26:40 +0200224 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900225 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/**
229 * n_tty_flush_buffer - clean input queue
230 * @tty: terminal device
231 *
232 * Flush the input buffer. Called when the line discipline is
233 * being closed, when the tty layer wants the buffer flushed (eg
234 * at hangup) or when the N_TTY line discipline internally has to
235 * clean the pending queue (for example some signals).
236 *
Alan Cox17b82062008-10-13 10:45:06 +0100237 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Alan Cox4edf1822008-02-08 04:18:44 -0800239
240static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Alan Cox04f378b2008-04-30 00:53:29 -0700242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* clear everything and unthrottle the driver */
244 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!tty->link)
247 return;
248
Alan Cox04f378b2008-04-30 00:53:29 -0700249 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (tty->link->packet) {
251 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
252 wake_up_interruptible(&tty->link->read_wait);
253 }
Alan Cox04f378b2008-04-30 00:53:29 -0700254 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/**
258 * n_tty_chars_in_buffer - report available bytes
259 * @tty: tty device
260 *
261 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800262 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100263 *
264 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Alan Cox4edf1822008-02-08 04:18:44 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
268{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200269 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned long flags;
271 ssize_t n = 0;
272
273 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200274 if (!ldata->icanon) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200275 n = ldata->read_cnt;
276 } else if (ldata->canon_data) {
277 n = (ldata->canon_head > ldata->read_tail) ?
278 ldata->canon_head - ldata->read_tail :
279 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 spin_unlock_irqrestore(&tty->read_lock, flags);
282 return n;
283}
284
285/**
286 * is_utf8_continuation - utf8 multibyte check
287 * @c: byte to check
288 *
289 * Returns true if the utf8 character 'c' is a multibyte continuation
290 * character. We use this to correctly compute the on screen size
291 * of the character when printing
292 */
Alan Cox4edf1822008-02-08 04:18:44 -0800293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static inline int is_utf8_continuation(unsigned char c)
295{
296 return (c & 0xc0) == 0x80;
297}
298
299/**
300 * is_continuation - multibyte check
301 * @c: byte to check
302 *
303 * Returns true if the utf8 character 'c' is a multibyte continuation
304 * character and the terminal is in unicode mode.
305 */
Alan Cox4edf1822008-02-08 04:18:44 -0800306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static inline int is_continuation(unsigned char c, struct tty_struct *tty)
308{
309 return I_IUTF8(tty) && is_utf8_continuation(c);
310}
311
312/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000313 * do_output_char - output one character
314 * @c: character (or partial unicode symbol)
315 * @tty: terminal device
316 * @space: space available in tty driver write buffer
317 *
318 * This is a helper function that handles one output character
319 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600320 * doing OPOST processing and putting the results in the
321 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000322 *
323 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
324 * and NLDLY. They simply aren't relevant in the world today.
325 * If you ever need them, add them here.
326 *
327 * Returns the number of bytes of buffer space used or -1 if
328 * no space left.
329 *
330 * Locking: should be called under the output_lock to protect
331 * the column state and space left in the buffer
332 */
333
334static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
335{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200336 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000337 int spaces;
338
339 if (!space)
340 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000341
Joe Petersona88a69c2009-01-02 13:40:53 +0000342 switch (c) {
343 case '\n':
344 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200345 ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000346 if (O_ONLCR(tty)) {
347 if (space < 2)
348 return -1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200349 ldata->canon_column = ldata->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700350 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000351 return 2;
352 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200353 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000354 break;
355 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200356 if (O_ONOCR(tty) && ldata->column == 0)
Joe Petersona88a69c2009-01-02 13:40:53 +0000357 return 0;
358 if (O_OCRNL(tty)) {
359 c = '\n';
360 if (O_ONLRET(tty))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200361 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000362 break;
363 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200364 ldata->canon_column = ldata->column = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000365 break;
366 case '\t':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200367 spaces = 8 - (ldata->column & 7);
Joe Petersona88a69c2009-01-02 13:40:53 +0000368 if (O_TABDLY(tty) == XTABS) {
369 if (space < spaces)
370 return -1;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200371 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000372 tty->ops->write(tty, " ", spaces);
373 return spaces;
374 }
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200375 ldata->column += spaces;
Joe Petersona88a69c2009-01-02 13:40:53 +0000376 break;
377 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200378 if (ldata->column > 0)
379 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000380 break;
381 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000382 if (!iscntrl(c)) {
383 if (O_OLCUC(tty))
384 c = toupper(c);
385 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200386 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000387 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000388 break;
389 }
390
391 tty_put_char(tty, c);
392 return 1;
393}
394
395/**
396 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * @c: character (or partial unicode symbol)
398 * @tty: terminal device
399 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600400 * Output one character with OPOST processing.
401 * Returns -1 when the output device is full and the character
402 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000404 * Locking: output_lock to protect column state and space left
405 * (also, this is called from n_tty_write under the
406 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Alan Cox4edf1822008-02-08 04:18:44 -0800408
Joe Petersona88a69c2009-01-02 13:40:53 +0000409static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Joe Petersona88a69c2009-01-02 13:40:53 +0000411 int space, retval;
412
413 mutex_lock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alan Coxf34d7a52008-04-30 00:54:13 -0700415 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000416 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Joe Petersona88a69c2009-01-02 13:40:53 +0000418 mutex_unlock(&tty->output_lock);
419 if (retval < 0)
420 return -1;
421 else
422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000426 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600428 * @buf: character buffer
429 * @nr: number of bytes to output
430 *
431 * Output a block of characters with OPOST processing.
432 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *
434 * This path is used to speed up block console writes, among other
435 * things when processing blocks of output data. It handles only
436 * the simple cases normally found and helps to generate blocks of
437 * symbols for the console driver and thus improve performance.
438 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000439 * Locking: output_lock to protect column state and space left
440 * (also, this is called from n_tty_write under the
441 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Alan Cox4edf1822008-02-08 04:18:44 -0800443
Joe Petersona88a69c2009-01-02 13:40:53 +0000444static ssize_t process_output_block(struct tty_struct *tty,
445 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200447 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100449 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 const unsigned char *cp;
451
Joe Petersona88a69c2009-01-02 13:40:53 +0000452 mutex_lock(&tty->output_lock);
453
Alan Coxf34d7a52008-04-30 00:54:13 -0700454 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000455 if (!space) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000456 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (nr > space)
460 nr = space;
461
462 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000463 unsigned char c = *cp;
464
465 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case '\n':
467 if (O_ONLRET(tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200468 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (O_ONLCR(tty))
470 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200471 ldata->canon_column = ldata->column;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case '\r':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200474 if (O_ONOCR(tty) && ldata->column == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto break_out;
476 if (O_OCRNL(tty))
477 goto break_out;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200478 ldata->canon_column = ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case '\t':
481 goto break_out;
482 case '\b':
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200483 if (ldata->column > 0)
484 ldata->column--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000487 if (!iscntrl(c)) {
488 if (O_OLCUC(tty))
489 goto break_out;
490 if (!is_continuation(c, tty))
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200491 ldata->column++;
Joe Petersona59c0d62009-01-02 13:43:25 +0000492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 }
495 }
496break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700497 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000498
499 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return i;
501}
502
Joe Petersona88a69c2009-01-02 13:40:53 +0000503/**
504 * process_echoes - write pending echo characters
505 * @tty: terminal device
506 *
507 * Write previously buffered echo (and other ldisc-generated)
508 * characters to the tty.
509 *
510 * Characters generated by the ldisc (including echoes) need to
511 * be buffered because the driver's write buffer can fill during
512 * heavy program output. Echoing straight to the driver will
513 * often fail under these conditions, causing lost characters and
514 * resulting mismatches of ldisc state information.
515 *
516 * Since the ldisc state must represent the characters actually sent
517 * to the driver at the time of the write, operations like certain
518 * changes in column state are also saved in the buffer and executed
519 * here.
520 *
521 * A circular fifo buffer is used so that the most recent characters
522 * are prioritized. Also, when control characters are echoed with a
523 * prefixed "^", the pair is treated atomically and thus not separated.
524 *
525 * Locking: output_lock to protect column state and space left,
526 * echo_lock to protect the echo buffer
527 */
528
529static void process_echoes(struct tty_struct *tty)
530{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200531 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000532 int space, nr;
533 unsigned char c;
534 unsigned char *cp, *buf_end;
535
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200536 if (!ldata->echo_cnt)
Joe Petersona88a69c2009-01-02 13:40:53 +0000537 return;
538
539 mutex_lock(&tty->output_lock);
540 mutex_lock(&tty->echo_lock);
541
542 space = tty_write_room(tty);
543
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200544 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
545 cp = ldata->echo_buf + ldata->echo_pos;
546 nr = ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000547 while (nr > 0) {
548 c = *cp;
549 if (c == ECHO_OP_START) {
550 unsigned char op;
551 unsigned char *opp;
552 int no_space_left = 0;
553
554 /*
555 * If the buffer byte is the start of a multi-byte
556 * operation, get the next byte, which is either the
557 * op code or a control character value.
558 */
559 opp = cp + 1;
560 if (opp == buf_end)
561 opp -= N_TTY_BUF_SIZE;
562 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000563
Joe Petersona88a69c2009-01-02 13:40:53 +0000564 switch (op) {
565 unsigned int num_chars, num_bs;
566
567 case ECHO_OP_ERASE_TAB:
568 if (++opp == buf_end)
569 opp -= N_TTY_BUF_SIZE;
570 num_chars = *opp;
571
572 /*
573 * Determine how many columns to go back
574 * in order to erase the tab.
575 * This depends on the number of columns
576 * used by other characters within the tab
577 * area. If this (modulo 8) count is from
578 * the start of input rather than from a
579 * previous tab, we offset by canon column.
580 * Otherwise, tab spacing is normal.
581 */
582 if (!(num_chars & 0x80))
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200583 num_chars += ldata->canon_column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000584 num_bs = 8 - (num_chars & 7);
585
586 if (num_bs > space) {
587 no_space_left = 1;
588 break;
589 }
590 space -= num_bs;
591 while (num_bs--) {
592 tty_put_char(tty, '\b');
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200593 if (ldata->column > 0)
594 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000595 }
596 cp += 3;
597 nr -= 3;
598 break;
599
600 case ECHO_OP_SET_CANON_COL:
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200601 ldata->canon_column = ldata->column;
Joe Petersona88a69c2009-01-02 13:40:53 +0000602 cp += 2;
603 nr -= 2;
604 break;
605
606 case ECHO_OP_MOVE_BACK_COL:
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200607 if (ldata->column > 0)
608 ldata->column--;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 cp += 2;
610 nr -= 2;
611 break;
612
613 case ECHO_OP_START:
614 /* This is an escaped echo op start code */
615 if (!space) {
616 no_space_left = 1;
617 break;
618 }
619 tty_put_char(tty, ECHO_OP_START);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200620 ldata->column++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000621 space--;
622 cp += 2;
623 nr -= 2;
624 break;
625
626 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000627 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600628 * If the op is not a special byte code,
629 * it is a ctrl char tagged to be echoed
630 * as "^X" (where X is the letter
631 * representing the control char).
632 * Note that we must ensure there is
633 * enough space for the whole ctrl pair.
634 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000635 */
Joe Peterson62b26352009-09-09 15:03:47 -0600636 if (space < 2) {
637 no_space_left = 1;
638 break;
639 }
640 tty_put_char(tty, '^');
641 tty_put_char(tty, op ^ 0100);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200642 ldata->column += 2;
Joe Peterson62b26352009-09-09 15:03:47 -0600643 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000644 cp += 2;
645 nr -= 2;
646 }
647
648 if (no_space_left)
649 break;
650 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600651 if (O_OPOST(tty) &&
652 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
653 int retval = do_output_char(c, tty, space);
654 if (retval < 0)
655 break;
656 space -= retval;
657 } else {
658 if (!space)
659 break;
660 tty_put_char(tty, c);
661 space -= 1;
662 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000663 cp += 1;
664 nr -= 1;
665 }
666
667 /* When end of circular buffer reached, wrap around */
668 if (cp >= buf_end)
669 cp -= N_TTY_BUF_SIZE;
670 }
671
672 if (nr == 0) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200673 ldata->echo_pos = 0;
674 ldata->echo_cnt = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200675 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000676 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200677 int num_processed = ldata->echo_cnt - nr;
678 ldata->echo_pos += num_processed;
679 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
680 ldata->echo_cnt = nr;
Joe Petersona88a69c2009-01-02 13:40:53 +0000681 if (num_processed > 0)
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200682 ldata->echo_overrun = 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000683 }
684
685 mutex_unlock(&tty->echo_lock);
686 mutex_unlock(&tty->output_lock);
687
688 if (tty->ops->flush_chars)
689 tty->ops->flush_chars(tty);
690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000693 * add_echo_byte - add a byte to the echo buffer
694 * @c: unicode byte to echo
695 * @tty: terminal device
696 *
697 * Add a character or operation byte to the echo buffer.
698 *
699 * Should be called under the echo lock to protect the echo buffer.
700 */
701
702static void add_echo_byte(unsigned char c, struct tty_struct *tty)
703{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200704 struct n_tty_data *ldata = tty->disc_data;
Joe Petersona88a69c2009-01-02 13:40:53 +0000705 int new_byte_pos;
706
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200707 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000708 /* Circular buffer is already at capacity */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200709 new_byte_pos = ldata->echo_pos;
Joe Petersona88a69c2009-01-02 13:40:53 +0000710
711 /*
712 * Since the buffer start position needs to be advanced,
713 * be sure to step by a whole operation byte group.
714 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200715 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
716 if (ldata->echo_buf[(ldata->echo_pos + 1) &
Joe Petersona88a69c2009-01-02 13:40:53 +0000717 (N_TTY_BUF_SIZE - 1)] ==
718 ECHO_OP_ERASE_TAB) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200719 ldata->echo_pos += 3;
720 ldata->echo_cnt -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000721 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200722 ldata->echo_pos += 2;
723 ldata->echo_cnt -= 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000724 }
725 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200726 ldata->echo_pos++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000727 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200728 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000729
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200730 ldata->echo_overrun = 1;
Joe Petersona88a69c2009-01-02 13:40:53 +0000731 } else {
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200732 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
Joe Petersona88a69c2009-01-02 13:40:53 +0000733 new_byte_pos &= N_TTY_BUF_SIZE - 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200734 ldata->echo_cnt++;
Joe Petersona88a69c2009-01-02 13:40:53 +0000735 }
736
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200737 ldata->echo_buf[new_byte_pos] = c;
Joe Petersona88a69c2009-01-02 13:40:53 +0000738}
739
740/**
741 * echo_move_back_col - add operation to move back a column
742 * @tty: terminal device
743 *
744 * Add an operation to the echo buffer to move back one column.
745 *
746 * Locking: echo_lock to protect the echo buffer
747 */
748
749static void echo_move_back_col(struct tty_struct *tty)
750{
751 mutex_lock(&tty->echo_lock);
752
753 add_echo_byte(ECHO_OP_START, tty);
754 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
755
756 mutex_unlock(&tty->echo_lock);
757}
758
759/**
760 * echo_set_canon_col - add operation to set the canon column
761 * @tty: terminal device
762 *
763 * Add an operation to the echo buffer to set the canon column
764 * to the current column.
765 *
766 * Locking: echo_lock to protect the echo buffer
767 */
768
769static void echo_set_canon_col(struct tty_struct *tty)
770{
771 mutex_lock(&tty->echo_lock);
772
773 add_echo_byte(ECHO_OP_START, tty);
774 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
775
776 mutex_unlock(&tty->echo_lock);
777}
778
779/**
780 * echo_erase_tab - add operation to erase a tab
781 * @num_chars: number of character columns already used
782 * @after_tab: true if num_chars starts after a previous tab
783 * @tty: terminal device
784 *
785 * Add an operation to the echo buffer to erase a tab.
786 *
787 * Called by the eraser function, which knows how many character
788 * columns have been used since either a previous tab or the start
789 * of input. This information will be used later, along with
790 * canon column (if applicable), to go back the correct number
791 * of columns.
792 *
793 * Locking: echo_lock to protect the echo buffer
794 */
795
796static void echo_erase_tab(unsigned int num_chars, int after_tab,
797 struct tty_struct *tty)
798{
799 mutex_lock(&tty->echo_lock);
800
801 add_echo_byte(ECHO_OP_START, tty);
802 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
803
804 /* We only need to know this modulo 8 (tab spacing) */
805 num_chars &= 7;
806
807 /* Set the high bit as a flag if num_chars is after a previous tab */
808 if (after_tab)
809 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000810
Joe Petersona88a69c2009-01-02 13:40:53 +0000811 add_echo_byte(num_chars, tty);
812
813 mutex_unlock(&tty->echo_lock);
814}
815
816/**
817 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * @c: unicode byte to echo
819 * @tty: terminal device
820 *
Alan Cox4edf1822008-02-08 04:18:44 -0800821 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100823 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000824 * This variant does not treat control characters specially.
825 *
826 * Locking: echo_lock to protect the echo buffer
827 */
828
829static void echo_char_raw(unsigned char c, struct tty_struct *tty)
830{
831 mutex_lock(&tty->echo_lock);
832
833 if (c == ECHO_OP_START) {
834 add_echo_byte(ECHO_OP_START, tty);
835 add_echo_byte(ECHO_OP_START, tty);
836 } else {
837 add_echo_byte(c, tty);
838 }
839
840 mutex_unlock(&tty->echo_lock);
841}
842
843/**
844 * echo_char - echo a character
845 * @c: unicode byte to echo
846 * @tty: terminal device
847 *
848 * Echo user input back onto the screen. This must be called only when
849 * L_ECHO(tty) is true. Called from the driver receive_buf path.
850 *
Joe Peterson62b26352009-09-09 15:03:47 -0600851 * This variant tags control characters to be echoed as "^X"
852 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000853 *
854 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856
857static void echo_char(unsigned char c, struct tty_struct *tty)
858{
Joe Petersona88a69c2009-01-02 13:40:53 +0000859 mutex_lock(&tty->echo_lock);
860
861 if (c == ECHO_OP_START) {
862 add_echo_byte(ECHO_OP_START, tty);
863 add_echo_byte(ECHO_OP_START, tty);
864 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600865 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Joe Petersona88a69c2009-01-02 13:40:53 +0000866 add_echo_byte(ECHO_OP_START, tty);
867 add_echo_byte(c, tty);
868 }
869
870 mutex_unlock(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Alan Cox17b82062008-10-13 10:45:06 +0100873/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000874 * finish_erasing - complete erase
Alan Cox17b82062008-10-13 10:45:06 +0100875 * @tty: tty doing the erase
Alan Cox17b82062008-10-13 10:45:06 +0100876 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static inline void finish_erasing(struct tty_struct *tty)
879{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200880 struct n_tty_data *ldata = tty->disc_data;
881 if (ldata->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000882 echo_char_raw('/', tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200883 ldata->erasing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885}
886
887/**
888 * eraser - handle erase function
889 * @c: character input
890 * @tty: terminal device
891 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200892 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * present in the stream from the driver layer. Handles the complexities
894 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100895 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000896 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Alan Cox4edf1822008-02-08 04:18:44 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static void eraser(unsigned char c, struct tty_struct *tty)
900{
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200901 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 enum { ERASE, WERASE, KILL } kill_type;
903 int head, seen_alnums, cnt;
904 unsigned long flags;
905
Alan Cox17b82062008-10-13 10:45:06 +0100906 /* FIXME: locking needed ? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200907 if (ldata->read_head == ldata->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000908 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
910 }
911 if (c == ERASE_CHAR(tty))
912 kill_type = ERASE;
913 else if (c == WERASE_CHAR(tty))
914 kill_type = WERASE;
915 else {
916 if (!L_ECHO(tty)) {
917 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200918 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200920 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 spin_unlock_irqrestore(&tty->read_lock, flags);
922 return;
923 }
924 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
925 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200926 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 (N_TTY_BUF_SIZE - 1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200928 ldata->read_head = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_unlock_irqrestore(&tty->read_lock, flags);
930 finish_erasing(tty);
931 echo_char(KILL_CHAR(tty), tty);
932 /* Add a newline if ECHOK is on and ECHOKE is off. */
933 if (L_ECHOK(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000934 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return;
936 }
937 kill_type = KILL;
938 }
939
940 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100941 /* FIXME: Locking ?? */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200942 while (ldata->read_head != ldata->canon_head) {
943 head = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* erase a single possibly multibyte character */
946 do {
947 head = (head - 1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200948 c = ldata->read_buf[head];
949 } while (is_continuation(c, tty) && head != ldata->canon_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* do not partially erase */
952 if (is_continuation(c, tty))
953 break;
954
955 if (kill_type == WERASE) {
956 /* Equivalent to BSD's ALTWERASE. */
957 if (isalnum(c) || c == '_')
958 seen_alnums++;
959 else if (seen_alnums)
960 break;
961 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200962 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200964 ldata->read_head = head;
965 ldata->read_cnt -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock_irqrestore(&tty->read_lock, flags);
967 if (L_ECHO(tty)) {
968 if (L_ECHOPRT(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200969 if (!ldata->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000970 echo_char_raw('\\', tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +0200971 ldata->erasing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 /* if cnt > 1, output a multi-byte character */
974 echo_char(c, tty);
975 while (--cnt > 0) {
976 head = (head+1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200977 echo_char_raw(ldata->read_buf[head], tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000978 echo_move_back_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
981 echo_char(ERASE_CHAR(tty), tty);
982 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000983 unsigned int num_chars = 0;
984 int after_tab = 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200985 unsigned long tail = ldata->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Joe Petersona88a69c2009-01-02 13:40:53 +0000987 /*
988 * Count the columns used for characters
989 * since the start of input or after a
990 * previous tab.
991 * This info is used to go back the correct
992 * number of columns.
993 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200994 while (tail != ldata->canon_head) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000995 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Jiri Slabyba2e68a2012-10-18 22:26:41 +0200996 c = ldata->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000997 if (c == '\t') {
998 after_tab = 1;
999 break;
Alan Cox300a6202009-01-02 13:41:04 +00001000 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +00001002 num_chars += 2;
1003 } else if (!is_continuation(c, tty)) {
1004 num_chars++;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001007 echo_erase_tab(num_chars, after_tab, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
1009 if (iscntrl(c) && L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001010 echo_char_raw('\b', tty);
1011 echo_char_raw(' ', tty);
1012 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001015 echo_char_raw('\b', tty);
1016 echo_char_raw(' ', tty);
1017 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 }
1020 }
1021 if (kill_type == ERASE)
1022 break;
1023 }
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001024 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 finish_erasing(tty);
1026}
1027
1028/**
1029 * isig - handle the ISIG optio
1030 * @sig: signal
1031 * @tty: terminal
1032 * @flush: force flush
1033 *
1034 * Called when a signal is being sent due to terminal input. This
1035 * may caus terminal flushing to take place according to the termios
1036 * settings and character used. Called from the driver receive_buf
1037 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001038 *
1039 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Alan Cox4edf1822008-02-08 04:18:44 -08001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static inline void isig(int sig, struct tty_struct *tty, int flush)
1043{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001044 if (tty->pgrp)
1045 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (flush || !L_NOFLSH(tty)) {
1047 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001048 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050}
1051
1052/**
1053 * n_tty_receive_break - handle break
1054 * @tty: terminal
1055 *
1056 * An RS232 break event has been hit in the incoming bitstream. This
1057 * can cause a variety of events depending upon the termios settings.
1058 *
1059 * Called from the receive_buf path so single threaded.
1060 */
Alan Cox4edf1822008-02-08 04:18:44 -08001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062static inline void n_tty_receive_break(struct tty_struct *tty)
1063{
1064 if (I_IGNBRK(tty))
1065 return;
1066 if (I_BRKINT(tty)) {
1067 isig(SIGINT, tty, 1);
1068 return;
1069 }
1070 if (I_PARMRK(tty)) {
1071 put_tty_queue('\377', tty);
1072 put_tty_queue('\0', tty);
1073 }
1074 put_tty_queue('\0', tty);
1075 wake_up_interruptible(&tty->read_wait);
1076}
1077
1078/**
1079 * n_tty_receive_overrun - handle overrun reporting
1080 * @tty: terminal
1081 *
1082 * Data arrived faster than we could process it. While the tty
1083 * driver has flagged this the bits that were missed are gone
1084 * forever.
1085 *
1086 * Called from the receive_buf path so single threaded. Does not
1087 * need locking as num_overrun and overrun_time are function
1088 * private.
1089 */
Alan Cox4edf1822008-02-08 04:18:44 -08001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static inline void n_tty_receive_overrun(struct tty_struct *tty)
1092{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001093 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 char buf[64];
1095
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001096 ldata->num_overrun++;
1097 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1098 time_after(ldata->overrun_time, jiffies)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1100 tty_name(tty, buf),
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001101 ldata->num_overrun);
1102 ldata->overrun_time = jiffies;
1103 ldata->num_overrun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105}
1106
1107/**
1108 * n_tty_receive_parity_error - error notifier
1109 * @tty: terminal device
1110 * @c: character
1111 *
1112 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001113 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1116 unsigned char c)
1117{
Alan Cox4edf1822008-02-08 04:18:44 -08001118 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (I_PARMRK(tty)) {
1121 put_tty_queue('\377', tty);
1122 put_tty_queue('\0', tty);
1123 put_tty_queue(c, tty);
1124 } else if (I_INPCK(tty))
1125 put_tty_queue('\0', tty);
1126 else
1127 put_tty_queue(c, tty);
1128 wake_up_interruptible(&tty->read_wait);
1129}
1130
1131/**
1132 * n_tty_receive_char - perform processing
1133 * @tty: terminal device
1134 * @c: character
1135 *
1136 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001137 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * driver above.
1139 */
1140
1141static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1142{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001143 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001145 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001147 if (ldata->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 put_tty_queue(c, tty);
1149 return;
1150 }
Alan Cox4edf1822008-02-08 04:18:44 -08001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (I_ISTRIP(tty))
1153 c &= 0x7f;
1154 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001155 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
hyc@symas.com26df6d12010-06-22 10:14:49 -07001157 if (L_EXTPROC(tty)) {
1158 put_tty_queue(c, tty);
1159 return;
1160 }
1161
Joe Peterson54d2a372008-02-06 01:37:59 -08001162 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001163 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1164 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001165 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001166 process_echoes(tty);
1167 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (tty->closing) {
1170 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001171 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001173 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001174 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 stop_tty(tty);
1176 }
1177 return;
1178 }
1179
1180 /*
1181 * If the previous character was LNEXT, or we know that this
1182 * character is not one of the characters that we'll have to
1183 * handle specially, do shortcut processing to speed things
1184 * up.
1185 */
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001186 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001187 ldata->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001188 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001189 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001190 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001191 if (L_ECHO(tty))
1192 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001193 return;
1194 }
1195 if (L_ECHO(tty)) {
1196 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001198 if (ldata->canon_head == ldata->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001199 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001201 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001203 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 put_tty_queue(c, tty);
1205 put_tty_queue(c, tty);
1206 return;
1207 }
Alan Cox4edf1822008-02-08 04:18:44 -08001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (I_IXON(tty)) {
1210 if (c == START_CHAR(tty)) {
1211 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001212 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return;
1214 }
1215 if (c == STOP_CHAR(tty)) {
1216 stop_tty(tty);
1217 return;
1218 }
1219 }
Joe Peterson575537b32008-04-30 00:53:30 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (L_ISIG(tty)) {
1222 int signal;
1223 signal = SIGINT;
1224 if (c == INTR_CHAR(tty))
1225 goto send_signal;
1226 signal = SIGQUIT;
1227 if (c == QUIT_CHAR(tty))
1228 goto send_signal;
1229 signal = SIGTSTP;
1230 if (c == SUSP_CHAR(tty)) {
1231send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001232 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001233 * Note that we do not use isig() here because we want
1234 * the order to be:
1235 * 1) flush, 2) echo, 3) signal
1236 */
1237 if (!L_NOFLSH(tty)) {
1238 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001239 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001240 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001241 if (I_IXON(tty))
1242 start_tty(tty);
1243 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001244 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001245 process_echoes(tty);
1246 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001247 if (tty->pgrp)
1248 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return;
1250 }
1251 }
Joe Peterson575537b32008-04-30 00:53:30 -07001252
1253 if (c == '\r') {
1254 if (I_IGNCR(tty))
1255 return;
1256 if (I_ICRNL(tty))
1257 c = '\n';
1258 } else if (c == '\n' && I_INLCR(tty))
1259 c = '\r';
1260
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001261 if (ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1263 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1264 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001265 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return;
1267 }
1268 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001269 ldata->lnext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (L_ECHO(tty)) {
1271 finish_erasing(tty);
1272 if (L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001273 echo_char_raw('^', tty);
1274 echo_char_raw('\b', tty);
1275 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 }
1278 return;
1279 }
1280 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1281 L_IEXTEN(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001282 unsigned long tail = ldata->canon_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 finish_erasing(tty);
1285 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001286 echo_char_raw('\n', tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001287 while (tail != ldata->read_head) {
1288 echo_char(ldata->read_buf[tail], tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1290 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001291 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return;
1293 }
1294 if (c == '\n') {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001295 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001296 if (L_ECHO(tty))
1297 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001298 return;
1299 }
1300 if (L_ECHO(tty) || L_ECHONL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001301 echo_char_raw('\n', tty);
1302 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 goto handle_newline;
1305 }
1306 if (c == EOF_CHAR(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001307 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
Joe Petersonacc71bb2009-01-02 13:43:32 +00001308 return;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001309 if (ldata->canon_head != ldata->read_head)
Alan Cox4edf1822008-02-08 04:18:44 -08001310 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 c = __DISABLED_CHAR;
1312 goto handle_newline;
1313 }
1314 if ((c == EOL_CHAR(tty)) ||
1315 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001316 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1317 ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001318 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001319 if (L_ECHO(tty))
1320 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001321 return;
1322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1325 */
1326 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001328 if (ldata->canon_head == ldata->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001329 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001331 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 /*
1334 * XXX does PARMRK doubling happen for
1335 * EOL_CHAR and EOL2_CHAR?
1336 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001337 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 put_tty_queue(c, tty);
1339
Alan Cox4edf1822008-02-08 04:18:44 -08001340handle_newline:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001342 set_bit(ldata->read_head, ldata->read_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 put_tty_queue_nolock(c, tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001344 ldata->canon_head = ldata->read_head;
1345 ldata->canon_data++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_unlock_irqrestore(&tty->read_lock, flags);
1347 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1348 if (waitqueue_active(&tty->read_wait))
1349 wake_up_interruptible(&tty->read_wait);
1350 return;
1351 }
1352 }
Alan Cox4edf1822008-02-08 04:18:44 -08001353
Joe Petersonacc71bb2009-01-02 13:43:32 +00001354 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001355 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001356 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001357 if (L_ECHO(tty))
1358 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001359 return;
1360 }
1361 if (L_ECHO(tty)) {
1362 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (c == '\n')
Joe Petersona88a69c2009-01-02 13:40:53 +00001364 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else {
1366 /* Record the column of first canon char. */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001367 if (ldata->canon_head == ldata->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001368 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 echo_char(c, tty);
1370 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001371 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Joe Petersonacc71bb2009-01-02 13:43:32 +00001374 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 put_tty_queue(c, tty);
1376
1377 put_tty_queue(c, tty);
Alan Cox4edf1822008-02-08 04:18:44 -08001378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381/**
1382 * n_tty_write_wakeup - asynchronous I/O notifier
1383 * @tty: tty device
1384 *
1385 * Required for the ptys, serial driver etc. since processes
1386 * that attach themselves to the master and rely on ASYNC
1387 * IO must be woken up
1388 */
1389
1390static void n_tty_write_wakeup(struct tty_struct *tty)
1391{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001392 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396/**
1397 * n_tty_receive_buf - data receive
1398 * @tty: terminal device
1399 * @cp: buffer
1400 * @fp: flag buffer
1401 * @count: characters
1402 *
1403 * Called by the terminal driver when a block of characters has
1404 * been received. This function must be called from soft contexts
1405 * not from interrupt context. The driver is responsible for making
1406 * calls one at a time and in order (or using flush_to_ldisc)
1407 */
Alan Cox4edf1822008-02-08 04:18:44 -08001408
Linus Torvalds55db4c62011-06-04 06:33:24 +09001409static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1410 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001412 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 const unsigned char *p;
1414 char *f, flags = TTY_NORMAL;
1415 int i;
1416 char buf[64];
1417 unsigned long cpuflags;
1418
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001419 if (ldata->real_raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 spin_lock_irqsave(&tty->read_lock, cpuflags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001421 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1422 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001424 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1425 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1426 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 cp += i;
1428 count -= i;
1429
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001430 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1431 N_TTY_BUF_SIZE - ldata->read_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 i = min(count, i);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001433 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1434 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1435 ldata->read_cnt += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1437 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001438 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (f)
1440 flags = *f++;
1441 switch (flags) {
1442 case TTY_NORMAL:
1443 n_tty_receive_char(tty, *p);
1444 break;
1445 case TTY_BREAK:
1446 n_tty_receive_break(tty);
1447 break;
1448 case TTY_PARITY:
1449 case TTY_FRAME:
1450 n_tty_receive_parity_error(tty, *p);
1451 break;
1452 case TTY_OVERRUN:
1453 n_tty_receive_overrun(tty);
1454 break;
1455 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001456 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 tty_name(tty, buf), flags);
1458 break;
1459 }
1460 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001461 if (tty->ops->flush_chars)
1462 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
Linus Torvalds55db4c62011-06-04 06:33:24 +09001465 n_tty_set_room(tty);
1466
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001467 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
hyc@symas.com26df6d12010-06-22 10:14:49 -07001468 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1470 if (waitqueue_active(&tty->read_wait))
1471 wake_up_interruptible(&tty->read_wait);
1472 }
1473
1474 /*
1475 * Check the remaining room for the input canonicalization
1476 * mode. We don't want to throttle the driver if we're in
1477 * canonical mode and don't have a newline yet!
1478 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001479 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001480 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001481
1482 /* FIXME: there is a tiny race here if the receive room check runs
1483 before the other work executes and empties the buffer (upping
1484 the receiving room and unthrottling. We then throttle and get
1485 stuck. This has been observed and traced down by Vincent Pillet/
1486 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489int is_ignored(int sig)
1490{
1491 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001492 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495/**
1496 * n_tty_set_termios - termios data changed
1497 * @tty: terminal
1498 * @old: previous data
1499 *
1500 * Called by the tty layer when the user changes termios flags so
1501 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001502 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 * guaranteed that this function will not be re-entered or in progress
1504 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001505 *
1506 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
Alan Cox4edf1822008-02-08 04:18:44 -08001508
1509static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001511 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01001512 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001513
1514 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001515 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001516 if (canon_change) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001517 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001518 ldata->canon_head = ldata->read_tail;
1519 ldata->canon_data = 0;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001520 ldata->erasing = 0;
Alan Cox47afa7a2008-10-13 10:44:17 +01001521 }
1522
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001523 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
Alan Cox47afa7a2008-10-13 10:44:17 +01001524 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001525
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001526 ldata->icanon = (L_ICANON(tty) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001528 ldata->raw = 1;
1529 ldata->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001530 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return;
1532 }
1533 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1534 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1535 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1536 I_PARMRK(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001537 bitmap_zero(ldata->process_char_map, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 if (I_IGNCR(tty) || I_ICRNL(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001540 set_bit('\r', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (I_INLCR(tty))
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001542 set_bit('\n', ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (L_ICANON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001545 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1546 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1547 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1548 set_bit('\n', ldata->process_char_map);
1549 set_bit(EOL_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (L_IEXTEN(tty)) {
1551 set_bit(WERASE_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001552 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 set_bit(LNEXT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001554 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 set_bit(EOL2_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001556 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (L_ECHO(tty))
1558 set_bit(REPRINT_CHAR(tty),
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001559 ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 }
1562 if (I_IXON(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001563 set_bit(START_CHAR(tty), ldata->process_char_map);
1564 set_bit(STOP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566 if (L_ISIG(tty)) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001567 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1568 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1569 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001571 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001572 ldata->raw = 0;
1573 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001575 ldata->raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1577 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1578 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001579 ldata->real_raw = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001581 ldata->real_raw = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001583 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001584 /* The termios change make the tty ready for I/O */
1585 wake_up_interruptible(&tty->write_wait);
1586 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/**
1590 * n_tty_close - close the ldisc for this tty
1591 * @tty: device
1592 *
Alan Cox4edf1822008-02-08 04:18:44 -08001593 * Called from the terminal layer when this line discipline is
1594 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * discipline change. The function will not be called while other
1596 * ldisc methods are in progress.
1597 */
Alan Cox4edf1822008-02-08 04:18:44 -08001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static void n_tty_close(struct tty_struct *tty)
1600{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001601 struct n_tty_data *ldata = tty->disc_data;
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 n_tty_flush_buffer(tty);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001604 kfree(ldata->read_buf);
1605 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001606 kfree(ldata);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001607 tty->disc_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/**
1611 * n_tty_open - open an ldisc
1612 * @tty: terminal to open
1613 *
Alan Cox4edf1822008-02-08 04:18:44 -08001614 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * terminal device. Can sleep. Called serialized so that no
1616 * other events will occur in parallel. No further open will occur
1617 * until a close.
1618 */
1619
1620static int n_tty_open(struct tty_struct *tty)
1621{
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001622 struct n_tty_data *ldata;
1623
1624 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1625 if (!ldata)
1626 goto err;
1627
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001628 ldata->overrun_time = jiffies;
1629
Joe Petersona88a69c2009-01-02 13:40:53 +00001630 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001631 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1632 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1633 if (!ldata->read_buf || !ldata->echo_buf)
Jiri Slabyb91939f2012-10-18 22:26:35 +02001634 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001635
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001636 tty->disc_data = ldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001638 tty_unthrottle(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001639 ldata->column = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 n_tty_set_termios(tty, NULL);
1641 tty->minimum_to_wake = 1;
1642 tty->closing = 0;
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001645err_free_bufs:
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001646 kfree(ldata->read_buf);
1647 kfree(ldata->echo_buf);
Jiri Slaby70ece7a2012-10-18 22:26:38 +02001648 kfree(ldata);
1649err:
Jiri Slabyb91939f2012-10-18 22:26:35 +02001650 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653static inline int input_available_p(struct tty_struct *tty, int amt)
1654{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001655 struct n_tty_data *ldata = tty->disc_data;
1656
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001657 tty_flush_to_ldisc(tty);
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001658 if (ldata->icanon && !L_EXTPROC(tty)) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001659 if (ldata->canon_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 1;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001661 } else if (ldata->read_cnt >= (amt ? amt : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 1;
1663
1664 return 0;
1665}
1666
1667/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001668 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * @tty: terminal device
1670 * @b: user data
1671 * @nr: size of data
1672 *
Alan Cox11a96d12008-10-13 10:46:24 +01001673 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 * ICANON is off; it copies characters straight from the tty queue to
1675 * user space directly. It can be profitably called twice; once to
1676 * drain the space from the tail pointer to the (physical) end of the
1677 * buffer, and once to drain the space from the (physical) beginning of
1678 * the buffer to head pointer.
1679 *
Paul Fulghum817d6d32006-06-28 04:26:47 -07001680 * Called under the tty->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
1682 */
Alan Cox4edf1822008-02-08 04:18:44 -08001683
Alan Cox33f0f882006-01-09 20:54:13 -08001684static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 unsigned char __user **b,
1686 size_t *nr)
1687
1688{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001689 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int retval;
1691 size_t n;
1692 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001693 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 retval = 0;
1696 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001697 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 n = min(*nr, n);
1699 spin_unlock_irqrestore(&tty->read_lock, flags);
1700 if (n) {
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001701 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001703 is_eof = n == 1 &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001704 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1705 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001706 ldata->icanon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001708 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1709 ldata->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001710 /* Turn single EOF into zero-length read */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001711 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001712 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 spin_unlock_irqrestore(&tty->read_lock, flags);
1714 *b += n;
1715 *nr -= n;
1716 }
1717 return retval;
1718}
1719
Al Virocc4191d2008-03-29 03:08:48 +00001720extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001721 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/**
1724 * job_control - check job control
1725 * @tty: tty
1726 * @file: file handle
1727 *
1728 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001729 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001731 *
1732 * FIXME:
1733 * Locking: None - redirected write test is safe, testing
1734 * current->signal should possibly lock current->sighand
1735 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Alan Cox4edf1822008-02-08 04:18:44 -08001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static int job_control(struct tty_struct *tty, struct file *file)
1739{
1740 /* Job control check -- must be done at start and after
1741 every sleep (POSIX.1 7.1.1.4). */
1742 /* NOTE: not yet done after every sleep pending a thorough
1743 check of the logic of this change. -- jlc */
1744 /* don't stop on /dev/console */
1745 if (file->f_op->write != redirected_tty_write &&
1746 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001747 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001748 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001749 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001751 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001753 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001754 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return -ERESTARTSYS;
1756 }
1757 }
1758 return 0;
1759}
Alan Cox4edf1822008-02-08 04:18:44 -08001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762/**
Alan Cox11a96d12008-10-13 10:46:24 +01001763 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * @tty: tty device
1765 * @file: file object
1766 * @buf: userspace buffer pointer
1767 * @nr: size of I/O
1768 *
1769 * Perform reads for the line discipline. We are guaranteed that the
1770 * line discipline will not be closed under us but we may get multiple
1771 * parallel readers and must handle this ourselves. We may also get
1772 * a hangup. Always called in user context, may sleep.
1773 *
1774 * This code must be sure never to sleep through a hangup.
1775 */
Alan Cox4edf1822008-02-08 04:18:44 -08001776
Alan Cox11a96d12008-10-13 10:46:24 +01001777static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 unsigned char __user *buf, size_t nr)
1779{
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001780 struct n_tty_data *ldata = tty->disc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned char __user *b = buf;
1782 DECLARE_WAITQUEUE(wait, current);
1783 int c;
1784 int minimum, time;
1785 ssize_t retval = 0;
1786 ssize_t size;
1787 long timeout;
1788 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001789 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001793 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 minimum = time = 0;
1797 timeout = MAX_SCHEDULE_TIMEOUT;
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001798 if (!ldata->icanon) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 time = (HZ / 10) * TIME_CHAR(tty);
1800 minimum = MIN_CHAR(tty);
1801 if (minimum) {
1802 if (time)
1803 tty->minimum_to_wake = 1;
1804 else if (!waitqueue_active(&tty->read_wait) ||
1805 (tty->minimum_to_wake > minimum))
1806 tty->minimum_to_wake = minimum;
1807 } else {
1808 timeout = 0;
1809 if (time) {
1810 timeout = time;
1811 time = 0;
1812 }
1813 tty->minimum_to_wake = minimum = 1;
1814 }
1815 }
1816
1817 /*
1818 * Internal serialization of reads.
1819 */
1820 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001821 if (!mutex_trylock(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001823 } else {
Ingo Molnar70522e12006-03-23 03:00:31 -08001824 if (mutex_lock_interruptible(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -ERESTARTSYS;
1826 }
Alan Cox04f378b2008-04-30 00:53:29 -07001827 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 while (nr) {
1831 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001832 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 unsigned char cs;
1834 if (b != buf)
1835 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001836 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 cs = tty->link->ctrl_status;
1838 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001839 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001840 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 retval = -EFAULT;
1842 b--;
1843 break;
1844 }
1845 nr--;
1846 break;
1847 }
1848 /* This statement must be first before checking for input
1849 so that any interrupt will set the state back to
1850 TASK_RUNNING. */
1851 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1854 ((minimum - (b - buf)) >= 1))
1855 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (!input_available_p(tty, 0)) {
1858 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1859 retval = -EIO;
1860 break;
1861 }
1862 if (tty_hung_up_p(file))
1863 break;
1864 if (!timeout)
1865 break;
1866 if (file->f_flags & O_NONBLOCK) {
1867 retval = -EAGAIN;
1868 break;
1869 }
1870 if (signal_pending(current)) {
1871 retval = -ERESTARTSYS;
1872 break;
1873 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001874 /* FIXME: does n_tty_set_room need locking ? */
1875 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 continue;
1878 }
1879 __set_current_state(TASK_RUNNING);
1880
1881 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001882 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001883 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 retval = -EFAULT;
1885 b--;
1886 break;
1887 }
1888 nr--;
1889 }
1890
Jiri Slaby53c5ee22012-10-18 22:26:39 +02001891 if (ldata->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* N.B. avoid overrun if nr == 0 */
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001893 spin_lock_irqsave(&tty->read_lock, flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001894 while (nr && ldata->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001895 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001897 eol = test_and_clear_bit(ldata->read_tail,
Jiri Slaby3fe780b2012-10-18 22:26:40 +02001898 ldata->read_flags);
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001899 c = ldata->read_buf[ldata->read_tail];
1900 ldata->read_tail = ((ldata->read_tail+1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 (N_TTY_BUF_SIZE-1));
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001902 ldata->read_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (eol) {
1904 /* this test should be redundant:
1905 * we shouldn't be reading data if
1906 * canon_data is 0
1907 */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02001908 if (--ldata->canon_data < 0)
1909 ldata->canon_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 spin_unlock_irqrestore(&tty->read_lock, flags);
1912
1913 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001914 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 retval = -EFAULT;
1916 b--;
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001917 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 break;
1919 }
1920 nr--;
1921 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001922 if (eol) {
1923 tty_audit_push(tty);
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001924 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001926 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001927 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001929 spin_unlock_irqrestore(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (retval)
1931 break;
1932 } else {
1933 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001934 /* The copy function takes the read lock and handles
1935 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 uncopied = copy_from_read_buf(tty, &b, &nr);
1937 uncopied += copy_from_read_buf(tty, &b, &nr);
1938 if (uncopied) {
1939 retval = -EFAULT;
1940 break;
1941 }
1942 }
1943
1944 /* If there is enough space in the read buffer now, let the
1945 * low-level driver know. We use n_tty_chars_in_buffer() to
1946 * check the buffer, as it now knows about canonical mode.
1947 * Otherwise, if the driver is throttled and the line is
1948 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1949 * we won't get any more characters.
1950 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001951 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1952 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 if (b - buf >= minimum)
1957 break;
1958 if (time)
1959 timeout = time;
1960 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001961 mutex_unlock(&tty->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 remove_wait_queue(&tty->read_wait, &wait);
1963
1964 if (!waitqueue_active(&tty->read_wait))
1965 tty->minimum_to_wake = minimum;
1966
1967 __set_current_state(TASK_RUNNING);
1968 size = b - buf;
1969 if (size) {
1970 retval = size;
1971 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001972 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001974 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds55db4c62011-06-04 06:33:24 +09001976 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return retval;
1978}
1979
1980/**
Alan Cox11a96d12008-10-13 10:46:24 +01001981 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 * @tty: tty device
1983 * @file: file object
1984 * @buf: userspace buffer pointer
1985 * @nr: size of I/O
1986 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001987 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001989 * and other such events. Since the receive code will echo characters,
1990 * thus calling driver write methods, the output_lock is used in
1991 * the output processing functions called here as well as in the
1992 * echo processing function to protect the column state and space
1993 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 *
1995 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001996 *
1997 * Locking: output_lock to protect column state and space left
1998 * (note that the process_output*() functions take this
1999 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Alan Cox4edf1822008-02-08 04:18:44 -08002001
Alan Cox11a96d12008-10-13 10:46:24 +01002002static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00002003 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 const unsigned char *b = buf;
2006 DECLARE_WAITQUEUE(wait, current);
2007 int c;
2008 ssize_t retval = 0;
2009
2010 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2011 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2012 retval = tty_check_change(tty);
2013 if (retval)
2014 return retval;
2015 }
2016
Joe Petersona88a69c2009-01-02 13:40:53 +00002017 /* Write out any echoed characters that are still pending */
2018 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 add_wait_queue(&tty->write_wait, &wait);
2021 while (1) {
2022 set_current_state(TASK_INTERRUPTIBLE);
2023 if (signal_pending(current)) {
2024 retval = -ERESTARTSYS;
2025 break;
2026 }
2027 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2028 retval = -EIO;
2029 break;
2030 }
2031 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2032 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00002033 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (num < 0) {
2035 if (num == -EAGAIN)
2036 break;
2037 retval = num;
2038 goto break_out;
2039 }
2040 b += num;
2041 nr -= num;
2042 if (nr == 0)
2043 break;
2044 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00002045 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 break;
2047 b++; nr--;
2048 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002049 if (tty->ops->flush_chars)
2050 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07002052 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002053 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07002054 if (c < 0) {
2055 retval = c;
2056 goto break_out;
2057 }
2058 if (!c)
2059 break;
2060 b += c;
2061 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 if (!nr)
2065 break;
2066 if (file->f_flags & O_NONBLOCK) {
2067 retval = -EAGAIN;
2068 break;
2069 }
2070 schedule();
2071 }
2072break_out:
2073 __set_current_state(TASK_RUNNING);
2074 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002075 if (b - buf != nr && tty->fasync)
2076 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return (b - buf) ? b - buf : retval;
2078}
2079
2080/**
Alan Cox11a96d12008-10-13 10:46:24 +01002081 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * @tty: terminal device
2083 * @file: file accessing it
2084 * @wait: poll table
2085 *
2086 * Called when the line discipline is asked to poll() for data or
2087 * for special events. This code is not serialized with respect to
2088 * other events save open/close.
2089 *
2090 * This code must be sure never to sleep through a hangup.
2091 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Alan Cox4edf1822008-02-08 04:18:44 -08002093
Alan Cox11a96d12008-10-13 10:46:24 +01002094static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002095 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 unsigned int mask = 0;
2098
2099 poll_wait(file, &tty->read_wait, wait);
2100 poll_wait(file, &tty->write_wait, wait);
2101 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2102 mask |= POLLIN | POLLRDNORM;
2103 if (tty->packet && tty->link->ctrl_status)
2104 mask |= POLLPRI | POLLIN | POLLRDNORM;
2105 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2106 mask |= POLLHUP;
2107 if (tty_hung_up_p(file))
2108 mask |= POLLHUP;
2109 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2110 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2111 tty->minimum_to_wake = MIN_CHAR(tty);
2112 else
2113 tty->minimum_to_wake = 1;
2114 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002115 if (tty->ops->write && !tty_is_writelocked(tty) &&
2116 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2117 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 mask |= POLLOUT | POLLWRNORM;
2119 return mask;
2120}
2121
Alan Cox47afa7a2008-10-13 10:44:17 +01002122static unsigned long inq_canon(struct tty_struct *tty)
2123{
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002124 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002125 int nr, head, tail;
2126
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002127 if (!ldata->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002128 return 0;
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002129 head = ldata->canon_head;
2130 tail = ldata->read_tail;
Alan Cox47afa7a2008-10-13 10:44:17 +01002131 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2132 /* Skip EOF-chars.. */
2133 while (head != tail) {
Jiri Slaby3fe780b2012-10-18 22:26:40 +02002134 if (test_bit(tail, ldata->read_flags) &&
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002135 ldata->read_buf[tail] == __DISABLED_CHAR)
Alan Cox47afa7a2008-10-13 10:44:17 +01002136 nr--;
2137 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2138 }
2139 return nr;
2140}
2141
2142static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2143 unsigned int cmd, unsigned long arg)
2144{
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002145 struct n_tty_data *ldata = tty->disc_data;
Alan Cox47afa7a2008-10-13 10:44:17 +01002146 int retval;
2147
2148 switch (cmd) {
2149 case TIOCOUTQ:
2150 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2151 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002152 /* FIXME: Locking */
Jiri Slabyba2e68a2012-10-18 22:26:41 +02002153 retval = ldata->read_cnt;
Alan Cox47afa7a2008-10-13 10:44:17 +01002154 if (L_ICANON(tty))
2155 retval = inq_canon(tty);
2156 return put_user(retval, (unsigned int __user *) arg);
2157 default:
2158 return n_tty_ioctl_helper(tty, file, cmd, arg);
2159 }
2160}
2161
Alan Coxa352def2008-07-16 21:53:12 +01002162struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002163 .magic = TTY_LDISC_MAGIC,
2164 .name = "n_tty",
2165 .open = n_tty_open,
2166 .close = n_tty_close,
2167 .flush_buffer = n_tty_flush_buffer,
2168 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002169 .read = n_tty_read,
2170 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002171 .ioctl = n_tty_ioctl,
2172 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002173 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002174 .receive_buf = n_tty_receive_buf,
2175 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002177
2178/**
2179 * n_tty_inherit_ops - inherit N_TTY methods
2180 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2181 *
2182 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2183 * methods.
2184 */
2185
2186void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2187{
2188 *ops = tty_ldisc_N_TTY;
2189 ops->owner = NULL;
2190 ops->refcount = ops->flags = 0;
2191}
2192EXPORT_SYMBOL_GPL(n_tty_inherit_ops);