xref: /freebsd/sys/isa/syscons_isa.c (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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 #include "opt_xbox.h"
64 
65 #ifdef XBOX
66 #include <machine/xbox.h>
67 #endif
68 
69 static devclass_t	sc_devclass;
70 
71 static sc_softc_t main_softc;
72 #ifdef SC_NO_SUSPEND_VTYSWITCH
73 static int sc_no_suspend_vtswitch = 1;
74 #else
75 static int sc_no_suspend_vtswitch = 0;
76 #endif
77 static int sc_cur_scr;
78 
79 TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", (int *)&sc_no_suspend_vtswitch);
80 SYSCTL_DECL(_hw_syscons);
81 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
82 	&sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
83 
84 static void
85 scidentify (driver_t *driver, device_t parent)
86 {
87 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
88 }
89 
90 static int
91 scprobe(device_t dev)
92 {
93 	/* No pnp support */
94 	if (isa_get_vendorid(dev))
95 		return (ENXIO);
96 
97 	device_set_desc(dev, "System console");
98 	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
99 }
100 
101 static int
102 scattach(device_t dev)
103 {
104 	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
105 }
106 
107 static int
108 scsuspend(device_t dev)
109 {
110 	int		retry = 10;
111 	sc_softc_t	*sc;
112 
113 	sc = &main_softc;
114 
115 	if (sc->cur_scp == NULL)
116 		return (0);
117 
118 	sc_cur_scr = sc->cur_scp->index;
119 
120 	if (sc_no_suspend_vtswitch)
121 		return (0);
122 
123 	do {
124 		sc_switch_scr(sc, 0);
125 		if (!sc->switch_in_progress) {
126 			break;
127 		}
128 		pause("scsuspend", hz);
129 	} while (retry--);
130 
131 	return (0);
132 }
133 
134 static int
135 scresume(device_t dev)
136 {
137 	sc_softc_t	*sc;
138 
139 	if (sc_no_suspend_vtswitch)
140 		return (0);
141 
142 	sc = &main_softc;
143 	sc_switch_scr(sc, sc_cur_scr);
144 
145 	return (0);
146 }
147 
148 int
149 sc_max_unit(void)
150 {
151 	return devclass_get_maxunit(sc_devclass);
152 }
153 
154 sc_softc_t
155 *sc_get_softc(int unit, int flags)
156 {
157 	sc_softc_t *sc;
158 
159 	if (unit < 0)
160 		return NULL;
161 	if (flags & SC_KERNEL_CONSOLE) {
162 		/* FIXME: clear if it is wired to another unit! */
163 		sc = &main_softc;
164 	} else {
165 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
166 		if (sc == NULL)
167 			return NULL;
168 	}
169 	sc->unit = unit;
170 	if (!(sc->flags & SC_INIT_DONE)) {
171 		sc->keyboard = -1;
172 		sc->adapter = -1;
173 		sc->cursor_char = SC_CURSOR_CHAR;
174 		sc->mouse_char = SC_MOUSE_CHAR;
175 	}
176 	return sc;
177 }
178 
179 sc_softc_t
180 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
181 {
182 	sc_softc_t *sc;
183 	int units;
184 	int i;
185 
186 	sc = &main_softc;
187 	if (((adp == NULL) || (adp == sc->adp))
188 	    && ((kbd == NULL) || (kbd == sc->kbd)))
189 		return sc;
190 	units = devclass_get_maxunit(sc_devclass);
191 	for (i = 0; i < units; ++i) {
192 	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
193 		if (sc == NULL)
194 			continue;
195 		if (((adp == NULL) || (adp == sc->adp))
196 		    && ((kbd == NULL) || (kbd == sc->kbd)))
197 			return sc;
198 	}
199 	return NULL;
200 }
201 
202 int
203 sc_get_cons_priority(int *unit, int *flags)
204 {
205 	const char *at;
206 	int u, f;
207 
208 #ifdef XBOX
209 	/*
210 	 * The XBox Loader does not support hints, which makes our initial
211 	 * console probe fail. Therefore, if an XBox is found, we hardcode the
212 	 * existence of the console, as it is always there anyway.
213 	 */
214 	if (arch_i386_is_xbox) {
215 		*unit = 0;
216 		*flags = SC_KERNEL_CONSOLE;
217 		return CN_INTERNAL;
218 	}
219 #endif
220 
221 	*unit = -1;
222 	for (u = 0; u < 16; u++) {
223 		if (resource_disabled(SC_DRIVER_NAME, u))
224 			continue;
225 		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
226 			continue;
227 		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
228 			f = 0;
229 		if (f & SC_KERNEL_CONSOLE) {
230 			/* the user designates this unit to be the console */
231 			*unit = u;
232 			*flags = f;
233 			break;
234 		}
235 		if (*unit < 0) {
236 			/* ...otherwise remember the first found unit */
237 			*unit = u;
238 			*flags = f;
239 		}
240 	}
241 	if (*unit < 0)
242 		return CN_DEAD;
243 #if 0
244 	return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
245 #endif
246 	return CN_INTERNAL;
247 }
248 
249 void
250 sc_get_bios_values(bios_values_t *values)
251 {
252 #if defined(__i386__) || defined(__amd64__)
253 	u_int8_t shift;
254 
255 	values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
256 	values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
257 	shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
258 	values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
259 			       | ((shift & BIOS_NLKED) ? NLKED : 0)
260 			       | ((shift & BIOS_SLKED) ? SLKED : 0)
261 			       | ((shift & BIOS_ALKED) ? ALKED : 0);
262 #else
263 	values->cursor_start = 0;
264 	values->cursor_end = 32;
265 	values->shift_state = 0;
266 #endif
267 	values->bell_pitch = BELL_PITCH;
268 }
269 
270 int
271 sc_tone(int herz)
272 {
273 #if defined(HAS_TIMER_SPKR)
274 	if (herz) {
275 		if (timer_spkr_acquire())
276 			return EBUSY;
277 		timer_spkr_setfreq(herz);
278 	} else {
279 		timer_spkr_release();
280 	}
281 #endif
282 
283 	return 0;
284 }
285 
286 static device_method_t sc_methods[] = {
287 	DEVMETHOD(device_identify,	scidentify),
288 	DEVMETHOD(device_probe,         scprobe),
289 	DEVMETHOD(device_attach,        scattach),
290 	DEVMETHOD(device_suspend,       scsuspend),
291 	DEVMETHOD(device_resume,        scresume),
292 	{ 0, 0 }
293 };
294 
295 static driver_t sc_driver = {
296 	SC_DRIVER_NAME,
297 	sc_methods,
298 	sizeof(sc_softc_t),
299 };
300 
301 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
302