blob: 95d0a9c2dd13e3e625d8b287d911d327cebe2ee4 [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#include <asm/system.h>
54
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64#define TTY_THRESHOLD_UNTHROTTLE 128
65
Joe Petersona88a69c2009-01-02 13:40:53 +000066/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
Miloslav Trmac522ed772007-07-15 23:40:56 -070077static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
78 unsigned char __user *ptr)
79{
80 tty_audit_add_data(tty, &x, 1);
81 return put_user(x, ptr);
82}
83
Alan Cox33f0f882006-01-09 20:54:13 -080084static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 if (tty->read_cnt < N_TTY_BUF_SIZE) {
87 tty->read_buf[tty->read_head] = c;
88 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
89 tty->read_cnt++;
90 }
91}
92
Alan Cox17b82062008-10-13 10:45:06 +010093/**
94 * put_tty_queue - add character to tty
95 * @c: character
96 * @tty: tty device
97 *
98 * Add a character to the tty read_buf queue. This is done under the
99 * read_lock to serialize character addition and also to protect us
100 * against parallel reads or flushes
101 */
102
Alan Cox33f0f882006-01-09 20:54:13 -0800103static void put_tty_queue(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned long flags;
106 /*
107 * The problem of stomping on the buffers ends here.
108 * Why didn't anyone see this one coming? --AJK
109 */
110 spin_lock_irqsave(&tty->read_lock, flags);
111 put_tty_queue_nolock(c, tty);
112 spin_unlock_irqrestore(&tty->read_lock, flags);
113}
114
115/**
116 * check_unthrottle - allow new receive data
117 * @tty; tty device
118 *
Alan Cox17b82062008-10-13 10:45:06 +0100119 * Check whether to call the driver unthrottle functions
120 *
Ingo Molnar70522e12006-03-23 03:00:31 -0800121 * Can sleep, may be called under the atomic_read_lock mutex but
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * this is not guaranteed.
123 */
Alan Cox4edf1822008-02-08 04:18:44 -0800124static void check_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Alan Cox39c2e602008-04-30 00:54:18 -0700126 if (tty->count)
127 tty_unthrottle(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/**
131 * reset_buffer_flags - reset buffer state
132 * @tty: terminal to reset
133 *
Alan Cox4edf1822008-02-08 04:18:44 -0800134 * Reset the read buffer counters, clear the flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * and make sure the driver is unthrottled. Called
136 * from n_tty_open() and n_tty_flush_buffer().
Alan Cox17b82062008-10-13 10:45:06 +0100137 *
138 * Locking: tty_read_lock for read fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static void reset_buffer_flags(struct tty_struct *tty)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&tty->read_lock, flags);
146 tty->read_head = tty->read_tail = tty->read_cnt = 0;
147 spin_unlock_irqrestore(&tty->read_lock, flags);
Joe Petersona88a69c2009-01-02 13:40:53 +0000148
149 mutex_lock(&tty->echo_lock);
150 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
151 mutex_unlock(&tty->echo_lock);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 tty->canon_head = tty->canon_data = tty->erasing = 0;
154 memset(&tty->read_flags, 0, sizeof tty->read_flags);
155 check_unthrottle(tty);
156}
157
158/**
159 * n_tty_flush_buffer - clean input queue
160 * @tty: terminal device
161 *
162 * Flush the input buffer. Called when the line discipline is
163 * being closed, when the tty layer wants the buffer flushed (eg
164 * at hangup) or when the N_TTY line discipline internally has to
165 * clean the pending queue (for example some signals).
166 *
Alan Cox17b82062008-10-13 10:45:06 +0100167 * Locking: ctrl_lock, read_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Alan Cox4edf1822008-02-08 04:18:44 -0800169
170static void n_tty_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Alan Cox04f378b2008-04-30 00:53:29 -0700172 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* clear everything and unthrottle the driver */
174 reset_buffer_flags(tty);
Alan Cox4edf1822008-02-08 04:18:44 -0800175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!tty->link)
177 return;
178
Alan Cox04f378b2008-04-30 00:53:29 -0700179 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (tty->link->packet) {
181 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
182 wake_up_interruptible(&tty->link->read_wait);
183 }
Alan Cox04f378b2008-04-30 00:53:29 -0700184 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/**
188 * n_tty_chars_in_buffer - report available bytes
189 * @tty: tty device
190 *
191 * Report the number of characters buffered to be delivered to user
Alan Cox4edf1822008-02-08 04:18:44 -0800192 * at this instant in time.
Alan Cox17b82062008-10-13 10:45:06 +0100193 *
194 * Locking: read_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Alan Cox4edf1822008-02-08 04:18:44 -0800196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
198{
199 unsigned long flags;
200 ssize_t n = 0;
201
202 spin_lock_irqsave(&tty->read_lock, flags);
203 if (!tty->icanon) {
204 n = tty->read_cnt;
205 } else if (tty->canon_data) {
206 n = (tty->canon_head > tty->read_tail) ?
207 tty->canon_head - tty->read_tail :
208 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
209 }
210 spin_unlock_irqrestore(&tty->read_lock, flags);
211 return n;
212}
213
214/**
215 * is_utf8_continuation - utf8 multibyte check
216 * @c: byte to check
217 *
218 * Returns true if the utf8 character 'c' is a multibyte continuation
219 * character. We use this to correctly compute the on screen size
220 * of the character when printing
221 */
Alan Cox4edf1822008-02-08 04:18:44 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static inline int is_utf8_continuation(unsigned char c)
224{
225 return (c & 0xc0) == 0x80;
226}
227
228/**
229 * is_continuation - multibyte check
230 * @c: byte to check
231 *
232 * Returns true if the utf8 character 'c' is a multibyte continuation
233 * character and the terminal is in unicode mode.
234 */
Alan Cox4edf1822008-02-08 04:18:44 -0800235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static inline int is_continuation(unsigned char c, struct tty_struct *tty)
237{
238 return I_IUTF8(tty) && is_utf8_continuation(c);
239}
240
241/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000242 * do_output_char - output one character
243 * @c: character (or partial unicode symbol)
244 * @tty: terminal device
245 * @space: space available in tty driver write buffer
246 *
247 * This is a helper function that handles one output character
248 * (including special characters like TAB, CR, LF, etc.),
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600249 * doing OPOST processing and putting the results in the
250 * tty driver's write buffer.
Joe Petersona88a69c2009-01-02 13:40:53 +0000251 *
252 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
253 * and NLDLY. They simply aren't relevant in the world today.
254 * If you ever need them, add them here.
255 *
256 * Returns the number of bytes of buffer space used or -1 if
257 * no space left.
258 *
259 * Locking: should be called under the output_lock to protect
260 * the column state and space left in the buffer
261 */
262
263static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
264{
265 int spaces;
266
267 if (!space)
268 return -1;
Alan Cox300a6202009-01-02 13:41:04 +0000269
Joe Petersona88a69c2009-01-02 13:40:53 +0000270 switch (c) {
271 case '\n':
272 if (O_ONLRET(tty))
273 tty->column = 0;
274 if (O_ONLCR(tty)) {
275 if (space < 2)
276 return -1;
277 tty->canon_column = tty->column = 0;
Linus Torvalds37f81fa2009-09-05 12:46:07 -0700278 tty->ops->write(tty, "\r\n", 2);
Joe Petersona88a69c2009-01-02 13:40:53 +0000279 return 2;
280 }
281 tty->canon_column = tty->column;
282 break;
283 case '\r':
284 if (O_ONOCR(tty) && tty->column == 0)
285 return 0;
286 if (O_OCRNL(tty)) {
287 c = '\n';
288 if (O_ONLRET(tty))
289 tty->canon_column = tty->column = 0;
290 break;
291 }
292 tty->canon_column = tty->column = 0;
293 break;
294 case '\t':
295 spaces = 8 - (tty->column & 7);
296 if (O_TABDLY(tty) == XTABS) {
297 if (space < spaces)
298 return -1;
299 tty->column += spaces;
300 tty->ops->write(tty, " ", spaces);
301 return spaces;
302 }
303 tty->column += spaces;
304 break;
305 case '\b':
306 if (tty->column > 0)
307 tty->column--;
308 break;
309 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000310 if (!iscntrl(c)) {
311 if (O_OLCUC(tty))
312 c = toupper(c);
313 if (!is_continuation(c, tty))
314 tty->column++;
315 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000316 break;
317 }
318
319 tty_put_char(tty, c);
320 return 1;
321}
322
323/**
324 * process_output - output post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * @c: character (or partial unicode symbol)
326 * @tty: terminal device
327 *
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600328 * Output one character with OPOST processing.
329 * Returns -1 when the output device is full and the character
330 * must be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000332 * Locking: output_lock to protect column state and space left
333 * (also, this is called from n_tty_write under the
334 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Alan Cox4edf1822008-02-08 04:18:44 -0800336
Joe Petersona88a69c2009-01-02 13:40:53 +0000337static int process_output(unsigned char c, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Joe Petersona88a69c2009-01-02 13:40:53 +0000339 int space, retval;
340
341 mutex_lock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Alan Coxf34d7a52008-04-30 00:54:13 -0700343 space = tty_write_room(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +0000344 retval = do_output_char(c, tty, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Joe Petersona88a69c2009-01-02 13:40:53 +0000346 mutex_unlock(&tty->output_lock);
347 if (retval < 0)
348 return -1;
349 else
350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000354 * process_output_block - block post processor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * @tty: terminal device
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600356 * @buf: character buffer
357 * @nr: number of bytes to output
358 *
359 * Output a block of characters with OPOST processing.
360 * Returns the number of characters output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * This path is used to speed up block console writes, among other
363 * things when processing blocks of output data. It handles only
364 * the simple cases normally found and helps to generate blocks of
365 * symbols for the console driver and thus improve performance.
366 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000367 * Locking: output_lock to protect column state and space left
368 * (also, this is called from n_tty_write under the
369 * tty layer write lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Alan Cox4edf1822008-02-08 04:18:44 -0800371
Joe Petersona88a69c2009-01-02 13:40:53 +0000372static ssize_t process_output_block(struct tty_struct *tty,
373 const unsigned char *buf, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int space;
376 int i;
377 const unsigned char *cp;
378
Joe Petersona88a69c2009-01-02 13:40:53 +0000379 mutex_lock(&tty->output_lock);
380
Alan Coxf34d7a52008-04-30 00:54:13 -0700381 space = tty_write_room(tty);
Alan Cox300a6202009-01-02 13:41:04 +0000382 if (!space) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000383 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +0000385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (nr > space)
387 nr = space;
388
389 for (i = 0, cp = buf; i < nr; i++, cp++) {
Joe Petersona59c0d62009-01-02 13:43:25 +0000390 unsigned char c = *cp;
391
392 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 case '\n':
394 if (O_ONLRET(tty))
395 tty->column = 0;
396 if (O_ONLCR(tty))
397 goto break_out;
398 tty->canon_column = tty->column;
399 break;
400 case '\r':
401 if (O_ONOCR(tty) && tty->column == 0)
402 goto break_out;
403 if (O_OCRNL(tty))
404 goto break_out;
405 tty->canon_column = tty->column = 0;
406 break;
407 case '\t':
408 goto break_out;
409 case '\b':
410 if (tty->column > 0)
411 tty->column--;
412 break;
413 default:
Joe Petersona59c0d62009-01-02 13:43:25 +0000414 if (!iscntrl(c)) {
415 if (O_OLCUC(tty))
416 goto break_out;
417 if (!is_continuation(c, tty))
418 tty->column++;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421 }
422 }
423break_out:
Alan Coxf34d7a52008-04-30 00:54:13 -0700424 i = tty->ops->write(tty, buf, i);
Joe Petersona88a69c2009-01-02 13:40:53 +0000425
426 mutex_unlock(&tty->output_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return i;
428}
429
Joe Petersona88a69c2009-01-02 13:40:53 +0000430/**
431 * process_echoes - write pending echo characters
432 * @tty: terminal device
433 *
434 * Write previously buffered echo (and other ldisc-generated)
435 * characters to the tty.
436 *
437 * Characters generated by the ldisc (including echoes) need to
438 * be buffered because the driver's write buffer can fill during
439 * heavy program output. Echoing straight to the driver will
440 * often fail under these conditions, causing lost characters and
441 * resulting mismatches of ldisc state information.
442 *
443 * Since the ldisc state must represent the characters actually sent
444 * to the driver at the time of the write, operations like certain
445 * changes in column state are also saved in the buffer and executed
446 * here.
447 *
448 * A circular fifo buffer is used so that the most recent characters
449 * are prioritized. Also, when control characters are echoed with a
450 * prefixed "^", the pair is treated atomically and thus not separated.
451 *
452 * Locking: output_lock to protect column state and space left,
453 * echo_lock to protect the echo buffer
454 */
455
456static void process_echoes(struct tty_struct *tty)
457{
458 int space, nr;
459 unsigned char c;
460 unsigned char *cp, *buf_end;
461
462 if (!tty->echo_cnt)
463 return;
464
465 mutex_lock(&tty->output_lock);
466 mutex_lock(&tty->echo_lock);
467
468 space = tty_write_room(tty);
469
470 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
471 cp = tty->echo_buf + tty->echo_pos;
472 nr = tty->echo_cnt;
473 while (nr > 0) {
474 c = *cp;
475 if (c == ECHO_OP_START) {
476 unsigned char op;
477 unsigned char *opp;
478 int no_space_left = 0;
479
480 /*
481 * If the buffer byte is the start of a multi-byte
482 * operation, get the next byte, which is either the
483 * op code or a control character value.
484 */
485 opp = cp + 1;
486 if (opp == buf_end)
487 opp -= N_TTY_BUF_SIZE;
488 op = *opp;
Alan Cox300a6202009-01-02 13:41:04 +0000489
Joe Petersona88a69c2009-01-02 13:40:53 +0000490 switch (op) {
491 unsigned int num_chars, num_bs;
492
493 case ECHO_OP_ERASE_TAB:
494 if (++opp == buf_end)
495 opp -= N_TTY_BUF_SIZE;
496 num_chars = *opp;
497
498 /*
499 * Determine how many columns to go back
500 * in order to erase the tab.
501 * This depends on the number of columns
502 * used by other characters within the tab
503 * area. If this (modulo 8) count is from
504 * the start of input rather than from a
505 * previous tab, we offset by canon column.
506 * Otherwise, tab spacing is normal.
507 */
508 if (!(num_chars & 0x80))
509 num_chars += tty->canon_column;
510 num_bs = 8 - (num_chars & 7);
511
512 if (num_bs > space) {
513 no_space_left = 1;
514 break;
515 }
516 space -= num_bs;
517 while (num_bs--) {
518 tty_put_char(tty, '\b');
519 if (tty->column > 0)
520 tty->column--;
521 }
522 cp += 3;
523 nr -= 3;
524 break;
525
526 case ECHO_OP_SET_CANON_COL:
527 tty->canon_column = tty->column;
528 cp += 2;
529 nr -= 2;
530 break;
531
532 case ECHO_OP_MOVE_BACK_COL:
533 if (tty->column > 0)
534 tty->column--;
535 cp += 2;
536 nr -= 2;
537 break;
538
539 case ECHO_OP_START:
540 /* This is an escaped echo op start code */
541 if (!space) {
542 no_space_left = 1;
543 break;
544 }
545 tty_put_char(tty, ECHO_OP_START);
546 tty->column++;
547 space--;
548 cp += 2;
549 nr -= 2;
550 break;
551
552 default:
Joe Petersona88a69c2009-01-02 13:40:53 +0000553 /*
Joe Peterson62b26352009-09-09 15:03:47 -0600554 * If the op is not a special byte code,
555 * it is a ctrl char tagged to be echoed
556 * as "^X" (where X is the letter
557 * representing the control char).
558 * Note that we must ensure there is
559 * enough space for the whole ctrl pair.
560 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000561 */
Joe Peterson62b26352009-09-09 15:03:47 -0600562 if (space < 2) {
563 no_space_left = 1;
564 break;
565 }
566 tty_put_char(tty, '^');
567 tty_put_char(tty, op ^ 0100);
568 tty->column += 2;
569 space -= 2;
Joe Petersona88a69c2009-01-02 13:40:53 +0000570 cp += 2;
571 nr -= 2;
572 }
573
574 if (no_space_left)
575 break;
576 } else {
Joe Petersonee5aa7b2009-09-09 15:03:13 -0600577 if (O_OPOST(tty) &&
578 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
579 int retval = do_output_char(c, tty, space);
580 if (retval < 0)
581 break;
582 space -= retval;
583 } else {
584 if (!space)
585 break;
586 tty_put_char(tty, c);
587 space -= 1;
588 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000589 cp += 1;
590 nr -= 1;
591 }
592
593 /* When end of circular buffer reached, wrap around */
594 if (cp >= buf_end)
595 cp -= N_TTY_BUF_SIZE;
596 }
597
598 if (nr == 0) {
599 tty->echo_pos = 0;
600 tty->echo_cnt = 0;
601 tty->echo_overrun = 0;
602 } else {
603 int num_processed = tty->echo_cnt - nr;
604 tty->echo_pos += num_processed;
605 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
606 tty->echo_cnt = nr;
607 if (num_processed > 0)
608 tty->echo_overrun = 0;
609 }
610
611 mutex_unlock(&tty->echo_lock);
612 mutex_unlock(&tty->output_lock);
613
614 if (tty->ops->flush_chars)
615 tty->ops->flush_chars(tty);
616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000619 * add_echo_byte - add a byte to the echo buffer
620 * @c: unicode byte to echo
621 * @tty: terminal device
622 *
623 * Add a character or operation byte to the echo buffer.
624 *
625 * Should be called under the echo lock to protect the echo buffer.
626 */
627
628static void add_echo_byte(unsigned char c, struct tty_struct *tty)
629{
630 int new_byte_pos;
631
632 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
633 /* Circular buffer is already at capacity */
634 new_byte_pos = tty->echo_pos;
635
636 /*
637 * Since the buffer start position needs to be advanced,
638 * be sure to step by a whole operation byte group.
639 */
Alan Cox300a6202009-01-02 13:41:04 +0000640 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000641 if (tty->echo_buf[(tty->echo_pos + 1) &
642 (N_TTY_BUF_SIZE - 1)] ==
643 ECHO_OP_ERASE_TAB) {
644 tty->echo_pos += 3;
645 tty->echo_cnt -= 2;
646 } else {
647 tty->echo_pos += 2;
648 tty->echo_cnt -= 1;
649 }
650 } else {
651 tty->echo_pos++;
652 }
653 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
654
655 tty->echo_overrun = 1;
656 } else {
657 new_byte_pos = tty->echo_pos + tty->echo_cnt;
658 new_byte_pos &= N_TTY_BUF_SIZE - 1;
659 tty->echo_cnt++;
660 }
661
662 tty->echo_buf[new_byte_pos] = c;
663}
664
665/**
666 * echo_move_back_col - add operation to move back a column
667 * @tty: terminal device
668 *
669 * Add an operation to the echo buffer to move back one column.
670 *
671 * Locking: echo_lock to protect the echo buffer
672 */
673
674static void echo_move_back_col(struct tty_struct *tty)
675{
676 mutex_lock(&tty->echo_lock);
677
678 add_echo_byte(ECHO_OP_START, tty);
679 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
680
681 mutex_unlock(&tty->echo_lock);
682}
683
684/**
685 * echo_set_canon_col - add operation to set the canon column
686 * @tty: terminal device
687 *
688 * Add an operation to the echo buffer to set the canon column
689 * to the current column.
690 *
691 * Locking: echo_lock to protect the echo buffer
692 */
693
694static void echo_set_canon_col(struct tty_struct *tty)
695{
696 mutex_lock(&tty->echo_lock);
697
698 add_echo_byte(ECHO_OP_START, tty);
699 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
700
701 mutex_unlock(&tty->echo_lock);
702}
703
704/**
705 * echo_erase_tab - add operation to erase a tab
706 * @num_chars: number of character columns already used
707 * @after_tab: true if num_chars starts after a previous tab
708 * @tty: terminal device
709 *
710 * Add an operation to the echo buffer to erase a tab.
711 *
712 * Called by the eraser function, which knows how many character
713 * columns have been used since either a previous tab or the start
714 * of input. This information will be used later, along with
715 * canon column (if applicable), to go back the correct number
716 * of columns.
717 *
718 * Locking: echo_lock to protect the echo buffer
719 */
720
721static void echo_erase_tab(unsigned int num_chars, int after_tab,
722 struct tty_struct *tty)
723{
724 mutex_lock(&tty->echo_lock);
725
726 add_echo_byte(ECHO_OP_START, tty);
727 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
728
729 /* We only need to know this modulo 8 (tab spacing) */
730 num_chars &= 7;
731
732 /* Set the high bit as a flag if num_chars is after a previous tab */
733 if (after_tab)
734 num_chars |= 0x80;
Alan Cox300a6202009-01-02 13:41:04 +0000735
Joe Petersona88a69c2009-01-02 13:40:53 +0000736 add_echo_byte(num_chars, tty);
737
738 mutex_unlock(&tty->echo_lock);
739}
740
741/**
742 * echo_char_raw - echo a character raw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * @c: unicode byte to echo
744 * @tty: terminal device
745 *
Alan Cox4edf1822008-02-08 04:18:44 -0800746 * Echo user input back onto the screen. This must be called only when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * L_ECHO(tty) is true. Called from the driver receive_buf path.
Alan Cox17b82062008-10-13 10:45:06 +0100748 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000749 * This variant does not treat control characters specially.
750 *
751 * Locking: echo_lock to protect the echo buffer
752 */
753
754static void echo_char_raw(unsigned char c, struct tty_struct *tty)
755{
756 mutex_lock(&tty->echo_lock);
757
758 if (c == ECHO_OP_START) {
759 add_echo_byte(ECHO_OP_START, tty);
760 add_echo_byte(ECHO_OP_START, tty);
761 } else {
762 add_echo_byte(c, tty);
763 }
764
765 mutex_unlock(&tty->echo_lock);
766}
767
768/**
769 * echo_char - echo a character
770 * @c: unicode byte to echo
771 * @tty: terminal device
772 *
773 * Echo user input back onto the screen. This must be called only when
774 * L_ECHO(tty) is true. Called from the driver receive_buf path.
775 *
Joe Peterson62b26352009-09-09 15:03:47 -0600776 * This variant tags control characters to be echoed as "^X"
777 * (where X is the letter representing the control char).
Joe Petersona88a69c2009-01-02 13:40:53 +0000778 *
779 * Locking: echo_lock to protect the echo buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781
782static void echo_char(unsigned char c, struct tty_struct *tty)
783{
Joe Petersona88a69c2009-01-02 13:40:53 +0000784 mutex_lock(&tty->echo_lock);
785
786 if (c == ECHO_OP_START) {
787 add_echo_byte(ECHO_OP_START, tty);
788 add_echo_byte(ECHO_OP_START, tty);
789 } else {
Joe Peterson62b26352009-09-09 15:03:47 -0600790 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
Joe Petersona88a69c2009-01-02 13:40:53 +0000791 add_echo_byte(ECHO_OP_START, tty);
792 add_echo_byte(c, tty);
793 }
794
795 mutex_unlock(&tty->echo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Alan Cox17b82062008-10-13 10:45:06 +0100798/**
Joe Petersona88a69c2009-01-02 13:40:53 +0000799 * finish_erasing - complete erase
Alan Cox17b82062008-10-13 10:45:06 +0100800 * @tty: tty doing the erase
Alan Cox17b82062008-10-13 10:45:06 +0100801 */
Joe Petersona88a69c2009-01-02 13:40:53 +0000802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static inline void finish_erasing(struct tty_struct *tty)
804{
805 if (tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000806 echo_char_raw('/', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 tty->erasing = 0;
808 }
809}
810
811/**
812 * eraser - handle erase function
813 * @c: character input
814 * @tty: terminal device
815 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200816 * Perform erase and necessary output when an erase character is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * present in the stream from the driver layer. Handles the complexities
818 * of UTF-8 multibyte symbols.
Alan Cox17b82062008-10-13 10:45:06 +0100819 *
Joe Petersona88a69c2009-01-02 13:40:53 +0000820 * Locking: read_lock for tty buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Alan Cox4edf1822008-02-08 04:18:44 -0800822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static void eraser(unsigned char c, struct tty_struct *tty)
824{
825 enum { ERASE, WERASE, KILL } kill_type;
826 int head, seen_alnums, cnt;
827 unsigned long flags;
828
Alan Cox17b82062008-10-13 10:45:06 +0100829 /* FIXME: locking needed ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (tty->read_head == tty->canon_head) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +0000831 /* process_output('\a', tty); */ /* what do you think? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833 }
834 if (c == ERASE_CHAR(tty))
835 kill_type = ERASE;
836 else if (c == WERASE_CHAR(tty))
837 kill_type = WERASE;
838 else {
839 if (!L_ECHO(tty)) {
840 spin_lock_irqsave(&tty->read_lock, flags);
841 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
842 (N_TTY_BUF_SIZE - 1));
843 tty->read_head = tty->canon_head;
844 spin_unlock_irqrestore(&tty->read_lock, flags);
845 return;
846 }
847 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
848 spin_lock_irqsave(&tty->read_lock, flags);
849 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
850 (N_TTY_BUF_SIZE - 1));
851 tty->read_head = tty->canon_head;
852 spin_unlock_irqrestore(&tty->read_lock, flags);
853 finish_erasing(tty);
854 echo_char(KILL_CHAR(tty), tty);
855 /* Add a newline if ECHOK is on and ECHOKE is off. */
856 if (L_ECHOK(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000857 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
859 }
860 kill_type = KILL;
861 }
862
863 seen_alnums = 0;
Alan Cox17b82062008-10-13 10:45:06 +0100864 /* FIXME: Locking ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 while (tty->read_head != tty->canon_head) {
866 head = tty->read_head;
867
868 /* erase a single possibly multibyte character */
869 do {
870 head = (head - 1) & (N_TTY_BUF_SIZE-1);
871 c = tty->read_buf[head];
872 } while (is_continuation(c, tty) && head != tty->canon_head);
873
874 /* do not partially erase */
875 if (is_continuation(c, tty))
876 break;
877
878 if (kill_type == WERASE) {
879 /* Equivalent to BSD's ALTWERASE. */
880 if (isalnum(c) || c == '_')
881 seen_alnums++;
882 else if (seen_alnums)
883 break;
884 }
885 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
886 spin_lock_irqsave(&tty->read_lock, flags);
887 tty->read_head = head;
888 tty->read_cnt -= cnt;
889 spin_unlock_irqrestore(&tty->read_lock, flags);
890 if (L_ECHO(tty)) {
891 if (L_ECHOPRT(tty)) {
892 if (!tty->erasing) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000893 echo_char_raw('\\', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 tty->erasing = 1;
895 }
896 /* if cnt > 1, output a multi-byte character */
897 echo_char(c, tty);
898 while (--cnt > 0) {
899 head = (head+1) & (N_TTY_BUF_SIZE-1);
Joe Petersona88a69c2009-01-02 13:40:53 +0000900 echo_char_raw(tty->read_buf[head], tty);
901 echo_move_back_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
904 echo_char(ERASE_CHAR(tty), tty);
905 } else if (c == '\t') {
Joe Petersona88a69c2009-01-02 13:40:53 +0000906 unsigned int num_chars = 0;
907 int after_tab = 0;
908 unsigned long tail = tty->read_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Joe Petersona88a69c2009-01-02 13:40:53 +0000910 /*
911 * Count the columns used for characters
912 * since the start of input or after a
913 * previous tab.
914 * This info is used to go back the correct
915 * number of columns.
916 */
917 while (tail != tty->canon_head) {
918 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 c = tty->read_buf[tail];
Joe Petersona88a69c2009-01-02 13:40:53 +0000920 if (c == '\t') {
921 after_tab = 1;
922 break;
Alan Cox300a6202009-01-02 13:41:04 +0000923 } else if (iscntrl(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (L_ECHOCTL(tty))
Joe Petersona88a69c2009-01-02 13:40:53 +0000925 num_chars += 2;
926 } else if (!is_continuation(c, tty)) {
927 num_chars++;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000930 echo_erase_tab(num_chars, after_tab, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 } else {
932 if (iscntrl(c) && L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000933 echo_char_raw('\b', tty);
934 echo_char_raw(' ', tty);
935 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 if (!iscntrl(c) || L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +0000938 echo_char_raw('\b', tty);
939 echo_char_raw(' ', tty);
940 echo_char_raw('\b', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 }
943 }
944 if (kill_type == ERASE)
945 break;
946 }
Joe Petersona88a69c2009-01-02 13:40:53 +0000947 if (tty->read_head == tty->canon_head && L_ECHO(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 finish_erasing(tty);
949}
950
951/**
952 * isig - handle the ISIG optio
953 * @sig: signal
954 * @tty: terminal
955 * @flush: force flush
956 *
957 * Called when a signal is being sent due to terminal input. This
958 * may caus terminal flushing to take place according to the termios
959 * settings and character used. Called from the driver receive_buf
960 * path so serialized.
Alan Cox17b82062008-10-13 10:45:06 +0100961 *
962 * Locking: ctrl_lock, read_lock (both via flush buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
Alan Cox4edf1822008-02-08 04:18:44 -0800964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static inline void isig(int sig, struct tty_struct *tty, int flush)
966{
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800967 if (tty->pgrp)
968 kill_pgrp(tty->pgrp, sig, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (flush || !L_NOFLSH(tty)) {
970 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -0700971 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973}
974
975/**
976 * n_tty_receive_break - handle break
977 * @tty: terminal
978 *
979 * An RS232 break event has been hit in the incoming bitstream. This
980 * can cause a variety of events depending upon the termios settings.
981 *
982 * Called from the receive_buf path so single threaded.
983 */
Alan Cox4edf1822008-02-08 04:18:44 -0800984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985static inline void n_tty_receive_break(struct tty_struct *tty)
986{
987 if (I_IGNBRK(tty))
988 return;
989 if (I_BRKINT(tty)) {
990 isig(SIGINT, tty, 1);
991 return;
992 }
993 if (I_PARMRK(tty)) {
994 put_tty_queue('\377', tty);
995 put_tty_queue('\0', tty);
996 }
997 put_tty_queue('\0', tty);
998 wake_up_interruptible(&tty->read_wait);
999}
1000
1001/**
1002 * n_tty_receive_overrun - handle overrun reporting
1003 * @tty: terminal
1004 *
1005 * Data arrived faster than we could process it. While the tty
1006 * driver has flagged this the bits that were missed are gone
1007 * forever.
1008 *
1009 * Called from the receive_buf path so single threaded. Does not
1010 * need locking as num_overrun and overrun_time are function
1011 * private.
1012 */
Alan Cox4edf1822008-02-08 04:18:44 -08001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static inline void n_tty_receive_overrun(struct tty_struct *tty)
1015{
1016 char buf[64];
1017
1018 tty->num_overrun++;
1019 if (time_before(tty->overrun_time, jiffies - HZ) ||
1020 time_after(tty->overrun_time, jiffies)) {
1021 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1022 tty_name(tty, buf),
1023 tty->num_overrun);
1024 tty->overrun_time = jiffies;
1025 tty->num_overrun = 0;
1026 }
1027}
1028
1029/**
1030 * n_tty_receive_parity_error - error notifier
1031 * @tty: terminal device
1032 * @c: character
1033 *
1034 * Process a parity error and queue the right data to indicate
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001035 * the error case if necessary. Locking as per n_tty_receive_buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1038 unsigned char c)
1039{
Alan Cox4edf1822008-02-08 04:18:44 -08001040 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (I_PARMRK(tty)) {
1043 put_tty_queue('\377', tty);
1044 put_tty_queue('\0', tty);
1045 put_tty_queue(c, tty);
1046 } else if (I_INPCK(tty))
1047 put_tty_queue('\0', tty);
1048 else
1049 put_tty_queue(c, tty);
1050 wake_up_interruptible(&tty->read_wait);
1051}
1052
1053/**
1054 * n_tty_receive_char - perform processing
1055 * @tty: terminal device
1056 * @c: character
1057 *
1058 * Process an individual character of input received from the driver.
Alan Cox4edf1822008-02-08 04:18:44 -08001059 * This is serialized with respect to itself by the rules for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * driver above.
1061 */
1062
1063static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1064{
1065 unsigned long flags;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001066 int parmrk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (tty->raw) {
1069 put_tty_queue(c, tty);
1070 return;
1071 }
Alan Cox4edf1822008-02-08 04:18:44 -08001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (I_ISTRIP(tty))
1074 c &= 0x7f;
1075 if (I_IUCLC(tty) && L_IEXTEN(tty))
Alan Cox300a6202009-01-02 13:41:04 +00001076 c = tolower(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
hyc@symas.com26df6d12010-06-22 10:14:49 -07001078 if (L_EXTPROC(tty)) {
1079 put_tty_queue(c, tty);
1080 return;
1081 }
1082
Joe Peterson54d2a372008-02-06 01:37:59 -08001083 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
Joe Petersona88a69c2009-01-02 13:40:53 +00001084 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1085 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
Joe Peterson54d2a372008-02-06 01:37:59 -08001086 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001087 process_echoes(tty);
1088 }
Joe Peterson54d2a372008-02-06 01:37:59 -08001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (tty->closing) {
1091 if (I_IXON(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001092 if (c == START_CHAR(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001094 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001095 } else if (c == STOP_CHAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 stop_tty(tty);
1097 }
1098 return;
1099 }
1100
1101 /*
1102 * If the previous character was LNEXT, or we know that this
1103 * character is not one of the characters that we'll have to
1104 * handle specially, do shortcut processing to speed things
1105 * up.
1106 */
1107 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 tty->lnext = 0;
Joe Petersonacc71bb2009-01-02 13:43:32 +00001109 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1110 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1111 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001112 if (L_ECHO(tty))
1113 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001114 return;
1115 }
1116 if (L_ECHO(tty)) {
1117 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* Record the column of first canon char. */
1119 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001120 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001122 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Joe Petersonacc71bb2009-01-02 13:43:32 +00001124 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 put_tty_queue(c, tty);
1126 put_tty_queue(c, tty);
1127 return;
1128 }
Alan Cox4edf1822008-02-08 04:18:44 -08001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (I_IXON(tty)) {
1131 if (c == START_CHAR(tty)) {
1132 start_tty(tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001133 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
1135 }
1136 if (c == STOP_CHAR(tty)) {
1137 stop_tty(tty);
1138 return;
1139 }
1140 }
Joe Peterson575537b2008-04-30 00:53:30 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (L_ISIG(tty)) {
1143 int signal;
1144 signal = SIGINT;
1145 if (c == INTR_CHAR(tty))
1146 goto send_signal;
1147 signal = SIGQUIT;
1148 if (c == QUIT_CHAR(tty))
1149 goto send_signal;
1150 signal = SIGTSTP;
1151 if (c == SUSP_CHAR(tty)) {
1152send_signal:
Joe Petersonec5b1152008-02-06 01:37:38 -08001153 /*
Joe Petersonec5b1152008-02-06 01:37:38 -08001154 * Note that we do not use isig() here because we want
1155 * the order to be:
1156 * 1) flush, 2) echo, 3) signal
1157 */
1158 if (!L_NOFLSH(tty)) {
1159 n_tty_flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001160 tty_driver_flush_buffer(tty);
Joe Petersonec5b1152008-02-06 01:37:38 -08001161 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001162 if (I_IXON(tty))
1163 start_tty(tty);
1164 if (L_ECHO(tty)) {
Joe Petersonec5b1152008-02-06 01:37:38 -08001165 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001166 process_echoes(tty);
1167 }
Joe Petersonec5b1152008-02-06 01:37:38 -08001168 if (tty->pgrp)
1169 kill_pgrp(tty->pgrp, signal, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return;
1171 }
1172 }
Joe Peterson575537b2008-04-30 00:53:30 -07001173
1174 if (c == '\r') {
1175 if (I_IGNCR(tty))
1176 return;
1177 if (I_ICRNL(tty))
1178 c = '\n';
1179 } else if (c == '\n' && I_INLCR(tty))
1180 c = '\r';
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (tty->icanon) {
1183 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1184 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1185 eraser(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001186 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
1188 }
1189 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1190 tty->lnext = 1;
1191 if (L_ECHO(tty)) {
1192 finish_erasing(tty);
1193 if (L_ECHOCTL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001194 echo_char_raw('^', tty);
1195 echo_char_raw('\b', tty);
1196 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 }
1199 return;
1200 }
1201 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1202 L_IEXTEN(tty)) {
1203 unsigned long tail = tty->canon_head;
1204
1205 finish_erasing(tty);
1206 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001207 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 while (tail != tty->read_head) {
1209 echo_char(tty->read_buf[tail], tty);
1210 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1211 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001212 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return;
1214 }
1215 if (c == '\n') {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001216 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001217 if (L_ECHO(tty))
1218 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001219 return;
1220 }
1221 if (L_ECHO(tty) || L_ECHONL(tty)) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001222 echo_char_raw('\n', tty);
1223 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 goto handle_newline;
1226 }
1227 if (c == EOF_CHAR(tty)) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001228 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1229 return;
Alan Cox4edf1822008-02-08 04:18:44 -08001230 if (tty->canon_head != tty->read_head)
1231 set_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 c = __DISABLED_CHAR;
1233 goto handle_newline;
1234 }
1235 if ((c == EOL_CHAR(tty)) ||
1236 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
Joe Petersonacc71bb2009-01-02 13:43:32 +00001237 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1238 ? 1 : 0;
1239 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001240 if (L_ECHO(tty))
1241 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001242 return;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /*
1245 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1246 */
1247 if (L_ECHO(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /* Record the column of first canon char. */
1249 if (tty->canon_head == tty->read_head)
Joe Petersona88a69c2009-01-02 13:40:53 +00001250 echo_set_canon_col(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 echo_char(c, tty);
Joe Petersona88a69c2009-01-02 13:40:53 +00001252 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 /*
1255 * XXX does PARMRK doubling happen for
1256 * EOL_CHAR and EOL2_CHAR?
1257 */
Joe Petersonacc71bb2009-01-02 13:43:32 +00001258 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 put_tty_queue(c, tty);
1260
Alan Cox4edf1822008-02-08 04:18:44 -08001261handle_newline:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 spin_lock_irqsave(&tty->read_lock, flags);
1263 set_bit(tty->read_head, tty->read_flags);
1264 put_tty_queue_nolock(c, tty);
1265 tty->canon_head = tty->read_head;
1266 tty->canon_data++;
1267 spin_unlock_irqrestore(&tty->read_lock, flags);
1268 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1269 if (waitqueue_active(&tty->read_wait))
1270 wake_up_interruptible(&tty->read_wait);
1271 return;
1272 }
1273 }
Alan Cox4edf1822008-02-08 04:18:44 -08001274
Joe Petersonacc71bb2009-01-02 13:43:32 +00001275 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1276 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1277 /* beep if no space */
Joe Peterson7e94b1d2009-01-02 13:43:40 +00001278 if (L_ECHO(tty))
1279 process_output('\a', tty);
Joe Petersonacc71bb2009-01-02 13:43:32 +00001280 return;
1281 }
1282 if (L_ECHO(tty)) {
1283 finish_erasing(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (c == '\n')
Joe Petersona88a69c2009-01-02 13:40:53 +00001285 echo_char_raw('\n', tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 else {
1287 /* 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);
1291 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001292 process_echoes(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Joe Petersonacc71bb2009-01-02 13:43:32 +00001295 if (parmrk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 put_tty_queue(c, tty);
1297
1298 put_tty_queue(c, tty);
Alan Cox4edf1822008-02-08 04:18:44 -08001299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302/**
1303 * n_tty_write_wakeup - asynchronous I/O notifier
1304 * @tty: tty device
1305 *
1306 * Required for the ptys, serial driver etc. since processes
1307 * that attach themselves to the master and rely on ASYNC
1308 * IO must be woken up
1309 */
1310
1311static void n_tty_write_wakeup(struct tty_struct *tty)
1312{
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001313 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317/**
1318 * n_tty_receive_buf - data receive
1319 * @tty: terminal device
1320 * @cp: buffer
1321 * @fp: flag buffer
1322 * @count: characters
1323 *
1324 * Called by the terminal driver when a block of characters has
1325 * been received. This function must be called from soft contexts
1326 * not from interrupt context. The driver is responsible for making
1327 * calls one at a time and in order (or using flush_to_ldisc)
1328 */
Alan Cox4edf1822008-02-08 04:18:44 -08001329
Felipe Balbib1c43f82011-03-21 12:25:08 +02001330static unsigned int n_tty_receive_buf(struct tty_struct *tty,
1331 const unsigned char *cp, char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 const unsigned char *p;
1334 char *f, flags = TTY_NORMAL;
1335 int i;
1336 char buf[64];
1337 unsigned long cpuflags;
Felipe Balbib1c43f82011-03-21 12:25:08 +02001338 int left;
1339 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (!tty->read_buf)
Felipe Balbib1c43f82011-03-21 12:25:08 +02001342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (tty->real_raw) {
1345 spin_lock_irqsave(&tty->read_lock, cpuflags);
1346 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1347 N_TTY_BUF_SIZE - tty->read_head);
1348 i = min(count, i);
1349 memcpy(tty->read_buf + tty->read_head, cp, i);
1350 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1351 tty->read_cnt += i;
Felipe Balbib1c43f82011-03-21 12:25:08 +02001352 ret += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 cp += i;
1354 count -= i;
1355
1356 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1357 N_TTY_BUF_SIZE - tty->read_head);
1358 i = min(count, i);
1359 memcpy(tty->read_buf + tty->read_head, cp, i);
1360 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1361 tty->read_cnt += i;
Felipe Balbib1c43f82011-03-21 12:25:08 +02001362 ret += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1364 } else {
Felipe Balbib1c43f82011-03-21 12:25:08 +02001365 ret = count;
Alan Cox4edf1822008-02-08 04:18:44 -08001366 for (i = count, p = cp, f = fp; i; i--, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (f)
1368 flags = *f++;
1369 switch (flags) {
1370 case TTY_NORMAL:
1371 n_tty_receive_char(tty, *p);
1372 break;
1373 case TTY_BREAK:
1374 n_tty_receive_break(tty);
1375 break;
1376 case TTY_PARITY:
1377 case TTY_FRAME:
1378 n_tty_receive_parity_error(tty, *p);
1379 break;
1380 case TTY_OVERRUN:
1381 n_tty_receive_overrun(tty);
1382 break;
1383 default:
Alan Cox4edf1822008-02-08 04:18:44 -08001384 printk(KERN_ERR "%s: unknown flag %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 tty_name(tty, buf), flags);
1386 break;
1387 }
1388 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001389 if (tty->ops->flush_chars)
1390 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
hyc@symas.com26df6d12010-06-22 10:14:49 -07001393 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1394 L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1396 if (waitqueue_active(&tty->read_wait))
1397 wake_up_interruptible(&tty->read_wait);
1398 }
1399
1400 /*
1401 * Check the remaining room for the input canonicalization
1402 * mode. We don't want to throttle the driver if we're in
1403 * canonical mode and don't have a newline yet!
1404 */
Felipe Balbib1c43f82011-03-21 12:25:08 +02001405 left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
1406
1407 if (left < TTY_THRESHOLD_THROTTLE)
Alan Cox39c2e602008-04-30 00:54:18 -07001408 tty_throttle(tty);
Felipe Balbib1c43f82011-03-21 12:25:08 +02001409
1410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413int is_ignored(int sig)
1414{
1415 return (sigismember(&current->blocked, sig) ||
Alan Cox4edf1822008-02-08 04:18:44 -08001416 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/**
1420 * n_tty_set_termios - termios data changed
1421 * @tty: terminal
1422 * @old: previous data
1423 *
1424 * Called by the tty layer when the user changes termios flags so
1425 * that the line discipline can plan ahead. This function cannot sleep
Alan Cox4edf1822008-02-08 04:18:44 -08001426 * and is protected from re-entry by the tty layer. The user is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * guaranteed that this function will not be re-entered or in progress
1428 * when the ldisc is closed.
Alan Cox17b82062008-10-13 10:45:06 +01001429 *
1430 * Locking: Caller holds tty->termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Alan Cox4edf1822008-02-08 04:18:44 -08001432
1433static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Alan Cox47afa7a2008-10-13 10:44:17 +01001435 int canon_change = 1;
1436 BUG_ON(!tty);
1437
1438 if (old)
1439 canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
1440 if (canon_change) {
1441 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1442 tty->canon_head = tty->read_tail;
1443 tty->canon_data = 0;
1444 tty->erasing = 0;
1445 }
1446
1447 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1448 wake_up_interruptible(&tty->read_wait);
Alan Cox4edf1822008-02-08 04:18:44 -08001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 tty->icanon = (L_ICANON(tty) != 0);
1451 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1452 tty->raw = 1;
1453 tty->real_raw = 1;
1454 return;
1455 }
1456 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1457 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1458 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1459 I_PARMRK(tty)) {
1460 memset(tty->process_char_map, 0, 256/8);
1461
1462 if (I_IGNCR(tty) || I_ICRNL(tty))
1463 set_bit('\r', tty->process_char_map);
1464 if (I_INLCR(tty))
1465 set_bit('\n', tty->process_char_map);
1466
1467 if (L_ICANON(tty)) {
1468 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1469 set_bit(KILL_CHAR(tty), tty->process_char_map);
1470 set_bit(EOF_CHAR(tty), tty->process_char_map);
1471 set_bit('\n', tty->process_char_map);
1472 set_bit(EOL_CHAR(tty), tty->process_char_map);
1473 if (L_IEXTEN(tty)) {
1474 set_bit(WERASE_CHAR(tty),
1475 tty->process_char_map);
1476 set_bit(LNEXT_CHAR(tty),
1477 tty->process_char_map);
1478 set_bit(EOL2_CHAR(tty),
1479 tty->process_char_map);
1480 if (L_ECHO(tty))
1481 set_bit(REPRINT_CHAR(tty),
1482 tty->process_char_map);
1483 }
1484 }
1485 if (I_IXON(tty)) {
1486 set_bit(START_CHAR(tty), tty->process_char_map);
1487 set_bit(STOP_CHAR(tty), tty->process_char_map);
1488 }
1489 if (L_ISIG(tty)) {
1490 set_bit(INTR_CHAR(tty), tty->process_char_map);
1491 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1492 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1493 }
1494 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1495 tty->raw = 0;
1496 tty->real_raw = 0;
1497 } else {
1498 tty->raw = 1;
1499 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1500 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1501 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1502 tty->real_raw = 1;
1503 else
1504 tty->real_raw = 0;
1505 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001506 /* The termios change make the tty ready for I/O */
1507 wake_up_interruptible(&tty->write_wait);
1508 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511/**
1512 * n_tty_close - close the ldisc for this tty
1513 * @tty: device
1514 *
Alan Cox4edf1822008-02-08 04:18:44 -08001515 * Called from the terminal layer when this line discipline is
1516 * being shut down, either because of a close or becsuse of a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 * discipline change. The function will not be called while other
1518 * ldisc methods are in progress.
1519 */
Alan Cox4edf1822008-02-08 04:18:44 -08001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521static void n_tty_close(struct tty_struct *tty)
1522{
1523 n_tty_flush_buffer(tty);
1524 if (tty->read_buf) {
Alan Cox0b4068a2009-06-11 13:05:49 +01001525 kfree(tty->read_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 tty->read_buf = NULL;
1527 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001528 if (tty->echo_buf) {
Alan Cox0b4068a2009-06-11 13:05:49 +01001529 kfree(tty->echo_buf);
Joe Petersona88a69c2009-01-02 13:40:53 +00001530 tty->echo_buf = NULL;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/**
1535 * n_tty_open - open an ldisc
1536 * @tty: terminal to open
1537 *
Alan Cox4edf1822008-02-08 04:18:44 -08001538 * Called when this line discipline is being attached to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * terminal device. Can sleep. Called serialized so that no
1540 * other events will occur in parallel. No further open will occur
1541 * until a close.
1542 */
1543
1544static int n_tty_open(struct tty_struct *tty)
1545{
1546 if (!tty)
1547 return -EINVAL;
1548
Joe Petersona88a69c2009-01-02 13:40:53 +00001549 /* These are ugly. Currently a malloc failure here can panic */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (!tty->read_buf) {
Alan Cox0b4068a2009-06-11 13:05:49 +01001551 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (!tty->read_buf)
1553 return -ENOMEM;
1554 }
Joe Petersona88a69c2009-01-02 13:40:53 +00001555 if (!tty->echo_buf) {
Alan Cox0b4068a2009-06-11 13:05:49 +01001556 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1557
Joe Petersona88a69c2009-01-02 13:40:53 +00001558 if (!tty->echo_buf)
1559 return -ENOMEM;
1560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 reset_buffer_flags(tty);
1562 tty->column = 0;
1563 n_tty_set_termios(tty, NULL);
1564 tty->minimum_to_wake = 1;
1565 tty->closing = 0;
1566 return 0;
1567}
1568
1569static inline int input_available_p(struct tty_struct *tty, int amt)
1570{
OGAWA Hirofumie043e422009-07-29 12:15:56 -07001571 tty_flush_to_ldisc(tty);
hyc@symas.com26df6d12010-06-22 10:14:49 -07001572 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (tty->canon_data)
1574 return 1;
1575 } else if (tty->read_cnt >= (amt ? amt : 1))
1576 return 1;
1577
1578 return 0;
1579}
1580
1581/**
1582 * copy_from_read_buf - copy read data directly
1583 * @tty: terminal device
1584 * @b: user data
1585 * @nr: size of data
1586 *
Alan Cox11a96d12008-10-13 10:46:24 +01001587 * Helper function to speed up n_tty_read. It is only called when
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * ICANON is off; it copies characters straight from the tty queue to
1589 * user space directly. It can be profitably called twice; once to
1590 * drain the space from the tail pointer to the (physical) end of the
1591 * buffer, and once to drain the space from the (physical) beginning of
1592 * the buffer to head pointer.
1593 *
Paul Fulghum817d6d32006-06-28 04:26:47 -07001594 * Called under the tty->atomic_read_lock sem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
1596 */
Alan Cox4edf1822008-02-08 04:18:44 -08001597
Alan Cox33f0f882006-01-09 20:54:13 -08001598static int copy_from_read_buf(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned char __user **b,
1600 size_t *nr)
1601
1602{
1603 int retval;
1604 size_t n;
1605 unsigned long flags;
1606
1607 retval = 0;
1608 spin_lock_irqsave(&tty->read_lock, flags);
1609 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1610 n = min(*nr, n);
1611 spin_unlock_irqrestore(&tty->read_lock, flags);
1612 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1614 n -= retval;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001615 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 spin_lock_irqsave(&tty->read_lock, flags);
1617 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1618 tty->read_cnt -= n;
hyc@symas.com26df6d12010-06-22 10:14:49 -07001619 /* Turn single EOF into zero-length read */
1620 if (L_EXTPROC(tty) && tty->icanon && n == 1) {
1621 if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
1622 n--;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 spin_unlock_irqrestore(&tty->read_lock, flags);
1625 *b += n;
1626 *nr -= n;
1627 }
1628 return retval;
1629}
1630
Al Virocc4191d2008-03-29 03:08:48 +00001631extern ssize_t redirected_tty_write(struct file *, const char __user *,
Alan Cox4edf1822008-02-08 04:18:44 -08001632 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634/**
1635 * job_control - check job control
1636 * @tty: tty
1637 * @file: file handle
1638 *
1639 * Perform job control management checks on this file/tty descriptor
Alan Cox4edf1822008-02-08 04:18:44 -08001640 * and if appropriate send any needed signals and return a negative
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * error code if action should be taken.
Alan Cox04f378b2008-04-30 00:53:29 -07001642 *
1643 * FIXME:
1644 * Locking: None - redirected write test is safe, testing
1645 * current->signal should possibly lock current->sighand
1646 * pgrp locking ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
Alan Cox4edf1822008-02-08 04:18:44 -08001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static int job_control(struct tty_struct *tty, struct file *file)
1650{
1651 /* Job control check -- must be done at start and after
1652 every sleep (POSIX.1 7.1.1.4). */
1653 /* NOTE: not yet done after every sleep pending a thorough
1654 check of the logic of this change. -- jlc */
1655 /* don't stop on /dev/console */
1656 if (file->f_op->write != redirected_tty_write &&
1657 current->signal->tty == tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001658 if (!tty->pgrp)
Alan Cox11a96d12008-10-13 10:46:24 +01001659 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001660 else if (task_pgrp(current) != tty->pgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (is_ignored(SIGTTIN) ||
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001662 is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return -EIO;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001664 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
Oleg Nesterov040b6362007-06-01 00:46:53 -07001665 set_thread_flag(TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -ERESTARTSYS;
1667 }
1668 }
1669 return 0;
1670}
Alan Cox4edf1822008-02-08 04:18:44 -08001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673/**
Alan Cox11a96d12008-10-13 10:46:24 +01001674 * n_tty_read - read function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * @tty: tty device
1676 * @file: file object
1677 * @buf: userspace buffer pointer
1678 * @nr: size of I/O
1679 *
1680 * Perform reads for the line discipline. We are guaranteed that the
1681 * line discipline will not be closed under us but we may get multiple
1682 * parallel readers and must handle this ourselves. We may also get
1683 * a hangup. Always called in user context, may sleep.
1684 *
1685 * This code must be sure never to sleep through a hangup.
1686 */
Alan Cox4edf1822008-02-08 04:18:44 -08001687
Alan Cox11a96d12008-10-13 10:46:24 +01001688static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 unsigned char __user *buf, size_t nr)
1690{
1691 unsigned char __user *b = buf;
1692 DECLARE_WAITQUEUE(wait, current);
1693 int c;
1694 int minimum, time;
1695 ssize_t retval = 0;
1696 ssize_t size;
1697 long timeout;
1698 unsigned long flags;
Alan Cox04f378b2008-04-30 00:53:29 -07001699 int packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701do_it_again:
1702
Alan Cox17b82062008-10-13 10:45:06 +01001703 BUG_ON(!tty->read_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 c = job_control(tty, file);
Alan Cox4edf1822008-02-08 04:18:44 -08001706 if (c < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return c;
Alan Cox4edf1822008-02-08 04:18:44 -08001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 minimum = time = 0;
1710 timeout = MAX_SCHEDULE_TIMEOUT;
1711 if (!tty->icanon) {
1712 time = (HZ / 10) * TIME_CHAR(tty);
1713 minimum = MIN_CHAR(tty);
1714 if (minimum) {
1715 if (time)
1716 tty->minimum_to_wake = 1;
1717 else if (!waitqueue_active(&tty->read_wait) ||
1718 (tty->minimum_to_wake > minimum))
1719 tty->minimum_to_wake = minimum;
1720 } else {
1721 timeout = 0;
1722 if (time) {
1723 timeout = time;
1724 time = 0;
1725 }
1726 tty->minimum_to_wake = minimum = 1;
1727 }
1728 }
1729
1730 /*
1731 * Internal serialization of reads.
1732 */
1733 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001734 if (!mutex_trylock(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return -EAGAIN;
Alan Cox4edf1822008-02-08 04:18:44 -08001736 } else {
Ingo Molnar70522e12006-03-23 03:00:31 -08001737 if (mutex_lock_interruptible(&tty->atomic_read_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return -ERESTARTSYS;
1739 }
Alan Cox04f378b2008-04-30 00:53:29 -07001740 packet = tty->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 add_wait_queue(&tty->read_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 while (nr) {
1744 /* First test for status change. */
Alan Cox04f378b2008-04-30 00:53:29 -07001745 if (packet && tty->link->ctrl_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 unsigned char cs;
1747 if (b != buf)
1748 break;
Alan Cox04f378b2008-04-30 00:53:29 -07001749 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 cs = tty->link->ctrl_status;
1751 tty->link->ctrl_status = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07001752 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
Miloslav Trmac522ed772007-07-15 23:40:56 -07001753 if (tty_put_user(tty, cs, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 retval = -EFAULT;
1755 b--;
1756 break;
1757 }
1758 nr--;
1759 break;
1760 }
1761 /* This statement must be first before checking for input
1762 so that any interrupt will set the state back to
1763 TASK_RUNNING. */
1764 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox4edf1822008-02-08 04:18:44 -08001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1767 ((minimum - (b - buf)) >= 1))
1768 tty->minimum_to_wake = (minimum - (b - buf));
Alan Cox4edf1822008-02-08 04:18:44 -08001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!input_available_p(tty, 0)) {
1771 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1772 retval = -EIO;
1773 break;
1774 }
1775 if (tty_hung_up_p(file))
1776 break;
1777 if (!timeout)
1778 break;
1779 if (file->f_flags & O_NONBLOCK) {
1780 retval = -EAGAIN;
1781 break;
1782 }
1783 if (signal_pending(current)) {
1784 retval = -ERESTARTSYS;
1785 break;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 continue;
1789 }
1790 __set_current_state(TASK_RUNNING);
1791
1792 /* Deal with packet mode. */
Alan Cox04f378b2008-04-30 00:53:29 -07001793 if (packet && b == buf) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001794 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 retval = -EFAULT;
1796 b--;
1797 break;
1798 }
1799 nr--;
1800 }
1801
hyc@symas.com26df6d12010-06-22 10:14:49 -07001802 if (tty->icanon && !L_EXTPROC(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /* N.B. avoid overrun if nr == 0 */
1804 while (nr && tty->read_cnt) {
Alan Cox4edf1822008-02-08 04:18:44 -08001805 int eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 eol = test_and_clear_bit(tty->read_tail,
1808 tty->read_flags);
1809 c = tty->read_buf[tty->read_tail];
1810 spin_lock_irqsave(&tty->read_lock, flags);
1811 tty->read_tail = ((tty->read_tail+1) &
1812 (N_TTY_BUF_SIZE-1));
1813 tty->read_cnt--;
1814 if (eol) {
1815 /* this test should be redundant:
1816 * we shouldn't be reading data if
1817 * canon_data is 0
1818 */
1819 if (--tty->canon_data < 0)
1820 tty->canon_data = 0;
1821 }
1822 spin_unlock_irqrestore(&tty->read_lock, flags);
1823
1824 if (!eol || (c != __DISABLED_CHAR)) {
Miloslav Trmac522ed772007-07-15 23:40:56 -07001825 if (tty_put_user(tty, c, b++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 retval = -EFAULT;
1827 b--;
1828 break;
1829 }
1830 nr--;
1831 }
Miloslav Trmac522ed772007-07-15 23:40:56 -07001832 if (eol) {
1833 tty_audit_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
Miloslav Trmac522ed772007-07-15 23:40:56 -07001835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837 if (retval)
1838 break;
1839 } else {
1840 int uncopied;
Alan Cox04f378b2008-04-30 00:53:29 -07001841 /* The copy function takes the read lock and handles
1842 locking internally for this case */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 uncopied = copy_from_read_buf(tty, &b, &nr);
1844 uncopied += copy_from_read_buf(tty, &b, &nr);
1845 if (uncopied) {
1846 retval = -EFAULT;
1847 break;
1848 }
1849 }
1850
1851 /* If there is enough space in the read buffer now, let the
1852 * low-level driver know. We use n_tty_chars_in_buffer() to
1853 * check the buffer, as it now knows about canonical mode.
1854 * Otherwise, if the driver is throttled and the line is
1855 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1856 * we won't get any more characters.
1857 */
Felipe Balbib1c43f82011-03-21 12:25:08 +02001858 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 check_unthrottle(tty);
1860
1861 if (b - buf >= minimum)
1862 break;
1863 if (time)
1864 timeout = time;
1865 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001866 mutex_unlock(&tty->atomic_read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 remove_wait_queue(&tty->read_wait, &wait);
1868
1869 if (!waitqueue_active(&tty->read_wait))
1870 tty->minimum_to_wake = minimum;
1871
1872 __set_current_state(TASK_RUNNING);
1873 size = b - buf;
1874 if (size) {
1875 retval = size;
1876 if (nr)
Alan Cox4edf1822008-02-08 04:18:44 -08001877 clear_bit(TTY_PUSH, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1879 goto do_it_again;
1880
1881 return retval;
1882}
1883
1884/**
Alan Cox11a96d12008-10-13 10:46:24 +01001885 * n_tty_write - write function for tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 * @tty: tty device
1887 * @file: file object
1888 * @buf: userspace buffer pointer
1889 * @nr: size of I/O
1890 *
Joe Petersona88a69c2009-01-02 13:40:53 +00001891 * Write function of the terminal device. This is serialized with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * respect to other write callers but not to termios changes, reads
Joe Petersona88a69c2009-01-02 13:40:53 +00001893 * and other such events. Since the receive code will echo characters,
1894 * thus calling driver write methods, the output_lock is used in
1895 * the output processing functions called here as well as in the
1896 * echo processing function to protect the column state and space
1897 * left in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 *
1899 * This code must be sure never to sleep through a hangup.
Joe Petersona88a69c2009-01-02 13:40:53 +00001900 *
1901 * Locking: output_lock to protect column state and space left
1902 * (note that the process_output*() functions take this
1903 * lock themselves)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
Alan Cox4edf1822008-02-08 04:18:44 -08001905
Alan Cox11a96d12008-10-13 10:46:24 +01001906static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
Joe Petersona88a69c2009-01-02 13:40:53 +00001907 const unsigned char *buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 const unsigned char *b = buf;
1910 DECLARE_WAITQUEUE(wait, current);
1911 int c;
1912 ssize_t retval = 0;
1913
1914 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1915 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1916 retval = tty_check_change(tty);
1917 if (retval)
1918 return retval;
1919 }
1920
Joe Petersona88a69c2009-01-02 13:40:53 +00001921 /* Write out any echoed characters that are still pending */
1922 process_echoes(tty);
Alan Cox300a6202009-01-02 13:41:04 +00001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 add_wait_queue(&tty->write_wait, &wait);
1925 while (1) {
1926 set_current_state(TASK_INTERRUPTIBLE);
1927 if (signal_pending(current)) {
1928 retval = -ERESTARTSYS;
1929 break;
1930 }
1931 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1932 retval = -EIO;
1933 break;
1934 }
1935 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1936 while (nr > 0) {
Joe Petersona88a69c2009-01-02 13:40:53 +00001937 ssize_t num = process_output_block(tty, b, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (num < 0) {
1939 if (num == -EAGAIN)
1940 break;
1941 retval = num;
1942 goto break_out;
1943 }
1944 b += num;
1945 nr -= num;
1946 if (nr == 0)
1947 break;
1948 c = *b;
Joe Petersona88a69c2009-01-02 13:40:53 +00001949 if (process_output(c, tty) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951 b++; nr--;
1952 }
Alan Coxf34d7a52008-04-30 00:54:13 -07001953 if (tty->ops->flush_chars)
1954 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 } else {
Roman Zippeld6afe272005-07-07 17:56:55 -07001956 while (nr > 0) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001957 c = tty->ops->write(tty, b, nr);
Roman Zippeld6afe272005-07-07 17:56:55 -07001958 if (c < 0) {
1959 retval = c;
1960 goto break_out;
1961 }
1962 if (!c)
1963 break;
1964 b += c;
1965 nr -= c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 if (!nr)
1969 break;
1970 if (file->f_flags & O_NONBLOCK) {
1971 retval = -EAGAIN;
1972 break;
1973 }
1974 schedule();
1975 }
1976break_out:
1977 __set_current_state(TASK_RUNNING);
1978 remove_wait_queue(&tty->write_wait, &wait);
Thomas Pfaffff8cb0f2009-01-02 13:47:13 +00001979 if (b - buf != nr && tty->fasync)
1980 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return (b - buf) ? b - buf : retval;
1982}
1983
1984/**
Alan Cox11a96d12008-10-13 10:46:24 +01001985 * n_tty_poll - poll method for N_TTY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 * @tty: terminal device
1987 * @file: file accessing it
1988 * @wait: poll table
1989 *
1990 * Called when the line discipline is asked to poll() for data or
1991 * for special events. This code is not serialized with respect to
1992 * other events save open/close.
1993 *
1994 * This code must be sure never to sleep through a hangup.
1995 * Called without the kernel lock held - fine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Alan Cox4edf1822008-02-08 04:18:44 -08001997
Alan Cox11a96d12008-10-13 10:46:24 +01001998static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
Alan Cox4edf1822008-02-08 04:18:44 -08001999 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 unsigned int mask = 0;
2002
2003 poll_wait(file, &tty->read_wait, wait);
2004 poll_wait(file, &tty->write_wait, wait);
2005 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2006 mask |= POLLIN | POLLRDNORM;
2007 if (tty->packet && tty->link->ctrl_status)
2008 mask |= POLLPRI | POLLIN | POLLRDNORM;
2009 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2010 mask |= POLLHUP;
2011 if (tty_hung_up_p(file))
2012 mask |= POLLHUP;
2013 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2014 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2015 tty->minimum_to_wake = MIN_CHAR(tty);
2016 else
2017 tty->minimum_to_wake = 1;
2018 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002019 if (tty->ops->write && !tty_is_writelocked(tty) &&
2020 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2021 tty_write_room(tty) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 mask |= POLLOUT | POLLWRNORM;
2023 return mask;
2024}
2025
Alan Cox47afa7a2008-10-13 10:44:17 +01002026static unsigned long inq_canon(struct tty_struct *tty)
2027{
2028 int nr, head, tail;
2029
Alan Cox17b82062008-10-13 10:45:06 +01002030 if (!tty->canon_data)
Alan Cox47afa7a2008-10-13 10:44:17 +01002031 return 0;
2032 head = tty->canon_head;
2033 tail = tty->read_tail;
2034 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2035 /* Skip EOF-chars.. */
2036 while (head != tail) {
2037 if (test_bit(tail, tty->read_flags) &&
2038 tty->read_buf[tail] == __DISABLED_CHAR)
2039 nr--;
2040 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2041 }
2042 return nr;
2043}
2044
2045static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2046 unsigned int cmd, unsigned long arg)
2047{
2048 int retval;
2049
2050 switch (cmd) {
2051 case TIOCOUTQ:
2052 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2053 case TIOCINQ:
Alan Cox17b82062008-10-13 10:45:06 +01002054 /* FIXME: Locking */
Alan Cox47afa7a2008-10-13 10:44:17 +01002055 retval = tty->read_cnt;
2056 if (L_ICANON(tty))
2057 retval = inq_canon(tty);
2058 return put_user(retval, (unsigned int __user *) arg);
2059 default:
2060 return n_tty_ioctl_helper(tty, file, cmd, arg);
2061 }
2062}
2063
Alan Coxa352def2008-07-16 21:53:12 +01002064struct tty_ldisc_ops tty_ldisc_N_TTY = {
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002065 .magic = TTY_LDISC_MAGIC,
2066 .name = "n_tty",
2067 .open = n_tty_open,
2068 .close = n_tty_close,
2069 .flush_buffer = n_tty_flush_buffer,
2070 .chars_in_buffer = n_tty_chars_in_buffer,
Alan Cox11a96d12008-10-13 10:46:24 +01002071 .read = n_tty_read,
2072 .write = n_tty_write,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002073 .ioctl = n_tty_ioctl,
2074 .set_termios = n_tty_set_termios,
Alan Cox11a96d12008-10-13 10:46:24 +01002075 .poll = n_tty_poll,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002076 .receive_buf = n_tty_receive_buf,
2077 .write_wakeup = n_tty_write_wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
Rodolfo Giometti572b9ad2010-03-10 15:23:46 -08002079
2080/**
2081 * n_tty_inherit_ops - inherit N_TTY methods
2082 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2083 *
2084 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2085 * methods.
2086 */
2087
2088void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2089{
2090 *ops = tty_ldisc_N_TTY;
2091 ops->owner = NULL;
2092 ops->refcount = ops->flags = 0;
2093}
2094EXPORT_SYMBOL_GPL(n_tty_inherit_ops);