OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [char/] [n_tty.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
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 Public
19
 * License.
20
 */
21
 
22
#include <linux/types.h>
23
#include <linux/major.h>
24
#include <linux/errno.h>
25
#include <linux/signal.h>
26
#include <linux/fcntl.h>
27
#include <linux/sched.h>
28
#include <linux/interrupt.h>
29
#include <linux/tty.h>
30
#include <linux/timer.h>
31
#include <linux/ctype.h>
32
#include <linux/kd.h>
33
#include <linux/mm.h>
34
#include <linux/string.h>
35
#include <linux/malloc.h>
36
 
37
#include <asm/segment.h>
38
#include <asm/system.h>
39
#include <asm/bitops.h>
40
 
41
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
42
 
43
#ifndef MIN
44
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
45
#endif
46
 
47
/* number of characters left in xmit buffer before select has we have room */
48
#define WAKEUP_CHARS 256
49
 
50
/*
51
 * This defines the low- and high-watermarks for throttling and
52
 * unthrottling the TTY driver.  These watermarks are used for
53
 * controlling the space in the read buffer.
54
 */
55
#define TTY_THRESHOLD_THROTTLE          (N_TTY_BUF_SIZE - 128)
56
#define TTY_THRESHOLD_UNTHROTTLE        128
57
 
58
static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
59
{
60
        if (tty->read_cnt < N_TTY_BUF_SIZE) {
61
                tty->read_buf[tty->read_head] = c;
62
                tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
63
                tty->read_cnt++;
64
        }
65
}
66
 
67
/*
68
 * Flush the input buffer
69
 */
70
void n_tty_flush_buffer(struct tty_struct * tty)
71
{
72
        tty->read_head = tty->read_tail = tty->read_cnt = 0;
73
        tty->canon_head = tty->canon_data = tty->erasing = 0;
74
        memset(&tty->read_flags, 0, sizeof tty->read_flags);
75
 
76
        if (!tty->link)
77
                return;
78
 
79
        if (tty->driver.unthrottle)
80
                (tty->driver.unthrottle)(tty);
81
        if (tty->link->packet) {
82
                tty->ctrl_status |= TIOCPKT_FLUSHREAD;
83
                wake_up_interruptible(&tty->link->read_wait);
84
        }
85
}
86
 
87
/*
88
 * Return number of characters buffered to be delivered to user
89
 */
90
int n_tty_chars_in_buffer(struct tty_struct *tty)
91
{
92
        if (tty->icanon) {
93
                if (!tty->canon_data) return 0;
94
 
95
                return (tty->canon_head > tty->read_tail) ?
96
                        tty->canon_head - tty->read_tail :
97
                        tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
98
        }
99
        return tty->read_cnt;
100
}
101
 
102
/*
103
 * Perform OPOST processing.  Returns -1 when the output device is
104
 * full and the character must be retried.
105
 */
106
static int opost(unsigned char c, struct tty_struct *tty)
107
{
108
        int     space, spaces;
109
 
110
        space = tty->driver.write_room(tty);
111
        if (!space)
112
                return -1;
113
 
114
        if (O_OPOST(tty)) {
115
                switch (c) {
116
                case '\n':
117
                        if (O_ONLRET(tty))
118
                                tty->column = 0;
119
                        if (O_ONLCR(tty)) {
120
                                if (space < 2)
121
                                        return -1;
122
                                tty->driver.put_char(tty, '\r');
123
                                tty->column = 0;
124
                        }
125
                        tty->canon_column = tty->column;
126
                        break;
127
                case '\r':
128
                        if (O_ONOCR(tty) && tty->column == 0)
129
                                return 0;
130
                        if (O_OCRNL(tty)) {
131
                                c = '\n';
132
                                if (O_ONLRET(tty))
133
                                        tty->canon_column = tty->column = 0;
134
                                break;
135
                        }
136
                        tty->canon_column = tty->column = 0;
137
                        break;
138
                case '\t':
139
                        spaces = 8 - (tty->column & 7);
140
                        if (O_TABDLY(tty) == XTABS) {
141
                                if (space < spaces)
142
                                        return -1;
143
                                tty->column += spaces;
144
                                tty->driver.write(tty, 0, "        ", spaces);
145
                                return 0;
146
                        }
147
                        tty->column += spaces;
148
                        break;
149
                case '\b':
150
                        if (tty->column > 0)
151
                                tty->column--;
152
                        break;
153
                default:
154
                        if (O_OLCUC(tty))
155
                                c = toupper(c);
156
                        if (!iscntrl(c))
157
                                tty->column++;
158
                        break;
159
                }
160
        }
161
        tty->driver.put_char(tty, c);
162
        return 0;
163
}
164
 
165
static inline void put_char(unsigned char c, struct tty_struct *tty)
166
{
167
        tty->driver.put_char(tty, c);
168
}
169
 
170
/* Must be called only when L_ECHO(tty) is true. */
171
 
172
static void echo_char(unsigned char c, struct tty_struct *tty)
173
{
174
        if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
175
                put_char('^', tty);
176
                put_char(c ^ 0100, tty);
177
                tty->column += 2;
178
        } else
179
                opost(c, tty);
180
}
181
 
