OSDN Git Service

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