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 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
33 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
37 #include "use_atkbdc.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/syslog.h>
46 #include <machine/clock.h>
48 #include "atkbdcreg.h"
50 #include <bus/isa/isareg.h>
53 #define lwkt_gettoken(x)
54 #define lwkt_reltoken(x)
59 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
64 #define MAX(x, y) ((x) > (y) ? (x) : (y))
67 #define kbdcp(p) ((atkbdc_softc_t *)(p))
68 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
69 #define availq(q) ((q)->head != (q)->tail)
71 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
73 #define emptyq(q) ((q)->tail = (q)->head = 0)
76 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
77 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
78 #define write_data(k, d) \
79 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
80 #define write_command(k, d) \
81 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
86 * We always need at least one copy of the kbdc_softc struct for the
87 * low-level console. As the low-level console accesses the keyboard
88 * controller before kbdc, and all other devices, is probed, we
89 * statically allocate one entry. XXX
91 static atkbdc_softc_t default_kbdc;
92 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
94 static int verbose = KBDIO_DEBUG;
96 /* function prototypes */
98 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
99 bus_space_handle_t h0, bus_space_handle_t h1);
100 static int addq(kbdkqueue *q, int c);
101 static int removeq(kbdkqueue *q);
102 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
103 static int wait_for_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
105 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
107 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
110 atkbdc_get_softc(int unit)
114 if (unit >= NELEM(atkbdc_softc))
116 sc = atkbdc_softc[unit];
118 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
119 atkbdc_softc[unit] = sc;
125 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
127 if (rman_get_start(port0) <= 0)
129 if (rman_get_start(port1) <= 0)
135 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
136 struct resource *port1)
138 return atkbdc_setup(sc, rman_get_bustag(port0),
139 rman_get_bushandle(port0),
140 rman_get_bushandle(port1));
143 /* the backdoor to the keyboard controller! XXX */
145 atkbdc_configure(void)
148 bus_space_handle_t h0;
149 bus_space_handle_t h1;
152 #if defined(__i386__) || defined(__x86_64__)
157 resource_int_value("atkbdc", 0, "port", &port0);
158 port1 = IO_KBD + KBD_STATUS_PORT;
160 resource_int_value("atkbdc", 0, "port", &port0);
163 /* XXX: tag should be passed from the caller */
164 #if defined(__i386__)
165 tag = I386_BUS_SPACE_IO;
171 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
172 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
174 h0 = (bus_space_handle_t)port0;
175 h1 = (bus_space_handle_t)port1;
178 #if defined(__i386__) || defined(__x86_64__)
180 * Check if we really have AT keyboard controller. Poll status
181 * register until we get "all clear" indication. If no such
182 * indication comes, it probably means that there is no AT
183 * keyboard controller present. Give up in such case. Check relies
184 * on the fact that reading from non-existing in/out port returns
185 * 0xff on i386. May or may not be true on other platforms.
187 for (i = 65536; i != 0; --i) {
188 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
196 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
200 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
201 bus_space_handle_t h1)
203 if (sc->ioh0 == 0) { /* XXX */
204 sc->command_byte = -1;
205 sc->command_mask = 0;
207 sc->kbd.head = sc->kbd.tail = 0;
208 sc->aux.head = sc->aux.tail = 0;
210 sc->kbd.call_count = 0;
211 sc->kbd.qcount = sc->kbd.max_qcount = 0;
212 sc->aux.call_count = 0;
213 sc->aux.qcount = sc->aux.max_qcount = 0;
222 /* open a keyboard controller */
224 atkbdc_open(int unit)
230 if ((atkbdc_softc[unit]->port0 != NULL)
231 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
232 return (KBDC)atkbdc_softc[unit];
237 * I/O access arbitration in `kbdio'
239 * The `kbdio' module uses a simplistic convention to arbitrate
240 * I/O access to the controller/keyboard/mouse. The convention requires
241 * close cooperation of the calling device driver.
243 * The device drivers which utilize the `kbdio' module are assumed to
244 * have the following set of routines.
245 * a. An interrupt handler (the bottom half of the driver).
246 * b. Timeout routines which may briefly poll the keyboard controller.
247 * c. Routines outside interrupt context (the top half of the driver).
248 * They should follow the rules below:
249 * 1. The interrupt handler may assume that it always has full access
250 * to the controller/keyboard/mouse.
251 * 2. The other routines must issue `spltty()' if they wish to
252 * prevent the interrupt handler from accessing
253 * the controller/keyboard/mouse.
254 * 3. The timeout routines and the top half routines of the device driver
255 * arbitrate I/O access by observing the lock flag in `kbdio'.
256 * The flag is manipulated via `kbdc_lock()'; when one wants to
257 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
258 * the call returns with TRUE. Otherwise the caller must back off.
259 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
260 * is finished. This mechanism does not prevent the interrupt
261 * handler from being invoked at any time and carrying out I/O.
262 * Therefore, `spltty()' must be strategically placed in the device
263 * driver code. Also note that the timeout routine may interrupt
264 * `kbdc_lock()' called by the top half of the driver, but this
265 * interruption is OK so long as the timeout routine observes
267 * 4. The interrupt and timeout routines should not extend I/O operation
268 * across more than one interrupt or timeout; they must complete any
269 * necessary I/O operation within one invocation of the routine.
270 * This means that if the timeout routine acquires the lock flag,
271 * it must reset the flag to FALSE before it returns.
274 /* set/reset polling lock */
276 kbdc_lock(KBDC p, int lock)
280 prevlock = kbdcp(p)->lock;
281 kbdcp(p)->lock = lock;
283 return (prevlock != lock);
286 /* check if any data is waiting to be processed */
288 kbdc_data_ready(KBDC p)
290 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
291 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
294 /* queuing functions */
297 addq(kbdkqueue *q, int c)
299 if (nextq(q->tail) != q->head) {
301 q->tail = nextq(q->tail);
305 if (q->qcount > q->max_qcount)
306 q->max_qcount = q->qcount;
314 removeq(kbdkqueue *q)
318 if (q->tail != q->head) {
320 q->head = nextq(q->head);
330 * device I/O routines
333 wait_while_controller_busy(struct atkbdc_softc *kbdc)
335 /* CPU will stay inside the loop for 100msec at most */
336 TOTALDELAY retry = { .us = 70000, .last_clock =0 }; /* 70ms */
340 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
341 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
342 DELAY(KBDD_DELAYTIME);
345 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
346 DELAY(KBDD_DELAYTIME);
350 DELAY(KBDC_DELAYTIME);
351 if (CHECKTIMEOUT(&retry))
358 * wait for any data; whether it's from the controller,
359 * the keyboard, or the aux device.
362 wait_for_data(struct atkbdc_softc *kbdc)
364 /* CPU will stay inside the loop for 200msec at most */
365 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
368 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
369 DELAY(KBDC_DELAYTIME);
370 if (CHECKTIMEOUT(&retry))
373 DELAY(KBDD_DELAYTIME);
377 /* wait for data from the keyboard */
379 wait_for_kbd_data(struct atkbdc_softc *kbdc)
381 /* CPU will stay inside the loop for 200msec at most */
382 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
386 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
387 != KBDS_KBD_BUFFER_FULL) {
388 if (f == KBDS_AUX_BUFFER_FULL) {
389 DELAY(KBDD_DELAYTIME);
393 DELAY(KBDC_DELAYTIME);
394 if (CHECKTIMEOUT(&retry))
397 DELAY(KBDD_DELAYTIME);
402 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
403 * queue anything else.
406 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
408 /* CPU will stay inside the loop for 200msec at most */
409 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
413 while (CHECKTIMEOUT(&retry) == 0) {
414 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
415 DELAY(KBDD_DELAYTIME);
417 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
418 if ((b == KBD_ACK) || (b == KBD_RESEND)
419 || (b == KBD_RESET_FAIL))
422 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
426 DELAY(KBDC_DELAYTIME);
431 /* wait for data from the aux device */
433 wait_for_aux_data(struct atkbdc_softc *kbdc)
435 /* CPU will stay inside the loop for 200msec at most */
436 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
440 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
441 != KBDS_AUX_BUFFER_FULL) {
442 if (f == KBDS_KBD_BUFFER_FULL) {
443 DELAY(KBDD_DELAYTIME);
447 DELAY(KBDC_DELAYTIME);
448 if (CHECKTIMEOUT(&retry))
451 DELAY(KBDD_DELAYTIME);
456 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
457 * queue anything else.
460 wait_for_aux_ack(struct atkbdc_softc *kbdc)
462 /* CPU will stay inside the loop for 200msec at most */
463 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
467 while (CHECKTIMEOUT(&retry) == 0) {
468 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
469 DELAY(KBDD_DELAYTIME);
471 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
472 if ((b == PSM_ACK) || (b == PSM_RESEND)
473 || (b == PSM_RESET_FAIL))
476 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
480 DELAY(KBDC_DELAYTIME);
486 * Returns read-back data or -1 on failure
489 write_controller_w1r1(KBDC p, int c, int d)
491 if (!write_controller_command(p, c))
493 if (!write_controller_data(p, d))
495 return (read_controller_data(p));
498 /* write a one byte command to the controller */
500 write_controller_command(KBDC p, int c)
502 if (!wait_while_controller_busy(kbdcp(p)))
504 write_command(kbdcp(p), c);
508 /* write a one byte data to the controller */
510 write_controller_data(KBDC p, int c)
512 if (!wait_while_controller_busy(kbdcp(p)))
514 write_data(kbdcp(p), c);
518 /* write a one byte keyboard command */
520 write_kbd_command(KBDC p, int c)
522 if (!wait_while_controller_busy(kbdcp(p)))
524 write_data(kbdcp(p), c);
528 /* write a one byte auxiliary device command */
530 write_aux_command(KBDC p, int c)
532 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
534 return write_controller_data(p, c);
537 /* send a command to the keyboard and wait for ACK */
539 send_kbd_command(KBDC p, int c)
541 int retry = KBD_MAXRETRY;
544 while (retry-- > 0) {
545 if (!write_kbd_command(p, c))
547 res = wait_for_kbd_ack(kbdcp(p));
554 /* send a command to the auxiliary device and wait for ACK */
556 send_aux_command(KBDC p, int c)
558 int retry = KBD_MAXRETRY;
561 while (retry-- > 0) {
562 if (!write_aux_command(p, c))
566 * The aux device may have already sent one or two bytes of
567 * status data, when a command is received. It will immediately
568 * stop data transmission, thus, leaving an incomplete data
569 * packet in our buffer. We have to discard any unprocessed
570 * data in order to remove such packets. Well, we may remove
571 * unprocessed, but necessary data byte as well...
573 emptyq(&kbdcp(p)->aux);
574 res = wait_for_aux_ack(kbdcp(p));
581 /* send a command and a data to the keyboard, wait for ACKs */
583 send_kbd_command_and_data(KBDC p, int c, int d)
588 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
589 if (!write_kbd_command(p, c))
591 res = wait_for_kbd_ack(kbdcp(p));
594 else if (res != KBD_RESEND)
600 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
601 if (!write_kbd_command(p, d))
603 res = wait_for_kbd_ack(kbdcp(p));
604 if (res != KBD_RESEND)
610 /* send a command and a data to the auxiliary device, wait for ACKs */
612 send_aux_command_and_data(KBDC p, int c, int d)
617 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
618 if (!write_aux_command(p, c))
620 emptyq(&kbdcp(p)->aux);
621 res = wait_for_aux_ack(kbdcp(p));
624 else if (res != PSM_RESEND)
630 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
631 if (!write_aux_command(p, d))
633 res = wait_for_aux_ack(kbdcp(p));
634 if (res != PSM_RESEND)
641 * read one byte from any source; whether from the controller,
642 * the keyboard, or the aux device
645 read_controller_data(KBDC p)
647 if (availq(&kbdcp(p)->kbd))
648 return removeq(&kbdcp(p)->kbd);
649 if (availq(&kbdcp(p)->aux))
650 return removeq(&kbdcp(p)->aux);
651 if (!wait_for_data(kbdcp(p)))
652 return -1; /* timeout */
653 return read_data(kbdcp(p));
660 /* read one byte from the keyboard */
662 read_kbd_data(KBDC p)
669 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
670 "aux q: %d calls, max %d chars\n",
671 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
672 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
676 if (availq(&kbdcp(p)->kbd))
677 return removeq(&kbdcp(p)->kbd);
678 if (!wait_for_kbd_data(kbdcp(p)))
679 return -1; /* timeout */
680 b = read_data(kbdcp(p));
684 /* read one byte from the keyboard, but return immediately if
688 read_kbd_data_no_wait(KBDC p)
696 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
697 "aux q: %d calls, max %d chars\n",
698 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
699 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
703 if (availq(&kbdcp(p)->kbd))
704 return removeq(&kbdcp(p)->kbd);
705 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
706 while (f == KBDS_AUX_BUFFER_FULL) {
707 DELAY(KBDD_DELAYTIME);
708 b = read_data(kbdcp(p));
709 addq(&kbdcp(p)->aux, b);
710 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
712 if (f == KBDS_KBD_BUFFER_FULL) {
713 DELAY(KBDD_DELAYTIME);
714 b = read_data(kbdcp(p));
717 return -1; /* no data */
720 /* read one byte from the aux device */
722 read_aux_data(KBDC p)
725 if (availq(&kbdcp(p)->aux))
726 return removeq(&kbdcp(p)->aux);
727 if (!wait_for_aux_data(kbdcp(p)))
728 return -1; /* timeout */
729 b = read_data(kbdcp(p));
733 /* read one byte from the aux device, but return immediately if
737 read_aux_data_no_wait(KBDC p)
742 if (availq(&kbdcp(p)->aux))
743 return removeq(&kbdcp(p)->aux);
744 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
745 while (f == KBDS_KBD_BUFFER_FULL) {
746 DELAY(KBDD_DELAYTIME);
747 b = read_data(kbdcp(p));
748 addq(&kbdcp(p)->kbd, b);
749 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
751 if (f == KBDS_AUX_BUFFER_FULL) {
752 DELAY(KBDD_DELAYTIME);
753 b = read_data(kbdcp(p));
756 return -1; /* no data */
759 /* discard data from the keyboard */
761 empty_kbd_buffer(KBDC p, int wait)
772 for (t = wait; t > 0; ) {
773 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
774 DELAY(KBDD_DELAYTIME);
775 b = read_data(kbdcp(p));
776 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
777 addq(&kbdcp(p)->aux, b);
791 if ((c1 > 0) || (c2 > 0))
792 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
795 emptyq(&kbdcp(p)->kbd);
798 /* discard data from the aux device */
800 empty_aux_buffer(KBDC p, int wait)
811 for (t = wait; t > 0; ) {
812 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
813 DELAY(KBDD_DELAYTIME);
814 b = read_data(kbdcp(p));
815 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
816 addq(&kbdcp(p)->kbd, b);
830 if ((c1 > 0) || (c2 > 0))
831 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
834 emptyq(&kbdcp(p)->aux);
837 /* discard any data from the keyboard or the aux device */
839 empty_both_buffers(KBDC p, int wait)
849 for (t = wait; t > 0; ) {
850 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
851 DELAY(KBDD_DELAYTIME);
852 (void)read_data(kbdcp(p));
854 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
866 if ((c1 > 0) || (c2 > 0))
867 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
870 emptyq(&kbdcp(p)->kbd);
871 emptyq(&kbdcp(p)->aux);
874 /* keyboard and mouse device control */
876 /* NOTE: enable the keyboard port but disable the keyboard
877 * interrupt before calling "reset_kbd()".
882 int retry = KBD_MAXRETRY;
883 int again = KBD_MAXWAIT;
884 int c = KBD_RESEND; /* keep the compiler happy */
886 while (retry-- > 0) {
887 empty_both_buffers(p, 10);
888 if (!write_kbd_command(p, KBDC_RESET_KBD))
890 emptyq(&kbdcp(p)->kbd);
891 c = read_controller_data(p);
892 if (verbose || bootverbose)
893 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
894 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
900 while (again-- > 0) {
901 /* wait awhile, well, in fact we must wait quite loooooooooooong */
902 DELAY(KBD_RESETDELAY*1000);
903 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
904 if (c != -1) /* wait again if the controller is not ready */
907 if (verbose || bootverbose)
908 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
909 if (c != KBD_RESET_DONE)
914 /* NOTE: enable the aux port but disable the aux interrupt
915 * before calling `reset_aux_dev()'.
918 reset_aux_dev(KBDC p)
920 int retry = KBD_MAXRETRY;
921 int again = KBD_MAXWAIT;
922 int c = PSM_RESEND; /* keep the compiler happy */
924 while (retry-- > 0) {
925 empty_both_buffers(p, 10);
926 if (!write_aux_command(p, PSMC_RESET_DEV))
928 emptyq(&kbdcp(p)->aux);
929 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
930 for (again = KBD_MAXWAIT; again > 0; --again) {
931 DELAY(KBD_RESETDELAY*1000);
932 c = read_aux_data_no_wait(p);
936 if (verbose || bootverbose)
937 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
938 if (c == PSM_ACK) /* aux dev is about to reset... */
944 for (again = KBD_MAXWAIT; again > 0; --again) {
945 /* wait awhile, well, quite looooooooooooong */
946 DELAY(KBD_RESETDELAY*1000);
947 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
948 if (c != -1) /* wait again if the controller is not ready */
951 if (verbose || bootverbose)
952 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
953 if (c != PSM_RESET_DONE) /* reset status */
956 c = read_aux_data(p); /* device ID */
957 if (verbose || bootverbose)
958 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
959 /* NOTE: we could check the device ID now, but leave it later... */
963 /* controller diagnostics and setup */
966 test_controller(KBDC p)
968 int retry = KBD_MAXRETRY;
969 int again = KBD_MAXWAIT;
970 int c = KBD_DIAG_FAIL;
972 while (retry-- > 0) {
973 empty_both_buffers(p, 10);
974 if (write_controller_command(p, KBDC_DIAGNOSE))
980 emptyq(&kbdcp(p)->kbd);
981 while (again-- > 0) {
983 DELAY(KBD_RESETDELAY*1000);
984 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
985 if (c != -1) /* wait again if the controller is not ready */
988 if (verbose || bootverbose)
989 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
990 return (c == KBD_DIAG_DONE);
994 test_kbd_port(KBDC p)
996 int retry = KBD_MAXRETRY;
997 int again = KBD_MAXWAIT;
1000 while (retry-- > 0) {
1001 empty_both_buffers(p, 10);
1002 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1008 emptyq(&kbdcp(p)->kbd);
1009 while (again-- > 0) {
1010 c = read_controller_data(p);
1011 if (c != -1) /* try again if the controller is not ready */
1014 if (verbose || bootverbose)
1015 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1020 test_aux_port(KBDC p)
1022 int retry = KBD_MAXRETRY;
1023 int again = KBD_MAXWAIT;
1026 while (retry-- > 0) {
1027 empty_both_buffers(p, 10);
1028 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1034 emptyq(&kbdcp(p)->kbd);
1035 while (again-- > 0) {
1036 c = read_controller_data(p);
1037 if (c != -1) /* try again if the controller is not ready */
1040 if (verbose || bootverbose)
1041 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1046 kbdc_get_device_mask(KBDC p)
1048 return kbdcp(p)->command_mask;
1052 kbdc_set_device_mask(KBDC p, int mask)
1054 kbdcp(p)->command_mask =
1055 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1059 get_controller_command_byte(KBDC p)
1061 if (kbdcp(p)->command_byte != -1)
1062 return kbdcp(p)->command_byte;
1063 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1065 emptyq(&kbdcp(p)->kbd);
1066 kbdcp(p)->command_byte = read_controller_data(p);
1067 return kbdcp(p)->command_byte;
1071 set_controller_command_byte(KBDC p, int mask, int command)
1073 if (get_controller_command_byte(p) == -1)
1076 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1078 if (mask & KBD_DISABLE_KBD_PORT) {
1079 if (command & KBD_DISABLE_KBD_PORT) {
1080 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1085 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1087 if (!write_controller_data(p, command))
1090 if (mask & KBD_DISABLE_KBD_PORT) {
1091 if ((command & KBD_DISABLE_KBD_PORT) == 0) {
1092 if (!write_controller_command(p, KBDC_ENABLE_KBD_PORT))
1097 kbdcp(p)->command_byte = command;
1100 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",