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