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 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
48 #include <dev/kbd/atkbdcreg.h>
50 #include <isa/isareg.h>
54 #define MAXKBDC 1 /* XXX */
59 #define MAX(x, y) ((x) > (y) ? (x) : (y))
62 #define kbdcp(p) ((atkbdc_softc_t *)(p))
63 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
64 #define availq(q) ((q)->head != (q)->tail)
66 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
68 #define emptyq(q) ((q)->tail = (q)->head = 0)
71 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
72 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
73 #define write_data(k, d) \
74 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
75 #define write_command(k, d) \
76 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
81 * We always need at least one copy of the kbdc_softc struct for the
82 * low-level console. As the low-level console accesses the keyboard
83 * controller before kbdc, and all other devices, is probed, we
84 * statically allocate one entry. XXX
86 static atkbdc_softc_t default_kbdc;
87 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
89 static int verbose = KBDIO_DEBUG;
91 /* function prototypes */
93 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
94 bus_space_handle_t h0, bus_space_handle_t h1);
95 static int addq(kqueue *q, int c);
96 static int removeq(kqueue *q);
97 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
98 static int wait_for_data(atkbdc_softc_t *kbdc);
99 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
100 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
101 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
102 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
105 *atkbdc_get_softc(int unit)
109 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
111 sc = atkbdc_softc[unit];
113 sc = atkbdc_softc[unit]
114 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
122 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
124 if (rman_get_start(port0) <= 0)
126 if (rman_get_start(port1) <= 0)
132 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
133 struct resource *port1)
135 return atkbdc_setup(sc, rman_get_bustag(port0),
136 rman_get_bushandle(port0),
137 rman_get_bushandle(port1));
140 /* the backdoor to the keyboard controller! XXX */
142 atkbdc_configure(void)
145 bus_space_handle_t h0;
146 bus_space_handle_t h1;
151 resource_int_value("atkbdc", 0, "port", &port0);
152 port1 = IO_KBD + KBD_STATUS_PORT;
154 resource_int_value("atkbdc", 0, "port", &port0);
157 /* XXX: tag should be passed from the caller */
158 #if defined(__i386__)
159 tag = I386_BUS_SPACE_IO;
160 #elif defined(__amd64__)
161 tag = AMD64_BUS_SPACE_IO;
162 #elif defined(__alpha__)
163 tag = busspace_isa_io;
164 #elif defined(__ia64__)
165 tag = IA64_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;
177 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
181 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
182 bus_space_handle_t h1)
184 if (sc->ioh0 == 0) { /* XXX */
185 sc->command_byte = -1;
186 sc->command_mask = 0;
188 sc->kbd.head = sc->kbd.tail = 0;
189 sc->aux.head = sc->aux.tail = 0;
191 sc->kbd.call_count = 0;
192 sc->kbd.qcount = sc->kbd.max_qcount = 0;
193 sc->aux.call_count = 0;
194 sc->aux.qcount = sc->aux.max_qcount = 0;
203 /* open a keyboard controller */
205 atkbdc_open(int unit)
211 if ((atkbdc_softc[unit]->port0 != NULL)
212 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
213 return (KBDC)atkbdc_softc[unit];
218 * I/O access arbitration in `kbdio'
220 * The `kbdio' module uses a simplistic convention to arbitrate
221 * I/O access to the controller/keyboard/mouse. The convention requires
222 * close cooperation of the calling device driver.
224 * The device drivers which utilize the `kbdio' module are assumed to
225 * have the following set of routines.
226 * a. An interrupt handler (the bottom half of the driver).
227 * b. Timeout routines which may briefly poll the keyboard controller.
228 * c. Routines outside interrupt context (the top half of the driver).
229 * They should follow the rules below:
230 * 1. The interrupt handler may assume that it always has full access
231 * to the controller/keyboard/mouse.
232 * 2. The other routines must issue `spltty()' if they wish to
233 * prevent the interrupt handler from accessing
234 * the controller/keyboard/mouse.
235 * 3. The timeout routines and the top half routines of the device driver
236 * arbitrate I/O access by observing the lock flag in `kbdio'.
237 * The flag is manipulated via `kbdc_lock()'; when one wants to
238 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
239 * the call returns with TRUE. Otherwise the caller must back off.
240 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
241 * is finished. This mechanism does not prevent the interrupt
242 * handler from being invoked at any time and carrying out I/O.
243 * Therefore, `spltty()' must be strategically placed in the device
244 * driver code. Also note that the timeout routine may interrupt
245 * `kbdc_lock()' called by the top half of the driver, but this
246 * interruption is OK so long as the timeout routine observes
248 * 4. The interrupt and timeout routines should not extend I/O operation
249 * across more than one interrupt or timeout; they must complete any
250 * necessary I/O operation within one invocation of the routine.
251 * This means that if the timeout routine acquires the lock flag,
252 * it must reset the flag to FALSE before it returns.
255 /* set/reset polling lock */
257 kbdc_lock(KBDC p, int lock)
261 prevlock = kbdcp(p)->lock;
262 kbdcp(p)->lock = lock;
264 return (prevlock != lock);
267 /* check if any data is waiting to be processed */
269 kbdc_data_ready(KBDC p)
271 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
272 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
275 /* queuing functions */
278 addq(kqueue *q, int c)
280 if (nextq(q->tail) != q->head) {
282 q->tail = nextq(q->tail);
286 if (q->qcount > q->max_qcount)
287 q->max_qcount = q->qcount;
299 if (q->tail != q->head) {
301 q->head = nextq(q->head);
311 * device I/O routines
314 wait_while_controller_busy(struct atkbdc_softc *kbdc)
316 /* CPU will stay inside the loop for 100msec at most */
320 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
321 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
322 DELAY(KBDD_DELAYTIME);
323 addq(&kbdc->kbd, read_data(kbdc));
324 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
325 DELAY(KBDD_DELAYTIME);
326 addq(&kbdc->aux, read_data(kbdc));
328 DELAY(KBDC_DELAYTIME);
336 * wait for any data; whether it's from the controller,
337 * the keyboard, or the aux device.
340 wait_for_data(struct atkbdc_softc *kbdc)
342 /* CPU will stay inside the loop for 200msec at most */
346 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
347 DELAY(KBDC_DELAYTIME);
351 DELAY(KBDD_DELAYTIME);
355 /* wait for data from the keyboard */
357 wait_for_kbd_data(struct atkbdc_softc *kbdc)
359 /* CPU will stay inside the loop for 200msec at most */
363 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
364 != KBDS_KBD_BUFFER_FULL) {
365 if (f == KBDS_AUX_BUFFER_FULL) {
366 DELAY(KBDD_DELAYTIME);
367 addq(&kbdc->aux, read_data(kbdc));
369 DELAY(KBDC_DELAYTIME);
373 DELAY(KBDD_DELAYTIME);
378 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
379 * queue anything else.
382 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
384 /* CPU will stay inside the loop for 200msec at most */
389 while (retry-- > 0) {
390 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
391 DELAY(KBDD_DELAYTIME);
393 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
394 if ((b == KBD_ACK) || (b == KBD_RESEND)
395 || (b == KBD_RESET_FAIL))
398 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
402 DELAY(KBDC_DELAYTIME);
407 /* wait for data from the aux device */
409 wait_for_aux_data(struct atkbdc_softc *kbdc)
411 /* CPU will stay inside the loop for 200msec at most */
415 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
416 != KBDS_AUX_BUFFER_FULL) {
417 if (f == KBDS_KBD_BUFFER_FULL) {
418 DELAY(KBDD_DELAYTIME);
419 addq(&kbdc->kbd, read_data(kbdc));
421 DELAY(KBDC_DELAYTIME);
425 DELAY(KBDD_DELAYTIME);
430 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
431 * queue anything else.
434 wait_for_aux_ack(struct atkbdc_softc *kbdc)
436 /* CPU will stay inside the loop for 200msec at most */
441 while (retry-- > 0) {
442 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
443 DELAY(KBDD_DELAYTIME);
445 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
446 if ((b == PSM_ACK) || (b == PSM_RESEND)
447 || (b == PSM_RESET_FAIL))
450 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
454 DELAY(KBDC_DELAYTIME);
459 /* write a one byte command to the controller */
461 write_controller_command(KBDC p, int c)
463 if (!wait_while_controller_busy(kbdcp(p)))
465 write_command(kbdcp(p), c);
469 /* write a one byte data to the controller */
471 write_controller_data(KBDC p, int c)
473 if (!wait_while_controller_busy(kbdcp(p)))
475 write_data(kbdcp(p), c);
479 /* write a one byte keyboard command */
481 write_kbd_command(KBDC p, int c)
483 if (!wait_while_controller_busy(kbdcp(p)))
485 write_data(kbdcp(p), c);
489 /* write a one byte auxiliary device command */
491 write_aux_command(KBDC p, int c)
493 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
495 return write_controller_data(p, c);
498 /* send a command to the keyboard and wait for ACK */
500 send_kbd_command(KBDC p, int c)
502 int retry = KBD_MAXRETRY;
505 while (retry-- > 0) {
506 if (!write_kbd_command(p, c))
508 res = wait_for_kbd_ack(kbdcp(p));
515 /* send a command to the auxiliary device and wait for ACK */
517 send_aux_command(KBDC p, int c)
519 int retry = KBD_MAXRETRY;
522 while (retry-- > 0) {
523 if (!write_aux_command(p, c))
527 * The aux device may have already sent one or two bytes of
528 * status data, when a command is received. It will immediately
529 * stop data transmission, thus, leaving an incomplete data
530 * packet in our buffer. We have to discard any unprocessed
531 * data in order to remove such packets. Well, we may remove
532 * unprocessed, but necessary data byte as well...
534 emptyq(&kbdcp(p)->aux);
535 res = wait_for_aux_ack(kbdcp(p));
542 /* send a command and a data to the keyboard, wait for ACKs */
544 send_kbd_command_and_data(KBDC p, int c, int d)
549 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
550 if (!write_kbd_command(p, c))
552 res = wait_for_kbd_ack(kbdcp(p));
555 else if (res != KBD_RESEND)
561 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
562 if (!write_kbd_command(p, d))
564 res = wait_for_kbd_ack(kbdcp(p));
565 if (res != KBD_RESEND)
571 /* send a command and a data to the auxiliary device, wait for ACKs */
573 send_aux_command_and_data(KBDC p, int c, int d)
578 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
579 if (!write_aux_command(p, c))
581 emptyq(&kbdcp(p)->aux);
582 res = wait_for_aux_ack(kbdcp(p));
585 else if (res != PSM_RESEND)
591 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
592 if (!write_aux_command(p, d))
594 res = wait_for_aux_ack(kbdcp(p));
595 if (res != PSM_RESEND)
602 * read one byte from any source; whether from the controller,
603 * the keyboard, or the aux device
606 read_controller_data(KBDC p)
608 if (availq(&kbdcp(p)->kbd))
609 return removeq(&kbdcp(p)->kbd);
610 if (availq(&kbdcp(p)->aux))
611 return removeq(&kbdcp(p)->aux);
612 if (!wait_for_data(kbdcp(p)))
613 return -1; /* timeout */
614 return read_data(kbdcp(p));
621 /* read one byte from the keyboard */
623 read_kbd_data(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 if (!wait_for_kbd_data(kbdcp(p)))
638 return -1; /* timeout */
639 return read_data(kbdcp(p));
642 /* read one byte from the keyboard, but return immediately if
646 read_kbd_data_no_wait(KBDC p)
653 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
654 "aux q: %d calls, max %d chars\n",
655 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
656 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
660 if (availq(&kbdcp(p)->kbd))
661 return removeq(&kbdcp(p)->kbd);
662 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
663 if (f == KBDS_AUX_BUFFER_FULL) {
664 DELAY(KBDD_DELAYTIME);
665 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
666 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
668 if (f == KBDS_KBD_BUFFER_FULL) {
669 DELAY(KBDD_DELAYTIME);
670 return read_data(kbdcp(p));
672 return -1; /* no data */
675 /* read one byte from the aux device */
677 read_aux_data(KBDC p)
679 if (availq(&kbdcp(p)->aux))
680 return removeq(&kbdcp(p)->aux);
681 if (!wait_for_aux_data(kbdcp(p)))
682 return -1; /* timeout */
683 return read_data(kbdcp(p));
686 /* read one byte from the aux device, but return immediately if
690 read_aux_data_no_wait(KBDC p)
694 if (availq(&kbdcp(p)->aux))
695 return removeq(&kbdcp(p)->aux);
696 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
697 if (f == KBDS_KBD_BUFFER_FULL) {
698 DELAY(KBDD_DELAYTIME);
699 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
700 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
702 if (f == KBDS_AUX_BUFFER_FULL) {
703 DELAY(KBDD_DELAYTIME);
704 return read_data(kbdcp(p));
706 return -1; /* no data */
709 /* discard data from the keyboard */
711 empty_kbd_buffer(KBDC p, int wait)
722 for (t = wait; t > 0; ) {
723 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
724 DELAY(KBDD_DELAYTIME);
725 b = read_data(kbdcp(p));
726 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
727 addq(&kbdcp(p)->aux, b);
741 if ((c1 > 0) || (c2 > 0))
742 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
745 emptyq(&kbdcp(p)->kbd);
748 /* discard data from the aux device */
750 empty_aux_buffer(KBDC p, int wait)
761 for (t = wait; t > 0; ) {
762 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
763 DELAY(KBDD_DELAYTIME);
764 b = read_data(kbdcp(p));
765 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
766 addq(&kbdcp(p)->kbd, b);
780 if ((c1 > 0) || (c2 > 0))
781 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
784 emptyq(&kbdcp(p)->aux);
787 /* discard any data from the keyboard or the aux device */
789 empty_both_buffers(KBDC p, int wait)
799 for (t = wait; t > 0; ) {
800 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
801 DELAY(KBDD_DELAYTIME);
802 (void)read_data(kbdcp(p));
804 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
816 if ((c1 > 0) || (c2 > 0))
817 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
820 emptyq(&kbdcp(p)->kbd);
821 emptyq(&kbdcp(p)->aux);
824 /* keyboard and mouse device control */
826 /* NOTE: enable the keyboard port but disable the keyboard
827 * interrupt before calling "reset_kbd()".
832 int retry = KBD_MAXRETRY;
833 int again = KBD_MAXWAIT;
834 int c = KBD_RESEND; /* keep the compiler happy */
836 while (retry-- > 0) {
837 empty_both_buffers(p, 10);
838 if (!write_kbd_command(p, KBDC_RESET_KBD))
840 emptyq(&kbdcp(p)->kbd);
841 c = read_controller_data(p);
842 if (verbose || bootverbose)
843 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
844 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
850 while (again-- > 0) {
851 /* wait awhile, well, in fact we must wait quite loooooooooooong */
852 DELAY(KBD_RESETDELAY*1000);
853 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
854 if (c != -1) /* wait again if the controller is not ready */
857 if (verbose || bootverbose)
858 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
859 if (c != KBD_RESET_DONE)
864 /* NOTE: enable the aux port but disable the aux interrupt
865 * before calling `reset_aux_dev()'.
868 reset_aux_dev(KBDC p)
870 int retry = KBD_MAXRETRY;
871 int again = KBD_MAXWAIT;
872 int c = PSM_RESEND; /* keep the compiler happy */
874 while (retry-- > 0) {
875 empty_both_buffers(p, 10);
876 if (!write_aux_command(p, PSMC_RESET_DEV))
878 emptyq(&kbdcp(p)->aux);
879 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
880 for (again = KBD_MAXWAIT; again > 0; --again) {
881 DELAY(KBD_RESETDELAY*1000);
882 c = read_aux_data_no_wait(p);
886 if (verbose || bootverbose)
887 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
888 if (c == PSM_ACK) /* aux dev is about to reset... */
894 for (again = KBD_MAXWAIT; again > 0; --again) {
895 /* wait awhile, well, quite looooooooooooong */
896 DELAY(KBD_RESETDELAY*1000);
897 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
898 if (c != -1) /* wait again if the controller is not ready */
901 if (verbose || bootverbose)
902 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
903 if (c != PSM_RESET_DONE) /* reset status */
906 c = read_aux_data(p); /* device ID */
907 if (verbose || bootverbose)
908 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
909 /* NOTE: we could check the device ID now, but leave it later... */
913 /* controller diagnostics and setup */
916 test_controller(KBDC p)
918 int retry = KBD_MAXRETRY;
919 int again = KBD_MAXWAIT;
920 int c = KBD_DIAG_FAIL;
922 while (retry-- > 0) {
923 empty_both_buffers(p, 10);
924 if (write_controller_command(p, KBDC_DIAGNOSE))
930 emptyq(&kbdcp(p)->kbd);
931 while (again-- > 0) {
933 DELAY(KBD_RESETDELAY*1000);
934 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
935 if (c != -1) /* wait again if the controller is not ready */
938 if (verbose || bootverbose)
939 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
940 return (c == KBD_DIAG_DONE);
944 test_kbd_port(KBDC p)
946 int retry = KBD_MAXRETRY;
947 int again = KBD_MAXWAIT;
950 while (retry-- > 0) {
951 empty_both_buffers(p, 10);
952 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
958 emptyq(&kbdcp(p)->kbd);
959 while (again-- > 0) {
960 c = read_controller_data(p);
961 if (c != -1) /* try again if the controller is not ready */
964 if (verbose || bootverbose)
965 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
970 test_aux_port(KBDC p)
972 int retry = KBD_MAXRETRY;
973 int again = KBD_MAXWAIT;
976 while (retry-- > 0) {
977 empty_both_buffers(p, 10);
978 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
984 emptyq(&kbdcp(p)->kbd);
985 while (again-- > 0) {
986 c = read_controller_data(p);
987 if (c != -1) /* try again if the controller is not ready */
990 if (verbose || bootverbose)
991 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
996 kbdc_get_device_mask(KBDC p)
998 return kbdcp(p)->command_mask;
1002 kbdc_set_device_mask(KBDC p, int mask)
1004 kbdcp(p)->command_mask =
1005 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1009 get_controller_command_byte(KBDC p)
1011 if (kbdcp(p)->command_byte != -1)
1012 return kbdcp(p)->command_byte;
1013 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1015 emptyq(&kbdcp(p)->kbd);
1016 kbdcp(p)->command_byte = read_controller_data(p);
1017 return kbdcp(p)->command_byte;
1021 set_controller_command_byte(KBDC p, int mask, int command)
1023 if (get_controller_command_byte(p) == -1)
1026 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1027 if (command & KBD_DISABLE_KBD_PORT) {
1028 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1031 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1033 if (!write_controller_data(p, command))
1035 kbdcp(p)->command_byte = command;
1038 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",