FreeBSD/Linux Kernel Cross Reference
sys/dev/atkbdc/atkbd.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: releng/12.0/sys/dev/atkbdc/atkbd.c 337721 2018-08-13 19:05:53Z wulf $");
32
33 #include "opt_kbd.h"
34 #include "opt_atkbd.h"
35 #include "opt_evdev.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/eventhandler.h>
42 #include <sys/proc.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
45
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48
49 #include <sys/kbio.h>
50 #include <dev/kbd/kbdreg.h>
51 #include <dev/atkbdc/atkbdreg.h>
52 #include <dev/atkbdc/atkbdcreg.h>
53
54 #ifdef EVDEV_SUPPORT
55 #include <dev/evdev/evdev.h>
56 #include <dev/evdev/input.h>
57 #endif
58
59 typedef struct atkbd_state {
60 KBDC kbdc; /* keyboard controller */
61 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
62 int ks_flags; /* flags */
63 #define COMPOSE (1 << 0)
64 int ks_polling;
65 int ks_state; /* shift/lock key state */
66 int ks_accents; /* accent key index (> 0) */
67 u_int ks_composed_char; /* composed char code (> 0) */
68 u_char ks_prefix; /* AT scan code prefix */
69 struct callout ks_timer;
70 #ifdef EVDEV_SUPPORT
71 struct evdev_dev *ks_evdev;
72 int ks_evdev_state;
73 #endif
74 } atkbd_state_t;
75
76 static void atkbd_timeout(void *arg);
77 static void atkbd_shutdown_final(void *v);
78 static int atkbd_reset(KBDC kbdc, int flags, int c);
79
80 #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q)
81 #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
82
83 #define DEFAULT_DELAY 0x1 /* 500ms */
84 #define DEFAULT_RATE 0x10 /* 14Hz */
85
86 #ifdef EVDEV_SUPPORT
87 #define PS2_KEYBOARD_VENDOR 1
88 #define PS2_KEYBOARD_PRODUCT 1
89 #endif
90
91 int
92 atkbd_probe_unit(device_t dev, int irq, int flags)
93 {
94 keyboard_switch_t *sw;
95 int args[2];
96 int error;
97
98 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
99 if (sw == NULL)
100 return ENXIO;
101
102 args[0] = device_get_unit(device_get_parent(dev));
103 args[1] = irq;
104 error = (*sw->probe)(device_get_unit(dev), args, flags);
105 if (error)
106 return error;
107 return 0;
108 }
109
110 int
111 atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
112 {
113 keyboard_switch_t *sw;
114 atkbd_state_t *state;
115 int args[2];
116 int error;
117 int unit;
118
119 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
120 if (sw == NULL)
121 return ENXIO;
122
123 /* reset, initialize and enable the device */
124 unit = device_get_unit(dev);
125 args[0] = device_get_unit(device_get_parent(dev));
126 args[1] = irq;
127 *kbd = NULL;
128 error = (*sw->probe)(unit, args, flags);
129 if (error)
130 return error;
131 error = (*sw->init)(unit, kbd, args, flags);
132 if (error)
133 return error;
134 (*sw->enable)(*kbd);
135
136 #ifdef KBD_INSTALL_CDEV
137 /* attach a virtual keyboard cdev */
138 error = kbd_attach(*kbd);
139 if (error)
140 return error;
141 #endif
142
143 /*
144 * This is a kludge to compensate for lost keyboard interrupts.
145 * A similar code used to be in syscons. See below. XXX
146 */
147 state = (atkbd_state_t *)(*kbd)->kb_data;
148 callout_init(&state->ks_timer, 0);
149 atkbd_timeout(*kbd);
150
151 if (bootverbose)
152 (*sw->diag)(*kbd, bootverbose);
153
154 EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
155 SHUTDOWN_PRI_DEFAULT);
156
157 return 0;
158 }
159
160 static void
161 atkbd_timeout(void *arg)
162 {
163 atkbd_state_t *state;
164 keyboard_t *kbd;
165 int s;
166
167 /*
168 * The original text of the following comments are extracted
169 * from syscons.c (1.287)
170 *
171 * With release 2.1 of the Xaccel server, the keyboard is left
172 * hanging pretty often. Apparently an interrupt from the
173 * keyboard is lost, and I don't know why (yet).
174 * This ugly hack calls the low-level interrupt routine if input
175 * is ready for the keyboard and conveniently hides the problem. XXX
176 *
177 * Try removing anything stuck in the keyboard controller; whether
178 * it's a keyboard scan code or mouse data. The low-level
179 * interrupt routine doesn't read the mouse data directly,
180 * but the keyboard controller driver will, as a side effect.
181 */
182 /*
183 * And here is bde's original comment about this:
184 *
185 * This is necessary to handle edge triggered interrupts - if we
186 * returned when our IRQ is high due to unserviced input, then there
187 * would be no more keyboard IRQs until the keyboard is reset by
188 * external powers.
189 *
190 * The keyboard apparently unwedges the irq in most cases.
191 */
192 s = spltty();
193 kbd = (keyboard_t *)arg;
194 if (kbdd_lock(kbd, TRUE)) {
195 /*
196 * We have seen the lock flag is not set. Let's reset
197 * the flag early, otherwise the LED update routine fails
198 * which may want the lock during the interrupt routine.
199 */
200 kbdd_lock(kbd, FALSE);
201 if (kbdd_check_char(kbd))
202 kbdd_intr(kbd, NULL);
203 }
204 splx(s);
205 state = (atkbd_state_t *)kbd->kb_data;
206 callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg);
207 }
208
209 /* LOW-LEVEL */
210
211 #define ATKBD_DEFAULT 0
212
213 /* keyboard driver declaration */
214 static int atkbd_configure(int flags);
215 static kbd_probe_t atkbd_probe;
216 static kbd_init_t atkbd_init;
217 static kbd_term_t atkbd_term;
218 static kbd_intr_t atkbd_intr;
219 static kbd_test_if_t atkbd_test_if;
220 static kbd_enable_t atkbd_enable;
221 static kbd_disable_t atkbd_disable;
222 static kbd_read_t atkbd_read;
223 static kbd_check_t atkbd_check;
224 static kbd_read_char_t atkbd_read_char;
225 static kbd_check_char_t atkbd_check_char;
226 static kbd_ioctl_t atkbd_ioctl;
227 static kbd_lock_t atkbd_lock;
228 static kbd_clear_state_t atkbd_clear_state;
229 static kbd_get_state_t atkbd_get_state;
230 static kbd_set_state_t atkbd_set_state;
231 static kbd_poll_mode_t atkbd_poll;
232
233 static keyboard_switch_t atkbdsw = {
234 atkbd_probe,
235 atkbd_init,
236 atkbd_term,
237 atkbd_intr,
238 atkbd_test_if,
239 atkbd_enable,
240 atkbd_disable,
241 atkbd_read,
242 atkbd_check,
243 atkbd_read_char,
244 atkbd_check_char,
245 atkbd_ioctl,
246 atkbd_lock,
247 atkbd_clear_state,
248 atkbd_get_state,
249 atkbd_set_state,
250 genkbd_get_fkeystr,
251 atkbd_poll,
252 genkbd_diag,
253 };
254
255 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
256
257 /* local functions */
258 static int set_typematic(keyboard_t *kbd);
259 static int setup_kbd_port(KBDC kbdc, int port, int intr);
260 static int get_kbd_echo(KBDC kbdc);
261 static int probe_keyboard(KBDC kbdc, int flags);
262 static int init_keyboard(KBDC kbdc, int *type, int flags);
263 static int write_kbd(KBDC kbdc, int command, int data);
264 static int get_kbd_id(KBDC kbdc);
265 static int typematic(int delay, int rate);
266 static int typematic_delay(int delay);
267 static int typematic_rate(int rate);
268
269 #ifdef EVDEV_SUPPORT
270 static evdev_event_t atkbd_ev_event;
271
272 static const struct evdev_methods atkbd_evdev_methods = {
273 .ev_event = atkbd_ev_event,
274 };
275 #endif
276
277 /* local variables */
278
279 /* the initial key map, accent map and fkey strings */
280 #ifdef ATKBD_DFLT_KEYMAP
281 #define KBD_DFLT_KEYMAP
282 #include "atkbdmap.h"
283 #endif
284 #include <dev/kbd/kbdtables.h>
285
286 /* structures for the default keyboard */
287 static keyboard_t default_kbd;
288 static atkbd_state_t default_kbd_state;
289 static keymap_t default_keymap;
290 static accentmap_t default_accentmap;
291 static fkeytab_t default_fkeytab[NUM_FKEYS];
292
293 /*
294 * The back door to the keyboard driver!
295 * This function is called by the console driver, via the kbdio module,
296 * to tickle keyboard drivers when the low-level console is being initialized.
297 * Almost nothing in the kernel has been initialied yet. Try to probe
298 * keyboards if possible.
299 * NOTE: because of the way the low-level console is initialized, this routine
300 * may be called more than once!!
301 */
302 static int
303 atkbd_configure(int flags)
304 {
305 keyboard_t *kbd;
306 int arg[2];
307 int i;
308
309 /*
310 * Probe the keyboard controller, if not present or if the driver
311 * is disabled, unregister the keyboard if any.
312 */
313 if (atkbdc_configure() != 0 ||
314 resource_disabled("atkbd", ATKBD_DEFAULT)) {
315 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
316 if (i >= 0) {
317 kbd = kbd_get_keyboard(i);
318 kbd_unregister(kbd);
319 kbd->kb_flags &= ~KB_REGISTERED;
320 }
321 return 0;
322 }
323
324 /* XXX: a kludge to obtain the device configuration flags */
325 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
326 flags |= i;
327
328 /* probe the default keyboard */
329 arg[0] = -1;
330 arg[1] = -1;
331 kbd = NULL;
332 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
333 return 0;
334 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
335 return 0;
336
337 /* return the number of found keyboards */
338 return 1;
339 }
340
341 /* low-level functions */
342
343 /* detect a keyboard */
344 static int
345 atkbd_probe(int unit, void *arg, int flags)
346 {
347 KBDC kbdc;
348 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
349
350 /* XXX */
351 if (unit == ATKBD_DEFAULT) {
352 if (KBD_IS_PROBED(&default_kbd))
353 return 0;
354 }
355
356 kbdc = atkbdc_open(data[0]);
357 if (kbdc == NULL)
358 return ENXIO;
359 if (probe_keyboard(kbdc, flags)) {
360 if (flags & KB_CONF_FAIL_IF_NO_KBD)
361 return ENXIO;
362 }
363 return 0;
364 }
365
366 /* reset and initialize the device */
367 static int
368 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
369 {
370 keyboard_t *kbd;
371 atkbd_state_t *state;
372 keymap_t *keymap;
373 accentmap_t *accmap;
374 fkeytab_t *fkeymap;
375 int fkeymap_size;
376 int delay[2];
377 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
378 int error, needfree;
379 #ifdef EVDEV_SUPPORT
380 struct evdev_dev *evdev;
381 char phys_loc[8];
382 #endif
383
384 /* XXX */
385 if (unit == ATKBD_DEFAULT) {
386 *kbdp = kbd = &default_kbd;
387 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
388 return 0;
389 state = &default_kbd_state;
390 keymap = &default_keymap;
391 accmap = &default_accentmap;
392 fkeymap = default_fkeytab;
393 fkeymap_size = nitems(default_fkeytab);
394 needfree = 0;
395 } else if (*kbdp == NULL) {
396 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
397 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
398 /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
399 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
400 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
401 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
402 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
403 needfree = 1;
404 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
405 || (accmap == NULL) || (fkeymap == NULL)) {
406 error = ENOMEM;
407 goto bad;
408 }
409 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
410 return 0;
411 } else {
412 kbd = *kbdp;
413 state = (atkbd_state_t *)kbd->kb_data;
414 bzero(state, sizeof(*state));
415 keymap = kbd->kb_keymap;
416 accmap = kbd->kb_accentmap;
417 fkeymap = kbd->kb_fkeytab;
418 fkeymap_size = kbd->kb_fkeytab_size;
419 needfree = 0;
420 }
421
422 if (!KBD_IS_PROBED(kbd)) {
423 state->kbdc = atkbdc_open(data[0]);
424 if (state->kbdc == NULL) {
425 error = ENXIO;
426 goto bad;
427 }
428 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
429 0, 0);
430 bcopy(&key_map, keymap, sizeof(key_map));
431 bcopy(&accent_map, accmap, sizeof(accent_map));
432 bcopy(fkey_tab, fkeymap,
433 imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
434 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
435 kbd->kb_data = (void *)state;
436
437 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
438 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
439 error = ENXIO;
440 goto bad;
441 }
442 } else {
443 KBD_FOUND_DEVICE(kbd);
444 }
445 atkbd_clear_state(kbd);
446 state->ks_mode = K_XLATE;
447 /*
448 * FIXME: set the initial value for lock keys in ks_state
449 * according to the BIOS data?
450 */
451 KBD_PROBE_DONE(kbd);
452 }
453 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
454 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
455 if (KBD_HAS_DEVICE(kbd)
456 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
457 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
458 kbd_unregister(kbd);
459 error = ENXIO;
460 goto bad;
461 }
462 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
463 set_typematic(kbd);
464 delay[0] = kbd->kb_delay1;
465 delay[1] = kbd->kb_delay2;
466 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
467
468 #ifdef EVDEV_SUPPORT
469 /* register as evdev provider on first init */
470 if (state->ks_evdev == NULL) {
471 snprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit);
472 evdev = evdev_alloc();
473 evdev_set_name(evdev, "AT keyboard");
474 evdev_set_phys(evdev, phys_loc);
475 evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR,
476 PS2_KEYBOARD_PRODUCT, 0);
477 evdev_set_methods(evdev, kbd, &atkbd_evdev_methods);
478 evdev_support_event(evdev, EV_SYN);
479 evdev_support_event(evdev, EV_KEY);
480 evdev_support_event(evdev, EV_LED);
481 evdev_support_event(evdev, EV_REP);
482 evdev_support_all_known_keys(evdev);
483 evdev_support_led(evdev, LED_NUML);
484 evdev_support_led(evdev, LED_CAPSL);
485 evdev_support_led(evdev, LED_SCROLLL);
486
487 if (evdev_register(evdev))
488 evdev_free(evdev);
489 else
490 state->ks_evdev = evdev;
491 state->ks_evdev_state = 0;
492 }
493 #endif
494
495 KBD_INIT_DONE(kbd);
496 }
497 if (!KBD_IS_CONFIGURED(kbd)) {
498 if (kbd_register(kbd) < 0) {
499 error = ENXIO;
500 goto bad;
501 }
502 KBD_CONFIG_DONE(kbd);
503 }
504
505 return 0;
506 bad:
507 if (needfree) {
508 if (state != NULL)
509 free(state, M_DEVBUF);
510 if (keymap != NULL)
511 free(keymap, M_DEVBUF);
512 if (accmap != NULL)
513 free(accmap, M_DEVBUF);
514 if (fkeymap != NULL)
515 free(fkeymap, M_DEVBUF);
516 if (kbd != NULL) {
517 free(kbd, M_DEVBUF);
518 *kbdp = NULL; /* insure ref doesn't leak to caller */
519 }
520 }
521 return error;
522 }
523
524 /* finish using this keyboard */
525 static int
526 atkbd_term(keyboard_t *kbd)
527 {
528 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
529
530 kbd_unregister(kbd);
531 callout_drain(&state->ks_timer);
532 return 0;
533 }
534
535 /* keyboard interrupt routine */
536 static int
537 atkbd_intr(keyboard_t *kbd, void *arg)
538 {
539 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
540 int delay[2];
541 int c;
542
543 if (!KBD_HAS_DEVICE(kbd)) {
544 /*
545 * The keyboard was not detected before;
546 * it must have been reconnected!
547 */
548 init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
549 KBD_FOUND_DEVICE(kbd);
550 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
551 set_typematic(kbd);
552 delay[0] = kbd->kb_delay1;
553 delay[1] = kbd->kb_delay2;
554 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
555 }
556
557 if (state->ks_polling)
558 return 0;
559
560 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
561 /* let the callback function to process the input */
562 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
563 kbd->kb_callback.kc_arg);
564 } else {
565 /* read and discard the input; no one is waiting for input */
566 do {
567 c = atkbd_read_char(kbd, FALSE);
568 } while (c != NOKEY);
569 }
570 return 0;
571 }
572
573 /* test the interface to the device */
574 static int
575 atkbd_test_if(keyboard_t *kbd)
576 {
577 int error;
578 int s;
579
580 error = 0;
581 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
582 s = spltty();
583 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
584 error = EIO;
585 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
586 error = EIO;
587 splx(s);
588
589 return error;
590 }
591
592 /*
593 * Enable the access to the device; until this function is called,
594 * the client cannot read from the keyboard.
595 */
596 static int
597 atkbd_enable(keyboard_t *kbd)
598 {
599 int s;
600
601 s = spltty();
602 KBD_ACTIVATE(kbd);
603 splx(s);
604 return 0;
605 }
606
607 /* disallow the access to the device */
608 static int
609 atkbd_disable(keyboard_t *kbd)
610 {
611 int s;
612
613 s = spltty();
614 KBD_DEACTIVATE(kbd);
615 splx(s);
616 return 0;
617 }
618
619 /* read one byte from the keyboard if it's allowed */
620 static int
621 atkbd_read(keyboard_t *kbd, int wait)
622 {
623 int c;
624
625 if (wait)
626 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
627 else
628 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
629 if (c != -1)
630 ++kbd->kb_count;
631 return (KBD_IS_ACTIVE(kbd) ? c : -1);
632 }
633
634 /* check if data is waiting */
635 static int
636 atkbd_check(keyboard_t *kbd)
637 {
638 if (!KBD_IS_ACTIVE(kbd))
639 return FALSE;
640 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
641 }
642
643 /* read char from the keyboard */
644 static u_int
645 atkbd_read_char(keyboard_t *kbd, int wait)
646 {
647 atkbd_state_t *state;
648 u_int action;
649 int scancode;
650 int keycode;
651
652 state = (atkbd_state_t *)kbd->kb_data;
653 next_code:
654 /* do we have a composed char to return? */
655 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
656 action = state->ks_composed_char;
657 state->ks_composed_char = 0;
658 if (action > UCHAR_MAX)
659 return ERRKEY;
660 return action;
661 }
662
663 /* see if there is something in the keyboard port */
664 if (wait) {
665 do {
666 scancode = read_kbd_data(state->kbdc);
667 } while (scancode == -1);
668 } else {
669 scancode = read_kbd_data_no_wait(state->kbdc);
670 if (scancode == -1)
671 return NOKEY;
672 }
673 ++kbd->kb_count;
674
675 #if KBDIO_DEBUG >= 10
676 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
677 #endif
678
679 #ifdef EVDEV_SUPPORT
680 /* push evdev event */
681 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) {
682 keycode = evdev_scancode2key(&state->ks_evdev_state,
683 scancode);
684
685 if (keycode != KEY_RESERVED) {
686 evdev_push_event(state->ks_evdev, EV_KEY,
687 (uint16_t)keycode, scancode & 0x80 ? 0 : 1);
688 evdev_sync(state->ks_evdev);
689 }
690 }
691 #endif
692
693 /* return the byte as is for the K_RAW mode */
694 if (state->ks_mode == K_RAW)
695 return scancode;
696
697 /* translate the scan code into a keycode */
698 keycode = scancode & 0x7F;
699 switch (state->ks_prefix) {
700 case 0x00: /* normal scancode */
701 switch(scancode) {
702 case 0xB8: /* left alt (compose key) released */
703 if (state->ks_flags & COMPOSE) {
704 state->ks_flags &= ~COMPOSE;
705 if (state->ks_composed_char > UCHAR_MAX)
706 state->ks_composed_char = 0;
707 }
708 break;
709 case 0x38: /* left alt (compose key) pressed */
710 if (!(state->ks_flags & COMPOSE)) {
711 state->ks_flags |= COMPOSE;
712 state->ks_composed_char = 0;
713 }
714 break;
715 case 0xE0:
716 case 0xE1:
717 state->ks_prefix = scancode;
718 goto next_code;
719 }
720 break;
721 case 0xE0: /* 0xE0 prefix */
722 state->ks_prefix = 0;
723 switch (keycode) {
724 case 0x1C: /* right enter key */
725 keycode = 0x59;
726 break;
727 case 0x1D: /* right ctrl key */
728 keycode = 0x5A;
729 break;
730 case 0x35: /* keypad divide key */
731 keycode = 0x5B;
732 break;
733 case 0x37: /* print scrn key */
734 keycode = 0x5C;
735 break;
736 case 0x38: /* right alt key (alt gr) */
737 keycode = 0x5D;
738 break;
739 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
740 keycode = 0x68;
741 break;
742 case 0x47: /* grey home key */
743 keycode = 0x5E;
744 break;
745 case 0x48: /* grey up arrow key */
746 keycode = 0x5F;
747 break;
748 case 0x49: /* grey page up key */
749 keycode = 0x60;
750 break;
751 case 0x4B: /* grey left arrow key */
752 keycode = 0x61;
753 break;
754 case 0x4D: /* grey right arrow key */
755 keycode = 0x62;
756 break;
757 case 0x4F: /* grey end key */
758 keycode = 0x63;
759 break;
760 case 0x50: /* grey down arrow key */
761 keycode = 0x64;
762 break;
763 case 0x51: /* grey page down key */
764 keycode = 0x65;
765 break;
766 case 0x52: /* grey insert key */
767 keycode = 0x66;
768 break;
769 case 0x53: /* grey delete key */
770 keycode = 0x67;
771 break;
772 /* the following 3 are only used on the MS "Natural" keyboard */
773 case 0x5b: /* left Window key */
774 keycode = 0x69;
775 break;
776 case 0x5c: /* right Window key */
777 keycode = 0x6a;
778 break;
779 case 0x5d: /* menu key */
780 keycode = 0x6b;
781 break;
782 case 0x5e: /* power key */
783 keycode = 0x6d;
784 break;
785 case 0x5f: /* sleep key */
786 keycode = 0x6e;
787 break;
788 case 0x63: /* wake key */
789 keycode = 0x6f;
790 break;
791 default: /* ignore everything else */
792 goto next_code;
793 }
794 break;
795 case 0xE1: /* 0xE1 prefix */
796 /*
797 * The pause/break key on the 101 keyboard produces:
798 * E1-1D-45 E1-9D-C5
799 * Ctrl-pause/break produces:
800 * E0-46 E0-C6 (See above.)
801 */
802 state->ks_prefix = 0;
803 if (keycode == 0x1D)
804 state->ks_prefix = 0x1D;
805 goto next_code;
806 /* NOT REACHED */
807 case 0x1D: /* pause / break */
808 state->ks_prefix = 0;
809 if (keycode != 0x45)
810 goto next_code;
811 keycode = 0x68;
812 break;
813 }
814
815 if (kbd->kb_type == KB_84) {
816 switch (keycode) {
817 case 0x37: /* *(numpad)/print screen */
818 if (state->ks_flags & SHIFTS)
819 keycode = 0x5c; /* print screen */
820 break;
821 case 0x45: /* num lock/pause */
822 if (state->ks_flags & CTLS)
823 keycode = 0x68; /* pause */
824 break;
825 case 0x46: /* scroll lock/break */
826 if (state->ks_flags & CTLS)
827 keycode = 0x6c; /* break */
828 break;
829 }
830 } else if (kbd->kb_type == KB_101) {
831 switch (keycode) {
832 case 0x5c: /* print screen */
833 if (state->ks_flags & ALTS)
834 keycode = 0x54; /* sysrq */
835 break;
836 case 0x68: /* pause/break */
837 if (state->ks_flags & CTLS)
838 keycode = 0x6c; /* break */
839 break;
840 }
841 }
842
843 /* return the key code in the K_CODE mode */
844 if (state->ks_mode == K_CODE)
845 return (keycode | (scancode & 0x80));
846
847 /* compose a character code */
848 if (state->ks_flags & COMPOSE) {
849 switch (keycode | (scancode & 0x80)) {
850 /* key pressed, process it */
851 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
852 state->ks_composed_char *= 10;
853 state->ks_composed_char += keycode - 0x40;
854 if (state->ks_composed_char > UCHAR_MAX)
855 return ERRKEY;
856 goto next_code;
857 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
858 state->ks_composed_char *= 10;
859 state->ks_composed_char += keycode - 0x47;
860 if (state->ks_composed_char > UCHAR_MAX)
861 return ERRKEY;
862 goto next_code;
863 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
864 state->ks_composed_char *= 10;
865 state->ks_composed_char += keycode - 0x4E;
866 if (state->ks_composed_char > UCHAR_MAX)
867 return ERRKEY;
868 goto next_code;
869 case 0x52: /* keypad 0 */
870 state->ks_composed_char *= 10;
871 if (state->ks_composed_char > UCHAR_MAX)
872 return ERRKEY;
873 goto next_code;
874
875 /* key released, no interest here */
876 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
877 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
878 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
879 case 0xD2: /* keypad 0 */
880 goto next_code;
881
882 case 0x38: /* left alt key */
883 break;
884
885 default:
886 if (state->ks_composed_char > 0) {
887 state->ks_flags &= ~COMPOSE;
888 state->ks_composed_char = 0;
889 return ERRKEY;
890 }
891 break;
892 }
893 }
894
895 /* keycode to key action */
896 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
897 &state->ks_state, &state->ks_accents);
898 if (action == NOKEY)
899 goto next_code;
900 else
901 return action;
902 }
903
904 /* check if char is waiting */
905 static int
906 atkbd_check_char(keyboard_t *kbd)
907 {
908 atkbd_state_t *state;
909
910 if (!KBD_IS_ACTIVE(kbd))
911 return FALSE;
912 state = (atkbd_state_t *)kbd->kb_data;
913 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
914 return TRUE;
915 return kbdc_data_ready(state->kbdc);
916 }
917
918 /* some useful control functions */
919 static int
920 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
921 {
922 /* translate LED_XXX bits into the device specific bits */
923 static u_char ledmap[8] = {
924 0, 4, 2, 6, 1, 5, 3, 7,
925 };
926 atkbd_state_t *state = kbd->kb_data;
927 int error;
928 int s;
929 int i;
930 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
931 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
932 int ival;
933 #endif
934
935 s = spltty();
936 switch (cmd) {
937
938 case KDGKBMODE: /* get keyboard mode */
939 *(int *)arg = state->ks_mode;
940 break;
941 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
942 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
943 case _IO('K', 7):
944 ival = IOCPARM_IVAL(arg);
945 arg = (caddr_t)&ival;
946 /* FALLTHROUGH */
947 #endif
948 case KDSKBMODE: /* set keyboard mode */
949 switch (*(int *)arg) {
950 case K_XLATE:
951 if (state->ks_mode != K_XLATE) {
952 /* make lock key state and LED state match */
953 state->ks_state &= ~LOCK_MASK;
954 state->ks_state |= KBD_LED_VAL(kbd);
955 }
956 /* FALLTHROUGH */
957 case K_RAW:
958 case K_CODE:
959 if (state->ks_mode != *(int *)arg) {
960 atkbd_clear_state(kbd);
961 state->ks_mode = *(int *)arg;
962 }
963 break;
964 default:
965 splx(s);
966 return EINVAL;
967 }
968 break;
969
970 case KDGETLED: /* get keyboard LED */
971 *(int *)arg = KBD_LED_VAL(kbd);
972 break;
973 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
974 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
975 case _IO('K', 66):
976 ival = IOCPARM_IVAL(arg);
977 arg = (caddr_t)&ival;
978 /* FALLTHROUGH */
979 #endif
980 case KDSETLED: /* set keyboard LED */
981 /* NOTE: lock key state in ks_state won't be changed */
982 if (*(int *)arg & ~LOCK_MASK) {
983 splx(s);
984 return EINVAL;
985 }
986 i = *(int *)arg;
987 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
988 if (state->ks_mode == K_XLATE &&
989 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
990 if (i & ALKED)
991 i |= CLKED;
992 else
993 i &= ~CLKED;
994 }
995 if (KBD_HAS_DEVICE(kbd)) {
996 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
997 ledmap[i & LED_MASK]);
998 if (error) {
999 splx(s);
1000 return error;
1001 }
1002 }
1003 #ifdef EVDEV_SUPPORT
1004 /* push LED states to evdev */
1005 if (state->ks_evdev != NULL &&
1006 evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
1007 evdev_push_leds(state->ks_evdev, *(int *)arg);
1008 #endif
1009 KBD_LED_VAL(kbd) = *(int *)arg;
1010 break;
1011
1012 case KDGKBSTATE: /* get lock key state */
1013 *(int *)arg = state->ks_state & LOCK_MASK;
1014 break;
1015 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1016 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1017 case _IO('K', 20):
1018 ival = IOCPARM_IVAL(arg);
1019 arg = (caddr_t)&ival;
1020 /* FALLTHROUGH */
1021 #endif
1022 case KDSKBSTATE: /* set lock key state */
1023 if (*(int *)arg & ~LOCK_MASK) {
1024 splx(s);
1025 return EINVAL;
1026 }
1027 state->ks_state &= ~LOCK_MASK;
1028 state->ks_state |= *(int *)arg;
1029 splx(s);
1030 /* set LEDs and quit */
1031 return atkbd_ioctl(kbd, KDSETLED, arg);
1032
1033 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1034 splx(s);
1035 if (!KBD_HAS_DEVICE(kbd))
1036 return 0;
1037 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
1038 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
1039 if (error == 0) {
1040 kbd->kb_delay1 = typematic_delay(i);
1041 kbd->kb_delay2 = typematic_rate(i);
1042 #ifdef EVDEV_SUPPORT
1043 if (state->ks_evdev != NULL &&
1044 evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
1045 evdev_push_repeats(state->ks_evdev, kbd);
1046 #endif
1047 }
1048 return error;
1049
1050 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1051 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1052 case _IO('K', 67):
1053 ival = IOCPARM_IVAL(arg);
1054 arg = (caddr_t)&ival;
1055 /* FALLTHROUGH */
1056 #endif
1057 case KDSETRAD: /* set keyboard repeat rate (old interface) */
1058 splx(s);
1059 if (!KBD_HAS_DEVICE(kbd))
1060 return 0;
1061 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
1062 if (error == 0) {
1063 kbd->kb_delay1 = typematic_delay(*(int *)arg);
1064 kbd->kb_delay2 = typematic_rate(*(int *)arg);
1065 #ifdef EVDEV_SUPPORT
1066 if (state->ks_evdev != NULL &&
1067 evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
1068 evdev_push_repeats(state->ks_evdev, kbd);
1069 #endif
1070 }
1071 return error;
1072
1073 case PIO_KEYMAP: /* set keyboard translation table */
1074 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1075 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1076 case PIO_DEADKEYMAP: /* set accent key translation table */
1077 state->ks_accents = 0;
1078 /* FALLTHROUGH */
1079 default:
1080 splx(s);
1081 return genkbd_commonioctl(kbd, cmd, arg);
1082 }
1083
1084 splx(s);
1085 return 0;
1086 }
1087
1088 /* lock the access to the keyboard */
1089 static int
1090 atkbd_lock(keyboard_t *kbd, int lock)
1091 {
1092 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1093 }
1094
1095 /* clear the internal state of the keyboard */
1096 static void
1097 atkbd_clear_state(keyboard_t *kbd)
1098 {
1099 atkbd_state_t *state;
1100
1101 state = (atkbd_state_t *)kbd->kb_data;
1102 state->ks_flags = 0;
1103 state->ks_polling = 0;
1104 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1105 state->ks_accents = 0;
1106 state->ks_composed_char = 0;
1107 #if 0
1108 state->ks_prefix = 0; /* XXX */
1109 #endif
1110 }
1111
1112 /* save the internal state */
1113 static int
1114 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1115 {
1116 if (len == 0)
1117 return sizeof(atkbd_state_t);
1118 if (len < sizeof(atkbd_state_t))
1119 return -1;
1120 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1121 return 0;
1122 }
1123
1124 /* set the internal state */
1125 static int
1126 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1127 {
1128 if (len < sizeof(atkbd_state_t))
1129 return ENOMEM;
1130 if (((atkbd_state_t *)kbd->kb_data)->kbdc
1131 != ((atkbd_state_t *)buf)->kbdc)
1132 return ENOMEM;
1133 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1134 return 0;
1135 }
1136
1137 static int
1138 atkbd_poll(keyboard_t *kbd, int on)
1139 {
1140 atkbd_state_t *state;
1141 int s;
1142
1143 state = (atkbd_state_t *)kbd->kb_data;
1144 s = spltty();
1145 if (on)
1146 ++state->ks_polling;
1147 else
1148 --state->ks_polling;
1149 splx(s);
1150 return 0;
1151 }
1152
1153 static void
1154 atkbd_shutdown_final(void *v)
1155 {
1156 #ifdef __sparc64__
1157 keyboard_t *kbd = v;
1158 KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1159
1160 /*
1161 * Turn off the translation in preparation for handing the keyboard
1162 * over to the OFW as the OBP driver doesn't use translation and
1163 * also doesn't disable it itself resulting in a broken keymap at
1164 * the boot prompt. Also disable the aux port and the interrupts as
1165 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1166 * disabling the interrupts doesn't cause real problems but the
1167 * responsiveness is a bit better when they are turned off.
1168 */
1169 send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1170 set_controller_command_byte(kbdc,
1171 KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1172 KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1173 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1174 #endif
1175 }
1176
1177 static int
1178 atkbd_reset(KBDC kbdc, int flags, int c)
1179 {
1180 /* reset keyboard hardware */
1181 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1182 /*
1183 * KEYBOARD ERROR
1184 * Keyboard reset may fail either because the keyboard
1185 * doen't exist, or because the keyboard doesn't pass
1186 * the self-test, or the keyboard controller on the
1187 * motherboard and the keyboard somehow fail to shake hands.
1188 * It is just possible, particularly in the last case,
1189 * that the keyboard controller may be left in a hung state.
1190 * test_controller() and test_kbd_port() appear to bring
1191 * the keyboard controller back (I don't know why and how,
1192 * though.)
1193 */
1194 empty_both_buffers(kbdc, 10);
1195 test_controller(kbdc);
1196 test_kbd_port(kbdc);
1197 /*
1198 * We could disable the keyboard port and interrupt... but,
1199 * the keyboard may still exist (see above).
1200 */
1201 set_controller_command_byte(kbdc,
1202 ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1203 if (bootverbose)
1204 printf("atkbd: failed to reset the keyboard.\n");
1205 return (EIO);
1206 }
1207 return (0);
1208 }
1209
1210 #ifdef EVDEV_SUPPORT
1211 static void
1212 atkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
1213 int32_t value)
1214 {
1215 keyboard_t *kbd = evdev_get_softc(evdev);
1216
1217 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
1218 (type == EV_LED || type == EV_REP)) {
1219 mtx_lock(&Giant);
1220 kbd_ev_event(kbd, type, code, value);
1221 mtx_unlock(&Giant);
1222 }
1223 }
1224 #endif
1225
1226 /* local functions */
1227
1228 static int
1229 set_typematic(keyboard_t *kbd)
1230 {
1231 int val, error;
1232 atkbd_state_t *state = kbd->kb_data;
1233
1234 val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
1235 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
1236 if (error == 0) {
1237 kbd->kb_delay1 = typematic_delay(val);
1238 kbd->kb_delay2 = typematic_rate(val);
1239 }
1240
1241 return (error);
1242 }
1243
1244 static int
1245 setup_kbd_port(KBDC kbdc, int port, int intr)
1246 {
1247 if (!set_controller_command_byte(kbdc,
1248 KBD_KBD_CONTROL_BITS,
1249 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1250 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1251 return 1;
1252 return 0;
1253 }
1254
1255 static int
1256 get_kbd_echo(KBDC kbdc)
1257 {
1258 /* enable the keyboard port, but disable the keyboard intr. */
1259 if (setup_kbd_port(kbdc, TRUE, FALSE))
1260 /* CONTROLLER ERROR: there is very little we can do... */
1261 return ENXIO;
1262
1263 /* see if something is present */
1264 write_kbd_command(kbdc, KBDC_ECHO);
1265 if (read_kbd_data(kbdc) != KBD_ECHO) {
1266 empty_both_buffers(kbdc, 10);
1267 test_controller(kbdc);
1268 test_kbd_port(kbdc);
1269 return ENXIO;
1270 }
1271
1272 /* enable the keyboard port and intr. */
1273 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1274 /*
1275 * CONTROLLER ERROR
1276 * This is serious; the keyboard intr is left disabled!
1277 */
1278 return ENXIO;
1279 }
1280
1281 return 0;
1282 }
1283
1284 static int
1285 probe_keyboard(KBDC kbdc, int flags)
1286 {
1287 /*
1288 * Don't try to print anything in this function. The low-level
1289 * console may not have been initialized yet...
1290 */
1291 int err;
1292 int c;
1293 int m;
1294
1295 if (!kbdc_lock(kbdc, TRUE)) {
1296 /* driver error? */
1297 return ENXIO;
1298 }
1299
1300 /* temporarily block data transmission from the keyboard */
1301 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1302
1303 /* flush any noise in the buffer */
1304 empty_both_buffers(kbdc, 100);
1305
1306 /* save the current keyboard controller command byte */
1307 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1308 c = get_controller_command_byte(kbdc);
1309 if (c == -1) {
1310 /* CONTROLLER ERROR */
1311 kbdc_set_device_mask(kbdc, m);
1312 kbdc_lock(kbdc, FALSE);
1313 return ENXIO;
1314 }
1315
1316 /*
1317 * The keyboard may have been screwed up by the boot block.
1318 * We may just be able to recover from error by testing the controller
1319 * and the keyboard port. The controller command byte needs to be
1320 * saved before this recovery operation, as some controllers seem
1321 * to set the command byte to particular values.
1322 */
1323 test_controller(kbdc);
1324 if (!(flags & KB_CONF_NO_PROBE_TEST))
1325 test_kbd_port(kbdc);
1326
1327 err = get_kbd_echo(kbdc);
1328
1329 /*
1330 * Even if the keyboard doesn't seem to be present (err != 0),
1331 * we shall enable the keyboard port and interrupt so that
1332 * the driver will be operable when the keyboard is attached
1333 * to the system later. It is NOT recommended to hot-plug
1334 * the AT keyboard, but many people do so...
1335 */
1336 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1337 setup_kbd_port(kbdc, TRUE, TRUE);
1338 #if 0
1339 if (err == 0) {
1340 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1341 } else {
1342 /* try to restore the command byte as before */
1343 set_controller_command_byte(kbdc,
1344 ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1345 kbdc_set_device_mask(kbdc, m);
1346 }
1347 #endif
1348
1349 kbdc_lock(kbdc, FALSE);
1350 return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
1351 }
1352
1353 static int
1354 init_keyboard(KBDC kbdc, int *type, int flags)
1355 {
1356 int codeset;
1357 int id;
1358 int c;
1359
1360 if (!kbdc_lock(kbdc, TRUE)) {
1361 /* driver error? */
1362 return EIO;
1363 }
1364
1365 /* temporarily block data transmission from the keyboard */
1366 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1367
1368 /* save the current controller command byte */
1369 empty_both_buffers(kbdc, 200);
1370 c = get_controller_command_byte(kbdc);
1371 if (c == -1) {
1372 /* CONTROLLER ERROR */
1373 kbdc_lock(kbdc, FALSE);
1374 printf("atkbd: unable to get the current command byte value.\n");
1375 return EIO;
1376 }
1377 if (bootverbose)
1378 printf("atkbd: the current kbd controller command byte %04x\n",
1379 c);
1380 #if 0
1381 /* override the keyboard lock switch */
1382 c |= KBD_OVERRIDE_KBD_LOCK;
1383 #endif
1384
1385 /* enable the keyboard port, but disable the keyboard intr. */
1386 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1387 /* CONTROLLER ERROR: there is very little we can do... */
1388 printf("atkbd: unable to set the command byte.\n");
1389 kbdc_lock(kbdc, FALSE);
1390 return EIO;
1391 }
1392
1393 if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1394 atkbd_reset(kbdc, flags, c)) {
1395 kbdc_lock(kbdc, FALSE);
1396 return EIO;
1397 }
1398
1399 /*
1400 * Check if we have an XT keyboard before we attempt to reset it.
1401 * The procedure assumes that the keyboard and the controller have
1402 * been set up properly by BIOS and have not been messed up
1403 * during the boot process.
1404 */
1405 codeset = -1;
1406 if (flags & KB_CONF_ALT_SCANCODESET)
1407 /* the user says there is a XT keyboard */
1408 codeset = 1;
1409 #ifdef KBD_DETECT_XT_KEYBOARD
1410 else if ((c & KBD_TRANSLATION) == 0) {
1411 /* SET_SCANCODE_SET is not always supported; ignore error */
1412 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1413 == KBD_ACK)
1414 codeset = read_kbd_data(kbdc);
1415 }
1416 if (bootverbose)
1417 printf("atkbd: scancode set %d\n", codeset);
1418 #endif /* KBD_DETECT_XT_KEYBOARD */
1419
1420 *type = KB_OTHER;
1421 id = get_kbd_id(kbdc);
1422 switch(id) {
1423 case 0x41ab: /* 101/102/... Enhanced */
1424 case 0x83ab: /* ditto */
1425 case 0x54ab: /* SpaceSaver */
1426 case 0x84ab: /* ditto */
1427 #if 0
1428 case 0x90ab: /* 'G' */
1429 case 0x91ab: /* 'P' */
1430 case 0x92ab: /* 'A' */
1431 #endif
1432 *type = KB_101;
1433 break;
1434 case -1: /* AT 84 keyboard doesn't return ID */
1435 *type = KB_84;
1436 break;
1437 default:
1438 break;
1439 }
1440 if (bootverbose)
1441 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1442
1443 if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1444 atkbd_reset(kbdc, flags, c)) {
1445 kbdc_lock(kbdc, FALSE);
1446 return EIO;
1447 }
1448
1449 /*
1450 * Allow us to set the XT_KEYBD flag so that keyboards
1451 * such as those on the IBM ThinkPad laptop computers can be used
1452 * with the standard console driver.
1453 */
1454 if (codeset == 1) {
1455 if (send_kbd_command_and_data(kbdc,
1456 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1457 /* XT kbd doesn't need scan code translation */
1458 c &= ~KBD_TRANSLATION;
1459 } else {
1460 /*
1461 * KEYBOARD ERROR
1462 * The XT kbd isn't usable unless the proper scan
1463 * code set is selected.
1464 */
1465 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1466 ? 0xff : KBD_KBD_CONTROL_BITS, c);
1467 kbdc_lock(kbdc, FALSE);
1468 printf("atkbd: unable to set the XT keyboard mode.\n");
1469 return EIO;
1470 }
1471 }
1472
1473 #if defined(__sparc64__)
1474 if (send_kbd_command_and_data(
1475 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1476 printf("atkbd: can't set translation.\n");
1477 }
1478 c |= KBD_TRANSLATION;
1479 #endif
1480
1481 /*
1482 * Some keyboards require a SETLEDS command to be sent after
1483 * the reset command before they will send keystrokes to us
1484 */
1485 if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
1486 send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
1487 printf("atkbd: setleds failed\n");
1488 }
1489 if (!ALLOW_DISABLE_KBD(kbdc))
1490 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1491
1492 /* enable the keyboard port and intr. */
1493 if (!set_controller_command_byte(kbdc,
1494 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1495 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1496 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1497 /*
1498 * CONTROLLER ERROR
1499 * This is serious; we are left with the disabled
1500 * keyboard intr.
1501 */
1502 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1503 ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1504 KBD_OVERRIDE_KBD_LOCK), c);
1505 kbdc_lock(kbdc, FALSE);
1506 printf("atkbd: unable to enable the keyboard port and intr.\n");
1507 return EIO;
1508 }
1509
1510 kbdc_lock(kbdc, FALSE);
1511 return 0;
1512 }
1513
1514 static int
1515 write_kbd(KBDC kbdc, int command, int data)
1516 {
1517 int s;
1518
1519 /* prevent the timeout routine from polling the keyboard */
1520 if (!kbdc_lock(kbdc, TRUE))
1521 return EBUSY;
1522
1523 /* disable the keyboard and mouse interrupt */
1524 s = spltty();
1525 #if 0
1526 c = get_controller_command_byte(kbdc);
1527 if ((c == -1)
1528 || !set_controller_command_byte(kbdc,
1529 kbdc_get_device_mask(kbdc),
1530 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1531 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1532 /* CONTROLLER ERROR */
1533 kbdc_lock(kbdc, FALSE);
1534 splx(s);
1535 return EIO;
1536 }
1537 /*
1538 * Now that the keyboard controller is told not to generate
1539 * the keyboard and mouse interrupts, call `splx()' to allow
1540 * the other tty interrupts. The clock interrupt may also occur,
1541 * but the timeout routine (`scrn_timer()') will be blocked
1542 * by the lock flag set via `kbdc_lock()'
1543 */
1544 splx(s);
1545 #endif
1546 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1547 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1548 #if 0
1549 /* restore the interrupts */
1550 if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1551 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1552 /* CONTROLLER ERROR */
1553 }
1554 #else
1555 splx(s);
1556 #endif
1557 kbdc_lock(kbdc, FALSE);
1558
1559 return 0;
1560 }
1561
1562 static int
1563 get_kbd_id(KBDC kbdc)
1564 {
1565 int id1, id2;
1566
1567 empty_both_buffers(kbdc, 10);
1568 id1 = id2 = -1;
1569 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1570 return -1;
1571
1572 DELAY(10000); /* 10 msec delay */
1573 id1 = read_kbd_data(kbdc);
1574 if (id1 != -1)
1575 id2 = read_kbd_data(kbdc);
1576
1577 if ((id1 == -1) || (id2 == -1)) {
1578 empty_both_buffers(kbdc, 10);
1579 test_controller(kbdc);
1580 test_kbd_port(kbdc);
1581 return -1;
1582 }
1583 return ((id2 << 8) | id1);
1584 }
1585
1586 static int delays[] = { 250, 500, 750, 1000 };
1587 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1588 68, 76, 84, 92, 100, 110, 118, 126,
1589 136, 152, 168, 184, 200, 220, 236, 252,
1590 272, 304, 336, 368, 400, 440, 472, 504 };
1591
1592 static int
1593 typematic_delay(int i)
1594 {
1595 return delays[(i >> 5) & 3];
1596 }
1597
1598 static int
1599 typematic_rate(int i)
1600 {
1601 return rates[i & 0x1f];
1602 }
1603
1604 static int
1605 typematic(int delay, int rate)
1606 {
1607 int value;
1608 int i;
1609
1610 for (i = nitems(delays) - 1; i > 0; --i) {
1611 if (delay >= delays[i])
1612 break;
1613 }
1614 value = i << 5;
1615 for (i = nitems(rates) - 1; i > 0; --i) {
1616 if (rate >= rates[i])
1617 break;
1618 }
1619 value |= i;
1620 return value;
1621 }
Cache object: c8d0ddd22ce3a751ca7bafd7869b1681
|