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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [tty_io.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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
 * Move console and virtual terminal code to more appropriate files,
49
 * implement CONFIG_VT and generalize console device interface.
50
 *      -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51
 *
52
 * Rewrote init_dev and release_dev to eliminate races.
53
 *      -- Bill Hawes <whawes@star.net>, June 97
54
 *
55
 * Added devfs support.
56
 *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57
 *
58
 * Added support for a Unix98-style ptmx device.
59
 *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60
 *
61
 * Reduced memory usage for older ARM systems
62
 *      -- Russell King <rmk@arm.linux.org.uk>
63
 *
64
 * Move do_SAK() into process context.  Less stack use in devfs functions.
65
 * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
66
 */
67
 
68
#include <linux/config.h>
69
#include <linux/types.h>
70
#include <linux/major.h>
71
#include <linux/errno.h>
72
#include <linux/signal.h>
73
#include <linux/fcntl.h>
74
#include <linux/sched.h>
75
#include <linux/interrupt.h>
76
#include <linux/tty.h>
77
#include <linux/tty_driver.h>
78
#include <linux/tty_flip.h>
79
#include <linux/devpts_fs.h>
80
#include <linux/file.h>
81
#include <linux/console.h>
82
#include <linux/timer.h>
83
#include <linux/ctype.h>
84
#include <linux/kd.h>
85
#include <linux/mm.h>
86
#include <linux/string.h>
87
#include <linux/slab.h>
88
#include <linux/poll.h>
89
#include <linux/proc_fs.h>
90
#include <linux/init.h>
91
#include <linux/module.h>
92
#include <linux/smp_lock.h>
93
 
94
#include <asm/uaccess.h>
95
#include <asm/system.h>
96
#include <asm/bitops.h>
97
 
98
#include <linux/kbd_kern.h>
99
#include <linux/vt_kern.h>
100
#include <linux/selection.h>
101
#include <linux/devfs_fs_kernel.h>
102
 
103
#include <linux/kmod.h>
104
 
105
#ifdef CONFIG_VT
106
extern void con_init_devfs (void);
107
#endif
108
 
109
extern void disable_early_printk(void);
110
 
111
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
112
#define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
113
#define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
114
#define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
115
 
116
#undef TTY_DEBUG_HANGUP
117
 
118
#define TTY_PARANOIA_CHECK 1
119
#define CHECK_TTY_COUNT 1
120
 
121
struct termios tty_std_termios;         /* for the benefit of tty drivers  */
122
struct tty_driver *tty_drivers;         /* linked list of tty drivers */
123
struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
124
 
125
#ifdef CONFIG_UNIX98_PTYS
126
extern struct tty_driver ptm_driver[];  /* Unix98 pty masters; for /dev/ptmx */
127
extern struct tty_driver pts_driver[];  /* Unix98 pty slaves;  for /dev/ptmx */
128
#endif
129
 
130
static void initialize_tty_struct(struct tty_struct *tty);
131
 
132
static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
133
static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
134
static unsigned int tty_poll(struct file *, poll_table *);
135
static int tty_open(struct inode *, struct file *);
136
static int tty_release(struct inode *, struct file *);
137
int tty_ioctl(struct inode * inode, struct file * file,
138
              unsigned int cmd, unsigned long arg);
139
static int tty_fasync(int fd, struct file * filp, int on);
140
extern int vme_scc_init (void);
141
extern long vme_scc_console_init(void);
142
extern int serial167_init(void);
143
extern long serial167_console_init(void);
144
extern void console_8xx_init(void);
145
extern void au1x00_serial_console_init(void);
146
extern int rs_8xx_init(void);
147
extern void mac_scc_console_init(void);
148
extern void hwc_console_init(void);
149
extern void hwc_tty_init(void);
150
extern void con3215_init(void);
151
extern void tty3215_init(void);
152
extern void tub3270_con_init(void);
153
extern void tub3270_init(void);
154
extern void rs285_console_init(void);
155
extern void sa1100_rs_console_init(void);
156
extern void sgi_serial_console_init(void);
157
extern void sn_sal_serial_console_init(void);
158
extern void sci_console_init(void);
159
extern void dec_serial_console_init(void);
160
extern void tx3912_console_init(void);
161
extern void tx3912_rs_init(void);
162
extern void txx927_console_init(void);
163
extern void txx9_rs_init(void);
164
extern void txx9_serial_console_init(void);
165
extern void sb1250_serial_console_init(void);
166
extern void arc_console_init(void);
167
extern int hvc_console_init(void);
168
 
169
#ifndef MIN
170
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
171
#endif
172
#ifndef MAX
173
#define MAX(a,b)        ((a) < (b) ? (b) : (a))
174
#endif
175
 
176
static struct tty_struct *alloc_tty_struct(void)
177
{
178
        struct tty_struct *tty;
179
 
180
        tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
181
        if (tty)
182
                memset(tty, 0, sizeof(struct tty_struct));
183
        return tty;
184
}
185
 
186
static inline void free_tty_struct(struct tty_struct *tty)
187
{
188
        kfree(tty);
189
}
190
 
191
/*
192
 * This routine returns the name of tty.
193
 */
194
static char *
195
_tty_make_name(struct tty_struct *tty, const char *name, char *buf)
196
{
197
        int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
198
 
199
        if (!tty) /* Hmm.  NULL pointer.  That's fun. */
200
                strcpy(buf, "NULL tty");
201
        else
202
                sprintf(buf, name,
203
                        idx + tty->driver.name_base);
204
 
205
        return buf;
206
}
207
 
208
#define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
209
                         (tty)->driver.name_base)
210
 
211
char *tty_name(struct tty_struct *tty, char *buf)
212
{
213
        return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
214
}
215
 
216
inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
217
                              const char *routine)
218
{
219
#ifdef TTY_PARANOIA_CHECK
220
        static const char badmagic[] = KERN_WARNING
221
                "Warning: bad magic number for tty struct (%s) in %s\n";
222
        static const char badtty[] = KERN_WARNING
223
                "Warning: null TTY for (%s) in %s\n";
224
 
225
        if (!tty) {
226
                printk(badtty, kdevname(device), routine);
227
                return 1;
228
        }
229
        if (tty->magic != TTY_MAGIC) {
230
                printk(badmagic, kdevname(device), routine);
231
                return 1;
232
        }
233
#endif
234
        return 0;
235
}
236
 
237
static int check_tty_count(struct tty_struct *tty, const char *routine)
238
{
239
#ifdef CHECK_TTY_COUNT
240
        struct list_head *p;
241
        int count = 0;
242
 
243
        file_list_lock();
244
        for(p = tty->tty_files.next; p != &tty->tty_files; p = p->next) {
245
                if(list_entry(p, struct file, f_list)->private_data == tty)
246
                        count++;
247
        }
248
        file_list_unlock();
249
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
250
            tty->driver.subtype == PTY_TYPE_SLAVE &&
251
            tty->link && tty->link->count)
252
                count++;
253
        if (tty->count != count) {
254
                printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
255
                                    "!= #fd's(%d) in %s\n",
256
                       kdevname(tty->device), tty->count, count, routine);
257
                return count;
258
       }
259
#endif
260
        return 0;
261
}
262
 
263
int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
264
{
265
        if (disc < N_TTY || disc >= NR_LDISCS)
266
                return -EINVAL;
267
 
268
        if (new_ldisc) {
269
                ldiscs[disc] = *new_ldisc;
270
                ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
271
                ldiscs[disc].num = disc;
272
        } else
273
                memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
274
 
275
        return 0;
276
}
277
 
278
EXPORT_SYMBOL(tty_register_ldisc);
279
 
280
/* Set the discipline of a tty line. */
281
static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
282
{
283
        int     retval = 0;
284
        struct  tty_ldisc o_ldisc;
285
        char buf[64];
286
 
287
        if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
288
                return -EINVAL;
289
        /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
290
        /* Cyrus Durgin <cider@speakeasy.org> */
291
        if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
292
                char modname [20];
293
                sprintf(modname, "tty-ldisc-%d", ldisc);
294
                request_module (modname);
295
        }
296
        if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
297
                return -EINVAL;
298
 
299
        if (tty->ldisc.num == ldisc)
300
                return 0;        /* We are already in the desired discipline */
301
        o_ldisc = tty->ldisc;
302
 
303
        tty_wait_until_sent(tty, 0);
304
 
305
        /* Shutdown the current discipline. */
306
        if (tty->ldisc.close)
307
                (tty->ldisc.close)(tty);
308
 
309
        /* Now set up the new line discipline. */
310
        tty->ldisc = ldiscs[ldisc];
311
        tty->termios->c_line = ldisc;
312
        if (tty->ldisc.open)
313
                retval = (tty->ldisc.open)(tty);
314
        if (retval < 0) {
315
                tty->ldisc = o_ldisc;
316
                tty->termios->c_line = tty->ldisc.num;
317
                if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
318
                        tty->ldisc = ldiscs[N_TTY];
319
                        tty->termios->c_line = N_TTY;
320
                        if (tty->ldisc.open) {
321
                                int r = tty->ldisc.open(tty);
322
 
323
                                if (r < 0)
324
                                        panic("Couldn't open N_TTY ldisc for "
325
                                              "%s --- error %d.",
326
                                              tty_name(tty, buf), r);
327
                        }
328
                }
329
        }
330
        if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
331
                tty->driver.set_ldisc(tty);
332
        return retval;
333
}
334
 
335
/*
336
 * This routine returns a tty driver structure, given a device number
337
 */
338
struct tty_driver *get_tty_driver(kdev_t device)
339
{
340
        int     major, minor;
341
        struct tty_driver *p;
342
 
343
        minor = MINOR(device);
344
        major = MAJOR(device);
345
 
346
        for (p = tty_drivers; p; p = p->next) {
347
                if (p->major != major)
348
                        continue;
349
                if (minor < p->minor_start)
350
                        continue;
351
                if (minor >= p->minor_start + p->num)
352
                        continue;
353
                return p;
354
        }
355
        return NULL;
356
}
357
 
358
/*
359
 * If we try to write to, or set the state of, a terminal and we're
360
 * not in the foreground, send a SIGTTOU.  If the signal is blocked or
361
 * ignored, go ahead and perform the operation.  (POSIX 7.2)
362
 */
