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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [char/] [tty_io.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/drivers/char/tty_io.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
/*
8
 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9
 * or rs-channels. It also implements echoing, cooked mode etc.
10
 *
11
 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12
 *
13
 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14
 * tty_struct and tty_queue structures.  Previously there was an array
15
 * of 256 tty_struct's which was statically allocated, and the
16
 * tty_queue structures were allocated at boot time.  Both are now
17
 * dynamically allocated only when the tty is open.
18
 *
19
 * Also restructured routines so that there is more of a separation
20
 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21
 * the low-level tty routines (serial.c, pty.c, console.c).  This
22
 * makes for cleaner and more compact code.  -TYT, 9/17/92
23
 *
24
 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25
 * which can be dynamically activated and de-activated by the line
26
 * discipline handling modules (like SLIP).
27
 *
28
 * NOTE: pay no attention to the line discipline code (yet); its
29
 * interface is still subject to change in this version...
30
 * -- TYT, 1/31/92
31
 *
32
 * Added functionality to the OPOST tty handling.  No delays, but all
33
 * other bits should be there.
34
 *      -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
35
 *
36
 * Rewrote canonical mode and added more termios flags.
37
 *      -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38
 *
39
 * Reorganized FASYNC support so mouse code can share it.
40
 *      -- ctm@ardi.com, 9Sep95
41
 *
42
 * New TIOCLINUX variants added.
43
 *      -- mj@k332.feld.cvut.cz, 19-Nov-95
44
 *
45
 * Restrict vt switching via ioctl()
46
 *      -- grif@cs.ucr.edu, 5-Dec-95
47
 *
48
 * Rewrote init_dev and release_dev to eliminate races.
49
 *      -- Bill Hawes <whawes@star.net>, June 97
50
 *
51
 * Configurable CONSOLE, FRAMEBUFFER, and KEYBOARD
52
 *      -- Kenneth Albanowski <kjahds@kjahds.com>, '98
53
 */
54
 
55
#include <linux/config.h>
56
#include <linux/types.h>
57
#include <linux/major.h>
58
#include <linux/errno.h>
59
#include <linux/signal.h>
60
#include <linux/fcntl.h>
61
#include <linux/sched.h>
62
#include <linux/interrupt.h>
63
#include <linux/tty.h>
64
#include <linux/tty_flip.h>
65
#include <linux/timer.h>
66
#include <linux/ctype.h>
67
#include <linux/kd.h>
68
#include <linux/mm.h>
69
#include <linux/string.h>
70
#include <linux/malloc.h>
71
 
72
#include <asm/segment.h>
73
#include <asm/system.h>
74
#include <asm/bitops.h>
75
 
76
#include <linux/scc.h>
77
#include <linux/fb.h>
78
 
79
#include "kbd_kern.h"
80
#include "vt_kern.h"
81
#include "selection.h"
82
 
83
#ifdef CONFIG_KERNELD
84
#include <linux/kerneld.h>
85
#endif
86
 
87
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
88
#define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
89
 
90
#undef TTY_DEBUG_HANGUP
91
 
92
#define TTY_PARANOIA_CHECK
93
#define CHECK_TTY_COUNT
94
 
95
extern void do_blank_screen(int nopowersave);
96
extern void set_vesa_blanking(const unsigned long arg);
97
 
98
struct termios tty_std_termios;         /* for the benefit of tty drivers  */
99
struct tty_driver *tty_drivers = NULL;  /* linked list of tty drivers */
100
struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
101
 
102
/*
103
 * fg_console is the current virtual console,
104
 * last_console is the last used one,
105
 * want_console is the console we want to switch to,
106
 * kmsg_redirect is the console for kernel messages,
107
 * redirect is the pseudo-tty that console output
108
 * is redirected to if asked by TIOCCONS.
109
 */
110
int fg_console = 0;
111
int last_console = 0;
112
int want_console = -1;
113
int kmsg_redirect = 0;
114
struct tty_struct * redirect = NULL;
115
struct wait_queue * keypress_wait = NULL;
116
char vt_dont_switch = 0;
117
 
118
static void initialize_tty_struct(struct tty_struct *tty);
119
 
120
static int tty_read(struct inode *, struct file *, char *, int);
121
static int tty_write(struct inode *, struct file *, const char *, int);
122
static int tty_select(struct inode *, struct file *, int, select_table *);
123
static int tty_open(struct inode *, struct file *);
124
static void tty_release(struct inode *, struct file *);
125
static int tty_ioctl(struct inode * inode, struct file * file,
126
                     unsigned int cmd, unsigned long arg);
127
static int tty_fasync(struct inode * inode, struct file * filp, int on);
128
 
129
extern void reset_palette(int currcons) ;
130
extern void set_palette(void) ;
131
 
132
#ifndef MIN
133
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
134
#endif
135
 
136
/*
137
 * These two routines return the name of tty.  tty_name() should NOT
138
 * be used in interrupt drivers, since it's not re-entrant.  Use
139
 * _tty_name() instead.
140
 */
141
char *_tty_name(struct tty_struct *tty, char *buf)
142
{
143
        if (tty)
144
                sprintf(buf, "%s%d", tty->driver.name,
145
                        MINOR(tty->device) - tty->driver.minor_start +
146
                        tty->driver.name_base);
147
        else
148
                strcpy(buf, "NULL tty");
149
        return buf;
150
}
151
 
152
char *tty_name(struct tty_struct *tty)
153
{
154
        static char buf[64];
155
 
156
        return(_tty_name(tty, buf));
157
}
158
 
159
inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
160
                              const char *routine)
161
{
162
#ifdef TTY_PARANOIA_CHECK
163
        static const char *badmagic =
164
                "Warning: bad magic number for tty struct (%s) in %s\n";
165
        static const char *badtty =
166
                "Warning: null TTY for (%s) in %s\n";
167
 
168
        if (!tty) {
169
                printk(badtty, kdevname(device), routine);
170
                return 1;
171
        }
172
        if (tty->magic != TTY_MAGIC) {
173
                printk(badmagic, kdevname(device), routine);
174
                return 1;
175
        }
176
#endif
177
        return 0;
178
}
179
 
180
static int check_tty_count(struct tty_struct *tty, const char *routine)
181
{
182
#ifdef CHECK_TTY_COUNT
183
        struct file *f;
184
        int i, count = 0;
185
 
186
        for (f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
187
                if (!f->f_count)
188
                        continue;
189
                if (f->private_data == tty) {
190
                        count++;
191
                }
192
        }
193
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
194
            tty->driver.subtype == PTY_TYPE_SLAVE &&
195
            tty->link && tty->link->count)
196
                count++;
197
        if (tty->count != count) {
198
                printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
199
                       kdevname(tty->device), tty->count, count, routine);
200
                return count;
201
       }
202
#endif
203
        return 0;
204
}
205
 
206
int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
207
{
208
        if (disc < N_TTY || disc >= NR_LDISCS)
209
                return -EINVAL;
210
 
211
        if (new_ldisc) {
212
                ldiscs[disc] = *new_ldisc;
213
                ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
214
                ldiscs[disc].num = disc;
215
        } else
216
                memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
217
 
218
        return 0;
219
}
220
 
221
/* Set the discipline of a tty line. */
222
static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
223
{
224
        int     retval = 0;
225
        struct  tty_ldisc o_ldisc;
226
 
227
        if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
228
                return -EINVAL;
229
#ifdef CONFIG_KERNELD
230
        /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
231
        if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
232
                char modname [20];
233
                sprintf(modname, "tty-ldisc-%d", ldisc);
234
                request_module (modname);
235
        }
236
#endif
237
        if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
238
                return -EINVAL;
239
 
240
        if (tty->ldisc.num == ldisc)
241
                return 0;        /* We are already in the desired discipline */
242
        o_ldisc = tty->ldisc;
243
 
244
        tty_wait_until_sent(tty, 0);
245
 
246
        /* Shutdown the current discipline. */
247
        if (tty->ldisc.close)
248
                (tty->ldisc.close)(tty);
249
 
250
        /* Now set up the new line discipline. */
251
        tty->ldisc = ldiscs[ldisc];
252
        tty->termios->c_line = ldisc;
253
        if (tty->ldisc.open)
254
                retval = (tty->ldisc.open)(tty);
255
        if (retval < 0) {
256
                tty->ldisc = o_ldisc;
257
                tty->termios->c_line = tty->ldisc.num;
258
                if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
259
                        tty->ldisc = ldiscs[N_TTY];
260
                        tty->termios->c_line = N_TTY;
261
                        if (tty->ldisc.open) {
262
                                int r = tty->ldisc.open(tty);
263
 
264
                                if (r < 0)
265
                                        panic("Couldn't open N_TTY ldisc for "
266
                                              "%s --- error %d.",
267
                                              tty_name(tty), r);
268
                        }
269
                }
270
        }
271
        if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
272
                tty->driver.set_ldisc(tty);
273
        return retval;
274
}
275
 
276
/*
277
 * This routine returns a tty driver structure, given a device number
278
 */
