2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1996-1999
5 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/syslog.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
49 #if defined(__amd64__)
50 #include <machine/clock.h>
53 #include <dev/atkbdc/atkbdcreg.h>
55 #include <isa/isareg.h>
59 #define MAXKBDC 1 /* XXX */
64 #define MAX(x, y) ((x) > (y) ? (x) : (y))
67 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
68 #define availq(q) ((q)->head != (q)->tail)
70 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
72 #define emptyq(q) ((q)->tail = (q)->head = 0)
75 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
76 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
77 #define write_data(k, d) \
78 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
79 #define write_command(k, d) \
80 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
85 * We always need at least one copy of the kbdc_softc struct for the
86 * low-level console. As the low-level console accesses the keyboard
87 * controller before kbdc, and all other devices, is probed, we
88 * statically allocate one entry. XXX
90 static atkbdc_softc_t default_kbdc;
91 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
93 static int verbose = KBDIO_DEBUG;
95 /* function prototypes */
97 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
98 bus_space_handle_t h0, bus_space_handle_t h1);
99 static int addq(kqueue *q, int c);
100 static int removeq(kqueue *q);
101 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
102 static int wait_for_data(atkbdc_softc_t *kbdc);
103 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
105 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
108 struct atkbdc_quirks {
109 const char* bios_vendor;
115 static struct atkbdc_quirks quirks[] = {
116 {"coreboot", NULL, NULL,
117 KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT |
118 KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT},
120 {NULL, NULL, NULL, 0}
123 #define QUIRK_STR_MATCH(s1, s2) (s1 == NULL || \
124 (s2 != NULL && !strcmp(s1, s2)))
127 atkbdc_getquirks(void)
130 char* bios_vendor = kern_getenv("smbios.bios.vendor");
131 char* maker = kern_getenv("smbios.system.maker");
132 char* product = kern_getenv("smbios.system.product");
134 for (i=0; quirks[i].quirk != 0; ++i)
135 if (QUIRK_STR_MATCH(quirks[i].bios_vendor, bios_vendor) &&
136 QUIRK_STR_MATCH(quirks[i].maker, maker) &&
137 QUIRK_STR_MATCH(quirks[i].product, product))
138 return (quirks[i].quirk);
144 *atkbdc_get_softc(int unit)
148 if (unit >= nitems(atkbdc_softc))
150 sc = atkbdc_softc[unit];
152 sc = atkbdc_softc[unit]
153 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
161 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
163 if (rman_get_start(port0) <= 0)
165 if (rman_get_start(port1) <= 0)
171 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
172 struct resource *port1)
174 return atkbdc_setup(sc, rman_get_bustag(port0),
175 rman_get_bushandle(port0),
176 rman_get_bushandle(port1));
179 /* the backdoor to the keyboard controller! XXX */
181 atkbdc_configure(void)
184 bus_space_handle_t h0;
185 bus_space_handle_t h1;
186 #if defined(__i386__) || defined(__amd64__)
193 /* XXX: tag should be passed from the caller */
194 #if defined(__amd64__) || defined(__i386__)
195 tag = X86_BUS_SPACE_IO;
201 resource_int_value("atkbdc", 0, "port", &port0);
202 port1 = IO_KBD + KBD_STATUS_PORT;
204 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
205 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
207 h0 = (bus_space_handle_t)port0;
208 h1 = (bus_space_handle_t)port1;
211 #if defined(__i386__) || defined(__amd64__)
213 * Check if we really have AT keyboard controller. Poll status
214 * register until we get "all clear" indication. If no such
215 * indication comes, it probably means that there is no AT
216 * keyboard controller present. Give up in such case. Check relies
217 * on the fact that reading from non-existing in/out port returns
218 * 0xff on i386. May or may not be true on other platforms.
220 flags = intr_disable();
221 for (i = 0; i != 65535; i++) {
222 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
230 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
234 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
235 bus_space_handle_t h1)
237 #if defined(__amd64__)
238 u_int64_t tscval[3], read_delay;
242 if (sc->ioh0 == 0) { /* XXX */
243 sc->command_byte = -1;
244 sc->command_mask = 0;
246 sc->kbd.head = sc->kbd.tail = 0;
247 sc->aux.head = sc->aux.tail = 0;
248 sc->aux_mux_enabled = FALSE;
250 sc->kbd.call_count = 0;
251 sc->kbd.qcount = sc->kbd.max_qcount = 0;
252 sc->aux.call_count = 0;
253 sc->aux.qcount = sc->aux.max_qcount = 0;
260 #if defined(__amd64__)
262 * On certain chipsets AT keyboard controller isn't present and is
263 * emulated by BIOS using SMI interrupt. On those chipsets reading
264 * from the status port may be thousand times slower than usually.
265 * Sometimes this emilation is not working properly resulting in
266 * commands timing our and since we assume that inb() operation
267 * takes very little time to complete we need to adjust number of
268 * retries to keep waiting time within a designed limits (100ms).
269 * Measure time it takes to make read_status() call and adjust
270 * number of retries accordingly.
272 flags = intr_disable();
279 read_delay = tscval[1] - tscval[0];
280 read_delay /= (tscval[2] - tscval[1]) / 1000;
281 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
285 sc->quirks = atkbdc_getquirks();
290 /* open a keyboard controller */
292 atkbdc_open(int unit)
298 if ((atkbdc_softc[unit]->port0 != NULL)
299 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
300 return atkbdc_softc[unit];
305 * I/O access arbitration in `kbdio'
307 * The `kbdio' module uses a simplistic convention to arbitrate
308 * I/O access to the controller/keyboard/mouse. The convention requires
309 * close cooperation of the calling device driver.
311 * The device drivers which utilize the `kbdio' module are assumed to
312 * have the following set of routines.
313 * a. An interrupt handler (the bottom half of the driver).
314 * b. Timeout routines which may briefly poll the keyboard controller.
315 * c. Routines outside interrupt context (the top half of the driver).
316 * They should follow the rules below:
317 * 1. The interrupt handler may assume that it always has full access
318 * to the controller/keyboard/mouse.
319 * 2. The other routines must issue `spltty()' if they wish to
320 * prevent the interrupt handler from accessing
321 * the controller/keyboard/mouse.
322 * 3. The timeout routines and the top half routines of the device driver
323 * arbitrate I/O access by observing the lock flag in `kbdio'.
324 * The flag is manipulated via `kbdc_lock()'; when one wants to
325 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
326 * the call returns with TRUE. Otherwise the caller must back off.
327 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
328 * is finished. This mechanism does not prevent the interrupt
329 * handler from being invoked at any time and carrying out I/O.
330 * Therefore, `spltty()' must be strategically placed in the device
331 * driver code. Also note that the timeout routine may interrupt
332 * `kbdc_lock()' called by the top half of the driver, but this
333 * interruption is OK so long as the timeout routine observes
335 * 4. The interrupt and timeout routines should not extend I/O operation
336 * across more than one interrupt or timeout; they must complete any
337 * necessary I/O operation within one invocation of the routine.
338 * This means that if the timeout routine acquires the lock flag,
339 * it must reset the flag to FALSE before it returns.
342 /* set/reset polling lock */
344 kbdc_lock(KBDC p, int lock)
351 return (prevlock != lock);
354 /* check if any data is waiting to be processed */
356 kbdc_data_ready(KBDC p)
358 return (availq(&p->kbd) || availq(&p->aux)
359 || (read_status(p) & KBDS_ANY_BUFFER_FULL));
362 /* queuing functions */
365 addq(kqueue *q, int c)
367 if (nextq(q->tail) != q->head) {
369 q->tail = nextq(q->tail);
373 if (q->qcount > q->max_qcount)
374 q->max_qcount = q->qcount;
386 if (q->tail != q->head) {
388 q->head = nextq(q->head);
398 * device I/O routines
401 wait_while_controller_busy(struct atkbdc_softc *kbdc)
406 /* CPU will stay inside the loop for 100msec at most */
409 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
410 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
411 DELAY(KBDD_DELAYTIME);
412 addq(&kbdc->kbd, read_data(kbdc));
413 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
414 DELAY(KBDD_DELAYTIME);
415 addq(&kbdc->aux, read_data(kbdc));
417 DELAY(KBDC_DELAYTIME);
425 * wait for any data; whether it's from the controller,
426 * the keyboard, or the aux device.
429 wait_for_data(struct atkbdc_softc *kbdc)
434 /* CPU will stay inside the loop for 200msec at most */
435 retry = kbdc->retry * 2;
437 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
438 DELAY(KBDC_DELAYTIME);
442 DELAY(KBDD_DELAYTIME);
446 /* wait for data from the keyboard */
448 wait_for_kbd_data(struct atkbdc_softc *kbdc)
453 /* CPU will stay inside the loop for 200msec at most */
454 retry = kbdc->retry * 2;
456 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
457 != KBDS_KBD_BUFFER_FULL) {
458 if (f == KBDS_AUX_BUFFER_FULL) {
459 DELAY(KBDD_DELAYTIME);
460 addq(&kbdc->aux, read_data(kbdc));
462 DELAY(KBDC_DELAYTIME);
466 DELAY(KBDD_DELAYTIME);
471 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
472 * queue anything else.
475 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
481 /* CPU will stay inside the loop for 200msec at most */
482 retry = kbdc->retry * 2;
484 while (retry-- > 0) {
485 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
486 DELAY(KBDD_DELAYTIME);
488 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
489 if ((b == KBD_ACK) || (b == KBD_RESEND)
490 || (b == KBD_RESET_FAIL))
493 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
497 DELAY(KBDC_DELAYTIME);
502 /* wait for data from the aux device */
504 wait_for_aux_data(struct atkbdc_softc *kbdc)
509 /* CPU will stay inside the loop for 200msec at most */
510 retry = kbdc->retry * 2;
512 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
513 != KBDS_AUX_BUFFER_FULL) {
514 if (f == KBDS_KBD_BUFFER_FULL) {
515 DELAY(KBDD_DELAYTIME);
516 addq(&kbdc->kbd, read_data(kbdc));
518 DELAY(KBDC_DELAYTIME);
522 DELAY(KBDD_DELAYTIME);
527 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
528 * queue anything else.
531 wait_for_aux_ack(struct atkbdc_softc *kbdc)
537 /* CPU will stay inside the loop for 200msec at most */
538 retry = kbdc->retry * 2;
540 while (retry-- > 0) {
541 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
542 DELAY(KBDD_DELAYTIME);
544 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
545 if ((b == PSM_ACK) || (b == PSM_RESEND)
546 || (b == PSM_RESET_FAIL))
549 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
553 DELAY(KBDC_DELAYTIME);
558 /* write a one byte command to the controller */
560 write_controller_command(KBDC p, int c)
562 if (!wait_while_controller_busy(p))
568 /* write a one byte data to the controller */
570 write_controller_data(KBDC p, int c)
572 if (!wait_while_controller_busy(p))
578 /* write a one byte keyboard command */
580 write_kbd_command(KBDC p, int c)
582 if (!wait_while_controller_busy(p))
588 /* write a one byte auxiliary device command */
590 write_aux_command(KBDC p, int c)
594 f = aux_mux_is_enabled(p) ?
595 KBDC_WRITE_TO_AUX_MUX + p->aux_mux_port : KBDC_WRITE_TO_AUX;
597 if (!write_controller_command(p, f))
599 return write_controller_data(p, c);
602 /* send a command to the keyboard and wait for ACK */
604 send_kbd_command(KBDC p, int c)
606 int retry = KBD_MAXRETRY;
609 while (retry-- > 0) {
610 if (!write_kbd_command(p, c))
612 res = wait_for_kbd_ack(p);
619 /* send a command to the auxiliary device and wait for ACK */
621 send_aux_command(KBDC p, int c)
623 int retry = KBD_MAXRETRY;
626 while (retry-- > 0) {
627 if (!write_aux_command(p, c))
631 * The aux device may have already sent one or two bytes of
632 * status data, when a command is received. It will immediately
633 * stop data transmission, thus, leaving an incomplete data
634 * packet in our buffer. We have to discard any unprocessed
635 * data in order to remove such packets. Well, we may remove
636 * unprocessed, but necessary data byte as well...
639 res = wait_for_aux_ack(p);
646 /* send a command and a data to the keyboard, wait for ACKs */
648 send_kbd_command_and_data(KBDC p, int c, int d)
653 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
654 if (!write_kbd_command(p, c))
656 res = wait_for_kbd_ack(p);
659 else if (res != KBD_RESEND)
665 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
666 if (!write_kbd_command(p, d))
668 res = wait_for_kbd_ack(p);
669 if (res != KBD_RESEND)
675 /* send a command and a data to the auxiliary device, wait for ACKs */
677 send_aux_command_and_data(KBDC p, int c, int d)
682 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
683 if (!write_aux_command(p, c))
686 res = wait_for_aux_ack(p);
689 else if (res != PSM_RESEND)
695 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
696 if (!write_aux_command(p, d))
698 res = wait_for_aux_ack(p);
699 if (res != PSM_RESEND)
706 * read one byte from any source; whether from the controller,
707 * the keyboard, or the aux device
710 read_controller_data(KBDC p)
713 return removeq(&p->kbd);
715 return removeq(&p->aux);
716 if (!wait_for_data(p))
717 return -1; /* timeout */
725 /* read one byte from the keyboard */
727 read_kbd_data(KBDC p)
732 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
733 "aux q: %d calls, max %d chars\n",
734 p->kbd.call_count, p->kbd.max_qcount,
735 p->aux.call_count, p->aux.max_qcount);
740 return removeq(&p->kbd);
741 if (!wait_for_kbd_data(p))
742 return -1; /* timeout */
746 /* read one byte from the keyboard, but return immediately if
750 read_kbd_data_no_wait(KBDC p)
757 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
758 "aux q: %d calls, max %d chars\n",
759 p->kbd.call_count, p->kbd.max_qcount,
760 p->aux.call_count, p->aux.max_qcount);
765 return removeq(&p->kbd);
766 f = read_status(p) & KBDS_BUFFER_FULL;
767 if (f == KBDS_AUX_BUFFER_FULL) {
768 DELAY(KBDD_DELAYTIME);
769 addq(&p->aux, read_data(p));
770 f = read_status(p) & KBDS_BUFFER_FULL;
772 if (f == KBDS_KBD_BUFFER_FULL) {
773 DELAY(KBDD_DELAYTIME);
776 return -1; /* no data */
779 /* read one byte from the aux device */
781 read_aux_data(KBDC p)
784 return removeq(&p->aux);
785 if (!wait_for_aux_data(p))
786 return -1; /* timeout */
790 /* read one byte from the aux device, but return immediately if
794 read_aux_data_no_wait(KBDC p)
799 return removeq(&p->aux);
800 f = read_status(p) & KBDS_BUFFER_FULL;
801 if (f == KBDS_KBD_BUFFER_FULL) {
802 DELAY(KBDD_DELAYTIME);
803 addq(&p->kbd, read_data(p));
804 f = read_status(p) & KBDS_BUFFER_FULL;
806 if (f == KBDS_AUX_BUFFER_FULL) {
807 DELAY(KBDD_DELAYTIME);
810 return -1; /* no data */
813 /* discard data from the keyboard */
815 empty_kbd_buffer(KBDC p, int wait)
826 for (t = wait; t > 0; ) {
827 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
828 DELAY(KBDD_DELAYTIME);
830 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
845 if ((c1 > 0) || (c2 > 0))
846 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
852 /* discard data from the aux device */
854 empty_aux_buffer(KBDC p, int wait)
865 for (t = wait; t > 0; ) {
866 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
867 DELAY(KBDD_DELAYTIME);
869 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
884 if ((c1 > 0) || (c2 > 0))
885 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
891 /* discard any data from the keyboard or the aux device */
893 empty_both_buffers(KBDC p, int wait)
904 for (t = wait; t > 0; ) {
905 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
906 DELAY(KBDD_DELAYTIME);
909 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
920 * Some systems (Intel/IBM blades) do not have keyboard devices and
921 * will thus hang in this procedure. Time out after delta seconds to
922 * avoid this hang -- the keyboard attach will fail later on.
924 waited += (delta * 1000);
925 if (waited == (delta * 1000000))
931 if ((c1 > 0) || (c2 > 0))
932 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
939 /* keyboard and mouse device control */
941 /* NOTE: enable the keyboard port but disable the keyboard
942 * interrupt before calling "reset_kbd()".
947 int retry = KBD_MAXRETRY;
948 int again = KBD_MAXWAIT;
949 int c = KBD_RESEND; /* keep the compiler happy */
951 while (retry-- > 0) {
952 empty_both_buffers(p, 10);
953 if (!write_kbd_command(p, KBDC_RESET_KBD))
956 c = read_controller_data(p);
957 if (verbose || bootverbose)
958 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
959 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
965 while (again-- > 0) {
966 /* wait awhile, well, in fact we must wait quite loooooooooooong */
967 DELAY(KBD_RESETDELAY*1000);
968 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
969 if (c != -1) /* wait again if the controller is not ready */
972 if (verbose || bootverbose)
973 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
974 if (c != KBD_RESET_DONE)
979 /* NOTE: enable the aux port but disable the aux interrupt
980 * before calling `reset_aux_dev()'.
983 reset_aux_dev(KBDC p)
985 int retry = KBD_MAXRETRY;
986 int again = KBD_MAXWAIT;
987 int c = PSM_RESEND; /* keep the compiler happy */
989 while (retry-- > 0) {
990 empty_both_buffers(p, 10);
991 if (!write_aux_command(p, PSMC_RESET_DEV))
994 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
995 for (again = KBD_MAXWAIT; again > 0; --again) {
996 DELAY(KBD_RESETDELAY*1000);
997 c = read_aux_data_no_wait(p);
1001 if (verbose || bootverbose)
1002 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1003 if (c == PSM_ACK) /* aux dev is about to reset... */
1009 for (again = KBD_MAXWAIT; again > 0; --again) {
1010 /* wait awhile, well, quite looooooooooooong */
1011 DELAY(KBD_RESETDELAY*1000);
1012 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1013 if (c != -1) /* wait again if the controller is not ready */
1016 if (verbose || bootverbose)
1017 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1018 if (c != PSM_RESET_DONE) /* reset status */
1021 c = read_aux_data(p); /* device ID */
1022 if (verbose || bootverbose)
1023 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1024 /* NOTE: we could check the device ID now, but leave it later... */
1028 /* controller diagnostics and setup */
1031 test_controller(KBDC p)
1033 int retry = KBD_MAXRETRY;
1034 int again = KBD_MAXWAIT;
1035 int c = KBD_DIAG_FAIL;
1037 while (retry-- > 0) {
1038 empty_both_buffers(p, 10);
1039 if (write_controller_command(p, KBDC_DIAGNOSE))
1046 while (again-- > 0) {
1048 DELAY(KBD_RESETDELAY*1000);
1049 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1050 if (c != -1) /* wait again if the controller is not ready */
1053 if (verbose || bootverbose)
1054 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1055 return (c == KBD_DIAG_DONE);
1059 test_kbd_port(KBDC p)
1061 int retry = KBD_MAXRETRY;
1062 int again = KBD_MAXWAIT;
1065 while (retry-- > 0) {
1066 empty_both_buffers(p, 10);
1067 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1074 while (again-- > 0) {
1075 c = read_controller_data(p);
1076 if (c != -1) /* try again if the controller is not ready */
1079 if (verbose || bootverbose)
1080 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1085 test_aux_port(KBDC p)
1087 int retry = KBD_MAXRETRY;
1088 int again = KBD_MAXWAIT;
1091 while (retry-- > 0) {
1092 empty_both_buffers(p, 10);
1093 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1100 while (again-- > 0) {
1101 c = read_controller_data(p);
1102 if (c != -1) /* try again if the controller is not ready */
1105 if (verbose || bootverbose)
1106 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1111 kbdc_get_device_mask(KBDC p)
1113 return p->command_mask;
1117 kbdc_set_device_mask(KBDC p, int mask)
1120 mask & (((p->quirks & KBDC_QUIRK_KEEP_ACTIVATED)
1121 ? 0 : KBD_KBD_CONTROL_BITS) | KBD_AUX_CONTROL_BITS);
1125 get_controller_command_byte(KBDC p)
1127 if (p->command_byte != -1)
1128 return p->command_byte;
1129 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1132 p->command_byte = read_controller_data(p);
1133 return p->command_byte;
1137 set_controller_command_byte(KBDC p, int mask, int command)
1139 if (get_controller_command_byte(p) == -1)
1142 command = (p->command_byte & ~mask) | (command & mask);
1143 if (command & KBD_DISABLE_KBD_PORT) {
1144 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1147 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1149 if (!write_controller_data(p, command))
1151 p->command_byte = command;
1154 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1161 * Rudimentary support for active PS/2 AUX port multiplexing.
1162 * Only write commands can be routed to a selected AUX port.
1163 * Source port of data processed by read commands is totally ignored.
1166 set_aux_mux_state(KBDC p, int enabled)
1168 int command, version;
1170 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1171 write_controller_data(p, 0xF0) == 0 ||
1172 read_controller_data(p) != 0xF0)
1175 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1176 write_controller_data(p, 0x56) == 0 ||
1177 read_controller_data(p) != 0x56)
1180 command = enabled ? 0xa4 : 0xa5;
1181 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1182 write_controller_data(p, command) == 0 ||
1183 (version = read_controller_data(p)) == command)
1190 set_active_aux_mux_port(KBDC p, int port)
1193 if (!aux_mux_is_enabled(p))
1196 if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS)
1199 p->aux_mux_port = port;
1204 /* Checks for active multiplexing support and enables it */
1206 enable_aux_mux(KBDC p)
1210 version = set_aux_mux_state(p, TRUE);
1212 p->aux_mux_enabled = TRUE;
1213 set_active_aux_mux_port(p, 0);
1220 disable_aux_mux(KBDC p)
1223 p->aux_mux_enabled = FALSE;
1225 return (set_aux_mux_state(p, FALSE));
1229 aux_mux_is_enabled(KBDC p)
1232 return (p->aux_mux_enabled);