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