279
struct tty_driver *get_tty_driver(kdev_t device)
280
{
281
        int     major, minor;
282
        struct tty_driver *p;
283
 
284
        minor = MINOR(device);
285
        major = MAJOR(device);
286
 
287
        for (p = tty_drivers; p; p = p->next) {
288
                if (p->major != major)
289
                        continue;
290
                if (minor < p->minor_start)
291
                        continue;
292
                if (minor >= p->minor_start + p->num)
293
                        continue;
294
                return p;
295
        }
296
        return NULL;
297
}
298
 
299
/*
300
 * If we try to write to, or set the state of, a terminal and we're
301
 * not in the foreground, send a SIGTTOU.  If the signal is blocked or
302
 * ignored, go ahead and perform the operation.  (POSIX 7.2)
303
 */
304
int tty_check_change(struct tty_struct * tty)
305
{
306
        if (current->tty != tty)
307
                return 0;
308
        if (tty->pgrp <= 0) {
309
                printk("tty_check_change: tty->pgrp <= 0!\n");
310
                return 0;
311
        }
312
        if (current->pgrp == tty->pgrp)
313
                return 0;
314
        if (is_ignored(SIGTTOU))
315
                return 0;
316
        if (is_orphaned_pgrp(current->pgrp))
317
                return -EIO;
318
        (void) kill_pg(current->pgrp,SIGTTOU,1);
319
        return -ERESTARTSYS;
320
}
321
 
322
static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
323
{
324
        return 0;
325
}
326
 
327
static int hung_up_tty_write(struct inode * inode, struct file * file, const char * buf, int count)
328
{
329
        return -EIO;
330
}
331
 
332
static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
333
{
334
        return 1;
335
}
336
 
337
static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
338
                             unsigned int cmd, unsigned long arg)
339
{
340
        return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
341
}
342
 
343
static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
344
{
345
        return -ESPIPE;
346
}
347
 
348
static struct file_operations tty_fops = {
349
        tty_lseek,
350
        tty_read,
351
        tty_write,
352
        NULL,           /* tty_readdir */
353
        tty_select,
354
        tty_ioctl,
355
        NULL,           /* tty_mmap */
356
        tty_open,
357
        tty_release,
358
        NULL,           /* tty_fsync */
359
        tty_fasync
360
};
361
 
362
static struct file_operations hung_up_tty_fops = {
363
        tty_lseek,
364
        hung_up_tty_read,
365
        hung_up_tty_write,
366
        NULL,           /* hung_up_tty_readdir */
367
        hung_up_tty_select,
368
        hung_up_tty_ioctl,
369
        NULL,           /* hung_up_tty_mmap */
370
        NULL,           /* hung_up_tty_open */
371
        tty_release,    /* hung_up_tty_release */
372
        NULL,           /* hung_up_tty_fsync  */
373
        NULL            /* hung_up_tty_fasync */
374
};
375
 
376
void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
377
{
378
        int i;
379
        struct file * filp;
380
        struct task_struct *p;
381
 
382
        if (!tty)
383
                return;
384
        check_tty_count(tty, "do_tty_hangup");
385
        for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
386
                if (!filp->f_count)
387
                        continue;
388
                if (filp->private_data != tty)
389
                        continue;
390
                if (!filp->f_inode)
391
                        continue;
392
                if (filp->f_inode->i_rdev == CONSOLE_DEV)
393
                        continue;
394
                if (filp->f_op != &tty_fops)
395
                        continue;
396
                tty_fasync(filp->f_inode, filp, 0);
397
                filp->f_op = fops;
398
        }
399
 
400
        if (tty->ldisc.flush_buffer)
401
                tty->ldisc.flush_buffer(tty);
402
        if (tty->driver.flush_buffer)
403
                tty->driver.flush_buffer(tty);
404
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
405
            tty->ldisc.write_wakeup)
406
                (tty->ldisc.write_wakeup)(tty);
407
        wake_up_interruptible(&tty->write_wait);
408
        wake_up_interruptible(&tty->read_wait);
409
 
410
        /*
411
         * Shutdown the current line discipline, and reset it to
412
         * N_TTY.
413
         */
414
        if (tty->ldisc.num != ldiscs[N_TTY].num) {
415
                if (tty->ldisc.close)
416
                        (tty->ldisc.close)(tty);
417
                tty->ldisc = ldiscs[N_TTY];
418
                tty->termios->c_line = N_TTY;
419
                if (tty->ldisc.open) {
420
                        i = (tty->ldisc.open)(tty);
421
                        if (i < 0)
422
                                printk("do_tty_hangup: N_TTY open: error %d\n",
423
                                       -i);
424
                }
425
        }
426
 
427
        for_each_task(p) {
428
                if ((tty->session > 0) && (p->session == tty->session) &&
429
                    p->leader) {
430
                        send_sig(SIGHUP,p,1);
431
                        send_sig(SIGCONT,p,1);
432
                        if (tty->pgrp > 0)
433
                                p->tty_old_pgrp = tty->pgrp;
434
                }
435
                if (p->tty == tty)
436
                        p->tty = NULL;
437
        }
438
        tty->flags = 0;
439
        tty->session = 0;
440
        tty->pgrp = -1;
441
        tty->ctrl_status = 0;
442
        if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
443
                *tty->termios = tty->driver.init_termios;
444
        if (tty->driver.hangup)
445
                (tty->driver.hangup)(tty);
446
}
447
 
448
void tty_hangup(struct tty_struct * tty)
449
{
450
#ifdef TTY_DEBUG_HANGUP
451
        printk("%s hangup...\n", tty_name(tty));
452
#endif
453
        do_tty_hangup(tty, &hung_up_tty_fops);
454
}
455
 
456
void tty_vhangup(struct tty_struct * tty)
457
{
458
#ifdef TTY_DEBUG_HANGUP
459
        printk("%s vhangup...\n", tty_name(tty));
460
#endif
461
        do_tty_hangup(tty, &hung_up_tty_fops);
462
}
463
 
464
int tty_hung_up_p(struct file * filp)
465
{
466
        return (filp->f_op == &hung_up_tty_fops);
467
}
468
 
469
/*
470
 * This function is typically called only by the session leader, when
471
 * it wants to disassociate itself from its controlling tty.
472
 *
473
 * It performs the following functions:
474
 *      (1)  Sends a SIGHUP and SIGCONT to the foreground process group
475
 *      (2)  Clears the tty from being controlling the session
476
 *      (3)  Clears the controlling tty for all processes in the
477
 *              session group.
478
 *
479
 * The argument on_exit is set to 1 if called when a process is
480
 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
481
 */
482
void disassociate_ctty(int on_exit)
483
{
484
        struct tty_struct *tty = current->tty;
485
        struct task_struct *p;
486
        int tty_pgrp = -1;
487
 
488
        if (tty) {
489
                tty_pgrp = tty->pgrp;
490
                if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
491
                        tty_vhangup(tty);
492
        } else {
493
                if (current->tty_old_pgrp) {
494
                        kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
495
                        kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
496
                }
497
                return;
498
        }
499
        if (tty_pgrp > 0) {
500
                kill_pg(tty_pgrp, SIGHUP, on_exit);
501
                if (!on_exit)
502
                        kill_pg(tty_pgrp, SIGCONT, on_exit);
503
        }
504
 
505
        current->tty_old_pgrp = 0;
506
        tty->session = 0;
507
        tty->pgrp = -1;
508
 
509
        for_each_task(p)
510
                if (p->session == current->session)
511
                        p->tty = NULL;
512
}
513
 
514
#ifdef CONFIG_CONSOLE
515
/*
516
 * Sometimes we want to wait until a particular VT has been activated. We
517
 * do it in a very simple manner. Everybody waits on a single queue and
518
 * get woken up at once. Those that are satisfied go on with their business,
519
 * while those not ready go back to sleep. Seems overkill to add a wait
520
 * to each vt just for this - usually this does nothing!
521
 */
522
static struct wait_queue *vt_activate_queue = NULL;
523
 
524
/*
525
 * Sleeps until a vt is activated, or the task is interrupted. Returns
526
 * 0 if activation, -1 if interrupted.
527
 */
528
int vt_waitactive(void)
529
{
530
        interruptible_sleep_on(&vt_activate_queue);
531
        return (current->signal & ~current->blocked) ? -1 : 0;
532
}
533
 
534
#define vt_wake_waitactive() wake_up(&vt_activate_queue)
535
 
536
void reset_vc(unsigned int new_console)
537
{
538
        vt_cons[new_console]->vc_mode = KD_TEXT;
539
        kbd_table[new_console].kbdmode = VC_XLATE;
540
        vt_cons[new_console]->vt_mode.mode = VT_AUTO;
541
        vt_cons[new_console]->vt_mode.waitv = 0;
542
        vt_cons[new_console]->vt_mode.relsig = 0;
543
        vt_cons[new_console]->vt_mode.acqsig = 0;
544
        vt_cons[new_console]->vt_mode.frsig = 0;
545
        vt_cons[new_console]->vt_pid = -1;
546
        vt_cons[new_console]->vt_newvt = -1;
547
        reset_palette (new_console) ;
548
}
549
 
550
/*
551
 * Performs the back end of a vt switch
552
 */
