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