fbui | Framebuffer-based graphical environment |
| download: https://git.y1.nz/archives/fbui.tar.gz | |
| README | Files | Log | Refs |
drivers/char/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 write_chan returns
30 * EAGAIN
31 *
32 * 2004/09/20 Z Smith (fbui@comcast.net): erase-after-read of read_buf
33 */
34
35 #include <linux/types.h>
36 #include <linux/major.h>
37 #include <linux/errno.h>
38 #include <linux/signal.h>
39 #include <linux/fcntl.h>
40 #include <linux/sched.h>
41 #include <linux/interrupt.h>
42 #include <linux/tty.h>
43 #include <linux/timer.h>
44 #include <linux/ctype.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/slab.h>
48 #include <linux/poll.h>
49
50 #include <asm/uaccess.h>
51 #include <asm/system.h>
52 #include <asm/bitops.h>
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 static inline unsigned char *alloc_buf(void)
66 {
67 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
68
69 if (PAGE_SIZE != N_TTY_BUF_SIZE)
70 return kmalloc(N_TTY_BUF_SIZE, prio);
71 else
72 return (unsigned char *)__get_free_page(prio);
73 }
74
75 static inline void free_buf(unsigned char *buf)
76 {
77 if (PAGE_SIZE != N_TTY_BUF_SIZE)
78 kfree(buf);
79 else
80 free_page((unsigned long) buf);
81 }
82
83 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
84 {
85 if (tty->read_cnt < N_TTY_BUF_SIZE) {
86 tty->read_buf[tty->read_head] = c;
87 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
88 tty->read_cnt++;
89 }
90 }
91
92 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
93 {
94 unsigned long flags;
95 /*
96 * The problem of stomping on the buffers ends here.
97 * Why didn't anyone see this one coming? --AJK
98 */
99 spin_lock_irqsave(&tty->read_lock, flags);
100 put_tty_queue_nolock(c, tty);
101 spin_unlock_irqrestore(&tty->read_lock, flags);
102 }
103
104 /**
105 * check_unthrottle - allow new receive data
106 * @tty; tty device
107 *
108 * Check whether to call the driver.unthrottle function.
109 * We test the TTY_THROTTLED bit first so that it always
110 * indicates the current state. The decision about whether
111 * it is worth allowing more input has been taken by the caller.
112 * Can sleep, may be called under the atomic_read semaphore but
113 * this is not guaranteed.
114 */
115
116 static void check_unthrottle(struct tty_struct * tty)
117 {
118 if (tty->count &&
119 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
120 tty->driver->unthrottle)
121 tty->driver->unthrottle(tty);
122 }
123
124 /**
125 * reset_buffer_flags - reset buffer state
126 * @tty: terminal to reset
127 *
128 * Reset the read buffer counters, clear the flags,
129 * and make sure the driver is unthrottled. Called
130 * from n_tty_open() and n_tty_flush_buffer().
131 */
132 static void reset_buffer_flags(struct tty_struct *tty)
133 {
134 unsigned long flags;
135
136 spin_lock_irqsave(&tty->read_lock, flags);
137 tty->read_head = tty->read_tail = tty->read_cnt = 0;
138 spin_unlock_irqrestore(&tty->read_lock, flags);
139 tty->canon_head = tty->canon_data = tty->erasing = 0;
140 memset(&tty->read_flags, 0, sizeof tty->read_flags);
141 check_unthrottle(tty);
142 }
143
144 /**
145 * n_tty_flush_buffer - clean input queue
146 * @tty: terminal device
147 *
148 * Flush the input buffer. Called when the line discipline is
149 * being closed, when the tty layer wants the buffer flushed (eg
150 * at hangup) or when the N_TTY line discipline internally has to
151 * clean the pending queue (for example some signals).
152 *
153 * FIXME: tty->ctrl_status is not spinlocked and relies on
154 * lock_kernel() still.
155 */
156
157 void n_tty_flush_buffer(struct tty_struct * tty)
158 {
159 /* clear everything and unthrottle the driver */
160 reset_buffer_flags(tty);
161
162 if (!tty->link)
163 return;
164
165 if (tty->link->packet) {
166 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
167 wake_up_interruptible(&tty->link->read_wait);
168 }
169 }
170
171 /**
172 * n_tty_chars_in_buffer - report available bytes
173 * @tty: tty device
174 *
175 * Report the number of characters buffered to be delivered to user
176 * at this instant in time.
177 */
178
179 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
180 {
181 unsigned long flags;
182 ssize_t n = 0;
183
184 spin_lock_irqsave(&tty->read_lock, flags);
185 if (!tty->icanon) {
186 n = tty->read_cnt;
187 } else if (tty->canon_data) {
188 n = (tty->canon_head > tty->read_tail) ?
189 tty->canon_head - tty->read_tail :
190 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
191 }
192 spin_unlock_irqrestore(&tty->read_lock, flags);
193 return n;
194 }
195
196 /**
197 * is_utf8_continuation - utf8 multibyte check
198 * @c: byte to check
199 *
200 * Returns true if the utf8 character 'c' is a multibyte continuation
201 * character. We use this to correctly compute the on screen size
202 * of the character when printing
203 */
204
205 static inline int is_utf8_continuation(unsigned char c)
206 {
207 return (c & 0xc0) == 0x80;
208 }
209
210 /**
211 * is_continuation - multibyte check
212 * @c: byte to check
213 *
214 * Returns true if the utf8 character 'c' is a multibyte continuation
215 * character and the terminal is in unicode mode.
216 */
217
218 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
219 {
220 return I_IUTF8(tty) && is_utf8_continuation(c);
221 }
222
223 /**
224 * opost - output post processor
225 * @c: character (or partial unicode symbol)
226 * @tty: terminal device
227 *
228 * Perform OPOST processing. Returns -1 when the output device is
229 * full and the character must be retried. Note that Linux currently
230 * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
231 * relevant in the world today. If you ever need them, add them here.
232 *
233 * Called from both the receive and transmit sides and can be called
234 * re-entrantly. Relies on lock_kernel() still.
235 */
236
237 static int opost(unsigned char c, struct tty_struct *tty)
238 {
239 int space, spaces;
240
241 space = tty->driver->write_room(tty);
242 if (!space)
243 return -1;
244
245 if (O_OPOST(tty)) {
246 switch (c) {
247 case '\n':
248 if (O_ONLRET(tty))
249 tty->column = 0;
250 if (O_ONLCR(tty)) {
251 if (space < 2)
252 return -1;
253 tty->driver->put_char(tty, '\r');
254 tty->column = 0;
255 }
256 tty->canon_column = tty->column;
257 break;
258 case '\r':
259 if (O_ONOCR(tty) && tty->column == 0)
260 return 0;
261 if (O_OCRNL(tty)) {
262 c = '\n';
263 if (O_ONLRET(tty))
264 tty->canon_column = tty->column = 0;
265 break;
266 }
267 tty->canon_column = tty->column = 0;
268 break;
269 case '\t':
270 spaces = 8 - (tty->column & 7);
271 if (O_TABDLY(tty) == XTABS) {
272 if (space < spaces)
273 return -1;
274 tty->column += spaces;
275 tty->driver->write(tty, 0, " ", spaces);
276 return 0;
277 }
278 tty->column += spaces;
279 break;
280 case '\b':
281 if (tty->column > 0)
282 tty->column--;
283 break;
284 default:
285 if (O_OLCUC(tty))
286 c = toupper(c);
287 if (!iscntrl(c) && !is_continuation(c, tty))
288 tty->column++;
289 break;
290 }
291 }
292 tty->driver->put_char(tty, c);
293 return 0;
294 }
295
296 /**
297 * opost_block - block postprocess
298 * @tty: terminal device
299 * @inbuf: user buffer
300 * @nr: number of bytes
301 *
302 * This path is used to speed up block console writes, among other
303 * things when processing blocks of output data. It handles only
304 * the simple cases normally found and helps to generate blocks of
305 * symbols for the console driver and thus improve performance.
306 *
307 * Called from write_chan under the tty layer write lock.
308 */
309
310 static ssize_t opost_block(struct tty_struct * tty,
311 const unsigned char __user * inbuf, unsigned int nr)
312 {
313 char buf[80];
314 int space;
315 int i;
316 char *cp;
317
318 space = tty->driver->write_room(tty);
319 if (!space)
320 return 0;
321 if (nr > space)
322 nr = space;
323 if (nr > sizeof(buf))
324 nr = sizeof(buf);
325
326 if (copy_from_user(buf, inbuf, nr))
327 return -EFAULT;
328
329 for (i = 0, cp = buf; i < nr; i++, cp++) {
330 switch (*cp) {
331 case '\n':
332 if (O_ONLRET(tty))
333 tty->column = 0;
334 if (O_ONLCR(tty))
335 goto break_out;
336 tty->canon_column = tty->column;
337 break;
338 case '\r':
339 if (O_ONOCR(tty) && tty->column == 0)
340 goto break_out;
341 if (O_OCRNL(tty)) {
342 *cp = '\n';
343 if (O_ONLRET(tty))
344 tty->canon_column = tty->column = 0;
345 break;
346 }
347 tty->canon_column = tty->column = 0;
348 break;
349 case '\t':
350 goto break_out;
351 case '\b':
352 if (tty->column > 0)
353 tty->column--;
354 break;
355 default:
356 if (O_OLCUC(tty))
357 *cp = toupper(*cp);
358 if (!iscntrl(*cp))
359 tty->column++;
360 break;
361 }
362 }
363 break_out:
364 if (tty->driver->flush_chars)
365 tty->driver->flush_chars(tty);
366 i = tty->driver->write(tty, 0, buf, i);
367 return i;
368 }
369
370
371 /**
372 * put_char - write character to driver
373 * @c: character (or part of unicode symbol)
374 * @tty: terminal device
375 *
376 * Queue a byte to the driver layer for output
377 */
378
379 static inline void put_char(unsigned char c, struct tty_struct *tty)
380 {
381 tty->driver->put_char(tty, c);
382 }
383
384 /**
385 * echo_char - echo characters
386 * @c: unicode byte to echo
387 * @tty: terminal device
388 *
389 * Echo user input back onto the screen. This must be called only when
390 * L_ECHO(tty) is true. Called from the driver receive_buf path.
391 */
392
393 static void echo_char(unsigned char c, struct tty_struct *tty)
394 {
395 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
396 put_char('^', tty);
397 put_char(c ^ 0100, tty);
398 tty->column += 2;
399 } else
400 opost(c, tty);
401 }
402
403 static inline void finish_erasing(struct tty_struct *tty)
404 {
405 if (tty->erasing) {
406 put_char('/', tty);
407 tty->column++;
408 tty->erasing = 0;
409 }
410 }
411
412 /**
413 * eraser - handle erase function
414 * @c: character input
415 * @tty: terminal device
416 *
417 * Perform erase and neccessary output when an erase character is
418 * present in the stream from the driver layer. Handles the complexities
419 * of UTF-8 multibyte symbols.
420 */
421
422 static void eraser(unsigned char c, struct tty_struct *tty)
423 {
424 enum { ERASE, WERASE, KILL } kill_type;
425 int head, seen_alnums, cnt;
426 unsigned long flags;
427
428 if (tty->read_head == tty->canon_head) {
429 /* opost('\a', tty); */ /* what do you think? */
430 return;
431 }
432 if (c == ERASE_CHAR(tty))
433 kill_type = ERASE;
434 else if (c == WERASE_CHAR(tty))
435 kill_type = WERASE;
436 else {
437 if (!L_ECHO(tty)) {
438 spin_lock_irqsave(&tty->read_lock, flags);
439 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
440 (N_TTY_BUF_SIZE - 1));
441 tty->read_head = tty->canon_head;
442 spin_unlock_irqrestore(&tty->read_lock, flags);
443 return;
444 }
445 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
446 spin_lock_irqsave(&tty->read_lock, flags);
447 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
448 (N_TTY_BUF_SIZE - 1));
449 tty->read_head = tty->canon_head;
450 spin_unlock_irqrestore(&tty->read_lock, flags);
451 finish_erasing(tty);
452 echo_char(KILL_CHAR(tty), tty);
453 /* Add a newline if ECHOK is on and ECHOKE is off. */
454 if (L_ECHOK(tty))
455 opost('\n', tty);
456 return;
457 }
458 kill_type = KILL;
459 }
460
461 seen_alnums = 0;
462 while (tty->read_head != tty->canon_head) {
463 head = tty->read_head;
464
465 /* erase a single possibly multibyte character */
466 do {
467 head = (head - 1) & (N_TTY_BUF_SIZE-1);
468 c = tty->read_buf[head];
469 tty->read_buf[head] = 0; /* ZS */
470 } while (is_continuation(c, tty) && head != tty->canon_head);
471
472 /* do not partially erase */
473 if (is_continuation(c, tty))
474 break;
475
476 if (kill_type == WERASE) {
477 /* Equivalent to BSD's ALTWERASE. */
478 if (isalnum(c) || c == '_')
479 seen_alnums++;
480 else if (seen_alnums)
481 break;
482 }
483 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
484 spin_lock_irqsave(&tty->read_lock, flags);
485 tty->read_head = head;
486 tty->read_cnt -= cnt;
487 spin_unlock_irqrestore(&tty->read_lock, flags);
488 if (L_ECHO(tty)) {
489 if (L_ECHOPRT(tty)) {
490 if (!tty->erasing) {
491 put_char('\\', tty);
492 tty->column++;
493 tty->erasing = 1;
494 }
495 /* if cnt > 1, output a multi-byte character */
496 echo_char(c, tty);
497 while (--cnt > 0) {
498 head = (head+1) & (N_TTY_BUF_SIZE-1);
499 put_char(tty->read_buf[head], tty);
500 }
501 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
502 echo_char(ERASE_CHAR(tty), tty);
503 } else if (c == '\t') {
504 unsigned int col = tty->canon_column;
505 unsigned long tail = tty->canon_head;
506
507 /* Find the column of the last char. */
508 while (tail != tty->read_head) {
509 c = tty->read_buf[tail];
510 tty->read_buf[tail] = 0; /* ZS */
511 if (c == '\t')
512 col = (col | 7) + 1;
513 else if (iscntrl(c)) {
514 if (L_ECHOCTL(tty))
515 col += 2;
516 } else if (!is_continuation(c, tty))
517 col++;
518 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
519 }
520
521 /* should never happen */
522 if (tty->column > 0x80000000)
523 tty->column = 0;
524
525 /* Now backup to that column. */
526 while (tty->column > col) {
527 /* Can't use opost here. */
528 put_char('\b', tty);
529 if (tty->column > 0)
530 tty->column--;
531 }
532 } else {
533 if (iscntrl(c) && L_ECHOCTL(tty)) {
534 put_char('\b', tty);
535 put_char(' ', tty);
536 put_char('\b', tty);
537 if (tty->column > 0)
538 tty->column--;
539 }
540 if (!iscntrl(c) || L_ECHOCTL(tty)) {
541 put_char('\b', tty);
542 put_char(' ', tty);
543 put_char('\b', tty);
544 if (tty->column > 0)
545 tty->column--;
546 }
547 }
548 }
549 if (kill_type == ERASE)
550 break;
551 }
552 if (tty->read_head == tty->canon_head)
553 finish_erasing(tty);
554 }
555
556 /**
557 * isig - handle the ISIG optio
558 * @sig: signal
559 * @tty: terminal
560 * @flush: force flush
561 *
562 * Called when a signal is being sent due to terminal input. This
563 * may caus terminal flushing to take place according to the termios
564 * settings and character used. Called from the driver receive_buf
565 * path so serialized.
566 */
567
568 static inline void isig(int sig, struct tty_struct *tty, int flush)
569 {
570 if (tty->pgrp > 0)
571 kill_pg(tty->pgrp, sig, 1);
572 if (flush || !L_NOFLSH(tty)) {
573 n_tty_flush_buffer(tty);
574 if (tty->driver->flush_buffer)
575 tty->driver->flush_buffer(tty);
576 }
577 }
578
579 /**
580 * n_tty_receive_break - handle break
581 * @tty: terminal
582 *
583 * An RS232 break event has been hit in the incoming bitstream. This
584 * can cause a variety of events depending upon the termios settings.
585 *
586 * Called from the receive_buf path so single threaded.
587 */
588
589 static inline void n_tty_receive_break(struct tty_struct *tty)
590 {
591 if (I_IGNBRK(tty))
592 return;
593 if (I_BRKINT(tty)) {
594 isig(SIGINT, tty, 1);
595 return;
596 }
597 if (I_PARMRK(tty)) {
598 put_tty_queue('\377', tty);
599 put_tty_queue('\0', tty);
600 }
601 put_tty_queue('\0', tty);
602 wake_up_interruptible(&tty->read_wait);
603 }
604
605 /**
606 * n_tty_receive_overrun - handle overrun reporting
607 * @tty: terminal
608 *
609 * Data arrived faster than we could process it. While the tty
610 * driver has flagged this the bits that were missed are gone
611 * forever.
612 *
613 * Called from the receive_buf path so single threaded. Does not
614 * need locking as num_overrun and overrun_time are function
615 * private.
616 */
617
618 static inline void n_tty_receive_overrun(struct tty_struct *tty)
619 {
620 char buf[64];
621
622 tty->num_overrun++;
623 if (time_before(tty->overrun_time, jiffies - HZ)) {
624 printk(KERN_WARNING "%s: %d input overrun(s)\n", tty_name(tty, buf),
625 tty->num_overrun);
626 tty->overrun_time = jiffies;
627 tty->num_overrun = 0;
628 }
629 }
630
631 /**
632 * n_tty_receive_parity_error - error notifier
633 * @tty: terminal device
634 * @c: character
635 *
636 * Process a parity error and queue the right data to indicate
637 * the error case if neccessary. Locking as per n_tty_receive_buf.
638 */
639 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
640 unsigned char c)
641 {
642 if (I_IGNPAR(tty)) {
643 return;
644 }
645 if (I_PARMRK(tty)) {
646 put_tty_queue('\377', tty);
647 put_tty_queue('\0', tty);
648 put_tty_queue(c, tty);
649 } else if (I_INPCK(tty))
650 put_tty_queue('\0', tty);
651 else
652 put_tty_queue(c, tty);
653 wake_up_interruptible(&tty->read_wait);
654 }
655
656 /**
657 * n_tty_receive_char - perform processing
658 * @tty: terminal device
659 * @c: character
660 *
661 * Process an individual character of input received from the driver.
662 * This is serialized with respect to itself by the rules for the
663 * driver above.
664 */
665
666 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
667 {
668 unsigned long flags;
669
670 if (tty->raw) {
671 put_tty_queue(c, tty);
672 return;
673 }
674
675 if (tty->stopped && !tty->flow_stopped &&
676 I_IXON(tty) && I_IXANY(tty)) {
677 start_tty(tty);
678 return;
679 }
680
681 if (I_ISTRIP(tty))
682 c &= 0x7f;
683 if (I_IUCLC(tty) && L_IEXTEN(tty))
684 c=tolower(c);
685
686 if (tty->closing) {
687 if (I_IXON(tty)) {
688 if (c == START_CHAR(tty))
689 start_tty(tty);
690 else if (c == STOP_CHAR(tty))
691 stop_tty(tty);
692 }
693 return;
694 }
695
696 /*
697 * If the previous character was LNEXT, or we know that this
698 * character is not one of the characters that we'll have to
699 * handle specially, do shortcut processing to speed things
700 * up.
701 */
702 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
703 finish_erasing(tty);
704 tty->lnext = 0;
705 if (L_ECHO(tty)) {
706 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
707 put_char('\a', tty); /* beep if no space */
708 return;
709 }
710 /* Record the column of first canon char. */
711 if (tty->canon_head == tty->read_head)
712 tty->canon_column = tty->column;
713 echo_char(c, tty);
714 }
715 if (I_PARMRK(tty) && c == (unsigned char) '\377')
716 put_tty_queue(c, tty);
717 put_tty_queue(c, tty);
718 return;
719 }
720
721 if (c == '\r') {
722 if (I_IGNCR(tty))
723 return;
724 if (I_ICRNL(tty))
725 c = '\n';
726 } else if (c == '\n' && I_INLCR(tty))
727 c = '\r';
728 if (I_IXON(tty)) {
729 if (c == START_CHAR(tty)) {
730 start_tty(tty);
731 return;
732 }
733 if (c == STOP_CHAR(tty)) {
734 stop_tty(tty);
735 return;
736 }
737 }
738 if (L_ISIG(tty)) {
739 int signal;
740 signal = SIGINT;
741 if (c == INTR_CHAR(tty))
742 goto send_signal;
743 signal = SIGQUIT;
744 if (c == QUIT_CHAR(tty))
745 goto send_signal;
746 signal = SIGTSTP;
747 if (c == SUSP_CHAR(tty)) {
748 send_signal:
749 isig(signal, tty, 0);
750 return;
751 }
752 }
753 if (tty->icanon) {
754 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
755 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
756 eraser(c, tty);
757 return;
758 }
759 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
760 tty->lnext = 1;
761 if (L_ECHO(tty)) {
762 finish_erasing(tty);
763 if (L_ECHOCTL(tty)) {
764 put_char('^', tty);
765 put_char('\b', tty);
766 }
767 }
768 return;
769 }
770 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
771 L_IEXTEN(tty)) {
772 unsigned long tail = tty->canon_head;
773
774 finish_erasing(tty);
775 echo_char(c, tty);
776 opost('\n', tty);
777 while (tail != tty->read_head) {
778 echo_char(tty->read_buf[tail], tty);
779 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
780 }
781 return;
782 }
783 if (c == '\n') {
784 if (L_ECHO(tty) || L_ECHONL(tty)) {
785 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
786 put_char('\a', tty);
787 return;
788 }
789 opost('\n', tty);
790 }
791 goto handle_newline;
792 }
793 if (c == EOF_CHAR(tty)) {
794 if (tty->canon_head != tty->read_head)
795 set_bit(TTY_PUSH, &tty->flags);
796 c = __DISABLED_CHAR;
797 goto handle_newline;
798 }
799 if ((c == EOL_CHAR(tty)) ||
800 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
801 /*
802 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
803 */
804 if (L_ECHO(tty)) {
805 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
806 put_char('\a', tty);
807 return;
808 }
809 /* Record the column of first canon char. */
810 if (tty->canon_head == tty->read_head)
811 tty->canon_column = tty->column;
812 echo_char(c, tty);
813 }
814 /*
815 * XXX does PARMRK doubling happen for
816 * EOL_CHAR and EOL2_CHAR?
817 */
818 if (I_PARMRK(tty) && c == (unsigned char) '\377')
819 put_tty_queue(c, tty);
820
821 handle_newline:
822 spin_lock_irqsave(&tty->read_lock, flags);
823 set_bit(tty->read_head, tty->read_flags);
824 put_tty_queue_nolock(c, tty);
825 tty->canon_head = tty->read_head;
826 tty->canon_data++;
827 spin_unlock_irqrestore(&tty->read_lock, flags);
828 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
829 if (waitqueue_active(&tty->read_wait))
830 wake_up_interruptible(&tty->read_wait);
831 return;
832 }
833 }
834
835 finish_erasing(tty);
836 if (L_ECHO(tty)) {
837 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
838 put_char('\a', tty); /* beep if no space */
839 return;
840 }
841 if (c == '\n')
842 opost('\n', tty);
843 else {
844 /* Record the column of first canon char. */
845 if (tty->canon_head == tty->read_head)
846 tty->canon_column = tty->column;
847 echo_char(c, tty);
848 }
849 }
850
851 if (I_PARMRK(tty) && c == (unsigned char) '\377')
852 put_tty_queue(c, tty);
853
854 put_tty_queue(c, tty);
855 }
856
857 /**
858 * n_tty_receive_room - receive space
859 * @tty: terminal
860 *
861 * Called by the driver to find out how much data it is
862 * permitted to feed to the line discipline without any being lost
863 * and thus to manage flow control. Not serialized. Answers for the
864 * "instant".
865 */
866
867 static int n_tty_receive_room(struct tty_struct *tty)
868 {
869 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
870
871 /*
872 * If we are doing input canonicalization, and there are no
873 * pending newlines, let characters through without limit, so
874 * that erase characters will be handled. Other excess
875 * characters will be beeped.
876 */
877 if (tty->icanon && !tty->canon_data)
878 return N_TTY_BUF_SIZE;
879
880 if (left > 0)
881 return left;
882 return 0;
883 }
884
885 /**
886 * n_tty_write_wakeup - asynchronous I/O notifier
887 * @tty: tty device
888 *
889 * Required for the ptys, serial driver etc. since processes
890 * that attach themselves to the master and rely on ASYNC
891 * IO must be woken up
892 */
893
894 static void n_tty_write_wakeup(struct tty_struct *tty)
895 {
896 if (tty->fasync)
897 {
898 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
899 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
900 }
901 return;
902 }
903
904 /**
905 * n_tty_receive_buf - data receive
906 * @tty: terminal device
907 * @cp: buffer
908 * @fp: flag buffer
909 * @count: characters
910 *
911 * Called by the terminal driver when a block of characters has
912 * been received. This function must be called from soft contexts
913 * not from interrupt context. The driver is responsible for making
914 * calls one at a time and in order (or using flush_to_ldisc)
915 */
916
917 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
918 char *fp, int count)
919 {
920 const unsigned char *p;
921 char *f, flags = TTY_NORMAL;
922 int i;
923 char buf[64];
924 unsigned long cpuflags;
925
926 if (!tty->read_buf)
927 return;
928
929 if (tty->real_raw) {
930 spin_lock_irqsave(&tty->read_lock, cpuflags);
931 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
932 N_TTY_BUF_SIZE - tty->read_head);
933 i = min(count, i);
934 memcpy(tty->read_buf + tty->read_head, cp, i);
935 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
936 tty->read_cnt += i;
937 cp += i;
938 count -= i;
939
940 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
941 N_TTY_BUF_SIZE - tty->read_head);
942 i = min(count, i);
943 memcpy(tty->read_buf + tty->read_head, cp, i);
944 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
945 tty->read_cnt += i;
946 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
947 } else {
948 for (i=count, p = cp, f = fp; i; i--, p++) {
949 if (f)
950 flags = *f++;
951 switch (flags) {
952 case TTY_NORMAL:
953 n_tty_receive_char(tty, *p);
954 break;
955 case TTY_BREAK:
956 n_tty_receive_break(tty);
957 break;
958 case TTY_PARITY:
959 case TTY_FRAME:
960 n_tty_receive_parity_error(tty, *p);
961 break;
962 case TTY_OVERRUN:
963 n_tty_receive_overrun(tty);
964 break;
965 default:
966 printk("%s: unknown flag %d\n",
967 tty_name(tty, buf), flags);
968 break;
969 }
970 }
971 if (tty->driver->flush_chars)
972 tty->driver->flush_chars(tty);
973 }
974
975 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
976 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
977 if (waitqueue_active(&tty->read_wait))
978 wake_up_interruptible(&tty->read_wait);
979 }
980
981 /*
982 * Check the remaining room for the input canonicalization
983 * mode. We don't want to throttle the driver if we're in
984 * canonical mode and don't have a newline yet!
985 */
986 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
987 /* check TTY_THROTTLED first so it indicates our state */
988 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
989 tty->driver->throttle)
990 tty->driver->throttle(tty);
991 }
992 }
993
994 int is_ignored(int sig)
995 {
996 return (sigismember(¤t->blocked, sig) ||
997 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
998 }
999
1000 /**
1001 * n_tty_set_termios - termios data changed
1002 * @tty: terminal
1003 * @old: previous data
1004 *
1005 * Called by the tty layer when the user changes termios flags so
1006 * that the line discipline can plan ahead. This function cannot sleep
1007 * and is protected from re-entry by the tty layer. The user is
1008 * guaranteed that this function will not be re-entered or in progress
1009 * when the ldisc is closed.
1010 */
1011
1012 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
1013 {
1014 if (!tty)
1015 return;
1016
1017 tty->icanon = (L_ICANON(tty) != 0);
1018 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1019 tty->raw = 1;
1020 tty->real_raw = 1;
1021 return;
1022 }
1023 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1024 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1025 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1026 I_PARMRK(tty)) {
1027 memset(tty->process_char_map, 0, 256/8);
1028
1029 if (I_IGNCR(tty) || I_ICRNL(tty))
1030 set_bit('\r', tty->process_char_map);
1031 if (I_INLCR(tty))
1032 set_bit('\n', tty->process_char_map);
1033
1034 if (L_ICANON(tty)) {
1035 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1036 set_bit(KILL_CHAR(tty), tty->process_char_map);
1037 set_bit(EOF_CHAR(tty), tty->process_char_map);
1038 set_bit('\n', tty->process_char_map);
1039 set_bit(EOL_CHAR(tty), tty->process_char_map);
1040 if (L_IEXTEN(tty)) {
1041 set_bit(WERASE_CHAR(tty),
1042 tty->process_char_map);
1043 set_bit(LNEXT_CHAR(tty),
1044 tty->process_char_map);
1045 set_bit(EOL2_CHAR(tty),
1046 tty->process_char_map);
1047 if (L_ECHO(tty))
1048 set_bit(REPRINT_CHAR(tty),
1049 tty->process_char_map);
1050 }
1051 }
1052 if (I_IXON(tty)) {
1053 set_bit(START_CHAR(tty), tty->process_char_map);
1054 set_bit(STOP_CHAR(tty), tty->process_char_map);
1055 }
1056 if (L_ISIG(tty)) {
1057 set_bit(INTR_CHAR(tty), tty->process_char_map);
1058 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1059 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1060 }
1061 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1062 tty->raw = 0;
1063 tty->real_raw = 0;
1064 } else {
1065 tty->raw = 1;
1066 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1067 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1068 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1069 tty->real_raw = 1;
1070 else
1071 tty->real_raw = 0;
1072 }
1073 }
1074
1075 /**
1076 * n_tty_close - close the ldisc for this tty
1077 * @tty: device
1078 *
1079 * Called from the terminal layer when this line discipline is
1080 * being shut down, either because of a close or becsuse of a
1081 * discipline change. The function will not be called while other
1082 * ldisc methods are in progress.
1083 */
1084
1085 static void n_tty_close(struct tty_struct *tty)
1086 {
1087 n_tty_flush_buffer(tty);
1088 if (tty->read_buf) {
1089 free_buf(tty->read_buf);
1090 tty->read_buf = NULL;
1091 }
1092 }
1093
1094 /**
1095 * n_tty_open - open an ldisc
1096 * @tty: terminal to open
1097 *
1098 * Called when this line discipline is being attached to the
1099 * terminal device. Can sleep. Called serialized so that no
1100 * other events will occur in parallel. No further open will occur
1101 * until a close.
1102 */
1103
1104 static int n_tty_open(struct tty_struct *tty)
1105 {
1106 if (!tty)
1107 return -EINVAL;
1108
1109 /* This one is ugly. Currently a malloc failure here can panic */
1110 if (!tty->read_buf) {
1111 tty->read_buf = alloc_buf();
1112 if (!tty->read_buf)
1113 return -ENOMEM;
1114 }
1115 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
1116 reset_buffer_flags(tty);
1117 tty->column = 0;
1118 n_tty_set_termios(tty, NULL);
1119 tty->minimum_to_wake = 1;
1120 tty->closing = 0;
1121 return 0;
1122 }
1123
1124 static inline int input_available_p(struct tty_struct *tty, int amt)
1125 {
1126 if (tty->icanon) {
1127 if (tty->canon_data)
1128 return 1;
1129 } else if (tty->read_cnt >= (amt ? amt : 1))
1130 return 1;
1131
1132 return 0;
1133 }
1134
1135 /**
1136 * copy_from_read_buf - copy read data directly
1137 * @tty: terminal device
1138 * @b: user data
1139 * @nr: size of data
1140 *
1141 * Helper function to speed up read_chan. It is only called when
1142 * ICANON is off; it copies characters straight from the tty queue to
1143 * user space directly. It can be profitably called twice; once to
1144 * drain the space from the tail pointer to the (physical) end of the
1145 * buffer, and once to drain the space from the (physical) beginning of
1146 * the buffer to head pointer.
1147 *
1148 * Called under the tty->atomic_read sem and with TTY_DONT_FLIP set
1149 *
1150 */
1151
1152 static inline int copy_from_read_buf(struct tty_struct *tty,
1153 unsigned char __user **b,
1154 size_t *nr)
1155
1156 {
1157 int retval;
1158 ssize_t n;
1159 unsigned long flags;
1160
1161 retval = 0;
1162 spin_lock_irqsave(&tty->read_lock, flags);
1163 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1164 n = min((ssize_t)*nr, n);
1165 spin_unlock_irqrestore(&tty->read_lock, flags);
1166 if (n) {
1167 mb();
1168 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1169 n -= retval;
1170 spin_lock_irqsave(&tty->read_lock, flags);
1171 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1172 tty->read_cnt -= n;
1173 spin_unlock_irqrestore(&tty->read_lock, flags);
1174 *b += n;
1175 *nr -= n;
1176 }
1177 return retval;
1178 }
1179
1180 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
1181
1182 /**
1183 * job_control - check job control
1184 * @tty: tty
1185 * @file: file handle
1186 *
1187 * Perform job control management checks on this file/tty descriptor
1188 * and if appropriate send any needed signals and return a negative
1189 * error code if action should be taken.
1190 */
1191
1192 static int job_control(struct tty_struct *tty, struct file *file)
1193 {
1194 /* Job control check -- must be done at start and after
1195 every sleep (POSIX.1 7.1.1.4). */
1196 /* NOTE: not yet done after every sleep pending a thorough
1197 check of the logic of this change. -- jlc */
1198 /* don't stop on /dev/console */
1199 if (file->f_op->write != redirected_tty_write &&
1200 current->signal->tty == tty) {
1201 if (tty->pgrp <= 0)
1202 printk("read_chan: tty->pgrp <= 0!\n");
1203 else if (process_group(current) != tty->pgrp) {
1204 if (is_ignored(SIGTTIN) ||
1205 is_orphaned_pgrp(process_group(current)))
1206 return -EIO;
1207 kill_pg(process_group(current), SIGTTIN, 1);
1208 return -ERESTARTSYS;
1209 }
1210 }
1211 return 0;
1212 }
1213
1214
1215 /**
1216 * read_chan - read function for tty
1217 * @tty: tty device
1218 * @file: file object
1219 * @buf: userspace buffer pointer
1220 * @nr: size of I/O
1221 *
1222 * Perform reads for the line discipline. We are guaranteed that the
1223 * line discipline will not be closed under us but we may get multiple
1224 * parallel readers and must handle this ourselves. We may also get
1225 * a hangup. Always called in user context, may sleep.
1226 *
1227 * This code must be sure never to sleep through a hangup.
1228 */
1229
1230 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
1231 unsigned char __user *buf, size_t nr)
1232 {
1233 unsigned char __user *b = buf;
1234 DECLARE_WAITQUEUE(wait, current);
1235 int c;
1236 int minimum, time;
1237 ssize_t retval = 0;
1238 ssize_t size;
1239 long timeout;
1240 unsigned long flags;
1241
1242 do_it_again:
1243
1244 if (!tty->read_buf) {
1245 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
1246 return -EIO;
1247 }
1248
1249 c = job_control(tty, file);
1250 if(c < 0)
1251 return c;
1252
1253 minimum = time = 0;
1254 timeout = MAX_SCHEDULE_TIMEOUT;
1255 if (!tty->icanon) {
1256 time = (HZ / 10) * TIME_CHAR(tty);
1257 minimum = MIN_CHAR(tty);
1258 if (minimum) {
1259 if (time)
1260 tty->minimum_to_wake = 1;
1261 else if (!waitqueue_active(&tty->read_wait) ||
1262 (tty->minimum_to_wake > minimum))
1263 tty->minimum_to_wake = minimum;
1264 } else {
1265 timeout = 0;
1266 if (time) {
1267 timeout = time;
1268 time = 0;
1269 }
1270 tty->minimum_to_wake = minimum = 1;
1271 }
1272 }
1273
1274 /*
1275 * Internal serialization of reads.
1276 */
1277 if (file->f_flags & O_NONBLOCK) {
1278 if (down_trylock(&tty->atomic_read))
1279 return -EAGAIN;
1280 }
1281 else {
1282 if (down_interruptible(&tty->atomic_read))
1283 return -ERESTARTSYS;
1284 }
1285
1286 add_wait_queue(&tty->read_wait, &wait);
1287 set_bit(TTY_DONT_FLIP, &tty->flags);
1288 while (nr) {
1289 /* First test for status change. */
1290 if (tty->packet && tty->link->ctrl_status) {
1291 unsigned char cs;
1292 if (b != buf)
1293 break;
1294 cs = tty->link->ctrl_status;
1295 tty->link->ctrl_status = 0;
1296 if (put_user(cs, b++)) {
1297 retval = -EFAULT;
1298 b--;
1299 break;
1300 }
1301 nr--;
1302 break;
1303 }
1304 /* This statement must be first before checking for input
1305 so that any interrupt will set the state back to
1306 TASK_RUNNING. */
1307 set_current_state(TASK_INTERRUPTIBLE);
1308
1309 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1310 ((minimum - (b - buf)) >= 1))
1311 tty->minimum_to_wake = (minimum - (b - buf));
1312
1313 if (!input_available_p(tty, 0)) {
1314 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1315 retval = -EIO;
1316 break;
1317 }
1318 if (tty_hung_up_p(file))
1319 break;
1320 if (!timeout)
1321 break;
1322 if (file->f_flags & O_NONBLOCK) {
1323 retval = -EAGAIN;
1324 break;
1325 }
1326 if (signal_pending(current)) {
1327 retval = -ERESTARTSYS;
1328 break;
1329 }
1330 clear_bit(TTY_DONT_FLIP, &tty->flags);
1331 timeout = schedule_timeout(timeout);
1332 set_bit(TTY_DONT_FLIP, &tty->flags);
1333 continue;
1334 }
1335 __set_current_state(TASK_RUNNING);
1336
1337 /* Deal with packet mode. */
1338 if (tty->packet && b == buf) {
1339 if (put_user(TIOCPKT_DATA, b++)) {
1340 retval = -EFAULT;
1341 b--;
1342 break;
1343 }
1344 nr--;
1345 }
1346
1347 if (tty->icanon) {
1348 /* N.B. avoid overrun if nr == 0 */
1349 while (nr && tty->read_cnt) {
1350 int eol;
1351
1352 eol = test_and_clear_bit(tty->read_tail,
1353 tty->read_flags);
1354 c = tty->read_buf[tty->read_tail];
1355 tty->read_buf[tty->read_tail] = 0; /* ZS */
1356 spin_lock_irqsave(&tty->read_lock, flags);
1357 tty->read_tail = ((tty->read_tail+1) &
1358 (N_TTY_BUF_SIZE-1));
1359 tty->read_cnt--;
1360 if (eol) {
1361 /* this test should be redundant:
1362 * we shouldn't be reading data if
1363 * canon_data is 0
1364 */
1365 if (--tty->canon_data < 0)
1366 tty->canon_data = 0;
1367 }
1368 spin_unlock_irqrestore(&tty->read_lock, flags);
1369
1370 if (!eol || (c != __DISABLED_CHAR)) {
1371 if (put_user(c, b++)) {
1372 retval = -EFAULT;
1373 b--;
1374 break;
1375 }
1376 nr--;
1377 }
1378 if (eol)
1379 break;
1380 }
1381 if (retval)
1382 break;
1383 } else {
1384 int uncopied;
1385 uncopied = copy_from_read_buf(tty, &b, &nr);
1386 uncopied += copy_from_read_buf(tty, &b, &nr);
1387 if (uncopied) {
1388 retval = -EFAULT;
1389 break;
1390 }
1391 }
1392
1393 /* If there is enough space in the read buffer now, let the
1394 * low-level driver know. We use n_tty_chars_in_buffer() to
1395 * check the buffer, as it now knows about canonical mode.
1396 * Otherwise, if the driver is throttled and the line is
1397 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1398 * we won't get any more characters.
1399 */
1400 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1401 check_unthrottle(tty);
1402
1403 if (b - buf >= minimum)
1404 break;
1405 if (time)
1406 timeout = time;
1407 }
1408 clear_bit(TTY_DONT_FLIP, &tty->flags);
1409 up(&tty->atomic_read);
1410 remove_wait_queue(&tty->read_wait, &wait);
1411
1412 if (!waitqueue_active(&tty->read_wait))
1413 tty->minimum_to_wake = minimum;
1414
1415 __set_current_state(TASK_RUNNING);
1416 size = b - buf;
1417 if (size) {
1418 retval = size;
1419 if (nr)
1420 clear_bit(TTY_PUSH, &tty->flags);
1421 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1422 goto do_it_again;
1423
1424 return retval;
1425 }
1426
1427 /**
1428 * write_chan - write function for tty
1429 * @tty: tty device
1430 * @file: file object
1431 * @buf: userspace buffer pointer
1432 * @nr: size of I/O
1433 *
1434 * Write function of the terminal device. This is serialized with
1435 * respect to other write callers but not to termios changes, reads
1436 * and other such events. We must be careful with N_TTY as the receive
1437 * code will echo characters, thus calling driver write methods.
1438 *
1439 * This code must be sure never to sleep through a hangup.
1440 */
1441
1442 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1443 const unsigned char __user * buf, size_t nr)
1444 {
1445 const unsigned char __user *b = buf;
1446 DECLARE_WAITQUEUE(wait, current);
1447 int c;
1448 ssize_t retval = 0;
1449
1450 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1451 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1452 retval = tty_check_change(tty);
1453 if (retval)
1454 return retval;
1455 }
1456
1457 add_wait_queue(&tty->write_wait, &wait);
1458 while (1) {
1459 set_current_state(TASK_INTERRUPTIBLE);
1460 if (signal_pending(current)) {
1461 retval = -ERESTARTSYS;
1462 break;
1463 }
1464 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1465 retval = -EIO;
1466 break;
1467 }
1468 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1469 while (nr > 0) {
1470 ssize_t num = opost_block(tty, b, nr);
1471 if (num < 0) {
1472 if (num == -EAGAIN)
1473 break;
1474 retval = num;
1475 goto break_out;
1476 }
1477 b += num;
1478 nr -= num;
1479 if (nr == 0)
1480 break;
1481 get_user(c, b);
1482 if (opost(c, tty) < 0)
1483 break;
1484 b++; nr--;
1485 }
1486 if (tty->driver->flush_chars)
1487 tty->driver->flush_chars(tty);
1488 } else {
1489 c = tty->driver->write(tty, 1, b, nr);
1490 if (c < 0) {
1491 retval = c;
1492 goto break_out;
1493 }
1494 b += c;
1495 nr -= c;
1496 }
1497 if (!nr)
1498 break;
1499 if (file->f_flags & O_NONBLOCK) {
1500 retval = -EAGAIN;
1501 break;
1502 }
1503 schedule();
1504 }
1505 break_out:
1506 __set_current_state(TASK_RUNNING);
1507 remove_wait_queue(&tty->write_wait, &wait);
1508 return (b - buf) ? b - buf : retval;
1509 }
1510
1511 /**
1512 * normal_poll - poll method for N_TTY
1513 * @tty: terminal device
1514 * @file: file accessing it
1515 * @wait: poll table
1516 *
1517 * Called when the line discipline is asked to poll() for data or
1518 * for special events. This code is not serialized with respect to
1519 * other events save open/close.
1520 *
1521 * This code must be sure never to sleep through a hangup.
1522 * Called without the kernel lock held - fine
1523 *
1524 * FIXME: if someone changes the VMIN or discipline settings for the
1525 * terminal while another process is in poll() the poll does not
1526 * recompute the new limits. Possibly set_termios should issue
1527 * a read wakeup to fix this bug.
1528 */
1529
1530 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1531 {
1532 unsigned int mask = 0;
1533
1534 poll_wait(file, &tty->read_wait, wait);
1535 poll_wait(file, &tty->write_wait, wait);
1536 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1537 mask |= POLLIN | POLLRDNORM;
1538 if (tty->packet && tty->link->ctrl_status)
1539 mask |= POLLPRI | POLLIN | POLLRDNORM;
1540 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1541 mask |= POLLHUP;
1542 if (tty_hung_up_p(file))
1543 mask |= POLLHUP;
1544 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1545 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1546 tty->minimum_to_wake = MIN_CHAR(tty);
1547 else
1548 tty->minimum_to_wake = 1;
1549 }
1550 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1551 tty->driver->write_room(tty) > 0)
1552 mask |= POLLOUT | POLLWRNORM;
1553 return mask;
1554 }
1555
1556 struct tty_ldisc tty_ldisc_N_TTY = {
1557 TTY_LDISC_MAGIC, /* magic */
1558 "n_tty", /* name */
1559 0, /* num */
1560 0, /* flags */
1561 n_tty_open, /* open */
1562 n_tty_close, /* close */
1563 n_tty_flush_buffer, /* flush_buffer */
1564 n_tty_chars_in_buffer, /* chars_in_buffer */
1565 read_chan, /* read */
1566 write_chan, /* write */
1567 n_tty_ioctl, /* ioctl */
1568 n_tty_set_termios, /* set_termios */
1569 normal_poll, /* poll */
1570 NULL, /* hangup */
1571 n_tty_receive_buf, /* receive_buf */
1572 n_tty_receive_room, /* receive_room */
1573 n_tty_write_wakeup /* write_wakeup */
1574 };
1575
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.