553
void complete_change_console(unsigned int new_console)
554
{
555
        unsigned char old_vc_mode;
556
 
557
        if ((new_console == fg_console) || (vt_dont_switch))
558
                return;
559
        if (!vc_cons_allocated(new_console))
560
                return;
561
        last_console = fg_console;
562
 
563
        /*
564
         * If we're switching, we could be going from KD_GRAPHICS to
565
         * KD_TEXT mode or vice versa, which means we need to blank or
566
         * unblank the screen later.
567
         */
568
        old_vc_mode = vt_cons[fg_console]->vc_mode;
569
        update_screen(new_console);
570
 
571
        /*
572
         * If this new console is under process control, send it a signal
573
         * telling it that it has acquired. Also check if it has died and
574
         * clean up (similar to logic employed in change_console())
575
         */
576
        if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
577
        {
578
                /*
579
                 * Send the signal as privileged - kill_proc() will
580
                 * tell us if the process has gone or something else
581
                 * is awry
582
                 */
583
                if (kill_proc(vt_cons[new_console]->vt_pid,
584
                              vt_cons[new_console]->vt_mode.acqsig,
585
                              1) != 0)
586
                {
587
                /*
588
                 * The controlling process has died, so we revert back to
589
                 * normal operation. In this case, we'll also change back
590
                 * to KD_TEXT mode. I'm not sure if this is strictly correct
591
                 * but it saves the agony when the X server dies and the screen
592
                 * remains blanked due to KD_GRAPHICS! It would be nice to do
593
                 * this outside of VT_PROCESS but there is no single process
594
                 * to account for and tracking tty count may be undesirable.
595
                 */
596
                        reset_vc(new_console);
597
                }
598
        }
599
 
600
        /*
601
         * We do this here because the controlling process above may have
602
         * gone, and so there is now a new vc_mode
603
         */
604
        if (old_vc_mode != vt_cons[new_console]->vc_mode)
605
        {
606
                if (vt_cons[new_console]->vc_mode == KD_TEXT)
607
                        do_unblank_screen();
608
                else
609
                        do_blank_screen(1);
610
        }
611
 
612
        /* Set the colour palette for this VT */
613
        if (vt_cons[new_console]->vc_mode == KD_TEXT)
614
                set_palette() ;
615
 
616
        /*
617
         * Wake anyone waiting for their VT to activate
618
         */
619
        vt_wake_waitactive();
620
        return;
621
}
622
 
623
/*
624
 * Performs the front-end of a vt switch
625
 */
626
void change_console(unsigned int new_console)
627
{
628
        if ((new_console == fg_console) || (vt_dont_switch))
629
                return;
630
        if (!vc_cons_allocated(new_console))
631
                return;
632
 
633
        /*
634
         * If this vt is in process mode, then we need to handshake with
635
         * that process before switching. Essentially, we store where that
636
         * vt wants to switch to and wait for it to tell us when it's done
637
         * (via VT_RELDISP ioctl).
638
         *
639
         * We also check to see if the controlling process still exists.
640
         * If it doesn't, we reset this vt to auto mode and continue.
641
         * This is a cheap way to track process control. The worst thing
642
         * that can happen is: we send a signal to a process, it dies, and
643
         * the switch gets "lost" waiting for a response; hopefully, the
644
         * user will try again, we'll detect the process is gone (unless
645
         * the user waits just the right amount of time :-) and revert the
646
         * vt to auto control.
647
         */
648
        if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
649
        {
650
                /*
651
                 * Send the signal as privileged - kill_proc() will
652
                 * tell us if the process has gone or something else
653
                 * is awry
654
                 */
655
                if (kill_proc(vt_cons[fg_console]->vt_pid,
656
                              vt_cons[fg_console]->vt_mode.relsig,
657
                              1) == 0)
658
                {
659
                        /*
660
                         * It worked. Mark the vt to switch to and
661
                         * return. The process needs to send us a
662
                         * VT_RELDISP ioctl to complete the switch.
663
                         */
664
                        vt_cons[fg_console]->vt_newvt = new_console;
665
                        return;
666
                }
667
 
668
                /*
669
                 * The controlling process has died, so we revert back to
670
                 * normal operation. In this case, we'll also change back
671
                 * to KD_TEXT mode. I'm not sure if this is strictly correct
672
                 * but it saves the agony when the X server dies and the screen
673
                 * remains blanked due to KD_GRAPHICS! It would be nice to do
674
                 * this outside of VT_PROCESS but there is no single process
675
                 * to account for and tracking tty count may be undesirable.
676
                 */
677
                reset_vc(fg_console);
678
 
679
                /*
680
                 * Fall through to normal (VT_AUTO) handling of the switch...
681
                 */
682
        }
683
 
684
        /*
685
         * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
686
         */
687
        if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
688
                return;
689
 
690
        complete_change_console(new_console);
691
}
692
#endif /*CONFIG_CONSOLE*/
693
 
694
void wait_for_keypress(void)
695
{
696
        sleep_on(&keypress_wait);
697
}
698
 
699
void stop_tty(struct tty_struct *tty)
700
{
701
        if (tty->stopped)
702
                return;
703
        tty->stopped = 1;
704
        if (tty->link && tty->link->packet) {
705
                tty->ctrl_status &= ~TIOCPKT_START;
706
                tty->ctrl_status |= TIOCPKT_STOP;
707
                wake_up_interruptible(&tty->link->read_wait);
708
        }
709
        if (tty->driver.stop)
710
                (tty->driver.stop)(tty);
711
}
712
 
713
void start_tty(struct tty_struct *tty)
714
{
715
        if (!tty->stopped)
716
                return;
717
        tty->stopped = 0;
718
        if (tty->link && tty->link->packet) {
719
                tty->ctrl_status &= ~TIOCPKT_STOP;
720
                tty->ctrl_status |= TIOCPKT_START;
721
                wake_up_interruptible(&tty->link->read_wait);
722
        }
723
        if (tty->driver.start)
724
                (tty->driver.start)(tty);
725
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
726
            tty->ldisc.write_wakeup)
727
                (tty->ldisc.write_wakeup)(tty);
728
        wake_up_interruptible(&tty->write_wait);
729
}
730
 
731
static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
732
{
733
        int i;
734
        struct tty_struct * tty;
735
 
736
        tty = (struct tty_struct *)file->private_data;
737
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
738
                return -EIO;
739
        if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
740
                return -EIO;
741
 
742
        /* This check not only needs to be done before reading, but also
743
           whenever read_chan() gets woken up after sleeping, so I've
744
           moved it to there.  This should only be done for the N_TTY
745
           line discipline, anyway.  Same goes for write_chan(). -- jlc. */
746
#if 0
747
        if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
748
            (tty->pgrp > 0) &&
749
            (current->tty == tty) &&
750
            (tty->pgrp != current->pgrp))
751
                if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
752
                        return -EIO;
753
                else {
754
                        (void) kill_pg(current->pgrp, SIGTTIN, 1);
755
                        return -ERESTARTSYS;
756
                }
757
#endif
758
        if (tty->ldisc.read)
759
                /* XXX casts are for what kernel-wide prototypes should be. */
760
                i = (tty->ldisc.read)(tty,file,(unsigned char *)buf,(unsigned int)count);
761
        else
762
                i = -EIO;
763
        if (i > 0)
764
                inode->i_atime = CURRENT_TIME;
765
        return i;
766
}
767
 
768
/*
769
 * Split writes up in sane blocksizes to avoid
770
 * denial-of-service type attacks
771
 */
772
static inline int do_tty_write(
773
        int (*write)(struct tty_struct *, struct file *, const unsigned char *, unsigned int),
774
        struct inode *inode,
775
        struct tty_struct *tty,
776
        struct file *file,
777
        const unsigned char *buf,
778
        unsigned int count)
779
{
780
        int ret = 0, written = 0;
781
 
782
        for (;;) {
783
                unsigned int size = PAGE_SIZE*2;
784
                if (size > count)
785
                        size = count;
786
                ret = write(tty, file, buf, size);
787
                if (ret <= 0)
788
                        break;
789
                written += ret;
790
                buf += ret;
791
                count -= ret;
792
                if (!count)
793
                        break;
794
                ret = -ERESTARTSYS;
795
                if (current->signal & ~current->blocked)
796
                        break;
797
                if (need_resched)
798
                        schedule();
799
        }
800
        if (written) {
801
                inode->i_mtime = CURRENT_TIME;
802
                ret = written;
803
        }
804
        return ret;
805
}
806
 
807
 
808
static int tty_write(struct inode * inode, struct file * file, const char * buf, int count)
809
{
810
        int is_console;
811
        struct tty_struct * tty;
812
 
813
        is_console = (inode->i_rdev == CONSOLE_DEV);
814
 
815
        if (is_console && redirect)
816
                tty = redirect;
817
        else
818
                tty = (struct tty_struct *)file->private_data;
819
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
820
                return -EIO;
821
        if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
822
                return -EIO;
823
#if 0
824
        if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
825
            (current->tty == tty) && (tty->pgrp != current->pgrp)) {
826
                if (is_orphaned_pgrp(current->pgrp))
827
                        return -EIO;
828
                if (!is_ignored(SIGTTOU)) {
829
                        (void) kill_pg(current->pgrp, SIGTTOU, 1);
830
                        return -ERESTARTSYS;
831
                }
832
        }
833
#endif
834
        if (!tty->ldisc.write)
835
                return -EIO;
