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