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_pio.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
49 #include <dev/kbd/atkbdcreg.h>
51 #include <isa/isareg.h>
55 #define MAXKBDC 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 | M_ZERO);
123 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
125 if (rman_get_start(port0) <= 0)
127 if (rman_get_start(port1) <= 0)
133 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
134 struct resource *port1)
136 return atkbdc_setup(sc, rman_get_bustag(port0),
137 rman_get_bushandle(port0),
138 rman_get_bushandle(port1));
141 /* the backdoor to the keyboard controller! XXX */
143 atkbdc_configure(void)
146 bus_space_handle_t h0;
147 bus_space_handle_t h1;
152 resource_int_value("atkbdc", 0, "port", &port0);
153 port1 = IO_KBD + KBD_STATUS_PORT;
155 resource_int_value("atkbdc", 0, "port", &port0);
158 /* XXX: tag should be passed from the caller */
159 #if defined(__i386__)
160 tag = I386_BUS_SPACE_IO;
161 #elif defined(__amd64__)
162 tag = AMD64_BUS_SPACE_IO;
163 #elif defined(__alpha__)
164 tag = busspace_isa_io;
165 #elif defined(__ia64__)
166 tag = IA64_BUS_SPACE_IO;
172 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
173 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
175 h0 = (bus_space_handle_t)port0;
176 h1 = (bus_space_handle_t)port1;
178 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
182 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
183 bus_space_handle_t h1)
185 if (sc->ioh0 == 0) { /* XXX */
186 sc->command_byte = -1;
187 sc->command_mask = 0;
189 sc->kbd.head = sc->kbd.tail = 0;
190 sc->aux.head = sc->aux.tail = 0;
192 sc->kbd.call_count = 0;
193 sc->kbd.qcount = sc->kbd.max_qcount = 0;
194 sc->aux.call_count = 0;
195 sc->aux.qcount = sc->aux.max_qcount = 0;
204 /* open a keyboard controller */
206 atkbdc_open(int unit)
212 if ((atkbdc_softc[unit]->port0 != NULL)
213 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
214 return (KBDC)atkbdc_softc[unit];
219 * I/O access arbitration in `kbdio'
221 * The `kbdio' module uses a simplistic convention to arbitrate
222 * I/O access to the controller/keyboard/mouse. The convention requires
223 * close cooperation of the calling device driver.
225 * The device drivers which utilize the `kbdio' module are assumed to
226 * have the following set of routines.
227 * a. An interrupt handler (the bottom half of the driver).
228 * b. Timeout routines which may briefly poll the keyboard controller.
229 * c. Routines outside interrupt context (the top half of the driver).
230 * They should follow the rules below:
231 * 1. The interrupt handler may assume that it always has full access
232 * to the controller/keyboard/mouse.
233 * 2. The other routines must issue `spltty()' if they wish to
234 * prevent the interrupt handler from accessing
235 * the controller/keyboard/mouse.
236 * 3. The timeout routines and the top half routines of the device driver
237 * arbitrate I/O access by observing the lock flag in `kbdio'.
238 * The flag is manipulated via `kbdc_lock()'; when one wants to
239 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
240 * the call returns with TRUE. Otherwise the caller must back off.
241 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
242 * is finished. This mechanism does not prevent the interrupt
243 * handler from being invoked at any time and carrying out I/O.
244 * Therefore, `spltty()' must be strategically placed in the device
245 * driver code. Also note that the timeout routine may interrupt
246 * `kbdc_lock()' called by the top half of the driver, but this
247 * interruption is OK so long as the timeout routine observes
249 * 4. The interrupt and timeout routines should not extend I/O operation
250 * across more than one interrupt or timeout; they must complete any
251 * necessary I/O operation within one invocation of the routine.
252 * This means that if the timeout routine acquires the lock flag,
253 * it must reset the flag to FALSE before it returns.
256 /* set/reset polling lock */
258 kbdc_lock(KBDC p, int lock)
262 prevlock = kbdcp(p)->lock;
263 kbdcp(p)->lock = lock;
265 return (prevlock != lock);
268 /* check if any data is waiting to be processed */
270 kbdc_data_ready(KBDC p)
272 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
273 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
276 /* queuing functions */
279 addq(kqueue *q, int c)
281 if (nextq(q->tail) != q->head) {
283 q->tail = nextq(q->tail);
287 if (q->qcount > q->max_qcount)
288 q->max_qcount = q->qcount;
300 if (q->tail != q->head) {
302 q->head = nextq(q->head);
312 * device I/O routines
315 wait_while_controller_busy(struct atkbdc_softc *kbdc)
317 /* CPU will stay inside the loop for 100msec at most */
321 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
322 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
323 DELAY(KBDD_DELAYTIME);
324 addq(&kbdc->kbd, read_data(kbdc));
325 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
326 DELAY(KBDD_DELAYTIME);
327 addq(&kbdc->aux, read_data(kbdc));
329 DELAY(KBDC_DELAYTIME);
337 * wait for any data; whether it's from the controller,
338 * the keyboard, or the aux device.
341 wait_for_data(struct atkbdc_softc *kbdc)
343 /* CPU will stay inside the loop for 200msec at most */
347 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
348 DELAY(KBDC_DELAYTIME);
352 DELAY(KBDD_DELAYTIME);
356 /* wait for data from the keyboard */
358 wait_for_kbd_data(struct atkbdc_softc *kbdc)
360 /* CPU will stay inside the loop for 200msec at most */
364 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
365 != KBDS_KBD_BUFFER_FULL) {
366 if (f == KBDS_AUX_BUFFER_FULL) {
367 DELAY(KBDD_DELAYTIME);
368 addq(&kbdc->aux, read_data(kbdc));
370 DELAY(KBDC_DELAYTIME);
374 DELAY(KBDD_DELAYTIME);
379 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
380 * queue anything else.
383 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
385 /* CPU will stay inside the loop for 200msec at most */
390 while (retry-- > 0) {
391 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
392 DELAY(KBDD_DELAYTIME);
394 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
395 if ((b == KBD_ACK) || (b == KBD_RESEND)
396 || (b == KBD_RESET_FAIL))
399 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
403 DELAY(KBDC_DELAYTIME);
408 /* wait for data from the aux device */
410 wait_for_aux_data(struct atkbdc_softc *kbdc)
412 /* CPU will stay inside the loop for 200msec at most */
416 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
417 != KBDS_AUX_BUFFER_FULL) {
418 if (f == KBDS_KBD_BUFFER_FULL) {
419 DELAY(KBDD_DELAYTIME);
420 addq(&kbdc->kbd, read_data(kbdc));
422 DELAY(KBDC_DELAYTIME);
426 DELAY(KBDD_DELAYTIME);
431 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
432 * queue anything else.
435 wait_for_aux_ack(struct atkbdc_softc *kbdc)
437 /* CPU will stay inside the loop for 200msec at most */
442 while (retry-- > 0) {
443 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
444 DELAY(KBDD_DELAYTIME);
446 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
447 if ((b == PSM_ACK) || (b == PSM_RESEND)
448 || (b == PSM_RESET_FAIL))
451 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
455 DELAY(KBDC_DELAYTIME);
460 /* write a one byte command to the controller */
462 write_controller_command(KBDC p, int c)
464 if (!wait_while_controller_busy(kbdcp(p)))
466 write_command(kbdcp(p), c);
470 /* write a one byte data to the controller */
472 write_controller_data(KBDC p, int c)
474 if (!wait_while_controller_busy(kbdcp(p)))
476 write_data(kbdcp(p), c);
480 /* write a one byte keyboard command */
482 write_kbd_command(KBDC p, int c)
484 if (!wait_while_controller_busy(kbdcp(p)))
486 write_data(kbdcp(p), c);
490 /* write a one byte auxiliary device command */
492 write_aux_command(KBDC p, int c)
494 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
496 return write_controller_data(p, c);
499 /* send a command to the keyboard and wait for ACK */
501 send_kbd_command(KBDC p, int c)
503 int retry = KBD_MAXRETRY;
506 while (retry-- > 0) {
507 if (!write_kbd_command(p, c))
509 res = wait_for_kbd_ack(kbdcp(p));
516 /* send a command to the auxiliary device and wait for ACK */
518 send_aux_command(KBDC p, int c)
520 int retry = KBD_MAXRETRY;
523 while (retry-- > 0) {
524 if (!write_aux_command(p, c))
528 * The aux device may have already sent one or two bytes of
529 * status data, when a command is received. It will immediately
530 * stop data transmission, thus, leaving an incomplete data
531 * packet in our buffer. We have to discard any unprocessed
532 * data in order to remove such packets. Well, we may remove
533 * unprocessed, but necessary data byte as well...
535 emptyq(&kbdcp(p)->aux);
536 res = wait_for_aux_ack(kbdcp(p));
543 /* send a command and a data to the keyboard, wait for ACKs */
545 send_kbd_command_and_data(KBDC p, int c, int d)
550 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
551 if (!write_kbd_command(p, c))
553 res = wait_for_kbd_ack(kbdcp(p));
556 else if (res != KBD_RESEND)
562 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
563 if (!write_kbd_command(p, d))
565 res = wait_for_kbd_ack(kbdcp(p));
566 if (res != KBD_RESEND)
572 /* send a command and a data to the auxiliary device, wait for ACKs */
574 send_aux_command_and_data(KBDC p, int c, int d)
579 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
580 if (!write_aux_command(p, c))
582 emptyq(&kbdcp(p)->aux);
583 res = wait_for_aux_ack(kbdcp(p));
586 else if (res != PSM_RESEND)
592 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
593 if (!write_aux_command(p, d))
595 res = wait_for_aux_ack(kbdcp(p));
596 if (res != PSM_RESEND)
603 * read one byte from any source; whether from the controller,
604 * the keyboard, or the aux device
607 read_controller_data(KBDC p)
609 if (availq(&kbdcp(p)->kbd))
610 return removeq(&kbdcp(p)->kbd);
611 if (availq(&kbdcp(p)->aux))
612 return removeq(&kbdcp(p)->aux);
613 if (!wait_for_data(kbdcp(p)))
614 return -1; /* timeout */
615 return read_data(kbdcp(p));
622 /* read one byte from the keyboard */
624 read_kbd_data(KBDC p)
629 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
630 "aux q: %d calls, max %d chars\n",
631 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
632 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
636 if (availq(&kbdcp(p)->kbd))
637 return removeq(&kbdcp(p)->kbd);
638 if (!wait_for_kbd_data(kbdcp(p)))
639 return -1; /* timeout */
640 return read_data(kbdcp(p));
643 /* read one byte from the keyboard, but return immediately if
647 read_kbd_data_no_wait(KBDC p)
654 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
655 "aux q: %d calls, max %d chars\n",
656 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
657 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
661 if (availq(&kbdcp(p)->kbd))
662 return removeq(&kbdcp(p)->kbd);
663 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
664 if (f == KBDS_AUX_BUFFER_FULL) {
665 DELAY(KBDD_DELAYTIME);
666 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
667 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
669 if (f == KBDS_KBD_BUFFER_FULL) {
670 DELAY(KBDD_DELAYTIME);
671 return read_data(kbdcp(p));
673 return -1; /* no data */
676 /* read one byte from the aux device */
678 read_aux_data(KBDC p)
680 if (availq(&kbdcp(p)->aux))
681 return removeq(&kbdcp(p)->aux);
682 if (!wait_for_aux_data(kbdcp(p)))
683 return -1; /* timeout */
684 return read_data(kbdcp(p));
687 /* read one byte from the aux device, but return immediately if
691 read_aux_data_no_wait(KBDC p)
695 if (availq(&kbdcp(p)->aux))
696 return removeq(&kbdcp(p)->aux);
697 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
698 if (f == KBDS_KBD_BUFFER_FULL) {
699 DELAY(KBDD_DELAYTIME);
700 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
701 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
703 if (f == KBDS_AUX_BUFFER_FULL) {
704 DELAY(KBDD_DELAYTIME);
705 return read_data(kbdcp(p));
707 return -1; /* no data */
710 /* discard data from the keyboard */
712 empty_kbd_buffer(KBDC p, int wait)
723 for (t = wait; t > 0; ) {
724 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
725 DELAY(KBDD_DELAYTIME);
726 b = read_data(kbdcp(p));
727 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
728 addq(&kbdcp(p)->aux, b);
742 if ((c1 > 0) || (c2 > 0))
743 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
746 emptyq(&kbdcp(p)->kbd);
749 /* discard data from the aux device */
751 empty_aux_buffer(KBDC p, int wait)
762 for (t = wait; t > 0; ) {
763 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
764 DELAY(KBDD_DELAYTIME);
765 b = read_data(kbdcp(p));
766 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
767 addq(&kbdcp(p)->kbd, b);
781 if ((c1 > 0) || (c2 > 0))
782 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
785 emptyq(&kbdcp(p)->aux);
788 /* discard any data from the keyboard or the aux device */
790 empty_both_buffers(KBDC p, int wait)
800 for (t = wait; t > 0; ) {
801 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
802 DELAY(KBDD_DELAYTIME);
803 (void)read_data(kbdcp(p));
805 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
817 if ((c1 > 0) || (c2 > 0))
818 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
821 emptyq(&kbdcp(p)->kbd);
822 emptyq(&kbdcp(p)->aux);
825 /* keyboard and mouse device control */
827 /* NOTE: enable the keyboard port but disable the keyboard
828 * interrupt before calling "reset_kbd()".
833 int retry = KBD_MAXRETRY;
834 int again = KBD_MAXWAIT;
835 int c = KBD_RESEND; /* keep the compiler happy */
837 while (retry-- > 0) {
838 empty_both_buffers(p, 10);
839 if (!write_kbd_command(p, KBDC_RESET_KBD))
841 emptyq(&kbdcp(p)->kbd);
842 c = read_controller_data(p);
843 if (verbose || bootverbose)
844 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
845 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
851 while (again-- > 0) {
852 /* wait awhile, well, in fact we must wait quite loooooooooooong */
853 DELAY(KBD_RESETDELAY*1000);
854 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
855 if (c != -1) /* wait again if the controller is not ready */
858 if (verbose || bootverbose)
859 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
860 if (c != KBD_RESET_DONE)
865 /* NOTE: enable the aux port but disable the aux interrupt
866 * before calling `reset_aux_dev()'.
869 reset_aux_dev(KBDC p)
871 int retry = KBD_MAXRETRY;
872 int again = KBD_MAXWAIT;
873 int c = PSM_RESEND; /* keep the compiler happy */
875 while (retry-- > 0) {
876 empty_both_buffers(p, 10);
877 if (!write_aux_command(p, PSMC_RESET_DEV))
879 emptyq(&kbdcp(p)->aux);
880 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
881 for (again = KBD_MAXWAIT; again > 0; --again) {
882 DELAY(KBD_RESETDELAY*1000);
883 c = read_aux_data_no_wait(p);
887 if (verbose || bootverbose)
888 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
889 if (c == PSM_ACK) /* aux dev is about to reset... */
895 for (again = KBD_MAXWAIT; again > 0; --again) {
896 /* wait awhile, well, quite looooooooooooong */
897 DELAY(KBD_RESETDELAY*1000);
898 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
899 if (c != -1) /* wait again if the controller is not ready */
902 if (verbose || bootverbose)
903 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
904 if (c != PSM_RESET_DONE) /* reset status */
907 c = read_aux_data(p); /* device ID */
908 if (verbose || bootverbose)
909 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
910 /* NOTE: we could check the device ID now, but leave it later... */
914 /* controller diagnostics and setup */
917 test_controller(KBDC p)
919 int retry = KBD_MAXRETRY;
920 int again = KBD_MAXWAIT;
921 int c = KBD_DIAG_FAIL;
923 while (retry-- > 0) {
924 empty_both_buffers(p, 10);
925 if (write_controller_command(p, KBDC_DIAGNOSE))
931 emptyq(&kbdcp(p)->kbd);
932 while (again-- > 0) {
934 DELAY(KBD_RESETDELAY*1000);
935 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
936 if (c != -1) /* wait again if the controller is not ready */
939 if (verbose || bootverbose)
940 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
941 return (c == KBD_DIAG_DONE);
945 test_kbd_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_KBD_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_KBD_PORT status:%04x\n", c);
971 test_aux_port(KBDC p)
973 int retry = KBD_MAXRETRY;
974 int again = KBD_MAXWAIT;
977 while (retry-- > 0) {
978 empty_both_buffers(p, 10);
979 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
985 emptyq(&kbdcp(p)->kbd);
986 while (again-- > 0) {
987 c = read_controller_data(p);
988 if (c != -1) /* try again if the controller is not ready */
991 if (verbose || bootverbose)
992 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
997 kbdc_get_device_mask(KBDC p)
999 return kbdcp(p)->command_mask;
1003 kbdc_set_device_mask(KBDC p, int mask)
1005 kbdcp(p)->command_mask =
1006 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1010 get_controller_command_byte(KBDC p)
1012 if (kbdcp(p)->command_byte != -1)
1013 return kbdcp(p)->command_byte;
1014 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1016 emptyq(&kbdcp(p)->kbd);
1017 kbdcp(p)->command_byte = read_controller_data(p);
1018 return kbdcp(p)->command_byte;
1022 set_controller_command_byte(KBDC p, int mask, int command)
1024 if (get_controller_command_byte(p) == -1)
1027 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1028 if (command & KBD_DISABLE_KBD_PORT) {
1029 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1032 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1034 if (!write_controller_data(p, command))
1036 kbdcp(p)->command_byte = command;
1039 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",