2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.2 2003/06/17 04:28:28 dillon Exp $
31 #include "opt_atkbd.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
44 #include <machine/md_var.h>
45 #include <machine/psl.h>
46 #include <machine/vm86.h>
47 #include <machine/pc/bios.h>
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/kbd/atkbdreg.h>
56 #include <dev/kbd/atkbdcreg.h>
58 #include <isa/isareg.h>
60 static timeout_t atkbd_timeout;
63 atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
65 keyboard_switch_t *sw;
69 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
75 error = (*sw->probe)(unit, args, flags);
82 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
84 keyboard_switch_t *sw;
88 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
92 /* reset, initialize and enable the device */
96 error = (*sw->probe)(unit, args, flags);
99 error = (*sw->init)(unit, kbd, args, flags);
104 #ifdef KBD_INSTALL_CDEV
105 /* attach a virtual keyboard cdev */
106 error = kbd_attach(*kbd);
112 * This is a kludge to compensate for lost keyboard interrupts.
113 * A similar code used to be in syscons. See below. XXX
118 (*sw->diag)(*kbd, bootverbose);
123 atkbd_timeout(void *arg)
129 * The original text of the following comments are extracted
130 * from syscons.c (1.287)
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
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.
144 * And here is bde's original comment about this:
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
151 * The keyboard apparently unwedges the irq in most cases.
154 kbd = (keyboard_t *)arg;
155 if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
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.
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);
166 timeout(atkbd_timeout, arg, hz/10);
171 #include <machine/limits.h>
172 #include <machine/clock.h>
174 #define ATKBD_DEFAULT 0
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)
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 */
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;
211 keyboard_switch_t atkbdsw = {
233 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
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);
247 /* local variables */
249 /* the initial key map, accent map and fkey strings */
250 #ifdef ATKBD_DFLT_KEYMAP
251 #define KBD_DFLT_KEYMAP
252 #include "atkbdmap.h"
254 #include <dev/kbd/kbdtables.h>
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];
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!!
273 atkbd_configure(int flags)
279 /* probe the keyboard controller */
282 /* if the driver is disabled, unregister the keyboard if any */
283 if ((resource_int_value("atkbd", ATKBD_DEFAULT, "disabled", &i) == 0)
285 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
287 kbd = kbd_get_keyboard(i);
289 kbd->kb_flags &= ~KB_REGISTERED;
294 /* XXX: a kludge to obtain the device configuration flags */
295 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
298 /* probe the default keyboard */
302 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
304 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
307 /* return the number of found keyboards */
311 /* low-level functions */
313 /* detect a keyboard */
315 atkbd_probe(int unit, void *arg, int flags)
318 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
321 if (unit == ATKBD_DEFAULT) {
322 if (KBD_IS_PROBED(&default_kbd))
326 kbdc = atkbdc_open(data[0]);
329 if (probe_keyboard(kbdc, flags)) {
330 if (flags & KB_CONF_FAIL_IF_NO_KBD)
336 /* reset and initialize the device */
338 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
341 atkbd_state_t *state;
347 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
350 if (unit == ATKBD_DEFAULT) {
351 *kbdp = kbd = &default_kbd;
352 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
354 state = &default_kbd_state;
355 keymap = &default_keymap;
356 accmap = &default_accentmap;
357 fkeymap = default_fkeytab;
359 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
360 } else if (*kbdp == NULL) {
361 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT);
364 bzero(kbd, sizeof(*kbd));
365 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT);
366 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
367 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
368 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
369 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
370 if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
371 || (fkeymap == NULL)) {
373 free(state, M_DEVBUF);
375 free(keymap, M_DEVBUF);
377 free(accmap, M_DEVBUF);
379 free(fkeymap, M_DEVBUF);
383 bzero(state, sizeof(*state));
384 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
388 state = (atkbd_state_t *)kbd->kb_data;
389 bzero(state, sizeof(*state));
390 keymap = kbd->kb_keymap;
391 accmap = kbd->kb_accentmap;
392 fkeymap = kbd->kb_fkeytab;
393 fkeymap_size = kbd->kb_fkeytab_size;
396 if (!KBD_IS_PROBED(kbd)) {
397 state->kbdc = atkbdc_open(data[0]);
398 if (state->kbdc == NULL)
400 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
402 bcopy(&key_map, keymap, sizeof(key_map));
403 bcopy(&accent_map, accmap, sizeof(accent_map));
404 bcopy(fkey_tab, fkeymap,
405 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
406 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
407 kbd->kb_data = (void *)state;
409 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
410 if (flags & KB_CONF_FAIL_IF_NO_KBD)
413 KBD_FOUND_DEVICE(kbd);
415 atkbd_clear_state(kbd);
416 state->ks_mode = K_XLATE;
418 * FIXME: set the initial value for lock keys in ks_state
419 * according to the BIOS data?
423 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
424 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
425 if (KBD_HAS_DEVICE(kbd)
426 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
427 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
429 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
431 delay[0] = kbd->kb_delay1;
432 delay[1] = kbd->kb_delay2;
433 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
436 if (!KBD_IS_CONFIGURED(kbd)) {
437 if (kbd_register(kbd) < 0)
439 KBD_CONFIG_DONE(kbd);
445 /* finish using this keyboard */
447 atkbd_term(keyboard_t *kbd)
453 /* keyboard interrupt routine */
455 atkbd_intr(keyboard_t *kbd, void *arg)
457 atkbd_state_t *state;
461 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
462 /* let the callback function to process the input */
463 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
464 kbd->kb_callback.kc_arg);
466 /* read and discard the input; no one is waiting for input */
468 c = atkbd_read_char(kbd, FALSE);
469 } while (c != NOKEY);
471 if (!KBD_HAS_DEVICE(kbd)) {
473 * The keyboard was not detected before;
474 * it must have been reconnected!
476 state = (atkbd_state_t *)kbd->kb_data;
477 init_keyboard(state->kbdc, &kbd->kb_type,
479 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
481 delay[0] = kbd->kb_delay1;
482 delay[1] = kbd->kb_delay2;
483 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
484 KBD_FOUND_DEVICE(kbd);
490 /* test the interface to the device */
492 atkbd_test_if(keyboard_t *kbd)
498 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
500 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
502 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
510 * Enable the access to the device; until this function is called,
511 * the client cannot read from the keyboard.
514 atkbd_enable(keyboard_t *kbd)
524 /* disallow the access to the device */
526 atkbd_disable(keyboard_t *kbd)
536 /* read one byte from the keyboard if it's allowed */
538 atkbd_read(keyboard_t *kbd, int wait)
543 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
545 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
548 return (KBD_IS_ACTIVE(kbd) ? c : -1);
551 /* check if data is waiting */
553 atkbd_check(keyboard_t *kbd)
555 if (!KBD_IS_ACTIVE(kbd))
557 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
560 /* read char from the keyboard */
562 atkbd_read_char(keyboard_t *kbd, int wait)
564 atkbd_state_t *state;
569 state = (atkbd_state_t *)kbd->kb_data;
571 /* do we have a composed char to return? */
572 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
573 action = state->ks_composed_char;
574 state->ks_composed_char = 0;
575 if (action > UCHAR_MAX)
580 /* see if there is something in the keyboard port */
583 scancode = read_kbd_data(state->kbdc);
584 } while (scancode == -1);
586 scancode = read_kbd_data_no_wait(state->kbdc);
592 #if KBDIO_DEBUG >= 10
593 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
596 /* return the byte as is for the K_RAW mode */
597 if (state->ks_mode == K_RAW)
600 /* translate the scan code into a keycode */
601 keycode = scancode & 0x7F;
602 switch (state->ks_prefix) {
603 case 0x00: /* normal scancode */
605 case 0xB8: /* left alt (compose key) released */
606 if (state->ks_flags & COMPOSE) {
607 state->ks_flags &= ~COMPOSE;
608 if (state->ks_composed_char > UCHAR_MAX)
609 state->ks_composed_char = 0;
612 case 0x38: /* left alt (compose key) pressed */
613 if (!(state->ks_flags & COMPOSE)) {
614 state->ks_flags |= COMPOSE;
615 state->ks_composed_char = 0;
620 state->ks_prefix = scancode;
624 case 0xE0: /* 0xE0 prefix */
625 state->ks_prefix = 0;
627 case 0x1C: /* right enter key */
630 case 0x1D: /* right ctrl key */
633 case 0x35: /* keypad divide key */
636 case 0x37: /* print scrn key */
639 case 0x38: /* right alt key (alt gr) */
642 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
645 case 0x47: /* grey home key */
648 case 0x48: /* grey up arrow key */
651 case 0x49: /* grey page up key */
654 case 0x4B: /* grey left arrow key */
657 case 0x4D: /* grey right arrow key */
660 case 0x4F: /* grey end key */
663 case 0x50: /* grey down arrow key */
666 case 0x51: /* grey page down key */
669 case 0x52: /* grey insert key */
672 case 0x53: /* grey delete key */
675 /* the following 3 are only used on the MS "Natural" keyboard */
676 case 0x5b: /* left Window key */
679 case 0x5c: /* right Window key */
682 case 0x5d: /* menu key */
685 default: /* ignore everything else */
689 case 0xE1: /* 0xE1 prefix */
691 * The pause/break key on the 101 keyboard produces:
693 * Ctrl-pause/break produces:
694 * E0-46 E0-C6 (See above.)
696 state->ks_prefix = 0;
698 state->ks_prefix = 0x1D;
701 case 0x1D: /* pause / break */
702 state->ks_prefix = 0;
709 if (kbd->kb_type == KB_84) {
711 case 0x37: /* *(numpad)/print screen */
712 if (state->ks_flags & SHIFTS)
713 keycode = 0x5c; /* print screen */
715 case 0x45: /* num lock/pause */
716 if (state->ks_flags & CTLS)
717 keycode = 0x68; /* pause */
719 case 0x46: /* scroll lock/break */
720 if (state->ks_flags & CTLS)
721 keycode = 0x6c; /* break */
724 } else if (kbd->kb_type == KB_101) {
726 case 0x5c: /* print screen */
727 if (state->ks_flags & ALTS)
728 keycode = 0x54; /* sysrq */
730 case 0x68: /* pause/break */
731 if (state->ks_flags & CTLS)
732 keycode = 0x6c; /* break */
737 /* return the key code in the K_CODE mode */
738 if (state->ks_mode == K_CODE)
739 return (keycode | (scancode & 0x80));
741 /* compose a character code */
742 if (state->ks_flags & COMPOSE) {
743 switch (keycode | (scancode & 0x80)) {
744 /* key pressed, process it */
745 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
746 state->ks_composed_char *= 10;
747 state->ks_composed_char += keycode - 0x40;
748 if (state->ks_composed_char > UCHAR_MAX)
751 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
752 state->ks_composed_char *= 10;
753 state->ks_composed_char += keycode - 0x47;
754 if (state->ks_composed_char > UCHAR_MAX)
757 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
758 state->ks_composed_char *= 10;
759 state->ks_composed_char += keycode - 0x4E;
760 if (state->ks_composed_char > UCHAR_MAX)
763 case 0x52: /* keypad 0 */
764 state->ks_composed_char *= 10;
765 if (state->ks_composed_char > UCHAR_MAX)
769 /* key released, no interest here */
770 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
771 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
772 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
773 case 0xD2: /* keypad 0 */
776 case 0x38: /* left alt key */
780 if (state->ks_composed_char > 0) {
781 state->ks_flags &= ~COMPOSE;
782 state->ks_composed_char = 0;
789 /* keycode to key action */
790 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
791 &state->ks_state, &state->ks_accents);
798 /* check if char is waiting */
800 atkbd_check_char(keyboard_t *kbd)
802 atkbd_state_t *state;
804 if (!KBD_IS_ACTIVE(kbd))
806 state = (atkbd_state_t *)kbd->kb_data;
807 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
809 return kbdc_data_ready(state->kbdc);
812 /* some useful control functions */
814 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
816 /* trasnlate LED_XXX bits into the device specific bits */
817 static u_char ledmap[8] = {
818 0, 4, 2, 6, 1, 5, 3, 7,
820 atkbd_state_t *state = kbd->kb_data;
828 case KDGKBMODE: /* get keyboard mode */
829 *(int *)arg = state->ks_mode;
831 case KDSKBMODE: /* set keyboard mode */
832 switch (*(int *)arg) {
834 if (state->ks_mode != K_XLATE) {
835 /* make lock key state and LED state match */
836 state->ks_state &= ~LOCK_MASK;
837 state->ks_state |= KBD_LED_VAL(kbd);
842 if (state->ks_mode != *(int *)arg) {
843 atkbd_clear_state(kbd);
844 state->ks_mode = *(int *)arg;
853 case KDGETLED: /* get keyboard LED */
854 *(int *)arg = KBD_LED_VAL(kbd);
856 case KDSETLED: /* set keyboard LED */
857 /* NOTE: lock key state in ks_state won't be changed */
858 if (*(int *)arg & ~LOCK_MASK) {
863 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
864 if (state->ks_mode == K_XLATE &&
865 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
871 if (KBD_HAS_DEVICE(kbd)) {
872 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
873 ledmap[i & LED_MASK]);
879 KBD_LED_VAL(kbd) = *(int *)arg;
882 case KDGKBSTATE: /* get lock key state */
883 *(int *)arg = state->ks_state & LOCK_MASK;
885 case KDSKBSTATE: /* set lock key state */
886 if (*(int *)arg & ~LOCK_MASK) {
890 state->ks_state &= ~LOCK_MASK;
891 state->ks_state |= *(int *)arg;
893 /* set LEDs and quit */
894 return atkbd_ioctl(kbd, KDSETLED, arg);
896 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
898 if (!KBD_HAS_DEVICE(kbd))
900 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
901 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
903 kbd->kb_delay1 = typematic_delay(i);
904 kbd->kb_delay2 = typematic_rate(i);
908 case KDSETRAD: /* set keyboard repeat rate (old interface) */
910 if (!KBD_HAS_DEVICE(kbd))
912 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
914 kbd->kb_delay1 = typematic_delay(*(int *)arg);
915 kbd->kb_delay2 = typematic_rate(*(int *)arg);
919 case PIO_KEYMAP: /* set keyboard translation table */
920 case PIO_KEYMAPENT: /* set keyboard translation table entry */
921 case PIO_DEADKEYMAP: /* set accent key translation table */
922 state->ks_accents = 0;
926 return genkbd_commonioctl(kbd, cmd, arg);
933 /* lock the access to the keyboard */
935 atkbd_lock(keyboard_t *kbd, int lock)
937 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
940 /* clear the internal state of the keyboard */
942 atkbd_clear_state(keyboard_t *kbd)
944 atkbd_state_t *state;
946 state = (atkbd_state_t *)kbd->kb_data;
948 state->ks_polling = 0;
949 state->ks_state &= LOCK_MASK; /* preserve locking key state */
950 state->ks_accents = 0;
951 state->ks_composed_char = 0;
953 state->ks_prefix = 0; /* XXX */
957 /* save the internal state */
959 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
962 return sizeof(atkbd_state_t);
963 if (len < sizeof(atkbd_state_t))
965 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
969 /* set the internal state */
971 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
973 if (len < sizeof(atkbd_state_t))
975 if (((atkbd_state_t *)kbd->kb_data)->kbdc
976 != ((atkbd_state_t *)buf)->kbdc)
978 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
983 atkbd_poll(keyboard_t *kbd, int on)
985 atkbd_state_t *state;
988 state = (atkbd_state_t *)kbd->kb_data;
998 /* local functions */
1001 get_typematic(keyboard_t *kbd)
1005 * Only some systems allow us to retrieve the keyboard repeat
1006 * rate previously set via the BIOS...
1008 struct vm86frame vmf;
1011 bzero(&vmf, sizeof(vmf));
1012 vmf.vmf_ax = 0xc000;
1013 vm86_intcall(0x15, &vmf);
1014 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
1016 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
1017 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
1019 vmf.vmf_ax = 0x0900;
1020 vm86_intcall(0x16, &vmf);
1021 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1023 vmf.vmf_ax = 0x0306;
1024 vm86_intcall(0x16, &vmf);
1025 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1026 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1030 #endif /* __i386__ */
1034 setup_kbd_port(KBDC kbdc, int port, int intr)
1036 if (!set_controller_command_byte(kbdc,
1037 KBD_KBD_CONTROL_BITS,
1038 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1039 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1045 get_kbd_echo(KBDC kbdc)
1047 /* enable the keyboard port, but disable the keyboard intr. */
1048 if (setup_kbd_port(kbdc, TRUE, FALSE))
1049 /* CONTROLLER ERROR: there is very little we can do... */
1052 /* see if something is present */
1053 write_kbd_command(kbdc, KBDC_ECHO);
1054 if (read_kbd_data(kbdc) != KBD_ECHO) {
1055 empty_both_buffers(kbdc, 10);
1056 test_controller(kbdc);
1057 test_kbd_port(kbdc);
1061 /* enable the keyboard port and intr. */
1062 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1065 * This is serious; the keyboard intr is left disabled!
1074 probe_keyboard(KBDC kbdc, int flags)
1077 * Don't try to print anything in this function. The low-level
1078 * console may not have been initialized yet...
1084 if (!kbdc_lock(kbdc, TRUE)) {
1089 /* temporarily block data transmission from the keyboard */
1090 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1092 /* flush any noise in the buffer */
1093 empty_both_buffers(kbdc, 100);
1095 /* save the current keyboard controller command byte */
1096 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1097 c = get_controller_command_byte(kbdc);
1099 /* CONTROLLER ERROR */
1100 kbdc_set_device_mask(kbdc, m);
1101 kbdc_lock(kbdc, FALSE);
1106 * The keyboard may have been screwed up by the boot block.
1107 * We may just be able to recover from error by testing the controller
1108 * and the keyboard port. The controller command byte needs to be
1109 * saved before this recovery operation, as some controllers seem
1110 * to set the command byte to particular values.
1112 test_controller(kbdc);
1113 test_kbd_port(kbdc);
1115 err = get_kbd_echo(kbdc);
1118 * Even if the keyboard doesn't seem to be present (err != 0),
1119 * we shall enable the keyboard port and interrupt so that
1120 * the driver will be operable when the keyboard is attached
1121 * to the system later. It is NOT recommended to hot-plug
1122 * the AT keyboard, but many people do so...
1124 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1125 setup_kbd_port(kbdc, TRUE, TRUE);
1128 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1130 /* try to restore the command byte as before */
1131 set_controller_command_byte(kbdc, 0xff, c);
1132 kbdc_set_device_mask(kbdc, m);
1136 kbdc_lock(kbdc, FALSE);
1141 init_keyboard(KBDC kbdc, int *type, int flags)
1147 if (!kbdc_lock(kbdc, TRUE)) {
1152 /* temporarily block data transmission from the keyboard */
1153 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1155 /* save the current controller command byte */
1156 empty_both_buffers(kbdc, 200);
1157 c = get_controller_command_byte(kbdc);
1159 /* CONTROLLER ERROR */
1160 kbdc_lock(kbdc, FALSE);
1161 printf("atkbd: unable to get the current command byte value.\n");
1165 printf("atkbd: the current kbd controller command byte %04x\n",
1168 /* override the keyboard lock switch */
1169 c |= KBD_OVERRIDE_KBD_LOCK;
1172 /* enable the keyboard port, but disable the keyboard intr. */
1173 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1174 /* CONTROLLER ERROR: there is very little we can do... */
1175 printf("atkbd: unable to set the command byte.\n");
1176 kbdc_lock(kbdc, FALSE);
1181 * Check if we have an XT keyboard before we attempt to reset it.
1182 * The procedure assumes that the keyboard and the controller have
1183 * been set up properly by BIOS and have not been messed up
1184 * during the boot process.
1187 if (flags & KB_CONF_ALT_SCANCODESET)
1188 /* the user says there is a XT keyboard */
1190 #ifdef KBD_DETECT_XT_KEYBOARD
1191 else if ((c & KBD_TRANSLATION) == 0) {
1192 /* SET_SCANCODE_SET is not always supported; ignore error */
1193 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1195 codeset = read_kbd_data(kbdc);
1198 printf("atkbd: scancode set %d\n", codeset);
1199 #endif /* KBD_DETECT_XT_KEYBOARD */
1202 id = get_kbd_id(kbdc);
1204 case 0x41ab: /* 101/102/... Enhanced */
1205 case 0x83ab: /* ditto */
1206 case 0x54ab: /* SpaceSaver */
1207 case 0x84ab: /* ditto */
1209 case 0x90ab: /* 'G' */
1210 case 0x91ab: /* 'P' */
1211 case 0x92ab: /* 'A' */
1215 case -1: /* AT 84 keyboard doesn't return ID */
1222 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1224 /* reset keyboard hardware */
1225 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1228 * Keyboard reset may fail either because the keyboard
1229 * doen't exist, or because the keyboard doesn't pass
1230 * the self-test, or the keyboard controller on the
1231 * motherboard and the keyboard somehow fail to shake hands.
1232 * It is just possible, particularly in the last case,
1233 * that the keyoard controller may be left in a hung state.
1234 * test_controller() and test_kbd_port() appear to bring
1235 * the keyboard controller back (I don't know why and how,
1238 empty_both_buffers(kbdc, 10);
1239 test_controller(kbdc);
1240 test_kbd_port(kbdc);
1242 * We could disable the keyboard port and interrupt... but,
1243 * the keyboard may still exist (see above).
1245 set_controller_command_byte(kbdc, 0xff, c);
1246 kbdc_lock(kbdc, FALSE);
1248 printf("atkbd: failed to reset the keyboard.\n");
1253 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1254 * such as those on the IBM ThinkPad laptop computers can be used
1255 * with the standard console driver.
1258 if (send_kbd_command_and_data(kbdc,
1259 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1260 /* XT kbd doesn't need scan code translation */
1261 c &= ~KBD_TRANSLATION;
1265 * The XT kbd isn't usable unless the proper scan
1266 * code set is selected.
1268 set_controller_command_byte(kbdc, 0xff, c);
1269 kbdc_lock(kbdc, FALSE);
1270 printf("atkbd: unable to set the XT keyboard mode.\n");
1276 if (send_kbd_command_and_data(
1277 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1278 printf("atkbd: can't set translation.\n");
1281 c |= KBD_TRANSLATION;
1284 /* enable the keyboard port and intr. */
1285 if (!set_controller_command_byte(kbdc,
1286 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1287 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1288 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1291 * This is serious; we are left with the disabled
1294 set_controller_command_byte(kbdc, 0xff, c);
1295 kbdc_lock(kbdc, FALSE);
1296 printf("atkbd: unable to enable the keyboard port and intr.\n");
1300 kbdc_lock(kbdc, FALSE);
1305 write_kbd(KBDC kbdc, int command, int data)
1309 /* prevent the timeout routine from polling the keyboard */
1310 if (!kbdc_lock(kbdc, TRUE))
1313 /* disable the keyboard and mouse interrupt */
1316 c = get_controller_command_byte(kbdc);
1318 || !set_controller_command_byte(kbdc,
1319 kbdc_get_device_mask(kbdc),
1320 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1321 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1322 /* CONTROLLER ERROR */
1323 kbdc_lock(kbdc, FALSE);
1328 * Now that the keyboard controller is told not to generate
1329 * the keyboard and mouse interrupts, call `splx()' to allow
1330 * the other tty interrupts. The clock interrupt may also occur,
1331 * but the timeout routine (`scrn_timer()') will be blocked
1332 * by the lock flag set via `kbdc_lock()'
1337 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1338 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1341 /* restore the interrupts */
1342 if (!set_controller_command_byte(kbdc,
1343 kbdc_get_device_mask(kbdc),
1344 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1345 /* CONTROLLER ERROR */
1350 kbdc_lock(kbdc, FALSE);
1356 get_kbd_id(KBDC kbdc)
1360 empty_both_buffers(kbdc, 10);
1362 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1365 DELAY(10000); /* 10 msec delay */
1366 id1 = read_kbd_data(kbdc);
1368 id2 = read_kbd_data(kbdc);
1370 if ((id1 == -1) || (id2 == -1)) {
1371 empty_both_buffers(kbdc, 10);
1372 test_controller(kbdc);
1373 test_kbd_port(kbdc);
1376 return ((id2 << 8) | id1);
1379 static int delays[] = { 250, 500, 750, 1000 };
1380 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1381 68, 76, 84, 92, 100, 110, 118, 126,
1382 136, 152, 168, 184, 200, 220, 236, 252,
1383 272, 304, 336, 368, 400, 440, 472, 504 };
1386 typematic_delay(int i)
1388 return delays[(i >> 5) & 3];
1392 typematic_rate(int i)
1394 return rates[i & 0x1f];
1398 typematic(int delay, int rate)
1403 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1404 if (delay >= delays[i])
1408 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1409 if (rate >= rates[i])