363
int tty_check_change(struct tty_struct * tty)
364
{
365
        if (current->tty != tty)
366
                return 0;
367
        if (tty->pgrp <= 0) {
368
                printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
369
                return 0;
370
        }
371
        if (current->pgrp == tty->pgrp)
372
                return 0;
373
        if (is_ignored(SIGTTOU))
374
                return 0;
375
        if (is_orphaned_pgrp(current->pgrp))
376
                return -EIO;
377
        (void) kill_pg(current->pgrp,SIGTTOU,1);
378
        return -ERESTARTSYS;
379
}
380
 
381
static ssize_t hung_up_tty_read(struct file * file, char * buf,
382
                                size_t count, loff_t *ppos)
383
{
384
        /* Can't seek (pread) on ttys.  */
385
        if (ppos != &file->f_pos)
386
                return -ESPIPE;
387
        return 0;
388
}
389
 
390
static ssize_t hung_up_tty_write(struct file * file, const char * buf,
391
                                 size_t count, loff_t *ppos)
392
{
393
        /* Can't seek (pwrite) on ttys.  */
394
        if (ppos != &file->f_pos)
395
                return -ESPIPE;
396
        return -EIO;
397
}
398
 
399
/* No kernel lock held - none needed ;) */
400
static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
401
{
402
        return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
403
}
404
 
405
static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
406
                             unsigned int cmd, unsigned long arg)
407
{
408
        return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
409
}
410
 
411
static struct file_operations tty_fops = {
412
        llseek:         no_llseek,
413
        read:           tty_read,
414
        write:          tty_write,
415
        poll:           tty_poll,
416
        ioctl:          tty_ioctl,
417
        open:           tty_open,
418
        release:        tty_release,
419
        fasync:         tty_fasync,
420
};
421
 
422
static struct file_operations hung_up_tty_fops = {
423
        llseek:         no_llseek,
424
        read:           hung_up_tty_read,
425
        write:          hung_up_tty_write,
426
        poll:           hung_up_tty_poll,
427
        ioctl:          hung_up_tty_ioctl,
428
        release:        tty_release,
429
};
430
 
431
static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
432
static struct file *redirect;
433
/*
434
 * This can be called by the "eventd" kernel thread.  That is process synchronous,
435
 * but doesn't hold any locks, so we need to make sure we have the appropriate
436
 * locks for what we're doing..
437
 */
438
void do_tty_hangup(void *data)
439
{
440
        struct tty_struct *tty = (struct tty_struct *) data;
441
        struct file * cons_filp = NULL;
442
        struct file *f = NULL;
443
        struct task_struct *p;
444
        struct list_head *l;
445
        int    closecount = 0, n;
446
 
447
        if (!tty)
448
                return;
449
 
450
        /* inuse_filps is protected by the single kernel lock */
451
        lock_kernel();
452
 
453
        spin_lock(&redirect_lock);
454
        if (redirect && redirect->private_data == tty) {
455
                f = redirect;
456
                redirect = NULL;
457
        }
458
        spin_unlock(&redirect_lock);
459
 
460
        check_tty_count(tty, "do_tty_hangup");
461
        file_list_lock();
462
        for (l = tty->tty_files.next; l != &tty->tty_files; l = l->next) {
463
                struct file * filp = list_entry(l, struct file, f_list);
464
                if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
465
                    filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
466
                        cons_filp = filp;
467
                        continue;
468
                }
469
                if (filp->f_op != &tty_fops)
470
                        continue;
471
                closecount++;
472
                tty_fasync(-1, filp, 0); /* can't block */
473
                filp->f_op = &hung_up_tty_fops;
474
        }
475
        file_list_unlock();
476
 
477
        /* FIXME! What are the locking issues here? This may me overdoing things.. */
478
        {
479
                unsigned long flags;
480
 
481
                save_flags(flags); cli();
482
                if (tty->ldisc.flush_buffer)
483
                        tty->ldisc.flush_buffer(tty);
484
                if (tty->driver.flush_buffer)
485
                        tty->driver.flush_buffer(tty);
486
                if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
487
                    tty->ldisc.write_wakeup)
488
                        (tty->ldisc.write_wakeup)(tty);
489
                restore_flags(flags);
490
        }
491
 
492
        wake_up_interruptible(&tty->write_wait);
493
        wake_up_interruptible(&tty->read_wait);
494
 
495
        /*
496
         * Shutdown the current line discipline, and reset it to
497
         * N_TTY.
498
         */
499
        if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
500
                *tty->termios = tty->driver.init_termios;
501
        if (tty->ldisc.num != ldiscs[N_TTY].num) {
502
                if (tty->ldisc.close)
503
                        (tty->ldisc.close)(tty);
504
                tty->ldisc = ldiscs[N_TTY];
505
                tty->termios->c_line = N_TTY;
506
                if (tty->ldisc.open) {
507
                        int i = (tty->ldisc.open)(tty);
508
                        if (i < 0)
509
                                printk(KERN_ERR "do_tty_hangup: N_TTY open: "
510
                                                "error %d\n", -i);
511
                }
512
        }
513
 
514
        read_lock(&tasklist_lock);
515
        for_each_task(p) {
516
                if ((tty->session > 0) && (p->session == tty->session) &&
517
                    p->leader) {
518
                        send_sig(SIGHUP,p,1);
519
                        send_sig(SIGCONT,p,1);
520
                        if (tty->pgrp > 0)
521
                                p->tty_old_pgrp = tty->pgrp;
522
                }
523
                if (p->tty == tty)
524
                        p->tty = NULL;
525
        }
526
        read_unlock(&tasklist_lock);
527
 
528
        tty->flags = 0;
529
        tty->session = 0;
530
        tty->pgrp = -1;
531
        tty->ctrl_status = 0;
532
        /*
533
         *      If one of the devices matches a console pointer, we
534
         *      cannot just call hangup() because that will cause
535
         *      tty->count and state->count to go out of sync.
536
         *      So we just call close() the right number of times.
537
         */
538
        if (cons_filp) {
539
                if (tty->driver.close)
540
                        for (n = 0; n < closecount; n++)
541
                                tty->driver.close(tty, cons_filp);
542
        } else if (tty->driver.hangup)
543
                (tty->driver.hangup)(tty);
544
        unlock_kernel();
545
        if (f)
546
                fput(f);
547
}
548
 
549
void tty_hangup(struct tty_struct * tty)
550
{
551
#ifdef TTY_DEBUG_HANGUP
552
        char    buf[64];
553
 
554
        printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
555
#endif
556
        schedule_task(&tty->tq_hangup);
557
}
558
 
559
void tty_vhangup(struct tty_struct * tty)
560
{
561
#ifdef TTY_DEBUG_HANGUP
562
        char    buf[64];
563
 
564
        printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
565
#endif
566
        do_tty_hangup((void *) tty);
567
}
568
 
569
int tty_hung_up_p(struct file * filp)
570
{
571
        return (filp->f_op == &hung_up_tty_fops);
572
}
573
 
574
/*
575
 * This function is typically called only by the session leader, when
576
 * it wants to disassociate itself from its controlling tty.
577
 *
578
 * It performs the following functions:
579
 *      (1)  Sends a SIGHUP and SIGCONT to the foreground process group
580
 *      (2)  Clears the tty from being controlling the session
581
 *      (3)  Clears the controlling tty for all processes in the
582
 *              session group.
583
 *
584
 * The argument on_exit is set to 1 if called when a process is
585
 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
586
 */
587
void disassociate_ctty(int on_exit)
588
{
589
        struct tty_struct *tty = current->tty;
590
        struct task_struct *p;
591
        int tty_pgrp = -1;
592
 
593
        if (tty) {
594
                tty_pgrp = tty->pgrp;
595
                if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
596
                        tty_vhangup(tty);
597
        } else {
598
                if (current->tty_old_pgrp) {
599
                        kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
600
                        kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
601
                }
602
                return;
603
        }
604
        if (tty_pgrp > 0) {
605
                kill_pg(tty_pgrp, SIGHUP, on_exit);
606
                if (!on_exit)
607
                        kill_pg(tty_pgrp, SIGCONT, on_exit);
608
        }
609
 
610
        current->tty_old_pgrp = 0;
611
        tty->session = 0;
612
        tty->pgrp = -1;
613
 
614
        read_lock(&tasklist_lock);
615
        for_each_task(p)
616
                if (p->session == current->session)
617
                        p->tty = NULL;
618
        read_unlock(&tasklist_lock);
619
}
620
 
621
void stop_tty(struct tty_struct *tty)
622
{
623
        if (tty->stopped)
624
                return;
625
        tty->stopped = 1;
626
        if (tty->link && tty->link->packet) {
627
                tty->ctrl_status &= ~TIOCPKT_START;
628
                tty->ctrl_status |= TIOCPKT_STOP;
629
                wake_up_interruptible(&tty->link->read_wait);
630
        }
631
        if (tty->driver.stop)
632
                (tty->driver.stop)(tty);
633
}
634
 
635
void start_tty(struct tty_struct *tty)
636
{
637
        if (!tty->stopped || tty->flow_stopped)
638
                return;
639
        tty->stopped = 0;
640
        if (tty->link && tty->link->packet) {
641
                tty->ctrl_status &= ~TIOCPKT_STOP;
642
                tty->ctrl_status |= TIOCPKT_START;
643
                wake_up_interruptible(&tty->link->read_wait);
644
        }
645
        if (tty->driver.start)
646
                (tty->driver.start)(tty);
647
        if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
648
            tty->ldisc.write_wakeup)
649
                (tty->ldisc.write_wakeup)(tty);
650
        wake_up_interruptible(&tty->write_wait);
651
}
652
 
653
static ssize_t tty_read(struct file * file, char * buf, size_t count,
654
                        loff_t *ppos)
655
{
656
        int i;
657
        struct tty_struct * tty;
658
        struct inode *inode;
659
 
660
        /* Can't seek (pread) on ttys.  */
661
        if (ppos != &file->f_pos)
662
                return -ESPIPE;
663
 
664
        tty = (struct tty_struct *)file->private_data;
665
        inode = file->f_dentry->d_inode;
666
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
667
                return -EIO;
668
        if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
669
                return -EIO;
670
 
671
        /* This check not only needs to be done before reading, but also
672
           whenever read_chan() gets woken up after sleeping, so I've
673
           moved it to there.  This should only be done for the N_TTY
674
           line discipline, anyway.  Same goes for write_chan(). -- jlc. */
675
#if 0
676
        if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
677
            (tty->pgrp > 0) &&
678
            (current->tty == tty) &&
679
            (tty->pgrp != current->pgrp))
