2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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...
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 support for a Unix98-style ptmx device.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
59 #include <linux/config.h>
60 #include <linux/types.h>
61 #include <linux/major.h>
62 #include <linux/errno.h>
63 #include <linux/signal.h>
64 #include <linux/fcntl.h>
65 #include <linux/sched.h>
66 #include <linux/interrupt.h>
67 #include <linux/tty.h>
68 #include <linux/tty_driver.h>
69 #include <linux/tty_flip.h>
70 #include <linux/devpts_fs.h>
71 #include <linux/file.h>
72 #include <linux/console.h>
73 #include <linux/timer.h>
74 #include <linux/ctype.h>
77 #include <linux/string.h>
78 #include <linux/malloc.h>
79 #include <linux/poll.h>
80 #include <linux/proc_fs.h>
81 #include <linux/init.h>
82 #include <linux/smp_lock.h>
84 #include <asm/uaccess.h>
85 #include <asm/system.h>
86 #include <asm/bitops.h>
88 #include <linux/kbd_kern.h>
89 #include <linux/vt_kern.h>
90 #include <linux/selection.h>
93 #include <linux/kmod.h>
96 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
97 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
98 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
99 #define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
101 #undef TTY_DEBUG_HANGUP
103 #define TTY_PARANOIA_CHECK 1
104 #define CHECK_TTY_COUNT 1
106 struct termios tty_std_termios
; /* for the benefit of tty drivers */
107 struct tty_driver
*tty_drivers
= NULL
; /* linked list of tty drivers */
108 struct tty_ldisc ldiscs
[NR_LDISCS
]; /* line disc dispatch table */
110 #ifdef CONFIG_UNIX98_PTYS
111 extern struct tty_driver ptm_driver
[]; /* Unix98 pty masters; for /dev/ptmx */
115 * redirect is the pseudo-tty that console output
116 * is redirected to if asked by TIOCCONS.
118 struct tty_struct
* redirect
= NULL
;
120 static void initialize_tty_struct(struct tty_struct
*tty
);
122 static ssize_t
tty_read(struct file
*, char *, size_t, loff_t
*);
123 static ssize_t
tty_write(struct file
*, const char *, size_t, loff_t
*);
124 static unsigned int tty_poll(struct file
*, poll_table
*);
125 static int tty_open(struct inode
*, struct file
*);
126 static int tty_release(struct inode
*, struct file
*);
127 int tty_ioctl(struct inode
* inode
, struct file
* file
,
128 unsigned int cmd
, unsigned long arg
);
129 static int tty_fasync(int fd
, struct file
* filp
, int on
);
131 extern long console_8xx_init(long, long);
132 extern int rs_8xx_init(void);
133 #endif /* CONFIG_8xx */
136 #define MIN(a,b) ((a) < (b) ? (a) : (b))
140 * This routine returns the name of tty.
142 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
143 (tty)->driver.name_base)
145 char *tty_name(struct tty_struct
*tty
, char *buf
)
148 sprintf(buf
, "%s%d", tty
->driver
.name
, TTY_NUMBER(tty
));
150 strcpy(buf
, "NULL tty");
154 inline int tty_paranoia_check(struct tty_struct
*tty
, kdev_t device
,
157 #ifdef TTY_PARANOIA_CHECK
158 static const char *badmagic
=
159 "Warning: bad magic number for tty struct (%s) in %s\n";
160 static const char *badtty
=
161 "Warning: null TTY for (%s) in %s\n";
164 printk(badtty
, kdevname(device
), routine
);
167 if (tty
->magic
!= TTY_MAGIC
) {
168 printk(badmagic
, kdevname(device
), routine
);
175 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
177 #ifdef CHECK_TTY_COUNT
181 for(f
= inuse_filps
; f
; f
= f
->f_next
) {
182 if(f
->private_data
== tty
)
185 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
186 tty
->driver
.subtype
== PTY_TYPE_SLAVE
&&
187 tty
->link
&& tty
->link
->count
)
189 if (tty
->count
!= count
) {
190 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
191 kdevname(tty
->device
), tty
->count
, count
, routine
);
198 int tty_register_ldisc(int disc
, struct tty_ldisc
*new_ldisc
)
200 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
204 ldiscs
[disc
] = *new_ldisc
;
205 ldiscs
[disc
].flags
|= LDISC_FLAG_DEFINED
;
206 ldiscs
[disc
].num
= disc
;
208 memset(&ldiscs
[disc
], 0, sizeof(struct tty_ldisc
));
213 /* Set the discipline of a tty line. */
214 static int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
217 struct tty_ldisc o_ldisc
;
220 if ((ldisc
< N_TTY
) || (ldisc
>= NR_LDISCS
))
223 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
224 /* Cyrus Durgin <cider@speakeasy.org> */
225 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
)) {
227 sprintf(modname
, "tty-ldisc-%d", ldisc
);
228 request_module (modname
);
231 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
))
234 if (tty
->ldisc
.num
== ldisc
)
235 return 0; /* We are already in the desired discipline */
236 o_ldisc
= tty
->ldisc
;
238 tty_wait_until_sent(tty
, 0);
240 /* Shutdown the current discipline. */
241 if (tty
->ldisc
.close
)
242 (tty
->ldisc
.close
)(tty
);
244 /* Now set up the new line discipline. */
245 tty
->ldisc
= ldiscs
[ldisc
];
246 tty
->termios
->c_line
= ldisc
;
248 retval
= (tty
->ldisc
.open
)(tty
);
250 tty
->ldisc
= o_ldisc
;
251 tty
->termios
->c_line
= tty
->ldisc
.num
;
252 if (tty
->ldisc
.open
&& (tty
->ldisc
.open(tty
) < 0)) {
253 tty
->ldisc
= ldiscs
[N_TTY
];
254 tty
->termios
->c_line
= N_TTY
;
255 if (tty
->ldisc
.open
) {
256 int r
= tty
->ldisc
.open(tty
);
259 panic("Couldn't open N_TTY ldisc for "
261 tty_name(tty
, buf
), r
);
265 if (tty
->ldisc
.num
!= o_ldisc
.num
&& tty
->driver
.set_ldisc
)
266 tty
->driver
.set_ldisc(tty
);
271 * This routine returns a tty driver structure, given a device number
273 struct tty_driver
*get_tty_driver(kdev_t device
)
276 struct tty_driver
*p
;
278 minor
= MINOR(device
);
279 major
= MAJOR(device
);
281 for (p
= tty_drivers
; p
; p
= p
->next
) {
282 if (p
->major
!= major
)
284 if (minor
< p
->minor_start
)
286 if (minor
>= p
->minor_start
+ p
->num
)
294 * If we try to write to, or set the state of, a terminal and we're
295 * not in the foreground, send a SIGTTOU. If the signal is blocked or
296 * ignored, go ahead and perform the operation. (POSIX 7.2)
298 int tty_check_change(struct tty_struct
* tty
)
300 if (current
->tty
!= tty
)
302 if (tty
->pgrp
<= 0) {
303 printk("tty_check_change: tty->pgrp <= 0!\n");
306 if (current
->pgrp
== tty
->pgrp
)
308 if (is_ignored(SIGTTOU
))
310 if (is_orphaned_pgrp(current
->pgrp
))
312 (void) kill_pg(current
->pgrp
,SIGTTOU
,1);
316 static ssize_t
hung_up_tty_read(struct file
* file
, char * buf
,
317 size_t count
, loff_t
*ppos
)
319 /* Can't seek (pread) on ttys. */
320 if (ppos
!= &file
->f_pos
)
325 static ssize_t
hung_up_tty_write(struct file
* file
, const char * buf
,
326 size_t count
, loff_t
*ppos
)
328 /* Can't seek (pwrite) on ttys. */
329 if (ppos
!= &file
->f_pos
)
334 static unsigned int hung_up_tty_poll(struct file
* filp
, poll_table
* wait
)
336 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
339 static int hung_up_tty_ioctl(struct inode
* inode
, struct file
* file
,
340 unsigned int cmd
, unsigned long arg
)
342 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
345 static long long tty_lseek(struct file
* file
, long long offset
, int orig
)
350 static struct file_operations tty_fops
= {
354 NULL
, /* tty_readdir */
361 NULL
, /* tty_fsync */
365 static struct file_operations hung_up_tty_fops
= {
369 NULL
, /* hung_up_tty_readdir */
372 NULL
, /* hung_up_tty_mmap */
373 NULL
, /* hung_up_tty_open */
375 tty_release
, /* hung_up_tty_release */
376 NULL
, /* hung_up_tty_fsync */
377 NULL
/* hung_up_tty_fasync */
381 * This can be called through the "tq_scheduler"
382 * task-list. That is process synchronous, but
383 * doesn't hold any locks, so we need to make
384 * sure we have the appropriate locks for what
387 void do_tty_hangup(void *data
)
389 struct tty_struct
*tty
= (struct tty_struct
*) data
;
391 struct file
* cons_filp
= NULL
;
392 struct task_struct
*p
;
393 int closecount
= 0, n
;
398 /* inuse_filps is protected by the single kernel lock */
401 check_tty_count(tty
, "do_tty_hangup");
402 for (filp
= inuse_filps
; filp
; filp
= filp
->f_next
) {
403 if (filp
->private_data
!= tty
)
407 if (!filp
->f_dentry
->d_inode
)
409 if (filp
->f_dentry
->d_inode
->i_rdev
== CONSOLE_DEV
||
410 filp
->f_dentry
->d_inode
->i_rdev
== SYSCONS_DEV
) {
414 if (filp
->f_op
!= &tty_fops
)
417 tty_fasync(-1, filp
, 0);
418 filp
->f_op
= &hung_up_tty_fops
;
421 /* FIXME! What are the locking issues here? This may me overdoing things.. */
425 save_flags(flags
); cli();
426 if (tty
->ldisc
.flush_buffer
)
427 tty
->ldisc
.flush_buffer(tty
);
428 if (tty
->driver
.flush_buffer
)
429 tty
->driver
.flush_buffer(tty
);
430 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
431 tty
->ldisc
.write_wakeup
)
432 (tty
->ldisc
.write_wakeup
)(tty
);
433 restore_flags(flags
);
436 wake_up_interruptible(&tty
->write_wait
);
437 wake_up_interruptible(&tty
->read_wait
);
440 * Shutdown the current line discipline, and reset it to
443 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
)
444 *tty
->termios
= tty
->driver
.init_termios
;
445 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
446 if (tty
->ldisc
.close
)
447 (tty
->ldisc
.close
)(tty
);
448 tty
->ldisc
= ldiscs
[N_TTY
];
449 tty
->termios
->c_line
= N_TTY
;
450 if (tty
->ldisc
.open
) {
451 int i
= (tty
->ldisc
.open
)(tty
);
453 printk("do_tty_hangup: N_TTY open: error %d\n",
458 read_lock(&tasklist_lock
);
460 if ((tty
->session
> 0) && (p
->session
== tty
->session
) &&
462 send_sig(SIGHUP
,p
,1);
463 send_sig(SIGCONT
,p
,1);
465 p
->tty_old_pgrp
= tty
->pgrp
;
470 read_unlock(&tasklist_lock
);
475 tty
->ctrl_status
= 0;
477 * If one of the devices matches a console pointer, we
478 * cannot just call hangup() because that will cause
479 * tty->count and state->count to go out of sync.
480 * So we just call close() the right number of times.
483 if (tty
->driver
.close
)
484 for (n
= 0; n
< closecount
; n
++)
485 tty
->driver
.close(tty
, cons_filp
);
486 } else if (tty
->driver
.hangup
)
487 (tty
->driver
.hangup
)(tty
);
491 void tty_hangup(struct tty_struct
* tty
)
493 #ifdef TTY_DEBUG_HANGUP
496 printk("%s hangup...\n", tty_name(tty
, buf
));
498 queue_task(&tty
->tq_hangup
, &tq_scheduler
);
501 void tty_vhangup(struct tty_struct
* tty
)
503 #ifdef TTY_DEBUG_HANGUP
506 printk("%s vhangup...\n", tty_name(tty
, buf
));
508 do_tty_hangup((void *) tty
);
511 int tty_hung_up_p(struct file
* filp
)
513 return (filp
->f_op
== &hung_up_tty_fops
);
517 * This function is typically called only by the session leader, when
518 * it wants to disassociate itself from its controlling tty.
520 * It performs the following functions:
521 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
522 * (2) Clears the tty from being controlling the session
523 * (3) Clears the controlling tty for all processes in the
526 * The argument on_exit is set to 1 if called when a process is
527 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
529 void disassociate_ctty(int on_exit
)
531 struct tty_struct
*tty
= current
->tty
;
532 struct task_struct
*p
;
536 tty_pgrp
= tty
->pgrp
;
537 if (on_exit
&& tty
->driver
.type
!= TTY_DRIVER_TYPE_PTY
)
540 if (current
->tty_old_pgrp
) {
541 kill_pg(current
->tty_old_pgrp
, SIGHUP
, on_exit
);
542 kill_pg(current
->tty_old_pgrp
, SIGCONT
, on_exit
);
547 kill_pg(tty_pgrp
, SIGHUP
, on_exit
);
549 kill_pg(tty_pgrp
, SIGCONT
, on_exit
);
552 current
->tty_old_pgrp
= 0;
556 read_lock(&tasklist_lock
);
558 if (p
->session
== current
->session
)
560 read_unlock(&tasklist_lock
);
563 void wait_for_keypress(void)
565 struct console
*c
= console_drivers
;
566 if (c
) c
->wait_key(c
);
569 void stop_tty(struct tty_struct
*tty
)
574 if (tty
->link
&& tty
->link
->packet
) {
575 tty
->ctrl_status
&= ~TIOCPKT_START
;
576 tty
->ctrl_status
|= TIOCPKT_STOP
;
577 wake_up_interruptible(&tty
->link
->read_wait
);
579 if (tty
->driver
.stop
)
580 (tty
->driver
.stop
)(tty
);
583 void start_tty(struct tty_struct
*tty
)
585 if (!tty
->stopped
|| tty
->flow_stopped
)
588 if (tty
->link
&& tty
->link
->packet
) {
589 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
590 tty
->ctrl_status
|= TIOCPKT_START
;
591 wake_up_interruptible(&tty
->link
->read_wait
);
593 if (tty
->driver
.start
)
594 (tty
->driver
.start
)(tty
);
595 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
596 tty
->ldisc
.write_wakeup
)
597 (tty
->ldisc
.write_wakeup
)(tty
);
598 wake_up_interruptible(&tty
->write_wait
);
601 static ssize_t
tty_read(struct file
* file
, char * buf
, size_t count
,
605 struct tty_struct
* tty
;
608 /* Can't seek (pread) on ttys. */
609 if (ppos
!= &file
->f_pos
)
612 tty
= (struct tty_struct
*)file
->private_data
;
613 inode
= file
->f_dentry
->d_inode
;
614 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_read"))
616 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
619 /* This check not only needs to be done before reading, but also
620 whenever read_chan() gets woken up after sleeping, so I've
621 moved it to there. This should only be done for the N_TTY
622 line discipline, anyway. Same goes for write_chan(). -- jlc. */
624 if ((inode
->i_rdev
!= CONSOLE_DEV
) && /* don't stop on /dev/console */
626 (current
->tty
== tty
) &&
627 (tty
->pgrp
!= current
->pgrp
))
628 if (is_ignored(SIGTTIN
) || is_orphaned_pgrp(current
->pgrp
))
631 (void) kill_pg(current
->pgrp
, SIGTTIN
, 1);
636 i
= (tty
->ldisc
.read
)(tty
,file
,buf
,count
);
640 inode
->i_atime
= CURRENT_TIME
;
645 * Split writes up in sane blocksizes to avoid
646 * denial-of-service type attacks
648 static inline ssize_t
do_tty_write(
649 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
650 struct tty_struct
*tty
,
652 const unsigned char *buf
,
655 ssize_t ret
= 0, written
= 0;
656 struct inode
*inode
= file
->f_dentry
->d_inode
;
659 if (down_interruptible(&inode
->i_atomic_write
)) {
664 unsigned long size
= PAGE_SIZE
*2;
667 ret
= write(tty
, file
, buf
, size
);
676 if (signal_pending(current
))
678 if (current
->need_resched
)
682 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
;
685 up(&inode
->i_atomic_write
);
691 static ssize_t
tty_write(struct file
* file
, const char * buf
, size_t count
,
695 struct tty_struct
* tty
;
698 /* Can't seek (pwrite) on ttys. */
699 if (ppos
!= &file
->f_pos
)
703 * For now, we redirect writes from /dev/console as
706 inode
= file
->f_dentry
->d_inode
;
707 is_console
= (inode
->i_rdev
== SYSCONS_DEV
||
708 inode
->i_rdev
== CONSOLE_DEV
);
710 if (is_console
&& redirect
)
713 tty
= (struct tty_struct
*)file
->private_data
;
714 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_write"))
716 if (!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
719 if (!is_console
&& L_TOSTOP(tty
) && (tty
->pgrp
> 0) &&
720 (current
->tty
== tty
) && (tty
->pgrp
!= current
->pgrp
)) {
721 if (is_orphaned_pgrp(current
->pgrp
))
723 if (!is_ignored(SIGTTOU
)) {
724 (void) kill_pg(current
->pgrp
, SIGTTOU
, 1);
729 if (!tty
->ldisc
.write
)
731 return do_tty_write(tty
->ldisc
.write
, tty
, file
,
732 (const unsigned char *)buf
, count
);
735 /* Semaphore to protect creating and releasing a tty */
736 static struct semaphore tty_sem
= MUTEX
;
738 static void down_tty_sem(int index
)
743 static void up_tty_sem(int index
)
748 static void release_mem(struct tty_struct
*tty
, int idx
);
751 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
752 * failed open. The new code protects the open with a semaphore, so it's
753 * really quite straightforward. The semaphore locking can probably be
754 * relaxed for the (most common) case of reopening a tty.
756 static int init_dev(kdev_t device
, struct tty_struct
**ret_tty
)
758 struct tty_struct
*tty
, *o_tty
;
759 struct termios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
760 struct termios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
761 struct tty_driver
*driver
;
765 driver
= get_tty_driver(device
);
769 idx
= MINOR(device
) - driver
->minor_start
;
772 * Check whether we need to acquire the tty semaphore to avoid
773 * race conditions. For now, play it safe.
777 /* check whether we're reopening an existing tty */
778 tty
= driver
->table
[idx
];
779 if (tty
) goto fast_track
;
782 * First time open is complex, especially for PTY devices.
783 * This code guarantees that either everything succeeds and the
784 * TTY is ready for operation, or else the table slots are vacated
785 * and the allocated memory released. (Except that the termios
786 * and locked termios may be retained.)
793 tty
= (struct tty_struct
*) get_free_page(GFP_KERNEL
);
796 initialize_tty_struct(tty
);
797 tty
->device
= device
;
798 tty
->driver
= *driver
;
800 tp_loc
= &driver
->termios
[idx
];
802 tp
= (struct termios
*) kmalloc(sizeof(struct termios
),
806 *tp
= driver
->init_termios
;
809 ltp_loc
= &driver
->termios_locked
[idx
];
811 ltp
= (struct termios
*) kmalloc(sizeof(struct termios
),
815 memset(ltp
, 0, sizeof(struct termios
));
818 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
819 o_tty
= (struct tty_struct
*) get_free_page(GFP_KERNEL
);
822 initialize_tty_struct(o_tty
);
823 o_tty
->device
= (kdev_t
) MKDEV(driver
->other
->major
,
824 driver
->other
->minor_start
+ idx
);
825 o_tty
->driver
= *driver
->other
;
827 o_tp_loc
= &driver
->other
->termios
[idx
];
829 o_tp
= (struct termios
*)
830 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
833 *o_tp
= driver
->other
->init_termios
;
836 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
838 o_ltp
= (struct termios
*)
839 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
842 memset(o_ltp
, 0, sizeof(struct termios
));
846 * Everything allocated ... set up the o_tty structure.
848 driver
->other
->table
[idx
] = o_tty
;
853 o_tty
->termios
= *o_tp_loc
;
854 o_tty
->termios_locked
= *o_ltp_loc
;
855 (*driver
->other
->refcount
)++;
856 if (driver
->subtype
== PTY_TYPE_MASTER
)
859 /* Establish the links in both directions */
865 * All structures have been allocated, so now we install them.
866 * Failures after this point use release_mem to clean up, so
867 * there's no need to null out the local pointers.
869 driver
->table
[idx
] = tty
; /* FIXME: this is broken and
870 probably causes ^D bug. tty->private_date does not (yet) point
871 to a console, if keypress comes now, await armagedon.
873 also, driver->table is accessed from interrupt for vt case,
874 and this does not look like atomic access at all. */
880 tty
->termios
= *tp_loc
;
881 tty
->termios_locked
= *ltp_loc
;
882 (*driver
->refcount
)++;
886 * Structures all installed ... call the ldisc open routines.
887 * If we fail here just call release_mem to clean up. No need
888 * to decrement the use counts, as release_mem doesn't care.
890 if (tty
->ldisc
.open
) {
891 retval
= (tty
->ldisc
.open
)(tty
);
893 goto release_mem_out
;
895 if (o_tty
&& o_tty
->ldisc
.open
) {
896 retval
= (o_tty
->ldisc
.open
)(o_tty
);
898 if (tty
->ldisc
.close
)
899 (tty
->ldisc
.close
)(tty
);
900 goto release_mem_out
;
906 * This fast open can be used if the tty is already open.
907 * No memory is allocated, and the only failures are from
908 * attempting to open a closing tty or attempting multiple
909 * opens on a pty master.
912 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
916 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
917 driver
->subtype
== PTY_TYPE_MASTER
) {
919 * special case for PTY masters: only one open permitted,
920 * and the slave side open count is incremented as well.
929 tty
->driver
= *driver
; /* N.B. why do this every time?? */
934 /* All paths come through here to release the semaphore */
939 /* Release locally allocated memory ... nothing placed in slots */
942 kfree_s(o_tp
, sizeof(struct termios
));
944 free_page((unsigned long) o_tty
);
946 kfree_s(ltp
, sizeof(struct termios
));
948 kfree_s(tp
, sizeof(struct termios
));
949 free_page((unsigned long) tty
);
955 /* call the tty release_mem routine to clean out this slot */
957 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
);
958 release_mem(tty
, idx
);
963 * Releases memory associated with a tty structure, and clears out the
964 * driver table slots.
966 static void release_mem(struct tty_struct
*tty
, int idx
)
968 struct tty_struct
*o_tty
;
971 if ((o_tty
= tty
->link
) != NULL
) {
972 o_tty
->driver
.table
[idx
] = NULL
;
973 if (o_tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
974 tp
= o_tty
->driver
.termios
[idx
];
975 o_tty
->driver
.termios
[idx
] = NULL
;
976 kfree_s(tp
, sizeof(struct termios
));
979 (*o_tty
->driver
.refcount
)--;
980 free_page((unsigned long) o_tty
);
983 tty
->driver
.table
[idx
] = NULL
;
984 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
985 tp
= tty
->driver
.termios
[idx
];
986 tty
->driver
.termios
[idx
] = NULL
;
987 kfree_s(tp
, sizeof(struct termios
));
990 (*tty
->driver
.refcount
)--;
991 free_page((unsigned long) tty
);
995 * Even releasing the tty structures is a tricky business.. We have
996 * to be very careful that the structures are all released at the
997 * same time, as interrupts might otherwise get the wrong pointers.
999 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1000 * lead to double frees or releasing memory still in use.
1002 static void release_dev(struct file
* filp
)
1004 struct tty_struct
*tty
, *o_tty
;
1005 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1009 tty
= (struct tty_struct
*)filp
->private_data
;
1010 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "release_dev"))
1013 check_tty_count(tty
, "release_dev");
1015 tty_fasync(-1, filp
, 0);
1017 idx
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1018 pty_master
= (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1019 tty
->driver
.subtype
== PTY_TYPE_MASTER
);
1022 #ifdef TTY_PARANOIA_CHECK
1023 if (idx
< 0 || idx
>= tty
->driver
.num
) {
1024 printk("release_dev: bad idx when trying to free (%s)\n",
1025 kdevname(tty
->device
));
1028 if (tty
!= tty
->driver
.table
[idx
]) {
1029 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1030 idx
, kdevname(tty
->device
));
1033 if (tty
->termios
!= tty
->driver
.termios
[idx
]) {
1034 printk("release_dev: driver.termios[%d] not termios "
1036 idx
, kdevname(tty
->device
));
1039 if (tty
->termios_locked
!= tty
->driver
.termios_locked
[idx
]) {
1040 printk("release_dev: driver.termios_locked[%d] not "
1041 "termios_locked for (%s)\n",
1042 idx
, kdevname(tty
->device
));
1047 #ifdef TTY_DEBUG_HANGUP
1048 printk("release_dev of %s (tty count=%d)...", tty_name(tty
, buf
),
1052 #ifdef TTY_PARANOIA_CHECK
1053 if (tty
->driver
.other
) {
1054 if (o_tty
!= tty
->driver
.other
->table
[idx
]) {
1055 printk("release_dev: other->table[%d] not o_tty for ("
1057 idx
, kdevname(tty
->device
));
1060 if (o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) {
1061 printk("release_dev: other->termios[%d] not o_termios "
1063 idx
, kdevname(tty
->device
));
1066 if (o_tty
->termios_locked
!=
1067 tty
->driver
.other
->termios_locked
[idx
]) {
1068 printk("release_dev: other->termios_locked[%d] not "
1069 "o_termios_locked for (%s)\n",
1070 idx
, kdevname(tty
->device
));
1073 if (o_tty
->link
!= tty
) {
1074 printk("release_dev: bad pty pointers\n");
1080 if (tty
->driver
.close
)
1081 tty
->driver
.close(tty
, filp
);
1084 * Sanity check: if tty->count is going to zero, there shouldn't be
1085 * any waiters on tty->read_wait or tty->write_wait. We test the
1086 * wait queues and kick everyone out _before_ actually starting to
1087 * close. This ensures that we won't block while releasing the tty
1090 * The test for the o_tty closing is necessary, since the master and
1091 * slave sides may close in any order. If the slave side closes out
1092 * first, its count will be one, since the master side holds an open.
1093 * Thus this test wouldn't be triggered at the time the slave closes,
1096 * Note that it's possible for the tty to be opened again while we're
1097 * flushing out waiters. By recalculating the closing flags before
1098 * each iteration we avoid any problems.
1101 tty_closing
= tty
->count
<= 1;
1102 o_tty_closing
= o_tty
&&
1103 (o_tty
->count
<= (pty_master
? 1 : 0));
1107 if (waitqueue_active(&tty
->read_wait
)) {
1108 wake_up(&tty
->read_wait
);
1111 if (waitqueue_active(&tty
->write_wait
)) {
1112 wake_up(&tty
->write_wait
);
1116 if (o_tty_closing
) {
1117 if (waitqueue_active(&o_tty
->read_wait
)) {
1118 wake_up(&o_tty
->read_wait
);
1121 if (waitqueue_active(&o_tty
->write_wait
)) {
1122 wake_up(&o_tty
->write_wait
);
1129 printk("release_dev: %s: read/write wait queue active!\n",
1130 tty_name(tty
, buf
));
1135 * The closing flags are now consistent with the open counts on
1136 * both sides, and we've completed the last operation that could
1137 * block, so it's safe to proceed with closing.
1140 if (--o_tty
->count
< 0) {
1141 printk("release_dev: bad pty slave count (%d) for %s\n",
1142 o_tty
->count
, tty_name(o_tty
, buf
));
1146 if (--tty
->count
< 0) {
1147 printk("release_dev: bad tty->count (%d) for %s\n",
1148 tty
->count
, tty_name(tty
, buf
));
1153 * We've decremented tty->count, so we should zero out
1154 * filp->private_data, to break the link between the tty and
1155 * the file descriptor. Otherwise if close_fp() blocks before
1156 * the the file descriptor is removed from the inuse_filp
1157 * list, check_tty_count() could observe a discrepancy and
1158 * printk a warning message to the user.
1160 filp
->private_data
= 0;
1163 * Perform some housekeeping before deciding whether to return.
1165 * Set the TTY_CLOSING flag if this was the last open. In the
1166 * case of a pty we may have to wait around for the other side
1167 * to close, and TTY_CLOSING makes sure we can't be reopened.
1170 set_bit(TTY_CLOSING
, &tty
->flags
);
1172 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1175 * If _either_ side is closing, make sure there aren't any
1176 * processes that still think tty or o_tty is their controlling
1177 * tty. Also, clear redirect if it points to either tty.
1179 if (tty_closing
|| o_tty_closing
) {
1180 struct task_struct
*p
;
1182 read_lock(&tasklist_lock
);
1184 if (p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
))
1187 read_unlock(&tasklist_lock
);
1189 if (redirect
== tty
|| (o_tty
&& redirect
== o_tty
))
1193 /* check whether both sides are closing ... */
1194 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1197 #ifdef TTY_DEBUG_HANGUP
1198 printk("freeing tty structure...");
1202 * Shutdown the current line discipline, and reset it to N_TTY.
1203 * N.B. why reset ldisc when we're releasing the memory??
1205 if (tty
->ldisc
.close
)
1206 (tty
->ldisc
.close
)(tty
);
1207 tty
->ldisc
= ldiscs
[N_TTY
];
1208 tty
->termios
->c_line
= N_TTY
;
1210 if (o_tty
->ldisc
.close
)
1211 (o_tty
->ldisc
.close
)(o_tty
);
1212 o_tty
->ldisc
= ldiscs
[N_TTY
];
1216 * Make sure that the tty's task queue isn't activated.
1218 run_task_queue(&tq_timer
);
1219 run_task_queue(&tq_scheduler
);
1222 * The release_mem function takes care of the details of clearing
1223 * the slots and preserving the termios structure.
1225 release_mem(tty
, idx
);
1229 * tty_open and tty_release keep up the tty count that contains the
1230 * number of opens done on a tty. We cannot use the inode-count, as
1231 * different inodes might point to the same tty.
1233 * Open-counting is needed for pty masters, as well as for keeping
1234 * track of serial lines: DTR is dropped when the last close happens.
1235 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1237 * The termios state of a pty is reset on first open so that
1238 * settings don't persist across reuse.
1240 static int tty_open(struct inode
* inode
, struct file
* filp
)
1242 struct tty_struct
*tty
;
1245 unsigned short saved_flags
;
1248 saved_flags
= filp
->f_flags
;
1250 noctty
= filp
->f_flags
& O_NOCTTY
;
1251 device
= inode
->i_rdev
;
1252 if (device
== TTY_DEV
) {
1255 device
= current
->tty
->device
;
1256 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1260 if (device
== CONSOLE_DEV
) {
1261 extern int fg_console
;
1262 device
= MKDEV(TTY_MAJOR
, fg_console
+ 1);
1266 if (device
== SYSCONS_DEV
) {
1267 struct console
*c
= console_drivers
;
1268 while(c
&& !c
->device
)
1272 device
= c
->device(c
);
1273 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/console block */
1277 if (device
== PTMX_DEV
) {
1278 #ifdef CONFIG_UNIX98_PTYS
1280 /* find a free pty. */
1282 struct tty_driver
*driver
;
1284 /* find a device that is not in use. */
1286 for ( major
= 0 ; major
< UNIX98_NR_MAJORS
; major
++ ) {
1287 driver
= &ptm_driver
[major
];
1288 for (minor
= driver
->minor_start
;
1289 minor
< driver
->minor_start
+ driver
->num
;
1291 device
= MKDEV(driver
->major
, minor
);
1292 if (!init_dev(device
, &tty
)) goto ptmx_found
; /* ok! */
1295 return -EIO
; /* no free ptys */
1297 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
1298 minor
-= driver
->minor_start
;
1299 devpts_pty_new(driver
->other
->name_base
+ minor
, MKDEV(driver
->other
->major
, minor
+ driver
->other
->minor_start
));
1303 #else /* CONFIG_UNIX_98_PTYS */
1307 #endif /* CONFIG_UNIX_98_PTYS */
1310 retval
= init_dev(device
, &tty
);
1314 #ifdef CONFIG_UNIX98_PTYS
1317 filp
->private_data
= tty
;
1318 check_tty_count(tty
, "tty_open");
1319 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1320 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1322 #ifdef TTY_DEBUG_HANGUP
1323 printk("opening %s...", tty_name(tty
, buf
));
1325 if (tty
->driver
.open
)
1326 retval
= tty
->driver
.open(tty
, filp
);
1329 filp
->f_flags
= saved_flags
;
1331 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser())
1335 #ifdef TTY_DEBUG_HANGUP
1336 printk("error %d in opening %s...", retval
,
1337 tty_name(tty
, buf
));
1341 if (retval
!= -ERESTARTSYS
)
1343 if (signal_pending(current
))
1347 * Need to reset f_op in case a hangup happened.
1349 filp
->f_op
= &tty_fops
;
1355 tty
->session
== 0) {
1357 current
->tty_old_pgrp
= 0;
1358 tty
->session
= current
->session
;
1359 tty
->pgrp
= current
->pgrp
;
1361 if ((tty
->driver
.type
== TTY_DRIVER_TYPE_SERIAL
) &&
1362 (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) &&
1363 (tty
->count
== 1)) {
1364 static int nr_warns
= 0;
1366 printk(KERN_WARNING
"tty_io.c: "
1367 "process %d (%s) used obsolete /dev/%s - "
1368 "update software to use /dev/ttyS%d\n",
1369 current
->pid
, current
->comm
,
1370 tty_name(tty
, buf
), TTY_NUMBER(tty
));
1377 static int tty_release(struct inode
* inode
, struct file
* filp
)
1383 static unsigned int tty_poll(struct file
* filp
, poll_table
* wait
)
1385 struct tty_struct
* tty
;
1387 tty
= (struct tty_struct
*)filp
->private_data
;
1388 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_poll"))
1391 if (tty
->ldisc
.poll
)
1392 return (tty
->ldisc
.poll
)(tty
, filp
, wait
);
1397 * fasync_helper() is used by some character device drivers (mainly mice)
1398 * to set up the fasync queue. It returns negative on error, 0 if it did
1399 * no changes and positive if it added/deleted the entry.
1401 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
1403 struct fasync_struct
*fa
, **fp
;
1404 unsigned long flags
;
1406 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
1407 if (fa
->fa_file
== filp
)
1416 fa
= (struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1419 fa
->magic
= FASYNC_MAGIC
;
1424 fa
->fa_next
= *fapp
;
1426 restore_flags(flags
);
1434 restore_flags(flags
);
1439 static int tty_fasync(int fd
, struct file
* filp
, int on
)
1441 struct tty_struct
* tty
;
1444 tty
= (struct tty_struct
*)filp
->private_data
;
1445 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_fasync"))
1448 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
1453 if (!waitqueue_active(&tty
->read_wait
))
1454 tty
->minimum_to_wake
= 1;
1455 if (filp
->f_owner
.pid
== 0) {
1456 filp
->f_owner
.pid
= (-tty
->pgrp
) ? : current
->pid
;
1457 filp
->f_owner
.uid
= current
->uid
;
1458 filp
->f_owner
.euid
= current
->euid
;
1461 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
1462 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
1467 static int tiocsti(struct tty_struct
*tty
, char * arg
)
1471 if ((current
->tty
!= tty
) && !suser())
1473 if (get_user(ch
, arg
))
1475 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
, 1);
1479 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize
* arg
)
1481 if (copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
)))
1486 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
1487 struct winsize
* arg
)
1489 struct winsize tmp_ws
;
1491 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
1493 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
1496 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
1497 if ((real_tty
->pgrp
!= tty
->pgrp
) && (real_tty
->pgrp
> 0))
1498 kill_pg(real_tty
->pgrp
, SIGWINCH
, 1);
1499 tty
->winsize
= tmp_ws
;
1500 real_tty
->winsize
= tmp_ws
;
1504 static int tioccons(struct tty_struct
*tty
, struct tty_struct
*real_tty
)
1506 if (tty
->driver
.type
== TTY_DRIVER_TYPE_CONSOLE
||
1507 tty
->driver
.type
== TTY_DRIVER_TYPE_SYSCONS
) {
1515 redirect
= real_tty
;
1520 static int fionbio(struct file
*file
, int *arg
)
1524 if (get_user(nonblock
, arg
))
1528 file
->f_flags
|= O_NONBLOCK
;
1530 file
->f_flags
&= ~O_NONBLOCK
;
1534 static int tiocsctty(struct tty_struct
*tty
, int arg
)
1536 if (current
->leader
&&
1537 (current
->session
== tty
->session
))
1540 * The process must be a session leader and
1541 * not have a controlling tty already.
1543 if (!current
->leader
|| current
->tty
)
1545 if (tty
->session
> 0) {
1547 * This tty is already the controlling
1548 * tty for another session group!
1550 if ((arg
== 1) && suser()) {
1554 struct task_struct
*p
;
1556 read_lock(&tasklist_lock
);
1560 read_unlock(&tasklist_lock
);
1565 current
->tty_old_pgrp
= 0;
1566 tty
->session
= current
->session
;
1567 tty
->pgrp
= current
->pgrp
;
1571 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1574 * (tty == real_tty) is a cheap way of
1575 * testing if the tty is NOT a master pty.
1577 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1579 return put_user(real_tty
->pgrp
, arg
);
1582 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1585 int retval
= tty_check_change(real_tty
);
1591 if (!current
->tty
||
1592 (current
->tty
!= real_tty
) ||
1593 (real_tty
->session
!= current
->session
))
1595 get_user(pgrp
, (pid_t
*) arg
);
1598 if (session_of_pgrp(pgrp
) != current
->session
)
1600 real_tty
->pgrp
= pgrp
;
1604 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1607 * (tty == real_tty) is a cheap way of
1608 * testing if the tty is NOT a master pty.
1610 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1612 if (real_tty
->session
<= 0)
1614 return put_user(real_tty
->session
, arg
);
1617 static int tiocttygstruct(struct tty_struct
*tty
, struct tty_struct
*arg
)
1619 if (copy_to_user(arg
, tty
, sizeof(*arg
)))
1624 static int tiocsetd(struct tty_struct
*tty
, int *arg
)
1628 retval
= get_user(ldisc
, arg
);
1631 return tty_set_ldisc(tty
, ldisc
);
1634 static int send_break(struct tty_struct
*tty
, int duration
)
1636 current
->state
= TASK_INTERRUPTIBLE
;
1638 tty
->driver
.break_ctl(tty
, -1);
1639 if (!signal_pending(current
))
1640 schedule_timeout(duration
);
1641 tty
->driver
.break_ctl(tty
, 0);
1642 if (signal_pending(current
))
1648 * Split this up, as gcc can choke on it otherwise..
1650 int tty_ioctl(struct inode
* inode
, struct file
* file
,
1651 unsigned int cmd
, unsigned long arg
)
1653 struct tty_struct
*tty
, *real_tty
;
1656 tty
= (struct tty_struct
*)file
->private_data
;
1657 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_ioctl"))
1661 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1662 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1663 real_tty
= tty
->link
;
1666 * Break handling by driver
1668 if (!tty
->driver
.break_ctl
) {
1672 if (tty
->driver
.ioctl
)
1673 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1676 /* These two ioctl's always return success; even if */
1677 /* the driver doesn't support them. */
1680 if (!tty
->driver
.ioctl
)
1682 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1683 if (retval
== -ENOIOCTLCMD
)
1690 * Factor out some common prep work
1698 retval
= tty_check_change(tty
);
1701 if (cmd
!= TIOCCBRK
) {
1702 tty_wait_until_sent(tty
, 0);
1703 if (signal_pending(current
))
1711 return tiocsti(tty
, (char *)arg
);
1713 return tiocgwinsz(tty
, (struct winsize
*) arg
);
1715 return tiocswinsz(tty
, real_tty
, (struct winsize
*) arg
);
1717 return tioccons(tty
, real_tty
);
1719 return fionbio(file
, (int *) arg
);
1721 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1724 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1727 if (current
->tty
!= tty
)
1729 if (current
->leader
)
1730 disassociate_ctty(0);
1731 current
->tty
= NULL
;
1734 return tiocsctty(tty
, arg
);
1736 return tiocgpgrp(tty
, real_tty
, (pid_t
*) arg
);
1738 return tiocspgrp(tty
, real_tty
, (pid_t
*) arg
);
1740 return tiocgsid(tty
, real_tty
, (pid_t
*) arg
);
1742 return put_user(tty
->ldisc
.num
, (int *) arg
);
1744 return tiocsetd(tty
, (int *) arg
);
1747 return tioclinux(tty
, arg
);
1749 case TIOCTTYGSTRUCT
:
1750 return tiocttygstruct(tty
, (struct tty_struct
*) arg
);
1755 case TIOCSBRK
: /* Turn break on, unconditionally */
1756 tty
->driver
.break_ctl(tty
, -1);
1759 case TIOCCBRK
: /* Turn break off, unconditionally */
1760 tty
->driver
.break_ctl(tty
, 0);
1762 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1764 * XXX is the above comment correct, or the
1765 * code below correct? Is this ioctl used at
1769 return send_break(tty
, HZ
/4);
1771 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1772 return send_break(tty
, arg
? arg
*(HZ
/10) : HZ
/4);
1774 if (tty
->driver
.ioctl
) {
1775 int retval
= (tty
->driver
.ioctl
)(tty
, file
, cmd
, arg
);
1776 if (retval
!= -ENOIOCTLCMD
)
1779 if (tty
->ldisc
.ioctl
) {
1780 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
);
1781 if (retval
!= -ENOIOCTLCMD
)
1789 * This implements the "Secure Attention Key" --- the idea is to
1790 * prevent trojan horses by killing all processes associated with this
1791 * tty when the user hits the "Secure Attention Key". Required for
1792 * super-paranoid applications --- see the Orange Book for more details.
1794 * This code could be nicer; ideally it should send a HUP, wait a few
1795 * seconds, then send a INT, and then a KILL signal. But you then
1796 * have to coordinate with the init process, since all processes associated
1797 * with the current tty must be dead before the new getty is allowed
1800 void do_SAK( struct tty_struct
*tty
)
1805 struct task_struct
*p
;
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
);
1819 if ((p
->tty
== tty
) ||
1820 ((session
> 0) && (p
->session
== session
)))
1821 send_sig(SIGKILL
, p
, 1);
1822 else if (p
->files
) {
1823 for (i
=0; i
< p
->files
->max_fds
; i
++) {
1824 filp
= fcheck_task(p
, i
);
1825 if (filp
&& (filp
->f_op
== &tty_fops
) &&
1826 (filp
->private_data
== tty
)) {
1827 send_sig(SIGKILL
, p
, 1);
1833 read_unlock(&tasklist_lock
);
1838 * This routine is called out of the software interrupt to flush data
1839 * from the flip buffer to the line discipline.
1841 static void flush_to_ldisc(void *private_
)
1843 struct tty_struct
*tty
= (struct tty_struct
*) private_
;
1847 unsigned long flags
;
1849 if (tty
->flip
.buf_num
) {
1850 cp
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1851 fp
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1852 tty
->flip
.buf_num
= 0;
1854 save_flags(flags
); cli();
1855 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1856 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1858 cp
= tty
->flip
.char_buf
;
1859 fp
= tty
->flip
.flag_buf
;
1860 tty
->flip
.buf_num
= 1;
1862 save_flags(flags
); cli();
1863 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1864 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1866 count
= tty
->flip
.count
;
1867 tty
->flip
.count
= 0;
1868 restore_flags(flags
);
1870 tty
->ldisc
.receive_buf(tty
, cp
, fp
, count
);
1874 * Routine which returns the baud rate of the tty
1878 * This is used to figure out the divisor speeds and the timeouts
1880 static int baud_table
[] = {
1881 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1882 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
1884 int tty_get_baud_rate(struct tty_struct
*tty
)
1886 unsigned int cflag
, i
;
1888 cflag
= tty
->termios
->c_cflag
;
1894 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1898 if (i
==15 && tty
->alt_speed
) {
1900 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1903 return(tty
->alt_speed
);
1906 return baud_table
[i
];
1909 void tty_flip_buffer_push(struct tty_struct
*tty
)
1911 if (tty
->low_latency
)
1912 flush_to_ldisc((void *) tty
);
1914 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1918 * This subroutine initializes a tty structure.
1920 static void initialize_tty_struct(struct tty_struct
*tty
)
1922 memset(tty
, 0, sizeof(struct tty_struct
));
1923 tty
->magic
= TTY_MAGIC
;
1924 tty
->ldisc
= ldiscs
[N_TTY
];
1926 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1927 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1928 tty
->flip
.tqueue
.routine
= flush_to_ldisc
;
1929 tty
->flip
.tqueue
.data
= tty
;
1930 tty
->flip
.pty_sem
= MUTEX
;
1931 tty
->tq_hangup
.routine
= do_tty_hangup
;
1932 tty
->tq_hangup
.data
= tty
;
1936 * The default put_char routine if the driver did not define one.
1938 void tty_default_put_char(struct tty_struct
*tty
, unsigned char ch
)
1940 tty
->driver
.write(tty
, 0, &ch
, 1);
1944 * Called by a tty driver to register itself.
1946 int tty_register_driver(struct tty_driver
*driver
)
1950 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
1953 error
= register_chrdev(driver
->major
, driver
->name
, &tty_fops
);
1956 else if(driver
->major
== 0)
1957 driver
->major
= error
;
1959 if (!driver
->put_char
)
1960 driver
->put_char
= tty_default_put_char
;
1963 driver
->next
= tty_drivers
;
1964 if (tty_drivers
) tty_drivers
->prev
= driver
;
1965 tty_drivers
= driver
;
1967 proc_tty_register_driver(driver
);
1972 * Called by a tty driver to unregister itself.
1974 int tty_unregister_driver(struct tty_driver
*driver
)
1977 struct tty_driver
*p
;
1979 const char *othername
= NULL
;
1981 if (*driver
->refcount
)
1984 for (p
= tty_drivers
; p
; p
= p
->next
) {
1987 else if (p
->major
== driver
->major
)
1988 othername
= p
->name
;
1994 if (othername
== NULL
) {
1995 retval
= unregister_chrdev(driver
->major
, driver
->name
);
1999 register_chrdev(driver
->major
, othername
, &tty_fops
);
2002 driver
->prev
->next
= driver
->next
;
2004 tty_drivers
= driver
->next
;
2007 driver
->next
->prev
= driver
->prev
;
2009 proc_tty_unregister_driver(driver
);
2015 * Initialize the console device. This is called *early*, so
2016 * we can't necessarily depend on lots of kernel help here.
2017 * Just do some early initializations, and do the complex setup
2020 long __init
console_init(long kmem_start
, long kmem_end
)
2022 /* Setup the default TTY line discipline. */
2023 memset(ldiscs
, 0, sizeof(ldiscs
));
2024 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);
2027 * Set up the standard termios. Individual tty drivers may
2028 * deviate from this; this is used as a template.
2030 memset(&tty_std_termios
, 0, sizeof(struct termios
));
2031 memcpy(tty_std_termios
.c_cc
, INIT_C_CC
, NCCS
);
2032 tty_std_termios
.c_iflag
= ICRNL
| IXON
;
2033 tty_std_termios
.c_oflag
= OPOST
| ONLCR
;
2034 tty_std_termios
.c_cflag
= B38400
| CS8
| CREAD
| HUPCL
;
2035 tty_std_termios
.c_lflag
= ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
|
2036 ECHOCTL
| ECHOKE
| IEXTEN
;
2039 * set up the console device so that later boot sequences can
2040 * inform about problems etc..
2043 kmem_start
= con_init(kmem_start
);
2045 #ifdef CONFIG_SERIAL_CONSOLE
2047 kmem_start
= console_8xx_init(kmem_start
, kmem_end
);
2049 kmem_start
= serial_console_init(kmem_start
, kmem_end
);
2050 #endif /* CONFIG_8xx */
2055 static struct tty_driver dev_tty_driver
, dev_syscons_driver
;
2056 #ifdef CONFIG_UNIX98_PTYS
2057 static struct tty_driver dev_ptmx_driver
;
2060 static struct tty_driver dev_console_driver
;
2064 * Ok, now we can initialize the rest of the tty devices and can count
2065 * on memory allocations, interrupts etc..
2067 __initfunc(int tty_init(void))
2069 if (sizeof(struct tty_struct
) > PAGE_SIZE
)
2070 panic("size of tty structure > PAGE_SIZE!");
2073 * dev_tty_driver and dev_console_driver are actually magic
2074 * devices which get redirected at open time. Nevertheless,
2075 * we register them so that register_chrdev is called
2078 memset(&dev_tty_driver
, 0, sizeof(struct tty_driver
));
2079 dev_tty_driver
.magic
= TTY_DRIVER_MAGIC
;
2080 dev_tty_driver
.driver_name
= "/dev/tty";
2081 dev_tty_driver
.name
= dev_tty_driver
.driver_name
+ 5;
2082 dev_tty_driver
.name_base
= 0;
2083 dev_tty_driver
.major
= TTYAUX_MAJOR
;
2084 dev_tty_driver
.minor_start
= 0;
2085 dev_tty_driver
.num
= 1;
2086 dev_tty_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2087 dev_tty_driver
.subtype
= SYSTEM_TYPE_TTY
;
2089 if (tty_register_driver(&dev_tty_driver
))
2090 panic("Couldn't register /dev/tty driver\n");
2092 dev_syscons_driver
= dev_tty_driver
;
2093 dev_syscons_driver
.driver_name
= "/dev/console";
2094 dev_syscons_driver
.name
= dev_syscons_driver
.driver_name
+ 5;
2095 dev_syscons_driver
.major
= TTYAUX_MAJOR
;
2096 dev_syscons_driver
.minor_start
= 1;
2097 dev_syscons_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2098 dev_syscons_driver
.subtype
= SYSTEM_TYPE_SYSCONS
;
2100 if (tty_register_driver(&dev_syscons_driver
))
2101 panic("Couldn't register /dev/console driver\n");
2103 #ifdef CONFIG_UNIX98_PTYS
2104 dev_ptmx_driver
= dev_tty_driver
;
2105 dev_ptmx_driver
.driver_name
= "/dev/ptmx";
2106 dev_ptmx_driver
.name
= dev_ptmx_driver
.driver_name
+ 5;
2107 dev_ptmx_driver
.major
= MAJOR(PTMX_DEV
);
2108 dev_ptmx_driver
.minor_start
= MINOR(PTMX_DEV
);
2109 dev_ptmx_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2110 dev_ptmx_driver
.subtype
= SYSTEM_TYPE_SYSPTMX
;
2112 if (tty_register_driver(&dev_ptmx_driver
))
2113 panic("Couldn't register /dev/ptmx driver\n");
2117 dev_console_driver
= dev_tty_driver
;
2118 dev_console_driver
.driver_name
= "/dev/tty0";
2119 dev_console_driver
.name
= dev_console_driver
.driver_name
+ 5;
2120 dev_console_driver
.major
= TTY_MAJOR
;
2121 dev_console_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2122 dev_console_driver
.subtype
= SYSTEM_TYPE_CONSOLE
;
2124 if (tty_register_driver(&dev_console_driver
))
2125 panic("Couldn't register /dev/tty0 driver\n");
2129 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2132 #ifdef CONFIG_SERIAL
2135 #ifdef CONFIG_MAC_SERIAL
2138 #ifdef CONFIG_ROCKETPORT
2141 #ifdef CONFIG_MVME16x
2144 #ifdef CONFIG_CYCLADES
2147 #ifdef CONFIG_STALLION
2150 #ifdef CONFIG_ISTALLION
2156 #ifdef CONFIG_DIGIEPCA
2159 #ifdef CONFIG_RISCOM8
2162 #ifdef CONFIG_SPECIALIX
2167 #endif /* CONFIG_8xx */