xref: /openbsd-src/sys/dev/cninit.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: cninit.c,v 1.4 1998/06/17 14:58:35 mickey Exp $	*/
2 /*	$NetBSD: cninit.c,v 1.2 1995/04/11 22:08:10 pk Exp $	*/
3 
4 /*
5  * Copyright (c) 1988 University of Utah.
6  * Copyright (c) 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * from: Utah $Hdr: cons.c 1.7 92/01/21$
42  *
43  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/user.h>
50 #include <sys/buf.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/conf.h>
55 #include <sys/vnode.h>
56 
57 #include <dev/cons.h>
58 
59 extern struct consdev constab[];
60 
61 extern struct	consdev *cn_tab;	/* physical console device info */
62 
63 void
64 cninit()
65 {
66 	register struct consdev *cp;
67 
68 	/*
69 	 * Collect information about all possible consoles
70 	 * and find the one with highest priority
71 	 */
72 	for (cp = constab; cp->cn_probe; cp++) {
73 		(*cp->cn_probe)(cp);
74 		if (cp->cn_pri > CN_DEAD &&
75 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
76 			cn_tab = cp;
77 	}
78 	/*
79 	 * No console, we can handle it
80 	 */
81 	if ((cp = cn_tab) == NULL)
82 		return;
83 	/*
84 	 * Turn on console
85 	 */
86 	(*cp->cn_init)(cp);
87 }
88 
89 int
90 cnset(dev)
91 	dev_t dev;
92 {
93 	struct consdev *cp;
94 
95 	/*
96 	 * Look for the specified console device and use it.
97 	 */
98 	for (cp = constab; cp->cn_probe; cp++) {
99 		if (major(cp->cn_dev) == major(dev)) {
100 			/* short-circuit noop */
101 			if (cp == cn_tab && cp->cn_dev == dev)
102 				return (0);
103 			if (cp->cn_pri > CN_DEAD) {
104 				cn_tab = cp;
105 				cp->cn_dev = dev;
106 				/* Turn it on.  */
107 				(cp->cn_init)(cp);
108 				return (0);
109 			}
110 			break;
111 		}
112 	}
113 	return (1);
114 }
115