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