836
        return do_tty_write(tty->ldisc.write,
837
                inode, tty, file,
838
                (const unsigned char *)buf,
839
                (unsigned int)count);
840
}
841
 
842
/* Semaphore to protect creating and releasing a tty */
843
static struct semaphore tty_sem = MUTEX;
844
static void down_tty_sem(int index)
845
{
846
        down(&tty_sem);
847
}
848
static void up_tty_sem(int index)
849
{
850
        up(&tty_sem);
851
}
852
static void release_mem(struct tty_struct *tty, int idx);
853
 
854
/*
855
 * Rewritten to remove races and properly clean up after a failed open.
856
 * The new code protects the open with a semaphore, so it's really
857
 * quite straightforward.  The semaphore locking can probably be
858
 * relaxed for the (most common) case of reopening a tty.
859
 */
860
static int init_dev(kdev_t device, struct tty_struct **ret_tty)
861
{
862
        struct tty_struct *tty, *o_tty;
863
        struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
864
        struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
865
        struct tty_driver *driver;
866
        int retval;
867
        int idx;
868
 
869
        driver = get_tty_driver(device);
870
        if (!driver)
871
                return -ENODEV;
872
 
873
        idx = MINOR(device) - driver->minor_start;
874
 
875
        /*
876
         * Check whether we need to acquire the tty semaphore to avoid
877
         * race conditions.  For now, play it safe.
878
         */
879
        down_tty_sem(idx);
880
 
881
        /* check whether we're reopening an existing tty */
882
        tty = driver->table[idx];
883
        if(tty) goto fast_track;
884
 
885
        /*
886
         * First time open is complex, especially for PTY devices.
887
         * This code guarantees that either everything succeeds and the
888
         * TTY is ready for operation, or else the table slots are vacated
889
         * and the allocated memory released.  (Except that the termios
890
         * and locked termios may be retained.)
891
         */
892
 
893
        o_tty = NULL;
894
        tp = o_tp = NULL;
895
        ltp = o_ltp = NULL;
896
 
897
        tty = (struct tty_struct*) get_free_page(GFP_KERNEL);
898
        if(!tty)
899
                goto fail_no_mem;
900
        initialize_tty_struct(tty);
901
        tty->device = device;
902
        tty->driver = *driver;
903
 
904
        tp_loc = &driver->termios[idx];
905
        if (!*tp_loc) {
906
                tp = (struct termios *) kmalloc(sizeof(struct termios),
907
                                                GFP_KERNEL);
908
                if (!tp)
909
                        goto free_mem_out;
910
                *tp = driver->init_termios;
911
        }
912
 
913
        ltp_loc = &driver->termios_locked[idx];
914
        if (!*ltp_loc) {
915
                ltp = (struct termios *) kmalloc(sizeof(struct termios),
916
                                                 GFP_KERNEL);
917
                if (!ltp)
918
                        goto free_mem_out;
919
                memset(ltp, 0, sizeof(struct termios));
920
        }
921
 
922
        if (driver->type == TTY_DRIVER_TYPE_PTY) {
923
                o_tty = (struct tty_struct *) get_free_page(GFP_KERNEL);
924
                if (!o_tty)
925
                        goto free_mem_out;
926
                initialize_tty_struct(o_tty);
927
                o_tty->device = (kdev_t) MKDEV(driver->other->major,
928
                                        driver->other->minor_start + idx);
929
                o_tty->driver = *driver->other;
930
 
931
                o_tp_loc  = &driver->other->termios[idx];
932
                if (!*o_tp_loc) {
933
                        o_tp = (struct termios *)
934
                                kmalloc(sizeof(struct termios), GFP_KERNEL);
935
                        if (!o_tp)
936
                                goto free_mem_out;
937
                        *o_tp = driver->other->init_termios;
938
                }
939
 
940
                o_ltp_loc = &driver->other->termios_locked[idx];
941
                if (!*o_ltp_loc) {
942
                        o_ltp = (struct termios *)
943
                                kmalloc(sizeof(struct termios), GFP_KERNEL);
944
                        if (!o_ltp)
945
                                goto free_mem_out;
946
                        memset(o_ltp, 0, sizeof(struct termios));
947
                }
948
 
949
                /*
950
                 * Everything allocated ... set up the o_tty structure.
951
                 */
952
                driver->other->table[idx] = o_tty;
953
                if (!*o_tp_loc)
954
                        *o_tp_loc = o_tp;
955
                if (!*o_ltp_loc)
956
                        *o_ltp_loc = o_ltp;
957
                o_tty->termios = *o_tp_loc;
958
                o_tty->termios_locked = *o_ltp_loc;
959
                (*driver->other->refcount)++;
960
                if (driver->subtype == PTY_TYPE_MASTER)
961
                        o_tty->count++;
962
 
963
                /* Establish the links in both directions */
964
                tty->link   = o_tty;
965
                o_tty->link = tty;
966
        }
967
 
968
        /*
969
         * All structures have been allocated, so now we install them.
970
         * Failures after this point use release_mem to clean up, so
971
         * there's no need to null out the local pointers.
972
         */
973
        driver->table[idx] = tty;       /* FIXME: this is broken and
974
        probably causes ^D bug. tty->private_date does not (yet) point
975
        to a console, if keypress comes now, await armagedon.
976
 
977
        also, driver->table is accessed from interrupt for vt case,
978
        and this does not look like atomic access at all. */
979
 
980
        if (!*tp_loc)
981
                *tp_loc = tp;
982
        if (!*ltp_loc)
983
                *ltp_loc = ltp;
984
        tty->termios = *tp_loc;
985
        tty->termios_locked = *ltp_loc;
986
        (*driver->refcount)++;
987
        tty->count++;
988
 
989
        /*
990
         * Structures all installed ... call the ldisc open routines.
991
         * If we fail here just call release_mem to clean up.  No need
992
         * to decrement the use counts, as release_mem doesn't care.
993
         */
994
        if (tty->ldisc.open) {
995
                retval = (tty->ldisc.open)(tty);
996
                if (retval)
997
                        goto release_mem_out;
998
        }
999
        if (o_tty && o_tty->ldisc.open) {
1000
                retval = (o_tty->ldisc.open)(o_tty);
1001
                if (retval) {
1002
                        if (tty->ldisc.close)
1003
                                (tty->ldisc.close)(tty);
1004
                        goto release_mem_out;
1005
                }
1006
        }
1007
        goto success;
1008
 
1009
        /*
1010
         * This fast open can be used if the tty is already open.
1011
         * No memory is allocated, and the only failures are from
1012
         * attempting to open a closing tty or attempting multiple
1013
         * opens on a pty master.
1014
         */
1015
fast_track:
1016
        retval = -EIO;
1017
        if (test_bit(TTY_CLOSING, &tty->flags))
1018
                goto end_init;
1019
 
1020
        if (driver->type == TTY_DRIVER_TYPE_PTY &&
1021
            driver->subtype == PTY_TYPE_MASTER) {
1022
                /*
1023
                 * special case for PTY masters: only one open permitted,
1024
                 * and the slave side open count is incremented as well.
1025
                 */
1026
                if (tty->count)
1027
                        goto end_init;
1028
                tty->link->count++;
1029
        }
1030
        tty->count++;
1031
        tty->driver = *driver; /* N.B. why do this every time?? */
1032
 
1033
success:
1034
        retval = 0;
1035
        *ret_tty = tty;
1036
 
1037
        /* All paths come through here to release the semaphore */
1038
end_init:
1039
        up_tty_sem(idx);
1040
        return retval;
1041
 
1042
        /* Release locally allocated memory ... nothing placed in slots */
1043
free_mem_out:
1044
        if (o_tp)
1045
                kfree_s(o_tp, sizeof(struct termios));
1046
        if (o_tty)
1047
                free_page((unsigned long) o_tty);
1048
        if (ltp)
1049
                kfree_s(ltp, sizeof(struct termios));
1050
        if (tp)
1051
                kfree_s(tp, sizeof(struct termios));
1052
        free_page((unsigned long) tty);
1053
 
1054
fail_no_mem:
1055
        retval = -ENOMEM;
1056
        goto end_init;
1057
 
1058
        /* call the tty release_mem routine to clean out this slot */
1059
release_mem_out:
1060
        printk("init_dev: ldisc open failed, clearing slot %d\n", idx);
1061
        release_mem(tty, idx);
1062
        goto end_init;
1063
}
1064
 
1065
/*
1066
 * Releases memory associated with a tty structure, and clears out the
1067
 * driver table slots.
1068
 */
1069
static void release_mem(struct tty_struct *tty, int idx)
1070
{
1071
        struct tty_struct *o_tty;
1072
        struct termios *tp;
1073
 
1074
        if ((o_tty = tty->link) != NULL) {
1075
                o_tty->driver.table[idx] = NULL;
1076
                if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1077
                        tp = o_tty->driver.termios[idx];
1078
                        o_tty->driver.termios[idx] = NULL;
1079
                        kfree_s(tp, sizeof(struct termios));
1080
                }
1081
                o_tty->magic = 0;
1082
                (*o_tty->driver.refcount)--;
1083
                free_page((unsigned long) o_tty);
1084
        }
1085
 
1086
        tty->driver.table[idx] = NULL;
