pckbd.c revision 1.20.6.2 1 1.20.6.2 mjf /* $NetBSD: pckbd.c,v 1.20.6.2 2008/04/03 12:42:54 mjf Exp $ */
2 1.20.6.2 mjf
3 1.20.6.2 mjf /*-
4 1.20.6.2 mjf * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.20.6.2 mjf * All rights reserved.
6 1.20.6.2 mjf *
7 1.20.6.2 mjf * This code is derived from software contributed to The NetBSD Foundation
8 1.20.6.2 mjf * by Charles M. Hannum.
9 1.20.6.2 mjf *
10 1.20.6.2 mjf * Redistribution and use in source and binary forms, with or without
11 1.20.6.2 mjf * modification, are permitted provided that the following conditions
12 1.20.6.2 mjf * are met:
13 1.20.6.2 mjf * 1. Redistributions of source code must retain the above copyright
14 1.20.6.2 mjf * notice, this list of conditions and the following disclaimer.
15 1.20.6.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
16 1.20.6.2 mjf * notice, this list of conditions and the following disclaimer in the
17 1.20.6.2 mjf * documentation and/or other materials provided with the distribution.
18 1.20.6.2 mjf * 3. All advertising materials mentioning features or use of this software
19 1.20.6.2 mjf * must display the following acknowledgement:
20 1.20.6.2 mjf * This product includes software developed by the NetBSD
21 1.20.6.2 mjf * Foundation, Inc. and its contributors.
22 1.20.6.2 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.20.6.2 mjf * contributors may be used to endorse or promote products derived
24 1.20.6.2 mjf * from this software without specific prior written permission.
25 1.20.6.2 mjf *
26 1.20.6.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.20.6.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.20.6.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.20.6.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.20.6.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.20.6.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.20.6.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.20.6.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.20.6.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.20.6.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.20.6.2 mjf * POSSIBILITY OF SUCH DAMAGE.
37 1.20.6.2 mjf */
38 1.20.6.2 mjf
39 1.20.6.2 mjf /*-
40 1.20.6.2 mjf * Copyright (c) 1990 The Regents of the University of California.
41 1.20.6.2 mjf * All rights reserved.
42 1.20.6.2 mjf *
43 1.20.6.2 mjf * This code is derived from software contributed to Berkeley by
44 1.20.6.2 mjf * William Jolitz and Don Ahn.
45 1.20.6.2 mjf *
46 1.20.6.2 mjf * Redistribution and use in source and binary forms, with or without
47 1.20.6.2 mjf * modification, are permitted provided that the following conditions
48 1.20.6.2 mjf * are met:
49 1.20.6.2 mjf * 1. Redistributions of source code must retain the above copyright
50 1.20.6.2 mjf * notice, this list of conditions and the following disclaimer.
51 1.20.6.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
52 1.20.6.2 mjf * notice, this list of conditions and the following disclaimer in the
53 1.20.6.2 mjf * documentation and/or other materials provided with the distribution.
54 1.20.6.2 mjf * 3. Neither the name of the University nor the names of its contributors
55 1.20.6.2 mjf * may be used to endorse or promote products derived from this software
56 1.20.6.2 mjf * without specific prior written permission.
57 1.20.6.2 mjf *
58 1.20.6.2 mjf * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.20.6.2 mjf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.20.6.2 mjf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.20.6.2 mjf * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.20.6.2 mjf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.20.6.2 mjf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.20.6.2 mjf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.20.6.2 mjf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.20.6.2 mjf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.20.6.2 mjf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.20.6.2 mjf * SUCH DAMAGE.
69 1.20.6.2 mjf *
70 1.20.6.2 mjf * @(#)pccons.c 5.11 (Berkeley) 5/21/91
71 1.20.6.2 mjf */
72 1.20.6.2 mjf
73 1.20.6.2 mjf /*
74 1.20.6.2 mjf * code to work keyboard for PC-style console
75 1.20.6.2 mjf */
76 1.20.6.2 mjf
77 1.20.6.2 mjf #include <sys/cdefs.h>
78 1.20.6.2 mjf __KERNEL_RCSID(0, "$NetBSD: pckbd.c,v 1.20.6.2 2008/04/03 12:42:54 mjf Exp $");
79 1.20.6.2 mjf
80 1.20.6.2 mjf #include <sys/param.h>
81 1.20.6.2 mjf #include <sys/systm.h>
82 1.20.6.2 mjf #include <sys/device.h>
83 1.20.6.2 mjf #include <sys/malloc.h>
84 1.20.6.2 mjf #include <sys/ioctl.h>
85 1.20.6.2 mjf
86 1.20.6.2 mjf #include <sys/bus.h>
87 1.20.6.2 mjf
88 1.20.6.2 mjf #include <dev/pckbport/pckbportvar.h>
89 1.20.6.2 mjf
90 1.20.6.2 mjf #include <dev/wscons/wsconsio.h>
91 1.20.6.2 mjf #include <dev/wscons/wskbdvar.h>
92 1.20.6.2 mjf #include <dev/wscons/wsksymdef.h>
93 1.20.6.2 mjf #include <dev/wscons/wsksymvar.h>
94 1.20.6.2 mjf
95 1.20.6.2 mjf #include <dev/pckbport/pckbdreg.h>
96 1.20.6.2 mjf #include <dev/pckbport/pckbdvar.h>
97 1.20.6.2 mjf #include <dev/pckbport/wskbdmap_mfii.h>
98 1.20.6.2 mjf
99 1.20.6.2 mjf #include "locators.h"
100 1.20.6.2 mjf
101 1.20.6.2 mjf #include "opt_pckbd_layout.h"
102 1.20.6.2 mjf #include "opt_pckbd_cnattach_may_fail.h"
103 1.20.6.2 mjf #include "opt_wsdisplay_compat.h"
104 1.20.6.2 mjf
105 1.20.6.2 mjf struct pckbd_internal {
106 1.20.6.2 mjf int t_isconsole;
107 1.20.6.2 mjf pckbport_tag_t t_kbctag;
108 1.20.6.2 mjf pckbport_slot_t t_kbcslot;
109 1.20.6.2 mjf
110 1.20.6.2 mjf int t_lastchar;
111 1.20.6.2 mjf int t_extended0;
112 1.20.6.2 mjf int t_extended1;
113 1.20.6.2 mjf
114 1.20.6.2 mjf struct pckbd_softc *t_sc; /* back pointer */
115 1.20.6.2 mjf };
116 1.20.6.2 mjf
117 1.20.6.2 mjf struct pckbd_softc {
118 1.20.6.2 mjf device_t sc_dev;
119 1.20.6.2 mjf
120 1.20.6.2 mjf struct pckbd_internal *id;
121 1.20.6.2 mjf int sc_enabled;
122 1.20.6.2 mjf
123 1.20.6.2 mjf int sc_ledstate;
124 1.20.6.2 mjf
125 1.20.6.2 mjf device_t sc_wskbddev;
126 1.20.6.2 mjf #ifdef WSDISPLAY_COMPAT_RAWKBD
127 1.20.6.2 mjf int rawkbd;
128 1.20.6.2 mjf #endif
129 1.20.6.2 mjf };
130 1.20.6.2 mjf
131 1.20.6.2 mjf static int pckbd_is_console(pckbport_tag_t, pckbport_slot_t);
132 1.20.6.2 mjf
133 1.20.6.2 mjf int pckbdprobe(device_t, cfdata_t, void *);
134 1.20.6.2 mjf void pckbdattach(device_t, device_t, void *);
135 1.20.6.2 mjf
136 1.20.6.2 mjf CFATTACH_DECL_NEW(pckbd, sizeof(struct pckbd_softc),
137 1.20.6.2 mjf pckbdprobe, pckbdattach, NULL, NULL);
138 1.20.6.2 mjf
139 1.20.6.2 mjf int pckbd_enable(void *, int);
140 1.20.6.2 mjf void pckbd_set_leds(void *, int);
141 1.20.6.2 mjf int pckbd_ioctl(void *, u_long, void *, int, struct lwp *);
142 1.20.6.2 mjf
143 1.20.6.2 mjf const struct wskbd_accessops pckbd_accessops = {
144 1.20.6.2 mjf pckbd_enable,
145 1.20.6.2 mjf pckbd_set_leds,
146 1.20.6.2 mjf pckbd_ioctl,
147 1.20.6.2 mjf };
148 1.20.6.2 mjf
149 1.20.6.2 mjf void pckbd_cngetc(void *, u_int *, int *);
150 1.20.6.2 mjf void pckbd_cnpollc(void *, int);
151 1.20.6.2 mjf void pckbd_cnbell(void *, u_int, u_int, u_int);
152 1.20.6.2 mjf
153 1.20.6.2 mjf const struct wskbd_consops pckbd_consops = {
154 1.20.6.2 mjf pckbd_cngetc,
155 1.20.6.2 mjf pckbd_cnpollc,
156 1.20.6.2 mjf pckbd_cnbell,
157 1.20.6.2 mjf };
158 1.20.6.2 mjf
159 1.20.6.2 mjf const struct wskbd_mapdata pckbd_keymapdata = {
160 1.20.6.2 mjf pckbd_keydesctab,
161 1.20.6.2 mjf #ifdef PCKBD_LAYOUT
162 1.20.6.2 mjf PCKBD_LAYOUT,
163 1.20.6.2 mjf #else
164 1.20.6.2 mjf KB_US,
165 1.20.6.2 mjf #endif
166 1.20.6.2 mjf };
167 1.20.6.2 mjf
168 1.20.6.2 mjf /*
169 1.20.6.2 mjf * Hackish support for a bell on the PC Keyboard; when a suitable feeper
170 1.20.6.2 mjf * is found, it attaches itself into the pckbd driver here.
171 1.20.6.2 mjf */
172 1.20.6.2 mjf void (*pckbd_bell_fn)(void *, u_int, u_int, u_int, int);
173 1.20.6.2 mjf void *pckbd_bell_fn_arg;
174 1.20.6.2 mjf
175 1.20.6.2 mjf void pckbd_bell(u_int, u_int, u_int, int);
176 1.20.6.2 mjf
177 1.20.6.2 mjf int pckbd_set_xtscancode(pckbport_tag_t, pckbport_slot_t);
178 1.20.6.2 mjf int pckbd_init(struct pckbd_internal *, pckbport_tag_t, pckbport_slot_t,
179 1.20.6.2 mjf int);
180 1.20.6.2 mjf void pckbd_input(void *, int);
181 1.20.6.2 mjf
182 1.20.6.2 mjf static int pckbd_decode(struct pckbd_internal *, int, u_int *, int *);
183 1.20.6.2 mjf static int pckbd_led_encode(int);
184 1.20.6.2 mjf static int pckbd_led_decode(int);
185 1.20.6.2 mjf
186 1.20.6.2 mjf struct pckbd_internal pckbd_consdata;
187 1.20.6.2 mjf
188 1.20.6.2 mjf int
189 1.20.6.2 mjf pckbd_set_xtscancode(pckbport_tag_t kbctag, pckbport_slot_t kbcslot)
190 1.20.6.2 mjf {
191 1.20.6.2 mjf int res;
192 1.20.6.2 mjf u_char cmd[2];
193 1.20.6.2 mjf
194 1.20.6.2 mjf /*
195 1.20.6.2 mjf * Some keyboard/8042 combinations do not seem to work if the keyboard
196 1.20.6.2 mjf * is set to table 1; in fact, it would appear that some keyboards just
197 1.20.6.2 mjf * ignore the command altogether. So by default, we use the AT scan
198 1.20.6.2 mjf * codes and have the 8042 translate them. Unfortunately, this is
199 1.20.6.2 mjf * known to not work on some PS/2 machines. We try desperately to deal
200 1.20.6.2 mjf * with this by checking the (lack of a) translate bit in the 8042 and
201 1.20.6.2 mjf * attempting to set the keyboard to XT mode. If this all fails, well,
202 1.20.6.2 mjf * tough luck.
203 1.20.6.2 mjf *
204 1.20.6.2 mjf * XXX It would perhaps be a better choice to just use AT scan codes
205 1.20.6.2 mjf * and not bother with this.
206 1.20.6.2 mjf */
207 1.20.6.2 mjf if (pckbport_xt_translation(kbctag, kbcslot, 1)) {
208 1.20.6.2 mjf /* The 8042 is translating for us; use AT codes. */
209 1.20.6.2 mjf cmd[0] = KBC_SETTABLE;
210 1.20.6.2 mjf cmd[1] = 2;
211 1.20.6.2 mjf res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
212 1.20.6.2 mjf if (res) {
213 1.20.6.2 mjf u_char cmdb[1];
214 1.20.6.2 mjf #ifdef DEBUG
215 1.20.6.2 mjf printf("pckbd: error setting scanset 2\n");
216 1.20.6.2 mjf #endif
217 1.20.6.2 mjf /*
218 1.20.6.2 mjf * XXX at least one keyboard is reported to lock up
219 1.20.6.2 mjf * if a "set table" is attempted, thus the "reset".
220 1.20.6.2 mjf * XXX ignore errors, scanset 2 should be
221 1.20.6.2 mjf * default anyway.
222 1.20.6.2 mjf */
223 1.20.6.2 mjf cmdb[0] = KBC_RESET;
224 1.20.6.2 mjf (void)pckbport_poll_cmd(kbctag, kbcslot, cmdb, 1, 1, 0, 1);
225 1.20.6.2 mjf pckbport_flush(kbctag, kbcslot);
226 1.20.6.2 mjf res = 0;
227 1.20.6.2 mjf }
228 1.20.6.2 mjf } else {
229 1.20.6.2 mjf /* Stupid 8042; set keyboard to XT codes. */
230 1.20.6.2 mjf cmd[0] = KBC_SETTABLE;
231 1.20.6.2 mjf cmd[1] = 1;
232 1.20.6.2 mjf res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
233 1.20.6.2 mjf #ifdef DEBUG
234 1.20.6.2 mjf if (res)
235 1.20.6.2 mjf printf("pckbd: error setting scanset 1\n");
236 1.20.6.2 mjf #endif
237 1.20.6.2 mjf }
238 1.20.6.2 mjf return res;
239 1.20.6.2 mjf }
240 1.20.6.2 mjf
241 1.20.6.2 mjf static int
242 1.20.6.2 mjf pckbd_is_console(pckbport_tag_t tag, pckbport_slot_t slot)
243 1.20.6.2 mjf {
244 1.20.6.2 mjf
245 1.20.6.2 mjf return pckbd_consdata.t_isconsole &&
246 1.20.6.2 mjf tag == pckbd_consdata.t_kbctag && slot == pckbd_consdata.t_kbcslot;
247 1.20.6.2 mjf }
248 1.20.6.2 mjf
249 1.20.6.2 mjf static bool
250 1.20.6.2 mjf pckbd_suspend(device_t dv PMF_FN_ARGS)
251 1.20.6.2 mjf {
252 1.20.6.2 mjf struct pckbd_softc *sc = device_private(dv);
253 1.20.6.2 mjf u_char cmd[1];
254 1.20.6.2 mjf int res;
255 1.20.6.2 mjf
256 1.20.6.2 mjf /* XXX duped from pckbd_enable, but we want to disable
257 1.20.6.2 mjf * it even if it's the console kbd
258 1.20.6.2 mjf */
259 1.20.6.2 mjf cmd[0] = KBC_DISABLE;
260 1.20.6.2 mjf res = pckbport_enqueue_cmd(sc->id->t_kbctag,
261 1.20.6.2 mjf sc->id->t_kbcslot, cmd, 1, 0, 1, 0);
262 1.20.6.2 mjf if (res)
263 1.20.6.2 mjf return false;
264 1.20.6.2 mjf
265 1.20.6.2 mjf pckbport_slot_enable(sc->id->t_kbctag,
266 1.20.6.2 mjf sc->id->t_kbcslot, 0);
267 1.20.6.2 mjf
268 1.20.6.2 mjf sc->sc_enabled = 0;
269 1.20.6.2 mjf return true;
270 1.20.6.2 mjf }
271 1.20.6.2 mjf
272 1.20.6.2 mjf static bool
273 1.20.6.2 mjf pckbd_resume(device_t dv PMF_FN_ARGS)
274 1.20.6.2 mjf {
275 1.20.6.2 mjf struct pckbd_softc *sc = device_private(dv);
276 1.20.6.2 mjf u_char cmd[1], resp[1];
277 1.20.6.2 mjf int res;
278 1.20.6.2 mjf
279 1.20.6.2 mjf /* XXX jmcneill reset the keyboard */
280 1.20.6.2 mjf pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
281 1.20.6.2 mjf
282 1.20.6.2 mjf cmd[0] = KBC_RESET;
283 1.20.6.2 mjf res = pckbport_poll_cmd(sc->id->t_kbctag,
284 1.20.6.2 mjf sc->id->t_kbcslot, cmd, 1, 1, resp, 1);
285 1.20.6.2 mjf #ifdef DEBUG
286 1.20.6.2 mjf if (res)
287 1.20.6.2 mjf printf("pckbdprobe: reset error %d\n", res);
288 1.20.6.2 mjf #endif
289 1.20.6.2 mjf if (resp[0] != KBR_RSTDONE)
290 1.20.6.2 mjf printf("pckbdprobe: reset response 0x%x\n",
291 1.20.6.2 mjf resp[0]);
292 1.20.6.2 mjf
293 1.20.6.2 mjf pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
294 1.20.6.2 mjf
295 1.20.6.2 mjf pckbd_enable(sc, 1);
296 1.20.6.2 mjf
297 1.20.6.2 mjf return true;
298 1.20.6.2 mjf }
299 1.20.6.2 mjf
300 1.20.6.2 mjf /*
301 1.20.6.2 mjf * these are both bad jokes
302 1.20.6.2 mjf */
303 1.20.6.2 mjf int
304 1.20.6.2 mjf pckbdprobe(device_t parent, cfdata_t cf, void *aux)
305 1.20.6.2 mjf {
306 1.20.6.2 mjf struct pckbport_attach_args *pa = aux;
307 1.20.6.2 mjf int res;
308 1.20.6.2 mjf u_char cmd[1], resp[1];
309 1.20.6.2 mjf
310 1.20.6.2 mjf /*
311 1.20.6.2 mjf * XXX There are rumours that a keyboard can be connected
312 1.20.6.2 mjf * to the aux port as well. For me, this didn't work.
313 1.20.6.2 mjf * For further experiments, allow it if explicitly
314 1.20.6.2 mjf * wired in the config file.
315 1.20.6.2 mjf */
316 1.20.6.2 mjf if ((pa->pa_slot != PCKBPORT_KBD_SLOT) &&
317 1.20.6.2 mjf (cf->cf_loc[PCKBPORTCF_SLOT] == PCKBPORTCF_SLOT_DEFAULT))
318 1.20.6.2 mjf return 0;
319 1.20.6.2 mjf
320 1.20.6.2 mjf /* Flush any garbage. */
321 1.20.6.2 mjf pckbport_flush(pa->pa_tag, pa->pa_slot);
322 1.20.6.2 mjf
323 1.20.6.2 mjf /* Reset the keyboard. */
324 1.20.6.2 mjf cmd[0] = KBC_RESET;
325 1.20.6.2 mjf res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 1, resp, 1);
326 1.20.6.2 mjf if (res) {
327 1.20.6.2 mjf #ifdef DEBUG
328 1.20.6.2 mjf printf("pckbdprobe: reset error %d\n", res);
329 1.20.6.2 mjf #endif
330 1.20.6.2 mjf /*
331 1.20.6.2 mjf * There is probably no keyboard connected.
332 1.20.6.2 mjf * Let the probe succeed if the keyboard is used
333 1.20.6.2 mjf * as console input - it can be connected later.
334 1.20.6.2 mjf */
335 1.20.6.2 mjf return pckbd_is_console(pa->pa_tag, pa->pa_slot) ? 1 : 0;
336 1.20.6.2 mjf }
337 1.20.6.2 mjf if (resp[0] != KBR_RSTDONE) {
338 1.20.6.2 mjf printf("pckbdprobe: reset response 0x%x\n", resp[0]);
339 1.20.6.2 mjf return 0;
340 1.20.6.2 mjf }
341 1.20.6.2 mjf
342 1.20.6.2 mjf /*
343 1.20.6.2 mjf * Some keyboards seem to leave a second ack byte after the reset.
344 1.20.6.2 mjf * This is kind of stupid, but we account for them anyway by just
345 1.20.6.2 mjf * flushing the buffer.
346 1.20.6.2 mjf */
347 1.20.6.2 mjf pckbport_flush(pa->pa_tag, pa->pa_slot);
348 1.20.6.2 mjf
349 1.20.6.2 mjf if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot))
350 1.20.6.2 mjf return 0;
351 1.20.6.2 mjf
352 1.20.6.2 mjf return 2;
353 1.20.6.2 mjf }
354 1.20.6.2 mjf
355 1.20.6.2 mjf void
356 1.20.6.2 mjf pckbdattach(device_t parent, device_t self, void *aux)
357 1.20.6.2 mjf {
358 1.20.6.2 mjf struct pckbd_softc *sc = device_private(self);
359 1.20.6.2 mjf struct pckbport_attach_args *pa = aux;
360 1.20.6.2 mjf struct wskbddev_attach_args a;
361 1.20.6.2 mjf int isconsole;
362 1.20.6.2 mjf u_char cmd[1];
363 1.20.6.2 mjf
364 1.20.6.2 mjf aprint_naive("\n");
365 1.20.6.2 mjf aprint_normal("\n");
366 1.20.6.2 mjf
367 1.20.6.2 mjf sc->sc_dev = self;
368 1.20.6.2 mjf isconsole = pckbd_is_console(pa->pa_tag, pa->pa_slot);
369 1.20.6.2 mjf
370 1.20.6.2 mjf if (isconsole) {
371 1.20.6.2 mjf sc->id = &pckbd_consdata;
372 1.20.6.2 mjf
373 1.20.6.2 mjf /*
374 1.20.6.2 mjf * Some keyboards are not enabled after a reset,
375 1.20.6.2 mjf * so make sure it is enabled now.
376 1.20.6.2 mjf */
377 1.20.6.2 mjf cmd[0] = KBC_ENABLE;
378 1.20.6.2 mjf (void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
379 1.20.6.2 mjf cmd, 1, 0, 0, 0);
380 1.20.6.2 mjf sc->sc_enabled = 1;
381 1.20.6.2 mjf } else {
382 1.20.6.2 mjf sc->id = malloc(sizeof(struct pckbd_internal),
383 1.20.6.2 mjf M_DEVBUF, M_WAITOK);
384 1.20.6.2 mjf (void) pckbd_init(sc->id, pa->pa_tag, pa->pa_slot, 0);
385 1.20.6.2 mjf
386 1.20.6.2 mjf /* no interrupts until enabled */
387 1.20.6.2 mjf cmd[0] = KBC_DISABLE;
388 1.20.6.2 mjf (void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
389 1.20.6.2 mjf cmd, 1, 0, 0, 0);
390 1.20.6.2 mjf sc->sc_enabled = 0;
391 1.20.6.2 mjf }
392 1.20.6.2 mjf
393 1.20.6.2 mjf sc->id->t_sc = sc;
394 1.20.6.2 mjf
395 1.20.6.2 mjf pckbport_set_inputhandler(sc->id->t_kbctag, sc->id->t_kbcslot,
396 1.20.6.2 mjf pckbd_input, sc, device_xname(sc->sc_dev));
397 1.20.6.2 mjf
398 1.20.6.2 mjf a.console = isconsole;
399 1.20.6.2 mjf
400 1.20.6.2 mjf a.keymap = &pckbd_keymapdata;
401 1.20.6.2 mjf
402 1.20.6.2 mjf a.accessops = &pckbd_accessops;
403 1.20.6.2 mjf a.accesscookie = sc;
404 1.20.6.2 mjf
405 1.20.6.2 mjf if (!pmf_device_register(self, pckbd_suspend, pckbd_resume))
406 1.20.6.2 mjf aprint_error_dev(self, "couldn't establish power handler\n");
407 1.20.6.2 mjf
408 1.20.6.2 mjf /*
409 1.20.6.2 mjf * Attach the wskbd, saving a handle to it.
410 1.20.6.2 mjf * XXX XXX XXX
411 1.20.6.2 mjf */
412 1.20.6.2 mjf sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
413 1.20.6.2 mjf }
414 1.20.6.2 mjf
415 1.20.6.2 mjf int
416 1.20.6.2 mjf pckbd_enable(void *v, int on)
417 1.20.6.2 mjf {
418 1.20.6.2 mjf struct pckbd_softc *sc = v;
419 1.20.6.2 mjf int res;
420 1.20.6.2 mjf u_char cmd[1];
421 1.20.6.2 mjf
422 1.20.6.2 mjf if (on) {
423 1.20.6.2 mjf if (sc->sc_enabled) {
424 1.20.6.2 mjf #ifdef DIAGNOSTIC
425 1.20.6.2 mjf printf("pckbd_enable: bad enable\n");
426 1.20.6.2 mjf #endif
427 1.20.6.2 mjf return EBUSY;
428 1.20.6.2 mjf }
429 1.20.6.2 mjf
430 1.20.6.2 mjf pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 1);
431 1.20.6.2 mjf
432 1.20.6.2 mjf cmd[0] = KBC_ENABLE;
433 1.20.6.2 mjf res = pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
434 1.20.6.2 mjf cmd, 1, 0, NULL, 0);
435 1.20.6.2 mjf if (res) {
436 1.20.6.2 mjf printf("pckbd_enable: command error\n");
437 1.20.6.2 mjf return (res);
438 1.20.6.2 mjf }
439 1.20.6.2 mjf
440 1.20.6.2 mjf res = pckbd_set_xtscancode(sc->id->t_kbctag,
441 1.20.6.2 mjf sc->id->t_kbcslot);
442 1.20.6.2 mjf if (res)
443 1.20.6.2 mjf return res;
444 1.20.6.2 mjf
445 1.20.6.2 mjf sc->sc_enabled = 1;
446 1.20.6.2 mjf } else {
447 1.20.6.2 mjf if (sc->id->t_isconsole)
448 1.20.6.2 mjf return EBUSY;
449 1.20.6.2 mjf
450 1.20.6.2 mjf cmd[0] = KBC_DISABLE;
451 1.20.6.2 mjf res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
452 1.20.6.2 mjf cmd, 1, 0, 1, 0);
453 1.20.6.2 mjf if (res) {
454 1.20.6.2 mjf printf("pckbd_disable: command error\n");
455 1.20.6.2 mjf return res;
456 1.20.6.2 mjf }
457 1.20.6.2 mjf
458 1.20.6.2 mjf pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 0);
459 1.20.6.2 mjf
460 1.20.6.2 mjf sc->sc_enabled = 0;
461 1.20.6.2 mjf }
462 1.20.6.2 mjf
463 1.20.6.2 mjf return 0;
464 1.20.6.2 mjf }
465 1.20.6.2 mjf
466 1.20.6.2 mjf static int
467 1.20.6.2 mjf pckbd_decode(struct pckbd_internal *id, int datain, u_int *type, int *dataout)
468 1.20.6.2 mjf {
469 1.20.6.2 mjf int key;
470 1.20.6.2 mjf
471 1.20.6.2 mjf if (datain == KBR_EXTENDED0) {
472 1.20.6.2 mjf id->t_extended0 = 1;
473 1.20.6.2 mjf return 0;
474 1.20.6.2 mjf } else if (datain == KBR_EXTENDED1) {
475 1.20.6.2 mjf id->t_extended1 = 2;
476 1.20.6.2 mjf return 0;
477 1.20.6.2 mjf }
478 1.20.6.2 mjf
479 1.20.6.2 mjf /* map extended keys to (unused) codes 128-254 */
480 1.20.6.2 mjf key = (datain & 0x7f) | (id->t_extended0 ? 0x80 : 0);
481 1.20.6.2 mjf id->t_extended0 = 0;
482 1.20.6.2 mjf
483 1.20.6.2 mjf /*
484 1.20.6.2 mjf * process PAUSE (also break) key (EXT1 1D 45 EXT1 9D C5):
485 1.20.6.2 mjf * map to (unused) code 7F
486 1.20.6.2 mjf */
487 1.20.6.2 mjf if (id->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
488 1.20.6.2 mjf id->t_extended1 = 1;
489 1.20.6.2 mjf return 0;
490 1.20.6.2 mjf } else if (id->t_extended1 == 1 &&
491 1.20.6.2 mjf (datain == 0x45 || datain == 0xc5)) {
492 1.20.6.2 mjf id->t_extended1 = 0;
493 1.20.6.2 mjf key = 0x7f;
494 1.20.6.2 mjf } else if (id->t_extended1 > 0) {
495 1.20.6.2 mjf id->t_extended1 = 0;
496 1.20.6.2 mjf }
497 1.20.6.2 mjf
498 1.20.6.2 mjf if (datain & 0x80) {
499 1.20.6.2 mjf id->t_lastchar = 0;
500 1.20.6.2 mjf *type = WSCONS_EVENT_KEY_UP;
501 1.20.6.2 mjf } else {
502 1.20.6.2 mjf /* Always ignore typematic keys */
503 1.20.6.2 mjf if (key == id->t_lastchar)
504 1.20.6.2 mjf return 0;
505 1.20.6.2 mjf id->t_lastchar = key;
506 1.20.6.2 mjf *type = WSCONS_EVENT_KEY_DOWN;
507 1.20.6.2 mjf }
508 1.20.6.2 mjf
509 1.20.6.2 mjf *dataout = key;
510 1.20.6.2 mjf return 1;
511 1.20.6.2 mjf }
512 1.20.6.2 mjf
513 1.20.6.2 mjf int
514 1.20.6.2 mjf pckbd_init(struct pckbd_internal *t, pckbport_tag_t kbctag,
515 1.20.6.2 mjf pckbport_slot_t kbcslot, int console)
516 1.20.6.2 mjf {
517 1.20.6.2 mjf
518 1.20.6.2 mjf memset(t, 0, sizeof(struct pckbd_internal));
519 1.20.6.2 mjf
520 1.20.6.2 mjf t->t_isconsole = console;
521 1.20.6.2 mjf t->t_kbctag = kbctag;
522 1.20.6.2 mjf t->t_kbcslot = kbcslot;
523 1.20.6.2 mjf
524 1.20.6.2 mjf return pckbd_set_xtscancode(kbctag, kbcslot);
525 1.20.6.2 mjf }
526 1.20.6.2 mjf
527 1.20.6.2 mjf static int
528 1.20.6.2 mjf pckbd_led_encode(int led)
529 1.20.6.2 mjf {
530 1.20.6.2 mjf int res;
531 1.20.6.2 mjf
532 1.20.6.2 mjf res = 0;
533 1.20.6.2 mjf
534 1.20.6.2 mjf if (led & WSKBD_LED_SCROLL)
535 1.20.6.2 mjf res |= 0x01;
536 1.20.6.2 mjf if (led & WSKBD_LED_NUM)
537 1.20.6.2 mjf res |= 0x02;
538 1.20.6.2 mjf if (led & WSKBD_LED_CAPS)
539 1.20.6.2 mjf res |= 0x04;
540 1.20.6.2 mjf return res;
541 1.20.6.2 mjf }
542 1.20.6.2 mjf
543 1.20.6.2 mjf static int
544 1.20.6.2 mjf pckbd_led_decode(int led)
545 1.20.6.2 mjf {
546 1.20.6.2 mjf int res;
547 1.20.6.2 mjf
548 1.20.6.2 mjf res = 0;
549 1.20.6.2 mjf if (led & 0x01)
550 1.20.6.2 mjf res |= WSKBD_LED_SCROLL;
551 1.20.6.2 mjf if (led & 0x02)
552 1.20.6.2 mjf res |= WSKBD_LED_NUM;
553 1.20.6.2 mjf if (led & 0x04)
554 1.20.6.2 mjf res |= WSKBD_LED_CAPS;
555 1.20.6.2 mjf return res;
556 1.20.6.2 mjf }
557 1.20.6.2 mjf
558 1.20.6.2 mjf void
559 1.20.6.2 mjf pckbd_set_leds(void *v, int leds)
560 1.20.6.2 mjf {
561 1.20.6.2 mjf struct pckbd_softc *sc = v;
562 1.20.6.2 mjf u_char cmd[2];
563 1.20.6.2 mjf
564 1.20.6.2 mjf cmd[0] = KBC_MODEIND;
565 1.20.6.2 mjf cmd[1] = pckbd_led_encode(leds);
566 1.20.6.2 mjf sc->sc_ledstate = cmd[1];
567 1.20.6.2 mjf
568 1.20.6.2 mjf (void)pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
569 1.20.6.2 mjf cmd, 2, 0, 0, 0);
570 1.20.6.2 mjf }
571 1.20.6.2 mjf
572 1.20.6.2 mjf /*
573 1.20.6.2 mjf * Got a console receive interrupt -
574 1.20.6.2 mjf * the console processor wants to give us a character.
575 1.20.6.2 mjf */
576 1.20.6.2 mjf void
577 1.20.6.2 mjf pckbd_input(void *vsc, int data)
578 1.20.6.2 mjf {
579 1.20.6.2 mjf struct pckbd_softc *sc = vsc;
580 1.20.6.2 mjf int key;
581 1.20.6.2 mjf u_int type;
582 1.20.6.2 mjf
583 1.20.6.2 mjf #ifdef WSDISPLAY_COMPAT_RAWKBD
584 1.20.6.2 mjf if (sc->rawkbd) {
585 1.20.6.2 mjf u_char d = data;
586 1.20.6.2 mjf wskbd_rawinput(sc->sc_wskbddev, &d, 1);
587 1.20.6.2 mjf return;
588 1.20.6.2 mjf }
589 1.20.6.2 mjf #endif
590 1.20.6.2 mjf if (pckbd_decode(sc->id, data, &type, &key))
591 1.20.6.2 mjf wskbd_input(sc->sc_wskbddev, type, key);
592 1.20.6.2 mjf }
593 1.20.6.2 mjf
594 1.20.6.2 mjf int
595 1.20.6.2 mjf pckbd_ioctl(void *v, u_long cmd, void *data, int flag,
596 1.20.6.2 mjf struct lwp *l)
597 1.20.6.2 mjf {
598 1.20.6.2 mjf struct pckbd_softc *sc = v;
599 1.20.6.2 mjf
600 1.20.6.2 mjf switch (cmd) {
601 1.20.6.2 mjf case WSKBDIO_GTYPE:
602 1.20.6.2 mjf *(int *)data = WSKBD_TYPE_PC_XT;
603 1.20.6.2 mjf return 0;
604 1.20.6.2 mjf case WSKBDIO_SETLEDS:
605 1.20.6.2 mjf {
606 1.20.6.2 mjf int res;
607 1.20.6.2 mjf u_char cmdb[2];
608 1.20.6.2 mjf
609 1.20.6.2 mjf cmdb[0] = KBC_MODEIND;
610 1.20.6.2 mjf cmdb[1] = pckbd_led_encode(*(int *)data);
611 1.20.6.2 mjf sc->sc_ledstate = cmdb[1];
612 1.20.6.2 mjf res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
613 1.20.6.2 mjf cmdb, 2, 0, 1, 0);
614 1.20.6.2 mjf return res;
615 1.20.6.2 mjf }
616 1.20.6.2 mjf case WSKBDIO_GETLEDS:
617 1.20.6.2 mjf *(int *)data = pckbd_led_decode(sc->sc_ledstate);
618 1.20.6.2 mjf return 0;
619 1.20.6.2 mjf case WSKBDIO_COMPLEXBELL:
620 1.20.6.2 mjf #define d ((struct wskbd_bell_data *)data)
621 1.20.6.2 mjf /*
622 1.20.6.2 mjf * Keyboard can't beep directly; we have an
623 1.20.6.2 mjf * externally-provided global hook to do this.
624 1.20.6.2 mjf */
625 1.20.6.2 mjf pckbd_bell(d->pitch, d->period, d->volume, 0);
626 1.20.6.2 mjf #undef d
627 1.20.6.2 mjf return 0;
628 1.20.6.2 mjf #ifdef WSDISPLAY_COMPAT_RAWKBD
629 1.20.6.2 mjf case WSKBDIO_SETMODE:
630 1.20.6.2 mjf sc->rawkbd = (*(int *)data == WSKBD_RAW);
631 1.20.6.2 mjf return 0;
632 1.20.6.2 mjf #endif
633 1.20.6.2 mjf }
634 1.20.6.2 mjf return EPASSTHROUGH;
635 1.20.6.2 mjf }
636 1.20.6.2 mjf
637 1.20.6.2 mjf void
638 1.20.6.2 mjf pckbd_bell(u_int pitch, u_int period, u_int volume, int poll)
639 1.20.6.2 mjf {
640 1.20.6.2 mjf
641 1.20.6.2 mjf if (pckbd_bell_fn != NULL)
642 1.20.6.2 mjf (*pckbd_bell_fn)(pckbd_bell_fn_arg, pitch, period,
643 1.20.6.2 mjf volume, poll);
644 1.20.6.2 mjf }
645 1.20.6.2 mjf
646 1.20.6.2 mjf void
647 1.20.6.2 mjf pckbd_unhook_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
648 1.20.6.2 mjf {
649 1.20.6.2 mjf if (pckbd_bell_fn != fn && pckbd_bell_fn_arg != arg)
650 1.20.6.2 mjf return;
651 1.20.6.2 mjf pckbd_bell_fn = NULL;
652 1.20.6.2 mjf pckbd_bell_fn_arg = NULL;
653 1.20.6.2 mjf }
654 1.20.6.2 mjf
655 1.20.6.2 mjf void
656 1.20.6.2 mjf pckbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
657 1.20.6.2 mjf {
658 1.20.6.2 mjf
659 1.20.6.2 mjf if (pckbd_bell_fn == NULL) {
660 1.20.6.2 mjf pckbd_bell_fn = fn;
661 1.20.6.2 mjf pckbd_bell_fn_arg = arg;
662 1.20.6.2 mjf }
663 1.20.6.2 mjf }
664 1.20.6.2 mjf
665 1.20.6.2 mjf int
666 1.20.6.2 mjf pckbd_cnattach(pckbport_tag_t kbctag, int kbcslot)
667 1.20.6.2 mjf {
668 1.20.6.2 mjf int res;
669 1.20.6.2 mjf u_char cmd[1];
670 1.20.6.2 mjf
671 1.20.6.2 mjf res = pckbd_init(&pckbd_consdata, kbctag, kbcslot, 1);
672 1.20.6.2 mjf /* We may allow the console to be attached if no keyboard is present */
673 1.20.6.2 mjf #if defined(PCKBD_CNATTACH_MAY_FAIL)
674 1.20.6.2 mjf if (res)
675 1.20.6.2 mjf return res;
676 1.20.6.2 mjf #endif
677 1.20.6.2 mjf
678 1.20.6.2 mjf /* Just to be sure. */
679 1.20.6.2 mjf cmd[0] = KBC_ENABLE;
680 1.20.6.2 mjf res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 0, 0, 0);
681 1.20.6.2 mjf
682 1.20.6.2 mjf #if defined(PCKBD_CNATTACH_MAY_FAIL)
683 1.20.6.2 mjf if (res)
684 1.20.6.2 mjf return res;
685 1.20.6.2 mjf #endif
686 1.20.6.2 mjf
687 1.20.6.2 mjf wskbd_cnattach(&pckbd_consops, &pckbd_consdata, &pckbd_keymapdata);
688 1.20.6.2 mjf
689 1.20.6.2 mjf return 0;
690 1.20.6.2 mjf }
691 1.20.6.2 mjf
692 1.20.6.2 mjf /* ARGSUSED */
693 1.20.6.2 mjf void
694 1.20.6.2 mjf pckbd_cngetc(void *v, u_int *type, int *data)
695 1.20.6.2 mjf {
696 1.20.6.2 mjf struct pckbd_internal *t = v;
697 1.20.6.2 mjf int val;
698 1.20.6.2 mjf
699 1.20.6.2 mjf for (;;) {
700 1.20.6.2 mjf val = pckbport_poll_data(t->t_kbctag, t->t_kbcslot);
701 1.20.6.2 mjf if ((val != -1) && pckbd_decode(t, val, type, data))
702 1.20.6.2 mjf return;
703 1.20.6.2 mjf }
704 1.20.6.2 mjf }
705 1.20.6.2 mjf
706 1.20.6.2 mjf void
707 1.20.6.2 mjf pckbd_cnpollc(void *v, int on)
708 1.20.6.2 mjf {
709 1.20.6.2 mjf struct pckbd_internal *t = v;
710 1.20.6.2 mjf
711 1.20.6.2 mjf if (on) {
712 1.20.6.2 mjf u_char cmd[1];
713 1.20.6.2 mjf
714 1.20.6.2 mjf cmd[0] = KBC_ENABLE;
715 1.20.6.2 mjf (void)pckbport_poll_cmd(t->t_kbctag, t->t_kbcslot, cmd,
716 1.20.6.2 mjf 1, 0, 0, 0);
717 1.20.6.2 mjf }
718 1.20.6.2 mjf
719 1.20.6.2 mjf pckbport_set_poll(t->t_kbctag, t->t_kbcslot, on);
720 1.20.6.2 mjf }
721 1.20.6.2 mjf
722 1.20.6.2 mjf void
723 1.20.6.2 mjf pckbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
724 1.20.6.2 mjf {
725 1.20.6.2 mjf
726 1.20.6.2 mjf pckbd_bell(pitch, period, volume, 1);
727 1.20.6.2 mjf }
728