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 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>
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>
100 extern void con_init_devfs (void);
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 */
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
);
139 extern int sx_init (void);
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);
145 #ifdef CONFIG_SERIAL167
146 extern int serial167_init(void);
147 extern long serial167_console_init(void);
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 */
154 extern void hwc_console_init(void);
157 extern void con3215_init(void);
158 #endif /* CONFIG_3215 */
162 #define MIN(a,b) ((a) < (b) ? (a) : (b))
166 * This routine returns the name of tty.
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");
177 idx
+ tty
->driver
.name_base
);
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
,
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";
200 printk(badtty
, kdevname(device
), routine
);
203 if (tty
->magic
!= TTY_MAGIC
) {
204 printk(badmagic
, kdevname(device
), routine
);
211 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
213 #ifdef CHECK_TTY_COUNT
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
)
223 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
224 tty
->driver
.subtype
== PTY_TYPE_SLAVE
&&
225 tty
->link
&& tty
->link
->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
);
236 int tty_register_ldisc(int disc
, struct tty_ldisc
*new_ldisc
)
238 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
242 ldiscs
[disc
] = *new_ldisc
;
243 ldiscs
[disc
].flags
|= LDISC_FLAG_DEFINED
;
244 ldiscs
[disc
].num
= disc
;
246 memset(&ldiscs
[disc
], 0, sizeof(struct tty_ldisc
));
251 /* Set the discipline of a tty line. */
252 static int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
255 struct tty_ldisc o_ldisc
;
258 if ((ldisc
< N_TTY
) || (ldisc
>= NR_LDISCS
))
260 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
261 /* Cyrus Durgin <cider@speakeasy.org> */
262 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
)) {
264 sprintf(modname
, "tty-ldisc-%d", ldisc
);
265 request_module (modname
);
267 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
))
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
;
284 retval
= (tty
->ldisc
.open
)(tty
);
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
);
295 panic("Couldn't open N_TTY ldisc for "
297 tty_name(tty
, buf
), r
);
301 if (tty
->ldisc
.num
!= o_ldisc
.num
&& tty
->driver
.set_ldisc
)
302 tty
->driver
.set_ldisc(tty
);
307 * This routine returns a tty driver structure, given a device number
309 struct tty_driver
*get_tty_driver(kdev_t device
)
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
)
320 if (minor
< p
->minor_start
)
322 if (minor
>= p
->minor_start
+ p
->num
)
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
)
338 if (tty
->pgrp
<= 0) {
339 printk("tty_check_change: tty->pgrp <= 0!\n");
342 if (current
->pgrp
== tty
->pgrp
)
344 if (is_ignored(SIGTTOU
))
346 if (is_orphaned_pgrp(current
->pgrp
))
348 (void) kill_pg(current
->pgrp
,SIGTTOU
,1);
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
)
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
)
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
)
387 static struct file_operations tty_fops
= {
394 release
: tty_release
,
398 static struct file_operations hung_up_tty_fops
= {
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
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
;
420 int closecount
= 0, n
;
425 /* inuse_filps is protected by the single kernel lock */
428 check_tty_count(tty
, "do_tty_hangup");
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
);
434 if (filp
->f_dentry
->d_inode
->i_rdev
== CONSOLE_DEV
||
435 filp
->f_dentry
->d_inode
->i_rdev
== SYSCONS_DEV
) {
439 if (filp
->f_op
!= &tty_fops
)
442 tty_fasync(-1, filp
, 0); /* can't block */
443 filp
->f_op
= &hung_up_tty_fops
;
447 /* FIXME! What are the locking issues here? This may me overdoing things.. */
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
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
);
479 printk("do_tty_hangup: N_TTY open: error %d\n",
484 read_lock(&tasklist_lock
);
486 if ((tty
->session
> 0) && (p
->session
== tty
->session
) &&
488 send_sig(SIGHUP
,p
,1);
489 send_sig(SIGCONT
,p
,1);
491 p
->tty_old_pgrp
= tty
->pgrp
;
496 read_unlock(&tasklist_lock
);
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.
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
);
517 void tty_hangup(struct tty_struct
* tty
)
519 #ifdef TTY_DEBUG_HANGUP
522 printk("%s hangup...\n", tty_name(tty
, buf
));
524 queue_task(&tty
->tq_hangup
, &tq_scheduler
);
527 void tty_vhangup(struct tty_struct
* tty
)
529 #ifdef TTY_DEBUG_HANGUP
532 printk("%s vhangup...\n", tty_name(tty
, buf
));
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
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
;
562 tty_pgrp
= tty
->pgrp
;
563 if (on_exit
&& tty
->driver
.type
!= TTY_DRIVER_TYPE_PTY
)
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
);
573 kill_pg(tty_pgrp
, SIGHUP
, on_exit
);
575 kill_pg(tty_pgrp
, SIGCONT
, on_exit
);
578 current
->tty_old_pgrp
= 0;
582 read_lock(&tasklist_lock
);
584 if (p
->session
== current
->session
)
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
)
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
)
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
,
631 struct tty_struct
* tty
;
634 /* Can't seek (pread) on ttys. */
635 if (ppos
!= &file
->f_pos
)
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"))
642 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
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. */
650 if ((inode
->i_rdev
!= CONSOLE_DEV
) && /* don't stop on /dev/console */
652 (current
->tty
== tty
) &&
653 (tty
->pgrp
!= current
->pgrp
))
654 if (is_ignored(SIGTTIN
) || is_orphaned_pgrp(current
->pgrp
))
657 (void) kill_pg(current
->pgrp
, SIGTTIN
, 1);
663 i
= (tty
->ldisc
.read
)(tty
,file
,buf
,count
);
668 inode
->i_atime
= CURRENT_TIME
;
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
,
680 const unsigned char *buf
,
683 ssize_t ret
= 0, written
= 0;
684 struct inode
*inode
= file
->f_dentry
->d_inode
;
686 if (down_interruptible(&inode
->i_sem
)) {
689 if ( test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
) ) {
691 written
= write(tty
, file
, buf
, count
);
695 unsigned long size
= PAGE_SIZE
*2;
699 ret
= write(tty
, file
, buf
, size
);
709 if (signal_pending(current
))
711 if (current
->need_resched
)
716 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
;
724 static ssize_t
tty_write(struct file
* file
, const char * buf
, size_t count
,
728 struct tty_struct
* tty
;
731 /* Can't seek (pwrite) on ttys. */
732 if (ppos
!= &file
->f_pos
)
736 * For now, we redirect writes from /dev/console as
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
)
746 tty
= (struct tty_struct
*)file
->private_data
;
747 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_write"))
749 if (!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
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
))
756 if (!is_ignored(SIGTTOU
)) {
757 (void) kill_pg(current
->pgrp
, SIGTTOU
, 1);
762 if (!tty
->ldisc
.write
)
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
)
776 static void up_tty_sem(int index
)
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
;
798 driver
= get_tty_driver(device
);
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.
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.)
826 tty
= (struct tty_struct
*) get_zeroed_page(GFP_KERNEL
);
829 initialize_tty_struct(tty
);
830 tty
->device
= device
;
831 tty
->driver
= *driver
;
833 tp_loc
= &driver
->termios
[idx
];
835 tp
= (struct termios
*) kmalloc(sizeof(struct termios
),
839 *tp
= driver
->init_termios
;
842 ltp_loc
= &driver
->termios_locked
[idx
];
844 ltp
= (struct termios
*) kmalloc(sizeof(struct termios
),
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
);
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
];
862 o_tp
= (struct termios
*)
863 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
866 *o_tp
= driver
->other
->init_termios
;
869 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
871 o_ltp
= (struct termios
*)
872 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
875 memset(o_ltp
, 0, sizeof(struct termios
));
879 * Everything allocated ... set up the o_tty structure.
881 driver
->other
->table
[idx
] = o_tty
;
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
)
892 /* Establish the links in both directions */
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
;
908 tty
->termios
= *tp_loc
;
909 tty
->termios_locked
= *ltp_loc
;
910 (*driver
->refcount
)++;
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
);
921 goto release_mem_out
;
923 if (o_tty
&& o_tty
->ldisc
.open
) {
924 retval
= (o_tty
->ldisc
.open
)(o_tty
);
926 if (tty
->ldisc
.close
)
927 (tty
->ldisc
.close
)(tty
);
928 goto release_mem_out
;
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.
940 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
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.
957 tty
->driver
= *driver
; /* N.B. why do this every time?? */
962 /* All paths come through here to release the semaphore */
967 /* Release locally allocated memory ... nothing placed in slots */
970 kfree_s(o_tp
, sizeof(struct termios
));
972 free_page((unsigned long) o_tty
);
974 kfree_s(ltp
, sizeof(struct termios
));
976 kfree_s(tp
, sizeof(struct termios
));
977 free_page((unsigned long) tty
);
983 /* call the tty release_mem routine to clean out this slot */
985 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
);
986 release_mem(tty
, idx
);
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
;
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
));
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
));
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
;
1037 tty
= (struct tty_struct
*)filp
->private_data
;
1038 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "release_dev"))
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
);
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
));
1056 if (tty
!= tty
->driver
.table
[idx
]) {
1057 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1058 idx
, kdevname(tty
->device
));
1061 if (tty
->termios
!= tty
->driver
.termios
[idx
]) {
1062 printk("release_dev: driver.termios[%d] not termios "
1064 idx
, kdevname(tty
->device
));
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
));
1075 #ifdef TTY_DEBUG_HANGUP
1076 printk("release_dev of %s (tty count=%d)...", tty_name(tty
, buf
),
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 ("
1085 idx
, kdevname(tty
->device
));
1088 if (o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) {
1089 printk("release_dev: other->termios[%d] not o_termios "
1091 idx
, kdevname(tty
->device
));
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
));
1101 if (o_tty
->link
!= tty
) {
1102 printk("release_dev: bad pty pointers\n");
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
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,
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.
1129 tty_closing
= tty
->count
<= 1;
1130 o_tty_closing
= o_tty
&&
1131 (o_tty
->count
<= (pty_master
? 1 : 0));
1135 if (waitqueue_active(&tty
->read_wait
)) {
1136 wake_up(&tty
->read_wait
);
1139 if (waitqueue_active(&tty
->write_wait
)) {
1140 wake_up(&tty
->write_wait
);
1144 if (o_tty_closing
) {
1145 if (waitqueue_active(&o_tty
->read_wait
)) {
1146 wake_up(&o_tty
->read_wait
);
1149 if (waitqueue_active(&o_tty
->write_wait
)) {
1150 wake_up(&o_tty
->write_wait
);
1157 printk("release_dev: %s: read/write wait queue active!\n",
1158 tty_name(tty
, buf
));
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.
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
));
1174 if (--tty
->count
< 0) {
1175 printk("release_dev: bad tty->count (%d) for %s\n",
1176 tty
->count
, tty_name(tty
, buf
));
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.
1198 set_bit(TTY_CLOSING
, &tty
->flags
);
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
);
1212 if (p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
))
1215 read_unlock(&tasklist_lock
);
1217 if (redirect
== tty
|| (o_tty
&& redirect
== o_tty
))
1221 /* check whether both sides are closing ... */
1222 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1225 #ifdef TTY_DEBUG_HANGUP
1226 printk("freeing tty structure...");
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
;
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
;
1273 unsigned short saved_flags
;
1276 saved_flags
= filp
->f_flags
;
1278 noctty
= filp
->f_flags
& O_NOCTTY
;
1279 device
= inode
->i_rdev
;
1280 if (device
== TTY_DEV
) {
1283 device
= current
->tty
->device
;
1284 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1288 if (device
== CONSOLE_DEV
) {
1289 extern int fg_console
;
1290 device
= MKDEV(TTY_MAJOR
, fg_console
+ 1);
1294 if (device
== SYSCONS_DEV
) {
1295 struct console
*c
= console_drivers
;
1296 while(c
&& !c
->device
)
1300 device
= c
->device(c
);
1301 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/console block */
1305 if (device
== PTMX_DEV
) {
1306 #ifdef CONFIG_UNIX98_PTYS
1308 /* find a free pty. */
1310 struct tty_driver
*driver
;
1312 /* find a device that is not in use. */
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
;
1319 device
= MKDEV(driver
->major
, minor
);
1320 if (!init_dev(device
, &tty
)) goto ptmx_found
; /* ok! */
1323 return -EIO
; /* no free ptys */
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
);
1333 #else /* CONFIG_UNIX_98_PTYS */
1337 #endif /* CONFIG_UNIX_98_PTYS */
1340 retval
= init_dev(device
, &tty
);
1344 #ifdef CONFIG_UNIX98_PTYS
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
)
1353 #ifdef TTY_DEBUG_HANGUP
1354 printk("opening %s...", tty_name(tty
, buf
));
1356 if (tty
->driver
.open
)
1357 retval
= tty
->driver
.open(tty
, filp
);
1360 filp
->f_flags
= saved_flags
;
1362 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser())
1366 #ifdef TTY_DEBUG_HANGUP
1367 printk("error %d in opening %s...", retval
,
1368 tty_name(tty
, buf
));
1372 if (retval
!= -ERESTARTSYS
)
1374 if (signal_pending(current
))
1378 * Need to reset f_op in case a hangup happened.
1380 filp
->f_op
= &tty_fops
;
1386 tty
->session
== 0) {
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;
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
));
1410 static int tty_release(struct inode
* inode
, struct file
* filp
)
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"))
1425 if (tty
->ldisc
.poll
)
1426 return (tty
->ldisc
.poll
)(tty
, filp
, wait
);
1430 static int tty_fasync(int fd
, struct file
* filp
, int on
)
1432 struct tty_struct
* tty
;
1435 tty
= (struct tty_struct
*)filp
->private_data
;
1436 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_fasync"))
1439 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
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
;
1452 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
1453 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
1458 static int tiocsti(struct tty_struct
*tty
, char * arg
)
1462 if ((current
->tty
!= tty
) && !suser())
1464 if (get_user(ch
, arg
))
1466 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
, 1);
1470 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize
* arg
)
1472 if (copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
)))
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
)))
1484 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
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
;
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
) {
1507 redirect
= real_tty
;
1512 static int fionbio(struct file
*file
, int *arg
)
1516 if (get_user(nonblock
, arg
))
1520 file
->f_flags
|= O_NONBLOCK
;
1522 file
->f_flags
&= ~O_NONBLOCK
;
1526 static int tiocsctty(struct tty_struct
*tty
, int arg
)
1528 if (current
->leader
&&
1529 (current
->session
== tty
->session
))
1532 * The process must be a session leader and
1533 * not have a controlling tty already.
1535 if (!current
->leader
|| current
->tty
)
1537 if (tty
->session
> 0) {
1539 * This tty is already the controlling
1540 * tty for another session group!
1542 if ((arg
== 1) && suser()) {
1546 struct task_struct
*p
;
1548 read_lock(&tasklist_lock
);
1552 read_unlock(&tasklist_lock
);
1558 task_unlock(current
);
1559 current
->tty_old_pgrp
= 0;
1560 tty
->session
= current
->session
;
1561 tty
->pgrp
= current
->pgrp
;
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
)
1573 return put_user(real_tty
->pgrp
, arg
);
1576 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1579 int retval
= tty_check_change(real_tty
);
1585 if (!current
->tty
||
1586 (current
->tty
!= real_tty
) ||
1587 (real_tty
->session
!= current
->session
))
1589 get_user(pgrp
, (pid_t
*) arg
);
1592 if (session_of_pgrp(pgrp
) != current
->session
)
1594 real_tty
->pgrp
= pgrp
;
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
)
1606 if (real_tty
->session
<= 0)
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
)))
1618 static int tiocsetd(struct tty_struct
*tty
, int *arg
)
1622 retval
= get_user(ldisc
, arg
);
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
))
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
;
1650 tty
= (struct tty_struct
*)file
->private_data
;
1651 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_ioctl"))
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
) {
1666 if (tty
->driver
.ioctl
)
1667 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1670 /* These two ioctl's always return success; even if */
1671 /* the driver doesn't support them. */
1674 if (!tty
->driver
.ioctl
)
1676 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1677 if (retval
== -ENOIOCTLCMD
)
1684 * Factor out some common prep work
1692 retval
= tty_check_change(tty
);
1695 if (cmd
!= TIOCCBRK
) {
1696 tty_wait_until_sent(tty
, 0);
1697 if (signal_pending(current
))
1705 return tiocsti(tty
, (char *)arg
);
1707 return tiocgwinsz(tty
, (struct winsize
*) arg
);
1709 return tiocswinsz(tty
, real_tty
, (struct winsize
*) arg
);
1711 return tioccons(inode
, tty
, real_tty
);
1713 return fionbio(file
, (int *) arg
);
1715 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1718 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1721 if (current
->tty
!= tty
)
1723 if (current
->leader
)
1724 disassociate_ctty(0);
1726 current
->tty
= NULL
;
1727 task_unlock(current
);
1730 return tiocsctty(tty
, arg
);
1732 return tiocgpgrp(tty
, real_tty
, (pid_t
*) arg
);
1734 return tiocspgrp(tty
, real_tty
, (pid_t
*) arg
);
1736 return tiocgsid(tty
, real_tty
, (pid_t
*) arg
);
1738 return put_user(tty
->ldisc
.num
, (int *) arg
);
1740 return tiocsetd(tty
, (int *) arg
);
1743 return tioclinux(tty
, arg
);
1745 case TIOCTTYGSTRUCT
:
1746 return tiocttygstruct(tty
, (struct tty_struct
*) arg
);
1751 case TIOCSBRK
: /* Turn break on, unconditionally */
1752 tty
->driver
.break_ctl(tty
, -1);
1755 case TIOCCBRK
: /* Turn break off, unconditionally */
1756 tty
->driver
.break_ctl(tty
, 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
1765 return send_break(tty
, HZ
/4);
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
)
1775 if (tty
->ldisc
.ioctl
) {
1776 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
);
1777 if (retval
!= -ENOIOCTLCMD
)
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
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
)
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 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);
1833 read_unlock(&p
->files
->file_lock
);
1836 read_unlock(&tasklist_lock
);
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_
;
1850 unsigned long flags
;
1852 if (test_bit(TTY_DONT_FLIP
, &tty
->flags
)) {
1853 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
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
;
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,
1890 76800, 153600, 307200, 614400, 921600
1892 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1893 2500000, 3000000, 3500000, 4000000
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
;
1908 if (i
< 1 || i
+15 >= n_baud_table
)
1909 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1913 if (i
==15 && tty
->alt_speed
) {
1915 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
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
);
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
];
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
,
1969 #ifdef CONFIG_DEVFS_FS
1970 umode_t mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
;
1973 struct tty_struct tty
;
1976 tty
.driver
= *driver
;
1977 tty
.device
= MKDEV (driver
->major
, minor
);
1978 switch (tty
.device
) {
1981 mode
|= S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
1984 if (driver
->major
== PTY_MASTER_MAJOR
)
1985 flags
|= DEVFS_FL_AUTO_OWNER
;
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
);
1994 # ifdef CONFIG_UNIX98_PTYS
1995 if ( (driver
->major
>= UNIX98_PTY_SLAVE_MAJOR
) &&
1996 (driver
->major
< UNIX98_PTY_SLAVE_MAJOR
+ UNIX98_NR_MAJORS
) ) {
2001 devfs_register (NULL
, tty_name (&tty
, buf
), 0,flags
| DEVFS_FL_DEFAULT
,
2002 driver
->major
, minor
, mode
, uid
, gid
,
2004 #endif /* CONFIG_DEVFS_FS */
2007 void tty_unregister_devfs (struct tty_driver
*driver
, unsigned minor
)
2009 #ifdef CONFIG_DEVFS_FS
2011 struct tty_struct tty
;
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
)
2035 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
2038 error
= devfs_register_chrdev(driver
->major
, driver
->name
, &tty_fops
);
2041 else if(driver
->major
== 0)
2042 driver
->major
= error
;
2044 if (!driver
->put_char
)
2045 driver
->put_char
= tty_default_put_char
;
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
);
2061 * Called by a tty driver to unregister itself.
2063 int tty_unregister_driver(struct tty_driver
*driver
)
2066 struct tty_driver
*p
;
2069 const char *othername
= NULL
;
2071 if (*driver
->refcount
)
2074 for (p
= tty_drivers
; p
; p
= p
->next
) {
2077 else if (p
->major
== driver
->major
)
2078 othername
= p
->name
;
2084 if (othername
== NULL
) {
2085 retval
= devfs_unregister_chrdev(driver
->major
, driver
->name
);
2089 devfs_register_chrdev(driver
->major
, othername
, &tty_fops
);
2092 driver
->prev
->next
= driver
->next
;
2094 tty_drivers
= 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
];
2107 driver
->termios
[i
] = NULL
;
2108 kfree_s(tp
, sizeof(struct termios
));
2110 tp
= driver
->termios_locked
[i
];
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
);
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
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..
2153 #ifdef CONFIG_SERIAL_CONSOLE
2154 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
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();
2162 #if defined(CONFIG_SERIAL167)
2163 serial167_console_init();
2174 static struct tty_driver dev_tty_driver
, dev_syscons_driver
;
2175 #ifdef CONFIG_UNIX98_PTYS
2176 static struct tty_driver dev_ptmx_driver
;
2179 static struct tty_driver dev_console_driver
;
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
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.
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");
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");
2255 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2258 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2261 #ifdef CONFIG_COMPUTONE
2264 #ifdef CONFIG_MAC_SERIAL
2267 #ifdef CONFIG_ROCKETPORT
2270 #ifdef CONFIG_SERIAL167
2273 #ifdef CONFIG_CYCLADES
2276 #ifdef CONFIG_STALLION
2279 #ifdef CONFIG_ISTALLION
2285 #ifdef CONFIG_DIGIEPCA
2288 #ifdef CONFIG_RISCOM8
2291 #ifdef CONFIG_SPECIALIX
2300 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2302 #endif /* CONFIG_8xx */
2304 #ifdef CONFIG_MOXA_SMARTIO
2307 #ifdef CONFIG_MOXA_INTELLIO