xref: /freebsd/sys/isa/syscons_isa.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
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", &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 
88 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
89 }
90 
91 static int
92 scprobe(device_t dev)
93 {
94 
95 	/* No pnp support */
96 	if (isa_get_vendorid(dev))
97 		return (ENXIO);
98 
99 	device_set_desc(dev, "System console");
100 	return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev)));
101 }
102 
103 static int
104 scattach(device_t dev)
105 {
106 
107 	return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) |
108 	    SC_AUTODETECT_KBD));
109 }
110 
111 static int
112 scsuspend(device_t dev)
113 {
114 	int		retry = 10;
115 	sc_softc_t	*sc;
116 
117 	sc = &main_softc;
118 
119 	if (sc->cur_scp == NULL)
120 		return (0);
121 
122 	if (sc->suspend_in_progress == 0) {
123 		sc_cur_scr = sc->cur_scp->index;
124 		if (!sc_no_suspend_vtswitch && sc_cur_scr != 0)
125 			do {
126 				sc_switch_scr(sc, 0);
127 				if (!sc->switch_in_progress)
128 					break;
129 				pause("scsuspend", hz);
130 			} while (retry--);
131 	}
132 	sc->suspend_in_progress++;
133 
134 	return (0);
135 }
136 
137 static int
138 scresume(device_t dev)
139 {
140 	sc_softc_t	*sc;
141 
142 	sc = &main_softc;
143 
144 	if (sc->cur_scp == NULL)
145 		return (0);
146 
147 	sc->suspend_in_progress--;
148 	if (sc->suspend_in_progress == 0) {
149 		if (!sc_no_suspend_vtswitch && sc_cur_scr != 0)
150 			sc_switch_scr(sc, sc_cur_scr);
151 		else
152 			mark_all(sc->cur_scp);
153 	}
154 
155 	return (0);
156 }
157 
158 int
159 sc_max_unit(void)
160 {
161 
162 	return (devclass_get_maxunit(sc_devclass));
163 }
164 
165 sc_softc_t
166 *sc_get_softc(int unit, int flags)
167 {
168 	sc_softc_t *sc;
169 
170 	if (unit < 0)
171 		return (NULL);
172 	if ((flags & SC_KERNEL_CONSOLE) != 0) {
173 		/* FIXME: clear if it is wired to another unit! */
174 		sc = &main_softc;
175 	} else {
176 	        sc = device_get_softc(devclass_get_device(sc_devclass, unit));
177 		if (sc == NULL)
178 			return (NULL);
179 	}
180 	sc->unit = unit;
181 	if ((sc->flags & SC_INIT_DONE) == 0) {
182 		sc->keyboard = -1;
183 		sc->adapter = -1;
184 		sc->cursor_char = SC_CURSOR_CHAR;
185 		sc->mouse_char = SC_MOUSE_CHAR;
186 	}
187 	return (sc);
188 }
189 
190 sc_softc_t
191 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
192 {
193 	sc_softc_t *sc;
194 	int i;
195 	int units;
196 
197 	sc = &main_softc;
198 	if ((adp == NULL || adp == sc->adp) &&
199 	    (kbd == NULL || kbd == sc->kbd))
200 		return (sc);
201 	units = devclass_get_maxunit(sc_devclass);
202 	for (i = 0; i < units; ++i) {
203 	        sc = device_get_softc(devclass_get_device(sc_devclass, i));
204 		if (sc == NULL)
205 			continue;
206 		if ((adp == NULL || adp == sc->adp) &&
207 		    (kbd == NULL || kbd == sc->kbd))
208 			return (sc);
209 	}
210 	return (NULL);
211 }
212 
213 int
214 sc_get_cons_priority(int *unit, int *flags)
215 {
216 	const char *at;
217 	int f, u;
218 
219 #ifdef XBOX
220 	/*
221 	 * The XBox Loader does not support hints, which makes our initial
222 	 * console probe fail. Therefore, if an XBox is found, we hardcode the
223 	 * existence of the console, as it is always there anyway.
224 	 */
225 	if (arch_i386_is_xbox) {
226 		*unit = 0;
227 		*flags = SC_KERNEL_CONSOLE;
228 		return (CN_INTERNAL);
229 	}
230 #endif
231 
232 	*unit = -1;
233 	for (u = 0; u < 16; u++) {
234 		if (resource_disabled(SC_DRIVER_NAME, u))
235 			continue;
236 		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
237 			continue;
238 		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
239 			f = 0;
240 		if (f & SC_KERNEL_CONSOLE) {
241 			/* the user designates this unit to be the console */
242 			*unit = u;
243 			*flags = f;
244 			break;
245 		}
246 		if (*unit < 0) {
247 			/* ...otherwise remember the first found unit */
248 			*unit = u;
249 			*flags = f;
250 		}
251 	}
252 	if (*unit < 0) {
253 		*unit = 0;
254 		*flags = 0;
255 	}
256 #if 0
257 	return ((*flags & SC_KERNEL_CONSOLE) != 0 ? CN_INTERNAL : CN_NORMAL);
258 #endif
259 	return (CN_INTERNAL);
260 }
261 
262 void
263 sc_get_bios_values(bios_values_t *values)
264 {
265 #if defined(__i386__) || defined(__amd64__)
266 	uint8_t shift;
267 
268 	values->cursor_start = *(uint8_t *)BIOS_PADDRTOVADDR(0x461);
269 	values->cursor_end = *(uint8_t *)BIOS_PADDRTOVADDR(0x460);
270 	shift = *(uint8_t *)BIOS_PADDRTOVADDR(0x417);
271 	values->shift_state = ((shift & BIOS_CLKED) != 0 ? CLKED : 0) |
272 	    ((shift & BIOS_NLKED) != 0 ? NLKED : 0) |
273 	    ((shift & BIOS_SLKED) != 0 ? SLKED : 0) |
274 	    ((shift & BIOS_ALKED) != 0 ? ALKED : 0);
275 #else
276 	values->cursor_start = 0;
277 	values->cursor_end = 32;
278 	values->shift_state = 0;
279 #endif
280 	values->bell_pitch = BELL_PITCH;
281 }
282 
283 int
284 sc_tone(int herz)
285 {
286 
287 #if defined(HAS_TIMER_SPKR)
288 	if (herz) {
289 		if (timer_spkr_acquire())
290 			return (EBUSY);
291 		timer_spkr_setfreq(herz);
292 	} else
293 		timer_spkr_release();
294 #endif
295 
296 	return (0);
297 }
298 
299 static device_method_t sc_methods[] = {
300 	DEVMETHOD(device_identify,	scidentify),
301 	DEVMETHOD(device_probe,         scprobe),
302 	DEVMETHOD(device_attach,        scattach),
303 	DEVMETHOD(device_suspend,       scsuspend),
304 	DEVMETHOD(device_resume,        scresume),
305 	{ 0, 0 }
306 };
307 
308 static driver_t sc_driver = {
309 	SC_DRIVER_NAME,
310 	sc_methods,
311 	sizeof(sc_softc_t),
312 };
313 
314 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
315 
316 static devclass_t	scpm_devclass;
317 
318 static void
319 scpm_identify(driver_t *driver, device_t parent)
320 {
321 
322 	device_add_child(parent, "scpm", 0);
323 }
324 
325 static int
326 scpm_probe(device_t dev)
327 {
328 
329 	device_set_desc(dev, SC_DRIVER_NAME " suspend/resume");
330 	device_quiet(dev);
331 
332 	return (BUS_PROBE_DEFAULT);
333 }
334 
335 static int
336 scpm_attach(device_t dev)
337 {
338 
339 	bus_generic_probe(dev);
340 	bus_generic_attach(dev);
341 
342 	return (0);
343 }
344 
345 static int
346 scpm_suspend(device_t dev)
347 {
348 	int error;
349 
350 	error = bus_generic_suspend(dev);
351 	if (error != 0)
352 		return (error);
353 
354 	return (scsuspend(dev));
355 }
356 
357 static int
358 scpm_resume(device_t dev)
359 {
360 
361 	scresume(dev);
362 
363 	return (bus_generic_resume(dev));
364 }
365 
366 static device_method_t scpm_methods[] = {
367 	DEVMETHOD(device_identify,	scpm_identify),
368 	DEVMETHOD(device_probe,		scpm_probe),
369 	DEVMETHOD(device_attach,	scpm_attach),
370 	DEVMETHOD(device_suspend,	scpm_suspend),
371 	DEVMETHOD(device_resume,	scpm_resume),
372 	{ 0, 0 }
373 };
374 
375 static driver_t scpm_driver = {
376 	"scpm",
377 	scpm_methods,
378 	0
379 };
380 
381 DRIVER_MODULE(scpm, vgapm, scpm_driver, scpm_devclass, 0, 0);
382