ia64/xen-unstable

view linux-2.6.9-xen-sparse/drivers/char/tty_io.c @ 3033:2c98cd276123

bitkeeper revision 1.1159.176.4 (419cb819pVY6eBsMUAArh6ibXVHVbg)

Only check console_use_vt during initialization.
author cl349@arcadians.cl.cam.ac.uk
date Thu Nov 18 14:56:25 2004 +0000 (2004-11-18)
parents c7639d2f3d9d
children f25437a7e267
line source
1 /*
2 * linux/drivers/char/tty_io.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
10 *
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 *
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
18 *
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
23 *
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
27 *
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
31 *
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
35 *
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38 *
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
41 *
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
44 *
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
47 *
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51 *
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
54 *
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 *
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60 *
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
63 *
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
66 */
68 #include <linux/config.h>
69 #include <linux/types.h>
70 #include <linux/major.h>
71 #include <linux/errno.h>
72 #include <linux/signal.h>
73 #include <linux/fcntl.h>
74 #include <linux/sched.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/devpts_fs.h>
80 #include <linux/file.h>
81 #include <linux/console.h>
82 #include <linux/timer.h>
83 #include <linux/ctype.h>
84 #include <linux/kd.h>
85 #include <linux/mm.h>
86 #include <linux/string.h>
87 #include <linux/slab.h>
88 #include <linux/poll.h>
89 #include <linux/proc_fs.h>
90 #include <linux/init.h>
91 #include <linux/module.h>
92 #include <linux/smp_lock.h>
93 #include <linux/device.h>
94 #include <linux/idr.h>
95 #include <linux/wait.h>
97 #include <asm/uaccess.h>
98 #include <asm/system.h>
99 #include <asm/bitops.h>
101 #include <linux/kbd_kern.h>
102 #include <linux/vt_kern.h>
103 #include <linux/selection.h>
104 #include <linux/devfs_fs_kernel.h>
106 #include <linux/kmod.h>
108 #undef TTY_DEBUG_HANGUP
110 #define TTY_PARANOIA_CHECK 1
111 #define CHECK_TTY_COUNT 1
113 struct termios tty_std_termios = { /* for the benefit of tty drivers */
114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
119 .c_cc = INIT_C_CC
120 };
122 EXPORT_SYMBOL(tty_std_termios);
124 /* This list gets poked at by procfs and various bits of boot up code. This
125 could do with some rationalisation such as pulling the tty proc function
126 into this file */
128 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
130 /* Semaphore to protect creating and releasing a tty. This is shared with
131 vt.c for deeply disgusting hack reasons */
132 DECLARE_MUTEX(tty_sem);
134 int console_use_vt = 1;
136 #ifdef CONFIG_UNIX98_PTYS
137 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
138 extern int pty_limit; /* Config limit on Unix98 ptys */
139 static DEFINE_IDR(allocated_ptys);
140 static DECLARE_MUTEX(allocated_ptys_lock);
141 #endif
143 extern void disable_early_printk(void);
145 static void initialize_tty_struct(struct tty_struct *tty);
147 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
148 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
149 ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
150 static unsigned int tty_poll(struct file *, poll_table *);
151 static int tty_open(struct inode *, struct file *);
152 static int ptmx_open(struct inode *, struct file *);
153 static int tty_release(struct inode *, struct file *);
154 int tty_ioctl(struct inode * inode, struct file * file,
155 unsigned int cmd, unsigned long arg);
156 static int tty_fasync(int fd, struct file * filp, int on);
157 extern void rs_360_init(void);
158 static void release_mem(struct tty_struct *tty, int idx);
161 static struct tty_struct *alloc_tty_struct(void)
162 {
163 struct tty_struct *tty;
165 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
166 if (tty)
167 memset(tty, 0, sizeof(struct tty_struct));
168 return tty;
169 }
171 static inline void free_tty_struct(struct tty_struct *tty)
172 {
173 kfree(tty);
174 }
176 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
178 char *tty_name(struct tty_struct *tty, char *buf)
179 {
180 if (!tty) /* Hmm. NULL pointer. That's fun. */
181 strcpy(buf, "NULL tty");
182 else
183 strcpy(buf, tty->name);
184 return buf;
185 }
187 EXPORT_SYMBOL(tty_name);
189 inline int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
190 const char *routine)
191 {
192 #ifdef TTY_PARANOIA_CHECK
193 if (!tty) {
194 printk(KERN_WARNING
195 "null TTY for (%d:%d) in %s\n",
196 imajor(inode), iminor(inode), routine);
197 return 1;
198 }
199 if (tty->magic != TTY_MAGIC) {
200 printk(KERN_WARNING
201 "bad magic number for tty struct (%d:%d) in %s\n",
202 imajor(inode), iminor(inode), routine);
203 return 1;
204 }
205 #endif
206 return 0;
207 }
209 static int check_tty_count(struct tty_struct *tty, const char *routine)
210 {
211 #ifdef CHECK_TTY_COUNT
212 struct list_head *p;
213 int count = 0;
215 file_list_lock();
216 list_for_each(p, &tty->tty_files) {
217 count++;
218 }
219 file_list_unlock();
220 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
221 tty->driver->subtype == PTY_TYPE_SLAVE &&
222 tty->link && tty->link->count)
223 count++;
224 if (tty->count != count) {
225 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
226 "!= #fd's(%d) in %s\n",
227 tty->name, tty->count, count, routine);
228 return count;
229 }
230 #endif
231 return 0;
232 }
234 /*
235 * This is probably overkill for real world processors but
236 * they are not on hot paths so a little discipline won't do
237 * any harm.
238 */
240 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
241 {
242 down(&tty->termios_sem);
243 tty->termios->c_line = num;
244 up(&tty->termios_sem);
245 }
247 /*
248 * This guards the refcounted line discipline lists. The lock
249 * must be taken with irqs off because there are hangup path
250 * callers who will do ldisc lookups and cannot sleep.
251 */
253 static spinlock_t tty_ldisc_lock = SPIN_LOCK_UNLOCKED;
254 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
255 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
257 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
258 {
259 unsigned long flags;
260 int ret = 0;
262 if (disc < N_TTY || disc >= NR_LDISCS)
263 return -EINVAL;
265 spin_lock_irqsave(&tty_ldisc_lock, flags);
266 if (new_ldisc) {
267 tty_ldiscs[disc] = *new_ldisc;
268 tty_ldiscs[disc].num = disc;
269 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
270 tty_ldiscs[disc].refcount = 0;
271 } else {
272 if(tty_ldiscs[disc].refcount)
273 ret = -EBUSY;
274 else
275 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
276 }
277 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
279 return ret;
280 }
282 EXPORT_SYMBOL(tty_register_ldisc);
284 struct tty_ldisc *tty_ldisc_get(int disc)
285 {
286 unsigned long flags;
287 struct tty_ldisc *ld;
289 if (disc < N_TTY || disc >= NR_LDISCS)
290 return NULL;
292 spin_lock_irqsave(&tty_ldisc_lock, flags);
294 ld = &tty_ldiscs[disc];
295 /* Check the entry is defined */
296 if(ld->flags & LDISC_FLAG_DEFINED)
297 {
298 /* If the module is being unloaded we can't use it */
299 if (!try_module_get(ld->owner))
300 ld = NULL;
301 else /* lock it */
302 ld->refcount++;
303 }
304 else
305 ld = NULL;
306 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
307 return ld;
308 }
310 EXPORT_SYMBOL_GPL(tty_ldisc_get);
312 void tty_ldisc_put(int disc)
313 {
314 struct tty_ldisc *ld;
315 unsigned long flags;
317 if (disc < N_TTY || disc >= NR_LDISCS)
318 BUG();
320 spin_lock_irqsave(&tty_ldisc_lock, flags);
321 ld = &tty_ldiscs[disc];
322 if(ld->refcount == 0)
323 BUG();
324 ld->refcount --;
325 module_put(ld->owner);
326 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
327 }
329 EXPORT_SYMBOL_GPL(tty_ldisc_put);
331 void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
332 {
333 tty->ldisc = *ld;
334 tty->ldisc.refcount = 0;
335 }
337 /**
338 * tty_ldisc_try - internal helper
339 * @tty: the tty
340 *
341 * Make a single attempt to grab and bump the refcount on
342 * the tty ldisc. Return 0 on failure or 1 on success. This is
343 * used to implement both the waiting and non waiting versions
344 * of tty_ldisc_ref
345 */
347 static int tty_ldisc_try(struct tty_struct *tty)
348 {
349 unsigned long flags;
350 struct tty_ldisc *ld;
351 int ret = 0;
353 spin_lock_irqsave(&tty_ldisc_lock, flags);
354 ld = &tty->ldisc;
355 if(test_bit(TTY_LDISC, &tty->flags))
356 {
357 ld->refcount++;
358 ret = 1;
359 }
360 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
361 return ret;
362 }
364 /**
365 * tty_ldisc_ref_wait - wait for the tty ldisc
366 * @tty: tty device
367 *
368 * Dereference the line discipline for the terminal and take a
369 * reference to it. If the line discipline is in flux then
370 * wait patiently until it changes.
371 *
372 * Note: Must not be called from an IRQ/timer context. The caller
373 * must also be careful not to hold other locks that will deadlock
374 * against a discipline change, such as an existing ldisc reference
375 * (which we check for)
376 */
378 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
379 {
380 /* wait_event is a macro */
381 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
382 if(tty->ldisc.refcount == 0)
383 printk(KERN_ERR "tty_ldisc_ref_wait\n");
384 return &tty->ldisc;
385 }
387 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
389 /**
390 * tty_ldisc_ref - get the tty ldisc
391 * @tty: tty device
392 *
393 * Dereference the line discipline for the terminal and take a
394 * reference to it. If the line discipline is in flux then
395 * return NULL. Can be called from IRQ and timer functions.
396 */
398 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
399 {
400 if(tty_ldisc_try(tty))
401 return &tty->ldisc;
402 return NULL;
403 }
405 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
407 /**
408 * tty_ldisc_deref - free a tty ldisc reference
409 * @ld: reference to free up
410 *
411 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
412 * be called in IRQ context.
413 */
415 void tty_ldisc_deref(struct tty_ldisc *ld)
416 {
417 unsigned long flags;
419 if(ld == NULL)
420 BUG();
422 spin_lock_irqsave(&tty_ldisc_lock, flags);
423 if(ld->refcount == 0)
424 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
425 else
426 ld->refcount--;
427 if(ld->refcount == 0)
428 wake_up(&tty_ldisc_wait);
429 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
430 }
432 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
434 /**
435 * tty_ldisc_enable - allow ldisc use
436 * @tty: terminal to activate ldisc on
437 *
438 * Set the TTY_LDISC flag when the line discipline can be called
439 * again. Do neccessary wakeups for existing sleepers.
440 *
441 * Note: nobody should set this bit except via this function. Clearing
442 * directly is allowed.
443 */
445 static void tty_ldisc_enable(struct tty_struct *tty)
446 {
447 set_bit(TTY_LDISC, &tty->flags);
448 wake_up(&tty_ldisc_wait);
449 }
451 /**
452 * tty_set_ldisc - set line discipline
453 * @tty: the terminal to set
454 * @ldisc: the line discipline
455 *
456 * Set the discipline of a tty line. Must be called from a process
457 * context.
458 */
460 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
461 {
462 int retval = 0;
463 struct tty_ldisc o_ldisc;
464 char buf[64];
465 int work;
466 unsigned long flags;
467 struct tty_ldisc *ld;
469 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
470 return -EINVAL;
472 restart:
474 if (tty->ldisc.num == ldisc)
475 return 0; /* We are already in the desired discipline */
477 ld = tty_ldisc_get(ldisc);
478 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
479 /* Cyrus Durgin <cider@speakeasy.org> */
480 if (ld == NULL) {
481 request_module("tty-ldisc-%d", ldisc);
482 ld = tty_ldisc_get(ldisc);
483 }
484 if (ld == NULL)
485 return -EINVAL;
487 o_ldisc = tty->ldisc;
489 tty_wait_until_sent(tty, 0);
491 /*
492 * Make sure we don't change while someone holds a
493 * reference to the line discipline. The TTY_LDISC bit
494 * prevents anyone taking a reference once it is clear.
495 * We need the lock to avoid racing reference takers.
496 */
498 spin_lock_irqsave(&tty_ldisc_lock, flags);
499 if(tty->ldisc.refcount)
500 {
501 /* Free the new ldisc we grabbed. Must drop the lock
502 first. */
503 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
504 tty_ldisc_put(ldisc);
505 /*
506 * There are several reasons we may be busy, including
507 * random momentary I/O traffic. We must therefore
508 * retry. We could distinguish between blocking ops
509 * and retries if we made tty_ldisc_wait() smarter. That
510 * is up for discussion.
511 */
512 if(wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
513 return -ERESTARTSYS;
514 goto restart;
515 }
516 clear_bit(TTY_LDISC, &tty->flags);
517 clear_bit(TTY_DONT_FLIP, &tty->flags);
518 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
520 /*
521 * From this point on we know nobody has an ldisc
522 * usage reference, nor can they obtain one until
523 * we say so later on.
524 */
526 work = cancel_delayed_work(&tty->flip.work);
527 /*
528 * Wait for ->hangup_work and ->flip.work handlers to terminate
529 */
531 flush_scheduled_work();
532 /* Shutdown the current discipline. */
533 if (tty->ldisc.close)
534 (tty->ldisc.close)(tty);
536 /* Now set up the new line discipline. */
537 tty_ldisc_assign(tty, ld);
538 tty_set_termios_ldisc(tty, ldisc);
539 if (tty->ldisc.open)
540 retval = (tty->ldisc.open)(tty);
541 if (retval < 0) {
542 tty_ldisc_put(ldisc);
543 /* There is an outstanding reference here so this is safe */
544 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
545 tty_set_termios_ldisc(tty, tty->ldisc.num);
546 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
547 tty_ldisc_put(o_ldisc.num);
548 /* This driver is always present */
549 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
550 tty_set_termios_ldisc(tty, N_TTY);
551 if (tty->ldisc.open) {
552 int r = tty->ldisc.open(tty);
554 if (r < 0)
555 panic("Couldn't open N_TTY ldisc for "
556 "%s --- error %d.",
557 tty_name(tty, buf), r);
558 }
559 }
560 }
561 /* At this point we hold a reference to the new ldisc and a
562 a reference to the old ldisc. If we ended up flipping back
563 to the existing ldisc we have two references to it */
565 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
566 tty->driver->set_ldisc(tty);
568 tty_ldisc_put(o_ldisc.num);
570 /*
571 * Allow ldisc referencing to occur as soon as the driver
572 * ldisc callback completes.
573 */
575 tty_ldisc_enable(tty);
577 /* Restart it in case no characters kick it off. Safe if
578 already running */
579 if(work)
580 schedule_delayed_work(&tty->flip.work, 1);
581 return retval;
582 }
584 /*
585 * This routine returns a tty driver structure, given a device number
586 */
587 struct tty_driver *get_tty_driver(dev_t device, int *index)
588 {
589 struct tty_driver *p;
591 list_for_each_entry(p, &tty_drivers, tty_drivers) {
592 dev_t base = MKDEV(p->major, p->minor_start);
593 if (device < base || device >= base + p->num)
594 continue;
595 *index = device - base;
596 return p;
597 }
598 return NULL;
599 }
601 /*
602 * If we try to write to, or set the state of, a terminal and we're
603 * not in the foreground, send a SIGTTOU. If the signal is blocked or
604 * ignored, go ahead and perform the operation. (POSIX 7.2)
605 */
606 int tty_check_change(struct tty_struct * tty)
607 {
608 if (current->signal->tty != tty)
609 return 0;
610 if (tty->pgrp <= 0) {
611 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
612 return 0;
613 }
614 if (process_group(current) == tty->pgrp)
615 return 0;
616 if (is_ignored(SIGTTOU))
617 return 0;
618 if (is_orphaned_pgrp(process_group(current)))
619 return -EIO;
620 (void) kill_pg(process_group(current), SIGTTOU, 1);
621 return -ERESTARTSYS;
622 }
624 EXPORT_SYMBOL(tty_check_change);
626 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
627 size_t count, loff_t *ppos)
628 {
629 return 0;
630 }
632 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
633 size_t count, loff_t *ppos)
634 {
635 return -EIO;
636 }
638 /* No kernel lock held - none needed ;) */
639 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
640 {
641 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
642 }
644 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
645 unsigned int cmd, unsigned long arg)
646 {
647 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
648 }
650 static struct file_operations tty_fops = {
651 .llseek = no_llseek,
652 .read = tty_read,
653 .write = tty_write,
654 .poll = tty_poll,
655 .ioctl = tty_ioctl,
656 .open = tty_open,
657 .release = tty_release,
658 .fasync = tty_fasync,
659 };
661 #ifdef CONFIG_UNIX98_PTYS
662 static struct file_operations ptmx_fops = {
663 .llseek = no_llseek,
664 .read = tty_read,
665 .write = tty_write,
666 .poll = tty_poll,
667 .ioctl = tty_ioctl,
668 .open = ptmx_open,
669 .release = tty_release,
670 .fasync = tty_fasync,
671 };
672 #endif
674 static struct file_operations console_fops = {
675 .llseek = no_llseek,
676 .read = tty_read,
677 .write = redirected_tty_write,
678 .poll = tty_poll,
679 .ioctl = tty_ioctl,
680 .open = tty_open,
681 .release = tty_release,
682 .fasync = tty_fasync,
683 };
685 static struct file_operations hung_up_tty_fops = {
686 .llseek = no_llseek,
687 .read = hung_up_tty_read,
688 .write = hung_up_tty_write,
689 .poll = hung_up_tty_poll,
690 .ioctl = hung_up_tty_ioctl,
691 .release = tty_release,
692 };
694 static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
695 static struct file *redirect;
697 /**
698 * tty_wakeup - request more data
699 * @tty: terminal
700 *
701 * Internal and external helper for wakeups of tty. This function
702 * informs the line discipline if present that the driver is ready
703 * to receive more output data.
704 */
706 void tty_wakeup(struct tty_struct *tty)
707 {
708 struct tty_ldisc *ld;
710 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
711 ld = tty_ldisc_ref(tty);
712 if(ld) {
713 if(ld->write_wakeup)
714 ld->write_wakeup(tty);
715 tty_ldisc_deref(ld);
716 }
717 }
718 wake_up_interruptible(&tty->write_wait);
719 }
721 EXPORT_SYMBOL_GPL(tty_wakeup);
723 /**
724 * tty_ldisc_flush - flush line discipline queue
725 * @tty: tty
726 *
727 * Flush the line discipline queue (if any) for this tty. If there
728 * is no line discipline active this is a no-op.
729 */
731 void tty_ldisc_flush(struct tty_struct *tty)
732 {
733 struct tty_ldisc *ld = tty_ldisc_ref(tty);
734 if(ld) {
735 if(ld->flush_buffer)
736 ld->flush_buffer(tty);
737 tty_ldisc_deref(ld);
738 }
739 }
741 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
743 /*
744 * This can be called by the "eventd" kernel thread. That is process synchronous,
745 * but doesn't hold any locks, so we need to make sure we have the appropriate
746 * locks for what we're doing..
747 */
748 void do_tty_hangup(void *data)
749 {
750 struct tty_struct *tty = (struct tty_struct *) data;
751 struct file * cons_filp = NULL;
752 struct file *filp, *f = NULL;
753 struct task_struct *p;
754 struct tty_ldisc *ld;
755 int closecount = 0, n;
757 if (!tty)
758 return;
760 /* inuse_filps is protected by the single kernel lock */
761 lock_kernel();
763 spin_lock(&redirect_lock);
764 if (redirect && redirect->private_data == tty) {
765 f = redirect;
766 redirect = NULL;
767 }
768 spin_unlock(&redirect_lock);
770 check_tty_count(tty, "do_tty_hangup");
771 file_list_lock();
772 /* This breaks for file handles being sent over AF_UNIX sockets ? */
773 list_for_each_entry(filp, &tty->tty_files, f_list) {
774 if (filp->f_op->write == redirected_tty_write)
775 cons_filp = filp;
776 if (filp->f_op->write != tty_write)
777 continue;
778 closecount++;
779 tty_fasync(-1, filp, 0); /* can't block */
780 filp->f_op = &hung_up_tty_fops;
781 }
782 file_list_unlock();
784 /* FIXME! What are the locking issues here? This may me overdoing things..
785 * this question is especially important now that we've removed the irqlock. */
787 ld = tty_ldisc_ref(tty);
788 if(ld != NULL) /* We may have no line discipline at this point */
789 {
790 if (ld->flush_buffer)
791 ld->flush_buffer(tty);
792 if (tty->driver->flush_buffer)
793 tty->driver->flush_buffer(tty);
794 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
795 ld->write_wakeup)
796 ld->write_wakeup(tty);
797 if (ld->hangup)
798 ld->hangup(tty);
799 }
801 /* FIXME: Once we trust the LDISC code better we can wait here for
802 ldisc completion and fix the driver call race */
804 wake_up_interruptible(&tty->write_wait);
805 wake_up_interruptible(&tty->read_wait);
807 /*
808 * Shutdown the current line discipline, and reset it to
809 * N_TTY.
810 */
811 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
812 {
813 down(&tty->termios_sem);
814 *tty->termios = tty->driver->init_termios;
815 up(&tty->termios_sem);
816 }
818 /* Defer ldisc switch */
819 /* tty_deferred_ldisc_switch(N_TTY);
821 This should get done automatically when the port closes and
822 tty_release is called */
824 read_lock(&tasklist_lock);
825 if (tty->session > 0) {
826 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
827 if (p->signal->tty == tty)
828 p->signal->tty = NULL;
829 if (!p->signal->leader)
830 continue;
831 send_group_sig_info(SIGHUP, SEND_SIG_PRIV, p);
832 send_group_sig_info(SIGCONT, SEND_SIG_PRIV, p);
833 if (tty->pgrp > 0)
834 p->signal->tty_old_pgrp = tty->pgrp;
835 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
836 }
837 read_unlock(&tasklist_lock);
839 tty->flags = 0;
840 tty->session = 0;
841 tty->pgrp = -1;
842 tty->ctrl_status = 0;
843 /*
844 * If one of the devices matches a console pointer, we
845 * cannot just call hangup() because that will cause
846 * tty->count and state->count to go out of sync.
847 * So we just call close() the right number of times.
848 */
849 if (cons_filp) {
850 if (tty->driver->close)
851 for (n = 0; n < closecount; n++)
852 tty->driver->close(tty, cons_filp);
853 } else if (tty->driver->hangup)
854 (tty->driver->hangup)(tty);
856 /* We don't want to have driver/ldisc interactions beyond
857 the ones we did here. The driver layer expects no
858 calls after ->hangup() from the ldisc side. However we
859 can't yet guarantee all that */
861 set_bit(TTY_HUPPED, &tty->flags);
862 if (ld) {
863 tty_ldisc_enable(tty);
864 tty_ldisc_deref(ld);
865 }
866 unlock_kernel();
867 if (f)
868 fput(f);
869 }
871 void tty_hangup(struct tty_struct * tty)
872 {
873 #ifdef TTY_DEBUG_HANGUP
874 char buf[64];
876 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
877 #endif
878 schedule_work(&tty->hangup_work);
879 }
881 EXPORT_SYMBOL(tty_hangup);
883 void tty_vhangup(struct tty_struct * tty)
884 {
885 #ifdef TTY_DEBUG_HANGUP
886 char buf[64];
888 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
889 #endif
890 do_tty_hangup((void *) tty);
891 }
892 EXPORT_SYMBOL(tty_vhangup);
894 int tty_hung_up_p(struct file * filp)
895 {
896 return (filp->f_op == &hung_up_tty_fops);
897 }
899 EXPORT_SYMBOL(tty_hung_up_p);
901 /*
902 * This function is typically called only by the session leader, when
903 * it wants to disassociate itself from its controlling tty.
904 *
905 * It performs the following functions:
906 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
907 * (2) Clears the tty from being controlling the session
908 * (3) Clears the controlling tty for all processes in the
909 * session group.
910 *
911 * The argument on_exit is set to 1 if called when a process is
912 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
913 */
914 void disassociate_ctty(int on_exit)
915 {
916 struct tty_struct *tty;
917 struct task_struct *p;
918 int tty_pgrp = -1;
920 lock_kernel();
922 tty = current->signal->tty;
923 if (tty) {
924 tty_pgrp = tty->pgrp;
925 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
926 tty_vhangup(tty);
927 } else {
928 if (current->signal->tty_old_pgrp) {
929 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
930 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
931 }
932 unlock_kernel();
933 return;
934 }
935 if (tty_pgrp > 0) {
936 kill_pg(tty_pgrp, SIGHUP, on_exit);
937 if (!on_exit)
938 kill_pg(tty_pgrp, SIGCONT, on_exit);
939 }
941 current->signal->tty_old_pgrp = 0;
942 tty->session = 0;
943 tty->pgrp = -1;
945 read_lock(&tasklist_lock);
946 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
947 p->signal->tty = NULL;
948 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
949 read_unlock(&tasklist_lock);
950 unlock_kernel();
951 }
953 void stop_tty(struct tty_struct *tty)
954 {
955 if (tty->stopped)
956 return;
957 tty->stopped = 1;
958 if (tty->link && tty->link->packet) {
959 tty->ctrl_status &= ~TIOCPKT_START;
960 tty->ctrl_status |= TIOCPKT_STOP;
961 wake_up_interruptible(&tty->link->read_wait);
962 }
963 if (tty->driver->stop)
964 (tty->driver->stop)(tty);
965 }
967 EXPORT_SYMBOL(stop_tty);
969 void start_tty(struct tty_struct *tty)
970 {
971 if (!tty->stopped || tty->flow_stopped)
972 return;
973 tty->stopped = 0;
974 if (tty->link && tty->link->packet) {
975 tty->ctrl_status &= ~TIOCPKT_STOP;
976 tty->ctrl_status |= TIOCPKT_START;
977 wake_up_interruptible(&tty->link->read_wait);
978 }
979 if (tty->driver->start)
980 (tty->driver->start)(tty);
982 /* If we have a running line discipline it may need kicking */
983 tty_wakeup(tty);
984 wake_up_interruptible(&tty->write_wait);
985 }
987 EXPORT_SYMBOL(start_tty);
989 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
990 loff_t *ppos)
991 {
992 int i;
993 struct tty_struct * tty;
994 struct inode *inode;
995 struct tty_ldisc *ld;
997 tty = (struct tty_struct *)file->private_data;
998 inode = file->f_dentry->d_inode;
999 if (tty_paranoia_check(tty, inode, "tty_read"))
1000 return -EIO;
1001 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1002 return -EIO;
1004 /* We want to wait for the line discipline to sort out in this
1005 situation */
1006 ld = tty_ldisc_ref_wait(tty);
1007 lock_kernel();
1008 if (ld->read)
1009 i = (ld->read)(tty,file,buf,count);
1010 else
1011 i = -EIO;
1012 tty_ldisc_deref(ld);
1013 unlock_kernel();
1014 if (i > 0)
1015 inode->i_atime = CURRENT_TIME;
1016 return i;
1019 /*
1020 * Split writes up in sane blocksizes to avoid
1021 * denial-of-service type attacks
1022 */
1023 static inline ssize_t do_tty_write(
1024 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char __user *, size_t),
1025 struct tty_struct *tty,
1026 struct file *file,
1027 const unsigned char __user *buf,
1028 size_t count)
1030 ssize_t ret = 0, written = 0;
1032 if (down_interruptible(&tty->atomic_write)) {
1033 return -ERESTARTSYS;
1035 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
1036 lock_kernel();
1037 written = write(tty, file, buf, count);
1038 unlock_kernel();
1039 } else {
1040 for (;;) {
1041 unsigned long size = max((unsigned long)PAGE_SIZE*2, 16384UL);
1042 if (size > count)
1043 size = count;
1044 lock_kernel();
1045 ret = write(tty, file, buf, size);
1046 unlock_kernel();
1047 if (ret <= 0)
1048 break;
1049 written += ret;
1050 buf += ret;
1051 count -= ret;
1052 if (!count)
1053 break;
1054 ret = -ERESTARTSYS;
1055 if (signal_pending(current))
1056 break;
1057 cond_resched();
1060 if (written) {
1061 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1062 ret = written;
1064 up(&tty->atomic_write);
1065 return ret;
1069 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
1070 loff_t *ppos)
1072 struct tty_struct * tty;
1073 struct inode *inode = file->f_dentry->d_inode;
1074 ssize_t ret;
1075 struct tty_ldisc *ld;
1077 tty = (struct tty_struct *)file->private_data;
1078 if (tty_paranoia_check(tty, inode, "tty_write"))
1079 return -EIO;
1080 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1081 return -EIO;
1083 ld = tty_ldisc_ref_wait(tty);
1084 if (!ld->write)
1085 ret = -EIO;
1086 else
1087 ret = do_tty_write(ld->write, tty, file,
1088 (const unsigned char __user *)buf, count);
1089 tty_ldisc_deref(ld);
1090 return ret;
1093 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
1094 loff_t *ppos)
1096 struct file *p = NULL;
1098 spin_lock(&redirect_lock);
1099 if (redirect) {
1100 get_file(redirect);
1101 p = redirect;
1103 spin_unlock(&redirect_lock);
1105 if (p) {
1106 ssize_t res;
1107 res = vfs_write(p, buf, count, &p->f_pos);
1108 fput(p);
1109 return res;
1112 return tty_write(file, buf, count, ppos);
1115 static char ptychar[] = "pqrstuvwxyzabcde";
1117 static inline void pty_line_name(struct tty_driver *driver, int index, char *p)
1119 int i = index + driver->name_base;
1120 /* ->name is initialized to "ttyp", but "tty" is expected */
1121 sprintf(p, "%s%c%x",
1122 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1123 ptychar[i >> 4 & 0xf], i & 0xf);
1126 static inline void tty_line_name(struct tty_driver *driver, int index, char *p)
1128 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1131 /*
1132 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1133 * failed open. The new code protects the open with a semaphore, so it's
1134 * really quite straightforward. The semaphore locking can probably be
1135 * relaxed for the (most common) case of reopening a tty.
1136 */
1137 static int init_dev(struct tty_driver *driver, int idx,
1138 struct tty_struct **ret_tty)
1140 struct tty_struct *tty, *o_tty;
1141 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1142 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1143 int retval=0;
1145 /*
1146 * Check whether we need to acquire the tty semaphore to avoid
1147 * race conditions. For now, play it safe.
1148 */
1149 down(&tty_sem);
1151 /* check whether we're reopening an existing tty */
1152 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1153 tty = devpts_get_tty(idx);
1154 if (tty && driver->subtype == PTY_TYPE_MASTER)
1155 tty = tty->link;
1156 } else {
1157 tty = driver->ttys[idx];
1159 if (tty) goto fast_track;
1161 /*
1162 * First time open is complex, especially for PTY devices.
1163 * This code guarantees that either everything succeeds and the
1164 * TTY is ready for operation, or else the table slots are vacated
1165 * and the allocated memory released. (Except that the termios
1166 * and locked termios may be retained.)
1167 */
1169 if (!try_module_get(driver->owner)) {
1170 retval = -ENODEV;
1171 goto end_init;
1174 o_tty = NULL;
1175 tp = o_tp = NULL;
1176 ltp = o_ltp = NULL;
1178 tty = alloc_tty_struct();
1179 if(!tty)
1180 goto fail_no_mem;
1181 initialize_tty_struct(tty);
1182 tty->driver = driver;
1183 tty->index = idx;
1184 tty_line_name(driver, idx, tty->name);
1186 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1187 tp_loc = &tty->termios;
1188 ltp_loc = &tty->termios_locked;
1189 } else {
1190 tp_loc = &driver->termios[idx];
1191 ltp_loc = &driver->termios_locked[idx];
1194 if (!*tp_loc) {
1195 tp = (struct termios *) kmalloc(sizeof(struct termios),
1196 GFP_KERNEL);
1197 if (!tp)
1198 goto free_mem_out;
1199 *tp = driver->init_termios;
1202 if (!*ltp_loc) {
1203 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1204 GFP_KERNEL);
1205 if (!ltp)
1206 goto free_mem_out;
1207 memset(ltp, 0, sizeof(struct termios));
1210 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1211 o_tty = alloc_tty_struct();
1212 if (!o_tty)
1213 goto free_mem_out;
1214 initialize_tty_struct(o_tty);
1215 o_tty->driver = driver->other;
1216 o_tty->index = idx;
1217 tty_line_name(driver->other, idx, o_tty->name);
1219 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1220 o_tp_loc = &o_tty->termios;
1221 o_ltp_loc = &o_tty->termios_locked;
1222 } else {
1223 o_tp_loc = &driver->other->termios[idx];
1224 o_ltp_loc = &driver->other->termios_locked[idx];
1227 if (!*o_tp_loc) {
1228 o_tp = (struct termios *)
1229 kmalloc(sizeof(struct termios), GFP_KERNEL);
1230 if (!o_tp)
1231 goto free_mem_out;
1232 *o_tp = driver->other->init_termios;
1235 if (!*o_ltp_loc) {
1236 o_ltp = (struct termios *)
1237 kmalloc(sizeof(struct termios), GFP_KERNEL);
1238 if (!o_ltp)
1239 goto free_mem_out;
1240 memset(o_ltp, 0, sizeof(struct termios));
1243 /*
1244 * Everything allocated ... set up the o_tty structure.
1245 */
1246 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
1247 driver->other->ttys[idx] = o_tty;
1249 if (!*o_tp_loc)
1250 *o_tp_loc = o_tp;
1251 if (!*o_ltp_loc)
1252 *o_ltp_loc = o_ltp;
1253 o_tty->termios = *o_tp_loc;
1254 o_tty->termios_locked = *o_ltp_loc;
1255 driver->other->refcount++;
1256 if (driver->subtype == PTY_TYPE_MASTER)
1257 o_tty->count++;
1259 /* Establish the links in both directions */
1260 tty->link = o_tty;
1261 o_tty->link = tty;
1264 /*
1265 * All structures have been allocated, so now we install them.
1266 * Failures after this point use release_mem to clean up, so
1267 * there's no need to null out the local pointers.
1268 */
1269 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1270 driver->ttys[idx] = tty;
1273 if (!*tp_loc)
1274 *tp_loc = tp;
1275 if (!*ltp_loc)
1276 *ltp_loc = ltp;
1277 tty->termios = *tp_loc;
1278 tty->termios_locked = *ltp_loc;
1279 driver->refcount++;
1280 tty->count++;
1282 /*
1283 * Structures all installed ... call the ldisc open routines.
1284 * If we fail here just call release_mem to clean up. No need
1285 * to decrement the use counts, as release_mem doesn't care.
1286 */
1288 if (tty->ldisc.open) {
1289 retval = (tty->ldisc.open)(tty);
1290 if (retval)
1291 goto release_mem_out;
1293 if (o_tty && o_tty->ldisc.open) {
1294 retval = (o_tty->ldisc.open)(o_tty);
1295 if (retval) {
1296 if (tty->ldisc.close)
1297 (tty->ldisc.close)(tty);
1298 goto release_mem_out;
1300 tty_ldisc_enable(o_tty);
1302 tty_ldisc_enable(tty);
1303 goto success;
1305 /*
1306 * This fast open can be used if the tty is already open.
1307 * No memory is allocated, and the only failures are from
1308 * attempting to open a closing tty or attempting multiple
1309 * opens on a pty master.
1310 */
1311 fast_track:
1312 if (test_bit(TTY_CLOSING, &tty->flags)) {
1313 retval = -EIO;
1314 goto end_init;
1316 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1317 driver->subtype == PTY_TYPE_MASTER) {
1318 /*
1319 * special case for PTY masters: only one open permitted,
1320 * and the slave side open count is incremented as well.
1321 */
1322 if (tty->count) {
1323 retval = -EIO;
1324 goto end_init;
1326 tty->link->count++;
1328 tty->count++;
1329 tty->driver = driver; /* N.B. why do this every time?? */
1331 /* FIXME */
1332 if(!test_bit(TTY_LDISC, &tty->flags))
1333 printk(KERN_ERR "init_dev but no ldisc\n");
1334 success:
1335 *ret_tty = tty;
1337 /* All paths come through here to release the semaphore */
1338 end_init:
1339 up(&tty_sem);
1340 return retval;
1342 /* Release locally allocated memory ... nothing placed in slots */
1343 free_mem_out:
1344 if (o_tp)
1345 kfree(o_tp);
1346 if (o_tty)
1347 free_tty_struct(o_tty);
1348 if (ltp)
1349 kfree(ltp);
1350 if (tp)
1351 kfree(tp);
1352 free_tty_struct(tty);
1354 fail_no_mem:
1355 module_put(driver->owner);
1356 retval = -ENOMEM;
1357 goto end_init;
1359 /* call the tty release_mem routine to clean out this slot */
1360 release_mem_out:
1361 printk(KERN_INFO "init_dev: ldisc open failed, "
1362 "clearing slot %d\n", idx);
1363 release_mem(tty, idx);
1364 goto end_init;
1367 /*
1368 * Releases memory associated with a tty structure, and clears out the
1369 * driver table slots.
1370 */
1371 static void release_mem(struct tty_struct *tty, int idx)
1373 struct tty_struct *o_tty;
1374 struct termios *tp;
1375 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1377 if ((o_tty = tty->link) != NULL) {
1378 if (!devpts)
1379 o_tty->driver->ttys[idx] = NULL;
1380 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1381 tp = o_tty->termios;
1382 if (!devpts)
1383 o_tty->driver->termios[idx] = NULL;
1384 kfree(tp);
1386 tp = o_tty->termios_locked;
1387 if (!devpts)
1388 o_tty->driver->termios_locked[idx] = NULL;
1389 kfree(tp);
1391 o_tty->magic = 0;
1392 o_tty->driver->refcount--;
1393 file_list_lock();
1394 list_del_init(&o_tty->tty_files);
1395 file_list_unlock();
1396 free_tty_struct(o_tty);
1399 if (!devpts)
1400 tty->driver->ttys[idx] = NULL;
1401 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1402 tp = tty->termios;
1403 if (!devpts)
1404 tty->driver->termios[idx] = NULL;
1405 kfree(tp);
1407 tp = tty->termios_locked;
1408 if (!devpts)
1409 tty->driver->termios_locked[idx] = NULL;
1410 kfree(tp);
1413 tty->magic = 0;
1414 tty->driver->refcount--;
1415 file_list_lock();
1416 list_del_init(&tty->tty_files);
1417 file_list_unlock();
1418 module_put(tty->driver->owner);
1419 free_tty_struct(tty);
1422 /*
1423 * Even releasing the tty structures is a tricky business.. We have
1424 * to be very careful that the structures are all released at the
1425 * same time, as interrupts might otherwise get the wrong pointers.
1427 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1428 * lead to double frees or releasing memory still in use.
1429 */
1430 static void release_dev(struct file * filp)
1432 struct tty_struct *tty, *o_tty;
1433 int pty_master, tty_closing, o_tty_closing, do_sleep;
1434 int devpts_master, devpts;
1435 int idx;
1436 char buf[64];
1437 unsigned long flags;
1439 tty = (struct tty_struct *)filp->private_data;
1440 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
1441 return;
1443 check_tty_count(tty, "release_dev");
1445 tty_fasync(-1, filp, 0);
1447 idx = tty->index;
1448 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1449 tty->driver->subtype == PTY_TYPE_MASTER);
1450 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1451 devpts_master = pty_master && devpts;
1452 o_tty = tty->link;
1454 #ifdef TTY_PARANOIA_CHECK
1455 if (idx < 0 || idx >= tty->driver->num) {
1456 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1457 "free (%s)\n", tty->name);
1458 return;
1460 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1461 if (tty != tty->driver->ttys[idx]) {
1462 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1463 "for (%s)\n", idx, tty->name);
1464 return;
1466 if (tty->termios != tty->driver->termios[idx]) {
1467 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1468 "for (%s)\n",
1469 idx, tty->name);
1470 return;
1472 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1473 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1474 "termios_locked for (%s)\n",
1475 idx, tty->name);
1476 return;
1479 #endif
1481 #ifdef TTY_DEBUG_HANGUP
1482 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1483 tty_name(tty, buf), tty->count);
1484 #endif
1486 #ifdef TTY_PARANOIA_CHECK
1487 if (tty->driver->other &&
1488 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1489 if (o_tty != tty->driver->other->ttys[idx]) {
1490 printk(KERN_DEBUG "release_dev: other->table[%d] "
1491 "not o_tty for (%s)\n",
1492 idx, tty->name);
1493 return;
1495 if (o_tty->termios != tty->driver->other->termios[idx]) {
1496 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1497 "not o_termios for (%s)\n",
1498 idx, tty->name);
1499 return;
1501 if (o_tty->termios_locked !=
1502 tty->driver->other->termios_locked[idx]) {
1503 printk(KERN_DEBUG "release_dev: other->termios_locked["
1504 "%d] not o_termios_locked for (%s)\n",
1505 idx, tty->name);
1506 return;
1508 if (o_tty->link != tty) {
1509 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1510 return;
1513 #endif
1514 if (tty->driver->close)
1515 tty->driver->close(tty, filp);
1517 /*
1518 * Sanity check: if tty->count is going to zero, there shouldn't be
1519 * any waiters on tty->read_wait or tty->write_wait. We test the
1520 * wait queues and kick everyone out _before_ actually starting to
1521 * close. This ensures that we won't block while releasing the tty
1522 * structure.
1524 * The test for the o_tty closing is necessary, since the master and
1525 * slave sides may close in any order. If the slave side closes out
1526 * first, its count will be one, since the master side holds an open.
1527 * Thus this test wouldn't be triggered at the time the slave closes,
1528 * so we do it now.
1530 * Note that it's possible for the tty to be opened again while we're
1531 * flushing out waiters. By recalculating the closing flags before
1532 * each iteration we avoid any problems.
1533 */
1534 while (1) {
1535 tty_closing = tty->count <= 1;
1536 o_tty_closing = o_tty &&
1537 (o_tty->count <= (pty_master ? 1 : 0));
1538 do_sleep = 0;
1540 if (tty_closing) {
1541 if (waitqueue_active(&tty->read_wait)) {
1542 wake_up(&tty->read_wait);
1543 do_sleep++;
1545 if (waitqueue_active(&tty->write_wait)) {
1546 wake_up(&tty->write_wait);
1547 do_sleep++;
1550 if (o_tty_closing) {
1551 if (waitqueue_active(&o_tty->read_wait)) {
1552 wake_up(&o_tty->read_wait);
1553 do_sleep++;
1555 if (waitqueue_active(&o_tty->write_wait)) {
1556 wake_up(&o_tty->write_wait);
1557 do_sleep++;
1560 if (!do_sleep)
1561 break;
1563 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1564 "active!\n", tty_name(tty, buf));
1565 schedule();
1568 /*
1569 * The closing flags are now consistent with the open counts on
1570 * both sides, and we've completed the last operation that could
1571 * block, so it's safe to proceed with closing.
1572 */
1573 if (pty_master) {
1574 if (--o_tty->count < 0) {
1575 printk(KERN_WARNING "release_dev: bad pty slave count "
1576 "(%d) for %s\n",
1577 o_tty->count, tty_name(o_tty, buf));
1578 o_tty->count = 0;
1581 if (--tty->count < 0) {
1582 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1583 tty->count, tty_name(tty, buf));
1584 tty->count = 0;
1587 /*
1588 * We've decremented tty->count, so we need to remove this file
1589 * descriptor off the tty->tty_files list; this serves two
1590 * purposes:
1591 * - check_tty_count sees the correct number of file descriptors
1592 * associated with this tty.
1593 * - do_tty_hangup no longer sees this file descriptor as
1594 * something that needs to be handled for hangups.
1595 */
1596 file_kill(filp);
1597 filp->private_data = NULL;
1599 /*
1600 * Perform some housekeeping before deciding whether to return.
1602 * Set the TTY_CLOSING flag if this was the last open. In the
1603 * case of a pty we may have to wait around for the other side
1604 * to close, and TTY_CLOSING makes sure we can't be reopened.
1605 */
1606 if(tty_closing)
1607 set_bit(TTY_CLOSING, &tty->flags);
1608 if(o_tty_closing)
1609 set_bit(TTY_CLOSING, &o_tty->flags);
1611 /*
1612 * If _either_ side is closing, make sure there aren't any
1613 * processes that still think tty or o_tty is their controlling
1614 * tty.
1615 */
1616 if (tty_closing || o_tty_closing) {
1617 struct task_struct *p;
1619 read_lock(&tasklist_lock);
1620 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1621 p->signal->tty = NULL;
1622 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1623 if (o_tty)
1624 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
1625 p->signal->tty = NULL;
1626 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
1627 read_unlock(&tasklist_lock);
1630 /* check whether both sides are closing ... */
1631 if (!tty_closing || (o_tty && !o_tty_closing))
1632 return;
1634 #ifdef TTY_DEBUG_HANGUP
1635 printk(KERN_DEBUG "freeing tty structure...");
1636 #endif
1637 /*
1638 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
1639 * kill any delayed work. As this is the final close it does not
1640 * race with the set_ldisc code path.
1641 */
1642 clear_bit(TTY_LDISC, &tty->flags);
1643 clear_bit(TTY_DONT_FLIP, &tty->flags);
1644 cancel_delayed_work(&tty->flip.work);
1646 /*
1647 * Wait for ->hangup_work and ->flip.work handlers to terminate
1648 */
1650 flush_scheduled_work();
1652 /*
1653 * Wait for any short term users (we know they are just driver
1654 * side waiters as the file is closing so user count on the file
1655 * side is zero.
1656 */
1657 spin_lock_irqsave(&tty_ldisc_lock, flags);
1658 while(tty->ldisc.refcount)
1660 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1661 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
1662 spin_lock_irqsave(&tty_ldisc_lock, flags);
1664 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1665 /*
1666 * Shutdown the current line discipline, and reset it to N_TTY.
1667 * N.B. why reset ldisc when we're releasing the memory??
1669 * FIXME: this MUST get fixed for the new reflocking
1670 */
1671 if (tty->ldisc.close)
1672 (tty->ldisc.close)(tty);
1673 tty_ldisc_put(tty->ldisc.num);
1675 /*
1676 * Switch the line discipline back
1677 */
1678 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1679 tty_set_termios_ldisc(tty,N_TTY);
1680 if (o_tty) {
1681 /* FIXME: could o_tty be in setldisc here ? */
1682 clear_bit(TTY_LDISC, &o_tty->flags);
1683 if (o_tty->ldisc.close)
1684 (o_tty->ldisc.close)(o_tty);
1685 tty_ldisc_put(o_tty->ldisc.num);
1686 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
1687 tty_set_termios_ldisc(o_tty,N_TTY);
1689 /*
1690 * The release_mem function takes care of the details of clearing
1691 * the slots and preserving the termios structure.
1692 */
1693 release_mem(tty, idx);
1695 #ifdef CONFIG_UNIX98_PTYS
1696 /* Make this pty number available for reallocation */
1697 if (devpts) {
1698 down(&allocated_ptys_lock);
1699 idr_remove(&allocated_ptys, idx);
1700 up(&allocated_ptys_lock);
1702 #endif
1706 /*
1707 * tty_open and tty_release keep up the tty count that contains the
1708 * number of opens done on a tty. We cannot use the inode-count, as
1709 * different inodes might point to the same tty.
1711 * Open-counting is needed for pty masters, as well as for keeping
1712 * track of serial lines: DTR is dropped when the last close happens.
1713 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1715 * The termios state of a pty is reset on first open so that
1716 * settings don't persist across reuse.
1717 */
1718 static int tty_open(struct inode * inode, struct file * filp)
1720 struct tty_struct *tty;
1721 int noctty, retval;
1722 struct tty_driver *driver;
1723 int index;
1724 dev_t device = inode->i_rdev;
1725 unsigned short saved_flags = filp->f_flags;
1727 nonseekable_open(inode, filp);
1729 retry_open:
1730 noctty = filp->f_flags & O_NOCTTY;
1731 index = -1;
1732 retval = 0;
1734 if (device == MKDEV(TTYAUX_MAJOR,0)) {
1735 if (!current->signal->tty)
1736 return -ENXIO;
1737 driver = current->signal->tty->driver;
1738 index = current->signal->tty->index;
1739 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1740 /* noctty = 1; */
1741 goto got_driver;
1743 #ifdef CONFIG_VT
1744 if (device == MKDEV(TTY_MAJOR,0)) {
1745 extern int fg_console;
1746 extern struct tty_driver *console_driver;
1747 driver = console_driver;
1748 index = fg_console;
1749 noctty = 1;
1750 goto got_driver;
1752 #endif
1753 if (device == MKDEV(TTYAUX_MAJOR,1)) {
1754 driver = console_device(&index);
1755 if (driver) {
1756 /* Don't let /dev/console block */
1757 filp->f_flags |= O_NONBLOCK;
1758 noctty = 1;
1759 goto got_driver;
1761 return -ENODEV;
1764 driver = get_tty_driver(device, &index);
1765 if (!driver)
1766 return -ENODEV;
1767 got_driver:
1768 retval = init_dev(driver, index, &tty);
1769 if (retval)
1770 return retval;
1772 filp->private_data = tty;
1773 file_move(filp, &tty->tty_files);
1774 check_tty_count(tty, "tty_open");
1775 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1776 tty->driver->subtype == PTY_TYPE_MASTER)
1777 noctty = 1;
1778 #ifdef TTY_DEBUG_HANGUP
1779 printk(KERN_DEBUG "opening %s...", tty->name);
1780 #endif
1781 if (!retval) {
1782 if (tty->driver->open)
1783 retval = tty->driver->open(tty, filp);
1784 else
1785 retval = -ENODEV;
1787 filp->f_flags = saved_flags;
1789 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1790 retval = -EBUSY;
1792 if (retval) {
1793 #ifdef TTY_DEBUG_HANGUP
1794 printk(KERN_DEBUG "error %d in opening %s...", retval,
1795 tty->name);
1796 #endif
1797 release_dev(filp);
1798 if (retval != -ERESTARTSYS)
1799 return retval;
1800 if (signal_pending(current))
1801 return retval;
1802 schedule();
1803 /*
1804 * Need to reset f_op in case a hangup happened.
1805 */
1806 if (filp->f_op == &hung_up_tty_fops)
1807 filp->f_op = &tty_fops;
1808 goto retry_open;
1810 if (!noctty &&
1811 current->signal->leader &&
1812 !current->signal->tty &&
1813 tty->session == 0) {
1814 task_lock(current);
1815 current->signal->tty = tty;
1816 task_unlock(current);
1817 current->signal->tty_old_pgrp = 0;
1818 tty->session = current->signal->session;
1819 tty->pgrp = process_group(current);
1821 return 0;
1824 #ifdef CONFIG_UNIX98_PTYS
1825 static int ptmx_open(struct inode * inode, struct file * filp)
1827 struct tty_struct *tty;
1828 int retval;
1829 int index;
1830 int idr_ret;
1832 nonseekable_open(inode, filp);
1834 /* find a device that is not in use. */
1835 down(&allocated_ptys_lock);
1836 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
1837 up(&allocated_ptys_lock);
1838 return -ENOMEM;
1840 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
1841 if (idr_ret < 0) {
1842 up(&allocated_ptys_lock);
1843 if (idr_ret == -EAGAIN)
1844 return -ENOMEM;
1845 return -EIO;
1847 if (index >= pty_limit) {
1848 idr_remove(&allocated_ptys, index);
1849 up(&allocated_ptys_lock);
1850 return -EIO;
1852 up(&allocated_ptys_lock);
1854 retval = init_dev(ptm_driver, index, &tty);
1855 if (retval)
1856 goto out;
1858 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1859 filp->private_data = tty;
1860 file_move(filp, &tty->tty_files);
1862 retval = -ENOMEM;
1863 if (devpts_pty_new(tty->link))
1864 goto out1;
1866 check_tty_count(tty, "tty_open");
1867 retval = ptm_driver->open(tty, filp);
1868 if (!retval)
1869 return 0;
1870 out1:
1871 release_dev(filp);
1872 out:
1873 down(&allocated_ptys_lock);
1874 idr_remove(&allocated_ptys, index);
1875 up(&allocated_ptys_lock);
1876 return retval;
1878 #endif
1880 static int tty_release(struct inode * inode, struct file * filp)
1882 lock_kernel();
1883 release_dev(filp);
1884 unlock_kernel();
1885 return 0;
1888 /* No kernel lock held - fine */
1889 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1891 struct tty_struct * tty;
1892 struct tty_ldisc *ld;
1893 int ret = 0;
1895 tty = (struct tty_struct *)filp->private_data;
1896 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
1897 return 0;
1899 ld = tty_ldisc_ref_wait(tty);
1900 if (ld->poll)
1901 ret = (ld->poll)(tty, filp, wait);
1902 tty_ldisc_deref(ld);
1903 return ret;
1906 static int tty_fasync(int fd, struct file * filp, int on)
1908 struct tty_struct * tty;
1909 int retval;
1911 tty = (struct tty_struct *)filp->private_data;
1912 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
1913 return 0;
1915 retval = fasync_helper(fd, filp, on, &tty->fasync);
1916 if (retval <= 0)
1917 return retval;
1919 if (on) {
1920 if (!waitqueue_active(&tty->read_wait))
1921 tty->minimum_to_wake = 1;
1922 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
1923 if (retval)
1924 return retval;
1925 } else {
1926 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1927 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1929 return 0;
1932 static int tiocsti(struct tty_struct *tty, char __user *p)
1934 char ch, mbz = 0;
1935 struct tty_ldisc *ld;
1937 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1938 return -EPERM;
1939 if (get_user(ch, p))
1940 return -EFAULT;
1941 ld = tty_ldisc_ref_wait(tty);
1942 ld->receive_buf(tty, &ch, &mbz, 1);
1943 tty_ldisc_deref(ld);
1944 return 0;
1947 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
1949 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1950 return -EFAULT;
1951 return 0;
1954 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1955 struct winsize __user * arg)
1957 struct winsize tmp_ws;
1959 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1960 return -EFAULT;
1961 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1962 return 0;
1963 #ifdef CONFIG_VT
1964 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
1965 unsigned int currcons = tty->index;
1966 int rc;
1968 acquire_console_sem();
1969 rc = vc_resize(currcons, tmp_ws.ws_col, tmp_ws.ws_row);
1970 release_console_sem();
1971 if (rc)
1972 return -ENXIO;
1974 #endif
1975 if (tty->pgrp > 0)
1976 kill_pg(tty->pgrp, SIGWINCH, 1);
1977 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1978 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1979 tty->winsize = tmp_ws;
1980 real_tty->winsize = tmp_ws;
1981 return 0;
1984 static int tioccons(struct file *file)
1986 if (file->f_op->write == redirected_tty_write) {
1987 struct file *f;
1988 if (!capable(CAP_SYS_ADMIN))
1989 return -EPERM;
1990 spin_lock(&redirect_lock);
1991 f = redirect;
1992 redirect = NULL;
1993 spin_unlock(&redirect_lock);
1994 if (f)
1995 fput(f);
1996 return 0;
1998 spin_lock(&redirect_lock);
1999 if (redirect) {
2000 spin_unlock(&redirect_lock);
2001 return -EBUSY;
2003 get_file(file);
2004 redirect = file;
2005 spin_unlock(&redirect_lock);
2006 return 0;
2010 static int fionbio(struct file *file, int __user *p)
2012 int nonblock;
2014 if (get_user(nonblock, p))
2015 return -EFAULT;
2017 if (nonblock)
2018 file->f_flags |= O_NONBLOCK;
2019 else
2020 file->f_flags &= ~O_NONBLOCK;
2021 return 0;
2024 static int tiocsctty(struct tty_struct *tty, int arg)
2026 task_t *p;
2028 if (current->signal->leader &&
2029 (current->signal->session == tty->session))
2030 return 0;
2031 /*
2032 * The process must be a session leader and
2033 * not have a controlling tty already.
2034 */
2035 if (!current->signal->leader || current->signal->tty)
2036 return -EPERM;
2037 if (tty->session > 0) {
2038 /*
2039 * This tty is already the controlling
2040 * tty for another session group!
2041 */
2042 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
2043 /*
2044 * Steal it away
2045 */
2047 read_lock(&tasklist_lock);
2048 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2049 p->signal->tty = NULL;
2050 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2051 read_unlock(&tasklist_lock);
2052 } else
2053 return -EPERM;
2055 task_lock(current);
2056 current->signal->tty = tty;
2057 task_unlock(current);
2058 current->signal->tty_old_pgrp = 0;
2059 tty->session = current->signal->session;
2060 tty->pgrp = process_group(current);
2061 return 0;
2064 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2066 /*
2067 * (tty == real_tty) is a cheap way of
2068 * testing if the tty is NOT a master pty.
2069 */
2070 if (tty == real_tty && current->signal->tty != real_tty)
2071 return -ENOTTY;
2072 return put_user(real_tty->pgrp, p);
2075 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2077 pid_t pgrp;
2078 int retval = tty_check_change(real_tty);
2080 if (retval == -EIO)
2081 return -ENOTTY;
2082 if (retval)
2083 return retval;
2084 if (!current->signal->tty ||
2085 (current->signal->tty != real_tty) ||
2086 (real_tty->session != current->signal->session))
2087 return -ENOTTY;
2088 if (get_user(pgrp, p))
2089 return -EFAULT;
2090 if (pgrp < 0)
2091 return -EINVAL;
2092 if (session_of_pgrp(pgrp) != current->signal->session)
2093 return -EPERM;
2094 real_tty->pgrp = pgrp;
2095 return 0;
2098 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2100 /*
2101 * (tty == real_tty) is a cheap way of
2102 * testing if the tty is NOT a master pty.
2103 */
2104 if (tty == real_tty && current->signal->tty != real_tty)
2105 return -ENOTTY;
2106 if (real_tty->session <= 0)
2107 return -ENOTTY;
2108 return put_user(real_tty->session, p);
2111 static int tiocsetd(struct tty_struct *tty, int __user *p)
2113 int ldisc;
2115 if (get_user(ldisc, p))
2116 return -EFAULT;
2117 return tty_set_ldisc(tty, ldisc);
2120 static int send_break(struct tty_struct *tty, int duration)
2122 set_current_state(TASK_INTERRUPTIBLE);
2124 tty->driver->break_ctl(tty, -1);
2125 if (!signal_pending(current))
2126 schedule_timeout(duration);
2127 tty->driver->break_ctl(tty, 0);
2128 if (signal_pending(current))
2129 return -EINTR;
2130 return 0;
2133 static int
2134 tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2136 int retval = -EINVAL;
2138 if (tty->driver->tiocmget) {
2139 retval = tty->driver->tiocmget(tty, file);
2141 if (retval >= 0)
2142 retval = put_user(retval, p);
2144 return retval;
2147 static int
2148 tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2149 unsigned __user *p)
2151 int retval = -EINVAL;
2153 if (tty->driver->tiocmset) {
2154 unsigned int set, clear, val;
2156 retval = get_user(val, p);
2157 if (retval)
2158 return retval;
2160 set = clear = 0;
2161 switch (cmd) {
2162 case TIOCMBIS:
2163 set = val;
2164 break;
2165 case TIOCMBIC:
2166 clear = val;
2167 break;
2168 case TIOCMSET:
2169 set = val;
2170 clear = ~val;
2171 break;
2174 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2175 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2177 retval = tty->driver->tiocmset(tty, file, set, clear);
2179 return retval;
2182 /*
2183 * Split this up, as gcc can choke on it otherwise..
2184 */
2185 int tty_ioctl(struct inode * inode, struct file * file,
2186 unsigned int cmd, unsigned long arg)
2188 struct tty_struct *tty, *real_tty;
2189 void __user *p = (void __user *)arg;
2190 int retval;
2191 struct tty_ldisc *ld;
2193 tty = (struct tty_struct *)file->private_data;
2194 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2195 return -EINVAL;
2197 real_tty = tty;
2198 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2199 tty->driver->subtype == PTY_TYPE_MASTER)
2200 real_tty = tty->link;
2202 /*
2203 * Break handling by driver
2204 */
2205 if (!tty->driver->break_ctl) {
2206 switch(cmd) {
2207 case TIOCSBRK:
2208 case TIOCCBRK:
2209 if (tty->driver->ioctl)
2210 return tty->driver->ioctl(tty, file, cmd, arg);
2211 return -EINVAL;
2213 /* These two ioctl's always return success; even if */
2214 /* the driver doesn't support them. */
2215 case TCSBRK:
2216 case TCSBRKP:
2217 if (!tty->driver->ioctl)
2218 return 0;
2219 retval = tty->driver->ioctl(tty, file, cmd, arg);
2220 if (retval == -ENOIOCTLCMD)
2221 retval = 0;
2222 return retval;
2226 /*
2227 * Factor out some common prep work
2228 */
2229 switch (cmd) {
2230 case TIOCSETD:
2231 case TIOCSBRK:
2232 case TIOCCBRK:
2233 case TCSBRK:
2234 case TCSBRKP:
2235 retval = tty_check_change(tty);
2236 if (retval)
2237 return retval;
2238 if (cmd != TIOCCBRK) {
2239 tty_wait_until_sent(tty, 0);
2240 if (signal_pending(current))
2241 return -EINTR;
2243 break;
2246 switch (cmd) {
2247 case TIOCSTI:
2248 return tiocsti(tty, p);
2249 case TIOCGWINSZ:
2250 return tiocgwinsz(tty, p);
2251 case TIOCSWINSZ:
2252 return tiocswinsz(tty, real_tty, p);
2253 case TIOCCONS:
2254 return real_tty!=tty ? -EINVAL : tioccons(file);
2255 case FIONBIO:
2256 return fionbio(file, p);
2257 case TIOCEXCL:
2258 set_bit(TTY_EXCLUSIVE, &tty->flags);
2259 return 0;
2260 case TIOCNXCL:
2261 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2262 return 0;
2263 case TIOCNOTTY:
2264 if (current->signal->tty != tty)
2265 return -ENOTTY;
2266 if (current->signal->leader)
2267 disassociate_ctty(0);
2268 task_lock(current);
2269 current->signal->tty = NULL;
2270 task_unlock(current);
2271 return 0;
2272 case TIOCSCTTY:
2273 return tiocsctty(tty, arg);
2274 case TIOCGPGRP:
2275 return tiocgpgrp(tty, real_tty, p);
2276 case TIOCSPGRP:
2277 return tiocspgrp(tty, real_tty, p);
2278 case TIOCGSID:
2279 return tiocgsid(tty, real_tty, p);
2280 case TIOCGETD:
2281 /* FIXME: check this is ok */
2282 return put_user(tty->ldisc.num, (int __user *)p);
2283 case TIOCSETD:
2284 return tiocsetd(tty, p);
2285 #ifdef CONFIG_VT
2286 case TIOCLINUX:
2287 return tioclinux(tty, arg);
2288 #endif
2289 /*
2290 * Break handling
2291 */
2292 case TIOCSBRK: /* Turn break on, unconditionally */
2293 tty->driver->break_ctl(tty, -1);
2294 return 0;
2296 case TIOCCBRK: /* Turn break off, unconditionally */
2297 tty->driver->break_ctl(tty, 0);
2298 return 0;
2299 case TCSBRK: /* SVID version: non-zero arg --> no break */
2300 /*
2301 * XXX is the above comment correct, or the
2302 * code below correct? Is this ioctl used at
2303 * all by anyone?
2304 */
2305 if (!arg)
2306 return send_break(tty, HZ/4);
2307 return 0;
2308 case TCSBRKP: /* support for POSIX tcsendbreak() */
2309 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
2311 case TIOCMGET:
2312 return tty_tiocmget(tty, file, p);
2314 case TIOCMSET:
2315 case TIOCMBIC:
2316 case TIOCMBIS:
2317 return tty_tiocmset(tty, file, cmd, p);
2319 if (tty->driver->ioctl) {
2320 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
2321 if (retval != -ENOIOCTLCMD)
2322 return retval;
2324 ld = tty_ldisc_ref_wait(tty);
2325 retval = -EINVAL;
2326 if (ld->ioctl) {
2327 retval = ld->ioctl(tty, file, cmd, arg);
2328 if (retval == -ENOIOCTLCMD)
2329 retval = -EINVAL;
2331 tty_ldisc_deref(ld);
2332 return retval;
2336 /*
2337 * This implements the "Secure Attention Key" --- the idea is to
2338 * prevent trojan horses by killing all processes associated with this
2339 * tty when the user hits the "Secure Attention Key". Required for
2340 * super-paranoid applications --- see the Orange Book for more details.
2342 * This code could be nicer; ideally it should send a HUP, wait a few
2343 * seconds, then send a INT, and then a KILL signal. But you then
2344 * have to coordinate with the init process, since all processes associated
2345 * with the current tty must be dead before the new getty is allowed
2346 * to spawn.
2348 * Now, if it would be correct ;-/ The current code has a nasty hole -
2349 * it doesn't catch files in flight. We may send the descriptor to ourselves
2350 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2352 * Nasty bug: do_SAK is being called in interrupt context. This can
2353 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2354 */
2355 static void __do_SAK(void *arg)
2357 #ifdef TTY_SOFT_SAK
2358 tty_hangup(tty);
2359 #else
2360 struct tty_struct *tty = arg;
2361 struct task_struct *p;
2362 int session;
2363 int i;
2364 struct file *filp;
2365 struct tty_ldisc *disc;
2367 if (!tty)
2368 return;
2369 session = tty->session;
2371 /* We don't want an ldisc switch during this */
2372 disc = tty_ldisc_ref(tty);
2373 if (disc && disc->flush_buffer)
2374 disc->flush_buffer(tty);
2375 tty_ldisc_deref(disc);
2377 if (tty->driver->flush_buffer)
2378 tty->driver->flush_buffer(tty);
2380 read_lock(&tasklist_lock);
2381 do_each_task_pid(session, PIDTYPE_SID, p) {
2382 if (p->signal->tty == tty || session > 0) {
2383 printk(KERN_NOTICE "SAK: killed process %d"
2384 " (%s): p->signal->session==tty->session\n",
2385 p->pid, p->comm);
2386 send_sig(SIGKILL, p, 1);
2387 continue;
2389 task_lock(p);
2390 if (p->files) {
2391 spin_lock(&p->files->file_lock);
2392 for (i=0; i < p->files->max_fds; i++) {
2393 filp = fcheck_files(p->files, i);
2394 if (!filp)
2395 continue;
2396 if (filp->f_op->read == tty_read &&
2397 filp->private_data == tty) {
2398 printk(KERN_NOTICE "SAK: killed process %d"
2399 " (%s): fd#%d opened to the tty\n",
2400 p->pid, p->comm, i);
2401 send_sig(SIGKILL, p, 1);
2402 break;
2405 spin_unlock(&p->files->file_lock);
2407 task_unlock(p);
2408 } while_each_task_pid(session, PIDTYPE_SID, p);
2409 read_unlock(&tasklist_lock);
2410 #endif
2413 /*
2414 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2415 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2416 * the values which we write to it will be identical to the values which it
2417 * already has. --akpm
2418 */
2419 void do_SAK(struct tty_struct *tty)
2421 if (!tty)
2422 return;
2423 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
2424 schedule_work(&tty->SAK_work);
2427 EXPORT_SYMBOL(do_SAK);
2429 /*
2430 * This routine is called out of the software interrupt to flush data
2431 * from the flip buffer to the line discipline.
2432 */
2434 static void flush_to_ldisc(void *private_)
2436 struct tty_struct *tty = (struct tty_struct *) private_;
2437 unsigned char *cp;
2438 char *fp;
2439 int count;
2440 unsigned long flags;
2441 struct tty_ldisc *disc;
2443 disc = tty_ldisc_ref(tty);
2444 if (disc == NULL) /* !TTY_LDISC */
2445 return;
2447 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
2448 /*
2449 * Do it after the next timer tick:
2450 */
2451 schedule_delayed_work(&tty->flip.work, 1);
2452 goto out;
2454 spin_lock_irqsave(&tty->read_lock, flags);
2455 if (tty->flip.buf_num) {
2456 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2457 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2458 tty->flip.buf_num = 0;
2459 tty->flip.char_buf_ptr = tty->flip.char_buf;
2460 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2461 } else {
2462 cp = tty->flip.char_buf;
2463 fp = tty->flip.flag_buf;
2464 tty->flip.buf_num = 1;
2465 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2466 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2468 count = tty->flip.count;
2469 tty->flip.count = 0;
2470 spin_unlock_irqrestore(&tty->read_lock, flags);
2472 disc->receive_buf(tty, cp, fp, count);
2473 out:
2474 tty_ldisc_deref(disc);
2477 /*
2478 * Call the ldisc flush directly from a driver. This function may
2479 * return an error and need retrying by the user.
2480 */
2482 int tty_push_data(struct tty_struct *tty, unsigned char *cp, unsigned char *fp, int count)
2484 int ret = 0;
2485 struct tty_ldisc *disc;
2487 disc = tty_ldisc_ref(tty);
2488 if(test_bit(TTY_DONT_FLIP, &tty->flags))
2489 ret = -EAGAIN;
2490 else if(disc == NULL)
2491 ret = -EIO;
2492 else
2493 disc->receive_buf(tty, cp, fp, count);
2494 tty_ldisc_deref(disc);
2495 return ret;
2499 /*
2500 * Routine which returns the baud rate of the tty
2502 * Note that the baud_table needs to be kept in sync with the
2503 * include/asm/termbits.h file.
2504 */
2505 static int baud_table[] = {
2506 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
2507 9600, 19200, 38400, 57600, 115200, 230400, 460800,
2508 #ifdef __sparc__
2509 76800, 153600, 307200, 614400, 921600
2510 #else
2511 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2512 2500000, 3000000, 3500000, 4000000
2513 #endif
2514 };
2516 static int n_baud_table = ARRAY_SIZE(baud_table);
2518 /**
2519 * tty_termios_baud_rate
2520 * @termios: termios structure
2522 * Convert termios baud rate data into a speed. This should be called
2523 * with the termios lock held if this termios is a terminal termios
2524 * structure. May change the termios data.
2525 */
2527 int tty_termios_baud_rate(struct termios *termios)
2529 unsigned int cbaud;
2531 cbaud = termios->c_cflag & CBAUD;
2533 if (cbaud & CBAUDEX) {
2534 cbaud &= ~CBAUDEX;
2536 if (cbaud < 1 || cbaud + 15 > n_baud_table)
2537 termios->c_cflag &= ~CBAUDEX;
2538 else
2539 cbaud += 15;
2541 return baud_table[cbaud];
2544 EXPORT_SYMBOL(tty_termios_baud_rate);
2546 /**
2547 * tty_get_baud_rate - get tty bit rates
2548 * @tty: tty to query
2550 * Returns the baud rate as an integer for this terminal. The
2551 * termios lock must be held by the caller and the terminal bit
2552 * flags may be updated.
2553 */
2555 int tty_get_baud_rate(struct tty_struct *tty)
2557 int baud = tty_termios_baud_rate(tty->termios);
2559 if (baud == 38400 && tty->alt_speed) {
2560 if (!tty->warned) {
2561 printk(KERN_WARNING "Use of setserial/setrocket to "
2562 "set SPD_* flags is deprecated\n");
2563 tty->warned = 1;
2565 baud = tty->alt_speed;
2568 return baud;
2571 EXPORT_SYMBOL(tty_get_baud_rate);
2573 /**
2574 * tty_flip_buffer_push - terminal
2575 * @tty: tty to push
2577 * Queue a push of the terminal flip buffers to the line discipline. This
2578 * function must not be called from IRQ context if tty->low_latency is set.
2580 * In the event of the queue being busy for flipping the work will be
2581 * held off and retried later.
2582 */
2584 void tty_flip_buffer_push(struct tty_struct *tty)
2586 if (tty->low_latency)
2587 flush_to_ldisc((void *) tty);
2588 else
2589 schedule_delayed_work(&tty->flip.work, 1);
2592 EXPORT_SYMBOL(tty_flip_buffer_push);
2594 /*
2595 * This subroutine initializes a tty structure.
2596 */
2597 static void initialize_tty_struct(struct tty_struct *tty)
2599 memset(tty, 0, sizeof(struct tty_struct));
2600 tty->magic = TTY_MAGIC;
2601 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2602 tty->pgrp = -1;
2603 tty->flip.char_buf_ptr = tty->flip.char_buf;
2604 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2605 INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
2606 init_MUTEX(&tty->flip.pty_sem);
2607 init_MUTEX(&tty->termios_sem);
2608 init_waitqueue_head(&tty->write_wait);
2609 init_waitqueue_head(&tty->read_wait);
2610 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
2611 sema_init(&tty->atomic_read, 1);
2612 sema_init(&tty->atomic_write, 1);
2613 spin_lock_init(&tty->read_lock);
2614 INIT_LIST_HEAD(&tty->tty_files);
2615 INIT_WORK(&tty->SAK_work, NULL, NULL);
2618 /*
2619 * The default put_char routine if the driver did not define one.
2620 */
2621 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2623 tty->driver->write(tty, 0, &ch, 1);
2626 static struct class_simple *tty_class;
2628 /**
2629 * tty_register_device - register a tty device
2630 * @driver: the tty driver that describes the tty device
2631 * @index: the index in the tty driver for this tty device
2632 * @device: a struct device that is associated with this tty device.
2633 * This field is optional, if there is no known struct device for this
2634 * tty device it can be set to NULL safely.
2636 * This call is required to be made to register an individual tty device if
2637 * the tty driver's flags have the TTY_DRIVER_NO_DEVFS bit set. If that
2638 * bit is not set, this function should not be called.
2639 */
2640 void tty_register_device(struct tty_driver *driver, unsigned index,
2641 struct device *device)
2643 char name[64];
2644 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2646 if (index >= driver->num) {
2647 printk(KERN_ERR "Attempt to register invalid tty line number "
2648 " (%d).\n", index);
2649 return;
2652 devfs_mk_cdev(dev, S_IFCHR | S_IRUSR | S_IWUSR,
2653 "%s%d", driver->devfs_name, index + driver->name_base);
2655 if (driver->type == TTY_DRIVER_TYPE_PTY)
2656 pty_line_name(driver, index, name);
2657 else
2658 tty_line_name(driver, index, name);
2659 class_simple_device_add(tty_class, dev, device, name);
2662 /**
2663 * tty_unregister_device - unregister a tty device
2664 * @driver: the tty driver that describes the tty device
2665 * @index: the index in the tty driver for this tty device
2667 * If a tty device is registered with a call to tty_register_device() then
2668 * this function must be made when the tty device is gone.
2669 */
2670 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2672 devfs_remove("%s%d", driver->devfs_name, index + driver->name_base);
2673 class_simple_device_remove(MKDEV(driver->major, driver->minor_start) + index);
2676 EXPORT_SYMBOL(tty_register_device);
2677 EXPORT_SYMBOL(tty_unregister_device);
2679 struct tty_driver *alloc_tty_driver(int lines)
2681 struct tty_driver *driver;
2683 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
2684 if (driver) {
2685 memset(driver, 0, sizeof(struct tty_driver));
2686 driver->magic = TTY_DRIVER_MAGIC;
2687 driver->num = lines;
2688 /* later we'll move allocation of tables here */
2690 return driver;
2693 void put_tty_driver(struct tty_driver *driver)
2695 kfree(driver);
2698 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
2700 driver->open = op->open;
2701 driver->close = op->close;
2702 driver->write = op->write;
2703 driver->put_char = op->put_char;
2704 driver->flush_chars = op->flush_chars;
2705 driver->write_room = op->write_room;
2706 driver->chars_in_buffer = op->chars_in_buffer;
2707 driver->ioctl = op->ioctl;
2708 driver->set_termios = op->set_termios;
2709 driver->throttle = op->throttle;
2710 driver->unthrottle = op->unthrottle;
2711 driver->stop = op->stop;
2712 driver->start = op->start;
2713 driver->hangup = op->hangup;
2714 driver->break_ctl = op->break_ctl;
2715 driver->flush_buffer = op->flush_buffer;
2716 driver->set_ldisc = op->set_ldisc;
2717 driver->wait_until_sent = op->wait_until_sent;
2718 driver->send_xchar = op->send_xchar;
2719 driver->read_proc = op->read_proc;
2720 driver->write_proc = op->write_proc;
2721 driver->tiocmget = op->tiocmget;
2722 driver->tiocmset = op->tiocmset;
2726 EXPORT_SYMBOL(alloc_tty_driver);
2727 EXPORT_SYMBOL(put_tty_driver);
2728 EXPORT_SYMBOL(tty_set_operations);
2730 /*
2731 * Called by a tty driver to register itself.
2732 */
2733 int tty_register_driver(struct tty_driver *driver)
2735 int error;
2736 int i;
2737 dev_t dev;
2738 void **p = NULL;
2740 if (driver->flags & TTY_DRIVER_INSTALLED)
2741 return 0;
2743 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2744 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
2745 if (!p)
2746 return -ENOMEM;
2747 memset(p, 0, driver->num * 3 * sizeof(void *));
2750 if (!driver->major) {
2751 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
2752 (char*)driver->name);
2753 if (!error) {
2754 driver->major = MAJOR(dev);
2755 driver->minor_start = MINOR(dev);
2757 } else {
2758 dev = MKDEV(driver->major, driver->minor_start);
2759 error = register_chrdev_region(dev, driver->num,
2760 (char*)driver->name);
2762 if (error < 0) {
2763 kfree(p);
2764 return error;
2767 if (p) {
2768 driver->ttys = (struct tty_struct **)p;
2769 driver->termios = (struct termios **)(p + driver->num);
2770 driver->termios_locked = (struct termios **)(p + driver->num * 2);
2771 } else {
2772 driver->ttys = NULL;
2773 driver->termios = NULL;
2774 driver->termios_locked = NULL;
2777 cdev_init(&driver->cdev, &tty_fops);
2778 driver->cdev.owner = driver->owner;
2779 error = cdev_add(&driver->cdev, dev, driver->num);
2780 if (error) {
2781 cdev_del(&driver->cdev);
2782 unregister_chrdev_region(dev, driver->num);
2783 driver->ttys = NULL;
2784 driver->termios = driver->termios_locked = NULL;
2785 kfree(p);
2786 return error;
2789 if (!driver->put_char)
2790 driver->put_char = tty_default_put_char;
2792 list_add(&driver->tty_drivers, &tty_drivers);
2794 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2795 for(i = 0; i < driver->num; i++)
2796 tty_register_device(driver, i, NULL);
2798 proc_tty_register_driver(driver);
2799 return 0;
2802 EXPORT_SYMBOL(tty_register_driver);
2804 /*
2805 * Called by a tty driver to unregister itself.
2806 */
2807 int tty_unregister_driver(struct tty_driver *driver)
2809 int i;
2810 struct termios *tp;
2811 void *p;
2813 if (driver->refcount)
2814 return -EBUSY;
2816 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
2817 driver->num);
2819 list_del(&driver->tty_drivers);
2821 /*
2822 * Free the termios and termios_locked structures because
2823 * we don't want to get memory leaks when modular tty
2824 * drivers are removed from the kernel.
2825 */
2826 for (i = 0; i < driver->num; i++) {
2827 tp = driver->termios[i];
2828 if (tp) {
2829 driver->termios[i] = NULL;
2830 kfree(tp);
2832 tp = driver->termios_locked[i];
2833 if (tp) {
2834 driver->termios_locked[i] = NULL;
2835 kfree(tp);
2837 if (!(driver->flags & TTY_DRIVER_NO_DEVFS))
2838 tty_unregister_device(driver, i);
2840 p = driver->ttys;
2841 proc_tty_unregister_driver(driver);
2842 driver->ttys = NULL;
2843 driver->termios = driver->termios_locked = NULL;
2844 kfree(p);
2845 cdev_del(&driver->cdev);
2846 return 0;
2849 EXPORT_SYMBOL(tty_unregister_driver);
2852 /*
2853 * Initialize the console device. This is called *early*, so
2854 * we can't necessarily depend on lots of kernel help here.
2855 * Just do some early initializations, and do the complex setup
2856 * later.
2857 */
2858 void __init console_init(void)
2860 initcall_t *call;
2862 /* Setup the default TTY line discipline. */
2863 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2865 /*
2866 * set up the console device so that later boot sequences can
2867 * inform about problems etc..
2868 */
2869 #ifdef CONFIG_EARLY_PRINTK
2870 disable_early_printk();
2871 #endif
2872 #ifdef CONFIG_SERIAL_68360
2873 /* This is not a console initcall. I know not what it's doing here.
2874 So I haven't moved it. dwmw2 */
2875 rs_360_init();
2876 #endif
2877 call = &__con_initcall_start;
2878 while (call < &__con_initcall_end) {
2879 (*call)();
2880 call++;
2884 #ifdef CONFIG_VT
2885 extern int vty_init(void);
2886 #endif
2888 static int __init tty_class_init(void)
2890 tty_class = class_simple_create(THIS_MODULE, "tty");
2891 if (IS_ERR(tty_class))
2892 return PTR_ERR(tty_class);
2893 return 0;
2896 postcore_initcall(tty_class_init);
2898 /* 3/2004 jmc: why do these devices exist? */
2900 static struct cdev tty_cdev, console_cdev;
2901 #ifdef CONFIG_UNIX98_PTYS
2902 static struct cdev ptmx_cdev;
2903 #endif
2904 #ifdef CONFIG_VT
2905 static struct cdev vc0_cdev;
2906 #endif
2908 /*
2909 * Ok, now we can initialize the rest of the tty devices and can count
2910 * on memory allocations, interrupts etc..
2911 */
2912 static int __init tty_init(void)
2914 cdev_init(&tty_cdev, &tty_fops);
2915 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
2916 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
2917 panic("Couldn't register /dev/tty driver\n");
2918 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 0), S_IFCHR|S_IRUGO|S_IWUGO, "tty");
2919 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
2921 cdev_init(&console_cdev, &console_fops);
2922 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
2923 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
2924 panic("Couldn't register /dev/console driver\n");
2925 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 1), S_IFCHR|S_IRUSR|S_IWUSR, "console");
2926 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
2928 #ifdef CONFIG_UNIX98_PTYS
2929 cdev_init(&ptmx_cdev, &ptmx_fops);
2930 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
2931 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
2932 panic("Couldn't register /dev/ptmx driver\n");
2933 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 2), S_IFCHR|S_IRUGO|S_IWUGO, "ptmx");
2934 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
2935 #endif
2937 #ifdef CONFIG_VT
2938 if (console_use_vt) {
2939 cdev_init(&vc0_cdev, &console_fops);
2940 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2941 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1,
2942 "/dev/vc/0") < 0)
2943 panic("Couldn't register /dev/tty0 driver\n");
2944 devfs_mk_cdev(MKDEV(TTY_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR,
2945 "vc/0");
2946 class_simple_device_add(tty_class, MKDEV(TTY_MAJOR, 0), NULL,
2947 "tty0");
2949 vty_init();
2951 #endif
2952 return 0;
2954 module_init(tty_init);