syscons_isa.c revision 117167
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 117167 2003-07-02 16:09:02Z jhb $");
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#ifdef __i386__
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
51#include <i386/isa/timerreg.h>
52
53#define BIOS_CLKED	(1 << 6)
54#define BIOS_NLKED	(1 << 5)
55#define BIOS_SLKED	(1 << 4)
56#define BIOS_ALKED	0
57
58#endif /* __i386__ */
59
60#include <dev/syscons/syscons.h>
61
62#include <isa/isareg.h>
63#include <isa/isavar.h>
64
65static devclass_t	sc_devclass;
66
67static sc_softc_t main_softc;
68#ifdef SC_NO_SUSPEND_VTYSWITCH
69static int sc_no_suspend_vtswitch = 1;
70#else
71static int sc_no_suspend_vtswitch = 0;
72#endif
73static int sc_cur_scr;
74
75TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", (int *)&sc_no_suspend_vtswitch);
76SYSCTL_DECL(_hw_syscons);
77SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
78	&sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
79
80static void
81scidentify (driver_t *driver, device_t parent)
82{
83	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
84}
85
86static int
87scprobe(device_t dev)
88{
89	/* No pnp support */
90	if (isa_get_vendorid(dev))
91		return (ENXIO);
92
93	device_set_desc(dev, "System console");
94	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
95}
96
97static int
98scattach(device_t dev)
99{
100	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
101}
102
103static int
104scsuspend(device_t dev)
105{
106	int		retry = 10;
107	static int	dummy;
108	sc_softc_t	*sc;
109
110	sc = &main_softc;
111	sc_cur_scr = sc->cur_scp->index;
112
113	if (sc_no_suspend_vtswitch)
114		return (0);
115
116	do {
117		sc_switch_scr(sc, 0);
118		if (!sc->switch_in_progress) {
119			break;
120		}
121		tsleep(&dummy, 0, "scsuspend", 100);
122	} while (retry--);
123
124	return (0);
125}
126
127static int
128scresume(device_t dev)
129{
130	sc_softc_t	*sc;
131
132	if (sc_no_suspend_vtswitch)
133		return (0);
134
135	sc = &main_softc;
136	sc_switch_scr(sc, sc_cur_scr);
137
138	return (0);
139}
140
141int
142sc_max_unit(void)
143{
144	return devclass_get_maxunit(sc_devclass);
145}
146
147sc_softc_t
148*sc_get_softc(int unit, int flags)
149{
150	sc_softc_t *sc;
151
152	if (unit < 0)
153		return NULL;
154	if (flags & SC_KERNEL_CONSOLE) {
155		/* FIXME: clear if it is wired to another unit! */
156		sc = &main_softc;
157	} else {
158	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
159		if (sc == NULL)
160			return NULL;
161	}
162	sc->unit = unit;
163	if (!(sc->flags & SC_INIT_DONE)) {
164		sc->keyboard = -1;
165		sc->adapter = -1;
166		sc->cursor_char = SC_CURSOR_CHAR;
167		sc->mouse_char = SC_MOUSE_CHAR;
168	}
169	return sc;
170}
171
172sc_softc_t
173*sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
174{
175	sc_softc_t *sc;
176	int units;
177	int i;
178
179	sc = &main_softc;
180	if (((adp == NULL) || (adp == sc->adp))
181	    && ((kbd == NULL) || (kbd == sc->kbd)))
182		return sc;
183	units = devclass_get_maxunit(sc_devclass);
184	for (i = 0; i < units; ++i) {
185	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
186		if (sc == NULL)
187			continue;
188		if (((adp == NULL) || (adp == sc->adp))
189		    && ((kbd == NULL) || (kbd == sc->kbd)))
190			return sc;
191	}
192	return NULL;
193}
194
195int
196sc_get_cons_priority(int *unit, int *flags)
197{
198	const char *at;
199	int u, f;
200
201	*unit = -1;
202	for (u = 0; u < 16; u++) {
203		if (resource_disabled(SC_DRIVER_NAME, u))
204			continue;
205		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
206			continue;
207		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
208			f = 0;
209		if (f & SC_KERNEL_CONSOLE) {
210			/* the user designates this unit to be the console */
211			*unit = u;
212			*flags = f;
213			break;
214		}
215		if (*unit < 0) {
216			/* ...otherwise remember the first found unit */
217			*unit = u;
218			*flags = f;
219		}
220	}
221	if (*unit < 0)
222		return CN_DEAD;
223#if 0
224	return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
225#endif
226	return CN_INTERNAL;
227}
228
229void
230sc_get_bios_values(bios_values_t *values)
231{
232#ifdef __i386__
233	u_int8_t shift;
234
235	values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
236	values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
237	shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
238	values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
239			       | ((shift & BIOS_NLKED) ? NLKED : 0)
240			       | ((shift & BIOS_SLKED) ? SLKED : 0)
241			       | ((shift & BIOS_ALKED) ? ALKED : 0);
242	values->bell_pitch = BELL_PITCH;
243#else /* !__i386__ */
244	values->cursor_start = 0;
245	values->cursor_end = 32;
246	values->shift_state = 0;
247	values->bell_pitch = BELL_PITCH;
248#endif /* __i386__ */
249}
250
251int
252sc_tone(int herz)
253{
254#ifdef __i386__
255	int pitch;
256
257	if (herz) {
258		/* set command for counter 2, 2 byte write */
259		if (acquire_timer2(TIMER_16BIT | TIMER_SQWAVE))
260			return EBUSY;
261		/* set pitch */
262		pitch = timer_freq/herz;
263		outb(TIMER_CNTR2, pitch);
264		outb(TIMER_CNTR2, pitch >> 8);
265		/* enable counter 2 output to speaker */
266		outb(IO_PPI, inb(IO_PPI) | 3);
267	} else {
268		/* disable counter 2 output to speaker */
269		outb(IO_PPI, inb(IO_PPI) & 0xFC);
270		release_timer2();
271	}
272#endif /* __i386__ */
273
274	return 0;
275}
276
277static device_method_t sc_methods[] = {
278	DEVMETHOD(device_identify,	scidentify),
279	DEVMETHOD(device_probe,         scprobe),
280	DEVMETHOD(device_attach,        scattach),
281	DEVMETHOD(device_suspend,       scsuspend),
282	DEVMETHOD(device_resume,        scresume),
283	{ 0, 0 }
284};
285
286static driver_t sc_driver = {
287	SC_DRIVER_NAME,
288	sc_methods,
289	sizeof(sc_softc_t),
290};
291
292DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
293