1087
        if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1088
                tp = tty->driver.termios[idx];
1089
                tty->driver.termios[idx] = NULL;
1090
                kfree_s(tp, sizeof(struct termios));
1091
        }
1092
        tty->magic = 0;
1093
        (*tty->driver.refcount)--;
1094
        free_page((unsigned long) tty);
1095
}
1096
 
1097
/*
1098
 * Even releasing the tty structures is a tricky business.. We have
1099
 * to be very careful that the structures are all released at the
1100
 * same time, as interrupts might otherwise get the wrong pointers.
1101
 */
1102
static void release_dev(struct file * filp)
1103
{
1104
        struct tty_struct *tty, *o_tty;
1105
        int     pty_master, tty_closing, o_tty_closing, do_sleep;
1106
        int     idx;
1107
 
1108
        tty = (struct tty_struct *)filp->private_data;
1109
        if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
1110
                return;
1111
 
1112
        check_tty_count(tty, "release_dev");
1113
 
1114
        tty_fasync(filp->f_inode, filp, 0);
1115
 
1116
        idx = MINOR(tty->device) - tty->driver.minor_start;
1117
        pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1118
                      tty->driver.subtype == PTY_TYPE_MASTER);
1119
        o_tty = tty->link;
1120
 
1121
#ifdef TTY_PARANOIA_CHECK
1122
        if (idx < 0 || idx >= tty->driver.num) {
1123
                printk("release_dev: bad idx when trying to free (%s)\n",
1124
                       kdevname(tty->device));
1125
                return;
1126
        }
1127
        if (tty != tty->driver.table[idx]) {
1128
                printk("release_dev: driver.table[%d] not tty for (%s)\n",
1129
                       idx, kdevname(tty->device));
1130
                return;
1131
        }
1132
        if (tty->termios != tty->driver.termios[idx]) {
1133
                printk("release_dev: driver.termios[%d] not termios "
1134
                       "for (%s)\n",
1135
                       idx, kdevname(tty->device));
1136
                return;
1137
        }
1138
        if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1139
                printk("release_dev: driver.termios_locked[%d] not "
1140
                       "termios_locked for (%s)\n",
1141
                       idx, kdevname(tty->device));
1142
                return;
1143
        }
1144
#endif
1145
 
1146
#ifdef TTY_DEBUG_HANGUP
1147
        printk("release_dev of %s (tty count=%d)...", tty_name(tty),
1148
               tty->count);
1149
#endif
1150
 
1151
#ifdef TTY_PARANOIA_CHECK
1152
        if (tty->driver.other) {
1153
                if (o_tty != tty->driver.other->table[idx]) {
1154
                        printk("release_dev: other->table[%d] not o_tty for ("
1155
                               "%s)\n",
1156
                               idx, kdevname(tty->device));
1157
                        return;
1158
                }
1159
                if (o_tty->termios != tty->driver.other->termios[idx]) {
1160
                        printk("release_dev: other->termios[%d] not o_termios "
1161
                               "for (%s)\n",
1162
                               idx, kdevname(tty->device));
1163
                        return;
1164
                }
1165
                if (o_tty->termios_locked !=
1166
                      tty->driver.other->termios_locked[idx]) {
1167
                        printk("release_dev: other->termios_locked[%d] not "
1168
                               "o_termios_locked for (%s)\n",
1169
                               idx, kdevname(tty->device));
1170
                        return;
1171
                }
1172
                if (o_tty->link != tty) {
1173
                        printk("release_dev: bad pty pointers\n");
1174
                        return;
1175
                }
1176
        }
1177
#endif
1178
 
1179
        if (tty->driver.close)
1180
                tty->driver.close(tty, filp);
1181
 
1182
        /*
1183
         * Sanity check: if tty->count is going to zero, there shouldn't be
1184
         * any waiters on tty->read_wait or tty->write_wait.  We test the
1185
         * wait queues and kick everyone out _before_ actually starting to
1186
         * close.  This ensures that we won't block while releasing the tty
1187
         * structure.
1188
         *
1189
         * The test for the o_tty closing is necessary, since the master and
1190
         * slave sides may close in any order.  If the slave side closes out
1191
         * first, its count will be one, since the master side holds an open.
1192
         * Thus this test wouldn't be triggered at the time the slave closes,
1193
         * so we do it now.
1194
         *
1195
         * Note that it's possible for the tty to be opened again while we're
1196
         * flushing out waiters.  By recalculating the closing flags before
1197
         * each iteration we avoid any problems.
1198
         */
1199
        while (1) {
1200
                tty_closing = tty->count <= 1;
1201
                o_tty_closing = o_tty &&
1202
                        (o_tty->count <= (pty_master ? 1 : 0));
1203
                do_sleep = 0;
1204
 
1205
                if (tty_closing) {
1206
                        if (waitqueue_active(&tty->read_wait)) {
1207
                                wake_up(&tty->read_wait);
1208
                                do_sleep++;
1209
                        }
1210
                        if (waitqueue_active(&tty->write_wait)) {
1211
                                wake_up(&tty->write_wait);
1212
                                do_sleep++;
1213
                        }
1214
                }
1215
                if (o_tty_closing) {
1216
                        if (waitqueue_active(&o_tty->read_wait)) {
1217
                                wake_up(&o_tty->read_wait);
1218
                                do_sleep++;
1219
                        }
1220
                        if (waitqueue_active(&o_tty->write_wait)) {
1221
                                wake_up(&o_tty->write_wait);
1222
                                do_sleep++;
1223
                        }
1224
                }
1225
                if (!do_sleep)
1226
                        break;
1227
 
1228
                printk("release_dev: %s: read/write wait queue active!\n",
1229
                       tty_name(tty));
1230
                schedule();
1231
        }
1232
 
1233
        /*
1234
         * The closing flags are now consistent with the open counts on
1235
         * both sides, and we've completed the last operation that could
1236
         * block, so it's safe to proceed with closing.
1237
         */
1238
 
1239
        if (pty_master) {
1240
                if (--o_tty->count < 0) {
1241
                        printk("release_dev: bad pty slave count (%d) for %s\n",
1242
                               o_tty->count, tty_name(o_tty));
1243
                        o_tty->count = 0;
1244
                }
1245
        }
1246
        if (--tty->count < 0) {
1247
                printk("release_dev: bad tty->count (%d) for %s\n",
1248
                       tty->count, tty_name(tty));
1249
                tty->count = 0;
1250
        }
1251
 
1252
        /*
1253
         * Perform some housekeeping before deciding whether to return.
1254
         *
1255
         * Set the TTY_CLOSING flag if this was the last open.  In the
1256
         * case of a pty we may have to wait around for the other side
1257
         * to close, and TTY_CLOSING makes sure we can't be reopened.
1258
         */
1259
        if(tty_closing)
1260
                set_bit(TTY_CLOSING, &tty->flags);
1261
        if(o_tty_closing)
1262
                set_bit(TTY_CLOSING, &o_tty->flags);
1263
 
1264
        /*
1265
         * If _either_ side is closing, make sure there aren't any
1266
         * processes that still think tty or o_tty is their controlling
1267
         * tty.  Also, clear redirect if it points to either tty.
1268
         */
1269
        if (tty_closing || o_tty_closing) {
1270
                struct task_struct *p;
1271
 
1272
                for_each_task(p) {
1273
                        if (p->tty == tty || (o_tty && p->tty == o_tty))
1274
                                p->tty = NULL;
1275
                }
1276
 
1277
                if (redirect == tty || (o_tty && redirect == o_tty))
1278
                        redirect = NULL;
1279
        }
1280
 
1281
        /* check whether both sides are closing ... */
1282
        if (!tty_closing || (o_tty && !o_tty_closing))
1283
                return;
1284
        filp->private_data = 0;
1285
 
1286
#ifdef TTY_DEBUG_HANGUP
1287
        printk("freeing tty structure...");
1288
#endif
1289
 
1290
        /*
1291
         * Shutdown the current line discipline, and reset it to N_TTY.
1292
         * N.B. why reset ldisc when we're releasing the memory??
1293
         */
1294
        if (tty->ldisc.close)
1295
                (tty->ldisc.close)(tty);
1296
        tty->ldisc = ldiscs[N_TTY];
1297
        tty->termios->c_line = N_TTY;
1298
        if (o_tty) {
1299
                if (o_tty->ldisc.close)
1300
                        (o_tty->ldisc.close)(o_tty);
1301
                o_tty->ldisc = ldiscs[N_TTY];
1302
        }
1303
 
1304
        /*
1305
         * Make sure that the tty's task queue isn't activated.  If it
1306
         * is, take it out of the linked list.  The tqueue isn't used by
1307
         * pty's, so skip the test for them.
1308
         */
1309
        if (tty->driver.type != TTY_DRIVER_TYPE_PTY) {
1310
                cli();
1311
                if (tty->flip.tqueue.sync) {
1312
                        struct tq_struct *tq, *prev;
1313
 
1314
                        for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1315
                                if (tq == &tty->flip.tqueue) {
1316
                                        if (prev)
1317
                                                prev->next = tq->next;
1318
                                        else
1319
                                                tq_timer = tq->next;
1320
                                        break;
1321
                                }
1322
                        }
1323
                }
