pckbd.c revision 1.1 1 /* $NetBSD: pckbd.c,v 1.1 2004/03/13 17:31:33 bjh21 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.1 2004/03/13 17:31:33 bjh21 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/pckbport/pckbdreg.h>
91 #include <dev/pckbport/pckbdvar.h>
92 #include <dev/pckbport/wskbdmap_mfii.h>
93
94 #include <dev/wscons/wsconsio.h>
95 #include <dev/wscons/wskbdvar.h>
96 #include <dev/wscons/wsksymdef.h>
97 #include <dev/wscons/wsksymvar.h>
98
99 #include "locators.h"
100
101 #include "opt_pckbd_layout.h"
102 #include "opt_wsdisplay_compat.h"
103
104 struct pckbd_internal {
105 int t_isconsole;
106 pckbport_tag_t t_kbctag;
107 pckbport_slot_t t_kbcslot;
108
109 int t_lastchar;
110 int t_extended0;
111 int t_extended1;
112
113 struct pckbd_softc *t_sc; /* back pointer */
114 };
115
116 struct pckbd_softc {
117 struct device sc_dev;
118
119 struct pckbd_internal *id;
120 int sc_enabled;
121
122 int sc_ledstate;
123
124 struct device *sc_wskbddev;
125 #ifdef WSDISPLAY_COMPAT_RAWKBD
126 int rawkbd;
127 #endif
128 };
129
130 static int pckbd_is_console __P((pckbport_tag_t, pckbport_slot_t));
131
132 int pckbdprobe __P((struct device *, struct cfdata *, void *));
133 void pckbdattach __P((struct device *, struct device *, void *));
134
135 CFATTACH_DECL(pckbd, sizeof(struct pckbd_softc),
136 pckbdprobe, pckbdattach, NULL, NULL);
137
138 int pckbd_enable __P((void *, int));
139 void pckbd_set_leds __P((void *, int));
140 int pckbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
141
142 const struct wskbd_accessops pckbd_accessops = {
143 pckbd_enable,
144 pckbd_set_leds,
145 pckbd_ioctl,
146 };
147
148 void pckbd_cngetc __P((void *, u_int *, int *));
149 void pckbd_cnpollc __P((void *, int));
150 void pckbd_cnbell __P((void *, u_int, u_int, u_int));
151
152 const struct wskbd_consops pckbd_consops = {
153 pckbd_cngetc,
154 pckbd_cnpollc,
155 pckbd_cnbell,
156 };
157
158 const struct wskbd_mapdata pckbd_keymapdata = {
159 pckbd_keydesctab,
160 #ifdef PCKBD_LAYOUT
161 PCKBD_LAYOUT,
162 #else
163 KB_US,
164 #endif
165 };
166
167 /*
168 * Hackish support for a bell on the PC Keyboard; when a suitable feeper
169 * is found, it attaches itself into the pckbd driver here.
170 */
171 void (*pckbd_bell_fn) __P((void *, u_int, u_int, u_int, int));
172 void *pckbd_bell_fn_arg;
173
174 void pckbd_bell __P((u_int, u_int, u_int, int));
175
176 int pckbd_set_xtscancode __P((pckbport_tag_t, pckbport_slot_t));
177 int pckbd_init __P((struct pckbd_internal *, pckbport_tag_t, pckbport_slot_t,
178 int));
179 void pckbd_input __P((void *, int));
180
181 static int pckbd_decode __P((struct pckbd_internal *, int,
182 u_int *, int *));
183 static int pckbd_led_encode __P((int));
184 static int pckbd_led_decode __P((int));
185
186 struct pckbd_internal pckbd_consdata;
187
188 int
189 pckbd_set_xtscancode(kbctag, kbcslot)
190 pckbport_tag_t kbctag;
191 pckbport_slot_t kbcslot;
192 {
193 u_char cmd[2];
194 int res;
195
196 /*
197 * Some keyboard/8042 combinations do not seem to work if the keyboard
198 * is set to table 1; in fact, it would appear that some keyboards just
199 * ignore the command altogether. So by default, we use the AT scan
200 * codes and have the 8042 translate them. Unfortunately, this is
201 * known to not work on some PS/2 machines. We try desperately to deal
202 * with this by checking the (lack of a) translate bit in the 8042 and
203 * attempting to set the keyboard to XT mode. If this all fails, well,
204 * tough luck.
205 *
206 * XXX It would perhaps be a better choice to just use AT scan codes
207 * and not bother with this.
208 */
209 if (pckbport_xt_translation(kbctag, kbcslot, 1)) {
210 /* The 8042 is translating for us; use AT codes. */
211 cmd[0] = KBC_SETTABLE;
212 cmd[1] = 2;
213 res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
214 if (res) {
215 u_char cmd[1];
216 #ifdef DEBUG
217 printf("pckbd: error setting scanset 2\n");
218 #endif
219 /*
220 * XXX at least one keyboard is reported to lock up
221 * if a "set table" is attempted, thus the "reset".
222 * XXX ignore errors, scanset 2 should be
223 * default anyway.
224 */
225 cmd[0] = KBC_RESET;
226 (void)pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 1, 0, 1);
227 pckbport_flush(kbctag, kbcslot);
228 res = 0;
229 }
230 } else {
231 /* Stupid 8042; set keyboard to XT codes. */
232 cmd[0] = KBC_SETTABLE;
233 cmd[1] = 1;
234 res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
235 #ifdef DEBUG
236 if (res)
237 printf("pckbd: error setting scanset 1\n");
238 #endif
239 }
240 return (res);
241 }
242
243 static int
244 pckbd_is_console(tag, slot)
245 pckbport_tag_t tag;
246 pckbport_slot_t slot;
247 {
248 return (pckbd_consdata.t_isconsole &&
249 (tag == pckbd_consdata.t_kbctag) &&
250 (slot == pckbd_consdata.t_kbcslot));
251 }
252
253 /*
254 * these are both bad jokes
255 */
256 int
257 pckbdprobe(parent, cf, aux)
258 struct device *parent;
259 struct cfdata *cf;
260 void *aux;
261 {
262 struct pckbport_attach_args *pa = aux;
263 u_char cmd[1], resp[1];
264 int res;
265
266 /*
267 * XXX There are rumours that a keyboard can be connected
268 * to the aux port as well. For me, this didn't work.
269 * For further experiments, allow it if explicitly
270 * wired in the config file.
271 */
272 if ((pa->pa_slot != PCKBPORT_KBD_SLOT) &&
273 (cf->cf_loc[PCKBPORTCF_SLOT] == PCKBPORTCF_SLOT_DEFAULT))
274 return (0);
275
276 /* Flush any garbage. */
277 pckbport_flush(pa->pa_tag, pa->pa_slot);
278
279 /* Reset the keyboard. */
280 cmd[0] = KBC_RESET;
281 res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 1, resp, 1);
282 if (res) {
283 #ifdef DEBUG
284 printf("pckbdprobe: reset error %d\n", res);
285 #endif
286 /*
287 * There is probably no keyboard connected.
288 * Let the probe succeed if the keyboard is used
289 * as console input - it can be connected later.
290 */
291 return (pckbd_is_console(pa->pa_tag, pa->pa_slot) ? 1 : 0);
292 }
293 if (resp[0] != KBR_RSTDONE) {
294 printf("pckbdprobe: reset response 0x%x\n", resp[0]);
295 return (0);
296 }
297
298 /*
299 * Some keyboards seem to leave a second ack byte after the reset.
300 * This is kind of stupid, but we account for them anyway by just
301 * flushing the buffer.
302 */
303 pckbport_flush(pa->pa_tag, pa->pa_slot);
304
305 if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot))
306 return (0);
307
308 return (2);
309 }
310
311 void
312 pckbdattach(parent, self, aux)
313 struct device *parent, *self;
314 void *aux;
315 {
316 struct pckbd_softc *sc = (void *)self;
317 struct pckbport_attach_args *pa = aux;
318 int isconsole;
319 struct wskbddev_attach_args a;
320 u_char cmd[1];
321
322 printf("\n");
323
324 isconsole = pckbd_is_console(pa->pa_tag, pa->pa_slot);
325
326 if (isconsole) {
327 sc->id = &pckbd_consdata;
328
329 /*
330 * Some keyboards are not enabled after a reset,
331 * so make sure it is enabled now.
332 */
333 cmd[0] = KBC_ENABLE;
334 (void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
335 cmd, 1, 0, 0, 0);
336 sc->sc_enabled = 1;
337 } else {
338 sc->id = malloc(sizeof(struct pckbd_internal),
339 M_DEVBUF, M_WAITOK);
340 (void) pckbd_init(sc->id, pa->pa_tag, pa->pa_slot, 0);
341
342 /* no interrupts until enabled */
343 cmd[0] = KBC_DISABLE;
344 (void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
345 cmd, 1, 0, 0, 0);
346 sc->sc_enabled = 0;
347 }
348
349 sc->id->t_sc = sc;
350
351 pckbport_set_inputhandler(sc->id->t_kbctag, sc->id->t_kbcslot,
352 pckbd_input, sc, sc->sc_dev.dv_xname);
353
354 a.console = isconsole;
355
356 a.keymap = &pckbd_keymapdata;
357
358 a.accessops = &pckbd_accessops;
359 a.accesscookie = sc;
360
361 /*
362 * Attach the wskbd, saving a handle to it.
363 * XXX XXX XXX
364 */
365 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
366 }
367
368 int
369 pckbd_enable(v, on)
370 void *v;
371 int on;
372 {
373 struct pckbd_softc *sc = v;
374 u_char cmd[1];
375 int res;
376
377 if (on) {
378 if (sc->sc_enabled) {
379 #ifdef DIAGNOSTIC
380 printf("pckbd_enable: bad enable\n");
381 #endif
382 return (EBUSY);
383 }
384
385 pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 1);
386
387 cmd[0] = KBC_ENABLE;
388 res = pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
389 cmd, 1, 0, NULL, 0);
390 if (res) {
391 printf("pckbd_enable: command error\n");
392 return (res);
393 }
394
395 res = pckbd_set_xtscancode(sc->id->t_kbctag,
396 sc->id->t_kbcslot);
397 if (res)
398 return (res);
399
400 sc->sc_enabled = 1;
401 } else {
402 if (sc->id->t_isconsole)
403 return (EBUSY);
404
405 cmd[0] = KBC_DISABLE;
406 res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
407 cmd, 1, 0, 1, 0);
408 if (res) {
409 printf("pckbd_disable: command error\n");
410 return (res);
411 }
412
413 pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 0);
414
415 sc->sc_enabled = 0;
416 }
417
418 return (0);
419 }
420
421 static int
422 pckbd_decode(id, datain, type, dataout)
423 struct pckbd_internal *id;
424 int datain;
425 u_int *type;
426 int *dataout;
427 {
428 int key;
429
430 if (datain == KBR_EXTENDED0) {
431 id->t_extended0 = 1;
432 return(0);
433 } else if (datain == KBR_EXTENDED1) {
434 id->t_extended1 = 2;
435 return(0);
436 }
437
438 /* map extended keys to (unused) codes 128-254 */
439 key = (datain & 0x7f) | (id->t_extended0 ? 0x80 : 0);
440 id->t_extended0 = 0;
441
442 /*
443 * process PAUSE (also break) key (EXT1 1D 45 EXT1 9D C5):
444 * map to (unused) code 7F
445 */
446 if (id->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
447 id->t_extended1 = 1;
448 return(0);
449 } else if (id->t_extended1 == 1 &&
450 (datain == 0x45 || datain == 0xc5)) {
451 id->t_extended1 = 0;
452 key = 0x7f;
453 } else if (id->t_extended1 > 0) {
454 id->t_extended1 = 0;
455 }
456
457 if (datain & 0x80) {
458 id->t_lastchar = 0;
459 *type = WSCONS_EVENT_KEY_UP;
460 } else {
461 /* Always ignore typematic keys */
462 if (key == id->t_lastchar)
463 return(0);
464 id->t_lastchar = key;
465 *type = WSCONS_EVENT_KEY_DOWN;
466 }
467
468 *dataout = key;
469 return(1);
470 }
471
472 int
473 pckbd_init(t, kbctag, kbcslot, console)
474 struct pckbd_internal *t;
475 pckbport_tag_t kbctag;
476 pckbport_slot_t kbcslot;
477 int console;
478 {
479 memset(t, 0, sizeof(struct pckbd_internal));
480
481 t->t_isconsole = console;
482 t->t_kbctag = kbctag;
483 t->t_kbcslot = kbcslot;
484
485 return (pckbd_set_xtscancode(kbctag, kbcslot));
486 }
487
488 static int
489 pckbd_led_encode(led)
490 int led;
491 {
492 int res;
493
494 res = 0;
495
496 if (led & WSKBD_LED_SCROLL)
497 res |= 0x01;
498 if (led & WSKBD_LED_NUM)
499 res |= 0x02;
500 if (led & WSKBD_LED_CAPS)
501 res |= 0x04;
502 return(res);
503 }
504
505 static int
506 pckbd_led_decode(led)
507 int led;
508 {
509 int res;
510
511 res = 0;
512 if (led & 0x01)
513 res |= WSKBD_LED_SCROLL;
514 if (led & 0x02)
515 res |= WSKBD_LED_NUM;
516 if (led & 0x04)
517 res |= WSKBD_LED_CAPS;
518 return(res);
519 }
520
521 void
522 pckbd_set_leds(v, leds)
523 void *v;
524 int leds;
525 {
526 struct pckbd_softc *sc = v;
527 u_char cmd[2];
528
529 cmd[0] = KBC_MODEIND;
530 cmd[1] = pckbd_led_encode(leds);
531 sc->sc_ledstate = cmd[1];
532
533 (void) pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
534 cmd, 2, 0, 0, 0);
535 }
536
537 /*
538 * Got a console receive interrupt -
539 * the console processor wants to give us a character.
540 */
541 void
542 pckbd_input(vsc, data)
543 void *vsc;
544 int data;
545 {
546 struct pckbd_softc *sc = vsc;
547 int key;
548 u_int type;
549
550 #ifdef WSDISPLAY_COMPAT_RAWKBD
551 if (sc->rawkbd) {
552 u_char d = data;
553 wskbd_rawinput(sc->sc_wskbddev, &d, 1);
554 return;
555 }
556 #endif
557 if (pckbd_decode(sc->id, data, &type, &key))
558 wskbd_input(sc->sc_wskbddev, type, key);
559 }
560
561 int
562 pckbd_ioctl(v, cmd, data, flag, p)
563 void *v;
564 u_long cmd;
565 caddr_t data;
566 int flag;
567 struct proc *p;
568 {
569 struct pckbd_softc *sc = v;
570
571 switch (cmd) {
572 case WSKBDIO_GTYPE:
573 *(int *)data = WSKBD_TYPE_PC_XT;
574 return 0;
575 case WSKBDIO_SETLEDS: {
576 u_char cmd[2];
577 int res;
578 cmd[0] = KBC_MODEIND;
579 cmd[1] = pckbd_led_encode(*(int *)data);
580 sc->sc_ledstate = cmd[1];
581 res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
582 cmd, 2, 0, 1, 0);
583 return (res);
584 }
585 case WSKBDIO_GETLEDS:
586 *(int *)data = pckbd_led_decode(sc->sc_ledstate);
587 return (0);
588 case WSKBDIO_COMPLEXBELL:
589 #define d ((struct wskbd_bell_data *)data)
590 /*
591 * Keyboard can't beep directly; we have an
592 * externally-provided global hook to do this.
593 */
594 pckbd_bell(d->pitch, d->period, d->volume, 0);
595 #undef d
596 return (0);
597 #ifdef WSDISPLAY_COMPAT_RAWKBD
598 case WSKBDIO_SETMODE:
599 sc->rawkbd = (*(int *)data == WSKBD_RAW);
600 return (0);
601 #endif
602 }
603 return EPASSTHROUGH;
604 }
605
606 void
607 pckbd_bell(pitch, period, volume, poll)
608 u_int pitch, period, volume;
609 int poll;
610 {
611
612 if (pckbd_bell_fn != NULL)
613 (*pckbd_bell_fn)(pckbd_bell_fn_arg, pitch, period,
614 volume, poll);
615 }
616
617 void
618 pckbd_hookup_bell(fn, arg)
619 void (*fn) __P((void *, u_int, u_int, u_int, int));
620 void *arg;
621 {
622
623 if (pckbd_bell_fn == NULL) {
624 pckbd_bell_fn = fn;
625 pckbd_bell_fn_arg = arg;
626 }
627 }
628
629 int
630 pckbd_cnattach(kbctag, kbcslot)
631 pckbport_tag_t kbctag;
632 int kbcslot;
633 {
634 u_char cmd[1];
635 int res;
636
637 res = pckbd_init(&pckbd_consdata, kbctag, kbcslot, 1);
638 #if 0 /* we allow the console to be attached if no keyboard is present */
639 if (res)
640 return (res);
641 #endif
642
643 /* Just to be sure. */
644 cmd[0] = KBC_ENABLE;
645 res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 0, 0, 0);
646
647 #if 0
648 if (res)
649 return (res);
650 #endif
651
652 wskbd_cnattach(&pckbd_consops, &pckbd_consdata, &pckbd_keymapdata);
653
654 return (0);
655 }
656
657 /* ARGSUSED */
658 void
659 pckbd_cngetc(v, type, data)
660 void *v;
661 u_int *type;
662 int *data;
663 {
664 struct pckbd_internal *t = v;
665 int val;
666
667 for (;;) {
668 val = pckbport_poll_data(t->t_kbctag, t->t_kbcslot);
669 if ((val != -1) && pckbd_decode(t, val, type, data))
670 return;
671 }
672 }
673
674 void
675 pckbd_cnpollc(v, on)
676 void *v;
677 int on;
678 {
679 struct pckbd_internal *t = v;
680
681 pckbport_set_poll(t->t_kbctag, t->t_kbcslot, on);
682 }
683
684 void
685 pckbd_cnbell(v, pitch, period, volume)
686 void *v;
687 u_int pitch, period, volume;
688 {
689
690 pckbd_bell(pitch, period, volume, 1);
691 }
692