182
static inline void finish_erasing(struct tty_struct *tty)
183
{
184
        if (tty->erasing) {
185
                put_char('/', tty);
186
                tty->column += 2;
187
                tty->erasing = 0;
188
        }
189
}
190
 
191
static void eraser(unsigned char c, struct tty_struct *tty)
192
{
193
        enum { ERASE, WERASE, KILL } kill_type;
194
        int head, seen_alnums;
195
 
196
        if (tty->read_head == tty->canon_head) {
197
                /* opost('\a', tty); */         /* what do you think? */
198
                return;
199
        }
200
        if (c == ERASE_CHAR(tty))
201
                kill_type = ERASE;
202
        else if (c == WERASE_CHAR(tty))
203
                kill_type = WERASE;
204
        else {
205
                if (!L_ECHO(tty)) {
206
                        tty->read_cnt -= ((tty->read_head - tty->canon_head) &
207
                                          (N_TTY_BUF_SIZE - 1));
208
                        tty->read_head = tty->canon_head;
209
                        return;
210
                }
211
                if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
212
                        tty->read_cnt -= ((tty->read_head - tty->canon_head) &
213
                                          (N_TTY_BUF_SIZE - 1));
214
                        tty->read_head = tty->canon_head;
215
                        finish_erasing(tty);
216
                        echo_char(KILL_CHAR(tty), tty);
217
                        /* Add a newline if ECHOK is on and ECHOKE is off. */
218
                        if (L_ECHOK(tty))
219
                                opost('\n', tty);
220
                        return;
221
                }
222
                kill_type = KILL;
223
        }
224
 
225
        seen_alnums = 0;
226
        while (tty->read_head != tty->canon_head) {
227
                head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
228
                c = tty->read_buf[head];
229
                if (kill_type == WERASE) {
230
                        /* Equivalent to BSD's ALTWERASE. */
231
                        if (isalnum(c) || c == '_')
232
                                seen_alnums++;
233
                        else if (seen_alnums)
234
                                break;
235
                }
236
                tty->read_head = head;
237
                tty->read_cnt--;
238
                if (L_ECHO(tty)) {
239
                        if (L_ECHOPRT(tty)) {
240
                                if (!tty->erasing) {
241
                                        put_char('\\', tty);
242
                                        tty->column++;
243
                                        tty->erasing = 1;
244
                                }
245
                                echo_char(c, tty);
246
                        } else if (kill_type == ERASE && !L_ECHOE(tty)) {
247
                                echo_char(ERASE_CHAR(tty), tty);
248
                        } else if (c == '\t') {
249
                                unsigned int col = tty->canon_column;
250
                                unsigned long tail = tty->canon_head;
251
 
252
                                /* Find the column of the last char. */
253
                                while (tail != tty->read_head) {
254
                                        c = tty->read_buf[tail];
255
                                        if (c == '\t')
256
                                                col = (col | 7) + 1;
257
                                        else if (iscntrl(c)) {
258
                                                if (L_ECHOCTL(tty))
259
                                                        col += 2;
260
                                        } else
261
                                                col++;
262
                                        tail = (tail+1) & (N_TTY_BUF_SIZE-1);
263
                                }
264
 
265
                                /* should never happen */
266
                                if (tty->column > 0x80000000)
267
                                        tty->column = 0;
268
 
269
                                /* Now backup to that column. */
270
                                while (tty->column > col) {
271
                                        /* Can't use opost here. */
272
                                        put_char('\b', tty);
273
                                        if (tty->column > 0)
274
                                                tty->column--;
275
                                }
276
                        } else {
277
                                if (iscntrl(c) && L_ECHOCTL(tty)) {
278
                                        put_char('\b', tty);
279
                                        put_char(' ', tty);
280
                                        put_char('\b', tty);
281
                                        if (tty->column > 0)
282
                                                tty->column--;
283
                                }
284
                                if (!iscntrl(c) || L_ECHOCTL(tty)) {
285
                                        put_char('\b', tty);
286
                                        put_char(' ', tty);
287
                                        put_char('\b', tty);
288
                                        if (tty->column > 0)
289
                                                tty->column--;
290
                                }
291
                        }
292
                }
293
                if (kill_type == ERASE)
294
                        break;
295
        }
