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