GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / tty / pty.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *
4  *  Added support for a Unix98-style ptmx device.
5  *    -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
6  *
7  */
8
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/interrupt.h>
12 #include <linux/tty.h>
13 #include <linux/tty_flip.h>
14 #include <linux/fcntl.h>
15 #include <linux/sched.h>
16 #include <linux/string.h>
17 #include <linux/major.h>
18 #include <linux/mm.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/uaccess.h>
22 #include <linux/bitops.h>
23 #include <linux/devpts_fs.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/poll.h>
27
28 #undef TTY_DEBUG_HANGUP
29 #ifdef TTY_DEBUG_HANGUP
30 # define tty_debug_hangup(tty, f, args...)      tty_debug(tty, f, ##args)
31 #else
32 # define tty_debug_hangup(tty, f, args...)      do {} while (0)
33 #endif
34
35 #ifdef CONFIG_UNIX98_PTYS
36 static struct tty_driver *ptm_driver;
37 static struct tty_driver *pts_driver;
38 static DEFINE_MUTEX(devpts_mutex);
39 #endif
40
41 static void pty_close(struct tty_struct *tty, struct file *filp)
42 {
43         BUG_ON(!tty);
44         if (tty->driver->subtype == PTY_TYPE_MASTER)
45                 WARN_ON(tty->count > 1);
46         else {
47                 if (tty_io_error(tty))
48                         return;
49                 if (tty->count > 2)
50                         return;
51         }
52         set_bit(TTY_IO_ERROR, &tty->flags);
53         wake_up_interruptible(&tty->read_wait);
54         wake_up_interruptible(&tty->write_wait);
55         spin_lock_irq(&tty->ctrl_lock);
56         tty->packet = 0;
57         spin_unlock_irq(&tty->ctrl_lock);
58         /* Review - krefs on tty_link ?? */
59         if (!tty->link)
60                 return;
61         set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
62         wake_up_interruptible(&tty->link->read_wait);
63         wake_up_interruptible(&tty->link->write_wait);
64         if (tty->driver->subtype == PTY_TYPE_MASTER) {
65                 set_bit(TTY_OTHER_CLOSED, &tty->flags);
66 #ifdef CONFIG_UNIX98_PTYS
67                 if (tty->driver == ptm_driver) {
68                         mutex_lock(&devpts_mutex);
69                         if (tty->link->driver_data)
70                                 devpts_pty_kill(tty->link->driver_data);
71                         mutex_unlock(&devpts_mutex);
72                 }
73 #endif
74                 tty_vhangup(tty->link);
75         }
76 }
77
78 /*
79  * The unthrottle routine is called by the line discipline to signal
80  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
81  * flag is always set, to force the line discipline to always call the
82  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
83  * characters in the queue.  This is necessary since each time this
84  * happens, we need to wake up any sleeping processes that could be
85  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
86  * for the pty buffer to be drained.
87  */
88 static void pty_unthrottle(struct tty_struct *tty)
89 {
90         tty_wakeup(tty->link);
91         set_bit(TTY_THROTTLED, &tty->flags);
92 }
93
94 /**
95  *      pty_write               -       write to a pty
96  *      @tty: the tty we write from
97  *      @buf: kernel buffer of data
98  *      @count: bytes to write
99  *
100  *      Our "hardware" write method. Data is coming from the ldisc which
101  *      may be in a non sleeping state. We simply throw this at the other
102  *      end of the link as if we were an IRQ handler receiving stuff for
103  *      the other side of the pty/tty pair.
104  */
105
106 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
107 {
108         struct tty_struct *to = tty->link;
109
110         if (tty->stopped || !c)
111                 return 0;
112
113         return tty_insert_flip_string_and_push_buffer(to->port, buf, c);
114 }
115
116 /**
117  *      pty_write_room  -       write space
118  *      @tty: tty we are writing from
119  *
120  *      Report how many bytes the ldisc can send into the queue for
121  *      the other device.
122  */
123
124 static int pty_write_room(struct tty_struct *tty)
125 {
126         if (tty->stopped)
127                 return 0;
128         return tty_buffer_space_avail(tty->link->port);
129 }
130
131 /**
132  *      pty_chars_in_buffer     -       characters currently in our tx queue
133  *      @tty: our tty
134  *
135  *      Report how much we have in the transmit queue. As everything is
136  *      instantly at the other end this is easy to implement.
137  */
138
139 static int pty_chars_in_buffer(struct tty_struct *tty)
140 {
141         return 0;
142 }
143
144 /* Set the lock flag on a pty */
145 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
146 {
147         int val;
148         if (get_user(val, arg))
149                 return -EFAULT;
150         if (val)
151                 set_bit(TTY_PTY_LOCK, &tty->flags);
152         else
153                 clear_bit(TTY_PTY_LOCK, &tty->flags);
154         return 0;
155 }
156
157 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
158 {
159         int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
160         return put_user(locked, arg);
161 }
162
163 /* Set the packet mode on a pty */
164 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
165 {
166         int pktmode;
167
168         if (get_user(pktmode, arg))
169                 return -EFAULT;
170
171         spin_lock_irq(&tty->ctrl_lock);
172         if (pktmode) {
173                 if (!tty->packet) {
174                         tty->link->ctrl_status = 0;
175                         smp_mb();
176                         tty->packet = 1;
177                 }
178         } else
179                 tty->packet = 0;
180         spin_unlock_irq(&tty->ctrl_lock);
181
182         return 0;
183 }
184
185 /* Get the packet mode of a pty */
186 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
187 {
188         int pktmode = tty->packet;
189         return put_user(pktmode, arg);
190 }
191
192 /* Send a signal to the slave */
193 static int pty_signal(struct tty_struct *tty, int sig)
194 {
195         struct pid *pgrp;
196
197         if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
198                 return -EINVAL;
199
200         if (tty->link) {
201                 pgrp = tty_get_pgrp(tty->link);
202                 if (pgrp)
203                         kill_pgrp(pgrp, sig, 1);
204                 put_pid(pgrp);
205         }
206         return 0;
207 }
208
209 static void pty_flush_buffer(struct tty_struct *tty)
210 {
211         struct tty_struct *to = tty->link;
212
213         if (!to)
214                 return;
215
216         tty_buffer_flush(to, NULL);
217         if (to->packet) {
218                 spin_lock_irq(&tty->ctrl_lock);
219                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
220                 wake_up_interruptible(&to->read_wait);
221                 spin_unlock_irq(&tty->ctrl_lock);
222         }
223 }
224
225 static int pty_open(struct tty_struct *tty, struct file *filp)
226 {
227         if (!tty || !tty->link)
228                 return -ENODEV;
229
230         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
231                 goto out;
232         if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
233                 goto out;
234         if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
235                 goto out;
236
237         clear_bit(TTY_IO_ERROR, &tty->flags);
238         clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
239         set_bit(TTY_THROTTLED, &tty->flags);
240         return 0;
241
242 out:
243         set_bit(TTY_IO_ERROR, &tty->flags);
244         return -EIO;
245 }
246
247 static void pty_set_termios(struct tty_struct *tty,
248                                         struct ktermios *old_termios)
249 {
250         /* See if packet mode change of state. */
251         if (tty->link && tty->link->packet) {
252                 int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
253                 int old_flow = ((old_termios->c_iflag & IXON) &&
254                                 (old_termios->c_cc[VSTOP] == '\023') &&
255                                 (old_termios->c_cc[VSTART] == '\021'));
256                 int new_flow = (I_IXON(tty) &&
257                                 STOP_CHAR(tty) == '\023' &&
258                                 START_CHAR(tty) == '\021');
259                 if ((old_flow != new_flow) || extproc) {
260                         spin_lock_irq(&tty->ctrl_lock);
261                         if (old_flow != new_flow) {
262                                 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
263                                 if (new_flow)
264                                         tty->ctrl_status |= TIOCPKT_DOSTOP;
265                                 else
266                                         tty->ctrl_status |= TIOCPKT_NOSTOP;
267                         }
268                         if (extproc)
269                                 tty->ctrl_status |= TIOCPKT_IOCTL;
270                         spin_unlock_irq(&tty->ctrl_lock);
271                         wake_up_interruptible(&tty->link->read_wait);
272                 }
273         }
274
275         tty->termios.c_cflag &= ~(CSIZE | PARENB);
276         tty->termios.c_cflag |= (CS8 | CREAD);
277 }
278
279 /**
280  *      pty_do_resize           -       resize event
281  *      @tty: tty being resized
282  *      @ws: window size being set.
283  *
284  *      Update the termios variables and send the necessary signals to
285  *      peform a terminal resize correctly
286  */
287
288 static int pty_resize(struct tty_struct *tty,  struct winsize *ws)
289 {
290         struct pid *pgrp, *rpgrp;
291         struct tty_struct *pty = tty->link;
292
293         /* For a PTY we need to lock the tty side */
294         mutex_lock(&tty->winsize_mutex);
295         if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
296                 goto done;
297
298         /* Signal the foreground process group of both ptys */
299         pgrp = tty_get_pgrp(tty);
300         rpgrp = tty_get_pgrp(pty);
301
302         if (pgrp)
303                 kill_pgrp(pgrp, SIGWINCH, 1);
304         if (rpgrp != pgrp && rpgrp)
305                 kill_pgrp(rpgrp, SIGWINCH, 1);
306
307         put_pid(pgrp);
308         put_pid(rpgrp);
309
310         tty->winsize = *ws;
311         pty->winsize = *ws;     /* Never used so will go away soon */
312 done:
313         mutex_unlock(&tty->winsize_mutex);
314         return 0;
315 }
316
317 /**
318  *      pty_start - start() handler
319  *      pty_stop  - stop() handler
320  *      @tty: tty being flow-controlled
321  *
322  *      Propagates the TIOCPKT status to the master pty.
323  *
324  *      NB: only the master pty can be in packet mode so only the slave
325  *          needs start()/stop() handlers
326  */
327 static void pty_start(struct tty_struct *tty)
328 {
329         unsigned long flags;
330
331         if (tty->link && tty->link->packet) {
332                 spin_lock_irqsave(&tty->ctrl_lock, flags);
333                 tty->ctrl_status &= ~TIOCPKT_STOP;
334                 tty->ctrl_status |= TIOCPKT_START;
335                 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
336                 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
337         }
338 }
339
340 static void pty_stop(struct tty_struct *tty)
341 {
342         unsigned long flags;
343
344         if (tty->link && tty->link->packet) {
345                 spin_lock_irqsave(&tty->ctrl_lock, flags);
346                 tty->ctrl_status &= ~TIOCPKT_START;
347                 tty->ctrl_status |= TIOCPKT_STOP;
348                 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
349                 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
350         }
351 }
352
353 /**
354  *      pty_common_install              -       set up the pty pair
355  *      @driver: the pty driver
356  *      @tty: the tty being instantiated
357  *      @legacy: true if this is BSD style
358  *
359  *      Perform the initial set up for the tty/pty pair. Called from the
360  *      tty layer when the port is first opened.
361  *
362  *      Locking: the caller must hold the tty_mutex
363  */
364 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
365                 bool legacy)
366 {
367         struct tty_struct *o_tty;
368         struct tty_port *ports[2];
369         int idx = tty->index;
370         int retval = -ENOMEM;
371
372         /* Opening the slave first has always returned -EIO */
373         if (driver->subtype != PTY_TYPE_MASTER)
374                 return -EIO;
375
376         ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
377         ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
378         if (!ports[0] || !ports[1])
379                 goto err;
380         if (!try_module_get(driver->other->owner)) {
381                 /* This cannot in fact currently happen */
382                 goto err;
383         }
384         o_tty = alloc_tty_struct(driver->other, idx);
385         if (!o_tty)
386                 goto err_put_module;
387
388         tty_set_lock_subclass(o_tty);
389         lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
390
391         if (legacy) {
392                 /* We always use new tty termios data so we can do this
393                    the easy way .. */
394                 tty_init_termios(tty);
395                 tty_init_termios(o_tty);
396
397                 driver->other->ttys[idx] = o_tty;
398                 driver->ttys[idx] = tty;
399         } else {
400                 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
401                 tty->termios = driver->init_termios;
402                 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
403                 o_tty->termios = driver->other->init_termios;
404         }
405
406         /*
407          * Everything allocated ... set up the o_tty structure.
408          */
409         tty_driver_kref_get(driver->other);
410         /* Establish the links in both directions */
411         tty->link   = o_tty;
412         o_tty->link = tty;
413         tty_port_init(ports[0]);
414         tty_port_init(ports[1]);
415         tty_buffer_set_limit(ports[0], 8192);
416         tty_buffer_set_limit(ports[1], 8192);
417         o_tty->port = ports[0];
418         tty->port = ports[1];
419         o_tty->port->itty = o_tty;
420
421         tty_buffer_set_lock_subclass(o_tty->port);
422
423         tty_driver_kref_get(driver);
424         tty->count++;
425         o_tty->count++;
426         return 0;
427
428 err_put_module:
429         module_put(driver->other->owner);
430 err:
431         kfree(ports[0]);
432         kfree(ports[1]);
433         return retval;
434 }
435
436 static void pty_cleanup(struct tty_struct *tty)
437 {
438         tty_port_put(tty->port);
439 }
440
441 /* Traditional BSD devices */
442 #ifdef CONFIG_LEGACY_PTYS
443
444 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
445 {
446         return pty_common_install(driver, tty, true);
447 }
448
449 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
450 {
451         struct tty_struct *pair = tty->link;
452         driver->ttys[tty->index] = NULL;
453         if (pair)
454                 pair->driver->ttys[pair->index] = NULL;
455 }
456
457 static int pty_bsd_ioctl(struct tty_struct *tty,
458                          unsigned int cmd, unsigned long arg)
459 {
460         switch (cmd) {
461         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
462                 return pty_set_lock(tty, (int __user *) arg);
463         case TIOCGPTLCK: /* Get PT Lock status */
464                 return pty_get_lock(tty, (int __user *)arg);
465         case TIOCPKT: /* Set PT packet mode */
466                 return pty_set_pktmode(tty, (int __user *)arg);
467         case TIOCGPKT: /* Get PT packet mode */
468                 return pty_get_pktmode(tty, (int __user *)arg);
469         case TIOCSIG:    /* Send signal to other side of pty */
470                 return pty_signal(tty, (int) arg);
471         case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
472                 return -EINVAL;
473         }
474         return -ENOIOCTLCMD;
475 }
476
477 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
478 /*
479  * not really modular, but the easiest way to keep compat with existing
480  * bootargs behaviour is to continue using module_param here.
481  */
482 module_param(legacy_count, int, 0);
483
484 /*
485  * The master side of a pty can do TIOCSPTLCK and thus
486  * has pty_bsd_ioctl.
487  */
488 static const struct tty_operations master_pty_ops_bsd = {
489         .install = pty_install,
490         .open = pty_open,
491         .close = pty_close,
492         .write = pty_write,
493         .write_room = pty_write_room,
494         .flush_buffer = pty_flush_buffer,
495         .chars_in_buffer = pty_chars_in_buffer,
496         .unthrottle = pty_unthrottle,
497         .ioctl = pty_bsd_ioctl,
498         .cleanup = pty_cleanup,
499         .resize = pty_resize,
500         .remove = pty_remove
501 };
502
503 static const struct tty_operations slave_pty_ops_bsd = {
504         .install = pty_install,
505         .open = pty_open,
506         .close = pty_close,
507         .write = pty_write,
508         .write_room = pty_write_room,
509         .flush_buffer = pty_flush_buffer,
510         .chars_in_buffer = pty_chars_in_buffer,
511         .unthrottle = pty_unthrottle,
512         .set_termios = pty_set_termios,
513         .cleanup = pty_cleanup,
514         .resize = pty_resize,
515         .start = pty_start,
516         .stop = pty_stop,
517         .remove = pty_remove
518 };
519
520 static void __init legacy_pty_init(void)
521 {
522         struct tty_driver *pty_driver, *pty_slave_driver;
523
524         if (legacy_count <= 0)
525                 return;
526
527         pty_driver = tty_alloc_driver(legacy_count,
528                         TTY_DRIVER_RESET_TERMIOS |
529                         TTY_DRIVER_REAL_RAW |
530                         TTY_DRIVER_DYNAMIC_ALLOC);
531         if (IS_ERR(pty_driver))
532                 panic("Couldn't allocate pty driver");
533
534         pty_slave_driver = tty_alloc_driver(legacy_count,
535                         TTY_DRIVER_RESET_TERMIOS |
536                         TTY_DRIVER_REAL_RAW |
537                         TTY_DRIVER_DYNAMIC_ALLOC);
538         if (IS_ERR(pty_slave_driver))
539                 panic("Couldn't allocate pty slave driver");
540
541         pty_driver->driver_name = "pty_master";
542         pty_driver->name = "pty";
543         pty_driver->major = PTY_MASTER_MAJOR;
544         pty_driver->minor_start = 0;
545         pty_driver->type = TTY_DRIVER_TYPE_PTY;
546         pty_driver->subtype = PTY_TYPE_MASTER;
547         pty_driver->init_termios = tty_std_termios;
548         pty_driver->init_termios.c_iflag = 0;
549         pty_driver->init_termios.c_oflag = 0;
550         pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
551         pty_driver->init_termios.c_lflag = 0;
552         pty_driver->init_termios.c_ispeed = 38400;
553         pty_driver->init_termios.c_ospeed = 38400;
554         pty_driver->other = pty_slave_driver;
555         tty_set_operations(pty_driver, &master_pty_ops_bsd);
556
557         pty_slave_driver->driver_name = "pty_slave";
558         pty_slave_driver->name = "ttyp";
559         pty_slave_driver->major = PTY_SLAVE_MAJOR;
560         pty_slave_driver->minor_start = 0;
561         pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
562         pty_slave_driver->subtype = PTY_TYPE_SLAVE;
563         pty_slave_driver->init_termios = tty_std_termios;
564         pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
565         pty_slave_driver->init_termios.c_ispeed = 38400;
566         pty_slave_driver->init_termios.c_ospeed = 38400;
567         pty_slave_driver->other = pty_driver;
568         tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
569
570         if (tty_register_driver(pty_driver))
571                 panic("Couldn't register pty driver");
572         if (tty_register_driver(pty_slave_driver))
573                 panic("Couldn't register pty slave driver");
574 }
575 #else
576 static inline void legacy_pty_init(void) { }
577 #endif
578
579 /* Unix98 devices */
580 #ifdef CONFIG_UNIX98_PTYS
581
582 static struct cdev ptmx_cdev;
583
584 static int pty_unix98_ioctl(struct tty_struct *tty,
585                             unsigned int cmd, unsigned long arg)
586 {
587         switch (cmd) {
588         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
589                 return pty_set_lock(tty, (int __user *)arg);
590         case TIOCGPTLCK: /* Get PT Lock status */
591                 return pty_get_lock(tty, (int __user *)arg);
592         case TIOCPKT: /* Set PT packet mode */
593                 return pty_set_pktmode(tty, (int __user *)arg);
594         case TIOCGPKT: /* Get PT packet mode */
595                 return pty_get_pktmode(tty, (int __user *)arg);
596         case TIOCGPTN: /* Get PT Number */
597                 return put_user(tty->index, (unsigned int __user *)arg);
598         case TIOCSIG:    /* Send signal to other side of pty */
599                 return pty_signal(tty, (int) arg);
600         }
601
602         return -ENOIOCTLCMD;
603 }
604
605 /**
606  *      ptm_unix98_lookup       -       find a pty master
607  *      @driver: ptm driver
608  *      @idx: tty index
609  *
610  *      Look up a pty master device. Called under the tty_mutex for now.
611  *      This provides our locking.
612  */
613
614 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
615                 struct file *file, int idx)
616 {
617         /* Master must be open via /dev/ptmx */
618         return ERR_PTR(-EIO);
619 }
620
621 /**
622  *      pts_unix98_lookup       -       find a pty slave
623  *      @driver: pts driver
624  *      @idx: tty index
625  *
626  *      Look up a pty master device. Called under the tty_mutex for now.
627  *      This provides our locking for the tty pointer.
628  */
629
630 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
631                 struct file *file, int idx)
632 {
633         struct tty_struct *tty;
634
635         mutex_lock(&devpts_mutex);
636         tty = devpts_get_priv(file->f_path.dentry);
637         mutex_unlock(&devpts_mutex);
638         /* Master must be open before slave */
639         if (!tty)
640                 return ERR_PTR(-EIO);
641         return tty;
642 }
643
644 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
645 {
646         return pty_common_install(driver, tty, false);
647 }
648
649 /* this is called once with whichever end is closed last */
650 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
651 {
652         struct pts_fs_info *fsi;
653
654         if (tty->driver->subtype == PTY_TYPE_MASTER)
655                 fsi = tty->driver_data;
656         else
657                 fsi = tty->link->driver_data;
658
659         if (fsi) {
660                 devpts_kill_index(fsi, tty->index);
661                 devpts_release(fsi);
662         }
663 }
664
665 static const struct tty_operations ptm_unix98_ops = {
666         .lookup = ptm_unix98_lookup,
667         .install = pty_unix98_install,
668         .remove = pty_unix98_remove,
669         .open = pty_open,
670         .close = pty_close,
671         .write = pty_write,
672         .write_room = pty_write_room,
673         .flush_buffer = pty_flush_buffer,
674         .chars_in_buffer = pty_chars_in_buffer,
675         .unthrottle = pty_unthrottle,
676         .ioctl = pty_unix98_ioctl,
677         .resize = pty_resize,
678         .cleanup = pty_cleanup
679 };
680
681 static const struct tty_operations pty_unix98_ops = {
682         .lookup = pts_unix98_lookup,
683         .install = pty_unix98_install,
684         .remove = pty_unix98_remove,
685         .open = pty_open,
686         .close = pty_close,
687         .write = pty_write,
688         .write_room = pty_write_room,
689         .flush_buffer = pty_flush_buffer,
690         .chars_in_buffer = pty_chars_in_buffer,
691         .unthrottle = pty_unthrottle,
692         .set_termios = pty_set_termios,
693         .start = pty_start,
694         .stop = pty_stop,
695         .cleanup = pty_cleanup,
696 };
697
698 /**
699  *      ptmx_open               -       open a unix 98 pty master
700  *      @inode: inode of device file
701  *      @filp: file pointer to tty
702  *
703  *      Allocate a unix98 pty master device from the ptmx driver.
704  *
705  *      Locking: tty_mutex protects the init_dev work. tty->count should
706  *              protect the rest.
707  *              allocated_ptys_lock handles the list of free pty numbers
708  */
709
710 static int ptmx_open(struct inode *inode, struct file *filp)
711 {
712         struct pts_fs_info *fsi;
713         struct tty_struct *tty;
714         struct dentry *dentry;
715         int retval;
716         int index;
717
718         nonseekable_open(inode, filp);
719
720         /* We refuse fsnotify events on ptmx, since it's a shared resource */
721         filp->f_mode |= FMODE_NONOTIFY;
722
723         retval = tty_alloc_file(filp);
724         if (retval)
725                 return retval;
726
727         fsi = devpts_acquire(filp);
728         if (IS_ERR(fsi)) {
729                 retval = PTR_ERR(fsi);
730                 goto out_free_file;
731         }
732
733         /* find a device that is not in use. */
734         mutex_lock(&devpts_mutex);
735         index = devpts_new_index(fsi);
736         mutex_unlock(&devpts_mutex);
737
738         retval = index;
739         if (index < 0)
740                 goto out_put_fsi;
741
742
743         mutex_lock(&tty_mutex);
744         tty = tty_init_dev(ptm_driver, index);
745         /* The tty returned here is locked so we can safely
746            drop the mutex */
747         mutex_unlock(&tty_mutex);
748
749         retval = PTR_ERR(tty);
750         if (IS_ERR(tty))
751                 goto out;
752
753         /*
754          * From here on out, the tty is "live", and the index and
755          * fsi will be killed/put by the tty_release()
756          */
757         set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
758         tty->driver_data = fsi;
759
760         tty_add_file(tty, filp);
761
762         dentry = devpts_pty_new(fsi, index, tty->link);
763         if (IS_ERR(dentry)) {
764                 retval = PTR_ERR(dentry);
765                 goto err_release;
766         }
767         tty->link->driver_data = dentry;
768
769         retval = ptm_driver->ops->open(tty, filp);
770         if (retval)
771                 goto err_release;
772
773         tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
774
775         tty_unlock(tty);
776         return 0;
777 err_release:
778         tty_unlock(tty);
779         // This will also put-ref the fsi
780         tty_release(inode, filp);
781         return retval;
782 out:
783         devpts_kill_index(fsi, index);
784 out_put_fsi:
785         devpts_release(fsi);
786 out_free_file:
787         tty_free_file(filp);
788         return retval;
789 }
790
791 static struct file_operations ptmx_fops __ro_after_init;
792
793 static void __init unix98_pty_init(void)
794 {
795         ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
796                         TTY_DRIVER_RESET_TERMIOS |
797                         TTY_DRIVER_REAL_RAW |
798                         TTY_DRIVER_DYNAMIC_DEV |
799                         TTY_DRIVER_DEVPTS_MEM |
800                         TTY_DRIVER_DYNAMIC_ALLOC);
801         if (IS_ERR(ptm_driver))
802                 panic("Couldn't allocate Unix98 ptm driver");
803         pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
804                         TTY_DRIVER_RESET_TERMIOS |
805                         TTY_DRIVER_REAL_RAW |
806                         TTY_DRIVER_DYNAMIC_DEV |
807                         TTY_DRIVER_DEVPTS_MEM |
808                         TTY_DRIVER_DYNAMIC_ALLOC);
809         if (IS_ERR(pts_driver))
810                 panic("Couldn't allocate Unix98 pts driver");
811
812         ptm_driver->driver_name = "pty_master";
813         ptm_driver->name = "ptm";
814         ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
815         ptm_driver->minor_start = 0;
816         ptm_driver->type = TTY_DRIVER_TYPE_PTY;
817         ptm_driver->subtype = PTY_TYPE_MASTER;
818         ptm_driver->init_termios = tty_std_termios;
819         ptm_driver->init_termios.c_iflag = 0;
820         ptm_driver->init_termios.c_oflag = 0;
821         ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
822         ptm_driver->init_termios.c_lflag = 0;
823         ptm_driver->init_termios.c_ispeed = 38400;
824         ptm_driver->init_termios.c_ospeed = 38400;
825         ptm_driver->other = pts_driver;
826         tty_set_operations(ptm_driver, &ptm_unix98_ops);
827
828         pts_driver->driver_name = "pty_slave";
829         pts_driver->name = "pts";
830         pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
831         pts_driver->minor_start = 0;
832         pts_driver->type = TTY_DRIVER_TYPE_PTY;
833         pts_driver->subtype = PTY_TYPE_SLAVE;
834         pts_driver->init_termios = tty_std_termios;
835         pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
836         pts_driver->init_termios.c_ispeed = 38400;
837         pts_driver->init_termios.c_ospeed = 38400;
838         pts_driver->other = ptm_driver;
839         tty_set_operations(pts_driver, &pty_unix98_ops);
840
841         if (tty_register_driver(ptm_driver))
842                 panic("Couldn't register Unix98 ptm driver");
843         if (tty_register_driver(pts_driver))
844                 panic("Couldn't register Unix98 pts driver");
845
846         /* Now create the /dev/ptmx special device */
847         tty_default_fops(&ptmx_fops);
848         ptmx_fops.open = ptmx_open;
849
850         cdev_init(&ptmx_cdev, &ptmx_fops);
851         if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
852             register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
853                 panic("Couldn't register /dev/ptmx driver");
854         device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
855 }
856
857 #else
858 static inline void unix98_pty_init(void) { }
859 #endif
860
861 static int __init pty_init(void)
862 {
863         legacy_pty_init();
864         unix98_pty_init();
865         return 0;
866 }
867 device_initcall(pty_init);