1324
                sti();
1325
        }
1326
 
1327
        /*
1328
         * The release_mem function takes care of the details of clearing
1329
         * the slots and preserving the termios structure.
1330
         */
1331
        release_mem(tty, idx);
1332
}
1333
 
1334
/*
1335
 * tty_open and tty_release keep up the tty count that contains the
1336
 * number of opens done on a tty. We cannot use the inode-count, as
1337
 * different inodes might point to the same tty.
1338
 *
1339
 * Open-counting is needed for pty masters, as well as for keeping
1340
 * track of serial lines: DTR is dropped when the last close happens.
1341
 * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1342
 *
1343
 * The termios state of a pty is reset on first open so that
1344
 * settings don't persist across reuse.
1345
 */
1346
static int tty_open(struct inode * inode, struct file * filp)
1347
{
1348
        struct tty_struct *tty;
1349
        int minor;
1350
        int noctty, retval;
1351
        kdev_t device;
1352
 
1353
retry_open:
1354
        noctty = filp->f_flags & O_NOCTTY;
1355
        device = inode->i_rdev;
1356
        if (device == TTY_DEV) {
1357
                if (!current->tty)
1358
                        return -ENXIO;
1359
                device = current->tty->device;
1360
                /* noctty = 1; */
1361
        }
1362
        if (device == CONSOLE_DEV) {
1363
                device = MKDEV(TTY_MAJOR, fg_console+1);
1364
                noctty = 1;
1365
        }
1366
        minor = MINOR(device);
1367
 
1368
        retval = init_dev(device, &tty);
1369
        if (retval)
1370
                return retval;
1371
        filp->private_data = tty;
1372
        check_tty_count(tty, "tty_open");
1373
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1374
            tty->driver.subtype == PTY_TYPE_MASTER)
1375
                noctty = 1;
1376
#ifdef TTY_DEBUG_HANGUP
1377
        printk("opening %s...", tty_name(tty));
1378
#endif
1379
        if (tty->driver.open)
1380
                retval = tty->driver.open(tty, filp);
1381
        else
1382
                retval = -ENODEV;
1383
 
1384
        if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1385
                retval = -EBUSY;
1386
 
1387
        if (retval) {
1388
#ifdef TTY_DEBUG_HANGUP
1389
                printk("error %d in opening %s...", retval, tty_name(tty));
1390
#endif
1391
 
1392
                release_dev(filp);
1393
                if (retval != -ERESTARTSYS)
1394
                        return retval;
1395
                if (current->signal & ~current->blocked)
1396
                        return retval;
1397
                schedule();
1398
                /*
1399
                 * Need to reset f_op in case a hangup happened.
1400
                 */
1401
                filp->f_op = &tty_fops;
1402
                goto retry_open;
1403
        }
1404
        if (!noctty &&
1405
            current->leader &&
1406
            !current->tty &&
1407
            tty->session == 0) {
1408
                current->tty = tty;
1409
                current->tty_old_pgrp = 0;
1410
                tty->session = current->session;
1411
                tty->pgrp = current->pgrp;
1412
        }
1413
        return 0;
1414
}
1415
 
1416
static void tty_release(struct inode * inode, struct file * filp)
1417
{
1418
        release_dev(filp);
1419
}
1420
 
1421
static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
1422
{
1423
        struct tty_struct * tty;
1424
 
1425
        tty = (struct tty_struct *)filp->private_data;
1426
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1427
                return 0;
1428
 
1429
        if (tty->ldisc.select)
1430
                return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1431
        return 0;
1432
}
1433
 
1434
/*
1435
 * fasync_helper() is used by some character device drivers (mainly mice)
1436
 * to set up the fasync queue. It returns negative on error, 0 if it did
1437
 * no changes and positive if it added/deleted the entry.
1438
 */
1439
int fasync_helper(struct inode * inode, struct file * filp, int on, struct fasync_struct **fapp)
1440
{
1441
        struct fasync_struct *fa, **fp;
1442
        unsigned long flags;
1443
 
1444
        for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
1445
                if (fa->fa_file == filp)
1446
                        break;
1447
        }
1448
 
1449
        if (on) {
1450
                if (fa)
1451
                        return 0;
1452
                fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1453
                if (!fa)
1454
                        return -ENOMEM;
1455
                fa->magic = FASYNC_MAGIC;
1456
                fa->fa_file = filp;
1457
                save_flags(flags);
1458
                cli();
1459
                fa->fa_next = *fapp;
1460
                *fapp = fa;
1461
                restore_flags(flags);
1462
                return 1;
1463
        }
1464
        if (!fa)
1465
                return 0;
1466
        save_flags(flags);
1467
        cli();
1468
        *fp = fa->fa_next;
1469
        restore_flags(flags);
1470
        kfree(fa);
1471
        return 1;
1472
}
1473
 
1474
static int tty_fasync(struct inode * inode, struct file * filp, int on)
1475
{
1476
        struct tty_struct * tty;
1477
        int retval;
1478
 
1479
        tty = (struct tty_struct *)filp->private_data;
1480
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1481
                return 0;
1482
 
1483
        retval = fasync_helper(inode, filp, on, &tty->fasync);
1484
        if (retval <= 0)
1485
                return retval;
1486
 
1487
        if (on) {
1488
                if (!waitqueue_active(&tty->read_wait))
1489
                        tty->minimum_to_wake = 1;
1490
                if (filp->f_owner.pid == 0) {
1491
                        filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1492
                        filp->f_owner.uid = current->uid;
1493
                        filp->f_owner.euid = current->euid;
1494
                }
1495
        } else {
1496
                if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1497
                        tty->minimum_to_wake = N_TTY_BUF_SIZE;
1498
        }
1499
        return 0;
1500
}
1501
 
1502
#if 0
1503
/*
1504
 * XXX does anyone use this anymore?!?
1505
 */
1506
static int do_get_ps_info(unsigned long arg)
1507
{
1508
        struct tstruct {
1509
                int flag;
1510
                int present[NR_TASKS];
1511
                struct task_struct tasks[NR_TASKS];
1512
        };
1513
        struct tstruct *ts = (struct tstruct *)arg;
1514
        struct task_struct **p;
1515
        char *c, *d;
1516
        int i, n = 0;
1517
 
1518
        i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1519
        if (i)
1520
                return i;
1521
        for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1522
                if (*p)
1523
                {
1524
                        c = (char *)(*p);
1525
                        d = (char *)(ts->tasks+n);
1526
                        for (i=0 ; i<sizeof(struct task_struct) ; i++)
1527
                                put_user(*c++, d++);
1528
                        put_user(1, ts->present+n);
1529
                }
1530
                else
1531
                        put_user(0, ts->present+n);
1532
        return(0);
1533
}
1534
#endif
1535
 
1536
static int tty_ioctl(struct inode * inode, struct file * file,
1537
                     unsigned int cmd, unsigned long arg)
1538
{
1539
        int     retval;
1540
        struct tty_struct * tty;
1541
        struct tty_struct * real_tty;
1542
        struct winsize tmp_ws;
1543
        pid_t pgrp;
1544
        unsigned char   ch;
1545
        char    mbz = 0;
1546
 
1547
        tty = (struct tty_struct *)file->private_data;
1548
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1549
                return -EINVAL;
1550
 
1551
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1552
            tty->driver.subtype == PTY_TYPE_MASTER)
1553
                real_tty = tty->link;
1554
        else
1555
                real_tty = tty;
1556
 
1557
        switch (cmd) {
1558
                case TIOCSTI:
1559
                        if ((current->tty != tty) && !suser())
1560
                                return -EPERM;
1561
                        retval = verify_area(VERIFY_READ, (void *) arg, 1);
1562
                        if (retval)
1563
                                return retval;
1564
                        ch = get_user((char *) arg);
1565
                        tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1566
                        return 0;
1567
                case TIOCGWINSZ:
1568
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
1569
                                             sizeof (struct winsize));
1570
                        if (retval)
1571
                                return retval;
1572
                        memcpy_tofs((struct winsize *) arg, &tty->winsize,
1573
                                    sizeof (struct winsize));
1574
                        return 0;
1575
                case TIOCSWINSZ:
1576
                        retval = verify_area(VERIFY_READ, (void *) arg,
1577
                                             sizeof (struct winsize));
1578
                        if (retval)
1579
                                return retval;
1580
                        memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
1581
                                      sizeof (struct winsize));
1582
                        if (memcmp(&tmp_ws, &tty->winsize,
1583
                                   sizeof(struct winsize))) {
1584
                                if (tty->pgrp > 0)
1585
                                        kill_pg(tty->pgrp, SIGWINCH, 1);
1586
                                if ((real_tty->pgrp != tty->pgrp) &&
1587
                                    (real_tty->pgrp > 0))
1588
                                        kill_pg(real_tty->pgrp, SIGWINCH, 1);
1589
                        }
1590
                        tty->winsize = tmp_ws;
1591
                        real_tty->winsize = tmp_ws;
1592
                        return 0;
1593
                case TIOCCONS:
1594
                        if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1595
                                if (!suser())
1596
                                        return -EPERM;
1597
                                redirect = NULL;
1598
                                return 0;
1599
                        }
1600
                        if (redirect)
