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