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 * $DragonFly: src/sys/dev/misc/kbd/atkbdc.c,v 1.11 2007/04/22 10:54:43 y0netan1 Exp $
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
36 #include "use_atkbdc.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
45 #include <machine/clock.h>
47 #include "atkbdcreg.h"
49 #include <bus/isa/isareg.h>
52 #define lwkt_gettoken(x)
53 #define lwkt_reltoken(x)
58 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
63 #define MAX(x, y) ((x) > (y) ? (x) : (y))
66 #define kbdcp(p) ((atkbdc_softc_t *)(p))
67 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
68 #define availq(q) ((q)->head != (q)->tail)
70 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
72 #define emptyq(q) ((q)->tail = (q)->head = 0)
75 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
76 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
77 #define write_data(k, d) \
78 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
79 #define write_command(k, d) \
80 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
85 * We always need at least one copy of the kbdc_softc struct for the
86 * low-level console. As the low-level console accesses the keyboard
87 * controller before kbdc, and all other devices, is probed, we
88 * statically allocate one entry. XXX
90 static atkbdc_softc_t default_kbdc;
91 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
93 static int verbose = KBDIO_DEBUG;
95 /* function prototypes */
97 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
98 bus_space_handle_t h0, bus_space_handle_t h1);
99 static int addq(kbdkqueue *q, int c);
100 static int removeq(kbdkqueue *q);
101 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
102 static int wait_for_data(atkbdc_softc_t *kbdc);
103 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
105 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
109 atkbdc_get_softc(int unit)
113 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
115 sc = atkbdc_softc[unit];
117 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
118 atkbdc_softc[unit] = 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;
151 #if defined(__i386__)
156 resource_int_value("atkbdc", 0, "port", &port0);
157 port1 = IO_KBD + KBD_STATUS_PORT;
159 resource_int_value("atkbdc", 0, "port", &port0);
162 /* XXX: tag should be passed from the caller */
163 #if defined(__i386__)
164 tag = I386_BUS_SPACE_IO;
170 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
171 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
173 h0 = (bus_space_handle_t)port0;
174 h1 = (bus_space_handle_t)port1;
177 #if defined(__i386__)
179 * Check if we really have AT keyboard controller. Poll status
180 * register until we get "all clear" indication. If no such
181 * indication comes, it probably means that there is no AT
182 * keyboard controller present. Give up in such case. Check relies
183 * on the fact that reading from non-existing in/out port returns
184 * 0xff on i386. May or may not be true on other platforms.
186 for (i = 65536; i != 0; --i) {
187 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
195 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
199 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
200 bus_space_handle_t h1)
202 if (sc->ioh0 == 0) { /* XXX */
203 sc->command_byte = -1;
204 sc->command_mask = 0;
206 sc->kbd.head = sc->kbd.tail = 0;
207 sc->aux.head = sc->aux.tail = 0;
209 sc->kbd.call_count = 0;
210 sc->kbd.qcount = sc->kbd.max_qcount = 0;
211 sc->aux.call_count = 0;
212 sc->aux.qcount = sc->aux.max_qcount = 0;
221 /* open a keyboard controller */
223 atkbdc_open(int unit)
229 if ((atkbdc_softc[unit]->port0 != NULL)
230 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
231 return (KBDC)atkbdc_softc[unit];
236 * I/O access arbitration in `kbdio'
238 * The `kbdio' module uses a simplistic convention to arbitrate
239 * I/O access to the controller/keyboard/mouse. The convention requires
240 * close cooperation of the calling device driver.
242 * The device drivers which utilize the `kbdio' module are assumed to
243 * have the following set of routines.
244 * a. An interrupt handler (the bottom half of the driver).
245 * b. Timeout routines which may briefly poll the keyboard controller.
246 * c. Routines outside interrupt context (the top half of the driver).
247 * They should follow the rules below:
248 * 1. The interrupt handler may assume that it always has full access
249 * to the controller/keyboard/mouse.
250 * 2. The other routines must issue `spltty()' if they wish to
251 * prevent the interrupt handler from accessing
252 * the controller/keyboard/mouse.
253 * 3. The timeout routines and the top half routines of the device driver
254 * arbitrate I/O access by observing the lock flag in `kbdio'.
255 * The flag is manipulated via `kbdc_lock()'; when one wants to
256 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
257 * the call returns with TRUE. Otherwise the caller must back off.
258 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
259 * is finished. This mechanism does not prevent the interrupt
260 * handler from being invoked at any time and carrying out I/O.
261 * Therefore, `spltty()' must be strategically placed in the device
262 * driver code. Also note that the timeout routine may interrupt
263 * `kbdc_lock()' called by the top half of the driver, but this
264 * interruption is OK so long as the timeout routine observes
266 * 4. The interrupt and timeout routines should not extend I/O operation
267 * across more than one interrupt or timeout; they must complete any
268 * necessary I/O operation within one invocation of the routine.
269 * This means that if the timeout routine acquires the lock flag,
270 * it must reset the flag to FALSE before it returns.
273 /* set/reset polling lock */
275 kbdc_lock(KBDC p, int lock)
279 prevlock = kbdcp(p)->lock;
280 kbdcp(p)->lock = lock;
282 return (prevlock != lock);
285 /* check if any data is waiting to be processed */
287 kbdc_data_ready(KBDC p)
289 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
290 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
293 /* queuing functions */
296 addq(kbdkqueue *q, int c)
298 if (nextq(q->tail) != q->head) {
300 q->tail = nextq(q->tail);
304 if (q->qcount > q->max_qcount)
305 q->max_qcount = q->qcount;
313 removeq(kbdkqueue *q)
317 if (q->tail != q->head) {
319 q->head = nextq(q->head);
329 * device I/O routines
332 wait_while_controller_busy(struct atkbdc_softc *kbdc)
334 /* CPU will stay inside the loop for 100msec at most */
338 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
339 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
340 DELAY(KBDD_DELAYTIME);
341 addq(&kbdc->kbd, read_data(kbdc));
342 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
343 DELAY(KBDD_DELAYTIME);
344 addq(&kbdc->aux, read_data(kbdc));
346 DELAY(KBDC_DELAYTIME);
354 * wait for any data; whether it's from the controller,
355 * the keyboard, or the aux device.
358 wait_for_data(struct atkbdc_softc *kbdc)
360 /* CPU will stay inside the loop for 200msec at most */
364 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
365 DELAY(KBDC_DELAYTIME);
369 DELAY(KBDD_DELAYTIME);
373 /* wait for data from the keyboard */
375 wait_for_kbd_data(struct atkbdc_softc *kbdc)
377 /* CPU will stay inside the loop for 200msec at most */
381 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
382 != KBDS_KBD_BUFFER_FULL) {
383 if (f == KBDS_AUX_BUFFER_FULL) {
384 DELAY(KBDD_DELAYTIME);
385 addq(&kbdc->aux, read_data(kbdc));
387 DELAY(KBDC_DELAYTIME);
391 DELAY(KBDD_DELAYTIME);
396 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
397 * queue anything else.
400 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
402 /* CPU will stay inside the loop for 200msec at most */
407 while (retry-- > 0) {
408 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
409 DELAY(KBDD_DELAYTIME);
411 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
412 if ((b == KBD_ACK) || (b == KBD_RESEND)
413 || (b == KBD_RESET_FAIL))
416 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
420 DELAY(KBDC_DELAYTIME);
425 /* wait for data from the aux device */
427 wait_for_aux_data(struct atkbdc_softc *kbdc)
429 /* CPU will stay inside the loop for 200msec at most */
433 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
434 != KBDS_AUX_BUFFER_FULL) {
435 if (f == KBDS_KBD_BUFFER_FULL) {
436 DELAY(KBDD_DELAYTIME);
437 addq(&kbdc->kbd, read_data(kbdc));
439 DELAY(KBDC_DELAYTIME);
443 DELAY(KBDD_DELAYTIME);
448 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
449 * queue anything else.
452 wait_for_aux_ack(struct atkbdc_softc *kbdc)
454 /* CPU will stay inside the loop for 200msec at most */
459 while (retry-- > 0) {
460 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
461 DELAY(KBDD_DELAYTIME);
463 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
464 if ((b == PSM_ACK) || (b == PSM_RESEND)
465 || (b == PSM_RESET_FAIL))
468 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
472 DELAY(KBDC_DELAYTIME);
477 /* write a one byte command to the controller */
479 write_controller_command(KBDC p, int c)
481 if (!wait_while_controller_busy(kbdcp(p)))
483 write_command(kbdcp(p), c);
487 /* write a one byte data to the controller */
489 write_controller_data(KBDC p, int c)
491 if (!wait_while_controller_busy(kbdcp(p)))
493 write_data(kbdcp(p), c);
497 /* write a one byte keyboard command */
499 write_kbd_command(KBDC p, int c)
501 if (!wait_while_controller_busy(kbdcp(p)))
503 write_data(kbdcp(p), c);
507 /* write a one byte auxiliary device command */
509 write_aux_command(KBDC p, int c)
511 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
513 return write_controller_data(p, c);
516 /* send a command to the keyboard and wait for ACK */
518 send_kbd_command(KBDC p, int c)
520 int retry = KBD_MAXRETRY;
523 while (retry-- > 0) {
524 if (!write_kbd_command(p, c))
526 res = wait_for_kbd_ack(kbdcp(p));
533 /* send a command to the auxiliary device and wait for ACK */
535 send_aux_command(KBDC p, int c)
537 int retry = KBD_MAXRETRY;
540 while (retry-- > 0) {
541 if (!write_aux_command(p, c))
545 * The aux device may have already sent one or two bytes of
546 * status data, when a command is received. It will immediately
547 * stop data transmission, thus, leaving an incomplete data
548 * packet in our buffer. We have to discard any unprocessed
549 * data in order to remove such packets. Well, we may remove
550 * unprocessed, but necessary data byte as well...
552 emptyq(&kbdcp(p)->aux);
553 res = wait_for_aux_ack(kbdcp(p));
560 /* send a command and a data to the keyboard, wait for ACKs */
562 send_kbd_command_and_data(KBDC p, int c, int d)
567 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
568 if (!write_kbd_command(p, c))
570 res = wait_for_kbd_ack(kbdcp(p));
573 else if (res != KBD_RESEND)
579 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
580 if (!write_kbd_command(p, d))
582 res = wait_for_kbd_ack(kbdcp(p));
583 if (res != KBD_RESEND)
589 /* send a command and a data to the auxiliary device, wait for ACKs */
591 send_aux_command_and_data(KBDC p, int c, int d)
596 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
597 if (!write_aux_command(p, c))
599 emptyq(&kbdcp(p)->aux);
600 res = wait_for_aux_ack(kbdcp(p));
603 else if (res != PSM_RESEND)
609 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
610 if (!write_aux_command(p, d))
612 res = wait_for_aux_ack(kbdcp(p));
613 if (res != PSM_RESEND)
620 * read one byte from any source; whether from the controller,
621 * the keyboard, or the aux device
624 read_controller_data(KBDC p)
626 if (availq(&kbdcp(p)->kbd))
627 return removeq(&kbdcp(p)->kbd);
628 if (availq(&kbdcp(p)->aux))
629 return removeq(&kbdcp(p)->aux);
630 if (!wait_for_data(kbdcp(p)))
631 return -1; /* timeout */
632 return read_data(kbdcp(p));
639 /* read one byte from the keyboard */
641 read_kbd_data(KBDC p)
646 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
647 "aux q: %d calls, max %d chars\n",
648 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
649 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
653 if (availq(&kbdcp(p)->kbd))
654 return removeq(&kbdcp(p)->kbd);
655 if (!wait_for_kbd_data(kbdcp(p)))
656 return -1; /* timeout */
657 return read_data(kbdcp(p));
660 /* read one byte from the keyboard, but return immediately if
664 read_kbd_data_no_wait(KBDC p)
671 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
672 "aux q: %d calls, max %d chars\n",
673 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
674 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
678 if (availq(&kbdcp(p)->kbd))
679 return removeq(&kbdcp(p)->kbd);
680 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
681 if (f == KBDS_AUX_BUFFER_FULL) {
682 DELAY(KBDD_DELAYTIME);
683 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
684 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
686 if (f == KBDS_KBD_BUFFER_FULL) {
687 DELAY(KBDD_DELAYTIME);
688 return read_data(kbdcp(p));
690 return -1; /* no data */
693 /* read one byte from the aux device */
695 read_aux_data(KBDC p)
697 if (availq(&kbdcp(p)->aux))
698 return removeq(&kbdcp(p)->aux);
699 if (!wait_for_aux_data(kbdcp(p)))
700 return -1; /* timeout */
701 return read_data(kbdcp(p));
704 /* read one byte from the aux device, but return immediately if
708 read_aux_data_no_wait(KBDC p)
712 if (availq(&kbdcp(p)->aux))
713 return removeq(&kbdcp(p)->aux);
714 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
715 if (f == KBDS_KBD_BUFFER_FULL) {
716 DELAY(KBDD_DELAYTIME);
717 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
718 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
720 if (f == KBDS_AUX_BUFFER_FULL) {
721 DELAY(KBDD_DELAYTIME);
722 return read_data(kbdcp(p));
724 return -1; /* no data */
727 /* discard data from the keyboard */
729 empty_kbd_buffer(KBDC p, int wait)
740 for (t = wait; t > 0; ) {
741 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
742 DELAY(KBDD_DELAYTIME);
743 b = read_data(kbdcp(p));
744 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
745 addq(&kbdcp(p)->aux, b);
759 if ((c1 > 0) || (c2 > 0))
760 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
763 emptyq(&kbdcp(p)->kbd);
766 /* discard data from the aux device */
768 empty_aux_buffer(KBDC p, int wait)
779 for (t = wait; t > 0; ) {
780 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
781 DELAY(KBDD_DELAYTIME);
782 b = read_data(kbdcp(p));
783 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
784 addq(&kbdcp(p)->kbd, b);
798 if ((c1 > 0) || (c2 > 0))
799 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
802 emptyq(&kbdcp(p)->aux);
805 /* discard any data from the keyboard or the aux device */
807 empty_both_buffers(KBDC p, int wait)
817 for (t = wait; t > 0; ) {
818 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
819 DELAY(KBDD_DELAYTIME);
820 (void)read_data(kbdcp(p));
822 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
834 if ((c1 > 0) || (c2 > 0))
835 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
838 emptyq(&kbdcp(p)->kbd);
839 emptyq(&kbdcp(p)->aux);
842 /* keyboard and mouse device control */
844 /* NOTE: enable the keyboard port but disable the keyboard
845 * interrupt before calling "reset_kbd()".
850 int retry = KBD_MAXRETRY;
851 int again = KBD_MAXWAIT;
852 int c = KBD_RESEND; /* keep the compiler happy */
854 while (retry-- > 0) {
855 empty_both_buffers(p, 10);
856 if (!write_kbd_command(p, KBDC_RESET_KBD))
858 emptyq(&kbdcp(p)->kbd);
859 c = read_controller_data(p);
860 if (verbose || bootverbose)
861 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
862 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
868 while (again-- > 0) {
869 /* wait awhile, well, in fact we must wait quite loooooooooooong */
870 DELAY(KBD_RESETDELAY*1000);
871 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
872 if (c != -1) /* wait again if the controller is not ready */
875 if (verbose || bootverbose)
876 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
877 if (c != KBD_RESET_DONE)
882 /* NOTE: enable the aux port but disable the aux interrupt
883 * before calling `reset_aux_dev()'.
886 reset_aux_dev(KBDC p)
888 int retry = KBD_MAXRETRY;
889 int again = KBD_MAXWAIT;
890 int c = PSM_RESEND; /* keep the compiler happy */
892 while (retry-- > 0) {
893 empty_both_buffers(p, 10);
894 if (!write_aux_command(p, PSMC_RESET_DEV))
896 emptyq(&kbdcp(p)->aux);
897 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
898 for (again = KBD_MAXWAIT; again > 0; --again) {
899 DELAY(KBD_RESETDELAY*1000);
900 c = read_aux_data_no_wait(p);
904 if (verbose || bootverbose)
905 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
906 if (c == PSM_ACK) /* aux dev is about to reset... */
912 for (again = KBD_MAXWAIT; again > 0; --again) {
913 /* wait awhile, well, quite looooooooooooong */
914 DELAY(KBD_RESETDELAY*1000);
915 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
916 if (c != -1) /* wait again if the controller is not ready */
919 if (verbose || bootverbose)
920 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
921 if (c != PSM_RESET_DONE) /* reset status */
924 c = read_aux_data(p); /* device ID */
925 if (verbose || bootverbose)
926 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
927 /* NOTE: we could check the device ID now, but leave it later... */
931 /* controller diagnostics and setup */
934 test_controller(KBDC p)
936 int retry = KBD_MAXRETRY;
937 int again = KBD_MAXWAIT;
938 int c = KBD_DIAG_FAIL;
940 while (retry-- > 0) {
941 empty_both_buffers(p, 10);
942 if (write_controller_command(p, KBDC_DIAGNOSE))
948 emptyq(&kbdcp(p)->kbd);
949 while (again-- > 0) {
951 DELAY(KBD_RESETDELAY*1000);
952 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
953 if (c != -1) /* wait again if the controller is not ready */
956 if (verbose || bootverbose)
957 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
958 return (c == KBD_DIAG_DONE);
962 test_kbd_port(KBDC p)
964 int retry = KBD_MAXRETRY;
965 int again = KBD_MAXWAIT;
968 while (retry-- > 0) {
969 empty_both_buffers(p, 10);
970 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
976 emptyq(&kbdcp(p)->kbd);
977 while (again-- > 0) {
978 c = read_controller_data(p);
979 if (c != -1) /* try again if the controller is not ready */
982 if (verbose || bootverbose)
983 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
988 test_aux_port(KBDC p)
990 int retry = KBD_MAXRETRY;
991 int again = KBD_MAXWAIT;
994 while (retry-- > 0) {
995 empty_both_buffers(p, 10);
996 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1002 emptyq(&kbdcp(p)->kbd);
1003 while (again-- > 0) {
1004 c = read_controller_data(p);
1005 if (c != -1) /* try again if the controller is not ready */
1008 if (verbose || bootverbose)
1009 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1014 kbdc_get_device_mask(KBDC p)
1016 return kbdcp(p)->command_mask;
1020 kbdc_set_device_mask(KBDC p, int mask)
1022 kbdcp(p)->command_mask =
1023 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1027 get_controller_command_byte(KBDC p)
1029 if (kbdcp(p)->command_byte != -1)
1030 return kbdcp(p)->command_byte;
1031 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1033 emptyq(&kbdcp(p)->kbd);
1034 kbdcp(p)->command_byte = read_controller_data(p);
1035 return kbdcp(p)->command_byte;
1039 set_controller_command_byte(KBDC p, int mask, int command)
1041 if (get_controller_command_byte(p) == -1)
1044 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1045 if (command & KBD_DISABLE_KBD_PORT) {
1046 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1049 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1051 if (!write_controller_data(p, command))
1053 kbdcp(p)->command_byte = command;
1056 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",