debuggers.hg

view xenolinux-2.4.21-sparse/drivers/char/tty_io.c @ 653:3a237a10a0c6

bitkeeper revision 1.344 (3f12be53t1VivuAfELH_J3XUhJ2Frg)

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