tty: Check tty->count instead of TTY_CLOSING in tty_reopen()
[linux-2.6-block.git] / drivers / tty / pty.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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
1da177e4 6 *
1da177e4
LT
7 */
8
fe9cd962 9#include <linux/module.h>
1da177e4
LT
10
11#include <linux/errno.h>
1da177e4
LT
12#include <linux/interrupt.h>
13#include <linux/tty.h>
14#include <linux/tty_flip.h>
15#include <linux/fcntl.h>
d43c36dc 16#include <linux/sched.h>
1da177e4
LT
17#include <linux/string.h>
18#include <linux/major.h>
19#include <linux/mm.h>
20#include <linux/init.h>
d81ed103 21#include <linux/device.h>
fe9cd962 22#include <linux/uaccess.h>
1da177e4
LT
23#include <linux/bitops.h>
24#include <linux/devpts_fs.h>
5a0e3ad6 25#include <linux/slab.h>
d739e65b 26#include <linux/mutex.h>
01adc807 27#include <linux/poll.h>
1da177e4 28
fe9cd962 29
1da177e4 30#ifdef CONFIG_UNIX98_PTYS
da2bdf9a 31static struct tty_driver *ptm_driver;
1da177e4 32static struct tty_driver *pts_driver;
d739e65b 33static DEFINE_MUTEX(devpts_mutex);
1da177e4
LT
34#endif
35
fe9cd962 36static void pty_close(struct tty_struct *tty, struct file *filp)
1da177e4 37{
fe9cd962
AC
38 BUG_ON(!tty);
39 if (tty->driver->subtype == PTY_TYPE_MASTER)
40 WARN_ON(tty->count > 1);
41 else {
69939035
PH
42 if (test_bit(TTY_IO_ERROR, &tty->flags))
43 return;
1da177e4
LT
44 if (tty->count > 2)
45 return;
46 }
69939035 47 set_bit(TTY_IO_ERROR, &tty->flags);
1da177e4
LT
48 wake_up_interruptible(&tty->read_wait);
49 wake_up_interruptible(&tty->write_wait);
4ed60fc2 50 spin_lock_irq(&tty->ctrl_lock);
1da177e4 51 tty->packet = 0;
4ed60fc2 52 spin_unlock_irq(&tty->ctrl_lock);
89c8d91e 53 /* Review - krefs on tty_link ?? */
1da177e4
LT
54 if (!tty->link)
55 return;
1da177e4
LT
56 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
57 wake_up_interruptible(&tty->link->read_wait);
58 wake_up_interruptible(&tty->link->write_wait);
59 if (tty->driver->subtype == PTY_TYPE_MASTER) {
60 set_bit(TTY_OTHER_CLOSED, &tty->flags);
ce1000dd 61#ifdef CONFIG_UNIX98_PTYS
d739e65b 62 if (tty->driver == ptm_driver) {
b9f8033f 63 mutex_lock(&devpts_mutex);
7acf6cd8
PH
64 if (tty->link->driver_data)
65 devpts_pty_kill(tty->link->driver_data);
b9f8033f 66 mutex_unlock(&devpts_mutex);
d739e65b 67 }
ce1000dd 68#endif
89c8d91e 69 tty_unlock(tty);
11dbf203 70 tty_vhangup(tty->link);
89c8d91e 71 tty_lock(tty);
1da177e4
LT
72 }
73}
74
75/*
76 * The unthrottle routine is called by the line discipline to signal
77 * that it can receive more characters. For PTY's, the TTY_THROTTLED
78 * flag is always set, to force the line discipline to always call the
fe9cd962 79 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
1da177e4
LT
80 * characters in the queue. This is necessary since each time this
81 * happens, we need to wake up any sleeping processes that could be
82 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
83 * for the pty buffer to be drained.
84 */
fe9cd962 85static void pty_unthrottle(struct tty_struct *tty)
1da177e4 86{
d945cb9c 87 tty_wakeup(tty->link);
1da177e4
LT
88 set_bit(TTY_THROTTLED, &tty->flags);
89}
90
d945cb9c
AC
91/**
92 * pty_space - report space left for writing
93 * @to: tty we are writing into
1da177e4 94 *
7bfe0b71 95 * Limit the buffer space used by ptys to 8k.
d945cb9c
AC
96 */
97
98static int pty_space(struct tty_struct *to)
99{
7bfe0b71
PH
100 int n = tty_buffer_space_avail(to->port);
101 return min(n, 8192);
d945cb9c
AC
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
762faaed 109 *
d945cb9c
AC
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.
1da177e4 114 */
d945cb9c 115
ac89a917 116static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1da177e4
LT
117{
118 struct tty_struct *to = tty->link;
1da177e4 119
d945cb9c 120 if (tty->stopped)
1da177e4 121 return 0;
d945cb9c 122
d945cb9c
AC
123 if (c > 0) {
124 /* Stuff the data into the input queue of the other end */
05c7cd39 125 c = tty_insert_flip_string(to->port, buf, c);
d945cb9c 126 /* And shovel */
5ede5253 127 if (c)
2e124b4a 128 tty_flip_buffer_push(to->port);
762faaed 129 }
1da177e4
LT
130 return c;
131}
132
d945cb9c
AC
133/**
134 * pty_write_room - write space
135 * @tty: tty we are writing from
136 *
137 * Report how many bytes the ldisc can send into the queue for
138 * the other device.
139 */
140
1da177e4
LT
141static int pty_write_room(struct tty_struct *tty)
142{
85dfd81d
LT
143 if (tty->stopped)
144 return 0;
d945cb9c 145 return pty_space(tty->link);
1da177e4
LT
146}
147
d945cb9c
AC
148/**
149 * pty_chars_in_buffer - characters currently in our tx queue
150 * @tty: our tty
fe9cd962 151 *
d945cb9c
AC
152 * Report how much we have in the transmit queue. As everything is
153 * instantly at the other end this is easy to implement.
1da177e4 154 */
d945cb9c 155
1da177e4
LT
156static int pty_chars_in_buffer(struct tty_struct *tty)
157{
d945cb9c 158 return 0;
1da177e4
LT
159}
160
161/* Set the lock flag on a pty */
fe9cd962 162static int pty_set_lock(struct tty_struct *tty, int __user *arg)
1da177e4
LT
163{
164 int val;
fe9cd962 165 if (get_user(val, arg))
1da177e4
LT
166 return -EFAULT;
167 if (val)
168 set_bit(TTY_PTY_LOCK, &tty->flags);
169 else
170 clear_bit(TTY_PTY_LOCK, &tty->flags);
171 return 0;
172}
173
84fd7bdf
CG
174static int pty_get_lock(struct tty_struct *tty, int __user *arg)
175{
176 int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
177 return put_user(locked, arg);
178}
179
06026d91
CG
180/* Set the packet mode on a pty */
181static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
182{
06026d91
CG
183 int pktmode;
184
185 if (get_user(pktmode, arg))
186 return -EFAULT;
187
6054c16e 188 spin_lock_irq(&tty->ctrl_lock);
06026d91
CG
189 if (pktmode) {
190 if (!tty->packet) {
06026d91 191 tty->link->ctrl_status = 0;
2622d73e
PH
192 smp_mb();
193 tty->packet = 1;
06026d91
CG
194 }
195 } else
196 tty->packet = 0;
6054c16e 197 spin_unlock_irq(&tty->ctrl_lock);
06026d91
CG
198
199 return 0;
200}
201
84fd7bdf
CG
202/* Get the packet mode of a pty */
203static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
204{
205 int pktmode = tty->packet;
206 return put_user(pktmode, arg);
207}
208
26df6d13 209/* Send a signal to the slave */
210static int pty_signal(struct tty_struct *tty, int sig)
211{
26df6d13 212 struct pid *pgrp;
213
214 if (tty->link) {
5b239542
PH
215 pgrp = tty_get_pgrp(tty->link);
216 if (pgrp)
217 kill_pgrp(pgrp, sig, 1);
26df6d13 218 put_pid(pgrp);
219 }
220 return 0;
221}
222
1da177e4
LT
223static void pty_flush_buffer(struct tty_struct *tty)
224{
225 struct tty_struct *to = tty->link;
fe9cd962 226
1da177e4
LT
227 if (!to)
228 return;
d945cb9c 229 /* tty_buffer_flush(to); FIXME */
1da177e4 230 if (to->packet) {
6054c16e 231 spin_lock_irq(&tty->ctrl_lock);
1da177e4
LT
232 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
233 wake_up_interruptible(&to->read_wait);
6054c16e 234 spin_unlock_irq(&tty->ctrl_lock);
1da177e4
LT
235 }
236}
237
fe9cd962 238static int pty_open(struct tty_struct *tty, struct file *filp)
1da177e4 239{
1da177e4 240 if (!tty || !tty->link)
7c61c3d8 241 return -ENODEV;
69939035 242
1da177e4
LT
243 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
244 goto out;
245 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
246 goto out;
80cace72 247 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
1da177e4
LT
248 goto out;
249
69939035 250 clear_bit(TTY_IO_ERROR, &tty->flags);
1da177e4
LT
251 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
252 set_bit(TTY_THROTTLED, &tty->flags);
7c61c3d8
PH
253 return 0;
254
1da177e4 255out:
7c61c3d8
PH
256 set_bit(TTY_IO_ERROR, &tty->flags);
257 return -EIO;
1da177e4
LT
258}
259
fe9cd962
AC
260static void pty_set_termios(struct tty_struct *tty,
261 struct ktermios *old_termios)
1da177e4 262{
dbfcd851
PH
263 /* See if packet mode change of state. */
264 if (tty->link && tty->link->packet) {
265 int extproc = (old_termios->c_lflag & EXTPROC) |
266 (tty->termios.c_lflag & EXTPROC);
267 int old_flow = ((old_termios->c_iflag & IXON) &&
268 (old_termios->c_cc[VSTOP] == '\023') &&
269 (old_termios->c_cc[VSTART] == '\021'));
270 int new_flow = (I_IXON(tty) &&
271 STOP_CHAR(tty) == '\023' &&
272 START_CHAR(tty) == '\021');
273 if ((old_flow != new_flow) || extproc) {
4d8c1dff 274 spin_lock_irq(&tty->ctrl_lock);
dbfcd851
PH
275 if (old_flow != new_flow) {
276 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
277 if (new_flow)
278 tty->ctrl_status |= TIOCPKT_DOSTOP;
279 else
280 tty->ctrl_status |= TIOCPKT_NOSTOP;
281 }
282 if (extproc)
283 tty->ctrl_status |= TIOCPKT_IOCTL;
4d8c1dff 284 spin_unlock_irq(&tty->ctrl_lock);
dbfcd851
PH
285 wake_up_interruptible(&tty->link->read_wait);
286 }
287 }
288
adc8d746
AC
289 tty->termios.c_cflag &= ~(CSIZE | PARENB);
290 tty->termios.c_cflag |= (CS8 | CREAD);
1da177e4
LT
291}
292
fc6f6238
AC
293/**
294 * pty_do_resize - resize event
295 * @tty: tty being resized
c774bda2 296 * @ws: window size being set.
fc6f6238 297 *
3ad2f3fb 298 * Update the termios variables and send the necessary signals to
fc6f6238
AC
299 * peform a terminal resize correctly
300 */
301
159a8e92 302static int pty_resize(struct tty_struct *tty, struct winsize *ws)
fc6f6238
AC
303{
304 struct pid *pgrp, *rpgrp;
fc6f6238
AC
305 struct tty_struct *pty = tty->link;
306
307 /* For a PTY we need to lock the tty side */
dee4a0be 308 mutex_lock(&tty->winsize_mutex);
fc6f6238
AC
309 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
310 goto done;
311
5b239542
PH
312 /* Signal the foreground process group of both ptys */
313 pgrp = tty_get_pgrp(tty);
314 rpgrp = tty_get_pgrp(pty);
fc6f6238
AC
315
316 if (pgrp)
317 kill_pgrp(pgrp, SIGWINCH, 1);
318 if (rpgrp != pgrp && rpgrp)
319 kill_pgrp(rpgrp, SIGWINCH, 1);
320
321 put_pid(pgrp);
322 put_pid(rpgrp);
323
324 tty->winsize = *ws;
325 pty->winsize = *ws; /* Never used so will go away soon */
326done:
dee4a0be 327 mutex_unlock(&tty->winsize_mutex);
fc6f6238
AC
328 return 0;
329}
330
01adc807
PH
331/**
332 * pty_start - start() handler
333 * pty_stop - stop() handler
334 * @tty: tty being flow-controlled
335 *
336 * Propagates the TIOCPKT status to the master pty.
337 *
338 * NB: only the master pty can be in packet mode so only the slave
339 * needs start()/stop() handlers
340 */
341static void pty_start(struct tty_struct *tty)
342{
343 unsigned long flags;
344
01adc807 345 if (tty->link && tty->link->packet) {
54e8e5fc 346 spin_lock_irqsave(&tty->ctrl_lock, flags);
01adc807
PH
347 tty->ctrl_status &= ~TIOCPKT_STOP;
348 tty->ctrl_status |= TIOCPKT_START;
54e8e5fc 349 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
01adc807
PH
350 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
351 }
01adc807
PH
352}
353
354static void pty_stop(struct tty_struct *tty)
355{
356 unsigned long flags;
357
01adc807 358 if (tty->link && tty->link->packet) {
54e8e5fc 359 spin_lock_irqsave(&tty->ctrl_lock, flags);
01adc807
PH
360 tty->ctrl_status &= ~TIOCPKT_START;
361 tty->ctrl_status |= TIOCPKT_STOP;
54e8e5fc 362 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
01adc807
PH
363 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
364 }
01adc807
PH
365}
366
d155255a
AC
367/**
368 * pty_common_install - set up the pty pair
369 * @driver: the pty driver
370 * @tty: the tty being instantiated
2c964a2f 371 * @legacy: true if this is BSD style
d155255a
AC
372 *
373 * Perform the initial set up for the tty/pty pair. Called from the
374 * tty layer when the port is first opened.
375 *
376 * Locking: the caller must hold the tty_mutex
377 */
5d249bc6
JS
378static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
379 bool legacy)
bf970ee4
AC
380{
381 struct tty_struct *o_tty;
d03702a2 382 struct tty_port *ports[2];
bf970ee4 383 int idx = tty->index;
5d249bc6 384 int retval = -ENOMEM;
bf970ee4 385
d03702a2
JS
386 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
387 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
6f9ea7ad 388 if (!ports[0] || !ports[1])
2c964a2f 389 goto err;
bf970ee4
AC
390 if (!try_module_get(driver->other->owner)) {
391 /* This cannot in fact currently happen */
2c964a2f 392 goto err;
bf970ee4 393 }
2c964a2f
RV
394 o_tty = alloc_tty_struct(driver->other, idx);
395 if (!o_tty)
396 goto err_put_module;
bf970ee4 397
5d249bc6
JS
398 if (legacy) {
399 /* We always use new tty termios data so we can do this
400 the easy way .. */
401 retval = tty_init_termios(tty);
402 if (retval)
403 goto err_deinit_tty;
404
405 retval = tty_init_termios(o_tty);
406 if (retval)
407 goto err_free_termios;
408
409 driver->other->ttys[idx] = o_tty;
410 driver->ttys[idx] = tty;
411 } else {
adc8d746
AC
412 memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
413 tty->termios = driver->init_termios;
414 memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
415 o_tty->termios = driver->other->init_termios;
5d249bc6 416 }
fe9cd962 417
bf970ee4
AC
418 /*
419 * Everything allocated ... set up the o_tty structure.
420 */
bf970ee4
AC
421 tty_driver_kref_get(driver->other);
422 if (driver->subtype == PTY_TYPE_MASTER)
423 o_tty->count++;
424 /* Establish the links in both directions */
425 tty->link = o_tty;
426 o_tty->link = tty;
d03702a2
JS
427 tty_port_init(ports[0]);
428 tty_port_init(ports[1]);
429 o_tty->port = ports[0];
430 tty->port = ports[1];
967fab69 431 o_tty->port->itty = o_tty;
bf970ee4
AC
432
433 tty_driver_kref_get(driver);
434 tty->count++;
bf970ee4 435 return 0;
8a1b8d70 436err_free_termios:
5d249bc6
JS
437 if (legacy)
438 tty_free_termios(tty);
a9dccddb
JS
439err_deinit_tty:
440 deinitialize_tty_struct(o_tty);
2c964a2f
RV
441 free_tty_struct(o_tty);
442err_put_module:
07584d4a 443 module_put(driver->other->owner);
2c964a2f 444err:
d03702a2
JS
445 kfree(ports[0]);
446 kfree(ports[1]);
8a1b8d70 447 return retval;
bf970ee4
AC
448}
449
d03702a2
JS
450static void pty_cleanup(struct tty_struct *tty)
451{
81c79838 452 tty_port_put(tty->port);
d03702a2
JS
453}
454
5d249bc6
JS
455/* Traditional BSD devices */
456#ifdef CONFIG_LEGACY_PTYS
457
458static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
459{
460 return pty_common_install(driver, tty, true);
461}
462
d155255a
AC
463static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
464{
465 struct tty_struct *pair = tty->link;
466 driver->ttys[tty->index] = NULL;
467 if (pair)
468 pair->driver->ttys[pair->index] = NULL;
469}
470
6caa76b7 471static int pty_bsd_ioctl(struct tty_struct *tty,
1da177e4
LT
472 unsigned int cmd, unsigned long arg)
473{
474 switch (cmd) {
475 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
476 return pty_set_lock(tty, (int __user *) arg);
84fd7bdf
CG
477 case TIOCGPTLCK: /* Get PT Lock status */
478 return pty_get_lock(tty, (int __user *)arg);
06026d91
CG
479 case TIOCPKT: /* Set PT packet mode */
480 return pty_set_pktmode(tty, (int __user *)arg);
84fd7bdf
CG
481 case TIOCGPKT: /* Get PT packet mode */
482 return pty_get_pktmode(tty, (int __user *)arg);
26df6d13 483 case TIOCSIG: /* Send signal to other side of pty */
484 return pty_signal(tty, (int) arg);
ded2f295
JS
485 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
486 return -EINVAL;
1da177e4
LT
487 }
488 return -ENOIOCTLCMD;
489}
490
dc8c8587
KS
491static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
492module_param(legacy_count, int, 0);
493
342a5971
LT
494/*
495 * The master side of a pty can do TIOCSPTLCK and thus
496 * has pty_bsd_ioctl.
497 */
498static const struct tty_operations master_pty_ops_bsd = {
499 .install = pty_install,
3e8e88ca
AC
500 .open = pty_open,
501 .close = pty_close,
502 .write = pty_write,
503 .write_room = pty_write_room,
504 .flush_buffer = pty_flush_buffer,
505 .chars_in_buffer = pty_chars_in_buffer,
506 .unthrottle = pty_unthrottle,
3e8e88ca 507 .ioctl = pty_bsd_ioctl,
d03702a2 508 .cleanup = pty_cleanup,
d155255a
AC
509 .resize = pty_resize,
510 .remove = pty_remove
3e8e88ca
AC
511};
512
342a5971
LT
513static const struct tty_operations slave_pty_ops_bsd = {
514 .install = pty_install,
515 .open = pty_open,
516 .close = pty_close,
517 .write = pty_write,
518 .write_room = pty_write_room,
519 .flush_buffer = pty_flush_buffer,
520 .chars_in_buffer = pty_chars_in_buffer,
521 .unthrottle = pty_unthrottle,
522 .set_termios = pty_set_termios,
d03702a2 523 .cleanup = pty_cleanup,
d155255a 524 .resize = pty_resize,
01adc807
PH
525 .start = pty_start,
526 .stop = pty_stop,
d155255a 527 .remove = pty_remove
342a5971
LT
528};
529
1da177e4
LT
530static void __init legacy_pty_init(void)
531{
342a5971
LT
532 struct tty_driver *pty_driver, *pty_slave_driver;
533
dc8c8587
KS
534 if (legacy_count <= 0)
535 return;
1da177e4 536
21aca2fa
JS
537 pty_driver = tty_alloc_driver(legacy_count,
538 TTY_DRIVER_RESET_TERMIOS |
539 TTY_DRIVER_REAL_RAW |
540 TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344a 541 if (IS_ERR(pty_driver))
1da177e4
LT
542 panic("Couldn't allocate pty driver");
543
21aca2fa
JS
544 pty_slave_driver = tty_alloc_driver(legacy_count,
545 TTY_DRIVER_RESET_TERMIOS |
546 TTY_DRIVER_REAL_RAW |
547 TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344a 548 if (IS_ERR(pty_slave_driver))
1da177e4
LT
549 panic("Couldn't allocate pty slave driver");
550
1da177e4
LT
551 pty_driver->driver_name = "pty_master";
552 pty_driver->name = "pty";
1da177e4
LT
553 pty_driver->major = PTY_MASTER_MAJOR;
554 pty_driver->minor_start = 0;
555 pty_driver->type = TTY_DRIVER_TYPE_PTY;
556 pty_driver->subtype = PTY_TYPE_MASTER;
557 pty_driver->init_termios = tty_std_termios;
558 pty_driver->init_termios.c_iflag = 0;
559 pty_driver->init_termios.c_oflag = 0;
560 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
561 pty_driver->init_termios.c_lflag = 0;
606d099c
AC
562 pty_driver->init_termios.c_ispeed = 38400;
563 pty_driver->init_termios.c_ospeed = 38400;
1da177e4 564 pty_driver->other = pty_slave_driver;
342a5971 565 tty_set_operations(pty_driver, &master_pty_ops_bsd);
1da177e4 566
1da177e4
LT
567 pty_slave_driver->driver_name = "pty_slave";
568 pty_slave_driver->name = "ttyp";
1da177e4
LT
569 pty_slave_driver->major = PTY_SLAVE_MAJOR;
570 pty_slave_driver->minor_start = 0;
571 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
572 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
573 pty_slave_driver->init_termios = tty_std_termios;
574 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099c
AC
575 pty_slave_driver->init_termios.c_ispeed = 38400;
576 pty_slave_driver->init_termios.c_ospeed = 38400;
1da177e4 577 pty_slave_driver->other = pty_driver;
342a5971 578 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
1da177e4
LT
579
580 if (tty_register_driver(pty_driver))
581 panic("Couldn't register pty driver");
582 if (tty_register_driver(pty_slave_driver))
583 panic("Couldn't register pty slave driver");
584}
585#else
586static inline void legacy_pty_init(void) { }
587#endif
588
589/* Unix98 devices */
590#ifdef CONFIG_UNIX98_PTYS
1da177e4 591
d81ed103
AC
592static struct cdev ptmx_cdev;
593
6caa76b7 594static int pty_unix98_ioctl(struct tty_struct *tty,
1da177e4
LT
595 unsigned int cmd, unsigned long arg)
596{
597 switch (cmd) {
598 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
599 return pty_set_lock(tty, (int __user *)arg);
84fd7bdf
CG
600 case TIOCGPTLCK: /* Get PT Lock status */
601 return pty_get_lock(tty, (int __user *)arg);
06026d91
CG
602 case TIOCPKT: /* Set PT packet mode */
603 return pty_set_pktmode(tty, (int __user *)arg);
84fd7bdf
CG
604 case TIOCGPKT: /* Get PT packet mode */
605 return pty_get_pktmode(tty, (int __user *)arg);
1da177e4
LT
606 case TIOCGPTN: /* Get PT Number */
607 return put_user(tty->index, (unsigned int __user *)arg);
26df6d13 608 case TIOCSIG: /* Send signal to other side of pty */
609 return pty_signal(tty, (int) arg);
1da177e4
LT
610 }
611
612 return -ENOIOCTLCMD;
613}
614
99f1fe18
AC
615/**
616 * ptm_unix98_lookup - find a pty master
617 * @driver: ptm driver
618 * @idx: tty index
619 *
620 * Look up a pty master device. Called under the tty_mutex for now.
621 * This provides our locking.
622 */
623
15f1a633
SB
624static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
625 struct inode *ptm_inode, int idx)
99f1fe18 626{
593a27c4
KK
627 /* Master must be open via /dev/ptmx */
628 return ERR_PTR(-EIO);
99f1fe18
AC
629}
630
631/**
632 * pts_unix98_lookup - find a pty slave
633 * @driver: pts driver
634 * @idx: tty index
635 *
636 * Look up a pty master device. Called under the tty_mutex for now.
d739e65b 637 * This provides our locking for the tty pointer.
99f1fe18
AC
638 */
639
15f1a633
SB
640static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
641 struct inode *pts_inode, int idx)
99f1fe18 642{
d739e65b
AC
643 struct tty_struct *tty;
644
645 mutex_lock(&devpts_mutex);
8fcbaa2b 646 tty = devpts_get_priv(pts_inode);
d739e65b 647 mutex_unlock(&devpts_mutex);
99f1fe18
AC
648 /* Master must be open before slave */
649 if (!tty)
650 return ERR_PTR(-EIO);
651 return tty;
652}
653
8b0a88d5
AC
654/* We have no need to install and remove our tty objects as devpts does all
655 the work for us */
656
bf970ee4 657static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
8b0a88d5 658{
5d249bc6 659 return pty_common_install(driver, tty, false);
8b0a88d5
AC
660}
661
7171604a 662static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
8b0a88d5
AC
663{
664}
665
f4b208eb
JS
666/* this is called once with whichever end is closed last */
667static void pty_unix98_shutdown(struct tty_struct *tty)
668{
669 devpts_kill_index(tty->driver_data, tty->index);
670}
671
feebed65 672static const struct tty_operations ptm_unix98_ops = {
99f1fe18 673 .lookup = ptm_unix98_lookup,
bf970ee4 674 .install = pty_unix98_install,
7171604a 675 .remove = pty_unix98_remove,
3e8e88ca
AC
676 .open = pty_open,
677 .close = pty_close,
678 .write = pty_write,
679 .write_room = pty_write_room,
680 .flush_buffer = pty_flush_buffer,
681 .chars_in_buffer = pty_chars_in_buffer,
682 .unthrottle = pty_unthrottle,
feebed65 683 .ioctl = pty_unix98_ioctl,
36b3c070 684 .resize = pty_resize,
fa2ecfc5 685 .shutdown = pty_unix98_shutdown,
36b3c070 686 .cleanup = pty_cleanup
3e8e88ca
AC
687};
688
99f1fe18
AC
689static const struct tty_operations pty_unix98_ops = {
690 .lookup = pts_unix98_lookup,
bf970ee4 691 .install = pty_unix98_install,
7171604a 692 .remove = pty_unix98_remove,
99f1fe18
AC
693 .open = pty_open,
694 .close = pty_close,
695 .write = pty_write,
696 .write_room = pty_write_room,
697 .flush_buffer = pty_flush_buffer,
698 .chars_in_buffer = pty_chars_in_buffer,
699 .unthrottle = pty_unthrottle,
700 .set_termios = pty_set_termios,
01adc807
PH
701 .start = pty_start,
702 .stop = pty_stop,
fa2ecfc5 703 .shutdown = pty_unix98_shutdown,
d03702a2 704 .cleanup = pty_cleanup,
99f1fe18 705};
d81ed103
AC
706
707/**
708 * ptmx_open - open a unix 98 pty master
709 * @inode: inode of device file
710 * @filp: file pointer to tty
711 *
712 * Allocate a unix98 pty master device from the ptmx driver.
713 *
714 * Locking: tty_mutex protects the init_dev work. tty->count should
b9f8033f 715 * protect the rest.
d81ed103
AC
716 * allocated_ptys_lock handles the list of free pty numbers
717 */
718
64ba3dc3 719static int ptmx_open(struct inode *inode, struct file *filp)
d81ed103
AC
720{
721 struct tty_struct *tty;
162b97cf 722 struct inode *slave_inode;
d81ed103
AC
723 int retval;
724 int index;
725
726 nonseekable_open(inode, filp);
727
b0b88565
LT
728 /* We refuse fsnotify events on ptmx, since it's a shared resource */
729 filp->f_mode |= FMODE_NONOTIFY;
730
fa90e1c9
JS
731 retval = tty_alloc_file(filp);
732 if (retval)
733 return retval;
734
d81ed103 735 /* find a device that is not in use. */
89c8d91e 736 mutex_lock(&devpts_mutex);
15f1a633 737 index = devpts_new_index(inode);
fa90e1c9
JS
738 if (index < 0) {
739 retval = index;
05fb79e4 740 mutex_unlock(&devpts_mutex);
fa90e1c9
JS
741 goto err_file;
742 }
d81ed103 743
89c8d91e
AC
744 mutex_unlock(&devpts_mutex);
745
d81ed103 746 mutex_lock(&tty_mutex);
593a27c4 747 tty = tty_init_dev(ptm_driver, index);
d81ed103 748
73ec06fc
AC
749 if (IS_ERR(tty)) {
750 retval = PTR_ERR(tty);
d81ed103 751 goto out;
73ec06fc 752 }
d81ed103 753
89c8d91e
AC
754 /* The tty returned here is locked so we can safely
755 drop the mutex */
756 mutex_unlock(&tty_mutex);
757
d81ed103 758 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
7acf6cd8 759 tty->driver_data = inode;
ee2ffa0d 760
fa90e1c9 761 tty_add_file(tty, filp);
d81ed103 762
f11afb61
JS
763 slave_inode = devpts_pty_new(inode,
764 MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
765 tty->link);
162b97cf
JS
766 if (IS_ERR(slave_inode)) {
767 retval = PTR_ERR(slave_inode);
1177c0ef 768 goto err_release;
162b97cf 769 }
7acf6cd8 770 tty->link->driver_data = slave_inode;
d81ed103
AC
771
772 retval = ptm_driver->ops->open(tty, filp);
64ba3dc3 773 if (retval)
1177c0ef
JS
774 goto err_release;
775
89c8d91e 776 tty_unlock(tty);
1177c0ef
JS
777 return 0;
778err_release:
89c8d91e 779 tty_unlock(tty);
eeb89d91 780 tty_release(inode, filp);
d81ed103
AC
781 return retval;
782out:
89c8d91e 783 mutex_unlock(&tty_mutex);
d3bda529 784 devpts_kill_index(inode, index);
fa90e1c9
JS
785err_file:
786 tty_free_file(filp);
64ba3dc3 787 return retval;
d81ed103
AC
788}
789
790static struct file_operations ptmx_fops;
791
1da177e4
LT
792static void __init unix98_pty_init(void)
793{
21aca2fa
JS
794 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
795 TTY_DRIVER_RESET_TERMIOS |
796 TTY_DRIVER_REAL_RAW |
797 TTY_DRIVER_DYNAMIC_DEV |
798 TTY_DRIVER_DEVPTS_MEM |
799 TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344a 800 if (IS_ERR(ptm_driver))
1da177e4 801 panic("Couldn't allocate Unix98 ptm driver");
21aca2fa
JS
802 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
803 TTY_DRIVER_RESET_TERMIOS |
804 TTY_DRIVER_REAL_RAW |
805 TTY_DRIVER_DYNAMIC_DEV |
806 TTY_DRIVER_DEVPTS_MEM |
807 TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344a 808 if (IS_ERR(pts_driver))
1da177e4
LT
809 panic("Couldn't allocate Unix98 pts driver");
810
1da177e4
LT
811 ptm_driver->driver_name = "pty_master";
812 ptm_driver->name = "ptm";
813 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
814 ptm_driver->minor_start = 0;
815 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
816 ptm_driver->subtype = PTY_TYPE_MASTER;
817 ptm_driver->init_termios = tty_std_termios;
818 ptm_driver->init_termios.c_iflag = 0;
819 ptm_driver->init_termios.c_oflag = 0;
820 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
821 ptm_driver->init_termios.c_lflag = 0;
606d099c
AC
822 ptm_driver->init_termios.c_ispeed = 38400;
823 ptm_driver->init_termios.c_ospeed = 38400;
1da177e4 824 ptm_driver->other = pts_driver;
feebed65 825 tty_set_operations(ptm_driver, &ptm_unix98_ops);
1da177e4 826
1da177e4
LT
827 pts_driver->driver_name = "pty_slave";
828 pts_driver->name = "pts";
829 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
830 pts_driver->minor_start = 0;
831 pts_driver->type = TTY_DRIVER_TYPE_PTY;
832 pts_driver->subtype = PTY_TYPE_SLAVE;
833 pts_driver->init_termios = tty_std_termios;
834 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099c
AC
835 pts_driver->init_termios.c_ispeed = 38400;
836 pts_driver->init_termios.c_ospeed = 38400;
1da177e4 837 pts_driver->other = ptm_driver;
99f1fe18 838 tty_set_operations(pts_driver, &pty_unix98_ops);
fe9cd962 839
1da177e4
LT
840 if (tty_register_driver(ptm_driver))
841 panic("Couldn't register Unix98 ptm driver");
842 if (tty_register_driver(pts_driver))
843 panic("Couldn't register Unix98 pts driver");
844
d81ed103
AC
845 /* Now create the /dev/ptmx special device */
846 tty_default_fops(&ptmx_fops);
847 ptmx_fops.open = ptmx_open;
848
849 cdev_init(&ptmx_cdev, &ptmx_fops);
850 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
851 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
82f8c35f 852 panic("Couldn't register /dev/ptmx driver");
d81ed103 853 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
1da177e4 854}
d81ed103 855
1da177e4
LT
856#else
857static inline void unix98_pty_init(void) { }
858#endif
859
860static int __init pty_init(void)
861{
862 legacy_pty_init();
863 unix98_pty_init();
864 return 0;
865}
866module_init(pty_init);