Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / tty_io.c
blobd702851d787edf45da3f63ab943e73e5ab09626a
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_HWC
154 extern void hwc_console_init(void);
155 #endif
156 #ifdef CONFIG_3215
157 extern void con3215_init(void);
158 #endif /* CONFIG_3215 */
161 #ifndef MIN
162 #define MIN(a,b) ((a) < (b) ? (a) : (b))
163 #endif
166 * This routine returns the name of tty.
168 static char *
169 _tty_make_name(struct tty_struct *tty, const char *name, char *buf)
171 int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
173 if (!tty) /* Hmm. NULL pointer. That's fun. */
174 strcpy(buf, "NULL tty");
175 else
176 sprintf(buf, name,
177 idx + tty->driver.name_base);
179 return buf;
182 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
183 (tty)->driver.name_base)
185 char *tty_name(struct tty_struct *tty, char *buf)
187 return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
190 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
191 const char *routine)
193 #ifdef TTY_PARANOIA_CHECK
194 static const char *badmagic =
195 "Warning: bad magic number for tty struct (%s) in %s\n";
196 static const char *badtty =
197 "Warning: null TTY for (%s) in %s\n";
199 if (!tty) {
200 printk(badtty, kdevname(device), routine);
201 return 1;
203 if (tty->magic != TTY_MAGIC) {
204 printk(badmagic, kdevname(device), routine);
205 return 1;
207 #endif
208 return 0;
211 static int check_tty_count(struct tty_struct *tty, const char *routine)
213 #ifdef CHECK_TTY_COUNT
214 struct list_head *p;
215 int count = 0;
217 file_list_lock();
218 for(p = tty->tty_files.next; p != &tty->tty_files; p = p->next) {
219 if(list_entry(p, struct file, f_list)->private_data == tty)
220 count++;
222 file_list_unlock();
223 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
224 tty->driver.subtype == PTY_TYPE_SLAVE &&
225 tty->link && tty->link->count)
226 count++;
227 if (tty->count != count) {
228 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
229 kdevname(tty->device), tty->count, count, routine);
230 return count;
232 #endif
233 return 0;
236 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
238 if (disc < N_TTY || disc >= NR_LDISCS)
239 return -EINVAL;
241 if (new_ldisc) {
242 ldiscs[disc] = *new_ldisc;
243 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
244 ldiscs[disc].num = disc;
245 } else
246 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
248 return 0;
251 /* Set the discipline of a tty line. */
252 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
254 int retval = 0;
255 struct tty_ldisc o_ldisc;
256 char buf[64];
258 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
259 return -EINVAL;
260 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
261 /* Cyrus Durgin <cider@speakeasy.org> */
262 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
263 char modname [20];
264 sprintf(modname, "tty-ldisc-%d", ldisc);
265 request_module (modname);
267 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
268 return -EINVAL;
270 if (tty->ldisc.num == ldisc)
271 return 0; /* We are already in the desired discipline */
272 o_ldisc = tty->ldisc;
274 tty_wait_until_sent(tty, 0);
276 /* Shutdown the current discipline. */
277 if (tty->ldisc.close)
278 (tty->ldisc.close)(tty);
280 /* Now set up the new line discipline. */
281 tty->ldisc = ldiscs[ldisc];
282 tty->termios->c_line = ldisc;
283 if (tty->ldisc.open)
284 retval = (tty->ldisc.open)(tty);
285 if (retval < 0) {
286 tty->ldisc = o_ldisc;
287 tty->termios->c_line = tty->ldisc.num;
288 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
289 tty->ldisc = ldiscs[N_TTY];
290 tty->termios->c_line = N_TTY;
291 if (tty->ldisc.open) {
292 int r = tty->ldisc.open(tty);
294 if (r < 0)
295 panic("Couldn't open N_TTY ldisc for "
296 "%s --- error %d.",
297 tty_name(tty, buf), r);
301 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
302 tty->driver.set_ldisc(tty);
303 return retval;
307 * This routine returns a tty driver structure, given a device number
309 struct tty_driver *get_tty_driver(kdev_t device)
311 int major, minor;
312 struct tty_driver *p;
314 minor = MINOR(device);
315 major = MAJOR(device);
317 for (p = tty_drivers; p; p = p->next) {
318 if (p->major != major)
319 continue;
320 if (minor < p->minor_start)
321 continue;
322 if (minor >= p->minor_start + p->num)
323 continue;
324 return p;
326 return NULL;
330 * If we try to write to, or set the state of, a terminal and we're
331 * not in the foreground, send a SIGTTOU. If the signal is blocked or
332 * ignored, go ahead and perform the operation. (POSIX 7.2)
334 int tty_check_change(struct tty_struct * tty)
336 if (current->tty != tty)
337 return 0;
338 if (tty->pgrp <= 0) {
339 printk("tty_check_change: tty->pgrp <= 0!\n");
340 return 0;
342 if (current->pgrp == tty->pgrp)
343 return 0;
344 if (is_ignored(SIGTTOU))
345 return 0;
346 if (is_orphaned_pgrp(current->pgrp))
347 return -EIO;
348 (void) kill_pg(current->pgrp,SIGTTOU,1);
349 return -ERESTARTSYS;
352 static ssize_t hung_up_tty_read(struct file * file, char * buf,
353 size_t count, loff_t *ppos)
355 /* Can't seek (pread) on ttys. */
356 if (ppos != &file->f_pos)
357 return -ESPIPE;
358 return 0;
361 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
362 size_t count, loff_t *ppos)
364 /* Can't seek (pwrite) on ttys. */
365 if (ppos != &file->f_pos)
366 return -ESPIPE;
367 return -EIO;
370 /* No kernel lock held - none needed ;) */
371 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
373 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
376 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
377 unsigned int cmd, unsigned long arg)
379 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
382 static loff_t tty_lseek(struct file * file, loff_t offset, int orig)
384 return -ESPIPE;
387 static struct file_operations tty_fops = {
388 llseek: tty_lseek,
389 read: tty_read,
390 write: tty_write,
391 poll: tty_poll,
392 ioctl: tty_ioctl,
393 open: tty_open,
394 release: tty_release,
395 fasync: tty_fasync,
398 static struct file_operations hung_up_tty_fops = {
399 llseek: tty_lseek,
400 read: hung_up_tty_read,
401 write: hung_up_tty_write,
402 poll: hung_up_tty_poll,
403 ioctl: hung_up_tty_ioctl,
404 release: tty_release,
408 * This can be called through the "tq_scheduler"
409 * task-list. That is process synchronous, but
410 * doesn't hold any locks, so we need to make
411 * sure we have the appropriate locks for what
412 * we're doing..
414 void do_tty_hangup(void *data)
416 struct tty_struct *tty = (struct tty_struct *) data;
417 struct file * cons_filp = NULL;
418 struct task_struct *p;
419 struct list_head *l;
420 int closecount = 0, n;
422 if (!tty)
423 return;
425 /* inuse_filps is protected by the single kernel lock */
426 lock_kernel();
428 check_tty_count(tty, "do_tty_hangup");
429 file_list_lock();
430 for (l = tty->tty_files.next; l != &tty->tty_files; l = l->next) {
431 struct file * filp = list_entry(l, struct file, f_list);
432 if (!filp->f_dentry)
433 continue;
434 if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
435 filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
436 cons_filp = filp;
437 continue;
439 if (filp->f_op != &tty_fops)
440 continue;
441 closecount++;
442 tty_fasync(-1, filp, 0); /* can't block */
443 filp->f_op = &hung_up_tty_fops;
445 file_list_unlock();
447 /* FIXME! What are the locking issues here? This may me overdoing things.. */
449 unsigned long flags;
451 save_flags(flags); cli();
452 if (tty->ldisc.flush_buffer)
453 tty->ldisc.flush_buffer(tty);
454 if (tty->driver.flush_buffer)
455 tty->driver.flush_buffer(tty);
456 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
457 tty->ldisc.write_wakeup)
458 (tty->ldisc.write_wakeup)(tty);
459 restore_flags(flags);
462 wake_up_interruptible(&tty->write_wait);
463 wake_up_interruptible(&tty->read_wait);
466 * Shutdown the current line discipline, and reset it to
467 * N_TTY.
469 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
470 *tty->termios = tty->driver.init_termios;
471 if (tty->ldisc.num != ldiscs[N_TTY].num) {
472 if (tty->ldisc.close)
473 (tty->ldisc.close)(tty);
474 tty->ldisc = ldiscs[N_TTY];
475 tty->termios->c_line = N_TTY;
476 if (tty->ldisc.open) {
477 int i = (tty->ldisc.open)(tty);
478 if (i < 0)
479 printk("do_tty_hangup: N_TTY open: error %d\n",
480 -i);
484 read_lock(&tasklist_lock);
485 for_each_task(p) {
486 if ((tty->session > 0) && (p->session == tty->session) &&
487 p->leader) {
488 send_sig(SIGHUP,p,1);
489 send_sig(SIGCONT,p,1);
490 if (tty->pgrp > 0)
491 p->tty_old_pgrp = tty->pgrp;
493 if (p->tty == tty)
494 p->tty = NULL;
496 read_unlock(&tasklist_lock);
498 tty->flags = 0;
499 tty->session = 0;
500 tty->pgrp = -1;
501 tty->ctrl_status = 0;
503 * If one of the devices matches a console pointer, we
504 * cannot just call hangup() because that will cause
505 * tty->count and state->count to go out of sync.
506 * So we just call close() the right number of times.
508 if (cons_filp) {
509 if (tty->driver.close)
510 for (n = 0; n < closecount; n++)
511 tty->driver.close(tty, cons_filp);
512 } else if (tty->driver.hangup)
513 (tty->driver.hangup)(tty);
514 unlock_kernel();
517 void tty_hangup(struct tty_struct * tty)
519 #ifdef TTY_DEBUG_HANGUP
520 char buf[64];
522 printk("%s hangup...\n", tty_name(tty, buf));
523 #endif
524 queue_task(&tty->tq_hangup, &tq_scheduler);
527 void tty_vhangup(struct tty_struct * tty)
529 #ifdef TTY_DEBUG_HANGUP
530 char buf[64];
532 printk("%s vhangup...\n", tty_name(tty, buf));
533 #endif
534 do_tty_hangup((void *) tty);
537 int tty_hung_up_p(struct file * filp)
539 return (filp->f_op == &hung_up_tty_fops);
543 * This function is typically called only by the session leader, when
544 * it wants to disassociate itself from its controlling tty.
546 * It performs the following functions:
547 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
548 * (2) Clears the tty from being controlling the session
549 * (3) Clears the controlling tty for all processes in the
550 * session group.
552 * The argument on_exit is set to 1 if called when a process is
553 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
555 void disassociate_ctty(int on_exit)
557 struct tty_struct *tty = current->tty;
558 struct task_struct *p;
559 int tty_pgrp = -1;
561 if (tty) {
562 tty_pgrp = tty->pgrp;
563 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
564 tty_vhangup(tty);
565 } else {
566 if (current->tty_old_pgrp) {
567 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
568 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
570 return;
572 if (tty_pgrp > 0) {
573 kill_pg(tty_pgrp, SIGHUP, on_exit);
574 if (!on_exit)
575 kill_pg(tty_pgrp, SIGCONT, on_exit);
578 current->tty_old_pgrp = 0;
579 tty->session = 0;
580 tty->pgrp = -1;
582 read_lock(&tasklist_lock);
583 for_each_task(p)
584 if (p->session == current->session)
585 p->tty = NULL;
586 read_unlock(&tasklist_lock);
589 void wait_for_keypress(void)
591 struct console *c = console_drivers;
592 if (c) c->wait_key(c);
595 void stop_tty(struct tty_struct *tty)
597 if (tty->stopped)
598 return;
599 tty->stopped = 1;
600 if (tty->link && tty->link->packet) {
601 tty->ctrl_status &= ~TIOCPKT_START;
602 tty->ctrl_status |= TIOCPKT_STOP;
603 wake_up_interruptible(&tty->link->read_wait);
605 if (tty->driver.stop)
606 (tty->driver.stop)(tty);
609 void start_tty(struct tty_struct *tty)
611 if (!tty->stopped || tty->flow_stopped)
612 return;
613 tty->stopped = 0;
614 if (tty->link && tty->link->packet) {
615 tty->ctrl_status &= ~TIOCPKT_STOP;
616 tty->ctrl_status |= TIOCPKT_START;
617 wake_up_interruptible(&tty->link->read_wait);
619 if (tty->driver.start)
620 (tty->driver.start)(tty);
621 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
622 tty->ldisc.write_wakeup)
623 (tty->ldisc.write_wakeup)(tty);
624 wake_up_interruptible(&tty->write_wait);
627 static ssize_t tty_read(struct file * file, char * buf, size_t count,
628 loff_t *ppos)
630 int i;
631 struct tty_struct * tty;
632 struct inode *inode;
634 /* Can't seek (pread) on ttys. */
635 if (ppos != &file->f_pos)
636 return -ESPIPE;
638 tty = (struct tty_struct *)file->private_data;
639 inode = file->f_dentry->d_inode;
640 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
641 return -EIO;
642 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
643 return -EIO;
645 /* This check not only needs to be done before reading, but also
646 whenever read_chan() gets woken up after sleeping, so I've
647 moved it to there. This should only be done for the N_TTY
648 line discipline, anyway. Same goes for write_chan(). -- jlc. */
649 #if 0
650 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
651 (tty->pgrp > 0) &&
652 (current->tty == tty) &&
653 (tty->pgrp != current->pgrp))
654 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
655 return -EIO;
656 else {
657 (void) kill_pg(current->pgrp, SIGTTIN, 1);
658 return -ERESTARTSYS;
660 #endif
661 lock_kernel();
662 if (tty->ldisc.read)
663 i = (tty->ldisc.read)(tty,file,buf,count);
664 else
665 i = -EIO;
666 unlock_kernel();
667 if (i > 0)
668 inode->i_atime = CURRENT_TIME;
669 return i;
673 * Split writes up in sane blocksizes to avoid
674 * denial-of-service type attacks
676 static inline ssize_t do_tty_write(
677 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
678 struct tty_struct *tty,
679 struct file *file,
680 const unsigned char *buf,
681 size_t count)
683 ssize_t ret = 0, written = 0;
684 struct inode *inode = file->f_dentry->d_inode;
686 if (down_interruptible(&inode->i_sem)) {
687 return -ERESTARTSYS;
689 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
690 lock_kernel();
691 written = write(tty, file, buf, count);
692 unlock_kernel();
693 } else {
694 for (;;) {
695 unsigned long size = PAGE_SIZE*2;
696 if (size > count)
697 size = count;
698 lock_kernel();
699 ret = write(tty, file, buf, size);
700 unlock_kernel();
701 if (ret <= 0)
702 break;
703 written += ret;
704 buf += ret;
705 count -= ret;
706 if (!count)
707 break;
708 ret = -ERESTARTSYS;
709 if (signal_pending(current))
710 break;
711 if (current->need_resched)
712 schedule();
715 if (written) {
716 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
717 ret = written;
719 up(&inode->i_sem);
720 return ret;
724 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
725 loff_t *ppos)
727 int is_console;
728 struct tty_struct * tty;
729 struct inode *inode;
731 /* Can't seek (pwrite) on ttys. */
732 if (ppos != &file->f_pos)
733 return -ESPIPE;
736 * For now, we redirect writes from /dev/console as
737 * well as /dev/tty0.
739 inode = file->f_dentry->d_inode;
740 is_console = (inode->i_rdev == SYSCONS_DEV ||
741 inode->i_rdev == CONSOLE_DEV);
743 if (is_console && redirect)
744 tty = redirect;
745 else
746 tty = (struct tty_struct *)file->private_data;
747 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
748 return -EIO;
749 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
750 return -EIO;
751 #if 0
752 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
753 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
754 if (is_orphaned_pgrp(current->pgrp))
755 return -EIO;
756 if (!is_ignored(SIGTTOU)) {
757 (void) kill_pg(current->pgrp, SIGTTOU, 1);
758 return -ERESTARTSYS;
761 #endif
762 if (!tty->ldisc.write)
763 return -EIO;
764 return do_tty_write(tty->ldisc.write, tty, file,
765 (const unsigned char *)buf, count);
768 /* Semaphore to protect creating and releasing a tty */
769 static DECLARE_MUTEX(tty_sem);
771 static void down_tty_sem(int index)
773 down(&tty_sem);
776 static void up_tty_sem(int index)
778 up(&tty_sem);
781 static void release_mem(struct tty_struct *tty, int idx);
784 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
785 * failed open. The new code protects the open with a semaphore, so it's
786 * really quite straightforward. The semaphore locking can probably be
787 * relaxed for the (most common) case of reopening a tty.
789 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
791 struct tty_struct *tty, *o_tty;
792 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
793 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
794 struct tty_driver *driver;
795 int retval=0;
796 int idx;
798 driver = get_tty_driver(device);
799 if (!driver)
800 return -ENODEV;
802 idx = MINOR(device) - driver->minor_start;
805 * Check whether we need to acquire the tty semaphore to avoid
806 * race conditions. For now, play it safe.
808 down_tty_sem(idx);
810 /* check whether we're reopening an existing tty */
811 tty = driver->table[idx];
812 if (tty) goto fast_track;
815 * First time open is complex, especially for PTY devices.
816 * This code guarantees that either everything succeeds and the
817 * TTY is ready for operation, or else the table slots are vacated
818 * and the allocated memory released. (Except that the termios
819 * and locked termios may be retained.)
822 o_tty = NULL;
823 tp = o_tp = NULL;
824 ltp = o_ltp = NULL;
826 tty = (struct tty_struct*) get_zeroed_page(GFP_KERNEL);
827 if(!tty)
828 goto fail_no_mem;
829 initialize_tty_struct(tty);
830 tty->device = device;
831 tty->driver = *driver;
833 tp_loc = &driver->termios[idx];
834 if (!*tp_loc) {
835 tp = (struct termios *) kmalloc(sizeof(struct termios),
836 GFP_KERNEL);
837 if (!tp)
838 goto free_mem_out;
839 *tp = driver->init_termios;
842 ltp_loc = &driver->termios_locked[idx];
843 if (!*ltp_loc) {
844 ltp = (struct termios *) kmalloc(sizeof(struct termios),
845 GFP_KERNEL);
846 if (!ltp)
847 goto free_mem_out;
848 memset(ltp, 0, sizeof(struct termios));
851 if (driver->type == TTY_DRIVER_TYPE_PTY) {
852 o_tty = (struct tty_struct *) get_zeroed_page(GFP_KERNEL);
853 if (!o_tty)
854 goto free_mem_out;
855 initialize_tty_struct(o_tty);
856 o_tty->device = (kdev_t) MKDEV(driver->other->major,
857 driver->other->minor_start + idx);
858 o_tty->driver = *driver->other;
860 o_tp_loc = &driver->other->termios[idx];
861 if (!*o_tp_loc) {
862 o_tp = (struct termios *)
863 kmalloc(sizeof(struct termios), GFP_KERNEL);
864 if (!o_tp)
865 goto free_mem_out;
866 *o_tp = driver->other->init_termios;
869 o_ltp_loc = &driver->other->termios_locked[idx];
870 if (!*o_ltp_loc) {
871 o_ltp = (struct termios *)
872 kmalloc(sizeof(struct termios), GFP_KERNEL);
873 if (!o_ltp)
874 goto free_mem_out;
875 memset(o_ltp, 0, sizeof(struct termios));
879 * Everything allocated ... set up the o_tty structure.
881 driver->other->table[idx] = o_tty;
882 if (!*o_tp_loc)
883 *o_tp_loc = o_tp;
884 if (!*o_ltp_loc)
885 *o_ltp_loc = o_ltp;
886 o_tty->termios = *o_tp_loc;
887 o_tty->termios_locked = *o_ltp_loc;
888 (*driver->other->refcount)++;
889 if (driver->subtype == PTY_TYPE_MASTER)
890 o_tty->count++;
892 /* Establish the links in both directions */
893 tty->link = o_tty;
894 o_tty->link = tty;
898 * All structures have been allocated, so now we install them.
899 * Failures after this point use release_mem to clean up, so
900 * there's no need to null out the local pointers.
902 driver->table[idx] = tty;
904 if (!*tp_loc)
905 *tp_loc = tp;
906 if (!*ltp_loc)
907 *ltp_loc = ltp;
908 tty->termios = *tp_loc;
909 tty->termios_locked = *ltp_loc;
910 (*driver->refcount)++;
911 tty->count++;
914 * Structures all installed ... call the ldisc open routines.
915 * If we fail here just call release_mem to clean up. No need
916 * to decrement the use counts, as release_mem doesn't care.
918 if (tty->ldisc.open) {
919 retval = (tty->ldisc.open)(tty);
920 if (retval)
921 goto release_mem_out;
923 if (o_tty && o_tty->ldisc.open) {
924 retval = (o_tty->ldisc.open)(o_tty);
925 if (retval) {
926 if (tty->ldisc.close)
927 (tty->ldisc.close)(tty);
928 goto release_mem_out;
931 goto success;
934 * This fast open can be used if the tty is already open.
935 * No memory is allocated, and the only failures are from
936 * attempting to open a closing tty or attempting multiple
937 * opens on a pty master.
939 fast_track:
940 if (test_bit(TTY_CLOSING, &tty->flags)) {
941 retval = -EIO;
942 goto end_init;
944 if (driver->type == TTY_DRIVER_TYPE_PTY &&
945 driver->subtype == PTY_TYPE_MASTER) {
947 * special case for PTY masters: only one open permitted,
948 * and the slave side open count is incremented as well.
950 if (tty->count) {
951 retval = -EIO;
952 goto end_init;
954 tty->link->count++;
956 tty->count++;
957 tty->driver = *driver; /* N.B. why do this every time?? */
959 success:
960 *ret_tty = tty;
962 /* All paths come through here to release the semaphore */
963 end_init:
964 up_tty_sem(idx);
965 return retval;
967 /* Release locally allocated memory ... nothing placed in slots */
968 free_mem_out:
969 if (o_tp)
970 kfree_s(o_tp, sizeof(struct termios));
971 if (o_tty)
972 free_page((unsigned long) o_tty);
973 if (ltp)
974 kfree_s(ltp, sizeof(struct termios));
975 if (tp)
976 kfree_s(tp, sizeof(struct termios));
977 free_page((unsigned long) tty);
979 fail_no_mem:
980 retval = -ENOMEM;
981 goto end_init;
983 /* call the tty release_mem routine to clean out this slot */
984 release_mem_out:
985 printk("init_dev: ldisc open failed, clearing slot %d\n", idx);
986 release_mem(tty, idx);
987 goto end_init;
991 * Releases memory associated with a tty structure, and clears out the
992 * driver table slots.
994 static void release_mem(struct tty_struct *tty, int idx)
996 struct tty_struct *o_tty;
997 struct termios *tp;
999 if ((o_tty = tty->link) != NULL) {
1000 o_tty->driver.table[idx] = NULL;
1001 if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1002 tp = o_tty->driver.termios[idx];
1003 o_tty->driver.termios[idx] = NULL;
1004 kfree_s(tp, sizeof(struct termios));
1006 o_tty->magic = 0;
1007 (*o_tty->driver.refcount)--;
1008 free_page((unsigned long) o_tty);
1011 tty->driver.table[idx] = NULL;
1012 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1013 tp = tty->driver.termios[idx];
1014 tty->driver.termios[idx] = NULL;
1015 kfree_s(tp, sizeof(struct termios));
1017 tty->magic = 0;
1018 (*tty->driver.refcount)--;
1019 free_page((unsigned long) tty);
1023 * Even releasing the tty structures is a tricky business.. We have
1024 * to be very careful that the structures are all released at the
1025 * same time, as interrupts might otherwise get the wrong pointers.
1027 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1028 * lead to double frees or releasing memory still in use.
1030 static void release_dev(struct file * filp)
1032 struct tty_struct *tty, *o_tty;
1033 int pty_master, tty_closing, o_tty_closing, do_sleep;
1034 int idx;
1035 char buf[64];
1037 tty = (struct tty_struct *)filp->private_data;
1038 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1039 return;
1041 check_tty_count(tty, "release_dev");
1043 tty_fasync(-1, filp, 0);
1045 idx = MINOR(tty->device) - tty->driver.minor_start;
1046 pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1047 tty->driver.subtype == PTY_TYPE_MASTER);
1048 o_tty = tty->link;
1050 #ifdef TTY_PARANOIA_CHECK
1051 if (idx < 0 || idx >= tty->driver.num) {
1052 printk("release_dev: bad idx when trying to free (%s)\n",
1053 kdevname(tty->device));
1054 return;
1056 if (tty != tty->driver.table[idx]) {
1057 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1058 idx, kdevname(tty->device));
1059 return;
1061 if (tty->termios != tty->driver.termios[idx]) {
1062 printk("release_dev: driver.termios[%d] not termios "
1063 "for (%s)\n",
1064 idx, kdevname(tty->device));
1065 return;
1067 if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1068 printk("release_dev: driver.termios_locked[%d] not "
1069 "termios_locked for (%s)\n",
1070 idx, kdevname(tty->device));
1071 return;
1073 #endif
1075 #ifdef TTY_DEBUG_HANGUP
1076 printk("release_dev of %s (tty count=%d)...", tty_name(tty, buf),
1077 tty->count);
1078 #endif
1080 #ifdef TTY_PARANOIA_CHECK
1081 if (tty->driver.other) {
1082 if (o_tty != tty->driver.other->table[idx]) {
1083 printk("release_dev: other->table[%d] not o_tty for ("
1084 "%s)\n",
1085 idx, kdevname(tty->device));
1086 return;
1088 if (o_tty->termios != tty->driver.other->termios[idx]) {
1089 printk("release_dev: other->termios[%d] not o_termios "
1090 "for (%s)\n",
1091 idx, kdevname(tty->device));
1092 return;
1094 if (o_tty->termios_locked !=
1095 tty->driver.other->termios_locked[idx]) {
1096 printk("release_dev: other->termios_locked[%d] not "
1097 "o_termios_locked for (%s)\n",
1098 idx, kdevname(tty->device));
1099 return;
1101 if (o_tty->link != tty) {
1102 printk("release_dev: bad pty pointers\n");
1103 return;
1106 #endif
1108 if (tty->driver.close)
1109 tty->driver.close(tty, filp);
1112 * Sanity check: if tty->count is going to zero, there shouldn't be
1113 * any waiters on tty->read_wait or tty->write_wait. We test the
1114 * wait queues and kick everyone out _before_ actually starting to
1115 * close. This ensures that we won't block while releasing the tty
1116 * structure.
1118 * The test for the o_tty closing is necessary, since the master and
1119 * slave sides may close in any order. If the slave side closes out
1120 * first, its count will be one, since the master side holds an open.
1121 * Thus this test wouldn't be triggered at the time the slave closes,
1122 * so we do it now.
1124 * Note that it's possible for the tty to be opened again while we're
1125 * flushing out waiters. By recalculating the closing flags before
1126 * each iteration we avoid any problems.
1128 while (1) {
1129 tty_closing = tty->count <= 1;
1130 o_tty_closing = o_tty &&
1131 (o_tty->count <= (pty_master ? 1 : 0));
1132 do_sleep = 0;
1134 if (tty_closing) {
1135 if (waitqueue_active(&tty->read_wait)) {
1136 wake_up(&tty->read_wait);
1137 do_sleep++;
1139 if (waitqueue_active(&tty->write_wait)) {
1140 wake_up(&tty->write_wait);
1141 do_sleep++;
1144 if (o_tty_closing) {
1145 if (waitqueue_active(&o_tty->read_wait)) {
1146 wake_up(&o_tty->read_wait);
1147 do_sleep++;
1149 if (waitqueue_active(&o_tty->write_wait)) {
1150 wake_up(&o_tty->write_wait);
1151 do_sleep++;
1154 if (!do_sleep)
1155 break;
1157 printk("release_dev: %s: read/write wait queue active!\n",
1158 tty_name(tty, buf));
1159 schedule();
1163 * The closing flags are now consistent with the open counts on
1164 * both sides, and we've completed the last operation that could
1165 * block, so it's safe to proceed with closing.
1167 if (pty_master) {
1168 if (--o_tty->count < 0) {
1169 printk("release_dev: bad pty slave count (%d) for %s\n",
1170 o_tty->count, tty_name(o_tty, buf));
1171 o_tty->count = 0;
1174 if (--tty->count < 0) {
1175 printk("release_dev: bad tty->count (%d) for %s\n",
1176 tty->count, tty_name(tty, buf));
1177 tty->count = 0;
1181 * We've decremented tty->count, so we should zero out
1182 * filp->private_data, to break the link between the tty and
1183 * the file descriptor. Otherwise if filp_close() blocks before
1184 * the the file descriptor is removed from the inuse_filp
1185 * list, check_tty_count() could observe a discrepancy and
1186 * printk a warning message to the user.
1188 filp->private_data = 0;
1191 * Perform some housekeeping before deciding whether to return.
1193 * Set the TTY_CLOSING flag if this was the last open. In the
1194 * case of a pty we may have to wait around for the other side
1195 * to close, and TTY_CLOSING makes sure we can't be reopened.
1197 if(tty_closing)
1198 set_bit(TTY_CLOSING, &tty->flags);
1199 if(o_tty_closing)
1200 set_bit(TTY_CLOSING, &o_tty->flags);
1203 * If _either_ side is closing, make sure there aren't any
1204 * processes that still think tty or o_tty is their controlling
1205 * tty. Also, clear redirect if it points to either tty.
1207 if (tty_closing || o_tty_closing) {
1208 struct task_struct *p;
1210 read_lock(&tasklist_lock);
1211 for_each_task(p) {
1212 if (p->tty == tty || (o_tty && p->tty == o_tty))
1213 p->tty = NULL;
1215 read_unlock(&tasklist_lock);
1217 if (redirect == tty || (o_tty && redirect == o_tty))
1218 redirect = NULL;
1221 /* check whether both sides are closing ... */
1222 if (!tty_closing || (o_tty && !o_tty_closing))
1223 return;
1225 #ifdef TTY_DEBUG_HANGUP
1226 printk("freeing tty structure...");
1227 #endif
1230 * Shutdown the current line discipline, and reset it to N_TTY.
1231 * N.B. why reset ldisc when we're releasing the memory??
1233 if (tty->ldisc.close)
1234 (tty->ldisc.close)(tty);
1235 tty->ldisc = ldiscs[N_TTY];
1236 tty->termios->c_line = N_TTY;
1237 if (o_tty) {
1238 if (o_tty->ldisc.close)
1239 (o_tty->ldisc.close)(o_tty);
1240 o_tty->ldisc = ldiscs[N_TTY];
1244 * Make sure that the tty's task queue isn't activated.
1246 run_task_queue(&tq_timer);
1247 run_task_queue(&tq_scheduler);
1250 * The release_mem function takes care of the details of clearing
1251 * the slots and preserving the termios structure.
1253 release_mem(tty, idx);
1257 * tty_open and tty_release keep up the tty count that contains the
1258 * number of opens done on a tty. We cannot use the inode-count, as
1259 * different inodes might point to the same tty.
1261 * Open-counting is needed for pty masters, as well as for keeping
1262 * track of serial lines: DTR is dropped when the last close happens.
1263 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1265 * The termios state of a pty is reset on first open so that
1266 * settings don't persist across reuse.
1268 static int tty_open(struct inode * inode, struct file * filp)
1270 struct tty_struct *tty;
1271 int noctty, retval;
1272 kdev_t device;
1273 unsigned short saved_flags;
1274 char buf[64];
1276 saved_flags = filp->f_flags;
1277 retry_open:
1278 noctty = filp->f_flags & O_NOCTTY;
1279 device = inode->i_rdev;
1280 if (device == TTY_DEV) {
1281 if (!current->tty)
1282 return -ENXIO;
1283 device = current->tty->device;
1284 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1285 /* noctty = 1; */
1287 #ifdef CONFIG_VT
1288 if (device == CONSOLE_DEV) {
1289 extern int fg_console;
1290 device = MKDEV(TTY_MAJOR, fg_console + 1);
1291 noctty = 1;
1293 #endif
1294 if (device == SYSCONS_DEV) {
1295 struct console *c = console_drivers;
1296 while(c && !c->device)
1297 c = c->next;
1298 if (!c)
1299 return -ENODEV;
1300 device = c->device(c);
1301 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1302 noctty = 1;
1305 if (device == PTMX_DEV) {
1306 #ifdef CONFIG_UNIX98_PTYS
1308 /* find a free pty. */
1309 int major, minor;
1310 struct tty_driver *driver;
1312 /* find a device that is not in use. */
1313 retval = -1;
1314 for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1315 driver = &ptm_driver[major];
1316 for (minor = driver->minor_start ;
1317 minor < driver->minor_start + driver->num ;
1318 minor++) {
1319 device = MKDEV(driver->major, minor);
1320 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1323 return -EIO; /* no free ptys */
1324 ptmx_found:
1325 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1326 minor -= driver->minor_start;
1327 devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1328 tty_register_devfs(&pts_driver[major], DEVFS_FL_NO_PERSISTENCE,
1329 pts_driver[major].minor_start + minor);
1330 noctty = 1;
1331 goto init_dev_done;
1333 #else /* CONFIG_UNIX_98_PTYS */
1335 return -ENODEV;
1337 #endif /* CONFIG_UNIX_98_PTYS */
1340 retval = init_dev(device, &tty);
1341 if (retval)
1342 return retval;
1344 #ifdef CONFIG_UNIX98_PTYS
1345 init_dev_done:
1346 #endif
1347 filp->private_data = tty;
1348 file_move(filp, &tty->tty_files);
1349 check_tty_count(tty, "tty_open");
1350 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1351 tty->driver.subtype == PTY_TYPE_MASTER)
1352 noctty = 1;
1353 #ifdef TTY_DEBUG_HANGUP
1354 printk("opening %s...", tty_name(tty, buf));
1355 #endif
1356 if (tty->driver.open)
1357 retval = tty->driver.open(tty, filp);
1358 else
1359 retval = -ENODEV;
1360 filp->f_flags = saved_flags;
1362 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1363 retval = -EBUSY;
1365 if (retval) {
1366 #ifdef TTY_DEBUG_HANGUP
1367 printk("error %d in opening %s...", retval,
1368 tty_name(tty, buf));
1369 #endif
1371 release_dev(filp);
1372 if (retval != -ERESTARTSYS)
1373 return retval;
1374 if (signal_pending(current))
1375 return retval;
1376 schedule();
1378 * Need to reset f_op in case a hangup happened.
1380 filp->f_op = &tty_fops;
1381 goto retry_open;
1383 if (!noctty &&
1384 current->leader &&
1385 !current->tty &&
1386 tty->session == 0) {
1387 task_lock(current);
1388 current->tty = tty;
1389 task_unlock(current);
1390 current->tty_old_pgrp = 0;
1391 tty->session = current->session;
1392 tty->pgrp = current->pgrp;
1394 if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1395 (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1396 (tty->count == 1)) {
1397 static int nr_warns = 0;
1398 if (nr_warns < 5) {
1399 printk(KERN_WARNING "tty_io.c: "
1400 "process %d (%s) used obsolete /dev/%s - "
1401 "update software to use /dev/ttyS%d\n",
1402 current->pid, current->comm,
1403 tty_name(tty, buf), TTY_NUMBER(tty));
1404 nr_warns++;
1407 return 0;
1410 static int tty_release(struct inode * inode, struct file * filp)
1412 release_dev(filp);
1413 return 0;
1416 /* No kernel lock held - fine */
1417 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1419 struct tty_struct * tty;
1421 tty = (struct tty_struct *)filp->private_data;
1422 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1423 return 0;
1425 if (tty->ldisc.poll)
1426 return (tty->ldisc.poll)(tty, filp, wait);
1427 return 0;
1430 static int tty_fasync(int fd, struct file * filp, int on)
1432 struct tty_struct * tty;
1433 int retval;
1435 tty = (struct tty_struct *)filp->private_data;
1436 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1437 return 0;
1439 retval = fasync_helper(fd, filp, on, &tty->fasync);
1440 if (retval <= 0)
1441 return retval;
1443 if (on) {
1444 if (!waitqueue_active(&tty->read_wait))
1445 tty->minimum_to_wake = 1;
1446 if (filp->f_owner.pid == 0) {
1447 filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1448 filp->f_owner.uid = current->uid;
1449 filp->f_owner.euid = current->euid;
1451 } else {
1452 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1453 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1455 return 0;
1458 static int tiocsti(struct tty_struct *tty, char * arg)
1460 char ch, mbz = 0;
1462 if ((current->tty != tty) && !suser())
1463 return -EPERM;
1464 if (get_user(ch, arg))
1465 return -EFAULT;
1466 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1467 return 0;
1470 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1472 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1473 return -EFAULT;
1474 return 0;
1477 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1478 struct winsize * arg)
1480 struct winsize tmp_ws;
1482 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1483 return -EFAULT;
1484 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1485 return 0;
1486 if (tty->pgrp > 0)
1487 kill_pg(tty->pgrp, SIGWINCH, 1);
1488 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1489 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1490 tty->winsize = tmp_ws;
1491 real_tty->winsize = tmp_ws;
1492 return 0;
1495 static int tioccons(struct inode *inode,
1496 struct tty_struct *tty, struct tty_struct *real_tty)
1498 if (inode->i_rdev == SYSCONS_DEV ||
1499 inode->i_rdev == CONSOLE_DEV) {
1500 if (!suser())
1501 return -EPERM;
1502 redirect = NULL;
1503 return 0;
1505 if (redirect)
1506 return -EBUSY;
1507 redirect = real_tty;
1508 return 0;
1512 static int fionbio(struct file *file, int *arg)
1514 int nonblock;
1516 if (get_user(nonblock, arg))
1517 return -EFAULT;
1519 if (nonblock)
1520 file->f_flags |= O_NONBLOCK;
1521 else
1522 file->f_flags &= ~O_NONBLOCK;
1523 return 0;
1526 static int tiocsctty(struct tty_struct *tty, int arg)
1528 if (current->leader &&
1529 (current->session == tty->session))
1530 return 0;
1532 * The process must be a session leader and
1533 * not have a controlling tty already.
1535 if (!current->leader || current->tty)
1536 return -EPERM;
1537 if (tty->session > 0) {
1539 * This tty is already the controlling
1540 * tty for another session group!
1542 if ((arg == 1) && suser()) {
1544 * Steal it away
1546 struct task_struct *p;
1548 read_lock(&tasklist_lock);
1549 for_each_task(p)
1550 if (p->tty == tty)
1551 p->tty = NULL;
1552 read_unlock(&tasklist_lock);
1553 } else
1554 return -EPERM;
1556 task_lock(current);
1557 current->tty = tty;
1558 task_unlock(current);
1559 current->tty_old_pgrp = 0;
1560 tty->session = current->session;
1561 tty->pgrp = current->pgrp;
1562 return 0;
1565 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1568 * (tty == real_tty) is a cheap way of
1569 * testing if the tty is NOT a master pty.
1571 if (tty == real_tty && current->tty != real_tty)
1572 return -ENOTTY;
1573 return put_user(real_tty->pgrp, arg);
1576 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1578 pid_t pgrp;
1579 int retval = tty_check_change(real_tty);
1581 if (retval == -EIO)
1582 return -ENOTTY;
1583 if (retval)
1584 return retval;
1585 if (!current->tty ||
1586 (current->tty != real_tty) ||
1587 (real_tty->session != current->session))
1588 return -ENOTTY;
1589 get_user(pgrp, (pid_t *) arg);
1590 if (pgrp < 0)
1591 return -EINVAL;
1592 if (session_of_pgrp(pgrp) != current->session)
1593 return -EPERM;
1594 real_tty->pgrp = pgrp;
1595 return 0;
1598 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1601 * (tty == real_tty) is a cheap way of
1602 * testing if the tty is NOT a master pty.
1604 if (tty == real_tty && current->tty != real_tty)
1605 return -ENOTTY;
1606 if (real_tty->session <= 0)
1607 return -ENOTTY;
1608 return put_user(real_tty->session, arg);
1611 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1613 if (copy_to_user(arg, tty, sizeof(*arg)))
1614 return -EFAULT;
1615 return 0;
1618 static int tiocsetd(struct tty_struct *tty, int *arg)
1620 int retval, ldisc;
1622 retval = get_user(ldisc, arg);
1623 if (retval)
1624 return retval;
1625 return tty_set_ldisc(tty, ldisc);
1628 static int send_break(struct tty_struct *tty, int duration)
1630 set_current_state(TASK_INTERRUPTIBLE);
1632 tty->driver.break_ctl(tty, -1);
1633 if (!signal_pending(current))
1634 schedule_timeout(duration);
1635 tty->driver.break_ctl(tty, 0);
1636 if (signal_pending(current))
1637 return -EINTR;
1638 return 0;
1642 * Split this up, as gcc can choke on it otherwise..
1644 int tty_ioctl(struct inode * inode, struct file * file,
1645 unsigned int cmd, unsigned long arg)
1647 struct tty_struct *tty, *real_tty;
1648 int retval;
1650 tty = (struct tty_struct *)file->private_data;
1651 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1652 return -EINVAL;
1654 real_tty = tty;
1655 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1656 tty->driver.subtype == PTY_TYPE_MASTER)
1657 real_tty = tty->link;
1660 * Break handling by driver
1662 if (!tty->driver.break_ctl) {
1663 switch(cmd) {
1664 case TIOCSBRK:
1665 case TIOCCBRK:
1666 if (tty->driver.ioctl)
1667 return tty->driver.ioctl(tty, file, cmd, arg);
1668 return -EINVAL;
1670 /* These two ioctl's always return success; even if */
1671 /* the driver doesn't support them. */
1672 case TCSBRK:
1673 case TCSBRKP:
1674 if (!tty->driver.ioctl)
1675 return 0;
1676 retval = tty->driver.ioctl(tty, file, cmd, arg);
1677 if (retval == -ENOIOCTLCMD)
1678 retval = 0;
1679 return retval;
1684 * Factor out some common prep work
1686 switch (cmd) {
1687 case TIOCSETD:
1688 case TIOCSBRK:
1689 case TIOCCBRK:
1690 case TCSBRK:
1691 case TCSBRKP:
1692 retval = tty_check_change(tty);
1693 if (retval)
1694 return retval;
1695 if (cmd != TIOCCBRK) {
1696 tty_wait_until_sent(tty, 0);
1697 if (signal_pending(current))
1698 return -EINTR;
1700 break;
1703 switch (cmd) {
1704 case TIOCSTI:
1705 return tiocsti(tty, (char *)arg);
1706 case TIOCGWINSZ:
1707 return tiocgwinsz(tty, (struct winsize *) arg);
1708 case TIOCSWINSZ:
1709 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1710 case TIOCCONS:
1711 return tioccons(inode, tty, real_tty);
1712 case FIONBIO:
1713 return fionbio(file, (int *) arg);
1714 case TIOCEXCL:
1715 set_bit(TTY_EXCLUSIVE, &tty->flags);
1716 return 0;
1717 case TIOCNXCL:
1718 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1719 return 0;
1720 case TIOCNOTTY:
1721 if (current->tty != tty)
1722 return -ENOTTY;
1723 if (current->leader)
1724 disassociate_ctty(0);
1725 task_lock(current);
1726 current->tty = NULL;
1727 task_unlock(current);
1728 return 0;
1729 case TIOCSCTTY:
1730 return tiocsctty(tty, arg);
1731 case TIOCGPGRP:
1732 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1733 case TIOCSPGRP:
1734 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1735 case TIOCGSID:
1736 return tiocgsid(tty, real_tty, (pid_t *) arg);
1737 case TIOCGETD:
1738 return put_user(tty->ldisc.num, (int *) arg);
1739 case TIOCSETD:
1740 return tiocsetd(tty, (int *) arg);
1741 #ifdef CONFIG_VT
1742 case TIOCLINUX:
1743 return tioclinux(tty, arg);
1744 #endif
1745 case TIOCTTYGSTRUCT:
1746 return tiocttygstruct(tty, (struct tty_struct *) arg);
1749 * Break handling
1751 case TIOCSBRK: /* Turn break on, unconditionally */
1752 tty->driver.break_ctl(tty, -1);
1753 return 0;
1755 case TIOCCBRK: /* Turn break off, unconditionally */
1756 tty->driver.break_ctl(tty, 0);
1757 return 0;
1758 case TCSBRK: /* SVID version: non-zero arg --> no break */
1760 * XXX is the above comment correct, or the
1761 * code below correct? Is this ioctl used at
1762 * all by anyone?
1764 if (!arg)
1765 return send_break(tty, HZ/4);
1766 return 0;
1767 case TCSBRKP: /* support for POSIX tcsendbreak() */
1768 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1770 if (tty->driver.ioctl) {
1771 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1772 if (retval != -ENOIOCTLCMD)
1773 return retval;
1775 if (tty->ldisc.ioctl) {
1776 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1777 if (retval != -ENOIOCTLCMD)
1778 return retval;
1780 return -EINVAL;
1785 * This implements the "Secure Attention Key" --- the idea is to
1786 * prevent trojan horses by killing all processes associated with this
1787 * tty when the user hits the "Secure Attention Key". Required for
1788 * super-paranoid applications --- see the Orange Book for more details.
1790 * This code could be nicer; ideally it should send a HUP, wait a few
1791 * seconds, then send a INT, and then a KILL signal. But you then
1792 * have to coordinate with the init process, since all processes associated
1793 * with the current tty must be dead before the new getty is allowed
1794 * to spawn.
1796 * Now, if it would be correct ;-/ The current code has a nasty hole -
1797 * it doesn't catch files in flight. We may send the descriptor to ourselves
1798 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1800 void do_SAK( struct tty_struct *tty)
1802 #ifdef TTY_SOFT_SAK
1803 tty_hangup(tty);
1804 #else
1805 struct task_struct *p;
1806 int session;
1807 int i;
1808 struct file *filp;
1810 if (!tty)
1811 return;
1812 session = tty->session;
1813 if (tty->ldisc.flush_buffer)
1814 tty->ldisc.flush_buffer(tty);
1815 if (tty->driver.flush_buffer)
1816 tty->driver.flush_buffer(tty);
1817 read_lock(&tasklist_lock);
1818 for_each_task(p) {
1819 if ((p->tty == tty) ||
1820 ((session > 0) && (p->session == session)))
1821 send_sig(SIGKILL, p, 1);
1822 else if (p->files) {
1823 read_lock(&p->files->file_lock);
1824 /* FIXME: p->files could change */
1825 for (i=0; i < p->files->max_fds; i++) {
1826 filp = fcheck_files(p->files, i);
1827 if (filp && (filp->f_op == &tty_fops) &&
1828 (filp->private_data == tty)) {
1829 send_sig(SIGKILL, p, 1);
1830 break;
1833 read_unlock(&p->files->file_lock);
1836 read_unlock(&tasklist_lock);
1837 #endif
1841 * This routine is called out of the software interrupt to flush data
1842 * from the flip buffer to the line discipline.
1844 static void flush_to_ldisc(void *private_)
1846 struct tty_struct *tty = (struct tty_struct *) private_;
1847 unsigned char *cp;
1848 char *fp;
1849 int count;
1850 unsigned long flags;
1852 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1853 queue_task(&tty->flip.tqueue, &tq_timer);
1854 return;
1856 if (tty->flip.buf_num) {
1857 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1858 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1859 tty->flip.buf_num = 0;
1861 save_flags(flags); cli();
1862 tty->flip.char_buf_ptr = tty->flip.char_buf;
1863 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1864 } else {
1865 cp = tty->flip.char_buf;
1866 fp = tty->flip.flag_buf;
1867 tty->flip.buf_num = 1;
1869 save_flags(flags); cli();
1870 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1871 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1873 count = tty->flip.count;
1874 tty->flip.count = 0;
1875 restore_flags(flags);
1877 tty->ldisc.receive_buf(tty, cp, fp, count);
1881 * Routine which returns the baud rate of the tty
1883 * Note that the baud_table needs to be kept in sync with the
1884 * include/asm/termbits.h file.
1886 static int baud_table[] = {
1887 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1888 9600, 19200, 38400, 57600, 115200, 230400, 460800,
1889 #ifdef __sparc__
1890 76800, 153600, 307200, 614400, 921600
1891 #else
1892 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1893 2500000, 3000000, 3500000, 4000000
1894 #endif
1897 static int n_baud_table = sizeof(baud_table)/sizeof(int);
1899 int tty_get_baud_rate(struct tty_struct *tty)
1901 unsigned int cflag, i;
1903 cflag = tty->termios->c_cflag;
1905 i = cflag & CBAUD;
1906 if (i & CBAUDEX) {
1907 i &= ~CBAUDEX;
1908 if (i < 1 || i+15 >= n_baud_table)
1909 tty->termios->c_cflag &= ~CBAUDEX;
1910 else
1911 i += 15;
1913 if (i==15 && tty->alt_speed) {
1914 if (!tty->warned) {
1915 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1916 tty->warned = 1;
1918 return(tty->alt_speed);
1921 return baud_table[i];
1924 void tty_flip_buffer_push(struct tty_struct *tty)
1926 if (tty->low_latency)
1927 flush_to_ldisc((void *) tty);
1928 else
1929 queue_task(&tty->flip.tqueue, &tq_timer);
1933 * This subroutine initializes a tty structure.
1935 static void initialize_tty_struct(struct tty_struct *tty)
1937 memset(tty, 0, sizeof(struct tty_struct));
1938 tty->magic = TTY_MAGIC;
1939 tty->ldisc = ldiscs[N_TTY];
1940 tty->pgrp = -1;
1941 tty->flip.char_buf_ptr = tty->flip.char_buf;
1942 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1943 tty->flip.tqueue.routine = flush_to_ldisc;
1944 tty->flip.tqueue.data = tty;
1945 init_MUTEX(&tty->flip.pty_sem);
1946 init_waitqueue_head(&tty->write_wait);
1947 init_waitqueue_head(&tty->read_wait);
1948 tty->tq_hangup.routine = do_tty_hangup;
1949 tty->tq_hangup.data = tty;
1950 sema_init(&tty->atomic_read, 1);
1951 spin_lock_init(&tty->read_lock);
1952 INIT_LIST_HEAD(&tty->tty_files);
1956 * The default put_char routine if the driver did not define one.
1958 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1960 tty->driver.write(tty, 0, &ch, 1);
1964 * Register a tty device described by <driver>, with minor number <minor>.
1966 void tty_register_devfs (struct tty_driver *driver, unsigned int flags,
1967 unsigned int minor)
1969 #ifdef CONFIG_DEVFS_FS
1970 umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
1971 uid_t uid = 0;
1972 gid_t gid = 0;
1973 struct tty_struct tty;
1974 char buf[32];
1976 tty.driver = *driver;
1977 tty.device = MKDEV (driver->major, minor);
1978 switch (tty.device) {
1979 case TTY_DEV:
1980 case PTMX_DEV:
1981 mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1982 break;
1983 default:
1984 if (driver->major == PTY_MASTER_MAJOR)
1985 flags |= DEVFS_FL_AUTO_OWNER;
1986 break;
1988 if ( (minor < driver->minor_start) ||
1989 (minor >= driver->minor_start + driver->num) ) {
1990 printk(KERN_ERR "Attempt to register invalid minor number "
1991 "with devfs (%d:%d).\n", (int)driver->major,(int)minor);
1992 return;
1994 # ifdef CONFIG_UNIX98_PTYS
1995 if ( (driver->major >= UNIX98_PTY_SLAVE_MAJOR) &&
1996 (driver->major < UNIX98_PTY_SLAVE_MAJOR + UNIX98_NR_MAJORS) ) {
1997 uid = current->uid;
1998 gid = current->gid;
2000 # endif
2001 devfs_register (NULL, tty_name (&tty, buf), 0,flags | DEVFS_FL_DEFAULT,
2002 driver->major, minor, mode, uid, gid,
2003 &tty_fops, NULL);
2004 #endif /* CONFIG_DEVFS_FS */
2007 void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2009 #ifdef CONFIG_DEVFS_FS
2010 void * handle;
2011 struct tty_struct tty;
2012 char buf[32];
2014 tty.driver = *driver;
2015 tty.device = MKDEV(driver->major, minor);
2017 handle = devfs_find_handle (NULL, tty_name (&tty, buf), 0,
2018 driver->major, minor,
2019 DEVFS_SPECIAL_CHR, 0);
2020 devfs_unregister (handle);
2021 #endif /* CONFIG_DEVFS_FS */
2024 EXPORT_SYMBOL(tty_register_devfs);
2025 EXPORT_SYMBOL(tty_unregister_devfs);
2028 * Called by a tty driver to register itself.
2030 int tty_register_driver(struct tty_driver *driver)
2032 int error;
2033 int i;
2035 if (driver->flags & TTY_DRIVER_INSTALLED)
2036 return 0;
2038 error = devfs_register_chrdev(driver->major, driver->name, &tty_fops);
2039 if (error < 0)
2040 return error;
2041 else if(driver->major == 0)
2042 driver->major = error;
2044 if (!driver->put_char)
2045 driver->put_char = tty_default_put_char;
2047 driver->prev = 0;
2048 driver->next = tty_drivers;
2049 if (tty_drivers) tty_drivers->prev = driver;
2050 tty_drivers = driver;
2052 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2053 for(i = 0; i < driver->num; i++)
2054 tty_register_devfs(driver, 0, driver->minor_start + i);
2056 proc_tty_register_driver(driver);
2057 return error;
2061 * Called by a tty driver to unregister itself.
2063 int tty_unregister_driver(struct tty_driver *driver)
2065 int retval;
2066 struct tty_driver *p;
2067 int i, found = 0;
2068 struct termios *tp;
2069 const char *othername = NULL;
2071 if (*driver->refcount)
2072 return -EBUSY;
2074 for (p = tty_drivers; p; p = p->next) {
2075 if (p == driver)
2076 found++;
2077 else if (p->major == driver->major)
2078 othername = p->name;
2081 if (!found)
2082 return -ENOENT;
2084 if (othername == NULL) {
2085 retval = devfs_unregister_chrdev(driver->major, driver->name);
2086 if (retval)
2087 return retval;
2088 } else
2089 devfs_register_chrdev(driver->major, othername, &tty_fops);
2091 if (driver->prev)
2092 driver->prev->next = driver->next;
2093 else
2094 tty_drivers = driver->next;
2096 if (driver->next)
2097 driver->next->prev = driver->prev;
2100 * Free the termios and termios_locked structures because
2101 * we don't want to get memory leaks when modular tty
2102 * drivers are removed from the kernel.
2104 for (i = 0; i < driver->num; i++) {
2105 tp = driver->termios[i];
2106 if (tp) {
2107 driver->termios[i] = NULL;
2108 kfree_s(tp, sizeof(struct termios));
2110 tp = driver->termios_locked[i];
2111 if (tp) {
2112 driver->termios_locked[i] = NULL;
2113 kfree_s(tp, sizeof(struct termios));
2115 tty_unregister_devfs(driver, driver->minor_start + i);
2117 proc_tty_unregister_driver(driver);
2118 return 0;
2123 * Initialize the console device. This is called *early*, so
2124 * we can't necessarily depend on lots of kernel help here.
2125 * Just do some early initializations, and do the complex setup
2126 * later.
2128 void __init console_init(void)
2130 /* Setup the default TTY line discipline. */
2131 memset(ldiscs, 0, sizeof(ldiscs));
2132 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2135 * Set up the standard termios. Individual tty drivers may
2136 * deviate from this; this is used as a template.
2138 memset(&tty_std_termios, 0, sizeof(struct termios));
2139 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2140 tty_std_termios.c_iflag = ICRNL | IXON;
2141 tty_std_termios.c_oflag = OPOST | ONLCR;
2142 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2143 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2144 ECHOCTL | ECHOKE | IEXTEN;
2147 * set up the console device so that later boot sequences can
2148 * inform about problems etc..
2150 #ifdef CONFIG_VT
2151 con_init();
2152 #endif
2153 #ifdef CONFIG_SERIAL_CONSOLE
2154 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2155 console_8xx_init();
2156 #elif defined(CONFIG_SERIAL)
2157 serial_console_init();
2158 #endif /* CONFIG_8xx */
2159 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2160 vme_scc_console_init();
2161 #endif
2162 #if defined(CONFIG_SERIAL167)
2163 serial167_console_init();
2164 #endif
2165 #endif
2166 #ifdef CONFIG_3215
2167 con3215_init();
2168 #endif
2169 #ifdef CONFIG_HWC
2170 hwc_console_init();
2171 #endif
2174 static struct tty_driver dev_tty_driver, dev_syscons_driver;
2175 #ifdef CONFIG_UNIX98_PTYS
2176 static struct tty_driver dev_ptmx_driver;
2177 #endif
2178 #ifdef CONFIG_VT
2179 static struct tty_driver dev_console_driver;
2180 #endif
2183 * Ok, now we can initialize the rest of the tty devices and can count
2184 * on memory allocations, interrupts etc..
2186 void __init tty_init(void)
2188 if (sizeof(struct tty_struct) > PAGE_SIZE)
2189 panic("size of tty structure > PAGE_SIZE!");
2192 * dev_tty_driver and dev_console_driver are actually magic
2193 * devices which get redirected at open time. Nevertheless,
2194 * we register them so that register_chrdev is called
2195 * appropriately.
2197 memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2198 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2199 dev_tty_driver.driver_name = "/dev/tty";
2200 dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2201 dev_tty_driver.name_base = 0;
2202 dev_tty_driver.major = TTYAUX_MAJOR;
2203 dev_tty_driver.minor_start = 0;
2204 dev_tty_driver.num = 1;
2205 dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2206 dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2208 if (tty_register_driver(&dev_tty_driver))
2209 panic("Couldn't register /dev/tty driver\n");
2211 dev_syscons_driver = dev_tty_driver;
2212 dev_syscons_driver.driver_name = "/dev/console";
2213 dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2214 dev_syscons_driver.major = TTYAUX_MAJOR;
2215 dev_syscons_driver.minor_start = 1;
2216 dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2217 dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2219 if (tty_register_driver(&dev_syscons_driver))
2220 panic("Couldn't register /dev/console driver\n");
2222 /* console calls tty_register_driver() before kmalloc() works.
2223 * Thus, we can't devfs_register() then. Do so now, instead.
2225 #ifdef CONFIG_VT
2226 con_init_devfs();
2227 #endif
2229 #ifdef CONFIG_UNIX98_PTYS
2230 dev_ptmx_driver = dev_tty_driver;
2231 dev_ptmx_driver.driver_name = "/dev/ptmx";
2232 dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2233 dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2234 dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2235 dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2236 dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2238 if (tty_register_driver(&dev_ptmx_driver))
2239 panic("Couldn't register /dev/ptmx driver\n");
2240 #endif
2242 #ifdef CONFIG_VT
2243 dev_console_driver = dev_tty_driver;
2244 dev_console_driver.driver_name = "/dev/vc/0";
2245 dev_console_driver.name = dev_console_driver.driver_name + 5;
2246 dev_console_driver.major = TTY_MAJOR;
2247 dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2248 dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2250 if (tty_register_driver(&dev_console_driver))
2251 panic("Couldn't register /dev/tty0 driver\n");
2253 kbd_init();
2254 #endif
2255 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2256 espserial_init();
2257 #endif
2258 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2259 vme_scc_init();
2260 #endif
2261 #ifdef CONFIG_COMPUTONE
2262 ip2_init();
2263 #endif
2264 #ifdef CONFIG_MAC_SERIAL
2265 macserial_init();
2266 #endif
2267 #ifdef CONFIG_ROCKETPORT
2268 rp_init();
2269 #endif
2270 #ifdef CONFIG_SERIAL167
2271 serial167_init();
2272 #endif
2273 #ifdef CONFIG_CYCLADES
2274 cy_init();
2275 #endif
2276 #ifdef CONFIG_STALLION
2277 stl_init();
2278 #endif
2279 #ifdef CONFIG_ISTALLION
2280 stli_init();
2281 #endif
2282 #ifdef CONFIG_DIGI
2283 pcxe_init();
2284 #endif
2285 #ifdef CONFIG_DIGIEPCA
2286 pc_init();
2287 #endif
2288 #ifdef CONFIG_RISCOM8
2289 riscom8_init();
2290 #endif
2291 #ifdef CONFIG_SPECIALIX
2292 specialix_init();
2293 #endif
2294 #ifdef CONFIG_SX
2295 sx_init();
2296 #endif
2297 #ifdef CONFIG_RIO
2298 rio_init();
2299 #endif
2300 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2301 rs_8xx_init();
2302 #endif /* CONFIG_8xx */
2303 pty_init();
2304 #ifdef CONFIG_MOXA_SMARTIO
2305 mxser_init();
2306 #endif
2307 #ifdef CONFIG_MOXA_INTELLIO
2308 moxa_init();
2309 #endif
2310 #ifdef CONFIG_VT
2311 vcs_init();
2312 #endif