1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Added support for a Unix98-style ptmx device.
6 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
10 #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/signal.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>
27 #include <linux/poll.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <linux/ioctl.h>
31 #include <linux/compat.h>
33 #undef TTY_DEBUG_HANGUP
34 #ifdef TTY_DEBUG_HANGUP
35 # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
37 # define tty_debug_hangup(tty, f, args...) do {} while (0)
40 #ifdef CONFIG_UNIX98_PTYS
41 static struct tty_driver *ptm_driver;
42 static struct tty_driver *pts_driver;
43 static DEFINE_MUTEX(devpts_mutex);
46 static void pty_close(struct tty_struct *tty, struct file *filp)
48 if (tty->driver->subtype == PTY_TYPE_MASTER)
49 WARN_ON(tty->count > 1);
51 if (tty_io_error(tty))
56 set_bit(TTY_IO_ERROR, &tty->flags);
57 wake_up_interruptible(&tty->read_wait);
58 wake_up_interruptible(&tty->write_wait);
59 spin_lock_irq(&tty->ctrl_lock);
61 spin_unlock_irq(&tty->ctrl_lock);
62 /* Review - krefs on tty_link ?? */
65 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
66 wake_up_interruptible(&tty->link->read_wait);
67 wake_up_interruptible(&tty->link->write_wait);
68 if (tty->driver->subtype == PTY_TYPE_MASTER) {
69 set_bit(TTY_OTHER_CLOSED, &tty->flags);
70 #ifdef CONFIG_UNIX98_PTYS
71 if (tty->driver == ptm_driver) {
72 mutex_lock(&devpts_mutex);
73 if (tty->link->driver_data)
74 devpts_pty_kill(tty->link->driver_data);
75 mutex_unlock(&devpts_mutex);
78 tty_vhangup(tty->link);
83 * The unthrottle routine is called by the line discipline to signal
84 * that it can receive more characters. For PTY's, the TTY_THROTTLED
85 * flag is always set, to force the line discipline to always call the
86 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
87 * characters in the queue. This is necessary since each time this
88 * happens, we need to wake up any sleeping processes that could be
89 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
90 * for the pty buffer to be drained.
92 static void pty_unthrottle(struct tty_struct *tty)
94 tty_wakeup(tty->link);
95 set_bit(TTY_THROTTLED, &tty->flags);
99 * pty_write - write to a pty
100 * @tty: the tty we write from
101 * @buf: kernel buffer of data
104 * Our "hardware" write method. Data is coming from the ldisc which
105 * may be in a non sleeping state. We simply throw this at the other
106 * end of the link as if we were an IRQ handler receiving stuff for
107 * the other side of the pty/tty pair.
110 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
112 struct tty_struct *to = tty->link;
119 spin_lock_irqsave(&to->port->lock, flags);
120 /* Stuff the data into the input queue of the other end */
121 c = tty_insert_flip_string(to->port, buf, c);
122 spin_unlock_irqrestore(&to->port->lock, flags);
125 tty_flip_buffer_push(to->port);
131 * pty_write_room - write space
132 * @tty: tty we are writing from
134 * Report how many bytes the ldisc can send into the queue for
138 static int pty_write_room(struct tty_struct *tty)
142 return tty_buffer_space_avail(tty->link->port);
146 * pty_chars_in_buffer - characters currently in our tx queue
149 * Report how much we have in the transmit queue. As everything is
150 * instantly at the other end this is easy to implement.
153 static int pty_chars_in_buffer(struct tty_struct *tty)
158 /* Set the lock flag on a pty */
159 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
163 if (get_user(val, arg))
166 set_bit(TTY_PTY_LOCK, &tty->flags);
168 clear_bit(TTY_PTY_LOCK, &tty->flags);
172 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
174 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)
184 if (get_user(pktmode, arg))
187 spin_lock_irq(&tty->ctrl_lock);
190 tty->link->ctrl_status = 0;
196 spin_unlock_irq(&tty->ctrl_lock);
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;
206 return put_user(pktmode, arg);
209 /* Send a signal to the slave */
210 static int pty_signal(struct tty_struct *tty, int sig)
214 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
218 pgrp = tty_get_pgrp(tty->link);
220 kill_pgrp(pgrp, sig, 1);
226 static void pty_flush_buffer(struct tty_struct *tty)
228 struct tty_struct *to = tty->link;
233 tty_buffer_flush(to, NULL);
235 spin_lock_irq(&tty->ctrl_lock);
236 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
237 wake_up_interruptible(&to->read_wait);
238 spin_unlock_irq(&tty->ctrl_lock);
242 static int pty_open(struct tty_struct *tty, struct file *filp)
244 if (!tty || !tty->link)
247 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
249 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
251 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
254 clear_bit(TTY_IO_ERROR, &tty->flags);
255 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
256 set_bit(TTY_THROTTLED, &tty->flags);
260 set_bit(TTY_IO_ERROR, &tty->flags);
264 static void pty_set_termios(struct tty_struct *tty,
265 struct ktermios *old_termios)
267 /* See if packet mode change of state. */
268 if (tty->link && tty->link->packet) {
269 int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
270 int old_flow = ((old_termios->c_iflag & IXON) &&
271 (old_termios->c_cc[VSTOP] == '\023') &&
272 (old_termios->c_cc[VSTART] == '\021'));
273 int new_flow = (I_IXON(tty) &&
274 STOP_CHAR(tty) == '\023' &&
275 START_CHAR(tty) == '\021');
276 if ((old_flow != new_flow) || extproc) {
277 spin_lock_irq(&tty->ctrl_lock);
278 if (old_flow != new_flow) {
279 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
281 tty->ctrl_status |= TIOCPKT_DOSTOP;
283 tty->ctrl_status |= TIOCPKT_NOSTOP;
286 tty->ctrl_status |= TIOCPKT_IOCTL;
287 spin_unlock_irq(&tty->ctrl_lock);
288 wake_up_interruptible(&tty->link->read_wait);
292 tty->termios.c_cflag &= ~(CSIZE | PARENB);
293 tty->termios.c_cflag |= (CS8 | CREAD);
297 * pty_do_resize - resize event
298 * @tty: tty being resized
299 * @ws: window size being set.
301 * Update the termios variables and send the necessary signals to
302 * peform a terminal resize correctly
305 static int pty_resize(struct tty_struct *tty, struct winsize *ws)
307 struct pid *pgrp, *rpgrp;
308 struct tty_struct *pty = tty->link;
310 /* For a PTY we need to lock the tty side */
311 mutex_lock(&tty->winsize_mutex);
312 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
315 /* Signal the foreground process group of both ptys */
316 pgrp = tty_get_pgrp(tty);
317 rpgrp = tty_get_pgrp(pty);
320 kill_pgrp(pgrp, SIGWINCH, 1);
321 if (rpgrp != pgrp && rpgrp)
322 kill_pgrp(rpgrp, SIGWINCH, 1);
328 pty->winsize = *ws; /* Never used so will go away soon */
330 mutex_unlock(&tty->winsize_mutex);
335 * pty_start - start() handler
336 * pty_stop - stop() handler
337 * @tty: tty being flow-controlled
339 * Propagates the TIOCPKT status to the master pty.
341 * NB: only the master pty can be in packet mode so only the slave
342 * needs start()/stop() handlers
344 static void pty_start(struct tty_struct *tty)
348 if (tty->link && tty->link->packet) {
349 spin_lock_irqsave(&tty->ctrl_lock, flags);
350 tty->ctrl_status &= ~TIOCPKT_STOP;
351 tty->ctrl_status |= TIOCPKT_START;
352 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
353 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
357 static void pty_stop(struct tty_struct *tty)
361 if (tty->link && tty->link->packet) {
362 spin_lock_irqsave(&tty->ctrl_lock, flags);
363 tty->ctrl_status &= ~TIOCPKT_START;
364 tty->ctrl_status |= TIOCPKT_STOP;
365 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
366 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
371 * pty_common_install - set up the pty pair
372 * @driver: the pty driver
373 * @tty: the tty being instantiated
374 * @legacy: true if this is BSD style
376 * Perform the initial set up for the tty/pty pair. Called from the
377 * tty layer when the port is first opened.
379 * Locking: the caller must hold the tty_mutex
381 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
384 struct tty_struct *o_tty;
385 struct tty_port *ports[2];
386 int idx = tty->index;
387 int retval = -ENOMEM;
389 /* Opening the slave first has always returned -EIO */
390 if (driver->subtype != PTY_TYPE_MASTER)
393 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
394 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
395 if (!ports[0] || !ports[1])
397 if (!try_module_get(driver->other->owner)) {
398 /* This cannot in fact currently happen */
401 o_tty = alloc_tty_struct(driver->other, idx);
405 tty_set_lock_subclass(o_tty);
406 lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
409 /* We always use new tty termios data so we can do this
411 tty_init_termios(tty);
412 tty_init_termios(o_tty);
414 driver->other->ttys[idx] = o_tty;
415 driver->ttys[idx] = tty;
417 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
418 tty->termios = driver->init_termios;
419 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
420 o_tty->termios = driver->other->init_termios;
424 * Everything allocated ... set up the o_tty structure.
426 tty_driver_kref_get(driver->other);
427 /* Establish the links in both directions */
430 tty_port_init(ports[0]);
431 tty_port_init(ports[1]);
432 tty_buffer_set_limit(ports[0], 8192);
433 tty_buffer_set_limit(ports[1], 8192);
434 o_tty->port = ports[0];
435 tty->port = ports[1];
436 o_tty->port->itty = o_tty;
438 tty_buffer_set_lock_subclass(o_tty->port);
440 tty_driver_kref_get(driver);
446 module_put(driver->other->owner);
453 static void pty_cleanup(struct tty_struct *tty)
455 tty_port_put(tty->port);
458 /* Traditional BSD devices */
459 #ifdef CONFIG_LEGACY_PTYS
461 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
463 return pty_common_install(driver, tty, true);
466 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
468 struct tty_struct *pair = tty->link;
470 driver->ttys[tty->index] = NULL;
472 pair->driver->ttys[pair->index] = NULL;
475 static int pty_bsd_ioctl(struct tty_struct *tty,
476 unsigned int cmd, unsigned long arg)
479 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
480 return pty_set_lock(tty, (int __user *) arg);
481 case TIOCGPTLCK: /* Get PT Lock status */
482 return pty_get_lock(tty, (int __user *)arg);
483 case TIOCPKT: /* Set PT packet mode */
484 return pty_set_pktmode(tty, (int __user *)arg);
485 case TIOCGPKT: /* Get PT packet mode */
486 return pty_get_pktmode(tty, (int __user *)arg);
487 case TIOCSIG: /* Send signal to other side of pty */
488 return pty_signal(tty, (int) arg);
489 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
496 static long pty_bsd_compat_ioctl(struct tty_struct *tty,
497 unsigned int cmd, unsigned long arg)
500 * PTY ioctls don't require any special translation between 32-bit and
501 * 64-bit userspace, they are already compatible.
503 return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
506 #define pty_bsd_compat_ioctl NULL
509 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
511 * not really modular, but the easiest way to keep compat with existing
512 * bootargs behaviour is to continue using module_param here.
514 module_param(legacy_count, int, 0);
517 * The master side of a pty can do TIOCSPTLCK and thus
520 static const struct tty_operations master_pty_ops_bsd = {
521 .install = pty_install,
525 .write_room = pty_write_room,
526 .flush_buffer = pty_flush_buffer,
527 .chars_in_buffer = pty_chars_in_buffer,
528 .unthrottle = pty_unthrottle,
529 .ioctl = pty_bsd_ioctl,
530 .compat_ioctl = pty_bsd_compat_ioctl,
531 .cleanup = pty_cleanup,
532 .resize = pty_resize,
536 static const struct tty_operations slave_pty_ops_bsd = {
537 .install = pty_install,
541 .write_room = pty_write_room,
542 .flush_buffer = pty_flush_buffer,
543 .chars_in_buffer = pty_chars_in_buffer,
544 .unthrottle = pty_unthrottle,
545 .set_termios = pty_set_termios,
546 .cleanup = pty_cleanup,
547 .resize = pty_resize,
553 static void __init legacy_pty_init(void)
555 struct tty_driver *pty_driver, *pty_slave_driver;
557 if (legacy_count <= 0)
560 pty_driver = tty_alloc_driver(legacy_count,
561 TTY_DRIVER_RESET_TERMIOS |
562 TTY_DRIVER_REAL_RAW |
563 TTY_DRIVER_DYNAMIC_ALLOC);
564 if (IS_ERR(pty_driver))
565 panic("Couldn't allocate pty driver");
567 pty_slave_driver = tty_alloc_driver(legacy_count,
568 TTY_DRIVER_RESET_TERMIOS |
569 TTY_DRIVER_REAL_RAW |
570 TTY_DRIVER_DYNAMIC_ALLOC);
571 if (IS_ERR(pty_slave_driver))
572 panic("Couldn't allocate pty slave driver");
574 pty_driver->driver_name = "pty_master";
575 pty_driver->name = "pty";
576 pty_driver->major = PTY_MASTER_MAJOR;
577 pty_driver->minor_start = 0;
578 pty_driver->type = TTY_DRIVER_TYPE_PTY;
579 pty_driver->subtype = PTY_TYPE_MASTER;
580 pty_driver->init_termios = tty_std_termios;
581 pty_driver->init_termios.c_iflag = 0;
582 pty_driver->init_termios.c_oflag = 0;
583 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
584 pty_driver->init_termios.c_lflag = 0;
585 pty_driver->init_termios.c_ispeed = 38400;
586 pty_driver->init_termios.c_ospeed = 38400;
587 pty_driver->other = pty_slave_driver;
588 tty_set_operations(pty_driver, &master_pty_ops_bsd);
590 pty_slave_driver->driver_name = "pty_slave";
591 pty_slave_driver->name = "ttyp";
592 pty_slave_driver->major = PTY_SLAVE_MAJOR;
593 pty_slave_driver->minor_start = 0;
594 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
595 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
596 pty_slave_driver->init_termios = tty_std_termios;
597 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
598 pty_slave_driver->init_termios.c_ispeed = 38400;
599 pty_slave_driver->init_termios.c_ospeed = 38400;
600 pty_slave_driver->other = pty_driver;
601 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
603 if (tty_register_driver(pty_driver))
604 panic("Couldn't register pty driver");
605 if (tty_register_driver(pty_slave_driver))
606 panic("Couldn't register pty slave driver");
609 static inline void legacy_pty_init(void) { }
613 #ifdef CONFIG_UNIX98_PTYS
614 static struct cdev ptmx_cdev;
617 * ptm_open_peer - open the peer of a pty
618 * @master: the open struct file of the ptmx device node
619 * @tty: the master of the pty being opened
620 * @flags: the flags for open
622 * Provide a race free way for userspace to open the slave end of a pty
623 * (where they have the master fd and cannot access or trust the mount
624 * namespace /dev/pts was mounted inside).
626 int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
630 int retval = -EINVAL;
633 if (tty->driver != ptm_driver)
636 fd = get_unused_fd_flags(flags);
642 /* Compute the slave's path */
643 path.mnt = devpts_mntget(master, tty->driver_data);
644 if (IS_ERR(path.mnt)) {
645 retval = PTR_ERR(path.mnt);
648 path.dentry = tty->link->driver_data;
650 filp = dentry_open(&path, flags, current_cred());
653 retval = PTR_ERR(filp);
657 fd_install(fd, filp);
666 static int pty_unix98_ioctl(struct tty_struct *tty,
667 unsigned int cmd, unsigned long arg)
670 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
671 return pty_set_lock(tty, (int __user *)arg);
672 case TIOCGPTLCK: /* Get PT Lock status */
673 return pty_get_lock(tty, (int __user *)arg);
674 case TIOCPKT: /* Set PT packet mode */
675 return pty_set_pktmode(tty, (int __user *)arg);
676 case TIOCGPKT: /* Get PT packet mode */
677 return pty_get_pktmode(tty, (int __user *)arg);
678 case TIOCGPTN: /* Get PT Number */
679 return put_user(tty->index, (unsigned int __user *)arg);
680 case TIOCSIG: /* Send signal to other side of pty */
681 return pty_signal(tty, (int) arg);
688 static long pty_unix98_compat_ioctl(struct tty_struct *tty,
689 unsigned int cmd, unsigned long arg)
692 * PTY ioctls don't require any special translation between 32-bit and
693 * 64-bit userspace, they are already compatible.
695 return pty_unix98_ioctl(tty, cmd,
696 cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
699 #define pty_unix98_compat_ioctl NULL
703 * ptm_unix98_lookup - find a pty master
704 * @driver: ptm driver
708 * Look up a pty master device. Called under the tty_mutex for now.
709 * This provides our locking.
712 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
713 struct file *file, int idx)
715 /* Master must be open via /dev/ptmx */
716 return ERR_PTR(-EIO);
720 * pts_unix98_lookup - find a pty slave
721 * @driver: pts driver
722 * @file: file pointer to tty
725 * Look up a pty master device. Called under the tty_mutex for now.
726 * This provides our locking for the tty pointer.
729 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
730 struct file *file, int idx)
732 struct tty_struct *tty;
734 mutex_lock(&devpts_mutex);
735 tty = devpts_get_priv(file->f_path.dentry);
736 mutex_unlock(&devpts_mutex);
737 /* Master must be open before slave */
739 return ERR_PTR(-EIO);
743 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
745 return pty_common_install(driver, tty, false);
748 /* this is called once with whichever end is closed last */
749 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
751 struct pts_fs_info *fsi;
753 if (tty->driver->subtype == PTY_TYPE_MASTER)
754 fsi = tty->driver_data;
756 fsi = tty->link->driver_data;
759 devpts_kill_index(fsi, tty->index);
764 static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
766 seq_printf(m, "tty-index:\t%d\n", tty->index);
769 static const struct tty_operations ptm_unix98_ops = {
770 .lookup = ptm_unix98_lookup,
771 .install = pty_unix98_install,
772 .remove = pty_unix98_remove,
776 .write_room = pty_write_room,
777 .flush_buffer = pty_flush_buffer,
778 .chars_in_buffer = pty_chars_in_buffer,
779 .unthrottle = pty_unthrottle,
780 .ioctl = pty_unix98_ioctl,
781 .compat_ioctl = pty_unix98_compat_ioctl,
782 .resize = pty_resize,
783 .cleanup = pty_cleanup,
784 .show_fdinfo = pty_show_fdinfo,
787 static const struct tty_operations pty_unix98_ops = {
788 .lookup = pts_unix98_lookup,
789 .install = pty_unix98_install,
790 .remove = pty_unix98_remove,
794 .write_room = pty_write_room,
795 .flush_buffer = pty_flush_buffer,
796 .chars_in_buffer = pty_chars_in_buffer,
797 .unthrottle = pty_unthrottle,
798 .set_termios = pty_set_termios,
801 .cleanup = pty_cleanup,
805 * ptmx_open - open a unix 98 pty master
806 * @inode: inode of device file
807 * @filp: file pointer to tty
809 * Allocate a unix98 pty master device from the ptmx driver.
811 * Locking: tty_mutex protects the init_dev work. tty->count should
813 * allocated_ptys_lock handles the list of free pty numbers
816 static int ptmx_open(struct inode *inode, struct file *filp)
818 struct pts_fs_info *fsi;
819 struct tty_struct *tty;
820 struct dentry *dentry;
824 nonseekable_open(inode, filp);
826 /* We refuse fsnotify events on ptmx, since it's a shared resource */
827 filp->f_mode |= FMODE_NONOTIFY;
829 retval = tty_alloc_file(filp);
833 fsi = devpts_acquire(filp);
835 retval = PTR_ERR(fsi);
839 /* find a device that is not in use. */
840 mutex_lock(&devpts_mutex);
841 index = devpts_new_index(fsi);
842 mutex_unlock(&devpts_mutex);
849 mutex_lock(&tty_mutex);
850 tty = tty_init_dev(ptm_driver, index);
851 /* The tty returned here is locked so we can safely
853 mutex_unlock(&tty_mutex);
855 retval = PTR_ERR(tty);
860 * From here on out, the tty is "live", and the index and
861 * fsi will be killed/put by the tty_release()
863 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
864 tty->driver_data = fsi;
866 tty_add_file(tty, filp);
868 dentry = devpts_pty_new(fsi, index, tty->link);
869 if (IS_ERR(dentry)) {
870 retval = PTR_ERR(dentry);
873 tty->link->driver_data = dentry;
875 retval = ptm_driver->ops->open(tty, filp);
879 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
885 // This will also put-ref the fsi
886 tty_release(inode, filp);
889 devpts_kill_index(fsi, index);
897 static struct file_operations ptmx_fops __ro_after_init;
899 static void __init unix98_pty_init(void)
901 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
902 TTY_DRIVER_RESET_TERMIOS |
903 TTY_DRIVER_REAL_RAW |
904 TTY_DRIVER_DYNAMIC_DEV |
905 TTY_DRIVER_DEVPTS_MEM |
906 TTY_DRIVER_DYNAMIC_ALLOC);
907 if (IS_ERR(ptm_driver))
908 panic("Couldn't allocate Unix98 ptm driver");
909 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
910 TTY_DRIVER_RESET_TERMIOS |
911 TTY_DRIVER_REAL_RAW |
912 TTY_DRIVER_DYNAMIC_DEV |
913 TTY_DRIVER_DEVPTS_MEM |
914 TTY_DRIVER_DYNAMIC_ALLOC);
915 if (IS_ERR(pts_driver))
916 panic("Couldn't allocate Unix98 pts driver");
918 ptm_driver->driver_name = "pty_master";
919 ptm_driver->name = "ptm";
920 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
921 ptm_driver->minor_start = 0;
922 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
923 ptm_driver->subtype = PTY_TYPE_MASTER;
924 ptm_driver->init_termios = tty_std_termios;
925 ptm_driver->init_termios.c_iflag = 0;
926 ptm_driver->init_termios.c_oflag = 0;
927 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
928 ptm_driver->init_termios.c_lflag = 0;
929 ptm_driver->init_termios.c_ispeed = 38400;
930 ptm_driver->init_termios.c_ospeed = 38400;
931 ptm_driver->other = pts_driver;
932 tty_set_operations(ptm_driver, &ptm_unix98_ops);
934 pts_driver->driver_name = "pty_slave";
935 pts_driver->name = "pts";
936 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
937 pts_driver->minor_start = 0;
938 pts_driver->type = TTY_DRIVER_TYPE_PTY;
939 pts_driver->subtype = PTY_TYPE_SLAVE;
940 pts_driver->init_termios = tty_std_termios;
941 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
942 pts_driver->init_termios.c_ispeed = 38400;
943 pts_driver->init_termios.c_ospeed = 38400;
944 pts_driver->other = ptm_driver;
945 tty_set_operations(pts_driver, &pty_unix98_ops);
947 if (tty_register_driver(ptm_driver))
948 panic("Couldn't register Unix98 ptm driver");
949 if (tty_register_driver(pts_driver))
950 panic("Couldn't register Unix98 pts driver");
952 /* Now create the /dev/ptmx special device */
953 tty_default_fops(&ptmx_fops);
954 ptmx_fops.open = ptmx_open;
956 cdev_init(&ptmx_cdev, &ptmx_fops);
957 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
958 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
959 panic("Couldn't register /dev/ptmx driver");
960 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
964 static inline void unix98_pty_init(void) { }
967 static int __init pty_init(void)
973 device_initcall(pty_init);