680
                if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
681
                        return -EIO;
682
                else {
683
                        (void) kill_pg(current->pgrp, SIGTTIN, 1);
684
                        return -ERESTARTSYS;
685
                }
686
#endif
687
        lock_kernel();
688
        if (tty->ldisc.read)
689
                i = (tty->ldisc.read)(tty,file,buf,count);
690
        else
691
                i = -EIO;
692
        unlock_kernel();
693
        if (i > 0)
694
                inode->i_atime = CURRENT_TIME;
695
        return i;
696
}
697
 
698
/*
699
 * Split writes up in sane blocksizes to avoid
700
 * denial-of-service type attacks
701
 */
702
static inline ssize_t do_tty_write(
703
        ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
704
        struct tty_struct *tty,
705
        struct file *file,
706
        const unsigned char *buf,
707
        size_t count)
708
{
709
        ssize_t ret = 0, written = 0;
710
 
711
        if (file->f_flags & O_NONBLOCK) {
712
                if (down_trylock(&tty->atomic_write))
713
                        return -EAGAIN;
714
        }
715
        else {
716
                if (down_interruptible(&tty->atomic_write))
717
                        return -ERESTARTSYS;
718
        }
719
        if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
720
                lock_kernel();
721
                written = write(tty, file, buf, count);
722
                unlock_kernel();
723
        } else {
724
                for (;;) {
725
                        unsigned long size = MAX(PAGE_SIZE*2,16384);
726
                        if (size > count)
727
                                size = count;
728
                        lock_kernel();
729
                        ret = write(tty, file, buf, size);
730
                        unlock_kernel();
731
                        if (ret <= 0)
732
                                break;
733
                        written += ret;
734
                        buf += ret;
735
                        count -= ret;
736
                        if (!count)
737
                                break;
738
                        ret = -ERESTARTSYS;
739
                        if (signal_pending(current))
740
                                break;
741
                        if (current->need_resched)
742
                                schedule();
743
                }
744
        }
745
        if (written) {
746
                file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
747
                ret = written;
748
        }
749
        up(&tty->atomic_write);
750
        return ret;
751
}
752
 
753
 
754
static ssize_t tty_write(struct file * file, const char * buf, size_t count,
755
                         loff_t *ppos)
756
{
757
        int is_console;
758
        struct tty_struct * tty;
759
        struct inode *inode = file->f_dentry->d_inode;
760
 
761
        /* Can't seek (pwrite) on ttys.  */
762
        if (ppos != &file->f_pos)
763
                return -ESPIPE;
764
 
765
        /*
766
         *      For now, we redirect writes from /dev/console as
767
         *      well as /dev/tty0.
768
         */
769
        inode = file->f_dentry->d_inode;
770
        is_console = (inode->i_rdev == SYSCONS_DEV ||
771
                      inode->i_rdev == CONSOLE_DEV);
772
 
773
        if (is_console) {
774
                struct file *p = NULL;
775
 
776
                spin_lock(&redirect_lock);
777
                if (redirect) {
778
                        get_file(redirect);
779
                        p = redirect;
780
                }
781
                spin_unlock(&redirect_lock);
782
 
783
                if (p) {
784
                        ssize_t res = p->f_op->write(p, buf, count, &p->f_pos);
785
                        fput(p);
786
                        return res;
787
                }
788
        }
789
 
790
        tty = (struct tty_struct *)file->private_data;
791
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
792
                return -EIO;
793
        if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
794
                return -EIO;
795
#if 0
796
        if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
797
            (current->tty == tty) && (tty->pgrp != current->pgrp)) {
798
                if (is_orphaned_pgrp(current->pgrp))
799
                        return -EIO;
800
                if (!is_ignored(SIGTTOU)) {
801
                        (void) kill_pg(current->pgrp, SIGTTOU, 1);
802
                        return -ERESTARTSYS;
803
                }
804
        }
805
#endif
806
        if (!tty->ldisc.write)
807
                return -EIO;
808
        return do_tty_write(tty->ldisc.write, tty, file,
809
                            (const unsigned char *)buf, count);
810
}
811
 
812
/* Semaphore to protect creating and releasing a tty */
813
static DECLARE_MUTEX(tty_sem);
814
 
815
static void down_tty_sem(int index)
816
{
817
        down(&tty_sem);
818
}
819
 
820
static void up_tty_sem(int index)
821
{
822
        up(&tty_sem);
823
}
824
 
825
static void release_mem(struct tty_struct *tty, int idx);
826
 
827
/*
828
 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
829
 * failed open.  The new code protects the open with a semaphore, so it's
830
 * really quite straightforward.  The semaphore locking can probably be
831
 * relaxed for the (most common) case of reopening a tty.
832
 */
833
static int init_dev(kdev_t device, struct tty_struct **ret_tty)
834
{
835
        struct tty_struct *tty, *o_tty;
836
        struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
837
        struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
838
        struct tty_driver *driver;
839
        int retval=0;
840
        int idx;
841
 
842
        driver = get_tty_driver(device);
843
        if (!driver)
844
                return -ENODEV;
845
 
846
        idx = MINOR(device) - driver->minor_start;
847
 
848
        /*
849
         * Check whether we need to acquire the tty semaphore to avoid
850
         * race conditions.  For now, play it safe.
851
         */
852
        down_tty_sem(idx);
853
 
854
        /* check whether we're reopening an existing tty */
855
        tty = driver->table[idx];
856
        if (tty) goto fast_track;
857
 
858
        /*
859
         * First time open is complex, especially for PTY devices.
860
         * This code guarantees that either everything succeeds and the
861
         * TTY is ready for operation, or else the table slots are vacated
862
         * and the allocated memory released.  (Except that the termios
863
         * and locked termios may be retained.)
864
         */
865
 
866
        o_tty = NULL;
867
        tp = o_tp = NULL;
868
        ltp = o_ltp = NULL;
869
 
870
        tty = alloc_tty_struct();
871
        if(!tty)
872
                goto fail_no_mem;
873
        initialize_tty_struct(tty);
874
        tty->device = device;
875
        tty->driver = *driver;
876
 
877
        tp_loc = &driver->termios[idx];
878
        if (!*tp_loc) {
879
                tp = (struct termios *) kmalloc(sizeof(struct termios),
880
                                                GFP_KERNEL);
881
                if (!tp)
882
                        goto free_mem_out;
883
                *tp = driver->init_termios;
884
        }
885
 
886
        ltp_loc = &driver->termios_locked[idx];
887
        if (!*ltp_loc) {
888
                ltp = (struct termios *) kmalloc(sizeof(struct termios),
889
                                                 GFP_KERNEL);
890
                if (!ltp)
891
                        goto free_mem_out;
892
                memset(ltp, 0, sizeof(struct termios));
893
        }
894
 
895
        if (driver->type == TTY_DRIVER_TYPE_PTY) {
896
                o_tty = alloc_tty_struct();
897
                if (!o_tty)
898
                        goto free_mem_out;
899
                initialize_tty_struct(o_tty);
900
                o_tty->device = (kdev_t) MKDEV(driver->other->major,
901
                                        driver->other->minor_start + idx);
902
                o_tty->driver = *driver->other;
903
 
904
                o_tp_loc  = &driver->other->termios[idx];
905
                if (!*o_tp_loc) {
906
                        o_tp = (struct termios *)
907
                                kmalloc(sizeof(struct termios), GFP_KERNEL);
908
                        if (!o_tp)
909
                                goto free_mem_out;
910
                        *o_tp = driver->other->init_termios;
911
                }
912
 
913
                o_ltp_loc = &driver->other->termios_locked[idx];
914
                if (!*o_ltp_loc) {
915
                        o_ltp = (struct termios *)
916
                                kmalloc(sizeof(struct termios), GFP_KERNEL);
917
                        if (!o_ltp)
918
                                goto free_mem_out;
919
                        memset(o_ltp, 0, sizeof(struct termios));
920
                }
921
 
922
                /*
923
                 * Everything allocated ... set up the o_tty structure.
924
                 */
925
                driver->other->table[idx] = o_tty;
926
                if (!*o_tp_loc)
927
                        *o_tp_loc = o_tp;
928
                if (!*o_ltp_loc)
929
                        *o_ltp_loc = o_ltp;
930
                o_tty->termios = *o_tp_loc;
931
                o_tty->termios_locked = *o_ltp_loc;
932
                (*driver->other->refcount)++;
933
                if (driver->subtype == PTY_TYPE_MASTER)
934
                        o_tty->count++;
935
 
936
                /* Establish the links in both directions */
937
                tty->link   = o_tty;
938
                o_tty->link = tty;
939
        }
940
 
941
        /*
942
         * All structures have been allocated, so now we install them.
943
         * Failures after this point use release_mem to clean up, so
944
         * there's no need to null out the local pointers.
945
         */
946
        driver->table[idx] = tty;
947
 
948
        if (!*tp_loc)
949
                *tp_loc = tp;
950
        if (!*ltp_loc)
951
                *ltp_loc = ltp;
952
        tty->termios = *tp_loc;
953
        tty->termios_locked = *ltp_loc;
954
        (*driver->refcount)++;
955
        tty->count++;
956
 
957
        /*
958
         * Structures all installed ... call the ldisc open routines.
959
         * If we fail here just call release_mem to clean up.  No need
960
         * to decrement the use counts, as release_mem doesn't care.
961
         */
962
        if (tty->ldisc.open) {
963
                retval = (tty->ldisc.open)(tty);
964
                if (retval)
965
                        goto release_mem_out;
966
        }
967
        if (o_tty && o_tty->ldisc.open) {
968
                retval = (o_tty->ldisc.open)(o_tty);
969
                if (retval) {
970
                        if (tty->ldisc.close)
971
                                (tty->ldisc.close)(tty);
972
                        goto release_mem_out;
973
                }
974
        }
975
        goto success;
976
 
977
        /*
978
         * This fast open can be used if the tty is already open.
979
         * No memory is allocated, and the only failures are from
980
         * attempting to open a closing tty or attempting multiple
981
         * opens on a pty master.
982
         */
983
fast_track:
984
        if (test_bit(TTY_CLOSING, &tty->flags)) {
985
                retval = -EIO;
986
                goto end_init;
987
        }
988
        if (driver->type == TTY_DRIVER_TYPE_PTY &&
989
            driver->subtype == PTY_TYPE_MASTER) {
990
                /*
991
                 * special case for PTY masters: only one open permitted,
992
                 * and the slave side open count is incremented as well.
993
                 */
994
                if (tty->count) {
995
                        retval = -EIO;
996
                        goto end_init;
997
                }
998
                tty->link->count++;
999
        }
1000
        tty->count++;
1001
        tty->driver = *driver; /* N.B. why do this every time?? */
1002
 
1003
success:
1004
        *ret_tty = tty;
1005
 
1006
        /* All paths come through here to release the semaphore */
1007
end_init:
1008
        up_tty_sem(idx);
1009
        return retval;
1010
 
1011
        /* Release locally allocated memory ... nothing placed in slots */
1012
free_mem_out:
1013
        if (o_tp)
1014
                kfree(o_tp);
1015
        if (o_tty)
1016
                free_tty_struct(o_tty);
1017
        if (ltp)
1018
                kfree(ltp);
1019
        if (tp)
1020
                kfree(tp);
1021
        free_tty_struct(tty);
1022
 
1023
fail_no_mem:
1024
        retval = -ENOMEM;
1025
        goto end_init;
1026
 
1027
        /* call the tty release_mem routine to clean out this slot */
1028
release_mem_out:
1029
        printk(KERN_INFO "init_dev: ldisc open failed, "
1030
                         "clearing slot %d\n", idx);
1031
        release_mem(tty, idx);
1032
        goto end_init;
1033
}
1034
 
