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