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

Subversion Repositories or1k_old

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

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

powered by: WebSVN 2.1.0

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