IB/core: Use GRH when the path hop-limit > 0
[pandora-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  *  When reading this code see also fs/devpts. In particular note that the
8  *  driver_data field is used by the devpts side as a binding to the devpts
9  *  inode.
10  */
11
12 #include <linux/module.h>
13
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/fcntl.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/major.h>
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/sysctl.h>
25 #include <linux/device.h>
26 #include <linux/uaccess.h>
27 #include <linux/bitops.h>
28 #include <linux/devpts_fs.h>
29 #include <linux/slab.h>
30
31 #include <asm/system.h>
32
33 #ifdef CONFIG_UNIX98_PTYS
34 static struct tty_driver *ptm_driver;
35 static struct tty_driver *pts_driver;
36 #endif
37
38 static void pty_close(struct tty_struct *tty, struct file *filp)
39 {
40         BUG_ON(!tty);
41         if (tty->driver->subtype == PTY_TYPE_MASTER)
42                 WARN_ON(tty->count > 1);
43         else {
44                 if (tty->count > 2)
45                         return;
46         }
47         wake_up_interruptible(&tty->read_wait);
48         wake_up_interruptible(&tty->write_wait);
49         tty->packet = 0;
50         if (!tty->link)
51                 return;
52         set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
53         wake_up_interruptible(&tty->link->read_wait);
54         wake_up_interruptible(&tty->link->write_wait);
55         if (tty->driver->subtype == PTY_TYPE_MASTER) {
56                 set_bit(TTY_OTHER_CLOSED, &tty->flags);
57 #ifdef CONFIG_UNIX98_PTYS
58                 if (tty->driver == ptm_driver)
59                         devpts_pty_kill(tty->link);
60 #endif
61                 tty_unlock();
62                 tty_vhangup(tty->link);
63                 tty_lock();
64         }
65 }
66
67 /*
68  * The unthrottle routine is called by the line discipline to signal
69  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
70  * flag is always set, to force the line discipline to always call the
71  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
72  * characters in the queue.  This is necessary since each time this
73  * happens, we need to wake up any sleeping processes that could be
74  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
75  * for the pty buffer to be drained.
76  */
77 static void pty_unthrottle(struct tty_struct *tty)
78 {
79         tty_wakeup(tty->link);
80         set_bit(TTY_THROTTLED, &tty->flags);
81 }
82
83 /**
84  *      pty_space       -       report space left for writing
85  *      @to: tty we are writing into
86  *
87  *      The tty buffers allow 64K but we sneak a peak and clip at 8K this
88  *      allows a lot of overspill room for echo and other fun messes to
89  *      be handled properly
90  */
91
92 static int pty_space(struct tty_struct *to)
93 {
94         int n = 8192 - to->buf.memory_used;
95         if (n < 0)
96                 return 0;
97         return n;
98 }
99
100 /**
101  *      pty_write               -       write to a pty
102  *      @tty: the tty we write from
103  *      @buf: kernel buffer of data
104  *      @count: bytes to write
105  *
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.
110  */
111
112 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
113 {
114         struct tty_struct *to = tty->link;
115
116         if (tty->stopped)
117                 return 0;
118
119         if (c > 0) {
120                 /* Stuff the data into the input queue of the other end */
121                 c = tty_insert_flip_string(to, buf, c);
122                 /* And shovel */
123                 if (c) {
124                         tty_flip_buffer_push(to);
125                         tty_wakeup(tty);
126                 }
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 /* Send a signal to the slave */
173 static int pty_signal(struct tty_struct *tty, int sig)
174 {
175         unsigned long flags;
176         struct pid *pgrp;
177
178         if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
179                 return -EINVAL;
180
181         if (tty->link) {
182                 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
183                 pgrp = get_pid(tty->link->pgrp);
184                 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
185
186                 kill_pgrp(pgrp, sig, 1);
187                 put_pid(pgrp);
188         }
189         return 0;
190 }
191
192 static void pty_flush_buffer(struct tty_struct *tty)
193 {
194         struct tty_struct *to = tty->link;
195         unsigned long flags;
196
197         if (!to)
198                 return;
199         /* tty_buffer_flush(to); FIXME */
200         if (to->packet) {
201                 spin_lock_irqsave(&tty->ctrl_lock, flags);
202                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
203                 wake_up_interruptible(&to->read_wait);
204                 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
205         }
206 }
207
208 static int pty_open(struct tty_struct *tty, struct file *filp)
209 {
210         int     retval = -ENODEV;
211
212         if (!tty || !tty->link)
213                 goto out;
214
215         retval = -EIO;
216         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
217                 goto out;
218         if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
219                 goto out;
220         if (tty->link->count != 1)
221                 goto out;
222
223         clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
224         set_bit(TTY_THROTTLED, &tty->flags);
225         retval = 0;
226 out:
227         return retval;
228 }
229
230 static void pty_set_termios(struct tty_struct *tty,
231                                         struct ktermios *old_termios)
232 {
233         tty->termios->c_cflag &= ~(CSIZE | PARENB);
234         tty->termios->c_cflag |= (CS8 | CREAD);
235 }
236
237 /**
238  *      pty_do_resize           -       resize event
239  *      @tty: tty being resized
240  *      @ws: window size being set.
241  *
242  *      Update the termios variables and send the necessary signals to
243  *      peform a terminal resize correctly
244  */
245
246 int pty_resize(struct tty_struct *tty,  struct winsize *ws)
247 {
248         struct pid *pgrp, *rpgrp;
249         unsigned long flags;
250         struct tty_struct *pty = tty->link;
251
252         /* For a PTY we need to lock the tty side */
253         mutex_lock(&tty->termios_mutex);
254         if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
255                 goto done;
256
257         /* Get the PID values and reference them so we can
258            avoid holding the tty ctrl lock while sending signals.
259            We need to lock these individually however. */
260
261         spin_lock_irqsave(&tty->ctrl_lock, flags);
262         pgrp = get_pid(tty->pgrp);
263         spin_unlock_irqrestore(&tty->ctrl_lock, flags);
264
265         spin_lock_irqsave(&pty->ctrl_lock, flags);
266         rpgrp = get_pid(pty->pgrp);
267         spin_unlock_irqrestore(&pty->ctrl_lock, flags);
268
269         if (pgrp)
270                 kill_pgrp(pgrp, SIGWINCH, 1);
271         if (rpgrp != pgrp && rpgrp)
272                 kill_pgrp(rpgrp, SIGWINCH, 1);
273
274         put_pid(pgrp);
275         put_pid(rpgrp);
276
277         tty->winsize = *ws;
278         pty->winsize = *ws;     /* Never used so will go away soon */
279 done:
280         mutex_unlock(&tty->termios_mutex);
281         return 0;
282 }
283
284 /* Traditional BSD devices */
285 #ifdef CONFIG_LEGACY_PTYS
286
287 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
288 {
289         struct tty_struct *o_tty;
290         int idx = tty->index;
291         int retval;
292
293         o_tty = alloc_tty_struct();
294         if (!o_tty)
295                 return -ENOMEM;
296         if (!try_module_get(driver->other->owner)) {
297                 /* This cannot in fact currently happen */
298                 retval = -ENOMEM;
299                 goto err_free_tty;
300         }
301         initialize_tty_struct(o_tty, driver->other, idx);
302
303         /* We always use new tty termios data so we can do this
304            the easy way .. */
305         retval = tty_init_termios(tty);
306         if (retval)
307                 goto err_deinit_tty;
308
309         retval = tty_init_termios(o_tty);
310         if (retval)
311                 goto err_free_termios;
312
313         /*
314          * Everything allocated ... set up the o_tty structure.
315          */
316         driver->other->ttys[idx] = o_tty;
317         tty_driver_kref_get(driver->other);
318         if (driver->subtype == PTY_TYPE_MASTER)
319                 o_tty->count++;
320         /* Establish the links in both directions */
321         tty->link   = o_tty;
322         o_tty->link = tty;
323
324         tty_driver_kref_get(driver);
325         tty->count++;
326         driver->ttys[idx] = tty;
327         return 0;
328 err_free_termios:
329         tty_free_termios(tty);
330 err_deinit_tty:
331         deinitialize_tty_struct(o_tty);
332         module_put(o_tty->driver->owner);
333 err_free_tty:
334         free_tty_struct(o_tty);
335         return retval;
336 }
337
338 static int pty_bsd_ioctl(struct tty_struct *tty,
339                          unsigned int cmd, unsigned long arg)
340 {
341         switch (cmd) {
342         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
343                 return pty_set_lock(tty, (int __user *) arg);
344         case TIOCSIG:    /* Send signal to other side of pty */
345                 return pty_signal(tty, (int) arg);
346         }
347         return -ENOIOCTLCMD;
348 }
349
350 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
351 module_param(legacy_count, int, 0);
352
353 /*
354  * The master side of a pty can do TIOCSPTLCK and thus
355  * has pty_bsd_ioctl.
356  */
357 static const struct tty_operations master_pty_ops_bsd = {
358         .install = pty_install,
359         .open = pty_open,
360         .close = pty_close,
361         .write = pty_write,
362         .write_room = pty_write_room,
363         .flush_buffer = pty_flush_buffer,
364         .chars_in_buffer = pty_chars_in_buffer,
365         .unthrottle = pty_unthrottle,
366         .set_termios = pty_set_termios,
367         .ioctl = pty_bsd_ioctl,
368         .resize = pty_resize
369 };
370
371 static const struct tty_operations slave_pty_ops_bsd = {
372         .install = pty_install,
373         .open = pty_open,
374         .close = pty_close,
375         .write = pty_write,
376         .write_room = pty_write_room,
377         .flush_buffer = pty_flush_buffer,
378         .chars_in_buffer = pty_chars_in_buffer,
379         .unthrottle = pty_unthrottle,
380         .set_termios = pty_set_termios,
381         .resize = pty_resize
382 };
383
384 static void __init legacy_pty_init(void)
385 {
386         struct tty_driver *pty_driver, *pty_slave_driver;
387
388         if (legacy_count <= 0)
389                 return;
390
391         pty_driver = alloc_tty_driver(legacy_count);
392         if (!pty_driver)
393                 panic("Couldn't allocate pty driver");
394
395         pty_slave_driver = alloc_tty_driver(legacy_count);
396         if (!pty_slave_driver)
397                 panic("Couldn't allocate pty slave driver");
398
399         pty_driver->owner = THIS_MODULE;
400         pty_driver->driver_name = "pty_master";
401         pty_driver->name = "pty";
402         pty_driver->major = PTY_MASTER_MAJOR;
403         pty_driver->minor_start = 0;
404         pty_driver->type = TTY_DRIVER_TYPE_PTY;
405         pty_driver->subtype = PTY_TYPE_MASTER;
406         pty_driver->init_termios = tty_std_termios;
407         pty_driver->init_termios.c_iflag = 0;
408         pty_driver->init_termios.c_oflag = 0;
409         pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
410         pty_driver->init_termios.c_lflag = 0;
411         pty_driver->init_termios.c_ispeed = 38400;
412         pty_driver->init_termios.c_ospeed = 38400;
413         pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
414         pty_driver->other = pty_slave_driver;
415         tty_set_operations(pty_driver, &master_pty_ops_bsd);
416
417         pty_slave_driver->owner = THIS_MODULE;
418         pty_slave_driver->driver_name = "pty_slave";
419         pty_slave_driver->name = "ttyp";
420         pty_slave_driver->major = PTY_SLAVE_MAJOR;
421         pty_slave_driver->minor_start = 0;
422         pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
423         pty_slave_driver->subtype = PTY_TYPE_SLAVE;
424         pty_slave_driver->init_termios = tty_std_termios;
425         pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
426         pty_slave_driver->init_termios.c_ispeed = 38400;
427         pty_slave_driver->init_termios.c_ospeed = 38400;
428         pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
429                                         TTY_DRIVER_REAL_RAW;
430         pty_slave_driver->other = pty_driver;
431         tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
432
433         if (tty_register_driver(pty_driver))
434                 panic("Couldn't register pty driver");
435         if (tty_register_driver(pty_slave_driver))
436                 panic("Couldn't register pty slave driver");
437 }
438 #else
439 static inline void legacy_pty_init(void) { }
440 #endif
441
442 /* Unix98 devices */
443 #ifdef CONFIG_UNIX98_PTYS
444 /*
445  * sysctl support for setting limits on the number of Unix98 ptys allocated.
446  * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
447  */
448 int pty_limit = NR_UNIX98_PTY_DEFAULT;
449 static int pty_limit_min;
450 static int pty_limit_max = NR_UNIX98_PTY_MAX;
451 static int tty_count;
452 static int pty_count;
453
454 static inline void pty_inc_count(void)
455 {
456         pty_count = (++tty_count) / 2;
457 }
458
459 static inline void pty_dec_count(void)
460 {
461         pty_count = (--tty_count) / 2;
462 }
463
464 static struct cdev ptmx_cdev;
465
466 static struct ctl_table pty_table[] = {
467         {
468                 .procname       = "max",
469                 .maxlen         = sizeof(int),
470                 .mode           = 0644,
471                 .data           = &pty_limit,
472                 .proc_handler   = proc_dointvec_minmax,
473                 .extra1         = &pty_limit_min,
474                 .extra2         = &pty_limit_max,
475         }, {
476                 .procname       = "nr",
477                 .maxlen         = sizeof(int),
478                 .mode           = 0444,
479                 .data           = &pty_count,
480                 .proc_handler   = proc_dointvec,
481         }, 
482         {}
483 };
484
485 static struct ctl_table pty_kern_table[] = {
486         {
487                 .procname       = "pty",
488                 .mode           = 0555,
489                 .child          = pty_table,
490         },
491         {}
492 };
493
494 static struct ctl_table pty_root_table[] = {
495         {
496                 .procname       = "kernel",
497                 .mode           = 0555,
498                 .child          = pty_kern_table,
499         },
500         {}
501 };
502
503
504 static int pty_unix98_ioctl(struct tty_struct *tty,
505                             unsigned int cmd, unsigned long arg)
506 {
507         switch (cmd) {
508         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
509                 return pty_set_lock(tty, (int __user *)arg);
510         case TIOCGPTN: /* Get PT Number */
511                 return put_user(tty->index, (unsigned int __user *)arg);
512         case TIOCSIG:    /* Send signal to other side of pty */
513                 return pty_signal(tty, (int) arg);
514         }
515
516         return -ENOIOCTLCMD;
517 }
518
519 /**
520  *      ptm_unix98_lookup       -       find a pty master
521  *      @driver: ptm driver
522  *      @idx: tty index
523  *
524  *      Look up a pty master device. Called under the tty_mutex for now.
525  *      This provides our locking.
526  */
527
528 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
529                 struct inode *ptm_inode, int idx)
530 {
531         struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
532         if (tty)
533                 tty = tty->link;
534         return tty;
535 }
536
537 /**
538  *      pts_unix98_lookup       -       find a pty slave
539  *      @driver: pts driver
540  *      @idx: tty index
541  *
542  *      Look up a pty master device. Called under the tty_mutex for now.
543  *      This provides our locking.
544  */
545
546 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
547                 struct inode *pts_inode, int idx)
548 {
549         struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
550         /* Master must be open before slave */
551         if (!tty)
552                 return ERR_PTR(-EIO);
553         return tty;
554 }
555
556 static void pty_unix98_shutdown(struct tty_struct *tty)
557 {
558         tty_driver_remove_tty(tty->driver, tty);
559         /* We have our own method as we don't use the tty index */
560         kfree(tty->termios);
561 }
562
563 /* We have no need to install and remove our tty objects as devpts does all
564    the work for us */
565
566 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
567 {
568         struct tty_struct *o_tty;
569         int idx = tty->index;
570
571         o_tty = alloc_tty_struct();
572         if (!o_tty)
573                 return -ENOMEM;
574         if (!try_module_get(driver->other->owner)) {
575                 /* This cannot in fact currently happen */
576                 goto err_free_tty;
577         }
578         initialize_tty_struct(o_tty, driver->other, idx);
579
580         tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
581         if (tty->termios == NULL)
582                 goto err_free_mem;
583         *tty->termios = driver->init_termios;
584         tty->termios_locked = tty->termios + 1;
585
586         o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
587         if (o_tty->termios == NULL)
588                 goto err_free_mem;
589         *o_tty->termios = driver->other->init_termios;
590         o_tty->termios_locked = o_tty->termios + 1;
591
592         tty_driver_kref_get(driver->other);
593         if (driver->subtype == PTY_TYPE_MASTER)
594                 o_tty->count++;
595         /* Establish the links in both directions */
596         tty->link   = o_tty;
597         o_tty->link = tty;
598         /*
599          * All structures have been allocated, so now we install them.
600          * Failures after this point use release_tty to clean up, so
601          * there's no need to null out the local pointers.
602          */
603         tty_driver_kref_get(driver);
604         tty->count++;
605         pty_inc_count(); /* tty */
606         pty_inc_count(); /* tty->link */
607         return 0;
608 err_free_mem:
609         deinitialize_tty_struct(o_tty);
610         kfree(o_tty->termios);
611         kfree(tty->termios);
612         module_put(o_tty->driver->owner);
613 err_free_tty:
614         free_tty_struct(o_tty);
615         return -ENOMEM;
616 }
617
618 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
619 {
620         pty_dec_count();
621 }
622
623 static const struct tty_operations ptm_unix98_ops = {
624         .lookup = ptm_unix98_lookup,
625         .install = pty_unix98_install,
626         .remove = pty_unix98_remove,
627         .open = pty_open,
628         .close = pty_close,
629         .write = pty_write,
630         .write_room = pty_write_room,
631         .flush_buffer = pty_flush_buffer,
632         .chars_in_buffer = pty_chars_in_buffer,
633         .unthrottle = pty_unthrottle,
634         .set_termios = pty_set_termios,
635         .ioctl = pty_unix98_ioctl,
636         .shutdown = pty_unix98_shutdown,
637         .resize = pty_resize
638 };
639
640 static const struct tty_operations pty_unix98_ops = {
641         .lookup = pts_unix98_lookup,
642         .install = pty_unix98_install,
643         .remove = pty_unix98_remove,
644         .open = pty_open,
645         .close = pty_close,
646         .write = pty_write,
647         .write_room = pty_write_room,
648         .flush_buffer = pty_flush_buffer,
649         .chars_in_buffer = pty_chars_in_buffer,
650         .unthrottle = pty_unthrottle,
651         .set_termios = pty_set_termios,
652         .shutdown = pty_unix98_shutdown
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         int retval;
671         int index;
672
673         nonseekable_open(inode, filp);
674
675         /* We refuse fsnotify events on ptmx, since it's a shared resource */
676         filp->f_mode |= FMODE_NONOTIFY;
677
678         retval = tty_alloc_file(filp);
679         if (retval)
680                 return retval;
681
682         /* find a device that is not in use. */
683         tty_lock();
684         index = devpts_new_index(inode);
685         tty_unlock();
686         if (index < 0) {
687                 retval = index;
688                 goto err_file;
689         }
690
691         mutex_lock(&tty_mutex);
692         tty_lock();
693         tty = tty_init_dev(ptm_driver, index, 1);
694         mutex_unlock(&tty_mutex);
695
696         if (IS_ERR(tty)) {
697                 retval = PTR_ERR(tty);
698                 goto out;
699         }
700
701         set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
702
703         tty_add_file(tty, filp);
704
705         retval = devpts_pty_new(inode, tty->link);
706         if (retval)
707                 goto err_release;
708
709         retval = ptm_driver->ops->open(tty, filp);
710         if (retval)
711                 goto err_release;
712
713         tty_unlock();
714         return 0;
715 err_release:
716         tty_unlock();
717         tty_release(inode, filp);
718         return retval;
719 out:
720         devpts_kill_index(inode, index);
721         tty_unlock();
722 err_file:
723         tty_free_file(filp);
724         return retval;
725 }
726
727 static struct file_operations ptmx_fops;
728
729 static void __init unix98_pty_init(void)
730 {
731         ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
732         if (!ptm_driver)
733                 panic("Couldn't allocate Unix98 ptm driver");
734         pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
735         if (!pts_driver)
736                 panic("Couldn't allocate Unix98 pts driver");
737
738         ptm_driver->owner = THIS_MODULE;
739         ptm_driver->driver_name = "pty_master";
740         ptm_driver->name = "ptm";
741         ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
742         ptm_driver->minor_start = 0;
743         ptm_driver->type = TTY_DRIVER_TYPE_PTY;
744         ptm_driver->subtype = PTY_TYPE_MASTER;
745         ptm_driver->init_termios = tty_std_termios;
746         ptm_driver->init_termios.c_iflag = 0;
747         ptm_driver->init_termios.c_oflag = 0;
748         ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
749         ptm_driver->init_termios.c_lflag = 0;
750         ptm_driver->init_termios.c_ispeed = 38400;
751         ptm_driver->init_termios.c_ospeed = 38400;
752         ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
753                 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
754         ptm_driver->other = pts_driver;
755         tty_set_operations(ptm_driver, &ptm_unix98_ops);
756
757         pts_driver->owner = THIS_MODULE;
758         pts_driver->driver_name = "pty_slave";
759         pts_driver->name = "pts";
760         pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
761         pts_driver->minor_start = 0;
762         pts_driver->type = TTY_DRIVER_TYPE_PTY;
763         pts_driver->subtype = PTY_TYPE_SLAVE;
764         pts_driver->init_termios = tty_std_termios;
765         pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
766         pts_driver->init_termios.c_ispeed = 38400;
767         pts_driver->init_termios.c_ospeed = 38400;
768         pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
769                 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
770         pts_driver->other = ptm_driver;
771         tty_set_operations(pts_driver, &pty_unix98_ops);
772
773         if (tty_register_driver(ptm_driver))
774                 panic("Couldn't register Unix98 ptm driver");
775         if (tty_register_driver(pts_driver))
776                 panic("Couldn't register Unix98 pts driver");
777
778         register_sysctl_table(pty_root_table);
779
780         /* Now create the /dev/ptmx special device */
781         tty_default_fops(&ptmx_fops);
782         ptmx_fops.open = ptmx_open;
783
784         cdev_init(&ptmx_cdev, &ptmx_fops);
785         if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
786             register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
787                 panic("Couldn't register /dev/ptmx driver\n");
788         device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
789 }
790
791 #else
792 static inline void unix98_pty_init(void) { }
793 #endif
794
795 static int __init pty_init(void)
796 {
797         legacy_pty_init();
798         unix98_pty_init();
799         return 0;
800 }
801 module_init(pty_init);