2 * linux/drivers/char/pty.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Added support for a Unix98-style ptmx device.
7 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
8 * Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to
9 * waiting writers -- Sapan Bhatia <sapan@corewars.org>
14 #include <linux/config.h>
15 #include <linux/module.h> /* For EXPORT_SYMBOL */
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/interrupt.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/fcntl.h>
23 #include <linux/string.h>
24 #include <linux/major.h>
26 #include <linux/init.h>
27 #include <linux/devfs_fs_kernel.h>
28 #include <linux/sysctl.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/bitops.h>
33 #include <linux/devpts_fs.h>
35 #if defined(CONFIG_LEGACY_PTYS) || defined(CONFIG_UNIX98_PTYS)
37 #ifdef CONFIG_LEGACY_PTYS
38 static struct tty_driver *pty_driver, *pty_slave_driver;
41 /* These are global because they are accessed in tty_io.c */
42 #ifdef CONFIG_UNIX98_PTYS
43 struct tty_driver *ptm_driver;
44 struct tty_driver *pts_driver;
47 static void pty_close(struct tty_struct * tty, struct file * filp)
51 if (tty->driver->subtype == PTY_TYPE_MASTER) {
53 printk("master pty_close: count = %d!!\n", tty->count);
58 wake_up_interruptible(&tty->read_wait);
59 wake_up_interruptible(&tty->write_wait);
63 tty->link->packet = 0;
64 wake_up_interruptible(&tty->link->read_wait);
65 wake_up_interruptible(&tty->link->write_wait);
66 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
67 if (tty->driver->subtype == PTY_TYPE_MASTER) {
68 set_bit(TTY_OTHER_CLOSED, &tty->flags);
69 #ifdef CONFIG_UNIX98_PTYS
70 if (tty->driver == ptm_driver)
71 devpts_pty_kill(tty->index);
73 tty_vhangup(tty->link);
78 * The unthrottle routine is called by the line discipline to signal
79 * that it can receive more characters. For PTY's, the TTY_THROTTLED
80 * flag is always set, to force the line discipline to always call the
81 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
82 * characters in the queue. This is necessary since each time this
83 * happens, we need to wake up any sleeping processes that could be
84 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
85 * for the pty buffer to be drained.
87 static void pty_unthrottle(struct tty_struct * tty)
89 struct tty_struct *o_tty = tty->link;
94 if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
95 o_tty->ldisc.write_wakeup)
96 (o_tty->ldisc.write_wakeup)(o_tty);
97 wake_up_interruptible(&o_tty->write_wait);
98 set_bit(TTY_THROTTLED, &tty->flags);
102 * WSH 05/24/97: modified to
103 * (1) use space in tty->flip instead of a shared temp buffer
104 * The flip buffers aren't being used for a pty, so there's lots
105 * of space available. The buffer is protected by a per-pty
106 * semaphore that should almost never come under contention.
107 * (2) avoid redundant copying for cases where count >> receive_room
108 * N.B. Calls from user space may now return an error code instead of
111 static int pty_write(struct tty_struct * tty, int from_user,
112 const unsigned char *buf, int count)
114 struct tty_struct *to = tty->link;
118 if (!to || tty->stopped)
122 down(&tty->flip.pty_sem);
123 temp_buffer = &tty->flip.char_buf[0];
125 /* check space so we don't copy needlessly */
126 n = to->ldisc.receive_room(to);
131 n = min(n, PTY_BUF_SIZE);
132 n -= copy_from_user(temp_buffer, buf, n);
139 /* check again in case the buffer filled up */
140 room = to->ldisc.receive_room(to);
147 to->ldisc.receive_buf(to, temp_buffer, NULL, n);
149 up(&tty->flip.pty_sem);
151 c = to->ldisc.receive_room(to);
154 to->ldisc.receive_buf(to, buf, NULL, c);
160 static int pty_write_room(struct tty_struct *tty)
162 struct tty_struct *to = tty->link;
164 if (!to || tty->stopped)
167 return to->ldisc.receive_room(to);
171 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
172 * The chars_in_buffer() value is used by the ldisc select() function
173 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
174 * The pty driver chars_in_buffer() Master/Slave must behave differently:
176 * The Master side needs to allow typed-ahead commands to accumulate
177 * while being canonicalized, so we report "our buffer" as empty until
178 * some threshold is reached, and then report the count. (Any count >
179 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
180 * the count returned must be 0 if no canonical data is available to be
181 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
183 * The Slave side passes all characters in raw mode to the Master side's
184 * buffer where they can be read immediately, so in this case we can
185 * return the true count in the buffer.
187 static int pty_chars_in_buffer(struct tty_struct *tty)
189 struct tty_struct *to = tty->link;
192 if (!to || !to->ldisc.chars_in_buffer)
195 /* The ldisc must report 0 if no characters available to be read */
196 count = to->ldisc.chars_in_buffer(to);
198 if (tty->driver->subtype == PTY_TYPE_SLAVE) return count;
200 /* Master side driver ... if the other side's read buffer is less than
201 * half full, return 0 to allow writers to proceed; otherwise return
202 * the count. This leaves a comfortable margin to avoid overflow,
203 * and still allows half a buffer's worth of typed-ahead commands.
205 return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
209 * Return the device number of a Unix98 PTY (only!). This lets us open a
210 * master pty with the multi-headed ptmx device, then find out which
211 * one we got after it is open, with an ioctl.
213 #ifdef CONFIG_UNIX98_PTYS
214 static int pty_get_device_number(struct tty_struct *tty, unsigned __user *value)
216 unsigned int result = tty->index;
217 return put_user(result, value);
221 /* Set the lock flag on a pty */
222 static int pty_set_lock(struct tty_struct *tty, int __user * arg)
225 if (get_user(val,arg))
228 set_bit(TTY_PTY_LOCK, &tty->flags);
230 clear_bit(TTY_PTY_LOCK, &tty->flags);
234 #ifdef CONFIG_LEGACY_PTYS
235 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
236 unsigned int cmd, unsigned long arg)
239 printk("pty_ioctl called with NULL tty!\n");
243 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
244 return pty_set_lock(tty, (int __user *) arg);
250 #ifdef CONFIG_UNIX98_PTYS
251 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
252 unsigned int cmd, unsigned long arg)
255 printk("pty_unix98_ioctl called with NULL tty!\n");
259 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
260 return pty_set_lock(tty, (int __user *)arg);
261 case TIOCGPTN: /* Get PT Number */
262 return pty_get_device_number(tty, (unsigned int __user *)arg);
269 static void pty_flush_buffer(struct tty_struct *tty)
271 struct tty_struct *to = tty->link;
276 if (to->ldisc.flush_buffer)
277 to->ldisc.flush_buffer(to);
280 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
281 wake_up_interruptible(&to->read_wait);
285 static int pty_open(struct tty_struct *tty, struct file * filp)
287 int retval = -ENODEV;
289 if (!tty || !tty->link)
293 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
295 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
297 if (tty->link->count != 1)
300 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
301 set_bit(TTY_THROTTLED, &tty->flags);
302 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
308 static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
310 tty->termios->c_cflag &= ~(CSIZE | PARENB);
311 tty->termios->c_cflag |= (CS8 | CREAD);
314 static struct tty_operations pty_ops = {
318 .write_room = pty_write_room,
319 .flush_buffer = pty_flush_buffer,
320 .chars_in_buffer = pty_chars_in_buffer,
321 .unthrottle = pty_unthrottle,
322 .set_termios = pty_set_termios,
325 /* sysctl support for setting limits on the number of Unix98 ptys allocated.
326 Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. */
327 #ifdef CONFIG_UNIX98_PTYS
328 int pty_limit = NR_UNIX98_PTY_DEFAULT;
329 static int pty_limit_min = 0;
330 static int pty_limit_max = NR_UNIX98_PTY_MAX;
332 ctl_table pty_table[] = {
336 .maxlen = sizeof(int),
339 .proc_handler = &proc_dointvec_minmax,
340 .strategy = &sysctl_intvec,
341 .extra1 = &pty_limit_min,
342 .extra2 = &pty_limit_max,
346 .maxlen = sizeof(int),
348 .proc_handler = &proc_dointvec,
357 static int __init pty_init(void)
359 #ifdef CONFIG_LEGACY_PTYS
360 /* Traditional BSD devices */
362 pty_driver = alloc_tty_driver(NR_PTYS);
364 panic("Couldn't allocate pty driver");
366 pty_slave_driver = alloc_tty_driver(NR_PTYS);
367 if (!pty_slave_driver)
368 panic("Couldn't allocate pty slave driver");
370 pty_driver->owner = THIS_MODULE;
371 pty_driver->driver_name = "pty_master";
372 pty_driver->name = "pty";
373 pty_driver->devfs_name = "pty/m";
374 pty_driver->major = PTY_MASTER_MAJOR;
375 pty_driver->minor_start = 0;
376 pty_driver->type = TTY_DRIVER_TYPE_PTY;
377 pty_driver->subtype = PTY_TYPE_MASTER;
378 pty_driver->init_termios = tty_std_termios;
379 pty_driver->init_termios.c_iflag = 0;
380 pty_driver->init_termios.c_oflag = 0;
381 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
382 pty_driver->init_termios.c_lflag = 0;
383 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
384 pty_driver->other = pty_slave_driver;
385 tty_set_operations(pty_driver, &pty_ops);
386 pty_driver->ioctl = pty_bsd_ioctl;
388 pty_slave_driver->owner = THIS_MODULE;
389 pty_slave_driver->driver_name = "pty_slave";
390 pty_slave_driver->name = "ttyp";
391 pty_slave_driver->devfs_name = "pty/s";
392 pty_slave_driver->major = PTY_SLAVE_MAJOR;
393 pty_slave_driver->minor_start = 0;
394 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
395 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
396 pty_slave_driver->init_termios = tty_std_termios;
397 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
398 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
400 pty_slave_driver->other = pty_driver;
401 tty_set_operations(pty_slave_driver, &pty_ops);
403 if (tty_register_driver(pty_driver))
404 panic("Couldn't register pty driver");
405 if (tty_register_driver(pty_slave_driver))
406 panic("Couldn't register pty slave driver");
408 #endif /* CONFIG_LEGACY_PTYS */
410 #ifdef CONFIG_UNIX98_PTYS
413 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
415 panic("Couldn't allocate Unix98 ptm driver");
416 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
418 panic("Couldn't allocate Unix98 pts driver");
420 ptm_driver->owner = THIS_MODULE;
421 ptm_driver->driver_name = "pty_master";
422 ptm_driver->name = "ptm";
423 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
424 ptm_driver->minor_start = 0;
425 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
426 ptm_driver->subtype = PTY_TYPE_MASTER;
427 ptm_driver->init_termios = tty_std_termios;
428 ptm_driver->init_termios.c_iflag = 0;
429 ptm_driver->init_termios.c_oflag = 0;
430 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
431 ptm_driver->init_termios.c_lflag = 0;
432 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
433 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
434 ptm_driver->other = pts_driver;
435 tty_set_operations(ptm_driver, &pty_ops);
436 ptm_driver->ioctl = pty_unix98_ioctl;
438 pts_driver->owner = THIS_MODULE;
439 pts_driver->driver_name = "pty_slave";
440 pts_driver->name = "pts";
441 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
442 pts_driver->minor_start = 0;
443 pts_driver->type = TTY_DRIVER_TYPE_PTY;
444 pts_driver->subtype = PTY_TYPE_SLAVE;
445 pts_driver->init_termios = tty_std_termios;
446 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
447 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
448 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
449 pts_driver->other = ptm_driver;
450 tty_set_operations(pts_driver, &pty_ops);
452 if (tty_register_driver(ptm_driver))
453 panic("Couldn't register Unix98 ptm driver");
454 if (tty_register_driver(pts_driver))
455 panic("Couldn't register Unix98 pts driver");
457 pty_table[1].data = &ptm_driver->refcount;
458 #endif /* CONFIG_UNIX98_PTYS */
462 module_init(pty_init);
464 #endif /* CONFIG_LEGACY_PTYS || CONFIG_UNIX98_PTYS */