Commit 3a33f489 authored by Alexander Viro's avatar Alexander Viro Committed by Linus Torvalds

[PATCH] tty_driver refcounting

drivers/char/pty.c converted to dynamic allocation
parent 956bf124
......@@ -31,24 +31,12 @@
#include <asm/bitops.h>
#include <linux/devpts_fs.h>
struct pty_struct {
int magic;
wait_queue_head_t open_wait;
};
#define PTY_MAGIC 0x5001
static struct tty_driver pty_driver, pty_slave_driver;
/* Note: one set of tables for BSD and one for Unix98 */
static struct pty_struct pty_state[NR_PTYS];
static struct tty_driver *pty_driver, *pty_slave_driver;
#ifdef CONFIG_UNIX98_PTYS
/* These are global because they are accessed in tty_io.c */
struct tty_driver ptm_driver;
struct tty_driver pts_driver;
static struct pty_struct ptm_state[UNIX98_NR_MAJORS*NR_PTYS];
struct tty_driver *ptm_driver;
struct tty_driver *pts_driver;
#endif
static void pty_close(struct tty_struct * tty, struct file * filp)
......@@ -74,7 +62,7 @@ static void pty_close(struct tty_struct * tty, struct file * filp)
if (tty->driver->subtype == PTY_TYPE_MASTER) {
set_bit(TTY_OTHER_CLOSED, &tty->flags);
#ifdef CONFIG_UNIX98_PTYS
if (tty->driver == &ptm_driver)
if (tty->driver == ptm_driver)
devpts_pty_kill(tty->index);
#endif
tty_vhangup(tty->link);
......@@ -287,16 +275,10 @@ static void pty_flush_buffer(struct tty_struct *tty)
static int pty_open(struct tty_struct *tty, struct file * filp)
{
int retval;
int line;
struct pty_struct *pty;
int retval = -ENODEV;
retval = -ENODEV;
if (!tty || !tty->link)
goto out;
line = tty->index;
pty = (struct pty_struct *)(tty->driver->driver_state) + line;
tty->driver_data = pty;
retval = -EIO;
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
......@@ -307,7 +289,6 @@ static int pty_open(struct tty_struct *tty, struct file * filp)
goto out;
clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
wake_up_interruptible(&pty->open_wait);
set_bit(TTY_THROTTLED, &tty->flags);
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
retval = 0;
......@@ -321,105 +302,114 @@ static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
tty->termios->c_cflag |= (CS8 | CREAD);
}
static struct tty_operations pty_ops = {
.open = pty_open,
.close = pty_close,
.write = pty_write,
.write_room = pty_write_room,
.flush_buffer = pty_flush_buffer,
.chars_in_buffer = pty_chars_in_buffer,
.unthrottle = pty_unthrottle,
.set_termios = pty_set_termios,
};
int __init pty_init(void)
{
int i;
/* Traditional BSD devices */
memset(&pty_state, 0, sizeof(pty_state));
for (i = 0; i < NR_PTYS; i++)
init_waitqueue_head(&pty_state[i].open_wait);
memset(&pty_driver, 0, sizeof(struct tty_driver));
pty_driver.magic = TTY_DRIVER_MAGIC;
pty_driver.owner = THIS_MODULE;
pty_driver.driver_name = "pty_master";
pty_driver.name = "pty";
pty_driver.devfs_name = "pty/m";
pty_driver.major = PTY_MASTER_MAJOR;
pty_driver.minor_start = 0;
pty_driver.num = NR_PTYS;
pty_driver.type = TTY_DRIVER_TYPE_PTY;
pty_driver.subtype = PTY_TYPE_MASTER;
pty_driver.init_termios = tty_std_termios;
pty_driver.init_termios.c_iflag = 0;
pty_driver.init_termios.c_oflag = 0;
pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
pty_driver.init_termios.c_lflag = 0;
pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
pty_driver.driver_state = pty_state;
pty_driver.other = &pty_slave_driver;
pty_driver.open = pty_open;
pty_driver.close = pty_close;
pty_driver.write = pty_write;
pty_driver.write_room = pty_write_room;
pty_driver.flush_buffer = pty_flush_buffer;
pty_driver.chars_in_buffer = pty_chars_in_buffer;
pty_driver.unthrottle = pty_unthrottle;
pty_driver.set_termios = pty_set_termios;
pty_slave_driver = pty_driver;
pty_slave_driver.driver_name = "pty_slave";
pty_slave_driver.proc_entry = 0;
pty_slave_driver.name = "ttyp";
pty_slave_driver.devfs_name = "pty/s";
pty_slave_driver.subtype = PTY_TYPE_SLAVE;
pty_slave_driver.major = PTY_SLAVE_MAJOR;
pty_slave_driver.minor_start = 0;
pty_slave_driver.init_termios = tty_std_termios;
pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
/* Slave ptys are registered when their corresponding master pty
* is opened, and unregistered when the pair is closed.
*/
pty_slave_driver.flags |= TTY_DRIVER_NO_DEVFS;
pty_slave_driver.driver_state = pty_state;
pty_slave_driver.other = &pty_driver;
if (tty_register_driver(&pty_driver))
pty_driver = alloc_tty_driver(NR_PTYS);
if (!pty_driver)
panic("Couldn't allocate pty driver");
pty_slave_driver = alloc_tty_driver(NR_PTYS);
if (!pty_slave_driver)
panic("Couldn't allocate pty slave driver");
pty_driver->owner = THIS_MODULE;
pty_driver->driver_name = "pty_master";
pty_driver->name = "pty";
pty_driver->devfs_name = "pty/m";
pty_driver->major = PTY_MASTER_MAJOR;
pty_driver->minor_start = 0;
pty_driver->type = TTY_DRIVER_TYPE_PTY;
pty_driver->subtype = PTY_TYPE_MASTER;
pty_driver->init_termios = tty_std_termios;
pty_driver->init_termios.c_iflag = 0;
pty_driver->init_termios.c_oflag = 0;
pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
pty_driver->init_termios.c_lflag = 0;
pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
pty_driver->other = pty_slave_driver;
tty_set_operations(pty_driver, &pty_ops);
pty_driver->ioctl = pty_bsd_ioctl;
pty_slave_driver->owner = THIS_MODULE;
pty_slave_driver->driver_name = "pty_slave";
pty_slave_driver->name = "ttyp";
pty_slave_driver->devfs_name = "pty/s";
pty_slave_driver->major = PTY_SLAVE_MAJOR;
pty_slave_driver->minor_start = 0;
pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
pty_slave_driver->subtype = PTY_TYPE_SLAVE;
pty_slave_driver->init_termios = tty_std_termios;
pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
pty_slave_driver->other = pty_driver;
tty_set_operations(pty_slave_driver, &pty_ops);
if (tty_register_driver(pty_driver))
panic("Couldn't register pty driver");
if (tty_register_driver(&pty_slave_driver))
if (tty_register_driver(pty_slave_driver))
panic("Couldn't register pty slave driver");
/*
* only the master pty gets this ioctl (which is why we
* assign it here, instead of up with the rest of the
* pty_driver initialization. <cananian@alumni.princeton.edu>
*/
pty_driver.ioctl = pty_bsd_ioctl;
/* Unix98 devices */
#ifdef CONFIG_UNIX98_PTYS
devfs_mk_dir("pts");
printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS*NR_PTYS);
ptm_driver = pty_driver;
ptm_driver.name = "ptm";
ptm_driver.proc_entry = 0;
ptm_driver.major = UNIX98_PTY_MASTER_MAJOR;
ptm_driver.minor_start = 0;
ptm_driver.num = UNIX98_NR_MAJORS * NR_PTYS;
ptm_driver.other = &pts_driver;
ptm_driver.flags |= TTY_DRIVER_NO_DEVFS;
ptm_driver.driver_state = ptm_state;
for (i = 0; i < UNIX98_NR_MAJORS*NR_PTYS; i++)
init_waitqueue_head(&ptm_state[i].open_wait);
pts_driver = pty_slave_driver;
pts_driver.name = "pts";
pts_driver.proc_entry = 0;
pts_driver.major = UNIX98_PTY_SLAVE_MAJOR;
pts_driver.minor_start = 0;
pts_driver.num = UNIX98_NR_MAJORS * NR_PTYS;
pts_driver.other = &ptm_driver;
pts_driver.flags |= TTY_DRIVER_NO_DEVFS;
pts_driver.driver_state = ptm_state;
ptm_driver.ioctl = pty_unix98_ioctl;
if (tty_register_driver(&ptm_driver))
ptm_driver = alloc_tty_driver(UNIX98_NR_MAJORS * NR_PTYS);
if (!ptm_driver)
panic("Couldn't allocate Unix98 ptm driver");
pts_driver = alloc_tty_driver(UNIX98_NR_MAJORS * NR_PTYS);
if (!pts_driver)
panic("Couldn't allocate Unix98 pts driver");
ptm_driver->owner = THIS_MODULE;
ptm_driver->driver_name = "pty_master";
ptm_driver->name = "ptm";
ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
ptm_driver->minor_start = 0;
ptm_driver->type = TTY_DRIVER_TYPE_PTY;
ptm_driver->subtype = PTY_TYPE_MASTER;
ptm_driver->init_termios = tty_std_termios;
ptm_driver->init_termios.c_iflag = 0;
ptm_driver->init_termios.c_oflag = 0;
ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
ptm_driver->init_termios.c_lflag = 0;
ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
TTY_DRIVER_NO_DEVFS;
ptm_driver->other = pts_driver;
tty_set_operations(ptm_driver, &pty_ops);
ptm_driver->ioctl = pty_unix98_ioctl;
pts_driver->owner = THIS_MODULE;
pts_driver->driver_name = "pty_slave";
pts_driver->name = "pts";
pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
pts_driver->minor_start = 0;
pts_driver->type = TTY_DRIVER_TYPE_PTY;
pts_driver->subtype = PTY_TYPE_SLAVE;
pts_driver->init_termios = tty_std_termios;
pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
pts_driver->flags = TTY_DRIVER_RESET_TERMIOS |
TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
pts_driver->other = ptm_driver;
tty_set_operations(pts_driver, &pty_ops);
if (tty_register_driver(ptm_driver))
panic("Couldn't register Unix98 ptm driver");
if (tty_register_driver(&pts_driver))
if (tty_register_driver(pts_driver))
panic("Couldn't register Unix98 pts driver");
#endif
return 0;
......
......@@ -126,8 +126,8 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers */
struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
#ifdef CONFIG_UNIX98_PTYS
extern struct tty_driver ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
extern struct tty_driver pts_driver; /* Unix98 pty slaves; for /dev/ptmx */
extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
extern struct tty_driver *pts_driver; /* Unix98 pty slaves; for /dev/ptmx */
#endif
extern void disable_early_printk(void);
......@@ -1349,14 +1349,14 @@ static int tty_open(struct inode * inode, struct file * filp)
#ifdef CONFIG_UNIX98_PTYS
/* find a device that is not in use. */
retval = -1;
driver = &ptm_driver;
driver = ptm_driver;
for (index = 0; index < driver->num ; index++)
if (!init_dev(driver, index, &tty))
goto ptmx_found; /* ok! */
return -EIO; /* no free ptys */
ptmx_found:
set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
devpts_pty_new(index, MKDEV(pts_driver.major, pts_driver.minor_start) + index);
devpts_pty_new(index, MKDEV(pts_driver->major, pts_driver->minor_start) + index);
noctty = 1;
#else
return -ENODEV;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment