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