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