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