1035
/*
1036
 * Releases memory associated with a tty structure, and clears out the
1037
 * driver table slots.
1038
 */
1039
static void release_mem(struct tty_struct *tty, int idx)
1040
{
1041
        struct tty_struct *o_tty;
1042
        struct termios *tp;
1043
 
1044
        if ((o_tty = tty->link) != NULL) {
1045
                o_tty->driver.table[idx] = NULL;
1046
                if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1047
                        tp = o_tty->driver.termios[idx];
1048
                        o_tty->driver.termios[idx] = NULL;
1049
                        kfree(tp);
1050
                }
1051
                o_tty->magic = 0;
1052
                (*o_tty->driver.refcount)--;
1053
                list_del_init(&o_tty->tty_files);
1054
                free_tty_struct(o_tty);
1055
        }
1056
 
1057
        tty->driver.table[idx] = NULL;
1058
        if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1059
                tp = tty->driver.termios[idx];
1060
                tty->driver.termios[idx] = NULL;
1061
                kfree(tp);
1062
        }
1063
        tty->magic = 0;
1064
        (*tty->driver.refcount)--;
1065
        list_del_init(&tty->tty_files);
1066
        free_tty_struct(tty);
1067
}
1068
 
1069
/*
1070
 * Even releasing the tty structures is a tricky business.. We have
1071
 * to be very careful that the structures are all released at the
1072
 * same time, as interrupts might otherwise get the wrong pointers.
1073
 *
1074
 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1075
 * lead to double frees or releasing memory still in use.
1076
 */
1077
static void release_dev(struct file * filp)
1078
{
1079
        struct tty_struct *tty, *o_tty;
1080
        int     pty_master, tty_closing, o_tty_closing, do_sleep;
1081
        int     idx;
1082
        char    buf[64];
1083
 
1084
        tty = (struct tty_struct *)filp->private_data;
1085
        if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1086
                return;
1087
 
1088
        check_tty_count(tty, "release_dev");
1089
 
1090
        tty_fasync(-1, filp, 0);
1091
 
1092
        idx = MINOR(tty->device) - tty->driver.minor_start;
1093
        pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1094
                      tty->driver.subtype == PTY_TYPE_MASTER);
1095
        o_tty = tty->link;
1096
 
1097
#ifdef TTY_PARANOIA_CHECK
1098
        if (idx < 0 || idx >= tty->driver.num) {
1099
                printk(KERN_DEBUG "release_dev: bad idx when trying to "
1100
                                  "free (%s)\n", kdevname(tty->device));
1101
                return;
1102
        }
1103
        if (tty != tty->driver.table[idx]) {
1104
                printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1105
                                  "for (%s)\n", idx, kdevname(tty->device));
1106
                return;
1107
        }
1108
        if (tty->termios != tty->driver.termios[idx]) {
1109
                printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1110
                       "for (%s)\n",
1111
                       idx, kdevname(tty->device));
1112
                return;
1113
        }
1114
        if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1115
                printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1116
                       "termios_locked for (%s)\n",
1117
                       idx, kdevname(tty->device));
1118
                return;
1119
        }
1120
#endif
1121
 
1122
#ifdef TTY_DEBUG_HANGUP
1123
        printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1124
               tty_name(tty, buf), tty->count);
1125
#endif
1126
 
1127
#ifdef TTY_PARANOIA_CHECK
1128
        if (tty->driver.other) {
1129
                if (o_tty != tty->driver.other->table[idx]) {
1130
                        printk(KERN_DEBUG "release_dev: other->table[%d] "
1131
                                          "not o_tty for (%s)\n",
1132
                               idx, kdevname(tty->device));
1133
                        return;
1134
                }
1135
                if (o_tty->termios != tty->driver.other->termios[idx]) {
1136
                        printk(KERN_DEBUG "release_dev: other->termios[%d] "
1137
                                          "not o_termios for (%s)\n",
1138
                               idx, kdevname(tty->device));
1139
                        return;
1140
                }
1141
                if (o_tty->termios_locked !=
1142
                      tty->driver.other->termios_locked[idx]) {
1143
                        printk(KERN_DEBUG "release_dev: other->termios_locked["
1144
                                          "%d] not o_termios_locked for (%s)\n",
1145
                               idx, kdevname(tty->device));
1146
                        return;
1147
                }
1148
                if (o_tty->link != tty) {
1149
                        printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1150
                        return;
1151
                }
1152
        }
1153
#endif
1154
 
1155
        if (tty->driver.close)
1156
                tty->driver.close(tty, filp);
1157
 
1158
        /*
1159
         * Sanity check: if tty->count is going to zero, there shouldn't be
1160
         * any waiters on tty->read_wait or tty->write_wait.  We test the
1161
         * wait queues and kick everyone out _before_ actually starting to
1162
         * close.  This ensures that we won't block while releasing the tty
1163
         * structure.
1164
         *
1165
         * The test for the o_tty closing is necessary, since the master and
1166
         * slave sides may close in any order.  If the slave side closes out
1167
         * first, its count will be one, since the master side holds an open.
1168
         * Thus this test wouldn't be triggered at the time the slave closes,
1169
         * so we do it now.
1170
         *
1171
         * Note that it's possible for the tty to be opened again while we're
1172
         * flushing out waiters.  By recalculating the closing flags before
1173
         * each iteration we avoid any problems.
1174
         */
1175
        while (1) {
1176
                tty_closing = tty->count <= 1;
1177
                o_tty_closing = o_tty &&
1178
                        (o_tty->count <= (pty_master ? 1 : 0));
1179
                do_sleep = 0;
1180
 
1181
                if (tty_closing) {
1182
                        if (waitqueue_active(&tty->read_wait)) {
1183
                                wake_up(&tty->read_wait);
1184
                                do_sleep++;
1185
                        }
1186
                        if (waitqueue_active(&tty->write_wait)) {
1187
                                wake_up(&tty->write_wait);
1188
                                do_sleep++;
1189
                        }
1190
                }
1191
                if (o_tty_closing) {
1192
                        if (waitqueue_active(&o_tty->read_wait)) {
1193
                                wake_up(&o_tty->read_wait);
1194
                                do_sleep++;
1195
                        }
1196
                        if (waitqueue_active(&o_tty->write_wait)) {
1197
                                wake_up(&o_tty->write_wait);
1198
                                do_sleep++;
1199
                        }
1200
                }
1201
                if (!do_sleep)
1202
                        break;
1203
 
1204
                printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1205
                                    "active!\n", tty_name(tty, buf));
1206
                schedule();
1207
        }
1208
 
1209
        /*
1210
         * The closing flags are now consistent with the open counts on
1211
         * both sides, and we've completed the last operation that could
1212
         * block, so it's safe to proceed with closing.
1213
         */
1214
        if (pty_master) {
1215
                if (--o_tty->count < 0) {
1216
                        printk(KERN_WARNING "release_dev: bad pty slave count "
1217
                                            "(%d) for %s\n",
1218
                               o_tty->count, tty_name(o_tty, buf));
1219
                        o_tty->count = 0;
1220
                }
1221
        }
1222
        if (--tty->count < 0) {
1223
                printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1224
                       tty->count, tty_name(tty, buf));
1225
                tty->count = 0;
1226
        }
1227
 
1228
        /*
1229
         * We've decremented tty->count, so we should zero out
1230
         * filp->private_data, to break the link between the tty and
1231
         * the file descriptor.  Otherwise if filp_close() blocks before
1232
         * the file descriptor is removed from the inuse_filp
1233
         * list, check_tty_count() could observe a discrepancy and
1234
         * printk a warning message to the user.
1235
         */
1236
        filp->private_data = 0;
1237
 
1238
        /*
1239
         * Perform some housekeeping before deciding whether to return.
1240
         *
1241
         * Set the TTY_CLOSING flag if this was the last open.  In the
1242
         * case of a pty we may have to wait around for the other side
1243
         * to close, and TTY_CLOSING makes sure we can't be reopened.
1244
         */
1245
        if(tty_closing)
1246
                set_bit(TTY_CLOSING, &tty->flags);
1247
        if(o_tty_closing)
1248
                set_bit(TTY_CLOSING, &o_tty->flags);
1249
 
1250
        /*
1251
         * If _either_ side is closing, make sure there aren't any
1252
         * processes that still think tty or o_tty is their controlling
1253
         * tty.
1254
         */
1255
        if (tty_closing || o_tty_closing) {
1256
                struct task_struct *p;
1257
 
1258
                read_lock(&tasklist_lock);
1259
                for_each_task(p) {
1260
                        if (p->tty == tty || (o_tty && p->tty == o_tty))
1261
                                p->tty = NULL;
1262
                }
1263
                read_unlock(&tasklist_lock);
1264
        }
1265
 
1266
        /* check whether both sides are closing ... */
1267
        if (!tty_closing || (o_tty && !o_tty_closing))
1268
                return;
1269
 
