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