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