]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/isa/syscons_isa.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / isa / syscons_isa.c
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 #if defined(__i386__) || defined(__amd64__)
43
44 #include <machine/clock.h>
45 #include <machine/md_var.h>
46 #include <machine/ppireg.h>
47 #include <machine/timerreg.h>
48 #include <machine/pc/bios.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_param.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
60
61 #include <dev/syscons/syscons.h>
62
63 #include <isa/isavar.h>
64
65 #include "opt_xbox.h"
66
67 #ifdef XBOX
68 #include <machine/xbox.h>
69 #endif
70
71 static devclass_t       sc_devclass;
72
73 static sc_softc_t main_softc;
74 #ifdef SC_NO_SUSPEND_VTYSWITCH
75 static int sc_no_suspend_vtswitch = 1;
76 #else
77 static int sc_no_suspend_vtswitch = 0;
78 #endif
79 static int sc_cur_scr;
80
81 TUNABLE_INT("hw.syscons.sc_no_suspend_vtswitch", (int *)&sc_no_suspend_vtswitch);
82 SYSCTL_DECL(_hw_syscons);
83 SYSCTL_INT(_hw_syscons, OID_AUTO, sc_no_suspend_vtswitch, CTLFLAG_RW,
84         &sc_no_suspend_vtswitch, 0, "Disable VT switch before suspend.");
85
86 static void
87 scidentify (driver_t *driver, device_t parent)
88 {
89         BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "sc", 0);
90 }
91
92 static int
93 scprobe(device_t dev)
94 {
95         /* No pnp support */
96         if (isa_get_vendorid(dev))
97                 return (ENXIO);
98
99         device_set_desc(dev, "System console");
100         return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
101 }
102
103 static int
104 scattach(device_t dev)
105 {
106
107         return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) |
108             SC_AUTODETECT_KBD));
109 }
110
111 static int
112 scsuspend(device_t dev)
113 {
114         int             retry = 10;
115         sc_softc_t      *sc;
116
117         sc = &main_softc;
118
119         if (sc->cur_scp == NULL)
120                 return (0);
121
122         sc_cur_scr = sc->cur_scp->index;
123
124         if (sc_no_suspend_vtswitch)
125                 return (0);
126
127         do {
128                 sc_switch_scr(sc, 0);
129                 if (!sc->switch_in_progress) {
130                         break;
131                 }
132                 pause("scsuspend", hz);
133         } while (retry--);
134
135         return (0);
136 }
137
138 static int
139 scresume(device_t dev)
140 {
141         sc_softc_t      *sc;
142
143         if (sc_no_suspend_vtswitch)
144                 return (0);
145
146         sc = &main_softc;
147         sc_switch_scr(sc, sc_cur_scr);
148
149         return (0);
150 }
151
152 int
153 sc_max_unit(void)
154 {
155         return devclass_get_maxunit(sc_devclass);
156 }
157
158 sc_softc_t
159 *sc_get_softc(int unit, int flags)
160 {
161         sc_softc_t *sc;
162
163         if (unit < 0)
164                 return NULL;
165         if (flags & SC_KERNEL_CONSOLE) {
166                 /* FIXME: clear if it is wired to another unit! */
167                 sc = &main_softc;
168         } else {
169                 sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
170                 if (sc == NULL)
171                         return NULL;
172         }
173         sc->unit = unit;
174         if (!(sc->flags & SC_INIT_DONE)) {
175                 sc->keyboard = -1;
176                 sc->adapter = -1;
177                 sc->cursor_char = SC_CURSOR_CHAR;
178                 sc->mouse_char = SC_MOUSE_CHAR;
179         }
180         return sc;
181 }
182
183 sc_softc_t
184 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
185 {
186         sc_softc_t *sc;
187         int units;
188         int i;
189
190         sc = &main_softc;
191         if (((adp == NULL) || (adp == sc->adp))
192             && ((kbd == NULL) || (kbd == sc->kbd)))
193                 return sc;
194         units = devclass_get_maxunit(sc_devclass);
195         for (i = 0; i < units; ++i) {
196                 sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
197                 if (sc == NULL)
198                         continue;
199                 if (((adp == NULL) || (adp == sc->adp))
200                     && ((kbd == NULL) || (kbd == sc->kbd)))
201                         return sc;
202         }
203         return NULL;
204 }
205
206 int
207 sc_get_cons_priority(int *unit, int *flags)
208 {
209         const char *at;
210         int u, f;
211
212 #ifdef XBOX
213         /*
214          * The XBox Loader does not support hints, which makes our initial
215          * console probe fail. Therefore, if an XBox is found, we hardcode the
216          * existence of the console, as it is always there anyway.
217          */
218         if (arch_i386_is_xbox) {
219                 *unit = 0;
220                 *flags = SC_KERNEL_CONSOLE;
221                 return CN_INTERNAL;
222         }
223 #endif
224
225         *unit = -1;
226         for (u = 0; u < 16; u++) {
227                 if (resource_disabled(SC_DRIVER_NAME, u))
228                         continue;
229                 if (resource_string_value(SC_DRIVER_NAME, u, "at", &at) != 0)
230                         continue;
231                 if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
232                         f = 0;
233                 if (f & SC_KERNEL_CONSOLE) {
234                         /* the user designates this unit to be the console */
235                         *unit = u;
236                         *flags = f;
237                         break;
238                 }
239                 if (*unit < 0) {
240                         /* ...otherwise remember the first found unit */
241                         *unit = u;
242                         *flags = f;
243                 }
244         }
245         if (*unit < 0) {
246                 *unit = 0;
247                 *flags = 0;
248         }
249 #if 0
250         return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
251 #endif
252         return CN_INTERNAL;
253 }
254
255 void
256 sc_get_bios_values(bios_values_t *values)
257 {
258 #if defined(__i386__) || defined(__amd64__)
259         u_int8_t shift;
260
261         values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
262         values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
263         shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
264         values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
265                                | ((shift & BIOS_NLKED) ? NLKED : 0)
266                                | ((shift & BIOS_SLKED) ? SLKED : 0)
267                                | ((shift & BIOS_ALKED) ? ALKED : 0);
268 #else
269         values->cursor_start = 0;
270         values->cursor_end = 32;
271         values->shift_state = 0;
272 #endif
273         values->bell_pitch = BELL_PITCH;
274 }
275
276 int
277 sc_tone(int herz)
278 {
279 #if defined(__i386__) || defined(__amd64__)
280         if (herz) {
281                 /* set command for counter 2, 2 byte write */
282                 if (timer_spkr_acquire())
283                         return EBUSY;
284                 /* set pitch */
285                 spkr_set_pitch(timer_freq / herz);
286                 /* enable counter 2 output to speaker */
287                 ppi_spkr_on();
288         } else {
289                 /* disable counter 2 output to speaker */
290                 ppi_spkr_off();
291                 timer_spkr_release();
292         }
293 #endif
294
295         return 0;
296 }
297
298 static device_method_t sc_methods[] = {
299         DEVMETHOD(device_identify,      scidentify),
300         DEVMETHOD(device_probe,         scprobe),
301         DEVMETHOD(device_attach,        scattach),
302         DEVMETHOD(device_suspend,       scsuspend),
303         DEVMETHOD(device_resume,        scresume),
304         { 0, 0 }
305 };
306
307 static driver_t sc_driver = {
308         SC_DRIVER_NAME,
309         sc_methods,
310         sizeof(sc_softc_t),
311 };
312
313 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);