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
30 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
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>
40 #include <sys/malloc.h>
41 #include <sys/syslog.h>
42 #include <machine/bus_pio.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
47 #include <machine/clock.h>
49 #include <dev/kbd/atkbdcreg.h>
51 #include <isa/isareg.h>
55 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
60 #define MAX(x, y) ((x) > (y) ? (x) : (y))
63 #define kbdcp(p) ((atkbdc_softc_t *)(p))
64 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
65 #define availq(q) ((q)->head != (q)->tail)
67 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
69 #define emptyq(q) ((q)->tail = (q)->head = 0)
72 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
73 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
74 #define write_data(k, d) \
75 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
76 #define write_command(k, d) \
77 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
82 * We always need at least one copy of the kbdc_softc struct for the
83 * low-level console. As the low-level console accesses the keyboard
84 * controller before kbdc, and all other devices, is probed, we
85 * statically allocate one entry. XXX
87 static atkbdc_softc_t default_kbdc;
88 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
90 static int verbose = KBDIO_DEBUG;
92 /* function prototypes */
94 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
95 bus_space_handle_t h0, bus_space_handle_t h1);
96 static int addq(kqueue *q, int c);
97 static int removeq(kqueue *q);
98 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
99 static int wait_for_data(atkbdc_softc_t *kbdc);
100 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
101 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
102 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
103 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
106 *atkbdc_get_softc(int unit)
110 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
112 sc = atkbdc_softc[unit];
114 sc = atkbdc_softc[unit]
115 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
118 bzero(sc, sizeof(*sc));
124 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
126 if (rman_get_start(port0) <= 0)
128 if (rman_get_start(port1) <= 0)
134 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
135 struct resource *port1)
137 return atkbdc_setup(sc, rman_get_bustag(port0),
138 rman_get_bushandle(port0),
139 rman_get_bushandle(port1));
142 /* the backdoor to the keyboard controller! XXX */
144 atkbdc_configure(void)
147 bus_space_handle_t h0;
148 bus_space_handle_t h1;
153 resource_int_value("atkbdc", 0, "port", &port0);
154 port1 = IO_KBD + KBD_STATUS_PORT;
156 resource_int_value("atkbdc", 0, "port", &port0);
159 /* XXX: tag should be passed from the caller */
160 #if defined(__i386__)
161 tag = I386_BUS_SPACE_IO;
162 #elif defined(__alpha__)
163 tag = ALPHA_BUS_SPACE_IO;
167 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
168 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
170 h0 = (bus_space_handle_t)port0;
171 h1 = (bus_space_handle_t)port1;
173 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
177 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
178 bus_space_handle_t h1)
180 if (sc->ioh0 == 0) { /* XXX */
181 sc->command_byte = -1;
182 sc->command_mask = 0;
184 sc->kbd.head = sc->kbd.tail = 0;
185 sc->aux.head = sc->aux.tail = 0;
187 sc->kbd.call_count = 0;
188 sc->kbd.qcount = sc->kbd.max_qcount = 0;
189 sc->aux.call_count = 0;
190 sc->aux.qcount = sc->aux.max_qcount = 0;
199 /* open a keyboard controller */
201 atkbdc_open(int unit)
207 if ((atkbdc_softc[unit]->port0 != NULL)
208 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
209 return (KBDC)atkbdc_softc[unit];
214 * I/O access arbitration in `kbdio'
216 * The `kbdio' module uses a simplistic convention to arbitrate
217 * I/O access to the controller/keyboard/mouse. The convention requires
218 * close cooperation of the calling device driver.
220 * The device drivers which utilize the `kbdio' module are assumed to
221 * have the following set of routines.
222 * a. An interrupt handler (the bottom half of the driver).
223 * b. Timeout routines which may briefly poll the keyboard controller.
224 * c. Routines outside interrupt context (the top half of the driver).
225 * They should follow the rules below:
226 * 1. The interrupt handler may assume that it always has full access
227 * to the controller/keyboard/mouse.
228 * 2. The other routines must issue `spltty()' if they wish to
229 * prevent the interrupt handler from accessing
230 * the controller/keyboard/mouse.
231 * 3. The timeout routines and the top half routines of the device driver
232 * arbitrate I/O access by observing the lock flag in `kbdio'.
233 * The flag is manipulated via `kbdc_lock()'; when one wants to
234 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
235 * the call returns with TRUE. Otherwise the caller must back off.
236 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
237 * is finished. This mechanism does not prevent the interrupt
238 * handler from being invoked at any time and carrying out I/O.
239 * Therefore, `spltty()' must be strategically placed in the device
240 * driver code. Also note that the timeout routine may interrupt
241 * `kbdc_lock()' called by the top half of the driver, but this
242 * interruption is OK so long as the timeout routine observes
244 * 4. The interrupt and timeout routines should not extend I/O operation
245 * across more than one interrupt or timeout; they must complete any
246 * necessary I/O operation within one invocation of the routine.
247 * This means that if the timeout routine acquires the lock flag,
248 * it must reset the flag to FALSE before it returns.
251 /* set/reset polling lock */
253 kbdc_lock(KBDC p, int lock)
257 prevlock = kbdcp(p)->lock;
258 kbdcp(p)->lock = lock;
260 return (prevlock != lock);
263 /* check if any data is waiting to be processed */
265 kbdc_data_ready(KBDC p)
267 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
268 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
271 /* queuing functions */
274 addq(kqueue *q, int c)
276 if (nextq(q->tail) != q->head) {
278 q->tail = nextq(q->tail);
282 if (q->qcount > q->max_qcount)
283 q->max_qcount = q->qcount;
295 if (q->tail != q->head) {
297 q->head = nextq(q->head);
307 * device I/O routines
310 wait_while_controller_busy(struct atkbdc_softc *kbdc)
312 /* CPU will stay inside the loop for 100msec at most */
316 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
317 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
318 DELAY(KBDD_DELAYTIME);
319 addq(&kbdc->kbd, read_data(kbdc));
320 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
321 DELAY(KBDD_DELAYTIME);
322 addq(&kbdc->aux, read_data(kbdc));
324 DELAY(KBDC_DELAYTIME);
332 * wait for any data; whether it's from the controller,
333 * the keyboard, or the aux device.
336 wait_for_data(struct atkbdc_softc *kbdc)
338 /* CPU will stay inside the loop for 200msec at most */
342 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
343 DELAY(KBDC_DELAYTIME);
347 DELAY(KBDD_DELAYTIME);
351 /* wait for data from the keyboard */
353 wait_for_kbd_data(struct atkbdc_softc *kbdc)
355 /* CPU will stay inside the loop for 200msec at most */
359 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
360 != KBDS_KBD_BUFFER_FULL) {
361 if (f == KBDS_AUX_BUFFER_FULL) {
362 DELAY(KBDD_DELAYTIME);
363 addq(&kbdc->aux, read_data(kbdc));
365 DELAY(KBDC_DELAYTIME);
369 DELAY(KBDD_DELAYTIME);
374 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
375 * queue anything else.
378 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
380 /* CPU will stay inside the loop for 200msec at most */
385 while (retry-- > 0) {
386 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
387 DELAY(KBDD_DELAYTIME);
389 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
390 if ((b == KBD_ACK) || (b == KBD_RESEND)
391 || (b == KBD_RESET_FAIL))
394 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
398 DELAY(KBDC_DELAYTIME);
403 /* wait for data from the aux device */
405 wait_for_aux_data(struct atkbdc_softc *kbdc)
407 /* CPU will stay inside the loop for 200msec at most */
411 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
412 != KBDS_AUX_BUFFER_FULL) {
413 if (f == KBDS_KBD_BUFFER_FULL) {
414 DELAY(KBDD_DELAYTIME);
415 addq(&kbdc->kbd, read_data(kbdc));
417 DELAY(KBDC_DELAYTIME);
421 DELAY(KBDD_DELAYTIME);
426 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
427 * queue anything else.
430 wait_for_aux_ack(struct atkbdc_softc *kbdc)
432 /* CPU will stay inside the loop for 200msec at most */
437 while (retry-- > 0) {
438 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
439 DELAY(KBDD_DELAYTIME);
441 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
442 if ((b == PSM_ACK) || (b == PSM_RESEND)
443 || (b == PSM_RESET_FAIL))
446 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
450 DELAY(KBDC_DELAYTIME);
455 /* write a one byte command to the controller */
457 write_controller_command(KBDC p, int c)
459 if (!wait_while_controller_busy(kbdcp(p)))
461 write_command(kbdcp(p), c);
465 /* write a one byte data to the controller */
467 write_controller_data(KBDC p, int c)
469 if (!wait_while_controller_busy(kbdcp(p)))
471 write_data(kbdcp(p), c);
475 /* write a one byte keyboard command */
477 write_kbd_command(KBDC p, int c)
479 if (!wait_while_controller_busy(kbdcp(p)))
481 write_data(kbdcp(p), c);
485 /* write a one byte auxiliary device command */
487 write_aux_command(KBDC p, int c)
489 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
491 return write_controller_data(p, c);
494 /* send a command to the keyboard and wait for ACK */
496 send_kbd_command(KBDC p, int c)
498 int retry = KBD_MAXRETRY;
501 while (retry-- > 0) {
502 if (!write_kbd_command(p, c))
504 res = wait_for_kbd_ack(kbdcp(p));
511 /* send a command to the auxiliary device and wait for ACK */
513 send_aux_command(KBDC p, int c)
515 int retry = KBD_MAXRETRY;
518 while (retry-- > 0) {
519 if (!write_aux_command(p, c))
523 * The aux device may have already sent one or two bytes of
524 * status data, when a command is received. It will immediately
525 * stop data transmission, thus, leaving an incomplete data
526 * packet in our buffer. We have to discard any unprocessed
527 * data in order to remove such packets. Well, we may remove
528 * unprocessed, but necessary data byte as well...
530 emptyq(&kbdcp(p)->aux);
531 res = wait_for_aux_ack(kbdcp(p));
538 /* send a command and a data to the keyboard, wait for ACKs */
540 send_kbd_command_and_data(KBDC p, int c, int d)
545 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
546 if (!write_kbd_command(p, c))
548 res = wait_for_kbd_ack(kbdcp(p));
551 else if (res != KBD_RESEND)
557 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
558 if (!write_kbd_command(p, d))
560 res = wait_for_kbd_ack(kbdcp(p));
561 if (res != KBD_RESEND)
567 /* send a command and a data to the auxiliary device, wait for ACKs */
569 send_aux_command_and_data(KBDC p, int c, int d)
574 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
575 if (!write_aux_command(p, c))
577 emptyq(&kbdcp(p)->aux);
578 res = wait_for_aux_ack(kbdcp(p));
581 else if (res != PSM_RESEND)
587 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
588 if (!write_aux_command(p, d))
590 res = wait_for_aux_ack(kbdcp(p));
591 if (res != PSM_RESEND)
598 * read one byte from any source; whether from the controller,
599 * the keyboard, or the aux device
602 read_controller_data(KBDC p)
604 if (availq(&kbdcp(p)->kbd))
605 return removeq(&kbdcp(p)->kbd);
606 if (availq(&kbdcp(p)->aux))
607 return removeq(&kbdcp(p)->aux);
608 if (!wait_for_data(kbdcp(p)))
609 return -1; /* timeout */
610 return read_data(kbdcp(p));
617 /* read one byte from the keyboard */
619 read_kbd_data(KBDC p)
624 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
625 "aux q: %d calls, max %d chars\n",
626 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
627 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
631 if (availq(&kbdcp(p)->kbd))
632 return removeq(&kbdcp(p)->kbd);
633 if (!wait_for_kbd_data(kbdcp(p)))
634 return -1; /* timeout */
635 return read_data(kbdcp(p));
638 /* read one byte from the keyboard, but return immediately if
642 read_kbd_data_no_wait(KBDC p)
649 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
650 "aux q: %d calls, max %d chars\n",
651 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
652 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
656 if (availq(&kbdcp(p)->kbd))
657 return removeq(&kbdcp(p)->kbd);
658 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
659 if (f == KBDS_AUX_BUFFER_FULL) {
660 DELAY(KBDD_DELAYTIME);
661 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
662 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
664 if (f == KBDS_KBD_BUFFER_FULL) {
665 DELAY(KBDD_DELAYTIME);
666 return read_data(kbdcp(p));
668 return -1; /* no data */
671 /* read one byte from the aux device */
673 read_aux_data(KBDC p)
675 if (availq(&kbdcp(p)->aux))
676 return removeq(&kbdcp(p)->aux);
677 if (!wait_for_aux_data(kbdcp(p)))
678 return -1; /* timeout */
679 return read_data(kbdcp(p));
682 /* read one byte from the aux device, but return immediately if
686 read_aux_data_no_wait(KBDC p)
690 if (availq(&kbdcp(p)->aux))
691 return removeq(&kbdcp(p)->aux);
692 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
693 if (f == KBDS_KBD_BUFFER_FULL) {
694 DELAY(KBDD_DELAYTIME);
695 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
696 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
698 if (f == KBDS_AUX_BUFFER_FULL) {
699 DELAY(KBDD_DELAYTIME);
700 return read_data(kbdcp(p));
702 return -1; /* no data */
705 /* discard data from the keyboard */
707 empty_kbd_buffer(KBDC p, int wait)
718 for (t = wait; t > 0; ) {
719 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
720 DELAY(KBDD_DELAYTIME);
721 b = read_data(kbdcp(p));
722 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
723 addq(&kbdcp(p)->aux, b);
737 if ((c1 > 0) || (c2 > 0))
738 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
741 emptyq(&kbdcp(p)->kbd);
744 /* discard data from the aux device */
746 empty_aux_buffer(KBDC p, int wait)
757 for (t = wait; t > 0; ) {
758 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
759 DELAY(KBDD_DELAYTIME);
760 b = read_data(kbdcp(p));
761 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
762 addq(&kbdcp(p)->kbd, b);
776 if ((c1 > 0) || (c2 > 0))
777 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
780 emptyq(&kbdcp(p)->aux);
783 /* discard any data from the keyboard or the aux device */
785 empty_both_buffers(KBDC p, int wait)
795 for (t = wait; t > 0; ) {
796 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
797 DELAY(KBDD_DELAYTIME);
798 (void)read_data(kbdcp(p));
800 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
812 if ((c1 > 0) || (c2 > 0))
813 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
816 emptyq(&kbdcp(p)->kbd);
817 emptyq(&kbdcp(p)->aux);
820 /* keyboard and mouse device control */
822 /* NOTE: enable the keyboard port but disable the keyboard
823 * interrupt before calling "reset_kbd()".
828 int retry = KBD_MAXRETRY;
829 int again = KBD_MAXWAIT;
830 int c = KBD_RESEND; /* keep the compiler happy */
832 while (retry-- > 0) {
833 empty_both_buffers(p, 10);
834 if (!write_kbd_command(p, KBDC_RESET_KBD))
836 emptyq(&kbdcp(p)->kbd);
837 c = read_controller_data(p);
838 if (verbose || bootverbose)
839 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
840 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
846 while (again-- > 0) {
847 /* wait awhile, well, in fact we must wait quite loooooooooooong */
848 DELAY(KBD_RESETDELAY*1000);
849 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
850 if (c != -1) /* wait again if the controller is not ready */
853 if (verbose || bootverbose)
854 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
855 if (c != KBD_RESET_DONE)
860 /* NOTE: enable the aux port but disable the aux interrupt
861 * before calling `reset_aux_dev()'.
864 reset_aux_dev(KBDC p)
866 int retry = KBD_MAXRETRY;
867 int again = KBD_MAXWAIT;
868 int c = PSM_RESEND; /* keep the compiler happy */
870 while (retry-- > 0) {
871 empty_both_buffers(p, 10);
872 if (!write_aux_command(p, PSMC_RESET_DEV))
874 emptyq(&kbdcp(p)->aux);
875 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
876 for (again = KBD_MAXWAIT; again > 0; --again) {
877 DELAY(KBD_RESETDELAY*1000);
878 c = read_aux_data_no_wait(p);
882 if (verbose || bootverbose)
883 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
884 if (c == PSM_ACK) /* aux dev is about to reset... */
890 for (again = KBD_MAXWAIT; again > 0; --again) {
891 /* wait awhile, well, quite looooooooooooong */
892 DELAY(KBD_RESETDELAY*1000);
893 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
894 if (c != -1) /* wait again if the controller is not ready */
897 if (verbose || bootverbose)
898 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
899 if (c != PSM_RESET_DONE) /* reset status */
902 c = read_aux_data(p); /* device ID */
903 if (verbose || bootverbose)
904 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
905 /* NOTE: we could check the device ID now, but leave it later... */
909 /* controller diagnostics and setup */
912 test_controller(KBDC p)
914 int retry = KBD_MAXRETRY;
915 int again = KBD_MAXWAIT;
916 int c = KBD_DIAG_FAIL;
918 while (retry-- > 0) {
919 empty_both_buffers(p, 10);
920 if (write_controller_command(p, KBDC_DIAGNOSE))
926 emptyq(&kbdcp(p)->kbd);
927 while (again-- > 0) {
929 DELAY(KBD_RESETDELAY*1000);
930 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
931 if (c != -1) /* wait again if the controller is not ready */
934 if (verbose || bootverbose)
935 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
936 return (c == KBD_DIAG_DONE);
940 test_kbd_port(KBDC p)
942 int retry = KBD_MAXRETRY;
943 int again = KBD_MAXWAIT;
946 while (retry-- > 0) {
947 empty_both_buffers(p, 10);
948 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
954 emptyq(&kbdcp(p)->kbd);
955 while (again-- > 0) {
956 c = read_controller_data(p);
957 if (c != -1) /* try again if the controller is not ready */
960 if (verbose || bootverbose)
961 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
966 test_aux_port(KBDC p)
968 int retry = KBD_MAXRETRY;
969 int again = KBD_MAXWAIT;
972 while (retry-- > 0) {
973 empty_both_buffers(p, 10);
974 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
980 emptyq(&kbdcp(p)->kbd);
981 while (again-- > 0) {
982 c = read_controller_data(p);
983 if (c != -1) /* try again if the controller is not ready */
986 if (verbose || bootverbose)
987 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
992 kbdc_get_device_mask(KBDC p)
994 return kbdcp(p)->command_mask;
998 kbdc_set_device_mask(KBDC p, int mask)
1000 kbdcp(p)->command_mask =
1001 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1005 get_controller_command_byte(KBDC p)
1007 if (kbdcp(p)->command_byte != -1)
1008 return kbdcp(p)->command_byte;
1009 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1011 emptyq(&kbdcp(p)->kbd);
1012 kbdcp(p)->command_byte = read_controller_data(p);
1013 return kbdcp(p)->command_byte;
1017 set_controller_command_byte(KBDC p, int mask, int command)
1019 if (get_controller_command_byte(p) == -1)
1022 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1023 if (command & KBD_DISABLE_KBD_PORT) {
1024 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1027 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1029 if (!write_controller_data(p, command))
1031 kbdcp(p)->command_byte = command;
1034 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",