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