1601
                                return -EBUSY;
1602
                        redirect = real_tty;
1603
                        return 0;
1604
                case FIONBIO:
1605
                        retval = verify_area(VERIFY_READ, (void *) arg, sizeof(int));
1606
                        if (retval)
1607
                                return retval;
1608
                        arg = get_user((unsigned int *) arg);
1609
                        if (arg)
1610
                                file->f_flags |= O_NONBLOCK;
1611
                        else
1612
                                file->f_flags &= ~O_NONBLOCK;
1613
                        return 0;
1614
                case TIOCEXCL:
1615
                        set_bit(TTY_EXCLUSIVE, &tty->flags);
1616
                        return 0;
1617
                case TIOCNXCL:
1618
                        clear_bit(TTY_EXCLUSIVE, &tty->flags);
1619
                        return 0;
1620
                case TIOCNOTTY:
1621
                        if (current->tty != tty)
1622
                                return -ENOTTY;
1623
                        if (current->leader)
1624
                                disassociate_ctty(0);
1625
                        current->tty = NULL;
1626
                        return 0;
1627
                case TIOCSCTTY:
1628
                        if (current->leader &&
1629
                            (current->session == tty->session))
1630
                                return 0;
1631
                        /*
1632
                         * The process must be a session leader and
1633
                         * not have a controlling tty already.
1634
                         */
1635
                        if (!current->leader || current->tty)
1636
                                return -EPERM;
1637
                        if (tty->session > 0) {
1638
                                /*
1639
                                 * This tty is already the controlling
1640
                                 * tty for another session group!
1641
                                 */
1642
                                if ((arg == 1) && suser()) {
1643
                                        /*
1644
                                         * Steal it away
1645
                                         */
1646
                                        struct task_struct *p;
1647
 
1648
                                        for_each_task(p)
1649
                                                if (p->tty == tty)
1650
                                                        p->tty = NULL;
1651
                                } else
1652
                                        return -EPERM;
1653
                        }
1654
                        current->tty = tty;
1655
                        current->tty_old_pgrp = 0;
1656
                        tty->session = current->session;
1657
                        tty->pgrp = current->pgrp;
1658
                        return 0;
1659
                case TIOCGPGRP:
1660
                        /*
1661
                         * (tty == real_tty) is a cheap way of
1662
                         * testing if the tty is NOT a master pty.
1663
                         */
1664
                        if (tty == real_tty && current->tty != real_tty)
1665
                                return -ENOTTY;
1666
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
1667
                                             sizeof (pid_t));
1668
                        if (retval)
1669
                                return retval;
1670
                        put_user(real_tty->pgrp, (pid_t *) arg);
1671
                        return 0;
1672
                case TIOCSPGRP:
1673
                        retval = tty_check_change(real_tty);
1674
                        if (retval == -EIO)
1675
                                return -ENOTTY;
1676
                        if (retval)
1677
                                return retval;
1678
                        if (!current->tty ||
1679
                            (current->tty != real_tty) ||
1680
                            (real_tty->session != current->session))
1681
                                return -ENOTTY;
1682
                        pgrp = get_user((pid_t *) arg);
1683
                        if (pgrp < 0)
1684
                                return -EINVAL;
1685
                        if (session_of_pgrp(pgrp) != current->session)
1686
                                return -EPERM;
1687
                        real_tty->pgrp = pgrp;
1688
                        return 0;
1689
                case TIOCGETD:
1690
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
1691
                                             sizeof (int));
1692
                        if (retval)
1693
                                return retval;
1694
                        put_user(tty->ldisc.num, (int *) arg);
1695
                        return 0;
1696
                case TIOCSETD:
1697
                        retval = tty_check_change(tty);
1698
                        if (retval)
1699
                                return retval;
1700
                        retval = verify_area(VERIFY_READ, (void *) arg,
1701
                                             sizeof (int));
1702
                        if (retval)
1703
                                return retval;
1704
                        arg = get_user((int *) arg);
1705
                        return tty_set_ldisc(tty, arg);
1706
#ifdef CONFIG_CONSOLE
1707
                case TIOCLINUX:
1708
                        if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
1709
                                return -EINVAL;
1710
                        if (current->tty != tty && !suser())
1711
                                return -EPERM;
1712
                        retval = verify_area(VERIFY_READ, (void *) arg, 1);
1713
                        if (retval)
1714
                                return retval;
1715
                        switch (retval = get_user((char *)arg))
1716
                        {
1717
                                case 0:
1718
                                case 8:
1719
                                case 9:
1720
                                        printk("TIOCLINUX (0/8/9) ioctl is gone - use /dev/vcs\n");
1721
                                        return -EINVAL;
1722
#if 0
1723
                                case 1:
1724
                                        printk("Deprecated TIOCLINUX (1) ioctl\n");
1725
                                        return do_get_ps_info(arg);
1726
#endif
1727
                                case 2:
1728
                                        return set_selection(arg, tty, 1);
1729
                                case 3:
1730
                                        return paste_selection(tty);
1731
                                case 4:
1732
                                        do_unblank_screen();
1733
                                        return 0;
1734
                                case 5:
1735
                                        return sel_loadlut(arg);
1736
                                case 6:
1737
                        /*
1738
                         * Make it possible to react to Shift+Mousebutton.
1739
                         * Note that 'shift_state' is an undocumented
1740
                         * kernel-internal variable; programs not closely
1741
                         * related to the kernel should not use this.
1742
                         */
1743
                                        retval = verify_area(VERIFY_WRITE, (void *) arg, 1);
1744
                                        if (retval)
1745
                                                return retval;
1746
                                        put_user(shift_state,(char *) arg);
1747
                                        return 0;
1748
                                case 7:
1749
                                        retval = verify_area(VERIFY_WRITE, (void *) arg, 1);
1750
                                        if (retval)
1751
                                                return retval;
1752
                                        put_user(mouse_reporting(),(char *) arg);
1753
                                        return 0;
1754
                                case 10:
1755
                                        set_vesa_blanking(arg);
1756
                                        return 0;
1757
                                case 11:        /* set kmsg redirect */
1758
                                        if (!suser())
1759
                                                return -EPERM;
1760
                                        retval = verify_area(VERIFY_READ,
1761
                                                (void *) arg+1, 1);
1762
                                        if (retval)
1763
                                                return retval;
1764
                                        kmsg_redirect = get_user((char *)arg+1);
1765
                                        return 0;
1766
                                case 12:        /* get fg_console */
1767
                                        return fg_console;
1768
                                default:
1769
                                        return -EINVAL;
1770
                        }
1771
#endif /*CONFIG_CONSOLE*/
1772
 
1773
                case TIOCTTYGSTRUCT:
1774
                        retval = verify_area(VERIFY_WRITE, (void *) arg,
1775
                                                sizeof(struct tty_struct));
1776
                        if (retval)
1777
                                return retval;
1778
                        memcpy_tofs((struct tty_struct *) arg,
1779
                                    tty, sizeof(struct tty_struct));
1780
                        return 0;
1781
                default:
1782
                        if (tty->driver.ioctl) {
1783
                                retval = (tty->driver.ioctl)(tty, file,
1784
                                                             cmd, arg);
1785
                                if (retval != -ENOIOCTLCMD)
1786
                                        return retval;
1787
                        }
1788
                        if (tty->ldisc.ioctl) {
1789
                                retval = (tty->ldisc.ioctl)(tty, file,
1790
                                                            cmd, arg);
1791
                                if (retval != -ENOIOCTLCMD)
1792
                                        return retval;
1793
                        }
1794
                        return -EINVAL;
1795
                }
1796
}
1797
 
1798
 
1799
/*
1800
 * This implements the "Secure Attention Key" ---  the idea is to
1801
 * prevent trojan horses by killing all processes associated with this
1802
 * tty when the user hits the "Secure Attention Key".  Required for
1803
 * super-paranoid applications --- see the Orange Book for more details.
1804
 *
1805
 * This code could be nicer; ideally it should send a HUP, wait a few
1806
 * seconds, then send a INT, and then a KILL signal.  But you then
1807
 * have to coordinate with the init process, since all processes associated
1808
 * with the current tty must be dead before the new getty is allowed
1809
 * to spawn.
1810
 */
1811
void do_SAK( struct tty_struct *tty)
1812
{
1813
#ifdef TTY_SOFT_SAK
1814
        tty_hangup(tty);
1815
#else
1816
        struct task_struct **p;
1817
        int session;
1818
        int             i;
1819
        struct file     *filp;
1820
 
1821
        if (!tty)
1822
                return;
1823
        session  = tty->session;
1824
        if (tty->ldisc.flush_buffer)
1825
                tty->ldisc.flush_buffer(tty);
1826
        if (tty->driver.flush_buffer)
1827
                tty->driver.flush_buffer(tty);
1828
        for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1829
                if (!(*p))
1830
                        continue;
1831
                if (((*p)->tty == tty) ||
1832
                    ((session > 0) && ((*p)->session == session)))
1833
                        send_sig(SIGKILL, *p, 1);
1834
                else if ((*p)->files) {
1835
                        for (i=0; i < NR_OPEN; i++) {
1836
                                filp = (*p)->files->fd[i];
1837
                                if (filp && (filp->f_op == &tty_fops) &&
1838
                                    (filp->private_data == tty)) {
1839
                                        send_sig(SIGKILL, *p, 1);
1840
                                        break;
1841
                                }
1842
                        }
1843
                }
1844
        }
