direct-io.hg

view linux-2.4.30-xen-sparse/drivers/char/tty_io.c @ 5647:6f462a11a08e

Register the portio handler only once.
Signed-off-by: Arun Sharma <arun.sharma@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jul 01 21:25:45 2005 +0000 (2005-07-01)
parents 85fcf3b1b7a5
children 56a63f9f378f
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>
94 #include <asm/uaccess.h>
95 #include <asm/system.h>
96 #include <asm/bitops.h>
98 #include <linux/kbd_kern.h>
99 #include <linux/vt_kern.h>
100 #include <linux/selection.h>
101 #include <linux/devfs_fs_kernel.h>
103 #include <linux/kmod.h>
105 #ifdef CONFIG_XEN_CONSOLE
106 extern void xen_console_init(void);
107 #endif
109 #ifdef CONFIG_VT
110 extern void con_init_devfs (void);
111 #endif
113 extern void disable_early_printk(void);
115 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
116 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
117 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
118 #define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
120 #undef TTY_DEBUG_HANGUP
122 #define TTY_PARANOIA_CHECK 1
123 #define CHECK_TTY_COUNT 1
125 struct termios tty_std_termios; /* for the benefit of tty drivers */
126 struct tty_driver *tty_drivers; /* linked list of tty drivers */
128 #ifdef CONFIG_UNIX98_PTYS
129 extern struct tty_driver ptm_driver[]; /* Unix98 pty masters; for /dev/ptmx */
130 extern struct tty_driver pts_driver[]; /* Unix98 pty slaves; for /dev/ptmx */
131 #endif
133 static void initialize_tty_struct(struct tty_struct *tty);
135 static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
136 static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
137 static unsigned int tty_poll(struct file *, poll_table *);
138 static int tty_open(struct inode *, struct file *);
139 static int tty_release(struct inode *, struct file *);
140 int tty_ioctl(struct inode * inode, struct file * file,
141 unsigned int cmd, unsigned long arg);
142 static int tty_fasync(int fd, struct file * filp, int on);
143 extern int vme_scc_init (void);
144 extern long vme_scc_console_init(void);
145 extern int serial167_init(void);
146 extern long serial167_console_init(void);
147 extern void console_8xx_init(void);
148 extern void au1x00_serial_console_init(void);
149 extern int rs_8xx_init(void);
150 extern void mac_scc_console_init(void);
151 extern void hwc_console_init(void);
152 extern void hwc_tty_init(void);
153 extern void con3215_init(void);
154 extern void tty3215_init(void);
155 extern void tub3270_con_init(void);
156 extern void tub3270_init(void);
157 extern void rs285_console_init(void);
158 extern void sa1100_rs_console_init(void);
159 extern void sgi_serial_console_init(void);
160 extern void sn_sal_serial_console_init(void);
161 extern void sci_console_init(void);
162 extern void dec_serial_console_init(void);
163 extern void tx3912_console_init(void);
164 extern void tx3912_rs_init(void);
165 extern void txx927_console_init(void);
166 extern void txx9_rs_init(void);
167 extern void txx9_serial_console_init(void);
168 extern void sb1250_serial_console_init(void);
169 extern void arc_console_init(void);
170 extern int hvc_console_init(void);
172 #ifndef MIN
173 #define MIN(a,b) ((a) < (b) ? (a) : (b))
174 #endif
175 #ifndef MAX
176 #define MAX(a,b) ((a) < (b) ? (b) : (a))
177 #endif
179 static struct tty_struct *alloc_tty_struct(void)
180 {
181 struct tty_struct *tty;
183 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
184 if (tty)
185 memset(tty, 0, sizeof(struct tty_struct));
186 return tty;
187 }
189 static inline void free_tty_struct(struct tty_struct *tty)
190 {
191 kfree(tty);
192 }
194 /*
195 * This routine returns the name of tty.
196 */
197 static char *
198 _tty_make_name(struct tty_struct *tty, const char *name, char *buf)
199 {
200 int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
202 if (!tty) /* Hmm. NULL pointer. That's fun. */
203 strcpy(buf, "NULL tty");
204 else
205 sprintf(buf, name,
206 idx + tty->driver.name_base);
208 return buf;
209 }
211 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
212 (tty)->driver.name_base)
214 char *tty_name(struct tty_struct *tty, char *buf)
215 {
216 return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
217 }
219 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
220 const char *routine)
221 {
222 #ifdef TTY_PARANOIA_CHECK
223 static const char badmagic[] = KERN_WARNING
224 "Warning: bad magic number for tty struct (%s) in %s\n";
225 static const char badtty[] = KERN_WARNING
226 "Warning: null TTY for (%s) in %s\n";
228 if (!tty) {
229 printk(badtty, kdevname(device), routine);
230 return 1;
231 }
232 if (tty->magic != TTY_MAGIC) {
233 printk(badmagic, kdevname(device), routine);
234 return 1;
235 }
236 #endif
237 return 0;
238 }
240 static int check_tty_count(struct tty_struct *tty, const char *routine)
241 {
242 #ifdef CHECK_TTY_COUNT
243 struct list_head *p;
244 int count = 0;
246 file_list_lock();
247 for(p = tty->tty_files.next; p != &tty->tty_files; p = p->next) {
248 if(list_entry(p, struct file, f_list)->private_data == tty)
249 count++;
250 }
251 file_list_unlock();
252 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
253 tty->driver.subtype == PTY_TYPE_SLAVE &&
254 tty->link && tty->link->count)
255 count++;
256 if (tty->count != count) {
257 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
258 "!= #fd's(%d) in %s\n",
259 kdevname(tty->device), tty->count, count, routine);
260 return count;
261 }
262 #endif
263 return 0;
264 }
266 /*
267 * This is probably overkill for real world processors but
268 * they are not on hot paths so a little discipline won't do
269 * any harm.
270 */
272 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
273 {
274 down(&tty->termios_sem);
275 tty->termios->c_line = num;
276 up(&tty->termios_sem);
277 }
279 /*
280 * This guards the refcounted line discipline lists. The lock
281 * must be taken with irqs off because there are hangup path
282 * callers who will do ldisc lookups and cannot sleep.
283 */
285 spinlock_t tty_ldisc_lock = SPIN_LOCK_UNLOCKED;
286 DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
287 struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
289 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
290 {
292 unsigned long flags;
293 int ret = 0;
295 if (disc < N_TTY || disc >= NR_LDISCS)
296 return -EINVAL;
298 spin_lock_irqsave(&tty_ldisc_lock, flags);
299 if (new_ldisc) {
300 tty_ldiscs[disc] = *new_ldisc;
301 tty_ldiscs[disc].num = disc;
302 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
303 tty_ldiscs[disc].refcount = 0;
304 } else {
305 if(tty_ldiscs[disc].refcount)
306 ret = -EBUSY;
307 else
308 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
309 }
310 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
312 return ret;
314 }
317 EXPORT_SYMBOL(tty_register_ldisc);
319 struct tty_ldisc *tty_ldisc_get(int disc)
320 {
321 unsigned long flags;
322 struct tty_ldisc *ld;
324 if (disc < N_TTY || disc >= NR_LDISCS)
325 return NULL;
327 spin_lock_irqsave(&tty_ldisc_lock, flags);
329 ld = &tty_ldiscs[disc];
330 /* Check the entry is defined */
331 if(ld->flags & LDISC_FLAG_DEFINED)
332 ld->refcount++;
333 else
334 ld = NULL;
335 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
336 return ld;
337 }
339 EXPORT_SYMBOL_GPL(tty_ldisc_get);
341 void tty_ldisc_put(int disc)
342 {
343 struct tty_ldisc *ld;
344 unsigned long flags;
346 if (disc < N_TTY || disc >= NR_LDISCS)
347 BUG();
349 spin_lock_irqsave(&tty_ldisc_lock, flags);
350 ld = &tty_ldiscs[disc];
351 if(ld->refcount <= 0)
352 BUG();
353 ld->refcount--;
354 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
355 }
357 EXPORT_SYMBOL_GPL(tty_ldisc_put);
359 void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
360 {
361 tty->ldisc = *ld;
362 tty->ldisc.refcount = 0;
363 }
365 /**
366 * tty_ldisc_try - internal helper
367 * @tty: the tty
368 *
369 * Make a single attempt to grab and bump the refcount on
370 * the tty ldisc. Return 0 on failure or 1 on success. This is
371 * used to implement both the waiting and non waiting versions
372 * of tty_ldisc_ref
373 */
375 static int tty_ldisc_try(struct tty_struct *tty)
376 {
377 unsigned long flags;
378 struct tty_ldisc *ld;
379 int ret = 0;
381 spin_lock_irqsave(&tty_ldisc_lock, flags);
382 ld = &tty->ldisc;
383 if(test_bit(TTY_LDISC, &tty->flags))
384 {
385 ld->refcount++;
386 ret = 1;
387 }
388 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
389 return ret;
390 }
392 /**
393 * tty_ldisc_ref_wait - wait for the tty ldisc
394 * @tty: tty device
395 *
396 * Dereference the line discipline for the terminal and take a
397 * reference to it. If the line discipline is in flux then
398 * wait patiently until it changes.
399 *
400 * Note: Must not be called from an IRQ/timer context. The caller
401 * must also be careful not to hold other locks that will deadlock
402 * against a discipline change, such as an existing ldisc reference
403 * (which we check for)
404 */
406 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
407 {
408 /* wait_event is a macro */
409 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
410 return &tty->ldisc;
411 }
413 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
415 /**
416 * tty_ldisc_ref - get the tty ldisc
417 * @tty: tty device
418 *
419 * Dereference the line discipline for the terminal and take a
420 * reference to it. If the line discipline is in flux then
421 * return NULL. Can be called from IRQ and timer functions.
422 */
424 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
425 {
426 if(tty_ldisc_try(tty))
427 return &tty->ldisc;
428 return NULL;
429 }
431 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
434 void tty_ldisc_deref(struct tty_ldisc *ld)
435 {
437 unsigned long flags;
439 if(ld == NULL)
440 BUG();
442 spin_lock_irqsave(&tty_ldisc_lock, flags);
443 if(ld->refcount == 0)
444 printk(KERN_EMERG "tty_ldisc_deref: no references.\n");
445 else
446 ld->refcount--;
447 if(ld->refcount == 0)
448 wake_up(&tty_ldisc_wait);
449 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
450 }
452 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
454 /**
455 * tty_ldisc_enable - allow ldisc use
456 * @tty: terminal to activate ldisc on
457 *
458 * Set the TTY_LDISC flag when the line discipline can be called
459 * again. Do neccessary wakeups for existing sleepers.
460 *
461 * Note: nobody should set this bit except via this function. Clearing
462 * directly is allowed.
463 */
465 static void tty_ldisc_enable(struct tty_struct *tty)
466 {
467 set_bit(TTY_LDISC, &tty->flags);
468 wake_up(&tty_ldisc_wait);
469 }
471 /**
472 * tty_set_ldisc - set line discipline
473 * @tty: the terminal to set
474 * @ldisc: the line discipline
475 *
476 * Set the discipline of a tty line. Must be called from a process
477 * context.
478 */
480 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
481 {
482 int retval = 0;
483 struct tty_ldisc o_ldisc;
484 char buf[64];
485 unsigned long flags;
486 struct tty_ldisc *ld;
488 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
489 return -EINVAL;
491 restart:
493 if (tty->ldisc.num == ldisc)
494 return 0; /* We are already in the desired discipline */
496 ld = tty_ldisc_get(ldisc);
497 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
498 /* Cyrus Durgin <cider@speakeasy.org> */
499 if (ld == NULL)
500 {
501 char modname [20];
502 sprintf(modname, "tty-ldisc-%d", ldisc);
503 request_module (modname);
504 ld = tty_ldisc_get(ldisc);
505 }
507 if (ld == NULL)
508 return -EINVAL;
511 o_ldisc = tty->ldisc;
512 tty_wait_until_sent(tty, 0);
514 /*
515 * Make sure we don't change while someone holds a
516 * reference to the line discipline. The TTY_LDISC bit
517 * prevents anyone taking a reference once it is clear.
518 * We need the lock to avoid racing reference takers.
519 */
521 spin_lock_irqsave(&tty_ldisc_lock, flags);
522 if(tty->ldisc.refcount)
523 {
524 /* Free the new ldisc we grabbed. Must drop the lock
525 first. */
526 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
527 tty_ldisc_put(ldisc);
528 /*
529 * There are several reasons we may be busy, including
530 * random momentary I/O traffic. We must therefore
531 * retry. We could distinguish between blocking ops
532 * and retries if we made tty_ldisc_wait() smarter. That
533 * is up for discussion.
534 */
535 if(wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
536 return -ERESTARTSYS;
537 goto restart;
538 }
539 clear_bit(TTY_LDISC, &tty->flags);
540 clear_bit(TTY_DONT_FLIP, &tty->flags);
541 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
543 /*
544 * From this point on we know nobody has an ldisc
545 * usage reference, nor can they obtain one until
546 * we say so later on.
547 */
549 /*
550 * Wait for ->hangup_work and ->flip.work handlers to terminate
551 */
552 run_task_queue(&tq_timer);
553 flush_scheduled_tasks();
555 /* Shutdown the current discipline. */
556 if (tty->ldisc.close)
557 (tty->ldisc.close)(tty);
559 /* Now set up the new line discipline. */
560 tty_ldisc_assign(tty, ld);
561 tty_set_termios_ldisc(tty, ldisc);
562 if (tty->ldisc.open)
563 retval = (tty->ldisc.open)(tty);
564 if (retval < 0) {
565 tty_ldisc_put(ldisc);
566 /* There is an outstanding reference here so this is safe */
567 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
568 tty_set_termios_ldisc(tty, tty->ldisc.num);
569 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
570 tty_ldisc_put(o_ldisc.num);
571 /* This driver is always present */
572 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
573 tty_set_termios_ldisc(tty, N_TTY);
574 if (tty->ldisc.open) {
575 int r = tty->ldisc.open(tty);
577 if (r < 0)
578 panic("Couldn't open N_TTY ldisc for "
579 "%s --- error %d.",
580 tty_name(tty, buf), r);
581 }
582 }
583 }
584 /* At this point we hold a reference to the new ldisc and a
585 reference to the old ldisc. If we ended up flipping back
586 to the existing ldisc we have two references to it */
588 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
589 tty->driver.set_ldisc(tty);
591 tty_ldisc_put(o_ldisc.num);
593 /*
594 * Allow ldisc referencing to occur as soon as the driver
595 * ldisc callback completes.
596 */
597 tty_ldisc_enable(tty);
599 return retval;
600 }
602 /*
603 * This routine returns a tty driver structure, given a device number
604 */
605 struct tty_driver *get_tty_driver(kdev_t device)
606 {
607 int major, minor;
608 struct tty_driver *p;
610 minor = MINOR(device);
611 major = MAJOR(device);
613 for (p = tty_drivers; p; p = p->next) {
614 if (p->major != major)
615 continue;
616 if (minor < p->minor_start)
617 continue;
618 if (minor >= p->minor_start + p->num)
619 continue;
620 return p;
621 }
622 return NULL;
623 }
625 /*
626 * If we try to write to, or set the state of, a terminal and we're
627 * not in the foreground, send a SIGTTOU. If the signal is blocked or
628 * ignored, go ahead and perform the operation. (POSIX 7.2)
629 */
630 int tty_check_change(struct tty_struct * tty)
631 {
632 if (current->tty != tty)
633 return 0;
634 if (tty->pgrp <= 0) {
635 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
636 return 0;
637 }
638 if (current->pgrp == tty->pgrp)
639 return 0;
640 if (is_ignored(SIGTTOU))
641 return 0;
642 if (is_orphaned_pgrp(current->pgrp))
643 return -EIO;
644 (void) kill_pg(current->pgrp,SIGTTOU,1);
645 return -ERESTARTSYS;
646 }
648 static ssize_t hung_up_tty_read(struct file * file, char * buf,
649 size_t count, loff_t *ppos)
650 {
651 /* Can't seek (pread) on ttys. */
652 if (ppos != &file->f_pos)
653 return -ESPIPE;
654 return 0;
655 }
657 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
658 size_t count, loff_t *ppos)
659 {
660 /* Can't seek (pwrite) on ttys. */
661 if (ppos != &file->f_pos)
662 return -ESPIPE;
663 return -EIO;
664 }
666 /* No kernel lock held - none needed ;) */
667 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
668 {
669 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
670 }
672 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
673 unsigned int cmd, unsigned long arg)
674 {
675 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
676 }
678 static struct file_operations tty_fops = {
679 llseek: no_llseek,
680 read: tty_read,
681 write: tty_write,
682 poll: tty_poll,
683 ioctl: tty_ioctl,
684 open: tty_open,
685 release: tty_release,
686 fasync: tty_fasync,
687 };
689 static struct file_operations hung_up_tty_fops = {
690 llseek: no_llseek,
691 read: hung_up_tty_read,
692 write: hung_up_tty_write,
693 poll: hung_up_tty_poll,
694 ioctl: hung_up_tty_ioctl,
695 release: tty_release,
696 };
698 static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
699 static struct file *redirect;
701 /**
702 * tty_wakeup - request more data
703 * @tty: terminal
704 *
705 * Internal and external helper for wakeups of tty. This function
706 * informs the line discipline if present that the driver is ready\
707 * to receive more output data.
708 */
710 void tty_wakeup(struct tty_struct *tty)
711 {
712 struct tty_ldisc *ld;
714 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
715 ld = tty_ldisc_ref(tty);
716 if(ld) {
717 if(ld->write_wakeup)
718 ld->write_wakeup(tty);
719 tty_ldisc_deref(ld);
720 }
721 }
722 wake_up_interruptible(&tty->write_wait);
723 }
725 /*
726 * tty_wakeup/tty_ldisc_flush are actually _GPL exports but we can't do
727 * that in 2.4 for modutils compat reasons.
728 */
729 EXPORT_SYMBOL(tty_wakeup);
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 }
743 /*
744 * tty_wakeup/tty_ldisc_flush are actually _GPL exports but we can't do
745 * that in 2.4 for modutils compat reasons.
746 */
747 EXPORT_SYMBOL(tty_ldisc_flush);
749 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 *f = NULL;
754 struct task_struct *p;
755 struct list_head *l;
756 struct tty_ldisc *ld;
757 int closecount = 0, n;
759 if (!tty)
760 return;
762 /* inuse_filps is protected by the single kernel lock */
763 lock_kernel();
765 spin_lock(&redirect_lock);
766 if (redirect && redirect->private_data == tty) {
767 f = redirect;
768 redirect = NULL;
769 }
770 spin_unlock(&redirect_lock);
772 check_tty_count(tty, "do_tty_hangup");
773 file_list_lock();
774 for (l = tty->tty_files.next; l != &tty->tty_files; l = l->next) {
775 struct file * filp = list_entry(l, struct file, f_list);
776 if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
777 filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
778 cons_filp = filp;
779 continue;
780 }
781 if (filp->f_op != &tty_fops)
782 continue;
783 closecount++;
784 tty_fasync(-1, filp, 0); /* can't block */
785 filp->f_op = &hung_up_tty_fops;
786 }
787 file_list_unlock();
789 /* FIXME! What are the locking issues here? This may me overdoing things.. */
790 ld = tty_ldisc_ref(tty);
791 if(ld != NULL)
792 {
793 if (ld->flush_buffer)
794 ld->flush_buffer(tty);
795 if (tty->driver.flush_buffer)
796 tty->driver.flush_buffer(tty);
797 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && ld->write_wakeup)
798 ld->write_wakeup(tty);
799 if (ld->hangup)
800 ld->hangup(tty);
801 }
803 /* FIXME: Once we trust the LDISC code better we can wait here for
804 ldisc completion and fix the driver call race */
806 wake_up_interruptible(&tty->write_wait);
807 wake_up_interruptible(&tty->read_wait);
809 /*
810 * Shutdown the current line discipline, and reset it to
811 * N_TTY.
812 */
814 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
815 {
816 down(&tty->termios_sem);
817 *tty->termios = tty->driver.init_termios;
818 up(&tty->termios_sem);
819 }
821 /* Defer ldisc switch */
822 /* tty_deferred_ldisc_switch(N_TTY)
823 This should get done automatically when the port closes and
824 tty_release is called */
826 read_lock(&tasklist_lock);
827 for_each_task(p) {
828 if ((tty->session > 0) && (p->session == tty->session) &&
829 p->leader) {
830 send_sig(SIGHUP,p,1);
831 send_sig(SIGCONT,p,1);
832 if (tty->pgrp > 0)
833 p->tty_old_pgrp = tty->pgrp;
834 }
835 if (p->tty == tty)
836 p->tty = NULL;
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_task(&tty->tq_hangup);
880 }
882 void tty_vhangup(struct tty_struct * tty)
883 {
884 #ifdef TTY_DEBUG_HANGUP
885 char buf[64];
887 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
888 #endif
889 do_tty_hangup((void *) tty);
890 }
892 int tty_hung_up_p(struct file * filp)
893 {
894 return (filp->f_op == &hung_up_tty_fops);
895 }
897 /*
898 * This function is typically called only by the session leader, when
899 * it wants to disassociate itself from its controlling tty.
900 *
901 * It performs the following functions:
902 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
903 * (2) Clears the tty from being controlling the session
904 * (3) Clears the controlling tty for all processes in the
905 * session group.
906 *
907 * The argument on_exit is set to 1 if called when a process is
908 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
909 */
910 void disassociate_ctty(int on_exit)
911 {
912 struct tty_struct *tty = current->tty;
913 struct task_struct *p;
914 int tty_pgrp = -1;
916 if (tty) {
917 tty_pgrp = tty->pgrp;
918 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
919 tty_vhangup(tty);
920 } else {
921 if (current->tty_old_pgrp) {
922 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
923 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
924 }
925 return;
926 }
927 if (tty_pgrp > 0) {
928 kill_pg(tty_pgrp, SIGHUP, on_exit);
929 if (!on_exit)
930 kill_pg(tty_pgrp, SIGCONT, on_exit);
931 }
933 current->tty_old_pgrp = 0;
934 tty->session = 0;
935 tty->pgrp = -1;
937 read_lock(&tasklist_lock);
938 for_each_task(p)
939 if (p->session == current->session)
940 p->tty = NULL;
941 read_unlock(&tasklist_lock);
942 }
944 void stop_tty(struct tty_struct *tty)
945 {
946 if (tty->stopped)
947 return;
948 tty->stopped = 1;
949 if (tty->link && tty->link->packet) {
950 tty->ctrl_status &= ~TIOCPKT_START;
951 tty->ctrl_status |= TIOCPKT_STOP;
952 wake_up_interruptible(&tty->link->read_wait);
953 }
954 if (tty->driver.stop)
955 (tty->driver.stop)(tty);
956 }
958 void start_tty(struct tty_struct *tty)
959 {
960 if (!tty->stopped || tty->flow_stopped)
961 return;
962 tty->stopped = 0;
963 if (tty->link && tty->link->packet) {
964 tty->ctrl_status &= ~TIOCPKT_STOP;
965 tty->ctrl_status |= TIOCPKT_START;
966 wake_up_interruptible(&tty->link->read_wait);
967 }
968 if (tty->driver.start)
969 (tty->driver.start)(tty);
970 /* If we have a running line discipline it may need kicking */
971 tty_wakeup(tty);
972 }
974 static ssize_t tty_read(struct file * file, char * buf, size_t count,
975 loff_t *ppos)
976 {
977 int i;
978 struct tty_struct * tty;
979 struct inode *inode;
980 struct tty_ldisc *ld;
982 /* Can't seek (pread) on ttys. */
983 if (ppos != &file->f_pos)
984 return -ESPIPE;
986 tty = (struct tty_struct *)file->private_data;
987 inode = file->f_dentry->d_inode;
988 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
989 return -EIO;
990 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
991 return -EIO;
993 /* This check not only needs to be done before reading, but also
994 whenever read_chan() gets woken up after sleeping, so I've
995 moved it to there. This should only be done for the N_TTY
996 line discipline, anyway. Same goes for write_chan(). -- jlc. */
997 #if 0
998 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
999 (tty->pgrp > 0) &&
1000 (current->tty == tty) &&
1001 (tty->pgrp != current->pgrp))
1002 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
1003 return -EIO;
1004 else {
1005 (void) kill_pg(current->pgrp, SIGTTIN, 1);
1006 return -ERESTARTSYS;
1008 #endif
1009 /* We want to wait for the line discipline to sort out in this
1010 situation */
1011 ld = tty_ldisc_ref_wait(tty);
1012 lock_kernel();
1013 if (ld->read)
1014 i = (ld->read)(tty,file,buf,count);
1015 else
1016 i = -EIO;
1017 tty_ldisc_deref(ld);
1018 unlock_kernel();
1019 if (i > 0)
1020 inode->i_atime = CURRENT_TIME;
1021 return i;
1024 /*
1025 * Split writes up in sane blocksizes to avoid
1026 * denial-of-service type attacks
1027 */
1028 static inline ssize_t do_tty_write(
1029 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1030 struct tty_struct *tty,
1031 struct file *file,
1032 const unsigned char *buf,
1033 size_t count)
1035 ssize_t ret = 0, written = 0;
1037 if (file->f_flags & O_NONBLOCK) {
1038 if (down_trylock(&tty->atomic_write))
1039 return -EAGAIN;
1041 else {
1042 if (down_interruptible(&tty->atomic_write))
1043 return -ERESTARTSYS;
1045 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
1046 lock_kernel();
1047 written = write(tty, file, buf, count);
1048 unlock_kernel();
1049 } else {
1050 for (;;) {
1051 unsigned long size = MAX(PAGE_SIZE*2,16384);
1052 if (size > count)
1053 size = count;
1054 lock_kernel();
1055 ret = write(tty, file, buf, size);
1056 unlock_kernel();
1057 if (ret <= 0)
1058 break;
1059 written += ret;
1060 buf += ret;
1061 count -= ret;
1062 if (!count)
1063 break;
1064 ret = -ERESTARTSYS;
1065 if (signal_pending(current))
1066 break;
1067 if (current->need_resched)
1068 schedule();
1071 if (written) {
1072 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1073 ret = written;
1075 up(&tty->atomic_write);
1076 return ret;
1080 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
1081 loff_t *ppos)
1083 int is_console;
1084 struct tty_struct * tty;
1085 struct inode *inode = file->f_dentry->d_inode;
1086 ssize_t ret;
1087 struct tty_ldisc *ld;
1089 /* Can't seek (pwrite) on ttys. */
1090 if (ppos != &file->f_pos)
1091 return -ESPIPE;
1093 /*
1094 * For now, we redirect writes from /dev/console as
1095 * well as /dev/tty0.
1096 */
1097 inode = file->f_dentry->d_inode;
1098 is_console = (inode->i_rdev == SYSCONS_DEV ||
1099 inode->i_rdev == CONSOLE_DEV);
1101 if (is_console) {
1102 struct file *p = NULL;
1104 spin_lock(&redirect_lock);
1105 if (redirect) {
1106 get_file(redirect);
1107 p = redirect;
1109 spin_unlock(&redirect_lock);
1111 if (p) {
1112 ssize_t res = p->f_op->write(p, buf, count, &p->f_pos);
1113 fput(p);
1114 return res;
1118 tty = (struct tty_struct *)file->private_data;
1119 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
1120 return -EIO;
1121 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
1122 return -EIO;
1123 #if 0
1124 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
1125 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
1126 if (is_orphaned_pgrp(current->pgrp))
1127 return -EIO;
1128 if (!is_ignored(SIGTTOU)) {
1129 (void) kill_pg(current->pgrp, SIGTTOU, 1);
1130 return -ERESTARTSYS;
1133 #endif
1135 ld = tty_ldisc_ref_wait(tty);
1136 if (!ld->write)
1137 ret = -EIO;
1138 else
1139 ret = do_tty_write(ld->write, tty, file,
1140 (const unsigned char __user *)buf, count);
1141 tty_ldisc_deref(ld);
1142 return ret;
1145 /* Semaphore to protect creating and releasing a tty. This is shared with
1146 vt.c for deeply disgusting hack reasons */
1147 static DECLARE_MUTEX(tty_sem);
1149 static void down_tty_sem(int index)
1151 down(&tty_sem);
1154 static void up_tty_sem(int index)
1156 up(&tty_sem);
1159 static void release_mem(struct tty_struct *tty, int idx);
1161 /*
1162 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1163 * failed open. The new code protects the open with a semaphore, so it's
1164 * really quite straightforward. The semaphore locking can probably be
1165 * relaxed for the (most common) case of reopening a tty.
1166 */
1167 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
1169 struct tty_struct *tty, *o_tty;
1170 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
1171 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1172 struct tty_driver *driver;
1173 int retval=0;
1174 int idx;
1176 driver = get_tty_driver(device);
1177 if (!driver)
1178 return -ENODEV;
1180 idx = MINOR(device) - driver->minor_start;
1182 /*
1183 * Check whether we need to acquire the tty semaphore to avoid
1184 * race conditions. For now, play it safe.
1185 */
1186 down_tty_sem(idx);
1188 /* check whether we're reopening an existing tty */
1189 tty = driver->table[idx];
1190 if (tty) goto fast_track;
1192 /*
1193 * First time open is complex, especially for PTY devices.
1194 * This code guarantees that either everything succeeds and the
1195 * TTY is ready for operation, or else the table slots are vacated
1196 * and the allocated memory released. (Except that the termios
1197 * and locked termios may be retained.)
1198 */
1200 o_tty = NULL;
1201 tp = o_tp = NULL;
1202 ltp = o_ltp = NULL;
1204 tty = alloc_tty_struct();
1205 if(!tty)
1206 goto fail_no_mem;
1207 initialize_tty_struct(tty);
1208 tty->device = device;
1209 tty->driver = *driver;
1211 tp_loc = &driver->termios[idx];
1212 if (!*tp_loc) {
1213 tp = (struct termios *) kmalloc(sizeof(struct termios),
1214 GFP_KERNEL);
1215 if (!tp)
1216 goto free_mem_out;
1217 *tp = driver->init_termios;
1220 ltp_loc = &driver->termios_locked[idx];
1221 if (!*ltp_loc) {
1222 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1223 GFP_KERNEL);
1224 if (!ltp)
1225 goto free_mem_out;
1226 memset(ltp, 0, sizeof(struct termios));
1229 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1230 o_tty = alloc_tty_struct();
1231 if (!o_tty)
1232 goto free_mem_out;
1233 initialize_tty_struct(o_tty);
1234 o_tty->device = (kdev_t) MKDEV(driver->other->major,
1235 driver->other->minor_start + idx);
1236 o_tty->driver = *driver->other;
1238 o_tp_loc = &driver->other->termios[idx];
1239 if (!*o_tp_loc) {
1240 o_tp = (struct termios *)
1241 kmalloc(sizeof(struct termios), GFP_KERNEL);
1242 if (!o_tp)
1243 goto free_mem_out;
1244 *o_tp = driver->other->init_termios;
1247 o_ltp_loc = &driver->other->termios_locked[idx];
1248 if (!*o_ltp_loc) {
1249 o_ltp = (struct termios *)
1250 kmalloc(sizeof(struct termios), GFP_KERNEL);
1251 if (!o_ltp)
1252 goto free_mem_out;
1253 memset(o_ltp, 0, sizeof(struct termios));
1256 /*
1257 * Everything allocated ... set up the o_tty structure.
1258 */
1259 driver->other->table[idx] = o_tty;
1260 if (!*o_tp_loc)
1261 *o_tp_loc = o_tp;
1262 if (!*o_ltp_loc)
1263 *o_ltp_loc = o_ltp;
1264 o_tty->termios = *o_tp_loc;
1265 o_tty->termios_locked = *o_ltp_loc;
1266 (*driver->other->refcount)++;
1267 if (driver->subtype == PTY_TYPE_MASTER)
1268 o_tty->count++;
1270 /* Establish the links in both directions */
1271 tty->link = o_tty;
1272 o_tty->link = tty;
1275 /*
1276 * All structures have been allocated, so now we install them.
1277 * Failures after this point use release_mem to clean up, so
1278 * there's no need to null out the local pointers.
1279 */
1280 driver->table[idx] = tty;
1282 if (!*tp_loc)
1283 *tp_loc = tp;
1284 if (!*ltp_loc)
1285 *ltp_loc = ltp;
1286 tty->termios = *tp_loc;
1287 tty->termios_locked = *ltp_loc;
1288 (*driver->refcount)++;
1289 tty->count++;
1291 /*
1292 * Structures all installed ... call the ldisc open routines.
1293 * If we fail here just call release_mem to clean up. No need
1294 * to decrement the use counts, as release_mem doesn't care.
1295 */
1296 if (tty->ldisc.open) {
1297 retval = (tty->ldisc.open)(tty);
1298 if (retval)
1299 goto release_mem_out;
1301 if (o_tty && o_tty->ldisc.open) {
1302 retval = (o_tty->ldisc.open)(o_tty);
1303 if (retval) {
1304 if (tty->ldisc.close)
1305 (tty->ldisc.close)(tty);
1306 goto release_mem_out;
1308 set_bit(TTY_LDISC, &o_tty->flags);
1309 tty_ldisc_enable(o_tty);
1311 tty_ldisc_enable(tty);
1312 goto success;
1314 /*
1315 * This fast open can be used if the tty is already open.
1316 * No memory is allocated, and the only failures are from
1317 * attempting to open a closing tty or attempting multiple
1318 * opens on a pty master.
1319 */
1320 fast_track:
1321 if (test_bit(TTY_CLOSING, &tty->flags)) {
1322 retval = -EIO;
1323 goto end_init;
1325 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1326 driver->subtype == PTY_TYPE_MASTER) {
1327 /*
1328 * special case for PTY masters: only one open permitted,
1329 * and the slave side open count is incremented as well.
1330 */
1331 if (tty->count) {
1332 retval = -EIO;
1333 goto end_init;
1335 tty->link->count++;
1337 tty->count++;
1338 tty->driver = *driver; /* N.B. why do this every time?? */
1339 /* FIXME */
1340 if(!test_bit(TTY_LDISC, &tty->flags))
1341 printk(KERN_ERR "init_dev but no ldisc\n");
1342 success:
1343 *ret_tty = tty;
1345 /* All paths come through here to release the semaphore */
1346 end_init:
1347 up_tty_sem(idx);
1348 return retval;
1350 /* Release locally allocated memory ... nothing placed in slots */
1351 free_mem_out:
1352 if (o_tp)
1353 kfree(o_tp);
1354 if (o_tty)
1355 free_tty_struct(o_tty);
1356 if (ltp)
1357 kfree(ltp);
1358 if (tp)
1359 kfree(tp);
1360 free_tty_struct(tty);
1362 fail_no_mem:
1363 retval = -ENOMEM;
1364 goto end_init;
1366 /* call the tty release_mem routine to clean out this slot */
1367 release_mem_out:
1368 printk(KERN_INFO "init_dev: ldisc open failed, "
1369 "clearing slot %d\n", idx);
1370 release_mem(tty, idx);
1371 goto end_init;
1374 /*
1375 * Releases memory associated with a tty structure, and clears out the
1376 * driver table slots.
1377 */
1378 static void release_mem(struct tty_struct *tty, int idx)
1380 struct tty_struct *o_tty;
1381 struct termios *tp;
1383 if ((o_tty = tty->link) != NULL) {
1384 o_tty->driver.table[idx] = NULL;
1385 if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1386 tp = o_tty->driver.termios[idx];
1387 o_tty->driver.termios[idx] = NULL;
1388 kfree(tp);
1390 o_tty->magic = 0;
1391 (*o_tty->driver.refcount)--;
1392 list_del_init(&o_tty->tty_files);
1393 free_tty_struct(o_tty);
1396 tty->driver.table[idx] = NULL;
1397 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1398 tp = tty->driver.termios[idx];
1399 tty->driver.termios[idx] = NULL;
1400 kfree(tp);
1402 tty->magic = 0;
1403 (*tty->driver.refcount)--;
1404 list_del_init(&tty->tty_files);
1405 free_tty_struct(tty);
1408 /*
1409 * Even releasing the tty structures is a tricky business.. We have
1410 * to be very careful that the structures are all released at the
1411 * same time, as interrupts might otherwise get the wrong pointers.
1413 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1414 * lead to double frees or releasing memory still in use.
1415 */
1416 static void release_dev(struct file * filp)
1418 struct tty_struct *tty, *o_tty;
1419 int pty_master, tty_closing, o_tty_closing, do_sleep;
1420 int idx;
1421 char buf[64];
1422 unsigned long flags;
1424 tty = (struct tty_struct *)filp->private_data;
1425 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1426 return;
1428 check_tty_count(tty, "release_dev");
1430 tty_fasync(-1, filp, 0);
1432 idx = MINOR(tty->device) - tty->driver.minor_start;
1433 pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1434 tty->driver.subtype == PTY_TYPE_MASTER);
1435 o_tty = tty->link;
1437 #ifdef TTY_PARANOIA_CHECK
1438 if (idx < 0 || idx >= tty->driver.num) {
1439 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1440 "free (%s)\n", kdevname(tty->device));
1441 return;
1443 if (tty != tty->driver.table[idx]) {
1444 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1445 "for (%s)\n", idx, kdevname(tty->device));
1446 return;
1448 if (tty->termios != tty->driver.termios[idx]) {
1449 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1450 "for (%s)\n",
1451 idx, kdevname(tty->device));
1452 return;
1454 if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1455 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1456 "termios_locked for (%s)\n",
1457 idx, kdevname(tty->device));
1458 return;
1460 #endif
1462 #ifdef TTY_DEBUG_HANGUP
1463 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1464 tty_name(tty, buf), tty->count);
1465 #endif
1467 #ifdef TTY_PARANOIA_CHECK
1468 if (tty->driver.other) {
1469 if (o_tty != tty->driver.other->table[idx]) {
1470 printk(KERN_DEBUG "release_dev: other->table[%d] "
1471 "not o_tty for (%s)\n",
1472 idx, kdevname(tty->device));
1473 return;
1475 if (o_tty->termios != tty->driver.other->termios[idx]) {
1476 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1477 "not o_termios for (%s)\n",
1478 idx, kdevname(tty->device));
1479 return;
1481 if (o_tty->termios_locked !=
1482 tty->driver.other->termios_locked[idx]) {
1483 printk(KERN_DEBUG "release_dev: other->termios_locked["
1484 "%d] not o_termios_locked for (%s)\n",
1485 idx, kdevname(tty->device));
1486 return;
1488 if (o_tty->link != tty) {
1489 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1490 return;
1493 #endif
1495 if (tty->driver.close)
1496 tty->driver.close(tty, filp);
1498 /*
1499 * Sanity check: if tty->count is going to zero, there shouldn't be
1500 * any waiters on tty->read_wait or tty->write_wait. We test the
1501 * wait queues and kick everyone out _before_ actually starting to
1502 * close. This ensures that we won't block while releasing the tty
1503 * structure.
1505 * The test for the o_tty closing is necessary, since the master and
1506 * slave sides may close in any order. If the slave side closes out
1507 * first, its count will be one, since the master side holds an open.
1508 * Thus this test wouldn't be triggered at the time the slave closes,
1509 * so we do it now.
1511 * Note that it's possible for the tty to be opened again while we're
1512 * flushing out waiters. By recalculating the closing flags before
1513 * each iteration we avoid any problems.
1514 */
1515 while (1) {
1516 tty_closing = tty->count <= 1;
1517 o_tty_closing = o_tty &&
1518 (o_tty->count <= (pty_master ? 1 : 0));
1519 do_sleep = 0;
1521 if (tty_closing) {
1522 if (waitqueue_active(&tty->read_wait)) {
1523 wake_up(&tty->read_wait);
1524 do_sleep++;
1526 if (waitqueue_active(&tty->write_wait)) {
1527 wake_up(&tty->write_wait);
1528 do_sleep++;
1531 if (o_tty_closing) {
1532 if (waitqueue_active(&o_tty->read_wait)) {
1533 wake_up(&o_tty->read_wait);
1534 do_sleep++;
1536 if (waitqueue_active(&o_tty->write_wait)) {
1537 wake_up(&o_tty->write_wait);
1538 do_sleep++;
1541 if (!do_sleep)
1542 break;
1544 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1545 "active!\n", tty_name(tty, buf));
1546 schedule();
1549 /*
1550 * The closing flags are now consistent with the open counts on
1551 * both sides, and we've completed the last operation that could
1552 * block, so it's safe to proceed with closing.
1553 */
1554 if (pty_master) {
1555 if (--o_tty->count < 0) {
1556 printk(KERN_WARNING "release_dev: bad pty slave count "
1557 "(%d) for %s\n",
1558 o_tty->count, tty_name(o_tty, buf));
1559 o_tty->count = 0;
1562 if (--tty->count < 0) {
1563 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1564 tty->count, tty_name(tty, buf));
1565 tty->count = 0;
1568 /*
1569 * We've decremented tty->count, so we should zero out
1570 * filp->private_data, to break the link between the tty and
1571 * the file descriptor. Otherwise if filp_close() blocks before
1572 * the file descriptor is removed from the inuse_filp
1573 * list, check_tty_count() could observe a discrepancy and
1574 * printk a warning message to the user.
1575 */
1576 filp->private_data = 0;
1578 /*
1579 * Perform some housekeeping before deciding whether to return.
1581 * Set the TTY_CLOSING flag if this was the last open. In the
1582 * case of a pty we may have to wait around for the other side
1583 * to close, and TTY_CLOSING makes sure we can't be reopened.
1584 */
1585 if(tty_closing)
1586 set_bit(TTY_CLOSING, &tty->flags);
1587 if(o_tty_closing)
1588 set_bit(TTY_CLOSING, &o_tty->flags);
1590 /*
1591 * If _either_ side is closing, make sure there aren't any
1592 * processes that still think tty or o_tty is their controlling
1593 * tty.
1594 */
1595 if (tty_closing || o_tty_closing) {
1596 struct task_struct *p;
1598 read_lock(&tasklist_lock);
1599 for_each_task(p) {
1600 if (p->tty == tty || (o_tty && p->tty == o_tty))
1601 p->tty = NULL;
1603 read_unlock(&tasklist_lock);
1606 /* check whether both sides are closing ... */
1607 if (!tty_closing || (o_tty && !o_tty_closing))
1608 return;
1610 #ifdef TTY_DEBUG_HANGUP
1611 printk(KERN_DEBUG "freeing tty structure...");
1612 #endif
1614 /*
1615 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
1616 * kill any delayed work. As this is the final close it does not
1617 * race with the set_ldisc code path.
1618 */
1619 clear_bit(TTY_LDISC, &tty->flags);
1620 clear_bit(TTY_DONT_FLIP, &tty->flags);
1622 /*
1623 * Wait for ->hangup_work and ->flip.work handlers to terminate
1624 */
1626 run_task_queue(&tq_timer);
1627 flush_scheduled_tasks();
1629 /*
1630 * Wait for any short term users (we know they are just driver
1631 * side waiters as the file is closing so user count on the file
1632 * side is zero.
1633 */
1635 spin_lock_irqsave(&tty_ldisc_lock, flags);
1636 while(tty->ldisc.refcount)
1638 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1639 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
1640 spin_lock_irqsave(&tty_ldisc_lock, flags);
1642 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1644 /*
1645 * Shutdown the current line discipline, and reset it to N_TTY.
1646 * N.B. why reset ldisc when we're releasing the memory??
1647 * FIXME: this MUST get fixed for the new reflocking
1648 */
1649 if (tty->ldisc.close)
1650 (tty->ldisc.close)(tty);
1651 tty_ldisc_put(tty->ldisc.num);
1653 /*
1654 * Switch the line discipline back
1655 */
1656 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1657 tty_set_termios_ldisc(tty,N_TTY);
1659 if (o_tty) {
1660 /* FIXME: could o_tty be in setldisc here ? */
1661 clear_bit(TTY_LDISC, &o_tty->flags);
1662 if (o_tty->ldisc.close)
1663 (o_tty->ldisc.close)(o_tty);
1664 tty_ldisc_put(o_tty->ldisc.num);
1665 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
1666 tty_set_termios_ldisc(o_tty,N_TTY);
1669 /*
1670 * The release_mem function takes care of the details of clearing
1671 * the slots and preserving the termios structure.
1672 */
1673 release_mem(tty, idx);
1676 /*
1677 * tty_open and tty_release keep up the tty count that contains the
1678 * number of opens done on a tty. We cannot use the inode-count, as
1679 * different inodes might point to the same tty.
1681 * Open-counting is needed for pty masters, as well as for keeping
1682 * track of serial lines: DTR is dropped when the last close happens.
1683 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1685 * The termios state of a pty is reset on first open so that
1686 * settings don't persist across reuse.
1687 */
1688 static int tty_open(struct inode * inode, struct file * filp)
1690 struct tty_struct *tty;
1691 int noctty, retval;
1692 kdev_t device;
1693 unsigned short saved_flags;
1694 char buf[64];
1696 saved_flags = filp->f_flags;
1697 retry_open:
1698 noctty = filp->f_flags & O_NOCTTY;
1699 device = inode->i_rdev;
1700 if (device == TTY_DEV) {
1701 if (!current->tty)
1702 return -ENXIO;
1703 device = current->tty->device;
1704 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1705 /* noctty = 1; */
1707 #ifdef CONFIG_VT
1708 if (device == CONSOLE_DEV) {
1709 extern int fg_console;
1710 device = MKDEV(TTY_MAJOR, fg_console + 1);
1711 noctty = 1;
1713 #endif
1714 if (device == SYSCONS_DEV) {
1715 struct console *c = console_drivers;
1716 while(c && !c->device)
1717 c = c->next;
1718 if (!c)
1719 return -ENODEV;
1720 device = c->device(c);
1721 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1722 noctty = 1;
1725 if (device == PTMX_DEV) {
1726 #ifdef CONFIG_UNIX98_PTYS
1728 /* find a free pty. */
1729 int major, minor;
1730 struct tty_driver *driver;
1732 /* find a device that is not in use. */
1733 retval = -1;
1734 for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1735 driver = &ptm_driver[major];
1736 for (minor = driver->minor_start ;
1737 minor < driver->minor_start + driver->num ;
1738 minor++) {
1739 device = MKDEV(driver->major, minor);
1740 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1743 return -EIO; /* no free ptys */
1744 ptmx_found:
1745 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1746 minor -= driver->minor_start;
1747 devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1748 tty_register_devfs(&pts_driver[major], DEVFS_FL_DEFAULT,
1749 pts_driver[major].minor_start + minor);
1750 noctty = 1;
1751 goto init_dev_done;
1753 #else /* CONFIG_UNIX_98_PTYS */
1755 return -ENODEV;
1757 #endif /* CONFIG_UNIX_98_PTYS */
1760 retval = init_dev(device, &tty);
1761 if (retval)
1762 return retval;
1764 #ifdef CONFIG_UNIX98_PTYS
1765 init_dev_done:
1766 #endif
1767 filp->private_data = tty;
1768 file_move(filp, &tty->tty_files);
1769 check_tty_count(tty, "tty_open");
1770 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1771 tty->driver.subtype == PTY_TYPE_MASTER)
1772 noctty = 1;
1773 #ifdef TTY_DEBUG_HANGUP
1774 printk(KERN_DEBUG "opening %s...", tty_name(tty, buf));
1775 #endif
1776 if (tty->driver.open)
1777 retval = tty->driver.open(tty, filp);
1778 else
1779 retval = -ENODEV;
1780 filp->f_flags = saved_flags;
1782 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1783 retval = -EBUSY;
1785 if (retval) {
1786 #ifdef TTY_DEBUG_HANGUP
1787 printk(KERN_DEBUG "error %d in opening %s...", retval,
1788 tty_name(tty, buf));
1789 #endif
1791 release_dev(filp);
1792 if (retval != -ERESTARTSYS)
1793 return retval;
1794 if (signal_pending(current))
1795 return retval;
1796 schedule();
1797 /*
1798 * Need to reset f_op in case a hangup happened.
1799 */
1800 filp->f_op = &tty_fops;
1801 goto retry_open;
1803 if (!noctty &&
1804 current->leader &&
1805 !current->tty &&
1806 tty->session == 0) {
1807 task_lock(current);
1808 current->tty = tty;
1809 task_unlock(current);
1810 current->tty_old_pgrp = 0;
1811 tty->session = current->session;
1812 tty->pgrp = current->pgrp;
1814 if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1815 (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1816 (tty->count == 1)) {
1817 static int nr_warns;
1818 if (nr_warns < 5) {
1819 printk(KERN_WARNING "tty_io.c: "
1820 "process %d (%s) used obsolete /dev/%s - "
1821 "update software to use /dev/ttyS%d\n",
1822 current->pid, current->comm,
1823 tty_name(tty, buf), TTY_NUMBER(tty));
1824 nr_warns++;
1827 return 0;
1830 static int tty_release(struct inode * inode, struct file * filp)
1832 lock_kernel();
1833 release_dev(filp);
1834 unlock_kernel();
1835 return 0;
1838 /* No kernel lock held - fine */
1839 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1841 struct tty_struct * tty;
1842 struct tty_ldisc *ld;
1843 int ret = 0;
1845 tty = (struct tty_struct *)filp->private_data;
1846 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1847 return 0;
1849 ld = tty_ldisc_ref_wait(tty);
1850 if (ld->poll)
1851 ret = (ld->poll)(tty, filp, wait);
1852 tty_ldisc_deref(ld);
1853 return ret;
1856 static int tty_fasync(int fd, struct file * filp, int on)
1858 struct tty_struct * tty;
1859 int retval;
1861 tty = (struct tty_struct *)filp->private_data;
1862 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1863 return 0;
1865 retval = fasync_helper(fd, filp, on, &tty->fasync);
1866 if (retval <= 0)
1867 return retval;
1869 if (on) {
1870 if (!waitqueue_active(&tty->read_wait))
1871 tty->minimum_to_wake = 1;
1872 if (filp->f_owner.pid == 0) {
1873 filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1874 filp->f_owner.uid = current->uid;
1875 filp->f_owner.euid = current->euid;
1877 } else {
1878 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1879 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1881 return 0;
1884 static int tiocsti(struct tty_struct *tty, char * arg)
1886 char ch, mbz = 0;
1887 struct tty_ldisc *ld;
1889 if ((current->tty != tty) && !suser())
1890 return -EPERM;
1891 if (get_user(ch, arg))
1892 return -EFAULT;
1893 ld = tty_ldisc_ref_wait(tty);
1894 ld->receive_buf(tty, &ch, &mbz, 1);
1895 tty_ldisc_deref(ld);
1896 return 0;
1899 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1901 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1902 return -EFAULT;
1903 return 0;
1906 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1907 struct winsize * arg)
1909 struct winsize tmp_ws;
1911 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1912 return -EFAULT;
1913 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1914 return 0;
1915 if (tty->pgrp > 0)
1916 kill_pg(tty->pgrp, SIGWINCH, 1);
1917 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1918 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1919 tty->winsize = tmp_ws;
1920 real_tty->winsize = tmp_ws;
1921 return 0;
1924 static int tioccons(struct inode *inode, struct file *file)
1926 if (inode->i_rdev == SYSCONS_DEV ||
1927 inode->i_rdev == CONSOLE_DEV) {
1928 struct file *f;
1929 if (!suser())
1930 return -EPERM;
1931 spin_lock(&redirect_lock);
1932 f = redirect;
1933 redirect = NULL;
1934 spin_unlock(&redirect_lock);
1935 if (f)
1936 fput(f);
1937 return 0;
1939 spin_lock(&redirect_lock);
1940 if (redirect) {
1941 spin_unlock(&redirect_lock);
1942 return -EBUSY;
1944 get_file(file);
1945 redirect = file;
1946 spin_unlock(&redirect_lock);
1947 return 0;
1951 static int fionbio(struct file *file, int *arg)
1953 int nonblock;
1955 if (get_user(nonblock, arg))
1956 return -EFAULT;
1958 if (nonblock)
1959 file->f_flags |= O_NONBLOCK;
1960 else
1961 file->f_flags &= ~O_NONBLOCK;
1962 return 0;
1965 static int tiocsctty(struct tty_struct *tty, int arg)
1967 if (current->leader &&
1968 (current->session == tty->session))
1969 return 0;
1970 /*
1971 * The process must be a session leader and
1972 * not have a controlling tty already.
1973 */
1974 if (!current->leader || current->tty)
1975 return -EPERM;
1976 if (tty->session > 0) {
1977 /*
1978 * This tty is already the controlling
1979 * tty for another session group!
1980 */
1981 if ((arg == 1) && suser()) {
1982 /*
1983 * Steal it away
1984 */
1985 struct task_struct *p;
1987 read_lock(&tasklist_lock);
1988 for_each_task(p)
1989 if (p->tty == tty)
1990 p->tty = NULL;
1991 read_unlock(&tasklist_lock);
1992 } else
1993 return -EPERM;
1995 task_lock(current);
1996 current->tty = tty;
1997 task_unlock(current);
1998 current->tty_old_pgrp = 0;
1999 tty->session = current->session;
2000 tty->pgrp = current->pgrp;
2001 return 0;
2004 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
2006 /*
2007 * (tty == real_tty) is a cheap way of
2008 * testing if the tty is NOT a master pty.
2009 */
2010 if (tty == real_tty && current->tty != real_tty)
2011 return -ENOTTY;
2012 return put_user(real_tty->pgrp, arg);
2015 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
2017 pid_t pgrp;
2018 int retval = tty_check_change(real_tty);
2020 if (retval == -EIO)
2021 return -ENOTTY;
2022 if (retval)
2023 return retval;
2024 if (!current->tty ||
2025 (current->tty != real_tty) ||
2026 (real_tty->session != current->session))
2027 return -ENOTTY;
2028 if (get_user(pgrp, (pid_t *) arg))
2029 return -EFAULT;
2030 if (pgrp < 0)
2031 return -EINVAL;
2032 if (session_of_pgrp(pgrp) != current->session)
2033 return -EPERM;
2034 real_tty->pgrp = pgrp;
2035 return 0;
2038 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
2040 /*
2041 * (tty == real_tty) is a cheap way of
2042 * testing if the tty is NOT a master pty.
2043 */
2044 if (tty == real_tty && current->tty != real_tty)
2045 return -ENOTTY;
2046 if (real_tty->session <= 0)
2047 return -ENOTTY;
2048 return put_user(real_tty->session, arg);
2051 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
2053 if (copy_to_user(arg, tty, sizeof(*arg)))
2054 return -EFAULT;
2055 return 0;
2058 static int tiocsetd(struct tty_struct *tty, int *arg)
2060 int ldisc;
2062 if (get_user(ldisc, arg))
2063 return -EFAULT;
2064 return tty_set_ldisc(tty, ldisc);
2067 static int send_break(struct tty_struct *tty, int duration)
2069 tty->driver.break_ctl(tty, -1);
2070 if (!signal_pending(current)) {
2071 set_current_state(TASK_INTERRUPTIBLE);
2072 schedule_timeout(duration);
2074 tty->driver.break_ctl(tty, 0);
2075 if (signal_pending(current))
2076 return -EINTR;
2077 return 0;
2080 static int tty_generic_brk(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2082 if (cmd == TCSBRK && arg)
2084 /* tcdrain case */
2085 int retval = tty_check_change(tty);
2086 if (retval)
2087 return retval;
2088 tty_wait_until_sent(tty, 0);
2089 if (signal_pending(current))
2090 return -EINTR;
2092 return 0;
2095 /*
2096 * Split this up, as gcc can choke on it otherwise..
2097 */
2098 int tty_ioctl(struct inode * inode, struct file * file,
2099 unsigned int cmd, unsigned long arg)
2101 struct tty_struct *tty, *real_tty;
2102 int retval;
2103 struct tty_ldisc *ld;
2105 tty = (struct tty_struct *)file->private_data;
2106 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
2107 return -EINVAL;
2109 real_tty = tty;
2110 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
2111 tty->driver.subtype == PTY_TYPE_MASTER)
2112 real_tty = tty->link;
2114 /*
2115 * Break handling by driver
2116 */
2117 if (!tty->driver.break_ctl) {
2118 switch(cmd) {
2119 case TIOCSBRK:
2120 case TIOCCBRK:
2121 if (tty->driver.ioctl)
2122 return tty->driver.ioctl(tty, file, cmd, arg);
2123 return -EINVAL;
2125 /* These two ioctl's always return success; even if */
2126 /* the driver doesn't support them. */
2127 case TCSBRK:
2128 case TCSBRKP:
2129 retval = -ENOIOCTLCMD;
2130 if (tty->driver.ioctl)
2131 retval = tty->driver.ioctl(tty, file, cmd, arg);
2132 /* Not driver handled */
2133 if (retval == -ENOIOCTLCMD)
2134 retval = tty_generic_brk(tty, file, cmd, arg);
2135 return retval;
2139 /*
2140 * Factor out some common prep work
2141 */
2142 switch (cmd) {
2143 case TIOCSETD:
2144 case TIOCSBRK:
2145 case TIOCCBRK:
2146 case TCSBRK:
2147 case TCSBRKP:
2148 retval = tty_check_change(tty);
2149 if (retval)
2150 return retval;
2151 if (cmd != TIOCCBRK) {
2152 tty_wait_until_sent(tty, 0);
2153 if (signal_pending(current))
2154 return -EINTR;
2156 break;
2159 switch (cmd) {
2160 case TIOCSTI:
2161 return tiocsti(tty, (char *)arg);
2162 case TIOCGWINSZ:
2163 return tiocgwinsz(tty, (struct winsize *) arg);
2164 case TIOCSWINSZ:
2165 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
2166 case TIOCCONS:
2167 return real_tty!=tty ? -EINVAL : tioccons(inode, file);
2168 case FIONBIO:
2169 return fionbio(file, (int *) arg);
2170 case TIOCEXCL:
2171 set_bit(TTY_EXCLUSIVE, &tty->flags);
2172 return 0;
2173 case TIOCNXCL:
2174 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2175 return 0;
2176 case TIOCNOTTY:
2177 if (current->tty != tty)
2178 return -ENOTTY;
2179 if (current->leader)
2180 disassociate_ctty(0);
2181 task_lock(current);
2182 current->tty = NULL;
2183 task_unlock(current);
2184 return 0;
2185 case TIOCSCTTY:
2186 return tiocsctty(tty, arg);
2187 case TIOCGPGRP:
2188 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
2189 case TIOCSPGRP:
2190 return tiocspgrp(tty, real_tty, (pid_t *) arg);
2191 case TIOCGSID:
2192 return tiocgsid(tty, real_tty, (pid_t *) arg);
2193 case TIOCGETD:
2194 /* FIXME: check this is ok */
2195 return put_user(tty->ldisc.num, (int *) arg);
2196 case TIOCSETD:
2197 return tiocsetd(tty, (int *) arg);
2198 #ifdef CONFIG_VT
2199 case TIOCLINUX:
2200 return tioclinux(tty, arg);
2201 #endif
2202 case TIOCTTYGSTRUCT:
2203 return tiocttygstruct(tty, (struct tty_struct *) arg);
2205 /*
2206 * Break handling
2207 */
2208 case TIOCSBRK: /* Turn break on, unconditionally */
2209 tty->driver.break_ctl(tty, -1);
2210 return 0;
2212 case TIOCCBRK: /* Turn break off, unconditionally */
2213 tty->driver.break_ctl(tty, 0);
2214 return 0;
2215 case TCSBRK: /* SVID version: non-zero arg --> no break */
2216 /*
2217 * XXX is the above comment correct, or the
2218 * code below correct? Is this ioctl used at
2219 * all by anyone?
2220 */
2221 if (!arg)
2222 return send_break(tty, HZ/4);
2223 return 0;
2224 case TCSBRKP: /* support for POSIX tcsendbreak() */
2225 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
2227 if (tty->driver.ioctl) {
2228 retval = (tty->driver.ioctl)(tty, file, cmd, arg);
2229 if (retval != -ENOIOCTLCMD)
2230 return retval;
2232 ld = tty_ldisc_ref_wait(tty);
2233 retval = -EINVAL;
2234 if (ld->ioctl) {
2235 retval = ld->ioctl(tty, file, cmd, arg);
2236 if (retval == -ENOIOCTLCMD)
2237 retval = -EINVAL;
2239 tty_ldisc_deref(ld);
2240 return retval;
2244 /*
2245 * This implements the "Secure Attention Key" --- the idea is to
2246 * prevent trojan horses by killing all processes associated with this
2247 * tty when the user hits the "Secure Attention Key". Required for
2248 * super-paranoid applications --- see the Orange Book for more details.
2250 * This code could be nicer; ideally it should send a HUP, wait a few
2251 * seconds, then send a INT, and then a KILL signal. But you then
2252 * have to coordinate with the init process, since all processes associated
2253 * with the current tty must be dead before the new getty is allowed
2254 * to spawn.
2256 * Now, if it would be correct ;-/ The current code has a nasty hole -
2257 * it doesn't catch files in flight. We may send the descriptor to ourselves
2258 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2260 * Nasty bug: do_SAK is being called in interrupt context. This can
2261 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2262 */
2263 static void __do_SAK(void *arg)
2265 #ifdef TTY_SOFT_SAK
2266 tty_hangup(tty);
2267 #else
2268 struct tty_struct *tty = arg;
2269 struct task_struct *p;
2270 int session;
2271 int i;
2272 struct file *filp;
2273 struct tty_ldisc *disc;
2275 if (!tty)
2276 return;
2277 session = tty->session;
2278 /* We don't want an ldisc switch during this */
2279 disc = tty_ldisc_ref(tty);
2280 if (disc && disc->flush_buffer)
2281 disc->flush_buffer(tty);
2282 tty_ldisc_deref(disc);
2284 if (tty->driver.flush_buffer)
2285 tty->driver.flush_buffer(tty);
2287 read_lock(&tasklist_lock);
2288 for_each_task(p) {
2289 if ((p->tty == tty) ||
2290 ((session > 0) && (p->session == session))) {
2291 send_sig(SIGKILL, p, 1);
2292 continue;
2294 task_lock(p);
2295 if (p->files) {
2296 read_lock(&p->files->file_lock);
2297 for (i=0; i < p->files->max_fds; i++) {
2298 filp = fcheck_files(p->files, i);
2299 if (filp && (filp->f_op == &tty_fops) &&
2300 (filp->private_data == tty)) {
2301 send_sig(SIGKILL, p, 1);
2302 break;
2305 read_unlock(&p->files->file_lock);
2307 task_unlock(p);
2309 read_unlock(&tasklist_lock);
2310 #endif
2313 /*
2314 * The tq handling here is a little racy - tty->SAK_tq may already be queued.
2315 * But there's no mechanism to fix that without futzing with tqueue_lock.
2316 * Fortunately we don't need to worry, because if ->SAK_tq is already queued,
2317 * the values which we write to it will be identical to the values which it
2318 * already has. --akpm
2319 */
2320 void do_SAK(struct tty_struct *tty)
2322 if (!tty)
2323 return;
2324 PREPARE_TQUEUE(&tty->SAK_tq, __do_SAK, tty);
2325 schedule_task(&tty->SAK_tq);
2328 /*
2329 * This routine is called out of the software interrupt to flush data
2330 * from the flip buffer to the line discipline.
2331 */
2332 static void flush_to_ldisc(void *private_)
2334 struct tty_struct *tty = (struct tty_struct *) private_;
2335 unsigned char *cp;
2336 char *fp;
2337 int count;
2338 unsigned long flags;
2339 struct tty_ldisc *disc;
2341 disc = tty_ldisc_ref(tty);
2342 if (disc == NULL) /* !TTY_LDISC */
2343 return;
2345 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
2346 queue_task(&tty->flip.tqueue, &tq_timer);
2347 goto out;
2349 if (tty->flip.buf_num) {
2350 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2351 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2352 tty->flip.buf_num = 0;
2354 save_flags(flags); cli();
2355 tty->flip.char_buf_ptr = tty->flip.char_buf;
2356 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2357 } else {
2358 cp = tty->flip.char_buf;
2359 fp = tty->flip.flag_buf;
2360 tty->flip.buf_num = 1;
2362 save_flags(flags); cli();
2363 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
2364 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
2366 count = tty->flip.count;
2367 tty->flip.count = 0;
2368 restore_flags(flags);
2370 disc->receive_buf(tty, cp, fp, count);
2371 out:
2372 tty_ldisc_deref(disc);
2375 /*
2376 * Call the ldisc flush directly from a driver. This function may
2377 * return an error and need retrying by the user.
2378 */
2380 int tty_push_data(struct tty_struct *tty, unsigned char *cp, unsigned char *fp, int count)
2382 int ret = 0;
2383 struct tty_ldisc *disc;
2385 disc = tty_ldisc_ref(tty);
2386 if(test_bit(TTY_DONT_FLIP, &tty->flags))
2387 ret = -EAGAIN;
2388 else if(disc == NULL)
2389 ret = -EIO;
2390 else
2391 disc->receive_buf(tty, cp, fp, count);
2392 tty_ldisc_deref(disc);
2393 return ret;
2397 /*
2398 * Routine which returns the baud rate of the tty
2400 * Note that the baud_table needs to be kept in sync with the
2401 * include/asm/termbits.h file.
2402 */
2403 static int baud_table[] = {
2404 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
2405 9600, 19200, 38400, 57600, 115200, 230400, 460800,
2406 #ifdef __sparc__
2407 76800, 153600, 307200, 614400, 921600
2408 #else
2409 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2410 2500000, 3000000, 3500000, 4000000
2411 #endif
2412 };
2414 static int n_baud_table = sizeof(baud_table)/sizeof(int);
2416 int tty_get_baud_rate(struct tty_struct *tty)
2418 unsigned int cflag, i;
2420 cflag = tty->termios->c_cflag;
2422 i = cflag & CBAUD;
2423 if (i & CBAUDEX) {
2424 i &= ~CBAUDEX;
2425 if (i < 1 || i+15 >= n_baud_table)
2426 tty->termios->c_cflag &= ~CBAUDEX;
2427 else
2428 i += 15;
2430 if (i==15 && tty->alt_speed) {
2431 if (!tty->warned) {
2432 printk(KERN_WARNING "Use of setserial/setrocket to "
2433 "set SPD_* flags is deprecated\n");
2434 tty->warned = 1;
2436 return(tty->alt_speed);
2439 return baud_table[i];
2442 void tty_flip_buffer_push(struct tty_struct *tty)
2444 if (tty->low_latency)
2445 flush_to_ldisc((void *) tty);
2446 else
2447 queue_task(&tty->flip.tqueue, &tq_timer);
2450 /*
2451 * This subroutine initializes a tty structure.
2452 */
2453 static void initialize_tty_struct(struct tty_struct *tty)
2455 memset(tty, 0, sizeof(struct tty_struct));
2456 tty->magic = TTY_MAGIC;
2457 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
2458 tty->pgrp = -1;
2459 tty->flip.char_buf_ptr = tty->flip.char_buf;
2460 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2461 tty->flip.tqueue.routine = flush_to_ldisc;
2462 tty->flip.tqueue.data = tty;
2463 init_MUTEX(&tty->flip.pty_sem);
2464 init_MUTEX(&tty->termios_sem);
2465 init_waitqueue_head(&tty->write_wait);
2466 init_waitqueue_head(&tty->read_wait);
2467 tty->tq_hangup.routine = do_tty_hangup;
2468 tty->tq_hangup.data = tty;
2469 sema_init(&tty->atomic_read, 1);
2470 sema_init(&tty->atomic_write, 1);
2471 spin_lock_init(&tty->read_lock);
2472 INIT_LIST_HEAD(&tty->tty_files);
2473 INIT_TQUEUE(&tty->SAK_tq, 0, 0);
2476 /*
2477 * The default put_char routine if the driver did not define one.
2478 */
2479 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2481 tty->driver.write(tty, 0, &ch, 1);
2484 /*
2485 * Register a tty device described by <driver>, with minor number <minor>.
2486 */
2487 void tty_register_devfs (struct tty_driver *driver, unsigned int flags, unsigned minor)
2489 #ifdef CONFIG_DEVFS_FS
2490 umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
2491 kdev_t device = MKDEV (driver->major, minor);
2492 int idx = minor - driver->minor_start;
2493 char buf[32];
2495 switch (device) {
2496 case TTY_DEV:
2497 case PTMX_DEV:
2498 mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2499 break;
2500 default:
2501 if (driver->major == PTY_MASTER_MAJOR)
2502 mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2503 break;
2505 if ( (minor < driver->minor_start) ||
2506 (minor >= driver->minor_start + driver->num) ) {
2507 printk(KERN_ERR "Attempt to register invalid minor number "
2508 "with devfs (%d:%d).\n", (int)driver->major,(int)minor);
2509 return;
2511 # ifdef CONFIG_UNIX98_PTYS
2512 if ( (driver->major >= UNIX98_PTY_SLAVE_MAJOR) &&
2513 (driver->major < UNIX98_PTY_SLAVE_MAJOR + UNIX98_NR_MAJORS) )
2514 flags |= DEVFS_FL_CURRENT_OWNER;
2515 # endif
2516 sprintf(buf, driver->name, idx + driver->name_base);
2517 devfs_register (NULL, buf, flags | DEVFS_FL_DEFAULT,
2518 driver->major, minor, mode, &tty_fops, NULL);
2519 #endif /* CONFIG_DEVFS_FS */
2522 void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2524 #ifdef CONFIG_DEVFS_FS
2525 void * handle;
2526 int idx = minor - driver->minor_start;
2527 char buf[32];
2529 sprintf(buf, driver->name, idx + driver->name_base);
2530 handle = devfs_find_handle (NULL, buf, driver->major, minor,
2531 DEVFS_SPECIAL_CHR, 0);
2532 devfs_unregister (handle);
2533 #endif /* CONFIG_DEVFS_FS */
2536 EXPORT_SYMBOL(tty_register_devfs);
2537 EXPORT_SYMBOL(tty_unregister_devfs);
2539 /*
2540 * Called by a tty driver to register itself.
2541 */
2542 int tty_register_driver(struct tty_driver *driver)
2544 int error;
2545 int i;
2547 if (driver->flags & TTY_DRIVER_INSTALLED)
2548 return 0;
2550 error = devfs_register_chrdev(driver->major, driver->name, &tty_fops);
2551 if (error < 0)
2552 return error;
2553 else if(driver->major == 0)
2554 driver->major = error;
2556 if (!driver->put_char)
2557 driver->put_char = tty_default_put_char;
2559 driver->prev = 0;
2560 driver->next = tty_drivers;
2561 if (tty_drivers) tty_drivers->prev = driver;
2562 tty_drivers = driver;
2564 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2565 for(i = 0; i < driver->num; i++)
2566 tty_register_devfs(driver, 0, driver->minor_start + i);
2568 proc_tty_register_driver(driver);
2569 return error;
2572 /*
2573 * Called by a tty driver to unregister itself.
2574 */
2575 int tty_unregister_driver(struct tty_driver *driver)
2577 int retval;
2578 struct tty_driver *p;
2579 int i, found = 0;
2580 struct termios *tp;
2581 const char *othername = NULL;
2583 if (*driver->refcount)
2584 return -EBUSY;
2586 for (p = tty_drivers; p; p = p->next) {
2587 if (p == driver)
2588 found++;
2589 else if (p->major == driver->major)
2590 othername = p->name;
2593 if (!found)
2594 return -ENOENT;
2596 if (othername == NULL) {
2597 retval = devfs_unregister_chrdev(driver->major, driver->name);
2598 if (retval)
2599 return retval;
2600 } else
2601 devfs_register_chrdev(driver->major, othername, &tty_fops);
2603 if (driver->prev)
2604 driver->prev->next = driver->next;
2605 else
2606 tty_drivers = driver->next;
2608 if (driver->next)
2609 driver->next->prev = driver->prev;
2611 /*
2612 * Free the termios and termios_locked structures because
2613 * we don't want to get memory leaks when modular tty
2614 * drivers are removed from the kernel.
2615 */
2616 for (i = 0; i < driver->num; i++) {
2617 tp = driver->termios[i];
2618 if (tp) {
2619 driver->termios[i] = NULL;
2620 kfree(tp);
2622 tp = driver->termios_locked[i];
2623 if (tp) {
2624 driver->termios_locked[i] = NULL;
2625 kfree(tp);
2627 tty_unregister_devfs(driver, driver->minor_start + i);
2629 proc_tty_unregister_driver(driver);
2630 return 0;
2634 /*
2635 * Initialize the console device. This is called *early*, so
2636 * we can't necessarily depend on lots of kernel help here.
2637 * Just do some early initializations, and do the complex setup
2638 * later.
2639 */
2640 void __init console_init(void)
2642 /* Setup the default TTY line discipline. */
2643 memset(tty_ldiscs, 0, NR_LDISCS*sizeof(struct tty_ldisc));
2644 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2646 /*
2647 * Set up the standard termios. Individual tty drivers may
2648 * deviate from this; this is used as a template.
2649 */
2650 memset(&tty_std_termios, 0, sizeof(struct termios));
2651 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2652 tty_std_termios.c_iflag = ICRNL | IXON;
2653 tty_std_termios.c_oflag = OPOST | ONLCR;
2654 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2655 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2656 ECHOCTL | ECHOKE | IEXTEN;
2658 /*
2659 * set up the console device so that later boot sequences can
2660 * inform about problems etc..
2661 */
2662 #ifdef CONFIG_EARLY_PRINTK
2663 disable_early_printk();
2664 #endif
2666 #ifdef CONFIG_XEN_CONSOLE
2667 xen_console_init();
2668 #endif
2670 #ifdef CONFIG_VT
2671 con_init();
2672 #endif
2673 #ifdef CONFIG_AU1X00_SERIAL_CONSOLE
2674 au1x00_serial_console_init();
2675 #endif
2676 #ifdef CONFIG_SERIAL_CONSOLE
2677 #if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2678 console_8xx_init();
2679 #elif defined(CONFIG_MAC_SERIAL) && defined(CONFIG_SERIAL)
2680 if (_machine == _MACH_Pmac)
2681 mac_scc_console_init();
2682 else
2683 serial_console_init();
2684 #elif defined(CONFIG_MAC_SERIAL)
2685 mac_scc_console_init();
2686 #elif defined(CONFIG_PARISC)
2687 pdc_console_init();
2688 #elif defined(CONFIG_SERIAL)
2689 serial_console_init();
2690 #endif /* CONFIG_8xx */
2691 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2692 vme_scc_console_init();
2693 #endif
2694 #if defined(CONFIG_SERIAL167)
2695 serial167_console_init();
2696 #endif
2697 #if defined(CONFIG_SH_SCI)
2698 sci_console_init();
2699 #endif
2700 #endif
2701 #ifdef CONFIG_SERIAL_DEC_CONSOLE
2702 dec_serial_console_init();
2703 #endif
2704 #ifdef CONFIG_TN3270_CONSOLE
2705 tub3270_con_init();
2706 #endif
2707 #ifdef CONFIG_TN3215
2708 con3215_init();
2709 #endif
2710 #ifdef CONFIG_HWC
2711 hwc_console_init();
2712 #endif
2713 #ifdef CONFIG_STDIO_CONSOLE
2714 stdio_console_init();
2715 #endif
2716 #ifdef CONFIG_SERIAL_21285_CONSOLE
2717 rs285_console_init();
2718 #endif
2719 #ifdef CONFIG_SERIAL_SA1100_CONSOLE
2720 sa1100_rs_console_init();
2721 #endif
2722 #ifdef CONFIG_ARC_CONSOLE
2723 arc_console_init();
2724 #endif
2725 #ifdef CONFIG_SERIAL_AMBA_CONSOLE
2726 ambauart_console_init();
2727 #endif
2728 #ifdef CONFIG_SERIAL_TX3912_CONSOLE
2729 tx3912_console_init();
2730 #endif
2731 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
2732 txx927_console_init();
2733 #endif
2734 #ifdef CONFIG_SERIAL_TXX9_CONSOLE
2735 txx9_serial_console_init();
2736 #endif
2737 #ifdef CONFIG_SIBYTE_SB1250_DUART_CONSOLE
2738 sb1250_serial_console_init();
2739 #endif
2740 #ifdef CONFIG_IP22_SERIAL
2741 sgi_serial_console_init();
2742 #endif
2745 static struct tty_driver dev_tty_driver, dev_syscons_driver;
2746 #ifdef CONFIG_UNIX98_PTYS
2747 static struct tty_driver dev_ptmx_driver;
2748 #endif
2749 #ifdef CONFIG_HVC_CONSOLE
2750 hvc_console_init();
2751 #endif
2752 #ifdef CONFIG_VT
2753 static struct tty_driver dev_console_driver;
2754 #endif
2756 /*
2757 * Ok, now we can initialize the rest of the tty devices and can count
2758 * on memory allocations, interrupts etc..
2759 */
2760 void __init tty_init(void)
2762 /*
2763 * dev_tty_driver and dev_console_driver are actually magic
2764 * devices which get redirected at open time. Nevertheless,
2765 * we register them so that register_chrdev is called
2766 * appropriately.
2767 */
2768 memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2769 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2770 dev_tty_driver.driver_name = "/dev/tty";
2771 dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2772 dev_tty_driver.name_base = 0;
2773 dev_tty_driver.major = TTYAUX_MAJOR;
2774 dev_tty_driver.minor_start = 0;
2775 dev_tty_driver.num = 1;
2776 dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2777 dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2779 if (tty_register_driver(&dev_tty_driver))
2780 panic("Couldn't register /dev/tty driver\n");
2782 dev_syscons_driver = dev_tty_driver;
2783 dev_syscons_driver.driver_name = "/dev/console";
2784 dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2785 dev_syscons_driver.major = TTYAUX_MAJOR;
2786 dev_syscons_driver.minor_start = 1;
2787 dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2788 dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2790 if (tty_register_driver(&dev_syscons_driver))
2791 panic("Couldn't register /dev/console driver\n");
2793 /* console calls tty_register_driver() before kmalloc() works.
2794 * Thus, we can't devfs_register() then. Do so now, instead.
2795 */
2796 #ifdef CONFIG_VT
2797 con_init_devfs();
2798 #endif
2800 #ifdef CONFIG_UNIX98_PTYS
2801 dev_ptmx_driver = dev_tty_driver;
2802 dev_ptmx_driver.driver_name = "/dev/ptmx";
2803 dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2804 dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2805 dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2806 dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2807 dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2809 if (tty_register_driver(&dev_ptmx_driver))
2810 panic("Couldn't register /dev/ptmx driver\n");
2811 #endif
2813 #ifdef CONFIG_VT
2814 dev_console_driver = dev_tty_driver;
2815 dev_console_driver.driver_name = "/dev/vc/0";
2816 dev_console_driver.name = dev_console_driver.driver_name + 5;
2817 dev_console_driver.major = TTY_MAJOR;
2818 dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2819 dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2821 if (tty_register_driver(&dev_console_driver))
2822 panic("Couldn't register /dev/tty0 driver\n");
2824 kbd_init();
2825 #endif
2827 #ifdef CONFIG_SGI_L1_SERIAL_CONSOLE
2828 if (ia64_platform_is("sn2")) {
2829 sn_sal_serial_console_init();
2830 return; /* only one console right now for SN2 */
2832 #endif
2833 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2834 espserial_init();
2835 #endif
2836 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2837 vme_scc_init();
2838 #endif
2839 #ifdef CONFIG_SERIAL_TX3912
2840 tx3912_rs_init();
2841 #endif
2842 #ifdef CONFIG_ROCKETPORT
2843 rp_init();
2844 #endif
2845 #ifdef CONFIG_SERIAL167
2846 serial167_init();
2847 #endif
2848 #ifdef CONFIG_CYCLADES
2849 cy_init();
2850 #endif
2851 #ifdef CONFIG_STALLION
2852 stl_init();
2853 #endif
2854 #ifdef CONFIG_ISTALLION
2855 stli_init();
2856 #endif
2857 #ifdef CONFIG_DIGI
2858 pcxe_init();
2859 #endif
2860 #ifdef CONFIG_DIGIEPCA
2861 pc_init();
2862 #endif
2863 #ifdef CONFIG_SPECIALIX
2864 specialix_init();
2865 #endif
2866 #if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2867 rs_8xx_init();
2868 #endif /* CONFIG_8xx */
2869 pty_init();
2870 #ifdef CONFIG_MOXA_SMARTIO
2871 mxser_init();
2872 #endif
2873 #ifdef CONFIG_MOXA_INTELLIO
2874 moxa_init();
2875 #endif
2876 #ifdef CONFIG_VT
2877 vcs_init();
2878 #endif
2879 #ifdef CONFIG_TN3270
2880 tub3270_init();
2881 #endif
2882 #ifdef CONFIG_TN3215
2883 tty3215_init();
2884 #endif
2885 #ifdef CONFIG_HWC
2886 hwc_tty_init();
2887 #endif
2888 #ifdef CONFIG_A2232
2889 a2232board_init();
2890 #endif