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>
56 #include <dev/ofw/openfirm.h>
57 #include <machine/bus_private.h>
58 #include <machine/ofw_machdep.h>
60 #include <isa/isareg.h>
65 #define MAXKBDC 1 /* XXX */
70 #define MAX(x, y) ((x) > (y) ? (x) : (y))
73 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
74 #define availq(q) ((q)->head != (q)->tail)
76 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
78 #define emptyq(q) ((q)->tail = (q)->head = 0)
81 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
82 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
83 #define write_data(k, d) \
84 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
85 #define write_command(k, d) \
86 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
91 * We always need at least one copy of the kbdc_softc struct for the
92 * low-level console. As the low-level console accesses the keyboard
93 * controller before kbdc, and all other devices, is probed, we
94 * statically allocate one entry. XXX
96 static atkbdc_softc_t default_kbdc;
97 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
99 static int verbose = KBDIO_DEBUG;
102 static struct bus_space_tag atkbdc_bst_store[MAXKBDC];
105 /* function prototypes */
107 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
108 bus_space_handle_t h0, bus_space_handle_t h1);
109 static int addq(kqueue *q, int c);
110 static int removeq(kqueue *q);
111 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
112 static int wait_for_data(atkbdc_softc_t *kbdc);
113 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
114 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
115 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
116 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
118 struct atkbdc_quirks {
119 const char* bios_vendor;
125 static struct atkbdc_quirks quirks[] = {
126 {"coreboot", NULL, NULL,
127 KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT |
128 KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT},
130 {NULL, NULL, NULL, 0}
133 #define QUIRK_STR_MATCH(s1, s2) (s1 == NULL || \
134 (s2 != NULL && !strcmp(s1, s2)))
137 atkbdc_getquirks(void)
140 char* bios_vendor = kern_getenv("smbios.bios.vendor");
141 char* maker = kern_getenv("smbios.system.maker");
142 char* product = kern_getenv("smbios.system.product");
144 for (i=0; quirks[i].quirk != 0; ++i)
145 if (QUIRK_STR_MATCH(quirks[i].bios_vendor, bios_vendor) &&
146 QUIRK_STR_MATCH(quirks[i].maker, maker) &&
147 QUIRK_STR_MATCH(quirks[i].product, product))
148 return (quirks[i].quirk);
154 *atkbdc_get_softc(int unit)
158 if (unit >= nitems(atkbdc_softc))
160 sc = atkbdc_softc[unit];
162 sc = atkbdc_softc[unit]
163 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
171 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
173 if (rman_get_start(port0) <= 0)
175 if (rman_get_start(port1) <= 0)
181 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
182 struct resource *port1)
184 return atkbdc_setup(sc, rman_get_bustag(port0),
185 rman_get_bushandle(port0),
186 rman_get_bushandle(port1));
189 /* the backdoor to the keyboard controller! XXX */
191 atkbdc_configure(void)
194 bus_space_handle_t h0;
195 bus_space_handle_t h1;
196 #if defined(__i386__) || defined(__amd64__)
202 phandle_t chosen, node;
212 /* XXX: tag should be passed from the caller */
213 #if defined(__amd64__) || defined(__i386__)
214 tag = X86_BUS_SPACE_IO;
215 #elif defined(__sparc64__)
216 tag = &atkbdc_bst_store[0];
222 if ((chosen = OF_finddevice("/chosen")) == -1)
224 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
226 if ((node = OF_instance_to_package(stdin)) == -1)
228 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
230 name[sizeof(name) - 1] = '\0';
231 if (strcmp(name, "kb_ps2") != 0)
234 * The stdin handle points to an instance of a PS/2 keyboard
235 * package but we want the 8042 controller, which is the parent
236 * of that keyboard node.
238 if ((node = OF_parent(node)) == 0)
240 if (OF_decode_addr(node, 0, &space, &port0) != 0)
242 h0 = sparc64_fake_bustag(space, port0, tag);
243 bus_space_subregion(tag, h0, KBD_DATA_PORT, 1, &h0);
244 if (OF_decode_addr(node, 1, &space, &port1) != 0)
246 h1 = sparc64_fake_bustag(space, port1, tag);
247 bus_space_subregion(tag, h1, KBD_STATUS_PORT, 1, &h1);
250 resource_int_value("atkbdc", 0, "port", &port0);
251 port1 = IO_KBD + KBD_STATUS_PORT;
253 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
254 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
256 h0 = (bus_space_handle_t)port0;
257 h1 = (bus_space_handle_t)port1;
261 #if defined(__i386__) || defined(__amd64__)
263 * Check if we really have AT keyboard controller. Poll status
264 * register until we get "all clear" indication. If no such
265 * indication comes, it probably means that there is no AT
266 * keyboard controller present. Give up in such case. Check relies
267 * on the fact that reading from non-existing in/out port returns
268 * 0xff on i386. May or may not be true on other platforms.
270 flags = intr_disable();
271 for (i = 0; i != 65535; i++) {
272 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
280 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
284 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
285 bus_space_handle_t h1)
287 #if defined(__amd64__)
288 u_int64_t tscval[3], read_delay;
292 if (sc->ioh0 == 0) { /* XXX */
293 sc->command_byte = -1;
294 sc->command_mask = 0;
296 sc->kbd.head = sc->kbd.tail = 0;
297 sc->aux.head = sc->aux.tail = 0;
298 sc->aux_mux_enabled = FALSE;
300 sc->kbd.call_count = 0;
301 sc->kbd.qcount = sc->kbd.max_qcount = 0;
302 sc->aux.call_count = 0;
303 sc->aux.qcount = sc->aux.max_qcount = 0;
310 #if defined(__amd64__)
312 * On certain chipsets AT keyboard controller isn't present and is
313 * emulated by BIOS using SMI interrupt. On those chipsets reading
314 * from the status port may be thousand times slower than usually.
315 * Sometimes this emilation is not working properly resulting in
316 * commands timing our and since we assume that inb() operation
317 * takes very little time to complete we need to adjust number of
318 * retries to keep waiting time within a designed limits (100ms).
319 * Measure time it takes to make read_status() call and adjust
320 * number of retries accordingly.
322 flags = intr_disable();
329 read_delay = tscval[1] - tscval[0];
330 read_delay /= (tscval[2] - tscval[1]) / 1000;
331 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
335 sc->quirks = atkbdc_getquirks();
340 /* open a keyboard controller */
342 atkbdc_open(int unit)
348 if ((atkbdc_softc[unit]->port0 != NULL)
349 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
350 return atkbdc_softc[unit];
355 * I/O access arbitration in `kbdio'
357 * The `kbdio' module uses a simplistic convention to arbitrate
358 * I/O access to the controller/keyboard/mouse. The convention requires
359 * close cooperation of the calling device driver.
361 * The device drivers which utilize the `kbdio' module are assumed to
362 * have the following set of routines.
363 * a. An interrupt handler (the bottom half of the driver).
364 * b. Timeout routines which may briefly poll the keyboard controller.
365 * c. Routines outside interrupt context (the top half of the driver).
366 * They should follow the rules below:
367 * 1. The interrupt handler may assume that it always has full access
368 * to the controller/keyboard/mouse.
369 * 2. The other routines must issue `spltty()' if they wish to
370 * prevent the interrupt handler from accessing
371 * the controller/keyboard/mouse.
372 * 3. The timeout routines and the top half routines of the device driver
373 * arbitrate I/O access by observing the lock flag in `kbdio'.
374 * The flag is manipulated via `kbdc_lock()'; when one wants to
375 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
376 * the call returns with TRUE. Otherwise the caller must back off.
377 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
378 * is finished. This mechanism does not prevent the interrupt
379 * handler from being invoked at any time and carrying out I/O.
380 * Therefore, `spltty()' must be strategically placed in the device
381 * driver code. Also note that the timeout routine may interrupt
382 * `kbdc_lock()' called by the top half of the driver, but this
383 * interruption is OK so long as the timeout routine observes
385 * 4. The interrupt and timeout routines should not extend I/O operation
386 * across more than one interrupt or timeout; they must complete any
387 * necessary I/O operation within one invocation of the routine.
388 * This means that if the timeout routine acquires the lock flag,
389 * it must reset the flag to FALSE before it returns.
392 /* set/reset polling lock */
394 kbdc_lock(KBDC p, int lock)
401 return (prevlock != lock);
404 /* check if any data is waiting to be processed */
406 kbdc_data_ready(KBDC p)
408 return (availq(&p->kbd) || availq(&p->aux)
409 || (read_status(p) & KBDS_ANY_BUFFER_FULL));
412 /* queuing functions */
415 addq(kqueue *q, int c)
417 if (nextq(q->tail) != q->head) {
419 q->tail = nextq(q->tail);
423 if (q->qcount > q->max_qcount)
424 q->max_qcount = q->qcount;
436 if (q->tail != q->head) {
438 q->head = nextq(q->head);
448 * device I/O routines
451 wait_while_controller_busy(struct atkbdc_softc *kbdc)
456 /* CPU will stay inside the loop for 100msec at most */
459 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
460 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
461 DELAY(KBDD_DELAYTIME);
462 addq(&kbdc->kbd, read_data(kbdc));
463 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
464 DELAY(KBDD_DELAYTIME);
465 addq(&kbdc->aux, read_data(kbdc));
467 DELAY(KBDC_DELAYTIME);
475 * wait for any data; whether it's from the controller,
476 * the keyboard, or the aux device.
479 wait_for_data(struct atkbdc_softc *kbdc)
484 /* CPU will stay inside the loop for 200msec at most */
485 retry = kbdc->retry * 2;
487 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
488 DELAY(KBDC_DELAYTIME);
492 DELAY(KBDD_DELAYTIME);
496 /* wait for data from the keyboard */
498 wait_for_kbd_data(struct atkbdc_softc *kbdc)
503 /* CPU will stay inside the loop for 200msec at most */
504 retry = kbdc->retry * 2;
506 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
507 != KBDS_KBD_BUFFER_FULL) {
508 if (f == KBDS_AUX_BUFFER_FULL) {
509 DELAY(KBDD_DELAYTIME);
510 addq(&kbdc->aux, read_data(kbdc));
512 DELAY(KBDC_DELAYTIME);
516 DELAY(KBDD_DELAYTIME);
521 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
522 * queue anything else.
525 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
531 /* CPU will stay inside the loop for 200msec at most */
532 retry = kbdc->retry * 2;
534 while (retry-- > 0) {
535 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
536 DELAY(KBDD_DELAYTIME);
538 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
539 if ((b == KBD_ACK) || (b == KBD_RESEND)
540 || (b == KBD_RESET_FAIL))
543 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
547 DELAY(KBDC_DELAYTIME);
552 /* wait for data from the aux device */
554 wait_for_aux_data(struct atkbdc_softc *kbdc)
559 /* CPU will stay inside the loop for 200msec at most */
560 retry = kbdc->retry * 2;
562 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
563 != KBDS_AUX_BUFFER_FULL) {
564 if (f == KBDS_KBD_BUFFER_FULL) {
565 DELAY(KBDD_DELAYTIME);
566 addq(&kbdc->kbd, read_data(kbdc));
568 DELAY(KBDC_DELAYTIME);
572 DELAY(KBDD_DELAYTIME);
577 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
578 * queue anything else.
581 wait_for_aux_ack(struct atkbdc_softc *kbdc)
587 /* CPU will stay inside the loop for 200msec at most */
588 retry = kbdc->retry * 2;
590 while (retry-- > 0) {
591 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
592 DELAY(KBDD_DELAYTIME);
594 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
595 if ((b == PSM_ACK) || (b == PSM_RESEND)
596 || (b == PSM_RESET_FAIL))
599 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
603 DELAY(KBDC_DELAYTIME);
608 /* write a one byte command to the controller */
610 write_controller_command(KBDC p, int c)
612 if (!wait_while_controller_busy(p))
618 /* write a one byte data to the controller */
620 write_controller_data(KBDC p, int c)
622 if (!wait_while_controller_busy(p))
628 /* write a one byte keyboard command */
630 write_kbd_command(KBDC p, int c)
632 if (!wait_while_controller_busy(p))
638 /* write a one byte auxiliary device command */
640 write_aux_command(KBDC p, int c)
644 f = aux_mux_is_enabled(p) ?
645 KBDC_WRITE_TO_AUX_MUX + p->aux_mux_port : KBDC_WRITE_TO_AUX;
647 if (!write_controller_command(p, f))
649 return write_controller_data(p, c);
652 /* send a command to the keyboard and wait for ACK */
654 send_kbd_command(KBDC p, int c)
656 int retry = KBD_MAXRETRY;
659 while (retry-- > 0) {
660 if (!write_kbd_command(p, c))
662 res = wait_for_kbd_ack(p);
669 /* send a command to the auxiliary device and wait for ACK */
671 send_aux_command(KBDC p, int c)
673 int retry = KBD_MAXRETRY;
676 while (retry-- > 0) {
677 if (!write_aux_command(p, c))
681 * The aux device may have already sent one or two bytes of
682 * status data, when a command is received. It will immediately
683 * stop data transmission, thus, leaving an incomplete data
684 * packet in our buffer. We have to discard any unprocessed
685 * data in order to remove such packets. Well, we may remove
686 * unprocessed, but necessary data byte as well...
689 res = wait_for_aux_ack(p);
696 /* send a command and a data to the keyboard, wait for ACKs */
698 send_kbd_command_and_data(KBDC p, int c, int d)
703 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
704 if (!write_kbd_command(p, c))
706 res = wait_for_kbd_ack(p);
709 else if (res != KBD_RESEND)
715 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
716 if (!write_kbd_command(p, d))
718 res = wait_for_kbd_ack(p);
719 if (res != KBD_RESEND)
725 /* send a command and a data to the auxiliary device, wait for ACKs */
727 send_aux_command_and_data(KBDC p, int c, int d)
732 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
733 if (!write_aux_command(p, c))
736 res = wait_for_aux_ack(p);
739 else if (res != PSM_RESEND)
745 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
746 if (!write_aux_command(p, d))
748 res = wait_for_aux_ack(p);
749 if (res != PSM_RESEND)
756 * read one byte from any source; whether from the controller,
757 * the keyboard, or the aux device
760 read_controller_data(KBDC p)
763 return removeq(&p->kbd);
765 return removeq(&p->aux);
766 if (!wait_for_data(p))
767 return -1; /* timeout */
775 /* read one byte from the keyboard */
777 read_kbd_data(KBDC p)
782 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
783 "aux q: %d calls, max %d chars\n",
784 p->kbd.call_count, p->kbd.max_qcount,
785 p->aux.call_count, p->aux.max_qcount);
790 return removeq(&p->kbd);
791 if (!wait_for_kbd_data(p))
792 return -1; /* timeout */
796 /* read one byte from the keyboard, but return immediately if
800 read_kbd_data_no_wait(KBDC p)
807 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
808 "aux q: %d calls, max %d chars\n",
809 p->kbd.call_count, p->kbd.max_qcount,
810 p->aux.call_count, p->aux.max_qcount);
815 return removeq(&p->kbd);
816 f = read_status(p) & KBDS_BUFFER_FULL;
817 if (f == KBDS_AUX_BUFFER_FULL) {
818 DELAY(KBDD_DELAYTIME);
819 addq(&p->aux, read_data(p));
820 f = read_status(p) & KBDS_BUFFER_FULL;
822 if (f == KBDS_KBD_BUFFER_FULL) {
823 DELAY(KBDD_DELAYTIME);
826 return -1; /* no data */
829 /* read one byte from the aux device */
831 read_aux_data(KBDC p)
834 return removeq(&p->aux);
835 if (!wait_for_aux_data(p))
836 return -1; /* timeout */
840 /* read one byte from the aux device, but return immediately if
844 read_aux_data_no_wait(KBDC p)
849 return removeq(&p->aux);
850 f = read_status(p) & KBDS_BUFFER_FULL;
851 if (f == KBDS_KBD_BUFFER_FULL) {
852 DELAY(KBDD_DELAYTIME);
853 addq(&p->kbd, read_data(p));
854 f = read_status(p) & KBDS_BUFFER_FULL;
856 if (f == KBDS_AUX_BUFFER_FULL) {
857 DELAY(KBDD_DELAYTIME);
860 return -1; /* no data */
863 /* discard data from the keyboard */
865 empty_kbd_buffer(KBDC p, int wait)
876 for (t = wait; t > 0; ) {
877 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
878 DELAY(KBDD_DELAYTIME);
880 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
895 if ((c1 > 0) || (c2 > 0))
896 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
902 /* discard data from the aux device */
904 empty_aux_buffer(KBDC p, int wait)
915 for (t = wait; t > 0; ) {
916 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
917 DELAY(KBDD_DELAYTIME);
919 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
934 if ((c1 > 0) || (c2 > 0))
935 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
941 /* discard any data from the keyboard or the aux device */
943 empty_both_buffers(KBDC p, int wait)
954 for (t = wait; t > 0; ) {
955 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
956 DELAY(KBDD_DELAYTIME);
959 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
970 * Some systems (Intel/IBM blades) do not have keyboard devices and
971 * will thus hang in this procedure. Time out after delta seconds to
972 * avoid this hang -- the keyboard attach will fail later on.
974 waited += (delta * 1000);
975 if (waited == (delta * 1000000))
981 if ((c1 > 0) || (c2 > 0))
982 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
989 /* keyboard and mouse device control */
991 /* NOTE: enable the keyboard port but disable the keyboard
992 * interrupt before calling "reset_kbd()".
997 int retry = KBD_MAXRETRY;
998 int again = KBD_MAXWAIT;
999 int c = KBD_RESEND; /* keep the compiler happy */
1001 while (retry-- > 0) {
1002 empty_both_buffers(p, 10);
1003 if (!write_kbd_command(p, KBDC_RESET_KBD))
1006 c = read_controller_data(p);
1007 if (verbose || bootverbose)
1008 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
1009 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
1015 while (again-- > 0) {
1016 /* wait awhile, well, in fact we must wait quite loooooooooooong */
1017 DELAY(KBD_RESETDELAY*1000);
1018 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
1019 if (c != -1) /* wait again if the controller is not ready */
1022 if (verbose || bootverbose)
1023 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
1024 if (c != KBD_RESET_DONE)
1029 /* NOTE: enable the aux port but disable the aux interrupt
1030 * before calling `reset_aux_dev()'.
1033 reset_aux_dev(KBDC p)
1035 int retry = KBD_MAXRETRY;
1036 int again = KBD_MAXWAIT;
1037 int c = PSM_RESEND; /* keep the compiler happy */
1039 while (retry-- > 0) {
1040 empty_both_buffers(p, 10);
1041 if (!write_aux_command(p, PSMC_RESET_DEV))
1044 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1045 for (again = KBD_MAXWAIT; again > 0; --again) {
1046 DELAY(KBD_RESETDELAY*1000);
1047 c = read_aux_data_no_wait(p);
1051 if (verbose || bootverbose)
1052 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1053 if (c == PSM_ACK) /* aux dev is about to reset... */
1059 for (again = KBD_MAXWAIT; again > 0; --again) {
1060 /* wait awhile, well, quite looooooooooooong */
1061 DELAY(KBD_RESETDELAY*1000);
1062 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1063 if (c != -1) /* wait again if the controller is not ready */
1066 if (verbose || bootverbose)
1067 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1068 if (c != PSM_RESET_DONE) /* reset status */
1071 c = read_aux_data(p); /* device ID */
1072 if (verbose || bootverbose)
1073 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1074 /* NOTE: we could check the device ID now, but leave it later... */
1078 /* controller diagnostics and setup */
1081 test_controller(KBDC p)
1083 int retry = KBD_MAXRETRY;
1084 int again = KBD_MAXWAIT;
1085 int c = KBD_DIAG_FAIL;
1087 while (retry-- > 0) {
1088 empty_both_buffers(p, 10);
1089 if (write_controller_command(p, KBDC_DIAGNOSE))
1096 while (again-- > 0) {
1098 DELAY(KBD_RESETDELAY*1000);
1099 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1100 if (c != -1) /* wait again if the controller is not ready */
1103 if (verbose || bootverbose)
1104 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1105 return (c == KBD_DIAG_DONE);
1109 test_kbd_port(KBDC p)
1111 int retry = KBD_MAXRETRY;
1112 int again = KBD_MAXWAIT;
1115 while (retry-- > 0) {
1116 empty_both_buffers(p, 10);
1117 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1124 while (again-- > 0) {
1125 c = read_controller_data(p);
1126 if (c != -1) /* try again if the controller is not ready */
1129 if (verbose || bootverbose)
1130 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1135 test_aux_port(KBDC p)
1137 int retry = KBD_MAXRETRY;
1138 int again = KBD_MAXWAIT;
1141 while (retry-- > 0) {
1142 empty_both_buffers(p, 10);
1143 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1150 while (again-- > 0) {
1151 c = read_controller_data(p);
1152 if (c != -1) /* try again if the controller is not ready */
1155 if (verbose || bootverbose)
1156 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1161 kbdc_get_device_mask(KBDC p)
1163 return p->command_mask;
1167 kbdc_set_device_mask(KBDC p, int mask)
1170 mask & (((p->quirks & KBDC_QUIRK_KEEP_ACTIVATED)
1171 ? 0 : KBD_KBD_CONTROL_BITS) | KBD_AUX_CONTROL_BITS);
1175 get_controller_command_byte(KBDC p)
1177 if (p->command_byte != -1)
1178 return p->command_byte;
1179 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1182 p->command_byte = read_controller_data(p);
1183 return p->command_byte;
1187 set_controller_command_byte(KBDC p, int mask, int command)
1189 if (get_controller_command_byte(p) == -1)
1192 command = (p->command_byte & ~mask) | (command & mask);
1193 if (command & KBD_DISABLE_KBD_PORT) {
1194 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1197 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1199 if (!write_controller_data(p, command))
1201 p->command_byte = command;
1204 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1211 * Rudimentary support for active PS/2 AUX port multiplexing.
1212 * Only write commands can be routed to a selected AUX port.
1213 * Source port of data processed by read commands is totally ignored.
1216 set_aux_mux_state(KBDC p, int enabled)
1218 int command, version;
1220 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1221 write_controller_data(p, 0xF0) == 0 ||
1222 read_controller_data(p) != 0xF0)
1225 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1226 write_controller_data(p, 0x56) == 0 ||
1227 read_controller_data(p) != 0x56)
1230 command = enabled ? 0xa4 : 0xa5;
1231 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1232 write_controller_data(p, command) == 0 ||
1233 (version = read_controller_data(p)) == command)
1240 set_active_aux_mux_port(KBDC p, int port)
1243 if (!aux_mux_is_enabled(p))
1246 if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS)
1249 p->aux_mux_port = port;
1254 /* Checks for active multiplexing support and enables it */
1256 enable_aux_mux(KBDC p)
1260 version = set_aux_mux_state(p, TRUE);
1262 p->aux_mux_enabled = TRUE;
1263 set_active_aux_mux_port(p, 0);
1270 disable_aux_mux(KBDC p)
1273 p->aux_mux_enabled = FALSE;
1275 return (set_aux_mux_state(p, FALSE));
1279 aux_mux_is_enabled(KBDC p)
1282 return (p->aux_mux_enabled);