2 * Copyright (c) 1996-1999
3 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/syslog.h>
43 #include <machine/clock.h>
45 #include <dev/kbd/atkbdcreg.h>
47 #include <isa/isareg.h>
51 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
56 #define MAX(x, y) ((x) > (y) ? (x) : (y))
59 #define kbdcp(p) ((atkbdc_softc_t *)(p))
60 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
61 #define availq(q) ((q)->head != (q)->tail)
63 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
65 #define emptyq(q) ((q)->tail = (q)->head = 0)
71 * We always need at least one copy of the kbdc_softc struct for the
72 * low-level console. As the low-level console accesses the keyboard
73 * controller before kbdc, and all other devices, is probed, we
74 * statically allocate one entry. XXX
76 static atkbdc_softc_t default_kbdc;
77 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
79 static int verbose = KBDIO_DEBUG;
81 /* function prototypes */
83 static int atkbdc_setup(atkbdc_softc_t *sc, int port);
84 static int addq(kqueue *q, int c);
85 static int removeq(kqueue *q);
86 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
87 static int wait_for_data(atkbdc_softc_t *kbdc);
88 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
89 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
90 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
91 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
94 *atkbdc_get_softc(int unit)
98 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
100 sc = atkbdc_softc[unit];
102 sc = atkbdc_softc[unit]
103 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
106 bzero(sc, sizeof(*sc));
107 sc->port = -1; /* XXX */
113 atkbdc_probe_unit(int unit, int port)
121 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, int port)
123 return atkbdc_setup(sc, port);
126 /* the backdoor to the keyboard controller! XXX */
128 atkbdc_configure(void)
130 return atkbdc_setup(atkbdc_softc[0], -1);
134 atkbdc_setup(atkbdc_softc_t *sc, int port)
140 sc->command_byte = -1;
141 sc->command_mask = 0;
143 sc->kbd.head = sc->kbd.tail = 0;
144 sc->aux.head = sc->aux.tail = 0;
146 sc->kbd.call_count = 0;
147 sc->kbd.qcount = sc->kbd.max_qcount = 0;
148 sc->aux.call_count = 0;
149 sc->aux.qcount = sc->aux.max_qcount = 0;
152 sc->port = port; /* may override the previous value */
156 /* associate a port number with a KBDC */
168 for (i = 0; i < sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]); ++i) {
169 if (atkbdc_softc[i] == NULL)
171 if (atkbdc_softc[i]->port == port) {
173 return (KBDC)atkbdc_softc[i];
175 if (atkbdc_softc[i]->port <= 0) {
176 if (atkbdc_setup(atkbdc_softc[i], port))
179 return (KBDC)atkbdc_softc[i];
187 * I/O access arbitration in `kbdio'
189 * The `kbdio' module uses a simplistic convention to arbitrate
190 * I/O access to the controller/keyboard/mouse. The convention requires
191 * close cooperation of the calling device driver.
193 * The device driver which utilizes the `kbdio' module are assumed to
194 * have the following set of routines.
195 * a. An interrupt handler (the bottom half of the driver).
196 * b. Timeout routines which may briefly polls the keyboard controller.
197 * c. Routines outside interrupt context (the top half of the driver).
198 * They should follow the rules below:
199 * 1. The interrupt handler may assume that it always has full access
200 * to the controller/keyboard/mouse.
201 * 2. The other routines must issue `spltty()' if they wish to
202 * prevent the interrupt handler from accessing
203 * the controller/keyboard/mouse.
204 * 3. The timeout routines and the top half routines of the device driver
205 * arbitrate I/O access by observing the lock flag in `kbdio'.
206 * The flag is manipulated via `kbdc_lock()'; when one wants to
207 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
208 * the call returns with TRUE. Otherwise the caller must back off.
209 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
210 * is finished. This mechanism does not prevent the interrupt
211 * handler from being invoked at any time and carrying out I/O.
212 * Therefore, `spltty()' must be strategically placed in the device
213 * driver code. Also note that the timeout routine may interrupt
214 * `kbdc_lock()' called by the top half of the driver, but this
215 * interruption is OK so long as the timeout routine observes the
217 * 4. The interrupt and timeout routines should not extend I/O operation
218 * across more than one interrupt or timeout; they must complete
219 * necessary I/O operation within one invokation of the routine.
220 * This measns that if the timeout routine acquires the lock flag,
221 * it must reset the flag to FALSE before it returns.
224 /* set/reset polling lock */
226 kbdc_lock(KBDC p, int lock)
230 prevlock = kbdcp(p)->lock;
231 kbdcp(p)->lock = lock;
233 return (prevlock != lock);
236 /* check if any data is waiting to be processed */
238 kbdc_data_ready(KBDC p)
240 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
241 || (inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL));
244 /* queuing functions */
247 addq(kqueue *q, int c)
249 if (nextq(q->tail) != q->head) {
251 q->tail = nextq(q->tail);
255 if (q->qcount > q->max_qcount)
256 q->max_qcount = q->qcount;
268 if (q->tail != q->head) {
270 q->head = nextq(q->head);
280 * device I/O routines
283 wait_while_controller_busy(struct atkbdc_softc *kbdc)
285 /* CPU will stay inside the loop for 100msec at most */
287 int port = kbdc->port;
290 while ((f = inb(port + KBD_STATUS_PORT)) & KBDS_INPUT_BUFFER_FULL) {
291 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
292 DELAY(KBDD_DELAYTIME);
293 addq(&kbdc->kbd, inb(port + KBD_DATA_PORT));
294 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
295 DELAY(KBDD_DELAYTIME);
296 addq(&kbdc->aux, inb(port + KBD_DATA_PORT));
298 DELAY(KBDC_DELAYTIME);
306 * wait for any data; whether it's from the controller,
307 * the keyboard, or the aux device.
310 wait_for_data(struct atkbdc_softc *kbdc)
312 /* CPU will stay inside the loop for 200msec at most */
314 int port = kbdc->port;
317 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) == 0) {
318 DELAY(KBDC_DELAYTIME);
322 DELAY(KBDD_DELAYTIME);
326 /* wait for data from the keyboard */
328 wait_for_kbd_data(struct atkbdc_softc *kbdc)
330 /* CPU will stay inside the loop for 200msec at most */
332 int port = kbdc->port;
335 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
336 != KBDS_KBD_BUFFER_FULL) {
337 if (f == KBDS_AUX_BUFFER_FULL) {
338 DELAY(KBDD_DELAYTIME);
339 addq(&kbdc->aux, inb(port + KBD_DATA_PORT));
341 DELAY(KBDC_DELAYTIME);
345 DELAY(KBDD_DELAYTIME);
350 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
351 * queue anything else.
354 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
356 /* CPU will stay inside the loop for 200msec at most */
358 int port = kbdc->port;
362 while (retry-- > 0) {
363 if ((f = inb(port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
364 DELAY(KBDD_DELAYTIME);
365 b = inb(port + KBD_DATA_PORT);
366 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
367 if ((b == KBD_ACK) || (b == KBD_RESEND)
368 || (b == KBD_RESET_FAIL))
371 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
375 DELAY(KBDC_DELAYTIME);
380 /* wait for data from the aux device */
382 wait_for_aux_data(struct atkbdc_softc *kbdc)
384 /* CPU will stay inside the loop for 200msec at most */
386 int port = kbdc->port;
389 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
390 != KBDS_AUX_BUFFER_FULL) {
391 if (f == KBDS_KBD_BUFFER_FULL) {
392 DELAY(KBDD_DELAYTIME);
393 addq(&kbdc->kbd, inb(port + KBD_DATA_PORT));
395 DELAY(KBDC_DELAYTIME);
399 DELAY(KBDD_DELAYTIME);
404 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
405 * queue anything else.
408 wait_for_aux_ack(struct atkbdc_softc *kbdc)
410 /* CPU will stay inside the loop for 200msec at most */
412 int port = kbdc->port;
416 while (retry-- > 0) {
417 if ((f = inb(port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
418 DELAY(KBDD_DELAYTIME);
419 b = inb(port + KBD_DATA_PORT);
420 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
421 if ((b == PSM_ACK) || (b == PSM_RESEND)
422 || (b == PSM_RESET_FAIL))
425 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
429 DELAY(KBDC_DELAYTIME);
434 /* write a one byte command to the controller */
436 write_controller_command(KBDC p, int c)
438 if (!wait_while_controller_busy(kbdcp(p)))
440 outb(kbdcp(p)->port + KBD_COMMAND_PORT, c);
444 /* write a one byte data to the controller */
446 write_controller_data(KBDC p, int c)
448 if (!wait_while_controller_busy(kbdcp(p)))
450 outb(kbdcp(p)->port + KBD_DATA_PORT, c);
454 /* write a one byte keyboard command */
456 write_kbd_command(KBDC p, int c)
458 if (!wait_while_controller_busy(kbdcp(p)))
460 outb(kbdcp(p)->port + KBD_DATA_PORT, c);
464 /* write a one byte auxiliary device command */
466 write_aux_command(KBDC p, int c)
468 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
470 return write_controller_data(p, c);
473 /* send a command to the keyboard and wait for ACK */
475 send_kbd_command(KBDC p, int c)
477 int retry = KBD_MAXRETRY;
480 while (retry-- > 0) {
481 if (!write_kbd_command(p, c))
483 res = wait_for_kbd_ack(kbdcp(p));
490 /* send a command to the auxiliary device and wait for ACK */
492 send_aux_command(KBDC p, int c)
494 int retry = KBD_MAXRETRY;
497 while (retry-- > 0) {
498 if (!write_aux_command(p, c))
502 * The aux device may have already sent one or two bytes of
503 * status data, when a command is received. It will immediately
504 * stop data transmission, thus, leaving an incomplete data
505 * packet in our buffer. We have to discard any unprocessed
506 * data in order to remove such packets. Well, we may remove
507 * unprocessed, but necessary data byte as well...
509 emptyq(&kbdcp(p)->aux);
510 res = wait_for_aux_ack(kbdcp(p));
517 /* send a command and a data to the keyboard, wait for ACKs */
519 send_kbd_command_and_data(KBDC p, int c, int d)
524 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
525 if (!write_kbd_command(p, c))
527 res = wait_for_kbd_ack(kbdcp(p));
530 else if (res != KBD_RESEND)
536 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
537 if (!write_kbd_command(p, d))
539 res = wait_for_kbd_ack(kbdcp(p));
540 if (res != KBD_RESEND)
546 /* send a command and a data to the auxiliary device, wait for ACKs */
548 send_aux_command_and_data(KBDC p, int c, int d)
553 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
554 if (!write_aux_command(p, c))
556 emptyq(&kbdcp(p)->aux);
557 res = wait_for_aux_ack(kbdcp(p));
560 else if (res != PSM_RESEND)
566 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
567 if (!write_aux_command(p, d))
569 res = wait_for_aux_ack(kbdcp(p));
570 if (res != PSM_RESEND)
577 * read one byte from any source; whether from the controller,
578 * the keyboard, or the aux device
581 read_controller_data(KBDC p)
583 if (availq(&kbdcp(p)->kbd))
584 return removeq(&kbdcp(p)->kbd);
585 if (availq(&kbdcp(p)->aux))
586 return removeq(&kbdcp(p)->aux);
587 if (!wait_for_data(kbdcp(p)))
588 return -1; /* timeout */
589 return inb(kbdcp(p)->port + KBD_DATA_PORT);
596 /* read one byte from the keyboard */
598 read_kbd_data(KBDC p)
603 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
604 "aux q: %d calls, max %d chars\n",
605 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
606 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
610 if (availq(&kbdcp(p)->kbd))
611 return removeq(&kbdcp(p)->kbd);
612 if (!wait_for_kbd_data(kbdcp(p)))
613 return -1; /* timeout */
614 return inb(kbdcp(p)->port + KBD_DATA_PORT);
617 /* read one byte from the keyboard, but return immediately if
621 read_kbd_data_no_wait(KBDC p)
628 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
629 "aux q: %d calls, max %d chars\n",
630 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
631 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
635 if (availq(&kbdcp(p)->kbd))
636 return removeq(&kbdcp(p)->kbd);
637 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
638 if (f == KBDS_AUX_BUFFER_FULL) {
639 DELAY(KBDD_DELAYTIME);
640 addq(&kbdcp(p)->aux, inb(kbdcp(p)->port + KBD_DATA_PORT));
641 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
643 if (f == KBDS_KBD_BUFFER_FULL) {
644 DELAY(KBDD_DELAYTIME);
645 return inb(kbdcp(p)->port + KBD_DATA_PORT);
647 return -1; /* no data */
650 /* read one byte from the aux device */
652 read_aux_data(KBDC p)
654 if (availq(&kbdcp(p)->aux))
655 return removeq(&kbdcp(p)->aux);
656 if (!wait_for_aux_data(kbdcp(p)))
657 return -1; /* timeout */
658 return inb(kbdcp(p)->port + KBD_DATA_PORT);
661 /* read one byte from the aux device, but return immediately if
665 read_aux_data_no_wait(KBDC p)
669 if (availq(&kbdcp(p)->aux))
670 return removeq(&kbdcp(p)->aux);
671 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
672 if (f == KBDS_KBD_BUFFER_FULL) {
673 DELAY(KBDD_DELAYTIME);
674 addq(&kbdcp(p)->kbd, inb(kbdcp(p)->port + KBD_DATA_PORT));
675 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
677 if (f == KBDS_AUX_BUFFER_FULL) {
678 DELAY(KBDD_DELAYTIME);
679 return inb(kbdcp(p)->port + KBD_DATA_PORT);
681 return -1; /* no data */
684 /* discard data from the keyboard */
686 empty_kbd_buffer(KBDC p, int wait)
697 for (t = wait; t > 0; ) {
698 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
699 DELAY(KBDD_DELAYTIME);
700 b = inb(kbdcp(p)->port + KBD_DATA_PORT);
701 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
702 addq(&kbdcp(p)->aux, b);
716 if ((c1 > 0) || (c2 > 0))
717 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
720 emptyq(&kbdcp(p)->kbd);
723 /* discard data from the aux device */
725 empty_aux_buffer(KBDC p, int wait)
736 for (t = wait; t > 0; ) {
737 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
738 DELAY(KBDD_DELAYTIME);
739 b = inb(kbdcp(p)->port + KBD_DATA_PORT);
740 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
741 addq(&kbdcp(p)->kbd, b);
755 if ((c1 > 0) || (c2 > 0))
756 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
759 emptyq(&kbdcp(p)->aux);
762 /* discard any data from the keyboard or the aux device */
764 empty_both_buffers(KBDC p, int wait)
774 for (t = wait; t > 0; ) {
775 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
776 DELAY(KBDD_DELAYTIME);
777 (void)inb(kbdcp(p)->port + KBD_DATA_PORT);
779 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
791 if ((c1 > 0) || (c2 > 0))
792 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
795 emptyq(&kbdcp(p)->kbd);
796 emptyq(&kbdcp(p)->aux);
799 /* keyboard and mouse device control */
801 /* NOTE: enable the keyboard port but disable the keyboard
802 * interrupt before calling "reset_kbd()".
807 int retry = KBD_MAXRETRY;
808 int again = KBD_MAXWAIT;
809 int c = KBD_RESEND; /* keep the compiler happy */
811 while (retry-- > 0) {
812 empty_both_buffers(p, 10);
813 if (!write_kbd_command(p, KBDC_RESET_KBD))
815 emptyq(&kbdcp(p)->kbd);
816 c = read_controller_data(p);
817 if (verbose || bootverbose)
818 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
819 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
825 while (again-- > 0) {
826 /* wait awhile, well, in fact we must wait quite loooooooooooong */
827 DELAY(KBD_RESETDELAY*1000);
828 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
829 if (c != -1) /* wait again if the controller is not ready */
832 if (verbose || bootverbose)
833 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
834 if (c != KBD_RESET_DONE)
839 /* NOTE: enable the aux port but disable the aux interrupt
840 * before calling `reset_aux_dev()'.
843 reset_aux_dev(KBDC p)
845 int retry = KBD_MAXRETRY;
846 int again = KBD_MAXWAIT;
847 int c = PSM_RESEND; /* keep the compiler happy */
849 while (retry-- > 0) {
850 empty_both_buffers(p, 10);
851 if (!write_aux_command(p, PSMC_RESET_DEV))
853 emptyq(&kbdcp(p)->aux);
854 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
855 for (again = KBD_MAXWAIT; again > 0; --again) {
856 DELAY(KBD_RESETDELAY*1000);
857 c = read_aux_data_no_wait(p);
861 if (verbose || bootverbose)
862 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
863 if (c == PSM_ACK) /* aux dev is about to reset... */
869 for (again = KBD_MAXWAIT; again > 0; --again) {
870 /* wait awhile, well, quite looooooooooooong */
871 DELAY(KBD_RESETDELAY*1000);
872 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
873 if (c != -1) /* wait again if the controller is not ready */
876 if (verbose || bootverbose)
877 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
878 if (c != PSM_RESET_DONE) /* reset status */
881 c = read_aux_data(p); /* device ID */
882 if (verbose || bootverbose)
883 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
884 /* NOTE: we could check the device ID now, but leave it later... */
888 /* controller diagnostics and setup */
891 test_controller(KBDC p)
893 int retry = KBD_MAXRETRY;
894 int again = KBD_MAXWAIT;
895 int c = KBD_DIAG_FAIL;
897 while (retry-- > 0) {
898 empty_both_buffers(p, 10);
899 if (write_controller_command(p, KBDC_DIAGNOSE))
905 emptyq(&kbdcp(p)->kbd);
906 while (again-- > 0) {
908 DELAY(KBD_RESETDELAY*1000);
909 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
910 if (c != -1) /* wait again if the controller is not ready */
913 if (verbose || bootverbose)
914 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
915 return (c == KBD_DIAG_DONE);
919 test_kbd_port(KBDC p)
921 int retry = KBD_MAXRETRY;
922 int again = KBD_MAXWAIT;
925 while (retry-- > 0) {
926 empty_both_buffers(p, 10);
927 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
933 emptyq(&kbdcp(p)->kbd);
934 while (again-- > 0) {
935 c = read_controller_data(p);
936 if (c != -1) /* try again if the controller is not ready */
939 if (verbose || bootverbose)
940 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
945 test_aux_port(KBDC p)
947 int retry = KBD_MAXRETRY;
948 int again = KBD_MAXWAIT;
951 while (retry-- > 0) {
952 empty_both_buffers(p, 10);
953 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
959 emptyq(&kbdcp(p)->kbd);
960 while (again-- > 0) {
961 c = read_controller_data(p);
962 if (c != -1) /* try again if the controller is not ready */
965 if (verbose || bootverbose)
966 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
971 kbdc_get_device_mask(KBDC p)
973 return kbdcp(p)->command_mask;
977 kbdc_set_device_mask(KBDC p, int mask)
979 kbdcp(p)->command_mask =
980 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
984 get_controller_command_byte(KBDC p)
986 if (kbdcp(p)->command_byte != -1)
987 return kbdcp(p)->command_byte;
988 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
990 emptyq(&kbdcp(p)->kbd);
991 kbdcp(p)->command_byte = read_controller_data(p);
992 return kbdcp(p)->command_byte;
996 set_controller_command_byte(KBDC p, int mask, int command)
998 if (get_controller_command_byte(p) == -1)
1001 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1002 if (command & KBD_DISABLE_KBD_PORT) {
1003 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1006 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1008 if (!write_controller_data(p, command))
1010 kbdcp(p)->command_byte = command;
1013 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",