1845
#endif
1846
}
1847
 
1848
/*
1849
 * This routine is called out of the software interrupt to flush data
1850
 * from the flip buffer to the line discipline.
1851
 */
1852
static void flush_to_ldisc(void *private_)
1853
{
1854
        struct tty_struct *tty = (struct tty_struct *) private_;
1855
        unsigned char   *cp;
1856
        char            *fp;
1857
        int             count;
1858
 
1859
        if (tty->flip.buf_num) {
1860
                cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1861
                fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1862
                tty->flip.buf_num = 0;
1863
 
1864
                cli();
1865
                tty->flip.char_buf_ptr = tty->flip.char_buf;
1866
                tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1867
        } else {
1868
                cp = tty->flip.char_buf;
1869
                fp = tty->flip.flag_buf;
1870
                tty->flip.buf_num = 1;
1871
 
1872
                cli();
1873
                tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1874
                tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1875
        }
1876
        count = tty->flip.count;
1877
        tty->flip.count = 0;
1878
        sti();
1879
 
1880
#if 0
1881
        if (count > tty->max_flip_cnt)
1882
                tty->max_flip_cnt = count;
1883
#endif
1884
        tty->ldisc.receive_buf(tty, cp, fp, count);
1885
}
1886
 
1887
/*
1888
 * This subroutine initializes a tty structure.
1889
 */
1890
static void initialize_tty_struct(struct tty_struct *tty)
1891
{
1892
        memset(tty, 0, sizeof(struct tty_struct));
1893
        tty->magic = TTY_MAGIC;
1894
        tty->ldisc = ldiscs[N_TTY];
1895
        tty->pgrp = -1;
1896
        tty->flip.char_buf_ptr = tty->flip.char_buf;
1897
        tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1898
        tty->flip.tqueue.routine = flush_to_ldisc;
1899
        tty->flip.tqueue.data = tty;
1900
}
1901
 
1902
/*
1903
 * The default put_char routine if the driver did not define one.
1904
 */
1905
void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1906
{
1907
        tty->driver.write(tty, 0, &ch, 1);
1908
}
1909
 
1910
/*
1911
 * Called by a tty driver to register itself.
1912
 */
1913
int tty_register_driver(struct tty_driver *driver)
1914
{
1915
        int error;
1916
 
1917
        if (driver->flags & TTY_DRIVER_INSTALLED)
1918
                return 0;
1919
 
1920
        error = register_chrdev(driver->major, driver->name, &tty_fops);
1921
        if (error < 0)
1922
                return error;
1923 582 simons
 
1924 199 simons
        else if(driver->major == 0)
1925
                driver->major = error;
1926
 
1927
        if (!driver->put_char)
1928
                driver->put_char = tty_default_put_char;
1929
 
1930
        driver->prev = 0;
1931
        driver->next = tty_drivers;
1932
        if (tty_drivers) tty_drivers->prev = driver;
1933
        tty_drivers = driver;
1934
        return error;
1935
}
1936
 
1937
/*
1938
 * Called by a tty driver to unregister itself.
1939
 */
1940
int tty_unregister_driver(struct tty_driver *driver)
1941
{
1942
        int     retval;
1943
        struct tty_driver *p;
1944
        int     i, found = 0;
1945
        const char *othername = NULL;
1946
        struct termios *tp;
1947
 
1948
        if (*driver->refcount)
1949
                return -EBUSY;
1950
 
1951
        for (p = tty_drivers; p; p = p->next) {
1952
                if (p == driver)
1953
                        found++;
1954
                else if (p->major == driver->major)
1955
                        othername = p->name;
1956
        }
1957
 
1958
        if (!found)
1959
                return -ENOENT;
1960
 
1961
        if (othername == NULL) {
1962
                retval = unregister_chrdev(driver->major, driver->name);
1963
                if (retval)
1964
                        return retval;
1965
        } else
1966
                register_chrdev(driver->major, othername, &tty_fops);
1967
 
1968
        if (driver->prev)
1969
                driver->prev->next = driver->next;
1970
        else
1971
                tty_drivers = driver->next;
1972
 
1973
        if (driver->next)
1974
                driver->next->prev = driver->prev;
1975
 
1976
        for (i = 0; i < driver->num; i++) {
1977
                tp = driver->termios[i];
1978
                if (tp != NULL) {
1979
                        kfree_s(tp, sizeof(struct termios));
1980
                        driver->termios[i] = NULL;
1981
                }
1982
                tp = driver->termios_locked[i];
1983
                if (tp != NULL) {
1984
                        kfree_s(tp, sizeof(struct termios));
1985
                        driver->termios_locked[i] = NULL;
1986
                }
1987
        }
1988
        return 0;
1989
}
1990
 
1991
 
1992
/*
1993
 * Initialize the console device. This is called *early*, so
1994
 * we can't necessarily depend on lots of kernel help here.
1995
 * Just do some early initializations, and do the complex setup
1996
 * later.
1997
 */
1998
long console_init(long kmem_start, long kmem_end)
1999
{
2000
        /* Setup the default TTY line discipline. */
2001
        memset(ldiscs, 0, sizeof(ldiscs));
2002
        (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2003
 
2004
        /*
2005
         * Set up the standard termios.  Individual tty drivers may
2006
         * deviate from this; this is used as a template.
2007
         */
2008
        memset(&tty_std_termios, 0, sizeof(struct termios));
2009
        memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2010
        tty_std_termios.c_iflag = ICRNL | IXON;
2011
        tty_std_termios.c_oflag = OPOST | ONLCR;
2012
        tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2013
        tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2014
                ECHOCTL | ECHOKE | IEXTEN;
2015
 
2016
#ifdef CONFIG_CONSOLE
2017
        /*
2018
         * set up the console device so that later boot sequences can
2019
         * inform about problems etc..
2020
         */
2021
        return con_init(kmem_start);
2022
#else /* !CONFIG_CONSOLE */
2023
        return kmem_start;
2024
#endif /* !CONFIG_CONSOLE */
2025
}
2026
 
2027
static struct tty_driver dev_tty_driver, dev_console_driver;
2028
 
2029
/*
2030
 * Ok, now we can initialize the rest of the tty devices and can count
2031
 * on memory allocations, interrupts etc..
2032
 */
2033
int tty_init(void)
2034
{
2035
        if (sizeof(struct tty_struct) > PAGE_SIZE)
2036
                panic("size of tty structure > PAGE_SIZE!");
2037
 
2038
        /*
2039
         * dev_tty_driver and dev_console_driver are actually magic
2040
         * devices which get redirected at open time.  Nevertheless,
2041
         * we register them so that register_chrdev is called
2042
         * appropriately.
2043
         */
2044
        memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2045
        dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2046
        dev_tty_driver.name = "tty";
2047
        dev_tty_driver.name_base = 0;
2048
        dev_tty_driver.major = TTY_MAJOR;
2049
        dev_tty_driver.minor_start = 0;
2050
        dev_tty_driver.num = 1;
2051
 
2052
        if (tty_register_driver(&dev_tty_driver))
2053
                panic("Couldn't register /dev/tty driver\n");
2054
 
2055
        dev_console_driver = dev_tty_driver;
2056
        dev_console_driver.name = "console";
2057
        dev_console_driver.major = TTYAUX_MAJOR;
2058
 
2059
        if (tty_register_driver(&dev_console_driver))
2060
                panic("Couldn't register /dev/console driver\n");
2061
 
2062
#ifdef CONFIG_FRAMEBUFFER
2063
        fbmem_init();
2064
#endif
2065
 
2066
#ifdef CONFIG_KEYBOARD
2067
        kbd_init();
2068
#endif
2069
#ifdef CONFIG_COLDFIRE_SERIAL
2070
        mcfrs_init();
2071
#endif
2072
#ifdef CONFIG_SERIAL
2073
        rs_init();
2074
#endif
2075
 
2076
#ifdef CONFIG_68328_SERIAL
2077
        rs68328_init();
2078
#endif
2079
 
2080
#ifdef CONFIG_68332_SERIAL
2081
        rs68332_init();
2082
#endif
2083
#ifdef CONFIG_SCC
2084
        scc_init();
2085
#endif
2086
#ifdef CONFIG_CYCLADES
2087
        cy_init();
2088
#endif
2089
#ifdef CONFIG_STALLION
2090
        stl_init();
2091
#endif
2092
#ifdef CONFIG_ISTALLION
2093
        stli_init();
2094
#endif
2095
#ifdef CONFIG_DIGI
2096
        pcxe_init();
2097
#endif
2098
#ifdef CONFIG_RISCOM8
2099
        riscom8_init();
2100
#endif
2101
#ifdef CONFIG_SPECIALIX
2102
        specialix_init();
2103
#endif
2104
        pty_init();
2105
#ifdef CONFIG_CONSOLE
2106
        vcs_init();
2107
#endif
2108
#ifdef CONFIG_68328_DIGI
2109
        mc68328digi_init();
2110
#endif
2111
        return 0;
2112
}

powered by: WebSVN 2.1.0

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