- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / tty_io.c
blob33f5bc879af259dfe27e702a57b594510efb8f6b
1 /*
2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
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.
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
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).
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
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.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
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
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
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>
91 #include <asm/uaccess.h>
92 #include <asm/system.h>
93 #include <asm/bitops.h>
95 #include <linux/kbd_kern.h>
96 #include <linux/vt_kern.h>
97 #include <linux/selection.h>
98 #include <linux/devfs_fs_kernel.h>
100 #include <linux/kmod.h>
102 #ifdef CONFIG_VT
103 extern void con_init_devfs (void);
104 #endif
105 extern int rio_init(void);
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)
112 #undef TTY_DEBUG_HANGUP
114 #define TTY_PARANOIA_CHECK 1
115 #define CHECK_TTY_COUNT 1
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 */
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
127 * redirect is the pseudo-tty that console output
128 * is redirected to if asked by TIOCCONS.
130 struct tty_struct * redirect;
132 static void initialize_tty_struct(struct tty_struct *tty);
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);
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
163 static inline struct tty_struct *alloc_tty_struct(void)
165 struct tty_struct *tty;
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);
174 return tty;
177 static inline void free_tty_struct(struct tty_struct *tty)
179 if (PAGE_SIZE > 8192)
180 kfree(tty);
181 else
182 free_page((unsigned long) tty);
186 * This routine returns the name of tty.
188 static char *
189 _tty_make_name(struct tty_struct *tty, const char *name, char *buf)
191 int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
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);
199 return buf;
202 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
203 (tty)->driver.name_base)
205 char *tty_name(struct tty_struct *tty, char *buf)
207 return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
210 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
211 const char *routine)
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";
219 if (!tty) {
220 printk(badtty, kdevname(device), routine);
221 return 1;
223 if (tty->magic != TTY_MAGIC) {
224 printk(badmagic, kdevname(device), routine);
225 return 1;
227 #endif
228 return 0;
231 static int check_tty_count(struct tty_struct *tty, const char *routine)
233 #ifdef CHECK_TTY_COUNT
234 struct list_head *p;
235 int count = 0;
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++;
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;
252 #endif
253 return 0;
256 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
258 if (disc < N_TTY || disc >= NR_LDISCS)
259 return -EINVAL;
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));
268 return 0;
271 /* Set the discipline of a tty line. */
272 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
274 int retval = 0;
275 struct tty_ldisc o_ldisc;
276 char buf[64];
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);
287 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
288 return -EINVAL;
290 if (tty->ldisc.num == ldisc)
291 return 0; /* We are already in the desired discipline */
292 o_ldisc = tty->ldisc;
294 tty_wait_until_sent(tty, 0);
296 /* Shutdown the current discipline. */
297 if (tty->ldisc.close)
298 (tty->ldisc.close)(tty);
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);
314 if (r < 0)
315 panic("Couldn't open N_TTY ldisc for "
316 "%s --- error %d.",
317 tty_name(tty, buf), r);
321 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
322 tty->driver.set_ldisc(tty);
323 return retval;
327 * This routine returns a tty driver structure, given a device number
329 struct tty_driver *get_tty_driver(kdev_t device)
331 int major, minor;
332 struct tty_driver *p;
334 minor = MINOR(device);
335 major = MAJOR(device);
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;
346 return NULL;
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)
354 int tty_check_change(struct tty_struct * tty)
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;
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;
372 static ssize_t hung_up_tty_read(struct file * file, char * buf,
373 size_t count, loff_t *ppos)
375 /* Can't seek (pread) on ttys. */
376 if (ppos != &file->f_pos)
377 return -ESPIPE;
378 return 0;
381 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
382 size_t count, loff_t *ppos)
384 /* Can't seek (pwrite) on ttys. */
385 if (ppos != &file->f_pos)
386 return -ESPIPE;
387 return -EIO;
390 /* No kernel lock held - none needed ;) */
391 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
393 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
396 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
397 unsigned int cmd, unsigned long arg)
399 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
402 static loff_t tty_lseek(struct file * file, loff_t offset, int orig)
404 return -ESPIPE;
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,
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,
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..
432 void do_tty_hangup(void *data)
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;
440 if (!tty)
441 return;
443 /* inuse_filps is protected by the single kernel lock */
444 lock_kernel();
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;
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;
463 file_list_unlock();
465 /* FIXME! What are the locking issues here? This may me overdoing things.. */
467 unsigned long flags;
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);
480 wake_up_interruptible(&tty->write_wait);
481 wake_up_interruptible(&tty->read_wait);
484 * Shutdown the current line discipline, and reset it to
485 * N_TTY.
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);
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;
511 if (p->tty == tty)
512 p->tty = NULL;
514 read_unlock(&tasklist_lock);
516 tty->flags = 0;
517 tty->session = 0;
518 tty->pgrp = -1;
519 tty->ctrl_status = 0;
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.
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();
535 void tty_hangup(struct tty_struct * tty)
537 #ifdef TTY_DEBUG_HANGUP
538 char buf[64];
540 printk("%s hangup...\n", tty_name(tty, buf));
541 #endif
542 schedule_task(&tty->tq_hangup);
545 void tty_vhangup(struct tty_struct * tty)
547 #ifdef TTY_DEBUG_HANGUP
548 char buf[64];
550 printk("%s vhangup...\n", tty_name(tty, buf));
551 #endif
552 do_tty_hangup((void *) tty);
555 int tty_hung_up_p(struct file * filp)
557 return (filp->f_op == &hung_up_tty_fops);
561 * This function is typically called only by the session leader, when
562 * it wants to disassociate itself from its controlling tty.
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.
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.
573 void disassociate_ctty(int on_exit)
575 struct tty_struct *tty = current->tty;
576 struct task_struct *p;
577 int tty_pgrp = -1;
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);
588 return;
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);
596 current->tty_old_pgrp = 0;
597 tty->session = 0;
598 tty->pgrp = -1;
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);
607 void wait_for_keypress(void)
609 struct console *c = console_drivers;
610 if (c) c->wait_key(c);
613 void stop_tty(struct tty_struct *tty)
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);
623 if (tty->driver.stop)
624 (tty->driver.stop)(tty);
627 void start_tty(struct tty_struct *tty)
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);
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);
645 static ssize_t tty_read(struct file * file, char * buf, size_t count,
646 loff_t *ppos)
648 int i;
649 struct tty_struct * tty;
650 struct inode *inode;
652 /* Can't seek (pread) on ttys. */
653 if (ppos != &file->f_pos)
654 return -ESPIPE;
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;
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;
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;
691 * Split writes up in sane blocksizes to avoid
692 * denial-of-service type attacks
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)
701 ssize_t ret = 0, written = 0;
702 struct inode *inode = file->f_dentry->d_inode;
704 if (down_interruptible(&inode->i_sem)) {
705 return -ERESTARTSYS;
707 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
708 lock_kernel();
709 written = write(tty, file, buf, count);
710 unlock_kernel();
711 } else {
712 for (;;) {
713 unsigned long size = MAX(PAGE_SIZE*2,16384);
714 if (size > count)
715 size = count;
716 lock_kernel();
717 ret = write(tty, file, buf, size);
718 unlock_kernel();
719 if (ret <= 0)
720 break;
721 written += ret;
722 buf += ret;
723 count -= ret;
724 if (!count)
725 break;
726 ret = -ERESTARTSYS;
727 if (signal_pending(current))
728 break;
729 if (current->need_resched)
730 schedule();
733 if (written) {
734 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
735 ret = written;
737 up(&inode->i_sem);
738 return ret;
742 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
743 loff_t *ppos)
745 int is_console;
746 struct tty_struct * tty;
747 struct inode *inode;
749 /* Can't seek (pwrite) on ttys. */
750 if (ppos != &file->f_pos)
751 return -ESPIPE;
754 * For now, we redirect writes from /dev/console as
755 * well as /dev/tty0.
757 inode = file->f_dentry->d_inode;
758 is_console = (inode->i_rdev == SYSCONS_DEV ||
759 inode->i_rdev == CONSOLE_DEV);
761 if (is_console && redirect)
762 tty = redirect;
763 else
764 tty = (struct tty_struct *)file->private_data;
765 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
766 return -EIO;
767 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
768 return -EIO;
769 #if 0
770 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
771 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
772 if (is_orphaned_pgrp(current->pgrp))
773 return -EIO;
774 if (!is_ignored(SIGTTOU)) {
775 (void) kill_pg(current->pgrp, SIGTTOU, 1);
776 return -ERESTARTSYS;
779 #endif
780 if (!tty->ldisc.write)
781 return -EIO;
782 return do_tty_write(tty->ldisc.write, tty, file,
783 (const unsigned char *)buf, count);
786 /* Semaphore to protect creating and releasing a tty */
787 static DECLARE_MUTEX(tty_sem);
789 static void down_tty_sem(int index)
791 down(&tty_sem);
794 static void up_tty_sem(int index)
796 up(&tty_sem);
799 static void release_mem(struct tty_struct *tty, int idx);
802 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
803 * failed open. The new code protects the open with a semaphore, so it's
804 * really quite straightforward. The semaphore locking can probably be
805 * relaxed for the (most common) case of reopening a tty.
807 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
809 struct tty_struct *tty, *o_tty;
810 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
811 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
812 struct tty_driver *driver;
813 int retval=0;
814 int idx;
816 driver = get_tty_driver(device);
817 if (!driver)
818 return -ENODEV;
820 idx = MINOR(device) - driver->minor_start;
823 * Check whether we need to acquire the tty semaphore to avoid
824 * race conditions. For now, play it safe.
826 down_tty_sem(idx);
828 /* check whether we're reopening an existing tty */
829 tty = driver->table[idx];
830 if (tty) goto fast_track;
833 * First time open is complex, especially for PTY devices.
834 * This code guarantees that either everything succeeds and the
835 * TTY is ready for operation, or else the table slots are vacated
836 * and the allocated memory released. (Except that the termios
837 * and locked termios may be retained.)
840 o_tty = NULL;
841 tp = o_tp = NULL;
842 ltp = o_ltp = NULL;
844 tty = alloc_tty_struct();
845 if(!tty)
846 goto fail_no_mem;
847 initialize_tty_struct(tty);
848 tty->device = device;
849 tty->driver = *driver;
851 tp_loc = &driver->termios[idx];
852 if (!*tp_loc) {
853 tp = (struct termios *) kmalloc(sizeof(struct termios),
854 GFP_KERNEL);
855 if (!tp)
856 goto free_mem_out;
857 *tp = driver->init_termios;
860 ltp_loc = &driver->termios_locked[idx];
861 if (!*ltp_loc) {
862 ltp = (struct termios *) kmalloc(sizeof(struct termios),
863 GFP_KERNEL);
864 if (!ltp)
865 goto free_mem_out;
866 memset(ltp, 0, sizeof(struct termios));
869 if (driver->type == TTY_DRIVER_TYPE_PTY) {
870 o_tty = alloc_tty_struct();
871 if (!o_tty)
872 goto free_mem_out;
873 initialize_tty_struct(o_tty);
874 o_tty->device = (kdev_t) MKDEV(driver->other->major,
875 driver->other->minor_start + idx);
876 o_tty->driver = *driver->other;
878 o_tp_loc = &driver->other->termios[idx];
879 if (!*o_tp_loc) {
880 o_tp = (struct termios *)
881 kmalloc(sizeof(struct termios), GFP_KERNEL);
882 if (!o_tp)
883 goto free_mem_out;
884 *o_tp = driver->other->init_termios;
887 o_ltp_loc = &driver->other->termios_locked[idx];
888 if (!*o_ltp_loc) {
889 o_ltp = (struct termios *)
890 kmalloc(sizeof(struct termios), GFP_KERNEL);
891 if (!o_ltp)
892 goto free_mem_out;
893 memset(o_ltp, 0, sizeof(struct termios));
897 * Everything allocated ... set up the o_tty structure.
899 driver->other->table[idx] = o_tty;
900 if (!*o_tp_loc)
901 *o_tp_loc = o_tp;
902 if (!*o_ltp_loc)
903 *o_ltp_loc = o_ltp;
904 o_tty->termios = *o_tp_loc;
905 o_tty->termios_locked = *o_ltp_loc;
906 (*driver->other->refcount)++;
907 if (driver->subtype == PTY_TYPE_MASTER)
908 o_tty->count++;
910 /* Establish the links in both directions */
911 tty->link = o_tty;
912 o_tty->link = tty;
916 * All structures have been allocated, so now we install them.
917 * Failures after this point use release_mem to clean up, so
918 * there's no need to null out the local pointers.
920 driver->table[idx] = tty;
922 if (!*tp_loc)
923 *tp_loc = tp;
924 if (!*ltp_loc)
925 *ltp_loc = ltp;
926 tty->termios = *tp_loc;
927 tty->termios_locked = *ltp_loc;
928 (*driver->refcount)++;
929 tty->count++;
932 * Structures all installed ... call the ldisc open routines.
933 * If we fail here just call release_mem to clean up. No need
934 * to decrement the use counts, as release_mem doesn't care.
936 if (tty->ldisc.open) {
937 retval = (tty->ldisc.open)(tty);
938 if (retval)
939 goto release_mem_out;
941 if (o_tty && o_tty->ldisc.open) {
942 retval = (o_tty->ldisc.open)(o_tty);
943 if (retval) {
944 if (tty->ldisc.close)
945 (tty->ldisc.close)(tty);
946 goto release_mem_out;
949 goto success;
952 * This fast open can be used if the tty is already open.
953 * No memory is allocated, and the only failures are from
954 * attempting to open a closing tty or attempting multiple
955 * opens on a pty master.
957 fast_track:
958 if (test_bit(TTY_CLOSING, &tty->flags)) {
959 retval = -EIO;
960 goto end_init;
962 if (driver->type == TTY_DRIVER_TYPE_PTY &&
963 driver->subtype == PTY_TYPE_MASTER) {
965 * special case for PTY masters: only one open permitted,
966 * and the slave side open count is incremented as well.
968 if (tty->count) {
969 retval = -EIO;
970 goto end_init;
972 tty->link->count++;
974 tty->count++;
975 tty->driver = *driver; /* N.B. why do this every time?? */
977 success:
978 *ret_tty = tty;
980 /* All paths come through here to release the semaphore */
981 end_init:
982 up_tty_sem(idx);
983 return retval;
985 /* Release locally allocated memory ... nothing placed in slots */
986 free_mem_out:
987 if (o_tp)
988 kfree(o_tp);
989 if (o_tty)
990 free_tty_struct(o_tty);
991 if (ltp)
992 kfree(ltp);
993 if (tp)
994 kfree(tp);
995 free_tty_struct(tty);
997 fail_no_mem:
998 retval = -ENOMEM;
999 goto end_init;
1001 /* call the tty release_mem routine to clean out this slot */
1002 release_mem_out:
1003 printk("init_dev: ldisc open failed, clearing slot %d\n", idx);
1004 release_mem(tty, idx);
1005 goto end_init;
1009 * Releases memory associated with a tty structure, and clears out the
1010 * driver table slots.
1012 static void release_mem(struct tty_struct *tty, int idx)
1014 struct tty_struct *o_tty;
1015 struct termios *tp;
1017 if ((o_tty = tty->link) != NULL) {
1018 o_tty->driver.table[idx] = NULL;
1019 if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1020 tp = o_tty->driver.termios[idx];
1021 o_tty->driver.termios[idx] = NULL;
1022 kfree(tp);
1024 o_tty->magic = 0;
1025 (*o_tty->driver.refcount)--;
1026 free_tty_struct(o_tty);
1029 tty->driver.table[idx] = NULL;
1030 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1031 tp = tty->driver.termios[idx];
1032 tty->driver.termios[idx] = NULL;
1033 kfree(tp);
1035 tty->magic = 0;
1036 (*tty->driver.refcount)--;
1037 free_tty_struct(tty);
1041 * Even releasing the tty structures is a tricky business.. We have
1042 * to be very careful that the structures are all released at the
1043 * same time, as interrupts might otherwise get the wrong pointers.
1045 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1046 * lead to double frees or releasing memory still in use.
1048 static void release_dev(struct file * filp)
1050 struct tty_struct *tty, *o_tty;
1051 int pty_master, tty_closing, o_tty_closing, do_sleep;
1052 int idx;
1053 char buf[64];
1055 tty = (struct tty_struct *)filp->private_data;
1056 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1057 return;
1059 check_tty_count(tty, "release_dev");
1061 tty_fasync(-1, filp, 0);
1063 idx = MINOR(tty->device) - tty->driver.minor_start;
1064 pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1065 tty->driver.subtype == PTY_TYPE_MASTER);
1066 o_tty = tty->link;
1068 #ifdef TTY_PARANOIA_CHECK
1069 if (idx < 0 || idx >= tty->driver.num) {
1070 printk("release_dev: bad idx when trying to free (%s)\n",
1071 kdevname(tty->device));
1072 return;
1074 if (tty != tty->driver.table[idx]) {
1075 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1076 idx, kdevname(tty->device));
1077 return;
1079 if (tty->termios != tty->driver.termios[idx]) {
1080 printk("release_dev: driver.termios[%d] not termios "
1081 "for (%s)\n",
1082 idx, kdevname(tty->device));
1083 return;
1085 if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1086 printk("release_dev: driver.termios_locked[%d] not "
1087 "termios_locked for (%s)\n",
1088 idx, kdevname(tty->device));
1089 return;
1091 #endif
1093 #ifdef TTY_DEBUG_HANGUP
1094 printk("release_dev of %s (tty count=%d)...", tty_name(tty, buf),
1095 tty->count);
1096 #endif
1098 #ifdef TTY_PARANOIA_CHECK
1099 if (tty->driver.other) {
1100 if (o_tty != tty->driver.other->table[idx]) {
1101 printk("release_dev: other->table[%d] not o_tty for ("
1102 "%s)\n",
1103 idx, kdevname(tty->device));
1104 return;
1106 if (o_tty->termios != tty->driver.other->termios[idx]) {
1107 printk("release_dev: other->termios[%d] not o_termios "
1108 "for (%s)\n",
1109 idx, kdevname(tty->device));
1110 return;
1112 if (o_tty->termios_locked !=
1113 tty->driver.other->termios_locked[idx]) {
1114 printk("release_dev: other->termios_locked[%d] not "
1115 "o_termios_locked for (%s)\n",
1116 idx, kdevname(tty->device));
1117 return;
1119 if (o_tty->link != tty) {
1120 printk("release_dev: bad pty pointers\n");
1121 return;
1124 #endif
1126 if (tty->driver.close)
1127 tty->driver.close(tty, filp);
1130 * Sanity check: if tty->count is going to zero, there shouldn't be
1131 * any waiters on tty->read_wait or tty->write_wait. We test the
1132 * wait queues and kick everyone out _before_ actually starting to
1133 * close. This ensures that we won't block while releasing the tty
1134 * structure.
1136 * The test for the o_tty closing is necessary, since the master and
1137 * slave sides may close in any order. If the slave side closes out
1138 * first, its count will be one, since the master side holds an open.
1139 * Thus this test wouldn't be triggered at the time the slave closes,
1140 * so we do it now.
1142 * Note that it's possible for the tty to be opened again while we're
1143 * flushing out waiters. By recalculating the closing flags before
1144 * each iteration we avoid any problems.
1146 while (1) {
1147 tty_closing = tty->count <= 1;
1148 o_tty_closing = o_tty &&
1149 (o_tty->count <= (pty_master ? 1 : 0));
1150 do_sleep = 0;
1152 if (tty_closing) {
1153 if (waitqueue_active(&tty->read_wait)) {
1154 wake_up(&tty->read_wait);
1155 do_sleep++;
1157 if (waitqueue_active(&tty->write_wait)) {
1158 wake_up(&tty->write_wait);
1159 do_sleep++;
1162 if (o_tty_closing) {
1163 if (waitqueue_active(&o_tty->read_wait)) {
1164 wake_up(&o_tty->read_wait);
1165 do_sleep++;
1167 if (waitqueue_active(&o_tty->write_wait)) {
1168 wake_up(&o_tty->write_wait);
1169 do_sleep++;
1172 if (!do_sleep)
1173 break;
1175 printk("release_dev: %s: read/write wait queue active!\n",
1176 tty_name(tty, buf));
1177 schedule();
1181 * The closing flags are now consistent with the open counts on
1182 * both sides, and we've completed the last operation that could
1183 * block, so it's safe to proceed with closing.
1185 if (pty_master) {
1186 if (--o_tty->count < 0) {
1187 printk("release_dev: bad pty slave count (%d) for %s\n",
1188 o_tty->count, tty_name(o_tty, buf));
1189 o_tty->count = 0;
1192 if (--tty->count < 0) {
1193 printk("release_dev: bad tty->count (%d) for %s\n",
1194 tty->count, tty_name(tty, buf));
1195 tty->count = 0;
1199 * We've decremented tty->count, so we should zero out
1200 * filp->private_data, to break the link between the tty and
1201 * the file descriptor. Otherwise if filp_close() blocks before
1202 * the the file descriptor is removed from the inuse_filp
1203 * list, check_tty_count() could observe a discrepancy and
1204 * printk a warning message to the user.
1206 filp->private_data = 0;
1209 * Perform some housekeeping before deciding whether to return.
1211 * Set the TTY_CLOSING flag if this was the last open. In the
1212 * case of a pty we may have to wait around for the other side
1213 * to close, and TTY_CLOSING makes sure we can't be reopened.
1215 if(tty_closing)
1216 set_bit(TTY_CLOSING, &tty->flags);
1217 if(o_tty_closing)
1218 set_bit(TTY_CLOSING, &o_tty->flags);
1221 * If _either_ side is closing, make sure there aren't any
1222 * processes that still think tty or o_tty is their controlling
1223 * tty. Also, clear redirect if it points to either tty.
1225 if (tty_closing || o_tty_closing) {
1226 struct task_struct *p;
1228 read_lock(&tasklist_lock);
1229 for_each_task(p) {
1230 if (p->tty == tty || (o_tty && p->tty == o_tty))
1231 p->tty = NULL;
1233 read_unlock(&tasklist_lock);
1235 if (redirect == tty || (o_tty && redirect == o_tty))
1236 redirect = NULL;
1239 /* check whether both sides are closing ... */
1240 if (!tty_closing || (o_tty && !o_tty_closing))
1241 return;
1243 #ifdef TTY_DEBUG_HANGUP
1244 printk("freeing tty structure...");
1245 #endif
1248 * Shutdown the current line discipline, and reset it to N_TTY.
1249 * N.B. why reset ldisc when we're releasing the memory??
1251 if (tty->ldisc.close)
1252 (tty->ldisc.close)(tty);
1253 tty->ldisc = ldiscs[N_TTY];
1254 tty->termios->c_line = N_TTY;
1255 if (o_tty) {
1256 if (o_tty->ldisc.close)
1257 (o_tty->ldisc.close)(o_tty);
1258 o_tty->ldisc = ldiscs[N_TTY];
1262 * Make sure that the tty's task queue isn't activated.
1264 run_task_queue(&tq_timer);
1265 run_schedule_tasks();
1268 * The release_mem function takes care of the details of clearing
1269 * the slots and preserving the termios structure.
1271 release_mem(tty, idx);
1275 * tty_open and tty_release keep up the tty count that contains the
1276 * number of opens done on a tty. We cannot use the inode-count, as
1277 * different inodes might point to the same tty.
1279 * Open-counting is needed for pty masters, as well as for keeping
1280 * track of serial lines: DTR is dropped when the last close happens.
1281 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1283 * The termios state of a pty is reset on first open so that
1284 * settings don't persist across reuse.
1286 static int tty_open(struct inode * inode, struct file * filp)
1288 struct tty_struct *tty;
1289 int noctty, retval;
1290 kdev_t device;
1291 unsigned short saved_flags;
1292 char buf[64];
1294 saved_flags = filp->f_flags;
1295 retry_open:
1296 noctty = filp->f_flags & O_NOCTTY;
1297 device = inode->i_rdev;
1298 if (device == TTY_DEV) {
1299 if (!current->tty)
1300 return -ENXIO;
1301 device = current->tty->device;
1302 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1303 /* noctty = 1; */
1305 #ifdef CONFIG_VT
1306 if (device == CONSOLE_DEV) {
1307 extern int fg_console;
1308 device = MKDEV(TTY_MAJOR, fg_console + 1);
1309 noctty = 1;
1311 #endif
1312 if (device == SYSCONS_DEV) {
1313 struct console *c = console_drivers;
1314 while(c && !c->device)
1315 c = c->next;
1316 if (!c)
1317 return -ENODEV;
1318 device = c->device(c);
1319 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1320 noctty = 1;
1323 if (device == PTMX_DEV) {
1324 #ifdef CONFIG_UNIX98_PTYS
1326 /* find a free pty. */
1327 int major, minor;
1328 struct tty_driver *driver;
1330 /* find a device that is not in use. */
1331 retval = -1;
1332 for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1333 driver = &ptm_driver[major];
1334 for (minor = driver->minor_start ;
1335 minor < driver->minor_start + driver->num ;
1336 minor++) {
1337 device = MKDEV(driver->major, minor);
1338 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1341 return -EIO; /* no free ptys */
1342 ptmx_found:
1343 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1344 minor -= driver->minor_start;
1345 devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1346 tty_register_devfs(&pts_driver[major], DEVFS_FL_NO_PERSISTENCE,
1347 pts_driver[major].minor_start + minor);
1348 noctty = 1;
1349 goto init_dev_done;
1351 #else /* CONFIG_UNIX_98_PTYS */
1353 return -ENODEV;
1355 #endif /* CONFIG_UNIX_98_PTYS */
1358 retval = init_dev(device, &tty);
1359 if (retval)
1360 return retval;
1362 #ifdef CONFIG_UNIX98_PTYS
1363 init_dev_done:
1364 #endif
1365 filp->private_data = tty;
1366 file_move(filp, &tty->tty_files);
1367 check_tty_count(tty, "tty_open");
1368 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1369 tty->driver.subtype == PTY_TYPE_MASTER)
1370 noctty = 1;
1371 #ifdef TTY_DEBUG_HANGUP
1372 printk("opening %s...", tty_name(tty, buf));
1373 #endif
1374 if (tty->driver.open)
1375 retval = tty->driver.open(tty, filp);
1376 else
1377 retval = -ENODEV;
1378 filp->f_flags = saved_flags;
1380 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1381 retval = -EBUSY;
1383 if (retval) {
1384 #ifdef TTY_DEBUG_HANGUP
1385 printk("error %d in opening %s...", retval,
1386 tty_name(tty, buf));
1387 #endif
1389 release_dev(filp);
1390 if (retval != -ERESTARTSYS)
1391 return retval;
1392 if (signal_pending(current))
1393 return retval;
1394 schedule();
1396 * Need to reset f_op in case a hangup happened.
1398 filp->f_op = &tty_fops;
1399 goto retry_open;
1401 if (!noctty &&
1402 current->leader &&
1403 !current->tty &&
1404 tty->session == 0) {
1405 task_lock(current);
1406 current->tty = tty;
1407 task_unlock(current);
1408 current->tty_old_pgrp = 0;
1409 tty->session = current->session;
1410 tty->pgrp = current->pgrp;
1412 if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1413 (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1414 (tty->count == 1)) {
1415 static int nr_warns;
1416 if (nr_warns < 5) {
1417 printk(KERN_WARNING "tty_io.c: "
1418 "process %d (%s) used obsolete /dev/%s - "
1419 "update software to use /dev/ttyS%d\n",
1420 current->pid, current->comm,
1421 tty_name(tty, buf), TTY_NUMBER(tty));
1422 nr_warns++;
1425 return 0;
1428 static int tty_release(struct inode * inode, struct file * filp)
1430 lock_kernel();
1431 release_dev(filp);
1432 unlock_kernel();
1433 return 0;
1436 /* No kernel lock held - fine */
1437 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1439 struct tty_struct * tty;
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;
1445 if (tty->ldisc.poll)
1446 return (tty->ldisc.poll)(tty, filp, wait);
1447 return 0;
1450 static int tty_fasync(int fd, struct file * filp, int on)
1452 struct tty_struct * tty;
1453 int retval;
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;
1459 retval = fasync_helper(fd, filp, on, &tty->fasync);
1460 if (retval <= 0)
1461 return retval;
1463 if (on) {
1464 if (!waitqueue_active(&tty->read_wait))
1465 tty->minimum_to_wake = 1;
1466 if (filp->f_owner.pid == 0) {
1467 filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1468 filp->f_owner.uid = current->uid;
1469 filp->f_owner.euid = current->euid;
1471 } else {
1472 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1473 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1475 return 0;
1478 static int tiocsti(struct tty_struct *tty, char * arg)
1480 char ch, mbz = 0;
1482 if ((current->tty != tty) && !suser())
1483 return -EPERM;
1484 if (get_user(ch, arg))
1485 return -EFAULT;
1486 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1487 return 0;
1490 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1492 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1493 return -EFAULT;
1494 return 0;
1497 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1498 struct winsize * arg)
1500 struct winsize tmp_ws;
1502 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1503 return -EFAULT;
1504 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1505 return 0;
1506 if (tty->pgrp > 0)
1507 kill_pg(tty->pgrp, SIGWINCH, 1);
1508 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1509 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1510 tty->winsize = tmp_ws;
1511 real_tty->winsize = tmp_ws;
1512 return 0;
1515 static int tioccons(struct inode *inode,
1516 struct tty_struct *tty, struct tty_struct *real_tty)
1518 if (inode->i_rdev == SYSCONS_DEV ||
1519 inode->i_rdev == CONSOLE_DEV) {
1520 if (!suser())
1521 return -EPERM;
1522 redirect = NULL;
1523 return 0;
1525 if (redirect)
1526 return -EBUSY;
1527 redirect = real_tty;
1528 return 0;
1532 static int fionbio(struct file *file, int *arg)
1534 int nonblock;
1536 if (get_user(nonblock, arg))
1537 return -EFAULT;
1539 if (nonblock)
1540 file->f_flags |= O_NONBLOCK;
1541 else
1542 file->f_flags &= ~O_NONBLOCK;
1543 return 0;
1546 static int tiocsctty(struct tty_struct *tty, int arg)
1548 if (current->leader &&
1549 (current->session == tty->session))
1550 return 0;
1552 * The process must be a session leader and
1553 * not have a controlling tty already.
1555 if (!current->leader || current->tty)
1556 return -EPERM;
1557 if (tty->session > 0) {
1559 * This tty is already the controlling
1560 * tty for another session group!
1562 if ((arg == 1) && suser()) {
1564 * Steal it away
1566 struct task_struct *p;
1568 read_lock(&tasklist_lock);
1569 for_each_task(p)
1570 if (p->tty == tty)
1571 p->tty = NULL;
1572 read_unlock(&tasklist_lock);
1573 } else
1574 return -EPERM;
1576 task_lock(current);
1577 current->tty = tty;
1578 task_unlock(current);
1579 current->tty_old_pgrp = 0;
1580 tty->session = current->session;
1581 tty->pgrp = current->pgrp;
1582 return 0;
1585 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1588 * (tty == real_tty) is a cheap way of
1589 * testing if the tty is NOT a master pty.
1591 if (tty == real_tty && current->tty != real_tty)
1592 return -ENOTTY;
1593 return put_user(real_tty->pgrp, arg);
1596 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1598 pid_t pgrp;
1599 int retval = tty_check_change(real_tty);
1601 if (retval == -EIO)
1602 return -ENOTTY;
1603 if (retval)
1604 return retval;
1605 if (!current->tty ||
1606 (current->tty != real_tty) ||
1607 (real_tty->session != current->session))
1608 return -ENOTTY;
1609 get_user(pgrp, (pid_t *) arg);
1610 if (pgrp < 0)
1611 return -EINVAL;
1612 if (session_of_pgrp(pgrp) != current->session)
1613 return -EPERM;
1614 real_tty->pgrp = pgrp;
1615 return 0;
1618 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1621 * (tty == real_tty) is a cheap way of
1622 * testing if the tty is NOT a master pty.
1624 if (tty == real_tty && current->tty != real_tty)
1625 return -ENOTTY;
1626 if (real_tty->session <= 0)
1627 return -ENOTTY;
1628 return put_user(real_tty->session, arg);
1631 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1633 if (copy_to_user(arg, tty, sizeof(*arg)))
1634 return -EFAULT;
1635 return 0;
1638 static int tiocsetd(struct tty_struct *tty, int *arg)
1640 int retval, ldisc;
1642 retval = get_user(ldisc, arg);
1643 if (retval)
1644 return retval;
1645 return tty_set_ldisc(tty, ldisc);
1648 static int send_break(struct tty_struct *tty, int duration)
1650 set_current_state(TASK_INTERRUPTIBLE);
1652 tty->driver.break_ctl(tty, -1);
1653 if (!signal_pending(current))
1654 schedule_timeout(duration);
1655 tty->driver.break_ctl(tty, 0);
1656 if (signal_pending(current))
1657 return -EINTR;
1658 return 0;
1662 * Split this up, as gcc can choke on it otherwise..
1664 int tty_ioctl(struct inode * inode, struct file * file,
1665 unsigned int cmd, unsigned long arg)
1667 struct tty_struct *tty, *real_tty;
1668 int retval;
1670 tty = (struct tty_struct *)file->private_data;
1671 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1672 return -EINVAL;
1674 real_tty = tty;
1675 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1676 tty->driver.subtype == PTY_TYPE_MASTER)
1677 real_tty = tty->link;
1680 * Break handling by driver
1682 if (!tty->driver.break_ctl) {
1683 switch(cmd) {
1684 case TIOCSBRK:
1685 case TIOCCBRK:
1686 if (tty->driver.ioctl)
1687 return tty->driver.ioctl(tty, file, cmd, arg);
1688 return -EINVAL;
1690 /* These two ioctl's always return success; even if */
1691 /* the driver doesn't support them. */
1692 case TCSBRK:
1693 case TCSBRKP:
1694 if (!tty->driver.ioctl)
1695 return 0;
1696 retval = tty->driver.ioctl(tty, file, cmd, arg);
1697 if (retval == -ENOIOCTLCMD)
1698 retval = 0;
1699 return retval;
1704 * Factor out some common prep work
1706 switch (cmd) {
1707 case TIOCSETD:
1708 case TIOCSBRK:
1709 case TIOCCBRK:
1710 case TCSBRK:
1711 case TCSBRKP:
1712 retval = tty_check_change(tty);
1713 if (retval)
1714 return retval;
1715 if (cmd != TIOCCBRK) {
1716 tty_wait_until_sent(tty, 0);
1717 if (signal_pending(current))
1718 return -EINTR;
1720 break;
1723 switch (cmd) {
1724 case TIOCSTI:
1725 return tiocsti(tty, (char *)arg);
1726 case TIOCGWINSZ:
1727 return tiocgwinsz(tty, (struct winsize *) arg);
1728 case TIOCSWINSZ:
1729 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1730 case TIOCCONS:
1731 return tioccons(inode, tty, real_tty);
1732 case FIONBIO:
1733 return fionbio(file, (int *) arg);
1734 case TIOCEXCL:
1735 set_bit(TTY_EXCLUSIVE, &tty->flags);
1736 return 0;
1737 case TIOCNXCL:
1738 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1739 return 0;
1740 case TIOCNOTTY:
1741 if (current->tty != tty)
1742 return -ENOTTY;
1743 if (current->leader)
1744 disassociate_ctty(0);
1745 task_lock(current);
1746 current->tty = NULL;
1747 task_unlock(current);
1748 return 0;
1749 case TIOCSCTTY:
1750 return tiocsctty(tty, arg);
1751 case TIOCGPGRP:
1752 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1753 case TIOCSPGRP:
1754 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1755 case TIOCGSID:
1756 return tiocgsid(tty, real_tty, (pid_t *) arg);
1757 case TIOCGETD:
1758 return put_user(tty->ldisc.num, (int *) arg);
1759 case TIOCSETD:
1760 return tiocsetd(tty, (int *) arg);
1761 #ifdef CONFIG_VT
1762 case TIOCLINUX:
1763 return tioclinux(tty, arg);
1764 #endif
1765 case TIOCTTYGSTRUCT:
1766 return tiocttygstruct(tty, (struct tty_struct *) arg);
1769 * Break handling
1771 case TIOCSBRK: /* Turn break on, unconditionally */
1772 tty->driver.break_ctl(tty, -1);
1773 return 0;
1775 case TIOCCBRK: /* Turn break off, unconditionally */
1776 tty->driver.break_ctl(tty, 0);
1777 return 0;
1778 case TCSBRK: /* SVID version: non-zero arg --> no break */
1780 * XXX is the above comment correct, or the
1781 * code below correct? Is this ioctl used at
1782 * all by anyone?
1784 if (!arg)
1785 return send_break(tty, HZ/4);
1786 return 0;
1787 case TCSBRKP: /* support for POSIX tcsendbreak() */
1788 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1790 if (tty->driver.ioctl) {
1791 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1792 if (retval != -ENOIOCTLCMD)
1793 return retval;
1795 if (tty->ldisc.ioctl) {
1796 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1797 if (retval != -ENOIOCTLCMD)
1798 return retval;
1800 return -EINVAL;
1805 * This implements the "Secure Attention Key" --- the idea is to
1806 * prevent trojan horses by killing all processes associated with this
1807 * tty when the user hits the "Secure Attention Key". Required for
1808 * super-paranoid applications --- see the Orange Book for more details.
1810 * This code could be nicer; ideally it should send a HUP, wait a few
1811 * seconds, then send a INT, and then a KILL signal. But you then
1812 * have to coordinate with the init process, since all processes associated
1813 * with the current tty must be dead before the new getty is allowed
1814 * to spawn.
1816 * Now, if it would be correct ;-/ The current code has a nasty hole -
1817 * it doesn't catch files in flight. We may send the descriptor to ourselves
1818 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1820 void do_SAK( struct tty_struct *tty)
1822 #ifdef TTY_SOFT_SAK
1823 tty_hangup(tty);
1824 #else
1825 struct task_struct *p;
1826 int session;
1827 int i;
1828 struct file *filp;
1830 if (!tty)
1831 return;
1832 session = tty->session;
1833 if (tty->ldisc.flush_buffer)
1834 tty->ldisc.flush_buffer(tty);
1835 if (tty->driver.flush_buffer)
1836 tty->driver.flush_buffer(tty);
1837 read_lock(&tasklist_lock);
1838 for_each_task(p) {
1839 if ((p->tty == tty) ||
1840 ((session > 0) && (p->session == session))) {
1841 send_sig(SIGKILL, p, 1);
1842 continue;
1844 task_lock(p);
1845 if (p->files) {
1846 read_lock(&p->files->file_lock);
1847 /* FIXME: p->files could change */
1848 for (i=0; i < p->files->max_fds; i++) {
1849 filp = fcheck_files(p->files, i);
1850 if (filp && (filp->f_op == &tty_fops) &&
1851 (filp->private_data == tty)) {
1852 send_sig(SIGKILL, p, 1);
1853 break;
1856 read_unlock(&p->files->file_lock);
1858 task_unlock(p);
1860 read_unlock(&tasklist_lock);
1861 #endif
1865 * This routine is called out of the software interrupt to flush data
1866 * from the flip buffer to the line discipline.
1868 static void flush_to_ldisc(void *private_)
1870 struct tty_struct *tty = (struct tty_struct *) private_;
1871 unsigned char *cp;
1872 char *fp;
1873 int count;
1874 unsigned long flags;
1876 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1877 queue_task(&tty->flip.tqueue, &tq_timer);
1878 return;
1880 if (tty->flip.buf_num) {
1881 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1882 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1883 tty->flip.buf_num = 0;
1885 save_flags(flags); cli();
1886 tty->flip.char_buf_ptr = tty->flip.char_buf;
1887 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1888 } else {
1889 cp = tty->flip.char_buf;
1890 fp = tty->flip.flag_buf;
1891 tty->flip.buf_num = 1;
1893 save_flags(flags); cli();
1894 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1895 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1897 count = tty->flip.count;
1898 tty->flip.count = 0;
1899 restore_flags(flags);
1901 tty->ldisc.receive_buf(tty, cp, fp, count);
1905 * Routine which returns the baud rate of the tty
1907 * Note that the baud_table needs to be kept in sync with the
1908 * include/asm/termbits.h file.
1910 static int baud_table[] = {
1911 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1912 9600, 19200, 38400, 57600, 115200, 230400, 460800,
1913 #ifdef __sparc__
1914 76800, 153600, 307200, 614400, 921600
1915 #else
1916 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1917 2500000, 3000000, 3500000, 4000000
1918 #endif
1921 static int n_baud_table = sizeof(baud_table)/sizeof(int);
1923 int tty_get_baud_rate(struct tty_struct *tty)
1925 unsigned int cflag, i;
1927 cflag = tty->termios->c_cflag;
1929 i = cflag & CBAUD;
1930 if (i & CBAUDEX) {
1931 i &= ~CBAUDEX;
1932 if (i < 1 || i+15 >= n_baud_table)
1933 tty->termios->c_cflag &= ~CBAUDEX;
1934 else
1935 i += 15;
1937 if (i==15 && tty->alt_speed) {
1938 if (!tty->warned) {
1939 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1940 tty->warned = 1;
1942 return(tty->alt_speed);
1945 return baud_table[i];
1948 void tty_flip_buffer_push(struct tty_struct *tty)
1950 if (tty->low_latency)
1951 flush_to_ldisc((void *) tty);
1952 else
1953 queue_task(&tty->flip.tqueue, &tq_timer);
1957 * This subroutine initializes a tty structure.
1959 static void initialize_tty_struct(struct tty_struct *tty)
1961 memset(tty, 0, sizeof(struct tty_struct));
1962 tty->magic = TTY_MAGIC;
1963 tty->ldisc = ldiscs[N_TTY];
1964 tty->pgrp = -1;
1965 tty->flip.char_buf_ptr = tty->flip.char_buf;
1966 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1967 tty->flip.tqueue.routine = flush_to_ldisc;
1968 tty->flip.tqueue.data = tty;
1969 init_MUTEX(&tty->flip.pty_sem);
1970 init_waitqueue_head(&tty->write_wait);
1971 init_waitqueue_head(&tty->read_wait);
1972 tty->tq_hangup.routine = do_tty_hangup;
1973 tty->tq_hangup.data = tty;
1974 sema_init(&tty->atomic_read, 1);
1975 spin_lock_init(&tty->read_lock);
1976 INIT_LIST_HEAD(&tty->tty_files);
1980 * The default put_char routine if the driver did not define one.
1982 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1984 tty->driver.write(tty, 0, &ch, 1);
1988 * Register a tty device described by <driver>, with minor number <minor>.
1990 void tty_register_devfs (struct tty_driver *driver, unsigned int flags,
1991 unsigned int minor)
1993 #ifdef CONFIG_DEVFS_FS
1994 umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
1995 struct tty_struct tty;
1996 char buf[32];
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;
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;
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 */
2026 void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2028 #ifdef CONFIG_DEVFS_FS
2029 void * handle;
2030 struct tty_struct tty;
2031 char buf[32];
2033 tty.driver = *driver;
2034 tty.device = MKDEV(driver->major, minor);
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 */
2043 EXPORT_SYMBOL(tty_register_devfs);
2044 EXPORT_SYMBOL(tty_unregister_devfs);
2047 * Called by a tty driver to register itself.
2049 int tty_register_driver(struct tty_driver *driver)
2051 int error;
2052 int i;
2054 if (driver->flags & TTY_DRIVER_INSTALLED)
2055 return 0;
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;
2063 if (!driver->put_char)
2064 driver->put_char = tty_default_put_char;
2066 driver->prev = 0;
2067 driver->next = tty_drivers;
2068 if (tty_drivers) tty_drivers->prev = driver;
2069 tty_drivers = driver;
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);
2075 proc_tty_register_driver(driver);
2076 return error;
2080 * Called by a tty driver to unregister itself.
2082 int tty_unregister_driver(struct tty_driver *driver)
2084 int retval;
2085 struct tty_driver *p;
2086 int i, found = 0;
2087 struct termios *tp;
2088 const char *othername = NULL;
2090 if (*driver->refcount)
2091 return -EBUSY;
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;
2100 if (!found)
2101 return -ENOENT;
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);
2110 if (driver->prev)
2111 driver->prev->next = driver->next;
2112 else
2113 tty_drivers = driver->next;
2115 if (driver->next)
2116 driver->next->prev = driver->prev;
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.
2123 for (i = 0; i < driver->num; i++) {
2124 tp = driver->termios[i];
2125 if (tp) {
2126 driver->termios[i] = NULL;
2127 kfree(tp);
2129 tp = driver->termios_locked[i];
2130 if (tp) {
2131 driver->termios_locked[i] = NULL;
2132 kfree(tp);
2134 tty_unregister_devfs(driver, driver->minor_start + i);
2136 proc_tty_unregister_driver(driver);
2137 return 0;
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.
2147 void __init console_init(void)
2149 /* Setup the default TTY line discipline. */
2150 memset(ldiscs, 0, sizeof(ldiscs));
2151 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2154 * Set up the standard termios. Individual tty drivers may
2155 * deviate from this; this is used as a template.
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;
2166 * set up the console device so that later boot sequences can
2167 * inform about problems etc..
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
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
2217 * Ok, now we can initialize the rest of the tty devices and can count
2218 * on memory allocations, interrupts etc..
2220 void __init tty_init(void)
2222 if (sizeof(struct tty_struct) > PAGE_SIZE)
2223 panic("size of tty structure > PAGE_SIZE!");
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.
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;
2242 if (tty_register_driver(&dev_tty_driver))
2243 panic("Couldn't register /dev/tty driver\n");
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;
2253 if (tty_register_driver(&dev_syscons_driver))
2254 panic("Couldn't register /dev/console driver\n");
2256 /* console calls tty_register_driver() before kmalloc() works.
2257 * Thus, we can't devfs_register() then. Do so now, instead.
2259 #ifdef CONFIG_VT
2260 con_init_devfs();
2261 #endif
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;
2272 if (tty_register_driver(&dev_ptmx_driver))
2273 panic("Couldn't register /dev/ptmx driver\n");
2274 #endif
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;
2284 if (tty_register_driver(&dev_console_driver))
2285 panic("Couldn't register /dev/tty0 driver\n");
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_RISCOM8
2323 riscom8_init();
2324 #endif
2325 #ifdef CONFIG_SPECIALIX
2326 specialix_init();
2327 #endif
2328 #ifdef CONFIG_SX
2329 sx_init();
2330 #endif
2331 #ifdef CONFIG_RIO
2332 rio_init();
2333 #endif
2334 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2335 rs_8xx_init();
2336 #endif /* CONFIG_8xx */
2337 pty_init();
2338 #ifdef CONFIG_MOXA_SMARTIO
2339 mxser_init();
2340 #endif
2341 #ifdef CONFIG_MOXA_INTELLIO
2342 moxa_init();
2343 #endif
2344 #ifdef CONFIG_VT
2345 vcs_init();
2346 #endif