1270
#ifdef TTY_DEBUG_HANGUP
1271
        printk(KERN_DEBUG "freeing tty structure...");
1272
#endif
1273
 
1274
        /*
1275
         * Shutdown the current line discipline, and reset it to N_TTY.
1276
         * N.B. why reset ldisc when we're releasing the memory??
1277
         */
1278
        if (tty->ldisc.close)
1279
                (tty->ldisc.close)(tty);
1280
        tty->ldisc = ldiscs[N_TTY];
1281
        tty->termios->c_line = N_TTY;
1282
        if (o_tty) {
1283
                if (o_tty->ldisc.close)
1284
                        (o_tty->ldisc.close)(o_tty);
1285
                o_tty->ldisc = ldiscs[N_TTY];
1286
        }
1287
 
1288
        /*
1289
         * Make sure that the tty's task queue isn't activated.
1290
         */
1291
        run_task_queue(&tq_timer);
1292
        flush_scheduled_tasks();
1293
 
1294
        /*
1295
         * The release_mem function takes care of the details of clearing
1296
         * the slots and preserving the termios structure.
1297
         */
1298
        release_mem(tty, idx);
1299
}
1300
 
1301
/*
1302
 * tty_open and tty_release keep up the tty count that contains the
1303
 * number of opens done on a tty. We cannot use the inode-count, as
1304
 * different inodes might point to the same tty.
1305
 *
1306
 * Open-counting is needed for pty masters, as well as for keeping
1307
 * track of serial lines: DTR is dropped when the last close happens.
1308
 * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1309
 *
1310
 * The termios state of a pty is reset on first open so that
1311
 * settings don't persist across reuse.
1312
 */
1313
static int tty_open(struct inode * inode, struct file * filp)
1314
{
1315
        struct tty_struct *tty;
1316
        int noctty, retval;
1317
        kdev_t device;
1318
        unsigned short saved_flags;
1319
        char    buf[64];
1320
 
1321
        saved_flags = filp->f_flags;
1322
retry_open:
1323
        noctty = filp->f_flags & O_NOCTTY;
1324
        device = inode->i_rdev;
1325
        if (device == TTY_DEV) {
1326
                if (!current->tty)
1327
                        return -ENXIO;
1328
                device = current->tty->device;
1329
                filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1330
                /* noctty = 1; */
1331
        }
1332
#ifdef CONFIG_VT
1333
        if (device == CONSOLE_DEV) {
1334
                extern int fg_console;
1335
                device = MKDEV(TTY_MAJOR, fg_console + 1);
1336
                noctty = 1;
1337
        }
1338
#endif
1339
        if (device == SYSCONS_DEV) {
1340
                struct console *c = console_drivers;
1341
                while(c && !c->device)
1342
                        c = c->next;
1343
                if (!c)
1344
                        return -ENODEV;
1345
                device = c->device(c);
1346
                filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1347
                noctty = 1;
1348
        }
1349
 
1350
        if (device == PTMX_DEV) {
1351
#ifdef CONFIG_UNIX98_PTYS
1352
 
1353
                /* find a free pty. */
1354
                int major, minor;
1355
                struct tty_driver *driver;
1356
 
1357
                /* find a device that is not in use. */
1358
                retval = -1;
1359
                for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1360
                        driver = &ptm_driver[major];
1361
                        for (minor = driver->minor_start ;
1362
                             minor < driver->minor_start + driver->num ;
1363
                             minor++) {
1364
                                device = MKDEV(driver->major, minor);
1365
                                if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1366
                        }
1367
                }
1368
                return -EIO; /* no free ptys */
1369
        ptmx_found:
1370
                set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1371
                minor -= driver->minor_start;
1372
                devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1373
                tty_register_devfs(&pts_driver[major], DEVFS_FL_DEFAULT,
1374
                                   pts_driver[major].minor_start + minor);
1375
                noctty = 1;
1376
                goto init_dev_done;
1377
 
1378
#else   /* CONFIG_UNIX_98_PTYS */
1379
 
1380
                return -ENODEV;
1381
 
1382
#endif  /* CONFIG_UNIX_98_PTYS */
1383
        }
1384
 
1385
        retval = init_dev(device, &tty);
1386
        if (retval)
1387
                return retval;
1388
 
1389
#ifdef CONFIG_UNIX98_PTYS
1390
init_dev_done:
1391
#endif
1392
        filp->private_data = tty;
1393
        file_move(filp, &tty->tty_files);
1394
        check_tty_count(tty, "tty_open");
1395
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1396
            tty->driver.subtype == PTY_TYPE_MASTER)
1397
                noctty = 1;
1398
#ifdef TTY_DEBUG_HANGUP
1399
        printk(KERN_DEBUG "opening %s...", tty_name(tty, buf));
1400
#endif
1401
        if (tty->driver.open)
1402
                retval = tty->driver.open(tty, filp);
1403
        else
1404
                retval = -ENODEV;
1405
        filp->f_flags = saved_flags;
1406
 
1407
        if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1408
                retval = -EBUSY;
1409
 
1410
        if (retval) {
1411
#ifdef TTY_DEBUG_HANGUP
1412
                printk(KERN_DEBUG "error %d in opening %s...", retval,
1413
                       tty_name(tty, buf));
1414
#endif
1415
 
1416
                release_dev(filp);
1417
                if (retval != -ERESTARTSYS)
1418
                        return retval;
1419
                if (signal_pending(current))
1420
                        return retval;
1421
                schedule();
1422
                /*
1423
                 * Need to reset f_op in case a hangup happened.
1424
                 */
1425
                filp->f_op = &tty_fops;
1426
                goto retry_open;
1427
        }
1428
        if (!noctty &&
1429
            current->leader &&
1430
            !current->tty &&
1431
            tty->session == 0) {
1432
                task_lock(current);
1433
                current->tty = tty;
1434
                task_unlock(current);
1435
                current->tty_old_pgrp = 0;
1436
                tty->session = current->session;
1437
                tty->pgrp = current->pgrp;
1438
        }
1439
        if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1440
            (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1441
            (tty->count == 1)) {
1442
                static int nr_warns;
1443
                if (nr_warns < 5) {
1444
                        printk(KERN_WARNING "tty_io.c: "
1445
                                "process %d (%s) used obsolete /dev/%s - "
1446
                                "update software to use /dev/ttyS%d\n",
1447
                                current->pid, current->comm,
1448
                                tty_name(tty, buf), TTY_NUMBER(tty));
1449
                        nr_warns++;
1450
                }
1451
        }
1452
        return 0;
1453
}
1454
 
1455
static int tty_release(struct inode * inode, struct file * filp)
1456
{
1457
        lock_kernel();
1458
        release_dev(filp);
1459
        unlock_kernel();
1460
        return 0;
1461
}
1462
 
1463
/* No kernel lock held - fine */
1464
static unsigned int tty_poll(struct file * filp, poll_table * wait)
1465
{
1466
        struct tty_struct * tty;
1467
 
1468
        tty = (struct tty_struct *)filp->private_data;
1469
        if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1470
                return 0;
1471
 
1472
        if (tty->ldisc.poll)
1473
                return (tty->ldisc.poll)(tty, filp, wait);
1474
        return 0;
1475
}
1476
 