296
        if (tty->read_head == tty->canon_head)
297
                finish_erasing(tty);
298
}
299
 
300
static inline void isig(int sig, struct tty_struct *tty, int flush)
301
{
302
        if (tty->pgrp > 0)
303
                kill_pg(tty->pgrp, sig, 1);
304
        if (flush || !L_NOFLSH(tty)) {
305
                n_tty_flush_buffer(tty);
306
                if (tty->driver.flush_buffer)
307
                        tty->driver.flush_buffer(tty);
308
        }
309
}
310
 
311
static inline void n_tty_receive_break(struct tty_struct *tty)
312
{
313
        if (I_IGNBRK(tty))
314
                return;
315
        if (I_BRKINT(tty)) {
316
                isig(SIGINT, tty, 1);
317
                return;
318
        }
319
        if (I_PARMRK(tty)) {
320
                put_tty_queue('\377', tty);
321
                put_tty_queue('\0', tty);
322
        }
323
        put_tty_queue('\0', tty);
324
        wake_up_interruptible(&tty->read_wait);
325
}
326
 
327
static inline void n_tty_receive_overrun(struct tty_struct *tty)
328
{
329
        char buf[64];
330
 
331
        tty->num_overrun++;
332
        if (tty->overrun_time < (jiffies - HZ)) {
333
                printk("%s: %d input overrun(s)\n", _tty_name(tty, buf),
334
                       tty->num_overrun);
335
                tty->overrun_time = jiffies;
336
                tty->num_overrun = 0;
337
        }
338
}
339
 
340
static inline void n_tty_receive_parity_error(struct tty_struct *tty,
341
                                              unsigned char c)
342
{
343
        if (I_IGNPAR(tty)) {
344
                return;
345
        }
346
        if (I_PARMRK(tty)) {
347
                put_tty_queue('\377', tty);
348
                put_tty_queue('\0', tty);
349
                put_tty_queue(c, tty);
350
        } else  if (I_INPCK(tty))
351
                put_tty_queue('\0', tty);
352
        else
353
                put_tty_queue(c, tty);
354
        wake_up_interruptible(&tty->read_wait);
355
}
356
 
357
static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
358
{
359
        if (tty->raw) {
360
                put_tty_queue(c, tty);
361
                return;
362
        }
363
 
364
        if (tty->stopped && I_IXON(tty) && I_IXANY(tty)) {
365
                start_tty(tty);
366
                return;
367
        }
368
 
369
        if (I_ISTRIP(tty))
370
                c &= 0x7f;
371
        if (I_IUCLC(tty) && L_IEXTEN(tty))
372
                c=tolower(c);
373
 
374
        if (tty->closing) {
375
                if (I_IXON(tty)) {
376
                        if (c == START_CHAR(tty))
377
                                start_tty(tty);
378
                        else if (c == STOP_CHAR(tty))
379
                                stop_tty(tty);
380
                }
381
                return;
382
        }
383
 
384
        /*
385
         * If the previous character was LNEXT, or we know that this
386
         * character is not one of the characters that we'll have to
387
         * handle specially, do shortcut processing to speed things
388
         * up.
389
         */
390
        if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
391
                finish_erasing(tty);
392
                tty->lnext = 0;
393
                if (L_ECHO(tty)) {
394
                        if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
395
                                put_char('\a', tty); /* beep if no space */
396
                                return;
397
                        }
398
                        /* Record the column of first canon char. */
399
                        if (tty->canon_head == tty->read_head)
400
                                tty->canon_column = tty->column;
401
                        echo_char(c, tty);
402
                }
403
                if (I_PARMRK(tty) && c == (unsigned char) '\377')
404
                        put_tty_queue(c, tty);
405
                put_tty_queue(c, tty);
406
                return;
407
        }
408
 
