direct-io.hg

view linux-2.6-xen-sparse/drivers/char/tty_io.c @ 7547:03612b7f69d5

Don't use XendRoot.get_vif_bridge to provide a default bridge -- instead, pass
no bridge parameter and let the vif-bridge script find one for itself.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Sun Oct 30 13:45:06 2005 +0100 (2005-10-30)
parents 06d84bf87159
children fd9b2c1bb577
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>
96 #include <linux/bitops.h>
98 #include <asm/uaccess.h>
99 #include <asm/system.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 static int ptmx_open(struct inode *, struct file *);
142 #endif
144 extern void disable_early_printk(void);
146 static void initialize_tty_struct(struct tty_struct *tty);
148 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
149 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
150 ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
151 static unsigned int tty_poll(struct file *, poll_table *);
152 static int tty_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->write_buf);
174 kfree(tty);
175 }
177 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
179 char *tty_name(struct tty_struct *tty, char *buf)
180 {
181 if (!tty) /* Hmm. NULL pointer. That's fun. */
182 strcpy(buf, "NULL tty");
183 else
184 strcpy(buf, tty->name);
185 return buf;
186 }
188 EXPORT_SYMBOL(tty_name);
190 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
191 const char *routine)
192 {
193 #ifdef TTY_PARANOIA_CHECK
194 if (!tty) {
195 printk(KERN_WARNING
196 "null TTY for (%d:%d) in %s\n",
197 imajor(inode), iminor(inode), routine);
198 return 1;
199 }
200 if (tty->magic != TTY_MAGIC) {
201 printk(KERN_WARNING
202 "bad magic number for tty struct (%d:%d) in %s\n",
203 imajor(inode), iminor(inode), routine);
204 return 1;
205 }
206 #endif
207 return 0;
208 }
210 static int check_tty_count(struct tty_struct *tty, const char *routine)
211 {
212 #ifdef CHECK_TTY_COUNT
213 struct list_head *p;
214 int count = 0;
216 file_list_lock();
217 list_for_each(p, &tty->tty_files) {
218 count++;
219 }
220 file_list_unlock();
221 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
222 tty->driver->subtype == PTY_TYPE_SLAVE &&
223 tty->link && tty->link->count)
224 count++;
225 if (tty->count != count) {
226 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
227 "!= #fd's(%d) in %s\n",
228 tty->name, tty->count, count, routine);
229 return count;
230 }
231 #endif
232 return 0;
233 }
235 /*
236 * This is probably overkill for real world processors but
237 * they are not on hot paths so a little discipline won't do
238 * any harm.
239 */
241 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
242 {
243 down(&tty->termios_sem);
244 tty->termios->c_line = num;
245 up(&tty->termios_sem);
246 }
248 /*
249 * This guards the refcounted line discipline lists. The lock
250 * must be taken with irqs off because there are hangup path
251 * callers who will do ldisc lookups and cannot sleep.
252 */
254 static DEFINE_SPINLOCK(tty_ldisc_lock);
255 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
256 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
258 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
259 {
260 unsigned long flags;
261 int ret = 0;
263 if (disc < N_TTY || disc >= NR_LDISCS)
264 return -EINVAL;
266 spin_lock_irqsave(&tty_ldisc_lock, flags);
267 if (new_ldisc) {
268 tty_ldiscs[disc] = *new_ldisc;
269 tty_ldiscs[disc].num = disc;
270 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
271 tty_ldiscs[disc].refcount = 0;
272 } else {
273 if(tty_ldiscs[disc].refcount)
274 ret = -EBUSY;
275 else
276 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
277 }
278 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
280 return ret;
281 }
283 EXPORT_SYMBOL(tty_register_ldisc);
285 struct tty_ldisc *tty_ldisc_get(int disc)
286 {
287 unsigned long flags;
288 struct tty_ldisc *ld;
290 if (disc < N_TTY || disc >= NR_LDISCS)
291 return NULL;
293 spin_lock_irqsave(&tty_ldisc_lock, flags);
295 ld = &tty_ldiscs[disc];
296 /* Check the entry is defined */
297 if(ld->flags & LDISC_FLAG_DEFINED)
298 {
299 /* If the module is being unloaded we can't use it */
300 if (!try_module_get(ld->owner))
301 ld = NULL;
302 else /* lock it */
303 ld->refcount++;
304 }
305 else
306 ld = NULL;
307 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
308 return ld;
309 }
311 EXPORT_SYMBOL_GPL(tty_ldisc_get);
313 void tty_ldisc_put(int disc)
314 {
315 struct tty_ldisc *ld;
316 unsigned long flags;
318 if (disc < N_TTY || disc >= NR_LDISCS)
319 BUG();
321 spin_lock_irqsave(&tty_ldisc_lock, flags);
322 ld = &tty_ldiscs[disc];
323 if(ld->refcount == 0)
324 BUG();
325 ld->refcount --;
326 module_put(ld->owner);
327 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
328 }
330 EXPORT_SYMBOL_GPL(tty_ldisc_put);
332 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
333 {
334 tty->ldisc = *ld;
335 tty->ldisc.refcount = 0;
336 }
338 /**
339 * tty_ldisc_try - internal helper
340 * @tty: the tty
341 *
342 * Make a single attempt to grab and bump the refcount on
343 * the tty ldisc. Return 0 on failure or 1 on success. This is
344 * used to implement both the waiting and non waiting versions
345 * of tty_ldisc_ref
346 */
348 static int tty_ldisc_try(struct tty_struct *tty)
349 {
350 unsigned long flags;
351 struct tty_ldisc *ld;
352 int ret = 0;
354 spin_lock_irqsave(&tty_ldisc_lock, flags);
355 ld = &tty->ldisc;
356 if(test_bit(TTY_LDISC, &tty->flags))
357 {
358 ld->refcount++;
359 ret = 1;
360 }
361 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
362 return ret;
363 }
365 /**
366 * tty_ldisc_ref_wait - wait for the tty ldisc
367 * @tty: tty device
368 *
369 * Dereference the line discipline for the terminal and take a
370 * reference to it. If the line discipline is in flux then
371 * wait patiently until it changes.
372 *
373 * Note: Must not be called from an IRQ/timer context. The caller
374 * must also be careful not to hold other locks that will deadlock
375 * against a discipline change, such as an existing ldisc reference
376 * (which we check for)
377 */
379 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
380 {
381 /* wait_event is a macro */
382 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
383 if(tty->ldisc.refcount == 0)
384 printk(KERN_ERR "tty_ldisc_ref_wait\n");
385 return &tty->ldisc;
386 }
388 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
390 /**
391 * tty_ldisc_ref - get the tty ldisc
392 * @tty: tty device
393 *
394 * Dereference the line discipline for the terminal and take a
395 * reference to it. If the line discipline is in flux then
396 * return NULL. Can be called from IRQ and timer functions.
397 */
399 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
400 {
401 if(tty_ldisc_try(tty))
402 return &tty->ldisc;
403 return NULL;
404 }
406 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
408 /**
409 * tty_ldisc_deref - free a tty ldisc reference
410 * @ld: reference to free up
411 *
412 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
413 * be called in IRQ context.
414 */
416 void tty_ldisc_deref(struct tty_ldisc *ld)
417 {
418 unsigned long flags;
420 if(ld == NULL)
421 BUG();
423 spin_lock_irqsave(&tty_ldisc_lock, flags);
424 if(ld->refcount == 0)
425 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
426 else
427 ld->refcount--;
428 if(ld->refcount == 0)
429 wake_up(&tty_ldisc_wait);
430 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
431 }
433 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
435 /**
436 * tty_ldisc_enable - allow ldisc use
437 * @tty: terminal to activate ldisc on
438 *
439 * Set the TTY_LDISC flag when the line discipline can be called
440 * again. Do neccessary wakeups for existing sleepers.
441 *
442 * Note: nobody should set this bit except via this function. Clearing
443 * directly is allowed.
444 */
446 static void tty_ldisc_enable(struct tty_struct *tty)
447 {
448 set_bit(TTY_LDISC, &tty->flags);
449 wake_up(&tty_ldisc_wait);
450 }
452 /**
453 * tty_set_ldisc - set line discipline
454 * @tty: the terminal to set
455 * @ldisc: the line discipline
456 *
457 * Set the discipline of a tty line. Must be called from a process
458 * context.
459 */
461 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
462 {
463 int retval = 0;
464 struct tty_ldisc o_ldisc;
465 char buf[64];
466 int work;
467 unsigned long flags;
468 struct tty_ldisc *ld;
470 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
471 return -EINVAL;
473 restart:
475 if (tty->ldisc.num == ldisc)
476 return 0; /* We are already in the desired discipline */
478 ld = tty_ldisc_get(ldisc);
479 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
480 /* Cyrus Durgin <cider@speakeasy.org> */
481 if (ld == NULL) {
482 request_module("tty-ldisc-%d", ldisc);
483 ld = tty_ldisc_get(ldisc);
484 }
485 if (ld == NULL)
486 return -EINVAL;
488 o_ldisc = tty->ldisc;
490 tty_wait_until_sent(tty, 0);
492 /*
493 * Make sure we don't change while someone holds a
494 * reference to the line discipline. The TTY_LDISC bit
495 * prevents anyone taking a reference once it is clear.
496 * We need the lock to avoid racing reference takers.
497 */
499 spin_lock_irqsave(&tty_ldisc_lock, flags);
500 if(tty->ldisc.refcount)
501 {
502 /* Free the new ldisc we grabbed. Must drop the lock
503 first. */
504 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
505 tty_ldisc_put(ldisc);
506 /*
507 * There are several reasons we may be busy, including
508 * random momentary I/O traffic. We must therefore
509 * retry. We could distinguish between blocking ops
510 * and retries if we made tty_ldisc_wait() smarter. That
511 * is up for discussion.
512 */
513 if(wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
514 return -ERESTARTSYS;
515 goto restart;
516 }
517 clear_bit(TTY_LDISC, &tty->flags);
518 clear_bit(TTY_DONT_FLIP, &tty->flags);
519 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
521 /*
522 * From this point on we know nobody has an ldisc
523 * usage reference, nor can they obtain one until
524 * we say so later on.
525 */
527 work = cancel_delayed_work(&tty->flip.work);
528 /*
529 * Wait for ->hangup_work and ->flip.work handlers to terminate
530 */
532 flush_scheduled_work();
533 /* Shutdown the current discipline. */
534 if (tty->ldisc.close)
535 (tty->ldisc.close)(tty);
537 /* Now set up the new line discipline. */
538 tty_ldisc_assign(tty, ld);
539 tty_set_termios_ldisc(tty, ldisc);
540 if (tty->ldisc.open)
541 retval = (tty->ldisc.open)(tty);
542 if (retval < 0) {
543 tty_ldisc_put(ldisc);
544 /* There is an outstanding reference here so this is safe */
545 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
546 tty_set_termios_ldisc(tty, tty->ldisc.num);
547 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
548 tty_ldisc_put(o_ldisc.num);
549 /* This driver is always present */
550 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
551 tty_set_termios_ldisc(tty, N_TTY);
552 if (tty->ldisc.open) {
553 int r = tty->ldisc.open(tty);
555 if (r < 0)
556 panic("Couldn't open N_TTY ldisc for "
557 "%s --- error %d.",
558 tty_name(tty, buf), r);
559 }
560 }
561 }
562 /* At this point we hold a reference to the new ldisc and a
563 a reference to the old ldisc. If we ended up flipping back
564 to the existing ldisc we have two references to it */
566 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
567 tty->driver->set_ldisc(tty);
569 tty_ldisc_put(o_ldisc.num);
571 /*
572 * Allow ldisc referencing to occur as soon as the driver
573 * ldisc callback completes.
574 */
576 tty_ldisc_enable(tty);
578 /* Restart it in case no characters kick it off. Safe if
579 already running */
580 if(work)
581 schedule_delayed_work(&tty->flip.work, 1);
582 return retval;
583 }
585 /*
586 * This routine returns a tty driver structure, given a device number
587 */
588 static struct tty_driver *get_tty_driver(dev_t device, int *index)
589 {
590 struct tty_driver *p;
592 list_for_each_entry(p, &tty_drivers, tty_drivers) {
593 dev_t base = MKDEV(p->major, p->minor_start);
594 if (device < base || device >= base + p->num)
595 continue;
596 *index = device - base;
597 return p;
598 }
599 return NULL;
600 }
602 /*
603 * If we try to write to, or set the state of, a terminal and we're
604 * not in the foreground, send a SIGTTOU. If the signal is blocked or
605 * ignored, go ahead and perform the operation. (POSIX 7.2)
606 */
607 int tty_check_change(struct tty_struct * tty)
608 {
609 if (current->signal->tty != tty)
610 return 0;
611 if (tty->pgrp <= 0) {
612 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
613 return 0;
614 }
615 if (process_group(current) == tty->pgrp)
616 return 0;
617 if (is_ignored(SIGTTOU))
618 return 0;
619 if (is_orphaned_pgrp(process_group(current)))
620 return -EIO;
621 (void) kill_pg(process_group(current), SIGTTOU, 1);
622 return -ERESTARTSYS;
623 }
625 EXPORT_SYMBOL(tty_check_change);
627 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
628 size_t count, loff_t *ppos)
629 {
630 return 0;
631 }
633 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
634 size_t count, loff_t *ppos)
635 {
636 return -EIO;
637 }
639 /* No kernel lock held - none needed ;) */
640 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
641 {
642 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
643 }
645 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
646 unsigned int cmd, unsigned long arg)
647 {
648 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
649 }
651 static struct file_operations tty_fops = {
652 .llseek = no_llseek,
653 .read = tty_read,
654 .write = tty_write,
655 .poll = tty_poll,
656 .ioctl = tty_ioctl,
657 .open = tty_open,
658 .release = tty_release,
659 .fasync = tty_fasync,
660 };
662 #ifdef CONFIG_UNIX98_PTYS
663 static struct file_operations ptmx_fops = {
664 .llseek = no_llseek,
665 .read = tty_read,
666 .write = tty_write,
667 .poll = tty_poll,
668 .ioctl = tty_ioctl,
669 .open = ptmx_open,
670 .release = tty_release,
671 .fasync = tty_fasync,
672 };
673 #endif
675 static struct file_operations console_fops = {
676 .llseek = no_llseek,
677 .read = tty_read,
678 .write = redirected_tty_write,
679 .poll = tty_poll,
680 .ioctl = tty_ioctl,
681 .open = tty_open,
682 .release = tty_release,
683 .fasync = tty_fasync,
684 };
686 static struct file_operations hung_up_tty_fops = {
687 .llseek = no_llseek,
688 .read = hung_up_tty_read,
689 .write = hung_up_tty_write,
690 .poll = hung_up_tty_poll,
691 .ioctl = hung_up_tty_ioctl,
692 .release = tty_release,
693 };
695 static DEFINE_SPINLOCK(redirect_lock);
696 static struct file *redirect;
698 /**
699 * tty_wakeup - request more data
700 * @tty: terminal
701 *
702 * Internal and external helper for wakeups of tty. This function
703 * informs the line discipline if present that the driver is ready
704 * to receive more output data.
705 */
707 void tty_wakeup(struct tty_struct *tty)
708 {
709 struct tty_ldisc *ld;
711 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
712 ld = tty_ldisc_ref(tty);
713 if(ld) {
714 if(ld->write_wakeup)
715 ld->write_wakeup(tty);
716 tty_ldisc_deref(ld);
717 }
718 }
719 wake_up_interruptible(&tty->write_wait);
720 }
722 EXPORT_SYMBOL_GPL(tty_wakeup);
724 /**
725 * tty_ldisc_flush - flush line discipline queue
726 * @tty: tty
727 *
728 * Flush the line discipline queue (if any) for this tty. If there
729 * is no line discipline active this is a no-op.
730 */
732 void tty_ldisc_flush(struct tty_struct *tty)
733 {
734 struct tty_ldisc *ld = tty_ldisc_ref(tty);
735 if(ld) {
736 if(ld->flush_buffer)
737 ld->flush_buffer(tty);
738 tty_ldisc_deref(ld);
739 }
740 }
742 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
744 /*
745 * This can be called by the "eventd" kernel thread. That is process synchronous,
746 * but doesn't hold any locks, so we need to make sure we have the appropriate
747 * locks for what we're doing..
748 */
749 static void do_tty_hangup(void *data)
750 {
751 struct tty_struct *tty = (struct tty_struct *) data;
752 struct file * cons_filp = NULL;
753 struct file *filp, *f = NULL;
754 struct task_struct *p;
755 struct tty_ldisc *ld;
756 int closecount = 0, n;
758 if (!tty)
759 return;
761 /* inuse_filps is protected by the single kernel lock */
762 lock_kernel();
764 spin_lock(&redirect_lock);
765 if (redirect && redirect->private_data == tty) {
766 f = redirect;
767 redirect = NULL;
768 }
769 spin_unlock(&redirect_lock);
771 check_tty_count(tty, "do_tty_hangup");
772 file_list_lock();
773 /* This breaks for file handles being sent over AF_UNIX sockets ? */
774 list_for_each_entry(filp, &tty->tty_files, f_list) {
775 if (filp->f_op->write == redirected_tty_write)
776 cons_filp = filp;
777 if (filp->f_op->write != tty_write)
778 continue;
779 closecount++;
780 tty_fasync(-1, filp, 0); /* can't block */
781 filp->f_op = &hung_up_tty_fops;
782 }
783 file_list_unlock();
785 /* FIXME! What are the locking issues here? This may me overdoing things..
786 * this question is especially important now that we've removed the irqlock. */
788 ld = tty_ldisc_ref(tty);
789 if(ld != NULL) /* We may have no line discipline at this point */
790 {
791 if (ld->flush_buffer)
792 ld->flush_buffer(tty);
793 if (tty->driver->flush_buffer)
794 tty->driver->flush_buffer(tty);
795 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
796 ld->write_wakeup)
797 ld->write_wakeup(tty);
798 if (ld->hangup)
799 ld->hangup(tty);
800 }
802 /* FIXME: Once we trust the LDISC code better we can wait here for
803 ldisc completion and fix the driver call race */
805 wake_up_interruptible(&tty->write_wait);
806 wake_up_interruptible(&tty->read_wait);
808 /*
809 * Shutdown the current line discipline, and reset it to
810 * N_TTY.
811 */
812 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
813 {
814 down(&tty->termios_sem);
815 *tty->termios = tty->driver->init_termios;
816 up(&tty->termios_sem);
817 }
819 /* Defer ldisc switch */
820 /* tty_deferred_ldisc_switch(N_TTY);
822 This should get done automatically when the port closes and
823 tty_release is called */
825 read_lock(&tasklist_lock);
826 if (tty->session > 0) {
827 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
828 if (p->signal->tty == tty)
829 p->signal->tty = NULL;
830 if (!p->signal->leader)
831 continue;
832 send_group_sig_info(SIGHUP, SEND_SIG_PRIV, p);
833 send_group_sig_info(SIGCONT, SEND_SIG_PRIV, p);
834 if (tty->pgrp > 0)
835 p->signal->tty_old_pgrp = tty->pgrp;
836 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
837 }
838 read_unlock(&tasklist_lock);
840 tty->flags = 0;
841 tty->session = 0;
842 tty->pgrp = -1;
843 tty->ctrl_status = 0;
844 /*
845 * If one of the devices matches a console pointer, we
846 * cannot just call hangup() because that will cause
847 * tty->count and state->count to go out of sync.
848 * So we just call close() the right number of times.
849 */
850 if (cons_filp) {
851 if (tty->driver->close)
852 for (n = 0; n < closecount; n++)
853 tty->driver->close(tty, cons_filp);
854 } else if (tty->driver->hangup)
855 (tty->driver->hangup)(tty);
857 /* We don't want to have driver/ldisc interactions beyond
858 the ones we did here. The driver layer expects no
859 calls after ->hangup() from the ldisc side. However we
860 can't yet guarantee all that */
862 set_bit(TTY_HUPPED, &tty->flags);
863 if (ld) {
864 tty_ldisc_enable(tty);
865 tty_ldisc_deref(ld);
866 }
867 unlock_kernel();
868 if (f)
869 fput(f);
870 }
872 void tty_hangup(struct tty_struct * tty)
873 {
874 #ifdef TTY_DEBUG_HANGUP
875 char buf[64];
877 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
878 #endif
879 schedule_work(&tty->hangup_work);
880 }
882 EXPORT_SYMBOL(tty_hangup);
884 void tty_vhangup(struct tty_struct * tty)
885 {
886 #ifdef TTY_DEBUG_HANGUP
887 char buf[64];
889 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
890 #endif
891 do_tty_hangup((void *) tty);
892 }
893 EXPORT_SYMBOL(tty_vhangup);
895 int tty_hung_up_p(struct file * filp)
896 {
897 return (filp->f_op == &hung_up_tty_fops);
898 }
900 EXPORT_SYMBOL(tty_hung_up_p);
902 /*
903 * This function is typically called only by the session leader, when
904 * it wants to disassociate itself from its controlling tty.
905 *
906 * It performs the following functions:
907 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
908 * (2) Clears the tty from being controlling the session
909 * (3) Clears the controlling tty for all processes in the
910 * session group.
911 *
912 * The argument on_exit is set to 1 if called when a process is
913 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
914 */
915 void disassociate_ctty(int on_exit)
916 {
917 struct tty_struct *tty;
918 struct task_struct *p;
919 int tty_pgrp = -1;
921 lock_kernel();
923 down(&tty_sem);
924 tty = current->signal->tty;
925 if (tty) {
926 tty_pgrp = tty->pgrp;
927 up(&tty_sem);
928 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
929 tty_vhangup(tty);
930 } else {
931 if (current->signal->tty_old_pgrp) {
932 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
933 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
934 }
935 up(&tty_sem);
936 unlock_kernel();
937 return;
938 }
939 if (tty_pgrp > 0) {
940 kill_pg(tty_pgrp, SIGHUP, on_exit);
941 if (!on_exit)
942 kill_pg(tty_pgrp, SIGCONT, on_exit);
943 }
945 /* Must lock changes to tty_old_pgrp */
946 down(&tty_sem);
947 current->signal->tty_old_pgrp = 0;
948 tty->session = 0;
949 tty->pgrp = -1;
951 /* Now clear signal->tty under the lock */
952 read_lock(&tasklist_lock);
953 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
954 p->signal->tty = NULL;
955 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
956 read_unlock(&tasklist_lock);
957 up(&tty_sem);
958 unlock_kernel();
959 }
961 void stop_tty(struct tty_struct *tty)
962 {
963 if (tty->stopped)
964 return;
965 tty->stopped = 1;
966 if (tty->link && tty->link->packet) {
967 tty->ctrl_status &= ~TIOCPKT_START;
968 tty->ctrl_status |= TIOCPKT_STOP;
969 wake_up_interruptible(&tty->link->read_wait);
970 }
971 if (tty->driver->stop)
972 (tty->driver->stop)(tty);
973 }
975 EXPORT_SYMBOL(stop_tty);
977 void start_tty(struct tty_struct *tty)
978 {
979 if (!tty->stopped || tty->flow_stopped)
980 return;
981 tty->stopped = 0;
982 if (tty->link && tty->link->packet) {
983 tty->ctrl_status &= ~TIOCPKT_STOP;
984 tty->ctrl_status |= TIOCPKT_START;
985 wake_up_interruptible(&tty->link->read_wait);
986 }
987 if (tty->driver->start)
988 (tty->driver->start)(tty);
990 /* If we have a running line discipline it may need kicking */
991 tty_wakeup(tty);
992 wake_up_interruptible(&tty->write_wait);
993 }
995 EXPORT_SYMBOL(start_tty);
997 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
998 loff_t *ppos)
999 {
1000 int i;
1001 struct tty_struct * tty;
1002 struct inode *inode;
1003 struct tty_ldisc *ld;
1005 tty = (struct tty_struct *)file->private_data;
1006 inode = file->f_dentry->d_inode;
1007 if (tty_paranoia_check(tty, inode, "tty_read"))
1008 return -EIO;
1009 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1010 return -EIO;
1012 /* We want to wait for the line discipline to sort out in this
1013 situation */
1014 ld = tty_ldisc_ref_wait(tty);
1015 lock_kernel();
1016 if (ld->read)
1017 i = (ld->read)(tty,file,buf,count);
1018 else
1019 i = -EIO;
1020 tty_ldisc_deref(ld);
1021 unlock_kernel();
1022 if (i > 0)
1023 inode->i_atime = current_fs_time(inode->i_sb);
1024 return i;
1027 /*
1028 * Split writes up in sane blocksizes to avoid
1029 * denial-of-service type attacks
1030 */
1031 static inline ssize_t do_tty_write(
1032 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1033 struct tty_struct *tty,
1034 struct file *file,
1035 const char __user *buf,
1036 size_t count)
1038 ssize_t ret = 0, written = 0;
1039 unsigned int chunk;
1041 if (down_interruptible(&tty->atomic_write)) {
1042 return -ERESTARTSYS;
1045 /*
1046 * We chunk up writes into a temporary buffer. This
1047 * simplifies low-level drivers immensely, since they
1048 * don't have locking issues and user mode accesses.
1050 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1051 * big chunk-size..
1053 * The default chunk-size is 2kB, because the NTTY
1054 * layer has problems with bigger chunks. It will
1055 * claim to be able to handle more characters than
1056 * it actually does.
1057 */
1058 chunk = 2048;
1059 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1060 chunk = 65536;
1061 if (count < chunk)
1062 chunk = count;
1064 /* write_buf/write_cnt is protected by the atomic_write semaphore */
1065 if (tty->write_cnt < chunk) {
1066 unsigned char *buf;
1068 if (chunk < 1024)
1069 chunk = 1024;
1071 buf = kmalloc(chunk, GFP_KERNEL);
1072 if (!buf) {
1073 up(&tty->atomic_write);
1074 return -ENOMEM;
1076 kfree(tty->write_buf);
1077 tty->write_cnt = chunk;
1078 tty->write_buf = buf;
1081 /* Do the write .. */
1082 for (;;) {
1083 size_t size = count;
1084 if (size > chunk)
1085 size = chunk;
1086 ret = -EFAULT;
1087 if (copy_from_user(tty->write_buf, buf, size))
1088 break;
1089 lock_kernel();
1090 ret = write(tty, file, tty->write_buf, size);
1091 unlock_kernel();
1092 if (ret <= 0)
1093 break;
1094 written += ret;
1095 buf += ret;
1096 count -= ret;
1097 if (!count)
1098 break;
1099 ret = -ERESTARTSYS;
1100 if (signal_pending(current))
1101 break;
1102 cond_resched();
1104 if (written) {
1105 struct inode *inode = file->f_dentry->d_inode;
1106 inode->i_mtime = current_fs_time(inode->i_sb);
1107 ret = written;
1109 up(&tty->atomic_write);
1110 return ret;
1114 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
1115 loff_t *ppos)
1117 struct tty_struct * tty;
1118 struct inode *inode = file->f_dentry->d_inode;
1119 ssize_t ret;
1120 struct tty_ldisc *ld;
1122 tty = (struct tty_struct *)file->private_data;
1123 if (tty_paranoia_check(tty, inode, "tty_write"))
1124 return -EIO;
1125 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1126 return -EIO;
1128 ld = tty_ldisc_ref_wait(tty);
1129 if (!ld->write)
1130 ret = -EIO;
1131 else
1132 ret = do_tty_write(ld->write, tty, file, buf, count);
1133 tty_ldisc_deref(ld);
1134 return ret;
1137 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
1138 loff_t *ppos)
1140 struct file *p = NULL;
1142 spin_lock(&redirect_lock);
1143 if (redirect) {
1144 get_file(redirect);
1145 p = redirect;
1147 spin_unlock(&redirect_lock);
1149 if (p) {
1150 ssize_t res;
1151 res = vfs_write(p, buf, count, &p->f_pos);
1152 fput(p);
1153 return res;
1156 return tty_write(file, buf, count, ppos);
1159 static char ptychar[] = "pqrstuvwxyzabcde";
1161 static inline void pty_line_name(struct tty_driver *driver, int index, char *p)
1163 int i = index + driver->name_base;
1164 /* ->name is initialized to "ttyp", but "tty" is expected */
1165 sprintf(p, "%s%c%x",
1166 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1167 ptychar[i >> 4 & 0xf], i & 0xf);
1170 static inline void tty_line_name(struct tty_driver *driver, int index, char *p)
1172 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1175 /*
1176 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1177 * failed open. The new code protects the open with a semaphore, so it's
1178 * really quite straightforward. The semaphore locking can probably be
1179 * relaxed for the (most common) case of reopening a tty.
1180 */
1181 static int init_dev(struct tty_driver *driver, int idx,
1182 struct tty_struct **ret_tty)
1184 struct tty_struct *tty, *o_tty;
1185 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1186 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1187 int retval=0;
1189 /* check whether we're reopening an existing tty */
1190 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1191 tty = devpts_get_tty(idx);
1192 if (tty && driver->subtype == PTY_TYPE_MASTER)
1193 tty = tty->link;
1194 } else {
1195 tty = driver->ttys[idx];
1197 if (tty) goto fast_track;
1199 /*
1200 * First time open is complex, especially for PTY devices.
1201 * This code guarantees that either everything succeeds and the
1202 * TTY is ready for operation, or else the table slots are vacated
1203 * and the allocated memory released. (Except that the termios
1204 * and locked termios may be retained.)
1205 */
1207 if (!try_module_get(driver->owner)) {
1208 retval = -ENODEV;
1209 goto end_init;
1212 o_tty = NULL;
1213 tp = o_tp = NULL;
1214 ltp = o_ltp = NULL;
1216 tty = alloc_tty_struct();
1217 if(!tty)
1218 goto fail_no_mem;
1219 initialize_tty_struct(tty);
1220 tty->driver = driver;
1221 tty->index = idx;
1222 tty_line_name(driver, idx, tty->name);
1224 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1225 tp_loc = &tty->termios;
1226 ltp_loc = &tty->termios_locked;
1227 } else {
1228 tp_loc = &driver->termios[idx];
1229 ltp_loc = &driver->termios_locked[idx];
1232 if (!*tp_loc) {
1233 tp = (struct termios *) kmalloc(sizeof(struct termios),
1234 GFP_KERNEL);
1235 if (!tp)
1236 goto free_mem_out;
1237 *tp = driver->init_termios;
1240 if (!*ltp_loc) {
1241 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1242 GFP_KERNEL);
1243 if (!ltp)
1244 goto free_mem_out;
1245 memset(ltp, 0, sizeof(struct termios));
1248 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1249 o_tty = alloc_tty_struct();
1250 if (!o_tty)
1251 goto free_mem_out;
1252 initialize_tty_struct(o_tty);
1253 o_tty->driver = driver->other;
1254 o_tty->index = idx;
1255 tty_line_name(driver->other, idx, o_tty->name);
1257 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1258 o_tp_loc = &o_tty->termios;
1259 o_ltp_loc = &o_tty->termios_locked;
1260 } else {
1261 o_tp_loc = &driver->other->termios[idx];
1262 o_ltp_loc = &driver->other->termios_locked[idx];
1265 if (!*o_tp_loc) {
1266 o_tp = (struct termios *)
1267 kmalloc(sizeof(struct termios), GFP_KERNEL);
1268 if (!o_tp)
1269 goto free_mem_out;
1270 *o_tp = driver->other->init_termios;
1273 if (!*o_ltp_loc) {
1274 o_ltp = (struct termios *)
1275 kmalloc(sizeof(struct termios), GFP_KERNEL);
1276 if (!o_ltp)
1277 goto free_mem_out;
1278 memset(o_ltp, 0, sizeof(struct termios));
1281 /*
1282 * Everything allocated ... set up the o_tty structure.
1283 */
1284 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
1285 driver->other->ttys[idx] = o_tty;
1287 if (!*o_tp_loc)
1288 *o_tp_loc = o_tp;
1289 if (!*o_ltp_loc)
1290 *o_ltp_loc = o_ltp;
1291 o_tty->termios = *o_tp_loc;
1292 o_tty->termios_locked = *o_ltp_loc;
1293 driver->other->refcount++;
1294 if (driver->subtype == PTY_TYPE_MASTER)
1295 o_tty->count++;
1297 /* Establish the links in both directions */
1298 tty->link = o_tty;
1299 o_tty->link = tty;
1302 /*
1303 * All structures have been allocated, so now we install them.
1304 * Failures after this point use release_mem to clean up, so
1305 * there's no need to null out the local pointers.
1306 */
1307 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1308 driver->ttys[idx] = tty;
1311 if (!*tp_loc)
1312 *tp_loc = tp;
1313 if (!*ltp_loc)
1314 *ltp_loc = ltp;
1315 tty->termios = *tp_loc;
1316 tty->termios_locked = *ltp_loc;
1317 driver->refcount++;
1318 tty->count++;
1320 /*
1321 * Structures all installed ... call the ldisc open routines.
1322 * If we fail here just call release_mem to clean up. No need
1323 * to decrement the use counts, as release_mem doesn't care.
1324 */
1326 if (tty->ldisc.open) {
1327 retval = (tty->ldisc.open)(tty);
1328 if (retval)
1329 goto release_mem_out;
1331 if (o_tty && o_tty->ldisc.open) {
1332 retval = (o_tty->ldisc.open)(o_tty);
1333 if (retval) {
1334 if (tty->ldisc.close)
1335 (tty->ldisc.close)(tty);
1336 goto release_mem_out;
1338 tty_ldisc_enable(o_tty);
1340 tty_ldisc_enable(tty);
1341 goto success;
1343 /*
1344 * This fast open can be used if the tty is already open.
1345 * No memory is allocated, and the only failures are from
1346 * attempting to open a closing tty or attempting multiple
1347 * opens on a pty master.
1348 */
1349 fast_track:
1350 if (test_bit(TTY_CLOSING, &tty->flags)) {
1351 retval = -EIO;
1352 goto end_init;
1354 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1355 driver->subtype == PTY_TYPE_MASTER) {
1356 /*
1357 * special case for PTY masters: only one open permitted,
1358 * and the slave side open count is incremented as well.
1359 */
1360 if (tty->count) {
1361 retval = -EIO;
1362 goto end_init;
1364 tty->link->count++;
1366 tty->count++;
1367 tty->driver = driver; /* N.B. why do this every time?? */
1369 /* FIXME */
1370 if(!test_bit(TTY_LDISC, &tty->flags))
1371 printk(KERN_ERR "init_dev but no ldisc\n");
1372 success:
1373 *ret_tty = tty;
1375 /* All paths come through here to release the semaphore */
1376 end_init:
1377 return retval;
1379 /* Release locally allocated memory ... nothing placed in slots */
1380 free_mem_out:
1381 if (o_tp)
1382 kfree(o_tp);
1383 if (o_tty)
1384 free_tty_struct(o_tty);
1385 if (ltp)
1386 kfree(ltp);
1387 if (tp)
1388 kfree(tp);
1389 free_tty_struct(tty);
1391 fail_no_mem:
1392 module_put(driver->owner);
1393 retval = -ENOMEM;
1394 goto end_init;
1396 /* call the tty release_mem routine to clean out this slot */
1397 release_mem_out:
1398 printk(KERN_INFO "init_dev: ldisc open failed, "
1399 "clearing slot %d\n", idx);
1400 release_mem(tty, idx);
1401 goto end_init;
1404 /*
1405 * Releases memory associated with a tty structure, and clears out the
1406 * driver table slots.
1407 */
1408 static void release_mem(struct tty_struct *tty, int idx)
1410 struct tty_struct *o_tty;
1411 struct termios *tp;
1412 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
1414 if ((o_tty = tty->link) != NULL) {
1415 if (!devpts)
1416 o_tty->driver->ttys[idx] = NULL;
1417 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1418 tp = o_tty->termios;
1419 if (!devpts)
1420 o_tty->driver->termios[idx] = NULL;
1421 kfree(tp);
1423 tp = o_tty->termios_locked;
1424 if (!devpts)
1425 o_tty->driver->termios_locked[idx] = NULL;
1426 kfree(tp);
1428 o_tty->magic = 0;
1429 o_tty->driver->refcount--;
1430 file_list_lock();
1431 list_del_init(&o_tty->tty_files);
1432 file_list_unlock();
1433 free_tty_struct(o_tty);
1436 if (!devpts)
1437 tty->driver->ttys[idx] = NULL;
1438 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1439 tp = tty->termios;
1440 if (!devpts)
1441 tty->driver->termios[idx] = NULL;
1442 kfree(tp);
1444 tp = tty->termios_locked;
1445 if (!devpts)
1446 tty->driver->termios_locked[idx] = NULL;
1447 kfree(tp);
1450 tty->magic = 0;
1451 tty->driver->refcount--;
1452 file_list_lock();
1453 list_del_init(&tty->tty_files);
1454 file_list_unlock();
1455 module_put(tty->driver->owner);
1456 free_tty_struct(tty);
1459 /*
1460 * Even releasing the tty structures is a tricky business.. We have
1461 * to be very careful that the structures are all released at the
1462 * same time, as interrupts might otherwise get the wrong pointers.
1464 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1465 * lead to double frees or releasing memory still in use.
1466 */
1467 static void release_dev(struct file * filp)
1469 struct tty_struct *tty, *o_tty;
1470 int pty_master, tty_closing, o_tty_closing, do_sleep;
1471 int devpts_master, devpts;
1472 int idx;
1473 char buf[64];
1474 unsigned long flags;
1476 tty = (struct tty_struct *)filp->private_data;
1477 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
1478 return;
1480 check_tty_count(tty, "release_dev");
1482 tty_fasync(-1, filp, 0);
1484 idx = tty->index;
1485 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1486 tty->driver->subtype == PTY_TYPE_MASTER);
1487 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1488 devpts_master = pty_master && devpts;
1489 o_tty = tty->link;
1491 #ifdef TTY_PARANOIA_CHECK
1492 if (idx < 0 || idx >= tty->driver->num) {
1493 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1494 "free (%s)\n", tty->name);
1495 return;
1497 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1498 if (tty != tty->driver->ttys[idx]) {
1499 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1500 "for (%s)\n", idx, tty->name);
1501 return;
1503 if (tty->termios != tty->driver->termios[idx]) {
1504 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1505 "for (%s)\n",
1506 idx, tty->name);
1507 return;
1509 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1510 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1511 "termios_locked for (%s)\n",
1512 idx, tty->name);
1513 return;
1516 #endif
1518 #ifdef TTY_DEBUG_HANGUP
1519 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1520 tty_name(tty, buf), tty->count);
1521 #endif
1523 #ifdef TTY_PARANOIA_CHECK
1524 if (tty->driver->other &&
1525 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1526 if (o_tty != tty->driver->other->ttys[idx]) {
1527 printk(KERN_DEBUG "release_dev: other->table[%d] "
1528 "not o_tty for (%s)\n",
1529 idx, tty->name);
1530 return;
1532 if (o_tty->termios != tty->driver->other->termios[idx]) {
1533 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1534 "not o_termios for (%s)\n",
1535 idx, tty->name);
1536 return;
1538 if (o_tty->termios_locked !=
1539 tty->driver->other->termios_locked[idx]) {
1540 printk(KERN_DEBUG "release_dev: other->termios_locked["
1541 "%d] not o_termios_locked for (%s)\n",
1542 idx, tty->name);
1543 return;
1545 if (o_tty->link != tty) {
1546 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1547 return;
1550 #endif
1551 if (tty->driver->close)
1552 tty->driver->close(tty, filp);
1554 /*
1555 * Sanity check: if tty->count is going to zero, there shouldn't be
1556 * any waiters on tty->read_wait or tty->write_wait. We test the
1557 * wait queues and kick everyone out _before_ actually starting to
1558 * close. This ensures that we won't block while releasing the tty
1559 * structure.
1561 * The test for the o_tty closing is necessary, since the master and
1562 * slave sides may close in any order. If the slave side closes out
1563 * first, its count will be one, since the master side holds an open.
1564 * Thus this test wouldn't be triggered at the time the slave closes,
1565 * so we do it now.
1567 * Note that it's possible for the tty to be opened again while we're
1568 * flushing out waiters. By recalculating the closing flags before
1569 * each iteration we avoid any problems.
1570 */
1571 while (1) {
1572 /* Guard against races with tty->count changes elsewhere and
1573 opens on /dev/tty */
1575 down(&tty_sem);
1576 tty_closing = tty->count <= 1;
1577 o_tty_closing = o_tty &&
1578 (o_tty->count <= (pty_master ? 1 : 0));
1579 up(&tty_sem);
1580 do_sleep = 0;
1582 if (tty_closing) {
1583 if (waitqueue_active(&tty->read_wait)) {
1584 wake_up(&tty->read_wait);
1585 do_sleep++;
1587 if (waitqueue_active(&tty->write_wait)) {
1588 wake_up(&tty->write_wait);
1589 do_sleep++;
1592 if (o_tty_closing) {
1593 if (waitqueue_active(&o_tty->read_wait)) {
1594 wake_up(&o_tty->read_wait);
1595 do_sleep++;
1597 if (waitqueue_active(&o_tty->write_wait)) {
1598 wake_up(&o_tty->write_wait);
1599 do_sleep++;
1602 if (!do_sleep)
1603 break;
1605 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1606 "active!\n", tty_name(tty, buf));
1607 schedule();
1610 /*
1611 * The closing flags are now consistent with the open counts on
1612 * both sides, and we've completed the last operation that could
1613 * block, so it's safe to proceed with closing.
1614 */
1616 down(&tty_sem);
1617 if (pty_master) {
1618 if (--o_tty->count < 0) {
1619 printk(KERN_WARNING "release_dev: bad pty slave count "
1620 "(%d) for %s\n",
1621 o_tty->count, tty_name(o_tty, buf));
1622 o_tty->count = 0;
1625 if (--tty->count < 0) {
1626 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1627 tty->count, tty_name(tty, buf));
1628 tty->count = 0;
1630 up(&tty_sem);
1632 /*
1633 * We've decremented tty->count, so we need to remove this file
1634 * descriptor off the tty->tty_files list; this serves two
1635 * purposes:
1636 * - check_tty_count sees the correct number of file descriptors
1637 * associated with this tty.
1638 * - do_tty_hangup no longer sees this file descriptor as
1639 * something that needs to be handled for hangups.
1640 */
1641 file_kill(filp);
1642 filp->private_data = NULL;
1644 /*
1645 * Perform some housekeeping before deciding whether to return.
1647 * Set the TTY_CLOSING flag if this was the last open. In the
1648 * case of a pty we may have to wait around for the other side
1649 * to close, and TTY_CLOSING makes sure we can't be reopened.
1650 */
1651 if(tty_closing)
1652 set_bit(TTY_CLOSING, &tty->flags);
1653 if(o_tty_closing)
1654 set_bit(TTY_CLOSING, &o_tty->flags);
1656 /*
1657 * If _either_ side is closing, make sure there aren't any
1658 * processes that still think tty or o_tty is their controlling
1659 * tty.
1660 */
1661 if (tty_closing || o_tty_closing) {
1662 struct task_struct *p;
1664 read_lock(&tasklist_lock);
1665 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1666 p->signal->tty = NULL;
1667 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1668 if (o_tty)
1669 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
1670 p->signal->tty = NULL;
1671 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
1672 read_unlock(&tasklist_lock);
1675 /* check whether both sides are closing ... */
1676 if (!tty_closing || (o_tty && !o_tty_closing))
1677 return;
1679 #ifdef TTY_DEBUG_HANGUP
1680 printk(KERN_DEBUG "freeing tty structure...");
1681 #endif
1682 /*
1683 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
1684 * kill any delayed work. As this is the final close it does not
1685 * race with the set_ldisc code path.
1686 */
1687 clear_bit(TTY_LDISC, &tty->flags);
1688 clear_bit(TTY_DONT_FLIP, &tty->flags);
1689 cancel_delayed_work(&tty->flip.work);
1691 /*
1692 * Wait for ->hangup_work and ->flip.work handlers to terminate
1693 */
1695 flush_scheduled_work();
1697 /*
1698 * Wait for any short term users (we know they are just driver
1699 * side waiters as the file is closing so user count on the file
1700 * side is zero.
1701 */
1702 spin_lock_irqsave(&tty_ldisc_lock, flags);
1703 while(tty->ldisc.refcount)
1705 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1706 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
1707 spin_lock_irqsave(&tty_ldisc_lock, flags);
1709 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1710 /*
1711 * Shutdown the current line discipline, and reset it to N_TTY.
1712 * N.B. why reset ldisc when we're releasing the memory??
1714 * FIXME: this MUST get fixed for the new reflocking
1715 */
1716 if (tty->ldisc.close)
1717 (tty->ldisc.close)(tty);
1718 tty_ldisc_put(tty->ldisc.num);
1720 /*
1721 * Switch the line discipline back
1722 */
1723 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1724 tty_set_termios_ldisc(tty,N_TTY);
1725 if (o_tty) {
1726 /* FIXME: could o_tty be in setldisc here ? */
1727 clear_bit(TTY_LDISC, &o_tty->flags);
1728 if (o_tty->ldisc.close)
1729 (o_tty->ldisc.close)(o_tty);
1730 tty_ldisc_put(o_tty->ldisc.num);
1731 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
1732 tty_set_termios_ldisc(o_tty,N_TTY);
1734 /*
1735 * The release_mem function takes care of the details of clearing
1736 * the slots and preserving the termios structure.
1737 */
1738 release_mem(tty, idx);
1740 #ifdef CONFIG_UNIX98_PTYS
1741 /* Make this pty number available for reallocation */
1742 if (devpts) {
1743 down(&allocated_ptys_lock);
1744 idr_remove(&allocated_ptys, idx);
1745 up(&allocated_ptys_lock);
1747 #endif
1751 /*
1752 * tty_open and tty_release keep up the tty count that contains the
1753 * number of opens done on a tty. We cannot use the inode-count, as
1754 * different inodes might point to the same tty.
1756 * Open-counting is needed for pty masters, as well as for keeping
1757 * track of serial lines: DTR is dropped when the last close happens.
1758 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1760 * The termios state of a pty is reset on first open so that
1761 * settings don't persist across reuse.
1762 */
1763 static int tty_open(struct inode * inode, struct file * filp)
1765 struct tty_struct *tty;
1766 int noctty, retval;
1767 struct tty_driver *driver;
1768 int index;
1769 dev_t device = inode->i_rdev;
1770 unsigned short saved_flags = filp->f_flags;
1772 nonseekable_open(inode, filp);
1774 retry_open:
1775 noctty = filp->f_flags & O_NOCTTY;
1776 index = -1;
1777 retval = 0;
1779 down(&tty_sem);
1781 if (device == MKDEV(TTYAUX_MAJOR,0)) {
1782 if (!current->signal->tty) {
1783 up(&tty_sem);
1784 return -ENXIO;
1786 driver = current->signal->tty->driver;
1787 index = current->signal->tty->index;
1788 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1789 /* noctty = 1; */
1790 goto got_driver;
1792 #ifdef CONFIG_VT
1793 if (console_use_vt && (device == MKDEV(TTY_MAJOR,0))) {
1794 extern struct tty_driver *console_driver;
1795 driver = console_driver;
1796 index = fg_console;
1797 noctty = 1;
1798 goto got_driver;
1800 #endif
1801 if (device == MKDEV(TTYAUX_MAJOR,1)) {
1802 driver = console_device(&index);
1803 if (driver) {
1804 /* Don't let /dev/console block */
1805 filp->f_flags |= O_NONBLOCK;
1806 noctty = 1;
1807 goto got_driver;
1809 up(&tty_sem);
1810 return -ENODEV;
1813 driver = get_tty_driver(device, &index);
1814 if (!driver) {
1815 up(&tty_sem);
1816 return -ENODEV;
1818 got_driver:
1819 retval = init_dev(driver, index, &tty);
1820 up(&tty_sem);
1821 if (retval)
1822 return retval;
1824 filp->private_data = tty;
1825 file_move(filp, &tty->tty_files);
1826 check_tty_count(tty, "tty_open");
1827 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1828 tty->driver->subtype == PTY_TYPE_MASTER)
1829 noctty = 1;
1830 #ifdef TTY_DEBUG_HANGUP
1831 printk(KERN_DEBUG "opening %s...", tty->name);
1832 #endif
1833 if (!retval) {
1834 if (tty->driver->open)
1835 retval = tty->driver->open(tty, filp);
1836 else
1837 retval = -ENODEV;
1839 filp->f_flags = saved_flags;
1841 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1842 retval = -EBUSY;
1844 if (retval) {
1845 #ifdef TTY_DEBUG_HANGUP
1846 printk(KERN_DEBUG "error %d in opening %s...", retval,
1847 tty->name);
1848 #endif
1849 release_dev(filp);
1850 if (retval != -ERESTARTSYS)
1851 return retval;
1852 if (signal_pending(current))
1853 return retval;
1854 schedule();
1855 /*
1856 * Need to reset f_op in case a hangup happened.
1857 */
1858 if (filp->f_op == &hung_up_tty_fops)
1859 filp->f_op = &tty_fops;
1860 goto retry_open;
1862 if (!noctty &&
1863 current->signal->leader &&
1864 !current->signal->tty &&
1865 tty->session == 0) {
1866 task_lock(current);
1867 current->signal->tty = tty;
1868 task_unlock(current);
1869 current->signal->tty_old_pgrp = 0;
1870 tty->session = current->signal->session;
1871 tty->pgrp = process_group(current);
1873 return 0;
1876 #ifdef CONFIG_UNIX98_PTYS
1877 static int ptmx_open(struct inode * inode, struct file * filp)
1879 struct tty_struct *tty;
1880 int retval;
1881 int index;
1882 int idr_ret;
1884 nonseekable_open(inode, filp);
1886 /* find a device that is not in use. */
1887 down(&allocated_ptys_lock);
1888 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
1889 up(&allocated_ptys_lock);
1890 return -ENOMEM;
1892 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
1893 if (idr_ret < 0) {
1894 up(&allocated_ptys_lock);
1895 if (idr_ret == -EAGAIN)
1896 return -ENOMEM;
1897 return -EIO;
1899 if (index >= pty_limit) {
1900 idr_remove(&allocated_ptys, index);
1901 up(&allocated_ptys_lock);
1902 return -EIO;
1904 up(&allocated_ptys_lock);
1906 down(&tty_sem);
1907 retval = init_dev(ptm_driver, index, &tty);
1908 up(&tty_sem);
1910 if (retval)
1911 goto out;
1913 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1914 filp->private_data = tty;
1915 file_move(filp, &tty->tty_files);
1917 retval = -ENOMEM;
1918 if (devpts_pty_new(tty->link))
1919 goto out1;
1921 check_tty_count(tty, "tty_open");
1922 retval = ptm_driver->open(tty, filp);
1923 if (!retval)
1924 return 0;
1925 out1:
1926 release_dev(filp);
1927 out:
1928 down(&allocated_ptys_lock);
1929 idr_remove(&allocated_ptys, index);
1930 up(&allocated_ptys_lock);
1931 return retval;
1933 #endif
1935 static int tty_release(struct inode * inode, struct file * filp)
1937 lock_kernel();
1938 release_dev(filp);
1939 unlock_kernel();
1940 return 0;
1943 /* No kernel lock held - fine */
1944 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1946 struct tty_struct * tty;
1947 struct tty_ldisc *ld;
1948 int ret = 0;
1950 tty = (struct tty_struct *)filp->private_data;
1951 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
1952 return 0;
1954 ld = tty_ldisc_ref_wait(tty);
1955 if (ld->poll)
1956 ret = (ld->poll)(tty, filp, wait);
1957 tty_ldisc_deref(ld);
1958 return ret;
1961 static int tty_fasync(int fd, struct file * filp, int on)
1963 struct tty_struct * tty;
1964 int retval;
1966 tty = (struct tty_struct *)filp->private_data;
1967 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
1968 return 0;
1970 retval = fasync_helper(fd, filp, on, &tty->fasync);
1971 if (retval <= 0)
1972 return retval;
1974 if (on) {
1975 if (!waitqueue_active(&tty->read_wait))
1976 tty->minimum_to_wake = 1;
1977 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
1978 if (retval)
1979 return retval;
1980 } else {
1981 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1982 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1984 return 0;
1987 static int tiocsti(struct tty_struct *tty, char __user *p)
1989 char ch, mbz = 0;
1990 struct tty_ldisc *ld;
1992 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1993 return -EPERM;
1994 if (get_user(ch, p))
1995 return -EFAULT;
1996 ld = tty_ldisc_ref_wait(tty);
1997 ld->receive_buf(tty, &ch, &mbz, 1);
1998 tty_ldisc_deref(ld);
1999 return 0;
2002 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2004 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
2005 return -EFAULT;
2006 return 0;
2009 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2010 struct winsize __user * arg)
2012 struct winsize tmp_ws;
2014 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2015 return -EFAULT;
2016 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
2017 return 0;
2018 #ifdef CONFIG_VT
2019 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
2020 int rc;
2022 acquire_console_sem();
2023 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
2024 release_console_sem();
2025 if (rc)
2026 return -ENXIO;
2028 #endif
2029 if (tty->pgrp > 0)
2030 kill_pg(tty->pgrp, SIGWINCH, 1);
2031 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
2032 kill_pg(real_tty->pgrp, SIGWINCH, 1);
2033 tty->winsize = tmp_ws;
2034 real_tty->winsize = tmp_ws;
2035 return 0;
2038 static int tioccons(struct file *file)
2040 if (!capable(CAP_SYS_ADMIN))
2041 return -EPERM;
2042 if (file->f_op->write == redirected_tty_write) {
2043 struct file *f;
2044 spin_lock(&redirect_lock);
2045 f = redirect;
2046 redirect = NULL;
2047 spin_unlock(&redirect_lock);
2048 if (f)
2049 fput(f);
2050 return 0;
2052 spin_lock(&redirect_lock);
2053 if (redirect) {
2054 spin_unlock(&redirect_lock);
2055 return -EBUSY;
2057 get_file(file);
2058 redirect = file;
2059 spin_unlock(&redirect_lock);
2060 return 0;
2064 static int fionbio(struct file *file, int __user *p)
2066 int nonblock;
2068 if (get_user(nonblock, p))
2069 return -EFAULT;
2071 if (nonblock)
2072 file->f_flags |= O_NONBLOCK;
2073 else
2074 file->f_flags &= ~O_NONBLOCK;
2075 return 0;
2078 static int tiocsctty(struct tty_struct *tty, int arg)
2080 task_t *p;
2082 if (current->signal->leader &&
2083 (current->signal->session == tty->session))
2084 return 0;
2085 /*
2086 * The process must be a session leader and
2087 * not have a controlling tty already.
2088 */
2089 if (!current->signal->leader || current->signal->tty)
2090 return -EPERM;
2091 if (tty->session > 0) {
2092 /*
2093 * This tty is already the controlling
2094 * tty for another session group!
2095 */
2096 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
2097 /*
2098 * Steal it away
2099 */
2101 read_lock(&tasklist_lock);
2102 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
2103 p->signal->tty = NULL;
2104 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2105 read_unlock(&tasklist_lock);
2106 } else
2107 return -EPERM;
2109 task_lock(current);
2110 current->signal->tty = tty;
2111 task_unlock(current);
2112 current->signal->tty_old_pgrp = 0;
2113 tty->session = current->signal->session;
2114 tty->pgrp = process_group(current);
2115 return 0;
2118 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2120 /*
2121 * (tty == real_tty) is a cheap way of
2122 * testing if the tty is NOT a master pty.
2123 */
2124 if (tty == real_tty && current->signal->tty != real_tty)
2125 return -ENOTTY;
2126 return put_user(real_tty->pgrp, p);
2129 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2131 pid_t pgrp;
2132 int retval = tty_check_change(real_tty);
2134 if (retval == -EIO)
2135 return -ENOTTY;
2136 if (retval)
2137 return retval;
2138 if (!current->signal->tty ||
2139 (current->signal->tty != real_tty) ||
2140 (real_tty->session != current->signal->session))
2141 return -ENOTTY;
2142 if (get_user(pgrp, p))
2143 return -EFAULT;
2144 if (pgrp < 0)
2145 return -EINVAL;
2146 if (session_of_pgrp(pgrp) != current->signal->session)
2147 return -EPERM;
2148 real_tty->pgrp = pgrp;
2149 return 0;
2152 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2154 /*
2155 * (tty == real_tty) is a cheap way of
2156 * testing if the tty is NOT a master pty.
2157 */
2158 if (tty == real_tty && current->signal->tty != real_tty)
2159 return -ENOTTY;
2160 if (real_tty->session <= 0)
2161 return -ENOTTY;
2162 return put_user(real_tty->session, p);
2165 static int tiocsetd(struct tty_struct *tty, int __user *p)
2167 int ldisc;
2169 if (get_user(ldisc, p))
2170 return -EFAULT;
2171 return tty_set_ldisc(tty, ldisc);
2174 static int send_break(struct tty_struct *tty, int duration)
2176 tty->driver->break_ctl(tty, -1);
2177 if (!signal_pending(current)) {
2178 set_current_state(TASK_INTERRUPTIBLE);
2179 schedule_timeout(duration);
2181 tty->driver->break_ctl(tty, 0);
2182 if (signal_pending(current))
2183 return -EINTR;
2184 return 0;
2187 static int
2188 tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2190 int retval = -EINVAL;
2192 if (tty->driver->tiocmget) {
2193 retval = tty->driver->tiocmget(tty, file);
2195 if (retval >= 0)
2196 retval = put_user(retval, p);
2198 return retval;
2201 static int
2202 tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2203 unsigned __user *p)
2205 int retval = -EINVAL;
2207 if (tty->driver->tiocmset) {
2208 unsigned int set, clear, val;
2210 retval = get_user(val, p);
2211 if (retval)
2212 return retval;
2214 set = clear = 0;
2215 switch (cmd) {
2216 case TIOCMBIS:
2217 set = val;
2218 break;
2219 case TIOCMBIC:
2220 clear = val;
2221 break;
2222 case TIOCMSET:
2223 set = val;
2224 clear = ~val;
2225 break;
2228 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2229 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2231 retval = tty->driver->tiocmset(tty, file, set, clear);
2233 return retval;
2236 /*
2237 * Split this up, as gcc can choke on it otherwise..
2238 */
2239 int tty_ioctl(struct inode * inode, struct file * file,
2240 unsigned int cmd, unsigned long arg)
2242 struct tty_struct *tty, *real_tty;
2243 void __user *p = (void __user *)arg;
2244 int retval;
2245 struct tty_ldisc *ld;
2247 tty = (struct tty_struct *)file->private_data;
2248 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2249 return -EINVAL;
2251 real_tty = tty;
2252 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2253 tty->driver->subtype == PTY_TYPE_MASTER)
2254 real_tty = tty->link;
2256 /*
2257 * Break handling by driver
2258 */
2259 if (!tty->driver->break_ctl) {
2260 switch(cmd) {
2261 case TIOCSBRK:
2262 case TIOCCBRK:
2263 if (tty->driver->ioctl)
2264 return tty->driver->ioctl(tty, file, cmd, arg);
2265 return -EINVAL;
2267 /* These two ioctl's always return success; even if */
2268 /* the driver doesn't support them. */
2269 case TCSBRK:
2270 case TCSBRKP:
2271 if (!tty->driver->ioctl)
2272 return 0;
2273 retval = tty->driver->ioctl(tty, file, cmd, arg);
2274 if (retval == -ENOIOCTLCMD)
2275 retval = 0;
2276 return retval;
2280 /*
2281 * Factor out some common prep work
2282 */
2283 switch (cmd) {
2284 case TIOCSETD:
2285 case TIOCSBRK:
2286 case TIOCCBRK:
2287 case TCSBRK:
2288 case TCSBRKP:
2289 retval = tty_check_change(tty);
2290 if (retval)
2291 return retval;
2292 if (cmd != TIOCCBRK) {
2293 tty_wait_until_sent(tty, 0);
2294 if (signal_pending(current))
2295 return -EINTR;
2297 break;
2300 switch (cmd) {
2301 case TIOCSTI:
2302 return tiocsti(tty, p);
2303 case TIOCGWINSZ:
2304 return tiocgwinsz(tty, p);
2305 case TIOCSWINSZ:
2306 return tiocswinsz(tty, real_tty, p);
2307 case TIOCCONS:
2308 return real_tty!=tty ? -EINVAL : tioccons(file);
2309 case FIONBIO:
2310 return fionbio(file, p);
2311 case TIOCEXCL:
2312 set_bit(TTY_EXCLUSIVE, &tty->flags);
2313 return 0;
2314 case TIOCNXCL:
2315 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2316 return 0;
2317 case TIOCNOTTY:
2318 if (current->signal->tty != tty)
2319 return -ENOTTY;
2320 if (current->signal->leader)
2321 disassociate_ctty(0);
2322 task_lock(current);
2323 current->signal->tty = NULL;
2324 task_unlock(current);
2325 return 0;
2326 case TIOCSCTTY:
2327 return tiocsctty(tty, arg);
2328 case TIOCGPGRP:
2329 return tiocgpgrp(tty, real_tty, p);
2330 case TIOCSPGRP:
2331 return tiocspgrp(tty, real_tty, p);
2332 case TIOCGSID:
2333 return tiocgsid(tty, real_tty, p);
2334 case TIOCGETD:
2335 /* FIXME: check this is ok */
2336 return put_user(tty->ldisc.num, (int __user *)p);
2337 case TIOCSETD:
2338 return tiocsetd(tty, p);
2339 #ifdef CONFIG_VT
2340 case TIOCLINUX:
2341 return tioclinux(tty, arg);
2342 #endif
2343 /*
2344 * Break handling
2345 */
2346 case TIOCSBRK: /* Turn break on, unconditionally */
2347 tty->driver->break_ctl(tty, -1);
2348 return 0;
2350 case TIOCCBRK: /* Turn break off, unconditionally */
2351 tty->driver->break_ctl(tty, 0);
2352 return 0;
2353 case TCSBRK: /* SVID version: non-zero arg --> no break */
2354 /*
2355 * XXX is the above comment correct, or the
2356 * code below correct? Is this ioctl used at
2357 * all by anyone?
2358 */
2359 if (!arg)
2360 return send_break(tty, HZ/4);
2361 return 0;
2362 case TCSBRKP: /* support for POSIX tcsendbreak() */
2363 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
2365 case TIOCMGET:
2366 return tty_tiocmget(tty, file, p);
2368 case TIOCMSET:
2369 case TIOCMBIC:
2370 case TIOCMBIS:
2371 return tty_tiocmset(tty, file, cmd, p);
2373 if (tty->driver->ioctl) {
2374 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
2375 if (retval != -ENOIOCTLCMD)
2376 return retval;
2378 ld = tty_ldisc_ref_wait(tty);
2379 retval = -EINVAL;
2380 if (ld->ioctl) {
2381 retval = ld->ioctl(tty, file, cmd, arg);
2382 if (retval == -ENOIOCTLCMD)
2383 retval = -EINVAL;
2385 tty_ldisc_deref(ld);
2386 return retval;
2390 /*
2391 * This implements the "Secure Attention Key" --- the idea is to
2392 * prevent trojan horses by killing all processes associated with this
2393 * tty when the user hits the "Secure Attention Key". Required for
2394 * super-paranoid applications --- see the Orange Book for more details.
2396 * This code could be nicer; ideally it should send a HUP, wait a few
2397 * seconds, then send a INT, and then a KILL signal. But you then
2398 * have to coordinate with the init process, since all processes associated
2399 * with the current tty must be dead before the new getty is allowed
2400 * to spawn.
2402 * Now, if it would be correct ;-/ The current code has a nasty hole -
2403 * it doesn't catch files in flight. We may send the descriptor to ourselves
2404 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2406 * Nasty bug: do_SAK is being called in interrupt context. This can
2407 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2408 */
2409 static void __do_SAK(void *arg)
2411 #ifdef TTY_SOFT_SAK
2412 tty_hangup(tty);
2413 #else
2414 struct tty_struct *tty = arg;
2415 struct task_struct *p;
2416 int session;
2417 int i;
2418 struct file *filp;
2419 struct tty_ldisc *disc;
2421 if (!tty)
2422 return;
2423 session = tty->session;
2425 /* We don't want an ldisc switch during this */
2426 disc = tty_ldisc_ref(tty);
2427 if (disc && disc->flush_buffer)
2428 disc->flush_buffer(tty);
2429 tty_ldisc_deref(disc);
2431 if (tty->driver->flush_buffer)
2432 tty->driver->flush_buffer(tty);
2434 read_lock(&tasklist_lock);
2435 do_each_task_pid(session, PIDTYPE_SID, p) {
2436 if (p->signal->tty == tty || session > 0) {
2437 printk(KERN_NOTICE "SAK: killed process %d"
2438 " (%s): p->signal->session==tty->session\n",
2439 p->pid, p->comm);
2440 send_sig(SIGKILL, p, 1);
2441 continue;
2443 task_lock(p);
2444 if (p->files) {
2445 spin_lock(&p->files->file_lock);
2446 for (i=0; i < p->files->max_fds; i++) {
2447 filp = fcheck_files(p->files, i);
2448 if (!filp)
2449 continue;
2450 if (filp->f_op->read == tty_read &&
2451 filp->private_data == tty) {
2452 printk(KERN_NOTICE "SAK: killed process %d"
2453 " (%s): fd#%d opened to the tty\n",
2454 p->pid, p->comm, i);
2455 send_sig(SIGKILL, p, 1);
2456 break;
2459 spin_unlock(&p->files->file_lock);
2461 task_unlock(p);
2462 } while_each_task_pid(session, PIDTYPE_SID, p);
2463 read_unlock(&tasklist_lock);
2464 #endif
2467 /*
2468 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2469 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2470 * the values which we write to it will be identical to the values which it
2471 * already has. --akpm
2472 */
2473 void do_SAK(struct tty_struct *tty)
2475 if (!tty)
2476 return;
2477 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
2478 schedule_work(&tty->SAK_work);
2481 EXPORT_SYMBOL(do_SAK);
2483 /*
2484 * This routine is called out of the software interrupt to flush data
2485 * from the flip buffer to the line discipline.
2486 */
2488 static void flush_to_ldisc(void *private_)
2490 struct tty_struct *tty = (struct tty_struct *) private_;
2491 unsigned char *cp;
2492 char *fp;
2493 int count;
2494 unsigned long flags;
2495 struct tty_ldisc *disc;
2497 disc = tty_ldisc_ref(tty);
2498 if (disc == NULL) /* !TTY_LDISC */
2499 return;
2501 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
2502 /*
2503 * Do it after the next timer tick:
2504 */
2505 schedule_delayed_work(&tty->flip.work, 1);
2506 goto out;
2508 spin_lock_irqsave(&tty->read_lock, flags);
2509 if (tty->flip.buf_num) {
2510 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2511 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2512 tty->flip.buf_num = 0;
2513 tty->flip.char_buf_ptr = tty->flip.char_buf;
2514 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2515 } else {
2516 cp = tty->flip.char_buf;
2517 fp = tty->flip.flag_buf;
2518 tty->flip.buf_num = 1;
2519 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2520 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2522 count = tty->flip.count;
2523 tty->flip.count = 0;
2524 spin_unlock_irqrestore(&tty->read_lock, flags);
2526 disc->receive_buf(tty, cp, fp, count);
2527 out:
2528 tty_ldisc_deref(disc);
2531 /*
2532 * Routine which returns the baud rate of the tty
2534 * Note that the baud_table needs to be kept in sync with the
2535 * include/asm/termbits.h file.
2536 */
2537 static int baud_table[] = {
2538 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
2539 9600, 19200, 38400, 57600, 115200, 230400, 460800,
2540 #ifdef __sparc__
2541 76800, 153600, 307200, 614400, 921600
2542 #else
2543 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2544 2500000, 3000000, 3500000, 4000000
2545 #endif
2546 };
2548 static int n_baud_table = ARRAY_SIZE(baud_table);
2550 /**
2551 * tty_termios_baud_rate
2552 * @termios: termios structure
2554 * Convert termios baud rate data into a speed. This should be called
2555 * with the termios lock held if this termios is a terminal termios
2556 * structure. May change the termios data.
2557 */
2559 int tty_termios_baud_rate(struct termios *termios)
2561 unsigned int cbaud;
2563 cbaud = termios->c_cflag & CBAUD;
2565 if (cbaud & CBAUDEX) {
2566 cbaud &= ~CBAUDEX;
2568 if (cbaud < 1 || cbaud + 15 > n_baud_table)
2569 termios->c_cflag &= ~CBAUDEX;
2570 else
2571 cbaud += 15;
2573 return baud_table[cbaud];
2576 EXPORT_SYMBOL(tty_termios_baud_rate);
2578 /**
2579 * tty_get_baud_rate - get tty bit rates
2580 * @tty: tty to query
2582 * Returns the baud rate as an integer for this terminal. The
2583 * termios lock must be held by the caller and the terminal bit
2584 * flags may be updated.
2585 */
2587 int tty_get_baud_rate(struct tty_struct *tty)
2589 int baud = tty_termios_baud_rate(tty->termios);
2591 if (baud == 38400 && tty->alt_speed) {
2592 if (!tty->warned) {
2593 printk(KERN_WARNING "Use of setserial/setrocket to "
2594 "set SPD_* flags is deprecated\n");
2595 tty->warned = 1;
2597 baud = tty->alt_speed;
2600 return baud;
2603 EXPORT_SYMBOL(tty_get_baud_rate);
2605 /**
2606 * tty_flip_buffer_push - terminal
2607 * @tty: tty to push
2609 * Queue a push of the terminal flip buffers to the line discipline. This
2610 * function must not be called from IRQ context if tty->low_latency is set.
2612 * In the event of the queue being busy for flipping the work will be
2613 * held off and retried later.
2614 */
2616 void tty_flip_buffer_push(struct tty_struct *tty)
2618 if (tty->low_latency)
2619 flush_to_ldisc((void *) tty);
2620 else
2621 schedule_delayed_work(&tty->flip.work, 1);
2624 EXPORT_SYMBOL(tty_flip_buffer_push);
2626 /*
2627 * This subroutine initializes a tty structure.
2628 */
2629 static void initialize_tty_struct(struct tty_struct *tty)
2631 memset(tty, 0, sizeof(struct tty_struct));
2632 tty->magic = TTY_MAGIC;
2633 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2634 tty->pgrp = -1;
2635 tty->overrun_time = jiffies;
2636 tty->flip.char_buf_ptr = tty->flip.char_buf;
2637 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2638 INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
2639 init_MUTEX(&tty->flip.pty_sem);
2640 init_MUTEX(&tty->termios_sem);
2641 init_waitqueue_head(&tty->write_wait);
2642 init_waitqueue_head(&tty->read_wait);
2643 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
2644 sema_init(&tty->atomic_read, 1);
2645 sema_init(&tty->atomic_write, 1);
2646 spin_lock_init(&tty->read_lock);
2647 INIT_LIST_HEAD(&tty->tty_files);
2648 INIT_WORK(&tty->SAK_work, NULL, NULL);
2651 /*
2652 * The default put_char routine if the driver did not define one.
2653 */
2654 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2656 tty->driver->write(tty, &ch, 1);
2659 static struct class_simple *tty_class;
2661 /**
2662 * tty_register_device - register a tty device
2663 * @driver: the tty driver that describes the tty device
2664 * @index: the index in the tty driver for this tty device
2665 * @device: a struct device that is associated with this tty device.
2666 * This field is optional, if there is no known struct device for this
2667 * tty device it can be set to NULL safely.
2669 * This call is required to be made to register an individual tty device if
2670 * the tty driver's flags have the TTY_DRIVER_NO_DEVFS bit set. If that
2671 * bit is not set, this function should not be called.
2672 */
2673 void tty_register_device(struct tty_driver *driver, unsigned index,
2674 struct device *device)
2676 char name[64];
2677 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2679 if (index >= driver->num) {
2680 printk(KERN_ERR "Attempt to register invalid tty line number "
2681 " (%d).\n", index);
2682 return;
2685 devfs_mk_cdev(dev, S_IFCHR | S_IRUSR | S_IWUSR,
2686 "%s%d", driver->devfs_name, index + driver->name_base);
2688 if (driver->type == TTY_DRIVER_TYPE_PTY)
2689 pty_line_name(driver, index, name);
2690 else
2691 tty_line_name(driver, index, name);
2692 class_simple_device_add(tty_class, dev, device, name);
2695 /**
2696 * tty_unregister_device - unregister a tty device
2697 * @driver: the tty driver that describes the tty device
2698 * @index: the index in the tty driver for this tty device
2700 * If a tty device is registered with a call to tty_register_device() then
2701 * this function must be made when the tty device is gone.
2702 */
2703 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2705 devfs_remove("%s%d", driver->devfs_name, index + driver->name_base);
2706 class_simple_device_remove(MKDEV(driver->major, driver->minor_start) + index);
2709 EXPORT_SYMBOL(tty_register_device);
2710 EXPORT_SYMBOL(tty_unregister_device);
2712 struct tty_driver *alloc_tty_driver(int lines)
2714 struct tty_driver *driver;
2716 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
2717 if (driver) {
2718 memset(driver, 0, sizeof(struct tty_driver));
2719 driver->magic = TTY_DRIVER_MAGIC;
2720 driver->num = lines;
2721 /* later we'll move allocation of tables here */
2723 return driver;
2726 void put_tty_driver(struct tty_driver *driver)
2728 kfree(driver);
2731 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
2733 driver->open = op->open;
2734 driver->close = op->close;
2735 driver->write = op->write;
2736 driver->put_char = op->put_char;
2737 driver->flush_chars = op->flush_chars;
2738 driver->write_room = op->write_room;
2739 driver->chars_in_buffer = op->chars_in_buffer;
2740 driver->ioctl = op->ioctl;
2741 driver->set_termios = op->set_termios;
2742 driver->throttle = op->throttle;
2743 driver->unthrottle = op->unthrottle;
2744 driver->stop = op->stop;
2745 driver->start = op->start;
2746 driver->hangup = op->hangup;
2747 driver->break_ctl = op->break_ctl;
2748 driver->flush_buffer = op->flush_buffer;
2749 driver->set_ldisc = op->set_ldisc;
2750 driver->wait_until_sent = op->wait_until_sent;
2751 driver->send_xchar = op->send_xchar;
2752 driver->read_proc = op->read_proc;
2753 driver->write_proc = op->write_proc;
2754 driver->tiocmget = op->tiocmget;
2755 driver->tiocmset = op->tiocmset;
2759 EXPORT_SYMBOL(alloc_tty_driver);
2760 EXPORT_SYMBOL(put_tty_driver);
2761 EXPORT_SYMBOL(tty_set_operations);
2763 /*
2764 * Called by a tty driver to register itself.
2765 */
2766 int tty_register_driver(struct tty_driver *driver)
2768 int error;
2769 int i;
2770 dev_t dev;
2771 void **p = NULL;
2773 if (driver->flags & TTY_DRIVER_INSTALLED)
2774 return 0;
2776 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2777 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
2778 if (!p)
2779 return -ENOMEM;
2780 memset(p, 0, driver->num * 3 * sizeof(void *));
2783 if (!driver->major) {
2784 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
2785 (char*)driver->name);
2786 if (!error) {
2787 driver->major = MAJOR(dev);
2788 driver->minor_start = MINOR(dev);
2790 } else {
2791 dev = MKDEV(driver->major, driver->minor_start);
2792 error = register_chrdev_region(dev, driver->num,
2793 (char*)driver->name);
2795 if (error < 0) {
2796 kfree(p);
2797 return error;
2800 if (p) {
2801 driver->ttys = (struct tty_struct **)p;
2802 driver->termios = (struct termios **)(p + driver->num);
2803 driver->termios_locked = (struct termios **)(p + driver->num * 2);
2804 } else {
2805 driver->ttys = NULL;
2806 driver->termios = NULL;
2807 driver->termios_locked = NULL;
2810 cdev_init(&driver->cdev, &tty_fops);
2811 driver->cdev.owner = driver->owner;
2812 error = cdev_add(&driver->cdev, dev, driver->num);
2813 if (error) {
2814 cdev_del(&driver->cdev);
2815 unregister_chrdev_region(dev, driver->num);
2816 driver->ttys = NULL;
2817 driver->termios = driver->termios_locked = NULL;
2818 kfree(p);
2819 return error;
2822 if (!driver->put_char)
2823 driver->put_char = tty_default_put_char;
2825 list_add(&driver->tty_drivers, &tty_drivers);
2827 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2828 for(i = 0; i < driver->num; i++)
2829 tty_register_device(driver, i, NULL);
2831 proc_tty_register_driver(driver);
2832 return 0;
2835 EXPORT_SYMBOL(tty_register_driver);
2837 /*
2838 * Called by a tty driver to unregister itself.
2839 */
2840 int tty_unregister_driver(struct tty_driver *driver)
2842 int i;
2843 struct termios *tp;
2844 void *p;
2846 if (driver->refcount)
2847 return -EBUSY;
2849 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
2850 driver->num);
2852 list_del(&driver->tty_drivers);
2854 /*
2855 * Free the termios and termios_locked structures because
2856 * we don't want to get memory leaks when modular tty
2857 * drivers are removed from the kernel.
2858 */
2859 for (i = 0; i < driver->num; i++) {
2860 tp = driver->termios[i];
2861 if (tp) {
2862 driver->termios[i] = NULL;
2863 kfree(tp);
2865 tp = driver->termios_locked[i];
2866 if (tp) {
2867 driver->termios_locked[i] = NULL;
2868 kfree(tp);
2870 if (!(driver->flags & TTY_DRIVER_NO_DEVFS))
2871 tty_unregister_device(driver, i);
2873 p = driver->ttys;
2874 proc_tty_unregister_driver(driver);
2875 driver->ttys = NULL;
2876 driver->termios = driver->termios_locked = NULL;
2877 kfree(p);
2878 cdev_del(&driver->cdev);
2879 return 0;
2882 EXPORT_SYMBOL(tty_unregister_driver);
2885 /*
2886 * Initialize the console device. This is called *early*, so
2887 * we can't necessarily depend on lots of kernel help here.
2888 * Just do some early initializations, and do the complex setup
2889 * later.
2890 */
2891 void __init console_init(void)
2893 initcall_t *call;
2895 /* Setup the default TTY line discipline. */
2896 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2898 /*
2899 * set up the console device so that later boot sequences can
2900 * inform about problems etc..
2901 */
2902 #ifdef CONFIG_EARLY_PRINTK
2903 disable_early_printk();
2904 #endif
2905 #ifdef CONFIG_SERIAL_68360
2906 /* This is not a console initcall. I know not what it's doing here.
2907 So I haven't moved it. dwmw2 */
2908 rs_360_init();
2909 #endif
2910 call = __con_initcall_start;
2911 while (call < __con_initcall_end) {
2912 (*call)();
2913 call++;
2917 #ifdef CONFIG_VT
2918 extern int vty_init(void);
2919 #endif
2921 static int __init tty_class_init(void)
2923 tty_class = class_simple_create(THIS_MODULE, "tty");
2924 if (IS_ERR(tty_class))
2925 return PTR_ERR(tty_class);
2926 return 0;
2929 postcore_initcall(tty_class_init);
2931 /* 3/2004 jmc: why do these devices exist? */
2933 static struct cdev tty_cdev, console_cdev;
2934 #ifdef CONFIG_UNIX98_PTYS
2935 static struct cdev ptmx_cdev;
2936 #endif
2937 #ifdef CONFIG_VT
2938 static struct cdev vc0_cdev;
2939 #endif
2941 /*
2942 * Ok, now we can initialize the rest of the tty devices and can count
2943 * on memory allocations, interrupts etc..
2944 */
2945 static int __init tty_init(void)
2947 cdev_init(&tty_cdev, &tty_fops);
2948 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
2949 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
2950 panic("Couldn't register /dev/tty driver\n");
2951 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 0), S_IFCHR|S_IRUGO|S_IWUGO, "tty");
2952 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
2954 cdev_init(&console_cdev, &console_fops);
2955 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
2956 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
2957 panic("Couldn't register /dev/console driver\n");
2958 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 1), S_IFCHR|S_IRUSR|S_IWUSR, "console");
2959 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
2961 #ifdef CONFIG_UNIX98_PTYS
2962 cdev_init(&ptmx_cdev, &ptmx_fops);
2963 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
2964 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
2965 panic("Couldn't register /dev/ptmx driver\n");
2966 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 2), S_IFCHR|S_IRUGO|S_IWUGO, "ptmx");
2967 class_simple_device_add(tty_class, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
2968 #endif
2970 #ifdef CONFIG_VT
2971 if (console_use_vt) {
2972 cdev_init(&vc0_cdev, &console_fops);
2973 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2974 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1,
2975 "/dev/vc/0") < 0)
2976 panic("Couldn't register /dev/tty0 driver\n");
2977 devfs_mk_cdev(MKDEV(TTY_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR,
2978 "vc/0");
2979 class_simple_device_add(tty_class, MKDEV(TTY_MAJOR, 0), NULL,
2980 "tty0");
2982 vty_init();
2984 #endif
2985 return 0;
2987 module_init(tty_init);