kernel - Fix keyboard probe for chromebooks
[dragonfly.git] / sys / dev / misc / kbd / atkbd.c
... / ...
CommitLineData
1/*-
2 * (MPSAFE)
3 *
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/dev/kbd/atkbd.c,v 1.25.2.4 2002/04/08 19:21:38 asmodai Exp $
29 */
30/*
31 * NOTE: All locks are handled by the kbd wrappers.
32 */
33
34#include "opt_kbd.h"
35#include "opt_atkbd.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/bus.h>
41#include <sys/proc.h>
42#include <sys/malloc.h>
43#include <sys/thread2.h>
44
45#ifdef __i386__
46#include <machine/md_var.h>
47#include <machine/psl.h>
48#include <machine/vm86.h>
49#include <machine/pc/bios.h>
50
51#include <vm/vm.h>
52#include <vm/pmap.h>
53#endif /* __i386__ */
54
55#include <sys/kbio.h>
56#include "kbdreg.h"
57#include "atkbdreg.h"
58#include "atkbdcreg.h"
59
60#include <bus/isa/isareg.h>
61
62static timeout_t atkbd_timeout;
63
64#if 0
65static int atkbd_setmuxmode(KBDC kbdc, int value, int *mux_version);
66#endif
67
68int
69atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
70{
71 keyboard_switch_t *sw;
72 int args[2];
73 int error;
74
75 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
76 if (sw == NULL) {
77 return ENXIO;
78 }
79
80 args[0] = ctlr;
81 args[1] = irq;
82 error = (*sw->probe)(unit, args, flags);
83
84 if (error)
85 return error;
86 return 0;
87}
88
89int
90atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
91{
92 keyboard_switch_t *sw;
93 int args[2];
94 int error;
95
96 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
97 if (sw == NULL) {
98 return ENXIO;
99 }
100
101 /* reset, initialize and enable the device */
102 args[0] = ctlr;
103 args[1] = irq;
104 *kbd = NULL;
105 error = (*sw->probe)(unit, args, flags);
106 if (error) {
107 return error;
108 }
109 error = (*sw->init)(unit, kbd, args, flags);
110 if (error) {
111 return error;
112 }
113 (*sw->enable)(*kbd);
114
115#ifdef KBD_INSTALL_CDEV
116 /* attach a virtual keyboard cdev */
117 error = kbd_attach(*kbd);
118 if (error) {
119 return error;
120 }
121#endif
122
123 /*
124 * This is a kludge to compensate for lost keyboard interrupts.
125 * A similar code used to be in syscons. See below. XXX
126 */
127 atkbd_timeout(*kbd);
128
129 if (bootverbose)
130 (*sw->diag)(*kbd, bootverbose);
131
132 return 0;
133}
134
135static void
136atkbd_timeout(void *arg)
137{
138 keyboard_t *kbd;
139
140 /*
141 * The original text of the following comments are extracted
142 * from syscons.c (1.287)
143 *
144 * With release 2.1 of the Xaccel server, the keyboard is left
145 * hanging pretty often. Apparently an interrupt from the
146 * keyboard is lost, and I don't know why (yet).
147 * This ugly hack calls the low-level interrupt routine if input
148 * is ready for the keyboard and conveniently hides the problem. XXX
149 *
150 * Try removing anything stuck in the keyboard controller; whether
151 * it's a keyboard scan code or mouse data. The low-level
152 * interrupt routine doesn't read the mouse data directly,
153 * but the keyboard controller driver will, as a side effect.
154 */
155 /*
156 * And here is bde's original comment about this:
157 *
158 * This is necessary to handle edge triggered interrupts - if we
159 * returned when our IRQ is high due to unserviced input, then there
160 * would be no more keyboard IRQs until the keyboard is reset by
161 * external powers.
162 *
163 * The keyboard apparently unwedges the irq in most cases.
164 */
165 crit_enter();
166 kbd = (keyboard_t *)arg;
167 if (kbd_lock(kbd, TRUE)) {
168 /*
169 * We have seen the lock flag is not set. Let's reset
170 * the flag early, otherwise the LED update routine fails
171 * which may want the lock during the interrupt routine.
172 */
173 kbd_lock(kbd, FALSE);
174 if (kbd_check_char(kbd))
175 kbd_intr(kbd, NULL);
176 }
177 callout_reset(&kbd->kb_atkbd_timeout_ch, hz / 10, atkbd_timeout, arg);
178 crit_exit();
179}
180
181/* LOW-LEVEL */
182
183#include <machine/limits.h>
184#include <machine/clock.h>
185
186#define ATKBD_DEFAULT 0
187
188typedef struct atkbd_state {
189 KBDC kbdc; /* keyboard controller */
190 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
191 int ks_flags; /* flags */
192#define COMPOSE (1 << 0)
193 int ks_polling;
194 int ks_state; /* shift/lock key state */
195 int ks_accents; /* accent key index (> 0) */
196 u_int ks_composed_char; /* composed char code (> 0) */
197 u_char ks_prefix; /* AT scan code prefix */
198} atkbd_state_t;
199
200/* keyboard driver declaration */
201static int atkbd_configure(int flags);
202static kbd_probe_t atkbd_probe;
203static kbd_init_t atkbd_init;
204static kbd_term_t atkbd_term;
205static kbd_intr_t atkbd_intr;
206static kbd_test_if_t atkbd_test_if;
207static kbd_enable_t atkbd_enable;
208static kbd_disable_t atkbd_disable;
209static kbd_read_t atkbd_read;
210static kbd_check_t atkbd_check;
211static kbd_read_char_t atkbd_read_char;
212static kbd_check_char_t atkbd_check_char;
213static kbd_ioctl_t atkbd_ioctl;
214static kbd_lock_t atkbd_lock;
215static kbd_clear_state_t atkbd_clear_state;
216static kbd_get_state_t atkbd_get_state;
217static kbd_set_state_t atkbd_set_state;
218static kbd_poll_mode_t atkbd_poll;
219
220keyboard_switch_t atkbdsw = {
221 atkbd_probe,
222 atkbd_init,
223 atkbd_term,
224 atkbd_intr,
225 atkbd_test_if,
226 atkbd_enable,
227 atkbd_disable,
228 atkbd_read,
229 atkbd_check,
230 atkbd_read_char,
231 atkbd_check_char,
232 atkbd_ioctl,
233 atkbd_lock,
234 atkbd_clear_state,
235 atkbd_get_state,
236 atkbd_set_state,
237 genkbd_get_fkeystr,
238 atkbd_poll,
239 genkbd_diag,
240};
241
242KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
243
244/* local functions */
245static int get_typematic(keyboard_t *kbd);
246static int setup_kbd_port(KBDC kbdc, int port, int intr);
247static int get_kbd_echo(KBDC kbdc);
248static int probe_keyboard(KBDC kbdc, int flags);
249static int init_keyboard(KBDC kbdc, int *type, int flags);
250static int write_kbd(KBDC kbdc, int command, int data);
251static int get_kbd_id(KBDC kbdc, int cmd);
252static int typematic(int delay, int rate);
253static int typematic_delay(int delay);
254static int typematic_rate(int rate);
255
256/* local variables */
257
258/* the initial key map, accent map and fkey strings */
259#ifdef ATKBD_DFLT_KEYMAP
260#define KBD_DFLT_KEYMAP
261#include "atkbdmap.h"
262#endif
263#include "kbdtables.h"
264
265/* structures for the default keyboard */
266static keyboard_t default_kbd;
267static atkbd_state_t default_kbd_state;
268static keymap_t default_keymap;
269static accentmap_t default_accentmap;
270static fkeytab_t default_fkeytab[NUM_FKEYS];
271
272/*
273 * The back door to the keyboard driver!
274 * This function is called by the console driver, via the kbdio module,
275 * to tickle keyboard drivers when the low-level console is being initialized.
276 * Almost nothing in the kernel has been initialied yet. Try to probe
277 * keyboards if possible.
278 * NOTE: because of the way the low-level console is initialized, this routine
279 * may be called more than once!!
280 */
281static int
282atkbd_configure(int flags)
283{
284 keyboard_t *kbd;
285 int arg[2];
286 int i;
287
288 /*
289 * Probe the keyboard controller, if not present or if the driver
290 * is disabled, unregister the keyboard if any.
291 */
292 if (atkbdc_configure() != 0 ||
293 resource_disabled("atkbd", ATKBD_DEFAULT)) {
294 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
295 if (i >= 0) {
296 kbd = kbd_get_keyboard(i);
297 KBD_ALWAYS_LOCK(kbd);
298 kbd_unregister(kbd);
299 kbd = NULL; /* huh? */
300 }
301 return 0;
302 }
303
304 /* XXX: a kludge to obtain the device configuration flags */
305 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
306 flags |= i;
307
308 /* probe the default keyboard */
309 arg[0] = -1;
310 arg[1] = -1;
311 kbd = NULL;
312 if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) {
313 return 0;
314 }
315 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) {
316 return 0;
317 }
318
319 /* return the number of found keyboards */
320 return 1;
321}
322
323/* low-level functions */
324
325/* detect a keyboard */
326static int
327atkbd_probe(int unit, void *arg, int flags)
328{
329 KBDC kbdc;
330 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
331
332
333 if (unit == ATKBD_DEFAULT) {
334 if (KBD_IS_PROBED(&default_kbd)) {
335 return 0;
336 }
337 }
338
339 kbdc = atkbdc_open(data[0]);
340 if (kbdc == NULL) {
341 return ENXIO;
342 }
343 if (probe_keyboard(kbdc, flags)) {
344 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
345 return ENXIO;
346 }
347 }
348
349 return 0;
350}
351
352/* reset and initialize the device */
353static int
354atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
355{
356 keyboard_t *kbd;
357 atkbd_state_t *state;
358 keymap_t *keymap;
359 accentmap_t *accmap;
360 fkeytab_t *fkeymap;
361 int fkeymap_size;
362 int delay[2];
363 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
364
365 /* XXX */
366 if (unit == ATKBD_DEFAULT) {
367 *kbdp = kbd = &default_kbd;
368 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) {
369 return 0;
370 }
371 state = &default_kbd_state;
372 keymap = &default_keymap;
373 accmap = &default_accentmap;
374 fkeymap = default_fkeytab;
375 fkeymap_size = NELEM(default_fkeytab);
376 } else if (*kbdp == NULL) {
377 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO);
378 state = kmalloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO);
379 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_WAITOK);
380 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_WAITOK);
381 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK);
382 fkeymap_size = NELEM(fkey_tab);
383 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
384 return 0;
385 } else {
386 kbd = *kbdp;
387 state = (atkbd_state_t *)kbd->kb_data;
388 bzero(state, sizeof(*state));
389 keymap = kbd->kb_keymap;
390 accmap = kbd->kb_accentmap;
391 fkeymap = kbd->kb_fkeytab;
392 fkeymap_size = kbd->kb_fkeytab_size;
393 }
394
395 if (!KBD_IS_PROBED(kbd)) {
396 state->kbdc = atkbdc_open(data[0]);
397 if (state->kbdc == NULL) {
398 return ENXIO;
399 }
400 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
401 KB_PRI_ATKBD, 0, 0);
402 bcopy(&key_map, keymap, sizeof(key_map));
403 bcopy(&accent_map, accmap, sizeof(accent_map));
404 bcopy(fkey_tab, fkeymap,
405 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
406 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
407 kbd->kb_data = (void *)state;
408
409 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
410 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
411 return ENXIO;
412 }
413 } else {
414 KBD_FOUND_DEVICE(kbd);
415 }
416 atkbd_clear_state(kbd);
417 state->ks_mode = K_XLATE;
418 /*
419 * FIXME: set the initial value for lock keys in ks_state
420 * according to the BIOS data?
421 */
422 KBD_PROBE_DONE(kbd);
423 }
424 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
425 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
426 if (!KBD_HAS_DEVICE(kbd)
427 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
428 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
429 return ENXIO;
430 }
431 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
432 get_typematic(kbd);
433 delay[0] = kbd->kb_delay1;
434 delay[1] = kbd->kb_delay2;
435 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
436 KBD_FOUND_DEVICE(kbd);
437 KBD_INIT_DONE(kbd);
438 }
439 if (!KBD_IS_CONFIGURED(kbd)) {
440 if (kbd_register(kbd) < 0) {
441 return ENXIO;
442 }
443 KBD_CONFIG_DONE(kbd);
444 }
445
446 return 0;
447}
448
449/* finish using this keyboard */
450static int
451atkbd_term(keyboard_t *kbd)
452{
453 kbd_unregister(kbd);
454 return 0;
455}
456
457/* keyboard interrupt routine */
458static int
459atkbd_intr(keyboard_t *kbd, void *arg)
460{
461 atkbd_state_t *state;
462 int delay[2];
463 int c;
464
465 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
466 /* let the callback function to process the input */
467 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
468 kbd->kb_callback.kc_arg);
469 } else {
470 /* read and discard the input; no one is waiting for input */
471 do {
472 c = atkbd_read_char(kbd, FALSE);
473 } while (c != NOKEY);
474
475 if (!KBD_HAS_DEVICE(kbd)) {
476 /*
477 * The keyboard was not detected before;
478 * it must have been reconnected!
479 */
480 state = (atkbd_state_t *)kbd->kb_data;
481 init_keyboard(state->kbdc, &kbd->kb_type,
482 kbd->kb_config);
483 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
484 get_typematic(kbd);
485 delay[0] = kbd->kb_delay1;
486 delay[1] = kbd->kb_delay2;
487 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
488 KBD_FOUND_DEVICE(kbd);
489 }
490 }
491
492 return 0;
493}
494
495/* test the interface to the device */
496static int
497atkbd_test_if(keyboard_t *kbd)
498{
499 int error;
500
501 error = 0;
502 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
503 crit_enter();
504 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
505 error = EIO;
506 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
507 error = EIO;
508 crit_exit();
509
510 return error;
511}
512
513/*
514 * Enable the access to the device; until this function is called,
515 * the client cannot read from the keyboard.
516 */
517static int
518atkbd_enable(keyboard_t *kbd)
519{
520 crit_enter();
521 KBD_ACTIVATE(kbd);
522 crit_exit();
523 return 0;
524}
525
526/* disallow the access to the device */
527static int
528atkbd_disable(keyboard_t *kbd)
529{
530 crit_enter();
531 KBD_DEACTIVATE(kbd);
532 crit_exit();
533 return 0;
534}
535
536/* read one byte from the keyboard if it's allowed */
537static int
538atkbd_read(keyboard_t *kbd, int wait)
539{
540 int c, ret;
541
542 if (wait)
543 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
544 else
545 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
546 if (c != -1)
547 ++kbd->kb_count;
548
549 ret = (KBD_IS_ACTIVE(kbd) ? c : -1);
550
551 return ret;
552}
553
554/* check if data is waiting */
555static int
556atkbd_check(keyboard_t *kbd)
557{
558 int ret;
559
560 if (!KBD_IS_ACTIVE(kbd)) {
561 return FALSE;
562 }
563 ret = kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
564
565 return ret;
566}
567
568/* read char from the keyboard */
569static u_int
570atkbd_read_char(keyboard_t *kbd, int wait)
571{
572 atkbd_state_t *state;
573 u_int action;
574 int scancode;
575 int keycode;
576
577 state = (atkbd_state_t *)kbd->kb_data;
578next_code:
579 /* do we have a composed char to return? */
580 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
581 action = state->ks_composed_char;
582 state->ks_composed_char = 0;
583 if (action > UCHAR_MAX) {
584 return ERRKEY;
585 }
586 return action;
587 }
588
589 /* see if there is something in the keyboard port */
590 if (wait) {
591 do {
592 scancode = read_kbd_data(state->kbdc);
593 } while (scancode == -1);
594 } else {
595 scancode = read_kbd_data_no_wait(state->kbdc);
596 if (scancode == -1) {
597 return NOKEY;
598 }
599 }
600 ++kbd->kb_count;
601
602#if KBDIO_DEBUG >= 10
603 kprintf("atkbd_read_char(): scancode:0x%x\n", scancode);
604#endif
605
606 /* return the byte as is for the K_RAW mode */
607 if (state->ks_mode == K_RAW) {
608 return scancode;
609 }
610
611 /* translate the scan code into a keycode */
612 keycode = scancode & 0x7F;
613 switch (state->ks_prefix) {
614 case 0x00: /* normal scancode */
615 switch(scancode) {
616 case 0xB8: /* left alt (compose key) released */
617 if (state->ks_flags & COMPOSE) {
618 state->ks_flags &= ~COMPOSE;
619 if (state->ks_composed_char > UCHAR_MAX)
620 state->ks_composed_char = 0;
621 }
622 break;
623 case 0x38: /* left alt (compose key) pressed */
624 if (!(state->ks_flags & COMPOSE)) {
625 state->ks_flags |= COMPOSE;
626 state->ks_composed_char = 0;
627 }
628 break;
629 case 0xE0:
630 case 0xE1:
631 state->ks_prefix = scancode;
632 goto next_code;
633 }
634 break;
635 case 0xE0: /* 0xE0 prefix */
636 state->ks_prefix = 0;
637 switch (keycode) {
638 case 0x1C: /* right enter key */
639 keycode = 0x59;
640 break;
641 case 0x1D: /* right ctrl key */
642 keycode = 0x5A;
643 break;
644 case 0x35: /* keypad divide key */
645 keycode = 0x5B;
646 break;
647 case 0x37: /* print scrn key */
648 keycode = 0x5C;
649 break;
650 case 0x38: /* right alt key (alt gr) */
651 keycode = 0x5D;
652 break;
653 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
654 keycode = 0x68;
655 break;
656 case 0x47: /* grey home key */
657 keycode = 0x5E;
658 break;
659 case 0x48: /* grey up arrow key */
660 keycode = 0x5F;
661 break;
662 case 0x49: /* grey page up key */
663 keycode = 0x60;
664 break;
665 case 0x4B: /* grey left arrow key */
666 keycode = 0x61;
667 break;
668 case 0x4D: /* grey right arrow key */
669 keycode = 0x62;
670 break;
671 case 0x4F: /* grey end key */
672 keycode = 0x63;
673 break;
674 case 0x50: /* grey down arrow key */
675 keycode = 0x64;
676 break;
677 case 0x51: /* grey page down key */
678 keycode = 0x65;
679 break;
680 case 0x52: /* grey insert key */
681 keycode = 0x66;
682 break;
683 case 0x53: /* grey delete key */
684 keycode = 0x67;
685 break;
686 /* the following 3 are only used on the MS "Natural" keyboard */
687 case 0x5b: /* left Window key */
688 keycode = 0x69;
689 break;
690 case 0x5c: /* right Window key */
691 keycode = 0x6a;
692 break;
693 case 0x5d: /* menu key */
694 keycode = 0x6b;
695 break;
696 default: /* ignore everything else */
697 goto next_code;
698 }
699 break;
700 case 0xE1: /* 0xE1 prefix */
701 /*
702 * The pause/break key on the 101 keyboard produces:
703 * E1-1D-45 E1-9D-C5
704 * Ctrl-pause/break produces:
705 * E0-46 E0-C6 (See above.)
706 */
707 state->ks_prefix = 0;
708 if (keycode == 0x1D)
709 state->ks_prefix = 0x1D;
710 goto next_code;
711 /* NOT REACHED */
712 case 0x1D: /* pause / break */
713 state->ks_prefix = 0;
714 if (keycode != 0x45)
715 goto next_code;
716 keycode = 0x68;
717 break;
718 }
719
720 if (kbd->kb_type == KB_84) {
721 switch (keycode) {
722 case 0x37: /* *(numpad)/print screen */
723 if (state->ks_flags & SHIFTS)
724 keycode = 0x5c; /* print screen */
725 break;
726 case 0x45: /* num lock/pause */
727 if (state->ks_flags & CTLS)
728 keycode = 0x68; /* pause */
729 break;
730 case 0x46: /* scroll lock/break */
731 if (state->ks_flags & CTLS)
732 keycode = 0x6c; /* break */
733 break;
734 }
735 } else if (kbd->kb_type == KB_101) {
736 switch (keycode) {
737 case 0x5c: /* print screen */
738 if (state->ks_flags & ALTS)
739 keycode = 0x54; /* sysrq */
740 break;
741 case 0x68: /* pause/break */
742 if (state->ks_flags & CTLS)
743 keycode = 0x6c; /* break */
744 break;
745 }
746 }
747
748 /* return the key code in the K_CODE mode */
749 if (state->ks_mode == K_CODE) {
750 return (keycode | (scancode & 0x80));
751 }
752
753 /* compose a character code */
754 if (state->ks_flags & COMPOSE) {
755 switch (keycode | (scancode & 0x80)) {
756 /* key pressed, process it */
757 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
758 state->ks_composed_char *= 10;
759 state->ks_composed_char += keycode - 0x40;
760 if (state->ks_composed_char > UCHAR_MAX)
761 return ERRKEY;
762 goto next_code;
763 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
764 state->ks_composed_char *= 10;
765 state->ks_composed_char += keycode - 0x47;
766 if (state->ks_composed_char > UCHAR_MAX)
767 return ERRKEY;
768 goto next_code;
769 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
770 state->ks_composed_char *= 10;
771 state->ks_composed_char += keycode - 0x4E;
772 if (state->ks_composed_char > UCHAR_MAX)
773 return ERRKEY;
774 goto next_code;
775 case 0x52: /* keypad 0 */
776 state->ks_composed_char *= 10;
777 if (state->ks_composed_char > UCHAR_MAX)
778 return ERRKEY;
779 goto next_code;
780
781 /* key released, no interest here */
782 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
783 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
784 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
785 case 0xD2: /* keypad 0 */
786 goto next_code;
787
788 case 0x38: /* left alt key */
789 break;
790
791 default:
792 if (state->ks_composed_char > 0) {
793 state->ks_flags &= ~COMPOSE;
794 state->ks_composed_char = 0;
795 return ERRKEY;
796 }
797 break;
798 }
799 }
800
801 /* keycode to key action */
802 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
803 &state->ks_state, &state->ks_accents);
804 if (action == NOKEY) {
805 goto next_code;
806 } else {
807 return action;
808 }
809}
810
811/* check if char is waiting */
812static int
813atkbd_check_char(keyboard_t *kbd)
814{
815 atkbd_state_t *state;
816 int ret;
817
818 if (!KBD_IS_ACTIVE(kbd)) {
819 return FALSE;
820 }
821 state = (atkbd_state_t *)kbd->kb_data;
822 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
823 return TRUE;
824 }
825 ret = kbdc_data_ready(state->kbdc);
826 return ret;
827}
828
829/* some useful control functions */
830static int
831atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
832{
833 /* trasnlate LED_XXX bits into the device specific bits */
834 static u_char ledmap[8] = {
835 0, 4, 2, 6, 1, 5, 3, 7,
836 };
837 atkbd_state_t *state = kbd->kb_data;
838 int error;
839 int i;
840
841 crit_enter();
842
843 switch (cmd) {
844
845 case KDGKBMODE: /* get keyboard mode */
846 *(int *)arg = state->ks_mode;
847 break;
848 case KDSKBMODE: /* set keyboard mode */
849 switch (*(int *)arg) {
850 case K_XLATE:
851 if (state->ks_mode != K_XLATE) {
852 /* make lock key state and LED state match */
853 state->ks_state &= ~LOCK_MASK;
854 state->ks_state |= KBD_LED_VAL(kbd);
855 }
856 /* FALL THROUGH */
857 case K_RAW:
858 case K_CODE:
859 if (state->ks_mode != *(int *)arg) {
860 atkbd_clear_state(kbd);
861 state->ks_mode = *(int *)arg;
862 }
863 break;
864 default:
865 crit_exit();
866 return EINVAL;
867 }
868 break;
869
870 case KDGETLED: /* get keyboard LED */
871 *(int *)arg = KBD_LED_VAL(kbd);
872 break;
873 case KDSETLED: /* set keyboard LED */
874 /* NOTE: lock key state in ks_state won't be changed */
875 if (*(int *)arg & ~LOCK_MASK) {
876 crit_exit();
877 return EINVAL;
878 }
879 i = *(int *)arg;
880 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
881 if (state->ks_mode == K_XLATE &&
882 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
883 if (i & ALKED)
884 i |= CLKED;
885 else
886 i &= ~CLKED;
887 }
888 if (KBD_HAS_DEVICE(kbd)) {
889 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
890 ledmap[i & LED_MASK]);
891 if (error) {
892 crit_exit();
893 return error;
894 }
895 }
896 KBD_LED_VAL(kbd) = *(int *)arg;
897 break;
898
899 case KDGKBSTATE: /* get lock key state */
900 *(int *)arg = state->ks_state & LOCK_MASK;
901 break;
902 case KDSKBSTATE: /* set lock key state */
903 if (*(int *)arg & ~LOCK_MASK) {
904 crit_exit();
905 return EINVAL;
906 }
907 state->ks_state &= ~LOCK_MASK;
908 state->ks_state |= *(int *)arg;
909 crit_exit();
910 /* set LEDs and quit */
911 return atkbd_ioctl(kbd, KDSETLED, arg);
912
913 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
914 crit_exit();
915 if (!KBD_HAS_DEVICE(kbd)) {
916 return 0;
917 }
918 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
919 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
920 if (error == 0) {
921 kbd->kb_delay1 = typematic_delay(i);
922 kbd->kb_delay2 = typematic_rate(i);
923 }
924 return error;
925
926 case KDSETRAD: /* set keyboard repeat rate (old interface) */
927 crit_exit();
928 if (!KBD_HAS_DEVICE(kbd)) {
929 return 0;
930 }
931 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
932 if (error == 0) {
933 kbd->kb_delay1 = typematic_delay(*(int *)arg);
934 kbd->kb_delay2 = typematic_rate(*(int *)arg);
935 }
936 return error;
937
938 case PIO_KEYMAP: /* set keyboard translation table */
939 case PIO_KEYMAPENT: /* set keyboard translation table entry */
940 case PIO_DEADKEYMAP: /* set accent key translation table */
941 state->ks_accents = 0;
942 /* FALL THROUGH */
943 default:
944 crit_exit();
945 return genkbd_commonioctl(kbd, cmd, arg);
946 }
947
948 crit_exit();
949 return 0;
950}
951
952/* lock the access to the keyboard */
953static int
954atkbd_lock(keyboard_t *kbd, int lock)
955{
956 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
957}
958
959/* clear the internal state of the keyboard */
960static void
961atkbd_clear_state(keyboard_t *kbd)
962{
963 atkbd_state_t *state;
964
965 state = (atkbd_state_t *)kbd->kb_data;
966 state->ks_flags = 0;
967 state->ks_polling = 0;
968 state->ks_state &= LOCK_MASK; /* preserve locking key state */
969 state->ks_accents = 0;
970 state->ks_composed_char = 0;
971#if 0
972 state->ks_prefix = 0; /* XXX */
973#endif
974}
975
976/* save the internal state */
977static int
978atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
979{
980 if (len == 0)
981 return sizeof(atkbd_state_t);
982 if (len < sizeof(atkbd_state_t))
983 return -1;
984
985 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
986 return 0;
987}
988
989/* set the internal state */
990static int
991atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
992{
993 if (len < sizeof(atkbd_state_t))
994 return ENOMEM;
995 if (((atkbd_state_t *)kbd->kb_data)->kbdc
996 != ((atkbd_state_t *)buf)->kbdc)
997 return ENOMEM;
998 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
999 return 0;
1000}
1001
1002static int
1003atkbd_poll(keyboard_t *kbd, int on)
1004{
1005 atkbd_state_t *state;
1006
1007 state = (atkbd_state_t *)kbd->kb_data;
1008 crit_enter();
1009 if (on)
1010 state->ks_polling = 1;
1011 else
1012 state->ks_polling = 0;
1013 crit_exit();
1014 return 0;
1015}
1016
1017/* local functions */
1018
1019static int
1020get_typematic(keyboard_t *kbd)
1021{
1022#ifdef __i386__
1023 /*
1024 * Only some systems allow us to retrieve the keyboard repeat
1025 * rate previously set via the BIOS...
1026 */
1027 struct vm86frame vmf;
1028 u_int32_t p;
1029
1030 bzero(&vmf, sizeof(vmf));
1031 vmf.vmf_ax = 0xc000;
1032 vm86_intcall(0x15, &vmf);
1033 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
1034 return ENODEV;
1035 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
1036 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
1037 return ENODEV;
1038 vmf.vmf_ax = 0x0900;
1039 vm86_intcall(0x16, &vmf);
1040 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1041 return ENODEV;
1042 vmf.vmf_ax = 0x0306;
1043 vm86_intcall(0x16, &vmf);
1044 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1045 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1046 return 0;
1047#else
1048 return ENODEV;
1049#endif /* __i386__ */
1050}
1051
1052static int
1053setup_kbd_port(KBDC kbdc, int port, int intr)
1054{
1055 if (!set_controller_command_byte(kbdc,
1056 KBD_KBD_CONTROL_BITS,
1057 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1058 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1059 return 1;
1060 return 0;
1061}
1062
1063static int
1064get_kbd_echo(KBDC kbdc)
1065{
1066 /* enable the keyboard port, but disable the keyboard intr. */
1067 if (setup_kbd_port(kbdc, TRUE, FALSE))
1068 /* CONTROLLER ERROR: there is very little we can do... */
1069 return ENXIO;
1070
1071 /* see if something is present */
1072 write_kbd_command(kbdc, KBDC_ECHO);
1073 if (read_kbd_data(kbdc) != KBD_ECHO) {
1074 empty_both_buffers(kbdc, 10);
1075 test_controller(kbdc);
1076 test_kbd_port(kbdc);
1077 return ENXIO;
1078 }
1079
1080 /* enable the keyboard port and intr. */
1081 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1082 /*
1083 * CONTROLLER ERROR
1084 * This is serious; the keyboard intr is left disabled!
1085 */
1086 return ENXIO;
1087 }
1088
1089 return 0;
1090}
1091
1092static int
1093probe_keyboard(KBDC kbdc, int flags)
1094{
1095 /*
1096 * Don't try to print anything in this function. The low-level
1097 * console may not have been initialized yet...
1098 */
1099 int err;
1100 int c;
1101 int m;
1102
1103 if (!kbdc_lock(kbdc, TRUE)) {
1104 /* driver error? */
1105 return ENXIO;
1106 }
1107
1108 /* temporarily block data transmission from the keyboard */
1109 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1110
1111 /* flush any noise in the buffer */
1112 empty_both_buffers(kbdc, 100);
1113
1114 /* save the current keyboard controller command byte */
1115 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1116 c = get_controller_command_byte(kbdc);
1117 if (c == -1) {
1118 /* CONTROLLER ERROR */
1119 kbdc_set_device_mask(kbdc, m);
1120 kbdc_lock(kbdc, FALSE);
1121 return ENXIO;
1122 }
1123
1124 /*
1125 * The keyboard may have been screwed up by the boot block.
1126 * We may just be able to recover from error by testing the controller
1127 * and the keyboard port. The controller command byte needs to be
1128 * saved before this recovery operation, as some controllers seem
1129 * to set the command byte to particular values.
1130 */
1131 test_controller(kbdc);
1132 test_kbd_port(kbdc);
1133
1134 err = get_kbd_echo(kbdc);
1135
1136 /*
1137 * Even if the keyboard doesn't seem to be present (err != 0),
1138 * we shall enable the keyboard port and interrupt so that
1139 * the driver will be operable when the keyboard is attached
1140 * to the system later. It is NOT recommended to hot-plug
1141 * the AT keyboard, but many people do so...
1142 */
1143 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1144 setup_kbd_port(kbdc, TRUE, TRUE);
1145#if 0
1146 if (err == 0) {
1147 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1148 } else {
1149 /* try to restore the command byte as before */
1150 set_controller_command_byte(kbdc, 0xff, c);
1151 kbdc_set_device_mask(kbdc, m);
1152 }
1153#endif
1154
1155 kbdc_lock(kbdc, FALSE);
1156 return err;
1157}
1158
1159static int
1160init_keyboard(KBDC kbdc, int *type, int flags)
1161{
1162 int codeset;
1163 int id;
1164 int c;
1165 int mux_version;
1166 int mux_mask;
1167 int mux_val;
1168
1169 if (!kbdc_lock(kbdc, TRUE)) {
1170 /* driver error? */
1171 return EIO;
1172 }
1173
1174 /* temporarily block data transmission from the keyboard */
1175 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1176
1177#if 0
1178 if (atkbd_setmuxmode(kbdc, 1, &mux_version)) {
1179 kprintf("atkbd: no mux\n");
1180 mux_version = -1;
1181 } else {
1182 kprintf("atkbd: mux present version %d\n", mux_version);
1183 }
1184#else
1185 mux_version = -1;
1186#endif
1187
1188 /* save the current controller command byte */
1189 empty_both_buffers(kbdc, 200);
1190 c = get_controller_command_byte(kbdc);
1191 if (c == -1) {
1192 /* CONTROLLER ERROR */
1193 kbdc_lock(kbdc, FALSE);
1194 kprintf("atkbd: unable to get the current command byte value.\n");
1195 return EIO;
1196 }
1197 if (bootverbose)
1198 kprintf("atkbd: the current kbd controller command byte %04x\n",
1199 c);
1200#if 0
1201 /* override the keyboard lock switch */
1202 c |= KBD_OVERRIDE_KBD_LOCK;
1203#endif
1204
1205 /* enable the keyboard port, but disable the keyboard intr. */
1206 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1207 /* CONTROLLER ERROR: there is very little we can do... */
1208 kprintf("atkbd: unable to set the command byte.\n");
1209 kbdc_lock(kbdc, FALSE);
1210 return EIO;
1211 }
1212
1213 /* default codeset */
1214 codeset = -1;
1215
1216 /* reset keyboard hardware */
1217 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1218 /*
1219 * KEYBOARD ERROR
1220 * Keyboard reset may fail either because the keyboard
1221 * doen't exist, or because the keyboard doesn't pass
1222 * the self-test, or the keyboard controller on the
1223 * motherboard and the keyboard somehow fail to shake hands.
1224 * It is just possible, particularly in the last case,
1225 * that the keyoard controller may be left in a hung state.
1226 * test_controller() and test_kbd_port() appear to bring
1227 * the keyboard controller back (I don't know why and how,
1228 * though.)
1229 */
1230 empty_both_buffers(kbdc, 10);
1231 test_controller(kbdc);
1232 test_kbd_port(kbdc);
1233 /*
1234 * We could disable the keyboard port and interrupt... but,
1235 * the keyboard may still exist (see above).
1236 */
1237 set_controller_command_byte(kbdc, 0xff, c);
1238 kbdc_lock(kbdc, FALSE);
1239 if (bootverbose)
1240 kprintf("atkbd: failed to reset the keyboard.\n");
1241 return EIO;
1242 }
1243
1244 /*
1245 * Check if we have an XT keyboard before we attempt to reset it.
1246 * The procedure assumes that the keyboard and the controller have
1247 * been set up properly by BIOS and have not been messed up
1248 * during the boot process.
1249 */
1250 codeset = -1;
1251 if (flags & KB_CONF_ALT_SCANCODESET)
1252 /* the user says there is a XT keyboard */
1253 codeset = 1;
1254#ifdef KBD_DETECT_XT_KEYBOARD
1255 else if ((c & KBD_TRANSLATION) == 0) {
1256 /* SET_SCANCODE_SET is not always supported; ignore error */
1257 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1258 == KBD_ACK)
1259 codeset = read_kbd_data(kbdc);
1260 }
1261#endif /* KBD_DETECT_XT_KEYBOARD */
1262 if (bootverbose)
1263 kprintf("atkbd: scancode set %d\n", codeset);
1264
1265 /*
1266 * Get the keyboard id.
1267 */
1268 *type = KB_OTHER;
1269 id = get_kbd_id(kbdc, ATKBD_CMD_GETID);
1270 switch(id) {
1271 case 0x41ab: /* 101/102/... Enhanced */
1272 case 0x83ab: /* ditto */
1273 case 0x54ab: /* SpaceSaver */
1274 case 0x84ab: /* ditto */
1275#if 0
1276 case 0x90ab: /* 'G' */
1277 case 0x91ab: /* 'P' */
1278 case 0x92ab: /* 'A' */
1279#endif
1280 *type = KB_101;
1281 break;
1282 case -1: /* AT 84 keyboard doesn't return ID */
1283 *type = KB_84;
1284 break;
1285 default:
1286 break;
1287 }
1288 if (bootverbose)
1289 kprintf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1290
1291 /*
1292 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1293 * such as those on the IBM ThinkPad laptop computers can be used
1294 * with the standard console driver.
1295 */
1296 if (codeset == 1) {
1297 if (send_kbd_command_and_data(kbdc,
1298 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1299 /* XT kbd doesn't need scan code translation */
1300 c &= ~KBD_TRANSLATION;
1301 } else {
1302 /*
1303 * KEYBOARD ERROR
1304 * The XT kbd isn't usable unless the proper scan
1305 * code set is selected.
1306 */
1307 set_controller_command_byte(kbdc, 0xff, c);
1308 kbdc_lock(kbdc, FALSE);
1309 kprintf("atkbd: unable to set the XT keyboard mode.\n");
1310 return EIO;
1311 }
1312 }
1313
1314#if 0
1315 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_EX_ENABLE, 0x71) != KBD_ACK)
1316 kprintf("atkbd: can't CMD_EX_ENABLE\n");
1317
1318 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MB) != KBD_ACK)
1319 kprintf("atkbd: can't SETALL_MB\n");
1320 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MBR) != KBD_ACK)
1321 kprintf("atkbd: can't SETALL_MBR\n");
1322#endif
1323#if 0
1324 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SSCANSET, 2) != KBD_ACK)
1325 kprintf("atkbd: can't SSCANSET\n");
1326 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_GSCANSET, 0) != KBD_ACK)
1327 kprintf("atkbd: can't SSCANSET\n");
1328 else
1329 kprintf("atkbd: scanset %d\n", read_kbd_data(kbdc));
1330#endif
1331#if 0
1332 kprintf("atkbd: id %04x\n", get_kbd_id(kbdc, ATKBD_CMD_OK_GETID));
1333 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK)
1334 kprintf("atkbd: setleds failed\n");
1335 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETREP, 255) != KBD_ACK)
1336 kprintf("atkbd: setrep failed\n");
1337 if (send_kbd_command(kbdc, ATKBD_CMD_RESEND) != KBD_ACK)
1338 kprintf("atkbd: resend failed\n");
1339#endif
1340 /*
1341 * Some keyboards require a SETLEDS command to be sent after
1342 * the reset command before they will send keystrokes to us
1343 * (Acer C720).
1344 */
1345 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK)
1346 kprintf("atkbd: setleds failed\n");
1347 send_kbd_command(kbdc, ATKBD_CMD_ENABLE);
1348
1349#if 0
1350 /* DEBUGGING */
1351 {
1352 int retry;
1353 int c;
1354 kprintf("atkbd: waiting for keypress");
1355 for (retry = 0; retry < 10; ++retry) {
1356 c = read_kbd_data_no_wait(kbdc);
1357 kprintf(" %d", c);
1358 tsleep(&c, 0, "wait", hz);
1359 }
1360 kprintf("\n");
1361 }
1362#endif
1363
1364 if (mux_version == -1) {
1365 mux_mask = 0;
1366 mux_val = 0;
1367 } else {
1368 mux_mask = KBD_AUX_CONTROL_BITS;
1369 mux_val = 0;
1370 kprintf("atkbd: setaux for multiplexer\n");
1371 }
1372
1373 /* enable the keyboard port and intr. */
1374 if (!set_controller_command_byte(kbdc,
1375 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1376 KBD_OVERRIDE_KBD_LOCK | mux_mask,
1377 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1378 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT | mux_val)) {
1379 /*
1380 * CONTROLLER ERROR
1381 * This is serious; we are left with the disabled
1382 * keyboard intr.
1383 */
1384 set_controller_command_byte(kbdc, 0xff, c);
1385 kbdc_lock(kbdc, FALSE);
1386 kprintf("atkbd: unable to enable the keyboard port and intr.\n");
1387 return EIO;
1388 }
1389
1390 kbdc_lock(kbdc, FALSE);
1391 return 0;
1392}
1393
1394#if 0
1395
1396static
1397int
1398atkbd_setmuxmode(KBDC kbdc, int enable, int *mux_version)
1399{
1400 int param;
1401 int val;
1402 int i;
1403
1404 kbdc->mux_active = 0;
1405 empty_both_buffers(kbdc, 100);
1406 val = 0xf0;
1407 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1408 kprintf("setmuxmode: fail1\n");
1409 return(-1);
1410 }
1411 val = enable ? 0x56 : 0xf6;
1412 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1413 kprintf("setmuxmode: fail2\n");
1414 return(-1);
1415 }
1416 val = enable ? 0xa4 : 0xa5;
1417 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1418 kprintf("setmuxmode: fail3\n");
1419 return(-1);
1420 }
1421 kprintf("mux version %02x\n", param);
1422 if (param == 0xac) {
1423 kprintf("setmuxmode: fail4\n");
1424 return(-1);
1425 }
1426
1427 if (enable) {
1428 for (i = 0; i < KBD_NUM_MUX_PORTS; ++i) {
1429 write_controller_command(kbdc, KBDC_MUX_PFX + i);
1430 write_controller_command(kbdc, KBDC_ENABLE_AUX_PORT);
1431
1432 }
1433 }
1434 kbdc->mux_active = 1;
1435 if (mux_version)
1436 *mux_version = param;
1437 return 0;
1438}
1439
1440#endif
1441
1442static int
1443write_kbd(KBDC kbdc, int command, int data)
1444{
1445 /* prevent the timeout routine from polling the keyboard */
1446 if (!kbdc_lock(kbdc, TRUE))
1447 return EBUSY;
1448
1449 /* disable the keyboard and mouse interrupt */
1450 crit_enter();
1451#if 0
1452 c = get_controller_command_byte(kbdc);
1453 if ((c == -1)
1454 || !set_controller_command_byte(kbdc,
1455 kbdc_get_device_mask(kbdc),
1456 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1457 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1458 /* CONTROLLER ERROR */
1459 kbdc_lock(kbdc, FALSE);
1460 crit_exit();
1461 return EIO;
1462 }
1463 /*
1464 * Now that the keyboard controller is told not to generate
1465 * the keyboard and mouse interrupts, call `splx()' to allow
1466 * the other tty interrupts. The clock interrupt may also occur,
1467 * but the timeout routine (`scrn_timer()') will be blocked
1468 * by the lock flag set via `kbdc_lock()'
1469 */
1470 crit_exit();
1471#endif
1472
1473 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1474 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1475
1476#if 0
1477 /* restore the interrupts */
1478 if (!set_controller_command_byte(kbdc,
1479 kbdc_get_device_mask(kbdc),
1480 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1481 /* CONTROLLER ERROR */
1482 }
1483#else
1484 crit_exit();
1485#endif
1486 kbdc_lock(kbdc, FALSE);
1487
1488 return 0;
1489}
1490
1491static int
1492get_kbd_id(KBDC kbdc, int cmd)
1493{
1494 int id1, id2;
1495
1496 empty_both_buffers(kbdc, 10);
1497 id1 = id2 = -1;
1498 if (send_kbd_command(kbdc, cmd) != KBD_ACK)
1499 return -1;
1500
1501 DELAY(10000); /* 10 msec delay */
1502 id1 = read_kbd_data(kbdc);
1503 if (id1 != -1)
1504 id2 = read_kbd_data(kbdc);
1505
1506 if ((id1 == -1) || (id2 == -1)) {
1507 empty_both_buffers(kbdc, 10);
1508 test_controller(kbdc);
1509 test_kbd_port(kbdc);
1510 return -1;
1511 }
1512 return ((id2 << 8) | id1);
1513}
1514
1515static int delays[] = { 250, 500, 750, 1000 };
1516static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1517 68, 76, 84, 92, 100, 110, 118, 126,
1518 136, 152, 168, 184, 200, 220, 236, 252,
1519 272, 304, 336, 368, 400, 440, 472, 504 };
1520
1521static int
1522typematic_delay(int i)
1523{
1524 return delays[(i >> 5) & 3];
1525}
1526
1527static int
1528typematic_rate(int i)
1529{
1530 return rates[i & 0x1f];
1531}
1532
1533static int
1534typematic(int delay, int rate)
1535{
1536 int value;
1537 int i;
1538
1539 for (i = NELEM(delays) - 1; i > 0; --i) {
1540 if (delay >= delays[i])
1541 break;
1542 }
1543 value = i << 5;
1544 for (i = NELEM(rates) - 1; i > 0; --i) {
1545 if (rate >= rates[i])
1546 break;
1547 }
1548 value |= i;
1549 return value;
1550}