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