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
10 #include <linux/config.h>
11 #include <linux/module.h> /* For EXPORT_SYMBOL */
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/fcntl.h>
19 #include <linux/string.h>
20 #include <linux/major.h>
22 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
26 #include <asm/bitops.h>
28 #define BUILDING_PTY_C 1
29 #include <linux/devpts_fs.h>
33 wait_queue_head_t open_wait
;
36 #define PTY_MAGIC 0x5001
38 static struct tty_driver pty_driver
, pty_slave_driver
;
39 static int pty_refcount
;
41 /* Note: one set of tables for BSD and one for Unix98 */
42 static struct tty_struct
*pty_table
[NR_PTYS
];
43 static struct termios
*pty_termios
[NR_PTYS
];
44 static struct termios
*pty_termios_locked
[NR_PTYS
];
45 static struct tty_struct
*ttyp_table
[NR_PTYS
];
46 static struct termios
*ttyp_termios
[NR_PTYS
];
47 static struct termios
*ttyp_termios_locked
[NR_PTYS
];
48 static struct pty_struct pty_state
[NR_PTYS
];
50 #ifdef CONFIG_UNIX98_PTYS
51 /* These are global because they are accessed in tty_io.c */
52 struct tty_driver ptm_driver
[UNIX98_NR_MAJORS
];
53 struct tty_driver pts_driver
[UNIX98_NR_MAJORS
];
55 static struct tty_struct
*ptm_table
[UNIX98_NR_MAJORS
][NR_PTYS
];
56 static struct termios
*ptm_termios
[UNIX98_NR_MAJORS
][NR_PTYS
];
57 static struct termios
*ptm_termios_locked
[UNIX98_NR_MAJORS
][NR_PTYS
];
58 static struct tty_struct
*pts_table
[UNIX98_NR_MAJORS
][NR_PTYS
];
59 static struct termios
*pts_termios
[UNIX98_NR_MAJORS
][NR_PTYS
];
60 static struct termios
*pts_termios_locked
[UNIX98_NR_MAJORS
][NR_PTYS
];
61 static struct pty_struct ptm_state
[UNIX98_NR_MAJORS
][NR_PTYS
];
64 #define MIN(a,b) ((a) < (b) ? (a) : (b))
66 static void pty_close(struct tty_struct
* tty
, struct file
* filp
)
70 if (tty
->driver
.subtype
== PTY_TYPE_MASTER
) {
72 printk("master pty_close: count = %d!!\n", tty
->count
);
77 wake_up_interruptible(&tty
->read_wait
);
78 wake_up_interruptible(&tty
->write_wait
);
82 tty
->link
->packet
= 0;
83 wake_up_interruptible(&tty
->link
->read_wait
);
84 wake_up_interruptible(&tty
->link
->write_wait
);
85 set_bit(TTY_OTHER_CLOSED
, &tty
->link
->flags
);
86 if (tty
->driver
.subtype
== PTY_TYPE_MASTER
) {
87 set_bit(TTY_OTHER_CLOSED
, &tty
->flags
);
88 #ifdef CONFIG_UNIX98_PTYS
90 unsigned int major
= MAJOR(tty
->device
) - UNIX98_PTY_MASTER_MAJOR
;
91 if ( major
< UNIX98_NR_MAJORS
) {
92 devpts_pty_kill( MINOR(tty
->device
)
93 - tty
->driver
.minor_start
+ tty
->driver
.name_base
);
97 tty_vhangup(tty
->link
);
102 * The unthrottle routine is called by the line discipline to signal
103 * that it can receive more characters. For PTY's, the TTY_THROTTLED
104 * flag is always set, to force the line discipline to always call the
105 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
106 * characters in the queue. This is necessary since each time this
107 * happens, we need to wake up any sleeping processes that could be
108 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
109 * for the pty buffer to be drained.
111 static void pty_unthrottle(struct tty_struct
* tty
)
113 struct tty_struct
*o_tty
= tty
->link
;
118 if ((o_tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
119 o_tty
->ldisc
.write_wakeup
)
120 (o_tty
->ldisc
.write_wakeup
)(o_tty
);
121 wake_up_interruptible(&o_tty
->write_wait
);
122 set_bit(TTY_THROTTLED
, &tty
->flags
);
126 * WSH 05/24/97: modified to
127 * (1) use space in tty->flip instead of a shared temp buffer
128 * The flip buffers aren't being used for a pty, so there's lots
129 * of space available. The buffer is protected by a per-pty
130 * semaphore that should almost never come under contention.
131 * (2) avoid redundant copying for cases where count >> receive_room
132 * N.B. Calls from user space may now return an error code instead of
135 static int pty_write(struct tty_struct
* tty
, int from_user
,
136 const unsigned char *buf
, int count
)
138 struct tty_struct
*to
= tty
->link
;
142 if (!to
|| tty
->stopped
)
146 down(&tty
->flip
.pty_sem
);
147 temp_buffer
= &tty
->flip
.char_buf
[0];
149 /* check space so we don't copy needlessly */
150 n
= MIN(count
, to
->ldisc
.receive_room(to
));
153 n
= MIN(n
, PTY_BUF_SIZE
);
154 n
-= copy_from_user(temp_buffer
, buf
, n
);
161 /* check again in case the buffer filled up */
162 n
= MIN(n
, to
->ldisc
.receive_room(to
));
167 to
->ldisc
.receive_buf(to
, temp_buffer
, 0, n
);
169 up(&tty
->flip
.pty_sem
);
171 c
= MIN(count
, to
->ldisc
.receive_room(to
));
172 to
->ldisc
.receive_buf(to
, buf
, 0, c
);
178 static int pty_write_room(struct tty_struct
*tty
)
180 struct tty_struct
*to
= tty
->link
;
182 if (!to
|| tty
->stopped
)
185 return to
->ldisc
.receive_room(to
);
189 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
190 * The chars_in_buffer() value is used by the ldisc select() function
191 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
192 * The pty driver chars_in_buffer() Master/Slave must behave differently:
194 * The Master side needs to allow typed-ahead commands to accumulate
195 * while being canonicalized, so we report "our buffer" as empty until
196 * some threshold is reached, and then report the count. (Any count >
197 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
198 * the count returned must be 0 if no canonical data is available to be
199 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
201 * The Slave side passes all characters in raw mode to the Master side's
202 * buffer where they can be read immediately, so in this case we can
203 * return the true count in the buffer.
205 static int pty_chars_in_buffer(struct tty_struct
*tty
)
207 struct tty_struct
*to
= tty
->link
;
210 if (!to
|| !to
->ldisc
.chars_in_buffer
)
213 /* The ldisc must report 0 if no characters available to be read */
214 count
= to
->ldisc
.chars_in_buffer(to
);
216 if (tty
->driver
.subtype
== PTY_TYPE_SLAVE
) return count
;
218 /* Master side driver ... if the other side's read buffer is less than
219 * half full, return 0 to allow writers to proceed; otherwise return
220 * the count. This leaves a comfortable margin to avoid overflow,
221 * and still allows half a buffer's worth of typed-ahead commands.
223 return ((count
< N_TTY_BUF_SIZE
/2) ? 0 : count
);
227 * Return the device number of a Unix98 PTY (only!). This lets us open a
228 * master pty with the multi-headed ptmx device, then find out which
229 * one we got after it is open, with an ioctl.
231 #ifdef CONFIG_UNIX98_PTYS
232 static int pty_get_device_number(struct tty_struct
*tty
, unsigned int *value
)
234 unsigned int result
= MINOR(tty
->device
)
235 - tty
->driver
.minor_start
+ tty
->driver
.name_base
;
236 return put_user(result
, value
);
240 /* Set the lock flag on a pty */
241 static int pty_set_lock(struct tty_struct
*tty
, int * arg
)
244 if (get_user(val
,arg
))
247 set_bit(TTY_PTY_LOCK
, &tty
->flags
);
249 clear_bit(TTY_PTY_LOCK
, &tty
->flags
);
253 static int pty_bsd_ioctl(struct tty_struct
*tty
, struct file
*file
,
254 unsigned int cmd
, unsigned long arg
)
257 printk("pty_ioctl called with NULL tty!\n");
261 case TIOCSPTLCK
: /* Set PT Lock (disallow slave open) */
262 return pty_set_lock(tty
, (int *) arg
);
267 #ifdef CONFIG_UNIX98_PTYS
268 static int pty_unix98_ioctl(struct tty_struct
*tty
, struct file
*file
,
269 unsigned int cmd
, unsigned long arg
)
272 printk("pty_unix98_ioctl called with NULL tty!\n");
276 case TIOCGPTN
: /* Get PT Number */
277 return pty_get_device_number(tty
, (unsigned int *)arg
);
280 return pty_bsd_ioctl(tty
,file
,cmd
,arg
);
284 static void pty_flush_buffer(struct tty_struct
*tty
)
286 struct tty_struct
*to
= tty
->link
;
291 if (to
->ldisc
.flush_buffer
)
292 to
->ldisc
.flush_buffer(to
);
295 tty
->ctrl_status
|= TIOCPKT_FLUSHWRITE
;
296 wake_up_interruptible(&to
->read_wait
);
300 static int pty_open(struct tty_struct
*tty
, struct file
* filp
)
304 struct pty_struct
*pty
;
307 if (!tty
|| !tty
->link
)
309 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
310 if ((line
< 0) || (line
>= NR_PTYS
))
312 pty
= (struct pty_struct
*)(tty
->driver
.driver_state
) + line
;
313 tty
->driver_data
= pty
;
316 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
318 if (test_bit(TTY_PTY_LOCK
, &tty
->link
->flags
))
320 if (tty
->link
->count
!= 1)
323 clear_bit(TTY_OTHER_CLOSED
, &tty
->link
->flags
);
324 wake_up_interruptible(&pty
->open_wait
);
325 set_bit(TTY_THROTTLED
, &tty
->flags
);
331 static void pty_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
333 tty
->termios
->c_cflag
&= ~(CSIZE
| PARENB
);
334 tty
->termios
->c_cflag
|= (CS8
| CREAD
);
337 int __init
pty_init(void)
341 /* Traditional BSD devices */
343 memset(&pty_state
, 0, sizeof(pty_state
));
344 for (i
= 0; i
< NR_PTYS
; i
++)
345 init_waitqueue_head(&pty_state
[i
].open_wait
);
346 memset(&pty_driver
, 0, sizeof(struct tty_driver
));
347 pty_driver
.magic
= TTY_DRIVER_MAGIC
;
348 pty_driver
.driver_name
= "pty_master";
349 pty_driver
.name
= "pty";
350 pty_driver
.major
= PTY_MASTER_MAJOR
;
351 pty_driver
.minor_start
= 0;
352 pty_driver
.num
= NR_PTYS
;
353 pty_driver
.type
= TTY_DRIVER_TYPE_PTY
;
354 pty_driver
.subtype
= PTY_TYPE_MASTER
;
355 pty_driver
.init_termios
= tty_std_termios
;
356 pty_driver
.init_termios
.c_iflag
= 0;
357 pty_driver
.init_termios
.c_oflag
= 0;
358 pty_driver
.init_termios
.c_cflag
= B38400
| CS8
| CREAD
;
359 pty_driver
.init_termios
.c_lflag
= 0;
360 pty_driver
.flags
= TTY_DRIVER_RESET_TERMIOS
| TTY_DRIVER_REAL_RAW
;
361 pty_driver
.refcount
= &pty_refcount
;
362 pty_driver
.table
= pty_table
;
363 pty_driver
.termios
= pty_termios
;
364 pty_driver
.termios_locked
= pty_termios_locked
;
365 pty_driver
.driver_state
= pty_state
;
366 pty_driver
.other
= &pty_slave_driver
;
368 pty_driver
.open
= pty_open
;
369 pty_driver
.close
= pty_close
;
370 pty_driver
.write
= pty_write
;
371 pty_driver
.write_room
= pty_write_room
;
372 pty_driver
.flush_buffer
= pty_flush_buffer
;
373 pty_driver
.chars_in_buffer
= pty_chars_in_buffer
;
374 pty_driver
.unthrottle
= pty_unthrottle
;
375 pty_driver
.set_termios
= pty_set_termios
;
377 pty_slave_driver
= pty_driver
;
378 pty_slave_driver
.driver_name
= "pty_slave";
379 pty_slave_driver
.proc_entry
= 0;
380 pty_slave_driver
.name
= "ttyp";
381 pty_slave_driver
.subtype
= PTY_TYPE_SLAVE
;
382 pty_slave_driver
.major
= PTY_SLAVE_MAJOR
;
383 pty_slave_driver
.minor_start
= 0;
384 pty_slave_driver
.init_termios
= tty_std_termios
;
385 pty_slave_driver
.init_termios
.c_cflag
= B38400
| CS8
| CREAD
;
386 pty_slave_driver
.table
= ttyp_table
;
387 pty_slave_driver
.termios
= ttyp_termios
;
388 pty_slave_driver
.termios_locked
= ttyp_termios_locked
;
389 pty_slave_driver
.driver_state
= pty_state
;
390 pty_slave_driver
.other
= &pty_driver
;
392 if (tty_register_driver(&pty_driver
))
393 panic("Couldn't register pty driver");
394 if (tty_register_driver(&pty_slave_driver
))
395 panic("Couldn't register pty slave driver");
398 * only the master pty gets this ioctl (which is why we
399 * assign it here, instead of up with the rest of the
400 * pty_driver initialization. <cananian@alumni.princeton.edu>
402 pty_driver
.ioctl
= pty_bsd_ioctl
;
405 #ifdef CONFIG_UNIX98_PTYS
406 printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS
*NR_PTYS
);
407 for ( i
= 0 ; i
< UNIX98_NR_MAJORS
; i
++ ) {
410 ptm_driver
[i
] = pty_driver
;
411 ptm_driver
[i
].name
= "ptm";
412 ptm_driver
[i
].proc_entry
= 0;
413 ptm_driver
[i
].major
= UNIX98_PTY_MASTER_MAJOR
+i
;
414 ptm_driver
[i
].minor_start
= 0;
415 ptm_driver
[i
].name_base
= i
*NR_PTYS
;
416 ptm_driver
[i
].num
= NR_PTYS
;
417 ptm_driver
[i
].other
= &pts_driver
[i
];
418 ptm_driver
[i
].table
= ptm_table
[i
];
419 ptm_driver
[i
].termios
= ptm_termios
[i
];
420 ptm_driver
[i
].termios_locked
= ptm_termios_locked
[i
];
421 ptm_driver
[i
].driver_state
= ptm_state
[i
];
423 for (j
= 0; j
< NR_PTYS
; j
++)
424 init_waitqueue_head(&ptm_state
[i
][j
].open_wait
);
426 pts_driver
[i
] = pty_slave_driver
;
427 pts_driver
[i
].name
= "pts";
428 pts_driver
[i
].proc_entry
= 0;
429 pts_driver
[i
].major
= UNIX98_PTY_SLAVE_MAJOR
+i
;
430 pts_driver
[i
].minor_start
= 0;
431 pts_driver
[i
].name_base
= i
*NR_PTYS
;
432 pts_driver
[i
].num
= ptm_driver
[i
].num
;
433 pts_driver
[i
].other
= &ptm_driver
[i
];
434 pts_driver
[i
].table
= pts_table
[i
];
435 pts_driver
[i
].termios
= pts_termios
[i
];
436 pts_driver
[i
].termios_locked
= pts_termios_locked
[i
];
437 pts_driver
[i
].driver_state
= ptm_state
[i
];
439 ptm_driver
[i
].ioctl
= pty_unix98_ioctl
;
441 if (tty_register_driver(&ptm_driver
[i
]))
442 panic("Couldn't register Unix98 ptm driver major %d",
443 ptm_driver
[i
].major
);
444 if (tty_register_driver(&pts_driver
[i
]))
445 panic("Couldn't register Unix98 pts driver major %d",
446 pts_driver
[i
].major
);