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