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