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