2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
9 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/interrupt.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/fcntl.h>
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include <linux/major.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/uaccess.h>
23 #include <linux/bitops.h>
24 #include <linux/devpts_fs.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
29 #ifdef CONFIG_UNIX98_PTYS
30 static struct tty_driver *ptm_driver;
31 static struct tty_driver *pts_driver;
32 static DEFINE_MUTEX(devpts_mutex);
35 static void pty_close(struct tty_struct *tty, struct file *filp)
38 if (tty->driver->subtype == PTY_TYPE_MASTER)
39 WARN_ON(tty->count > 1);
44 wake_up_interruptible(&tty->read_wait);
45 wake_up_interruptible(&tty->write_wait);
47 /* Review - krefs on tty_link ?? */
50 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
51 wake_up_interruptible(&tty->link->read_wait);
52 wake_up_interruptible(&tty->link->write_wait);
53 if (tty->driver->subtype == PTY_TYPE_MASTER) {
54 set_bit(TTY_OTHER_CLOSED, &tty->flags);
55 #ifdef CONFIG_UNIX98_PTYS
56 if (tty->driver == ptm_driver) {
57 mutex_lock(&devpts_mutex);
58 devpts_pty_kill(tty->link->driver_data);
59 mutex_unlock(&devpts_mutex);
63 tty_vhangup(tty->link);
69 * The unthrottle routine is called by the line discipline to signal
70 * that it can receive more characters. For PTY's, the TTY_THROTTLED
71 * flag is always set, to force the line discipline to always call the
72 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
73 * characters in the queue. This is necessary since each time this
74 * happens, we need to wake up any sleeping processes that could be
75 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
76 * for the pty buffer to be drained.
78 static void pty_unthrottle(struct tty_struct *tty)
80 tty_wakeup(tty->link);
81 set_bit(TTY_THROTTLED, &tty->flags);
85 * pty_space - report space left for writing
86 * @to: tty we are writing into
88 * The tty buffers allow 64K but we sneak a peak and clip at 8K this
89 * allows a lot of overspill room for echo and other fun messes to
93 static int pty_space(struct tty_struct *to)
95 int n = 8192 - to->port->buf.memory_used;
102 * pty_write - write to a pty
103 * @tty: the tty we write from
104 * @buf: kernel buffer of data
105 * @count: bytes to write
107 * Our "hardware" write method. Data is coming from the ldisc which
108 * may be in a non sleeping state. We simply throw this at the other
109 * end of the link as if we were an IRQ handler receiving stuff for
110 * the other side of the pty/tty pair.
113 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
115 struct tty_struct *to = tty->link;
121 /* Stuff the data into the input queue of the other end */
122 c = tty_insert_flip_string(to->port, buf, c);
125 tty_flip_buffer_push(to->port);
133 * pty_write_room - write space
134 * @tty: tty we are writing from
136 * Report how many bytes the ldisc can send into the queue for
140 static int pty_write_room(struct tty_struct *tty)
144 return pty_space(tty->link);
148 * pty_chars_in_buffer - characters currently in our tx queue
151 * Report how much we have in the transmit queue. As everything is
152 * instantly at the other end this is easy to implement.
155 static int pty_chars_in_buffer(struct tty_struct *tty)
160 /* Set the lock flag on a pty */
161 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
164 if (get_user(val, arg))
167 set_bit(TTY_PTY_LOCK, &tty->flags);
169 clear_bit(TTY_PTY_LOCK, &tty->flags);
173 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
175 int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
176 return put_user(locked, arg);
179 /* Set the packet mode on a pty */
180 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
185 if (get_user(pktmode, arg))
188 spin_lock_irqsave(&tty->ctrl_lock, flags);
192 tty->link->ctrl_status = 0;
196 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
201 /* Get the packet mode of a pty */
202 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
204 int pktmode = tty->packet;
205 return put_user(pktmode, arg);
208 /* Send a signal to the slave */
209 static int pty_signal(struct tty_struct *tty, int sig)
215 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
216 pgrp = get_pid(tty->link->pgrp);
217 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
219 kill_pgrp(pgrp, sig, 1);
225 static void pty_flush_buffer(struct tty_struct *tty)
227 struct tty_struct *to = tty->link;
232 /* tty_buffer_flush(to); FIXME */
234 spin_lock_irqsave(&tty->ctrl_lock, flags);
235 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
236 wake_up_interruptible(&to->read_wait);
237 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
241 static int pty_open(struct tty_struct *tty, struct file *filp)
243 int retval = -ENODEV;
245 if (!tty || !tty->link)
249 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
251 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
253 if (tty->link->count != 1)
256 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
257 set_bit(TTY_THROTTLED, &tty->flags);
263 static void pty_set_termios(struct tty_struct *tty,
264 struct ktermios *old_termios)
266 tty->termios.c_cflag &= ~(CSIZE | PARENB);
267 tty->termios.c_cflag |= (CS8 | CREAD);
271 * pty_do_resize - resize event
272 * @tty: tty being resized
273 * @ws: window size being set.
275 * Update the termios variables and send the necessary signals to
276 * peform a terminal resize correctly
279 static int pty_resize(struct tty_struct *tty, struct winsize *ws)
281 struct pid *pgrp, *rpgrp;
283 struct tty_struct *pty = tty->link;
285 /* For a PTY we need to lock the tty side */
286 mutex_lock(&tty->termios_mutex);
287 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
290 /* Get the PID values and reference them so we can
291 avoid holding the tty ctrl lock while sending signals.
292 We need to lock these individually however. */
294 spin_lock_irqsave(&tty->ctrl_lock, flags);
295 pgrp = get_pid(tty->pgrp);
296 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
298 spin_lock_irqsave(&pty->ctrl_lock, flags);
299 rpgrp = get_pid(pty->pgrp);
300 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
303 kill_pgrp(pgrp, SIGWINCH, 1);
304 if (rpgrp != pgrp && rpgrp)
305 kill_pgrp(rpgrp, SIGWINCH, 1);
311 pty->winsize = *ws; /* Never used so will go away soon */
313 mutex_unlock(&tty->termios_mutex);
318 * pty_common_install - set up the pty pair
319 * @driver: the pty driver
320 * @tty: the tty being instantiated
321 * @bool: legacy, true if this is BSD style
323 * Perform the initial set up for the tty/pty pair. Called from the
324 * tty layer when the port is first opened.
326 * Locking: the caller must hold the tty_mutex
328 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
331 struct tty_struct *o_tty;
332 struct tty_port *ports[2];
333 int idx = tty->index;
334 int retval = -ENOMEM;
336 o_tty = alloc_tty_struct();
339 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
340 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
341 if (!ports[0] || !ports[1])
343 if (!try_module_get(driver->other->owner)) {
344 /* This cannot in fact currently happen */
347 initialize_tty_struct(o_tty, driver->other, idx);
350 /* We always use new tty termios data so we can do this
352 retval = tty_init_termios(tty);
356 retval = tty_init_termios(o_tty);
358 goto err_free_termios;
360 driver->other->ttys[idx] = o_tty;
361 driver->ttys[idx] = tty;
363 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
364 tty->termios = driver->init_termios;
365 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
366 o_tty->termios = driver->other->init_termios;
370 * Everything allocated ... set up the o_tty structure.
372 tty_driver_kref_get(driver->other);
373 if (driver->subtype == PTY_TYPE_MASTER)
375 /* Establish the links in both directions */
378 tty_port_init(ports[0]);
379 tty_port_init(ports[1]);
380 o_tty->port = ports[0];
381 tty->port = ports[1];
382 o_tty->port->itty = o_tty;
384 tty_driver_kref_get(driver);
389 tty_free_termios(tty);
391 deinitialize_tty_struct(o_tty);
392 module_put(o_tty->driver->owner);
396 free_tty_struct(o_tty);
401 /* this is called once with whichever end is closed last */
402 static void pty_unix98_shutdown(struct tty_struct *tty)
404 devpts_kill_index(tty->driver_data, tty->index);
407 static void pty_cleanup(struct tty_struct *tty)
409 tty->port->itty = NULL;
410 tty_port_put(tty->port);
413 /* Traditional BSD devices */
414 #ifdef CONFIG_LEGACY_PTYS
416 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
418 return pty_common_install(driver, tty, true);
421 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
423 struct tty_struct *pair = tty->link;
424 driver->ttys[tty->index] = NULL;
426 pair->driver->ttys[pair->index] = NULL;
429 static int pty_bsd_ioctl(struct tty_struct *tty,
430 unsigned int cmd, unsigned long arg)
433 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
434 return pty_set_lock(tty, (int __user *) arg);
435 case TIOCGPTLCK: /* Get PT Lock status */
436 return pty_get_lock(tty, (int __user *)arg);
437 case TIOCPKT: /* Set PT packet mode */
438 return pty_set_pktmode(tty, (int __user *)arg);
439 case TIOCGPKT: /* Get PT packet mode */
440 return pty_get_pktmode(tty, (int __user *)arg);
441 case TIOCSIG: /* Send signal to other side of pty */
442 return pty_signal(tty, (int) arg);
447 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
448 module_param(legacy_count, int, 0);
451 * The master side of a pty can do TIOCSPTLCK and thus
454 static const struct tty_operations master_pty_ops_bsd = {
455 .install = pty_install,
459 .write_room = pty_write_room,
460 .flush_buffer = pty_flush_buffer,
461 .chars_in_buffer = pty_chars_in_buffer,
462 .unthrottle = pty_unthrottle,
463 .set_termios = pty_set_termios,
464 .ioctl = pty_bsd_ioctl,
465 .cleanup = pty_cleanup,
466 .resize = pty_resize,
470 static const struct tty_operations slave_pty_ops_bsd = {
471 .install = pty_install,
475 .write_room = pty_write_room,
476 .flush_buffer = pty_flush_buffer,
477 .chars_in_buffer = pty_chars_in_buffer,
478 .unthrottle = pty_unthrottle,
479 .set_termios = pty_set_termios,
480 .cleanup = pty_cleanup,
481 .resize = pty_resize,
485 static void __init legacy_pty_init(void)
487 struct tty_driver *pty_driver, *pty_slave_driver;
489 if (legacy_count <= 0)
492 pty_driver = tty_alloc_driver(legacy_count,
493 TTY_DRIVER_RESET_TERMIOS |
494 TTY_DRIVER_REAL_RAW |
495 TTY_DRIVER_DYNAMIC_ALLOC);
496 if (IS_ERR(pty_driver))
497 panic("Couldn't allocate pty driver");
499 pty_slave_driver = tty_alloc_driver(legacy_count,
500 TTY_DRIVER_RESET_TERMIOS |
501 TTY_DRIVER_REAL_RAW |
502 TTY_DRIVER_DYNAMIC_ALLOC);
503 if (IS_ERR(pty_slave_driver))
504 panic("Couldn't allocate pty slave driver");
506 pty_driver->driver_name = "pty_master";
507 pty_driver->name = "pty";
508 pty_driver->major = PTY_MASTER_MAJOR;
509 pty_driver->minor_start = 0;
510 pty_driver->type = TTY_DRIVER_TYPE_PTY;
511 pty_driver->subtype = PTY_TYPE_MASTER;
512 pty_driver->init_termios = tty_std_termios;
513 pty_driver->init_termios.c_iflag = 0;
514 pty_driver->init_termios.c_oflag = 0;
515 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
516 pty_driver->init_termios.c_lflag = 0;
517 pty_driver->init_termios.c_ispeed = 38400;
518 pty_driver->init_termios.c_ospeed = 38400;
519 pty_driver->other = pty_slave_driver;
520 tty_set_operations(pty_driver, &master_pty_ops_bsd);
522 pty_slave_driver->driver_name = "pty_slave";
523 pty_slave_driver->name = "ttyp";
524 pty_slave_driver->major = PTY_SLAVE_MAJOR;
525 pty_slave_driver->minor_start = 0;
526 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
527 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
528 pty_slave_driver->init_termios = tty_std_termios;
529 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
530 pty_slave_driver->init_termios.c_ispeed = 38400;
531 pty_slave_driver->init_termios.c_ospeed = 38400;
532 pty_slave_driver->other = pty_driver;
533 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
535 if (tty_register_driver(pty_driver))
536 panic("Couldn't register pty driver");
537 if (tty_register_driver(pty_slave_driver))
538 panic("Couldn't register pty slave driver");
541 static inline void legacy_pty_init(void) { }
545 #ifdef CONFIG_UNIX98_PTYS
547 static struct cdev ptmx_cdev;
549 static int pty_unix98_ioctl(struct tty_struct *tty,
550 unsigned int cmd, unsigned long arg)
553 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
554 return pty_set_lock(tty, (int __user *)arg);
555 case TIOCGPTLCK: /* Get PT Lock status */
556 return pty_get_lock(tty, (int __user *)arg);
557 case TIOCPKT: /* Set PT packet mode */
558 return pty_set_pktmode(tty, (int __user *)arg);
559 case TIOCGPKT: /* Get PT packet mode */
560 return pty_get_pktmode(tty, (int __user *)arg);
561 case TIOCGPTN: /* Get PT Number */
562 return put_user(tty->index, (unsigned int __user *)arg);
563 case TIOCSIG: /* Send signal to other side of pty */
564 return pty_signal(tty, (int) arg);
571 * ptm_unix98_lookup - find a pty master
572 * @driver: ptm driver
575 * Look up a pty master device. Called under the tty_mutex for now.
576 * This provides our locking.
579 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
580 struct inode *ptm_inode, int idx)
582 /* Master must be open via /dev/ptmx */
583 return ERR_PTR(-EIO);
587 * pts_unix98_lookup - find a pty slave
588 * @driver: pts driver
591 * Look up a pty master device. Called under the tty_mutex for now.
592 * This provides our locking for the tty pointer.
595 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
596 struct inode *pts_inode, int idx)
598 struct tty_struct *tty;
600 mutex_lock(&devpts_mutex);
601 tty = devpts_get_priv(pts_inode);
602 mutex_unlock(&devpts_mutex);
603 /* Master must be open before slave */
605 return ERR_PTR(-EIO);
609 /* We have no need to install and remove our tty objects as devpts does all
612 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
614 return pty_common_install(driver, tty, false);
617 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
621 static const struct tty_operations ptm_unix98_ops = {
622 .lookup = ptm_unix98_lookup,
623 .install = pty_unix98_install,
624 .remove = pty_unix98_remove,
628 .write_room = pty_write_room,
629 .flush_buffer = pty_flush_buffer,
630 .chars_in_buffer = pty_chars_in_buffer,
631 .unthrottle = pty_unthrottle,
632 .set_termios = pty_set_termios,
633 .ioctl = pty_unix98_ioctl,
634 .resize = pty_resize,
635 .shutdown = pty_unix98_shutdown,
636 .cleanup = pty_cleanup
639 static const struct tty_operations pty_unix98_ops = {
640 .lookup = pts_unix98_lookup,
641 .install = pty_unix98_install,
642 .remove = pty_unix98_remove,
646 .write_room = pty_write_room,
647 .flush_buffer = pty_flush_buffer,
648 .chars_in_buffer = pty_chars_in_buffer,
649 .unthrottle = pty_unthrottle,
650 .set_termios = pty_set_termios,
651 .shutdown = pty_unix98_shutdown,
652 .cleanup = pty_cleanup,
656 * ptmx_open - open a unix 98 pty master
657 * @inode: inode of device file
658 * @filp: file pointer to tty
660 * Allocate a unix98 pty master device from the ptmx driver.
662 * Locking: tty_mutex protects the init_dev work. tty->count should
664 * allocated_ptys_lock handles the list of free pty numbers
667 static int ptmx_open(struct inode *inode, struct file *filp)
669 struct tty_struct *tty;
670 struct inode *slave_inode;
674 nonseekable_open(inode, filp);
676 retval = tty_alloc_file(filp);
680 /* find a device that is not in use. */
681 mutex_lock(&devpts_mutex);
682 index = devpts_new_index(inode);
685 mutex_unlock(&devpts_mutex);
689 mutex_unlock(&devpts_mutex);
691 mutex_lock(&tty_mutex);
692 tty = tty_init_dev(ptm_driver, index);
695 retval = PTR_ERR(tty);
699 /* The tty returned here is locked so we can safely
701 mutex_unlock(&tty_mutex);
703 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
705 tty_add_file(tty, filp);
707 slave_inode = devpts_pty_new(inode,
708 MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
710 if (IS_ERR(slave_inode)) {
711 retval = PTR_ERR(slave_inode);
715 retval = ptm_driver->ops->open(tty, filp);
720 tty->driver_data = inode;
721 tty->link->driver_data = slave_inode;
725 tty_release(inode, filp);
728 mutex_unlock(&tty_mutex);
729 devpts_kill_index(inode, index);
735 static struct file_operations ptmx_fops;
737 static void __init unix98_pty_init(void)
739 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
740 TTY_DRIVER_RESET_TERMIOS |
741 TTY_DRIVER_REAL_RAW |
742 TTY_DRIVER_DYNAMIC_DEV |
743 TTY_DRIVER_DEVPTS_MEM |
744 TTY_DRIVER_DYNAMIC_ALLOC);
745 if (IS_ERR(ptm_driver))
746 panic("Couldn't allocate Unix98 ptm driver");
747 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
748 TTY_DRIVER_RESET_TERMIOS |
749 TTY_DRIVER_REAL_RAW |
750 TTY_DRIVER_DYNAMIC_DEV |
751 TTY_DRIVER_DEVPTS_MEM |
752 TTY_DRIVER_DYNAMIC_ALLOC);
753 if (IS_ERR(pts_driver))
754 panic("Couldn't allocate Unix98 pts driver");
756 ptm_driver->driver_name = "pty_master";
757 ptm_driver->name = "ptm";
758 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
759 ptm_driver->minor_start = 0;
760 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
761 ptm_driver->subtype = PTY_TYPE_MASTER;
762 ptm_driver->init_termios = tty_std_termios;
763 ptm_driver->init_termios.c_iflag = 0;
764 ptm_driver->init_termios.c_oflag = 0;
765 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
766 ptm_driver->init_termios.c_lflag = 0;
767 ptm_driver->init_termios.c_ispeed = 38400;
768 ptm_driver->init_termios.c_ospeed = 38400;
769 ptm_driver->other = pts_driver;
770 tty_set_operations(ptm_driver, &ptm_unix98_ops);
772 pts_driver->driver_name = "pty_slave";
773 pts_driver->name = "pts";
774 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
775 pts_driver->minor_start = 0;
776 pts_driver->type = TTY_DRIVER_TYPE_PTY;
777 pts_driver->subtype = PTY_TYPE_SLAVE;
778 pts_driver->init_termios = tty_std_termios;
779 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
780 pts_driver->init_termios.c_ispeed = 38400;
781 pts_driver->init_termios.c_ospeed = 38400;
782 pts_driver->other = ptm_driver;
783 tty_set_operations(pts_driver, &pty_unix98_ops);
785 if (tty_register_driver(ptm_driver))
786 panic("Couldn't register Unix98 ptm driver");
787 if (tty_register_driver(pts_driver))
788 panic("Couldn't register Unix98 pts driver");
790 /* Now create the /dev/ptmx special device */
791 tty_default_fops(&ptmx_fops);
792 ptmx_fops.open = ptmx_open;
794 cdev_init(&ptmx_cdev, &ptmx_fops);
795 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
796 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
797 panic("Couldn't register /dev/ptmx driver");
798 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
802 static inline void unix98_pty_init(void) { }
805 static int __init pty_init(void)
811 module_init(pty_init);