409
        if (c == '\r') {
410
                if (I_IGNCR(tty))
411
                        return;
412
                if (I_ICRNL(tty))
413
                        c = '\n';
414
        } else if (c == '\n' && I_INLCR(tty))
415
                c = '\r';
416
        if (I_IXON(tty)) {
417
                if (c == START_CHAR(tty)) {
418
                        start_tty(tty);
419
                        return;
420
                }
421
                if (c == STOP_CHAR(tty)) {
422
                        stop_tty(tty);
423
                        return;
424
                }
425
        }
426
        if (L_ISIG(tty)) {
427
                int signal;
428
                signal = SIGINT;
429
                if (c == INTR_CHAR(tty))
430
                        goto send_signal;
431
                signal = SIGQUIT;
432
                if (c == QUIT_CHAR(tty))
433
                        goto send_signal;
434
                signal = SIGTSTP;
435
                if (c == SUSP_CHAR(tty)) {
436
send_signal:
437
                        isig(signal, tty, 0);
438
                        return;
439
                }
440
        }
441
        if (L_ICANON(tty)) {
442
                if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
443
                    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
444
                        eraser(c, tty);
445
                        return;
446
                }
447
                if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
448
                        tty->lnext = 1;
449
                        if (L_ECHO(tty)) {
450
                                finish_erasing(tty);
451
                                if (L_ECHOCTL(tty)) {
452
                                        put_char('^', tty);
453
                                        put_char('\b', tty);
454
                                }
455
                        }
456
                        return;
457
                }
458
                if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
459
                    L_IEXTEN(tty)) {
460
                        unsigned long tail = tty->canon_head;
461
 
462
                        finish_erasing(tty);
463
                        echo_char(c, tty);
464
                        opost('\n', tty);
465
                        while (tail != tty->read_head) {
466
                                echo_char(tty->read_buf[tail], tty);
467
                                tail = (tail+1) & (N_TTY_BUF_SIZE-1);
468
                        }
469
                        return;
470
                }
471
                if (c == '\n') {
472
                        if (L_ECHO(tty) || L_ECHONL(tty)) {
473
                                if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
474
                                        put_char('\a', tty);
475
                                        return;
476
                                }
477
                                opost('\n', tty);
478
                        }
479
                        goto handle_newline;
480
                }
481
                if (c == EOF_CHAR(tty)) {
482
                        if (tty->canon_head != tty->read_head)
483
                                set_bit(TTY_PUSH, &tty->flags);
484
                        c = __DISABLED_CHAR;
485
                        goto handle_newline;
486
                }
487
                if ((c == EOL_CHAR(tty)) ||
488
                    (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
489
                        /*
490
                         * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
491
                         */
492
                        if (L_ECHO(tty)) {
493
                                if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
494
                                        put_char('\a', tty);
495
                                        return;
496
                                }
497
                                /* Record the column of first canon char. */
498
                                if (tty->canon_head == tty->read_head)
499
                                        tty->canon_column = tty->column;
500
                                echo_char(c, tty);
501
                        }
502
                        /*
503
                         * XXX does PARMRK doubling happen for
504
                         * EOL_CHAR and EOL2_CHAR?
505
                         */
506
                        if (I_PARMRK(tty) && c == (unsigned char) '\377')
507
                                put_tty_queue(c, tty);
508
 
509
                handle_newline:
510
                        set_bit(tty->read_head, &tty->read_flags);
511
                        put_tty_queue(c, tty);
512
                        tty->canon_head = tty->read_head;
513
                        tty->canon_data++;
514
                        if (tty->fasync)
515
                                kill_fasync(tty->fasync, SIGIO);
516
                        if (tty->read_wait)
517
                                wake_up_interruptible(&tty->read_wait);
518
                        return;
519
                }
520
        }
521
 
522
        finish_erasing(tty);
523
        if (L_ECHO(tty)) {
524
                if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
525
                        put_char('\a', tty); /* beep if no space */
526
                        return;
527
                }
528
                if (c == '\n')
529
                        opost('\n', tty);
530
                else {
531
                        /* Record the column of first canon char. */
532
                        if (tty->canon_head == tty->read_head)
533
                                tty->canon_column = tty->column;
534
                        echo_char(c, tty);
535
                }
536
        }
537
 
538
        if (I_PARMRK(tty) && c == (unsigned char) '\377')
539
                put_tty_queue(c, tty);
540
 
541
        put_tty_queue(c, tty);
542
}
543
 
544
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
545
                              char *fp, int count)
546
{
547
        const unsigned char *p;
548
        char *f, flags = 0;
549
        int     i;
550
 
551
        if (!tty->read_buf)
552
                return;
553
 
554
        if (tty->real_raw) {
555
                i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
556
                                   N_TTY_BUF_SIZE - tty->read_head));
557
                memcpy(tty->read_buf + tty->read_head, cp, i);
558
                tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
559
                tty->read_cnt += i;
560
                cp += i;
561
                count -= i;
562
 
563
                i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
564
                               N_TTY_BUF_SIZE - tty->read_head));
565
                memcpy(tty->read_buf + tty->read_head, cp, i);
566
                tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
567
                tty->read_cnt += i;
568
        } else {
569
                for (i=count, p = cp, f = fp; i; i--, p++) {
570
                        if (f)
571
                                flags = *f++;
572
                        switch (flags) {
573
                        case TTY_NORMAL:
574
                                n_tty_receive_char(tty, *p);
575
                                break;
576
                        case TTY_BREAK:
577
                                n_tty_receive_break(tty);
578
                                break;
579
                        case TTY_PARITY:
580
                        case TTY_FRAME:
581
                                n_tty_receive_parity_error(tty, *p);
582
                                break;
583
                        case TTY_OVERRUN:
584
                                n_tty_receive_overrun(tty);
585
                                break;
586
                        default:
587
                                printk("%s: unknown flag %d\n", tty_name(tty),
588
                                       flags);
589
                                break;
590
                        }
591
                }
592
                if (tty->driver.flush_chars)
593
                        tty->driver.flush_chars(tty);
594
        }
595
 
596
        if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
597
                if (tty->fasync)
598
                        kill_fasync(tty->fasync, SIGIO);
599
                if (tty->read_wait)
600
                        wake_up_interruptible(&tty->read_wait);
601
        }
602
 
603
        if ((tty->read_cnt >= TTY_THRESHOLD_THROTTLE) &&
604
            tty->driver.throttle &&
605
            !set_bit(TTY_THROTTLED, &tty->flags))
606
                tty->driver.throttle(tty);
607
}
608
 
609
static int n_tty_receive_room(struct tty_struct *tty)
610
{
611
        int     left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
612
 
613
        /*
614
         * If we are doing input canonicalization, and there are no
615
         * pending newlines, let characters through without limit, so
616
         * that erase characters will be handled.  Other excess
617
         * characters will be beeped.
618
         */
619
        if (tty->icanon && !tty->canon_data)
620
                return N_TTY_BUF_SIZE;
621
 
622
        if (left > 0)
623
                return left;
624
        return 0;
625
}
626
 
627
int is_ignored(int sig)
628
{
629
        return ((current->blocked & (1<<(sig-1))) ||
630
                (current->sig->action[sig-1].sa_handler == SIG_IGN));
631
}
632
 
633
static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
634
{
635
        if (!tty)
636
                return;
637
 
638
        tty->icanon = (L_ICANON(tty) != 0);
639
        if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
640
            I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
641
            I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
642
            I_PARMRK(tty)) {
643
                cli();
644
                memset(tty->process_char_map, 0, 256/8);
645
 
646
                if (I_IGNCR(tty) || I_ICRNL(tty))
647
                        set_bit('\r', &tty->process_char_map);
648
                if (I_INLCR(tty))
649
                        set_bit('\n', &tty->process_char_map);
650
 
651
                if (L_ICANON(tty)) {
652
                        set_bit(ERASE_CHAR(tty), &tty->process_char_map);
653
                        set_bit(KILL_CHAR(tty), &tty->process_char_map);
654
                        set_bit(EOF_CHAR(tty), &tty->process_char_map);
655
                        set_bit('\n', &tty->process_char_map);
656
                        set_bit(EOL_CHAR(tty), &tty->process_char_map);
657
                        if (L_IEXTEN(tty)) {
658
                                set_bit(WERASE_CHAR(tty),
659
                                        &tty->process_char_map);
660
                                set_bit(LNEXT_CHAR(tty),
661
                                        &tty->process_char_map);
662
                                set_bit(EOL2_CHAR(tty),
663
                                        &tty->process_char_map);
664
                                if (L_ECHO(tty))
665
                                        set_bit(REPRINT_CHAR(tty),
666
                                                &tty->process_char_map);
667
                        }
668
                }
669
                if (I_IXON(tty)) {
670
                        set_bit(START_CHAR(tty), &tty->process_char_map);
671
                        set_bit(STOP_CHAR(tty), &tty->process_char_map);
672
                }
673
                if (L_ISIG(tty)) {
674
                        set_bit(INTR_CHAR(tty), &tty->process_char_map);
675
                        set_bit(QUIT_CHAR(tty), &tty->process_char_map);
676
                        set_bit(SUSP_CHAR(tty), &tty->process_char_map);
677
                }
678
                clear_bit(__DISABLED_CHAR, &tty->process_char_map);
679
                sti();
680
                tty->raw = 0;
681
                tty->real_raw = 0;
682
        } else {
683
                tty->raw = 1;
684
                if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
685
                    (I_IGNPAR(tty) || !I_INPCK(tty)) &&
686
                    (tty->driver.flags & TTY_DRIVER_REAL_RAW))
