[PATCH] s390: fix invalid return code in sclp_cpi
[linux-2.6.22.y-op.git] / drivers / char / pty.c
blob49f3997fd251d95cfaec443d98f5a15037f52c68
1 /*
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>
25 #include <linux/mm.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 <linux/bitops.h>
33 #include <linux/devpts_fs.h>
35 /* These are global because they are accessed in tty_io.c */
36 #ifdef CONFIG_UNIX98_PTYS
37 struct tty_driver *ptm_driver;
38 static struct tty_driver *pts_driver;
39 #endif
41 static void pty_close(struct tty_struct * tty, struct file * filp)
43 if (!tty)
44 return;
45 if (tty->driver->subtype == PTY_TYPE_MASTER) {
46 if (tty->count > 1)
47 printk("master pty_close: count = %d!!\n", tty->count);
48 } else {
49 if (tty->count > 2)
50 return;
52 wake_up_interruptible(&tty->read_wait);
53 wake_up_interruptible(&tty->write_wait);
54 tty->packet = 0;
55 if (!tty->link)
56 return;
57 tty->link->packet = 0;
58 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
59 wake_up_interruptible(&tty->link->read_wait);
60 wake_up_interruptible(&tty->link->write_wait);
61 if (tty->driver->subtype == PTY_TYPE_MASTER) {
62 set_bit(TTY_OTHER_CLOSED, &tty->flags);
63 #ifdef CONFIG_UNIX98_PTYS
64 if (tty->driver == ptm_driver)
65 devpts_pty_kill(tty->index);
66 #endif
67 tty_vhangup(tty->link);
72 * The unthrottle routine is called by the line discipline to signal
73 * that it can receive more characters. For PTY's, the TTY_THROTTLED
74 * flag is always set, to force the line discipline to always call the
75 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
76 * characters in the queue. This is necessary since each time this
77 * happens, we need to wake up any sleeping processes that could be
78 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
79 * for the pty buffer to be drained.
81 static void pty_unthrottle(struct tty_struct * tty)
83 struct tty_struct *o_tty = tty->link;
85 if (!o_tty)
86 return;
88 tty_wakeup(o_tty);
89 set_bit(TTY_THROTTLED, &tty->flags);
93 * WSH 05/24/97: modified to
94 * (1) use space in tty->flip instead of a shared temp buffer
95 * The flip buffers aren't being used for a pty, so there's lots
96 * of space available. The buffer is protected by a per-pty
97 * semaphore that should almost never come under contention.
98 * (2) avoid redundant copying for cases where count >> receive_room
99 * N.B. Calls from user space may now return an error code instead of
100 * a count.
102 * FIXME: Our pty_write method is called with our ldisc lock held but
103 * not our partners. We can't just take the other one blindly without
104 * risking deadlocks. There is also the small matter of TTY_DONT_FLIP
106 static int pty_write(struct tty_struct * tty, const unsigned char *buf, int count)
108 struct tty_struct *to = tty->link;
109 int c;
111 if (!to || tty->stopped)
112 return 0;
114 c = to->ldisc.receive_room(to);
115 if (c > count)
116 c = count;
117 to->ldisc.receive_buf(to, buf, NULL, c);
119 return c;
122 static int pty_write_room(struct tty_struct *tty)
124 struct tty_struct *to = tty->link;
126 if (!to || tty->stopped)
127 return 0;
129 return to->ldisc.receive_room(to);
133 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
134 * The chars_in_buffer() value is used by the ldisc select() function
135 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
136 * The pty driver chars_in_buffer() Master/Slave must behave differently:
138 * The Master side needs to allow typed-ahead commands to accumulate
139 * while being canonicalized, so we report "our buffer" as empty until
140 * some threshold is reached, and then report the count. (Any count >
141 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
142 * the count returned must be 0 if no canonical data is available to be
143 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
145 * The Slave side passes all characters in raw mode to the Master side's
146 * buffer where they can be read immediately, so in this case we can
147 * return the true count in the buffer.
149 static int pty_chars_in_buffer(struct tty_struct *tty)
151 struct tty_struct *to = tty->link;
152 int count;
154 /* We should get the line discipline lock for "tty->link" */
155 if (!to || !to->ldisc.chars_in_buffer)
156 return 0;
158 /* The ldisc must report 0 if no characters available to be read */
159 count = to->ldisc.chars_in_buffer(to);
161 if (tty->driver->subtype == PTY_TYPE_SLAVE) return count;
163 /* Master side driver ... if the other side's read buffer is less than
164 * half full, return 0 to allow writers to proceed; otherwise return
165 * the count. This leaves a comfortable margin to avoid overflow,
166 * and still allows half a buffer's worth of typed-ahead commands.
168 return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
171 /* Set the lock flag on a pty */
172 static int pty_set_lock(struct tty_struct *tty, int __user * arg)
174 int val;
175 if (get_user(val,arg))
176 return -EFAULT;
177 if (val)
178 set_bit(TTY_PTY_LOCK, &tty->flags);
179 else
180 clear_bit(TTY_PTY_LOCK, &tty->flags);
181 return 0;
184 static void pty_flush_buffer(struct tty_struct *tty)
186 struct tty_struct *to = tty->link;
188 if (!to)
189 return;
191 if (to->ldisc.flush_buffer)
192 to->ldisc.flush_buffer(to);
194 if (to->packet) {
195 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
196 wake_up_interruptible(&to->read_wait);
200 static int pty_open(struct tty_struct *tty, struct file * filp)
202 int retval = -ENODEV;
204 if (!tty || !tty->link)
205 goto out;
207 retval = -EIO;
208 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
209 goto out;
210 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
211 goto out;
212 if (tty->link->count != 1)
213 goto out;
215 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
216 set_bit(TTY_THROTTLED, &tty->flags);
217 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
218 retval = 0;
219 out:
220 return retval;
223 static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
225 tty->termios->c_cflag &= ~(CSIZE | PARENB);
226 tty->termios->c_cflag |= (CS8 | CREAD);
229 static struct tty_operations pty_ops = {
230 .open = pty_open,
231 .close = pty_close,
232 .write = pty_write,
233 .write_room = pty_write_room,
234 .flush_buffer = pty_flush_buffer,
235 .chars_in_buffer = pty_chars_in_buffer,
236 .unthrottle = pty_unthrottle,
237 .set_termios = pty_set_termios,
240 /* Traditional BSD devices */
241 #ifdef CONFIG_LEGACY_PTYS
242 static struct tty_driver *pty_driver, *pty_slave_driver;
244 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
245 unsigned int cmd, unsigned long arg)
247 switch (cmd) {
248 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
249 return pty_set_lock(tty, (int __user *) arg);
251 return -ENOIOCTLCMD;
254 static void __init legacy_pty_init(void)
257 pty_driver = alloc_tty_driver(NR_PTYS);
258 if (!pty_driver)
259 panic("Couldn't allocate pty driver");
261 pty_slave_driver = alloc_tty_driver(NR_PTYS);
262 if (!pty_slave_driver)
263 panic("Couldn't allocate pty slave driver");
265 pty_driver->owner = THIS_MODULE;
266 pty_driver->driver_name = "pty_master";
267 pty_driver->name = "pty";
268 pty_driver->devfs_name = "pty/m";
269 pty_driver->major = PTY_MASTER_MAJOR;
270 pty_driver->minor_start = 0;
271 pty_driver->type = TTY_DRIVER_TYPE_PTY;
272 pty_driver->subtype = PTY_TYPE_MASTER;
273 pty_driver->init_termios = tty_std_termios;
274 pty_driver->init_termios.c_iflag = 0;
275 pty_driver->init_termios.c_oflag = 0;
276 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
277 pty_driver->init_termios.c_lflag = 0;
278 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
279 pty_driver->other = pty_slave_driver;
280 tty_set_operations(pty_driver, &pty_ops);
281 pty_driver->ioctl = pty_bsd_ioctl;
283 pty_slave_driver->owner = THIS_MODULE;
284 pty_slave_driver->driver_name = "pty_slave";
285 pty_slave_driver->name = "ttyp";
286 pty_slave_driver->devfs_name = "pty/s";
287 pty_slave_driver->major = PTY_SLAVE_MAJOR;
288 pty_slave_driver->minor_start = 0;
289 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
290 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
291 pty_slave_driver->init_termios = tty_std_termios;
292 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
293 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
294 TTY_DRIVER_REAL_RAW;
295 pty_slave_driver->other = pty_driver;
296 tty_set_operations(pty_slave_driver, &pty_ops);
298 if (tty_register_driver(pty_driver))
299 panic("Couldn't register pty driver");
300 if (tty_register_driver(pty_slave_driver))
301 panic("Couldn't register pty slave driver");
303 #else
304 static inline void legacy_pty_init(void) { }
305 #endif
307 /* Unix98 devices */
308 #ifdef CONFIG_UNIX98_PTYS
310 * sysctl support for setting limits on the number of Unix98 ptys allocated.
311 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
313 int pty_limit = NR_UNIX98_PTY_DEFAULT;
314 static int pty_limit_min = 0;
315 static int pty_limit_max = NR_UNIX98_PTY_MAX;
317 ctl_table pty_table[] = {
319 .ctl_name = PTY_MAX,
320 .procname = "max",
321 .maxlen = sizeof(int),
322 .mode = 0644,
323 .data = &pty_limit,
324 .proc_handler = &proc_dointvec_minmax,
325 .strategy = &sysctl_intvec,
326 .extra1 = &pty_limit_min,
327 .extra2 = &pty_limit_max,
328 }, {
329 .ctl_name = PTY_NR,
330 .procname = "nr",
331 .maxlen = sizeof(int),
332 .mode = 0444,
333 .proc_handler = &proc_dointvec,
334 }, {
335 .ctl_name = 0
339 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
340 unsigned int cmd, unsigned long arg)
342 switch (cmd) {
343 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
344 return pty_set_lock(tty, (int __user *)arg);
345 case TIOCGPTN: /* Get PT Number */
346 return put_user(tty->index, (unsigned int __user *)arg);
349 return -ENOIOCTLCMD;
352 static void __init unix98_pty_init(void)
354 devfs_mk_dir("pts");
355 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
356 if (!ptm_driver)
357 panic("Couldn't allocate Unix98 ptm driver");
358 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
359 if (!pts_driver)
360 panic("Couldn't allocate Unix98 pts driver");
362 ptm_driver->owner = THIS_MODULE;
363 ptm_driver->driver_name = "pty_master";
364 ptm_driver->name = "ptm";
365 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
366 ptm_driver->minor_start = 0;
367 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
368 ptm_driver->subtype = PTY_TYPE_MASTER;
369 ptm_driver->init_termios = tty_std_termios;
370 ptm_driver->init_termios.c_iflag = 0;
371 ptm_driver->init_termios.c_oflag = 0;
372 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
373 ptm_driver->init_termios.c_lflag = 0;
374 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
375 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
376 ptm_driver->other = pts_driver;
377 tty_set_operations(ptm_driver, &pty_ops);
378 ptm_driver->ioctl = pty_unix98_ioctl;
380 pts_driver->owner = THIS_MODULE;
381 pts_driver->driver_name = "pty_slave";
382 pts_driver->name = "pts";
383 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
384 pts_driver->minor_start = 0;
385 pts_driver->type = TTY_DRIVER_TYPE_PTY;
386 pts_driver->subtype = PTY_TYPE_SLAVE;
387 pts_driver->init_termios = tty_std_termios;
388 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
389 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
390 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
391 pts_driver->other = ptm_driver;
392 tty_set_operations(pts_driver, &pty_ops);
394 if (tty_register_driver(ptm_driver))
395 panic("Couldn't register Unix98 ptm driver");
396 if (tty_register_driver(pts_driver))
397 panic("Couldn't register Unix98 pts driver");
399 pty_table[1].data = &ptm_driver->refcount;
401 #else
402 static inline void unix98_pty_init(void) { }
403 #endif
405 static int __init pty_init(void)
407 legacy_pty_init();
408 unix98_pty_init();
409 return 0;
411 module_init(pty_init);