1477
static int tty_fasync(int fd, 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, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1484
                return 0;
1485
 
1486
        retval = fasync_helper(fd, 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
static int tiocsti(struct tty_struct *tty, char * arg)
1506
{
1507
        char ch, mbz = 0;
1508
 
1509
        if ((current->tty != tty) && !suser())
1510
                return -EPERM;
1511
        if (get_user(ch, arg))
1512
                return -EFAULT;
1513
        tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1514
        return 0;
1515
}
1516
 
1517
static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1518
{
1519
        if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1520
                return -EFAULT;
1521
        return 0;
1522
}
1523
 
1524
static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1525
        struct winsize * arg)
1526
{
1527
        struct winsize tmp_ws;
1528
 
1529
        if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1530
                return -EFAULT;
1531
        if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1532
                return 0;
1533
        if (tty->pgrp > 0)
1534
                kill_pg(tty->pgrp, SIGWINCH, 1);
1535
        if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1536
                kill_pg(real_tty->pgrp, SIGWINCH, 1);
1537
        tty->winsize = tmp_ws;
1538
        real_tty->winsize = tmp_ws;
1539
        return 0;
1540
}
1541
 
1542
static int tioccons(struct inode *inode, struct file *file)
1543
{
1544
        if (inode->i_rdev == SYSCONS_DEV ||
1545
            inode->i_rdev == CONSOLE_DEV) {
1546
                struct file *f;
1547
                if (!suser())
1548
                        return -EPERM;
1549
                spin_lock(&redirect_lock);
1550
                f = redirect;
1551
                redirect = NULL;
1552
                spin_unlock(&redirect_lock);
1553
                if (f)
1554
                        fput(f);
1555
                return 0;
1556
        }
1557
        spin_lock(&redirect_lock);
1558
        if (redirect) {
1559
                spin_unlock(&redirect_lock);
1560
                return -EBUSY;
1561
        }
1562
        get_file(file);
1563
        redirect = file;
1564
        spin_unlock(&redirect_lock);
1565
        return 0;
1566
}
1567
 
1568
 
1569
static int fionbio(struct file *file, int *arg)
1570
{
1571
        int nonblock;
1572
 
1573
        if (get_user(nonblock, arg))
1574
                return -EFAULT;
1575
 
1576
        if (nonblock)
1577
                file->f_flags |= O_NONBLOCK;
1578
        else
1579
                file->f_flags &= ~O_NONBLOCK;
1580
        return 0;
1581
}
1582
 
1583
static int tiocsctty(struct tty_struct *tty, int arg)
1584
{
1585
        if (current->leader &&
1586
            (current->session == tty->session))
1587
                return 0;
1588
        /*
1589
         * The process must be a session leader and
1590
         * not have a controlling tty already.
1591
         */
1592
        if (!current->leader || current->tty)
1593
                return -EPERM;
1594
        if (tty->session > 0) {
1595
                /*
1596
                 * This tty is already the controlling
1597
                 * tty for another session group!
1598
                 */
1599
                if ((arg == 1) && suser()) {
1600
                        /*
1601
                         * Steal it away
1602
                         */
1603
                        struct task_struct *p;
1604
 
1605
                        read_lock(&tasklist_lock);
1606
                        for_each_task(p)
1607
                                if (p->tty == tty)
1608
                                        p->tty = NULL;
1609
                        read_unlock(&tasklist_lock);
1610
                } else
1611
                        return -EPERM;
1612
        }
1613
        task_lock(current);
1614
        current->tty = tty;
1615
        task_unlock(current);
1616
        current->tty_old_pgrp = 0;
1617
        tty->session = current->session;
1618
        tty->pgrp = current->pgrp;
1619
        return 0;
1620
}
1621
 
1622
static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1623
{
1624
        /*
1625
         * (tty == real_tty) is a cheap way of
1626
         * testing if the tty is NOT a master pty.
1627
         */
1628
        if (tty == real_tty && current->tty != real_tty)
1629
                return -ENOTTY;
1630
        return put_user(real_tty->pgrp, arg);
1631
}
1632
 
1633
static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1634
{
1635
        pid_t pgrp;
1636
        int retval = tty_check_change(real_tty);
1637
 
1638
        if (retval == -EIO)
1639
                return -ENOTTY;
1640
        if (retval)
1641
                return retval;
1642
        if (!current->tty ||
1643
            (current->tty != real_tty) ||
1644
            (real_tty->session != current->session))
1645
                return -ENOTTY;
1646
        if (get_user(pgrp, (pid_t *) arg))
1647
                return -EFAULT;
1648
        if (pgrp < 0)
1649
                return -EINVAL;
1650
        if (session_of_pgrp(pgrp) != current->session)
1651
                return -EPERM;
1652
        real_tty->pgrp = pgrp;
1653
        return 0;
1654
}
1655
 
1656
static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1657
{
1658
        /*
1659
         * (tty == real_tty) is a cheap way of
1660
         * testing if the tty is NOT a master pty.
1661
        */
1662
        if (tty == real_tty && current->tty != real_tty)
1663
                return -ENOTTY;
1664
        if (real_tty->session <= 0)
1665
                return -ENOTTY;
1666
        return put_user(real_tty->session, arg);
1667
}
1668
 
1669
static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1670
{
1671
        if (copy_to_user(arg, tty, sizeof(*arg)))
1672
                return -EFAULT;
1673
        return 0;
1674
}
1675
 
1676
static int tiocsetd(struct tty_struct *tty, int *arg)
1677
{
1678
        int ldisc;
1679
 
1680
        if (get_user(ldisc, arg))
1681
                return -EFAULT;
1682
        return tty_set_ldisc(tty, ldisc);
1683
}
1684
 
1685
static int send_break(struct tty_struct *tty, int duration)
1686
{
1687
        set_current_state(TASK_INTERRUPTIBLE);
1688
 
1689
        tty->driver.break_ctl(tty, -1);
1690
        if (!signal_pending(current))
1691
                schedule_timeout(duration);
1692
        tty->driver.break_ctl(tty, 0);
1693
        if (signal_pending(current))
1694
                return -EINTR;
1695
        return 0;
1696
}
1697
 
1698
static int tty_generic_brk(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1699
{
1700
        if (cmd == TCSBRK && arg)
1701
        {
1702
                /* tcdrain case */
1703
                int retval = tty_check_change(tty);
1704
                if (retval)
1705
                        return retval;
1706
                tty_wait_until_sent(tty, 0);
1707
                if (signal_pending(current))
1708
                        return -EINTR;
1709
        }
1710
        return 0;
1711
}
1712
 
1713
/*
1714
 * Split this up, as gcc can choke on it otherwise..
1715
 */
1716
int tty_ioctl(struct inode * inode, struct file * file,
1717
              unsigned int cmd, unsigned long arg)
1718
{
1719
        struct tty_struct *tty, *real_tty;
1720
        int retval;
1721
 
1722
        tty = (struct tty_struct *)file->private_data;
1723
        if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1724
                return -EINVAL;
1725
 
1726
        real_tty = tty;
1727
        if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1728
            tty->driver.subtype == PTY_TYPE_MASTER)
1729
                real_tty = tty->link;
1730
 
1731
        /*
1732
         * Break handling by driver
1733
         */
1734
        if (!tty->driver.break_ctl) {
1735
                switch(cmd) {
1736
                case TIOCSBRK:
1737
                case TIOCCBRK:
1738
                        if (tty->driver.ioctl)
1739
                                return tty->driver.ioctl(tty, file, cmd, arg);
1740
                        return -EINVAL;
1741
 
1742
                /* These two ioctl's always return success; even if */
1743
                /* the driver doesn't support them. */
1744
                case TCSBRK:
1745
                case TCSBRKP:
1746
                        retval = -ENOIOCTLCMD;
1747
                        if (tty->driver.ioctl)
1748
                                retval = tty->driver.ioctl(tty, file, cmd, arg);
1749
                        /* Not driver handled */
1750
                        if (retval == -ENOIOCTLCMD)
1751
                                retval = tty_generic_brk(tty, file, cmd, arg);
1752
                        return retval;
1753
                }
1754
        }
1755
 
1756
        /*
1757
         * Factor out some common prep work
1758
         */
1759
        switch (cmd) {
1760
        case TIOCSETD:
1761
        case TIOCSBRK:
1762
        case TIOCCBRK:
1763
        case TCSBRK:
1764
        case TCSBRKP:
1765
                retval = tty_check_change(tty);
1766
                if (retval)
1767
                        return retval;
1768
                if (cmd != TIOCCBRK) {
1769
                        tty_wait_until_sent(tty, 0);
1770
                        if (signal_pending(current))
1771
                                return -EINTR;
1772
                }
1773
                break;
1774
        }
1775
 
1776
        switch (cmd) {
1777
                case TIOCSTI:
1778
                        return tiocsti(tty, (char *)arg);
1779
                case TIOCGWINSZ:
1780
                        return tiocgwinsz(tty, (struct winsize *) arg);
1781
                case TIOCSWINSZ:
1782
                        return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1783
                case TIOCCONS:
1784
                        return real_tty!=tty ? -EINVAL : tioccons(inode, file);
1785
                case FIONBIO:
1786
                        return fionbio(file, (int *) arg);
1787
                case TIOCEXCL:
1788
                        set_bit(TTY_EXCLUSIVE, &tty->flags);
1789
                        return 0;
1790
                case TIOCNXCL:
1791
                        clear_bit(TTY_EXCLUSIVE, &tty->flags);
1792
                        return 0;
1793
                case TIOCNOTTY:
1794
                        if (current->tty != tty)
1795
                                return -ENOTTY;
1796
                        if (current->leader)
1797
                                disassociate_ctty(0);
1798
                        task_lock(current);
1799
                        current->tty = NULL;
1800
                        task_unlock(current);
1801
                        return 0;
1802
                case TIOCSCTTY:
1803
                        return tiocsctty(tty, arg);
1804
                case TIOCGPGRP:
1805
                        return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1806
                case TIOCSPGRP:
1807
                        return tiocspgrp(tty, real_tty, (pid_t *) arg);
1808
                case TIOCGSID:
1809
                        return tiocgsid(tty, real_tty, (pid_t *) arg);
1810
                case TIOCGETD:
1811
                        return put_user(tty->ldisc.num, (int *) arg);
1812
                case TIOCSETD:
1813
                        return tiocsetd(tty, (int *) arg);
1814
#ifdef CONFIG_VT
1815
                case TIOCLINUX:
1816
                        return tioclinux(tty, arg);
1817
#endif
1818
                case TIOCTTYGSTRUCT:
1819
                        return tiocttygstruct(tty, (struct tty_struct *) arg);
1820
 
1821
                /*
1822
                 * Break handling
1823
                 */
1824
                case TIOCSBRK:  /* Turn break on, unconditionally */
1825
                        tty->driver.break_ctl(tty, -1);
1826
                        return 0;
1827
 
1828
                case TIOCCBRK:  /* Turn break off, unconditionally */
1829
                        tty->driver.break_ctl(tty, 0);
1830
                        return 0;
1831
                case TCSBRK:   /* SVID version: non-zero arg --> no break */
1832
                        /*
1833
                         * XXX is the above comment correct, or the
1834
                         * code below correct?  Is this ioctl used at
1835
                         * all by anyone?
1836
                         */
1837
                        if (!arg)
1838
                                return send_break(tty, HZ/4);
1839
                        return 0;
1840
                case TCSBRKP:   /* support for POSIX tcsendbreak() */
1841
                        return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1842
        }
1843
        if (tty->driver.ioctl) {
1844
                int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1845
                if (retval != -ENOIOCTLCMD)
1846
                        return retval;
1847
        }
1848
        if (tty->ldisc.ioctl) {
1849
                int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1850
                if (retval != -ENOIOCTLCMD)
1851
                        return retval;
1852
        }
1853
        return -EINVAL;
1854
}
1855
 
1856
 
1857
/*
1858
 * This implements the "Secure Attention Key" ---  the idea is to
1859
 * prevent trojan horses by killing all processes associated with this
1860
 * tty when the user hits the "Secure Attention Key".  Required for
1861
 * super-paranoid applications --- see the Orange Book for more details.
1862
 *
1863
 * This code could be nicer; ideally it should send a HUP, wait a few
1864
 * seconds, then send a INT, and then a KILL signal.  But you then
1865
 * have to coordinate with the init process, since all processes associated
1866
 * with the current tty must be dead before the new getty is allowed
1867
 * to spawn.
1868
 *
1869
 * Now, if it would be correct ;-/ The current code has a nasty hole -
1870
 * it doesn't catch files in flight. We may send the descriptor to ourselves
1871
 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1872
 *
1873
 * Nasty bug: do_SAK is being called in interrupt context.  This can
1874
 * deadlock.  We punt it up to process context.  AKPM - 16Mar2001
1875
 */
1876
static void __do_SAK(void *arg)
1877
{
1878
#ifdef TTY_SOFT_SAK
1879
        tty_hangup(tty);
1880
#else
1881
        struct tty_struct *tty = arg;
1882
        struct task_struct *p;
1883
        int session;
1884
        int             i;
1885
        struct file     *filp;
1886
 
1887
        if (!tty)
1888
                return;
1889
        session  = tty->session;
1890
        if (tty->ldisc.flush_buffer)
1891
                tty->ldisc.flush_buffer(tty);
1892
        if (tty->driver.flush_buffer)
1893
                tty->driver.flush_buffer(tty);
1894
        read_lock(&tasklist_lock);
1895
        for_each_task(p) {
1896
                if ((p->tty == tty) ||
1897
                    ((session > 0) && (p->session == session))) {
1898
                        send_sig(SIGKILL, p, 1);
1899
                        continue;
1900
                }
1901
                task_lock(p);
1902
                if (p->files) {
1903
                        read_lock(&p->files->file_lock);
1904
                        for (i=0; i < p->files->max_fds; i++) {
1905
                                filp = fcheck_files(p->files, i);
1906
                                if (filp && (filp->f_op == &tty_fops) &&
1907
                                    (filp->private_data == tty)) {
1908
                                        send_sig(SIGKILL, p, 1);
1909
                                        break;
1910
                                }
1911
                        }
1912
                        read_unlock(&p->files->file_lock);
1913
                }
1914
                task_unlock(p);
1915
        }
1916
        read_unlock(&tasklist_lock);
1917
#endif
1918
}
1919
 
1920
/*
1921
 * The tq handling here is a little racy - tty->SAK_tq may already be queued.
1922
 * But there's no mechanism to fix that without futzing with tqueue_lock.
1923
 * Fortunately we don't need to worry, because if ->SAK_tq is already queued,
1924
 * the values which we write to it will be identical to the values which it
1925
 * already has. --akpm
1926
 */
1927
void do_SAK(struct tty_struct *tty)
1928
{
1929
        if (!tty)
1930
                return;
1931
        PREPARE_TQUEUE(&tty->SAK_tq, __do_SAK, tty);
1932
        schedule_task(&tty->SAK_tq);
1933
}
1934
 
1935
/*
1936
 * This routine is called out of the software interrupt to flush data
1937
 * from the flip buffer to the line discipline.
1938
 */
1939
static void flush_to_ldisc(void *private_)
1940
{
1941
        struct tty_struct *tty = (struct tty_struct *) private_;
1942
        unsigned char   *cp;
1943
        char            *fp;
1944
        int             count;
1945
        unsigned long flags;
1946
 
1947
        if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1948
                queue_task(&tty->flip.tqueue, &tq_timer);
1949
                return;
1950
        }
1951
        if (tty->flip.buf_num) {
1952
                cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1953
                fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1954
                tty->flip.buf_num = 0;
1955
 
1956
                save_flags(flags); cli();
1957
                tty->flip.char_buf_ptr = tty->flip.char_buf;
1958
                tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1959
        } else {
1960
                cp = tty->flip.char_buf;
1961
                fp = tty->flip.flag_buf;
1962
                tty->flip.buf_num = 1;
1963
 
1964
                save_flags(flags); cli();
1965
                tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1966
                tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1967
        }
1968
        count = tty->flip.count;
1969
        tty->flip.count = 0;
1970
        restore_flags(flags);
1971
 
1972
        tty->ldisc.receive_buf(tty, cp, fp, count);
1973
}
1974
 
