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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [char/] [n_tty.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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