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