patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / pty.c
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 #include <linux/sysctl.h>
29
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/bitops.h>
33 #include <linux/devpts_fs.h>
34
35 #if defined(CONFIG_LEGACY_PTYS) || defined(CONFIG_UNIX98_PTYS)
36
37 #ifdef CONFIG_LEGACY_PTYS
38 static struct tty_driver *pty_driver, *pty_slave_driver;
39 #endif
40
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;
45 #endif
46
47 static void pty_close(struct tty_struct * tty, struct file * filp)
48 {
49         if (!tty)
50                 return;
51         if (tty->driver->subtype == PTY_TYPE_MASTER) {
52                 if (tty->count > 1)
53                         printk("master pty_close: count = %d!!\n", tty->count);
54         } else {
55                 if (tty->count > 2)
56                         return;
57         }
58         wake_up_interruptible(&tty->read_wait);
59         wake_up_interruptible(&tty->write_wait);
60         tty->packet = 0;
61         if (!tty->link)
62                 return;
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);
72 #endif
73                 tty_vhangup(tty->link);
74         }
75 }
76
77 /*
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.
86  */
87 static void pty_unthrottle(struct tty_struct * tty)
88 {
89         struct tty_struct *o_tty = tty->link;
90
91         if (!o_tty)
92                 return;
93
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);
99 }
100
101 /*
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
109  * a count.
110  */
111 static int pty_write(struct tty_struct * tty, int from_user,
112                        const unsigned char *buf, int count)
113 {
114         struct tty_struct *to = tty->link;
115         int     c=0, n, room;
116         char    *temp_buffer;
117
118         if (!to || tty->stopped)
119                 return 0;
120
121         if (from_user) {
122                 down(&tty->flip.pty_sem);
123                 temp_buffer = &tty->flip.char_buf[0];
124                 while (count > 0) {
125                         /* check space so we don't copy needlessly */ 
126                         n = to->ldisc.receive_room(to);
127                         if (n > count)
128                                 n = count;
129                         if (!n) break;
130
131                         n  = min(n, PTY_BUF_SIZE);
132                         n -= copy_from_user(temp_buffer, buf, n);
133                         if (!n) {
134                                 if (!c)
135                                         c = -EFAULT;
136                                 break;
137                         }
138
139                         /* check again in case the buffer filled up */
140                         room = to->ldisc.receive_room(to);
141                         if (n > room)
142                                 n = room;
143                         if (!n) break;
144                         buf   += n; 
145                         c     += n;
146                         count -= n;
147                         to->ldisc.receive_buf(to, temp_buffer, 0, n);
148                 }
149                 up(&tty->flip.pty_sem);
150         } else {
151                 c = to->ldisc.receive_room(to);
152                 if (c > count)
153                         c = count;
154                 to->ldisc.receive_buf(to, buf, 0, c);
155         }
156         
157         return c;
158 }
159
160 static int pty_write_room(struct tty_struct *tty)
161 {
162         struct tty_struct *to = tty->link;
163
164         if (!to || tty->stopped)
165                 return 0;
166
167         return to->ldisc.receive_room(to);
168 }
169
170 /*
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:
175  *
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.)
182  *  
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.
186  */
187 static int pty_chars_in_buffer(struct tty_struct *tty)
188 {
189         struct tty_struct *to = tty->link;
190         int count;
191
192         if (!to || !to->ldisc.chars_in_buffer)
193                 return 0;
194
195         /* The ldisc must report 0 if no characters available to be read */
196         count = to->ldisc.chars_in_buffer(to);
197
198         if (tty->driver->subtype == PTY_TYPE_SLAVE) return count;
199
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.
204          */
205         return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
206 }
207
208 /* 
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.
212  */
213 #ifdef CONFIG_UNIX98_PTYS
214 static int pty_get_device_number(struct tty_struct *tty, unsigned __user *value)
215 {
216         unsigned int result = tty->index;
217         return put_user(result, value);
218 }
219 #endif
220
221 /* Set the lock flag on a pty */
222 static int pty_set_lock(struct tty_struct *tty, int __user * arg)
223 {
224         int val;
225         if (get_user(val,arg))
226                 return -EFAULT;
227         if (val)
228                 set_bit(TTY_PTY_LOCK, &tty->flags);
229         else
230                 clear_bit(TTY_PTY_LOCK, &tty->flags);
231         return 0;
232 }
233
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)
237 {
238         if (!tty) {
239                 printk("pty_ioctl called with NULL tty!\n");
240                 return -EIO;
241         }
242         switch(cmd) {
243         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
244                 return pty_set_lock(tty, (int __user *) arg);
245         }
246         return -ENOIOCTLCMD;
247 }
248 #endif
249
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)
253 {
254         if (!tty) {
255                 printk("pty_unix98_ioctl called with NULL tty!\n");
256                 return -EIO;
257         }
258         switch(cmd) {
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);
263         }
264
265         return -ENOIOCTLCMD;
266 }
267 #endif
268
269 static void pty_flush_buffer(struct tty_struct *tty)
270 {
271         struct tty_struct *to = tty->link;
272         
273         if (!to)
274                 return;
275         
276         if (to->ldisc.flush_buffer)
277                 to->ldisc.flush_buffer(to);
278         
279         if (to->packet) {
280                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
281                 wake_up_interruptible(&to->read_wait);
282         }
283 }
284
285 static int pty_open(struct tty_struct *tty, struct file * filp)
286 {
287         int     retval = -ENODEV;
288
289         if (!tty || !tty->link)
290                 goto out;
291
292         retval = -EIO;
293         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
294                 goto out;
295         if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
296                 goto out;
297         if (tty->link->count != 1)
298                 goto out;
299
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);
303         retval = 0;
304 out:
305         return retval;
306 }
307
308 static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
309 {
310         tty->termios->c_cflag &= ~(CSIZE | PARENB);
311         tty->termios->c_cflag |= (CS8 | CREAD);
312 }
313
314 static struct tty_operations pty_ops = {
315         .open = pty_open,
316         .close = pty_close,
317         .write = pty_write,
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,
323 };
324
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;
331
332 ctl_table pty_table[] = {
333         {
334                 .ctl_name       = PTY_MAX,
335                 .procname       = "max",
336                 .maxlen         = sizeof(int),
337                 .mode           = 0644,
338                 .data           = &pty_limit,
339                 .proc_handler   = &proc_dointvec_minmax,
340                 .strategy       = &sysctl_intvec,
341                 .extra1         = &pty_limit_min,
342                 .extra2         = &pty_limit_max,
343         }, {
344                 .ctl_name       = PTY_NR,
345                 .procname       = "nr",
346                 .maxlen         = sizeof(int),
347                 .mode           = 0444,
348                 .proc_handler   = &proc_dointvec,
349         }, {
350                 .ctl_name       = 0
351         }
352 };
353 #endif
354
355 /* Initialization */
356
357 static int __init pty_init(void)
358 {
359 #ifdef CONFIG_LEGACY_PTYS
360         /* Traditional BSD devices */
361
362         pty_driver = alloc_tty_driver(NR_PTYS);
363         if (!pty_driver)
364                 panic("Couldn't allocate pty driver");
365
366         pty_slave_driver = alloc_tty_driver(NR_PTYS);
367         if (!pty_slave_driver)
368                 panic("Couldn't allocate pty slave driver");
369
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;
387
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 |
399                                         TTY_DRIVER_REAL_RAW;
400         pty_slave_driver->other = pty_driver;
401         tty_set_operations(pty_slave_driver, &pty_ops);
402
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");
407
408 #endif /* CONFIG_LEGACY_PTYS */
409
410 #ifdef CONFIG_UNIX98_PTYS
411         /* Unix98 devices */
412         devfs_mk_dir("pts");
413         ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
414         if (!ptm_driver)
415                 panic("Couldn't allocate Unix98 ptm driver");
416         pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
417         if (!pts_driver)
418                 panic("Couldn't allocate Unix98 pts driver");
419
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;
437
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);
451         
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");
456
457         pty_table[1].data = &ptm_driver->refcount;
458 #endif /* CONFIG_UNIX98_PTYS */
459
460         return 0;
461 }
462 module_init(pty_init);
463
464 #endif /* CONFIG_LEGACY_PTYS || CONFIG_UNIX98_PTYS */