Merge branch 'perfcounters-fixes-for-linus' of git://git.kernel.org/pub/scm/linux...
[deliverable/linux.git] / drivers / char / pty.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/char/pty.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Added support for a Unix98-style ptmx device.
7 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
1da177e4 8 *
fe9cd962
AC
9 * When reading this code see also fs/devpts. In particular note that the
10 * driver_data field is used by the devpts side as a binding to the devpts
11 * inode.
1da177e4
LT
12 */
13
fe9cd962 14#include <linux/module.h>
1da177e4
LT
15
16#include <linux/errno.h>
1da177e4
LT
17#include <linux/interrupt.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/fcntl.h>
21#include <linux/string.h>
22#include <linux/major.h>
23#include <linux/mm.h>
24#include <linux/init.h>
405f5571 25#include <linux/smp_lock.h>
1da177e4 26#include <linux/sysctl.h>
d81ed103 27#include <linux/device.h>
fe9cd962 28#include <linux/uaccess.h>
1da177e4
LT
29#include <linux/bitops.h>
30#include <linux/devpts_fs.h>
31
fe9cd962
AC
32#include <asm/system.h>
33
1da177e4 34#ifdef CONFIG_UNIX98_PTYS
da2bdf9a 35static struct tty_driver *ptm_driver;
1da177e4
LT
36static struct tty_driver *pts_driver;
37#endif
38
fe9cd962 39static void pty_close(struct tty_struct *tty, struct file *filp)
1da177e4 40{
fe9cd962
AC
41 BUG_ON(!tty);
42 if (tty->driver->subtype == PTY_TYPE_MASTER)
43 WARN_ON(tty->count > 1);
44 else {
1da177e4
LT
45 if (tty->count > 2)
46 return;
47 }
48 wake_up_interruptible(&tty->read_wait);
49 wake_up_interruptible(&tty->write_wait);
50 tty->packet = 0;
51 if (!tty->link)
52 return;
53 tty->link->packet = 0;
54 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
55 wake_up_interruptible(&tty->link->read_wait);
56 wake_up_interruptible(&tty->link->write_wait);
57 if (tty->driver->subtype == PTY_TYPE_MASTER) {
58 set_bit(TTY_OTHER_CLOSED, &tty->flags);
59#ifdef CONFIG_UNIX98_PTYS
60 if (tty->driver == ptm_driver)
15f1a633 61 devpts_pty_kill(tty->link);
1da177e4
LT
62#endif
63 tty_vhangup(tty->link);
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
fe9cd962 71 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
1da177e4
LT
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 */
fe9cd962 77static void pty_unthrottle(struct tty_struct *tty)
1da177e4 78{
d945cb9c 79 tty_wakeup(tty->link);
1da177e4
LT
80 set_bit(TTY_THROTTLED, &tty->flags);
81}
82
d945cb9c
AC
83/**
84 * pty_space - report space left for writing
85 * @to: tty we are writing into
1da177e4 86 *
d945cb9c
AC
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
92static 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
762faaed 105 *
d945cb9c
AC
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.
1da177e4 110 */
d945cb9c 111
fe9cd962
AC
112static int pty_write(struct tty_struct *tty, const unsigned char *buf,
113 int count)
1da177e4
LT
114{
115 struct tty_struct *to = tty->link;
d945cb9c 116 int c;
1da177e4 117
d945cb9c 118 if (tty->stopped)
1da177e4 119 return 0;
d945cb9c
AC
120
121 /* This isn't locked but our 8K is quite sloppy so no
122 big deal */
123
124 c = pty_space(to);
125 if (c > count)
126 c = count;
127 if (c > 0) {
128 /* Stuff the data into the input queue of the other end */
129 c = tty_insert_flip_string(to, buf, c);
130 /* And shovel */
131 tty_flip_buffer_push(to);
132 tty_wakeup(tty);
762faaed 133 }
1da177e4
LT
134 return c;
135}
136
d945cb9c
AC
137/**
138 * pty_write_room - write space
139 * @tty: tty we are writing from
140 *
141 * Report how many bytes the ldisc can send into the queue for
142 * the other device.
143 */
144
1da177e4
LT
145static int pty_write_room(struct tty_struct *tty)
146{
d945cb9c 147 return pty_space(tty->link);
1da177e4
LT
148}
149
d945cb9c
AC
150/**
151 * pty_chars_in_buffer - characters currently in our tx queue
152 * @tty: our tty
fe9cd962 153 *
d945cb9c
AC
154 * Report how much we have in the transmit queue. As everything is
155 * instantly at the other end this is easy to implement.
1da177e4 156 */
d945cb9c 157
1da177e4
LT
158static int pty_chars_in_buffer(struct tty_struct *tty)
159{
d945cb9c 160 return 0;
1da177e4
LT
161}
162
163/* Set the lock flag on a pty */
fe9cd962 164static int pty_set_lock(struct tty_struct *tty, int __user *arg)
1da177e4
LT
165{
166 int val;
fe9cd962 167 if (get_user(val, arg))
1da177e4
LT
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
176static void pty_flush_buffer(struct tty_struct *tty)
177{
178 struct tty_struct *to = tty->link;
04f378b1 179 unsigned long flags;
fe9cd962 180
1da177e4
LT
181 if (!to)
182 return;
d945cb9c 183 /* tty_buffer_flush(to); FIXME */
1da177e4 184 if (to->packet) {
04f378b1 185 spin_lock_irqsave(&tty->ctrl_lock, flags);
1da177e4
LT
186 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
187 wake_up_interruptible(&to->read_wait);
04f378b1 188 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1da177e4
LT
189 }
190}
191
fe9cd962 192static int pty_open(struct tty_struct *tty, struct file *filp)
1da177e4
LT
193{
194 int retval = -ENODEV;
195
196 if (!tty || !tty->link)
197 goto out;
198
199 retval = -EIO;
200 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
201 goto out;
202 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
203 goto out;
204 if (tty->link->count != 1)
205 goto out;
206
207 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
208 set_bit(TTY_THROTTLED, &tty->flags);
1da177e4
LT
209 retval = 0;
210out:
211 return retval;
212}
213
fe9cd962
AC
214static void pty_set_termios(struct tty_struct *tty,
215 struct ktermios *old_termios)
1da177e4 216{
fe9cd962
AC
217 tty->termios->c_cflag &= ~(CSIZE | PARENB);
218 tty->termios->c_cflag |= (CS8 | CREAD);
1da177e4
LT
219}
220
fc6f6238
AC
221/**
222 * pty_do_resize - resize event
223 * @tty: tty being resized
c774bda2 224 * @ws: window size being set.
fc6f6238
AC
225 *
226 * Update the termios variables and send the neccessary signals to
227 * peform a terminal resize correctly
228 */
229
230int pty_resize(struct tty_struct *tty, struct winsize *ws)
231{
232 struct pid *pgrp, *rpgrp;
233 unsigned long flags;
234 struct tty_struct *pty = tty->link;
235
236 /* For a PTY we need to lock the tty side */
237 mutex_lock(&tty->termios_mutex);
238 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
239 goto done;
240
241 /* Get the PID values and reference them so we can
242 avoid holding the tty ctrl lock while sending signals.
243 We need to lock these individually however. */
244
245 spin_lock_irqsave(&tty->ctrl_lock, flags);
246 pgrp = get_pid(tty->pgrp);
247 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
248
249 spin_lock_irqsave(&pty->ctrl_lock, flags);
250 rpgrp = get_pid(pty->pgrp);
251 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
252
253 if (pgrp)
254 kill_pgrp(pgrp, SIGWINCH, 1);
255 if (rpgrp != pgrp && rpgrp)
256 kill_pgrp(rpgrp, SIGWINCH, 1);
257
258 put_pid(pgrp);
259 put_pid(rpgrp);
260
261 tty->winsize = *ws;
262 pty->winsize = *ws; /* Never used so will go away soon */
263done:
264 mutex_unlock(&tty->termios_mutex);
265 return 0;
266}
267
bf970ee4
AC
268static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
269{
270 struct tty_struct *o_tty;
271 int idx = tty->index;
272 int retval;
273
274 o_tty = alloc_tty_struct();
275 if (!o_tty)
276 return -ENOMEM;
277 if (!try_module_get(driver->other->owner)) {
278 /* This cannot in fact currently happen */
279 free_tty_struct(o_tty);
280 return -ENOMEM;
281 }
282 initialize_tty_struct(o_tty, driver->other, idx);
283
284 /* We always use new tty termios data so we can do this
285 the easy way .. */
286 retval = tty_init_termios(tty);
287 if (retval)
288 goto free_mem_out;
289
290 retval = tty_init_termios(o_tty);
291 if (retval) {
292 tty_free_termios(tty);
293 goto free_mem_out;
294 }
fe9cd962 295
bf970ee4
AC
296 /*
297 * Everything allocated ... set up the o_tty structure.
298 */
299 driver->other->ttys[idx] = o_tty;
300 tty_driver_kref_get(driver->other);
301 if (driver->subtype == PTY_TYPE_MASTER)
302 o_tty->count++;
303 /* Establish the links in both directions */
304 tty->link = o_tty;
305 o_tty->link = tty;
306
307 tty_driver_kref_get(driver);
308 tty->count++;
309 driver->ttys[idx] = tty;
310 return 0;
311free_mem_out:
312 module_put(o_tty->driver->owner);
313 free_tty_struct(o_tty);
314 return -ENOMEM;
315}
316
317
b68e31d0 318static const struct tty_operations pty_ops = {
bf970ee4 319 .install = pty_install,
1da177e4
LT
320 .open = pty_open,
321 .close = pty_close,
322 .write = pty_write,
323 .write_room = pty_write_room,
324 .flush_buffer = pty_flush_buffer,
325 .chars_in_buffer = pty_chars_in_buffer,
326 .unthrottle = pty_unthrottle,
327 .set_termios = pty_set_termios,
fc6f6238 328 .resize = pty_resize
1da177e4
LT
329};
330
331/* Traditional BSD devices */
332#ifdef CONFIG_LEGACY_PTYS
333static struct tty_driver *pty_driver, *pty_slave_driver;
334
335static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
336 unsigned int cmd, unsigned long arg)
337{
338 switch (cmd) {
339 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
340 return pty_set_lock(tty, (int __user *) arg);
341 }
342 return -ENOIOCTLCMD;
343}
344
dc8c8587
KS
345static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
346module_param(legacy_count, int, 0);
347
3e8e88ca
AC
348static const struct tty_operations pty_ops_bsd = {
349 .open = pty_open,
350 .close = pty_close,
351 .write = pty_write,
352 .write_room = pty_write_room,
353 .flush_buffer = pty_flush_buffer,
354 .chars_in_buffer = pty_chars_in_buffer,
355 .unthrottle = pty_unthrottle,
356 .set_termios = pty_set_termios,
357 .ioctl = pty_bsd_ioctl,
fc6f6238 358 .resize = pty_resize
3e8e88ca
AC
359};
360
1da177e4
LT
361static void __init legacy_pty_init(void)
362{
dc8c8587
KS
363 if (legacy_count <= 0)
364 return;
1da177e4 365
dc8c8587 366 pty_driver = alloc_tty_driver(legacy_count);
1da177e4
LT
367 if (!pty_driver)
368 panic("Couldn't allocate pty driver");
369
dc8c8587 370 pty_slave_driver = alloc_tty_driver(legacy_count);
1da177e4
LT
371 if (!pty_slave_driver)
372 panic("Couldn't allocate pty slave driver");
373
374 pty_driver->owner = THIS_MODULE;
375 pty_driver->driver_name = "pty_master";
376 pty_driver->name = "pty";
1da177e4
LT
377 pty_driver->major = PTY_MASTER_MAJOR;
378 pty_driver->minor_start = 0;
379 pty_driver->type = TTY_DRIVER_TYPE_PTY;
380 pty_driver->subtype = PTY_TYPE_MASTER;
381 pty_driver->init_termios = tty_std_termios;
382 pty_driver->init_termios.c_iflag = 0;
383 pty_driver->init_termios.c_oflag = 0;
384 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
385 pty_driver->init_termios.c_lflag = 0;
606d099c
AC
386 pty_driver->init_termios.c_ispeed = 38400;
387 pty_driver->init_termios.c_ospeed = 38400;
1da177e4
LT
388 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
389 pty_driver->other = pty_slave_driver;
390 tty_set_operations(pty_driver, &pty_ops);
1da177e4
LT
391
392 pty_slave_driver->owner = THIS_MODULE;
393 pty_slave_driver->driver_name = "pty_slave";
394 pty_slave_driver->name = "ttyp";
1da177e4
LT
395 pty_slave_driver->major = PTY_SLAVE_MAJOR;
396 pty_slave_driver->minor_start = 0;
397 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
398 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
399 pty_slave_driver->init_termios = tty_std_termios;
400 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099c
AC
401 pty_slave_driver->init_termios.c_ispeed = 38400;
402 pty_slave_driver->init_termios.c_ospeed = 38400;
1da177e4
LT
403 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
404 TTY_DRIVER_REAL_RAW;
405 pty_slave_driver->other = pty_driver;
406 tty_set_operations(pty_slave_driver, &pty_ops);
407
408 if (tty_register_driver(pty_driver))
409 panic("Couldn't register pty driver");
410 if (tty_register_driver(pty_slave_driver))
411 panic("Couldn't register pty slave driver");
412}
413#else
414static inline void legacy_pty_init(void) { }
415#endif
416
417/* Unix98 devices */
418#ifdef CONFIG_UNIX98_PTYS
419/*
420 * sysctl support for setting limits on the number of Unix98 ptys allocated.
421 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
422 */
423int pty_limit = NR_UNIX98_PTY_DEFAULT;
fe9cd962 424static int pty_limit_min;
1da177e4 425static int pty_limit_max = NR_UNIX98_PTY_MAX;
fe9cd962 426static int pty_count;
1da177e4 427
d81ed103
AC
428static struct cdev ptmx_cdev;
429
35834ca1 430static struct ctl_table pty_table[] = {
1da177e4
LT
431 {
432 .ctl_name = PTY_MAX,
433 .procname = "max",
434 .maxlen = sizeof(int),
435 .mode = 0644,
436 .data = &pty_limit,
437 .proc_handler = &proc_dointvec_minmax,
438 .strategy = &sysctl_intvec,
439 .extra1 = &pty_limit_min,
440 .extra2 = &pty_limit_max,
441 }, {
442 .ctl_name = PTY_NR,
443 .procname = "nr",
444 .maxlen = sizeof(int),
445 .mode = 0444,
bf970ee4 446 .data = &pty_count,
1da177e4
LT
447 .proc_handler = &proc_dointvec,
448 }, {
449 .ctl_name = 0
450 }
451};
452
35834ca1
EB
453static struct ctl_table pty_kern_table[] = {
454 {
455 .ctl_name = KERN_PTY,
456 .procname = "pty",
457 .mode = 0555,
458 .child = pty_table,
459 },
460 {}
461};
462
463static struct ctl_table pty_root_table[] = {
464 {
465 .ctl_name = CTL_KERN,
466 .procname = "kernel",
467 .mode = 0555,
468 .child = pty_kern_table,
469 },
470 {}
471};
472
473
1da177e4
LT
474static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
475 unsigned int cmd, unsigned long arg)
476{
477 switch (cmd) {
478 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
479 return pty_set_lock(tty, (int __user *)arg);
480 case TIOCGPTN: /* Get PT Number */
481 return put_user(tty->index, (unsigned int __user *)arg);
482 }
483
484 return -ENOIOCTLCMD;
485}
486
99f1fe18
AC
487/**
488 * ptm_unix98_lookup - find a pty master
489 * @driver: ptm driver
490 * @idx: tty index
491 *
492 * Look up a pty master device. Called under the tty_mutex for now.
493 * This provides our locking.
494 */
495
15f1a633
SB
496static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
497 struct inode *ptm_inode, int idx)
99f1fe18 498{
15f1a633 499 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
99f1fe18
AC
500 if (tty)
501 tty = tty->link;
502 return tty;
503}
504
505/**
506 * pts_unix98_lookup - find a pty slave
507 * @driver: pts driver
508 * @idx: tty index
509 *
510 * Look up a pty master device. Called under the tty_mutex for now.
511 * This provides our locking.
512 */
513
15f1a633
SB
514static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
515 struct inode *pts_inode, int idx)
99f1fe18 516{
15f1a633 517 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
99f1fe18
AC
518 /* Master must be open before slave */
519 if (!tty)
520 return ERR_PTR(-EIO);
521 return tty;
522}
523
bf970ee4 524static void pty_unix98_shutdown(struct tty_struct *tty)
feebed65
AC
525{
526 /* We have our own method as we don't use the tty index */
527 kfree(tty->termios);
feebed65
AC
528}
529
8b0a88d5
AC
530/* We have no need to install and remove our tty objects as devpts does all
531 the work for us */
532
bf970ee4 533static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
8b0a88d5 534{
bf970ee4
AC
535 struct tty_struct *o_tty;
536 int idx = tty->index;
537
538 o_tty = alloc_tty_struct();
539 if (!o_tty)
540 return -ENOMEM;
541 if (!try_module_get(driver->other->owner)) {
542 /* This cannot in fact currently happen */
543 free_tty_struct(o_tty);
544 return -ENOMEM;
545 }
546 initialize_tty_struct(o_tty, driver->other, idx);
547
8dff04ea 548 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
bf970ee4
AC
549 if (tty->termios == NULL)
550 goto free_mem_out;
551 *tty->termios = driver->init_termios;
8dff04ea
AC
552 tty->termios_locked = tty->termios + 1;
553
554 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
bf970ee4
AC
555 if (o_tty->termios == NULL)
556 goto free_mem_out;
557 *o_tty->termios = driver->other->init_termios;
8dff04ea 558 o_tty->termios_locked = o_tty->termios + 1;
bf970ee4
AC
559
560 tty_driver_kref_get(driver->other);
561 if (driver->subtype == PTY_TYPE_MASTER)
562 o_tty->count++;
563 /* Establish the links in both directions */
564 tty->link = o_tty;
565 o_tty->link = tty;
566 /*
567 * All structures have been allocated, so now we install them.
568 * Failures after this point use release_tty to clean up, so
569 * there's no need to null out the local pointers.
570 */
571 tty_driver_kref_get(driver);
572 tty->count++;
573 pty_count++;
8b0a88d5 574 return 0;
bf970ee4 575free_mem_out:
8dff04ea 576 kfree(o_tty->termios);
bf970ee4
AC
577 module_put(o_tty->driver->owner);
578 free_tty_struct(o_tty);
8dff04ea 579 kfree(tty->termios);
bf970ee4 580 return -ENOMEM;
8b0a88d5
AC
581}
582
bf970ee4 583static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
8b0a88d5 584{
bf970ee4 585 pty_count--;
8b0a88d5
AC
586}
587
feebed65 588static const struct tty_operations ptm_unix98_ops = {
99f1fe18 589 .lookup = ptm_unix98_lookup,
bf970ee4
AC
590 .install = pty_unix98_install,
591 .remove = pty_unix98_remove,
3e8e88ca
AC
592 .open = pty_open,
593 .close = pty_close,
594 .write = pty_write,
595 .write_room = pty_write_room,
596 .flush_buffer = pty_flush_buffer,
597 .chars_in_buffer = pty_chars_in_buffer,
598 .unthrottle = pty_unthrottle,
599 .set_termios = pty_set_termios,
feebed65 600 .ioctl = pty_unix98_ioctl,
fc6f6238
AC
601 .shutdown = pty_unix98_shutdown,
602 .resize = pty_resize
3e8e88ca
AC
603};
604
99f1fe18
AC
605static const struct tty_operations pty_unix98_ops = {
606 .lookup = pts_unix98_lookup,
bf970ee4
AC
607 .install = pty_unix98_install,
608 .remove = pty_unix98_remove,
99f1fe18
AC
609 .open = pty_open,
610 .close = pty_close,
611 .write = pty_write,
612 .write_room = pty_write_room,
613 .flush_buffer = pty_flush_buffer,
614 .chars_in_buffer = pty_chars_in_buffer,
615 .unthrottle = pty_unthrottle,
616 .set_termios = pty_set_termios,
bf970ee4 617 .shutdown = pty_unix98_shutdown
99f1fe18 618};
d81ed103
AC
619
620/**
621 * ptmx_open - open a unix 98 pty master
622 * @inode: inode of device file
623 * @filp: file pointer to tty
624 *
625 * Allocate a unix98 pty master device from the ptmx driver.
626 *
627 * Locking: tty_mutex protects the init_dev work. tty->count should
628 * protect the rest.
629 * allocated_ptys_lock handles the list of free pty numbers
630 */
631
632static int __ptmx_open(struct inode *inode, struct file *filp)
633{
634 struct tty_struct *tty;
635 int retval;
636 int index;
637
638 nonseekable_open(inode, filp);
639
640 /* find a device that is not in use. */
15f1a633 641 index = devpts_new_index(inode);
d81ed103
AC
642 if (index < 0)
643 return index;
644
645 mutex_lock(&tty_mutex);
73ec06fc 646 tty = tty_init_dev(ptm_driver, index, 1);
d81ed103
AC
647 mutex_unlock(&tty_mutex);
648
73ec06fc
AC
649 if (IS_ERR(tty)) {
650 retval = PTR_ERR(tty);
d81ed103 651 goto out;
73ec06fc 652 }
d81ed103
AC
653
654 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
655 filp->private_data = tty;
656 file_move(filp, &tty->tty_files);
657
15f1a633 658 retval = devpts_pty_new(inode, tty->link);
d81ed103
AC
659 if (retval)
660 goto out1;
661
662 retval = ptm_driver->ops->open(tty, filp);
663 if (!retval)
664 return 0;
665out1:
666 tty_release_dev(filp);
667 return retval;
668out:
15f1a633 669 devpts_kill_index(inode, index);
d81ed103
AC
670 return retval;
671}
672
673static int ptmx_open(struct inode *inode, struct file *filp)
674{
675 int ret;
676
677 lock_kernel();
678 ret = __ptmx_open(inode, filp);
679 unlock_kernel();
680 return ret;
681}
682
683static struct file_operations ptmx_fops;
684
1da177e4
LT
685static void __init unix98_pty_init(void)
686{
1da177e4
LT
687 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
688 if (!ptm_driver)
689 panic("Couldn't allocate Unix98 ptm driver");
690 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
691 if (!pts_driver)
692 panic("Couldn't allocate Unix98 pts driver");
693
694 ptm_driver->owner = THIS_MODULE;
695 ptm_driver->driver_name = "pty_master";
696 ptm_driver->name = "ptm";
697 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
698 ptm_driver->minor_start = 0;
699 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
700 ptm_driver->subtype = PTY_TYPE_MASTER;
701 ptm_driver->init_termios = tty_std_termios;
702 ptm_driver->init_termios.c_iflag = 0;
703 ptm_driver->init_termios.c_oflag = 0;
704 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
705 ptm_driver->init_termios.c_lflag = 0;
606d099c
AC
706 ptm_driver->init_termios.c_ispeed = 38400;
707 ptm_driver->init_termios.c_ospeed = 38400;
1da177e4 708 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
331b8319 709 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
1da177e4 710 ptm_driver->other = pts_driver;
feebed65 711 tty_set_operations(ptm_driver, &ptm_unix98_ops);
1da177e4
LT
712
713 pts_driver->owner = THIS_MODULE;
714 pts_driver->driver_name = "pty_slave";
715 pts_driver->name = "pts";
716 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
717 pts_driver->minor_start = 0;
718 pts_driver->type = TTY_DRIVER_TYPE_PTY;
719 pts_driver->subtype = PTY_TYPE_SLAVE;
720 pts_driver->init_termios = tty_std_termios;
721 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099c
AC
722 pts_driver->init_termios.c_ispeed = 38400;
723 pts_driver->init_termios.c_ospeed = 38400;
1da177e4 724 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
331b8319 725 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
1da177e4 726 pts_driver->other = ptm_driver;
99f1fe18 727 tty_set_operations(pts_driver, &pty_unix98_ops);
fe9cd962 728
1da177e4
LT
729 if (tty_register_driver(ptm_driver))
730 panic("Couldn't register Unix98 ptm driver");
731 if (tty_register_driver(pts_driver))
732 panic("Couldn't register Unix98 pts driver");
733
fe9cd962 734 register_sysctl_table(pty_root_table);
d81ed103
AC
735
736 /* Now create the /dev/ptmx special device */
737 tty_default_fops(&ptmx_fops);
738 ptmx_fops.open = ptmx_open;
739
740 cdev_init(&ptmx_cdev, &ptmx_fops);
741 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
742 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
743 panic("Couldn't register /dev/ptmx driver\n");
744 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
1da177e4 745}
d81ed103 746
1da177e4
LT
747#else
748static inline void unix98_pty_init(void) { }
749#endif
750
751static int __init pty_init(void)
752{
753 legacy_pty_init();
754 unix98_pty_init();
755 return 0;
756}
757module_init(pty_init);
This page took 0.466989 seconds and 5 git commands to generate.