mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / tty / pty.c
blobc3f9b9920d8d491cacd60c54e3c69d60d9d37b1d
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
7 */
9 #include <linux/module.h>
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>
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
35 static void pty_close(struct tty_struct *tty, struct file *filp)
37 BUG_ON(!tty);
38 if (tty->driver->subtype == PTY_TYPE_MASTER)
39 WARN_ON(tty->count > 1);
40 else {
41 if (test_bit(TTY_IO_ERROR, &tty->flags))
42 return;
43 if (tty->count > 2)
44 return;
46 set_bit(TTY_IO_ERROR, &tty->flags);
47 wake_up_interruptible(&tty->read_wait);
48 wake_up_interruptible(&tty->write_wait);
49 tty->packet = 0;
50 /* Review - krefs on tty_link ?? */
51 if (!tty->link)
52 return;
53 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
54 wake_up_interruptible(&tty->link->read_wait);
55 wake_up_interruptible(&tty->link->write_wait);
56 if (tty->driver->subtype == PTY_TYPE_MASTER) {
57 set_bit(TTY_OTHER_CLOSED, &tty->flags);
58 #ifdef CONFIG_UNIX98_PTYS
59 if (tty->driver == ptm_driver) {
60 mutex_lock(&devpts_mutex);
61 if (tty->link->driver_data)
62 devpts_pty_kill(tty->link->driver_data);
63 mutex_unlock(&devpts_mutex);
65 #endif
66 tty_unlock(tty);
67 tty_vhangup(tty->link);
68 tty_lock(tty);
73 * The unthrottle routine is called by the line discipline to signal
74 * that it can receive more characters. For PTY's, the TTY_THROTTLED
75 * flag is always set, to force the line discipline to always call the
76 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
77 * characters in the queue. This is necessary since each time this
78 * happens, we need to wake up any sleeping processes that could be
79 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
80 * for the pty buffer to be drained.
82 static void pty_unthrottle(struct tty_struct *tty)
84 tty_wakeup(tty->link);
85 set_bit(TTY_THROTTLED, &tty->flags);
88 /**
89 * pty_space - report space left for writing
90 * @to: tty we are writing into
92 * Limit the buffer space used by ptys to 8k.
95 static int pty_space(struct tty_struct *to)
97 int n = tty_buffer_space_avail(to->port);
98 return min(n, 8192);
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
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.
113 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
115 struct tty_struct *to = tty->link;
117 if (tty->stopped)
118 return 0;
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);
127 return c;
131 * pty_write_room - write space
132 * @tty: tty we are writing from
134 * Report how many bytes the ldisc can send into the queue for
135 * the other device.
138 static int pty_write_room(struct tty_struct *tty)
140 if (tty->stopped)
141 return 0;
142 return pty_space(tty->link);
146 * pty_chars_in_buffer - characters currently in our tx queue
147 * @tty: our tty
149 * Report how much we have in the transmit queue. As everything is
150 * instantly at the other end this is easy to implement.
153 static int pty_chars_in_buffer(struct tty_struct *tty)
155 return 0;
158 /* Set the lock flag on a pty */
159 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
161 int val;
162 if (get_user(val, arg))
163 return -EFAULT;
164 if (val)
165 set_bit(TTY_PTY_LOCK, &tty->flags);
166 else
167 clear_bit(TTY_PTY_LOCK, &tty->flags);
168 return 0;
171 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
173 int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
174 return put_user(locked, arg);
177 /* Set the packet mode on a pty */
178 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
180 unsigned long flags;
181 int pktmode;
183 if (get_user(pktmode, arg))
184 return -EFAULT;
186 spin_lock_irqsave(&tty->ctrl_lock, flags);
187 if (pktmode) {
188 if (!tty->packet) {
189 tty->packet = 1;
190 tty->link->ctrl_status = 0;
192 } else
193 tty->packet = 0;
194 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
196 return 0;
199 /* Get the packet mode of a pty */
200 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
202 int pktmode = tty->packet;
203 return put_user(pktmode, arg);
206 /* Send a signal to the slave */
207 static int pty_signal(struct tty_struct *tty, int sig)
209 unsigned long flags;
210 struct pid *pgrp;
212 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
213 return -EINVAL;
215 if (tty->link) {
216 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
217 pgrp = get_pid(tty->link->pgrp);
218 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
220 kill_pgrp(pgrp, sig, 1);
221 put_pid(pgrp);
223 return 0;
226 static void pty_flush_buffer(struct tty_struct *tty)
228 struct tty_struct *to = tty->link;
229 unsigned long flags;
231 if (!to)
232 return;
233 /* tty_buffer_flush(to); FIXME */
234 if (to->packet) {
235 spin_lock_irqsave(&tty->ctrl_lock, flags);
236 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
237 wake_up_interruptible(&to->read_wait);
238 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
242 static int pty_open(struct tty_struct *tty, struct file *filp)
244 if (!tty || !tty->link)
245 return -ENODEV;
247 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
248 goto out;
249 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
250 goto out;
251 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
252 goto out;
254 clear_bit(TTY_IO_ERROR, &tty->flags);
255 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
256 set_bit(TTY_THROTTLED, &tty->flags);
257 return 0;
259 out:
260 set_bit(TTY_IO_ERROR, &tty->flags);
261 return -EIO;
264 static void pty_set_termios(struct tty_struct *tty,
265 struct ktermios *old_termios)
267 tty->termios.c_cflag &= ~(CSIZE | PARENB);
268 tty->termios.c_cflag |= (CS8 | CREAD);
272 * pty_do_resize - resize event
273 * @tty: tty being resized
274 * @ws: window size being set.
276 * Update the termios variables and send the necessary signals to
277 * peform a terminal resize correctly
280 static int pty_resize(struct tty_struct *tty, struct winsize *ws)
282 struct pid *pgrp, *rpgrp;
283 unsigned long flags;
284 struct tty_struct *pty = tty->link;
286 /* For a PTY we need to lock the tty side */
287 mutex_lock(&tty->winsize_mutex);
288 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
289 goto done;
291 /* Get the PID values and reference them so we can
292 avoid holding the tty ctrl lock while sending signals.
293 We need to lock these individually however. */
295 spin_lock_irqsave(&tty->ctrl_lock, flags);
296 pgrp = get_pid(tty->pgrp);
297 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
299 spin_lock_irqsave(&pty->ctrl_lock, flags);
300 rpgrp = get_pid(pty->pgrp);
301 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
303 if (pgrp)
304 kill_pgrp(pgrp, SIGWINCH, 1);
305 if (rpgrp != pgrp && rpgrp)
306 kill_pgrp(rpgrp, SIGWINCH, 1);
308 put_pid(pgrp);
309 put_pid(rpgrp);
311 tty->winsize = *ws;
312 pty->winsize = *ws; /* Never used so will go away soon */
313 done:
314 mutex_unlock(&tty->winsize_mutex);
315 return 0;
319 * pty_common_install - set up the pty pair
320 * @driver: the pty driver
321 * @tty: the tty being instantiated
322 * @bool: legacy, true if this is BSD style
324 * Perform the initial set up for the tty/pty pair. Called from the
325 * tty layer when the port is first opened.
327 * Locking: the caller must hold the tty_mutex
329 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
330 bool legacy)
332 struct tty_struct *o_tty;
333 struct tty_port *ports[2];
334 int idx = tty->index;
335 int retval = -ENOMEM;
337 o_tty = alloc_tty_struct();
338 if (!o_tty)
339 goto err;
340 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
341 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
342 if (!ports[0] || !ports[1])
343 goto err_free_tty;
344 if (!try_module_get(driver->other->owner)) {
345 /* This cannot in fact currently happen */
346 goto err_free_tty;
348 initialize_tty_struct(o_tty, driver->other, idx);
350 if (legacy) {
351 /* We always use new tty termios data so we can do this
352 the easy way .. */
353 retval = tty_init_termios(tty);
354 if (retval)
355 goto err_deinit_tty;
357 retval = tty_init_termios(o_tty);
358 if (retval)
359 goto err_free_termios;
361 driver->other->ttys[idx] = o_tty;
362 driver->ttys[idx] = tty;
363 } else {
364 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
365 tty->termios = driver->init_termios;
366 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
367 o_tty->termios = driver->other->init_termios;
371 * Everything allocated ... set up the o_tty structure.
373 tty_driver_kref_get(driver->other);
374 if (driver->subtype == PTY_TYPE_MASTER)
375 o_tty->count++;
376 /* Establish the links in both directions */
377 tty->link = o_tty;
378 o_tty->link = tty;
379 tty_port_init(ports[0]);
380 tty_port_init(ports[1]);
381 o_tty->port = ports[0];
382 tty->port = ports[1];
383 o_tty->port->itty = o_tty;
385 tty_driver_kref_get(driver);
386 tty->count++;
387 return 0;
388 err_free_termios:
389 if (legacy)
390 tty_free_termios(tty);
391 err_deinit_tty:
392 deinitialize_tty_struct(o_tty);
393 module_put(o_tty->driver->owner);
394 err_free_tty:
395 kfree(ports[0]);
396 kfree(ports[1]);
397 free_tty_struct(o_tty);
398 err:
399 return retval;
402 static void pty_cleanup(struct tty_struct *tty)
404 tty_port_put(tty->port);
407 /* Traditional BSD devices */
408 #ifdef CONFIG_LEGACY_PTYS
410 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
412 return pty_common_install(driver, tty, true);
415 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
417 struct tty_struct *pair = tty->link;
418 driver->ttys[tty->index] = NULL;
419 if (pair)
420 pair->driver->ttys[pair->index] = NULL;
423 static int pty_bsd_ioctl(struct tty_struct *tty,
424 unsigned int cmd, unsigned long arg)
426 switch (cmd) {
427 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
428 return pty_set_lock(tty, (int __user *) arg);
429 case TIOCGPTLCK: /* Get PT Lock status */
430 return pty_get_lock(tty, (int __user *)arg);
431 case TIOCPKT: /* Set PT packet mode */
432 return pty_set_pktmode(tty, (int __user *)arg);
433 case TIOCGPKT: /* Get PT packet mode */
434 return pty_get_pktmode(tty, (int __user *)arg);
435 case TIOCSIG: /* Send signal to other side of pty */
436 return pty_signal(tty, (int) arg);
437 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
438 return -EINVAL;
440 return -ENOIOCTLCMD;
443 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
444 module_param(legacy_count, int, 0);
447 * The master side of a pty can do TIOCSPTLCK and thus
448 * has pty_bsd_ioctl.
450 static const struct tty_operations master_pty_ops_bsd = {
451 .install = pty_install,
452 .open = pty_open,
453 .close = pty_close,
454 .write = pty_write,
455 .write_room = pty_write_room,
456 .flush_buffer = pty_flush_buffer,
457 .chars_in_buffer = pty_chars_in_buffer,
458 .unthrottle = pty_unthrottle,
459 .set_termios = pty_set_termios,
460 .ioctl = pty_bsd_ioctl,
461 .cleanup = pty_cleanup,
462 .resize = pty_resize,
463 .remove = pty_remove
466 static const struct tty_operations slave_pty_ops_bsd = {
467 .install = pty_install,
468 .open = pty_open,
469 .close = pty_close,
470 .write = pty_write,
471 .write_room = pty_write_room,
472 .flush_buffer = pty_flush_buffer,
473 .chars_in_buffer = pty_chars_in_buffer,
474 .unthrottle = pty_unthrottle,
475 .set_termios = pty_set_termios,
476 .cleanup = pty_cleanup,
477 .resize = pty_resize,
478 .remove = pty_remove
481 static void __init legacy_pty_init(void)
483 struct tty_driver *pty_driver, *pty_slave_driver;
485 if (legacy_count <= 0)
486 return;
488 pty_driver = tty_alloc_driver(legacy_count,
489 TTY_DRIVER_RESET_TERMIOS |
490 TTY_DRIVER_REAL_RAW |
491 TTY_DRIVER_DYNAMIC_ALLOC);
492 if (IS_ERR(pty_driver))
493 panic("Couldn't allocate pty driver");
495 pty_slave_driver = tty_alloc_driver(legacy_count,
496 TTY_DRIVER_RESET_TERMIOS |
497 TTY_DRIVER_REAL_RAW |
498 TTY_DRIVER_DYNAMIC_ALLOC);
499 if (IS_ERR(pty_slave_driver))
500 panic("Couldn't allocate pty slave driver");
502 pty_driver->driver_name = "pty_master";
503 pty_driver->name = "pty";
504 pty_driver->major = PTY_MASTER_MAJOR;
505 pty_driver->minor_start = 0;
506 pty_driver->type = TTY_DRIVER_TYPE_PTY;
507 pty_driver->subtype = PTY_TYPE_MASTER;
508 pty_driver->init_termios = tty_std_termios;
509 pty_driver->init_termios.c_iflag = 0;
510 pty_driver->init_termios.c_oflag = 0;
511 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
512 pty_driver->init_termios.c_lflag = 0;
513 pty_driver->init_termios.c_ispeed = 38400;
514 pty_driver->init_termios.c_ospeed = 38400;
515 pty_driver->other = pty_slave_driver;
516 tty_set_operations(pty_driver, &master_pty_ops_bsd);
518 pty_slave_driver->driver_name = "pty_slave";
519 pty_slave_driver->name = "ttyp";
520 pty_slave_driver->major = PTY_SLAVE_MAJOR;
521 pty_slave_driver->minor_start = 0;
522 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
523 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
524 pty_slave_driver->init_termios = tty_std_termios;
525 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
526 pty_slave_driver->init_termios.c_ispeed = 38400;
527 pty_slave_driver->init_termios.c_ospeed = 38400;
528 pty_slave_driver->other = pty_driver;
529 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
531 if (tty_register_driver(pty_driver))
532 panic("Couldn't register pty driver");
533 if (tty_register_driver(pty_slave_driver))
534 panic("Couldn't register pty slave driver");
536 #else
537 static inline void legacy_pty_init(void) { }
538 #endif
540 /* Unix98 devices */
541 #ifdef CONFIG_UNIX98_PTYS
543 static struct cdev ptmx_cdev;
545 static int pty_unix98_ioctl(struct tty_struct *tty,
546 unsigned int cmd, unsigned long arg)
548 switch (cmd) {
549 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
550 return pty_set_lock(tty, (int __user *)arg);
551 case TIOCGPTLCK: /* Get PT Lock status */
552 return pty_get_lock(tty, (int __user *)arg);
553 case TIOCPKT: /* Set PT packet mode */
554 return pty_set_pktmode(tty, (int __user *)arg);
555 case TIOCGPKT: /* Get PT packet mode */
556 return pty_get_pktmode(tty, (int __user *)arg);
557 case TIOCGPTN: /* Get PT Number */
558 return put_user(tty->index, (unsigned int __user *)arg);
559 case TIOCSIG: /* Send signal to other side of pty */
560 return pty_signal(tty, (int) arg);
563 return -ENOIOCTLCMD;
567 * ptm_unix98_lookup - find a pty master
568 * @driver: ptm driver
569 * @idx: tty index
571 * Look up a pty master device. Called under the tty_mutex for now.
572 * This provides our locking.
575 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
576 struct inode *ptm_inode, int idx)
578 /* Master must be open via /dev/ptmx */
579 return ERR_PTR(-EIO);
583 * pts_unix98_lookup - find a pty slave
584 * @driver: pts driver
585 * @idx: tty index
587 * Look up a pty master device. Called under the tty_mutex for now.
588 * This provides our locking for the tty pointer.
591 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
592 struct inode *pts_inode, int idx)
594 struct tty_struct *tty;
596 mutex_lock(&devpts_mutex);
597 tty = devpts_get_priv(pts_inode);
598 mutex_unlock(&devpts_mutex);
599 /* Master must be open before slave */
600 if (!tty)
601 return ERR_PTR(-EIO);
602 return tty;
605 /* We have no need to install and remove our tty objects as devpts does all
606 the work for us */
608 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
610 return pty_common_install(driver, tty, false);
613 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
617 /* this is called once with whichever end is closed last */
618 static void pty_unix98_shutdown(struct tty_struct *tty)
620 struct inode *ptmx_inode;
622 if (tty->driver->subtype == PTY_TYPE_MASTER)
623 ptmx_inode = tty->driver_data;
624 else
625 ptmx_inode = tty->link->driver_data;
626 devpts_kill_index(ptmx_inode, tty->index);
627 devpts_del_ref(ptmx_inode);
630 static const struct tty_operations ptm_unix98_ops = {
631 .lookup = ptm_unix98_lookup,
632 .install = pty_unix98_install,
633 .remove = pty_unix98_remove,
634 .open = pty_open,
635 .close = pty_close,
636 .write = pty_write,
637 .write_room = pty_write_room,
638 .flush_buffer = pty_flush_buffer,
639 .chars_in_buffer = pty_chars_in_buffer,
640 .unthrottle = pty_unthrottle,
641 .set_termios = pty_set_termios,
642 .ioctl = pty_unix98_ioctl,
643 .resize = pty_resize,
644 .shutdown = pty_unix98_shutdown,
645 .cleanup = pty_cleanup
648 static const struct tty_operations pty_unix98_ops = {
649 .lookup = pts_unix98_lookup,
650 .install = pty_unix98_install,
651 .remove = pty_unix98_remove,
652 .open = pty_open,
653 .close = pty_close,
654 .write = pty_write,
655 .write_room = pty_write_room,
656 .flush_buffer = pty_flush_buffer,
657 .chars_in_buffer = pty_chars_in_buffer,
658 .unthrottle = pty_unthrottle,
659 .set_termios = pty_set_termios,
660 .shutdown = pty_unix98_shutdown,
661 .cleanup = pty_cleanup,
665 * ptmx_open - open a unix 98 pty master
666 * @inode: inode of device file
667 * @filp: file pointer to tty
669 * Allocate a unix98 pty master device from the ptmx driver.
671 * Locking: tty_mutex protects the init_dev work. tty->count should
672 * protect the rest.
673 * allocated_ptys_lock handles the list of free pty numbers
676 static int ptmx_open(struct inode *inode, struct file *filp)
678 struct tty_struct *tty;
679 struct inode *slave_inode;
680 int retval;
681 int index;
683 nonseekable_open(inode, filp);
685 /* We refuse fsnotify events on ptmx, since it's a shared resource */
686 filp->f_mode |= FMODE_NONOTIFY;
688 retval = tty_alloc_file(filp);
689 if (retval)
690 return retval;
692 /* find a device that is not in use. */
693 mutex_lock(&devpts_mutex);
694 index = devpts_new_index(inode);
695 if (index < 0) {
696 retval = index;
697 mutex_unlock(&devpts_mutex);
698 goto err_file;
701 mutex_unlock(&devpts_mutex);
703 mutex_lock(&tty_mutex);
704 tty = tty_init_dev(ptm_driver, index);
706 if (IS_ERR(tty)) {
707 retval = PTR_ERR(tty);
708 goto out;
711 /* The tty returned here is locked so we can safely
712 drop the mutex */
713 mutex_unlock(&tty_mutex);
715 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
716 tty->driver_data = inode;
719 * In the case where all references to ptmx inode are dropped and we
720 * still have /dev/tty opened pointing to the master/slave pair (ptmx
721 * is closed/released before /dev/tty), we must make sure that the inode
722 * is still valid when we call the final pty_unix98_shutdown, thus we
723 * hold an additional reference to the ptmx inode. For the same /dev/tty
724 * last close case, we also need to make sure the super_block isn't
725 * destroyed (devpts instance unmounted), before /dev/tty is closed and
726 * on its release devpts_kill_index is called.
728 devpts_add_ref(inode);
730 tty_add_file(tty, filp);
732 slave_inode = devpts_pty_new(inode,
733 MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
734 tty->link);
735 if (IS_ERR(slave_inode)) {
736 retval = PTR_ERR(slave_inode);
737 goto err_release;
739 tty->link->driver_data = slave_inode;
741 retval = ptm_driver->ops->open(tty, filp);
742 if (retval)
743 goto err_release;
745 tty_unlock(tty);
746 return 0;
747 err_release:
748 tty_unlock(tty);
749 tty_release(inode, filp);
750 return retval;
751 out:
752 mutex_unlock(&tty_mutex);
753 devpts_kill_index(inode, index);
754 err_file:
755 tty_free_file(filp);
756 return retval;
759 static struct file_operations ptmx_fops;
761 static void __init unix98_pty_init(void)
763 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
764 TTY_DRIVER_RESET_TERMIOS |
765 TTY_DRIVER_REAL_RAW |
766 TTY_DRIVER_DYNAMIC_DEV |
767 TTY_DRIVER_DEVPTS_MEM |
768 TTY_DRIVER_DYNAMIC_ALLOC);
769 if (IS_ERR(ptm_driver))
770 panic("Couldn't allocate Unix98 ptm driver");
771 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
772 TTY_DRIVER_RESET_TERMIOS |
773 TTY_DRIVER_REAL_RAW |
774 TTY_DRIVER_DYNAMIC_DEV |
775 TTY_DRIVER_DEVPTS_MEM |
776 TTY_DRIVER_DYNAMIC_ALLOC);
777 if (IS_ERR(pts_driver))
778 panic("Couldn't allocate Unix98 pts driver");
780 ptm_driver->driver_name = "pty_master";
781 ptm_driver->name = "ptm";
782 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
783 ptm_driver->minor_start = 0;
784 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
785 ptm_driver->subtype = PTY_TYPE_MASTER;
786 ptm_driver->init_termios = tty_std_termios;
787 ptm_driver->init_termios.c_iflag = 0;
788 ptm_driver->init_termios.c_oflag = 0;
789 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
790 ptm_driver->init_termios.c_lflag = 0;
791 ptm_driver->init_termios.c_ispeed = 38400;
792 ptm_driver->init_termios.c_ospeed = 38400;
793 ptm_driver->other = pts_driver;
794 tty_set_operations(ptm_driver, &ptm_unix98_ops);
796 pts_driver->driver_name = "pty_slave";
797 pts_driver->name = "pts";
798 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
799 pts_driver->minor_start = 0;
800 pts_driver->type = TTY_DRIVER_TYPE_PTY;
801 pts_driver->subtype = PTY_TYPE_SLAVE;
802 pts_driver->init_termios = tty_std_termios;
803 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
804 pts_driver->init_termios.c_ispeed = 38400;
805 pts_driver->init_termios.c_ospeed = 38400;
806 pts_driver->other = ptm_driver;
807 tty_set_operations(pts_driver, &pty_unix98_ops);
809 if (tty_register_driver(ptm_driver))
810 panic("Couldn't register Unix98 ptm driver");
811 if (tty_register_driver(pts_driver))
812 panic("Couldn't register Unix98 pts driver");
814 /* Now create the /dev/ptmx special device */
815 tty_default_fops(&ptmx_fops);
816 ptmx_fops.open = ptmx_open;
818 cdev_init(&ptmx_cdev, &ptmx_fops);
819 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
820 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
821 panic("Couldn't register /dev/ptmx driver");
822 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
825 #else
826 static inline void unix98_pty_init(void) { }
827 #endif
829 static int __init pty_init(void)
831 legacy_pty_init();
832 unix98_pty_init();
833 return 0;
835 module_init(pty_init);