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