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 */
153 #ifdef CONFIG_SGI_SERIAL
154 extern void sgi_serial_console_init(void);
157 extern void hwc_console_init(void);
160 extern void con3215_init(void);
161 #endif /* CONFIG_3215 */
165 #define MIN(a,b) ((a) < (b) ? (a) : (b))
169 * This routine returns the name of tty.
172 _tty_make_name(struct tty_struct
*tty
, const char *name
, char *buf
)
174 int idx
= (tty
)?MINOR(tty
->device
) - tty
->driver
.minor_start
:0;
176 if (!tty
) /* Hmm. NULL pointer. That's fun. */
177 strcpy(buf
, "NULL tty");
180 idx
+ tty
->driver
.name_base
);
185 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
186 (tty)->driver.name_base)
188 char *tty_name(struct tty_struct
*tty
, char *buf
)
190 return _tty_make_name(tty
, (tty
)?tty
->driver
.name
:NULL
, buf
);
193 inline int tty_paranoia_check(struct tty_struct
*tty
, kdev_t device
,
196 #ifdef TTY_PARANOIA_CHECK
197 static const char *badmagic
=
198 "Warning: bad magic number for tty struct (%s) in %s\n";
199 static const char *badtty
=
200 "Warning: null TTY for (%s) in %s\n";
203 printk(badtty
, kdevname(device
), routine
);
206 if (tty
->magic
!= TTY_MAGIC
) {
207 printk(badmagic
, kdevname(device
), routine
);
214 static int check_tty_count(struct tty_struct
*tty
, const char *routine
)
216 #ifdef CHECK_TTY_COUNT
221 for(p
= tty
->tty_files
.next
; p
!= &tty
->tty_files
; p
= p
->next
) {
222 if(list_entry(p
, struct file
, f_list
)->private_data
== tty
)
226 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
227 tty
->driver
.subtype
== PTY_TYPE_SLAVE
&&
228 tty
->link
&& tty
->link
->count
)
230 if (tty
->count
!= count
) {
231 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
232 kdevname(tty
->device
), tty
->count
, count
, routine
);
239 int tty_register_ldisc(int disc
, struct tty_ldisc
*new_ldisc
)
241 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
245 ldiscs
[disc
] = *new_ldisc
;
246 ldiscs
[disc
].flags
|= LDISC_FLAG_DEFINED
;
247 ldiscs
[disc
].num
= disc
;
249 memset(&ldiscs
[disc
], 0, sizeof(struct tty_ldisc
));
254 /* Set the discipline of a tty line. */
255 static int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
258 struct tty_ldisc o_ldisc
;
261 if ((ldisc
< N_TTY
) || (ldisc
>= NR_LDISCS
))
263 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
264 /* Cyrus Durgin <cider@speakeasy.org> */
265 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
)) {
267 sprintf(modname
, "tty-ldisc-%d", ldisc
);
268 request_module (modname
);
270 if (!(ldiscs
[ldisc
].flags
& LDISC_FLAG_DEFINED
))
273 if (tty
->ldisc
.num
== ldisc
)
274 return 0; /* We are already in the desired discipline */
275 o_ldisc
= tty
->ldisc
;
277 tty_wait_until_sent(tty
, 0);
279 /* Shutdown the current discipline. */
280 if (tty
->ldisc
.close
)
281 (tty
->ldisc
.close
)(tty
);
283 /* Now set up the new line discipline. */
284 tty
->ldisc
= ldiscs
[ldisc
];
285 tty
->termios
->c_line
= ldisc
;
287 retval
= (tty
->ldisc
.open
)(tty
);
289 tty
->ldisc
= o_ldisc
;
290 tty
->termios
->c_line
= tty
->ldisc
.num
;
291 if (tty
->ldisc
.open
&& (tty
->ldisc
.open(tty
) < 0)) {
292 tty
->ldisc
= ldiscs
[N_TTY
];
293 tty
->termios
->c_line
= N_TTY
;
294 if (tty
->ldisc
.open
) {
295 int r
= tty
->ldisc
.open(tty
);
298 panic("Couldn't open N_TTY ldisc for "
300 tty_name(tty
, buf
), r
);
304 if (tty
->ldisc
.num
!= o_ldisc
.num
&& tty
->driver
.set_ldisc
)
305 tty
->driver
.set_ldisc(tty
);
310 * This routine returns a tty driver structure, given a device number
312 struct tty_driver
*get_tty_driver(kdev_t device
)
315 struct tty_driver
*p
;
317 minor
= MINOR(device
);
318 major
= MAJOR(device
);
320 for (p
= tty_drivers
; p
; p
= p
->next
) {
321 if (p
->major
!= major
)
323 if (minor
< p
->minor_start
)
325 if (minor
>= p
->minor_start
+ p
->num
)
333 * If we try to write to, or set the state of, a terminal and we're
334 * not in the foreground, send a SIGTTOU. If the signal is blocked or
335 * ignored, go ahead and perform the operation. (POSIX 7.2)
337 int tty_check_change(struct tty_struct
* tty
)
339 if (current
->tty
!= tty
)
341 if (tty
->pgrp
<= 0) {
342 printk("tty_check_change: tty->pgrp <= 0!\n");
345 if (current
->pgrp
== tty
->pgrp
)
347 if (is_ignored(SIGTTOU
))
349 if (is_orphaned_pgrp(current
->pgrp
))
351 (void) kill_pg(current
->pgrp
,SIGTTOU
,1);
355 static ssize_t
hung_up_tty_read(struct file
* file
, char * buf
,
356 size_t count
, loff_t
*ppos
)
358 /* Can't seek (pread) on ttys. */
359 if (ppos
!= &file
->f_pos
)
364 static ssize_t
hung_up_tty_write(struct file
* file
, const char * buf
,
365 size_t count
, loff_t
*ppos
)
367 /* Can't seek (pwrite) on ttys. */
368 if (ppos
!= &file
->f_pos
)
373 /* No kernel lock held - none needed ;) */
374 static unsigned int hung_up_tty_poll(struct file
* filp
, poll_table
* wait
)
376 return POLLIN
| POLLOUT
| POLLERR
| POLLHUP
| POLLRDNORM
| POLLWRNORM
;
379 static int hung_up_tty_ioctl(struct inode
* inode
, struct file
* file
,
380 unsigned int cmd
, unsigned long arg
)
382 return cmd
== TIOCSPGRP
? -ENOTTY
: -EIO
;
385 static loff_t
tty_lseek(struct file
* file
, loff_t offset
, int orig
)
390 static struct file_operations tty_fops
= {
397 release
: tty_release
,
401 static struct file_operations hung_up_tty_fops
= {
403 read
: hung_up_tty_read
,
404 write
: hung_up_tty_write
,
405 poll
: hung_up_tty_poll
,
406 ioctl
: hung_up_tty_ioctl
,
407 release
: tty_release
,
411 * This can be called through the "tq_scheduler"
412 * task-list. That is process synchronous, but
413 * doesn't hold any locks, so we need to make
414 * sure we have the appropriate locks for what
417 void do_tty_hangup(void *data
)
419 struct tty_struct
*tty
= (struct tty_struct
*) data
;
420 struct file
* cons_filp
= NULL
;
421 struct task_struct
*p
;
423 int closecount
= 0, n
;
428 /* inuse_filps is protected by the single kernel lock */
431 check_tty_count(tty
, "do_tty_hangup");
433 for (l
= tty
->tty_files
.next
; l
!= &tty
->tty_files
; l
= l
->next
) {
434 struct file
* filp
= list_entry(l
, struct file
, f_list
);
437 if (filp
->f_dentry
->d_inode
->i_rdev
== CONSOLE_DEV
||
438 filp
->f_dentry
->d_inode
->i_rdev
== SYSCONS_DEV
) {
442 if (filp
->f_op
!= &tty_fops
)
445 tty_fasync(-1, filp
, 0); /* can't block */
446 filp
->f_op
= &hung_up_tty_fops
;
450 /* FIXME! What are the locking issues here? This may me overdoing things.. */
454 save_flags(flags
); cli();
455 if (tty
->ldisc
.flush_buffer
)
456 tty
->ldisc
.flush_buffer(tty
);
457 if (tty
->driver
.flush_buffer
)
458 tty
->driver
.flush_buffer(tty
);
459 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
460 tty
->ldisc
.write_wakeup
)
461 (tty
->ldisc
.write_wakeup
)(tty
);
462 restore_flags(flags
);
465 wake_up_interruptible(&tty
->write_wait
);
466 wake_up_interruptible(&tty
->read_wait
);
469 * Shutdown the current line discipline, and reset it to
472 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
)
473 *tty
->termios
= tty
->driver
.init_termios
;
474 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
475 if (tty
->ldisc
.close
)
476 (tty
->ldisc
.close
)(tty
);
477 tty
->ldisc
= ldiscs
[N_TTY
];
478 tty
->termios
->c_line
= N_TTY
;
479 if (tty
->ldisc
.open
) {
480 int i
= (tty
->ldisc
.open
)(tty
);
482 printk("do_tty_hangup: N_TTY open: error %d\n",
487 read_lock(&tasklist_lock
);
489 if ((tty
->session
> 0) && (p
->session
== tty
->session
) &&
491 send_sig(SIGHUP
,p
,1);
492 send_sig(SIGCONT
,p
,1);
494 p
->tty_old_pgrp
= tty
->pgrp
;
499 read_unlock(&tasklist_lock
);
504 tty
->ctrl_status
= 0;
506 * If one of the devices matches a console pointer, we
507 * cannot just call hangup() because that will cause
508 * tty->count and state->count to go out of sync.
509 * So we just call close() the right number of times.
512 if (tty
->driver
.close
)
513 for (n
= 0; n
< closecount
; n
++)
514 tty
->driver
.close(tty
, cons_filp
);
515 } else if (tty
->driver
.hangup
)
516 (tty
->driver
.hangup
)(tty
);
520 void tty_hangup(struct tty_struct
* tty
)
522 #ifdef TTY_DEBUG_HANGUP
525 printk("%s hangup...\n", tty_name(tty
, buf
));
527 queue_task(&tty
->tq_hangup
, &tq_scheduler
);
530 void tty_vhangup(struct tty_struct
* tty
)
532 #ifdef TTY_DEBUG_HANGUP
535 printk("%s vhangup...\n", tty_name(tty
, buf
));
537 do_tty_hangup((void *) tty
);
540 int tty_hung_up_p(struct file
* filp
)
542 return (filp
->f_op
== &hung_up_tty_fops
);
546 * This function is typically called only by the session leader, when
547 * it wants to disassociate itself from its controlling tty.
549 * It performs the following functions:
550 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
551 * (2) Clears the tty from being controlling the session
552 * (3) Clears the controlling tty for all processes in the
555 * The argument on_exit is set to 1 if called when a process is
556 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
558 void disassociate_ctty(int on_exit
)
560 struct tty_struct
*tty
= current
->tty
;
561 struct task_struct
*p
;
565 tty_pgrp
= tty
->pgrp
;
566 if (on_exit
&& tty
->driver
.type
!= TTY_DRIVER_TYPE_PTY
)
569 if (current
->tty_old_pgrp
) {
570 kill_pg(current
->tty_old_pgrp
, SIGHUP
, on_exit
);
571 kill_pg(current
->tty_old_pgrp
, SIGCONT
, on_exit
);
576 kill_pg(tty_pgrp
, SIGHUP
, on_exit
);
578 kill_pg(tty_pgrp
, SIGCONT
, on_exit
);
581 current
->tty_old_pgrp
= 0;
585 read_lock(&tasklist_lock
);
587 if (p
->session
== current
->session
)
589 read_unlock(&tasklist_lock
);
592 void wait_for_keypress(void)
594 struct console
*c
= console_drivers
;
595 if (c
) c
->wait_key(c
);
598 void stop_tty(struct tty_struct
*tty
)
603 if (tty
->link
&& tty
->link
->packet
) {
604 tty
->ctrl_status
&= ~TIOCPKT_START
;
605 tty
->ctrl_status
|= TIOCPKT_STOP
;
606 wake_up_interruptible(&tty
->link
->read_wait
);
608 if (tty
->driver
.stop
)
609 (tty
->driver
.stop
)(tty
);
612 void start_tty(struct tty_struct
*tty
)
614 if (!tty
->stopped
|| tty
->flow_stopped
)
617 if (tty
->link
&& tty
->link
->packet
) {
618 tty
->ctrl_status
&= ~TIOCPKT_STOP
;
619 tty
->ctrl_status
|= TIOCPKT_START
;
620 wake_up_interruptible(&tty
->link
->read_wait
);
622 if (tty
->driver
.start
)
623 (tty
->driver
.start
)(tty
);
624 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
625 tty
->ldisc
.write_wakeup
)
626 (tty
->ldisc
.write_wakeup
)(tty
);
627 wake_up_interruptible(&tty
->write_wait
);
630 static ssize_t
tty_read(struct file
* file
, char * buf
, size_t count
,
634 struct tty_struct
* tty
;
637 /* Can't seek (pread) on ttys. */
638 if (ppos
!= &file
->f_pos
)
641 tty
= (struct tty_struct
*)file
->private_data
;
642 inode
= file
->f_dentry
->d_inode
;
643 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_read"))
645 if (!tty
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
648 /* This check not only needs to be done before reading, but also
649 whenever read_chan() gets woken up after sleeping, so I've
650 moved it to there. This should only be done for the N_TTY
651 line discipline, anyway. Same goes for write_chan(). -- jlc. */
653 if ((inode
->i_rdev
!= CONSOLE_DEV
) && /* don't stop on /dev/console */
655 (current
->tty
== tty
) &&
656 (tty
->pgrp
!= current
->pgrp
))
657 if (is_ignored(SIGTTIN
) || is_orphaned_pgrp(current
->pgrp
))
660 (void) kill_pg(current
->pgrp
, SIGTTIN
, 1);
666 i
= (tty
->ldisc
.read
)(tty
,file
,buf
,count
);
671 inode
->i_atime
= CURRENT_TIME
;
676 * Split writes up in sane blocksizes to avoid
677 * denial-of-service type attacks
679 static inline ssize_t
do_tty_write(
680 ssize_t (*write
)(struct tty_struct
*, struct file
*, const unsigned char *, size_t),
681 struct tty_struct
*tty
,
683 const unsigned char *buf
,
686 ssize_t ret
= 0, written
= 0;
687 struct inode
*inode
= file
->f_dentry
->d_inode
;
689 if (down_interruptible(&inode
->i_sem
)) {
692 if ( test_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
) ) {
694 written
= write(tty
, file
, buf
, count
);
698 unsigned long size
= PAGE_SIZE
*2;
702 ret
= write(tty
, file
, buf
, size
);
712 if (signal_pending(current
))
714 if (current
->need_resched
)
719 file
->f_dentry
->d_inode
->i_mtime
= CURRENT_TIME
;
727 static ssize_t
tty_write(struct file
* file
, const char * buf
, size_t count
,
731 struct tty_struct
* tty
;
734 /* Can't seek (pwrite) on ttys. */
735 if (ppos
!= &file
->f_pos
)
739 * For now, we redirect writes from /dev/console as
742 inode
= file
->f_dentry
->d_inode
;
743 is_console
= (inode
->i_rdev
== SYSCONS_DEV
||
744 inode
->i_rdev
== CONSOLE_DEV
);
746 if (is_console
&& redirect
)
749 tty
= (struct tty_struct
*)file
->private_data
;
750 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_write"))
752 if (!tty
|| !tty
->driver
.write
|| (test_bit(TTY_IO_ERROR
, &tty
->flags
)))
755 if (!is_console
&& L_TOSTOP(tty
) && (tty
->pgrp
> 0) &&
756 (current
->tty
== tty
) && (tty
->pgrp
!= current
->pgrp
)) {
757 if (is_orphaned_pgrp(current
->pgrp
))
759 if (!is_ignored(SIGTTOU
)) {
760 (void) kill_pg(current
->pgrp
, SIGTTOU
, 1);
765 if (!tty
->ldisc
.write
)
767 return do_tty_write(tty
->ldisc
.write
, tty
, file
,
768 (const unsigned char *)buf
, count
);
771 /* Semaphore to protect creating and releasing a tty */
772 static DECLARE_MUTEX(tty_sem
);
774 static void down_tty_sem(int index
)
779 static void up_tty_sem(int index
)
784 static void release_mem(struct tty_struct
*tty
, int idx
);
787 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
788 * failed open. The new code protects the open with a semaphore, so it's
789 * really quite straightforward. The semaphore locking can probably be
790 * relaxed for the (most common) case of reopening a tty.
792 static int init_dev(kdev_t device
, struct tty_struct
**ret_tty
)
794 struct tty_struct
*tty
, *o_tty
;
795 struct termios
*tp
, **tp_loc
, *o_tp
, **o_tp_loc
;
796 struct termios
*ltp
, **ltp_loc
, *o_ltp
, **o_ltp_loc
;
797 struct tty_driver
*driver
;
801 driver
= get_tty_driver(device
);
805 idx
= MINOR(device
) - driver
->minor_start
;
808 * Check whether we need to acquire the tty semaphore to avoid
809 * race conditions. For now, play it safe.
813 /* check whether we're reopening an existing tty */
814 tty
= driver
->table
[idx
];
815 if (tty
) goto fast_track
;
818 * First time open is complex, especially for PTY devices.
819 * This code guarantees that either everything succeeds and the
820 * TTY is ready for operation, or else the table slots are vacated
821 * and the allocated memory released. (Except that the termios
822 * and locked termios may be retained.)
829 tty
= (struct tty_struct
*) get_zeroed_page(GFP_KERNEL
);
832 initialize_tty_struct(tty
);
833 tty
->device
= device
;
834 tty
->driver
= *driver
;
836 tp_loc
= &driver
->termios
[idx
];
838 tp
= (struct termios
*) kmalloc(sizeof(struct termios
),
842 *tp
= driver
->init_termios
;
845 ltp_loc
= &driver
->termios_locked
[idx
];
847 ltp
= (struct termios
*) kmalloc(sizeof(struct termios
),
851 memset(ltp
, 0, sizeof(struct termios
));
854 if (driver
->type
== TTY_DRIVER_TYPE_PTY
) {
855 o_tty
= (struct tty_struct
*) get_zeroed_page(GFP_KERNEL
);
858 initialize_tty_struct(o_tty
);
859 o_tty
->device
= (kdev_t
) MKDEV(driver
->other
->major
,
860 driver
->other
->minor_start
+ idx
);
861 o_tty
->driver
= *driver
->other
;
863 o_tp_loc
= &driver
->other
->termios
[idx
];
865 o_tp
= (struct termios
*)
866 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
869 *o_tp
= driver
->other
->init_termios
;
872 o_ltp_loc
= &driver
->other
->termios_locked
[idx
];
874 o_ltp
= (struct termios
*)
875 kmalloc(sizeof(struct termios
), GFP_KERNEL
);
878 memset(o_ltp
, 0, sizeof(struct termios
));
882 * Everything allocated ... set up the o_tty structure.
884 driver
->other
->table
[idx
] = o_tty
;
889 o_tty
->termios
= *o_tp_loc
;
890 o_tty
->termios_locked
= *o_ltp_loc
;
891 (*driver
->other
->refcount
)++;
892 if (driver
->subtype
== PTY_TYPE_MASTER
)
895 /* Establish the links in both directions */
901 * All structures have been allocated, so now we install them.
902 * Failures after this point use release_mem to clean up, so
903 * there's no need to null out the local pointers.
905 driver
->table
[idx
] = tty
;
911 tty
->termios
= *tp_loc
;
912 tty
->termios_locked
= *ltp_loc
;
913 (*driver
->refcount
)++;
917 * Structures all installed ... call the ldisc open routines.
918 * If we fail here just call release_mem to clean up. No need
919 * to decrement the use counts, as release_mem doesn't care.
921 if (tty
->ldisc
.open
) {
922 retval
= (tty
->ldisc
.open
)(tty
);
924 goto release_mem_out
;
926 if (o_tty
&& o_tty
->ldisc
.open
) {
927 retval
= (o_tty
->ldisc
.open
)(o_tty
);
929 if (tty
->ldisc
.close
)
930 (tty
->ldisc
.close
)(tty
);
931 goto release_mem_out
;
937 * This fast open can be used if the tty is already open.
938 * No memory is allocated, and the only failures are from
939 * attempting to open a closing tty or attempting multiple
940 * opens on a pty master.
943 if (test_bit(TTY_CLOSING
, &tty
->flags
)) {
947 if (driver
->type
== TTY_DRIVER_TYPE_PTY
&&
948 driver
->subtype
== PTY_TYPE_MASTER
) {
950 * special case for PTY masters: only one open permitted,
951 * and the slave side open count is incremented as well.
960 tty
->driver
= *driver
; /* N.B. why do this every time?? */
965 /* All paths come through here to release the semaphore */
970 /* Release locally allocated memory ... nothing placed in slots */
973 kfree_s(o_tp
, sizeof(struct termios
));
975 free_page((unsigned long) o_tty
);
977 kfree_s(ltp
, sizeof(struct termios
));
979 kfree_s(tp
, sizeof(struct termios
));
980 free_page((unsigned long) tty
);
986 /* call the tty release_mem routine to clean out this slot */
988 printk("init_dev: ldisc open failed, clearing slot %d\n", idx
);
989 release_mem(tty
, idx
);
994 * Releases memory associated with a tty structure, and clears out the
995 * driver table slots.
997 static void release_mem(struct tty_struct
*tty
, int idx
)
999 struct tty_struct
*o_tty
;
1002 if ((o_tty
= tty
->link
) != NULL
) {
1003 o_tty
->driver
.table
[idx
] = NULL
;
1004 if (o_tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
1005 tp
= o_tty
->driver
.termios
[idx
];
1006 o_tty
->driver
.termios
[idx
] = NULL
;
1007 kfree_s(tp
, sizeof(struct termios
));
1010 (*o_tty
->driver
.refcount
)--;
1011 free_page((unsigned long) o_tty
);
1014 tty
->driver
.table
[idx
] = NULL
;
1015 if (tty
->driver
.flags
& TTY_DRIVER_RESET_TERMIOS
) {
1016 tp
= tty
->driver
.termios
[idx
];
1017 tty
->driver
.termios
[idx
] = NULL
;
1018 kfree_s(tp
, sizeof(struct termios
));
1021 (*tty
->driver
.refcount
)--;
1022 free_page((unsigned long) tty
);
1026 * Even releasing the tty structures is a tricky business.. We have
1027 * to be very careful that the structures are all released at the
1028 * same time, as interrupts might otherwise get the wrong pointers.
1030 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1031 * lead to double frees or releasing memory still in use.
1033 static void release_dev(struct file
* filp
)
1035 struct tty_struct
*tty
, *o_tty
;
1036 int pty_master
, tty_closing
, o_tty_closing
, do_sleep
;
1040 tty
= (struct tty_struct
*)filp
->private_data
;
1041 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "release_dev"))
1044 check_tty_count(tty
, "release_dev");
1046 tty_fasync(-1, filp
, 0);
1048 idx
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1049 pty_master
= (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1050 tty
->driver
.subtype
== PTY_TYPE_MASTER
);
1053 #ifdef TTY_PARANOIA_CHECK
1054 if (idx
< 0 || idx
>= tty
->driver
.num
) {
1055 printk("release_dev: bad idx when trying to free (%s)\n",
1056 kdevname(tty
->device
));
1059 if (tty
!= tty
->driver
.table
[idx
]) {
1060 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1061 idx
, kdevname(tty
->device
));
1064 if (tty
->termios
!= tty
->driver
.termios
[idx
]) {
1065 printk("release_dev: driver.termios[%d] not termios "
1067 idx
, kdevname(tty
->device
));
1070 if (tty
->termios_locked
!= tty
->driver
.termios_locked
[idx
]) {
1071 printk("release_dev: driver.termios_locked[%d] not "
1072 "termios_locked for (%s)\n",
1073 idx
, kdevname(tty
->device
));
1078 #ifdef TTY_DEBUG_HANGUP
1079 printk("release_dev of %s (tty count=%d)...", tty_name(tty
, buf
),
1083 #ifdef TTY_PARANOIA_CHECK
1084 if (tty
->driver
.other
) {
1085 if (o_tty
!= tty
->driver
.other
->table
[idx
]) {
1086 printk("release_dev: other->table[%d] not o_tty for ("
1088 idx
, kdevname(tty
->device
));
1091 if (o_tty
->termios
!= tty
->driver
.other
->termios
[idx
]) {
1092 printk("release_dev: other->termios[%d] not o_termios "
1094 idx
, kdevname(tty
->device
));
1097 if (o_tty
->termios_locked
!=
1098 tty
->driver
.other
->termios_locked
[idx
]) {
1099 printk("release_dev: other->termios_locked[%d] not "
1100 "o_termios_locked for (%s)\n",
1101 idx
, kdevname(tty
->device
));
1104 if (o_tty
->link
!= tty
) {
1105 printk("release_dev: bad pty pointers\n");
1111 if (tty
->driver
.close
)
1112 tty
->driver
.close(tty
, filp
);
1115 * Sanity check: if tty->count is going to zero, there shouldn't be
1116 * any waiters on tty->read_wait or tty->write_wait. We test the
1117 * wait queues and kick everyone out _before_ actually starting to
1118 * close. This ensures that we won't block while releasing the tty
1121 * The test for the o_tty closing is necessary, since the master and
1122 * slave sides may close in any order. If the slave side closes out
1123 * first, its count will be one, since the master side holds an open.
1124 * Thus this test wouldn't be triggered at the time the slave closes,
1127 * Note that it's possible for the tty to be opened again while we're
1128 * flushing out waiters. By recalculating the closing flags before
1129 * each iteration we avoid any problems.
1132 tty_closing
= tty
->count
<= 1;
1133 o_tty_closing
= o_tty
&&
1134 (o_tty
->count
<= (pty_master
? 1 : 0));
1138 if (waitqueue_active(&tty
->read_wait
)) {
1139 wake_up(&tty
->read_wait
);
1142 if (waitqueue_active(&tty
->write_wait
)) {
1143 wake_up(&tty
->write_wait
);
1147 if (o_tty_closing
) {
1148 if (waitqueue_active(&o_tty
->read_wait
)) {
1149 wake_up(&o_tty
->read_wait
);
1152 if (waitqueue_active(&o_tty
->write_wait
)) {
1153 wake_up(&o_tty
->write_wait
);
1160 printk("release_dev: %s: read/write wait queue active!\n",
1161 tty_name(tty
, buf
));
1166 * The closing flags are now consistent with the open counts on
1167 * both sides, and we've completed the last operation that could
1168 * block, so it's safe to proceed with closing.
1171 if (--o_tty
->count
< 0) {
1172 printk("release_dev: bad pty slave count (%d) for %s\n",
1173 o_tty
->count
, tty_name(o_tty
, buf
));
1177 if (--tty
->count
< 0) {
1178 printk("release_dev: bad tty->count (%d) for %s\n",
1179 tty
->count
, tty_name(tty
, buf
));
1184 * We've decremented tty->count, so we should zero out
1185 * filp->private_data, to break the link between the tty and
1186 * the file descriptor. Otherwise if filp_close() blocks before
1187 * the the file descriptor is removed from the inuse_filp
1188 * list, check_tty_count() could observe a discrepancy and
1189 * printk a warning message to the user.
1191 filp
->private_data
= 0;
1194 * Perform some housekeeping before deciding whether to return.
1196 * Set the TTY_CLOSING flag if this was the last open. In the
1197 * case of a pty we may have to wait around for the other side
1198 * to close, and TTY_CLOSING makes sure we can't be reopened.
1201 set_bit(TTY_CLOSING
, &tty
->flags
);
1203 set_bit(TTY_CLOSING
, &o_tty
->flags
);
1206 * If _either_ side is closing, make sure there aren't any
1207 * processes that still think tty or o_tty is their controlling
1208 * tty. Also, clear redirect if it points to either tty.
1210 if (tty_closing
|| o_tty_closing
) {
1211 struct task_struct
*p
;
1213 read_lock(&tasklist_lock
);
1215 if (p
->tty
== tty
|| (o_tty
&& p
->tty
== o_tty
))
1218 read_unlock(&tasklist_lock
);
1220 if (redirect
== tty
|| (o_tty
&& redirect
== o_tty
))
1224 /* check whether both sides are closing ... */
1225 if (!tty_closing
|| (o_tty
&& !o_tty_closing
))
1228 #ifdef TTY_DEBUG_HANGUP
1229 printk("freeing tty structure...");
1233 * Shutdown the current line discipline, and reset it to N_TTY.
1234 * N.B. why reset ldisc when we're releasing the memory??
1236 if (tty
->ldisc
.close
)
1237 (tty
->ldisc
.close
)(tty
);
1238 tty
->ldisc
= ldiscs
[N_TTY
];
1239 tty
->termios
->c_line
= N_TTY
;
1241 if (o_tty
->ldisc
.close
)
1242 (o_tty
->ldisc
.close
)(o_tty
);
1243 o_tty
->ldisc
= ldiscs
[N_TTY
];
1247 * Make sure that the tty's task queue isn't activated.
1249 run_task_queue(&tq_timer
);
1250 run_task_queue(&tq_scheduler
);
1253 * The release_mem function takes care of the details of clearing
1254 * the slots and preserving the termios structure.
1256 release_mem(tty
, idx
);
1260 * tty_open and tty_release keep up the tty count that contains the
1261 * number of opens done on a tty. We cannot use the inode-count, as
1262 * different inodes might point to the same tty.
1264 * Open-counting is needed for pty masters, as well as for keeping
1265 * track of serial lines: DTR is dropped when the last close happens.
1266 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1268 * The termios state of a pty is reset on first open so that
1269 * settings don't persist across reuse.
1271 static int tty_open(struct inode
* inode
, struct file
* filp
)
1273 struct tty_struct
*tty
;
1276 unsigned short saved_flags
;
1279 saved_flags
= filp
->f_flags
;
1281 noctty
= filp
->f_flags
& O_NOCTTY
;
1282 device
= inode
->i_rdev
;
1283 if (device
== TTY_DEV
) {
1286 device
= current
->tty
->device
;
1287 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/tty block */
1291 if (device
== CONSOLE_DEV
) {
1292 extern int fg_console
;
1293 device
= MKDEV(TTY_MAJOR
, fg_console
+ 1);
1297 if (device
== SYSCONS_DEV
) {
1298 struct console
*c
= console_drivers
;
1299 while(c
&& !c
->device
)
1303 device
= c
->device(c
);
1304 filp
->f_flags
|= O_NONBLOCK
; /* Don't let /dev/console block */
1308 if (device
== PTMX_DEV
) {
1309 #ifdef CONFIG_UNIX98_PTYS
1311 /* find a free pty. */
1313 struct tty_driver
*driver
;
1315 /* find a device that is not in use. */
1317 for ( major
= 0 ; major
< UNIX98_NR_MAJORS
; major
++ ) {
1318 driver
= &ptm_driver
[major
];
1319 for (minor
= driver
->minor_start
;
1320 minor
< driver
->minor_start
+ driver
->num
;
1322 device
= MKDEV(driver
->major
, minor
);
1323 if (!init_dev(device
, &tty
)) goto ptmx_found
; /* ok! */
1326 return -EIO
; /* no free ptys */
1328 set_bit(TTY_PTY_LOCK
, &tty
->flags
); /* LOCK THE SLAVE */
1329 minor
-= driver
->minor_start
;
1330 devpts_pty_new(driver
->other
->name_base
+ minor
, MKDEV(driver
->other
->major
, minor
+ driver
->other
->minor_start
));
1331 tty_register_devfs(&pts_driver
[major
], DEVFS_FL_NO_PERSISTENCE
,
1332 pts_driver
[major
].minor_start
+ minor
);
1336 #else /* CONFIG_UNIX_98_PTYS */
1340 #endif /* CONFIG_UNIX_98_PTYS */
1343 retval
= init_dev(device
, &tty
);
1347 #ifdef CONFIG_UNIX98_PTYS
1350 filp
->private_data
= tty
;
1351 file_move(filp
, &tty
->tty_files
);
1352 check_tty_count(tty
, "tty_open");
1353 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1354 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1356 #ifdef TTY_DEBUG_HANGUP
1357 printk("opening %s...", tty_name(tty
, buf
));
1359 if (tty
->driver
.open
)
1360 retval
= tty
->driver
.open(tty
, filp
);
1363 filp
->f_flags
= saved_flags
;
1365 if (!retval
&& test_bit(TTY_EXCLUSIVE
, &tty
->flags
) && !suser())
1369 #ifdef TTY_DEBUG_HANGUP
1370 printk("error %d in opening %s...", retval
,
1371 tty_name(tty
, buf
));
1375 if (retval
!= -ERESTARTSYS
)
1377 if (signal_pending(current
))
1381 * Need to reset f_op in case a hangup happened.
1383 filp
->f_op
= &tty_fops
;
1389 tty
->session
== 0) {
1392 task_unlock(current
);
1393 current
->tty_old_pgrp
= 0;
1394 tty
->session
= current
->session
;
1395 tty
->pgrp
= current
->pgrp
;
1397 if ((tty
->driver
.type
== TTY_DRIVER_TYPE_SERIAL
) &&
1398 (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) &&
1399 (tty
->count
== 1)) {
1400 static int nr_warns
= 0;
1402 printk(KERN_WARNING
"tty_io.c: "
1403 "process %d (%s) used obsolete /dev/%s - "
1404 "update software to use /dev/ttyS%d\n",
1405 current
->pid
, current
->comm
,
1406 tty_name(tty
, buf
), TTY_NUMBER(tty
));
1413 static int tty_release(struct inode
* inode
, struct file
* filp
)
1419 /* No kernel lock held - fine */
1420 static unsigned int tty_poll(struct file
* filp
, poll_table
* wait
)
1422 struct tty_struct
* tty
;
1424 tty
= (struct tty_struct
*)filp
->private_data
;
1425 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_poll"))
1428 if (tty
->ldisc
.poll
)
1429 return (tty
->ldisc
.poll
)(tty
, filp
, wait
);
1434 * fasync_helper() is used by some character device drivers (mainly mice)
1435 * to set up the fasync queue. It returns negative on error, 0 if it did
1436 * no changes and positive if it added/deleted the entry.
1438 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
1440 struct fasync_struct
*fa
, **fp
;
1441 unsigned long flags
;
1443 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
1444 if (fa
->fa_file
== filp
)
1453 fa
= (struct fasync_struct
*)kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
1456 fa
->magic
= FASYNC_MAGIC
;
1461 fa
->fa_next
= *fapp
;
1463 restore_flags(flags
);
1471 restore_flags(flags
);
1476 static int tty_fasync(int fd
, struct file
* filp
, int on
)
1478 struct tty_struct
* tty
;
1481 tty
= (struct tty_struct
*)filp
->private_data
;
1482 if (tty_paranoia_check(tty
, filp
->f_dentry
->d_inode
->i_rdev
, "tty_fasync"))
1485 retval
= fasync_helper(fd
, filp
, on
, &tty
->fasync
);
1490 if (!waitqueue_active(&tty
->read_wait
))
1491 tty
->minimum_to_wake
= 1;
1492 if (filp
->f_owner
.pid
== 0) {
1493 filp
->f_owner
.pid
= (-tty
->pgrp
) ? : current
->pid
;
1494 filp
->f_owner
.uid
= current
->uid
;
1495 filp
->f_owner
.euid
= current
->euid
;
1498 if (!tty
->fasync
&& !waitqueue_active(&tty
->read_wait
))
1499 tty
->minimum_to_wake
= N_TTY_BUF_SIZE
;
1504 static int tiocsti(struct tty_struct
*tty
, char * arg
)
1508 if ((current
->tty
!= tty
) && !suser())
1510 if (get_user(ch
, arg
))
1512 tty
->ldisc
.receive_buf(tty
, &ch
, &mbz
, 1);
1516 static int tiocgwinsz(struct tty_struct
*tty
, struct winsize
* arg
)
1518 if (copy_to_user(arg
, &tty
->winsize
, sizeof(*arg
)))
1523 static int tiocswinsz(struct tty_struct
*tty
, struct tty_struct
*real_tty
,
1524 struct winsize
* arg
)
1526 struct winsize tmp_ws
;
1528 if (copy_from_user(&tmp_ws
, arg
, sizeof(*arg
)))
1530 if (!memcmp(&tmp_ws
, &tty
->winsize
, sizeof(*arg
)))
1533 kill_pg(tty
->pgrp
, SIGWINCH
, 1);
1534 if ((real_tty
->pgrp
!= tty
->pgrp
) && (real_tty
->pgrp
> 0))
1535 kill_pg(real_tty
->pgrp
, SIGWINCH
, 1);
1536 tty
->winsize
= tmp_ws
;
1537 real_tty
->winsize
= tmp_ws
;
1541 static int tioccons(struct inode
*inode
,
1542 struct tty_struct
*tty
, struct tty_struct
*real_tty
)
1544 if (inode
->i_rdev
== SYSCONS_DEV
||
1545 inode
->i_rdev
== CONSOLE_DEV
) {
1553 redirect
= real_tty
;
1558 static int fionbio(struct file
*file
, int *arg
)
1562 if (get_user(nonblock
, arg
))
1566 file
->f_flags
|= O_NONBLOCK
;
1568 file
->f_flags
&= ~O_NONBLOCK
;
1572 static int tiocsctty(struct tty_struct
*tty
, int arg
)
1574 if (current
->leader
&&
1575 (current
->session
== tty
->session
))
1578 * The process must be a session leader and
1579 * not have a controlling tty already.
1581 if (!current
->leader
|| current
->tty
)
1583 if (tty
->session
> 0) {
1585 * This tty is already the controlling
1586 * tty for another session group!
1588 if ((arg
== 1) && suser()) {
1592 struct task_struct
*p
;
1594 read_lock(&tasklist_lock
);
1598 read_unlock(&tasklist_lock
);
1604 task_unlock(current
);
1605 current
->tty_old_pgrp
= 0;
1606 tty
->session
= current
->session
;
1607 tty
->pgrp
= current
->pgrp
;
1611 static int tiocgpgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1614 * (tty == real_tty) is a cheap way of
1615 * testing if the tty is NOT a master pty.
1617 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1619 return put_user(real_tty
->pgrp
, arg
);
1622 static int tiocspgrp(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1625 int retval
= tty_check_change(real_tty
);
1631 if (!current
->tty
||
1632 (current
->tty
!= real_tty
) ||
1633 (real_tty
->session
!= current
->session
))
1635 get_user(pgrp
, (pid_t
*) arg
);
1638 if (session_of_pgrp(pgrp
) != current
->session
)
1640 real_tty
->pgrp
= pgrp
;
1644 static int tiocgsid(struct tty_struct
*tty
, struct tty_struct
*real_tty
, pid_t
*arg
)
1647 * (tty == real_tty) is a cheap way of
1648 * testing if the tty is NOT a master pty.
1650 if (tty
== real_tty
&& current
->tty
!= real_tty
)
1652 if (real_tty
->session
<= 0)
1654 return put_user(real_tty
->session
, arg
);
1657 static int tiocttygstruct(struct tty_struct
*tty
, struct tty_struct
*arg
)
1659 if (copy_to_user(arg
, tty
, sizeof(*arg
)))
1664 static int tiocsetd(struct tty_struct
*tty
, int *arg
)
1668 retval
= get_user(ldisc
, arg
);
1671 return tty_set_ldisc(tty
, ldisc
);
1674 static int send_break(struct tty_struct
*tty
, int duration
)
1676 set_current_state(TASK_INTERRUPTIBLE
);
1678 tty
->driver
.break_ctl(tty
, -1);
1679 if (!signal_pending(current
))
1680 schedule_timeout(duration
);
1681 tty
->driver
.break_ctl(tty
, 0);
1682 if (signal_pending(current
))
1688 * Split this up, as gcc can choke on it otherwise..
1690 int tty_ioctl(struct inode
* inode
, struct file
* file
,
1691 unsigned int cmd
, unsigned long arg
)
1693 struct tty_struct
*tty
, *real_tty
;
1696 tty
= (struct tty_struct
*)file
->private_data
;
1697 if (tty_paranoia_check(tty
, inode
->i_rdev
, "tty_ioctl"))
1701 if (tty
->driver
.type
== TTY_DRIVER_TYPE_PTY
&&
1702 tty
->driver
.subtype
== PTY_TYPE_MASTER
)
1703 real_tty
= tty
->link
;
1706 * Break handling by driver
1708 if (!tty
->driver
.break_ctl
) {
1712 if (tty
->driver
.ioctl
)
1713 return tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1716 /* These two ioctl's always return success; even if */
1717 /* the driver doesn't support them. */
1720 if (!tty
->driver
.ioctl
)
1722 retval
= tty
->driver
.ioctl(tty
, file
, cmd
, arg
);
1723 if (retval
== -ENOIOCTLCMD
)
1730 * Factor out some common prep work
1738 retval
= tty_check_change(tty
);
1741 if (cmd
!= TIOCCBRK
) {
1742 tty_wait_until_sent(tty
, 0);
1743 if (signal_pending(current
))
1751 return tiocsti(tty
, (char *)arg
);
1753 return tiocgwinsz(tty
, (struct winsize
*) arg
);
1755 return tiocswinsz(tty
, real_tty
, (struct winsize
*) arg
);
1757 return tioccons(inode
, tty
, real_tty
);
1759 return fionbio(file
, (int *) arg
);
1761 set_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1764 clear_bit(TTY_EXCLUSIVE
, &tty
->flags
);
1767 if (current
->tty
!= tty
)
1769 if (current
->leader
)
1770 disassociate_ctty(0);
1772 current
->tty
= NULL
;
1773 task_unlock(current
);
1776 return tiocsctty(tty
, arg
);
1778 return tiocgpgrp(tty
, real_tty
, (pid_t
*) arg
);
1780 return tiocspgrp(tty
, real_tty
, (pid_t
*) arg
);
1782 return tiocgsid(tty
, real_tty
, (pid_t
*) arg
);
1784 return put_user(tty
->ldisc
.num
, (int *) arg
);
1786 return tiocsetd(tty
, (int *) arg
);
1789 return tioclinux(tty
, arg
);
1791 case TIOCTTYGSTRUCT
:
1792 return tiocttygstruct(tty
, (struct tty_struct
*) arg
);
1797 case TIOCSBRK
: /* Turn break on, unconditionally */
1798 tty
->driver
.break_ctl(tty
, -1);
1801 case TIOCCBRK
: /* Turn break off, unconditionally */
1802 tty
->driver
.break_ctl(tty
, 0);
1804 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1806 * XXX is the above comment correct, or the
1807 * code below correct? Is this ioctl used at
1811 return send_break(tty
, HZ
/4);
1813 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1814 return send_break(tty
, arg
? arg
*(HZ
/10) : HZ
/4);
1816 if (tty
->driver
.ioctl
) {
1817 int retval
= (tty
->driver
.ioctl
)(tty
, file
, cmd
, arg
);
1818 if (retval
!= -ENOIOCTLCMD
)
1821 if (tty
->ldisc
.ioctl
) {
1822 int retval
= (tty
->ldisc
.ioctl
)(tty
, file
, cmd
, arg
);
1823 if (retval
!= -ENOIOCTLCMD
)
1831 * This implements the "Secure Attention Key" --- the idea is to
1832 * prevent trojan horses by killing all processes associated with this
1833 * tty when the user hits the "Secure Attention Key". Required for
1834 * super-paranoid applications --- see the Orange Book for more details.
1836 * This code could be nicer; ideally it should send a HUP, wait a few
1837 * seconds, then send a INT, and then a KILL signal. But you then
1838 * have to coordinate with the init process, since all processes associated
1839 * with the current tty must be dead before the new getty is allowed
1842 * Now, if it would be correct ;-/ The current code has a nasty hole -
1843 * it doesn't catch files in flight. We may send the descriptor to ourselves
1844 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1846 void do_SAK( struct tty_struct
*tty
)
1851 struct task_struct
*p
;
1858 session
= tty
->session
;
1859 if (tty
->ldisc
.flush_buffer
)
1860 tty
->ldisc
.flush_buffer(tty
);
1861 if (tty
->driver
.flush_buffer
)
1862 tty
->driver
.flush_buffer(tty
);
1863 read_lock(&tasklist_lock
);
1865 if ((p
->tty
== tty
) ||
1866 ((session
> 0) && (p
->session
== session
)))
1867 send_sig(SIGKILL
, p
, 1);
1868 else if (p
->files
) {
1869 read_lock(&p
->files
->file_lock
);
1870 /* FIXME: p->files could change */
1871 for (i
=0; i
< p
->files
->max_fds
; i
++) {
1872 filp
= fcheck_files(p
->files
, i
);
1873 if (filp
&& (filp
->f_op
== &tty_fops
) &&
1874 (filp
->private_data
== tty
)) {
1875 send_sig(SIGKILL
, p
, 1);
1879 read_unlock(&p
->files
->file_lock
);
1882 read_unlock(&tasklist_lock
);
1887 * This routine is called out of the software interrupt to flush data
1888 * from the flip buffer to the line discipline.
1890 static void flush_to_ldisc(void *private_
)
1892 struct tty_struct
*tty
= (struct tty_struct
*) private_
;
1896 unsigned long flags
;
1898 if (test_bit(TTY_DONT_FLIP
, &tty
->flags
)) {
1899 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1902 if (tty
->flip
.buf_num
) {
1903 cp
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1904 fp
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1905 tty
->flip
.buf_num
= 0;
1907 save_flags(flags
); cli();
1908 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1909 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1911 cp
= tty
->flip
.char_buf
;
1912 fp
= tty
->flip
.flag_buf
;
1913 tty
->flip
.buf_num
= 1;
1915 save_flags(flags
); cli();
1916 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
+ TTY_FLIPBUF_SIZE
;
1917 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
+ TTY_FLIPBUF_SIZE
;
1919 count
= tty
->flip
.count
;
1920 tty
->flip
.count
= 0;
1921 restore_flags(flags
);
1923 tty
->ldisc
.receive_buf(tty
, cp
, fp
, count
);
1927 * Routine which returns the baud rate of the tty
1929 * Note that the baud_table needs to be kept in sync with the
1930 * include/asm/termbits.h file.
1932 static int baud_table
[] = {
1933 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1934 9600, 19200, 38400, 57600, 115200, 230400, 460800,
1936 76800, 153600, 307200, 614400, 921600
1938 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1939 2500000, 3000000, 3500000, 4000000
1943 static int n_baud_table
= sizeof(baud_table
)/sizeof(int);
1945 int tty_get_baud_rate(struct tty_struct
*tty
)
1947 unsigned int cflag
, i
;
1949 cflag
= tty
->termios
->c_cflag
;
1954 if (i
< 1 || i
+15 >= n_baud_table
)
1955 tty
->termios
->c_cflag
&= ~CBAUDEX
;
1959 if (i
==15 && tty
->alt_speed
) {
1961 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1964 return(tty
->alt_speed
);
1967 return baud_table
[i
];
1970 void tty_flip_buffer_push(struct tty_struct
*tty
)
1972 if (tty
->low_latency
)
1973 flush_to_ldisc((void *) tty
);
1975 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
1979 * This subroutine initializes a tty structure.
1981 static void initialize_tty_struct(struct tty_struct
*tty
)
1983 memset(tty
, 0, sizeof(struct tty_struct
));
1984 tty
->magic
= TTY_MAGIC
;
1985 tty
->ldisc
= ldiscs
[N_TTY
];
1987 tty
->flip
.char_buf_ptr
= tty
->flip
.char_buf
;
1988 tty
->flip
.flag_buf_ptr
= tty
->flip
.flag_buf
;
1989 tty
->flip
.tqueue
.routine
= flush_to_ldisc
;
1990 tty
->flip
.tqueue
.data
= tty
;
1991 init_MUTEX(&tty
->flip
.pty_sem
);
1992 init_waitqueue_head(&tty
->write_wait
);
1993 init_waitqueue_head(&tty
->read_wait
);
1994 tty
->tq_hangup
.routine
= do_tty_hangup
;
1995 tty
->tq_hangup
.data
= tty
;
1996 sema_init(&tty
->atomic_read
, 1);
1997 spin_lock_init(&tty
->read_lock
);
1998 INIT_LIST_HEAD(&tty
->tty_files
);
2002 * The default put_char routine if the driver did not define one.
2004 void tty_default_put_char(struct tty_struct
*tty
, unsigned char ch
)
2006 tty
->driver
.write(tty
, 0, &ch
, 1);
2010 * Register a tty device described by <driver>, with minor number <minor>.
2012 void tty_register_devfs (struct tty_driver
*driver
, unsigned int flags
,
2015 #ifdef CONFIG_DEVFS_FS
2016 umode_t mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
;
2019 struct tty_struct tty
;
2022 tty
.driver
= *driver
;
2023 tty
.device
= MKDEV (driver
->major
, minor
);
2024 switch (tty
.device
) {
2027 mode
|= S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
2030 if (driver
->major
== PTY_MASTER_MAJOR
)
2031 flags
|= DEVFS_FL_AUTO_OWNER
;
2034 if ( (minor
< driver
->minor_start
) ||
2035 (minor
>= driver
->minor_start
+ driver
->num
) ) {
2036 printk(KERN_ERR
"Attempt to register invalid minor number "
2037 "with devfs (%d:%d).\n", (int)driver
->major
,(int)minor
);
2040 # ifdef CONFIG_UNIX98_PTYS
2041 if ( (driver
->major
>= UNIX98_PTY_SLAVE_MAJOR
) &&
2042 (driver
->major
< UNIX98_PTY_SLAVE_MAJOR
+ UNIX98_NR_MAJORS
) ) {
2047 devfs_register (NULL
, tty_name (&tty
, buf
), 0,flags
| DEVFS_FL_DEFAULT
,
2048 driver
->major
, minor
, mode
, uid
, gid
,
2050 #endif /* CONFIG_DEVFS_FS */
2053 void tty_unregister_devfs (struct tty_driver
*driver
, unsigned minor
)
2055 #ifdef CONFIG_DEVFS_FS
2057 struct tty_struct tty
;
2060 tty
.driver
= *driver
;
2061 tty
.device
= MKDEV(driver
->major
, minor
);
2063 handle
= devfs_find_handle (NULL
, tty_name (&tty
, buf
), 0,
2064 driver
->major
, minor
,
2065 DEVFS_SPECIAL_CHR
, 0);
2066 devfs_unregister (handle
);
2067 #endif /* CONFIG_DEVFS_FS */
2070 EXPORT_SYMBOL(tty_register_devfs
);
2071 EXPORT_SYMBOL(tty_unregister_devfs
);
2074 * Called by a tty driver to register itself.
2076 int tty_register_driver(struct tty_driver
*driver
)
2081 if (driver
->flags
& TTY_DRIVER_INSTALLED
)
2084 error
= devfs_register_chrdev(driver
->major
, driver
->name
, &tty_fops
);
2087 else if(driver
->major
== 0)
2088 driver
->major
= error
;
2090 if (!driver
->put_char
)
2091 driver
->put_char
= tty_default_put_char
;
2094 driver
->next
= tty_drivers
;
2095 if (tty_drivers
) tty_drivers
->prev
= driver
;
2096 tty_drivers
= driver
;
2098 if ( !(driver
->flags
& TTY_DRIVER_NO_DEVFS
) ) {
2099 for(i
= 0; i
< driver
->num
; i
++)
2100 tty_register_devfs(driver
, 0, driver
->minor_start
+ i
);
2102 proc_tty_register_driver(driver
);
2107 * Called by a tty driver to unregister itself.
2109 int tty_unregister_driver(struct tty_driver
*driver
)
2112 struct tty_driver
*p
;
2115 const char *othername
= NULL
;
2117 if (*driver
->refcount
)
2120 for (p
= tty_drivers
; p
; p
= p
->next
) {
2123 else if (p
->major
== driver
->major
)
2124 othername
= p
->name
;
2130 if (othername
== NULL
) {
2131 retval
= devfs_unregister_chrdev(driver
->major
, driver
->name
);
2135 devfs_register_chrdev(driver
->major
, othername
, &tty_fops
);
2138 driver
->prev
->next
= driver
->next
;
2140 tty_drivers
= driver
->next
;
2143 driver
->next
->prev
= driver
->prev
;
2146 * Free the termios and termios_locked structures because
2147 * we don't want to get memory leaks when modular tty
2148 * drivers are removed from the kernel.
2150 for (i
= 0; i
< driver
->num
; i
++) {
2151 tp
= driver
->termios
[i
];
2153 driver
->termios
[i
] = NULL
;
2154 kfree_s(tp
, sizeof(struct termios
));
2156 tp
= driver
->termios_locked
[i
];
2158 driver
->termios_locked
[i
] = NULL
;
2159 kfree_s(tp
, sizeof(struct termios
));
2161 tty_unregister_devfs(driver
, driver
->minor_start
+ i
);
2163 proc_tty_unregister_driver(driver
);
2169 * Initialize the console device. This is called *early*, so
2170 * we can't necessarily depend on lots of kernel help here.
2171 * Just do some early initializations, and do the complex setup
2174 void __init
console_init(void)
2176 /* Setup the default TTY line discipline. */
2177 memset(ldiscs
, 0, sizeof(ldiscs
));
2178 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);
2181 * Set up the standard termios. Individual tty drivers may
2182 * deviate from this; this is used as a template.
2184 memset(&tty_std_termios
, 0, sizeof(struct termios
));
2185 memcpy(tty_std_termios
.c_cc
, INIT_C_CC
, NCCS
);
2186 tty_std_termios
.c_iflag
= ICRNL
| IXON
;
2187 tty_std_termios
.c_oflag
= OPOST
| ONLCR
;
2188 tty_std_termios
.c_cflag
= B38400
| CS8
| CREAD
| HUPCL
;
2189 tty_std_termios
.c_lflag
= ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
|
2190 ECHOCTL
| ECHOKE
| IEXTEN
;
2193 * set up the console device so that later boot sequences can
2194 * inform about problems etc..
2199 #ifdef CONFIG_SERIAL_CONSOLE
2200 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2202 #elif defined(CONFIG_SERIAL)
2203 serial_console_init();
2204 #endif /* CONFIG_8xx */
2205 #ifdef CONFIG_SGI_SERIAL
2206 sgi_serial_console_init();
2208 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2209 vme_scc_console_init();
2211 #if defined(CONFIG_SERIAL167)
2212 serial167_console_init();
2223 static struct tty_driver dev_tty_driver
, dev_syscons_driver
;
2224 #ifdef CONFIG_UNIX98_PTYS
2225 static struct tty_driver dev_ptmx_driver
;
2228 static struct tty_driver dev_console_driver
;
2232 * Ok, now we can initialize the rest of the tty devices and can count
2233 * on memory allocations, interrupts etc..
2235 void __init
tty_init(void)
2237 if (sizeof(struct tty_struct
) > PAGE_SIZE
)
2238 panic("size of tty structure > PAGE_SIZE!");
2241 * dev_tty_driver and dev_console_driver are actually magic
2242 * devices which get redirected at open time. Nevertheless,
2243 * we register them so that register_chrdev is called
2246 memset(&dev_tty_driver
, 0, sizeof(struct tty_driver
));
2247 dev_tty_driver
.magic
= TTY_DRIVER_MAGIC
;
2248 dev_tty_driver
.driver_name
= "/dev/tty";
2249 dev_tty_driver
.name
= dev_tty_driver
.driver_name
+ 5;
2250 dev_tty_driver
.name_base
= 0;
2251 dev_tty_driver
.major
= TTYAUX_MAJOR
;
2252 dev_tty_driver
.minor_start
= 0;
2253 dev_tty_driver
.num
= 1;
2254 dev_tty_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2255 dev_tty_driver
.subtype
= SYSTEM_TYPE_TTY
;
2257 if (tty_register_driver(&dev_tty_driver
))
2258 panic("Couldn't register /dev/tty driver\n");
2260 dev_syscons_driver
= dev_tty_driver
;
2261 dev_syscons_driver
.driver_name
= "/dev/console";
2262 dev_syscons_driver
.name
= dev_syscons_driver
.driver_name
+ 5;
2263 dev_syscons_driver
.major
= TTYAUX_MAJOR
;
2264 dev_syscons_driver
.minor_start
= 1;
2265 dev_syscons_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2266 dev_syscons_driver
.subtype
= SYSTEM_TYPE_SYSCONS
;
2268 if (tty_register_driver(&dev_syscons_driver
))
2269 panic("Couldn't register /dev/console driver\n");
2271 /* console calls tty_register_driver() before kmalloc() works.
2272 * Thus, we can't devfs_register() then. Do so now, instead.
2278 #ifdef CONFIG_UNIX98_PTYS
2279 dev_ptmx_driver
= dev_tty_driver
;
2280 dev_ptmx_driver
.driver_name
= "/dev/ptmx";
2281 dev_ptmx_driver
.name
= dev_ptmx_driver
.driver_name
+ 5;
2282 dev_ptmx_driver
.major
= MAJOR(PTMX_DEV
);
2283 dev_ptmx_driver
.minor_start
= MINOR(PTMX_DEV
);
2284 dev_ptmx_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2285 dev_ptmx_driver
.subtype
= SYSTEM_TYPE_SYSPTMX
;
2287 if (tty_register_driver(&dev_ptmx_driver
))
2288 panic("Couldn't register /dev/ptmx driver\n");
2292 dev_console_driver
= dev_tty_driver
;
2293 dev_console_driver
.driver_name
= "/dev/vc/0";
2294 dev_console_driver
.name
= dev_console_driver
.driver_name
+ 5;
2295 dev_console_driver
.major
= TTY_MAJOR
;
2296 dev_console_driver
.type
= TTY_DRIVER_TYPE_SYSTEM
;
2297 dev_console_driver
.subtype
= SYSTEM_TYPE_CONSOLE
;
2299 if (tty_register_driver(&dev_console_driver
))
2300 panic("Couldn't register /dev/tty0 driver\n");
2304 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2307 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2310 #ifdef CONFIG_COMPUTONE
2313 #ifdef CONFIG_MAC_SERIAL
2316 #ifdef CONFIG_ROCKETPORT
2319 #ifdef CONFIG_SERIAL167
2322 #ifdef CONFIG_CYCLADES
2325 #ifdef CONFIG_STALLION
2328 #ifdef CONFIG_ISTALLION
2334 #ifdef CONFIG_DIGIEPCA
2337 #ifdef CONFIG_RISCOM8
2340 #ifdef CONFIG_SPECIALIX
2349 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2351 #endif /* CONFIG_8xx */
2353 #ifdef CONFIG_MOXA_SMARTIO
2356 #ifdef CONFIG_MOXA_INTELLIO