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