syscons_cbus.c revision 177650
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 177650 2008-03-26 22:02:51Z phk $
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#include <sys/sysctl.h>
39
40#include <machine/clock.h>
41
42#include <pc98/pc98/pc98_machdep.h>
43
44#include <dev/syscons/syscons.h>
45
46#include <isa/isavar.h>
47
48static devclass_t	sc_devclass;
49
50static sc_softc_t main_softc;
51#ifdef SC_NO_SUSPEND_VTYSWITCH
52static int sc_no_suspend_vtswitch = 1;
53#else
54static int sc_no_suspend_vtswitch = 0;
55#endif
56static int sc_cur_scr;
57
58TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", (int *)&sc_no_suspend_vtswitch);
59SYSCTL_DECL(_hw_syscons);
60SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
61	&sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
62
63static void
64scidentify (driver_t *driver, device_t parent)
65{
66	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
67}
68
69static int
70scprobe(device_t dev)
71{
72	/* No pnp support */
73	if (isa_get_vendorid(dev))
74		return (ENXIO);
75
76	device_set_desc(dev, "System console");
77	return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
78}
79
80static int
81scattach(device_t dev)
82{
83	return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
84}
85
86static int
87scsuspend(device_t dev)
88{
89	int		retry = 10;
90	sc_softc_t	*sc;
91
92	sc = &main_softc;
93
94	if (sc->cur_scp == NULL)
95		return (0);
96
97	sc_cur_scr = sc->cur_scp->index;
98
99	if (sc_no_suspend_vtswitch)
100		return (0);
101
102	do {
103		sc_switch_scr(sc, 0);
104		if (!sc->switch_in_progress) {
105			break;
106		}
107		pause("scsuspend", hz);
108	} while (retry--);
109
110	return (0);
111}
112
113static int
114scresume(device_t dev)
115{
116	sc_softc_t	*sc;
117
118	if (sc_no_suspend_vtswitch)
119		return (0);
120
121	sc = &main_softc;
122	sc_switch_scr(sc, sc_cur_scr);
123
124	return (0);
125}
126
127int
128sc_max_unit(void)
129{
130	return devclass_get_maxunit(sc_devclass);
131}
132
133sc_softc_t
134*sc_get_softc(int unit, int flags)
135{
136	sc_softc_t *sc;
137
138	if (unit < 0)
139		return NULL;
140	if (flags & SC_KERNEL_CONSOLE) {
141		/* FIXME: clear if it is wired to another unit! */
142		sc = &main_softc;
143	} else {
144	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
145		if (sc == NULL)
146			return NULL;
147	}
148	sc->unit = unit;
149	if (!(sc->flags & SC_INIT_DONE)) {
150		sc->keyboard = -1;
151		sc->adapter = -1;
152		sc->mouse_char = SC_MOUSE_CHAR;
153	}
154	return sc;
155}
156
157sc_softc_t
158*sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
159{
160	sc_softc_t *sc;
161	int units;
162	int i;
163
164	sc = &main_softc;
165	if (((adp == NULL) || (adp == sc->adp))
166	    && ((kbd == NULL) || (kbd == sc->kbd)))
167		return sc;
168	units = devclass_get_maxunit(sc_devclass);
169	for (i = 0; i < units; ++i) {
170	        sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
171		if (sc == NULL)
172			continue;
173		if (((adp == NULL) || (adp == sc->adp))
174		    && ((kbd == NULL) || (kbd == sc->kbd)))
175			return sc;
176	}
177	return NULL;
178}
179
180int
181sc_get_cons_priority(int *unit, int *flags)
182{
183	const char *at;
184	int u, f;
185
186	*unit = -1;
187	for (u = 0; u < 16; u++) {
188		if (resource_disabled(SC_DRIVER_NAME, u))
189			continue;
190		if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
191			continue;
192		if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
193			f = 0;
194		if (f & SC_KERNEL_CONSOLE) {
195			/* the user designates this unit to be the console */
196			*unit = u;
197			*flags = f;
198			break;
199		}
200		if (*unit < 0) {
201			/* ...otherwise remember the first found unit */
202			*unit = u;
203			*flags = f;
204		}
205	}
206	if (*unit < 0)
207		return CN_DEAD;
208	return CN_INTERNAL;
209}
210
211void
212sc_get_bios_values(bios_values_t *values)
213{
214	values->cursor_start = 15;
215	values->cursor_end = 16;
216	values->shift_state = 0;
217	if (pc98_machine_type & M_8M)
218		values->bell_pitch = BELL_PITCH_8M;
219	else
220		values->bell_pitch = BELL_PITCH_5M;
221}
222
223int
224sc_tone(int herz)
225{
226
227	if (herz) {
228		if (timer_spkr_acquire())
229			return EBUSY;
230		timer_spkr_setfreq(herz);
231	} else {
232		timer_spkr_release();
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