~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/tty_io.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.