687
                        tty->real_raw = 1;
688
                else
689
                        tty->real_raw = 0;
690
        }
691
}
692
 
693
static void n_tty_close(struct tty_struct *tty)
694
{
695
        n_tty_flush_buffer(tty);
696
        if (tty->read_buf) {
697
                free_page((unsigned long) tty->read_buf);
698
                tty->read_buf = 0;
699
        }
700
}
701
 
702
static int n_tty_open(struct tty_struct *tty)
703
{
704
        if (!tty)
705
                return -EINVAL;
706
 
707
        if (!tty->read_buf) {
708
                tty->read_buf = (unsigned char *)
709
                        get_free_page(intr_count ? GFP_ATOMIC : GFP_KERNEL);
710
                if (!tty->read_buf)
711
                        return -ENOMEM;
712
        }
713
        memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
714
        tty->read_head = tty->read_tail = tty->read_cnt = 0;
715
        tty->canon_head = tty->canon_data = tty->erasing = 0;
716
        tty->column = 0;
717
        memset(tty->read_flags, 0, sizeof(tty->read_flags));
718
        n_tty_set_termios(tty, 0);
719
        tty->minimum_to_wake = 1;
720
        tty->closing = 0;
721
        return 0;
722
}
723
 
724
static inline int input_available_p(struct tty_struct *tty, int amt)
725
{
726
        if (L_ICANON(tty)) {
727
                if (tty->canon_data)
728
                        return 1;
729
        } else if (tty->read_cnt >= (amt ? amt : 1))
730
                return 1;
731
 
732
        return 0;
733
}
734
 
735
/*
736
 * Helper function to speed up read_chan.  It is only called when
737
 * ICANON is off; it copies characters straight from the tty queue to
738
 * user space directly.  It can be profitably called twice; once to
739
 * drain the space from the tail pointer to the (physical) end of the
740
 * buffer, and once to drain the space from the (physical) beginning of
741
 * the buffer to head pointer.
742
 */
743
static inline void copy_from_read_buf(struct tty_struct *tty,
744
                                      unsigned char **b,
745
                                      unsigned int *nr)
746
 
747
{
748
        int     n;
749
 
750
        n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
751
        if (!n)
752
                return;
753
        memcpy_tofs(*b, &tty->read_buf[tty->read_tail], n);
754
        tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
755
        tty->read_cnt -= n;
756
        *b += n;
757
        *nr -= n;
758
}
759
 
760
static int read_chan(struct tty_struct *tty, struct file *file,
761
                     unsigned char *buf, unsigned int nr)
