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.8 2005/05/07 17:38:33 swildner 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>
56 #include "atkbdcreg.h"
58 #include <bus/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 callout_reset(&kbd->kb_atkbd_timeout_ch, hz / 10, atkbd_timeout, arg);
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 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
179 int ks_flags; /* flags */
180 #define COMPOSE (1 << 0)
182 int ks_state; /* shift/lock key state */
183 int ks_accents; /* accent key index (> 0) */
184 u_int ks_composed_char; /* composed char code (> 0) */
185 u_char ks_prefix; /* AT scan code prefix */
188 /* keyboard driver declaration */
189 static int atkbd_configure(int flags);
190 static kbd_probe_t atkbd_probe;
191 static kbd_init_t atkbd_init;
192 static kbd_term_t atkbd_term;
193 static kbd_intr_t atkbd_intr;
194 static kbd_test_if_t atkbd_test_if;
195 static kbd_enable_t atkbd_enable;
196 static kbd_disable_t atkbd_disable;
197 static kbd_read_t atkbd_read;
198 static kbd_check_t atkbd_check;
199 static kbd_read_char_t atkbd_read_char;
200 static kbd_check_char_t atkbd_check_char;
201 static kbd_ioctl_t atkbd_ioctl;
202 static kbd_lock_t atkbd_lock;
203 static kbd_clear_state_t atkbd_clear_state;
204 static kbd_get_state_t atkbd_get_state;
205 static kbd_set_state_t atkbd_set_state;
206 static kbd_poll_mode_t atkbd_poll;
208 keyboard_switch_t atkbdsw = {
230 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
232 /* local functions */
233 static int get_typematic(keyboard_t *kbd);
234 static int setup_kbd_port(KBDC kbdc, int port, int intr);
235 static int get_kbd_echo(KBDC kbdc);
236 static int probe_keyboard(KBDC kbdc, int flags);
237 static int init_keyboard(KBDC kbdc, int *type, int flags);
238 static int write_kbd(KBDC kbdc, int command, int data);
239 static int get_kbd_id(KBDC kbdc);
240 static int typematic(int delay, int rate);
241 static int typematic_delay(int delay);
242 static int typematic_rate(int rate);
244 /* local variables */
246 /* the initial key map, accent map and fkey strings */
247 #ifdef ATKBD_DFLT_KEYMAP
248 #define KBD_DFLT_KEYMAP
249 #include "atkbdmap.h"
251 #include "kbdtables.h"
253 /* structures for the default keyboard */
254 static keyboard_t default_kbd;
255 static atkbd_state_t default_kbd_state;
256 static keymap_t default_keymap;
257 static accentmap_t default_accentmap;
258 static fkeytab_t default_fkeytab[NUM_FKEYS];
261 * The back door to the keyboard driver!
262 * This function is called by the console driver, via the kbdio module,
263 * to tickle keyboard drivers when the low-level console is being initialized.
264 * Almost nothing in the kernel has been initialied yet. Try to probe
265 * keyboards if possible.
266 * NOTE: because of the way the low-level console is initialized, this routine
267 * may be called more than once!!
270 atkbd_configure(int flags)
276 /* probe the keyboard controller */
279 /* if the driver is disabled, unregister the keyboard if any */
280 if ((resource_int_value("atkbd", ATKBD_DEFAULT, "disabled", &i) == 0)
282 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
284 kbd = kbd_get_keyboard(i);
286 kbd->kb_flags &= ~KB_REGISTERED;
291 /* XXX: a kludge to obtain the device configuration flags */
292 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
295 /* probe the default keyboard */
299 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
301 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
304 /* return the number of found keyboards */
308 /* low-level functions */
310 /* detect a keyboard */
312 atkbd_probe(int unit, void *arg, int flags)
315 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
317 if (unit == ATKBD_DEFAULT) {
318 if (KBD_IS_PROBED(&default_kbd))
322 kbdc = atkbdc_open(data[0]);
325 if (probe_keyboard(kbdc, flags)) {
326 if (flags & KB_CONF_FAIL_IF_NO_KBD)
332 /* reset and initialize the device */
334 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
337 atkbd_state_t *state;
343 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
346 if (unit == ATKBD_DEFAULT) {
347 *kbdp = kbd = &default_kbd;
348 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
350 state = &default_kbd_state;
351 keymap = &default_keymap;
352 accmap = &default_accentmap;
353 fkeymap = default_fkeytab;
355 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
356 } else if (*kbdp == NULL) {
357 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO);
358 state = malloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO);
359 keymap = malloc(sizeof(key_map), M_DEVBUF, M_WAITOK);
360 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_WAITOK);
361 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK);
362 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
363 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
367 state = (atkbd_state_t *)kbd->kb_data;
368 bzero(state, sizeof(*state));
369 keymap = kbd->kb_keymap;
370 accmap = kbd->kb_accentmap;
371 fkeymap = kbd->kb_fkeytab;
372 fkeymap_size = kbd->kb_fkeytab_size;
375 if (!KBD_IS_PROBED(kbd)) {
376 state->kbdc = atkbdc_open(data[0]);
377 if (state->kbdc == NULL)
379 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
381 bcopy(&key_map, keymap, sizeof(key_map));
382 bcopy(&accent_map, accmap, sizeof(accent_map));
383 bcopy(fkey_tab, fkeymap,
384 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
385 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
386 kbd->kb_data = (void *)state;
388 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
389 if (flags & KB_CONF_FAIL_IF_NO_KBD)
392 KBD_FOUND_DEVICE(kbd);
394 atkbd_clear_state(kbd);
395 state->ks_mode = K_XLATE;
397 * FIXME: set the initial value for lock keys in ks_state
398 * according to the BIOS data?
402 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
403 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
404 if (KBD_HAS_DEVICE(kbd)
405 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
406 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
408 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
410 delay[0] = kbd->kb_delay1;
411 delay[1] = kbd->kb_delay2;
412 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
415 if (!KBD_IS_CONFIGURED(kbd)) {
416 if (kbd_register(kbd) < 0)
418 KBD_CONFIG_DONE(kbd);
424 /* finish using this keyboard */
426 atkbd_term(keyboard_t *kbd)
432 /* keyboard interrupt routine */
434 atkbd_intr(keyboard_t *kbd, void *arg)
436 atkbd_state_t *state;
440 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
441 /* let the callback function to process the input */
442 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
443 kbd->kb_callback.kc_arg);
445 /* read and discard the input; no one is waiting for input */
447 c = atkbd_read_char(kbd, FALSE);
448 } while (c != NOKEY);
450 if (!KBD_HAS_DEVICE(kbd)) {
452 * The keyboard was not detected before;
453 * it must have been reconnected!
455 state = (atkbd_state_t *)kbd->kb_data;
456 init_keyboard(state->kbdc, &kbd->kb_type,
458 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
460 delay[0] = kbd->kb_delay1;
461 delay[1] = kbd->kb_delay2;
462 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
463 KBD_FOUND_DEVICE(kbd);
469 /* test the interface to the device */
471 atkbd_test_if(keyboard_t *kbd)
477 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
479 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
481 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
489 * Enable the access to the device; until this function is called,
490 * the client cannot read from the keyboard.
493 atkbd_enable(keyboard_t *kbd)
503 /* disallow the access to the device */
505 atkbd_disable(keyboard_t *kbd)
515 /* read one byte from the keyboard if it's allowed */
517 atkbd_read(keyboard_t *kbd, int wait)
522 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
524 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
527 return (KBD_IS_ACTIVE(kbd) ? c : -1);
530 /* check if data is waiting */
532 atkbd_check(keyboard_t *kbd)
534 if (!KBD_IS_ACTIVE(kbd))
536 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
539 /* read char from the keyboard */
541 atkbd_read_char(keyboard_t *kbd, int wait)
543 atkbd_state_t *state;
548 state = (atkbd_state_t *)kbd->kb_data;
550 /* do we have a composed char to return? */
551 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
552 action = state->ks_composed_char;
553 state->ks_composed_char = 0;
554 if (action > UCHAR_MAX)
559 /* see if there is something in the keyboard port */
562 scancode = read_kbd_data(state->kbdc);
563 } while (scancode == -1);
565 scancode = read_kbd_data_no_wait(state->kbdc);
571 #if KBDIO_DEBUG >= 10
572 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
575 /* return the byte as is for the K_RAW mode */
576 if (state->ks_mode == K_RAW)
579 /* translate the scan code into a keycode */
580 keycode = scancode & 0x7F;
581 switch (state->ks_prefix) {
582 case 0x00: /* normal scancode */
584 case 0xB8: /* left alt (compose key) released */
585 if (state->ks_flags & COMPOSE) {
586 state->ks_flags &= ~COMPOSE;
587 if (state->ks_composed_char > UCHAR_MAX)
588 state->ks_composed_char = 0;
591 case 0x38: /* left alt (compose key) pressed */
592 if (!(state->ks_flags & COMPOSE)) {
593 state->ks_flags |= COMPOSE;
594 state->ks_composed_char = 0;
599 state->ks_prefix = scancode;
603 case 0xE0: /* 0xE0 prefix */
604 state->ks_prefix = 0;
606 case 0x1C: /* right enter key */
609 case 0x1D: /* right ctrl key */
612 case 0x35: /* keypad divide key */
615 case 0x37: /* print scrn key */
618 case 0x38: /* right alt key (alt gr) */
621 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
624 case 0x47: /* grey home key */
627 case 0x48: /* grey up arrow key */
630 case 0x49: /* grey page up key */
633 case 0x4B: /* grey left arrow key */
636 case 0x4D: /* grey right arrow key */
639 case 0x4F: /* grey end key */
642 case 0x50: /* grey down arrow key */
645 case 0x51: /* grey page down key */
648 case 0x52: /* grey insert key */
651 case 0x53: /* grey delete key */
654 /* the following 3 are only used on the MS "Natural" keyboard */
655 case 0x5b: /* left Window key */
658 case 0x5c: /* right Window key */
661 case 0x5d: /* menu key */
664 default: /* ignore everything else */
668 case 0xE1: /* 0xE1 prefix */
670 * The pause/break key on the 101 keyboard produces:
672 * Ctrl-pause/break produces:
673 * E0-46 E0-C6 (See above.)
675 state->ks_prefix = 0;
677 state->ks_prefix = 0x1D;
680 case 0x1D: /* pause / break */
681 state->ks_prefix = 0;
688 if (kbd->kb_type == KB_84) {
690 case 0x37: /* *(numpad)/print screen */
691 if (state->ks_flags & SHIFTS)
692 keycode = 0x5c; /* print screen */
694 case 0x45: /* num lock/pause */
695 if (state->ks_flags & CTLS)
696 keycode = 0x68; /* pause */
698 case 0x46: /* scroll lock/break */
699 if (state->ks_flags & CTLS)
700 keycode = 0x6c; /* break */
703 } else if (kbd->kb_type == KB_101) {
705 case 0x5c: /* print screen */
706 if (state->ks_flags & ALTS)
707 keycode = 0x54; /* sysrq */
709 case 0x68: /* pause/break */
710 if (state->ks_flags & CTLS)
711 keycode = 0x6c; /* break */
716 /* return the key code in the K_CODE mode */
717 if (state->ks_mode == K_CODE)
718 return (keycode | (scancode & 0x80));
720 /* compose a character code */
721 if (state->ks_flags & COMPOSE) {
722 switch (keycode | (scancode & 0x80)) {
723 /* key pressed, process it */
724 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
725 state->ks_composed_char *= 10;
726 state->ks_composed_char += keycode - 0x40;
727 if (state->ks_composed_char > UCHAR_MAX)
730 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
731 state->ks_composed_char *= 10;
732 state->ks_composed_char += keycode - 0x47;
733 if (state->ks_composed_char > UCHAR_MAX)
736 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
737 state->ks_composed_char *= 10;
738 state->ks_composed_char += keycode - 0x4E;
739 if (state->ks_composed_char > UCHAR_MAX)
742 case 0x52: /* keypad 0 */
743 state->ks_composed_char *= 10;
744 if (state->ks_composed_char > UCHAR_MAX)
748 /* key released, no interest here */
749 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
750 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
751 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
752 case 0xD2: /* keypad 0 */
755 case 0x38: /* left alt key */
759 if (state->ks_composed_char > 0) {
760 state->ks_flags &= ~COMPOSE;
761 state->ks_composed_char = 0;
768 /* keycode to key action */
769 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
770 &state->ks_state, &state->ks_accents);
777 /* check if char is waiting */
779 atkbd_check_char(keyboard_t *kbd)
781 atkbd_state_t *state;
783 if (!KBD_IS_ACTIVE(kbd))
785 state = (atkbd_state_t *)kbd->kb_data;
786 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
788 return kbdc_data_ready(state->kbdc);
791 /* some useful control functions */
793 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
795 /* trasnlate LED_XXX bits into the device specific bits */
796 static u_char ledmap[8] = {
797 0, 4, 2, 6, 1, 5, 3, 7,
799 atkbd_state_t *state = kbd->kb_data;
807 case KDGKBMODE: /* get keyboard mode */
808 *(int *)arg = state->ks_mode;
810 case KDSKBMODE: /* set keyboard mode */
811 switch (*(int *)arg) {
813 if (state->ks_mode != K_XLATE) {
814 /* make lock key state and LED state match */
815 state->ks_state &= ~LOCK_MASK;
816 state->ks_state |= KBD_LED_VAL(kbd);
821 if (state->ks_mode != *(int *)arg) {
822 atkbd_clear_state(kbd);
823 state->ks_mode = *(int *)arg;
832 case KDGETLED: /* get keyboard LED */
833 *(int *)arg = KBD_LED_VAL(kbd);
835 case KDSETLED: /* set keyboard LED */
836 /* NOTE: lock key state in ks_state won't be changed */
837 if (*(int *)arg & ~LOCK_MASK) {
842 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
843 if (state->ks_mode == K_XLATE &&
844 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
850 if (KBD_HAS_DEVICE(kbd)) {
851 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
852 ledmap[i & LED_MASK]);
858 KBD_LED_VAL(kbd) = *(int *)arg;
861 case KDGKBSTATE: /* get lock key state */
862 *(int *)arg = state->ks_state & LOCK_MASK;
864 case KDSKBSTATE: /* set lock key state */
865 if (*(int *)arg & ~LOCK_MASK) {
869 state->ks_state &= ~LOCK_MASK;
870 state->ks_state |= *(int *)arg;
872 /* set LEDs and quit */
873 return atkbd_ioctl(kbd, KDSETLED, arg);
875 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
877 if (!KBD_HAS_DEVICE(kbd))
879 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
880 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
882 kbd->kb_delay1 = typematic_delay(i);
883 kbd->kb_delay2 = typematic_rate(i);
887 case KDSETRAD: /* set keyboard repeat rate (old interface) */
889 if (!KBD_HAS_DEVICE(kbd))
891 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
893 kbd->kb_delay1 = typematic_delay(*(int *)arg);
894 kbd->kb_delay2 = typematic_rate(*(int *)arg);
898 case PIO_KEYMAP: /* set keyboard translation table */
899 case PIO_KEYMAPENT: /* set keyboard translation table entry */
900 case PIO_DEADKEYMAP: /* set accent key translation table */
901 state->ks_accents = 0;
905 return genkbd_commonioctl(kbd, cmd, arg);
912 /* lock the access to the keyboard */
914 atkbd_lock(keyboard_t *kbd, int lock)
916 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
919 /* clear the internal state of the keyboard */
921 atkbd_clear_state(keyboard_t *kbd)
923 atkbd_state_t *state;
925 state = (atkbd_state_t *)kbd->kb_data;
927 state->ks_polling = 0;
928 state->ks_state &= LOCK_MASK; /* preserve locking key state */
929 state->ks_accents = 0;
930 state->ks_composed_char = 0;
932 state->ks_prefix = 0; /* XXX */
936 /* save the internal state */
938 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
941 return sizeof(atkbd_state_t);
942 if (len < sizeof(atkbd_state_t))
944 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
948 /* set the internal state */
950 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
952 if (len < sizeof(atkbd_state_t))
954 if (((atkbd_state_t *)kbd->kb_data)->kbdc
955 != ((atkbd_state_t *)buf)->kbdc)
957 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
962 atkbd_poll(keyboard_t *kbd, int on)
964 atkbd_state_t *state;
967 state = (atkbd_state_t *)kbd->kb_data;
977 /* local functions */
980 get_typematic(keyboard_t *kbd)
984 * Only some systems allow us to retrieve the keyboard repeat
985 * rate previously set via the BIOS...
987 struct vm86frame vmf;
990 bzero(&vmf, sizeof(vmf));
992 vm86_intcall(0x15, &vmf);
993 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
995 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
996 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
999 vm86_intcall(0x16, &vmf);
1000 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1002 vmf.vmf_ax = 0x0306;
1003 vm86_intcall(0x16, &vmf);
1004 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1005 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1009 #endif /* __i386__ */
1013 setup_kbd_port(KBDC kbdc, int port, int intr)
1015 if (!set_controller_command_byte(kbdc,
1016 KBD_KBD_CONTROL_BITS,
1017 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1018 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1024 get_kbd_echo(KBDC kbdc)
1026 /* enable the keyboard port, but disable the keyboard intr. */
1027 if (setup_kbd_port(kbdc, TRUE, FALSE))
1028 /* CONTROLLER ERROR: there is very little we can do... */
1031 /* see if something is present */
1032 write_kbd_command(kbdc, KBDC_ECHO);
1033 if (read_kbd_data(kbdc) != KBD_ECHO) {
1034 empty_both_buffers(kbdc, 10);
1035 test_controller(kbdc);
1036 test_kbd_port(kbdc);
1040 /* enable the keyboard port and intr. */
1041 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1044 * This is serious; the keyboard intr is left disabled!
1053 probe_keyboard(KBDC kbdc, int flags)
1056 * Don't try to print anything in this function. The low-level
1057 * console may not have been initialized yet...
1063 if (!kbdc_lock(kbdc, TRUE)) {
1068 /* temporarily block data transmission from the keyboard */
1069 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1071 /* flush any noise in the buffer */
1072 empty_both_buffers(kbdc, 100);
1074 /* save the current keyboard controller command byte */
1075 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1076 c = get_controller_command_byte(kbdc);
1078 /* CONTROLLER ERROR */
1079 kbdc_set_device_mask(kbdc, m);
1080 kbdc_lock(kbdc, FALSE);
1085 * The keyboard may have been screwed up by the boot block.
1086 * We may just be able to recover from error by testing the controller
1087 * and the keyboard port. The controller command byte needs to be
1088 * saved before this recovery operation, as some controllers seem
1089 * to set the command byte to particular values.
1091 test_controller(kbdc);
1092 test_kbd_port(kbdc);
1094 err = get_kbd_echo(kbdc);
1097 * Even if the keyboard doesn't seem to be present (err != 0),
1098 * we shall enable the keyboard port and interrupt so that
1099 * the driver will be operable when the keyboard is attached
1100 * to the system later. It is NOT recommended to hot-plug
1101 * the AT keyboard, but many people do so...
1103 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1104 setup_kbd_port(kbdc, TRUE, TRUE);
1107 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1109 /* try to restore the command byte as before */
1110 set_controller_command_byte(kbdc, 0xff, c);
1111 kbdc_set_device_mask(kbdc, m);
1115 kbdc_lock(kbdc, FALSE);
1120 init_keyboard(KBDC kbdc, int *type, int flags)
1126 if (!kbdc_lock(kbdc, TRUE)) {
1131 /* temporarily block data transmission from the keyboard */
1132 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1134 /* save the current controller command byte */
1135 empty_both_buffers(kbdc, 200);
1136 c = get_controller_command_byte(kbdc);
1138 /* CONTROLLER ERROR */
1139 kbdc_lock(kbdc, FALSE);
1140 printf("atkbd: unable to get the current command byte value.\n");
1144 printf("atkbd: the current kbd controller command byte %04x\n",
1147 /* override the keyboard lock switch */
1148 c |= KBD_OVERRIDE_KBD_LOCK;
1151 /* enable the keyboard port, but disable the keyboard intr. */
1152 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1153 /* CONTROLLER ERROR: there is very little we can do... */
1154 printf("atkbd: unable to set the command byte.\n");
1155 kbdc_lock(kbdc, FALSE);
1160 * Check if we have an XT keyboard before we attempt to reset it.
1161 * The procedure assumes that the keyboard and the controller have
1162 * been set up properly by BIOS and have not been messed up
1163 * during the boot process.
1166 if (flags & KB_CONF_ALT_SCANCODESET)
1167 /* the user says there is a XT keyboard */
1169 #ifdef KBD_DETECT_XT_KEYBOARD
1170 else if ((c & KBD_TRANSLATION) == 0) {
1171 /* SET_SCANCODE_SET is not always supported; ignore error */
1172 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1174 codeset = read_kbd_data(kbdc);
1177 printf("atkbd: scancode set %d\n", codeset);
1178 #endif /* KBD_DETECT_XT_KEYBOARD */
1181 id = get_kbd_id(kbdc);
1183 case 0x41ab: /* 101/102/... Enhanced */
1184 case 0x83ab: /* ditto */
1185 case 0x54ab: /* SpaceSaver */
1186 case 0x84ab: /* ditto */
1188 case 0x90ab: /* 'G' */
1189 case 0x91ab: /* 'P' */
1190 case 0x92ab: /* 'A' */
1194 case -1: /* AT 84 keyboard doesn't return ID */
1201 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1203 /* reset keyboard hardware */
1204 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1207 * Keyboard reset may fail either because the keyboard
1208 * doen't exist, or because the keyboard doesn't pass
1209 * the self-test, or the keyboard controller on the
1210 * motherboard and the keyboard somehow fail to shake hands.
1211 * It is just possible, particularly in the last case,
1212 * that the keyoard controller may be left in a hung state.
1213 * test_controller() and test_kbd_port() appear to bring
1214 * the keyboard controller back (I don't know why and how,
1217 empty_both_buffers(kbdc, 10);
1218 test_controller(kbdc);
1219 test_kbd_port(kbdc);
1221 * We could disable the keyboard port and interrupt... but,
1222 * the keyboard may still exist (see above).
1224 set_controller_command_byte(kbdc, 0xff, c);
1225 kbdc_lock(kbdc, FALSE);
1227 printf("atkbd: failed to reset the keyboard.\n");
1232 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1233 * such as those on the IBM ThinkPad laptop computers can be used
1234 * with the standard console driver.
1237 if (send_kbd_command_and_data(kbdc,
1238 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1239 /* XT kbd doesn't need scan code translation */
1240 c &= ~KBD_TRANSLATION;
1244 * The XT kbd isn't usable unless the proper scan
1245 * code set is selected.
1247 set_controller_command_byte(kbdc, 0xff, c);
1248 kbdc_lock(kbdc, FALSE);
1249 printf("atkbd: unable to set the XT keyboard mode.\n");
1254 /* enable the keyboard port and intr. */
1255 if (!set_controller_command_byte(kbdc,
1256 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1257 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1258 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1261 * This is serious; we are left with the disabled
1264 set_controller_command_byte(kbdc, 0xff, c);
1265 kbdc_lock(kbdc, FALSE);
1266 printf("atkbd: unable to enable the keyboard port and intr.\n");
1270 kbdc_lock(kbdc, FALSE);
1275 write_kbd(KBDC kbdc, int command, int data)
1279 /* prevent the timeout routine from polling the keyboard */
1280 if (!kbdc_lock(kbdc, TRUE))
1283 /* disable the keyboard and mouse interrupt */
1286 c = get_controller_command_byte(kbdc);
1288 || !set_controller_command_byte(kbdc,
1289 kbdc_get_device_mask(kbdc),
1290 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1291 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1292 /* CONTROLLER ERROR */
1293 kbdc_lock(kbdc, FALSE);
1298 * Now that the keyboard controller is told not to generate
1299 * the keyboard and mouse interrupts, call `splx()' to allow
1300 * the other tty interrupts. The clock interrupt may also occur,
1301 * but the timeout routine (`scrn_timer()') will be blocked
1302 * by the lock flag set via `kbdc_lock()'
1307 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1308 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1311 /* restore the interrupts */
1312 if (!set_controller_command_byte(kbdc,
1313 kbdc_get_device_mask(kbdc),
1314 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1315 /* CONTROLLER ERROR */
1320 kbdc_lock(kbdc, FALSE);
1326 get_kbd_id(KBDC kbdc)
1330 empty_both_buffers(kbdc, 10);
1332 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1335 DELAY(10000); /* 10 msec delay */
1336 id1 = read_kbd_data(kbdc);
1338 id2 = read_kbd_data(kbdc);
1340 if ((id1 == -1) || (id2 == -1)) {
1341 empty_both_buffers(kbdc, 10);
1342 test_controller(kbdc);
1343 test_kbd_port(kbdc);
1346 return ((id2 << 8) | id1);
1349 static int delays[] = { 250, 500, 750, 1000 };
1350 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1351 68, 76, 84, 92, 100, 110, 118, 126,
1352 136, 152, 168, 184, 200, 220, 236, 252,
1353 272, 304, 336, 368, 400, 440, 472, 504 };
1356 typematic_delay(int i)
1358 return delays[(i >> 5) & 3];
1362 typematic_rate(int i)
1364 return rates[i & 0x1f];
1368 typematic(int delay, int rate)
1373 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1374 if (delay >= delays[i])
1378 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1379 if (rate >= rates[i])