12 #include <linux/module.h>
14 #include <linux/errno.h>
16 #include <linux/tty.h>
18 #include <linux/fcntl.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
24 #include <linux/device.h>
26 #include <linux/bitops.h>
28 #include <linux/slab.h>
32 #ifdef CONFIG_UNIX98_PTYS
53 tty->
link->packet = 0;
59 #ifdef CONFIG_UNIX98_PTYS
60 if (tty->
driver == ptm_driver) {
82 static void pty_unthrottle(
struct tty_struct *tty)
99 int n = 8192 - to->
buf.memory_used;
117 static int pty_write(
struct tty_struct *tty,
const unsigned char *
buf,
int c)
126 c = tty_insert_flip_string(to, buf, c);
144 static int pty_write_room(
struct tty_struct *tty)
148 return pty_space(tty->
link);
159 static int pty_chars_in_buffer(
struct tty_struct *tty)
165 static int pty_set_lock(
struct tty_struct *tty,
int __user *
arg)
185 pgrp = get_pid(tty->
link->pgrp);
186 spin_unlock_irqrestore(&tty->
link->ctrl_lock, flags);
194 static void pty_flush_buffer(
struct tty_struct *tty)
206 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
214 if (!tty || !tty->
link)
222 if (tty->
link->count != 1)
232 static void pty_set_termios(
struct tty_struct *tty,
236 tty->termios.c_cflag |= (
CS8 |
CREAD);
250 struct pid *pgrp, *rpgrp;
264 pgrp = get_pid(tty->
pgrp);
265 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
268 rpgrp = get_pid(pty->
pgrp);
269 spin_unlock_irqrestore(&pty->
ctrl_lock, flags);
273 if (rpgrp != pgrp && rpgrp)
310 if (!ports[0] || !ports[1])
312 if (!try_module_get(driver->
other->owner)) {
327 goto err_free_termios;
335 o_tty->termios = driver->
other->init_termios;
341 tty_driver_kref_get(driver->
other);
349 o_tty->
port = ports[0];
350 tty->
port = ports[1];
352 tty_driver_kref_get(driver);
360 module_put(o_tty->
driver->owner);
369 static void pty_cleanup(
struct tty_struct *tty)
375 #ifdef CONFIG_LEGACY_PTYS
379 return pty_common_install(driver, tty,
true);
390 static int pty_bsd_ioctl(
struct tty_struct *tty,
391 unsigned int cmd,
unsigned long arg)
395 return pty_set_lock(tty, (
int __user *) arg);
397 return pty_signal(tty, (
int) arg);
402 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
414 .write_room = pty_write_room,
415 .flush_buffer = pty_flush_buffer,
416 .chars_in_buffer = pty_chars_in_buffer,
417 .unthrottle = pty_unthrottle,
418 .set_termios = pty_set_termios,
419 .ioctl = pty_bsd_ioctl,
420 .cleanup = pty_cleanup,
430 .write_room = pty_write_room,
431 .flush_buffer = pty_flush_buffer,
432 .chars_in_buffer = pty_chars_in_buffer,
433 .unthrottle = pty_unthrottle,
434 .set_termios = pty_set_termios,
435 .cleanup = pty_cleanup,
440 static void __init legacy_pty_init(
void)
442 struct tty_driver *pty_driver, *pty_slave_driver;
444 if (legacy_count <= 0)
451 if (IS_ERR(pty_driver))
452 panic(
"Couldn't allocate pty driver");
458 if (IS_ERR(pty_slave_driver))
459 panic(
"Couldn't allocate pty slave driver");
462 pty_driver->
name =
"pty";
474 pty_driver->
other = pty_slave_driver;
478 pty_slave_driver->
name =
"ttyp";
487 pty_slave_driver->
other = pty_driver;
491 panic(
"Couldn't register pty driver");
493 panic(
"Couldn't register pty slave driver");
496 static inline void legacy_pty_init(
void) { }
500 #ifdef CONFIG_UNIX98_PTYS
502 static struct cdev ptmx_cdev;
504 static int pty_unix98_ioctl(
struct tty_struct *tty,
505 unsigned int cmd,
unsigned long arg)
509 return pty_set_lock(tty, (
int __user *)arg);
513 return pty_signal(tty, (
int) arg);
529 struct inode *ptm_inode,
int idx)
532 return ERR_PTR(-
EIO);
545 struct inode *pts_inode,
int idx)
554 return ERR_PTR(-
EIO);
563 return pty_common_install(driver, tty,
false);
571 .
lookup = ptm_unix98_lookup,
572 .install = pty_unix98_install,
573 .remove = pty_unix98_remove,
577 .write_room = pty_write_room,
578 .flush_buffer = pty_flush_buffer,
579 .chars_in_buffer = pty_chars_in_buffer,
580 .unthrottle = pty_unthrottle,
581 .set_termios = pty_set_termios,
582 .ioctl = pty_unix98_ioctl,
584 .cleanup = pty_cleanup
588 .
lookup = pts_unix98_lookup,
589 .install = pty_unix98_install,
590 .remove = pty_unix98_remove,
594 .write_room = pty_write_room,
595 .flush_buffer = pty_flush_buffer,
596 .chars_in_buffer = pty_chars_in_buffer,
597 .unthrottle = pty_unthrottle,
598 .set_termios = pty_set_termios,
599 .cleanup = pty_cleanup,
641 retval = PTR_ERR(tty);
657 retval = ptm_driver->
ops->open(tty, filp);
677 static void __init unix98_pty_init(
void)
685 if (IS_ERR(ptm_driver))
686 panic(
"Couldn't allocate Unix98 ptm driver");
693 if (IS_ERR(pts_driver))
694 panic(
"Couldn't allocate Unix98 pts driver");
697 ptm_driver->
name =
"ptm";
709 ptm_driver->
other = pts_driver;
713 pts_driver->
name =
"pts";
722 pts_driver->
other = ptm_driver;
726 panic(
"Couldn't register Unix98 ptm driver");
728 panic(
"Couldn't register Unix98 pts driver");
732 ptmx_fops.open = ptmx_open;
737 panic(
"Couldn't register /dev/ptmx driver\n");
742 static inline void unix98_pty_init(
void) { }
745 static int __init pty_init(
void)