762
{
763
        struct wait_queue wait = { current, NULL };
764
        int c;
765
        unsigned char *b = buf;
766
        int minimum, time;
767
        int retval = 0;
768
        int size;
769
 
770
do_it_again:
771
 
772
        if (!tty->read_buf) {
773
                printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
774
                return -EIO;
775
        }
776
 
777
        /* Job control check -- must be done at start and after
778
           every sleep (POSIX.1 7.1.1.4). */
779
        /* NOTE: not yet done after every sleep pending a thorough
780
           check of the logic of this change. -- jlc */
781
        /* don't stop on /dev/console */
782
        if (file->f_inode->i_rdev != CONSOLE_DEV &&
783
            current->tty == tty) {
784
                if (tty->pgrp <= 0)
785
                        printk("read_chan: tty->pgrp <= 0!\n");
786
                else if (current->pgrp != tty->pgrp) {
787
                        if (is_ignored(SIGTTIN) ||
788
                            is_orphaned_pgrp(current->pgrp))
789
                                return -EIO;
790
                        kill_pg(current->pgrp, SIGTTIN, 1);
791
                        return -ERESTARTSYS;
792
                }
793
        }
794
 
795
        if (L_ICANON(tty)) {
796
                minimum = time = 0;
797
                current->timeout = (unsigned long) -1;
798
        } else {
799
                time = (HZ / 10) * TIME_CHAR(tty);
800
                minimum = MIN_CHAR(tty);
801
                if (minimum) {
802
                        current->timeout = (unsigned long) -1;
803
                        if (time)
804
                                tty->minimum_to_wake = 1;
805
                        else if (!waitqueue_active(&tty->read_wait) ||
806
                                 (tty->minimum_to_wake > minimum))
807
                                tty->minimum_to_wake = minimum;
808
                } else {
809
                        if (time) {
810
                                current->timeout = time + jiffies;
811
                                time = 0;
812
                        } else
813
                                current->timeout = 0;
814
                        tty->minimum_to_wake = minimum = 1;
815
                }
816
        }
817
 
818
        add_wait_queue(&tty->read_wait, &wait);
819
        while (1) {
820
                /* First test for status change. */
821
                if (tty->packet && tty->link->ctrl_status) {
822
                        if (b != buf)
823
                                break;
824
                        put_user(tty->link->ctrl_status, b++);
825
                        tty->link->ctrl_status = 0;
826
                        break;
827
                }
828
                /* This statement must be first before checking for input
829
                   so that any interrupt will set the state back to
830
                   TASK_RUNNING. */
831
                current->state = TASK_INTERRUPTIBLE;
832
 
833
                if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
834
                    ((minimum - (b - buf)) >= 1))
835
                        tty->minimum_to_wake = (minimum - (b - buf));
836
 
837
                if (!input_available_p(tty, 0)) {
838
                        if (tty->flags & (1 << TTY_OTHER_CLOSED)) {
839
                                retval = -EIO;
840
                                break;
841
                        }
842
                        if (tty_hung_up_p(file))
843
                                break;
844
                        if (!current->timeout)
845
                                break;
846
                        if (file->f_flags & O_NONBLOCK) {
847
                                retval = -EAGAIN;
848
                                break;
849
                        }
850
                        if (current->signal & ~current->blocked) {
851
                                retval = -ERESTARTSYS;
852
                                break;
853
                        }
854
                        schedule();
855
                        continue;
856
                }
857
                current->state = TASK_RUNNING;
858
 
859
                /* Deal with packet mode. */
860
                if (tty->packet && b == buf) {
861
                        put_user(TIOCPKT_DATA, b++);
862
                        nr--;
863
                }
864
 
865
                if (L_ICANON(tty)) {
866
                        while (1) {
867
                                int eol;
868
 
869
                                disable_bh(TQUEUE_BH);
870
                                if (!tty->read_cnt) {
871
                                        enable_bh(TQUEUE_BH);
872
                                        break;
873
                                }
874
                                eol = clear_bit(tty->read_tail,
875
                                                &tty->read_flags);
876
                                c = tty->read_buf[tty->read_tail];
877
                                tty->read_tail = ((tty->read_tail+1) &
878
                                                  (N_TTY_BUF_SIZE-1));
879
                                tty->read_cnt--;
880
                                enable_bh(TQUEUE_BH);
881
                                if (!eol) {
882
                                        put_user(c, b++);
883
                                        if (--nr)
884
                                                continue;
885
                                        break;
886
                                }
887
                                if (--tty->canon_data < 0) {
888
                                        tty->canon_data = 0;
889
                                }
890
                                if (c != __DISABLED_CHAR) {
891
                                        put_user(c, b++);
892
                                        nr--;
893
                                }
894
                                break;
895
                        }
896
                } else {
897
                        disable_bh(TQUEUE_BH);
898
                        copy_from_read_buf(tty, &b, &nr);
899
                        copy_from_read_buf(tty, &b, &nr);
900
                        enable_bh(TQUEUE_BH);
901
                }
902
 
903
                /* If there is enough space in the read buffer now, let the
904
                   low-level driver know. */
905
                if (tty->driver.unthrottle &&
906
                    (tty->read_cnt <= TTY_THRESHOLD_UNTHROTTLE)
907
                    && clear_bit(TTY_THROTTLED, &tty->flags))
908
                        tty->driver.unthrottle(tty);
909
 
910
                if (b - buf >= minimum || !nr)
911
                        break;
912
                if (time)
913
                        current->timeout = time + jiffies;
914
        }
915
        remove_wait_queue(&tty->read_wait, &wait);
916
 
917
        if (!waitqueue_active(&tty->read_wait))
918
                tty->minimum_to_wake = minimum;
919
 
920
        current->state = TASK_RUNNING;
921
        current->timeout = 0;
922
        size = b - buf;
923
        if (size && nr)
924
                clear_bit(TTY_PUSH, &tty->flags);
925
        if (!size && clear_bit(TTY_PUSH, &tty->flags))
926
                goto do_it_again;
927
        if (!size && !retval)
928
                clear_bit(TTY_PUSH, &tty->flags);
929
        return (size ? size : retval);
930
}
931
 
932
static int write_chan(struct tty_struct * tty, struct file * file,
933
                      const unsigned char * buf, unsigned int nr)
934
{
935
        struct wait_queue wait = { current, NULL };
936
        int c;
937
        const unsigned char *b = buf;
938
        int retval = 0;
939
 
940
        /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
941
        if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
942
                retval = tty_check_change(tty);
943
                if (retval)
944
                        return retval;
945
        }
946
 
947
        add_wait_queue(&tty->write_wait, &wait);
948
        while (1) {
949
                current->state = TASK_INTERRUPTIBLE;
950
                if (current->signal & ~current->blocked) {
951
                        retval = -ERESTARTSYS;
952
                        break;
953
                }
954
                if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
955
                        retval = -EIO;
956
                        break;
957
                }
958
                if (O_OPOST(tty)) {
959
                        while (nr > 0) {
960
                                c = get_user(b);
961
                                if (opost(c, tty) < 0)
962
                                        break;
963
                                b++; nr--;
964
                        }
965
                        if (tty->driver.flush_chars)
966
                                tty->driver.flush_chars(tty);
967
                } else {
968
                        c = tty->driver.write(tty, 1, b, nr);
969
                        b += c;
970
                        nr -= c;
971
                }
972
                if (!nr)
973
                        break;
974
                if (file->f_flags & O_NONBLOCK) {
975
                        retval = -EAGAIN;
976
                        break;
977
                }
978
                schedule();
979
        }
980
        current->state = TASK_RUNNING;
981
        remove_wait_queue(&tty->write_wait, &wait);
982
        return (b - buf) ? b - buf : retval;
983
}
984
 
985
static int normal_select(struct tty_struct * tty, struct inode * inode,
986
                         struct file * file, int sel_type, select_table *wait)
987
{
988
        switch (sel_type) {
989
                case SEL_IN:
990
                        if (input_available_p(tty, TIME_CHAR(tty) ? 0 :
991
                                              MIN_CHAR(tty)))
992
                                return 1;
993
                        /* fall through */
994
                case SEL_EX:
995
                        if (tty->packet && tty->link->ctrl_status)
996
                                return 1;
997
                        if (tty->flags & (1 << TTY_OTHER_CLOSED))
998
                                return 1;
999
                        if (tty_hung_up_p(file))
1000
                                return 1;
1001
                        if (!waitqueue_active(&tty->read_wait)) {
1002
                                if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1003
                                        tty->minimum_to_wake = MIN_CHAR(tty);
1004
                                else
1005
                                        tty->minimum_to_wake = 1;
1006
                        }
1007
                        select_wait(&tty->read_wait, wait);
1008
                        return 0;
1009
                case SEL_OUT:
1010
                        if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1011
                                return 1;
1012
                        select_wait(&tty->write_wait, wait);
1013
                        return 0;
1014
        }
1015
        return 0;
1016
}
1017
 
1018
struct tty_ldisc tty_ldisc_N_TTY = {
1019
        TTY_LDISC_MAGIC,        /* magic */
1020
        0,                       /* num */
1021
        0,                       /* flags */
1022
        n_tty_open,             /* open */
1023
        n_tty_close,            /* close */
1024
        n_tty_flush_buffer,     /* flush_buffer */
1025
        n_tty_chars_in_buffer,  /* chars_in_buffer */
1026
        read_chan,              /* read */
1027
        write_chan,             /* write */
1028
        n_tty_ioctl,            /* ioctl */
1029
        n_tty_set_termios,      /* set_termios */
1030
        normal_select,          /* select */
1031
        n_tty_receive_buf,      /* receive_buf */
1032
        n_tty_receive_room,     /* receive_room */
1033
 
1034
};
1035
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.