1975
/*
1976
 * Routine which returns the baud rate of the tty
1977
 *
1978
 * Note that the baud_table needs to be kept in sync with the
1979
 * include/asm/termbits.h file.
1980
 */
1981
static int baud_table[] = {
1982
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1983
        9600, 19200, 38400, 57600, 115200, 230400, 460800,
1984
#ifdef __sparc__
1985
        76800, 153600, 307200, 614400, 921600
1986
#else
1987
        500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1988
        2500000, 3000000, 3500000, 4000000
1989
#endif
1990
};
1991
 
1992
static int n_baud_table = sizeof(baud_table)/sizeof(int);
1993
 
1994
int tty_get_baud_rate(struct tty_struct *tty)
1995
{
1996
        unsigned int cflag, i;
1997
 
1998
        cflag = tty->termios->c_cflag;
1999
 
2000
        i = cflag & CBAUD;
2001
        if (i & CBAUDEX) {
2002
                i &= ~CBAUDEX;
2003
                if (i < 1 || i+15 >= n_baud_table)
2004
                        tty->termios->c_cflag &= ~CBAUDEX;
2005
                else
2006
                        i += 15;
2007
        }
2008
        if (i==15 && tty->alt_speed) {
2009
                if (!tty->warned) {
2010
                        printk(KERN_WARNING "Use of setserial/setrocket to "
2011
                                            "set SPD_* flags is deprecated\n");
2012
                        tty->warned = 1;
2013
                }
2014
                return(tty->alt_speed);
2015
        }
2016
 
2017
        return baud_table[i];
2018
}
2019
 
2020
void tty_flip_buffer_push(struct tty_struct *tty)
2021
{
2022
        if (tty->low_latency)
2023
                flush_to_ldisc((void *) tty);
2024
        else
2025
                queue_task(&tty->flip.tqueue, &tq_timer);
2026
}
2027
 
2028
/*
2029
 * This subroutine initializes a tty structure.
2030
 */
2031
static void initialize_tty_struct(struct tty_struct *tty)
2032
{
2033
        memset(tty, 0, sizeof(struct tty_struct));
2034
        tty->magic = TTY_MAGIC;
2035
        tty->ldisc = ldiscs[N_TTY];
2036
        tty->pgrp = -1;
2037
        tty->flip.char_buf_ptr = tty->flip.char_buf;
2038
        tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2039
        tty->flip.tqueue.routine = flush_to_ldisc;
2040
        tty->flip.tqueue.data = tty;
2041
        init_MUTEX(&tty->flip.pty_sem);
2042
        init_waitqueue_head(&tty->write_wait);
2043
        init_waitqueue_head(&tty->read_wait);
2044
        tty->tq_hangup.routine = do_tty_hangup;
2045
        tty->tq_hangup.data = tty;
2046
        sema_init(&tty->atomic_read, 1);
2047
        sema_init(&tty->atomic_write, 1);
2048
        spin_lock_init(&tty->read_lock);
2049
        INIT_LIST_HEAD(&tty->tty_files);
2050
        INIT_TQUEUE(&tty->SAK_tq, 0, 0);
2051
}
2052
 
2053
/*
2054
 * The default put_char routine if the driver did not define one.
2055
 */
2056
void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2057
{
2058
        tty->driver.write(tty, 0, &ch, 1);
2059
}
2060
 
2061
/*
2062
 * Register a tty device described by <driver>, with minor number <minor>.
2063
 */
2064
void tty_register_devfs (struct tty_driver *driver, unsigned int flags, unsigned minor)
2065
{
2066
#ifdef CONFIG_DEVFS_FS
2067
        umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
2068
        kdev_t device = MKDEV (driver->major, minor);
2069
        int idx = minor - driver->minor_start;
2070
        char buf[32];
2071
 
2072
        switch (device) {
2073
                case TTY_DEV:
2074
                case PTMX_DEV:
2075
                        mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2076
                        break;
2077
                default:
2078
                        if (driver->major == PTY_MASTER_MAJOR)
2079
                                mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2080
                        break;
2081
        }
2082
        if ( (minor <  driver->minor_start) ||
2083
             (minor >= driver->minor_start + driver->num) ) {
2084
                printk(KERN_ERR "Attempt to register invalid minor number "
2085
                       "with devfs (%d:%d).\n", (int)driver->major,(int)minor);
2086
                return;
2087
        }
2088
#  ifdef CONFIG_UNIX98_PTYS
2089
        if ( (driver->major >= UNIX98_PTY_SLAVE_MAJOR) &&
2090
             (driver->major < UNIX98_PTY_SLAVE_MAJOR + UNIX98_NR_MAJORS) )
2091
                flags |= DEVFS_FL_CURRENT_OWNER;
2092
#  endif
2093
        sprintf(buf, driver->name, idx + driver->name_base);
2094
        devfs_register (NULL, buf, flags | DEVFS_FL_DEFAULT,
2095
                        driver->major, minor, mode, &tty_fops, NULL);
2096
#endif /* CONFIG_DEVFS_FS */
2097
}
2098
 
2099
void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2100
{
2101
#ifdef CONFIG_DEVFS_FS
2102
        void * handle;
2103
        int idx = minor - driver->minor_start;
2104
        char buf[32];
2105
 
2106
        sprintf(buf, driver->name, idx + driver->name_base);
2107
        handle = devfs_find_handle (NULL, buf, driver->major, minor,
2108
                                    DEVFS_SPECIAL_CHR, 0);
2109
        devfs_unregister (handle);
2110
#endif /* CONFIG_DEVFS_FS */
2111
}
2112
 
2113
EXPORT_SYMBOL(tty_register_devfs);
2114
EXPORT_SYMBOL(tty_unregister_devfs);
2115
 
2116
/*
2117
 * Called by a tty driver to register itself.
2118
 */
2119
int tty_register_driver(struct tty_driver *driver)
2120
{
2121
        int error;
2122
        int i;
2123
 
2124
        if (driver->flags & TTY_DRIVER_INSTALLED)
2125
                return 0;
2126
 
2127
        error = devfs_register_chrdev(driver->major, driver->name, &tty_fops);
2128
        if (error < 0)
2129
                return error;
2130
        else if(driver->major == 0)
2131
                driver->major = error;
2132
 
2133
        if (!driver->put_char)
2134
                driver->put_char = tty_default_put_char;
2135
 
2136
        driver->prev = 0;
2137
        driver->next = tty_drivers;
2138
        if (tty_drivers) tty_drivers->prev = driver;
2139
        tty_drivers = driver;
2140
 
2141
        if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2142
                for(i = 0; i < driver->num; i++)
2143
                    tty_register_devfs(driver, 0, driver->minor_start + i);
2144
        }
2145
        proc_tty_register_driver(driver);
2146
        return error;
2147
}
2148
 
2149
/*
2150
 * Called by a tty driver to unregister itself.
2151
 */
