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>
34 #undef TTY_DEBUG_HANGUP
35 #ifdef TTY_DEBUG_HANGUP
36 # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
38 # define tty_debug_hangup(tty, f, args...) do {} while (0)
41 #ifdef CONFIG_UNIX98_PTYS
42 static struct tty_driver *ptm_driver;
43 static struct tty_driver *pts_driver;
44 static DEFINE_MUTEX(devpts_mutex);
47 static void pty_close(struct tty_struct *tty, struct file *filp)
50 if (tty->driver->subtype == PTY_TYPE_MASTER)
51 WARN_ON(tty->count > 1);
53 if (tty_io_error(tty))
58 set_bit(TTY_IO_ERROR, &tty->flags);
59 wake_up_interruptible(&tty->read_wait);
60 wake_up_interruptible(&tty->write_wait);
61 spin_lock_irq(&tty->ctrl_lock);
63 spin_unlock_irq(&tty->ctrl_lock);
64 /* Review - krefs on tty_link ?? */
67 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
68 wake_up_interruptible(&tty->link->read_wait);
69 wake_up_interruptible(&tty->link->write_wait);
70 if (tty->driver->subtype == PTY_TYPE_MASTER) {
71 set_bit(TTY_OTHER_CLOSED, &tty->flags);
72 #ifdef CONFIG_UNIX98_PTYS
73 if (tty->driver == ptm_driver) {
74 mutex_lock(&devpts_mutex);
75 if (tty->link->driver_data)
76 devpts_pty_kill(tty->link->driver_data);
77 mutex_unlock(&devpts_mutex);
80 tty_vhangup(tty->link);
85 * The unthrottle routine is called by the line discipline to signal
86 * that it can receive more characters. For PTY's, the TTY_THROTTLED
87 * flag is always set, to force the line discipline to always call the
88 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
89 * characters in the queue. This is necessary since each time this
90 * happens, we need to wake up any sleeping processes that could be
91 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
92 * for the pty buffer to be drained.
94 static void pty_unthrottle(struct tty_struct *tty)
96 tty_wakeup(tty->link);
97 set_bit(TTY_THROTTLED, &tty->flags);
101 * pty_write - write to a pty
102 * @tty: the tty we write from
103 * @buf: kernel buffer of data
106 * Our "hardware" write method. Data is coming from the ldisc which
107 * may be in a non sleeping state. We simply throw this at the other
108 * end of the link as if we were an IRQ handler receiving stuff for
109 * the other side of the pty/tty pair.
112 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
114 struct tty_struct *to = tty->link;
116 if (tty->stopped || !c)
119 return tty_insert_flip_string_and_push_buffer(to->port, buf, c);
123 * pty_write_room - write space
124 * @tty: tty we are writing from
126 * Report how many bytes the ldisc can send into the queue for
130 static int pty_write_room(struct tty_struct *tty)
134 return tty_buffer_space_avail(tty->link->port);
138 * pty_chars_in_buffer - characters currently in our tx queue
141 * Report how much we have in the transmit queue. As everything is
142 * instantly at the other end this is easy to implement.
145 static int pty_chars_in_buffer(struct tty_struct *tty)
150 /* Set the lock flag on a pty */
151 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
154 if (get_user(val, arg))
157 set_bit(TTY_PTY_LOCK, &tty->flags);
159 clear_bit(TTY_PTY_LOCK, &tty->flags);
163 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
165 int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
166 return put_user(locked, arg);
169 /* Set the packet mode on a pty */
170 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
174 if (get_user(pktmode, arg))
177 spin_lock_irq(&tty->ctrl_lock);
180 tty->link->ctrl_status = 0;
186 spin_unlock_irq(&tty->ctrl_lock);
191 /* Get the packet mode of a pty */
192 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
194 int pktmode = tty->packet;
195 return put_user(pktmode, arg);
198 /* Send a signal to the slave */
199 static int pty_signal(struct tty_struct *tty, int sig)
203 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
207 pgrp = tty_get_pgrp(tty->link);
209 kill_pgrp(pgrp, sig, 1);
215 static void pty_flush_buffer(struct tty_struct *tty)
217 struct tty_struct *to = tty->link;
222 tty_buffer_flush(to, NULL);
224 spin_lock_irq(&tty->ctrl_lock);
225 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
226 wake_up_interruptible(&to->read_wait);
227 spin_unlock_irq(&tty->ctrl_lock);
231 static int pty_open(struct tty_struct *tty, struct file *filp)
233 if (!tty || !tty->link)
236 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
238 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
240 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
243 clear_bit(TTY_IO_ERROR, &tty->flags);
244 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
245 set_bit(TTY_THROTTLED, &tty->flags);
249 set_bit(TTY_IO_ERROR, &tty->flags);
253 static void pty_set_termios(struct tty_struct *tty,
254 struct ktermios *old_termios)
256 /* See if packet mode change of state. */
257 if (tty->link && tty->link->packet) {
258 int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
259 int old_flow = ((old_termios->c_iflag & IXON) &&
260 (old_termios->c_cc[VSTOP] == '\023') &&
261 (old_termios->c_cc[VSTART] == '\021'));
262 int new_flow = (I_IXON(tty) &&
263 STOP_CHAR(tty) == '\023' &&
264 START_CHAR(tty) == '\021');
265 if ((old_flow != new_flow) || extproc) {
266 spin_lock_irq(&tty->ctrl_lock);
267 if (old_flow != new_flow) {
268 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
270 tty->ctrl_status |= TIOCPKT_DOSTOP;
272 tty->ctrl_status |= TIOCPKT_NOSTOP;
275 tty->ctrl_status |= TIOCPKT_IOCTL;
276 spin_unlock_irq(&tty->ctrl_lock);
277 wake_up_interruptible(&tty->link->read_wait);
281 tty->termios.c_cflag &= ~(CSIZE | PARENB);
282 tty->termios.c_cflag |= (CS8 | CREAD);
286 * pty_do_resize - resize event
287 * @tty: tty being resized
288 * @ws: window size being set.
290 * Update the termios variables and send the necessary signals to
291 * peform a terminal resize correctly
294 static int pty_resize(struct tty_struct *tty, struct winsize *ws)
296 struct pid *pgrp, *rpgrp;
297 struct tty_struct *pty = tty->link;
299 /* For a PTY we need to lock the tty side */
300 mutex_lock(&tty->winsize_mutex);
301 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
304 /* Signal the foreground process group of both ptys */
305 pgrp = tty_get_pgrp(tty);
306 rpgrp = tty_get_pgrp(pty);
309 kill_pgrp(pgrp, SIGWINCH, 1);
310 if (rpgrp != pgrp && rpgrp)
311 kill_pgrp(rpgrp, SIGWINCH, 1);
317 pty->winsize = *ws; /* Never used so will go away soon */
319 mutex_unlock(&tty->winsize_mutex);
324 * pty_start - start() handler
325 * pty_stop - stop() handler
326 * @tty: tty being flow-controlled
328 * Propagates the TIOCPKT status to the master pty.
330 * NB: only the master pty can be in packet mode so only the slave
331 * needs start()/stop() handlers
333 static void pty_start(struct tty_struct *tty)
337 if (tty->link && tty->link->packet) {
338 spin_lock_irqsave(&tty->ctrl_lock, flags);
339 tty->ctrl_status &= ~TIOCPKT_STOP;
340 tty->ctrl_status |= TIOCPKT_START;
341 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
342 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
346 static void pty_stop(struct tty_struct *tty)
350 if (tty->link && tty->link->packet) {
351 spin_lock_irqsave(&tty->ctrl_lock, flags);
352 tty->ctrl_status &= ~TIOCPKT_START;
353 tty->ctrl_status |= TIOCPKT_STOP;
354 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
355 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
360 * pty_common_install - set up the pty pair
361 * @driver: the pty driver
362 * @tty: the tty being instantiated
363 * @legacy: true if this is BSD style
365 * Perform the initial set up for the tty/pty pair. Called from the
366 * tty layer when the port is first opened.
368 * Locking: the caller must hold the tty_mutex
370 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
373 struct tty_struct *o_tty;
374 struct tty_port *ports[2];
375 int idx = tty->index;
376 int retval = -ENOMEM;
378 /* Opening the slave first has always returned -EIO */
379 if (driver->subtype != PTY_TYPE_MASTER)
382 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
383 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
384 if (!ports[0] || !ports[1])
386 if (!try_module_get(driver->other->owner)) {
387 /* This cannot in fact currently happen */
390 o_tty = alloc_tty_struct(driver->other, idx);
394 tty_set_lock_subclass(o_tty);
395 lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
398 /* We always use new tty termios data so we can do this
400 tty_init_termios(tty);
401 tty_init_termios(o_tty);
403 driver->other->ttys[idx] = o_tty;
404 driver->ttys[idx] = tty;
406 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
407 tty->termios = driver->init_termios;
408 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
409 o_tty->termios = driver->other->init_termios;
413 * Everything allocated ... set up the o_tty structure.
415 tty_driver_kref_get(driver->other);
416 /* Establish the links in both directions */
419 tty_port_init(ports[0]);
420 tty_port_init(ports[1]);
421 tty_buffer_set_limit(ports[0], 8192);
422 tty_buffer_set_limit(ports[1], 8192);
423 o_tty->port = ports[0];
424 tty->port = ports[1];
425 o_tty->port->itty = o_tty;
427 tty_buffer_set_lock_subclass(o_tty->port);
429 tty_driver_kref_get(driver);
435 module_put(driver->other->owner);
442 static void pty_cleanup(struct tty_struct *tty)
444 tty_port_put(tty->port);
447 /* Traditional BSD devices */
448 #ifdef CONFIG_LEGACY_PTYS
450 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
452 return pty_common_install(driver, tty, true);
455 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
457 struct tty_struct *pair = tty->link;
458 driver->ttys[tty->index] = NULL;
460 pair->driver->ttys[pair->index] = NULL;
463 static int pty_bsd_ioctl(struct tty_struct *tty,
464 unsigned int cmd, unsigned long arg)
467 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
468 return pty_set_lock(tty, (int __user *) arg);
469 case TIOCGPTLCK: /* Get PT Lock status */
470 return pty_get_lock(tty, (int __user *)arg);
471 case TIOCPKT: /* Set PT packet mode */
472 return pty_set_pktmode(tty, (int __user *)arg);
473 case TIOCGPKT: /* Get PT packet mode */
474 return pty_get_pktmode(tty, (int __user *)arg);
475 case TIOCSIG: /* Send signal to other side of pty */
476 return pty_signal(tty, (int) arg);
477 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
484 static long pty_bsd_compat_ioctl(struct tty_struct *tty,
485 unsigned int cmd, unsigned long arg)
488 * PTY ioctls don't require any special translation between 32-bit and
489 * 64-bit userspace, they are already compatible.
491 return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
494 #define pty_bsd_compat_ioctl NULL
497 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
499 * not really modular, but the easiest way to keep compat with existing
500 * bootargs behaviour is to continue using module_param here.
502 module_param(legacy_count, int, 0);
505 * The master side of a pty can do TIOCSPTLCK and thus
508 static const struct tty_operations master_pty_ops_bsd = {
509 .install = pty_install,
513 .write_room = pty_write_room,
514 .flush_buffer = pty_flush_buffer,
515 .chars_in_buffer = pty_chars_in_buffer,
516 .unthrottle = pty_unthrottle,
517 .ioctl = pty_bsd_ioctl,
518 .compat_ioctl = pty_bsd_compat_ioctl,
519 .cleanup = pty_cleanup,
520 .resize = pty_resize,
524 static const struct tty_operations slave_pty_ops_bsd = {
525 .install = pty_install,
529 .write_room = pty_write_room,
530 .flush_buffer = pty_flush_buffer,
531 .chars_in_buffer = pty_chars_in_buffer,
532 .unthrottle = pty_unthrottle,
533 .set_termios = pty_set_termios,
534 .cleanup = pty_cleanup,
535 .resize = pty_resize,
541 static void __init legacy_pty_init(void)
543 struct tty_driver *pty_driver, *pty_slave_driver;
545 if (legacy_count <= 0)
548 pty_driver = tty_alloc_driver(legacy_count,
549 TTY_DRIVER_RESET_TERMIOS |
550 TTY_DRIVER_REAL_RAW |
551 TTY_DRIVER_DYNAMIC_ALLOC);
552 if (IS_ERR(pty_driver))
553 panic("Couldn't allocate pty driver");
555 pty_slave_driver = tty_alloc_driver(legacy_count,
556 TTY_DRIVER_RESET_TERMIOS |
557 TTY_DRIVER_REAL_RAW |
558 TTY_DRIVER_DYNAMIC_ALLOC);
559 if (IS_ERR(pty_slave_driver))
560 panic("Couldn't allocate pty slave driver");
562 pty_driver->driver_name = "pty_master";
563 pty_driver->name = "pty";
564 pty_driver->major = PTY_MASTER_MAJOR;
565 pty_driver->minor_start = 0;
566 pty_driver->type = TTY_DRIVER_TYPE_PTY;
567 pty_driver->subtype = PTY_TYPE_MASTER;
568 pty_driver->init_termios = tty_std_termios;
569 pty_driver->init_termios.c_iflag = 0;
570 pty_driver->init_termios.c_oflag = 0;
571 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
572 pty_driver->init_termios.c_lflag = 0;
573 pty_driver->init_termios.c_ispeed = 38400;
574 pty_driver->init_termios.c_ospeed = 38400;
575 pty_driver->other = pty_slave_driver;
576 tty_set_operations(pty_driver, &master_pty_ops_bsd);
578 pty_slave_driver->driver_name = "pty_slave";
579 pty_slave_driver->name = "ttyp";
580 pty_slave_driver->major = PTY_SLAVE_MAJOR;
581 pty_slave_driver->minor_start = 0;
582 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
583 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
584 pty_slave_driver->init_termios = tty_std_termios;
585 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
586 pty_slave_driver->init_termios.c_ispeed = 38400;
587 pty_slave_driver->init_termios.c_ospeed = 38400;
588 pty_slave_driver->other = pty_driver;
589 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
591 if (tty_register_driver(pty_driver))
592 panic("Couldn't register pty driver");
593 if (tty_register_driver(pty_slave_driver))
594 panic("Couldn't register pty slave driver");
597 static inline void legacy_pty_init(void) { }
601 #ifdef CONFIG_UNIX98_PTYS
602 static struct cdev ptmx_cdev;
605 * ptm_open_peer - open the peer of a pty
606 * @master: the open struct file of the ptmx device node
607 * @tty: the master of the pty being opened
608 * @flags: the flags for open
610 * Provide a race free way for userspace to open the slave end of a pty
611 * (where they have the master fd and cannot access or trust the mount
612 * namespace /dev/pts was mounted inside).
614 int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
618 int retval = -EINVAL;
621 if (tty->driver != ptm_driver)
624 fd = get_unused_fd_flags(flags);
630 /* Compute the slave's path */
631 path.mnt = devpts_mntget(master, tty->driver_data);
632 if (IS_ERR(path.mnt)) {
633 retval = PTR_ERR(path.mnt);
636 path.dentry = tty->link->driver_data;
638 filp = dentry_open(&path, flags, current_cred());
641 retval = PTR_ERR(filp);
645 fd_install(fd, filp);
654 static int pty_unix98_ioctl(struct tty_struct *tty,
655 unsigned int cmd, unsigned long arg)
658 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
659 return pty_set_lock(tty, (int __user *)arg);
660 case TIOCGPTLCK: /* Get PT Lock status */
661 return pty_get_lock(tty, (int __user *)arg);
662 case TIOCPKT: /* Set PT packet mode */
663 return pty_set_pktmode(tty, (int __user *)arg);
664 case TIOCGPKT: /* Get PT packet mode */
665 return pty_get_pktmode(tty, (int __user *)arg);
666 case TIOCGPTN: /* Get PT Number */
667 return put_user(tty->index, (unsigned int __user *)arg);
668 case TIOCSIG: /* Send signal to other side of pty */
669 return pty_signal(tty, (int) arg);
676 static long pty_unix98_compat_ioctl(struct tty_struct *tty,
677 unsigned int cmd, unsigned long arg)
680 * PTY ioctls don't require any special translation between 32-bit and
681 * 64-bit userspace, they are already compatible.
683 return pty_unix98_ioctl(tty, cmd,
684 cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
687 #define pty_unix98_compat_ioctl NULL
691 * ptm_unix98_lookup - find a pty master
692 * @driver: ptm driver
695 * Look up a pty master device. Called under the tty_mutex for now.
696 * This provides our locking.
699 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
700 struct file *file, int idx)
702 /* Master must be open via /dev/ptmx */
703 return ERR_PTR(-EIO);
707 * pts_unix98_lookup - find a pty slave
708 * @driver: pts driver
711 * Look up a pty master device. Called under the tty_mutex for now.
712 * This provides our locking for the tty pointer.
715 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
716 struct file *file, int idx)
718 struct tty_struct *tty;
720 mutex_lock(&devpts_mutex);
721 tty = devpts_get_priv(file->f_path.dentry);
722 mutex_unlock(&devpts_mutex);
723 /* Master must be open before slave */
725 return ERR_PTR(-EIO);
729 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
731 return pty_common_install(driver, tty, false);
734 /* this is called once with whichever end is closed last */
735 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
737 struct pts_fs_info *fsi;
739 if (tty->driver->subtype == PTY_TYPE_MASTER)
740 fsi = tty->driver_data;
742 fsi = tty->link->driver_data;
745 devpts_kill_index(fsi, tty->index);
750 static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
752 seq_printf(m, "tty-index:\t%d\n", tty->index);
755 static const struct tty_operations ptm_unix98_ops = {
756 .lookup = ptm_unix98_lookup,
757 .install = pty_unix98_install,
758 .remove = pty_unix98_remove,
762 .write_room = pty_write_room,
763 .flush_buffer = pty_flush_buffer,
764 .chars_in_buffer = pty_chars_in_buffer,
765 .unthrottle = pty_unthrottle,
766 .ioctl = pty_unix98_ioctl,
767 .compat_ioctl = pty_unix98_compat_ioctl,
768 .resize = pty_resize,
769 .cleanup = pty_cleanup,
770 .show_fdinfo = pty_show_fdinfo,
773 static const struct tty_operations pty_unix98_ops = {
774 .lookup = pts_unix98_lookup,
775 .install = pty_unix98_install,
776 .remove = pty_unix98_remove,
780 .write_room = pty_write_room,
781 .flush_buffer = pty_flush_buffer,
782 .chars_in_buffer = pty_chars_in_buffer,
783 .unthrottle = pty_unthrottle,
784 .set_termios = pty_set_termios,
787 .cleanup = pty_cleanup,
791 * ptmx_open - open a unix 98 pty master
792 * @inode: inode of device file
793 * @filp: file pointer to tty
795 * Allocate a unix98 pty master device from the ptmx driver.
797 * Locking: tty_mutex protects the init_dev work. tty->count should
799 * allocated_ptys_lock handles the list of free pty numbers
802 static int ptmx_open(struct inode *inode, struct file *filp)
804 struct pts_fs_info *fsi;
805 struct tty_struct *tty;
806 struct dentry *dentry;
810 nonseekable_open(inode, filp);
812 /* We refuse fsnotify events on ptmx, since it's a shared resource */
813 filp->f_mode |= FMODE_NONOTIFY;
815 retval = tty_alloc_file(filp);
819 fsi = devpts_acquire(filp);
821 retval = PTR_ERR(fsi);
825 /* find a device that is not in use. */
826 mutex_lock(&devpts_mutex);
827 index = devpts_new_index(fsi);
828 mutex_unlock(&devpts_mutex);
835 mutex_lock(&tty_mutex);
836 tty = tty_init_dev(ptm_driver, index);
837 /* The tty returned here is locked so we can safely
839 mutex_unlock(&tty_mutex);
841 retval = PTR_ERR(tty);
846 * From here on out, the tty is "live", and the index and
847 * fsi will be killed/put by the tty_release()
849 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
850 tty->driver_data = fsi;
852 tty_add_file(tty, filp);
854 dentry = devpts_pty_new(fsi, index, tty->link);
855 if (IS_ERR(dentry)) {
856 retval = PTR_ERR(dentry);
859 tty->link->driver_data = dentry;
861 retval = ptm_driver->ops->open(tty, filp);
865 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
871 // This will also put-ref the fsi
872 tty_release(inode, filp);
875 devpts_kill_index(fsi, index);
883 static struct file_operations ptmx_fops __ro_after_init;
885 static void __init unix98_pty_init(void)
887 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
888 TTY_DRIVER_RESET_TERMIOS |
889 TTY_DRIVER_REAL_RAW |
890 TTY_DRIVER_DYNAMIC_DEV |
891 TTY_DRIVER_DEVPTS_MEM |
892 TTY_DRIVER_DYNAMIC_ALLOC);
893 if (IS_ERR(ptm_driver))
894 panic("Couldn't allocate Unix98 ptm driver");
895 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
896 TTY_DRIVER_RESET_TERMIOS |
897 TTY_DRIVER_REAL_RAW |
898 TTY_DRIVER_DYNAMIC_DEV |
899 TTY_DRIVER_DEVPTS_MEM |
900 TTY_DRIVER_DYNAMIC_ALLOC);
901 if (IS_ERR(pts_driver))
902 panic("Couldn't allocate Unix98 pts driver");
904 ptm_driver->driver_name = "pty_master";
905 ptm_driver->name = "ptm";
906 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
907 ptm_driver->minor_start = 0;
908 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
909 ptm_driver->subtype = PTY_TYPE_MASTER;
910 ptm_driver->init_termios = tty_std_termios;
911 ptm_driver->init_termios.c_iflag = 0;
912 ptm_driver->init_termios.c_oflag = 0;
913 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
914 ptm_driver->init_termios.c_lflag = 0;
915 ptm_driver->init_termios.c_ispeed = 38400;
916 ptm_driver->init_termios.c_ospeed = 38400;
917 ptm_driver->other = pts_driver;
918 tty_set_operations(ptm_driver, &ptm_unix98_ops);
920 pts_driver->driver_name = "pty_slave";
921 pts_driver->name = "pts";
922 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
923 pts_driver->minor_start = 0;
924 pts_driver->type = TTY_DRIVER_TYPE_PTY;
925 pts_driver->subtype = PTY_TYPE_SLAVE;
926 pts_driver->init_termios = tty_std_termios;
927 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
928 pts_driver->init_termios.c_ispeed = 38400;
929 pts_driver->init_termios.c_ospeed = 38400;
930 pts_driver->other = ptm_driver;
931 tty_set_operations(pts_driver, &pty_unix98_ops);
933 if (tty_register_driver(ptm_driver))
934 panic("Couldn't register Unix98 ptm driver");
935 if (tty_register_driver(pts_driver))
936 panic("Couldn't register Unix98 pts driver");
938 /* Now create the /dev/ptmx special device */
939 tty_default_fops(&ptmx_fops);
940 ptmx_fops.open = ptmx_open;
942 cdev_init(&ptmx_cdev, &ptmx_fops);
943 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
944 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
945 panic("Couldn't register /dev/ptmx driver");
946 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
950 static inline void unix98_pty_init(void) { }
953 static int __init pty_init(void)
959 device_initcall(pty_init);