1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include "opt_syscons.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/cons.h>
38 #include <sys/kbio.h>
39 #include <sys/consio.h>
40 #include <sys/sysctl.h>
41
42 #if defined(__i386__) || defined(__amd64__)
43
44 #include <machine/clock.h>
45 #include <machine/md_var.h>
46 #include <machine/pc/bios.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_param.h>
51
52 #define BIOS_CLKED (1 << 6)
53 #define BIOS_NLKED (1 << 5)
54 #define BIOS_SLKED (1 << 4)
55 #define BIOS_ALKED 0
56
57 #endif
58
59 #include <dev/syscons/syscons.h>
60
61 #include <isa/isavar.h>
62
63 static devclass_t sc_devclass;
64
65 static sc_softc_t main_softc;
66
67 static void
scidentify(driver_t * driver,device_t parent)68 scidentify(driver_t *driver, device_t parent)
69 {
70
71 BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
72 }
73
74 static int
scprobe(device_t dev)75 scprobe(device_t dev)
76 {
77
78 /* No pnp support */
79 if (isa_get_vendorid(dev))
80 return (ENXIO);
81
82 device_set_desc(dev, "System console");
83 return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev)));
84 }
85
86 static int
scattach(device_t dev)87 scattach(device_t dev)
88 {
89
90 return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) |
91 SC_AUTODETECT_KBD));
92 }
93
94 int
sc_max_unit(void)95 sc_max_unit(void)
96 {
97
98 return (devclass_get_maxunit(sc_devclass));
99 }
100
101 sc_softc_t
sc_get_softc(int unit,int flags)102 *sc_get_softc(int unit, int flags)
103 {
104 sc_softc_t *sc;
105
106 if (unit < 0)
107 return (NULL);
108 if ((flags & SC_KERNEL_CONSOLE) != 0) {
109 /* FIXME: clear if it is wired to another unit! */
110 sc = &main_softc;
111 } else {
112 sc = device_get_softc(devclass_get_device(sc_devclass, unit));
113 if (sc == NULL)
114 return (NULL);
115 }
116 sc->unit = unit;
117 if ((sc->flags & SC_INIT_DONE) == 0) {
118 sc->adapter = -1;
119 sc->cursor_char = SC_CURSOR_CHAR;
120 sc->mouse_char = SC_MOUSE_CHAR;
121 }
122 return (sc);
123 }
124
125 sc_softc_t
sc_find_softc(struct video_adapter * adp,struct keyboard * kbd)126 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
127 {
128 sc_softc_t *sc;
129 int i;
130 int units;
131
132 sc = &main_softc;
133 if ((adp == NULL || adp == sc->adp) &&
134 (kbd == NULL || kbd == sc->kbd))
135 return (sc);
136 units = devclass_get_maxunit(sc_devclass);
137 for (i = 0; i < units; ++i) {
138 sc = device_get_softc(devclass_get_device(sc_devclass, i));
139 if (sc == NULL)
140 continue;
141 if ((adp == NULL || adp == sc->adp) &&
142 (kbd == NULL || kbd == sc->kbd))
143 return (sc);
144 }
145 return (NULL);
146 }
147
148 int
sc_get_cons_priority(int * unit,int * flags)149 sc_get_cons_priority(int *unit, int *flags)
150 {
151 const char *at;
152 int f, u;
153
154 *unit = -1;
155 for (u = 0; u < 16; u++) {
156 if (resource_disabled(SC_DRIVER_NAME, u))
157 continue;
158 if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
159 continue;
160 if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
161 f = 0;
162 if (f & SC_KERNEL_CONSOLE) {
163 /* the user designates this unit to be the console */
164 *unit = u;
165 *flags = f;
166 break;
167 }
168 if (*unit < 0) {
169 /* ...otherwise remember the first found unit */
170 *unit = u;
171 *flags = f;
172 }
173 }
174 if (*unit < 0) {
175 *unit = 0;
176 *flags = 0;
177 }
178 #if 0
179 return ((*flags & SC_KERNEL_CONSOLE) != 0 ? CN_INTERNAL : CN_NORMAL);
180 #endif
181 return (CN_INTERNAL);
182 }
183
184 void
sc_get_bios_values(bios_values_t * values)185 sc_get_bios_values(bios_values_t *values)
186 {
187 #if defined(__i386__) || defined(__amd64__)
188 uint8_t shift;
189
190 shift = *(uint8_t *)BIOS_PADDRTOVADDR(0x417);
191 values->shift_state = ((shift & BIOS_CLKED) != 0 ? CLKED : 0) |
192 ((shift & BIOS_NLKED) != 0 ? NLKED : 0) |
193 ((shift & BIOS_SLKED) != 0 ? SLKED : 0) |
194 ((shift & BIOS_ALKED) != 0 ? ALKED : 0);
195 #endif
196 values->bell_pitch = BELL_PITCH;
197 }
198
199 int
sc_tone(int herz)200 sc_tone(int herz)
201 {
202
203 #if defined(HAS_TIMER_SPKR)
204 if (herz) {
205 if (timer_spkr_acquire())
206 return (EBUSY);
207 timer_spkr_setfreq(herz);
208 } else
209 timer_spkr_release();
210 #endif
211
212 return (0);
213 }
214
215 static device_method_t sc_methods[] = {
216 DEVMETHOD(device_identify, scidentify),
217 DEVMETHOD(device_probe, scprobe),
218 DEVMETHOD(device_attach, scattach),
219 { 0, 0 }
220 };
221
222 static driver_t sc_driver = {
223 SC_DRIVER_NAME,
224 sc_methods,
225 sizeof(sc_softc_t),
226 };
227
228 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
229