ia64/xen-unstable

view linux-2.4.27-xen-sparse/drivers/char/tty_io.c @ 2621:9402048e2325

bitkeeper revision 1.1159.1.218 (416a8128OiHXHyk_Sy8FsA0YUQcEnA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-26dom0
author cl349@freefall.cl.cam.ac.uk
date Mon Oct 11 12:48:40 2004 +0000 (2004-10-11)
parents 869c20f2977b
children
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 */
127 struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
129 #ifdef CONFIG_UNIX98_PTYS
130 extern struct tty_driver ptm_driver[]; /* Unix98 pty masters; for /dev/ptmx */
131 extern struct tty_driver pts_driver[]; /* Unix98 pty slaves; for /dev/ptmx */
132 #endif
134 static void initialize_tty_struct(struct tty_struct *tty);
136 static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
137 static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
138 static unsigned int tty_poll(struct file *, poll_table *);
139 static int tty_open(struct inode *, struct file *);
140 static int tty_release(struct inode *, struct file *);
141 int tty_ioctl(struct inode * inode, struct file * file,
142 unsigned int cmd, unsigned long arg);
143 static int tty_fasync(int fd, struct file * filp, int on);
144 extern int vme_scc_init (void);
145 extern long vme_scc_console_init(void);
146 extern int serial167_init(void);
147 extern long serial167_console_init(void);
148 extern void console_8xx_init(void);
149 extern void au1x00_serial_console_init(void);
150 extern int rs_8xx_init(void);
151 extern void mac_scc_console_init(void);
152 extern void hwc_console_init(void);
153 extern void hwc_tty_init(void);
154 extern void con3215_init(void);
155 extern void tty3215_init(void);
156 extern void tub3270_con_init(void);
157 extern void tub3270_init(void);
158 extern void rs285_console_init(void);
159 extern void sa1100_rs_console_init(void);
160 extern void sgi_serial_console_init(void);
161 extern void sn_sal_serial_console_init(void);
162 extern void sci_console_init(void);
163 extern void dec_serial_console_init(void);
164 extern void tx3912_console_init(void);
165 extern void tx3912_rs_init(void);
166 extern void txx927_console_init(void);
167 extern void txx9_rs_init(void);
168 extern void txx9_serial_console_init(void);
169 extern void sb1250_serial_console_init(void);
170 extern void arc_console_init(void);
171 extern int hvc_console_init(void);
173 #ifndef MIN
174 #define MIN(a,b) ((a) < (b) ? (a) : (b))
175 #endif
176 #ifndef MAX
177 #define MAX(a,b) ((a) < (b) ? (b) : (a))
178 #endif
180 static struct tty_struct *alloc_tty_struct(void)
181 {
182 struct tty_struct *tty;
184 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
185 if (tty)
186 memset(tty, 0, sizeof(struct tty_struct));
187 return tty;
188 }
190 static inline void free_tty_struct(struct tty_struct *tty)
191 {
192 kfree(tty);
193 }
195 /*
196 * This routine returns the name of tty.
197 */
198 static char *
199 _tty_make_name(struct tty_struct *tty, const char *name, char *buf)
200 {
201 int idx = (tty)?MINOR(tty->device) - tty->driver.minor_start:0;
203 if (!tty) /* Hmm. NULL pointer. That's fun. */
204 strcpy(buf, "NULL tty");
205 else
206 sprintf(buf, name,
207 idx + tty->driver.name_base);
209 return buf;
210 }
212 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
213 (tty)->driver.name_base)
215 char *tty_name(struct tty_struct *tty, char *buf)
216 {
217 return _tty_make_name(tty, (tty)?tty->driver.name:NULL, buf);
218 }
220 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
221 const char *routine)
222 {
223 #ifdef TTY_PARANOIA_CHECK
224 static const char badmagic[] = KERN_WARNING
225 "Warning: bad magic number for tty struct (%s) in %s\n";
226 static const char badtty[] = KERN_WARNING
227 "Warning: null TTY for (%s) in %s\n";
229 if (!tty) {
230 printk(badtty, kdevname(device), routine);
231 return 1;
232 }
233 if (tty->magic != TTY_MAGIC) {
234 printk(badmagic, kdevname(device), routine);
235 return 1;
236 }
237 #endif
238 return 0;
239 }
241 static int check_tty_count(struct tty_struct *tty, const char *routine)
242 {
243 #ifdef CHECK_TTY_COUNT
244 struct list_head *p;
245 int count = 0;
247 file_list_lock();
248 for(p = tty->tty_files.next; p != &tty->tty_files; p = p->next) {
249 if(list_entry(p, struct file, f_list)->private_data == tty)
250 count++;
251 }
252 file_list_unlock();
253 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
254 tty->driver.subtype == PTY_TYPE_SLAVE &&
255 tty->link && tty->link->count)
256 count++;
257 if (tty->count != count) {
258 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
259 "!= #fd's(%d) in %s\n",
260 kdevname(tty->device), tty->count, count, routine);
261 return count;
262 }
263 #endif
264 return 0;
265 }
267 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
268 {
269 if (disc < N_TTY || disc >= NR_LDISCS)
270 return -EINVAL;
272 if (new_ldisc) {
273 ldiscs[disc] = *new_ldisc;
274 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
275 ldiscs[disc].num = disc;
276 } else
277 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
279 return 0;
280 }
282 EXPORT_SYMBOL(tty_register_ldisc);
284 /* Set the discipline of a tty line. */
285 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
286 {
287 int retval = 0;
288 struct tty_ldisc o_ldisc;
289 char buf[64];
291 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
292 return -EINVAL;
293 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
294 /* Cyrus Durgin <cider@speakeasy.org> */
295 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
296 char modname [20];
297 sprintf(modname, "tty-ldisc-%d", ldisc);
298 request_module (modname);
299 }
300 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
301 return -EINVAL;
303 if (tty->ldisc.num == ldisc)
304 return 0; /* We are already in the desired discipline */
305 o_ldisc = tty->ldisc;
307 tty_wait_until_sent(tty, 0);
309 /* Shutdown the current discipline. */
310 if (tty->ldisc.close)
311 (tty->ldisc.close)(tty);
313 /* Now set up the new line discipline. */
314 tty->ldisc = ldiscs[ldisc];
315 tty->termios->c_line = ldisc;
316 if (tty->ldisc.open)
317 retval = (tty->ldisc.open)(tty);
318 if (retval < 0) {
319 tty->ldisc = o_ldisc;
320 tty->termios->c_line = tty->ldisc.num;
321 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
322 tty->ldisc = ldiscs[N_TTY];
323 tty->termios->c_line = N_TTY;
324 if (tty->ldisc.open) {
325 int r = tty->ldisc.open(tty);
327 if (r < 0)
328 panic("Couldn't open N_TTY ldisc for "
329 "%s --- error %d.",
330 tty_name(tty, buf), r);
331 }
332 }
333 }
334 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
335 tty->driver.set_ldisc(tty);
336 return retval;
337 }
339 /*
340 * This routine returns a tty driver structure, given a device number
341 */
342 struct tty_driver *get_tty_driver(kdev_t device)
343 {
344 int major, minor;
345 struct tty_driver *p;
347 minor = MINOR(device);
348 major = MAJOR(device);
350 for (p = tty_drivers; p; p = p->next) {
351 if (p->major != major)
352 continue;
353 if (minor < p->minor_start)
354 continue;
355 if (minor >= p->minor_start + p->num)
356 continue;
357 return p;
358 }
359 return NULL;
360 }
362 /*
363 * If we try to write to, or set the state of, a terminal and we're
364 * not in the foreground, send a SIGTTOU. If the signal is blocked or
365 * ignored, go ahead and perform the operation. (POSIX 7.2)
366 */
367 int tty_check_change(struct tty_struct * tty)
368 {
369 if (current->tty != tty)
370 return 0;
371 if (tty->pgrp <= 0) {
372 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
373 return 0;
374 }
375 if (current->pgrp == tty->pgrp)
376 return 0;
377 if (is_ignored(SIGTTOU))
378 return 0;
379 if (is_orphaned_pgrp(current->pgrp))
380 return -EIO;
381 (void) kill_pg(current->pgrp,SIGTTOU,1);
382 return -ERESTARTSYS;
383 }
385 static ssize_t hung_up_tty_read(struct file * file, char * buf,
386 size_t count, loff_t *ppos)
387 {
388 /* Can't seek (pread) on ttys. */
389 if (ppos != &file->f_pos)
390 return -ESPIPE;
391 return 0;
392 }
394 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
395 size_t count, loff_t *ppos)
396 {
397 /* Can't seek (pwrite) on ttys. */
398 if (ppos != &file->f_pos)
399 return -ESPIPE;
400 return -EIO;
401 }
403 /* No kernel lock held - none needed ;) */
404 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
405 {
406 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
407 }
409 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
410 unsigned int cmd, unsigned long arg)
411 {
412 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
413 }
415 static struct file_operations tty_fops = {
416 llseek: no_llseek,
417 read: tty_read,
418 write: tty_write,
419 poll: tty_poll,
420 ioctl: tty_ioctl,
421 open: tty_open,
422 release: tty_release,
423 fasync: tty_fasync,
424 };
426 static struct file_operations hung_up_tty_fops = {
427 llseek: no_llseek,
428 read: hung_up_tty_read,
429 write: hung_up_tty_write,
430 poll: hung_up_tty_poll,
431 ioctl: hung_up_tty_ioctl,
432 release: tty_release,
433 };
435 static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
436 static struct file *redirect;
437 /*
438 * This can be called by the "eventd" kernel thread. That is process synchronous,
439 * but doesn't hold any locks, so we need to make sure we have the appropriate
440 * locks for what we're doing..
441 */
442 void do_tty_hangup(void *data)
443 {
444 struct tty_struct *tty = (struct tty_struct *) data;
445 struct file * cons_filp = NULL;
446 struct file *f = NULL;
447 struct task_struct *p;
448 struct list_head *l;
449 int closecount = 0, n;
451 if (!tty)
452 return;
454 /* inuse_filps is protected by the single kernel lock */
455 lock_kernel();
457 spin_lock(&redirect_lock);
458 if (redirect && redirect->private_data == tty) {
459 f = redirect;
460 redirect = NULL;
461 }
462 spin_unlock(&redirect_lock);
464 check_tty_count(tty, "do_tty_hangup");
465 file_list_lock();
466 for (l = tty->tty_files.next; l != &tty->tty_files; l = l->next) {
467 struct file * filp = list_entry(l, struct file, f_list);
468 if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
469 filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
470 cons_filp = filp;
471 continue;
472 }
473 if (filp->f_op != &tty_fops)
474 continue;
475 closecount++;
476 tty_fasync(-1, filp, 0); /* can't block */
477 filp->f_op = &hung_up_tty_fops;
478 }
479 file_list_unlock();
481 /* FIXME! What are the locking issues here? This may me overdoing things.. */
482 {
483 unsigned long flags;
485 save_flags(flags); cli();
486 if (tty->ldisc.flush_buffer)
487 tty->ldisc.flush_buffer(tty);
488 if (tty->driver.flush_buffer)
489 tty->driver.flush_buffer(tty);
490 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
491 tty->ldisc.write_wakeup)
492 (tty->ldisc.write_wakeup)(tty);
493 restore_flags(flags);
494 }
496 wake_up_interruptible(&tty->write_wait);
497 wake_up_interruptible(&tty->read_wait);
499 /*
500 * Shutdown the current line discipline, and reset it to
501 * N_TTY.
502 */
503 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
504 *tty->termios = tty->driver.init_termios;
505 if (tty->ldisc.num != ldiscs[N_TTY].num) {
506 if (tty->ldisc.close)
507 (tty->ldisc.close)(tty);
508 tty->ldisc = ldiscs[N_TTY];
509 tty->termios->c_line = N_TTY;
510 if (tty->ldisc.open) {
511 int i = (tty->ldisc.open)(tty);
512 if (i < 0)
513 printk(KERN_ERR "do_tty_hangup: N_TTY open: "
514 "error %d\n", -i);
515 }
516 }
518 read_lock(&tasklist_lock);
519 for_each_task(p) {
520 if ((tty->session > 0) && (p->session == tty->session) &&
521 p->leader) {
522 send_sig(SIGHUP,p,1);
523 send_sig(SIGCONT,p,1);
524 if (tty->pgrp > 0)
525 p->tty_old_pgrp = tty->pgrp;
526 }
527 if (p->tty == tty)
528 p->tty = NULL;
529 }
530 read_unlock(&tasklist_lock);
532 tty->flags = 0;
533 tty->session = 0;
534 tty->pgrp = -1;
535 tty->ctrl_status = 0;
536 /*
537 * If one of the devices matches a console pointer, we
538 * cannot just call hangup() because that will cause
539 * tty->count and state->count to go out of sync.
540 * So we just call close() the right number of times.
541 */
542 if (cons_filp) {
543 if (tty->driver.close)
544 for (n = 0; n < closecount; n++)
545 tty->driver.close(tty, cons_filp);
546 } else if (tty->driver.hangup)
547 (tty->driver.hangup)(tty);
548 unlock_kernel();
549 if (f)
550 fput(f);
551 }
553 void tty_hangup(struct tty_struct * tty)
554 {
555 #ifdef TTY_DEBUG_HANGUP
556 char buf[64];
558 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
559 #endif
560 schedule_task(&tty->tq_hangup);
561 }
563 void tty_vhangup(struct tty_struct * tty)
564 {
565 #ifdef TTY_DEBUG_HANGUP
566 char buf[64];
568 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
569 #endif
570 do_tty_hangup((void *) tty);
571 }
573 int tty_hung_up_p(struct file * filp)
574 {
575 return (filp->f_op == &hung_up_tty_fops);
576 }
578 /*
579 * This function is typically called only by the session leader, when
580 * it wants to disassociate itself from its controlling tty.
581 *
582 * It performs the following functions:
583 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
584 * (2) Clears the tty from being controlling the session
585 * (3) Clears the controlling tty for all processes in the
586 * session group.
587 *
588 * The argument on_exit is set to 1 if called when a process is
589 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
590 */
591 void disassociate_ctty(int on_exit)
592 {
593 struct tty_struct *tty = current->tty;
594 struct task_struct *p;
595 int tty_pgrp = -1;
597 if (tty) {
598 tty_pgrp = tty->pgrp;
599 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
600 tty_vhangup(tty);
601 } else {
602 if (current->tty_old_pgrp) {
603 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
604 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
605 }
606 return;
607 }
608 if (tty_pgrp > 0) {
609 kill_pg(tty_pgrp, SIGHUP, on_exit);
610 if (!on_exit)
611 kill_pg(tty_pgrp, SIGCONT, on_exit);
612 }
614 current->tty_old_pgrp = 0;
615 tty->session = 0;
616 tty->pgrp = -1;
618 read_lock(&tasklist_lock);
619 for_each_task(p)
620 if (p->session == current->session)
621 p->tty = NULL;
622 read_unlock(&tasklist_lock);
623 }
625 void stop_tty(struct tty_struct *tty)
626 {
627 if (tty->stopped)
628 return;
629 tty->stopped = 1;
630 if (tty->link && tty->link->packet) {
631 tty->ctrl_status &= ~TIOCPKT_START;
632 tty->ctrl_status |= TIOCPKT_STOP;
633 wake_up_interruptible(&tty->link->read_wait);
634 }
635 if (tty->driver.stop)
636 (tty->driver.stop)(tty);
637 }
639 void start_tty(struct tty_struct *tty)
640 {
641 if (!tty->stopped || tty->flow_stopped)
642 return;
643 tty->stopped = 0;
644 if (tty->link && tty->link->packet) {
645 tty->ctrl_status &= ~TIOCPKT_STOP;
646 tty->ctrl_status |= TIOCPKT_START;
647 wake_up_interruptible(&tty->link->read_wait);
648 }
649 if (tty->driver.start)
650 (tty->driver.start)(tty);
651 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
652 tty->ldisc.write_wakeup)
653 (tty->ldisc.write_wakeup)(tty);
654 wake_up_interruptible(&tty->write_wait);
655 }
657 static ssize_t tty_read(struct file * file, char * buf, size_t count,
658 loff_t *ppos)
659 {
660 int i;
661 struct tty_struct * tty;
662 struct inode *inode;
664 /* Can't seek (pread) on ttys. */
665 if (ppos != &file->f_pos)
666 return -ESPIPE;
668 tty = (struct tty_struct *)file->private_data;
669 inode = file->f_dentry->d_inode;
670 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
671 return -EIO;
672 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
673 return -EIO;
675 /* This check not only needs to be done before reading, but also
676 whenever read_chan() gets woken up after sleeping, so I've
677 moved it to there. This should only be done for the N_TTY
678 line discipline, anyway. Same goes for write_chan(). -- jlc. */
679 #if 0
680 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
681 (tty->pgrp > 0) &&
682 (current->tty == tty) &&
683 (tty->pgrp != current->pgrp))
684 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
685 return -EIO;
686 else {
687 (void) kill_pg(current->pgrp, SIGTTIN, 1);
688 return -ERESTARTSYS;
689 }
690 #endif
691 lock_kernel();
692 if (tty->ldisc.read)
693 i = (tty->ldisc.read)(tty,file,buf,count);
694 else
695 i = -EIO;
696 unlock_kernel();
697 if (i > 0)
698 inode->i_atime = CURRENT_TIME;
699 return i;
700 }
702 /*
703 * Split writes up in sane blocksizes to avoid
704 * denial-of-service type attacks
705 */
706 static inline ssize_t do_tty_write(
707 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
708 struct tty_struct *tty,
709 struct file *file,
710 const unsigned char *buf,
711 size_t count)
712 {
713 ssize_t ret = 0, written = 0;
715 if (file->f_flags & O_NONBLOCK) {
716 if (down_trylock(&tty->atomic_write))
717 return -EAGAIN;
718 }
719 else {
720 if (down_interruptible(&tty->atomic_write))
721 return -ERESTARTSYS;
722 }
723 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
724 lock_kernel();
725 written = write(tty, file, buf, count);
726 unlock_kernel();
727 } else {
728 for (;;) {
729 unsigned long size = MAX(PAGE_SIZE*2,16384);
730 if (size > count)
731 size = count;
732 lock_kernel();
733 ret = write(tty, file, buf, size);
734 unlock_kernel();
735 if (ret <= 0)
736 break;
737 written += ret;
738 buf += ret;
739 count -= ret;
740 if (!count)
741 break;
742 ret = -ERESTARTSYS;
743 if (signal_pending(current))
744 break;
745 if (current->need_resched)
746 schedule();
747 }
748 }
749 if (written) {
750 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
751 ret = written;
752 }
753 up(&tty->atomic_write);
754 return ret;
755 }
758 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
759 loff_t *ppos)
760 {
761 int is_console;
762 struct tty_struct * tty;
763 struct inode *inode = file->f_dentry->d_inode;
765 /* Can't seek (pwrite) on ttys. */
766 if (ppos != &file->f_pos)
767 return -ESPIPE;
769 /*
770 * For now, we redirect writes from /dev/console as
771 * well as /dev/tty0.
772 */
773 inode = file->f_dentry->d_inode;
774 is_console = (inode->i_rdev == SYSCONS_DEV ||
775 inode->i_rdev == CONSOLE_DEV);
777 if (is_console) {
778 struct file *p = NULL;
780 spin_lock(&redirect_lock);
781 if (redirect) {
782 get_file(redirect);
783 p = redirect;
784 }
785 spin_unlock(&redirect_lock);
787 if (p) {
788 ssize_t res = p->f_op->write(p, buf, count, &p->f_pos);
789 fput(p);
790 return res;
791 }
792 }
794 tty = (struct tty_struct *)file->private_data;
795 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
796 return -EIO;
797 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
798 return -EIO;
799 #if 0
800 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
801 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
802 if (is_orphaned_pgrp(current->pgrp))
803 return -EIO;
804 if (!is_ignored(SIGTTOU)) {
805 (void) kill_pg(current->pgrp, SIGTTOU, 1);
806 return -ERESTARTSYS;
807 }
808 }
809 #endif
810 if (!tty->ldisc.write)
811 return -EIO;
812 return do_tty_write(tty->ldisc.write, tty, file,
813 (const unsigned char *)buf, count);
814 }
816 /* Semaphore to protect creating and releasing a tty */
817 static DECLARE_MUTEX(tty_sem);
819 static void down_tty_sem(int index)
820 {
821 down(&tty_sem);
822 }
824 static void up_tty_sem(int index)
825 {
826 up(&tty_sem);
827 }
829 static void release_mem(struct tty_struct *tty, int idx);
831 /*
832 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
833 * failed open. The new code protects the open with a semaphore, so it's
834 * really quite straightforward. The semaphore locking can probably be
835 * relaxed for the (most common) case of reopening a tty.
836 */
837 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
838 {
839 struct tty_struct *tty, *o_tty;
840 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
841 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
842 struct tty_driver *driver;
843 int retval=0;
844 int idx;
846 driver = get_tty_driver(device);
847 if (!driver)
848 return -ENODEV;
850 idx = MINOR(device) - driver->minor_start;
852 /*
853 * Check whether we need to acquire the tty semaphore to avoid
854 * race conditions. For now, play it safe.
855 */
856 down_tty_sem(idx);
858 /* check whether we're reopening an existing tty */
859 tty = driver->table[idx];
860 if (tty) goto fast_track;
862 /*
863 * First time open is complex, especially for PTY devices.
864 * This code guarantees that either everything succeeds and the
865 * TTY is ready for operation, or else the table slots are vacated
866 * and the allocated memory released. (Except that the termios
867 * and locked termios may be retained.)
868 */
870 o_tty = NULL;
871 tp = o_tp = NULL;
872 ltp = o_ltp = NULL;
874 tty = alloc_tty_struct();
875 if(!tty)
876 goto fail_no_mem;
877 initialize_tty_struct(tty);
878 tty->device = device;
879 tty->driver = *driver;
881 tp_loc = &driver->termios[idx];
882 if (!*tp_loc) {
883 tp = (struct termios *) kmalloc(sizeof(struct termios),
884 GFP_KERNEL);
885 if (!tp)
886 goto free_mem_out;
887 *tp = driver->init_termios;
888 }
890 ltp_loc = &driver->termios_locked[idx];
891 if (!*ltp_loc) {
892 ltp = (struct termios *) kmalloc(sizeof(struct termios),
893 GFP_KERNEL);
894 if (!ltp)
895 goto free_mem_out;
896 memset(ltp, 0, sizeof(struct termios));
897 }
899 if (driver->type == TTY_DRIVER_TYPE_PTY) {
900 o_tty = alloc_tty_struct();
901 if (!o_tty)
902 goto free_mem_out;
903 initialize_tty_struct(o_tty);
904 o_tty->device = (kdev_t) MKDEV(driver->other->major,
905 driver->other->minor_start + idx);
906 o_tty->driver = *driver->other;
908 o_tp_loc = &driver->other->termios[idx];
909 if (!*o_tp_loc) {
910 o_tp = (struct termios *)
911 kmalloc(sizeof(struct termios), GFP_KERNEL);
912 if (!o_tp)
913 goto free_mem_out;
914 *o_tp = driver->other->init_termios;
915 }
917 o_ltp_loc = &driver->other->termios_locked[idx];
918 if (!*o_ltp_loc) {
919 o_ltp = (struct termios *)
920 kmalloc(sizeof(struct termios), GFP_KERNEL);
921 if (!o_ltp)
922 goto free_mem_out;
923 memset(o_ltp, 0, sizeof(struct termios));
924 }
926 /*
927 * Everything allocated ... set up the o_tty structure.
928 */
929 driver->other->table[idx] = o_tty;
930 if (!*o_tp_loc)
931 *o_tp_loc = o_tp;
932 if (!*o_ltp_loc)
933 *o_ltp_loc = o_ltp;
934 o_tty->termios = *o_tp_loc;
935 o_tty->termios_locked = *o_ltp_loc;
936 (*driver->other->refcount)++;
937 if (driver->subtype == PTY_TYPE_MASTER)
938 o_tty->count++;
940 /* Establish the links in both directions */
941 tty->link = o_tty;
942 o_tty->link = tty;
943 }
945 /*
946 * All structures have been allocated, so now we install them.
947 * Failures after this point use release_mem to clean up, so
948 * there's no need to null out the local pointers.
949 */
950 driver->table[idx] = tty;
952 if (!*tp_loc)
953 *tp_loc = tp;
954 if (!*ltp_loc)
955 *ltp_loc = ltp;
956 tty->termios = *tp_loc;
957 tty->termios_locked = *ltp_loc;
958 (*driver->refcount)++;
959 tty->count++;
961 /*
962 * Structures all installed ... call the ldisc open routines.
963 * If we fail here just call release_mem to clean up. No need
964 * to decrement the use counts, as release_mem doesn't care.
965 */
966 if (tty->ldisc.open) {
967 retval = (tty->ldisc.open)(tty);
968 if (retval)
969 goto release_mem_out;
970 }
971 if (o_tty && o_tty->ldisc.open) {
972 retval = (o_tty->ldisc.open)(o_tty);
973 if (retval) {
974 if (tty->ldisc.close)
975 (tty->ldisc.close)(tty);
976 goto release_mem_out;
977 }
978 }
979 goto success;
981 /*
982 * This fast open can be used if the tty is already open.
983 * No memory is allocated, and the only failures are from
984 * attempting to open a closing tty or attempting multiple
985 * opens on a pty master.
986 */
987 fast_track:
988 if (test_bit(TTY_CLOSING, &tty->flags)) {
989 retval = -EIO;
990 goto end_init;
991 }
992 if (driver->type == TTY_DRIVER_TYPE_PTY &&
993 driver->subtype == PTY_TYPE_MASTER) {
994 /*
995 * special case for PTY masters: only one open permitted,
996 * and the slave side open count is incremented as well.
997 */
998 if (tty->count) {
999 retval = -EIO;
1000 goto end_init;
1002 tty->link->count++;
1004 tty->count++;
1005 tty->driver = *driver; /* N.B. why do this every time?? */
1007 success:
1008 *ret_tty = tty;
1010 /* All paths come through here to release the semaphore */
1011 end_init:
1012 up_tty_sem(idx);
1013 return retval;
1015 /* Release locally allocated memory ... nothing placed in slots */
1016 free_mem_out:
1017 if (o_tp)
1018 kfree(o_tp);
1019 if (o_tty)
1020 free_tty_struct(o_tty);
1021 if (ltp)
1022 kfree(ltp);
1023 if (tp)
1024 kfree(tp);
1025 free_tty_struct(tty);
1027 fail_no_mem:
1028 retval = -ENOMEM;
1029 goto end_init;
1031 /* call the tty release_mem routine to clean out this slot */
1032 release_mem_out:
1033 printk(KERN_INFO "init_dev: ldisc open failed, "
1034 "clearing slot %d\n", idx);
1035 release_mem(tty, idx);
1036 goto end_init;
1039 /*
1040 * Releases memory associated with a tty structure, and clears out the
1041 * driver table slots.
1042 */
1043 static void release_mem(struct tty_struct *tty, int idx)
1045 struct tty_struct *o_tty;
1046 struct termios *tp;
1048 if ((o_tty = tty->link) != NULL) {
1049 o_tty->driver.table[idx] = NULL;
1050 if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1051 tp = o_tty->driver.termios[idx];
1052 o_tty->driver.termios[idx] = NULL;
1053 kfree(tp);
1055 o_tty->magic = 0;
1056 (*o_tty->driver.refcount)--;
1057 list_del_init(&o_tty->tty_files);
1058 free_tty_struct(o_tty);
1061 tty->driver.table[idx] = NULL;
1062 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1063 tp = tty->driver.termios[idx];
1064 tty->driver.termios[idx] = NULL;
1065 kfree(tp);
1067 tty->magic = 0;
1068 (*tty->driver.refcount)--;
1069 list_del_init(&tty->tty_files);
1070 free_tty_struct(tty);
1073 /*
1074 * Even releasing the tty structures is a tricky business.. We have
1075 * to be very careful that the structures are all released at the
1076 * same time, as interrupts might otherwise get the wrong pointers.
1078 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1079 * lead to double frees or releasing memory still in use.
1080 */
1081 static void release_dev(struct file * filp)
1083 struct tty_struct *tty, *o_tty;
1084 int pty_master, tty_closing, o_tty_closing, do_sleep;
1085 int idx;
1086 char buf[64];
1088 tty = (struct tty_struct *)filp->private_data;
1089 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1090 return;
1092 check_tty_count(tty, "release_dev");
1094 tty_fasync(-1, filp, 0);
1096 idx = MINOR(tty->device) - tty->driver.minor_start;
1097 pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1098 tty->driver.subtype == PTY_TYPE_MASTER);
1099 o_tty = tty->link;
1101 #ifdef TTY_PARANOIA_CHECK
1102 if (idx < 0 || idx >= tty->driver.num) {
1103 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1104 "free (%s)\n", kdevname(tty->device));
1105 return;
1107 if (tty != tty->driver.table[idx]) {
1108 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1109 "for (%s)\n", idx, kdevname(tty->device));
1110 return;
1112 if (tty->termios != tty->driver.termios[idx]) {
1113 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1114 "for (%s)\n",
1115 idx, kdevname(tty->device));
1116 return;
1118 if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1119 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1120 "termios_locked for (%s)\n",
1121 idx, kdevname(tty->device));
1122 return;
1124 #endif
1126 #ifdef TTY_DEBUG_HANGUP
1127 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1128 tty_name(tty, buf), tty->count);
1129 #endif
1131 #ifdef TTY_PARANOIA_CHECK
1132 if (tty->driver.other) {
1133 if (o_tty != tty->driver.other->table[idx]) {
1134 printk(KERN_DEBUG "release_dev: other->table[%d] "
1135 "not o_tty for (%s)\n",
1136 idx, kdevname(tty->device));
1137 return;
1139 if (o_tty->termios != tty->driver.other->termios[idx]) {
1140 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1141 "not o_termios for (%s)\n",
1142 idx, kdevname(tty->device));
1143 return;
1145 if (o_tty->termios_locked !=
1146 tty->driver.other->termios_locked[idx]) {
1147 printk(KERN_DEBUG "release_dev: other->termios_locked["
1148 "%d] not o_termios_locked for (%s)\n",
1149 idx, kdevname(tty->device));
1150 return;
1152 if (o_tty->link != tty) {
1153 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1154 return;
1157 #endif
1159 if (tty->driver.close)
1160 tty->driver.close(tty, filp);
1162 /*
1163 * Sanity check: if tty->count is going to zero, there shouldn't be
1164 * any waiters on tty->read_wait or tty->write_wait. We test the
1165 * wait queues and kick everyone out _before_ actually starting to
1166 * close. This ensures that we won't block while releasing the tty
1167 * structure.
1169 * The test for the o_tty closing is necessary, since the master and
1170 * slave sides may close in any order. If the slave side closes out
1171 * first, its count will be one, since the master side holds an open.
1172 * Thus this test wouldn't be triggered at the time the slave closes,
1173 * so we do it now.
1175 * Note that it's possible for the tty to be opened again while we're
1176 * flushing out waiters. By recalculating the closing flags before
1177 * each iteration we avoid any problems.
1178 */
1179 while (1) {
1180 tty_closing = tty->count <= 1;
1181 o_tty_closing = o_tty &&
1182 (o_tty->count <= (pty_master ? 1 : 0));
1183 do_sleep = 0;
1185 if (tty_closing) {
1186 if (waitqueue_active(&tty->read_wait)) {
1187 wake_up(&tty->read_wait);
1188 do_sleep++;
1190 if (waitqueue_active(&tty->write_wait)) {
1191 wake_up(&tty->write_wait);
1192 do_sleep++;
1195 if (o_tty_closing) {
1196 if (waitqueue_active(&o_tty->read_wait)) {
1197 wake_up(&o_tty->read_wait);
1198 do_sleep++;
1200 if (waitqueue_active(&o_tty->write_wait)) {
1201 wake_up(&o_tty->write_wait);
1202 do_sleep++;
1205 if (!do_sleep)
1206 break;
1208 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1209 "active!\n", tty_name(tty, buf));
1210 schedule();
1213 /*
1214 * The closing flags are now consistent with the open counts on
1215 * both sides, and we've completed the last operation that could
1216 * block, so it's safe to proceed with closing.
1217 */
1218 if (pty_master) {
1219 if (--o_tty->count < 0) {
1220 printk(KERN_WARNING "release_dev: bad pty slave count "
1221 "(%d) for %s\n",
1222 o_tty->count, tty_name(o_tty, buf));
1223 o_tty->count = 0;
1226 if (--tty->count < 0) {
1227 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1228 tty->count, tty_name(tty, buf));
1229 tty->count = 0;
1232 /*
1233 * We've decremented tty->count, so we should zero out
1234 * filp->private_data, to break the link between the tty and
1235 * the file descriptor. Otherwise if filp_close() blocks before
1236 * the file descriptor is removed from the inuse_filp
1237 * list, check_tty_count() could observe a discrepancy and
1238 * printk a warning message to the user.
1239 */
1240 filp->private_data = 0;
1242 /*
1243 * Perform some housekeeping before deciding whether to return.
1245 * Set the TTY_CLOSING flag if this was the last open. In the
1246 * case of a pty we may have to wait around for the other side
1247 * to close, and TTY_CLOSING makes sure we can't be reopened.
1248 */
1249 if(tty_closing)
1250 set_bit(TTY_CLOSING, &tty->flags);
1251 if(o_tty_closing)
1252 set_bit(TTY_CLOSING, &o_tty->flags);
1254 /*
1255 * If _either_ side is closing, make sure there aren't any
1256 * processes that still think tty or o_tty is their controlling
1257 * tty.
1258 */
1259 if (tty_closing || o_tty_closing) {
1260 struct task_struct *p;
1262 read_lock(&tasklist_lock);
1263 for_each_task(p) {
1264 if (p->tty == tty || (o_tty && p->tty == o_tty))
1265 p->tty = NULL;
1267 read_unlock(&tasklist_lock);
1270 /* check whether both sides are closing ... */
1271 if (!tty_closing || (o_tty && !o_tty_closing))
1272 return;
1274 #ifdef TTY_DEBUG_HANGUP
1275 printk(KERN_DEBUG "freeing tty structure...");
1276 #endif
1278 /*
1279 * Shutdown the current line discipline, and reset it to N_TTY.
1280 * N.B. why reset ldisc when we're releasing the memory??
1281 */
1282 if (tty->ldisc.close)
1283 (tty->ldisc.close)(tty);
1284 tty->ldisc = ldiscs[N_TTY];
1285 tty->termios->c_line = N_TTY;
1286 if (o_tty) {
1287 if (o_tty->ldisc.close)
1288 (o_tty->ldisc.close)(o_tty);
1289 o_tty->ldisc = ldiscs[N_TTY];
1292 /*
1293 * Make sure that the tty's task queue isn't activated.
1294 */
1295 run_task_queue(&tq_timer);
1296 flush_scheduled_tasks();
1298 /*
1299 * The release_mem function takes care of the details of clearing
1300 * the slots and preserving the termios structure.
1301 */
1302 release_mem(tty, idx);
1305 /*
1306 * tty_open and tty_release keep up the tty count that contains the
1307 * number of opens done on a tty. We cannot use the inode-count, as
1308 * different inodes might point to the same tty.
1310 * Open-counting is needed for pty masters, as well as for keeping
1311 * track of serial lines: DTR is dropped when the last close happens.
1312 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1314 * The termios state of a pty is reset on first open so that
1315 * settings don't persist across reuse.
1316 */
1317 static int tty_open(struct inode * inode, struct file * filp)
1319 struct tty_struct *tty;
1320 int noctty, retval;
1321 kdev_t device;
1322 unsigned short saved_flags;
1323 char buf[64];
1325 saved_flags = filp->f_flags;
1326 retry_open:
1327 noctty = filp->f_flags & O_NOCTTY;
1328 device = inode->i_rdev;
1329 if (device == TTY_DEV) {
1330 if (!current->tty)
1331 return -ENXIO;
1332 device = current->tty->device;
1333 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1334 /* noctty = 1; */
1336 #ifdef CONFIG_VT
1337 if (device == CONSOLE_DEV) {
1338 extern int fg_console;
1339 device = MKDEV(TTY_MAJOR, fg_console + 1);
1340 noctty = 1;
1342 #endif
1343 if (device == SYSCONS_DEV) {
1344 struct console *c = console_drivers;
1345 while(c && !c->device)
1346 c = c->next;
1347 if (!c)
1348 return -ENODEV;
1349 device = c->device(c);
1350 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1351 noctty = 1;
1354 if (device == PTMX_DEV) {
1355 #ifdef CONFIG_UNIX98_PTYS
1357 /* find a free pty. */
1358 int major, minor;
1359 struct tty_driver *driver;
1361 /* find a device that is not in use. */
1362 retval = -1;
1363 for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1364 driver = &ptm_driver[major];
1365 for (minor = driver->minor_start ;
1366 minor < driver->minor_start + driver->num ;
1367 minor++) {
1368 device = MKDEV(driver->major, minor);
1369 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1372 return -EIO; /* no free ptys */
1373 ptmx_found:
1374 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1375 minor -= driver->minor_start;
1376 devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1377 tty_register_devfs(&pts_driver[major], DEVFS_FL_DEFAULT,
1378 pts_driver[major].minor_start + minor);
1379 noctty = 1;
1380 goto init_dev_done;
1382 #else /* CONFIG_UNIX_98_PTYS */
1384 return -ENODEV;
1386 #endif /* CONFIG_UNIX_98_PTYS */
1389 retval = init_dev(device, &tty);
1390 if (retval)
1391 return retval;
1393 #ifdef CONFIG_UNIX98_PTYS
1394 init_dev_done:
1395 #endif
1396 filp->private_data = tty;
1397 file_move(filp, &tty->tty_files);
1398 check_tty_count(tty, "tty_open");
1399 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1400 tty->driver.subtype == PTY_TYPE_MASTER)
1401 noctty = 1;
1402 #ifdef TTY_DEBUG_HANGUP
1403 printk(KERN_DEBUG "opening %s...", tty_name(tty, buf));
1404 #endif
1405 if (tty->driver.open)
1406 retval = tty->driver.open(tty, filp);
1407 else
1408 retval = -ENODEV;
1409 filp->f_flags = saved_flags;
1411 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1412 retval = -EBUSY;
1414 if (retval) {
1415 #ifdef TTY_DEBUG_HANGUP
1416 printk(KERN_DEBUG "error %d in opening %s...", retval,
1417 tty_name(tty, buf));
1418 #endif
1420 release_dev(filp);
1421 if (retval != -ERESTARTSYS)
1422 return retval;
1423 if (signal_pending(current))
1424 return retval;
1425 schedule();
1426 /*
1427 * Need to reset f_op in case a hangup happened.
1428 */
1429 filp->f_op = &tty_fops;
1430 goto retry_open;
1432 if (!noctty &&
1433 current->leader &&
1434 !current->tty &&
1435 tty->session == 0) {
1436 task_lock(current);
1437 current->tty = tty;
1438 task_unlock(current);
1439 current->tty_old_pgrp = 0;
1440 tty->session = current->session;
1441 tty->pgrp = current->pgrp;
1443 if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1444 (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1445 (tty->count == 1)) {
1446 static int nr_warns;
1447 if (nr_warns < 5) {
1448 printk(KERN_WARNING "tty_io.c: "
1449 "process %d (%s) used obsolete /dev/%s - "
1450 "update software to use /dev/ttyS%d\n",
1451 current->pid, current->comm,
1452 tty_name(tty, buf), TTY_NUMBER(tty));
1453 nr_warns++;
1456 return 0;
1459 static int tty_release(struct inode * inode, struct file * filp)
1461 lock_kernel();
1462 release_dev(filp);
1463 unlock_kernel();
1464 return 0;
1467 /* No kernel lock held - fine */
1468 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1470 struct tty_struct * tty;
1472 tty = (struct tty_struct *)filp->private_data;
1473 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1474 return 0;
1476 if (tty->ldisc.poll)
1477 return (tty->ldisc.poll)(tty, filp, wait);
1478 return 0;
1481 static int tty_fasync(int fd, struct file * filp, int on)
1483 struct tty_struct * tty;
1484 int retval;
1486 tty = (struct tty_struct *)filp->private_data;
1487 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1488 return 0;
1490 retval = fasync_helper(fd, filp, on, &tty->fasync);
1491 if (retval <= 0)
1492 return retval;
1494 if (on) {
1495 if (!waitqueue_active(&tty->read_wait))
1496 tty->minimum_to_wake = 1;
1497 if (filp->f_owner.pid == 0) {
1498 filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1499 filp->f_owner.uid = current->uid;
1500 filp->f_owner.euid = current->euid;
1502 } else {
1503 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1504 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1506 return 0;
1509 static int tiocsti(struct tty_struct *tty, char * arg)
1511 char ch, mbz = 0;
1513 if ((current->tty != tty) && !suser())
1514 return -EPERM;
1515 if (get_user(ch, arg))
1516 return -EFAULT;
1517 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1518 return 0;
1521 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1523 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1524 return -EFAULT;
1525 return 0;
1528 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1529 struct winsize * arg)
1531 struct winsize tmp_ws;
1533 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1534 return -EFAULT;
1535 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1536 return 0;
1537 if (tty->pgrp > 0)
1538 kill_pg(tty->pgrp, SIGWINCH, 1);
1539 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1540 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1541 tty->winsize = tmp_ws;
1542 real_tty->winsize = tmp_ws;
1543 return 0;
1546 static int tioccons(struct inode *inode, struct file *file)
1548 if (inode->i_rdev == SYSCONS_DEV ||
1549 inode->i_rdev == CONSOLE_DEV) {
1550 struct file *f;
1551 if (!suser())
1552 return -EPERM;
1553 spin_lock(&redirect_lock);
1554 f = redirect;
1555 redirect = NULL;
1556 spin_unlock(&redirect_lock);
1557 if (f)
1558 fput(f);
1559 return 0;
1561 spin_lock(&redirect_lock);
1562 if (redirect) {
1563 spin_unlock(&redirect_lock);
1564 return -EBUSY;
1566 get_file(file);
1567 redirect = file;
1568 spin_unlock(&redirect_lock);
1569 return 0;
1573 static int fionbio(struct file *file, int *arg)
1575 int nonblock;
1577 if (get_user(nonblock, arg))
1578 return -EFAULT;
1580 if (nonblock)
1581 file->f_flags |= O_NONBLOCK;
1582 else
1583 file->f_flags &= ~O_NONBLOCK;
1584 return 0;
1587 static int tiocsctty(struct tty_struct *tty, int arg)
1589 if (current->leader &&
1590 (current->session == tty->session))
1591 return 0;
1592 /*
1593 * The process must be a session leader and
1594 * not have a controlling tty already.
1595 */
1596 if (!current->leader || current->tty)
1597 return -EPERM;
1598 if (tty->session > 0) {
1599 /*
1600 * This tty is already the controlling
1601 * tty for another session group!
1602 */
1603 if ((arg == 1) && suser()) {
1604 /*
1605 * Steal it away
1606 */
1607 struct task_struct *p;
1609 read_lock(&tasklist_lock);
1610 for_each_task(p)
1611 if (p->tty == tty)
1612 p->tty = NULL;
1613 read_unlock(&tasklist_lock);
1614 } else
1615 return -EPERM;
1617 task_lock(current);
1618 current->tty = tty;
1619 task_unlock(current);
1620 current->tty_old_pgrp = 0;
1621 tty->session = current->session;
1622 tty->pgrp = current->pgrp;
1623 return 0;
1626 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1628 /*
1629 * (tty == real_tty) is a cheap way of
1630 * testing if the tty is NOT a master pty.
1631 */
1632 if (tty == real_tty && current->tty != real_tty)
1633 return -ENOTTY;
1634 return put_user(real_tty->pgrp, arg);
1637 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1639 pid_t pgrp;
1640 int retval = tty_check_change(real_tty);
1642 if (retval == -EIO)
1643 return -ENOTTY;
1644 if (retval)
1645 return retval;
1646 if (!current->tty ||
1647 (current->tty != real_tty) ||
1648 (real_tty->session != current->session))
1649 return -ENOTTY;
1650 if (get_user(pgrp, (pid_t *) arg))
1651 return -EFAULT;
1652 if (pgrp < 0)
1653 return -EINVAL;
1654 if (session_of_pgrp(pgrp) != current->session)
1655 return -EPERM;
1656 real_tty->pgrp = pgrp;
1657 return 0;
1660 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1662 /*
1663 * (tty == real_tty) is a cheap way of
1664 * testing if the tty is NOT a master pty.
1665 */
1666 if (tty == real_tty && current->tty != real_tty)
1667 return -ENOTTY;
1668 if (real_tty->session <= 0)
1669 return -ENOTTY;
1670 return put_user(real_tty->session, arg);
1673 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1675 if (copy_to_user(arg, tty, sizeof(*arg)))
1676 return -EFAULT;
1677 return 0;
1680 static int tiocsetd(struct tty_struct *tty, int *arg)
1682 int ldisc;
1684 if (get_user(ldisc, arg))
1685 return -EFAULT;
1686 return tty_set_ldisc(tty, ldisc);
1689 static int send_break(struct tty_struct *tty, int duration)
1691 set_current_state(TASK_INTERRUPTIBLE);
1693 tty->driver.break_ctl(tty, -1);
1694 if (!signal_pending(current))
1695 schedule_timeout(duration);
1696 tty->driver.break_ctl(tty, 0);
1697 if (signal_pending(current))
1698 return -EINTR;
1699 return 0;
1702 static int tty_generic_brk(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1704 if (cmd == TCSBRK && arg)
1706 /* tcdrain case */
1707 int retval = tty_check_change(tty);
1708 if (retval)
1709 return retval;
1710 tty_wait_until_sent(tty, 0);
1711 if (signal_pending(current))
1712 return -EINTR;
1714 return 0;
1717 /*
1718 * Split this up, as gcc can choke on it otherwise..
1719 */
1720 int tty_ioctl(struct inode * inode, struct file * file,
1721 unsigned int cmd, unsigned long arg)
1723 struct tty_struct *tty, *real_tty;
1724 int retval;
1726 tty = (struct tty_struct *)file->private_data;
1727 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1728 return -EINVAL;
1730 real_tty = tty;
1731 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1732 tty->driver.subtype == PTY_TYPE_MASTER)
1733 real_tty = tty->link;
1735 /*
1736 * Break handling by driver
1737 */
1738 if (!tty->driver.break_ctl) {
1739 switch(cmd) {
1740 case TIOCSBRK:
1741 case TIOCCBRK:
1742 if (tty->driver.ioctl)
1743 return tty->driver.ioctl(tty, file, cmd, arg);
1744 return -EINVAL;
1746 /* These two ioctl's always return success; even if */
1747 /* the driver doesn't support them. */
1748 case TCSBRK:
1749 case TCSBRKP:
1750 retval = -ENOIOCTLCMD;
1751 if (tty->driver.ioctl)
1752 retval = tty->driver.ioctl(tty, file, cmd, arg);
1753 /* Not driver handled */
1754 if (retval == -ENOIOCTLCMD)
1755 retval = tty_generic_brk(tty, file, cmd, arg);
1756 return retval;
1760 /*
1761 * Factor out some common prep work
1762 */
1763 switch (cmd) {
1764 case TIOCSETD:
1765 case TIOCSBRK:
1766 case TIOCCBRK:
1767 case TCSBRK:
1768 case TCSBRKP:
1769 retval = tty_check_change(tty);
1770 if (retval)
1771 return retval;
1772 if (cmd != TIOCCBRK) {
1773 tty_wait_until_sent(tty, 0);
1774 if (signal_pending(current))
1775 return -EINTR;
1777 break;
1780 switch (cmd) {
1781 case TIOCSTI:
1782 return tiocsti(tty, (char *)arg);
1783 case TIOCGWINSZ:
1784 return tiocgwinsz(tty, (struct winsize *) arg);
1785 case TIOCSWINSZ:
1786 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1787 case TIOCCONS:
1788 return real_tty!=tty ? -EINVAL : tioccons(inode, file);
1789 case FIONBIO:
1790 return fionbio(file, (int *) arg);
1791 case TIOCEXCL:
1792 set_bit(TTY_EXCLUSIVE, &tty->flags);
1793 return 0;
1794 case TIOCNXCL:
1795 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1796 return 0;
1797 case TIOCNOTTY:
1798 if (current->tty != tty)
1799 return -ENOTTY;
1800 if (current->leader)
1801 disassociate_ctty(0);
1802 task_lock(current);
1803 current->tty = NULL;
1804 task_unlock(current);
1805 return 0;
1806 case TIOCSCTTY:
1807 return tiocsctty(tty, arg);
1808 case TIOCGPGRP:
1809 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1810 case TIOCSPGRP:
1811 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1812 case TIOCGSID:
1813 return tiocgsid(tty, real_tty, (pid_t *) arg);
1814 case TIOCGETD:
1815 return put_user(tty->ldisc.num, (int *) arg);
1816 case TIOCSETD:
1817 return tiocsetd(tty, (int *) arg);
1818 #ifdef CONFIG_VT
1819 case TIOCLINUX:
1820 return tioclinux(tty, arg);
1821 #endif
1822 case TIOCTTYGSTRUCT:
1823 return tiocttygstruct(tty, (struct tty_struct *) arg);
1825 /*
1826 * Break handling
1827 */
1828 case TIOCSBRK: /* Turn break on, unconditionally */
1829 tty->driver.break_ctl(tty, -1);
1830 return 0;
1832 case TIOCCBRK: /* Turn break off, unconditionally */
1833 tty->driver.break_ctl(tty, 0);
1834 return 0;
1835 case TCSBRK: /* SVID version: non-zero arg --> no break */
1836 /*
1837 * XXX is the above comment correct, or the
1838 * code below correct? Is this ioctl used at
1839 * all by anyone?
1840 */
1841 if (!arg)
1842 return send_break(tty, HZ/4);
1843 return 0;
1844 case TCSBRKP: /* support for POSIX tcsendbreak() */
1845 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1847 if (tty->driver.ioctl) {
1848 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1849 if (retval != -ENOIOCTLCMD)
1850 return retval;
1852 if (tty->ldisc.ioctl) {
1853 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1854 if (retval != -ENOIOCTLCMD)
1855 return retval;
1857 return -EINVAL;
1861 /*
1862 * This implements the "Secure Attention Key" --- the idea is to
1863 * prevent trojan horses by killing all processes associated with this
1864 * tty when the user hits the "Secure Attention Key". Required for
1865 * super-paranoid applications --- see the Orange Book for more details.
1867 * This code could be nicer; ideally it should send a HUP, wait a few
1868 * seconds, then send a INT, and then a KILL signal. But you then
1869 * have to coordinate with the init process, since all processes associated
1870 * with the current tty must be dead before the new getty is allowed
1871 * to spawn.
1873 * Now, if it would be correct ;-/ The current code has a nasty hole -
1874 * it doesn't catch files in flight. We may send the descriptor to ourselves
1875 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1877 * Nasty bug: do_SAK is being called in interrupt context. This can
1878 * deadlock. We punt it up to process context. AKPM - 16Mar2001
1879 */
1880 static void __do_SAK(void *arg)
1882 #ifdef TTY_SOFT_SAK
1883 tty_hangup(tty);
1884 #else
1885 struct tty_struct *tty = arg;
1886 struct task_struct *p;
1887 int session;
1888 int i;
1889 struct file *filp;
1891 if (!tty)
1892 return;
1893 session = tty->session;
1894 if (tty->ldisc.flush_buffer)
1895 tty->ldisc.flush_buffer(tty);
1896 if (tty->driver.flush_buffer)
1897 tty->driver.flush_buffer(tty);
1898 read_lock(&tasklist_lock);
1899 for_each_task(p) {
1900 if ((p->tty == tty) ||
1901 ((session > 0) && (p->session == session))) {
1902 send_sig(SIGKILL, p, 1);
1903 continue;
1905 task_lock(p);
1906 if (p->files) {
1907 read_lock(&p->files->file_lock);
1908 for (i=0; i < p->files->max_fds; i++) {
1909 filp = fcheck_files(p->files, i);
1910 if (filp && (filp->f_op == &tty_fops) &&
1911 (filp->private_data == tty)) {
1912 send_sig(SIGKILL, p, 1);
1913 break;
1916 read_unlock(&p->files->file_lock);
1918 task_unlock(p);
1920 read_unlock(&tasklist_lock);
1921 #endif
1924 /*
1925 * The tq handling here is a little racy - tty->SAK_tq may already be queued.
1926 * But there's no mechanism to fix that without futzing with tqueue_lock.
1927 * Fortunately we don't need to worry, because if ->SAK_tq is already queued,
1928 * the values which we write to it will be identical to the values which it
1929 * already has. --akpm
1930 */
1931 void do_SAK(struct tty_struct *tty)
1933 if (!tty)
1934 return;
1935 PREPARE_TQUEUE(&tty->SAK_tq, __do_SAK, tty);
1936 schedule_task(&tty->SAK_tq);
1939 /*
1940 * This routine is called out of the software interrupt to flush data
1941 * from the flip buffer to the line discipline.
1942 */
1943 static void flush_to_ldisc(void *private_)
1945 struct tty_struct *tty = (struct tty_struct *) private_;
1946 unsigned char *cp;
1947 char *fp;
1948 int count;
1949 unsigned long flags;
1951 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1952 queue_task(&tty->flip.tqueue, &tq_timer);
1953 return;
1955 if (tty->flip.buf_num) {
1956 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1957 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1958 tty->flip.buf_num = 0;
1960 save_flags(flags); cli();
1961 tty->flip.char_buf_ptr = tty->flip.char_buf;
1962 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1963 } else {
1964 cp = tty->flip.char_buf;
1965 fp = tty->flip.flag_buf;
1966 tty->flip.buf_num = 1;
1968 save_flags(flags); cli();
1969 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1970 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1972 count = tty->flip.count;
1973 tty->flip.count = 0;
1974 restore_flags(flags);
1976 tty->ldisc.receive_buf(tty, cp, fp, count);
1979 /*
1980 * Routine which returns the baud rate of the tty
1982 * Note that the baud_table needs to be kept in sync with the
1983 * include/asm/termbits.h file.
1984 */
1985 static int baud_table[] = {
1986 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1987 9600, 19200, 38400, 57600, 115200, 230400, 460800,
1988 #ifdef __sparc__
1989 76800, 153600, 307200, 614400, 921600
1990 #else
1991 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
1992 2500000, 3000000, 3500000, 4000000
1993 #endif
1994 };
1996 static int n_baud_table = sizeof(baud_table)/sizeof(int);
1998 int tty_get_baud_rate(struct tty_struct *tty)
2000 unsigned int cflag, i;
2002 cflag = tty->termios->c_cflag;
2004 i = cflag & CBAUD;
2005 if (i & CBAUDEX) {
2006 i &= ~CBAUDEX;
2007 if (i < 1 || i+15 >= n_baud_table)
2008 tty->termios->c_cflag &= ~CBAUDEX;
2009 else
2010 i += 15;
2012 if (i==15 && tty->alt_speed) {
2013 if (!tty->warned) {
2014 printk(KERN_WARNING "Use of setserial/setrocket to "
2015 "set SPD_* flags is deprecated\n");
2016 tty->warned = 1;
2018 return(tty->alt_speed);
2021 return baud_table[i];
2024 void tty_flip_buffer_push(struct tty_struct *tty)
2026 if (tty->low_latency)
2027 flush_to_ldisc((void *) tty);
2028 else
2029 queue_task(&tty->flip.tqueue, &tq_timer);
2032 /*
2033 * This subroutine initializes a tty structure.
2034 */
2035 static void initialize_tty_struct(struct tty_struct *tty)
2037 memset(tty, 0, sizeof(struct tty_struct));
2038 tty->magic = TTY_MAGIC;
2039 tty->ldisc = ldiscs[N_TTY];
2040 tty->pgrp = -1;
2041 tty->flip.char_buf_ptr = tty->flip.char_buf;
2042 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2043 tty->flip.tqueue.routine = flush_to_ldisc;
2044 tty->flip.tqueue.data = tty;
2045 init_MUTEX(&tty->flip.pty_sem);
2046 init_waitqueue_head(&tty->write_wait);
2047 init_waitqueue_head(&tty->read_wait);
2048 tty->tq_hangup.routine = do_tty_hangup;
2049 tty->tq_hangup.data = tty;
2050 sema_init(&tty->atomic_read, 1);
2051 sema_init(&tty->atomic_write, 1);
2052 spin_lock_init(&tty->read_lock);
2053 INIT_LIST_HEAD(&tty->tty_files);
2054 INIT_TQUEUE(&tty->SAK_tq, 0, 0);
2057 /*
2058 * The default put_char routine if the driver did not define one.
2059 */
2060 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2062 tty->driver.write(tty, 0, &ch, 1);
2065 /*
2066 * Register a tty device described by <driver>, with minor number <minor>.
2067 */
2068 void tty_register_devfs (struct tty_driver *driver, unsigned int flags, unsigned minor)
2070 #ifdef CONFIG_DEVFS_FS
2071 umode_t mode = S_IFCHR | S_IRUSR | S_IWUSR;
2072 kdev_t device = MKDEV (driver->major, minor);
2073 int idx = minor - driver->minor_start;
2074 char buf[32];
2076 switch (device) {
2077 case TTY_DEV:
2078 case PTMX_DEV:
2079 mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2080 break;
2081 default:
2082 if (driver->major == PTY_MASTER_MAJOR)
2083 mode |= S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2084 break;
2086 if ( (minor < driver->minor_start) ||
2087 (minor >= driver->minor_start + driver->num) ) {
2088 printk(KERN_ERR "Attempt to register invalid minor number "
2089 "with devfs (%d:%d).\n", (int)driver->major,(int)minor);
2090 return;
2092 # ifdef CONFIG_UNIX98_PTYS
2093 if ( (driver->major >= UNIX98_PTY_SLAVE_MAJOR) &&
2094 (driver->major < UNIX98_PTY_SLAVE_MAJOR + UNIX98_NR_MAJORS) )
2095 flags |= DEVFS_FL_CURRENT_OWNER;
2096 # endif
2097 sprintf(buf, driver->name, idx + driver->name_base);
2098 devfs_register (NULL, buf, flags | DEVFS_FL_DEFAULT,
2099 driver->major, minor, mode, &tty_fops, NULL);
2100 #endif /* CONFIG_DEVFS_FS */
2103 void tty_unregister_devfs (struct tty_driver *driver, unsigned minor)
2105 #ifdef CONFIG_DEVFS_FS
2106 void * handle;
2107 int idx = minor - driver->minor_start;
2108 char buf[32];
2110 sprintf(buf, driver->name, idx + driver->name_base);
2111 handle = devfs_find_handle (NULL, buf, driver->major, minor,
2112 DEVFS_SPECIAL_CHR, 0);
2113 devfs_unregister (handle);
2114 #endif /* CONFIG_DEVFS_FS */
2117 EXPORT_SYMBOL(tty_register_devfs);
2118 EXPORT_SYMBOL(tty_unregister_devfs);
2120 /*
2121 * Called by a tty driver to register itself.
2122 */
2123 int tty_register_driver(struct tty_driver *driver)
2125 int error;
2126 int i;
2128 if (driver->flags & TTY_DRIVER_INSTALLED)
2129 return 0;
2131 error = devfs_register_chrdev(driver->major, driver->name, &tty_fops);
2132 if (error < 0)
2133 return error;
2134 else if(driver->major == 0)
2135 driver->major = error;
2137 if (!driver->put_char)
2138 driver->put_char = tty_default_put_char;
2140 driver->prev = 0;
2141 driver->next = tty_drivers;
2142 if (tty_drivers) tty_drivers->prev = driver;
2143 tty_drivers = driver;
2145 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2146 for(i = 0; i < driver->num; i++)
2147 tty_register_devfs(driver, 0, driver->minor_start + i);
2149 proc_tty_register_driver(driver);
2150 return error;
2153 /*
2154 * Called by a tty driver to unregister itself.
2155 */
2156 int tty_unregister_driver(struct tty_driver *driver)
2158 int retval;
2159 struct tty_driver *p;
2160 int i, found = 0;
2161 struct termios *tp;
2162 const char *othername = NULL;
2164 if (*driver->refcount)
2165 return -EBUSY;
2167 for (p = tty_drivers; p; p = p->next) {
2168 if (p == driver)
2169 found++;
2170 else if (p->major == driver->major)
2171 othername = p->name;
2174 if (!found)
2175 return -ENOENT;
2177 if (othername == NULL) {
2178 retval = devfs_unregister_chrdev(driver->major, driver->name);
2179 if (retval)
2180 return retval;
2181 } else
2182 devfs_register_chrdev(driver->major, othername, &tty_fops);
2184 if (driver->prev)
2185 driver->prev->next = driver->next;
2186 else
2187 tty_drivers = driver->next;
2189 if (driver->next)
2190 driver->next->prev = driver->prev;
2192 /*
2193 * Free the termios and termios_locked structures because
2194 * we don't want to get memory leaks when modular tty
2195 * drivers are removed from the kernel.
2196 */
2197 for (i = 0; i < driver->num; i++) {
2198 tp = driver->termios[i];
2199 if (tp) {
2200 driver->termios[i] = NULL;
2201 kfree(tp);
2203 tp = driver->termios_locked[i];
2204 if (tp) {
2205 driver->termios_locked[i] = NULL;
2206 kfree(tp);
2208 tty_unregister_devfs(driver, driver->minor_start + i);
2210 proc_tty_unregister_driver(driver);
2211 return 0;
2215 /*
2216 * Initialize the console device. This is called *early*, so
2217 * we can't necessarily depend on lots of kernel help here.
2218 * Just do some early initializations, and do the complex setup
2219 * later.
2220 */
2221 void __init console_init(void)
2223 /* Setup the default TTY line discipline. */
2224 memset(ldiscs, 0, sizeof(ldiscs));
2225 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2227 /*
2228 * Set up the standard termios. Individual tty drivers may
2229 * deviate from this; this is used as a template.
2230 */
2231 memset(&tty_std_termios, 0, sizeof(struct termios));
2232 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2233 tty_std_termios.c_iflag = ICRNL | IXON;
2234 tty_std_termios.c_oflag = OPOST | ONLCR;
2235 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2236 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2237 ECHOCTL | ECHOKE | IEXTEN;
2239 /*
2240 * set up the console device so that later boot sequences can
2241 * inform about problems etc..
2242 */
2243 #ifdef CONFIG_EARLY_PRINTK
2244 disable_early_printk();
2245 #endif
2247 #ifdef CONFIG_XEN_CONSOLE
2248 xen_console_init();
2249 #endif
2251 #ifdef CONFIG_VT
2252 con_init();
2253 #endif
2254 #ifdef CONFIG_AU1X00_SERIAL_CONSOLE
2255 au1x00_serial_console_init();
2256 #endif
2257 #ifdef CONFIG_SERIAL_CONSOLE
2258 #if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2259 console_8xx_init();
2260 #elif defined(CONFIG_MAC_SERIAL) && defined(CONFIG_SERIAL)
2261 if (_machine == _MACH_Pmac)
2262 mac_scc_console_init();
2263 else
2264 serial_console_init();
2265 #elif defined(CONFIG_MAC_SERIAL)
2266 mac_scc_console_init();
2267 #elif defined(CONFIG_PARISC)
2268 pdc_console_init();
2269 #elif defined(CONFIG_SERIAL)
2270 serial_console_init();
2271 #endif /* CONFIG_8xx */
2272 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2273 vme_scc_console_init();
2274 #endif
2275 #if defined(CONFIG_SERIAL167)
2276 serial167_console_init();
2277 #endif
2278 #if defined(CONFIG_SH_SCI)
2279 sci_console_init();
2280 #endif
2281 #endif
2282 #ifdef CONFIG_SERIAL_DEC_CONSOLE
2283 dec_serial_console_init();
2284 #endif
2285 #ifdef CONFIG_TN3270_CONSOLE
2286 tub3270_con_init();
2287 #endif
2288 #ifdef CONFIG_TN3215
2289 con3215_init();
2290 #endif
2291 #ifdef CONFIG_HWC
2292 hwc_console_init();
2293 #endif
2294 #ifdef CONFIG_STDIO_CONSOLE
2295 stdio_console_init();
2296 #endif
2297 #ifdef CONFIG_SERIAL_21285_CONSOLE
2298 rs285_console_init();
2299 #endif
2300 #ifdef CONFIG_SERIAL_SA1100_CONSOLE
2301 sa1100_rs_console_init();
2302 #endif
2303 #ifdef CONFIG_ARC_CONSOLE
2304 arc_console_init();
2305 #endif
2306 #ifdef CONFIG_SERIAL_AMBA_CONSOLE
2307 ambauart_console_init();
2308 #endif
2309 #ifdef CONFIG_SERIAL_TX3912_CONSOLE
2310 tx3912_console_init();
2311 #endif
2312 #ifdef CONFIG_TXX927_SERIAL_CONSOLE
2313 txx927_console_init();
2314 #endif
2315 #ifdef CONFIG_SERIAL_TXX9_CONSOLE
2316 txx9_serial_console_init();
2317 #endif
2318 #ifdef CONFIG_SIBYTE_SB1250_DUART_CONSOLE
2319 sb1250_serial_console_init();
2320 #endif
2321 #ifdef CONFIG_IP22_SERIAL
2322 sgi_serial_console_init();
2323 #endif
2326 static struct tty_driver dev_tty_driver, dev_syscons_driver;
2327 #ifdef CONFIG_UNIX98_PTYS
2328 static struct tty_driver dev_ptmx_driver;
2329 #endif
2330 #ifdef CONFIG_HVC_CONSOLE
2331 hvc_console_init();
2332 #endif
2333 #ifdef CONFIG_VT
2334 static struct tty_driver dev_console_driver;
2335 #endif
2337 /*
2338 * Ok, now we can initialize the rest of the tty devices and can count
2339 * on memory allocations, interrupts etc..
2340 */
2341 void __init tty_init(void)
2343 /*
2344 * dev_tty_driver and dev_console_driver are actually magic
2345 * devices which get redirected at open time. Nevertheless,
2346 * we register them so that register_chrdev is called
2347 * appropriately.
2348 */
2349 memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2350 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2351 dev_tty_driver.driver_name = "/dev/tty";
2352 dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2353 dev_tty_driver.name_base = 0;
2354 dev_tty_driver.major = TTYAUX_MAJOR;
2355 dev_tty_driver.minor_start = 0;
2356 dev_tty_driver.num = 1;
2357 dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2358 dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2360 if (tty_register_driver(&dev_tty_driver))
2361 panic("Couldn't register /dev/tty driver\n");
2363 dev_syscons_driver = dev_tty_driver;
2364 dev_syscons_driver.driver_name = "/dev/console";
2365 dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2366 dev_syscons_driver.major = TTYAUX_MAJOR;
2367 dev_syscons_driver.minor_start = 1;
2368 dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2369 dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2371 if (tty_register_driver(&dev_syscons_driver))
2372 panic("Couldn't register /dev/console driver\n");
2374 /* console calls tty_register_driver() before kmalloc() works.
2375 * Thus, we can't devfs_register() then. Do so now, instead.
2376 */
2377 #ifdef CONFIG_VT
2378 con_init_devfs();
2379 #endif
2381 #ifdef CONFIG_UNIX98_PTYS
2382 dev_ptmx_driver = dev_tty_driver;
2383 dev_ptmx_driver.driver_name = "/dev/ptmx";
2384 dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2385 dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2386 dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2387 dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2388 dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2390 if (tty_register_driver(&dev_ptmx_driver))
2391 panic("Couldn't register /dev/ptmx driver\n");
2392 #endif
2394 #ifdef CONFIG_VT
2395 dev_console_driver = dev_tty_driver;
2396 dev_console_driver.driver_name = "/dev/vc/0";
2397 dev_console_driver.name = dev_console_driver.driver_name + 5;
2398 dev_console_driver.major = TTY_MAJOR;
2399 dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2400 dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2402 if (tty_register_driver(&dev_console_driver))
2403 panic("Couldn't register /dev/tty0 driver\n");
2405 kbd_init();
2406 #endif
2408 #ifdef CONFIG_SGI_L1_SERIAL_CONSOLE
2409 if (ia64_platform_is("sn2")) {
2410 sn_sal_serial_console_init();
2411 return; /* only one console right now for SN2 */
2413 #endif
2414 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2415 espserial_init();
2416 #endif
2417 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2418 vme_scc_init();
2419 #endif
2420 #ifdef CONFIG_SERIAL_TX3912
2421 tx3912_rs_init();
2422 #endif
2423 #ifdef CONFIG_ROCKETPORT
2424 rp_init();
2425 #endif
2426 #ifdef CONFIG_SERIAL167
2427 serial167_init();
2428 #endif
2429 #ifdef CONFIG_CYCLADES
2430 cy_init();
2431 #endif
2432 #ifdef CONFIG_STALLION
2433 stl_init();
2434 #endif
2435 #ifdef CONFIG_ISTALLION
2436 stli_init();
2437 #endif
2438 #ifdef CONFIG_DIGI
2439 pcxe_init();
2440 #endif
2441 #ifdef CONFIG_DIGIEPCA
2442 pc_init();
2443 #endif
2444 #ifdef CONFIG_SPECIALIX
2445 specialix_init();
2446 #endif
2447 #if (defined(CONFIG_8xx) || defined(CONFIG_CPM2))
2448 rs_8xx_init();
2449 #endif /* CONFIG_8xx */
2450 pty_init();
2451 #ifdef CONFIG_MOXA_SMARTIO
2452 mxser_init();
2453 #endif
2454 #ifdef CONFIG_MOXA_INTELLIO
2455 moxa_init();
2456 #endif
2457 #ifdef CONFIG_VT
2458 vcs_init();
2459 #endif
2460 #ifdef CONFIG_TN3270
2461 tub3270_init();
2462 #endif
2463 #ifdef CONFIG_TN3215
2464 tty3215_init();
2465 #endif
2466 #ifdef CONFIG_HWC
2467 hwc_tty_init();
2468 #endif
2469 #ifdef CONFIG_A2232
2470 a2232board_init();
2471 #endif