2152
int tty_unregister_driver(struct tty_driver *driver)
2153
{
2154
        int     retval;
2155
        struct tty_driver *p;
2156
        int     i, found = 0;
2157
        struct termios *tp;
2158
        const char *othername = NULL;
2159
 
2160
        if (*driver->refcount)
2161
                return -EBUSY;
2162
 
2163
        for (p = tty_drivers; p; p = p->next) {
2164
                if (p == driver)
2165
                        found++;
2166
                else if (p->major == driver->major)
2167
                        othername = p->name;
2168
        }
2169
 
2170
        if (!found)
2171
                return -ENOENT;
2172
 
2173
        if (othername == NULL) {
2174
                retval = devfs_unregister_chrdev(driver->major, driver->name);
2175
                if (retval)
2176
                        return retval;
2177
        } else
2178
                devfs_register_chrdev(driver->major, othername, &tty_fops);
2179
 
2180
        if (driver->prev)
2181
                driver->prev->next = driver->next;
2182
        else
2183
                tty_drivers = driver->next;
2184
 
2185
        if (driver->next)
2186
                driver->next->prev = driver->prev;
2187
 
2188
        /*
2189
         * Free the termios and termios_locked structures because
2190
         * we don't want to get memory leaks when modular tty
2191
         * drivers are removed from the kernel.
2192
         */
2193
        for (i = 0; i < driver->num; i++) {
2194
                tp = driver->termios[i];
2195
                if (tp) {
2196
                        driver->termios[i] = NULL;
2197
                        kfree(tp);
2198
                }
2199
                tp = driver->termios_locked[i];
2200
                if (tp) {
2201
                        driver->termios_locked[i] = NULL;
2202
                        kfree(tp);
2203
                }
2204
                tty_unregister_devfs(driver, driver->minor_start + i);
2205
        }
2206
        proc_tty_unregister_driver(driver);
2207
        return 0;
2208
}
2209
 
2210
 
2211
/*
2212
 * Initialize the console device. This is called *early*, so
2213
 * we can't necessarily depend on lots of kernel help here.
2214
 * Just do some early initializations, and do the complex setup
2215
 * later.
2216
 */
2217
void __init console_init(void)
2218
{
2219
        /* Setup the default TTY line discipline. */
2220
        memset(ldiscs, 0, sizeof(ldiscs));
2221
        (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2222
 
2223
        /*
2224
         * Set up the standard termios.  Individual tty drivers may
2225
         * deviate from this; this is used as a template.
2226
         */
2227
        memset(&tty_std_termios, 0, sizeof(struct termios));
2228
        memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2229
        tty_std_termios.c_iflag = ICRNL | IXON;
2230
        tty_std_termios.c_oflag = OPOST | ONLCR;
2231
        tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2232
        tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2233
                ECHOCTL | ECHOKE | IEXTEN;
2234
 
2235
        /*
2236
         * set up the console device so that later boot sequences can
2237
         * inform about problems etc..
2238
         */
2239
#ifdef CONFIG_EARLY_PRINTK
2240
        disable_early_printk();
2241
#endif
2242
#ifdef CONFIG_HVC_CONSOLE
2243
        hvc_console_init();
2244
#endif
2245
#ifdef CONFIG_VT
2246
        con_init();
2247
#endif
2248
#ifdef CONFIG_AU1X00_SERIAL_CONSOLE
2249
        au1x00_serial_console_init();
2250
#endif
2251
#ifdef CONFIG_SERIAL_CONSOLE
2252
#if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2253
        console_8xx_init();
2254
#elif defined(CONFIG_MAC_SERIAL) && defined(CONFIG_SERIAL)
2255
        if (_machine == _MACH_Pmac)
2256
                mac_scc_console_init();
2257
        else
2258
                serial_console_init();
2259
#elif defined(CONFIG_MAC_SERIAL)
2260
        mac_scc_console_init();
2261
#elif defined(CONFIG_PARISC)
2262
        pdc_console_init();
2263
#elif defined(CONFIG_SERIAL)
2264
        serial_console_init();
2265
#endif /* CONFIG_8xx */
2266
#if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2267
        vme_scc_console_init();
2268
#endif
2269
#if defined(CONFIG_SERIAL167)
2270
        serial167_console_init();
2271
#endif
2272
#if defined(CONFIG_SH_SCI)
2273
        sci_console_init();
2274
#endif
2275
#endif
2276
#ifdef CONFIG_SERIAL_DEC_CONSOLE
2277
        dec_serial_console_init();
2278
#endif
2279
#ifdef CONFIG_TN3270_CONSOLE
2280
        tub3270_con_init();
2281
#endif
2282
#ifdef CONFIG_TN3215
2283
        con3215_init();
2284
#endif
2285
#ifdef CONFIG_HWC
2286
        hwc_console_init();
2287
#endif
2288
#ifdef CONFIG_STDIO_CONSOLE
2289
        stdio_console_init();
2290
#endif
2291
#ifdef CONFIG_SERIAL_21285_CONSOLE
2292
        rs285_console_init();
2293
#endif
2294
#ifdef CONFIG_SERIAL_SA1100_CONSOLE
2295
        sa1100_rs_console_init();
2296
#endif
2297
#ifdef CONFIG_ARC_CONSOLE
2298
        arc_console_init();
2299
#endif
2300
#ifdef CONFIG_SERIAL_AMBA_CONSOLE
2301
        ambauart_console_init();
2302
#endif
2303
#ifdef CONFIG_SERIAL_TX3912_CONSOLE
2304
        tx3912_console_init();
2305
#endif
2306
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
2307
        txx927_console_init();
2308
#endif
2309
#ifdef CONFIG_SERIAL_TXX9_CONSOLE
2310
        txx9_serial_console_init();
2311
#endif
2312
#ifdef CONFIG_SIBYTE_SB1250_DUART_CONSOLE
2313
        sb1250_serial_console_init();
2314
#endif
2315
#ifdef CONFIG_IP22_SERIAL
2316
        sgi_serial_console_init();
2317
#endif
2318
}
2319
 
2320
static struct tty_driver dev_tty_driver, dev_syscons_driver;
2321
#ifdef CONFIG_UNIX98_PTYS
2322
static struct tty_driver dev_ptmx_driver;
2323
#endif
2324
#ifdef CONFIG_VT
2325
static struct tty_driver dev_console_driver;
2326
#endif
2327
 
2328
/*
2329
 * Ok, now we can initialize the rest of the tty devices and can count
2330
 * on memory allocations, interrupts etc..
2331
 */
2332
void __init tty_init(void)
2333
{
2334
        /*
2335
         * dev_tty_driver and dev_console_driver are actually magic
2336
         * devices which get redirected at open time.  Nevertheless,
2337
         * we register them so that register_chrdev is called
2338
         * appropriately.
2339
         */
2340
        memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2341
        dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2342
        dev_tty_driver.driver_name = "/dev/tty";
2343
        dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2344
        dev_tty_driver.name_base = 0;
2345
        dev_tty_driver.major = TTYAUX_MAJOR;
2346
        dev_tty_driver.minor_start = 0;
2347
        dev_tty_driver.num = 1;
2348
        dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2349
        dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2350
 
2351
        if (tty_register_driver(&dev_tty_driver))
2352
                panic("Couldn't register /dev/tty driver\n");
2353
 
2354
        dev_syscons_driver = dev_tty_driver;
2355
        dev_syscons_driver.driver_name = "/dev/console";
2356
        dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2357
        dev_syscons_driver.major = TTYAUX_MAJOR;
2358
        dev_syscons_driver.minor_start = 1;
2359
        dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2360
        dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2361
 
2362
        if (tty_register_driver(&dev_syscons_driver))
2363
                panic("Couldn't register /dev/console driver\n");
2364
 
2365
        /* console calls tty_register_driver() before kmalloc() works.
2366
         * Thus, we can't devfs_register() then.  Do so now, instead.
2367
         */
2368
#ifdef CONFIG_VT
2369
        con_init_devfs();
2370
#endif
2371
 
2372
#ifdef CONFIG_UNIX98_PTYS
2373
        dev_ptmx_driver = dev_tty_driver;
2374
        dev_ptmx_driver.driver_name = "/dev/ptmx";
2375
        dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2376
        dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2377
        dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2378
        dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2379
        dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2380
 
2381
        if (tty_register_driver(&dev_ptmx_driver))
2382
                panic("Couldn't register /dev/ptmx driver\n");
2383
#endif
2384
 
2385
#ifdef CONFIG_VT
2386
        dev_console_driver = dev_tty_driver;
2387
        dev_console_driver.driver_name = "/dev/vc/0";
2388
        dev_console_driver.name = dev_console_driver.driver_name + 5;
2389
        dev_console_driver.major = TTY_MAJOR;
2390
        dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2391
        dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2392
 
2393
        if (tty_register_driver(&dev_console_driver))
2394
                panic("Couldn't register /dev/tty0 driver\n");
2395
 
2396
        kbd_init();
2397
#endif
2398
 
2399
#ifdef CONFIG_SGI_L1_SERIAL_CONSOLE
2400
        if (ia64_platform_is("sn2")) {
2401
                sn_sal_serial_console_init();
2402
                return; /* only one console right now for SN2 */
2403
        }
2404
#endif
2405
#ifdef CONFIG_ESPSERIAL  /* init ESP before rs, so rs doesn't see the port */
2406
        espserial_init();
2407
#endif
2408
#if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2409
        vme_scc_init();
2410
#endif
2411
#ifdef CONFIG_SERIAL_TX3912
2412
        tx3912_rs_init();
2413
#endif
2414
#ifdef CONFIG_ROCKETPORT
2415
        rp_init();
2416
#endif
2417
#ifdef CONFIG_SERIAL167
2418
        serial167_init();
2419
#endif
2420
#ifdef CONFIG_CYCLADES
2421
        cy_init();
2422
#endif
2423
#ifdef CONFIG_STALLION
2424
        stl_init();
2425
#endif
2426
#ifdef CONFIG_ISTALLION
2427
        stli_init();
2428
#endif
2429
#ifdef CONFIG_DIGI
2430
        pcxe_init();
2431
#endif
2432
#ifdef CONFIG_DIGIEPCA
2433
        pc_init();
2434
#endif
2435
#ifdef CONFIG_SPECIALIX
2436
        specialix_init();
2437
#endif
2438
#if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2439
        rs_8xx_init();
2440
#endif /* CONFIG_8xx */
2441
        pty_init();
2442
#ifdef CONFIG_MOXA_SMARTIO
2443
        mxser_init();
2444
#endif  
2445
#ifdef CONFIG_MOXA_INTELLIO
2446
        moxa_init();
2447
#endif  
2448
#ifdef CONFIG_VT
2449
        vcs_init();
2450
#endif
2451
#ifdef CONFIG_TN3270
2452
        tub3270_init();
2453
#endif
2454
#ifdef CONFIG_TN3215
2455
        tty3215_init();
2456
#endif
2457
#ifdef CONFIG_HWC
2458
        hwc_tty_init();
2459
#endif
2460
#ifdef CONFIG_A2232
2461
        a2232board_init();
2462
#endif
2463
}

powered by: WebSVN 2.1.0

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