blob: 3ebab0cfceb254eb53c9301601bf75db3c3dfb13 [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
Miloslav Trmac522ed772007-07-15 23:40:56 -070076static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
77 unsigned char __user *ptr)
78{
Jiri Slaby6c633f22012-10-18 22:26:37 +020079 tty_audit_add_data(tty, &x, 1, tty->icanon);
Miloslav Trmac522ed772007-07-15 23:40:56 -070080 return put_user(x, ptr);
81}
82
Linus Torvalds55db4c62011-06-04 06:33:24 +090083/**
84 * n_tty_set__room - receive space
85 * @tty: terminal
86 *
87 * Called by the driver to find out how much data it is
88 * permitted to feed to the line discipline without any being lost
89 * and thus to manage flow control. Not serialized. Answers for the
90 * "instant".
91 */
92
93static void n_tty_set_room(struct tty_struct *tty)
94{
Jaeden Amero090abf72012-07-27 08:43:11 -050095 int left;
Linus Torvalds55db4c62011-06-04 06:33:24 +090096 int old_left;
97
Jaeden Amero090abf72012-07-27 08:43:11 -050098 /* tty->read_cnt is not read locked ? */
99 if (I_PARMRK(tty)) {
100 /* Multiply read_cnt by 3, since each byte might take up to
101 * three times as many spaces when PARMRK is set (depending on
102 * its flags, e.g. parity error). */
103 left = N_TTY_BUF_SIZE - tty->read_cnt * 3 - 1;
104 } else
105 left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
106
Linus Torvalds55db4c62011-06-04 06:33:24 +0900107 /*
108 * If we are doing input canonicalization, and there are no
109 * pending newlines, let characters through without limit, so
110 * that erase characters will be handled. Other excess
111 * characters will be beeped.
112 */
113 if (left <= 0)
114 left = tty->icanon && !tty->canon_data;
115 old_left = tty->receive_room;
116 tty->receive_room = left;
117
118 /* Did this open up the receive buffer? We may need to flip */
119 if (left && !old_left)
120 schedule_work(&tty->buf.work);
121}
122
Alan Cox33f0f882006-01-09 20:54:13 -0800123static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 if (tty->read_cnt < N_TTY_BUF_SIZE) {
126 tty->read_buf[tty->read_head] = c;
127 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
128 tty->read_cnt++;
129 }
130}
131
Alan Cox17b82062008-10-13 10:45:06 +0100132/**
133 * put_tty_queue - add character to tty
134 * @c: character
135 * @tty: tty device
136 *
137 * Add a character to the tty read_buf queue. This is done under the
138 * read_lock to serialize character addition and also to protect us
139 * against parallel reads or flushes
140 */
141
Alan Cox33f0f882006-01-09 20:54:13 -0800142static void put_tty_queue(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 unsigned long flags;
145 /*
146 * The problem of stomping on the buffers ends here.
147 * Why didn't anyone see this one coming? --AJK
148 */
149 spin_lock_irqsave(&tty->read_lock, flags);
150 put_tty_queue_nolock(c, tty);
151 spin_unlock_irqrestore(&tty->read_lock, flags);
152}
153
154/**
155 * check_unthrottle - allow new receive data
156 * @tty; tty device
157 *
Alan Cox17b82062008-10-13 10:45:06 +0100158 * Check whether to call the driver unthrottle functions
159 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800160 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * this is not guaranteed.
162 */
Alan Cox4edf1822008-02-08 04:18:44 -0800163static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Alan Cox39c2e602008-04-30 00:54:18 -0700165 if (tty->count)
166 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/**
170 * reset_buffer_flags - reset buffer state
171 * @tty: terminal to reset
172 *
Alan Cox4edf1822008-02-08 04:18:44 -0800173 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * and make sure the driver is unthrottled. Called
175 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100176 *
177 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void reset_buffer_flags(struct tty_struct *tty)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&tty->read_lock, flags);
185 tty->read_head = tty->read_tail = tty->read_cnt = 0;
186 spin_unlock_irqrestore(&tty->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000187
188 mutex_lock(&tty->echo_lock);
189 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
190 mutex_unlock(&tty->echo_lock);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tty->canon_head = tty->canon_data = tty->erasing = 0;
193 memset(&tty->read_flags, 0, sizeof tty->read_flags);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900194 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/**
198 * n_tty_flush_buffer - clean input queue
199 * @tty: terminal device
200 *
201 * Flush the input buffer. Called when the line discipline is
202 * being closed, when the tty layer wants the buffer flushed (eg
203 * at hangup) or when the N_TTY line discipline internally has to
204 * clean the pending queue (for example some signals).
205 *
Alan Cox17b82062008-10-13 10:45:06 +0100206 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Alan Cox4edf1822008-02-08 04:18:44 -0800208
209static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Alan Cox04f378b2008-04-30 00:53:29 -0700211 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* clear everything and unthrottle the driver */
213 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!tty->link)
216 return;
217
Alan Cox04f378b2008-04-30 00:53:29 -0700218 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (tty->link->packet) {
220 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
221 wake_up_interruptible(&tty->link->read_wait);
222 }
Alan Cox04f378b2008-04-30 00:53:29 -0700223 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/**
227 * n_tty_chars_in_buffer - report available bytes
228 * @tty: tty device
229 *
230 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800231 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100232 *
233 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Alan Cox4edf1822008-02-08 04:18:44 -0800235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
237{
238 unsigned long flags;
239 ssize_t n = 0;
240
241 spin_lock_irqsave(&tty->read_lock, flags);
242 if (!tty->icanon) {
243 n = tty->read_cnt;
244 } else if (tty->canon_data) {
245 n = (tty->canon_head > tty->read_tail) ?
246 tty->canon_head - tty->read_tail :
247 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
248 }
249 spin_unlock_irqrestore(&tty->read_lock, flags);
250 return n;
251}
252
253/**
254 * is_utf8_continuation - utf8 multibyte check
255 * @c: byte to check
256 *
257 * Returns true if the utf8 character 'c' is a multibyte continuation
258 * character. We use this to correctly compute the on screen size
259 * of the character when printing
260 */
Alan Cox4edf1822008-02-08 04:18:44 -0800261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262static inline int is_utf8_continuation(unsigned char c)
263{
264 return (c & 0xc0) == 0x80;
265}
266
267/**
268 * is_continuation - multibyte check
269 * @c: byte to check
270 *
271 * Returns true if the utf8 character 'c' is a multibyte continuation
272 * character and the terminal is in unicode mode.
273 */
Alan Cox4edf1822008-02-08 04:18:44 -0800274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static inline int is_continuation(unsigned char c, struct tty_struct *tty)
276{
277 return I_IUTF8(tty) && is_utf8_continuation(c);
278}
279
280/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000281 * do_output_char - output one character
282 * @c: character (or partial unicode symbol)
283 * @tty: terminal device
284 * @space: space available in tty driver write buffer
285 *
286 * This is a helper function that handles one output character
287 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600288 * doing OPOST processing and putting the results in the
289 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000290 *
291 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
292 * and NLDLY. They simply aren't relevant in the world today.
293 * If you ever need them, add them here.
294 *
295 * Returns the number of bytes of buffer space used or -1 if
296 * no space left.
297 *
298 * Locking: should be called under the output_lock to protect
299 * the column state and space left in the buffer
300 */
301
302static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
303{
304 int spaces;
305
306 if (!space)
307 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000308
Joe Petersona88a69c2009-01-02 13:40:53 +0000309 switch (c) {
310 case '\n':
311 if (O_ONLRET(tty))
312 tty->column = 0;
313 if (O_ONLCR(tty)) {
314 if (space < 2)
315 return -1;
316 tty->canon_column = tty->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700317 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000318 return 2;
319 }
320 tty->canon_column = tty->column;
321 break;
322 case '\r':
323 if (O_ONOCR(tty) && tty->column == 0)
324 return 0;
325 if (O_OCRNL(tty)) {
326 c = '\n';
327 if (O_ONLRET(tty))
328 tty->canon_column = tty->column = 0;
329 break;
330 }
331 tty->canon_column = tty->column = 0;
332 break;
333 case '\t':
334 spaces = 8 - (tty->column & 7);
335 if (O_TABDLY(tty) == XTABS) {
336 if (space < spaces)
337 return -1;
338 tty->column += spaces;
339 tty->ops->write(tty, " ", spaces);
340 return spaces;
341 }
342 tty->column += spaces;
343 break;
344 case '\b':
345 if (tty->column > 0)
346 tty->column--;
347 break;
348 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000349 if (!iscntrl(c)) {
350 if (O_OLCUC(tty))
351 c = toupper(c);
352 if (!is_continuation(c, tty))
353 tty->column++;
354 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000355 break;
356 }
357
358 tty_put_char(tty, c);
359 return 1;
360}
361
362/**
363 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * @c: character (or partial unicode symbol)
365 * @tty: terminal device
366 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600367 * Output one character with OPOST processing.
368 * Returns -1 when the output device is full and the character
369 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000371 * Locking: output_lock to protect column state and space left
372 * (also, this is called from n_tty_write under the
373 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Alan Cox4edf1822008-02-08 04:18:44 -0800375
Joe Petersona88a69c2009-01-02 13:40:53 +0000376static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Joe Petersona88a69c2009-01-02 13:40:53 +0000378 int space, retval;
379
380 mutex_lock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alan Coxf34d7a52008-04-30 00:54:13 -0700382 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000383 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Joe Petersona88a69c2009-01-02 13:40:53 +0000385 mutex_unlock(&tty->output_lock);
386 if (retval < 0)
387 return -1;
388 else
389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000393 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600395 * @buf: character buffer
396 * @nr: number of bytes to output
397 *
398 * Output a block of characters with OPOST processing.
399 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *
401 * This path is used to speed up block console writes, among other
402 * things when processing blocks of output data. It handles only
403 * the simple cases normally found and helps to generate blocks of
404 * symbols for the console driver and thus improve performance.
405 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000406 * Locking: output_lock to protect column state and space left
407 * (also, this is called from n_tty_write under the
408 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Alan Cox4edf1822008-02-08 04:18:44 -0800410
Joe Petersona88a69c2009-01-02 13:40:53 +0000411static ssize_t process_output_block(struct tty_struct *tty,
412 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int space;
Thorsten Wißmannbbd20752011-12-08 17:47:33 +0100415 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 const unsigned char *cp;
417
Joe Petersona88a69c2009-01-02 13:40:53 +0000418 mutex_lock(&tty->output_lock);
419
Alan Coxf34d7a52008-04-30 00:54:13 -0700420 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000421 if (!space) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000422 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (nr > space)
426 nr = space;
427
428 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000429 unsigned char c = *cp;
430
431 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 case '\n':
433 if (O_ONLRET(tty))
434 tty->column = 0;
435 if (O_ONLCR(tty))
436 goto break_out;
437 tty->canon_column = tty->column;
438 break;
439 case '\r':
440 if (O_ONOCR(tty) && tty->column == 0)
441 goto break_out;
442 if (O_OCRNL(tty))
443 goto break_out;
444 tty->canon_column = tty->column = 0;
445 break;
446 case '\t':
447 goto break_out;
448 case '\b':
449 if (tty->column > 0)
450 tty->column--;
451 break;
452 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000453 if (!iscntrl(c)) {
454 if (O_OLCUC(tty))
455 goto break_out;
456 if (!is_continuation(c, tty))
457 tty->column++;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 }
461 }
462break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700463 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000464
465 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return i;
467}
468
Joe Petersona88a69c2009-01-02 13:40:53 +0000469/**
470 * process_echoes - write pending echo characters
471 * @tty: terminal device
472 *
473 * Write previously buffered echo (and other ldisc-generated)
474 * characters to the tty.
475 *
476 * Characters generated by the ldisc (including echoes) need to
477 * be buffered because the driver's write buffer can fill during
478 * heavy program output. Echoing straight to the driver will
479 * often fail under these conditions, causing lost characters and
480 * resulting mismatches of ldisc state information.
481 *
482 * Since the ldisc state must represent the characters actually sent
483 * to the driver at the time of the write, operations like certain
484 * changes in column state are also saved in the buffer and executed
485 * here.
486 *
487 * A circular fifo buffer is used so that the most recent characters
488 * are prioritized. Also, when control characters are echoed with a
489 * prefixed "^", the pair is treated atomically and thus not separated.
490 *
491 * Locking: output_lock to protect column state and space left,
492 * echo_lock to protect the echo buffer
493 */
494
495static void process_echoes(struct tty_struct *tty)
496{
497 int space, nr;
498 unsigned char c;
499 unsigned char *cp, *buf_end;
500
501 if (!tty->echo_cnt)
502 return;
503
504 mutex_lock(&tty->output_lock);
505 mutex_lock(&tty->echo_lock);
506
507 space = tty_write_room(tty);
508
509 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
510 cp = tty->echo_buf + tty->echo_pos;
511 nr = tty->echo_cnt;
512 while (nr > 0) {
513 c = *cp;
514 if (c == ECHO_OP_START) {
515 unsigned char op;
516 unsigned char *opp;
517 int no_space_left = 0;
518
519 /*
520 * If the buffer byte is the start of a multi-byte
521 * operation, get the next byte, which is either the
522 * op code or a control character value.
523 */
524 opp = cp + 1;
525 if (opp == buf_end)
526 opp -= N_TTY_BUF_SIZE;
527 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000528
Joe Petersona88a69c2009-01-02 13:40:53 +0000529 switch (op) {
530 unsigned int num_chars, num_bs;
531
532 case ECHO_OP_ERASE_TAB:
533 if (++opp == buf_end)
534 opp -= N_TTY_BUF_SIZE;
535 num_chars = *opp;
536
537 /*
538 * Determine how many columns to go back
539 * in order to erase the tab.
540 * This depends on the number of columns
541 * used by other characters within the tab
542 * area. If this (modulo 8) count is from
543 * the start of input rather than from a
544 * previous tab, we offset by canon column.
545 * Otherwise, tab spacing is normal.
546 */
547 if (!(num_chars & 0x80))
548 num_chars += tty->canon_column;
549 num_bs = 8 - (num_chars & 7);
550
551 if (num_bs > space) {
552 no_space_left = 1;
553 break;
554 }
555 space -= num_bs;
556 while (num_bs--) {
557 tty_put_char(tty, '\b');
558 if (tty->column > 0)
559 tty->column--;
560 }
561 cp += 3;
562 nr -= 3;
563 break;
564
565 case ECHO_OP_SET_CANON_COL:
566 tty->canon_column = tty->column;
567 cp += 2;
568 nr -= 2;
569 break;
570
571 case ECHO_OP_MOVE_BACK_COL:
572 if (tty->column > 0)
573 tty->column--;
574 cp += 2;
575 nr -= 2;
576 break;
577
578 case ECHO_OP_START:
579 /* This is an escaped echo op start code */
580 if (!space) {
581 no_space_left = 1;
582 break;
583 }
584 tty_put_char(tty, ECHO_OP_START);
585 tty->column++;
586 space--;
587 cp += 2;
588 nr -= 2;
589 break;
590
591 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000592 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600593 * If the op is not a special byte code,
594 * it is a ctrl char tagged to be echoed
595 * as "^X" (where X is the letter
596 * representing the control char).
597 * Note that we must ensure there is
598 * enough space for the whole ctrl pair.
599 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000600 */
Joe Peterson62b26352009-09-09 15:03:47 -0600601 if (space < 2) {
602 no_space_left = 1;
603 break;
604 }
605 tty_put_char(tty, '^');
606 tty_put_char(tty, op ^ 0100);
607 tty->column += 2;
608 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000609 cp += 2;
610 nr -= 2;
611 }
612
613 if (no_space_left)
614 break;
615 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600616 if (O_OPOST(tty) &&
617 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
618 int retval = do_output_char(c, tty, space);
619 if (retval < 0)
620 break;
621 space -= retval;
622 } else {
623 if (!space)
624 break;
625 tty_put_char(tty, c);
626 space -= 1;
627 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000628 cp += 1;
629 nr -= 1;
630 }
631
632 /* When end of circular buffer reached, wrap around */
633 if (cp >= buf_end)
634 cp -= N_TTY_BUF_SIZE;
635 }
636
637 if (nr == 0) {
638 tty->echo_pos = 0;
639 tty->echo_cnt = 0;
640 tty->echo_overrun = 0;
641 } else {
642 int num_processed = tty->echo_cnt - nr;
643 tty->echo_pos += num_processed;
644 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
645 tty->echo_cnt = nr;
646 if (num_processed > 0)
647 tty->echo_overrun = 0;
648 }
649
650 mutex_unlock(&tty->echo_lock);
651 mutex_unlock(&tty->output_lock);
652
653 if (tty->ops->flush_chars)
654 tty->ops->flush_chars(tty);
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000658 * add_echo_byte - add a byte to the echo buffer
659 * @c: unicode byte to echo
660 * @tty: terminal device
661 *
662 * Add a character or operation byte to the echo buffer.
663 *
664 * Should be called under the echo lock to protect the echo buffer.
665 */
666
667static void add_echo_byte(unsigned char c, struct tty_struct *tty)
668{
669 int new_byte_pos;
670
671 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
672 /* Circular buffer is already at capacity */
673 new_byte_pos = tty->echo_pos;
674
675 /*
676 * Since the buffer start position needs to be advanced,
677 * be sure to step by a whole operation byte group.
678 */
Alan Cox300a6202009-01-02 13:41:04 +0000679 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000680 if (tty->echo_buf[(tty->echo_pos + 1) &
681 (N_TTY_BUF_SIZE - 1)] ==
682 ECHO_OP_ERASE_TAB) {
683 tty->echo_pos += 3;
684 tty->echo_cnt -= 2;
685 } else {
686 tty->echo_pos += 2;
687 tty->echo_cnt -= 1;
688 }
689 } else {
690 tty->echo_pos++;
691 }
692 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
693
694 tty->echo_overrun = 1;
695 } else {
696 new_byte_pos = tty->echo_pos + tty->echo_cnt;
697 new_byte_pos &= N_TTY_BUF_SIZE - 1;
698 tty->echo_cnt++;
699 }
700
701 tty->echo_buf[new_byte_pos] = c;
702}
703
704/**
705 * echo_move_back_col - add operation to move back a column
706 * @tty: terminal device
707 *
708 * Add an operation to the echo buffer to move back one column.
709 *
710 * Locking: echo_lock to protect the echo buffer
711 */
712
713static void echo_move_back_col(struct tty_struct *tty)
714{
715 mutex_lock(&tty->echo_lock);
716
717 add_echo_byte(ECHO_OP_START, tty);
718 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
719
720 mutex_unlock(&tty->echo_lock);
721}
722
723/**
724 * echo_set_canon_col - add operation to set the canon column
725 * @tty: terminal device
726 *
727 * Add an operation to the echo buffer to set the canon column
728 * to the current column.
729 *
730 * Locking: echo_lock to protect the echo buffer
731 */
732
733static void echo_set_canon_col(struct tty_struct *tty)
734{
735 mutex_lock(&tty->echo_lock);
736
737 add_echo_byte(ECHO_OP_START, tty);
738 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
739
740 mutex_unlock(&tty->echo_lock);
741}
742
743/**
744 * echo_erase_tab - add operation to erase a tab
745 * @num_chars: number of character columns already used
746 * @after_tab: true if num_chars starts after a previous tab
747 * @tty: terminal device
748 *
749 * Add an operation to the echo buffer to erase a tab.
750 *
751 * Called by the eraser function, which knows how many character
752 * columns have been used since either a previous tab or the start
753 * of input. This information will be used later, along with
754 * canon column (if applicable), to go back the correct number
755 * of columns.
756 *
757 * Locking: echo_lock to protect the echo buffer
758 */
759
760static void echo_erase_tab(unsigned int num_chars, int after_tab,
761 struct tty_struct *tty)
762{
763 mutex_lock(&tty->echo_lock);
764
765 add_echo_byte(ECHO_OP_START, tty);
766 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
767
768 /* We only need to know this modulo 8 (tab spacing) */
769 num_chars &= 7;
770
771 /* Set the high bit as a flag if num_chars is after a previous tab */
772 if (after_tab)
773 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000774
Joe Petersona88a69c2009-01-02 13:40:53 +0000775 add_echo_byte(num_chars, tty);
776
777 mutex_unlock(&tty->echo_lock);
778}
779
780/**
781 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * @c: unicode byte to echo
783 * @tty: terminal device
784 *
Alan Cox4edf1822008-02-08 04:18:44 -0800785 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100787 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000788 * This variant does not treat control characters specially.
789 *
790 * Locking: echo_lock to protect the echo buffer
791 */
792
793static void echo_char_raw(unsigned char c, struct tty_struct *tty)
794{
795 mutex_lock(&tty->echo_lock);
796
797 if (c == ECHO_OP_START) {
798 add_echo_byte(ECHO_OP_START, tty);
799 add_echo_byte(ECHO_OP_START, tty);
800 } else {
801 add_echo_byte(c, tty);
802 }
803
804 mutex_unlock(&tty->echo_lock);
805}
806
807/**
808 * echo_char - echo a character
809 * @c: unicode byte to echo
810 * @tty: terminal device
811 *
812 * Echo user input back onto the screen. This must be called only when
813 * L_ECHO(tty) is true. Called from the driver receive_buf path.
814 *
Joe Peterson62b26352009-09-09 15:03:47 -0600815 * This variant tags control characters to be echoed as "^X"
816 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000817 *
818 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
820
821static void echo_char(unsigned char c, struct tty_struct *tty)
822{
Joe Petersona88a69c2009-01-02 13:40:53 +0000823 mutex_lock(&tty->echo_lock);
824
825 if (c == ECHO_OP_START) {
826 add_echo_byte(ECHO_OP_START, tty);
827 add_echo_byte(ECHO_OP_START, tty);
828 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600829 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Joe Petersona88a69c2009-01-02 13:40:53 +0000830 add_echo_byte(ECHO_OP_START, tty);
831 add_echo_byte(c, tty);
832 }
833
834 mutex_unlock(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Alan Cox17b82062008-10-13 10:45:06 +0100837/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000838 * finish_erasing - complete erase
Alan Cox17b82062008-10-13 10:45:06 +0100839 * @tty: tty doing the erase
Alan Cox17b82062008-10-13 10:45:06 +0100840 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static inline void finish_erasing(struct tty_struct *tty)
843{
844 if (tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000845 echo_char_raw('/', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 tty->erasing = 0;
847 }
848}
849
850/**
851 * eraser - handle erase function
852 * @c: character input
853 * @tty: terminal device
854 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200855 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * present in the stream from the driver layer. Handles the complexities
857 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100858 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000859 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Alan Cox4edf1822008-02-08 04:18:44 -0800861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static void eraser(unsigned char c, struct tty_struct *tty)
863{
864 enum { ERASE, WERASE, KILL } kill_type;
865 int head, seen_alnums, cnt;
866 unsigned long flags;
867
Alan Cox17b82062008-10-13 10:45:06 +0100868 /* FIXME: locking needed ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (tty->read_head == tty->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000870 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return;
872 }
873 if (c == ERASE_CHAR(tty))
874 kill_type = ERASE;
875 else if (c == WERASE_CHAR(tty))
876 kill_type = WERASE;
877 else {
878 if (!L_ECHO(tty)) {
879 spin_lock_irqsave(&tty->read_lock, flags);
880 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
881 (N_TTY_BUF_SIZE - 1));
882 tty->read_head = tty->canon_head;
883 spin_unlock_irqrestore(&tty->read_lock, flags);
884 return;
885 }
886 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
887 spin_lock_irqsave(&tty->read_lock, flags);
888 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
889 (N_TTY_BUF_SIZE - 1));
890 tty->read_head = tty->canon_head;
891 spin_unlock_irqrestore(&tty->read_lock, flags);
892 finish_erasing(tty);
893 echo_char(KILL_CHAR(tty), tty);
894 /* Add a newline if ECHOK is on and ECHOKE is off. */
895 if (L_ECHOK(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000896 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return;
898 }
899 kill_type = KILL;
900 }
901
902 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100903 /* FIXME: Locking ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 while (tty->read_head != tty->canon_head) {
905 head = tty->read_head;
906
907 /* erase a single possibly multibyte character */
908 do {
909 head = (head - 1) & (N_TTY_BUF_SIZE-1);
910 c = tty->read_buf[head];
911 } while (is_continuation(c, tty) && head != tty->canon_head);
912
913 /* do not partially erase */
914 if (is_continuation(c, tty))
915 break;
916
917 if (kill_type == WERASE) {
918 /* Equivalent to BSD's ALTWERASE. */
919 if (isalnum(c) || c == '_')
920 seen_alnums++;
921 else if (seen_alnums)
922 break;
923 }
924 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
925 spin_lock_irqsave(&tty->read_lock, flags);
926 tty->read_head = head;
927 tty->read_cnt -= cnt;
928 spin_unlock_irqrestore(&tty->read_lock, flags);
929 if (L_ECHO(tty)) {
930 if (L_ECHOPRT(tty)) {
931 if (!tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000932 echo_char_raw('\\', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 tty->erasing = 1;
934 }
935 /* if cnt > 1, output a multi-byte character */
936 echo_char(c, tty);
937 while (--cnt > 0) {
938 head = (head+1) & (N_TTY_BUF_SIZE-1);
Joe Petersona88a69c2009-01-02 13:40:53 +0000939 echo_char_raw(tty->read_buf[head], tty);
940 echo_move_back_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
943 echo_char(ERASE_CHAR(tty), tty);
944 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000945 unsigned int num_chars = 0;
946 int after_tab = 0;
947 unsigned long tail = tty->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Joe Petersona88a69c2009-01-02 13:40:53 +0000949 /*
950 * Count the columns used for characters
951 * since the start of input or after a
952 * previous tab.
953 * This info is used to go back the correct
954 * number of columns.
955 */
956 while (tail != tty->canon_head) {
957 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 c = tty->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000959 if (c == '\t') {
960 after_tab = 1;
961 break;
Alan Cox300a6202009-01-02 13:41:04 +0000962 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000964 num_chars += 2;
965 } else if (!is_continuation(c, tty)) {
966 num_chars++;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000969 echo_erase_tab(num_chars, after_tab, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 } else {
971 if (iscntrl(c) && L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000972 echo_char_raw('\b', tty);
973 echo_char_raw(' ', tty);
974 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000977 echo_char_raw('\b', tty);
978 echo_char_raw(' ', tty);
979 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 }
982 }
983 if (kill_type == ERASE)
984 break;
985 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000986 if (tty->read_head == tty->canon_head && L_ECHO(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 finish_erasing(tty);
988}
989
990/**
991 * isig - handle the ISIG optio
992 * @sig: signal
993 * @tty: terminal
994 * @flush: force flush
995 *
996 * Called when a signal is being sent due to terminal input. This
997 * may caus terminal flushing to take place according to the termios
998 * settings and character used. Called from the driver receive_buf
999 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +01001000 *
1001 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Alan Cox4edf1822008-02-08 04:18:44 -08001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static inline void isig(int sig, struct tty_struct *tty, int flush)
1005{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001006 if (tty->pgrp)
1007 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (flush || !L_NOFLSH(tty)) {
1009 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001010 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012}
1013
1014/**
1015 * n_tty_receive_break - handle break
1016 * @tty: terminal
1017 *
1018 * An RS232 break event has been hit in the incoming bitstream. This
1019 * can cause a variety of events depending upon the termios settings.
1020 *
1021 * Called from the receive_buf path so single threaded.
1022 */
Alan Cox4edf1822008-02-08 04:18:44 -08001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static inline void n_tty_receive_break(struct tty_struct *tty)
1025{
1026 if (I_IGNBRK(tty))
1027 return;
1028 if (I_BRKINT(tty)) {
1029 isig(SIGINT, tty, 1);
1030 return;
1031 }
1032 if (I_PARMRK(tty)) {
1033 put_tty_queue('\377', tty);
1034 put_tty_queue('\0', tty);
1035 }
1036 put_tty_queue('\0', tty);
1037 wake_up_interruptible(&tty->read_wait);
1038}
1039
1040/**
1041 * n_tty_receive_overrun - handle overrun reporting
1042 * @tty: terminal
1043 *
1044 * Data arrived faster than we could process it. While the tty
1045 * driver has flagged this the bits that were missed are gone
1046 * forever.
1047 *
1048 * Called from the receive_buf path so single threaded. Does not
1049 * need locking as num_overrun and overrun_time are function
1050 * private.
1051 */
Alan Cox4edf1822008-02-08 04:18:44 -08001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static inline void n_tty_receive_overrun(struct tty_struct *tty)
1054{
1055 char buf[64];
1056
1057 tty->num_overrun++;
1058 if (time_before(tty->overrun_time, jiffies - HZ) ||
1059 time_after(tty->overrun_time, jiffies)) {
1060 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1061 tty_name(tty, buf),
1062 tty->num_overrun);
1063 tty->overrun_time = jiffies;
1064 tty->num_overrun = 0;
1065 }
1066}
1067
1068/**
1069 * n_tty_receive_parity_error - error notifier
1070 * @tty: terminal device
1071 * @c: character
1072 *
1073 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001074 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
1076static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1077 unsigned char c)
1078{
Alan Cox4edf1822008-02-08 04:18:44 -08001079 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (I_PARMRK(tty)) {
1082 put_tty_queue('\377', tty);
1083 put_tty_queue('\0', tty);
1084 put_tty_queue(c, tty);
1085 } else if (I_INPCK(tty))
1086 put_tty_queue('\0', tty);
1087 else
1088 put_tty_queue(c, tty);
1089 wake_up_interruptible(&tty->read_wait);
1090}
1091
1092/**
1093 * n_tty_receive_char - perform processing
1094 * @tty: terminal device
1095 * @c: character
1096 *
1097 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001098 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * driver above.
1100 */
1101
1102static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1103{
1104 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001105 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (tty->raw) {
1108 put_tty_queue(c, tty);
1109 return;
1110 }
Alan Cox4edf1822008-02-08 04:18:44 -08001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (I_ISTRIP(tty))
1113 c &= 0x7f;
1114 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001115 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
hyc@symas.com26df6d12010-06-22 10:14:49 -07001117 if (L_EXTPROC(tty)) {
1118 put_tty_queue(c, tty);
1119 return;
1120 }
1121
Joe Peterson54d2a372008-02-06 01:37:59 -08001122 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001123 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1124 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001125 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001126 process_echoes(tty);
1127 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (tty->closing) {
1130 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001131 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001133 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001134 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 stop_tty(tty);
1136 }
1137 return;
1138 }
1139
1140 /*
1141 * If the previous character was LNEXT, or we know that this
1142 * character is not one of the characters that we'll have to
1143 * handle specially, do shortcut processing to speed things
1144 * up.
1145 */
1146 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 tty->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001148 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1149 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1150 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001151 if (L_ECHO(tty))
1152 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001153 return;
1154 }
1155 if (L_ECHO(tty)) {
1156 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* Record the column of first canon char. */
1158 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001159 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001161 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001163 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 put_tty_queue(c, tty);
1165 put_tty_queue(c, tty);
1166 return;
1167 }
Alan Cox4edf1822008-02-08 04:18:44 -08001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (I_IXON(tty)) {
1170 if (c == START_CHAR(tty)) {
1171 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001172 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return;
1174 }
1175 if (c == STOP_CHAR(tty)) {
1176 stop_tty(tty);
1177 return;
1178 }
1179 }
Joe Peterson575537b32008-04-30 00:53:30 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (L_ISIG(tty)) {
1182 int signal;
1183 signal = SIGINT;
1184 if (c == INTR_CHAR(tty))
1185 goto send_signal;
1186 signal = SIGQUIT;
1187 if (c == QUIT_CHAR(tty))
1188 goto send_signal;
1189 signal = SIGTSTP;
1190 if (c == SUSP_CHAR(tty)) {
1191send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001192 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001193 * Note that we do not use isig() here because we want
1194 * the order to be:
1195 * 1) flush, 2) echo, 3) signal
1196 */
1197 if (!L_NOFLSH(tty)) {
1198 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001199 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001200 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001201 if (I_IXON(tty))
1202 start_tty(tty);
1203 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001204 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001205 process_echoes(tty);
1206 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001207 if (tty->pgrp)
1208 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return;
1210 }
1211 }
Joe Peterson575537b32008-04-30 00:53:30 -07001212
1213 if (c == '\r') {
1214 if (I_IGNCR(tty))
1215 return;
1216 if (I_ICRNL(tty))
1217 c = '\n';
1218 } else if (c == '\n' && I_INLCR(tty))
1219 c = '\r';
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (tty->icanon) {
1222 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1223 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1224 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001225 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return;
1227 }
1228 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1229 tty->lnext = 1;
1230 if (L_ECHO(tty)) {
1231 finish_erasing(tty);
1232 if (L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001233 echo_char_raw('^', tty);
1234 echo_char_raw('\b', tty);
1235 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 }
1238 return;
1239 }
1240 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1241 L_IEXTEN(tty)) {
1242 unsigned long tail = tty->canon_head;
1243
1244 finish_erasing(tty);
1245 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001246 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 while (tail != tty->read_head) {
1248 echo_char(tty->read_buf[tail], tty);
1249 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1250 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001251 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
1253 }
1254 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001255 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001256 if (L_ECHO(tty))
1257 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001258 return;
1259 }
1260 if (L_ECHO(tty) || L_ECHONL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001261 echo_char_raw('\n', tty);
1262 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 goto handle_newline;
1265 }
1266 if (c == EOF_CHAR(tty)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001267 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1268 return;
Alan Cox4edf1822008-02-08 04:18:44 -08001269 if (tty->canon_head != tty->read_head)
1270 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 c = __DISABLED_CHAR;
1272 goto handle_newline;
1273 }
1274 if ((c == EOL_CHAR(tty)) ||
1275 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001276 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1277 ? 1 : 0;
1278 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001279 if (L_ECHO(tty))
1280 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001281 return;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /*
1284 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1285 */
1286 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* Record the column of first canon char. */
1288 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001289 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001291 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 /*
1294 * XXX does PARMRK doubling happen for
1295 * EOL_CHAR and EOL2_CHAR?
1296 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001297 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 put_tty_queue(c, tty);
1299
Alan Cox4edf1822008-02-08 04:18:44 -08001300handle_newline:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 spin_lock_irqsave(&tty->read_lock, flags);
1302 set_bit(tty->read_head, tty->read_flags);
1303 put_tty_queue_nolock(c, tty);
1304 tty->canon_head = tty->read_head;
1305 tty->canon_data++;
1306 spin_unlock_irqrestore(&tty->read_lock, flags);
1307 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1308 if (waitqueue_active(&tty->read_wait))
1309 wake_up_interruptible(&tty->read_wait);
1310 return;
1311 }
1312 }
Alan Cox4edf1822008-02-08 04:18:44 -08001313
Joe Petersonacc71bb2009-01-02 13:43:32 +00001314 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1315 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1316 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001317 if (L_ECHO(tty))
1318 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001319 return;
1320 }
1321 if (L_ECHO(tty)) {
1322 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (c == '\n')
Joe Petersona88a69c2009-01-02 13:40:53 +00001324 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 else {
1326 /* Record the column of first canon char. */
1327 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001328 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 echo_char(c, tty);
1330 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001331 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Joe Petersonacc71bb2009-01-02 13:43:32 +00001334 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 put_tty_queue(c, tty);
1336
1337 put_tty_queue(c, tty);
Alan Cox4edf1822008-02-08 04:18:44 -08001338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341/**
1342 * n_tty_write_wakeup - asynchronous I/O notifier
1343 * @tty: tty device
1344 *
1345 * Required for the ptys, serial driver etc. since processes
1346 * that attach themselves to the master and rely on ASYNC
1347 * IO must be woken up
1348 */
1349
1350static void n_tty_write_wakeup(struct tty_struct *tty)
1351{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001352 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356/**
1357 * n_tty_receive_buf - data receive
1358 * @tty: terminal device
1359 * @cp: buffer
1360 * @fp: flag buffer
1361 * @count: characters
1362 *
1363 * Called by the terminal driver when a block of characters has
1364 * been received. This function must be called from soft contexts
1365 * not from interrupt context. The driver is responsible for making
1366 * calls one at a time and in order (or using flush_to_ldisc)
1367 */
Alan Cox4edf1822008-02-08 04:18:44 -08001368
Linus Torvalds55db4c62011-06-04 06:33:24 +09001369static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1370 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 const unsigned char *p;
1373 char *f, flags = TTY_NORMAL;
1374 int i;
1375 char buf[64];
1376 unsigned long cpuflags;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (tty->real_raw) {
1379 spin_lock_irqsave(&tty->read_lock, cpuflags);
1380 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1381 N_TTY_BUF_SIZE - tty->read_head);
1382 i = min(count, i);
1383 memcpy(tty->read_buf + tty->read_head, cp, i);
1384 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1385 tty->read_cnt += i;
1386 cp += i;
1387 count -= i;
1388
1389 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1390 N_TTY_BUF_SIZE - tty->read_head);
1391 i = min(count, i);
1392 memcpy(tty->read_buf + tty->read_head, cp, i);
1393 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1394 tty->read_cnt += i;
1395 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1396 } else {
Alan Cox4edf1822008-02-08 04:18:44 -08001397 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (f)
1399 flags = *f++;
1400 switch (flags) {
1401 case TTY_NORMAL:
1402 n_tty_receive_char(tty, *p);
1403 break;
1404 case TTY_BREAK:
1405 n_tty_receive_break(tty);
1406 break;
1407 case TTY_PARITY:
1408 case TTY_FRAME:
1409 n_tty_receive_parity_error(tty, *p);
1410 break;
1411 case TTY_OVERRUN:
1412 n_tty_receive_overrun(tty);
1413 break;
1414 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001415 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 tty_name(tty, buf), flags);
1417 break;
1418 }
1419 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001420 if (tty->ops->flush_chars)
1421 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Linus Torvalds55db4c62011-06-04 06:33:24 +09001424 n_tty_set_room(tty);
1425
hyc@symas.com26df6d12010-06-22 10:14:49 -07001426 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1427 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1429 if (waitqueue_active(&tty->read_wait))
1430 wake_up_interruptible(&tty->read_wait);
1431 }
1432
1433 /*
1434 * Check the remaining room for the input canonicalization
1435 * mode. We don't want to throttle the driver if we're in
1436 * canonical mode and don't have a newline yet!
1437 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001438 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001439 tty_throttle(tty);
Alan Cox0a44ab42012-06-22 16:40:20 +01001440
1441 /* FIXME: there is a tiny race here if the receive room check runs
1442 before the other work executes and empties the buffer (upping
1443 the receiving room and unthrottling. We then throttle and get
1444 stuck. This has been observed and traced down by Vincent Pillet/
1445 We need to address this when we sort out out the rx path locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448int is_ignored(int sig)
1449{
1450 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001451 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454/**
1455 * n_tty_set_termios - termios data changed
1456 * @tty: terminal
1457 * @old: previous data
1458 *
1459 * Called by the tty layer when the user changes termios flags so
1460 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001461 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * guaranteed that this function will not be re-entered or in progress
1463 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001464 *
1465 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Alan Cox4edf1822008-02-08 04:18:44 -08001467
1468static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Alan Cox47afa7a2008-10-13 10:44:17 +01001470 int canon_change = 1;
Alan Cox47afa7a2008-10-13 10:44:17 +01001471
1472 if (old)
Alan Coxadc8d742012-07-14 15:31:47 +01001473 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
Alan Cox47afa7a2008-10-13 10:44:17 +01001474 if (canon_change) {
1475 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1476 tty->canon_head = tty->read_tail;
1477 tty->canon_data = 0;
1478 tty->erasing = 0;
1479 }
1480
1481 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1482 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 tty->icanon = (L_ICANON(tty) != 0);
1485 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1486 tty->raw = 1;
1487 tty->real_raw = 1;
Linus Torvalds55db4c62011-06-04 06:33:24 +09001488 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490 }
1491 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1492 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1493 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1494 I_PARMRK(tty)) {
1495 memset(tty->process_char_map, 0, 256/8);
1496
1497 if (I_IGNCR(tty) || I_ICRNL(tty))
1498 set_bit('\r', tty->process_char_map);
1499 if (I_INLCR(tty))
1500 set_bit('\n', tty->process_char_map);
1501
1502 if (L_ICANON(tty)) {
1503 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1504 set_bit(KILL_CHAR(tty), tty->process_char_map);
1505 set_bit(EOF_CHAR(tty), tty->process_char_map);
1506 set_bit('\n', tty->process_char_map);
1507 set_bit(EOL_CHAR(tty), tty->process_char_map);
1508 if (L_IEXTEN(tty)) {
1509 set_bit(WERASE_CHAR(tty),
1510 tty->process_char_map);
1511 set_bit(LNEXT_CHAR(tty),
1512 tty->process_char_map);
1513 set_bit(EOL2_CHAR(tty),
1514 tty->process_char_map);
1515 if (L_ECHO(tty))
1516 set_bit(REPRINT_CHAR(tty),
1517 tty->process_char_map);
1518 }
1519 }
1520 if (I_IXON(tty)) {
1521 set_bit(START_CHAR(tty), tty->process_char_map);
1522 set_bit(STOP_CHAR(tty), tty->process_char_map);
1523 }
1524 if (L_ISIG(tty)) {
1525 set_bit(INTR_CHAR(tty), tty->process_char_map);
1526 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1527 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1528 }
1529 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1530 tty->raw = 0;
1531 tty->real_raw = 0;
1532 } else {
1533 tty->raw = 1;
1534 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1535 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1536 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1537 tty->real_raw = 1;
1538 else
1539 tty->real_raw = 0;
1540 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001541 n_tty_set_room(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001542 /* The termios change make the tty ready for I/O */
1543 wake_up_interruptible(&tty->write_wait);
1544 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547/**
1548 * n_tty_close - close the ldisc for this tty
1549 * @tty: device
1550 *
Alan Cox4edf1822008-02-08 04:18:44 -08001551 * Called from the terminal layer when this line discipline is
1552 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * discipline change. The function will not be called while other
1554 * ldisc methods are in progress.
1555 */
Alan Cox4edf1822008-02-08 04:18:44 -08001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557static void n_tty_close(struct tty_struct *tty)
1558{
1559 n_tty_flush_buffer(tty);
Jiri Slabyb91939f2012-10-18 22:26:35 +02001560 kfree(tty->read_buf);
1561 kfree(tty->echo_buf);
1562 tty->read_buf = NULL;
1563 tty->echo_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/**
1567 * n_tty_open - open an ldisc
1568 * @tty: terminal to open
1569 *
Alan Cox4edf1822008-02-08 04:18:44 -08001570 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * terminal device. Can sleep. Called serialized so that no
1572 * other events will occur in parallel. No further open will occur
1573 * until a close.
1574 */
1575
1576static int n_tty_open(struct tty_struct *tty)
1577{
Joe Petersona88a69c2009-01-02 13:40:53 +00001578 /* These are ugly. Currently a malloc failure here can panic */
Jiri Slabyb91939f2012-10-18 22:26:35 +02001579 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1580 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1581 if (!tty->read_buf || !tty->echo_buf)
1582 goto err_free_bufs;
Alan Cox0b4068a2009-06-11 13:05:49 +01001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 reset_buffer_flags(tty);
Andrew McGregor7b292b42011-06-13 11:31:31 +12001585 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 tty->column = 0;
1587 n_tty_set_termios(tty, NULL);
1588 tty->minimum_to_wake = 1;
1589 tty->closing = 0;
1590 return 0;
Jiri Slabyb91939f2012-10-18 22:26:35 +02001591err_free_bufs:
1592 kfree(tty->read_buf);
1593 kfree(tty->echo_buf);
1594
1595 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598static inline int input_available_p(struct tty_struct *tty, int amt)
1599{
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001600 tty_flush_to_ldisc(tty);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001601 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (tty->canon_data)
1603 return 1;
1604 } else if (tty->read_cnt >= (amt ? amt : 1))
1605 return 1;
1606
1607 return 0;
1608}
1609
1610/**
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001611 * copy_from_read_buf - copy read data directly
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * @tty: terminal device
1613 * @b: user data
1614 * @nr: size of data
1615 *
Alan Cox11a96d12008-10-13 10:46:24 +01001616 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * ICANON is off; it copies characters straight from the tty queue to
1618 * user space directly. It can be profitably called twice; once to
1619 * drain the space from the tail pointer to the (physical) end of the
1620 * buffer, and once to drain the space from the (physical) beginning of
1621 * the buffer to head pointer.
1622 *
Paul Fulghum817d6d32006-06-28 04:26:47 -07001623 * Called under the tty->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 *
1625 */
Alan Cox4edf1822008-02-08 04:18:44 -08001626
Alan Cox33f0f882006-01-09 20:54:13 -08001627static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 unsigned char __user **b,
1629 size_t *nr)
1630
1631{
1632 int retval;
1633 size_t n;
1634 unsigned long flags;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001635 bool is_eof;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 retval = 0;
1638 spin_lock_irqsave(&tty->read_lock, flags);
1639 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1640 n = min(*nr, n);
1641 spin_unlock_irqrestore(&tty->read_lock, flags);
1642 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1644 n -= retval;
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001645 is_eof = n == 1 &&
1646 tty->read_buf[tty->read_tail] == EOF_CHAR(tty);
Jiri Slaby6c633f22012-10-18 22:26:37 +02001647 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n,
1648 tty->icanon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 spin_lock_irqsave(&tty->read_lock, flags);
1650 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1651 tty->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001652 /* Turn single EOF into zero-length read */
Jiri Slaby3fa10cc2012-04-26 20:13:00 +02001653 if (L_EXTPROC(tty) && tty->icanon && is_eof && !tty->read_cnt)
1654 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 spin_unlock_irqrestore(&tty->read_lock, flags);
1656 *b += n;
1657 *nr -= n;
1658 }
1659 return retval;
1660}
1661
Al Virocc4191d2008-03-29 03:08:48 +00001662extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001663 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665/**
1666 * job_control - check job control
1667 * @tty: tty
1668 * @file: file handle
1669 *
1670 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001671 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001673 *
1674 * FIXME:
1675 * Locking: None - redirected write test is safe, testing
1676 * current->signal should possibly lock current->sighand
1677 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Alan Cox4edf1822008-02-08 04:18:44 -08001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static int job_control(struct tty_struct *tty, struct file *file)
1681{
1682 /* Job control check -- must be done at start and after
1683 every sleep (POSIX.1 7.1.1.4). */
1684 /* NOTE: not yet done after every sleep pending a thorough
1685 check of the logic of this change. -- jlc */
1686 /* don't stop on /dev/console */
1687 if (file->f_op->write != redirected_tty_write &&
1688 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001689 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001690 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001691 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001693 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001695 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001696 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -ERESTARTSYS;
1698 }
1699 }
1700 return 0;
1701}
Alan Cox4edf1822008-02-08 04:18:44 -08001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704/**
Alan Cox11a96d12008-10-13 10:46:24 +01001705 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 * @tty: tty device
1707 * @file: file object
1708 * @buf: userspace buffer pointer
1709 * @nr: size of I/O
1710 *
1711 * Perform reads for the line discipline. We are guaranteed that the
1712 * line discipline will not be closed under us but we may get multiple
1713 * parallel readers and must handle this ourselves. We may also get
1714 * a hangup. Always called in user context, may sleep.
1715 *
1716 * This code must be sure never to sleep through a hangup.
1717 */
Alan Cox4edf1822008-02-08 04:18:44 -08001718
Alan Cox11a96d12008-10-13 10:46:24 +01001719static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned char __user *buf, size_t nr)
1721{
1722 unsigned char __user *b = buf;
1723 DECLARE_WAITQUEUE(wait, current);
1724 int c;
1725 int minimum, time;
1726 ssize_t retval = 0;
1727 ssize_t size;
1728 long timeout;
1729 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001730 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732do_it_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001734 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 minimum = time = 0;
1738 timeout = MAX_SCHEDULE_TIMEOUT;
1739 if (!tty->icanon) {
1740 time = (HZ / 10) * TIME_CHAR(tty);
1741 minimum = MIN_CHAR(tty);
1742 if (minimum) {
1743 if (time)
1744 tty->minimum_to_wake = 1;
1745 else if (!waitqueue_active(&tty->read_wait) ||
1746 (tty->minimum_to_wake > minimum))
1747 tty->minimum_to_wake = minimum;
1748 } else {
1749 timeout = 0;
1750 if (time) {
1751 timeout = time;
1752 time = 0;
1753 }
1754 tty->minimum_to_wake = minimum = 1;
1755 }
1756 }
1757
1758 /*
1759 * Internal serialization of reads.
1760 */
1761 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001762 if (!mutex_trylock(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001764 } else {
Ingo Molnar70522e12006-03-23 03:00:31 -08001765 if (mutex_lock_interruptible(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -ERESTARTSYS;
1767 }
Alan Cox04f378b2008-04-30 00:53:29 -07001768 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 while (nr) {
1772 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001773 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 unsigned char cs;
1775 if (b != buf)
1776 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001777 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 cs = tty->link->ctrl_status;
1779 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001780 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001781 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 retval = -EFAULT;
1783 b--;
1784 break;
1785 }
1786 nr--;
1787 break;
1788 }
1789 /* This statement must be first before checking for input
1790 so that any interrupt will set the state back to
1791 TASK_RUNNING. */
1792 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1795 ((minimum - (b - buf)) >= 1))
1796 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (!input_available_p(tty, 0)) {
1799 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1800 retval = -EIO;
1801 break;
1802 }
1803 if (tty_hung_up_p(file))
1804 break;
1805 if (!timeout)
1806 break;
1807 if (file->f_flags & O_NONBLOCK) {
1808 retval = -EAGAIN;
1809 break;
1810 }
1811 if (signal_pending(current)) {
1812 retval = -ERESTARTSYS;
1813 break;
1814 }
Linus Torvalds55db4c62011-06-04 06:33:24 +09001815 /* FIXME: does n_tty_set_room need locking ? */
1816 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 continue;
1819 }
1820 __set_current_state(TASK_RUNNING);
1821
1822 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001823 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001824 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 retval = -EFAULT;
1826 b--;
1827 break;
1828 }
1829 nr--;
1830 }
1831
hyc@symas.com26df6d12010-06-22 10:14:49 -07001832 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* N.B. avoid overrun if nr == 0 */
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001834 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 while (nr && tty->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001836 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 eol = test_and_clear_bit(tty->read_tail,
1839 tty->read_flags);
1840 c = tty->read_buf[tty->read_tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 tty->read_tail = ((tty->read_tail+1) &
1842 (N_TTY_BUF_SIZE-1));
1843 tty->read_cnt--;
1844 if (eol) {
1845 /* this test should be redundant:
1846 * we shouldn't be reading data if
1847 * canon_data is 0
1848 */
1849 if (--tty->canon_data < 0)
1850 tty->canon_data = 0;
1851 }
1852 spin_unlock_irqrestore(&tty->read_lock, flags);
1853
1854 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001855 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 retval = -EFAULT;
1857 b--;
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001858 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860 }
1861 nr--;
1862 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001863 if (eol) {
1864 tty_audit_push(tty);
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001865 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001867 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001868 spin_lock_irqsave(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Stanislav Kozina00aaae02012-08-09 14:48:58 +01001870 spin_unlock_irqrestore(&tty->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (retval)
1872 break;
1873 } else {
1874 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001875 /* The copy function takes the read lock and handles
1876 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 uncopied = copy_from_read_buf(tty, &b, &nr);
1878 uncopied += copy_from_read_buf(tty, &b, &nr);
1879 if (uncopied) {
1880 retval = -EFAULT;
1881 break;
1882 }
1883 }
1884
1885 /* If there is enough space in the read buffer now, let the
1886 * low-level driver know. We use n_tty_chars_in_buffer() to
1887 * check the buffer, as it now knows about canonical mode.
1888 * Otherwise, if the driver is throttled and the line is
1889 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1890 * we won't get any more characters.
1891 */
Linus Torvalds55db4c62011-06-04 06:33:24 +09001892 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1893 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 check_unthrottle(tty);
Linus Torvalds55db4c62011-06-04 06:33:24 +09001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 if (b - buf >= minimum)
1898 break;
1899 if (time)
1900 timeout = time;
1901 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001902 mutex_unlock(&tty->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 remove_wait_queue(&tty->read_wait, &wait);
1904
1905 if (!waitqueue_active(&tty->read_wait))
1906 tty->minimum_to_wake = minimum;
1907
1908 __set_current_state(TASK_RUNNING);
1909 size = b - buf;
1910 if (size) {
1911 retval = size;
1912 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001913 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
Thorsten Wißmannbbd20752011-12-08 17:47:33 +01001915 goto do_it_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Linus Torvalds55db4c62011-06-04 06:33:24 +09001917 n_tty_set_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return retval;
1919}
1920
1921/**
Alan Cox11a96d12008-10-13 10:46:24 +01001922 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * @tty: tty device
1924 * @file: file object
1925 * @buf: userspace buffer pointer
1926 * @nr: size of I/O
1927 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001928 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001930 * and other such events. Since the receive code will echo characters,
1931 * thus calling driver write methods, the output_lock is used in
1932 * the output processing functions called here as well as in the
1933 * echo processing function to protect the column state and space
1934 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *
1936 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001937 *
1938 * Locking: output_lock to protect column state and space left
1939 * (note that the process_output*() functions take this
1940 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Alan Cox4edf1822008-02-08 04:18:44 -08001942
Alan Cox11a96d12008-10-13 10:46:24 +01001943static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00001944 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 const unsigned char *b = buf;
1947 DECLARE_WAITQUEUE(wait, current);
1948 int c;
1949 ssize_t retval = 0;
1950
1951 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1952 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1953 retval = tty_check_change(tty);
1954 if (retval)
1955 return retval;
1956 }
1957
Joe Petersona88a69c2009-01-02 13:40:53 +00001958 /* Write out any echoed characters that are still pending */
1959 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 add_wait_queue(&tty->write_wait, &wait);
1962 while (1) {
1963 set_current_state(TASK_INTERRUPTIBLE);
1964 if (signal_pending(current)) {
1965 retval = -ERESTARTSYS;
1966 break;
1967 }
1968 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1969 retval = -EIO;
1970 break;
1971 }
1972 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1973 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001974 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (num < 0) {
1976 if (num == -EAGAIN)
1977 break;
1978 retval = num;
1979 goto break_out;
1980 }
1981 b += num;
1982 nr -= num;
1983 if (nr == 0)
1984 break;
1985 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00001986 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
1988 b++; nr--;
1989 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001990 if (tty->ops->flush_chars)
1991 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07001993 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001994 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07001995 if (c < 0) {
1996 retval = c;
1997 goto break_out;
1998 }
1999 if (!c)
2000 break;
2001 b += c;
2002 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 if (!nr)
2006 break;
2007 if (file->f_flags & O_NONBLOCK) {
2008 retval = -EAGAIN;
2009 break;
2010 }
2011 schedule();
2012 }
2013break_out:
2014 __set_current_state(TASK_RUNNING);
2015 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00002016 if (b - buf != nr && tty->fasync)
2017 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return (b - buf) ? b - buf : retval;
2019}
2020
2021/**
Alan Cox11a96d12008-10-13 10:46:24 +01002022 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * @tty: terminal device
2024 * @file: file accessing it
2025 * @wait: poll table
2026 *
2027 * Called when the line discipline is asked to poll() for data or
2028 * for special events. This code is not serialized with respect to
2029 * other events save open/close.
2030 *
2031 * This code must be sure never to sleep through a hangup.
2032 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
Alan Cox4edf1822008-02-08 04:18:44 -08002034
Alan Cox11a96d12008-10-13 10:46:24 +01002035static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08002036 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 unsigned int mask = 0;
2039
2040 poll_wait(file, &tty->read_wait, wait);
2041 poll_wait(file, &tty->write_wait, wait);
2042 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2043 mask |= POLLIN | POLLRDNORM;
2044 if (tty->packet && tty->link->ctrl_status)
2045 mask |= POLLPRI | POLLIN | POLLRDNORM;
2046 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2047 mask |= POLLHUP;
2048 if (tty_hung_up_p(file))
2049 mask |= POLLHUP;
2050 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2051 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2052 tty->minimum_to_wake = MIN_CHAR(tty);
2053 else
2054 tty->minimum_to_wake = 1;
2055 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002056 if (tty->ops->write && !tty_is_writelocked(tty) &&
2057 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2058 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 mask |= POLLOUT | POLLWRNORM;
2060 return mask;
2061}
2062
Alan Cox47afa7a2008-10-13 10:44:17 +01002063static unsigned long inq_canon(struct tty_struct *tty)
2064{
2065 int nr, head, tail;
2066
Alan Cox17b82062008-10-13 10:45:06 +01002067 if (!tty->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002068 return 0;
2069 head = tty->canon_head;
2070 tail = tty->read_tail;
2071 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2072 /* Skip EOF-chars.. */
2073 while (head != tail) {
2074 if (test_bit(tail, tty->read_flags) &&
2075 tty->read_buf[tail] == __DISABLED_CHAR)
2076 nr--;
2077 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2078 }
2079 return nr;
2080}
2081
2082static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2083 unsigned int cmd, unsigned long arg)
2084{
2085 int retval;
2086
2087 switch (cmd) {
2088 case TIOCOUTQ:
2089 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2090 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002091 /* FIXME: Locking */
Alan Cox47afa7a2008-10-13 10:44:17 +01002092 retval = tty->read_cnt;
2093 if (L_ICANON(tty))
2094 retval = inq_canon(tty);
2095 return put_user(retval, (unsigned int __user *) arg);
2096 default:
2097 return n_tty_ioctl_helper(tty, file, cmd, arg);
2098 }
2099}
2100
Alan Coxa352def2008-07-16 21:53:12 +01002101struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002102 .magic = TTY_LDISC_MAGIC,
2103 .name = "n_tty",
2104 .open = n_tty_open,
2105 .close = n_tty_close,
2106 .flush_buffer = n_tty_flush_buffer,
2107 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002108 .read = n_tty_read,
2109 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002110 .ioctl = n_tty_ioctl,
2111 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002112 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002113 .receive_buf = n_tty_receive_buf,
2114 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002116
2117/**
2118 * n_tty_inherit_ops - inherit N_TTY methods
2119 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2120 *
2121 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2122 * methods.
2123 */
2124
2125void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2126{
2127 *ops = tty_ldisc_N_TTY;
2128 ops->owner = NULL;
2129 ops->refcount = ops->flags = 0;
2130}
2131EXPORT_SYMBOL_GPL(n_tty_inherit_ops);