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>
23 #include <linux/devfs_fs_kernel.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27 #include <asm/bitops.h>
29 #define BUILDING_PTY_C 1
30 #include <linux/devpts_fs.h>
34 wait_queue_head_t open_wait
;
37 #define PTY_MAGIC 0x5001
39 static struct tty_driver pty_driver
, pty_slave_driver
;
40 static int pty_refcount
;
42 /* Note: one set of tables for BSD and one for Unix98 */
43 static struct tty_struct
*pty_table
[NR_PTYS
];
44 static struct termios
*pty_termios
[NR_PTYS
];
45 static struct termios
*pty_termios_locked
[NR_PTYS
];
46 static struct tty_struct
*ttyp_table
[NR_PTYS
];
47 static struct termios
*ttyp_termios
[NR_PTYS
];
48 static struct termios
*ttyp_termios_locked
[NR_PTYS
];
49 static struct pty_struct pty_state
[NR_PTYS
];
51 #ifdef CONFIG_UNIX98_PTYS
52 /* These are global because they are accessed in tty_io.c */
53 struct tty_driver ptm_driver
[UNIX98_NR_MAJORS
];
54 struct tty_driver pts_driver
[UNIX98_NR_MAJORS
];
56 static struct tty_struct
*ptm_table
[UNIX98_NR_MAJORS
][NR_PTYS
];
57 static struct termios
*ptm_termios
[UNIX98_NR_MAJORS
][NR_PTYS
];
58 static struct termios
*ptm_termios_locked
[UNIX98_NR_MAJORS
][NR_PTYS
];
59 static struct tty_struct
*pts_table
[UNIX98_NR_MAJORS
][NR_PTYS
];
60 static struct termios
*pts_termios
[UNIX98_NR_MAJORS
][NR_PTYS
];
61 static struct termios
*pts_termios_locked
[UNIX98_NR_MAJORS
][NR_PTYS
];
62 static struct pty_struct ptm_state
[UNIX98_NR_MAJORS
][NR_PTYS
];
65 #define MIN(a,b) ((a) < (b) ? (a) : (b))
67 static void pty_close(struct tty_struct
* tty
, struct file
* filp
)
71 if (tty
->driver
.subtype
== PTY_TYPE_MASTER
) {
73 printk("master pty_close: count = %d!!\n", tty
->count
);
78 wake_up_interruptible(&tty
->read_wait
);
79 wake_up_interruptible(&tty
->write_wait
);
83 tty
->link
->packet
= 0;
84 wake_up_interruptible(&tty
->link
->read_wait
);
85 wake_up_interruptible(&tty
->link
->write_wait
);
86 set_bit(TTY_OTHER_CLOSED
, &tty
->link
->flags
);
87 if (tty
->driver
.subtype
== PTY_TYPE_MASTER
) {
88 set_bit(TTY_OTHER_CLOSED
, &tty
->flags
);
89 #ifdef CONFIG_UNIX98_PTYS
91 unsigned int major
= MAJOR(tty
->device
) - UNIX98_PTY_MASTER_MAJOR
;
92 if ( major
< UNIX98_NR_MAJORS
) {
93 devpts_pty_kill( MINOR(tty
->device
)
94 - tty
->driver
.minor_start
+ tty
->driver
.name_base
);
98 tty_unregister_devfs (&tty
->link
->driver
, MINOR (tty
->device
));
99 tty_vhangup(tty
->link
);
104 * The unthrottle routine is called by the line discipline to signal
105 * that it can receive more characters. For PTY's, the TTY_THROTTLED
106 * flag is always set, to force the line discipline to always call the
107 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
108 * characters in the queue. This is necessary since each time this
109 * happens, we need to wake up any sleeping processes that could be
110 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
111 * for the pty buffer to be drained.
113 static void pty_unthrottle(struct tty_struct
* tty
)
115 struct tty_struct
*o_tty
= tty
->link
;
120 if ((o_tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
121 o_tty
->ldisc
.write_wakeup
)
122 (o_tty
->ldisc
.write_wakeup
)(o_tty
);
123 wake_up_interruptible(&o_tty
->write_wait
);
124 set_bit(TTY_THROTTLED
, &tty
->flags
);
128 * WSH 05/24/97: modified to
129 * (1) use space in tty->flip instead of a shared temp buffer
130 * The flip buffers aren't being used for a pty, so there's lots
131 * of space available. The buffer is protected by a per-pty
132 * semaphore that should almost never come under contention.
133 * (2) avoid redundant copying for cases where count >> receive_room
134 * N.B. Calls from user space may now return an error code instead of
137 static int pty_write(struct tty_struct
* tty
, int from_user
,
138 const unsigned char *buf
, int count
)
140 struct tty_struct
*to
= tty
->link
;
144 if (!to
|| tty
->stopped
)
148 down(&tty
->flip
.pty_sem
);
149 temp_buffer
= &tty
->flip
.char_buf
[0];
151 /* check space so we don't copy needlessly */
152 n
= MIN(count
, to
->ldisc
.receive_room(to
));
155 n
= MIN(n
, PTY_BUF_SIZE
);
156 n
-= copy_from_user(temp_buffer
, buf
, n
);
163 /* check again in case the buffer filled up */
164 n
= MIN(n
, to
->ldisc
.receive_room(to
));
169 to
->ldisc
.receive_buf(to
, temp_buffer
, 0, n
);
171 up(&tty
->flip
.pty_sem
);
173 c
= to
->ldisc
.receive_room(to
);
176 to
->ldisc
.receive_buf(to
, buf
, 0, c
);
182 static int pty_write_room(struct tty_struct
*tty
)
184 struct tty_struct
*to
= tty
->link
;
186 if (!to
|| tty
->stopped
)
189 return to
->ldisc
.receive_room(to
);
193 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
194 * The chars_in_buffer() value is used by the ldisc select() function
195 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
196 * The pty driver chars_in_buffer() Master/Slave must behave differently:
198 * The Master side needs to allow typed-ahead commands to accumulate
199 * while being canonicalized, so we report "our buffer" as empty until
200 * some threshold is reached, and then report the count. (Any count >
201 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
202 * the count returned must be 0 if no canonical data is available to be
203 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
205 * The Slave side passes all characters in raw mode to the Master side's
206 * buffer where they can be read immediately, so in this case we can
207 * return the true count in the buffer.
209 static int pty_chars_in_buffer(struct tty_struct
*tty
)
211 struct tty_struct
*to
= tty
->link
;
214 if (!to
|| !to
->ldisc
.chars_in_buffer
)
217 /* The ldisc must report 0 if no characters available to be read */
218 count
= to
->ldisc
.chars_in_buffer(to
);
220 if (tty
->driver
.subtype
== PTY_TYPE_SLAVE
) return count
;
222 /* Master side driver ... if the other side's read buffer is less than
223 * half full, return 0 to allow writers to proceed; otherwise return
224 * the count. This leaves a comfortable margin to avoid overflow,
225 * and still allows half a buffer's worth of typed-ahead commands.
227 return ((count
< N_TTY_BUF_SIZE
/2) ? 0 : count
);
231 * Return the device number of a Unix98 PTY (only!). This lets us open a
232 * master pty with the multi-headed ptmx device, then find out which
233 * one we got after it is open, with an ioctl.
235 #ifdef CONFIG_UNIX98_PTYS
236 static int pty_get_device_number(struct tty_struct
*tty
, unsigned int *value
)
238 unsigned int result
= MINOR(tty
->device
)
239 - tty
->driver
.minor_start
+ tty
->driver
.name_base
;
240 return put_user(result
, value
);
244 /* Set the lock flag on a pty */
245 static int pty_set_lock(struct tty_struct
*tty
, int * arg
)
248 if (get_user(val
,arg
))
251 set_bit(TTY_PTY_LOCK
, &tty
->flags
);
253 clear_bit(TTY_PTY_LOCK
, &tty
->flags
);
257 static int pty_bsd_ioctl(struct tty_struct
*tty
, struct file
*file
,
258 unsigned int cmd
, unsigned long arg
)
261 printk("pty_ioctl called with NULL tty!\n");
265 case TIOCSPTLCK
: /* Set PT Lock (disallow slave open) */
266 return pty_set_lock(tty
, (int *) arg
);
271 #ifdef CONFIG_UNIX98_PTYS
272 static int pty_unix98_ioctl(struct tty_struct
*tty
, struct file
*file
,
273 unsigned int cmd
, unsigned long arg
)
276 printk("pty_unix98_ioctl called with NULL tty!\n");
280 case TIOCGPTN
: /* Get PT Number */
281 return pty_get_device_number(tty
, (unsigned int *)arg
);
284 return pty_bsd_ioctl(tty
,file
,cmd
,arg
);
288 static void pty_flush_buffer(struct tty_struct
*tty
)
290 struct tty_struct
*to
= tty
->link
;
295 if (to
->ldisc
.flush_buffer
)
296 to
->ldisc
.flush_buffer(to
);
299 tty
->ctrl_status
|= TIOCPKT_FLUSHWRITE
;
300 wake_up_interruptible(&to
->read_wait
);
304 static int pty_open(struct tty_struct
*tty
, struct file
* filp
)
308 struct pty_struct
*pty
;
311 if (!tty
|| !tty
->link
)
313 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
314 if ((line
< 0) || (line
>= NR_PTYS
))
316 pty
= (struct pty_struct
*)(tty
->driver
.driver_state
) + line
;
317 tty
->driver_data
= pty
;
320 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
322 if (test_bit(TTY_PTY_LOCK
, &tty
->link
->flags
))
324 if (tty
->link
->count
!= 1)
327 clear_bit(TTY_OTHER_CLOSED
, &tty
->link
->flags
);
328 wake_up_interruptible(&pty
->open_wait
);
329 set_bit(TTY_THROTTLED
, &tty
->flags
);
330 /* Register a slave for the master */
331 if (tty
->driver
.major
== PTY_MASTER_MAJOR
)
332 tty_register_devfs(&tty
->link
->driver
,
333 DEVFS_FL_AUTO_OWNER
| DEVFS_FL_WAIT
,
334 tty
->link
->driver
.minor_start
+
335 MINOR(tty
->device
)-tty
->driver
.minor_start
);
341 static void pty_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
343 tty
->termios
->c_cflag
&= ~(CSIZE
| PARENB
);
344 tty
->termios
->c_cflag
|= (CS8
| CREAD
);
347 int __init
pty_init(void)
351 /* Traditional BSD devices */
353 memset(&pty_state
, 0, sizeof(pty_state
));
354 for (i
= 0; i
< NR_PTYS
; i
++)
355 init_waitqueue_head(&pty_state
[i
].open_wait
);
356 memset(&pty_driver
, 0, sizeof(struct tty_driver
));
357 pty_driver
.magic
= TTY_DRIVER_MAGIC
;
358 pty_driver
.driver_name
= "pty_master";
359 pty_driver
.name
= "pty/m%d";
360 pty_driver
.major
= PTY_MASTER_MAJOR
;
361 pty_driver
.minor_start
= 0;
362 pty_driver
.num
= NR_PTYS
;
363 pty_driver
.type
= TTY_DRIVER_TYPE_PTY
;
364 pty_driver
.subtype
= PTY_TYPE_MASTER
;
365 pty_driver
.init_termios
= tty_std_termios
;
366 pty_driver
.init_termios
.c_iflag
= 0;
367 pty_driver
.init_termios
.c_oflag
= 0;
368 pty_driver
.init_termios
.c_cflag
= B38400
| CS8
| CREAD
;
369 pty_driver
.init_termios
.c_lflag
= 0;
370 pty_driver
.flags
= TTY_DRIVER_RESET_TERMIOS
| TTY_DRIVER_REAL_RAW
;
371 pty_driver
.refcount
= &pty_refcount
;
372 pty_driver
.table
= pty_table
;
373 pty_driver
.termios
= pty_termios
;
374 pty_driver
.termios_locked
= pty_termios_locked
;
375 pty_driver
.driver_state
= pty_state
;
376 pty_driver
.other
= &pty_slave_driver
;
378 pty_driver
.open
= pty_open
;
379 pty_driver
.close
= pty_close
;
380 pty_driver
.write
= pty_write
;
381 pty_driver
.write_room
= pty_write_room
;
382 pty_driver
.flush_buffer
= pty_flush_buffer
;
383 pty_driver
.chars_in_buffer
= pty_chars_in_buffer
;
384 pty_driver
.unthrottle
= pty_unthrottle
;
385 pty_driver
.set_termios
= pty_set_termios
;
387 pty_slave_driver
= pty_driver
;
388 pty_slave_driver
.driver_name
= "pty_slave";
389 pty_slave_driver
.proc_entry
= 0;
390 pty_slave_driver
.name
= "pty/s%d";
391 pty_slave_driver
.subtype
= PTY_TYPE_SLAVE
;
392 pty_slave_driver
.major
= PTY_SLAVE_MAJOR
;
393 pty_slave_driver
.minor_start
= 0;
394 pty_slave_driver
.init_termios
= tty_std_termios
;
395 pty_slave_driver
.init_termios
.c_cflag
= B38400
| CS8
| CREAD
;
396 /* Slave ptys are registered when their corresponding master pty
397 * is opened, and unregistered when the pair is closed.
399 pty_slave_driver
.flags
|= TTY_DRIVER_NO_DEVFS
;
400 pty_slave_driver
.table
= ttyp_table
;
401 pty_slave_driver
.termios
= ttyp_termios
;
402 pty_slave_driver
.termios_locked
= ttyp_termios_locked
;
403 pty_slave_driver
.driver_state
= pty_state
;
404 pty_slave_driver
.other
= &pty_driver
;
406 if (tty_register_driver(&pty_driver
))
407 panic("Couldn't register pty driver");
408 if (tty_register_driver(&pty_slave_driver
))
409 panic("Couldn't register pty slave driver");
412 * only the master pty gets this ioctl (which is why we
413 * assign it here, instead of up with the rest of the
414 * pty_driver initialization. <cananian@alumni.princeton.edu>
416 pty_driver
.ioctl
= pty_bsd_ioctl
;
419 #ifdef CONFIG_UNIX98_PTYS
420 devfs_mk_dir (NULL
, "pts", 3, NULL
);
421 printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS
*NR_PTYS
);
422 for ( i
= 0 ; i
< UNIX98_NR_MAJORS
; i
++ ) {
425 ptm_driver
[i
] = pty_driver
;
426 ptm_driver
[i
].name
= "ptm";
427 ptm_driver
[i
].proc_entry
= 0;
428 ptm_driver
[i
].major
= UNIX98_PTY_MASTER_MAJOR
+i
;
429 ptm_driver
[i
].minor_start
= 0;
430 ptm_driver
[i
].name_base
= i
*NR_PTYS
;
431 ptm_driver
[i
].num
= NR_PTYS
;
432 ptm_driver
[i
].other
= &pts_driver
[i
];
433 ptm_driver
[i
].flags
|= TTY_DRIVER_NO_DEVFS
;
434 ptm_driver
[i
].table
= ptm_table
[i
];
435 ptm_driver
[i
].termios
= ptm_termios
[i
];
436 ptm_driver
[i
].termios_locked
= ptm_termios_locked
[i
];
437 ptm_driver
[i
].driver_state
= ptm_state
[i
];
439 for (j
= 0; j
< NR_PTYS
; j
++)
440 init_waitqueue_head(&ptm_state
[i
][j
].open_wait
);
442 pts_driver
[i
] = pty_slave_driver
;
443 pts_driver
[i
].name
= "pts/%d";
444 pts_driver
[i
].proc_entry
= 0;
445 pts_driver
[i
].major
= UNIX98_PTY_SLAVE_MAJOR
+i
;
446 pts_driver
[i
].minor_start
= 0;
447 pts_driver
[i
].name_base
= i
*NR_PTYS
;
448 pts_driver
[i
].num
= ptm_driver
[i
].num
;
449 pts_driver
[i
].other
= &ptm_driver
[i
];
450 pts_driver
[i
].table
= pts_table
[i
];
451 pts_driver
[i
].termios
= pts_termios
[i
];
452 pts_driver
[i
].termios_locked
= pts_termios_locked
[i
];
453 pts_driver
[i
].driver_state
= ptm_state
[i
];
455 ptm_driver
[i
].ioctl
= pty_unix98_ioctl
;
457 if (tty_register_driver(&ptm_driver
[i
]))
458 panic("Couldn't register Unix98 ptm driver major %d",
459 ptm_driver
[i
].major
);
460 if (tty_register_driver(&pts_driver
[i
]))
461 panic("Couldn't register Unix98 pts driver major %d",
462 pts_driver
[i
].major
);