kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / bus / 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  * $FreeBSD: src/sys/isa/syscons_isa.c,v 1.11.2.2 2001/08/01 10:42:28 yokota Exp $
27  * $DragonFly: src/sys/bus/isa/syscons_isa.c,v 1.3 2003/08/07 21:16:46 dillon Exp $
28  */
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
39 #include <machine/console.h>
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/misc/syscons/syscons.h>
60
61 #include "isareg.h"
62 #include "isavar.h"
63
64 static devclass_t       sc_devclass;
65
66 static int      scprobe(device_t dev);
67 static int      scattach(device_t dev);
68
69 static device_method_t sc_methods[] = {
70         DEVMETHOD(device_probe,         scprobe),
71         DEVMETHOD(device_attach,        scattach),
72         { 0, 0 }
73 };
74
75 static driver_t sc_driver = {
76         SC_DRIVER_NAME,
77         sc_methods,
78         sizeof(sc_softc_t),
79 };
80
81 static sc_softc_t main_softc;
82
83 static int
84 scprobe(device_t dev)
85 {
86         /* No pnp support */
87         if (isa_get_vendorid(dev))
88                 return (ENXIO);
89
90         device_set_desc(dev, "System console");
91         return sc_probe_unit(device_get_unit(dev), device_get_flags(dev));
92 }
93
94 static int
95 scattach(device_t dev)
96 {
97         return sc_attach_unit(device_get_unit(dev), device_get_flags(dev));
98 }
99
100 int
101 sc_max_unit(void)
102 {
103         return devclass_get_maxunit(sc_devclass);
104 }
105
106 sc_softc_t
107 *sc_get_softc(int unit, int flags)
108 {
109         sc_softc_t *sc;
110
111         if (unit < 0)
112                 return NULL;
113         if (flags & SC_KERNEL_CONSOLE) {
114                 /* FIXME: clear if it is wired to another unit! */
115                 sc = &main_softc;
116         } else {
117                 sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, unit));
118                 if (sc == NULL)
119                         return NULL;
120         }
121         sc->unit = unit;
122         if (!(sc->flags & SC_INIT_DONE)) {
123                 sc->keyboard = -1;
124                 sc->adapter = -1;
125                 sc->cursor_char = SC_CURSOR_CHAR;
126                 sc->mouse_char = SC_MOUSE_CHAR;
127         }
128         return sc;
129 }
130
131 sc_softc_t
132 *sc_find_softc(struct video_adapter *adp, struct keyboard *kbd)
133 {
134         sc_softc_t *sc;
135         int units;
136         int i;
137
138         sc = &main_softc;
139         if (((adp == NULL) || (adp == sc->adp))
140             && ((kbd == NULL) || (kbd == sc->kbd)))
141                 return sc;
142         units = devclass_get_maxunit(sc_devclass);
143         for (i = 0; i < units; ++i) {
144                 sc = (sc_softc_t *)device_get_softc(devclass_get_device(sc_devclass, i));
145                 if (sc == NULL)
146                         continue;
147                 if (((adp == NULL) || (adp == sc->adp))
148                     && ((kbd == NULL) || (kbd == sc->kbd)))
149                         return sc;
150         }
151         return NULL;
152 }
153
154 int
155 sc_get_cons_priority(int *unit, int *flags)
156 {
157         int disabled;
158         int u, f;
159         int i;
160
161         *unit = -1;
162         for (i = -1; (i = resource_locate(i, SC_DRIVER_NAME)) >= 0;) {
163                 u = resource_query_unit(i);
164                 if ((resource_int_value(SC_DRIVER_NAME, u, "disabled",
165                                         &disabled) == 0) && disabled)
166                         continue;
167                 if (resource_int_value(SC_DRIVER_NAME, u, "flags", &f) != 0)
168                         f = 0;
169                 if (f & SC_KERNEL_CONSOLE) {
170                         /* the user designates this unit to be the console */
171                         *unit = u;
172                         *flags = f;
173                         break;
174                 }
175                 if (*unit < 0) {
176                         /* ...otherwise remember the first found unit */
177                         *unit = u;
178                         *flags = f;
179                 }
180         }
181         if ((i < 0) && (*unit < 0))
182                 return CN_DEAD;
183 #if 0
184         return ((*flags & SC_KERNEL_CONSOLE) ? CN_INTERNAL : CN_NORMAL);
185 #endif
186         return CN_INTERNAL;
187 }
188
189 void
190 sc_get_bios_values(bios_values_t *values)
191 {
192 #ifdef __i386__
193         u_int8_t shift;
194
195         values->cursor_start = *(u_int8_t *)BIOS_PADDRTOVADDR(0x461);
196         values->cursor_end = *(u_int8_t *)BIOS_PADDRTOVADDR(0x460);
197         shift = *(u_int8_t *)BIOS_PADDRTOVADDR(0x417);
198         values->shift_state = ((shift & BIOS_CLKED) ? CLKED : 0)
199                                | ((shift & BIOS_NLKED) ? NLKED : 0)
200                                | ((shift & BIOS_SLKED) ? SLKED : 0)
201                                | ((shift & BIOS_ALKED) ? ALKED : 0);
202         values->bell_pitch = BELL_PITCH;
203 #else /* !__i386__ */
204         values->cursor_start = 0;
205         values->cursor_end = 32;
206         values->shift_state = 0;
207         values->bell_pitch = BELL_PITCH;
208 #endif /* __i386__ */
209 }
210
211 int
212 sc_tone(int herz)
213 {
214 #ifdef __i386__
215         int pitch;
216
217         if (herz) {
218                 /* set command for counter 2, 2 byte write */
219                 if (acquire_timer2(TIMER_16BIT | TIMER_SQWAVE))
220                         return EBUSY;
221                 /* set pitch */
222                 pitch = timer_freq/herz;
223                 outb(TIMER_CNTR2, pitch);
224                 outb(TIMER_CNTR2, pitch >> 8);
225                 /* enable counter 2 output to speaker */
226                 outb(IO_PPI, inb(IO_PPI) | 3);
227         } else {
228                 /* disable counter 2 output to speaker */
229                 outb(IO_PPI, inb(IO_PPI) & 0xFC);
230                 release_timer2();
231         }
232 #endif /* __i386__ */
233
234         return 0;
235 }
236
237 DRIVER_MODULE(sc, isa, sc_driver, sc_devclass, 0, 0);