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
8 *  Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to
9 *      waiting writers -- Sapan Bhatia <sapan@corewars.org>
10 *
11 *
12 */
13
14#include <linux/config.h>
15#include <linux/module.h>	/* For EXPORT_SYMBOL */
16
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>
25#include <linux/mm.h>
26#include <linux/init.h>
27#include <linux/devfs_fs_kernel.h>
28
29#include <asm/uaccess.h>
30#include <asm/system.h>
31#include <asm/bitops.h>
32
33#define BUILDING_PTY_C 1
34#include <linux/devpts_fs.h>
35
36struct pty_struct {
37	int	magic;
38	wait_queue_head_t open_wait;
39};
40
41#define PTY_MAGIC 0x5001
42
43static struct tty_driver pty_driver, pty_slave_driver;
44static int pty_refcount;
45
46/* Note: one set of tables for BSD and one for Unix98 */
47static struct tty_struct *pty_table[NR_PTYS];
48static struct termios *pty_termios[NR_PTYS];
49static struct termios *pty_termios_locked[NR_PTYS];
50static struct tty_struct *ttyp_table[NR_PTYS];
51static struct termios *ttyp_termios[NR_PTYS];
52static struct termios *ttyp_termios_locked[NR_PTYS];
53static struct pty_struct pty_state[NR_PTYS];
54
55#ifdef CONFIG_UNIX98_PTYS
56/* These are global because they are accessed in tty_io.c */
57struct tty_driver ptm_driver[UNIX98_NR_MAJORS];
58struct tty_driver pts_driver[UNIX98_NR_MAJORS];
59
60static struct tty_struct *ptm_table[UNIX98_NR_MAJORS][NR_PTYS];
61static struct termios *ptm_termios[UNIX98_NR_MAJORS][NR_PTYS];
62static struct termios *ptm_termios_locked[UNIX98_NR_MAJORS][NR_PTYS];
63static struct tty_struct *pts_table[UNIX98_NR_MAJORS][NR_PTYS];
64static struct termios *pts_termios[UNIX98_NR_MAJORS][NR_PTYS];
65static struct termios *pts_termios_locked[UNIX98_NR_MAJORS][NR_PTYS];
66static struct pty_struct ptm_state[UNIX98_NR_MAJORS][NR_PTYS];
67#endif
68
69#define MIN(a,b)	((a) < (b) ? (a) : (b))
70
71static void pty_close(struct tty_struct * tty, struct file * filp)
72{
73	if (!tty)
74		return;
75	if (tty->driver.subtype == PTY_TYPE_MASTER) {
76		if (tty->count > 1)
77			printk("master pty_close: count = %d!!\n", tty->count);
78	} else {
79		if (tty->count > 2)
80			return;
81	}
82	wake_up_interruptible(&tty->read_wait);
83	wake_up_interruptible(&tty->write_wait);
84	tty->packet = 0;
85	if (!tty->link)
86		return;
87	tty->link->packet = 0;
88	wake_up_interruptible(&tty->link->read_wait);
89	wake_up_interruptible(&tty->link->write_wait);
90	set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
91	if (tty->driver.subtype == PTY_TYPE_MASTER) {
92		set_bit(TTY_OTHER_CLOSED, &tty->flags);
93#ifdef CONFIG_UNIX98_PTYS
94		{
95			unsigned int major = MAJOR(tty->device) - UNIX98_PTY_MASTER_MAJOR;
96			if ( major < UNIX98_NR_MAJORS ) {
97				devpts_pty_kill( MINOR(tty->device)
98			  - tty->driver.minor_start + tty->driver.name_base );
99			}
100		}
101#endif
102		tty_unregister_devfs (&tty->link->driver, MINOR (tty->device));
103		tty_vhangup(tty->link);
104	}
105}
106
107/*
108 * The unthrottle routine is called by the line discipline to signal
109 * that it can receive more characters.  For PTY's, the TTY_THROTTLED
110 * flag is always set, to force the line discipline to always call the
111 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
112 * characters in the queue.  This is necessary since each time this
113 * happens, we need to wake up any sleeping processes that could be
114 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
115 * for the pty buffer to be drained.
116 */
117static void pty_unthrottle(struct tty_struct * tty)
118{
119	struct tty_struct *o_tty = tty->link;
120
121	if (!o_tty)
122		return;
123
124	if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
125	    o_tty->ldisc.write_wakeup)
126		(o_tty->ldisc.write_wakeup)(o_tty);
127	wake_up_interruptible(&o_tty->write_wait);
128	set_bit(TTY_THROTTLED, &tty->flags);
129}
130
131/*
132 * WSH 05/24/97: modified to
133 *   (1) use space in tty->flip instead of a shared temp buffer
134 *	 The flip buffers aren't being used for a pty, so there's lots
135 *	 of space available.  The buffer is protected by a per-pty
136 *	 semaphore that should almost never come under contention.
137 *   (2) avoid redundant copying for cases where count >> receive_room
138 * N.B. Calls from user space may now return an error code instead of
139 * a count.
140 */
141static int pty_write(struct tty_struct * tty, int from_user,
142		       const unsigned char *buf, int count)
143{
144	struct tty_struct *to = tty->link;
145	int	c=0, n, room;
146	char	*temp_buffer;
147
148	if (!to || tty->stopped)
149		return 0;
150
151	if (from_user) {
152		down(&tty->flip.pty_sem);
153		temp_buffer = &tty->flip.char_buf[0];
154		while (count > 0) {
155			/* check space so we don't copy needlessly */
156			n = to->ldisc.receive_room(to);
157			if (n > count)
158				n = count;
159			if (!n) break;
160
161			n  = MIN(n, PTY_BUF_SIZE);
162			n -= copy_from_user(temp_buffer, buf, n);
163			if (!n) {
164				if (!c)
165					c = -EFAULT;
166				break;
167			}
168
169			/* check again in case the buffer filled up */
170			room = to->ldisc.receive_room(to);
171			if (n > room)
172				n = room;
173			if (!n) break;
174			buf   += n;
175			c     += n;
176			count -= n;
177			to->ldisc.receive_buf(to, temp_buffer, 0, n);
178		}
179		up(&tty->flip.pty_sem);
180	} else {
181		c = to->ldisc.receive_room(to);
182		if (c > count)
183			c = count;
184		to->ldisc.receive_buf(to, buf, 0, c);
185	}
186
187	return c;
188}
189
190static int pty_write_room(struct tty_struct *tty)
191{
192	struct tty_struct *to = tty->link;
193
194	if (!to || tty->stopped)
195		return 0;
196
197	return to->ldisc.receive_room(to);
198}
199
200/*
201 *	WSH 05/24/97:  Modified for asymmetric MASTER/SLAVE behavior
202 *	The chars_in_buffer() value is used by the ldisc select() function
203 *	to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
204 *	The pty driver chars_in_buffer() Master/Slave must behave differently:
205 *
206 *      The Master side needs to allow typed-ahead commands to accumulate
207 *      while being canonicalized, so we report "our buffer" as empty until
208 *	some threshold is reached, and then report the count. (Any count >
209 *	WAKEUP_CHARS is regarded by select() as "full".)  To avoid deadlock
210 *	the count returned must be 0 if no canonical data is available to be
211 *	read. (The N_TTY ldisc.chars_in_buffer now knows this.)
212 *
213 *	The Slave side passes all characters in raw mode to the Master side's
214 *	buffer where they can be read immediately, so in this case we can
215 *	return the true count in the buffer.
216 */
217static int pty_chars_in_buffer(struct tty_struct *tty)
218{
219	struct tty_struct *to = tty->link;
220	int count;
221
222	if (!to || !to->ldisc.chars_in_buffer)
223		return 0;
224
225	/* The ldisc must report 0 if no characters available to be read */
226	count = to->ldisc.chars_in_buffer(to);
227
228	if (tty->driver.subtype == PTY_TYPE_SLAVE) return count;
229
230	/* Master side driver ... if the other side's read buffer is less than
231	 * half full, return 0 to allow writers to proceed; otherwise return
232	 * the count.  This leaves a comfortable margin to avoid overflow,
233	 * and still allows half a buffer's worth of typed-ahead commands.
234	 */
235	return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
236}
237
238/*
239 * Return the device number of a Unix98 PTY (only!).  This lets us open a
240 * master pty with the multi-headed ptmx device, then find out which
241 * one we got after it is open, with an ioctl.
242 */
243#ifdef CONFIG_UNIX98_PTYS
244static int pty_get_device_number(struct tty_struct *tty, unsigned int *value)
245{
246	unsigned int result = MINOR(tty->device)
247		- tty->driver.minor_start + tty->driver.name_base;
248	return put_user(result, value);
249}
250#endif
251
252/* Set the lock flag on a pty */
253static int pty_set_lock(struct tty_struct *tty, int * arg)
254{
255	int val;
256	if (get_user(val,arg))
257		return -EFAULT;
258	if (val)
259		set_bit(TTY_PTY_LOCK, &tty->flags);
260	else
261		clear_bit(TTY_PTY_LOCK, &tty->flags);
262	return 0;
263}
264
265static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
266			unsigned int cmd, unsigned long arg)
267{
268	if (!tty) {
269		printk("pty_ioctl called with NULL tty!\n");
270		return -EIO;
271	}
272	switch(cmd) {
273	case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
274		return pty_set_lock(tty, (int *) arg);
275	}
276	return -ENOIOCTLCMD;
277}
278
279#ifdef CONFIG_UNIX98_PTYS
280static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
281			    unsigned int cmd, unsigned long arg)
282{
283	if (!tty) {
284		printk("pty_unix98_ioctl called with NULL tty!\n");
285		return -EIO;
286	}
287	switch(cmd) {
288	case TIOCGPTN: /* Get PT Number */
289		return pty_get_device_number(tty, (unsigned int *)arg);
290	}
291
292	return pty_bsd_ioctl(tty,file,cmd,arg);
293}
294#endif
295
296static void pty_flush_buffer(struct tty_struct *tty)
297{
298	struct tty_struct *to = tty->link;
299
300	if (!to)
301		return;
302
303	if (to->ldisc.flush_buffer)
304		to->ldisc.flush_buffer(to);
305
306	if (to->packet) {
307		tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
308		wake_up_interruptible(&to->read_wait);
309	}
310}
311
312static int pty_open(struct tty_struct *tty, struct file * filp)
313{
314	int	retval;
315	int	line;
316	struct	pty_struct *pty;
317
318	retval = -ENODEV;
319	if (!tty || !tty->link)
320		goto out;
321	line = MINOR(tty->device) - tty->driver.minor_start;
322	if ((line < 0) || (line >= NR_PTYS))
323		goto out;
324	pty = (struct pty_struct *)(tty->driver.driver_state) + line;
325	tty->driver_data = pty;
326
327	retval = -EIO;
328	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
329		goto out;
330	if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
331		goto out;
332	if (tty->link->count != 1)
333		goto out;
334
335	clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
336	wake_up_interruptible(&pty->open_wait);
337	set_bit(TTY_THROTTLED, &tty->flags);
338	set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
339
340	/*  Register a slave for the master  */
341	if (tty->driver.major == PTY_MASTER_MAJOR)
342		tty_register_devfs(&tty->link->driver,
343				   DEVFS_FL_CURRENT_OWNER | DEVFS_FL_WAIT,
344				   tty->link->driver.minor_start +
345				   MINOR(tty->device)-tty->driver.minor_start);
346	retval = 0;
347out:
348	return retval;
349}
350
351static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
352{
353        tty->termios->c_cflag &= ~(CSIZE | PARENB);
354        tty->termios->c_cflag |= (CS8 | CREAD);
355}
356
357int __init pty_init(void)
358{
359	int i;
360
361	/* Traditional BSD devices */
362
363	memset(&pty_state, 0, sizeof(pty_state));
364	for (i = 0; i < NR_PTYS; i++)
365		init_waitqueue_head(&pty_state[i].open_wait);
366	memset(&pty_driver, 0, sizeof(struct tty_driver));
367	pty_driver.magic = TTY_DRIVER_MAGIC;
368	pty_driver.driver_name = "pty_master";
369#ifdef CONFIG_DEVFS_FS
370	pty_driver.name = "pty/m%d";
371#else
372	pty_driver.name = "pty";
373#endif
374	pty_driver.major = PTY_MASTER_MAJOR;
375	pty_driver.minor_start = 0;
376	pty_driver.num = NR_PTYS;
377	pty_driver.type = TTY_DRIVER_TYPE_PTY;
378	pty_driver.subtype = PTY_TYPE_MASTER;
379	pty_driver.init_termios = tty_std_termios;
380	pty_driver.init_termios.c_iflag = 0;
381	pty_driver.init_termios.c_oflag = 0;
382	pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
383	pty_driver.init_termios.c_lflag = 0;
384	pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
385	pty_driver.refcount = &pty_refcount;
386	pty_driver.table = pty_table;
387	pty_driver.termios = pty_termios;
388	pty_driver.termios_locked = pty_termios_locked;
389	pty_driver.driver_state = pty_state;
390	pty_driver.other = &pty_slave_driver;
391
392	pty_driver.open = pty_open;
393	pty_driver.close = pty_close;
394	pty_driver.write = pty_write;
395	pty_driver.write_room = pty_write_room;
396	pty_driver.flush_buffer = pty_flush_buffer;
397	pty_driver.chars_in_buffer = pty_chars_in_buffer;
398	pty_driver.unthrottle = pty_unthrottle;
399	pty_driver.set_termios = pty_set_termios;
400
401	pty_slave_driver = pty_driver;
402	pty_slave_driver.driver_name = "pty_slave";
403	pty_slave_driver.proc_entry = 0;
404#ifdef CONFIG_DEVFS_FS
405	pty_slave_driver.name = "pty/s%d";
406#else
407	pty_slave_driver.name = "ttyp";
408#endif
409	pty_slave_driver.subtype = PTY_TYPE_SLAVE;
410	pty_slave_driver.major = PTY_SLAVE_MAJOR;
411	pty_slave_driver.minor_start = 0;
412	pty_slave_driver.init_termios = tty_std_termios;
413	pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
414	/* Slave ptys are registered when their corresponding master pty
415	 * is opened, and unregistered when the pair is closed.
416	 */
417	pty_slave_driver.flags |= TTY_DRIVER_NO_DEVFS;
418	pty_slave_driver.table = ttyp_table;
419	pty_slave_driver.termios = ttyp_termios;
420	pty_slave_driver.termios_locked = ttyp_termios_locked;
421	pty_slave_driver.driver_state = pty_state;
422	pty_slave_driver.other = &pty_driver;
423
424	if (tty_register_driver(&pty_driver))
425		panic("Couldn't register pty driver");
426	if (tty_register_driver(&pty_slave_driver))
427		panic("Couldn't register pty slave driver");
428
429	/*
430	 * only the master pty gets this ioctl (which is why we
431	 * assign it here, instead of up with the rest of the
432	 * pty_driver initialization. <cananian@alumni.princeton.edu>
433	 */
434	pty_driver.ioctl = pty_bsd_ioctl;
435
436	/* Unix98 devices */
437#ifdef CONFIG_UNIX98_PTYS
438	devfs_mk_dir (NULL, "pts", NULL);
439	printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS*NR_PTYS);
440	for ( i = 0 ; i < UNIX98_NR_MAJORS ; i++ ) {
441		int j;
442
443		ptm_driver[i] = pty_driver;
444		ptm_driver[i].name = "ptm";
445		ptm_driver[i].proc_entry = 0;
446		ptm_driver[i].major = UNIX98_PTY_MASTER_MAJOR+i;
447		ptm_driver[i].minor_start = 0;
448		ptm_driver[i].name_base = i*NR_PTYS;
449		ptm_driver[i].num = NR_PTYS;
450		ptm_driver[i].other = &pts_driver[i];
451		ptm_driver[i].flags |= TTY_DRIVER_NO_DEVFS;
452		ptm_driver[i].table = ptm_table[i];
453		ptm_driver[i].termios = ptm_termios[i];
454		ptm_driver[i].termios_locked = ptm_termios_locked[i];
455		ptm_driver[i].driver_state = ptm_state[i];
456
457		for (j = 0; j < NR_PTYS; j++)
458			init_waitqueue_head(&ptm_state[i][j].open_wait);
459
460		pts_driver[i] = pty_slave_driver;
461#ifdef CONFIG_DEVFS_FS
462		pts_driver[i].name = "pts/%d";
463#else
464		pts_driver[i].name = "pts";
465#endif
466		pts_driver[i].proc_entry = 0;
467		pts_driver[i].major = UNIX98_PTY_SLAVE_MAJOR+i;
468		pts_driver[i].minor_start = 0;
469		pts_driver[i].name_base = i*NR_PTYS;
470		pts_driver[i].num = ptm_driver[i].num;
471		pts_driver[i].other = &ptm_driver[i];
472		pts_driver[i].table = pts_table[i];
473		pts_driver[i].termios = pts_termios[i];
474		pts_driver[i].termios_locked = pts_termios_locked[i];
475		pts_driver[i].driver_state = ptm_state[i];
476
477		ptm_driver[i].ioctl = pty_unix98_ioctl;
478
479		if (tty_register_driver(&ptm_driver[i]))
480			panic("Couldn't register Unix98 ptm driver major %d",
481			      ptm_driver[i].major);
482		if (tty_register_driver(&pts_driver[i]))
483			panic("Couldn't register Unix98 pts driver major %d",
484			      pts_driver[i].major);
485	}
486#endif
487	return 0;
488}
489