4 * Copyright (c) 1996-1999
5 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
33 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
37 #include "use_atkbdc.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/syslog.h>
46 #include <machine/clock.h>
48 #include "atkbdcreg.h"
50 #include <bus/isa/isareg.h>
53 #define lwkt_gettoken(x)
54 #define lwkt_reltoken(x)
59 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
64 #define MAX(x, y) ((x) > (y) ? (x) : (y))
67 #define kbdcp(p) ((atkbdc_softc_t *)(p))
68 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
69 #define availq(q) ((q)->head != (q)->tail)
71 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
73 #define emptyq(q) ((q)->tail = (q)->head = 0)
76 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
77 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
78 #define write_data(k, d) \
79 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
80 #define write_command(k, d) \
81 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
86 * We always need at least one copy of the kbdc_softc struct for the
87 * low-level console. As the low-level console accesses the keyboard
88 * controller before kbdc, and all other devices, is probed, we
89 * statically allocate one entry. XXX
91 static atkbdc_softc_t default_kbdc;
92 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
94 static int verbose = KBDIO_DEBUG;
96 /* function prototypes */
98 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
99 bus_space_handle_t h0, bus_space_handle_t h1);
100 static int addq(kbdkqueue *q, int c);
101 static int removeq(kbdkqueue *q);
102 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
103 static int wait_for_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
105 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
107 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
110 atkbdc_get_softc(int unit)
114 if (unit >= NELEM(atkbdc_softc))
116 sc = atkbdc_softc[unit];
118 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
119 atkbdc_softc[unit] = sc;
125 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
127 if (rman_get_start(port0) <= 0)
129 if (rman_get_start(port1) <= 0)
135 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
136 struct resource *port1)
138 return atkbdc_setup(sc, rman_get_bustag(port0),
139 rman_get_bushandle(port0),
140 rman_get_bushandle(port1));
143 /* the backdoor to the keyboard controller! XXX */
145 atkbdc_configure(void)
148 bus_space_handle_t h0;
149 bus_space_handle_t h1;
152 #if defined(__i386__) || defined(__x86_64__)
157 resource_int_value("atkbdc", 0, "port", &port0);
158 port1 = IO_KBD + KBD_STATUS_PORT;
160 resource_int_value("atkbdc", 0, "port", &port0);
163 /* XXX: tag should be passed from the caller */
164 #if defined(__i386__)
165 tag = I386_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;
178 #if defined(__i386__) || defined(__x86_64__)
180 * Check if we really have AT keyboard controller. Poll status
181 * register until we get "all clear" indication. If no such
182 * indication comes, it probably means that there is no AT
183 * keyboard controller present. Give up in such case. Check relies
184 * on the fact that reading from non-existing in/out port returns
185 * 0xff on i386. May or may not be true on other platforms.
187 for (i = 65536; i != 0; --i) {
188 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
196 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
200 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
201 bus_space_handle_t h1)
203 if (sc->ioh0 == 0) { /* XXX */
204 sc->command_byte = -1;
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 */
335 TOTALDELAY retry = { .us = 70000, .last_clock =0 }; /* 70ms */
339 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
340 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
341 DELAY(KBDD_DELAYTIME);
344 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
345 DELAY(KBDD_DELAYTIME);
349 DELAY(KBDC_DELAYTIME);
350 if (CHECKTIMEOUT(&retry))
357 * wait for any data; whether it's from the controller,
358 * the keyboard, or the aux device.
361 wait_for_data(struct atkbdc_softc *kbdc)
363 /* CPU will stay inside the loop for 200msec at most */
364 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
367 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
368 DELAY(KBDC_DELAYTIME);
369 if (CHECKTIMEOUT(&retry))
372 DELAY(KBDD_DELAYTIME);
376 /* wait for data from the keyboard */
378 wait_for_kbd_data(struct atkbdc_softc *kbdc)
380 /* CPU will stay inside the loop for 200msec at most */
381 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
385 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
386 != KBDS_KBD_BUFFER_FULL) {
387 if (f == KBDS_AUX_BUFFER_FULL) {
388 DELAY(KBDD_DELAYTIME);
392 DELAY(KBDC_DELAYTIME);
393 if (CHECKTIMEOUT(&retry))
396 DELAY(KBDD_DELAYTIME);
401 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
402 * queue anything else.
405 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
407 /* CPU will stay inside the loop for 200msec at most */
408 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
412 while (CHECKTIMEOUT(&retry) == 0) {
413 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
414 DELAY(KBDD_DELAYTIME);
416 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
417 if ((b == KBD_ACK) || (b == KBD_RESEND)
418 || (b == KBD_RESET_FAIL))
421 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
425 DELAY(KBDC_DELAYTIME);
430 /* wait for data from the aux device */
432 wait_for_aux_data(struct atkbdc_softc *kbdc)
434 /* CPU will stay inside the loop for 200msec at most */
435 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
439 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
440 != KBDS_AUX_BUFFER_FULL) {
441 if (f == KBDS_KBD_BUFFER_FULL) {
442 DELAY(KBDD_DELAYTIME);
446 DELAY(KBDC_DELAYTIME);
447 if (CHECKTIMEOUT(&retry))
450 DELAY(KBDD_DELAYTIME);
455 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
456 * queue anything else.
459 wait_for_aux_ack(struct atkbdc_softc *kbdc)
461 /* CPU will stay inside the loop for 200msec at most */
462 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
466 while (CHECKTIMEOUT(&retry) == 0) {
467 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
468 DELAY(KBDD_DELAYTIME);
470 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
471 if ((b == PSM_ACK) || (b == PSM_RESEND)
472 || (b == PSM_RESET_FAIL))
475 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
479 DELAY(KBDC_DELAYTIME);
485 * Returns read-back data or -1 on failure
488 write_controller_w1r1(KBDC p, int c, int d)
490 if (!write_controller_command(p, c))
492 if (!write_controller_data(p, d))
494 return (read_controller_data(p));
497 /* write a one byte command to the controller */
499 write_controller_command(KBDC p, int c)
501 if (!wait_while_controller_busy(kbdcp(p)))
503 write_command(kbdcp(p), c);
507 /* write a one byte data to the controller */
509 write_controller_data(KBDC p, int c)
511 if (!wait_while_controller_busy(kbdcp(p)))
513 write_data(kbdcp(p), c);
517 /* write a one byte keyboard command */
519 write_kbd_command(KBDC p, int c)
521 if (!wait_while_controller_busy(kbdcp(p)))
523 write_data(kbdcp(p), c);
527 /* write a one byte auxiliary device command */
529 write_aux_command(KBDC p, int c)
531 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
533 return write_controller_data(p, c);
536 /* send a command to the keyboard and wait for ACK */
538 send_kbd_command(KBDC p, int c)
540 int retry = KBD_MAXRETRY;
543 while (retry-- > 0) {
544 if (!write_kbd_command(p, c))
546 res = wait_for_kbd_ack(kbdcp(p));
553 /* send a command to the auxiliary device and wait for ACK */
555 send_aux_command(KBDC p, int c)
557 int retry = KBD_MAXRETRY;
560 while (retry-- > 0) {
561 if (!write_aux_command(p, c))
565 * The aux device may have already sent one or two bytes of
566 * status data, when a command is received. It will immediately
567 * stop data transmission, thus, leaving an incomplete data
568 * packet in our buffer. We have to discard any unprocessed
569 * data in order to remove such packets. Well, we may remove
570 * unprocessed, but necessary data byte as well...
572 emptyq(&kbdcp(p)->aux);
573 res = wait_for_aux_ack(kbdcp(p));
580 /* send a command and a data to the keyboard, wait for ACKs */
582 send_kbd_command_and_data(KBDC p, int c, int d)
587 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
588 if (!write_kbd_command(p, c))
590 res = wait_for_kbd_ack(kbdcp(p));
593 else if (res != KBD_RESEND)
599 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
600 if (!write_kbd_command(p, d))
602 res = wait_for_kbd_ack(kbdcp(p));
603 if (res != KBD_RESEND)
609 /* send a command and a data to the auxiliary device, wait for ACKs */
611 send_aux_command_and_data(KBDC p, int c, int d)
616 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
617 if (!write_aux_command(p, c))
619 emptyq(&kbdcp(p)->aux);
620 res = wait_for_aux_ack(kbdcp(p));
623 else if (res != PSM_RESEND)
629 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
630 if (!write_aux_command(p, d))
632 res = wait_for_aux_ack(kbdcp(p));
633 if (res != PSM_RESEND)
640 * read one byte from any source; whether from the controller,
641 * the keyboard, or the aux device
644 read_controller_data(KBDC p)
646 if (availq(&kbdcp(p)->kbd))
647 return removeq(&kbdcp(p)->kbd);
648 if (availq(&kbdcp(p)->aux))
649 return removeq(&kbdcp(p)->aux);
650 if (!wait_for_data(kbdcp(p)))
651 return -1; /* timeout */
652 return read_data(kbdcp(p));
659 /* read one byte from the keyboard */
661 read_kbd_data(KBDC p)
668 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
669 "aux q: %d calls, max %d chars\n",
670 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
671 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
675 if (availq(&kbdcp(p)->kbd))
676 return removeq(&kbdcp(p)->kbd);
677 if (!wait_for_kbd_data(kbdcp(p)))
678 return -1; /* timeout */
679 b = read_data(kbdcp(p));
683 /* read one byte from the keyboard, but return immediately if
687 read_kbd_data_no_wait(KBDC p)
695 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
696 "aux q: %d calls, max %d chars\n",
697 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
698 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
702 if (availq(&kbdcp(p)->kbd))
703 return removeq(&kbdcp(p)->kbd);
704 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
705 while (f == KBDS_AUX_BUFFER_FULL) {
706 DELAY(KBDD_DELAYTIME);
707 b = read_data(kbdcp(p));
708 addq(&kbdcp(p)->aux, b);
709 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
711 if (f == KBDS_KBD_BUFFER_FULL) {
712 DELAY(KBDD_DELAYTIME);
713 b = read_data(kbdcp(p));
716 return -1; /* no data */
719 /* read one byte from the aux device */
721 read_aux_data(KBDC p)
724 if (availq(&kbdcp(p)->aux))
725 return removeq(&kbdcp(p)->aux);
726 if (!wait_for_aux_data(kbdcp(p)))
727 return -1; /* timeout */
728 b = read_data(kbdcp(p));
732 /* read one byte from the aux device, but return immediately if
736 read_aux_data_no_wait(KBDC p)
741 if (availq(&kbdcp(p)->aux))
742 return removeq(&kbdcp(p)->aux);
743 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
744 while (f == KBDS_KBD_BUFFER_FULL) {
745 DELAY(KBDD_DELAYTIME);
746 b = read_data(kbdcp(p));
747 addq(&kbdcp(p)->kbd, b);
748 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
750 if (f == KBDS_AUX_BUFFER_FULL) {
751 DELAY(KBDD_DELAYTIME);
752 b = read_data(kbdcp(p));
755 return -1; /* no data */
758 /* discard data from the keyboard */
760 empty_kbd_buffer(KBDC p, int wait)
771 for (t = wait; t > 0; ) {
772 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
773 DELAY(KBDD_DELAYTIME);
774 b = read_data(kbdcp(p));
775 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
776 addq(&kbdcp(p)->aux, b);
790 if ((c1 > 0) || (c2 > 0))
791 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
794 emptyq(&kbdcp(p)->kbd);
797 /* discard data from the aux device */
799 empty_aux_buffer(KBDC p, int wait)
810 for (t = wait; t > 0; ) {
811 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
812 DELAY(KBDD_DELAYTIME);
813 b = read_data(kbdcp(p));
814 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
815 addq(&kbdcp(p)->kbd, b);
829 if ((c1 > 0) || (c2 > 0))
830 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
833 emptyq(&kbdcp(p)->aux);
836 /* discard any data from the keyboard or the aux device */
838 empty_both_buffers(KBDC p, int wait)
848 for (t = wait; t > 0; ) {
849 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
850 DELAY(KBDD_DELAYTIME);
851 (void)read_data(kbdcp(p));
853 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
865 if ((c1 > 0) || (c2 > 0))
866 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
869 emptyq(&kbdcp(p)->kbd);
870 emptyq(&kbdcp(p)->aux);
873 /* keyboard and mouse device control */
875 /* NOTE: enable the keyboard port but disable the keyboard
876 * interrupt before calling "reset_kbd()".
881 int retry = KBD_MAXRETRY;
882 int again = KBD_MAXWAIT;
883 int c = KBD_RESEND; /* keep the compiler happy */
885 while (retry-- > 0) {
886 empty_both_buffers(p, 10);
887 if (!write_kbd_command(p, KBDC_RESET_KBD))
889 emptyq(&kbdcp(p)->kbd);
890 c = read_controller_data(p);
891 if (verbose || bootverbose)
892 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
893 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
899 while (again-- > 0) {
900 /* wait awhile, well, in fact we must wait quite loooooooooooong */
901 DELAY(KBD_RESETDELAY*1000);
902 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
903 if (c != -1) /* wait again if the controller is not ready */
906 if (verbose || bootverbose)
907 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
908 if (c != KBD_RESET_DONE)
913 /* NOTE: enable the aux port but disable the aux interrupt
914 * before calling `reset_aux_dev()'.
917 reset_aux_dev(KBDC p)
919 int retry = KBD_MAXRETRY;
920 int again = KBD_MAXWAIT;
921 int c = PSM_RESEND; /* keep the compiler happy */
923 while (retry-- > 0) {
924 empty_both_buffers(p, 10);
925 if (!write_aux_command(p, PSMC_RESET_DEV))
927 emptyq(&kbdcp(p)->aux);
928 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
929 for (again = KBD_MAXWAIT; again > 0; --again) {
930 DELAY(KBD_RESETDELAY*1000);
931 c = read_aux_data_no_wait(p);
935 if (verbose || bootverbose)
936 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
937 if (c == PSM_ACK) /* aux dev is about to reset... */
943 for (again = KBD_MAXWAIT; again > 0; --again) {
944 /* wait awhile, well, quite looooooooooooong */
945 DELAY(KBD_RESETDELAY*1000);
946 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
947 if (c != -1) /* wait again if the controller is not ready */
950 if (verbose || bootverbose)
951 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
952 if (c != PSM_RESET_DONE) /* reset status */
955 c = read_aux_data(p); /* device ID */
956 if (verbose || bootverbose)
957 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
958 /* NOTE: we could check the device ID now, but leave it later... */
962 /* controller diagnostics and setup */
965 test_controller(KBDC p)
967 int retry = KBD_MAXRETRY;
968 int again = KBD_MAXWAIT;
969 int c = KBD_DIAG_FAIL;
971 while (retry-- > 0) {
972 empty_both_buffers(p, 10);
973 if (write_controller_command(p, KBDC_DIAGNOSE))
979 emptyq(&kbdcp(p)->kbd);
980 while (again-- > 0) {
982 DELAY(KBD_RESETDELAY*1000);
983 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
984 if (c != -1) /* wait again if the controller is not ready */
987 if (verbose || bootverbose)
988 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
989 return (c == KBD_DIAG_DONE);
993 test_kbd_port(KBDC p)
995 int retry = KBD_MAXRETRY;
996 int again = KBD_MAXWAIT;
999 while (retry-- > 0) {
1000 empty_both_buffers(p, 10);
1001 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1007 emptyq(&kbdcp(p)->kbd);
1008 while (again-- > 0) {
1009 c = read_controller_data(p);
1010 if (c != -1) /* try again if the controller is not ready */
1013 if (verbose || bootverbose)
1014 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1019 test_aux_port(KBDC p)
1021 int retry = KBD_MAXRETRY;
1022 int again = KBD_MAXWAIT;
1025 while (retry-- > 0) {
1026 empty_both_buffers(p, 10);
1027 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1033 emptyq(&kbdcp(p)->kbd);
1034 while (again-- > 0) {
1035 c = read_controller_data(p);
1036 if (c != -1) /* try again if the controller is not ready */
1039 if (verbose || bootverbose)
1040 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1045 get_controller_command_byte(KBDC p)
1047 if (kbdcp(p)->command_byte != -1)
1048 return kbdcp(p)->command_byte;
1049 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1051 emptyq(&kbdcp(p)->kbd);
1052 kbdcp(p)->command_byte = read_controller_data(p);
1053 return kbdcp(p)->command_byte;
1057 set_controller_command_byte(KBDC p, int mask, int command)
1059 if (get_controller_command_byte(p) == -1)
1062 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1064 if (mask & KBD_DISABLE_KBD_PORT) {
1065 if (command & KBD_DISABLE_KBD_PORT) {
1066 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1071 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1073 if (!write_controller_data(p, command))
1076 if (mask & KBD_DISABLE_KBD_PORT) {
1077 if ((command & KBD_DISABLE_KBD_PORT) == 0) {
1078 if (!write_controller_command(p, KBDC_ENABLE_KBD_PORT))
1083 kbdcp(p)->command_byte = command;
1086 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",