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