syscons_cbus.c revision 105712
1/*-
2 * Copyright (c) 1999 FreeBSD(98) Porting Team.
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: head/sys/pc98/cbus/syscons_cbus.c 105712 2002-10-22 15:22:49Z nyan $
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/consio.h>
38
39#include <machine/clock.h>
40
41#include <pc98/pc98/pc98.h>
42#include <pc98/pc98/pc98_machdep.h>
43
44#include <dev/syscons/syscons.h>
45
46#include <i386/isa/timerreg.h>
47
48#include <isa/isavar.h>
49
50static devclass_t	sc_devclass;
51
52static sc_softc_t main_softc;
53
54static void
55scidentify (driver_t *driver, device_t parent)
56{
57	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
58}
59
60static int
61scprobe(device_t dev)
62{
63	/* No pnp support */
64	if (isa_get_vendorid(dev))
65		return (ENXIO);
66
67	device_set_desc(dev, "System console");
68	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
69}
70
71static int
72scattach(device_t dev)
73{
74	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
75}
76
77#ifndef SC_NO_SUSPEND_VTYSWITCH
78static int	sc_cur_scr;
79#endif
80
81static int
82scsuspend(device_t dev)
83{
84#ifndef SC_NO_SUSPEND_VTYSWITCH
85	int		retry = 10;
86	static int	dummy;
87	sc_softc_t	*sc;
88
89	sc = &main_softc;
90	sc_cur_scr = sc->cur_scp->index;
91	do {
92		sc_switch_scr(sc, 0);
93		if (!sc->switch_in_progress) {
94			break;
95		}
96		tsleep(&dummy, 0, "scsuspend", 100);
97	} while (retry--);
98
99#endif
100	return (0);
101}
102
103static int
104scresume(device_t dev)
105{
106#ifndef SC_NO_SUSPEND_VTYSWITCH
107	sc_softc_t	*sc;
108
109	sc = &main_softc;
110	sc_switch_scr(sc, sc_cur_scr);
111
112#endif
113	return (0);
114}
115
116int
117sc_max_unit(void)
118{
119	return devclass_get_maxunit(sc_devclass);
120}
121
122sc_softc_t
123*sc_get_softc(int unit, int flags)
124{
125	sc_softc_t *sc;
126
127	if (unit < 0)
128		return NULL;
129	if (flags & SC_KERNEL_CONSOLE) {
130		/* FIXME: clear if it is wired to another unit! */
131		sc = &main_softc;
132	} else {
133	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
134		if (sc == NULL)
135			return NULL;
136	}
137	sc->unit = unit;
138	if (!(sc->flags & SC_INIT_DONE)) {
139		sc->keyboard = -1;
140		sc->adapter = -1;
141		sc->mouse_char = SC_MOUSE_CHAR;
142	}
143	return sc;
144}
145
146sc_softc_t
147*sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
148{
149	sc_softc_t *sc;
150	int units;
151	int i;
152
153	sc = &main_softc;
154	if (((adp == NULL) || (adp == sc->adp))
155	    && ((kbd == NULL) || (kbd == sc->kbd)))
156		return sc;
157	units = devclass_get_maxunit(sc_devclass);
158	for (i = 0; i < units; ++i) {
159	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
160		if (sc == NULL)
161			continue;
162		if (((adp == NULL) || (adp == sc->adp))
163		    && ((kbd == NULL) || (kbd == sc->kbd)))
164			return sc;
165	}
166	return NULL;
167}
168
169int
170sc_get_cons_priority(int *unit, int *flags)
171{
172	int disabled;
173	const char *at;
174	int u, f;
175
176	*unit = -1;
177	for (u = 0; u < 16; u++) {
178		if ((resource_int_value(SC_DRIVER_NAME, u, "disabled",
179					&disabled) == 0) && disabled)
180			continue;
181		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
182			continue;
183		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
184			f = 0;
185		if (f & SC_KERNEL_CONSOLE) {
186			/* the user designates this unit to be the console */
187			*unit = u;
188			*flags = f;
189			break;
190		}
191		if (*unit < 0) {
192			/* ...otherwise remember the first found unit */
193			*unit = u;
194			*flags = f;
195		}
196	}
197	if (*unit < 0)
198		return CN_DEAD;
199	return CN_INTERNAL;
200}
201
202void
203sc_get_bios_values(bios_values_t *values)
204{
205	values->cursor_start = 15;
206	values->cursor_end = 16;
207	values->shift_state = 0;
208	if (pc98_machine_type & M_8M)
209		values->bell_pitch = BELL_PITCH_8M;
210	else
211		values->bell_pitch = BELL_PITCH_5M;
212}
213
214int
215sc_tone(int herz)
216{
217	int pitch;
218
219	if (herz) {
220		/* enable counter 1 */
221		outb(0x35, inb(0x35) & 0xf7);
222		/* set command for counter 1, 2 byte write */
223		if (acquire_timer1(TIMER_16BIT | TIMER_SQWAVE))
224			return EBUSY;
225		/* set pitch */
226		pitch = timer_freq/herz;
227		outb(TIMER_CNTR1, pitch);
228		outb(TIMER_CNTR1, pitch >> 8);
229	} else {
230		/* disable counter 1 */
231		outb(0x35, inb(0x35) | 0x08);
232		release_timer1();
233	}
234	return 0;
235}
236
237static device_method_t sc_methods[] = {
238	DEVMETHOD(device_identify,	scidentify),
239	DEVMETHOD(device_probe,         scprobe),
240	DEVMETHOD(device_attach,        scattach),
241	DEVMETHOD(device_suspend,       scsuspend),
242	DEVMETHOD(device_resume,        scresume),
243	{ 0, 0 }
244};
245
246static driver_t sc_driver = {
247	SC_DRIVER_NAME,
248	sc_methods,
249	sizeof(sc_softc_t),
250};
251
252DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);
253