1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/termios.h>
28 #include <sys/stream.h>
29 #include <sys/stropts.h>
30 #include <sys/kmem.h>
31 #include <sys/stat.h>
32 #include <sys/sunddi.h>
33 #include <sys/ddi.h>
34 #include <sys/bitmap.h>
35 #include <sys/sysmacros.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/zone.h>
38 #include <sys/thread.h>
39 #ifdef DEBUG
40 #include <sys/strlog.h>
41 #endif
42
43 #include <sys/consdev.h>
44 #include <sys/console.h>
45 #include <sys/wscons.h>
46 #include <sys/vt_impl.h>
47 #include <sys/note.h>
48 #include <sys/avl.h>
49
50 /* set if console driver is attached */
51 dev_info_t *wc_dip = NULL;
52 /* active virtual console minor number */
53 minor_t vc_active_console = VT_MINOR_INVALID;
54 /*
55 * console_user symbol link minor number.
56 * VT_MINOR_INVALID : /dev/console
57 * N : /dev/vt/N
58 */
59 minor_t vc_cons_user = VT_MINOR_INVALID;
60 /* vc_state_t AVL tree */
61 avl_tree_t vc_avl_root;
62 /* virtual console global lock */
63 kmutex_t vc_lock;
64
65 /*
66 * Called from vt devname part. Checks if dip is attached. If it is,
67 * return its major number.
68 */
69 major_t
70 vt_wc_attached(void)
71 {
72 major_t maj = (major_t)-1;
73
74 mutex_enter(&vc_lock);
75
76 if (wc_dip)
77 maj = ddi_driver_major(wc_dip);
78
79 mutex_exit(&vc_lock);
80
81 return (maj);
82 }
83
84 void
85 vt_getactive(char *buf, int buflen)
86 {
87 ASSERT(buf);
88 ASSERT(buflen != 0);
89
90 mutex_enter(&vc_lock);
91
92 if (vc_active_console == 0 || vc_active_console == VT_MINOR_INVALID)
93 (void) snprintf(buf, buflen, "/dev/console");
94 else
95 (void) snprintf(buf, buflen, "%u", vc_active_console);
96
97 mutex_exit(&vc_lock);
98 }
99
100 void
101 vt_getconsuser(char *buf, int buflen)
102 {
103 ASSERT(buf);
104 ASSERT(buflen != 0);
105
106 mutex_enter(&vc_lock);
107
108 if (vc_cons_user == VT_MINOR_INVALID) {
109 (void) snprintf(buf, buflen, "/dev/console");
110 mutex_exit(&vc_lock);
111 return;
112 }
113
114 (void) snprintf(buf, buflen, "%u", vc_cons_user);
115 mutex_exit(&vc_lock);
116 }
117
118 boolean_t
119 vt_minor_valid(minor_t minor)
120 {
121 if (consmode == CONS_FW) {
122 if (minor == 0)
123 return (B_TRUE);
124
125 return (B_FALSE);
126 }
127
128 mutex_enter(&vc_lock);
129 if (minor < VC_INSTANCES_COUNT) {
130 mutex_exit(&vc_lock);
131 return (B_TRUE);
132 }
133
134 mutex_exit(&vc_lock);
135 return (B_FALSE);
136
137 }