tty: ifx6x60: Remove unused suspend/resume callbacks
[deliverable/linux.git] / drivers / tty / n_tty.c
1 /*
2 * n_tty.c --- implements the N_TTY line discipline.
3 *
4 * 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
11 * to N_TTY if it can not switch to any other line discipline.
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
14 *
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 *
18 * 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 *
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * 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>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
30 * 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>
48 #include <linux/audit.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
53
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
66 /*
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
77 struct n_tty_data {
78 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
84
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
87
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
96
97 int canon_data;
98 unsigned long canon_head;
99 unsigned int canon_column;
100
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
104 raw_spinlock_t read_lock;
105 };
106
107 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
109 {
110 struct n_tty_data *ldata = tty->disc_data;
111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
113 return put_user(x, ptr);
114 }
115
116 /**
117 * n_tty_set__room - receive space
118 * @tty: terminal
119 *
120 * Called by the driver to find out how much data it is
121 * permitted to feed to the line discipline without any being lost
122 * and thus to manage flow control. Not serialized. Answers for the
123 * "instant".
124 */
125
126 static void n_tty_set_room(struct tty_struct *tty)
127 {
128 struct n_tty_data *ldata = tty->disc_data;
129 int left;
130 int old_left;
131
132 /* ldata->read_cnt is not read locked ? */
133 if (I_PARMRK(tty)) {
134 /* Multiply read_cnt by 3, since each byte might take up to
135 * three times as many spaces when PARMRK is set (depending on
136 * its flags, e.g. parity error). */
137 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
138 } else
139 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
140
141 /*
142 * If we are doing input canonicalization, and there are no
143 * pending newlines, let characters through without limit, so
144 * that erase characters will be handled. Other excess
145 * characters will be beeped.
146 */
147 if (left <= 0)
148 left = ldata->icanon && !ldata->canon_data;
149 old_left = tty->receive_room;
150 tty->receive_room = left;
151
152 /* Did this open up the receive buffer? We may need to flip */
153 if (left && !old_left) {
154 WARN_RATELIMIT(tty->port->itty == NULL,
155 "scheduling with invalid itty\n");
156 schedule_work(&tty->port->buf.work);
157 }
158 }
159
160 static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
161 {
162 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
163 ldata->read_buf[ldata->read_head] = c;
164 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
165 ldata->read_cnt++;
166 }
167 }
168
169 /**
170 * put_tty_queue - add character to tty
171 * @c: character
172 * @ldata: n_tty data
173 *
174 * Add a character to the tty read_buf queue. This is done under the
175 * read_lock to serialize character addition and also to protect us
176 * against parallel reads or flushes
177 */
178
179 static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
180 {
181 unsigned long flags;
182 /*
183 * The problem of stomping on the buffers ends here.
184 * Why didn't anyone see this one coming? --AJK
185 */
186 raw_spin_lock_irqsave(&ldata->read_lock, flags);
187 put_tty_queue_nolock(c, ldata);
188 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
189 }
190
191 /**
192 * reset_buffer_flags - reset buffer state
193 * @tty: terminal to reset
194 *
195 * Reset the read buffer counters, clear the flags,
196 * and make sure the driver is unthrottled. Called
197 * from n_tty_open() and n_tty_flush_buffer().
198 *
199 * Locking: tty_read_lock for read fields.
200 */
201
202 static void reset_buffer_flags(struct tty_struct *tty)
203 {
204 struct n_tty_data *ldata = tty->disc_data;
205 unsigned long flags;
206
207 raw_spin_lock_irqsave(&ldata->read_lock, flags);
208 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
209 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
210
211 mutex_lock(&ldata->echo_lock);
212 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
213 mutex_unlock(&ldata->echo_lock);
214
215 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
216 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
217 n_tty_set_room(tty);
218 }
219
220 /**
221 * n_tty_flush_buffer - clean input queue
222 * @tty: terminal device
223 *
224 * Flush the input buffer. Called when the line discipline is
225 * being closed, when the tty layer wants the buffer flushed (eg
226 * at hangup) or when the N_TTY line discipline internally has to
227 * clean the pending queue (for example some signals).
228 *
229 * Locking: ctrl_lock, read_lock.
230 */
231
232 static void n_tty_flush_buffer(struct tty_struct *tty)
233 {
234 unsigned long flags;
235 /* clear everything and unthrottle the driver */
236 reset_buffer_flags(tty);
237
238 if (!tty->link)
239 return;
240
241 spin_lock_irqsave(&tty->ctrl_lock, flags);
242 if (tty->link->packet) {
243 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
244 wake_up_interruptible(&tty->link->read_wait);
245 }
246 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
247 }
248
249 /**
250 * n_tty_chars_in_buffer - report available bytes
251 * @tty: tty device
252 *
253 * Report the number of characters buffered to be delivered to user
254 * at this instant in time.
255 *
256 * Locking: read_lock
257 */
258
259 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
260 {
261 struct n_tty_data *ldata = tty->disc_data;
262 unsigned long flags;
263 ssize_t n = 0;
264
265 raw_spin_lock_irqsave(&ldata->read_lock, flags);
266 if (!ldata->icanon) {
267 n = ldata->read_cnt;
268 } else if (ldata->canon_data) {
269 n = (ldata->canon_head > ldata->read_tail) ?
270 ldata->canon_head - ldata->read_tail :
271 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
272 }
273 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
274 return n;
275 }
276
277 /**
278 * is_utf8_continuation - utf8 multibyte check
279 * @c: byte to check
280 *
281 * Returns true if the utf8 character 'c' is a multibyte continuation
282 * character. We use this to correctly compute the on screen size
283 * of the character when printing
284 */
285
286 static inline int is_utf8_continuation(unsigned char c)
287 {
288 return (c & 0xc0) == 0x80;
289 }
290
291 /**
292 * is_continuation - multibyte check
293 * @c: byte to check
294 *
295 * Returns true if the utf8 character 'c' is a multibyte continuation
296 * character and the terminal is in unicode mode.
297 */
298
299 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
300 {
301 return I_IUTF8(tty) && is_utf8_continuation(c);
302 }
303
304 /**
305 * do_output_char - output one character
306 * @c: character (or partial unicode symbol)
307 * @tty: terminal device
308 * @space: space available in tty driver write buffer
309 *
310 * This is a helper function that handles one output character
311 * (including special characters like TAB, CR, LF, etc.),
312 * doing OPOST processing and putting the results in the
313 * tty driver's write buffer.
314 *
315 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
316 * and NLDLY. They simply aren't relevant in the world today.
317 * If you ever need them, add them here.
318 *
319 * Returns the number of bytes of buffer space used or -1 if
320 * no space left.
321 *
322 * Locking: should be called under the output_lock to protect
323 * the column state and space left in the buffer
324 */
325
326 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
327 {
328 struct n_tty_data *ldata = tty->disc_data;
329 int spaces;
330
331 if (!space)
332 return -1;
333
334 switch (c) {
335 case '\n':
336 if (O_ONLRET(tty))
337 ldata->column = 0;
338 if (O_ONLCR(tty)) {
339 if (space < 2)
340 return -1;
341 ldata->canon_column = ldata->column = 0;
342 tty->ops->write(tty, "\r\n", 2);
343 return 2;
344 }
345 ldata->canon_column = ldata->column;
346 break;
347 case '\r':
348 if (O_ONOCR(tty) && ldata->column == 0)
349 return 0;
350 if (O_OCRNL(tty)) {
351 c = '\n';
352 if (O_ONLRET(tty))
353 ldata->canon_column = ldata->column = 0;
354 break;
355 }
356 ldata->canon_column = ldata->column = 0;
357 break;
358 case '\t':
359 spaces = 8 - (ldata->column & 7);
360 if (O_TABDLY(tty) == XTABS) {
361 if (space < spaces)
362 return -1;
363 ldata->column += spaces;
364 tty->ops->write(tty, " ", spaces);
365 return spaces;
366 }
367 ldata->column += spaces;
368 break;
369 case '\b':
370 if (ldata->column > 0)
371 ldata->column--;
372 break;
373 default:
374 if (!iscntrl(c)) {
375 if (O_OLCUC(tty))
376 c = toupper(c);
377 if (!is_continuation(c, tty))
378 ldata->column++;
379 }
380 break;
381 }
382
383 tty_put_char(tty, c);
384 return 1;
385 }
386
387 /**
388 * process_output - output post processor
389 * @c: character (or partial unicode symbol)
390 * @tty: terminal device
391 *
392 * Output one character with OPOST processing.
393 * Returns -1 when the output device is full and the character
394 * must be retried.
395 *
396 * Locking: output_lock to protect column state and space left
397 * (also, this is called from n_tty_write under the
398 * tty layer write lock)
399 */
400
401 static int process_output(unsigned char c, struct tty_struct *tty)
402 {
403 struct n_tty_data *ldata = tty->disc_data;
404 int space, retval;
405
406 mutex_lock(&ldata->output_lock);
407
408 space = tty_write_room(tty);
409 retval = do_output_char(c, tty, space);
410
411 mutex_unlock(&ldata->output_lock);
412 if (retval < 0)
413 return -1;
414 else
415 return 0;
416 }
417
418 /**
419 * process_output_block - block post processor
420 * @tty: terminal device
421 * @buf: character buffer
422 * @nr: number of bytes to output
423 *
424 * Output a block of characters with OPOST processing.
425 * Returns the number of characters output.
426 *
427 * This path is used to speed up block console writes, among other
428 * things when processing blocks of output data. It handles only
429 * the simple cases normally found and helps to generate blocks of
430 * symbols for the console driver and thus improve performance.
431 *
432 * Locking: output_lock to protect column state and space left
433 * (also, this is called from n_tty_write under the
434 * tty layer write lock)
435 */
436
437 static ssize_t process_output_block(struct tty_struct *tty,
438 const unsigned char *buf, unsigned int nr)
439 {
440 struct n_tty_data *ldata = tty->disc_data;
441 int space;
442 int i;
443 const unsigned char *cp;
444
445 mutex_lock(&ldata->output_lock);
446
447 space = tty_write_room(tty);
448 if (!space) {
449 mutex_unlock(&ldata->output_lock);
450 return 0;
451 }
452 if (nr > space)
453 nr = space;
454
455 for (i = 0, cp = buf; i < nr; i++, cp++) {
456 unsigned char c = *cp;
457
458 switch (c) {
459 case '\n':
460 if (O_ONLRET(tty))
461 ldata->column = 0;
462 if (O_ONLCR(tty))
463 goto break_out;
464 ldata->canon_column = ldata->column;
465 break;
466 case '\r':
467 if (O_ONOCR(tty) && ldata->column == 0)
468 goto break_out;
469 if (O_OCRNL(tty))
470 goto break_out;
471 ldata->canon_column = ldata->column = 0;
472 break;
473 case '\t':
474 goto break_out;
475 case '\b':
476 if (ldata->column > 0)
477 ldata->column--;
478 break;
479 default:
480 if (!iscntrl(c)) {
481 if (O_OLCUC(tty))
482 goto break_out;
483 if (!is_continuation(c, tty))
484 ldata->column++;
485 }
486 break;
487 }
488 }
489 break_out:
490 i = tty->ops->write(tty, buf, i);
491
492 mutex_unlock(&ldata->output_lock);
493 return i;
494 }
495
496 /**
497 * process_echoes - write pending echo characters
498 * @tty: terminal device
499 *
500 * Write previously buffered echo (and other ldisc-generated)
501 * characters to the tty.
502 *
503 * Characters generated by the ldisc (including echoes) need to
504 * be buffered because the driver's write buffer can fill during
505 * heavy program output. Echoing straight to the driver will
506 * often fail under these conditions, causing lost characters and
507 * resulting mismatches of ldisc state information.
508 *
509 * Since the ldisc state must represent the characters actually sent
510 * to the driver at the time of the write, operations like certain
511 * changes in column state are also saved in the buffer and executed
512 * here.
513 *
514 * A circular fifo buffer is used so that the most recent characters
515 * are prioritized. Also, when control characters are echoed with a
516 * prefixed "^", the pair is treated atomically and thus not separated.
517 *
518 * Locking: output_lock to protect column state and space left,
519 * echo_lock to protect the echo buffer
520 */
521
522 static void process_echoes(struct tty_struct *tty)
523 {
524 struct n_tty_data *ldata = tty->disc_data;
525 int space, nr;
526 unsigned char c;
527 unsigned char *cp, *buf_end;
528
529 if (!ldata->echo_cnt)
530 return;
531
532 mutex_lock(&ldata->output_lock);
533 mutex_lock(&ldata->echo_lock);
534
535 space = tty_write_room(tty);
536
537 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
538 cp = ldata->echo_buf + ldata->echo_pos;
539 nr = ldata->echo_cnt;
540 while (nr > 0) {
541 c = *cp;
542 if (c == ECHO_OP_START) {
543 unsigned char op;
544 unsigned char *opp;
545 int no_space_left = 0;
546
547 /*
548 * If the buffer byte is the start of a multi-byte
549 * operation, get the next byte, which is either the
550 * op code or a control character value.
551 */
552 opp = cp + 1;
553 if (opp == buf_end)
554 opp -= N_TTY_BUF_SIZE;
555 op = *opp;
556
557 switch (op) {
558 unsigned int num_chars, num_bs;
559
560 case ECHO_OP_ERASE_TAB:
561 if (++opp == buf_end)
562 opp -= N_TTY_BUF_SIZE;
563 num_chars = *opp;
564
565 /*
566 * Determine how many columns to go back
567 * in order to erase the tab.
568 * This depends on the number of columns
569 * used by other characters within the tab
570 * area. If this (modulo 8) count is from
571 * the start of input rather than from a
572 * previous tab, we offset by canon column.
573 * Otherwise, tab spacing is normal.
574 */
575 if (!(num_chars & 0x80))
576 num_chars += ldata->canon_column;
577 num_bs = 8 - (num_chars & 7);
578
579 if (num_bs > space) {
580 no_space_left = 1;
581 break;
582 }
583 space -= num_bs;
584 while (num_bs--) {
585 tty_put_char(tty, '\b');
586 if (ldata->column > 0)
587 ldata->column--;
588 }
589 cp += 3;
590 nr -= 3;
591 break;
592
593 case ECHO_OP_SET_CANON_COL:
594 ldata->canon_column = ldata->column;
595 cp += 2;
596 nr -= 2;
597 break;
598
599 case ECHO_OP_MOVE_BACK_COL:
600 if (ldata->column > 0)
601 ldata->column--;
602 cp += 2;
603 nr -= 2;
604 break;
605
606 case ECHO_OP_START:
607 /* This is an escaped echo op start code */
608 if (!space) {
609 no_space_left = 1;
610 break;
611 }
612 tty_put_char(tty, ECHO_OP_START);
613 ldata->column++;
614 space--;
615 cp += 2;
616 nr -= 2;
617 break;
618
619 default:
620 /*
621 * If the op is not a special byte code,
622 * it is a ctrl char tagged to be echoed
623 * as "^X" (where X is the letter
624 * representing the control char).
625 * Note that we must ensure there is
626 * enough space for the whole ctrl pair.
627 *
628 */
629 if (space < 2) {
630 no_space_left = 1;
631 break;
632 }
633 tty_put_char(tty, '^');
634 tty_put_char(tty, op ^ 0100);
635 ldata->column += 2;
636 space -= 2;
637 cp += 2;
638 nr -= 2;
639 }
640
641 if (no_space_left)
642 break;
643 } else {
644 if (O_OPOST(tty) &&
645 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
646 int retval = do_output_char(c, tty, space);
647 if (retval < 0)
648 break;
649 space -= retval;
650 } else {
651 if (!space)
652 break;
653 tty_put_char(tty, c);
654 space -= 1;
655 }
656 cp += 1;
657 nr -= 1;
658 }
659
660 /* When end of circular buffer reached, wrap around */
661 if (cp >= buf_end)
662 cp -= N_TTY_BUF_SIZE;
663 }
664
665 if (nr == 0) {
666 ldata->echo_pos = 0;
667 ldata->echo_cnt = 0;
668 ldata->echo_overrun = 0;
669 } else {
670 int num_processed = ldata->echo_cnt - nr;
671 ldata->echo_pos += num_processed;
672 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
673 ldata->echo_cnt = nr;
674 if (num_processed > 0)
675 ldata->echo_overrun = 0;
676 }
677
678 mutex_unlock(&ldata->echo_lock);
679 mutex_unlock(&ldata->output_lock);
680
681 if (tty->ops->flush_chars)
682 tty->ops->flush_chars(tty);
683 }
684
685 /**
686 * add_echo_byte - add a byte to the echo buffer
687 * @c: unicode byte to echo
688 * @ldata: n_tty data
689 *
690 * Add a character or operation byte to the echo buffer.
691 *
692 * Should be called under the echo lock to protect the echo buffer.
693 */
694
695 static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
696 {
697 int new_byte_pos;
698
699 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
700 /* Circular buffer is already at capacity */
701 new_byte_pos = ldata->echo_pos;
702
703 /*
704 * Since the buffer start position needs to be advanced,
705 * be sure to step by a whole operation byte group.
706 */
707 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
708 if (ldata->echo_buf[(ldata->echo_pos + 1) &
709 (N_TTY_BUF_SIZE - 1)] ==
710 ECHO_OP_ERASE_TAB) {
711 ldata->echo_pos += 3;
712 ldata->echo_cnt -= 2;
713 } else {
714 ldata->echo_pos += 2;
715 ldata->echo_cnt -= 1;
716 }
717 } else {
718 ldata->echo_pos++;
719 }
720 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
721
722 ldata->echo_overrun = 1;
723 } else {
724 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
725 new_byte_pos &= N_TTY_BUF_SIZE - 1;
726 ldata->echo_cnt++;
727 }
728
729 ldata->echo_buf[new_byte_pos] = c;
730 }
731
732 /**
733 * echo_move_back_col - add operation to move back a column
734 * @ldata: n_tty data
735 *
736 * Add an operation to the echo buffer to move back one column.
737 *
738 * Locking: echo_lock to protect the echo buffer
739 */
740
741 static void echo_move_back_col(struct n_tty_data *ldata)
742 {
743 mutex_lock(&ldata->echo_lock);
744 add_echo_byte(ECHO_OP_START, ldata);
745 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
746 mutex_unlock(&ldata->echo_lock);
747 }
748
749 /**
750 * echo_set_canon_col - add operation to set the canon column
751 * @ldata: n_tty data
752 *
753 * Add an operation to the echo buffer to set the canon column
754 * to the current column.
755 *
756 * Locking: echo_lock to protect the echo buffer
757 */
758
759 static void echo_set_canon_col(struct n_tty_data *ldata)
760 {
761 mutex_lock(&ldata->echo_lock);
762 add_echo_byte(ECHO_OP_START, ldata);
763 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
764 mutex_unlock(&ldata->echo_lock);
765 }
766
767 /**
768 * echo_erase_tab - add operation to erase a tab
769 * @num_chars: number of character columns already used
770 * @after_tab: true if num_chars starts after a previous tab
771 * @ldata: n_tty data
772 *
773 * Add an operation to the echo buffer to erase a tab.
774 *
775 * Called by the eraser function, which knows how many character
776 * columns have been used since either a previous tab or the start
777 * of input. This information will be used later, along with
778 * canon column (if applicable), to go back the correct number
779 * of columns.
780 *
781 * Locking: echo_lock to protect the echo buffer
782 */
783
784 static void echo_erase_tab(unsigned int num_chars, int after_tab,
785 struct n_tty_data *ldata)
786 {
787 mutex_lock(&ldata->echo_lock);
788
789 add_echo_byte(ECHO_OP_START, ldata);
790 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
791
792 /* We only need to know this modulo 8 (tab spacing) */
793 num_chars &= 7;
794
795 /* Set the high bit as a flag if num_chars is after a previous tab */
796 if (after_tab)
797 num_chars |= 0x80;
798
799 add_echo_byte(num_chars, ldata);
800
801 mutex_unlock(&ldata->echo_lock);
802 }
803
804 /**
805 * echo_char_raw - echo a character raw
806 * @c: unicode byte to echo
807 * @tty: terminal device
808 *
809 * Echo user input back onto the screen. This must be called only when
810 * L_ECHO(tty) is true. Called from the driver receive_buf path.
811 *
812 * This variant does not treat control characters specially.
813 *
814 * Locking: echo_lock to protect the echo buffer
815 */
816
817 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
818 {
819 mutex_lock(&ldata->echo_lock);
820 if (c == ECHO_OP_START) {
821 add_echo_byte(ECHO_OP_START, ldata);
822 add_echo_byte(ECHO_OP_START, ldata);
823 } else {
824 add_echo_byte(c, ldata);
825 }
826 mutex_unlock(&ldata->echo_lock);
827 }
828
829 /**
830 * echo_char - echo a character
831 * @c: unicode byte to echo
832 * @tty: terminal device
833 *
834 * Echo user input back onto the screen. This must be called only when
835 * L_ECHO(tty) is true. Called from the driver receive_buf path.
836 *
837 * This variant tags control characters to be echoed as "^X"
838 * (where X is the letter representing the control char).
839 *
840 * Locking: echo_lock to protect the echo buffer
841 */
842
843 static void echo_char(unsigned char c, struct tty_struct *tty)
844 {
845 struct n_tty_data *ldata = tty->disc_data;
846
847 mutex_lock(&ldata->echo_lock);
848
849 if (c == ECHO_OP_START) {
850 add_echo_byte(ECHO_OP_START, ldata);
851 add_echo_byte(ECHO_OP_START, ldata);
852 } else {
853 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
854 add_echo_byte(ECHO_OP_START, ldata);
855 add_echo_byte(c, ldata);
856 }
857
858 mutex_unlock(&ldata->echo_lock);
859 }
860
861 /**
862 * finish_erasing - complete erase
863 * @ldata: n_tty data
864 */
865
866 static inline void finish_erasing(struct n_tty_data *ldata)
867 {
868 if (ldata->erasing) {
869 echo_char_raw('/', ldata);
870 ldata->erasing = 0;
871 }
872 }
873
874 /**
875 * eraser - handle erase function
876 * @c: character input
877 * @tty: terminal device
878 *
879 * Perform erase and necessary output when an erase character is
880 * present in the stream from the driver layer. Handles the complexities
881 * of UTF-8 multibyte symbols.
882 *
883 * Locking: read_lock for tty buffers
884 */
885
886 static void eraser(unsigned char c, struct tty_struct *tty)
887 {
888 struct n_tty_data *ldata = tty->disc_data;
889 enum { ERASE, WERASE, KILL } kill_type;
890 int head, seen_alnums, cnt;
891 unsigned long flags;
892
893 /* FIXME: locking needed ? */
894 if (ldata->read_head == ldata->canon_head) {
895 /* process_output('\a', tty); */ /* what do you think? */
896 return;
897 }
898 if (c == ERASE_CHAR(tty))
899 kill_type = ERASE;
900 else if (c == WERASE_CHAR(tty))
901 kill_type = WERASE;
902 else {
903 if (!L_ECHO(tty)) {
904 raw_spin_lock_irqsave(&ldata->read_lock, flags);
905 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
906 (N_TTY_BUF_SIZE - 1));
907 ldata->read_head = ldata->canon_head;
908 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
909 return;
910 }
911 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
912 raw_spin_lock_irqsave(&ldata->read_lock, flags);
913 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
914 (N_TTY_BUF_SIZE - 1));
915 ldata->read_head = ldata->canon_head;
916 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
917 finish_erasing(ldata);
918 echo_char(KILL_CHAR(tty), tty);
919 /* Add a newline if ECHOK is on and ECHOKE is off. */
920 if (L_ECHOK(tty))
921 echo_char_raw('\n', ldata);
922 return;
923 }
924 kill_type = KILL;
925 }
926
927 seen_alnums = 0;
928 /* FIXME: Locking ?? */
929 while (ldata->read_head != ldata->canon_head) {
930 head = ldata->read_head;
931
932 /* erase a single possibly multibyte character */
933 do {
934 head = (head - 1) & (N_TTY_BUF_SIZE-1);
935 c = ldata->read_buf[head];
936 } while (is_continuation(c, tty) && head != ldata->canon_head);
937
938 /* do not partially erase */
939 if (is_continuation(c, tty))
940 break;
941
942 if (kill_type == WERASE) {
943 /* Equivalent to BSD's ALTWERASE. */
944 if (isalnum(c) || c == '_')
945 seen_alnums++;
946 else if (seen_alnums)
947 break;
948 }
949 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
950 raw_spin_lock_irqsave(&ldata->read_lock, flags);
951 ldata->read_head = head;
952 ldata->read_cnt -= cnt;
953 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
954 if (L_ECHO(tty)) {
955 if (L_ECHOPRT(tty)) {
956 if (!ldata->erasing) {
957 echo_char_raw('\\', ldata);
958 ldata->erasing = 1;
959 }
960 /* if cnt > 1, output a multi-byte character */
961 echo_char(c, tty);
962 while (--cnt > 0) {
963 head = (head+1) & (N_TTY_BUF_SIZE-1);
964 echo_char_raw(ldata->read_buf[head],
965 ldata);
966 echo_move_back_col(ldata);
967 }
968 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
969 echo_char(ERASE_CHAR(tty), tty);
970 } else if (c == '\t') {
971 unsigned int num_chars = 0;
972 int after_tab = 0;
973 unsigned long tail = ldata->read_head;
974
975 /*
976 * Count the columns used for characters
977 * since the start of input or after a
978 * previous tab.
979 * This info is used to go back the correct
980 * number of columns.
981 */
982 while (tail != ldata->canon_head) {
983 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
984 c = ldata->read_buf[tail];
985 if (c == '\t') {
986 after_tab = 1;
987 break;
988 } else if (iscntrl(c)) {
989 if (L_ECHOCTL(tty))
990 num_chars += 2;
991 } else if (!is_continuation(c, tty)) {
992 num_chars++;
993 }
994 }
995 echo_erase_tab(num_chars, after_tab, ldata);
996 } else {
997 if (iscntrl(c) && L_ECHOCTL(tty)) {
998 echo_char_raw('\b', ldata);
999 echo_char_raw(' ', ldata);
1000 echo_char_raw('\b', ldata);
1001 }
1002 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1003 echo_char_raw('\b', ldata);
1004 echo_char_raw(' ', ldata);
1005 echo_char_raw('\b', ldata);
1006 }
1007 }
1008 }
1009 if (kill_type == ERASE)
1010 break;
1011 }
1012 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1013 finish_erasing(ldata);
1014 }
1015
1016 /**
1017 * isig - handle the ISIG optio
1018 * @sig: signal
1019 * @tty: terminal
1020 *
1021 * Called when a signal is being sent due to terminal input.
1022 * Called from the driver receive_buf path so serialized.
1023 *
1024 * Locking: ctrl_lock
1025 */
1026
1027 static inline void isig(int sig, struct tty_struct *tty)
1028 {
1029 struct pid *tty_pgrp = tty_get_pgrp(tty);
1030 if (tty_pgrp) {
1031 kill_pgrp(tty_pgrp, sig, 1);
1032 put_pid(tty_pgrp);
1033 }
1034 }
1035
1036 /**
1037 * n_tty_receive_break - handle break
1038 * @tty: terminal
1039 *
1040 * An RS232 break event has been hit in the incoming bitstream. This
1041 * can cause a variety of events depending upon the termios settings.
1042 *
1043 * Called from the receive_buf path so single threaded.
1044 */
1045
1046 static inline void n_tty_receive_break(struct tty_struct *tty)
1047 {
1048 struct n_tty_data *ldata = tty->disc_data;
1049
1050 if (I_IGNBRK(tty))
1051 return;
1052 if (I_BRKINT(tty)) {
1053 isig(SIGINT, tty);
1054 if (!L_NOFLSH(tty)) {
1055 n_tty_flush_buffer(tty);
1056 tty_driver_flush_buffer(tty);
1057 }
1058 return;
1059 }
1060 if (I_PARMRK(tty)) {
1061 put_tty_queue('\377', ldata);
1062 put_tty_queue('\0', ldata);
1063 }
1064 put_tty_queue('\0', ldata);
1065 wake_up_interruptible(&tty->read_wait);
1066 }
1067
1068 /**
1069 * n_tty_receive_overrun - handle overrun reporting
1070 * @tty: terminal
1071 *
1072 * Data arrived faster than we could process it. While the tty
1073 * driver has flagged this the bits that were missed are gone
1074 * forever.
1075 *
1076 * Called from the receive_buf path so single threaded. Does not
1077 * need locking as num_overrun and overrun_time are function
1078 * private.
1079 */
1080
1081 static inline void n_tty_receive_overrun(struct tty_struct *tty)
1082 {
1083 struct n_tty_data *ldata = tty->disc_data;
1084 char buf[64];
1085
1086 ldata->num_overrun++;
1087 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1088 time_after(ldata->overrun_time, jiffies)) {
1089 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1090 tty_name(tty, buf),
1091 ldata->num_overrun);
1092 ldata->overrun_time = jiffies;
1093 ldata->num_overrun = 0;
1094 }
1095 }
1096
1097 /**
1098 * n_tty_receive_parity_error - error notifier
1099 * @tty: terminal device
1100 * @c: character
1101 *
1102 * Process a parity error and queue the right data to indicate
1103 * the error case if necessary. Locking as per n_tty_receive_buf.
1104 */
1105 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1106 unsigned char c)
1107 {
1108 struct n_tty_data *ldata = tty->disc_data;
1109
1110 if (I_IGNPAR(tty))
1111 return;
1112 if (I_PARMRK(tty)) {
1113 put_tty_queue('\377', ldata);
1114 put_tty_queue('\0', ldata);
1115 put_tty_queue(c, ldata);
1116 } else if (I_INPCK(tty))
1117 put_tty_queue('\0', ldata);
1118 else
1119 put_tty_queue(c, ldata);
1120 wake_up_interruptible(&tty->read_wait);
1121 }
1122
1123 /**
1124 * n_tty_receive_char - perform processing
1125 * @tty: terminal device
1126 * @c: character
1127 *
1128 * Process an individual character of input received from the driver.
1129 * This is serialized with respect to itself by the rules for the
1130 * driver above.
1131 */
1132
1133 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1134 {
1135 struct n_tty_data *ldata = tty->disc_data;
1136 unsigned long flags;
1137 int parmrk;
1138
1139 if (ldata->raw) {
1140 put_tty_queue(c, ldata);
1141 return;
1142 }
1143
1144 if (I_ISTRIP(tty))
1145 c &= 0x7f;
1146 if (I_IUCLC(tty) && L_IEXTEN(tty))
1147 c = tolower(c);
1148
1149 if (L_EXTPROC(tty)) {
1150 put_tty_queue(c, ldata);
1151 return;
1152 }
1153
1154 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1155 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1156 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1157 start_tty(tty);
1158 process_echoes(tty);
1159 }
1160
1161 if (tty->closing) {
1162 if (I_IXON(tty)) {
1163 if (c == START_CHAR(tty)) {
1164 start_tty(tty);
1165 process_echoes(tty);
1166 } else if (c == STOP_CHAR(tty))
1167 stop_tty(tty);
1168 }
1169 return;
1170 }
1171
1172 /*
1173 * If the previous character was LNEXT, or we know that this
1174 * character is not one of the characters that we'll have to
1175 * handle specially, do shortcut processing to speed things
1176 * up.
1177 */
1178 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1179 ldata->lnext = 0;
1180 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1181 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1182 /* beep if no space */
1183 if (L_ECHO(tty))
1184 process_output('\a', tty);
1185 return;
1186 }
1187 if (L_ECHO(tty)) {
1188 finish_erasing(ldata);
1189 /* Record the column of first canon char. */
1190 if (ldata->canon_head == ldata->read_head)
1191 echo_set_canon_col(ldata);
1192 echo_char(c, tty);
1193 process_echoes(tty);
1194 }
1195 if (parmrk)
1196 put_tty_queue(c, ldata);
1197 put_tty_queue(c, ldata);
1198 return;
1199 }
1200
1201 if (I_IXON(tty)) {
1202 if (c == START_CHAR(tty)) {
1203 start_tty(tty);
1204 process_echoes(tty);
1205 return;
1206 }
1207 if (c == STOP_CHAR(tty)) {
1208 stop_tty(tty);
1209 return;
1210 }
1211 }
1212
1213 if (L_ISIG(tty)) {
1214 int signal;
1215 signal = SIGINT;
1216 if (c == INTR_CHAR(tty))
1217 goto send_signal;
1218 signal = SIGQUIT;
1219 if (c == QUIT_CHAR(tty))
1220 goto send_signal;
1221 signal = SIGTSTP;
1222 if (c == SUSP_CHAR(tty)) {
1223 send_signal:
1224 if (!L_NOFLSH(tty)) {
1225 n_tty_flush_buffer(tty);
1226 tty_driver_flush_buffer(tty);
1227 }
1228 if (I_IXON(tty))
1229 start_tty(tty);
1230 if (L_ECHO(tty)) {
1231 echo_char(c, tty);
1232 process_echoes(tty);
1233 }
1234 isig(signal, tty);
1235 return;
1236 }
1237 }
1238
1239 if (c == '\r') {
1240 if (I_IGNCR(tty))
1241 return;
1242 if (I_ICRNL(tty))
1243 c = '\n';
1244 } else if (c == '\n' && I_INLCR(tty))
1245 c = '\r';
1246
1247 if (ldata->icanon) {
1248 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1249 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1250 eraser(c, tty);
1251 process_echoes(tty);
1252 return;
1253 }
1254 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1255 ldata->lnext = 1;
1256 if (L_ECHO(tty)) {
1257 finish_erasing(ldata);
1258 if (L_ECHOCTL(tty)) {
1259 echo_char_raw('^', ldata);
1260 echo_char_raw('\b', ldata);
1261 process_echoes(tty);
1262 }
1263 }
1264 return;
1265 }
1266 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1267 L_IEXTEN(tty)) {
1268 unsigned long tail = ldata->canon_head;
1269
1270 finish_erasing(ldata);
1271 echo_char(c, tty);
1272 echo_char_raw('\n', ldata);
1273 while (tail != ldata->read_head) {
1274 echo_char(ldata->read_buf[tail], tty);
1275 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1276 }
1277 process_echoes(tty);
1278 return;
1279 }
1280 if (c == '\n') {
1281 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1282 if (L_ECHO(tty))
1283 process_output('\a', tty);
1284 return;
1285 }
1286 if (L_ECHO(tty) || L_ECHONL(tty)) {
1287 echo_char_raw('\n', ldata);
1288 process_echoes(tty);
1289 }
1290 goto handle_newline;
1291 }
1292 if (c == EOF_CHAR(tty)) {
1293 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1294 return;
1295 if (ldata->canon_head != ldata->read_head)
1296 set_bit(TTY_PUSH, &tty->flags);
1297 c = __DISABLED_CHAR;
1298 goto handle_newline;
1299 }
1300 if ((c == EOL_CHAR(tty)) ||
1301 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1302 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1303 ? 1 : 0;
1304 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1305 if (L_ECHO(tty))
1306 process_output('\a', tty);
1307 return;
1308 }
1309 /*
1310 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1311 */
1312 if (L_ECHO(tty)) {
1313 /* Record the column of first canon char. */
1314 if (ldata->canon_head == ldata->read_head)
1315 echo_set_canon_col(ldata);
1316 echo_char(c, tty);
1317 process_echoes(tty);
1318 }
1319 /*
1320 * XXX does PARMRK doubling happen for
1321 * EOL_CHAR and EOL2_CHAR?
1322 */
1323 if (parmrk)
1324 put_tty_queue(c, ldata);
1325
1326 handle_newline:
1327 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1328 set_bit(ldata->read_head, ldata->read_flags);
1329 put_tty_queue_nolock(c, ldata);
1330 ldata->canon_head = ldata->read_head;
1331 ldata->canon_data++;
1332 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1333 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1334 if (waitqueue_active(&tty->read_wait))
1335 wake_up_interruptible(&tty->read_wait);
1336 return;
1337 }
1338 }
1339
1340 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1341 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1342 /* beep if no space */
1343 if (L_ECHO(tty))
1344 process_output('\a', tty);
1345 return;
1346 }
1347 if (L_ECHO(tty)) {
1348 finish_erasing(ldata);
1349 if (c == '\n')
1350 echo_char_raw('\n', ldata);
1351 else {
1352 /* Record the column of first canon char. */
1353 if (ldata->canon_head == ldata->read_head)
1354 echo_set_canon_col(ldata);
1355 echo_char(c, tty);
1356 }
1357 process_echoes(tty);
1358 }
1359
1360 if (parmrk)
1361 put_tty_queue(c, ldata);
1362
1363 put_tty_queue(c, ldata);
1364 }
1365
1366
1367 /**
1368 * n_tty_write_wakeup - asynchronous I/O notifier
1369 * @tty: tty device
1370 *
1371 * Required for the ptys, serial driver etc. since processes
1372 * that attach themselves to the master and rely on ASYNC
1373 * IO must be woken up
1374 */
1375
1376 static void n_tty_write_wakeup(struct tty_struct *tty)
1377 {
1378 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1379 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1380 }
1381
1382 /**
1383 * n_tty_receive_buf - data receive
1384 * @tty: terminal device
1385 * @cp: buffer
1386 * @fp: flag buffer
1387 * @count: characters
1388 *
1389 * Called by the terminal driver when a block of characters has
1390 * been received. This function must be called from soft contexts
1391 * not from interrupt context. The driver is responsible for making
1392 * calls one at a time and in order (or using flush_to_ldisc)
1393 */
1394
1395 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1396 char *fp, int count)
1397 {
1398 struct n_tty_data *ldata = tty->disc_data;
1399 const unsigned char *p;
1400 char *f, flags = TTY_NORMAL;
1401 int i;
1402 char buf[64];
1403 unsigned long cpuflags;
1404
1405 if (ldata->real_raw) {
1406 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
1407 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1408 N_TTY_BUF_SIZE - ldata->read_head);
1409 i = min(count, i);
1410 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1411 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1412 ldata->read_cnt += i;
1413 cp += i;
1414 count -= i;
1415
1416 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1417 N_TTY_BUF_SIZE - ldata->read_head);
1418 i = min(count, i);
1419 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1420 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1421 ldata->read_cnt += i;
1422 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1423 } else {
1424 for (i = count, p = cp, f = fp; i; i--, p++) {
1425 if (f)
1426 flags = *f++;
1427 switch (flags) {
1428 case TTY_NORMAL:
1429 n_tty_receive_char(tty, *p);
1430 break;
1431 case TTY_BREAK:
1432 n_tty_receive_break(tty);
1433 break;
1434 case TTY_PARITY:
1435 case TTY_FRAME:
1436 n_tty_receive_parity_error(tty, *p);
1437 break;
1438 case TTY_OVERRUN:
1439 n_tty_receive_overrun(tty);
1440 break;
1441 default:
1442 printk(KERN_ERR "%s: unknown flag %d\n",
1443 tty_name(tty, buf), flags);
1444 break;
1445 }
1446 }
1447 if (tty->ops->flush_chars)
1448 tty->ops->flush_chars(tty);
1449 }
1450
1451 n_tty_set_room(tty);
1452
1453 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
1454 L_EXTPROC(tty)) {
1455 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1456 if (waitqueue_active(&tty->read_wait))
1457 wake_up_interruptible(&tty->read_wait);
1458 }
1459
1460 /*
1461 * Check the remaining room for the input canonicalization
1462 * mode. We don't want to throttle the driver if we're in
1463 * canonical mode and don't have a newline yet!
1464 */
1465 while (1) {
1466 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1467 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1468 break;
1469 if (!tty_throttle_safe(tty))
1470 break;
1471 }
1472 __tty_set_flow_change(tty, 0);
1473 }
1474
1475 int is_ignored(int sig)
1476 {
1477 return (sigismember(&current->blocked, sig) ||
1478 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1479 }
1480
1481 /**
1482 * n_tty_set_termios - termios data changed
1483 * @tty: terminal
1484 * @old: previous data
1485 *
1486 * Called by the tty layer when the user changes termios flags so
1487 * that the line discipline can plan ahead. This function cannot sleep
1488 * and is protected from re-entry by the tty layer. The user is
1489 * guaranteed that this function will not be re-entered or in progress
1490 * when the ldisc is closed.
1491 *
1492 * Locking: Caller holds tty->termios_mutex
1493 */
1494
1495 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1496 {
1497 struct n_tty_data *ldata = tty->disc_data;
1498 int canon_change = 1;
1499
1500 if (old)
1501 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1502 if (canon_change) {
1503 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1504 ldata->canon_head = ldata->read_tail;
1505 ldata->canon_data = 0;
1506 ldata->erasing = 0;
1507 }
1508
1509 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
1510 wake_up_interruptible(&tty->read_wait);
1511
1512 ldata->icanon = (L_ICANON(tty) != 0);
1513 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1514 ldata->raw = 1;
1515 ldata->real_raw = 1;
1516 n_tty_set_room(tty);
1517 return;
1518 }
1519 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1520 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1521 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1522 I_PARMRK(tty)) {
1523 bitmap_zero(ldata->process_char_map, 256);
1524
1525 if (I_IGNCR(tty) || I_ICRNL(tty))
1526 set_bit('\r', ldata->process_char_map);
1527 if (I_INLCR(tty))
1528 set_bit('\n', ldata->process_char_map);
1529
1530 if (L_ICANON(tty)) {
1531 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1532 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1533 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1534 set_bit('\n', ldata->process_char_map);
1535 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1536 if (L_IEXTEN(tty)) {
1537 set_bit(WERASE_CHAR(tty),
1538 ldata->process_char_map);
1539 set_bit(LNEXT_CHAR(tty),
1540 ldata->process_char_map);
1541 set_bit(EOL2_CHAR(tty),
1542 ldata->process_char_map);
1543 if (L_ECHO(tty))
1544 set_bit(REPRINT_CHAR(tty),
1545 ldata->process_char_map);
1546 }
1547 }
1548 if (I_IXON(tty)) {
1549 set_bit(START_CHAR(tty), ldata->process_char_map);
1550 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1551 }
1552 if (L_ISIG(tty)) {
1553 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1554 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1555 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1556 }
1557 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
1558 ldata->raw = 0;
1559 ldata->real_raw = 0;
1560 } else {
1561 ldata->raw = 1;
1562 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1563 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1564 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1565 ldata->real_raw = 1;
1566 else
1567 ldata->real_raw = 0;
1568 }
1569 n_tty_set_room(tty);
1570 /* The termios change make the tty ready for I/O */
1571 wake_up_interruptible(&tty->write_wait);
1572 wake_up_interruptible(&tty->read_wait);
1573 }
1574
1575 /**
1576 * n_tty_close - close the ldisc for this tty
1577 * @tty: device
1578 *
1579 * Called from the terminal layer when this line discipline is
1580 * being shut down, either because of a close or becsuse of a
1581 * discipline change. The function will not be called while other
1582 * ldisc methods are in progress.
1583 */
1584
1585 static void n_tty_close(struct tty_struct *tty)
1586 {
1587 struct n_tty_data *ldata = tty->disc_data;
1588
1589 n_tty_flush_buffer(tty);
1590 kfree(ldata->read_buf);
1591 kfree(ldata->echo_buf);
1592 kfree(ldata);
1593 tty->disc_data = NULL;
1594 }
1595
1596 /**
1597 * n_tty_open - open an ldisc
1598 * @tty: terminal to open
1599 *
1600 * Called when this line discipline is being attached to the
1601 * terminal device. Can sleep. Called serialized so that no
1602 * other events will occur in parallel. No further open will occur
1603 * until a close.
1604 */
1605
1606 static int n_tty_open(struct tty_struct *tty)
1607 {
1608 struct n_tty_data *ldata;
1609
1610 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1611 if (!ldata)
1612 goto err;
1613
1614 ldata->overrun_time = jiffies;
1615 mutex_init(&ldata->atomic_read_lock);
1616 mutex_init(&ldata->output_lock);
1617 mutex_init(&ldata->echo_lock);
1618 raw_spin_lock_init(&ldata->read_lock);
1619
1620 /* These are ugly. Currently a malloc failure here can panic */
1621 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1622 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1623 if (!ldata->read_buf || !ldata->echo_buf)
1624 goto err_free_bufs;
1625
1626 tty->disc_data = ldata;
1627 reset_buffer_flags(tty);
1628 tty_unthrottle(tty);
1629 ldata->column = 0;
1630 n_tty_set_termios(tty, NULL);
1631 tty->minimum_to_wake = 1;
1632 tty->closing = 0;
1633
1634 return 0;
1635 err_free_bufs:
1636 kfree(ldata->read_buf);
1637 kfree(ldata->echo_buf);
1638 kfree(ldata);
1639 err:
1640 return -ENOMEM;
1641 }
1642
1643 static inline int input_available_p(struct tty_struct *tty, int amt)
1644 {
1645 struct n_tty_data *ldata = tty->disc_data;
1646
1647 tty_flush_to_ldisc(tty);
1648 if (ldata->icanon && !L_EXTPROC(tty)) {
1649 if (ldata->canon_data)
1650 return 1;
1651 } else if (ldata->read_cnt >= (amt ? amt : 1))
1652 return 1;
1653
1654 return 0;
1655 }
1656
1657 /**
1658 * copy_from_read_buf - copy read data directly
1659 * @tty: terminal device
1660 * @b: user data
1661 * @nr: size of data
1662 *
1663 * Helper function to speed up n_tty_read. It is only called when
1664 * ICANON is off; it copies characters straight from the tty queue to
1665 * user space directly. It can be profitably called twice; once to
1666 * drain the space from the tail pointer to the (physical) end of the
1667 * buffer, and once to drain the space from the (physical) beginning of
1668 * the buffer to head pointer.
1669 *
1670 * Called under the ldata->atomic_read_lock sem
1671 *
1672 */
1673
1674 static int copy_from_read_buf(struct tty_struct *tty,
1675 unsigned char __user **b,
1676 size_t *nr)
1677
1678 {
1679 struct n_tty_data *ldata = tty->disc_data;
1680 int retval;
1681 size_t n;
1682 unsigned long flags;
1683 bool is_eof;
1684
1685 retval = 0;
1686 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1687 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1688 n = min(*nr, n);
1689 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1690 if (n) {
1691 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1692 n -= retval;
1693 is_eof = n == 1 &&
1694 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1695 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1696 ldata->icanon);
1697 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1698 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1699 ldata->read_cnt -= n;
1700 /* Turn single EOF into zero-length read */
1701 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
1702 n = 0;
1703 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1704 *b += n;
1705 *nr -= n;
1706 }
1707 return retval;
1708 }
1709
1710 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1711 size_t, loff_t *);
1712
1713 /**
1714 * job_control - check job control
1715 * @tty: tty
1716 * @file: file handle
1717 *
1718 * Perform job control management checks on this file/tty descriptor
1719 * and if appropriate send any needed signals and return a negative
1720 * error code if action should be taken.
1721 *
1722 * Locking: redirected write test is safe
1723 * current->signal->tty check is safe
1724 * ctrl_lock to safely reference tty->pgrp
1725 */
1726
1727 static int job_control(struct tty_struct *tty, struct file *file)
1728 {
1729 /* Job control check -- must be done at start and after
1730 every sleep (POSIX.1 7.1.1.4). */
1731 /* NOTE: not yet done after every sleep pending a thorough
1732 check of the logic of this change. -- jlc */
1733 /* don't stop on /dev/console */
1734 if (file->f_op->write == redirected_tty_write ||
1735 current->signal->tty != tty)
1736 return 0;
1737
1738 spin_lock_irq(&tty->ctrl_lock);
1739 if (!tty->pgrp)
1740 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1741 else if (task_pgrp(current) != tty->pgrp) {
1742 spin_unlock_irq(&tty->ctrl_lock);
1743 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1744 return -EIO;
1745 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1746 set_thread_flag(TIF_SIGPENDING);
1747 return -ERESTARTSYS;
1748 }
1749 spin_unlock_irq(&tty->ctrl_lock);
1750 return 0;
1751 }
1752
1753
1754 /**
1755 * n_tty_read - read function for tty
1756 * @tty: tty device
1757 * @file: file object
1758 * @buf: userspace buffer pointer
1759 * @nr: size of I/O
1760 *
1761 * Perform reads for the line discipline. We are guaranteed that the
1762 * line discipline will not be closed under us but we may get multiple
1763 * parallel readers and must handle this ourselves. We may also get
1764 * a hangup. Always called in user context, may sleep.
1765 *
1766 * This code must be sure never to sleep through a hangup.
1767 */
1768
1769 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1770 unsigned char __user *buf, size_t nr)
1771 {
1772 struct n_tty_data *ldata = tty->disc_data;
1773 unsigned char __user *b = buf;
1774 DECLARE_WAITQUEUE(wait, current);
1775 int c;
1776 int minimum, time;
1777 ssize_t retval = 0;
1778 ssize_t size;
1779 long timeout;
1780 unsigned long flags;
1781 int packet;
1782
1783 do_it_again:
1784 c = job_control(tty, file);
1785 if (c < 0)
1786 return c;
1787
1788 minimum = time = 0;
1789 timeout = MAX_SCHEDULE_TIMEOUT;
1790 if (!ldata->icanon) {
1791 time = (HZ / 10) * TIME_CHAR(tty);
1792 minimum = MIN_CHAR(tty);
1793 if (minimum) {
1794 if (time)
1795 tty->minimum_to_wake = 1;
1796 else if (!waitqueue_active(&tty->read_wait) ||
1797 (tty->minimum_to_wake > minimum))
1798 tty->minimum_to_wake = minimum;
1799 } else {
1800 timeout = 0;
1801 if (time) {
1802 timeout = time;
1803 time = 0;
1804 }
1805 tty->minimum_to_wake = minimum = 1;
1806 }
1807 }
1808
1809 /*
1810 * Internal serialization of reads.
1811 */
1812 if (file->f_flags & O_NONBLOCK) {
1813 if (!mutex_trylock(&ldata->atomic_read_lock))
1814 return -EAGAIN;
1815 } else {
1816 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1817 return -ERESTARTSYS;
1818 }
1819 packet = tty->packet;
1820
1821 add_wait_queue(&tty->read_wait, &wait);
1822 while (nr) {
1823 /* First test for status change. */
1824 if (packet && tty->link->ctrl_status) {
1825 unsigned char cs;
1826 if (b != buf)
1827 break;
1828 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1829 cs = tty->link->ctrl_status;
1830 tty->link->ctrl_status = 0;
1831 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1832 if (tty_put_user(tty, cs, b++)) {
1833 retval = -EFAULT;
1834 b--;
1835 break;
1836 }
1837 nr--;
1838 break;
1839 }
1840 /* This statement must be first before checking for input
1841 so that any interrupt will set the state back to
1842 TASK_RUNNING. */
1843 set_current_state(TASK_INTERRUPTIBLE);
1844
1845 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1846 ((minimum - (b - buf)) >= 1))
1847 tty->minimum_to_wake = (minimum - (b - buf));
1848
1849 if (!input_available_p(tty, 0)) {
1850 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1851 retval = -EIO;
1852 break;
1853 }
1854 if (tty_hung_up_p(file))
1855 break;
1856 if (!timeout)
1857 break;
1858 if (file->f_flags & O_NONBLOCK) {
1859 retval = -EAGAIN;
1860 break;
1861 }
1862 if (signal_pending(current)) {
1863 retval = -ERESTARTSYS;
1864 break;
1865 }
1866 /* FIXME: does n_tty_set_room need locking ? */
1867 n_tty_set_room(tty);
1868 timeout = schedule_timeout(timeout);
1869 continue;
1870 }
1871 __set_current_state(TASK_RUNNING);
1872
1873 /* Deal with packet mode. */
1874 if (packet && b == buf) {
1875 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1876 retval = -EFAULT;
1877 b--;
1878 break;
1879 }
1880 nr--;
1881 }
1882
1883 if (ldata->icanon && !L_EXTPROC(tty)) {
1884 /* N.B. avoid overrun if nr == 0 */
1885 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1886 while (nr && ldata->read_cnt) {
1887 int eol;
1888
1889 eol = test_and_clear_bit(ldata->read_tail,
1890 ldata->read_flags);
1891 c = ldata->read_buf[ldata->read_tail];
1892 ldata->read_tail = ((ldata->read_tail+1) &
1893 (N_TTY_BUF_SIZE-1));
1894 ldata->read_cnt--;
1895 if (eol) {
1896 /* this test should be redundant:
1897 * we shouldn't be reading data if
1898 * canon_data is 0
1899 */
1900 if (--ldata->canon_data < 0)
1901 ldata->canon_data = 0;
1902 }
1903 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1904
1905 if (!eol || (c != __DISABLED_CHAR)) {
1906 if (tty_put_user(tty, c, b++)) {
1907 retval = -EFAULT;
1908 b--;
1909 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1910 break;
1911 }
1912 nr--;
1913 }
1914 if (eol) {
1915 tty_audit_push(tty);
1916 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1917 break;
1918 }
1919 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1920 }
1921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1922 if (retval)
1923 break;
1924 } else {
1925 int uncopied;
1926 /* The copy function takes the read lock and handles
1927 locking internally for this case */
1928 uncopied = copy_from_read_buf(tty, &b, &nr);
1929 uncopied += copy_from_read_buf(tty, &b, &nr);
1930 if (uncopied) {
1931 retval = -EFAULT;
1932 break;
1933 }
1934 }
1935
1936 /* If there is enough space in the read buffer now, let the
1937 * low-level driver know. We use n_tty_chars_in_buffer() to
1938 * check the buffer, as it now knows about canonical mode.
1939 * Otherwise, if the driver is throttled and the line is
1940 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1941 * we won't get any more characters.
1942 */
1943 while (1) {
1944 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1945 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1946 break;
1947 if (!tty->count)
1948 break;
1949 n_tty_set_room(tty);
1950 if (!tty_unthrottle_safe(tty))
1951 break;
1952 }
1953 __tty_set_flow_change(tty, 0);
1954
1955 if (b - buf >= minimum)
1956 break;
1957 if (time)
1958 timeout = time;
1959 }
1960 mutex_unlock(&ldata->atomic_read_lock);
1961 remove_wait_queue(&tty->read_wait, &wait);
1962
1963 if (!waitqueue_active(&tty->read_wait))
1964 tty->minimum_to_wake = minimum;
1965
1966 __set_current_state(TASK_RUNNING);
1967 size = b - buf;
1968 if (size) {
1969 retval = size;
1970 if (nr)
1971 clear_bit(TTY_PUSH, &tty->flags);
1972 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1973 goto do_it_again;
1974
1975 n_tty_set_room(tty);
1976 return retval;
1977 }
1978
1979 /**
1980 * n_tty_write - write function for tty
1981 * @tty: tty device
1982 * @file: file object
1983 * @buf: userspace buffer pointer
1984 * @nr: size of I/O
1985 *
1986 * Write function of the terminal device. This is serialized with
1987 * respect to other write callers but not to termios changes, reads
1988 * and other such events. Since the receive code will echo characters,
1989 * thus calling driver write methods, the output_lock is used in
1990 * the output processing functions called here as well as in the
1991 * echo processing function to protect the column state and space
1992 * left in the buffer.
1993 *
1994 * This code must be sure never to sleep through a hangup.
1995 *
1996 * Locking: output_lock to protect column state and space left
1997 * (note that the process_output*() functions take this
1998 * lock themselves)
1999 */
2000
2001 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2002 const unsigned char *buf, size_t nr)
2003 {
2004 const unsigned char *b = buf;
2005 DECLARE_WAITQUEUE(wait, current);
2006 int c;
2007 ssize_t retval = 0;
2008
2009 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2010 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2011 retval = tty_check_change(tty);
2012 if (retval)
2013 return retval;
2014 }
2015
2016 /* Write out any echoed characters that are still pending */
2017 process_echoes(tty);
2018
2019 add_wait_queue(&tty->write_wait, &wait);
2020 while (1) {
2021 set_current_state(TASK_INTERRUPTIBLE);
2022 if (signal_pending(current)) {
2023 retval = -ERESTARTSYS;
2024 break;
2025 }
2026 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2027 retval = -EIO;
2028 break;
2029 }
2030 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2031 while (nr > 0) {
2032 ssize_t num = process_output_block(tty, b, nr);
2033 if (num < 0) {
2034 if (num == -EAGAIN)
2035 break;
2036 retval = num;
2037 goto break_out;
2038 }
2039 b += num;
2040 nr -= num;
2041 if (nr == 0)
2042 break;
2043 c = *b;
2044 if (process_output(c, tty) < 0)
2045 break;
2046 b++; nr--;
2047 }
2048 if (tty->ops->flush_chars)
2049 tty->ops->flush_chars(tty);
2050 } else {
2051 while (nr > 0) {
2052 c = tty->ops->write(tty, b, nr);
2053 if (c < 0) {
2054 retval = c;
2055 goto break_out;
2056 }
2057 if (!c)
2058 break;
2059 b += c;
2060 nr -= c;
2061 }
2062 }
2063 if (!nr)
2064 break;
2065 if (file->f_flags & O_NONBLOCK) {
2066 retval = -EAGAIN;
2067 break;
2068 }
2069 schedule();
2070 }
2071 break_out:
2072 __set_current_state(TASK_RUNNING);
2073 remove_wait_queue(&tty->write_wait, &wait);
2074 if (b - buf != nr && tty->fasync)
2075 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2076 return (b - buf) ? b - buf : retval;
2077 }
2078
2079 /**
2080 * n_tty_poll - poll method for N_TTY
2081 * @tty: terminal device
2082 * @file: file accessing it
2083 * @wait: poll table
2084 *
2085 * Called when the line discipline is asked to poll() for data or
2086 * for special events. This code is not serialized with respect to
2087 * other events save open/close.
2088 *
2089 * This code must be sure never to sleep through a hangup.
2090 * Called without the kernel lock held - fine
2091 */
2092
2093 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2094 poll_table *wait)
2095 {
2096 unsigned int mask = 0;
2097
2098 poll_wait(file, &tty->read_wait, wait);
2099 poll_wait(file, &tty->write_wait, wait);
2100 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2101 mask |= POLLIN | POLLRDNORM;
2102 if (tty->packet && tty->link->ctrl_status)
2103 mask |= POLLPRI | POLLIN | POLLRDNORM;
2104 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2105 mask |= POLLHUP;
2106 if (tty_hung_up_p(file))
2107 mask |= POLLHUP;
2108 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2109 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2110 tty->minimum_to_wake = MIN_CHAR(tty);
2111 else
2112 tty->minimum_to_wake = 1;
2113 }
2114 if (tty->ops->write && !tty_is_writelocked(tty) &&
2115 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2116 tty_write_room(tty) > 0)
2117 mask |= POLLOUT | POLLWRNORM;
2118 return mask;
2119 }
2120
2121 static unsigned long inq_canon(struct n_tty_data *ldata)
2122 {
2123 int nr, head, tail;
2124
2125 if (!ldata->canon_data)
2126 return 0;
2127 head = ldata->canon_head;
2128 tail = ldata->read_tail;
2129 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2130 /* Skip EOF-chars.. */
2131 while (head != tail) {
2132 if (test_bit(tail, ldata->read_flags) &&
2133 ldata->read_buf[tail] == __DISABLED_CHAR)
2134 nr--;
2135 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2136 }
2137 return nr;
2138 }
2139
2140 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2141 unsigned int cmd, unsigned long arg)
2142 {
2143 struct n_tty_data *ldata = tty->disc_data;
2144 int retval;
2145
2146 switch (cmd) {
2147 case TIOCOUTQ:
2148 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2149 case TIOCINQ:
2150 /* FIXME: Locking */
2151 retval = ldata->read_cnt;
2152 if (L_ICANON(tty))
2153 retval = inq_canon(ldata);
2154 return put_user(retval, (unsigned int __user *) arg);
2155 default:
2156 return n_tty_ioctl_helper(tty, file, cmd, arg);
2157 }
2158 }
2159
2160 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2161 .magic = TTY_LDISC_MAGIC,
2162 .name = "n_tty",
2163 .open = n_tty_open,
2164 .close = n_tty_close,
2165 .flush_buffer = n_tty_flush_buffer,
2166 .chars_in_buffer = n_tty_chars_in_buffer,
2167 .read = n_tty_read,
2168 .write = n_tty_write,
2169 .ioctl = n_tty_ioctl,
2170 .set_termios = n_tty_set_termios,
2171 .poll = n_tty_poll,
2172 .receive_buf = n_tty_receive_buf,
2173 .write_wakeup = n_tty_write_wakeup
2174 };
2175
2176 /**
2177 * n_tty_inherit_ops - inherit N_TTY methods
2178 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2179 *
2180 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2181 * methods.
2182 */
2183
2184 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2185 {
2186 *ops = tty_ldisc_N_TTY;
2187 ops->owner = NULL;
2188 ops->refcount = ops->flags = 0;
2189 }
2190 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
This page took 0.137703 seconds and 6 git commands to generate.