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